Configure SDL2 to use OpenGL #4

Merged
paoda merged 7 commits from opengl into main 2022-10-20 01:41:51 +00:00
3 changed files with 19 additions and 21 deletions
Showing only changes of commit f44a1a49fd - Show all commits

View File

@ -25,10 +25,10 @@ pub const Gui = struct {
// zig fmt: off // zig fmt: off
const vertices: [32]f32 = [_]f32{ const vertices: [32]f32 = [_]f32{
// Positions // Colours // Texture Coords // Positions // Colours // Texture Coords
1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, // Top Right 1.0, -1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 1.0, // Top Right
1.0, -1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, // Bottom Right 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, // Bottom Right
-1.0, -1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, // Bottom Left -1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, // Bottom Left
-1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, // Top Left -1.0, -1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, // Top Left
}; };
const indices: [6]u32 = [_]u32{ const indices: [6]u32 = [_]u32{
@ -47,8 +47,8 @@ pub const Gui = struct {
pub fn init(title: *const [12]u8, apu: *Apu, width: i32, height: i32) Self { pub fn init(title: *const [12]u8, apu: *Apu, width: i32, height: i32) Self {
if (SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_EVENTS | SDL.SDL_INIT_AUDIO) < 0) panic(); if (SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_EVENTS | SDL.SDL_INIT_AUDIO) < 0) panic();
if (SDL.SDL_GL_SetAttribute(SDL.SDL_GL_CONTEXT_PROFILE_MASK, SDL.SDL_GL_CONTEXT_PROFILE_CORE) < 0) panic(); if (SDL.SDL_GL_SetAttribute(SDL.SDL_GL_CONTEXT_PROFILE_MASK, SDL.SDL_GL_CONTEXT_PROFILE_CORE) < 0) panic();
if (SDL.SDL_GL_SetAttribute(SDL.SDL_GL_CONTEXT_MAJOR_VERSION, 4) < 0) panic(); if (SDL.SDL_GL_SetAttribute(SDL.SDL_GL_CONTEXT_MAJOR_VERSION, 3) < 0) panic();
if (SDL.SDL_GL_SetAttribute(SDL.SDL_GL_CONTEXT_MAJOR_VERSION, 4) < 0) panic(); if (SDL.SDL_GL_SetAttribute(SDL.SDL_GL_CONTEXT_MAJOR_VERSION, 3) < 0) panic();
const win_scale = @intCast(c_int, config.config().host.win_scale); const win_scale = @intCast(c_int, config.config().host.win_scale);
@ -121,13 +121,13 @@ pub const Gui = struct {
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, @sizeOf(@TypeOf(indices)), &indices, gl.STATIC_DRAW); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, @sizeOf(@TypeOf(indices)), &indices, gl.STATIC_DRAW);
// Position // Position
gl.vertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, 8 * @sizeOf(f32), &0); gl.vertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, 8 * @sizeOf(f32), @intToPtr(?*anyopaque, 0)); // lmao
gl.enableVertexAttribArray(0); gl.enableVertexAttribArray(0);
// Colour // Colour
gl.vertexAttribPointer(1, 3, gl.FLOAT, gl.FALSE, 8 * @sizeOf(f32), &(3 * @sizeOf(f32))); gl.vertexAttribPointer(1, 3, gl.FLOAT, gl.FALSE, 8 * @sizeOf(f32), @intToPtr(?*anyopaque, (3 * @sizeOf(f32))));
gl.enableVertexAttribArray(1); gl.enableVertexAttribArray(1);
// Texture Coord // Texture Coord
gl.vertexAttribPointer(2, 3, gl.FLOAT, gl.FALSE, 8 * @sizeOf(f32), &(6 * @sizeOf(f32))); gl.vertexAttribPointer(2, 2, gl.FLOAT, gl.FALSE, 8 * @sizeOf(f32), @intToPtr(?*anyopaque, (6 * @sizeOf(f32))));
gl.enableVertexAttribArray(2); gl.enableVertexAttribArray(2);
return .{ vao_id, vbo_id, ebo_id }; return .{ vao_id, vbo_id, ebo_id };
@ -138,13 +138,13 @@ pub const Gui = struct {
gl.genTextures(1, &tex_id); gl.genTextures(1, &tex_id);
gl.bindTexture(gl.TEXTURE_2D, tex_id); gl.bindTexture(gl.TEXTURE_2D, tex_id);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); // gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); // gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gba_width, gba_height, 0, gl.RGBA, gl.UNSIGNED_BYTE, buf.ptr); gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gba_width, gba_height, 0, gl.RGBA, gl.UNSIGNED_INT_8_8_8_8, buf.ptr);
// gl.generateMipmap(gl.TEXTURE_2D); // TODO: Remove? // gl.generateMipmap(gl.TEXTURE_2D); // TODO: Remove?
return tex_id; return tex_id;
@ -220,7 +220,7 @@ pub const Gui = struct {
// Emulator has an internal Double Buffer // Emulator has an internal Double Buffer
const framebuf = cpu.bus.ppu.framebuf.get(.Renderer); const framebuf = cpu.bus.ppu.framebuf.get(.Renderer);
gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gba_width, gba_height, gl.RGBA, gl.UNSIGNED_BYTE, framebuf.ptr); gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gba_width, gba_height, gl.RGBA, gl.UNSIGNED_INT_8_8_8_8, framebuf.ptr);
gl.useProgram(self.program_id); gl.useProgram(self.program_id);
gl.bindVertexArray(vao_id); gl.bindVertexArray(vao_id);

View File

@ -1,4 +1,4 @@
#version 450 core #version 330 core
out vec4 FragColor; out vec4 FragColor;
in vec3 ourColor; in vec3 ourColor;
@ -7,8 +7,7 @@ in vec2 TexCoord;
// texture sampler // texture sampler
uniform sampler2D texture1; uniform sampler2D texture1;
void main() void main() {
{ FragColor = texture(texture1, TexCoord);
FragColor = texture(texture1, TexCoord).wzyx;
} }

View File

@ -1,4 +1,4 @@
#version 450 core #version 330 core
layout (location = 0) in vec3 aPos; layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor; layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord; layout (location = 2) in vec2 aTexCoord;
@ -6,9 +6,8 @@ layout (location = 2) in vec2 aTexCoord;
out vec3 ourColor; out vec3 ourColor;
out vec2 TexCoord; out vec2 TexCoord;
void main() void main() {
{
gl_Position = vec4(aPos, 1.0); gl_Position = vec4(aPos, 1.0);
ourColor = aColor; ourColor = aColor;
TexCoord = vec2(aTexCoord.x, aTexCoord.y); TexCoord = aTexCoord;
} }