drxnfdx798517235 2019-01-04 23:17
浏览 192

OpenGL和SDL2无法打开窗口

I am trying to learn how to use OpenGL and SDL2 but when I run the simple program posted below, the window opens and instantly closes with the following message: exit status 3221225477.

main.go

package main

import (
    "fmt"

    "github.com/go-gl/gl/v4.1-core/gl"
    "github.com/veandco/go-sdl2/sdl"
)

var (
    window  *sdl.Window
    context sdl.GLContext
)

func main() {
    // Initialize sdl and create context
    initialize()

    // Initialize OpenGL pointers
    gl.Init()

    // Setup opengl attributes
    setOpenGLAttributes()

    // This makes our buffer swap syncronized with the monitor's vertical refresh
    sdl.GLSetSwapInterval(1)

    // Run and clean
    run()
    clean()
}

func initialize() {
    sdl.Init(sdl.INIT_EVERYTHING)

    window, err := sdl.CreateWindow("Test", sdl.WINDOWPOS_CENTERED, sdl.WINDOWPOS_CENTERED, 1280, 720, sdl.WINDOW_OPENGL)
    if err != nil {
        fmt.Println(err)
        return
    }

    context, err = window.GLCreateContext()
    if err != nil {
        fmt.Println(err)
        return
    }
}

func setOpenGLAttributes() {
    // SDL_GL_CONTEXT_CORE gives us only the newer version, deprecated functions are disabled
    sdl.GLSetAttribute(sdl.GL_CONTEXT_PROFILE_MASK, sdl.GL_CONTEXT_PROFILE_CORE)

    // 3.2 is part of the modern versions of OpenGL, but most video cards whould be able to run it
    sdl.GLSetAttribute(sdl.GL_CONTEXT_MAJOR_VERSION, 3)
    sdl.GLSetAttribute(sdl.GL_CONTEXT_MINOR_VERSION, 2)

    // Turn on double buffering with a 24bit Z buffer.
    sdl.GLSetAttribute(sdl.GL_DOUBLEBUFFER, 1)
}

func run() {
    for {
        for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
            switch event.(type) {
            case *sdl.QuitEvent:
                return
            }
        }

        gl.ClearColor(1, 8, 20, 1.0)
        gl.Clear(gl.COLOR_BUFFER_BIT)
        window.GLSwap()
    }
}

func clean() {
    // Delete our OpengL context
    sdl.GLDeleteContext(context)

    // Destroy our window
    window.Destroy()

    // Shutdown SDL 2
    sdl.Quit()
}

What am I doing wrong? I was following this tutorial: http://headerphile.com/sdl2/opengl-part-1-sdl-opengl-awesome/ and it doesn't look like I missed anything converting it to Go.

EDIT: So I added gl.Init() before setting the OpenGL attributes. I still get the same behavior, but the program keeps running. The window is invisible (cannot see it in the task bar) and the only way to quit is by pressing CTRL+C in the terminal. Am I missing some other function call?

  • 写回答

1条回答 默认 最新

  • douzhong3887 2019-01-05 00:42
    关注

    You can see a similar crash in ggez/ggez issue 141 (not related to Go, actually in Rust with OpenGL)

    The point is: check first your driver. That same issue mentioned:

    I probably find the reason.
    I updated my integrated graphics card driver, and everything is OK.
    I do not know why, but now everything can work properly.

    My device Info:

    • Intel(R) HD Graphics Family -> I updated this driver
    • AMD Radeon HD 8850M
    评论

报告相同问题?

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch