Beispiel #1
0
func initResources() {
	var err os.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
	program = gl.CreateProgram()
	program.AttachShader(vs)
	program.AttachShader(fs)
	program.Link()
	infoLog := program.GetInfoLog()
	if len(infoLog) != 0 {
		fmt.Printf("Program: %s\n", infoLog)
	}

	// Generate a buffer for the VertexBufferObject
	vboTriangle = gl.GenBuffer()
	vboTriangle.Bind(gl.ARRAY_BUFFER)
	// Submit the vertices of the triangle to the graphic card
	gl.BufferData(gl.ARRAY_BUFFER, len(triangleAttributes)*4, triangleAttributes, gl.STATIC_DRAW)
	// Unset the active buffer
	gl.Buffer(0).Bind(gl.ARRAY_BUFFER)

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

	attributeName = "v_color"
	attributeColor = program.GetAttribLocation(attributeName)
	if attributeColor == -1 {
		fmt.Printf("Could not bind attribute %s\n", attributeName)
	}

	uniformName := "fade"
	uniformFade = program.GetUniformLocation(uniformName)
	if uniformFade == -1 {
		fmt.Printf("Could not bind uniform %s\n", uniformName)
	}
}
Beispiel #2
0
func initResources() {
	var err os.Error
	// Load shaders
	vs, err = createShader("cube.v.glsl", VertexShaderType)
	if err != nil {
		fmt.Printf("Shader: %s\n", err)
		return
	}
	fs, err = createShader("cube.f.glsl", FragmentShaderType)
	if err != nil {
		fmt.Printf("Shader: %s\n", err)
		return
	}

	// Create GLSL program with loaded shaders
	program = gl.CreateProgram()
	program.AttachShader(vs)
	program.AttachShader(fs)
	program.Link()
	infoLog := program.GetInfoLog()
	if len(infoLog) != 0 {
		fmt.Printf("Program: %s\n", infoLog)
	}
	for i := 1; i < 6; i++ {
		cubeTexCoords = append(cubeTexCoords, cubeTexCoords...)
	}

	vboCubeTexCoords = gl.GenBuffer()
	vboCubeTexCoords.Bind(gl.ARRAY_BUFFER)
	gl.BufferData(gl.ARRAY_BUFFER, len(cubeTexCoords)*4, cubeTexCoords, gl.STATIC_DRAW)
	gl.Buffer(0).Bind(gl.ARRAY_BUFFER)

	vboCubeVertices = gl.GenBuffer()
	vboCubeVertices.Bind(gl.ARRAY_BUFFER)
	gl.BufferData(gl.ARRAY_BUFFER, len(cubeVertices)*4, cubeVertices, gl.STATIC_DRAW)
	gl.Buffer(0).Bind(gl.ARRAY_BUFFER)

	// Generate a buffer for the IndexBufferObject
	iboCubeElements = gl.GenBuffer()
	iboCubeElements.Bind(gl.ELEMENT_ARRAY_BUFFER)
	// Submit the indexes to the graphic card
	gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, len(cubeElements)*2, cubeElements, gl.STATIC_DRAW)
	// Unset the active buffer
	gl.Buffer(0).Bind(gl.ELEMENT_ARRAY_BUFFER)

	// Get the attribute location from the GLSL program (here from the vertex shader)
	attributeName := "coord3d"
	attributeCoord3d = program.GetAttribLocation(attributeName)
	if attributeCoord3d == -1 {
		fmt.Printf("Could not bind attribute %s\n", attributeName)
	}

	attributeName = "texcoord"
	attributeTexCoord = program.GetAttribLocation(attributeName)
	if attributeTexCoord == -1 {
		fmt.Printf("Could not bind attribute %s\n", attributeName)
	}

	uniformName := "mvp"
	uniformMTransform = program.GetUniformLocation(uniformName)
	if uniformMTransform == -1 {
		fmt.Printf("Could not bind attribute %s\n", uniformName)
	}

	uniformName = "mytexture"
	uniformTexture = program.GetUniformLocation(uniformName)
	if uniformTexture == -1 {
		fmt.Printf("Could not bind attribute %s\n", uniformName)
	}

	// Load texture
	texture, err = openSurface("texture.jpg")
	if err != nil {
		fmt.Printf("Texture: %s\n", err)
		return
	}
}