[LearnOpenGL.com具有“ Hello Window”教程here。
本教程的结尾是指向源代码here的链接。我试图将其移植到Go。根据教程,结果应该是一个具有不变的纯色填充的窗口。相反,我遇到了分段错误。堆栈跟踪指向此行:
gl.ClearColor(0.2, 0.3, 0.3, 1.0)
我的问题是在Go OpenGL程序中调用gl.ClearColor
之前是否必须设置一些我想念的GL状态,为什么C例子在这里起作用而Go例子却没有?
我正在Cednamon Spin的Fedora 31 Desktop上运行此代码。我的Go版本是1.13.7。
要复制的最小代码:
package main
import (
"fmt"
"log"
"runtime"
"github.com/go-gl/gl/v3.3-core/gl"
"github.com/go-gl/glfw/v3.3/glfw"
)
func init() {
runtime.LockOSThread()
}
func KeyHandler(w *glfw.Window, key glfw.Key, scan int, action glfw.Action, mods glfw.ModifierKey) {
if key == glfw.KeyEscape && action == glfw.Press {
fmt.Println("Escape pressed")
w.SetShouldClose(true)
}
}
func Resize(w *glfw.Window, width int, height int) {
gl.Viewport(0, 0, int32(width), int32(height))
}
func main() {
err := glfw.Init()
if err != nil {
log.Fatalln(err)
}
defer glfw.Terminate()
glfw.WindowHint(glfw.ContextVersionMajor, 3)
glfw.WindowHint(glfw.ContextVersionMinor, 3)
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
wwidth, wheight := 800, 600
w, err := glfw.CreateWindow(wheight, wwidth, "example", nil, nil)
if err != nil {
log.Fatalf("Failed to create window: %s", err)
}
w.MakeContextCurrent()
w.SetFramebufferSizeCallback(Resize)
glfw.SwapInterval(1)
w.SetKeyCallback(KeyHandler)
for !w.ShouldClose() {
gl.ClearColor(0.2, 0.3, 0.3, 1.0)
gl.Clear(gl.COLOR_BUFFER_BIT)
w.SwapBuffers()
glfw.PollEvents()
}
}
堆栈跟踪:
fatal error: unexpected signal during runtime execution
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x0]
runtime stack:
runtime.throw(0x525f18, 0x2a)
/usr/local/go/src/runtime/panic.go:774 +0x72
runtime.sigpanic()
/usr/local/go/src/runtime/signal_unix.go:378 +0x47c
runtime.asmcgocall(0x4761f1, 0x1)
/usr/local/go/src/runtime/asm_amd64.s:659 +0x70
goroutine 1 [syscall, locked to thread]:
runtime.cgocall(0x4c51b0, 0xc000052eb8, 0x0)
/usr/local/go/src/runtime/cgocall.go:128 +0x5b fp=0xc000052e88 sp=0xc000052e50 pc=0x4275db
github.com/go-gl/gl/v3.3-core/gl._Cfunc_glowClearColor(0x0, 0x3e99999a3e4ccccd, 0x3f8000003e99999a)
_cgo_gotypes.go:3778 +0x45 fp=0xc000052eb8 sp=0xc000052e88 pc=0x4be1e5
github.com/go-gl/gl/v3.3-core/gl.ClearColor(...)
/home/jrefior/home/go/pkg/mod/github.com/go-gl/[email protected]/v3.3-core/gl/package.go:8700
main.main()
/home/jrefior/home/opengl/window01/main.go:46 +0x235 fp=0xc000052f60 sp=0xc000052eb8 pc=0x4c4915
runtime.main()
/usr/local/go/src/runtime/proc.go:203 +0x21e fp=0xc000052fe0 sp=0xc000052f60 pc=0x4505fe
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1357 +0x1 fp=0xc000052fe8 sp=0xc000052fe0 pc=0x478cc1
您没有正确初始化Go GL绑定。正如documentation明确指出的那样:
import "github.com/go-gl/gl/v3.3-core/gl" func main() { window := ... // Open a window. window.MakeContextCurrent() // Important! Call gl.Init only under the presence of an active OpenGL context, // i.e., after MakeContextCurrent. if err := gl.Init(); err != nil { log.Fatalln(err) } }
init函数将为query the function pointers for all GL functions(在Windows上取决于GL上下文,因此它需要当前的GL上下文)。请注意,您链接的教程仅通过gladLoadGLLoader
生成的代码将glad GL loader generator用于相同的目的。