feat: add normal vectors to VAO creation for lighting calculations

This commit is contained in:
2025-10-02 01:01:36 +02:00
parent 20c7dc975c
commit 07a0b0260d

View File

@@ -60,7 +60,7 @@ fn offset<T>(n: u32) -> *const c_void {
// Get a null pointer (equivalent to an offset of 0)
// ptr::null()
unsafe fn create_vao(vertices: &Vec<f32>, colors: &Vec<f32>, indices: &Vec<u32>) -> u32 {
unsafe fn create_vao(vertices: &Vec<f32>, normals: &Vec<f32>, colors: &Vec<f32>, indices: &Vec<u32>) -> u32 {
let mut vao_id = 0;
gl::GenVertexArrays(1, &mut vao_id);
gl::BindVertexArray(vao_id);
@@ -109,6 +109,19 @@ unsafe fn create_vao(vertices: &Vec<f32>, colors: &Vec<f32>, indices: &Vec<u32>)
gl::VertexAttribPointer(1, 4, gl::FLOAT, gl::FALSE, 0, std::ptr::null());
gl::EnableVertexAttribArray(1);
// normal buffer
let mut nbo_id = 0;
gl::GenBuffers(1, &mut nbo_id);
gl::BindBuffer(gl::ARRAY_BUFFER, nbo_id);
gl::BufferData(
gl::ARRAY_BUFFER,
byte_size_of_array(&normals),
normals.as_ptr() as *const c_void,
gl::STATIC_DRAW,
);
gl::VertexAttribPointer(2, DIMENSIONS, gl::FLOAT, gl::FALSE, 0, std::ptr::null());
gl::EnableVertexAttribArray(2);
vao_id
}
@@ -184,7 +197,7 @@ fn main() {
// == // Set up your VAO around here
let terrain_mesh = mesh::Terrain::load("resources/lunarsurface.obj");
let terrain_vao = unsafe {
create_vao(&terrain_mesh.vertices, &terrain_mesh.colors, &terrain_mesh.indices)
create_vao(&terrain_mesh.vertices, &terrain_mesh.normals, &terrain_mesh.colors, &terrain_mesh.indices)
};
let vertices = vec![
@@ -241,7 +254,7 @@ fn main() {
1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0,
];
let billboard_vao = unsafe { create_vao(&billboard_vertices, &billboard_colors, &billboard_indices) };
let billboard_vao = unsafe { create_vao(&billboard_vertices, &Vec::new(), &billboard_colors, &billboard_indices) };
// Generate a grid of pointparticles
let grid_size = 200;
@@ -256,7 +269,7 @@ fn main() {
particle_colors.extend_from_slice(&[1.0, 1.0, 1.0, 1.0]);
}
}
let particle_vao = unsafe { create_vao(&particle_vertices, &particle_colors, &Vec::new()) };
let particle_vao = unsafe { create_vao(&particle_vertices, &Vec::new(), &particle_colors, &Vec::new()) };
let particle_count = (grid_size * grid_size) as i32;
// == // Set up your shaders here