// SetUniform sets a uniform variable using the appropriate glUniform* or glUniformMatrix* call. It supports arrays of float32 and float64 or Mat4 objects. // NB: The underlying API does not support double precision, being able to pass float64 values is for convenience only. // BUG: It does not support non-square matrices. func (p *Program) SetUniform(loc string, data interface{}) { uni := p.uni[loc] switch f := data.(type) { case float32: C.glUniform1f(uni, C.GLfloat(f)) case [1]float32: C.glUniform1f(uni, C.GLfloat(f[0])) case [2]float32: C.glUniform2f(uni, C.GLfloat(f[0]), C.GLfloat(f[1])) case [3]float32: C.glUniform3f(uni, C.GLfloat(f[0]), C.GLfloat(f[1]), C.GLfloat(f[2])) case [4]float32: C.glUniform4f(uni, C.GLfloat(f[0]), C.GLfloat(f[1]), C.GLfloat(f[2]), C.GLfloat(f[3])) case float64: C.glUniform1f(uni, C.GLfloat(f)) case [1]float64: C.glUniform1f(uni, C.GLfloat(f[0])) case [2]float64: C.glUniform2f(uni, C.GLfloat(f[0]), C.GLfloat(f[1])) case [3]float64: C.glUniform3f(uni, C.GLfloat(f[0]), C.GLfloat(f[1]), C.GLfloat(f[2])) case [4]float64: C.glUniform4f(uni, C.GLfloat(f[0]), C.GLfloat(f[1]), C.GLfloat(f[2]), C.GLfloat(f[3])) case int: C.glUniform1i(uni, C.GLint(f)) case [1]int: C.glUniform1i(uni, C.GLint(f[0])) case [2]int: C.glUniform2i(uni, C.GLint(f[0]), C.GLint(f[1])) case [3]int: C.glUniform3i(uni, C.GLint(f[0]), C.GLint(f[1]), C.GLint(f[2])) case [4]int: C.glUniform4i(uni, C.GLint(f[0]), C.GLint(f[1]), C.GLint(f[2]), C.GLint(f[3])) case [2][2]float32: g := [4]C.GLfloat{C.GLfloat(f[0][0]), C.GLfloat(f[1][0]), C.GLfloat(f[0][1]), C.GLfloat(f[1][1])} C.glUniformMatrix2fv(uni, 1, FALSE, &g[0]) case [2][2]float64: g := [4]C.GLfloat{C.GLfloat(f[0][0]), C.GLfloat(f[1][0]), C.GLfloat(f[0][1]), C.GLfloat(f[1][1])} C.glUniformMatrix2fv(uni, 1, FALSE, &g[0]) case [3][3]float32: g := [9]C.GLfloat{C.GLfloat(f[0][0]), C.GLfloat(f[1][0]), C.GLfloat(f[2][0]), C.GLfloat(f[0][1]), C.GLfloat(f[1][1]), C.GLfloat(f[2][1]), C.GLfloat(f[0][2]), C.GLfloat(f[1][2]), C.GLfloat(f[2][2])} C.glUniformMatrix3fv(uni, 1, FALSE, &g[0]) case [3][3]float64: g := [9]C.GLfloat{C.GLfloat(f[0][0]), C.GLfloat(f[1][0]), C.GLfloat(f[2][0]), C.GLfloat(f[0][1]), C.GLfloat(f[1][1]), C.GLfloat(f[2][1]), C.GLfloat(f[0][2]), C.GLfloat(f[1][2]), C.GLfloat(f[2][2])} C.glUniformMatrix3fv(uni, 1, FALSE, &g[0]) case [4][4]float32: g := [16]C.GLfloat{C.GLfloat(f[0][0]), C.GLfloat(f[1][0]), C.GLfloat(f[2][0]), C.GLfloat(f[3][0]), C.GLfloat(f[0][1]), C.GLfloat(f[1][1]), C.GLfloat(f[2][1]), C.GLfloat(f[3][1]), C.GLfloat(f[0][2]), C.GLfloat(f[1][2]), C.GLfloat(f[2][2]), C.GLfloat(f[3][2]), C.GLfloat(f[0][3]), C.GLfloat(f[1][3]), C.GLfloat(f[2][3]), C.GLfloat(f[3][3])} C.glUniformMatrix4fv(uni, 1, FALSE, &g[0]) case [4][4]float64: g := [16]C.GLfloat{C.GLfloat(f[0][0]), C.GLfloat(f[1][0]), C.GLfloat(f[2][0]), C.GLfloat(f[3][0]), C.GLfloat(f[0][1]), C.GLfloat(f[1][1]), C.GLfloat(f[2][1]), C.GLfloat(f[3][1]), C.GLfloat(f[0][2]), C.GLfloat(f[1][2]), C.GLfloat(f[2][2]), C.GLfloat(f[3][2]), C.GLfloat(f[0][3]), C.GLfloat(f[1][3]), C.GLfloat(f[2][3]), C.GLfloat(f[3][3])} C.glUniformMatrix4fv(uni, 1, FALSE, &g[0]) case Mat4: g := [16]C.GLfloat{C.GLfloat(f[0][0]), C.GLfloat(f[1][0]), C.GLfloat(f[2][0]), C.GLfloat(f[3][0]), C.GLfloat(f[0][1]), C.GLfloat(f[1][1]), C.GLfloat(f[2][1]), C.GLfloat(f[3][1]), C.GLfloat(f[0][2]), C.GLfloat(f[1][2]), C.GLfloat(f[2][2]), C.GLfloat(f[3][2]), C.GLfloat(f[0][3]), C.GLfloat(f[1][3]), C.GLfloat(f[2][3]), C.GLfloat(f[3][3])} C.glUniformMatrix4fv(uni, 1, FALSE, &g[0]) default: panic("invalid type passed to SetUniform()") } }
func Uniform2f( location int32, x float32, y float32) { C.glUniform2f( C.GLint(location), C.GLfloat(x), C.GLfloat(y)) }
func Uniform2f(dst Uniform, v0, v1 float32) { defer func() { errstr := errDrain() log.Printf("gl.Uniform2f(%v, %v, %v) %v", dst, v0, v1, errstr) }() C.glUniform2f(dst.c(), C.GLfloat(v0), C.GLfloat(v1)) }
// Uniform specifies the value of a uniform variable for the current program object. func (u Uniform) Uniform(vls ...interface{}) { if len(vls) == 0 || len(vls) > 4 { panic("Uniform requires 1, 2, 3, or 4 values") } switch vls[0].(type) { case float32: switch len(vls) { case 1: C.glUniform1f(C.GLint(u), C.GLfloat(vls[0].(float32))) case 2: C.glUniform2f(C.GLint(u), C.GLfloat(vls[0].(float32)), C.GLfloat(vls[1].(float32))) case 3: C.glUniform3f(C.GLint(u), C.GLfloat(vls[0].(float32)), C.GLfloat(vls[1].(float32)), C.GLfloat(vls[2].(float32))) case 4: C.glUniform4f(C.GLint(u), C.GLfloat(vls[0].(float32)), C.GLfloat(vls[1].(float32)), C.GLfloat(vls[2].(float32)), C.GLfloat(vls[3].(float32))) } case int: switch len(vls) { case 1: C.glUniform1i(C.GLint(u), C.GLint(vls[0].(int))) case 2: C.glUniform2i(C.GLint(u), C.GLint(vls[0].(int)), C.GLint(vls[1].(int))) case 3: C.glUniform3i(C.GLint(u), C.GLint(vls[0].(int)), C.GLint(vls[1].(int)), C.GLint(vls[2].(int))) case 4: C.glUniform4i(C.GLint(u), C.GLint(vls[0].(int)), C.GLint(vls[1].(int)), C.GLint(vls[2].(int)), C.GLint(vls[3].(int))) } default: panic("Uniform only accepts int and float32 typed parameters") } }
func (game *game) drawFrame() { time += .05 color := (C.GLclampf(math.Sin(time)) + 1) * .5 C.glUniform2f(C.GLint(game.offsetUni), C.GLfloat(game.offsetX), C.GLfloat(game.offsetY)) C.glUniform3f(C.GLint(game.colorUni), 1.0, C.GLfloat(color), 0) C.glClear(C.GL_COLOR_BUFFER_BIT | C.GL_DEPTH_BUFFER_BIT) C.glUseProgram(game.prog) C.glDrawArrays(C.GL_TRIANGLES, 0, 3) }
func Uniform2f(dst Uniform, v0, v1 float32) { C.glUniform2f(dst.c(), C.GLfloat(v0), C.GLfloat(v1)) }
func (location UniformLocation) Uniform2f(x float32, y float32) { C.glUniform2f(C.GLint(location), C.GLfloat(x), C.GLfloat(y)) }
func (u Uniform2f) Set(x, y float32) { C.glUniform2f(u.location, C.GLfloat(x), C.GLfloat(y)) }