From 4883fc68e2bd6cfe8b4123f54f0b04d60f979bb8 Mon Sep 17 00:00:00 2001 From: Dendy Date: Fri, 22 Jul 2022 16:46:13 +0200 Subject: [PATCH] Being able to set the color --- src/main.zig | 14 ++++++++++++-- src/shaders/fragment.fs | 4 ++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main.zig b/src/main.zig index c9d9253..a5bd152 100644 --- a/src/main.zig +++ b/src/main.zig @@ -28,6 +28,7 @@ pub fn main() !void { const ctx = try sdl.gl.createContext(window); defer sdl.gl.deleteContext(ctx); + var color_loc: u32 = undefined; const program = gl.Program.create(); { const vs = gl.Shader.create(.vertex); @@ -43,6 +44,7 @@ pub fn main() !void { program.attach(fs); defer program.detach(fs); program.link(); + color_loc = program.uniformLocation("color").?; } program.use(); @@ -51,8 +53,11 @@ pub fn main() !void { const vertex_buffer = [_]f32{ -1.0, -1.0, 0.0, - 1.0, -1.0, 0.0, + -1.0, 1.0, 0.0, 0.0, 1.0, 0.0, + 1.0, 1.0, 0.0, + 0.0, -1.0, 0.0, + 1.0, -1.0, 0.0, }; const buf = gl.Buffer.gen(); @@ -83,9 +88,14 @@ pub fn main() !void { gl.enableVertexAttribArray(0); buf.bind(.array_buffer); - gl.vertexAttribPointer(0, 3, .float, false, 0, 0); + + gl.uniform4f(color_loc, 0.45, 0.21, 0.70, 1); gl.drawArrays(.triangles, 0, 3); + + gl.uniform4f(color_loc, 0.2, 0.5, 1.0, 1); + gl.drawArrays(.triangles, 3, 3); + gl.disableVertexAttribArray(0); //current_state = switch (current_state) { diff --git a/src/shaders/fragment.fs b/src/shaders/fragment.fs index 6e8915f..2d1d3b0 100644 --- a/src/shaders/fragment.fs +++ b/src/shaders/fragment.fs @@ -1,7 +1,7 @@ #version 330 core -out vec3 color; +uniform vec4 color; void main(){ - color = vec3(1,0,0); + gl_FragColor = color; }