Ejemplo n.º 1
0
func initResources() {
	var err error
	// Load shaders
	vs, err = createShader("triangle.v.glsl", VertexShaderType)
	if err != nil {
		fmt.Printf("Shader: %s\n", err)
		return
	}
	fs, err = createShader("triangle.f.glsl", FragmentShaderType)
	if err != nil {
		fmt.Printf("Shader: %s\n", err)
		return
	}

	// Create GLSL program with loaded shaders
	var compileOk gl.Int
	program = gl.CreateProgram()
	gl.AttachShader(program, vs)
	gl.AttachShader(program, fs)
	gl.LinkProgram(program)
	gl.GetProgramiv(program, gl.LINK_STATUS, &compileOk)
	if compileOk == 0 {
		fmt.Printf("Error in program.\n")
	}

	// Generate a buffer for the VertexBufferObject
	gl.GenBuffers(1, &vboTriangle)
	gl.BindBuffer(gl.ARRAY_BUFFER, vboTriangle)
	// Submit the vertices of the triangle to the graphic card
	gl.BufferData(gl.ARRAY_BUFFER, gl.Sizeiptr(len(triangleVertices)*4), gl.Pointer(&triangleVertices[0]), gl.STATIC_DRAW)
	// Unset the active buffer
	gl.BindBuffer(gl.ARRAY_BUFFER, 0)

	// Generate a buffer for the Color-VBO
	gl.GenBuffers(1, &vboTriangleColors)
	gl.BindBuffer(gl.ARRAY_BUFFER, vboTriangleColors)
	gl.BufferData(gl.ARRAY_BUFFER, gl.Sizeiptr(len(triangleColors)*4), gl.Pointer(&triangleColors[0]), gl.STATIC_DRAW)
	gl.BindBuffer(gl.ARRAY_BUFFER, 0)

	// Get the attribute location from the GLSL program (here from the vertex shader)
	attributeName := gl.GLString("coord2d")
	defer gl.GLStringFree(attributeName)
	attributeTemp := gl.GetAttribLocation(program, attributeName)
	if attributeTemp == -1 {
		fmt.Printf("Could not bind attribute %s\n", gl.GoString(attributeName))
	}
	attributeCoord2d = gl.Uint(attributeTemp)

	attributeName = gl.GLString("v_color")
	defer gl.GLStringFree(attributeName)
	attributeTemp = gl.GetAttribLocation(program, attributeName)
	if attributeTemp == -1 {
		fmt.Printf("Could not bind attribute %s\n", gl.GoString(attributeName))
	}
	attributeColor = gl.Uint(attributeTemp)
}
Ejemplo n.º 2
0
func createShader(name string, shaderType int) (gl.Uint, error) {
	data, err := fileRead(name)
	if err != nil {
		return 0, err
	}
	if len(data) == 0 {
		return 0, errors.New("No shader code.")
	}
	var shader gl.Uint
	switch shaderType {
	case VertexShaderType:
		shader = gl.CreateShader(gl.VERTEX_SHADER)
	case FragmentShaderType:
		shader = gl.CreateShader(gl.FRAGMENT_SHADER)
	default:
		return 0, errors.New("Unknown ShaderType.")
	}
	src := gl.GLStringArray(string(data))
	defer gl.GLStringArrayFree(src)
	gl.ShaderSource(shader, gl.Sizei(1), &src[0], nil)
	gl.CompileShader(shader)

	// Similar to print_log in the C code example
	var length gl.Int
	gl.GetShaderiv(shader, gl.INFO_LOG_LENGTH, &length)
	if length > 1 {
		glString := gl.GLStringAlloc(gl.Sizei(length))
		defer gl.GLStringFree(glString)
		gl.GetShaderInfoLog(shader, gl.Sizei(length), nil, glString)
		return 0, errors.New(fmt.Sprintf("Shader log: %s", gl.GoString(glString)))
	}
	return shader, nil
}
Ejemplo n.º 3
0
func (shader *Shader) GetInfoLog() string {
	length := shader.Get(gl33.INFO_LOG_LENGTH)
	log := gl33.GLStringAlloc(gl33.Sizei(length + 1))
	defer gl33.GLStringFree(log)
	gl33.GetShaderInfoLog(shader.Id, gl33.Sizei(length), nil, log)
	return gl33.GoString(log)
}
Ejemplo n.º 4
0
func (p *Program) GetUniformLocation(name string) (*UniformLocation, error) {
	attributeName := gl.GLString(name)
	defer gl.GLStringFree(attributeName)
	id := gl.GetUniformLocation(p.id, attributeName)
	if id == -1 {
		return nil, errors.New(fmt.Sprintf("Could not bind uniform %s\n", gl.GoString(attributeName)))
	}
	return &UniformLocation{id: id}, nil
}
Ejemplo n.º 5
0
func (p *Program) getError() error {
	var logLength gl.Int
	gl.GetProgramiv(p.id, gl.INFO_LOG_LENGTH, &logLength)
	log := gl.GLStringAlloc(gl.Sizei(logLength))
	defer gl.GLStringFree(log)
	gl.GetProgramInfoLog(p.id, gl.Sizei(logLength), nil, log)
	err := gl.GoString(log)
	return errors.New(err)
}
Ejemplo n.º 6
0
func (s *Shader) getError() error {
	var logLength gl.Int
	gl.GetShaderiv(s.id, gl.INFO_LOG_LENGTH, &logLength)
	log := gl.GLStringAlloc(gl.Sizei(logLength))
	defer gl.GLStringFree(log)
	gl.GetShaderInfoLog(s.id, gl.Sizei(logLength), nil, log)
	err := gl.GoString(log)
	return errors.New(err)
}
Ejemplo n.º 7
0
func (p *Program) GetAttributeLocation(name string) (*AttributeLocation, error) {
	attributeName := gl.GLString(name)
	defer gl.GLStringFree(attributeName)
	attributeTemp := gl.GetAttribLocation(p.id, attributeName)
	if attributeTemp == -1 {
		return nil, errors.New(fmt.Sprintf("Could not bind attribute %s\n", gl.GoString(attributeName)))
	}

	return &AttributeLocation{id: gl.Uint(attributeTemp)}, nil
}
Ejemplo n.º 8
0
func initResources() {
	var compileOk gl.Int
	// Vertex Shader
	vs = gl.CreateShader(gl.VERTEX_SHADER)
	vsSrc := gl.GLStringArray(vsSource)
	defer gl.GLStringArrayFree(vsSrc)
	gl.ShaderSource(vs, gl.Sizei(len(vsSrc)), &vsSrc[0], nil)
	gl.CompileShader(vs)
	gl.GetShaderiv(vs, gl.COMPILE_STATUS, &compileOk)
	if compileOk == 0 {
		errNum := gl.GetError()
		fmt.Printf("Error in vertex shader: %d\n", errNum)
	}

	// Fragment Shader
	fs = gl.CreateShader(gl.FRAGMENT_SHADER)
	fsSrc := gl.GLStringArray(fsSource)
	defer gl.GLStringArrayFree(fsSrc)
	gl.ShaderSource(fs, gl.Sizei(1), &fsSrc[0], nil)
	gl.CompileShader(fs)
	gl.GetShaderiv(fs, gl.COMPILE_STATUS, &compileOk)
	if compileOk == 0 {
		errNum := gl.GetError()
		fmt.Printf("Error in fragment shader: %d\n", errNum)
	}

	// GLSL program
	program = gl.CreateProgram()
	gl.AttachShader(program, vs)
	gl.AttachShader(program, fs)
	gl.LinkProgram(program)
	gl.GetProgramiv(program, gl.LINK_STATUS, &compileOk)
	if compileOk == 0 {
		fmt.Printf("Error in program.\n")

	}

	// Get the attribute location from the GLSL program (here from the vertex shader)
	attributeName := gl.GLString("coord2d")
	defer gl.GLStringFree(attributeName)
	attributeTemp := gl.GetAttribLocation(program, attributeName)
	if attributeTemp == -1 {
		fmt.Printf("Could not bind attribute %s\n", gl.GoString(attributeName))
	}
	attributeCoord2d = gl.Uint(attributeTemp)
}