feat: add colors to vao and shaders

This commit is contained in:
2025-09-11 22:46:14 +02:00
committed by Your Name
parent 0f13f037ce
commit ba0b49c3b6
3 changed files with 32 additions and 16 deletions

View File

@@ -59,7 +59,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>, indices: &Vec<u32>) -> u32 {
unsafe fn create_vao(vertices: &Vec<f32>, colors: &Vec<f32>, indices: &Vec<u32>) -> u32 {
let mut vao_id = 0;
gl::GenVertexArrays(1, &mut vao_id);
gl::BindVertexArray(vao_id);
@@ -95,6 +95,26 @@ unsafe fn create_vao(vertices: &Vec<f32>, indices: &Vec<u32>) -> u32 {
gl::STATIC_DRAW,
);
// color buffer
let mut cbo_id = 0;
gl::GenBuffers(1, &mut cbo_id);
gl::BindBuffer(gl::ARRAY_BUFFER, cbo_id);
gl::BufferData(
gl::ARRAY_BUFFER,
byte_size_of_array(&colors),
colors.as_ptr() as *const c_void,
gl::STATIC_DRAW,
);
gl::VertexAttribPointer(
1,
4,
gl::FLOAT,
gl::FALSE,
0,
std::ptr::null(),
);
gl::EnableVertexAttribArray(1);
vao_id
}
@@ -170,7 +190,12 @@ fn main() {
let vertices = vec![-0.6, -0.6, 0., 0.6, -0.6, 0., 0., 0.6, 0.];
let indices = vec![0, 1, 2];
let my_vao = unsafe { create_vao(&vertices, &indices) };
let colors = vec![
1.0, 0.0, 0.0, 1.0, // vertex 1: red
0.0, 1.0, 0.0, 1.0, // vertex 2: green
0.0, 0.0, 1.0, 1.0, // vertex 3: blue
];
let my_vao = unsafe { create_vao(&vertices, &colors, &indices) };
// == // Set up your shaders here