Пример #1
0
Файл: gl.go Проект: extrame/gl
// 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()")
	}
}
Пример #2
0
func Uniform1f(dst Uniform, v float32) {
	defer func() {
		errstr := errDrain()
		log.Printf("gl.Uniform1f(%v, %v) %v", dst, v, errstr)
	}()
	C.glUniform1f(dst.c(), C.GLfloat(v))
}
Пример #3
0
// 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")
	}
}
Пример #4
0
func Uniform1f(dst Uniform, v float32) {
	C.glUniform1f(dst.c(), C.GLfloat(v))
}
Пример #5
0
func Uniform1f(
	location int32, x float32) {
	C.glUniform1f(
		C.GLint(location),
		C.GLfloat(x))
}
Пример #6
0
func (location UniformLocation) Uniform1f(x float32) {
	C.glUniform1f(C.GLint(location), C.GLfloat(x))
}
Пример #7
0
func (u Uniform1f) Set(x float32) {
	C.glUniform1f(u.location, C.GLfloat(x))
}