gpt4 book ai didi

go - 最小的 Go OpenGL 程序段错误

转载 作者:行者123 更新时间:2023-12-01 20:08:47 24 4
gpt4 key购买 nike

LearnOpenGL.com 有一个“Hello Window”教程here .

教程末尾有源代码链接here .我试图将其移植到 Go 中。根据教程,结果应该是一个具有不变的平面颜色填充的窗口。相反,我得到了段错误。堆栈跟踪指向这一行:

gl.ClearColor(0.2, 0.3, 0.3, 1.0)

我的问题是是否有一些我错过的 GL 状态必须在调用 gl.ClearColor 之前设置。在 Go OpenGL 程序中,为什么 C 示例在这里可以工作,而 Go 示例却不能?

我在 Fedora 31 Desktop、Cinnamon Spin 上运行此代码。我的 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/gl@v0.0.0-20190320180904-bf2b1f2f34d7/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 绑定(bind)。作为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)
}
}


初始化函数将 query the function pointers for all GL functions (在 Windows 上依赖于 GL 上下文,因此它需要当前的 GL 上下文)。请注意,您链接的教程使用 gladLoadGLLoader出于同样的目的,只是通过 glad GL loader generator 生成的代码.

关于go - 最小的 Go OpenGL 程序段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60139238/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com