writing normals to IQM, but they're backwards

main
_ 2022-02-22 00:35:28 +00:00
parent e23bf0af59
commit ce8f114998
1 changed files with 35 additions and 12 deletions

View File

@ -59,9 +59,9 @@ fn main () {
let pos_slice = &buffer [pos_offset..(pos_offset + pos.count () * 4 * 3)]; let pos_slice = &buffer [pos_offset..(pos_offset + pos.count () * 4 * 3)];
let norm = prim.get (&gltf::Semantic::Normals).unwrap (); let norm = prim.get (&gltf::Semantic::Normals).unwrap ();
let pos_view = pos.view ().unwrap (); let norm_view = norm.view ().unwrap ();
let pos_offset = pos.offset () + pos_view.offset (); let norm_offset = norm.offset () + norm_view.offset ();
let pos_slice = &buffer [pos_offset..(pos_offset + pos.count () * 4 * 3)]; let norm_slice = &buffer [norm_offset..(norm_offset + norm.count () * 4 * 3)];
let first_vertex = vertexes.len (); let first_vertex = vertexes.len ();
@ -95,6 +95,13 @@ fn main () {
pos_slice [i * 4 + 3], pos_slice [i * 4 + 3],
]); ]);
let read_norm_coord = |i| f32::from_le_bytes ([
norm_slice [i * 4 + 0],
norm_slice [i * 4 + 1],
norm_slice [i * 4 + 2],
norm_slice [i * 4 + 3],
]);
let read_pos = move |i| { let read_pos = move |i| {
let i = usize::try_from (i).unwrap (); let i = usize::try_from (i).unwrap ();
m.transform_point3 (Vec3::new ( m.transform_point3 (Vec3::new (
@ -104,6 +111,15 @@ fn main () {
)) ))
}; };
let read_norm = move |i| {
let i = usize::try_from (i).unwrap ();
m.transform_vector3 (Vec3::new (
read_norm_coord (i * 3 + 0),
read_norm_coord (i * 3 + 1),
read_norm_coord (i * 3 + 2),
))
};
let verts = [ let verts = [
read_pos (idxs [0]), read_pos (idxs [0]),
read_pos (idxs [2]), read_pos (idxs [2]),
@ -117,8 +133,14 @@ fn main () {
tri_start + 2, tri_start + 2,
]); ]);
for v in verts { for i in [idxs [0], idxs [2], idxs [1]] {
vertexes.push (v.to_array ()); let pos = read_pos (i);
let norm = read_norm (i);
vertexes.push (iqm::Vertex {
position: pos.to_array (),
normal: norm.to_array (),
});
} }
} }
} }
@ -242,14 +264,14 @@ fn main () {
// Vertex position // Vertex position
for t in &vertexes { for v in &vertexes {
for x in t { for x in v.position {
f.write_all (&(f32::to_le_bytes (*x))).unwrap (); f.write_all (&(f32::to_le_bytes (x))).unwrap ();
} }
} }
for t in &vertexes { for v in &vertexes {
for x in [0.0, 0.0, 1.0] { for x in v.normal {
f.write_all (&(f32::to_le_bytes (x))).unwrap (); f.write_all (&(f32::to_le_bytes (x))).unwrap ();
} }
} }
@ -294,7 +316,8 @@ mod iqm {
vertex: [u32; 3], vertex: [u32; 3],
} }
struct Vertex { pub struct Vertex {
position: [f32; 3], pub position: [f32; 3],
pub normal: [f32; 3],
} }
} }