Basic shaders for triangle coloring

This commit is contained in:
Dendy 2022-07-22 16:10:58 +02:00
parent 2385167e87
commit 07b47f3e82
3 changed files with 44 additions and 18 deletions

View File

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

7
src/shaders/fragment.fs Normal file
View File

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

8
src/shaders/vector.vs Normal file
View File

@ -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;
}