加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Golang glfw中使用opengl库的问题

发布时间:2020-12-16 18:33:10 所属栏目:大数据 来源:网络整理
导读:最近在看看一个Golang的项目,里面使用了GLFW和GL来写UI,之前从来没有接触过这两个库,安装官方文档写了一个例子。 package mainimport ( "runtime" "github.com/go-gl/glfw/v3.1/glfw")func init() { // This is needed to arrange that main() runs on ma

最近在看看一个Golang的项目,里面使用了GLFW和GL来写UI,之前从来没有接触过这两个库,安装官方文档写了一个例子。

package main

import (
    "runtime"
    "github.com/go-gl/glfw/v3.1/glfw"
)

func init() {
    // This is needed to arrange that main() runs on main thread.
    // See documentation for functions that are only allowed to be called from the main thread.
    runtime.LockOSThread()
}

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

    window,err := glfw.CreateWindow(640,480,"Testing",nil,nil)
    if err != nil {
        panic(err)
    }

    window.MakeContextCurrent()

    for !window.ShouldClose() {
        // Do OpenGL stuff.
        window.SwapBuffers()
        glfw.PollEvents()
    }
}

这个例子可以直接run,一切都很正常。后来看到一个C库的例子,想照猫画一下,但是就遇到问题了。

按照这个例子添加了如下代码:

        gl.Begin(gl.TRIANGLES)

        gl.Color3f(1.0,0.0,0.0) //red
        gl.Vertex3f(0.0,1.0,0.0)

        gl.Color3f(0.0,0.0) //Green
        gl.Vertex3f(-1.0,-1.0,1.0) //blue
        gl.Vertex3f(1.0,0.0)

        gl.End()

直接运行会报错:

fatal error: unexpected signal during runtime execution
找了很久都找不到为什么会报这个错。
突然意识到go是面向对象的语言,glfw都使用了初始化的函数,gl会不会也一样了,添加初始化代码之后就正常了。真的是才会新手犯的错啊。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读