From 07b47f3e82477da6d4539dfe8023a4088e06b7c4 Mon Sep 17 00:00:00 2001 From: Dendy Date: Fri, 22 Jul 2022 16:10:58 +0200 Subject: [PATCH] Basic shaders for triangle coloring --- src/main.zig | 47 +++++++++++++++++++++++++---------------- src/shaders/fragment.fs | 7 ++++++ src/shaders/vector.vs | 8 +++++++ 3 files changed, 44 insertions(+), 18 deletions(-) create mode 100644 src/shaders/fragment.fs create mode 100644 src/shaders/vector.vs diff --git a/src/main.zig b/src/main.zig index 5ae2fc0..c9d9253 100644 --- a/src/main.zig +++ b/src/main.zig @@ -11,8 +11,9 @@ pub fn main() !void { try sdl.init(.{ .video = true, .audio = true, .events = true }); defer sdl.quit(); - try sdl.gl.setAttribute(.{ .context_major_version = 2 }); - try sdl.gl.setAttribute(.{ .context_minor_version = 1 }); + // Set OpenGL version + try sdl.gl.setAttribute(.{ .context_major_version = 3 }); + try sdl.gl.setAttribute(.{ .context_minor_version = 3 }); const window = try sdl.createWindow( "USG", @@ -27,6 +28,24 @@ pub fn main() !void { const ctx = try sdl.gl.createContext(window); defer sdl.gl.deleteContext(ctx); + const program = gl.Program.create(); + { + const vs = gl.Shader.create(.vertex); + defer vs.delete(); + vs.source(1, &.{@embedFile("shaders/vector.vs")}); + vs.compile(); + const fs = gl.Shader.create(.fragment); + defer fs.delete(); + fs.source(1, &.{@embedFile("shaders/fragment.fs")}); + fs.compile(); + program.attach(vs); + defer program.detach(vs); + program.attach(fs); + defer program.detach(fs); + program.link(); + } + program.use(); + var vertex_array = gl.VertexArray.gen(); vertex_array.bind(); @@ -36,9 +55,9 @@ pub fn main() !void { 0.0, 1.0, 0.0, }; - const buffer = gl.Buffer.gen(); - buffer.bind(.array_buffer); - buffer.data(f32, &vertex_buffer, .static_draw); + const buf = gl.Buffer.gen(); + buf.bind(.array_buffer); + buf.data(f32, &vertex_buffer, .static_draw); //var renderer = SDL.SDL_CreateRenderer(window, -1, SDL.SDL_RENDERER_PRESENTVSYNC | SDL.SDL_RENDERER_ACCELERATED) orelse return; //defer _ = SDL.SDL_DestroyRenderer(renderer); @@ -48,9 +67,10 @@ pub fn main() !void { //var current_state: State = main_menu.state; - //_ = SDL.SDL_SetRenderDrawBlendMode(renderer, SDL.SDL_BLENDMODE_BLEND); //_ = SDL.IMG_Init(SDL.IMG_INIT_JPG); + gl.clearColor(0.91, 0.97, 1.00, 1.00); + mainLoop: while (true) { while (sdl.pollEvent()) |ev| { switch (ev) { @@ -59,29 +79,20 @@ pub fn main() !void { } } - gl.clear(.{}); + gl.clear(.{ .color = true }); gl.enableVertexAttribArray(0); - buffer.bind(.array_buffer); + buf.bind(.array_buffer); gl.vertexAttribPointer(0, 3, .float, false, 0, 0); gl.drawArrays(.triangles, 0, 3); gl.disableVertexAttribArray(0); - sdl.gl.swapWindow(window); - - //_ = SDL.SDL_SetRenderDrawColor(renderer, 231, 247, 255, 0xFF); - //_ = SDL.SDL_RenderClear(renderer); - //current_state = switch (current_state) { //.main_menu => main_menu.tick(), //.game => game.tick(), //}; - //SDL.SDL_RenderPresent(renderer); + sdl.gl.swapWindow(window); } } - -fn initGL() void { - -} diff --git a/src/shaders/fragment.fs b/src/shaders/fragment.fs new file mode 100644 index 0000000..6e8915f --- /dev/null +++ b/src/shaders/fragment.fs @@ -0,0 +1,7 @@ +#version 330 core + +out vec3 color; + +void main(){ + color = vec3(1,0,0); +} diff --git a/src/shaders/vector.vs b/src/shaders/vector.vs new file mode 100644 index 0000000..cb16d4d --- /dev/null +++ b/src/shaders/vector.vs @@ -0,0 +1,8 @@ +#version 330 core + +layout(location = 0) in vec3 vertexPosition_modelspace; + +void main() { + gl_Position.xyz = vertexPosition_modelspace; + gl_Position.w = 1.0; +}