Uniform colors

This commit is contained in:
2026-02-03 20:31:12 +01:00
parent b80c321e84
commit 4394179a71
6 changed files with 189 additions and 48 deletions

View File

@@ -110,19 +110,14 @@ when RENDER_BACKEND_OPENGL {
}
opengl_set_clear_color :: proc(renderer: ^Renderer, color: Color) {
r, g, b, a: u8 = max(u8), max(u8), max(u8), max(u8)
r, g, b, a: f32 = 1.0, 1.0, 1.0, 1.0
switch &c in color {
case RGB_Color: r, g, b = expand_values(c.rgb)
case RGBA_Color: r, g, b, a = expand_values(c.rgba)
case RGB_Color: r, g, b = expand_values(color_to_f32(c))
case RGBA_Color: r, g, b, a = expand_values(color_to_f32(c))
}
gl.ClearColor(
f32(r) / f32(max(u8)),
f32(g) / f32(max(u8)),
f32(b) / f32(max(u8)),
f32(a) / f32(max(u8)),
)
gl.ClearColor(r, g, b, a)
}
opengl_clear_screen :: proc(renderer: ^Renderer, clear_color: bool, clear_depth: bool) {
@@ -290,11 +285,15 @@ when RENDER_BACKEND_OPENGL {
}
}
opengl_activate_shader_program :: proc(program: ^Shader_Program) {
gl.UseProgram(program.backend.handle)
}
opengl_activate_material :: proc(material: ^Material, model_matrix, view_matrix, projection_matrix: linalg.Matrix4x4f32) {
gl.UseProgram(material.shader_program.backend.handle)
opengl_activate_bind_textures_in_material(material)
// TODO: SS - Move uniforms up one step to the renderer-agnostic part of the codebase.
model_matrix_loc := gl.GetUniformLocation(material.shader_program.backend.handle, "in_model_matrix")
if model_matrix_loc >= 0 {
model_matrix_as_f32_array := transmute([16]f32)(model_matrix)
@@ -321,17 +320,8 @@ when RENDER_BACKEND_OPENGL {
opengl_activate_fullscreen_material :: proc(material: ^Material) { // TODO: SS - Maybe remove.
assert(material != nil)
gl.UseProgram(material.shader_program.backend.handle)
assert(material.texture_count > 0)
gl.ActiveTexture(gl.TEXTURE0)
gl.BindTexture(gl.TEXTURE_2D, material.textures[0].backend.handle)
loc := gl.GetUniformLocation(material.shader_program.backend.handle, "texture0")
if loc != -1 {
gl.Uniform1i(loc, 0)
}
opengl_activate_bind_textures_in_material(material)
gl.Disable(gl.DEPTH_TEST)
gl.DepthMask(gl.FALSE)
@@ -538,13 +528,20 @@ when RENDER_BACKEND_OPENGL {
status := gl.CheckFramebufferStatus(gl.FRAMEBUFFER)
assert(status == gl.FRAMEBUFFER_COMPLETE)
assert(len(rt.color_textures) > 0)
texture: ^Texture = nil
if len(rt.color_textures) > 0 {
texture = rt.color_textures[0]
}
else {
texture = rt.depth_texture
}
assert(texture != nil)
gl.Viewport(
0,
0,
i32(rt.color_textures[0].width),
i32(rt.color_textures[0].height),
i32(texture.width),
i32(texture.height),
)
}
@@ -599,7 +596,7 @@ when RENDER_BACKEND_OPENGL {
opengl_set_shader_uniform_texture :: proc(program: ^Shader_Program, uniform: Uniform_Texture) -> bool {
gl.UseProgram(program.backend.handle)
opengl_activate_shader_program(program)
i := uniform.index
@@ -617,7 +614,7 @@ when RENDER_BACKEND_OPENGL {
}
opengl_set_shader_uniform_float :: proc(program: ^Shader_Program, uniform: Uniform_Float) -> bool {
gl.UseProgram(program.backend.handle)
opengl_activate_shader_program(program)
loc := gl.GetUniformLocation(program.backend.handle, fmt.ctprintf("%v", uniform.name))
if loc < 0 {
@@ -632,7 +629,7 @@ when RENDER_BACKEND_OPENGL {
// TODO: SS - This procedure and _float ^^ are very similar and will do pretty much the same thing but with calls to different gl.Uniform-- procedures. Make it generic instead then switch on the type?
opengl_set_shader_uniform_matrix4f32 :: proc(program: ^Shader_Program, uniform: Uniform_Matrix4f32) -> bool {
gl.UseProgram(program.backend.handle)
opengl_activate_shader_program(program)
loc := gl.GetUniformLocation(program.backend.handle, fmt.ctprintf("%v", uniform.name))
if loc < 0 {
@@ -647,7 +644,7 @@ when RENDER_BACKEND_OPENGL {
}
opengl_set_shader_uniform_vector3 :: proc(program: ^Shader_Program, uniform: Uniform_Vector3) -> bool {
gl.UseProgram(program.backend.handle)
opengl_activate_shader_program(program)
loc := gl.GetUniformLocation(program.backend.handle, fmt.ctprintf("%v", uniform.name))
if loc < 0 {