Being able to set the color

This commit is contained in:
Dendy 2022-07-22 16:46:13 +02:00
parent 07b47f3e82
commit 4883fc68e2
2 changed files with 14 additions and 4 deletions

View File

@ -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) {

View File

@ -1,7 +1,7 @@
#version 330 core
out vec3 color;
uniform vec4 color;
void main(){
color = vec3(1,0,0);
gl_FragColor = color;
}