0

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.

decitrig
  • 778
  • 5
  • 23

1 Answers1

1

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

decitrig
  • 778
  • 5
  • 23
  • For posterity: in C/C++, SDL and GLFW can load function pointers on behalf of other code, since they can reach outside of their own namespaces. In go, that's not possible so `gl` needs to explicitly initialize its own function pointers. – decitrig Sep 20 '21 at 10:53