如何在Windows上使用Go加载GL函数?

huangapple go评论65阅读模式
英文:

How do I get gl functions to load in go on windows?

问题

我正在尝试在Windows上使用go-gl,但加载gl函数时似乎出了问题。以下是我正在运行的代码:

package main

import (
	"log"
	"runtime"

	"github.com/go-gl/gl/v3.3-core/gl"
	"github.com/go-gl/glfw/v3.3/glfw"
)

func init() {
	runtime.LockOSThread()
}

func main() {
	if err := glfw.Init(); err != nil {
		log.Fatal(err)
	}
	defer glfw.Terminate()

	w, err := glfw.CreateWindow(800, 600, "Glfw window", nil, nil)
	if err != nil {
		log.Fatal(err)
	}
	w.MakeContextCurrent()
	for !w.ShouldClose() {
		gl.ClearColor(0, 0.1, 0, 1.0)
		gl.Clear(gl.COLOR_BUFFER_BIT)
		w.SwapBuffers()
		glfw.PollEvents()
	}
}

这个代码失败了:

Exception 0xc0000005 0x8 0x0 0x0
PC=0x0
signal arrived during external code execution

runtime.cgocall(0x7ff75869c4b0, 0xc000107f18)
        C:/Program Files/Go/src/runtime/cgocall.go:156 +0x4a fp=0xc000107ef0 sp=0xc000107eb8 pc=0x7ff758603a4a
github.com/go-gl/gl/v3.3-core/gl._Cfunc_glowClearColor(0x0, 0x0, 0x3dcccccd, 0x0, 0x3f800000)
        _cgo_gotypes.go:3907 +0x52 fp=0xc000107f18 sp=0xc000107ef0 pc=0x7ff758696cb2
github.com/go-gl/gl/v3.3-core/gl.ClearColor(...)
        C:/Users/rwsim/go/pkg/mod/github.com/go-gl/gl@v0.0.0-20210905235341-f7a045908259/v3.3-core/gl/package.go:8974
main.main()
        C:/Users/rwsim/go/src/github.com/decitrig/hellogl/wingl.go:27 +0x109 fp=0xc000107f80 sp=0xc000107f18 pc=0x7ff75869bae9
runtime.main()
        C:/Program Files/Go/src/runtime/proc.go:255 +0x217 fp=0xc000107fe0 sp=0xc000107f80 pc=0x7ff758637497
runtime.goexit()
        C:/Program Files/Go/src/runtime/asm_amd64.s:1581 +0x1 fp=0xc000107fe8 sp=0xc000107fe0 pc=0x7ff75865e861
rax     0x0
rbx     0xc000107f18
rcx     0xc000107f18
rdi     0xc000107f18
rsi     0x7ff7587c5200
rbp     0xc000107ee0
rsp     0x52e5fffa68
r8      0x7ff7587c53a0
r9      0x0
r10     0x8
r11     0x10
r12     0xc000107e08
r13     0x1
r14     0xc00003c000
r15     0xa
rip     0x0
rflags  0x10206
cs      0x33
fs      0x53
gs      0x2b

由于PC是0x0,我猜测某些东西无法加载OpenGL函数指针,因此对glClear的调用导致了段错误。一切都可以正常编译,只是无法调用OpenGL。

英文:

I'm trying to get go-gl to work in Windows, but something seems to be going wrong with loading the gl functions. Here's the code I'm running :

package main

import (
	"log"
	"runtime"

	"github.com/go-gl/gl/v3.3-core/gl"
	"github.com/go-gl/glfw/v3.3/glfw"
)

func init() {
	runtime.LockOSThread()
}

func main() {
	if err := glfw.Init(); err != nil {
		log.Fatal(err)
	}
	defer glfw.Terminate()

	w, err := glfw.CreateWindow(800, 600, "Glfw window", nil, nil)
	if err != nil {
		log.Fatal(err)
	}
	w.MakeContextCurrent()
	for !w.ShouldClose() {
		gl.ClearColor(0, 0.1, 0, 1.0)
		gl.Clear(gl.COLOR_BUFFER_BIT)
		w.SwapBuffers()
		glfw.PollEvents()
	}
}

This fails:

Exception 0xc0000005 0x8 0x0 0x0
PC=0x0
signal arrived during external code execution

runtime.cgocall(0x7ff75869c4b0, 0xc000107f18)
        C:/Program Files/Go/src/runtime/cgocall.go:156 +0x4a fp=0xc000107ef0 sp=0xc000107eb8 pc=0x7ff758603a4a
github.com/go-gl/gl/v3.3-core/gl._Cfunc_glowClearColor(0x0, 0x0, 0x3dcccccd, 0x0, 0x3f800000)
        _cgo_gotypes.go:3907 +0x52 fp=0xc000107f18 sp=0xc000107ef0 pc=0x7ff758696cb2
github.com/go-gl/gl/v3.3-core/gl.ClearColor(...)
        C:/Users/rwsim/go/pkg/mod/github.com/go-gl/gl@v0.0.0-20210905235341-f7a045908259/v3.3-core/gl/package.go:8974
main.main()
        C:/Users/rwsim/go/src/github.com/decitrig/hellogl/wingl.go:27 +0x109 fp=0xc000107f80 sp=0xc000107f18 pc=0x7ff75869bae9
runtime.main()
        C:/Program Files/Go/src/runtime/proc.go:255 +0x217 fp=0xc000107fe0 sp=0xc000107f80 pc=0x7ff758637497
runtime.goexit()
        C:/Program Files/Go/src/runtime/asm_amd64.s:1581 +0x1 fp=0xc000107fe8 sp=0xc000107fe0 pc=0x7ff75865e861
rax     0x0
rbx     0xc000107f18
rcx     0xc000107f18
rdi     0xc000107f18
rsi     0x7ff7587c5200
rbp     0xc000107ee0
rsp     0x52e5fffa68
r8      0x7ff7587c53a0
r9      0x0
r10     0x8
r11     0x10
r12     0xc000107e08
r13     0x1
r14     0xc00003c000
r15     0xa
rip     0x0
rflags  0x10206
cs      0x33
fs      0x53
gs      0x2b

Since PC is 0x0, I'm guessing something is failing to load the OpenGL function pointers and so the call to glClear is segfaulting. Everything compiles just fine, it just can't call into opengl.

答案1

得分: 1

啊哈,一个愚蠢的错误。缺少对gl.Init()的调用。

英文:

Aha, a silly mistake. Missing a call to gl.Init().

huangapple
  • 本文由 发表于 2021年9月20日 09:45:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/69248332.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定