1
我有一個程序,它最初的工作原理是使用go-go OpenGL封裝器爲Go繪製三角形。在玩代碼的過程中,事情開始變得怪異。有時形狀會被渲染,然後它就不會。有時保存文件然後再次運行代碼會有效,有時也會失敗。在這個過程中,文件從工作到破碎沒有任何改變。 glfw窗口仍然以背景顏色顯示,並使用我使用的頂點數組。我不確定這是否是我的代碼中的簡單錯誤,或者是否與硬件有關。Go go-gl OpenGL渲染問題
不知道這是否有幫助,但我使用最新的Atom編輯器和Go-Plus插件。先謝謝您的幫助!
package main
import (
"fmt"
"log"
"runtime"
"github.com/go-gl/gl/v4.1-core/gl"
"github.com/go-gl/glfw/v3.1/glfw"
)
const vertexSource = `
#version 330
in vec2 position;
void main() {
gl_Position = vec4(position,0.0, 1.0);
}
` + "\x00"
const fragmentSource = `
#version 330
uniform vec3 triangleColor;
out vec4 outputColor;
void main() {
outputColor = vec4(triangleColor,1.0);
}
` + "\x00"
var vao, vbo uint32
var vertices = []float32{
0.0, 0.5,
0.5, -0.5,
-0.5, -0.5,
}
func init() {
runtime.LockOSThread()
}
func main() {
if err := glfw.Init(); err != nil {
log.Fatalln("failed to initalize GL window:", err)
}
defer glfw.Terminate()
glfw.WindowHint(glfw.Resizable, glfw.True)
glfw.WindowHint(glfw.ContextVersionMajor, 4)
glfw.WindowHint(glfw.ContextVersionMinor, 1)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
windowHeight := 800
windowWidth := 800
window, err := glfw.CreateWindow(windowWidth, windowHeight, "HelloGL2.0", nil, nil)
if err != nil {
panic(err)
}
window.MakeContextCurrent()
if err := gl.Init(); err != nil {
panic(err)
} else {
fmt.Println("OpenGL Version:", gl.GoStr(gl.GetString(gl.VERSION)))
}
gl.GenVertexArrays(1, &vao)
gl.BindVertexArray(vao)
gl.GenBuffers(1, &vbo)
gl.BindBuffer(gl.ARRAY_BUFFER, vbo)
gl.BufferData(gl.ARRAY_BUFFER, len(vertices), gl.Ptr(vertices), gl.STREAM_DRAW)
vertexShader := gl.CreateShader(gl.VERTEX_SHADER)
vcsources, free := gl.Strs(vertexSource)
gl.ShaderSource(vertexShader, 1, vcsources, nil)
free()
gl.CompileShader(vertexShader)
fragmentShader := gl.CreateShader(gl.FRAGMENT_SHADER)
fcsources, free := gl.Strs(fragmentSource)
gl.ShaderSource(fragmentShader, 1, fcsources, nil)
gl.CompileShader(fragmentShader)
shaderProgram := gl.CreateProgram()
gl.AttachShader(shaderProgram, vertexShader)
gl.AttachShader(shaderProgram, fragmentShader)
gl.BindFragDataLocation(shaderProgram, 0, gl.Str("outputColor\x00"))
gl.LinkProgram(shaderProgram)
gl.UseProgram(shaderProgram)
posAttrib := uint32(gl.GetAttribLocation(shaderProgram, gl.Str("position\x00")))
gl.EnableVertexAttribArray(posAttrib)
gl.VertexAttribPointer(posAttrib, 2.0, gl.FLOAT, false, 0.0, gl.PtrOffset(0))
gl.GetUniformLocation(shaderProgram, gl.Str("triangleColor\x00"))
//GL_STATIC_DRAW: The vertex data will be uploaded once and drawn many times (e.g. the world).
//GL_DYNAMIC_DRAW: The vertex data will be changed from time to time, but drawn many times more than that.
//GL_STREAM_DRAW: The vertex data will change almost every time it's drawn (e.g. user interface).
for !window.ShouldClose() {
//gl.Uniform3f(uniColor, 2.0, 0.0, 1.0)
gl.ClearColor(0.9, 1.0, 0.3, 1.0)
gl.Clear(gl.COLOR_BUFFER_BIT)
gl.DrawArrays(gl.TRIANGLES, 0, 3)
window.SwapBuffers()
glfw.PollEvents()
}
gl.DeleteProgram(shaderProgram)
gl.DeleteShader(fragmentShader)
gl.DeleteShader(vertexShader)
gl.DeleteBuffers(1, &vbo)
gl.DeleteVertexArrays(1, &vao)
fmt.Println("Exiting!")
window.Destroy()
}