Skip to content

Instantly share code, notes, and snippets.

@thebirk
Created April 7, 2019 14:30
Show Gist options
  • Save thebirk/732e21eda4484f472092de204ee397f7 to your computer and use it in GitHub Desktop.
Save thebirk/732e21eda4484f472092de204ee397f7 to your computer and use it in GitHub Desktop.
TypeUniformMapping :: struct {
typetype: typeid,
set_uniform: proc(loc: i32, value: rawptr),
}
type_to_uniform_map := map[gl.Uniform_Type]TypeUniformMapping{
// If we map typeid to Uniform_Type, we could support stuff like array of floats and vec3s
gl.Uniform_Type.FLOAT = {typeid_of(f32), proc(loc: i32, value: rawptr) {
gl.Uniform1f(loc, (cast(^f32)value)^);
}},
gl.Uniform_Type.FLOAT_VEC3 = {typeid_of(math.Vec3), proc(loc: i32, value: rawptr) {
m := (cast(^math.Vec3)value)^;
gl.Uniform3f(loc, m.x, m.y, m.z);
}},
gl.Uniform_Type.FLOAT_MAT4 = {typeid_of(math.Mat4), proc(loc: i32, value: rawptr) {
m := (cast(^math.Mat4)value)^;
gl.UniformMatrix4fv(loc, 1, gl.FALSE, &m[0][0]);
}},
};
shader_set_uniform :: proc(shader: Shader, name: string, value: $T) {
u, ok := shader.uniforms[name];
if !ok {
log.error("Could not find and set uniform '%v'! Uniform could be inactive", name);
return;
}
mapping: TypeUniformMapping;
mapping, ok = type_to_uniform_map[u.kind];
if !ok {
log.error("Unsupported uniform type!");
return;
}
if mapping.typetype != typeid_of(T) {
log.error("Uniform '%v' has type '%v', trying to use type '%v'", name, mapping.typetype, typeid_of(T));
}
mapping.set_uniform(u.location, rawptr(&value));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment