diff --git a/src/glezz.rs b/src/glezz.rs index afce475..0733d42 100644 --- a/src/glezz.rs +++ b/src/glezz.rs @@ -127,8 +127,7 @@ impl Drop for VertexBuffer { } } - -pub struct IndexBuffer { +struct IndexBuffer { id: u32, // Not bytes. Number of indexes. len: usize, @@ -216,7 +215,7 @@ pub struct RenderableModel { uv: Vec , normals: Vec , - indexes: Vec , + indexes: IndexBuffer, meshes: Vec , } @@ -245,13 +244,9 @@ impl RenderableModel { LittleEndian::read_f32_into (normal_bytes, &mut normal_vec [..]); let index_slice = model.get_all_indexes (); + let indexes = IndexBuffer::from_slice (index_slice); - const IDX_SIZE: usize = 4; - - let mut indexes = vec! [0; index_slice.len () / IDX_SIZE]; - LittleEndian::read_u32_into (index_slice, &mut indexes [..]); - - let max_index: usize = (*indexes.iter ().max ().unwrap ()).try_into ().unwrap (); + let max_index: usize = indexes.max.try_into ().unwrap (); assert! (max_index * 3 < pos_vec.len ()); assert! (max_index * 2 < uv_vec.len ()); @@ -269,7 +264,7 @@ impl RenderableModel { uv: uv_vec, normals: normal_vec, - indexes: indexes, + indexes, meshes, } } @@ -279,11 +274,14 @@ impl RenderableModel { let mesh = &self.meshes [mesh_num]; unsafe { + gl::BindBuffer (gl::ARRAY_BUFFER, 0); + self.indexes.bind (); + vertex_attrib_pointer (attrs ["pos"], 3, &self.pos); vertex_attrib_pointer (attrs ["uv"], 2, &self.uv); vertex_attrib_pointer (attrs ["normal"], 3, &self.normals); - gl::DrawElements (gl::TRIANGLES, mesh.num_triangles * 3, gl::UNSIGNED_INT, &self.indexes [mesh.first_triangle * 3] as *const u32 as *const c_void); + gl::DrawElements (gl::TRIANGLES, mesh.num_triangles * 3, gl::UNSIGNED_INT, (mesh.first_triangle * 3 * 4) as *const u8 as *const c_void); } } }