How do I create an OpenGL 3.2 context in Go on MacOS (Lion-10.8)

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

How do I create an OpenGL 3.2 context in Go on MacOS (Lion-10.8)

问题

我正在尝试在MacOS上使用Go编写一个OpenGL应用程序,但无法弄清楚如何创建一个高于OpenGL 2.1版本的上下文。

我尝试使用了多个OpenGL绑定库,但最终选择了github.com/go-gl/gl

下面的示例将输出2.1 NVIDIA-8.12.47 310.40.00.05f01。我需要做什么来创建一个OpenGL 3.2上下文?

package main

import (
    "fmt"
    "github.com/go-gl/gl"
    glfw "github.com/go-gl/glfw3"
)

func main() {
    // 请求3.2上下文
    glfw.WindowHint(glfw.ContextVersionMajor, 3)
    glfw.WindowHint(glfw.ContextVersionMinor, 2)
    glfw.WindowHint(glfw.OpenglProfile, glfw.OpenglCoreProfile)
    if !glfw.Init() {
        panic("glfw初始化失败")
    }
    defer glfw.Terminate()

    // 创建并设置窗口上下文
    window, err := glfw.CreateWindow(64, 64, "foo", nil, nil)
    if err != nil {
        panic(err)
    }
    window.MakeContextCurrent()

    // 检查版本
    version := gl.GetString(gl.VERSION)
    fmt.Println(version)
}

希望对你有帮助!

英文:

I'm trying to write an OpenGL application in Go on MacOS, and can't figure out how to create a context with a version higher than OpenGL 2.1

I've tried using multiple OpenGL bindings out there, but settled on github.com/go-gl/gl

The below example will output 2.1 NVIDIA-8.12.47 310.40.00.05f01. What do I need to do to create an OpenGL 3.2 context?

package main

import (
    "fmt"
    "github.com/go-gl/gl"
    glfw "github.com/go-gl/glfw3"
)
func main() {
    //request 3.2 context
    glfw.WindowHint(glfw.ContextVersionMajor, 3)
    glfw.WindowHint(glfw.ContextVersionMinor, 2)
    glfw.WindowHint(glfw.OpenglProfile, glfw.OpenglCoreProfile)
    if !glfw.Init() {
        panic("glfw init failed")
    }
    defer glfw.Terminate()

    //create and set window context
    window, err := glfw.CreateWindow(64, 64, "foo", nil, nil)
    if err != nil {
        panic(err)
    }
    window.MakeContextCurrent()

    //check version
    version := gl.GetString(gl.VERSION)
    fmt.Println(version)
}

答案1

得分: 3

由于这是一个C库的包装器,你可能还需要在OS X上使用以下等效代码:

glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

但更重要的是,在调用任何其他GLFW3函数之前,你应该调用glfwInit(或其等效的glfw.Init)。据我所知,只有glfwSetErrorCallback可以在此调用之前使用。

英文:

Since this is a C library wrapper, you might also need the equivalent of:

glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

on OS X. But, more importantly, you should call glfwInit (or it's equivalent glfw.Init) prior to calling any other GLFW3 function. AFAIK, only glfwSetErrorCallback can be used prior to this call.

huangapple
  • 本文由 发表于 2013年9月24日 05:57:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/18969572.html
匿名

发表评论

匿名网友

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

确定