diff --git a/src/main.rs b/src/main.rs index 5e3d8e1..1f3ab4a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,8 +27,8 @@ fn main() { glfw.window_hint(glfw::WindowHint::OpenGlProfile(glfw::OpenGlProfileHint::Core)); glfw.window_hint(glfw::WindowHint::OpenGlForwardCompat(true)); glfw.window_hint(glfw::WindowHint::Resizable(false)); - glfw.window_hint(glfw::WindowHint::TransparentFramebuffer(true)); - glfw.window_hint(glfw::WindowHint::Decorated(false)); + glfw.window_hint(glfw::WindowHint::TransparentFramebuffer(false)); + glfw.window_hint(glfw::WindowHint::Decorated(true)); let (mut window, events) = glfw.create_window(SCR_WIDTH, SCR_HEIGHT, TITLE, glfw::WindowMode::Windowed).unwrap(); diff --git a/src/model.rs b/src/model.rs index 128eaaa..d513a4b 100644 --- a/src/model.rs +++ b/src/model.rs @@ -32,6 +32,7 @@ impl Model { pub fn Draw(&self, shader: &shader) { for mesh in &self.meshes { + unsafe { mesh.Draw(shader); } } } diff --git a/src/model/mesh.rs b/src/model/mesh.rs index c0bc897..92bda72 100644 --- a/src/model/mesh.rs +++ b/src/model/mesh.rs @@ -53,11 +53,11 @@ pub struct Mesh { pub vertices: Vec, pub indices: Vec, pub textures: Vec, - pub VAO: glow::VertexArray, + pub VAO: Option, /* Render data */ - VBO: glow::Buffer, - EBO: glow::Buffer, + VBO: Option, + EBO: Option, } impl Mesh { @@ -66,11 +66,10 @@ impl Mesh { let mut mesh = { unsafe { - let mut mesh = Mesh { + Mesh { vertices, indices, textures, - VAO: gl.create_vertex_array().unwrap(), VBO: gl.create_buffer().unwrap(), EBO: gl.create_buffer().unwrap() - }; - mesh + VAO: None, VBO: None, EBO: None + } } }; // now that we have all the required data, set the vertex buffers and its attribute pointers. @@ -109,65 +108,65 @@ impl Mesh { _ => panic!("unknown texture type") }; // now set the sampler to the correct texture unit - let sampler = String::from(format!("{}{}", name, number)); + let sampler = format!("{}{}", name, number); shader.setInt(&sampler, i as i32); + //shader.gl.uniform_1_i32(shader.gl.get_uniform_location(shader.ID, &sampler).as_ref(), i as i32); // and finally bind the texture shader.gl.bind_texture(glow::TEXTURE_2D, Some(texture.id)); + } // draw mesh - shader.gl.bind_vertex_array(Some(self.VAO)); + shader.gl.bind_vertex_array(self.VAO); shader.gl.draw_elements(glow::TRIANGLES, self.indices.len() as i32, glow::UNSIGNED_INT, 0); shader.gl.bind_vertex_array(None); - // always good practice to set everything back to defaults once configured. shader.gl.active_texture(glow::TEXTURE0); } unsafe fn setupMesh(&mut self, gl: std::rc::Rc) { + + self.VAO = Some(gl.create_vertex_array().unwrap()); + self.VBO = Some(gl.create_buffer().unwrap()); + self.EBO = Some(gl.create_buffer().unwrap()); + + gl.bind_vertex_array(self.VAO); // create buffers/arrays - gl.bind_vertex_array(Some(self.VAO)); - // load data into vertex buffers - gl.bind_buffer(glow::ARRAY_BUFFER, Some(self.VBO)); - // A great thing about structs with repr(C) is that their memory layout is sequential for all its items. - // The effect is that we can simply pass a pointer to the struct and it translates perfectly to a glm::vec3/2 array which - // again translates to 3/2 floats which translates to a byte array. - //let size = (self.vertices.len() * size_of::()) as isize; - //let data = &self.vertices[0] as *const Vertex as *const c_void; - let data: &[u8] = core::slice::from_raw_parts( + gl.bind_buffer(glow::ARRAY_BUFFER, self.VBO); + + println!("vertices.len is: {}", self.vertices.len()); + let data = core::slice::from_raw_parts( self.vertices.as_ptr() as *const u8, - self.vertices.len() * core::mem::size_of::(), + self.vertices.len() * core::mem::size_of::(), ); + gl.named_buffer_data_u8_slice(self.VBO.unwrap(), data, glow::STATIC_DRAW); - - gl.buffer_data_u8_slice(glow::ARRAY_BUFFER, data, glow::STATIC_DRAW); - - gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, Some(self.EBO)); - let data: &[u8] = core::slice::from_raw_parts( + gl.bind_buffer(glow::ELEMENT_ARRAY_BUFFER, self.EBO); + let data = core::slice::from_raw_parts( self.indices.as_ptr() as *const u8, - self.indices.len() * core::mem::size_of::(), + self.indices.len() * core::mem::size_of::(), ); - gl.buffer_data_u8_slice(glow::ELEMENT_ARRAY_BUFFER, data, glow::STATIC_DRAW); + gl.named_buffer_data_u8_slice(self.EBO.unwrap(), data, glow::STATIC_DRAW); // set the vertex attribute pointers let size = size_of::() as i32; // vertex Positions gl.enable_vertex_attrib_array(0); - gl.vertex_attrib_pointer_f32(0, 3, glow::FLOAT, false, size, offset_of!(Vertex => Position).get_byte_offset() as i32); + gl.vertex_attrib_pointer_f32(0, 3, glow::FLOAT, false, size, 0); // vertex normals gl.enable_vertex_attrib_array(1); - gl.vertex_attrib_pointer_f32(1, 3, glow::FLOAT, false, size, offset_of!(Vertex => Normal).get_byte_offset() as i32); + gl.vertex_attrib_pointer_f32(1, 3, glow::FLOAT, false, size, 3*4); // vertex texture coords gl.enable_vertex_attrib_array(2); - gl.vertex_attrib_pointer_f32(2, 2, glow::FLOAT, false, size, offset_of!(Vertex => TexCoords).get_byte_offset() as i32); + gl.vertex_attrib_pointer_f32(2, 2, glow::FLOAT, false, size, 6*4); // vertex tangent gl.enable_vertex_attrib_array(3); - gl.vertex_attrib_pointer_f32(3, 3, glow::FLOAT, false, size, offset_of!(Vertex => Tangent).get_byte_offset() as i32); + gl.vertex_attrib_pointer_f32(3, 3, glow::FLOAT, false, size, 8*4); // vertex bitangent gl.enable_vertex_attrib_array(4); - gl.vertex_attrib_pointer_f32(4, 3, glow::FLOAT, false, size, offset_of!(Vertex => Bitangent).get_byte_offset() as i32); + gl.vertex_attrib_pointer_f32(4, 3, glow::FLOAT, false, size, 11*4); gl.bind_vertex_array(None); }