diff options
-rw-r--r-- | src/main.zig | 46 |
1 files changed, 34 insertions, 12 deletions
diff --git a/src/main.zig b/src/main.zig index 13ff58b..8b9f927 100644 --- a/src/main.zig +++ b/src/main.zig @@ -17,20 +17,24 @@ const egl = @import("egl.zig"); const vertex_shader_src: [*c]const u8 = \\#version 330 core \\layout (location = 0) in vec3 pos; + \\layout (location = 1) in vec4 in_color; + \\out vec4 color; \\ \\void main() \\{ + \\ color = in_color; \\ gl_Position = vec4(pos.x, pos.y, pos.z, 1.0); \\} ; const frag_shader_src: [*c]const u8 = \\#version 330 core + \\in vec4 color; \\out vec4 frag_color; \\ \\void main() \\{ - \\ frag_color = vec4(1.0f, 0.2f, 0.2f, 1.0f); + \\ frag_color = vec4(color); \\} ; @@ -117,19 +121,24 @@ pub const Window = struct { /// OpenGl triangle shader program shader_prog: c_uint = undefined, /// Triangle vertices - vertices: [12]f32 = .{ + vertices: [9]f32 = .{ -0.5, -0.5, 0.0, // bl - 0.5, -0.5, 0.0, // br - 0.5, 0.5, 0.0, // tr - -0.5, 0.5, 0.0, // tl + 0.5, -0.5, 0.0, // br + 0.0, 0.5, 0.0, // t + }, + colors: [12]f32 = .{ + 1.0, 0.0, 0.0, 1.0, + 0.0, 1.0, 0.0, 1.0, + 0.0, 0.0, 1.0, 1.0, }, /// Triangles indices - indices: [6]c_uint = .{ + indices: [3]c_uint = .{ 0, 1, 2, - 2, 3, 0, }, /// OpenGL vertex buffer object vbo: c_uint = undefined, + /// OpenGL color vbo + cbo: c_uint = undefined, /// OpenGL vertex array object vao: c_uint = undefined, // OpenGL element buffer object @@ -194,6 +203,7 @@ pub const Window = struct { c.glGenVertexArrays(1, &win.vao); c.glGenBuffers(1, &win.vbo); + c.glGenBuffers(1, &win.cbo); c.glGenBuffers(1, &win.ebo); c.glBindVertexArray(win.vao); @@ -206,6 +216,20 @@ pub const Window = struct { c.GL_STATIC_DRAW, ); + c.glVertexAttribPointer(0, 3, c.GL_FLOAT, c.GL_FALSE, 3 * @sizeOf(f32), null); + c.glEnableVertexAttribArray(0); + + c.glBindBuffer(c.GL_ARRAY_BUFFER, win.cbo); + c.glBufferData( + c.GL_ARRAY_BUFFER, + @sizeOf(@TypeOf(win.colors)), + &win.colors, + c.GL_STATIC_DRAW, + ); + + c.glVertexAttribPointer(1, 3, c.GL_FLOAT, c.GL_FALSE, 4 * @sizeOf(f32), null); + c.glEnableVertexAttribArray(1); + c.glBindBuffer(c.GL_ELEMENT_ARRAY_BUFFER, win.ebo); c.glBufferData( c.GL_ELEMENT_ARRAY_BUFFER, @@ -214,9 +238,6 @@ pub const Window = struct { c.GL_STATIC_DRAW, ); - c.glVertexAttribPointer(0, 3, c.GL_FLOAT, c.GL_FALSE, 3 * @sizeOf(f32), null); - c.glEnableVertexAttribArray(0); - c.glBindBuffer(c.GL_ARRAY_BUFFER, 0); c.glBindVertexArray(0); @@ -251,12 +272,12 @@ pub const Window = struct { pub fn drawFrame(win: *Window) !void { try egl_display.swapInterval(0); - c.glClearColor(0, 0.5, 1, 1); + c.glClearColor(0, 0.05, 0.15, 1); c.glClear(c.GL_COLOR_BUFFER_BIT); c.glUseProgram(win.shader_prog); c.glBindVertexArray(win.vao); - c.glDrawElements(c.GL_TRIANGLES, 6, c.GL_UNSIGNED_INT, null); + c.glDrawElements(c.GL_TRIANGLES, win.indices.len, c.GL_UNSIGNED_INT, null); try egl_display.swapBuffers(win.egl_surface); } @@ -361,6 +382,7 @@ pub fn main() !void { .{ .type = .red_size, .value = 8 }, .{ .type = .green_size, .value = 8 }, .{ .type = .blue_size, .value = 8 }, + .{ .type = .alpha_size, .value = 8 }, .{ .type = .none, .value = 0 }, }; egl_display = try egl.Display.get(display); |