opengl: uniforms

This commit is contained in:
Mitchell Hashimoto
2022-04-01 19:05:16 -07:00
parent 045dd704ec
commit 80d064ed51
2 changed files with 21 additions and 1 deletions

View File

@ -70,6 +70,8 @@ pub fn main() !void {
c.glClear(c.GL_COLOR_BUFFER_BIT);
try program.use();
try program.setUniform("vertexColor", @Vector(4, f32){ 0.0, 1.0, 0.0, 1.0 });
try vao.bind();
c.glDrawArrays(c.GL_TRIANGLES, 0, 3);
@ -95,8 +97,10 @@ const fs_source =
\\#version 330 core
\\out vec4 FragColor;
\\
\\uniform vec4 vertexColor;
\\
\\void main()
\\{
\\ FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
\\ FragColor = vertexColor;
\\}
;

View File

@ -45,6 +45,22 @@ pub inline fn use(p: Program) !void {
c.glUseProgram(p.id);
}
/// Requires the program is currently in use.
pub inline fn setUniform(p: Program, n: [:0]const u8, value: anytype) !void {
const loc = c.glGetUniformLocation(p.id, n);
if (loc < 0) {
return error.UniformNameInvalid;
}
try errors.getError();
// Perform the correct call depending on the type of the value.
switch (@TypeOf(value)) {
@Vector(4, f32) => c.glUniform4f(loc, value[0], value[1], value[2], value[3]),
else => unreachable,
}
try errors.getError();
}
/// getInfoLog returns the info log for this program. This attempts to
/// keep the log fully stack allocated and is therefore limited to a max
/// amount of elements.