feat: PC change for gltf load algorithm change
This commit is contained in:
1 parent
508a69d385
commit
a97a14a254
4 files changed
+67
-5
No files matched your search
+1
-2
@@ -572,8 +572,7 @@ impl App {
|
|||||||
enabled_extensions: required_extensions,
|
enabled_extensions: required_extensions,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
)
|
).map_err(|e| {
|
||||||
.map_err(|e| {
|
|
||||||
let err = e.unwrap();
|
let err = e.unwrap();
|
||||||
format!("Failed to create instance: {err}")
|
format!("Failed to create instance: {err}")
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ impl Camera {
|
|||||||
/// (0, 0, 0), and a default FOV of 70 degrees.
|
/// (0, 0, 0), and a default FOV of 70 degrees.
|
||||||
pub fn new(extents: impl Into<Vector2<u32>>) -> Self {
|
pub fn new(extents: impl Into<Vector2<u32>>) -> Self {
|
||||||
let extents = extents.into();
|
let extents = extents.into();
|
||||||
let position = Vector3::new(-7.0, 7.0, -7.0);
|
let position = Vector3::new(10.0, 0.0, 0.0);
|
||||||
let target = Vector3::zeros();
|
let target = Vector3::zeros();
|
||||||
|
|
||||||
// Traveling along this vector moves us toward the target.
|
// Traveling along this vector moves us toward the target.
|
||||||
|
|||||||
@@ -1,9 +1,25 @@
|
|||||||
use crate::loaders::mesh_loader::{MeshFileLoader, MeshInfo};
|
use crate::loaders::mesh_loader::{MeshFileLoader, MeshInfo};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
use nalgebra::Matrix4;
|
||||||
|
|
||||||
pub struct GltfLoader;
|
pub struct GltfLoader;
|
||||||
|
|
||||||
|
impl GltfLoader {
|
||||||
|
/// Traverses and loads all information from a GLTF/GLB node.
|
||||||
|
pub fn traverse_node(
|
||||||
|
node: &gltf::Node,
|
||||||
|
parent_transform: &Matrix4<f32>,
|
||||||
|
buffers: &[Vec<u8>],
|
||||||
|
vertices: &mut Vec<[f32; 3]>,
|
||||||
|
normals: &mut Vec<[f32; 3]>,
|
||||||
|
uvs: &mut Vec<[f32; 3]>,
|
||||||
|
indices: &mut Vec<u32>
|
||||||
|
) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl MeshFileLoader for GltfLoader {
|
impl MeshFileLoader for GltfLoader {
|
||||||
fn load_mesh(&self, path: &Path) -> Result<MeshInfo> {
|
fn load_mesh(&self, path: &Path) -> Result<MeshInfo> {
|
||||||
let (document, buffers, _) = gltf::import(path)?;
|
let (document, buffers, _) = gltf::import(path)?;
|
||||||
@@ -13,6 +29,18 @@ impl MeshFileLoader for GltfLoader {
|
|||||||
let mut uvs = Vec::new();
|
let mut uvs = Vec::new();
|
||||||
let mut indices = Vec::<u32>::new();
|
let mut indices = Vec::<u32>::new();
|
||||||
|
|
||||||
|
let scenes = document.scenes();
|
||||||
|
|
||||||
|
// We need to iterate through scenes and apply the transforms from each scene to their
|
||||||
|
// corresponding vertices.
|
||||||
|
println!("Parsing {} scenes from {:?}", scenes.len(), path);
|
||||||
|
for scene in scenes {
|
||||||
|
for node in scene.nodes() {
|
||||||
|
let transform = node.transform().matrix();
|
||||||
|
println!("Scene has transform: {:?}", transform);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for mesh in document.meshes() {
|
for mesh in document.meshes() {
|
||||||
for primitive in mesh.primitives() {
|
for primitive in mesh.primitives() {
|
||||||
// Read the primitives as vertices and indices,
|
// Read the primitives as vertices and indices,
|
||||||
@@ -23,7 +51,7 @@ impl MeshFileLoader for GltfLoader {
|
|||||||
|
|
||||||
if let Some(positions) = reader.read_positions() {
|
if let Some(positions) = reader.read_positions() {
|
||||||
for position in positions {
|
for position in positions {
|
||||||
vertices.push(position);
|
vertices.push([position[0], position[2], -position[1]]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,7 +63,7 @@ impl MeshFileLoader for GltfLoader {
|
|||||||
|
|
||||||
if let Some(tex_coords) = reader.read_tex_coords(0) {
|
if let Some(tex_coords) = reader.read_tex_coords(0) {
|
||||||
for uv in tex_coords.into_f32() {
|
for uv in tex_coords.into_f32() {
|
||||||
uvs.push([uv[0], uv[1], 0.0]);
|
uvs.push([uv[0], 1.0 - uv[1], 0.0]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -64,3 +64,38 @@ impl MeshLoader {
|
|||||||
.load_mesh(path.as_ref())
|
.load_mesh(path.as_ref())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub mod test_load_meshes {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
fn load_gltf(loader: &MeshLoader) -> Result<MeshInfo> {
|
||||||
|
loader.load_mesh(&Path::new("meshes/laat_gltf/scene.gltf"))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn load_obj(loader: &MeshLoader) -> Result<MeshInfo> {
|
||||||
|
loader.load_mesh(&Path::new("meshes/laat_gunship/scene.obj"))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn compare_obj_gltf() -> Result<()> {
|
||||||
|
// Create the mesh loader
|
||||||
|
let loader = MeshLoader::new();
|
||||||
|
|
||||||
|
let obj = load_obj(&loader)?;
|
||||||
|
let gltf = load_gltf(&loader)?;
|
||||||
|
|
||||||
|
// Compare mesh information between the two MeshInfo structs.
|
||||||
|
obj.vertices.iter()
|
||||||
|
.zip(gltf.vertices.iter())
|
||||||
|
.for_each(|(&a, &b)| assert_eq!(a, b));
|
||||||
|
obj.indices.iter()
|
||||||
|
.zip(gltf.indices.iter())
|
||||||
|
.for_each(|(&a, &b)| assert_eq!(a, b));
|
||||||
|
obj.normals.iter()
|
||||||
|
.zip(gltf.normals.iter())
|
||||||
|
.for_each(|(&a, &b)| assert_eq!(a, b));
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user