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

@@ -2,20 +2,8 @@
out layout(location=0) vec4 color;
in vec4 gl_FragCoord;
in vec4 vColor;
void main() {
vec4 b = vec4(0.0f,0.0f,0.0f,0.0f);
vec4 w = vec4(1.0f, 1.0f, 1.0f, 1.0f);
float square_size = 128.0f;
bool x = mod(gl_FragCoord.x, square_size) < square_size / 2.0f;
bool y = mod(gl_FragCoord.y, square_size) < square_size / 2.0f;
if (x && y) {
color = b;
} else if (x && !y) {
color = w;
} else if (!x && y) {
color = w;
} else if (!x && !y) {
color = b;
}
color = vColor;
}

View File

@@ -1,7 +1,10 @@
#version 460 core
in layout(location=0) vec3 position;
in layout(location=1) vec4 aColor;
out vec4 vColor;
void main() {
gl_Position = vec4(position, 1.0f);
vColor = aColor;
}

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