diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main.zig | 32 |
1 files changed, 24 insertions, 8 deletions
diff --git a/src/main.zig b/src/main.zig index 7543090..13ff58b 100644 --- a/src/main.zig +++ b/src/main.zig @@ -70,7 +70,7 @@ fn checkGlError( const err = success == 0; if (err) { getLogFn(obj, 512, null, &gl_log); - std.debug.print("Shader compilation error: {s}\n", .{gl_log}); + std.debug.print("OpenGL shader error: {s}\n", .{gl_log}); } return err; } @@ -117,11 +117,23 @@ pub const Window = struct { /// OpenGl triangle shader program shader_prog: c_uint = undefined, /// Triangle vertices - vertices: [9]f32, + vertices: [12]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 + }, + /// Triangles indices + indices: [6]c_uint = .{ + 0, 1, 2, + 2, 3, 0, + }, /// OpenGL vertex buffer object vbo: c_uint = undefined, /// OpenGL vertex array object vao: c_uint = undefined, + // OpenGL element buffer object + ebo: c_uint = undefined, /// This window's title title: [*:0]const u8, /// Width in pixels @@ -147,11 +159,6 @@ pub const Window = struct { .allocator = allocator, .wl_surface = try compositor.createSurface(), .title = title, - .vertices = .{ - -0.5, -0.5, 0.0, - 0.5, -0.5, 0.0, - 0.0, 0.5, 0.0, - }, }; win.xdg_surface = try wm_base.getXdgSurface(win.wl_surface); errdefer win.xdg_surface.destroy(); @@ -187,6 +194,7 @@ pub const Window = struct { c.glGenVertexArrays(1, &win.vao); c.glGenBuffers(1, &win.vbo); + c.glGenBuffers(1, &win.ebo); c.glBindVertexArray(win.vao); @@ -198,6 +206,14 @@ pub const Window = struct { c.GL_STATIC_DRAW, ); + c.glBindBuffer(c.GL_ELEMENT_ARRAY_BUFFER, win.ebo); + c.glBufferData( + c.GL_ELEMENT_ARRAY_BUFFER, + @sizeOf(@TypeOf(win.indices)), + &win.indices, + c.GL_STATIC_DRAW, + ); + c.glVertexAttribPointer(0, 3, c.GL_FLOAT, c.GL_FALSE, 3 * @sizeOf(f32), null); c.glEnableVertexAttribArray(0); @@ -240,7 +256,7 @@ pub const Window = struct { c.glUseProgram(win.shader_prog); c.glBindVertexArray(win.vao); - c.glDrawArrays(c.GL_TRIANGLES, 0, 3); + c.glDrawElements(c.GL_TRIANGLES, 6, c.GL_UNSIGNED_INT, null); try egl_display.swapBuffers(win.egl_surface); } |