From 5fa37d5f06ed62e1b891f7e19315417b9c51b4be Mon Sep 17 00:00:00 2001 From: Melik Houij Date: Thu, 23 Feb 2023 09:28:48 +0100 Subject: [PATCH] Rewritten mesh.h from learnopengl.com --- Cargo.toml | 5 ++- src/main.rs | 47 ++++--------------------- src/mesh.rs | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 41 deletions(-) create mode 100644 src/mesh.rs diff --git a/Cargo.toml b/Cargo.toml index 0c37c9b..9532669 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,9 @@ edition = "2021" [dependencies] gl = "0.14.0" -glfw = "0.51.0" glm = "0.2.3" stb = { version = "0.3.2", default-features = false, features = ["stb_image"] } +field-offset = "0.3.4" + +[dependencies.glfw] +version = "*" diff --git a/src/main.rs b/src/main.rs index fb8cf42..2ba4b4e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,15 @@ use std::convert::TryInto; use glfw; -use glfw::Context; +use glfw::{Action, Context, Key}; use gl; const ScreenWidth: u32 = 480; const ScreenHeight: u32 = 320; const TITLE: &str = "GLFWtest"; - mod shader; +mod mesh; fn main() { @@ -18,10 +18,10 @@ fn main() { // set window hints glfw.window_hint(glfw::WindowHint::ContextVersion(3, 3)); glfw.window_hint(glfw::WindowHint::OpenGlProfile(glfw::OpenGlProfileHint::Core)); - glfw.window_hint(glfw::WindowHint::OpenGlForwardCompat(true)); + glfw.window_hint(glfw::WindowHint::OpenGlForwardCompat(false)); glfw.window_hint(glfw::WindowHint::Resizable(false)); glfw.window_hint(glfw::WindowHint::TransparentFramebuffer(true)); - glfw.window_hint(glfw::WindowHint::Decorated(true)); + glfw.window_hint(glfw::WindowHint::Decorated(false)); //create window @@ -36,38 +36,12 @@ fn main() { unsafe { gl::Viewport(0, 0, screen_width, screen_height); - gl::ClearColor(0.4, 0.4, 0.4, 1.0); + gl::ClearColor(1.0, 1.0, 1.0, 1.0); } // put biggie thingies here - let vertices = [ - -0.5f32, -0.5, 0.0, - 0.5, -0.5, 0.0, - 0.0, 0.5, 0.0, - ]; - - let mut vao = 0; - unsafe { gl::GenVertexArrays(1, &mut vao)}; - - let mut vbo = 0; - unsafe{gl::GenBuffers(1, &mut vbo)}; - - unsafe { - gl::BindVertexArray(vao); - - gl::BindBuffer(gl::ARRAY_BUFFER, vbo); - gl::BufferData(gl::ARRAY_BUFFER, std::mem::size_of_val(&vertices) as isize, vertices.as_ptr().cast(), gl::STATIC_DRAW); - - gl::VertexAttribPointer(0, 3, gl::FLOAT, gl::FALSE, 3 * std::mem::size_of::() as i32, 0 as *const _); - gl::EnableVertexAttribArray(0); - - gl::BindBuffer(gl::ARRAY_BUFFER, 0); - gl::BindVertexArray(0); - - } - - let mut test_Shader = shader::shader::new("basic"); + let test_Shader = shader::shader::new("basic"); // NOTE window loop while !window.should_close() { @@ -76,7 +50,7 @@ fn main() { glfw_handle_event(&mut window, event); } - clear_color(0.1, 0.1, 0.1, 0.0); + clear_color(0.1, 0.1, 0.1, -0.0); unsafe { gl::Clear(gl::COLOR_BUFFER_BIT); @@ -84,13 +58,6 @@ fn main() { test_Shader.Use(); - unsafe { - gl::BindVertexArray(vao); - - gl::DrawArrays(gl::TRIANGLES, 0, 3); - - gl::BindVertexArray(0); - } window.swap_buffers(); } diff --git a/src/mesh.rs b/src/mesh.rs new file mode 100644 index 0000000..73c0f81 --- /dev/null +++ b/src/mesh.rs @@ -0,0 +1,98 @@ + +use field_offset::offset_of; + +pub struct Vertex { + Position: glm::Vec3, + Normal: glm::Vec2, + TexCoords: glm::Vec2, + Tangent: glm::Vec3, + Bitangent: glm::Vec3, + m_BoneIDs: [i32; 4], + m_Weights: [f32; 4], +} + +pub struct Texture { + id: u32, + Type: String, + path: String, +} + +struct Mesh { + pub vertices: Vec, + pub indices: Vec, + pub textures: Vec, + pub VAO: Option, + VBO: Option, + EBO: Option, +} + + + + + +impl Mesh{ + pub fn new(vertices: Vec, indices: Vec, textures: Vec) + { + let mut temp = Mesh { + vertices, + indices, + textures, + VAO: None, + VBO: None, + EBO: None, + + }; + + } + fn setupMesh(mut temp: Mesh) + { + // create buffers/arrays + + unsafe { + + gl::GenVertexArrays(1, temp.VAO.as_mut().unwrap()); + gl::GenBuffers(1, temp.VBO.as_mut().unwrap()); + gl::GenBuffers(1, temp.EBO.as_mut().unwrap()); + + gl::BindVertexArray(temp.VAO.unwrap()); + + + // NOTE, i am not sure if this one will work + gl::BindBuffer(gl::ARRAY_BUFFER, temp.VBO.unwrap()); + gl::BufferData(gl::ARRAY_BUFFER, std::mem::size_of_val(&temp.vertices) as isize, temp.vertices.as_ptr().cast(), gl::STATIC_DRAW); + + // the vertex attribPointers + // vert pox + gl::EnableVertexAttribArray(0); + gl::VertexAttribPointer(0, 3, gl::FLOAT, gl::FALSE, std::mem::size_of::() as i32, 0 as *const _); + // vertex normals + gl::EnableVertexAttribArray(1); + gl::VertexAttribPointer(1, 3, gl::FLOAT, gl::FALSE, std::mem::size_of::() as i32, offset_of!(Vertex => Normal).get_byte_offset() as *const _); + + // vertex texture coords + + gl::EnableVertexAttribArray(2); + gl::VertexAttribPointer(2, 2, gl::FLOAT, gl::FALSE, std::mem::size_of::() as i32, offset_of!(Vertex => TexCoords).get_byte_offset() as *const _); + + //vertex tangent + + gl::EnableVertexAttribArray(3); + gl::VertexAttribPointer(3, 3, gl::FLOAT, gl::FALSE, std::mem::size_of::() as i32, offset_of!(Vertex => Tangent).get_byte_offset() as *const _); + + // vertex Bitangent + + gl::EnableVertexAttribArray(4); + gl::VertexAttribPointer(4, 3, gl::FLOAT, gl::FALSE, std::mem::size_of::() as i32, offset_of!(Vertex => Bitangent).get_byte_offset() as *const _); + + // ids + gl::EnableVertexAttribArray(5); + gl::VertexAttribIPointer(5, 4, gl::INT, std::mem::size_of::() as i32, offset_of!(Vertex => m_BoneIDs).get_byte_offset() as *const _); + + // weights + gl::EnableVertexAttribArray(6); + gl::VertexAttribPointer(6, 4, gl::FLOAT, gl::FALSE, std::mem::size_of::() as i32, offset_of!(Vertex => m_Weights).get_byte_offset() as *const _); + } + + } + +}