renderer/opengl: simplifying the custom shader to get things working

This commit is contained in:
Mitchell Hashimoto
2023-11-17 09:43:49 -08:00
parent 1fedc912f0
commit aff5090362
3 changed files with 46 additions and 7 deletions

View File

@ -7,6 +7,12 @@ const gl = @import("opengl");
program: gl.Program,
/// This VAO is used for all custom shaders. It contains a single quad
/// by using an EBO. The vertex ID (gl_VertexID) can be used to determine the
/// position of the vertex.
vao: gl.VertexArray,
ebo: gl.Buffer,
pub const Uniforms = extern struct {
resolution: [3]f32 align(16),
time: f32 align(4),
@ -33,24 +39,48 @@ pub fn createList(alloc: Allocator, srcs: []const [:0]const u8) ![]const CustomP
}
pub fn init(src: [:0]const u8) !CustomProgram {
_ = src;
const program = try gl.Program.createVF(
@embedFile("../shaders/custom.v.glsl"),
src,
//src,
@embedFile("../shaders/temp.f.glsl"),
);
errdefer program.destroy();
// Create our uniform buffer that is shared across all custom shaders
const ubo = try gl.Buffer.create();
errdefer ubo.destroy();
var ubobind = try ubo.bind(.uniform);
defer ubobind.unbind();
try ubobind.setDataNull(Uniforms, .static_draw);
{
var ubobind = try ubo.bind(.uniform);
defer ubobind.unbind();
try ubobind.setDataNull(Uniforms, .static_draw);
}
// Setup our VAO for the custom shader.
const vao = try gl.VertexArray.create();
errdefer vao.destroy();
const vaobind = try vao.bind();
defer vaobind.unbind();
// Element buffer (EBO)
const ebo = try gl.Buffer.create();
errdefer ebo.destroy();
var ebobind = try ebo.bind(.element_array);
defer ebobind.unbind();
try ebobind.setData([6]u8{
0, 1, 3, // Top-left triangle
1, 2, 3, // Bottom-right triangle
}, .static_draw);
return .{
.program = program,
.vao = vao,
.ebo = ebo,
};
}
pub fn deinit(self: CustomProgram) void {
self.ebo.destroy();
self.vao.destroy();
self.program.destroy();
}

View File

@ -1,7 +1,8 @@
#version 330 core
layout (location = 0) in vec2 position;
void main(){
gl_Position = vec4(position.x, position.y, 0.0f, 1.0f);
vec2 position;
position.x = (gl_VertexID == 0 || gl_VertexID == 1) ? 1. : 0.;
position.y = (gl_VertexID == 0 || gl_VertexID == 3) ? 0. : 1.;
gl_Position = vec4(position.xy, 0.0f, 1.0f);
}

View File

@ -0,0 +1,8 @@
#version 330 core
layout(location = 0) out vec4 out_FragColor;
void main() {
// read
out_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}