diff options
Diffstat (limited to 'src/mesh.rs')
| -rw-r--r-- | src/mesh.rs | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/src/mesh.rs b/src/mesh.rs index cfbbf91..56d735f 100644 --- a/src/mesh.rs +++ b/src/mesh.rs @@ -5,10 +5,15 @@ use crate::context; pub const ATTRIB_VERTEX: u32 = 0; pub const ATTRIB_NORMAL: u32 = 1; pub const ATTRIB_TEXCOORD: u32 = 2; +pub const ATTRIB_JOINT: u32 = 3; +pub const ATTRIB_WEIGHT: u32 = 4; pub struct Mesh { pub vao: glow::VertexArray, + pub mode: u32, // glow::TRIANGLES, etc. pub index_count: usize, + pub index_type: u32, // glow::BYTE, glow::FLOAT, etc. + pub index_offset: i32, } impl Mesh { @@ -23,8 +28,8 @@ impl Mesh { let vao = ctx.gl.create_vertex_array().expect("failed to initialize vao"); ctx.gl.bind_vertex_array(Some(vao)); - let vertices_vbo = ctx.gl.create_buffer().expect("failed to initialize vbo"); - ctx.gl.bind_buffer(glow::ARRAY_BUFFER, Some(vertices_vbo)); + let buf = ctx.gl.create_buffer().expect("failed to create buffer object"); + ctx.gl.bind_buffer(glow::ARRAY_BUFFER, Some(buf)); ctx.gl.buffer_data_u8_slice( glow::ARRAY_BUFFER, std::slice::from_raw_parts( @@ -36,7 +41,7 @@ impl Mesh { ctx.gl.vertex_attrib_pointer_f32(ATTRIB_VERTEX, 3, glow::FLOAT, false, 0, 0); ctx.gl.enable_vertex_attrib_array(ATTRIB_VERTEX); - let indices_vbo = ctx.gl.create_buffer().expect("failed to initialize vbo"); + let indices_vbo = ctx.gl.create_buffer().expect("failed to create buffer object"); ctx.gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, Some(indices_vbo)); ctx.gl.buffer_data_u8_slice( glow::ELEMENT_ARRAY_BUFFER, @@ -48,7 +53,7 @@ impl Mesh { ); if let Some(normals) = snormals { - let normals_vbo = ctx.gl.create_buffer().expect("failed to initialize vbo"); + let normals_vbo = ctx.gl.create_buffer().expect("failed to create buffer object"); ctx.gl.bind_buffer(glow::ARRAY_BUFFER, Some(normals_vbo)); ctx.gl.buffer_data_u8_slice( glow::ARRAY_BUFFER, @@ -63,7 +68,7 @@ impl Mesh { } if let Some(texcoords) = stexcoords { - let texcoords_vbo = ctx.gl.create_buffer().expect("failed to initialize vbo"); + let texcoords_vbo = ctx.gl.create_buffer().expect("failed to create buffer object"); ctx.gl.bind_buffer(glow::ARRAY_BUFFER, Some(texcoords_vbo)); ctx.gl.buffer_data_u8_slice( glow::ARRAY_BUFFER, @@ -79,12 +84,15 @@ impl Mesh { Self { vao, + mode: glow::TRIANGLES, index_count: indices.len(), + index_type: glow::UNSIGNED_INT, + index_offset: 0, } } } - pub fn new(ctx: &context::Context, mut bytes: &[u8]) -> Self { + pub fn from_obj(ctx: &context::Context, mut bytes: &[u8]) -> Self { let lopts = tobj::LoadOptions { triangulate: true, single_index: true, @@ -110,14 +118,14 @@ impl Mesh { pub fn render(&self, ctx: &context::Context) { unsafe { ctx.gl.bind_vertex_array(Some(self.vao)); - ctx.gl.draw_elements(glow::TRIANGLES, self.index_count as _, glow::UNSIGNED_INT, 0); + ctx.gl.draw_elements(self.mode, self.index_count as _, self.index_type, self.index_offset); } } pub fn render_instanced(&self, ctx: &context::Context, count: u64) { unsafe { ctx.gl.bind_vertex_array(Some(self.vao)); - ctx.gl.draw_elements_instanced(glow::TRIANGLES, self.index_count as _, glow::UNSIGNED_INT, 0, count as _); + ctx.gl.draw_elements_instanced(self.mode, self.index_count as _, self.index_type, self.index_offset, count as _); } } } |
