diff --git a/shaders/simple.frag b/shaders/simple.frag index ed0dbad..223ad88 100644 --- a/shaders/simple.frag +++ b/shaders/simple.frag @@ -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; } diff --git a/shaders/simple.vert b/shaders/simple.vert index 758240f..5f93d4e 100644 --- a/shaders/simple.vert +++ b/shaders/simple.vert @@ -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; } diff --git a/src/main.rs b/src/main.rs index ee52f90..0441ce9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,7 +59,7 @@ fn offset(n: u32) -> *const c_void { // Get a null pointer (equivalent to an offset of 0) // ptr::null() -unsafe fn create_vao(vertices: &Vec, indices: &Vec) -> u32 { +unsafe fn create_vao(vertices: &Vec, colors: &Vec, indices: &Vec) -> 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, indices: &Vec) -> 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