Ejemplo n.º 1
0
func (s *gl32Shader) buildUniformList() {
	s.uniforms = make(Parameters, 4)

	s.PreRender(nil)

	var uniformCount gl.Int
	gl.GetProgramiv(s.program, gl.ACTIVE_UNIFORMS, &uniformCount)
	uniformName := gl.GLStringAlloc(128)
	defer gl.GLStringFree(uniformName)

	textureUnit := 0

	for i := 0; i < int(uniformCount); i++ {
		var uniformType gl.Enum
		var uniformSize gl.Int
		var nameLength gl.Sizei

		gl.GetActiveUniform(s.program, gl.Uint(i), 128, &nameLength, &uniformSize, &uniformType, uniformName)

		// Ignore build in uniforms
		if !strings.HasPrefix(gl.GoString(uniformName), "gl_") {
			location := gl.GetUniformLocation(s.program, uniformName)

			if uniformType == gl.SAMPLER_2D {
				gl.Uniform1i(location, gl.Int(textureUnit))
				textureUnit++
			}

			if validUniformTypes[uniformType] {
				s.uniforms = append(s.uniforms, newGL32Uniform(location, glToUniform[uniformType], gl.GoStringN(uniformName, nameLength)))
			}
		}
	}
}
Ejemplo n.º 2
0
func newGL32Shader(name, vertexCode, fragmentCode string) (*gl32Shader, error) {

	vertexShader, err := compileShader(vertexCode, gl.VERTEX_SHADER)
	if err != nil {
		return nil, err
	}

	fragmentShader, err := compileShader(fragmentCode, gl.FRAGMENT_SHADER)
	if err != nil {
		return nil, err
	}

	shader := &gl32Shader{name, gl.CreateProgram(), nil}
	gl.AttachShader(shader.program, vertexShader)
	gl.AttachShader(shader.program, fragmentShader)

	gl.BindAttribLocation(shader.program, ATTRIB_POSITION, gl.GLString("position"))
	gl.BindAttribLocation(shader.program, ATTRIB_NORMAL, gl.GLString("normal"))
	gl.BindAttribLocation(shader.program, ATTRIB_UV0, gl.GLString("uv0"))
	gl.BindAttribLocation(shader.program, ATTRIB_UV1, gl.GLString("uv1"))
	gl.BindAttribLocation(shader.program, ATTRIB_UV2, gl.GLString("uv2"))
	gl.BindAttribLocation(shader.program, ATTRIB_UV3, gl.GLString("uv3"))
	gl.BindAttribLocation(shader.program, ATTRIB_COLOR, gl.GLString("color"))

	gl.LinkProgram(shader.program)

	var status gl.Int
	gl.GetProgramiv(shader.program, gl.LINK_STATUS, &status)
	if status != 1 {
		gl.DeleteShader(vertexShader)
		gl.DeleteShader(fragmentShader)
		gl.DeleteProgram(shader.program)

		return nil, errors.New(fmt.Sprintf("Could not link shader program '%s'", name))
	}

	shader.buildUniformList()

	return shader, nil
}