From 7cf8943ee643398412f808361c657505d09a8262 Mon Sep 17 00:00:00 2001 From: fredrikr79 Date: Thu, 2 Oct 2025 11:16:18 +0200 Subject: [PATCH] praise the ai --- gloom-rs/src/draw.rs | 72 ++++++++++++++++++++++++-------------------- gloom-rs/src/mesh.rs | 2 ++ 2 files changed, 41 insertions(+), 33 deletions(-) diff --git a/gloom-rs/src/draw.rs b/gloom-rs/src/draw.rs index 99b6043..239870d 100644 --- a/gloom-rs/src/draw.rs +++ b/gloom-rs/src/draw.rs @@ -70,21 +70,18 @@ unsafe fn draw_scene(node: &SceneNode, } } -// == // Generate your VAO here -unsafe fn create_vao(vertices: &Vec, indices: &Vec, colors: &Vec, normals: &Vec, id:u32) -> u32 { - // generate a VAO and bind it - let mut vao_id: u32 = id; +// draw.rs - Replace create_vao function +unsafe fn create_vao(vertices: &Vec, indices: &Vec, colors: &Vec, normals: &Vec) -> u32 { + // Let OpenGL generate the VAO ID + let mut vao_id: u32 = 0; gl::GenVertexArrays(1, &mut vao_id); gl::BindVertexArray(vao_id); // -- vertex buffer -- - - // generate a VBO and bind it let mut vertices_vbo_id: u32 = 0; gl::GenBuffers(1, &mut vertices_vbo_id); gl::BindBuffer(gl::ARRAY_BUFFER, vertices_vbo_id); - // fill it with data gl::BufferData( gl::ARRAY_BUFFER, byte_size_of_array(vertices), @@ -92,19 +89,15 @@ unsafe fn create_vao(vertices: &Vec, indices: &Vec, colors: &Vec, gl::STATIC_DRAW ); - // configure a VAP for the data and enable it let vertices_index = 0; gl::VertexAttribPointer(vertices_index, 3, gl::FLOAT, gl::FALSE, size_of::()*3, ptr::null()); gl::EnableVertexAttribArray(vertices_index); // -- color buffer -- - - // generate a VBO and bind it - let mut colors_vbo_id: u32 = 1; + let mut colors_vbo_id: u32 = 0; gl::GenBuffers(1, &mut colors_vbo_id); gl::BindBuffer(gl::ARRAY_BUFFER, colors_vbo_id); - // fill it with data gl::BufferData( gl::ARRAY_BUFFER, byte_size_of_array(colors), @@ -112,19 +105,15 @@ unsafe fn create_vao(vertices: &Vec, indices: &Vec, colors: &Vec, gl::STATIC_DRAW ); - // configure a VAP for the data and enable it let colors_index = 1; gl::VertexAttribPointer(colors_index, 4, gl::FLOAT, gl::FALSE, size_of::()*4, ptr::null()); gl::EnableVertexAttribArray(colors_index); // -- normal buffer -- - - // generate a VBO and bind it - let mut normals_vbo_id: u32 = 2; + let mut normals_vbo_id: u32 = 0; gl::GenBuffers(1, &mut normals_vbo_id); gl::BindBuffer(gl::ARRAY_BUFFER, normals_vbo_id); - // fill it with data gl::BufferData( gl::ARRAY_BUFFER, byte_size_of_array(normals), @@ -132,19 +121,15 @@ unsafe fn create_vao(vertices: &Vec, indices: &Vec, colors: &Vec, gl::STATIC_DRAW ); - // configure a VAP for the normal data and enable it let normals_index = 2; gl::VertexAttribPointer(normals_index, 3, gl::FLOAT, gl::FALSE, size_of::()*3, ptr::null()); gl::EnableVertexAttribArray(normals_index); // -- index buffer -- - - // generate a IBO and bind it let mut ibo_id: u32 = 0; gl::GenBuffers(1, &mut ibo_id); gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, ibo_id); - // fill it with data gl::BufferData( gl::ELEMENT_ARRAY_BUFFER, byte_size_of_array(indices), @@ -172,19 +157,21 @@ pub enum Nodes { } pub fn load_models() -> HashMap { - let lunarsurface = Terrain::load(&"resources/lunarsurface.obj"); + let mut lunarsurface = Terrain::load(&"resources/lunarsurface.obj"); let helicopter = Helicopter::load(&"resources/helicopter.obj"); - let heli_body = helicopter.body; - let heli_door = helicopter.door; - let heli_main_rotor = helicopter.main_rotor; - let heli_tail_rotor = helicopter.tail_rotor; + let mut heli_body = helicopter.body; + let mut heli_door = helicopter.door; + let mut heli_main_rotor = helicopter.main_rotor; + let mut heli_tail_rotor = helicopter.tail_rotor; + unsafe { - create_vao(&lunarsurface.vertices, &lunarsurface.indices, &lunarsurface.colors, &lunarsurface.normals, Nodes::LunarSurface as u32); - create_vao(&heli_body.vertices, &heli_body.indices, &heli_body.colors, &heli_body.normals, Nodes::HeliBody as u32); - create_vao(&heli_door.vertices, &heli_door.indices, &heli_door.colors, &heli_door.normals, Nodes::HeliDoor as u32); - create_vao(&heli_main_rotor.vertices, &heli_main_rotor.indices, &heli_main_rotor.colors, &heli_main_rotor.normals, Nodes::HeliMainRotor as u32); - create_vao(&heli_tail_rotor.vertices, &heli_tail_rotor.indices, &heli_tail_rotor.colors, &heli_tail_rotor.normals, Nodes::HeliTailRotor as u32); + lunarsurface.vao_id = create_vao(&lunarsurface.vertices, &lunarsurface.indices, &lunarsurface.colors, &lunarsurface.normals); + heli_body.vao_id = create_vao(&heli_body.vertices, &heli_body.indices, &heli_body.colors, &heli_body.normals); + heli_door.vao_id = create_vao(&heli_door.vertices, &heli_door.indices, &heli_door.colors, &heli_door.normals); + heli_main_rotor.vao_id = create_vao(&heli_main_rotor.vertices, &heli_main_rotor.indices, &heli_main_rotor.colors, &heli_main_rotor.normals); + heli_tail_rotor.vao_id = create_vao(&heli_tail_rotor.vertices, &heli_tail_rotor.indices, &heli_tail_rotor.colors, &heli_tail_rotor.normals); } + return HashMap::from([ (Nodes::LunarSurface, lunarsurface), (Nodes::HeliBody, heli_body), @@ -194,12 +181,13 @@ pub fn load_models() -> HashMap { ]); } + pub fn setup_scene_graph(meshes: &HashMap) -> HashMap>>> { let mut nodes = HashMap::new(); - // Create all nodes + // Create all nodes with correct VAO IDs from meshes for (node, mesh) in meshes { - nodes.insert(*node, SceneNode::from_vao(*node as u32, mesh.index_count)); + nodes.insert(*node, SceneNode::from_vao(mesh.vao_id, mesh.index_count)); } nodes.insert(Nodes::HeliRoot, SceneNode::new()); @@ -217,9 +205,27 @@ pub fn setup_scene_graph(meshes: &HashMap) -> HashMap, pub indices : Vec, pub index_count : i32, + pub vao_id : u32, } impl Mesh { @@ -25,6 +26,7 @@ impl Mesh { indices: mesh.indices, colors: generate_color_vec(color, num_verts), index_count, + vao_id: 0 } } }