From 3a2ae5044433aa3125c6d753d64d0a277b496c4d Mon Sep 17 00:00:00 2001 From: fredrikr79 Date: Wed, 27 Aug 2025 21:23:52 +0200 Subject: [PATCH] trungle --- shaders/simple.frag | 4 +-- shaders/simple.vert | 6 ++-- src/main.rs | 67 +++++++++++++++++++++++++++++---------------- 3 files changed, 49 insertions(+), 28 deletions(-) diff --git a/shaders/simple.frag b/shaders/simple.frag index 1029717..e9f180f 100644 --- a/shaders/simple.frag +++ b/shaders/simple.frag @@ -1,8 +1,8 @@ -#version 430 core +#version 450 core out vec4 color; void main() { color = vec4(1.0f, 1.0f, 1.0f, 1.0f); -} \ No newline at end of file +} diff --git a/shaders/simple.vert b/shaders/simple.vert index 446d1dc..0d3541b 100644 --- a/shaders/simple.vert +++ b/shaders/simple.vert @@ -1,8 +1,8 @@ -#version 430 core +#version 450 core -in vec3 position; +in layout(location=0) vec3 position; void main() { gl_Position = vec4(position, 1.0f); -} \ No newline at end of file +} diff --git a/src/main.rs b/src/main.rs index 8617821..169524b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -52,22 +52,43 @@ fn offset(n: u32) -> *const c_void { // ptr::null() -// == // Generate your VAO here unsafe fn create_vao(vertices: &Vec, indices: &Vec) -> u32 { - // Implement me! + // generate a VAO and bind it + let mut vao_id: u32 = 0; + gl::GenVertexArrays(1, &mut vao_id); + gl::BindVertexArray(vao_id); - // Also, feel free to delete comments :) + // generate a VBO and bind it + let mut vbo_id: u32 = 0; + gl::GenBuffers(1, &mut vbo_id); + gl::BindBuffer(gl::ARRAY_BUFFER, vbo_id); - // This should: - // * Generate a VAO and bind it - // * Generate a VBO and bind it - // * Fill it with data - // * Configure a VAP for the data and enable it - // * Generate a IBO and bind it - // * Fill it with data - // * Return the ID of the VAO + // fill it with data + gl::BufferData( + gl::ARRAY_BUFFER, + byte_size_of_array(&vertices), + pointer_to_array(&vertices), + gl::STATIC_DRAW + ); - 0 + // configure a VAP for the data and enable it + gl::VertexAttribPointer(0, 3, gl::FLOAT, gl::FALSE, size_of::()*3, ptr::null()); + gl::EnableVertexAttribArray(0); + + // 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), + pointer_to_array(&indices), + gl::STATIC_DRAW + ); + + return vao_id; } @@ -130,9 +151,13 @@ fn main() { println!("GLSL\t: {}", util::get_gl_string(gl::SHADING_LANGUAGE_VERSION)); } - // == // Set up your VAO around here - - let my_vao = unsafe { 1337 }; + let vertices = vec![ + -0.6, -0.6, 0.0, + 0.6, -0.6, 0.0, + 0.0, 0.6, 0.0 + ]; + let indices = vec![ 0, 1, 2 ]; + let vao_id = unsafe { create_vao(&vertices, &indices) }; // == // Set up your shaders here @@ -144,13 +169,13 @@ fn main() { // This snippet is not enough to do the exercise, and will need to be modified (outside // of just using the correct path), but it only needs to be called once - /* let simple_shader = unsafe { shader::ShaderBuilder::new() - .attach_file("./path/to/simple/shader.file") + .attach_file("./shaders/simple.vert") + .attach_file("./shaders/simple.frag") .link() }; - */ + unsafe { simple_shader.activate(); }; // Used to demonstrate keyboard handling for exercise 2. @@ -215,11 +240,7 @@ fn main() { gl::ClearColor(0.035, 0.046, 0.078, 1.0); // night sky gl::Clear(gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT); - - // == // Issue the necessary gl:: commands to draw your scene here - - - + gl::DrawElements(gl::TRIANGLE_STRIP, 3, gl::UNSIGNED_INT, ptr::null()); } // Display the new color buffer on the display