[go + SDL + OpenGL + MacOS示例=在DrawArrays()之后我得到了INVALID_OPERATION

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[go + SDL + OpenGL + MacOS示例=在DrawArrays()之后我得到了INVALID_OPERATION相关的知识,希望对你有一定的参考价值。

SDL版本:2.0.12OpenGL版本:3.3 INTEL-10.4.14我必须通过请求核心配置文件将OpenGL版本强制为3.3,否则我将获得的默认版本为1.2

按照此视频教程http://youtu.be/yxAPS2eyKmA 带有Go的游戏,我尝试了OpenGL示例代码,它仅显示一个黑色窗口。应该渲染一个橙色的三角形。

倒数第二个语句INVALID_OPERATION之后我得到gl.DrawArrays(gl.TRIANGLES, 0, 3)

https://gist.github.com/ErnestoBorio/87b09bf3cc496657a83637fd4876e943

    package main

    import (
        "fmt"
        "strings"

        "github.com/go-gl/gl/v3.3-core/gl"
        "github.com/veandco/go-sdl2/sdl"
    )

    func DontPanic(err error) {
        if err != nil {
            panic(err)
        }
    }

    var GLversion = [...]int {3, 3}

    func main() {
        DontPanic(
            sdl.Init(sdl.INIT_EVERYTHING))
        defer sdl.Quit()

        DontPanic(
            sdl.GLSetAttribute(sdl.GL_CONTEXT_PROFILE_MASK, sdl.GL_CONTEXT_PROFILE_CORE))
        DontPanic(
            sdl.GLSetAttribute(sdl.GL_CONTEXT_MAJOR_VERSION, GLversion[0]))
        DontPanic(
            sdl.GLSetAttribute(sdl.GL_CONTEXT_MINOR_VERSION, GLversion[1]))

        window, err := sdl.CreateWindow("OpenGL", 200, 200, 1280, 720, sdl.WINDOW_OPENGL)
        DontPanic(err)

        window.GLCreateContext()
        defer window.Destroy()

        GLversionStr := fmt.Sprintf("%d%d0", GLversion[0], GLversion[1])

        gl.Init()
        version := gl.GoStr(gl.GetString(gl.VERSION))
        fmt.Println("OpenGL Version", version)

        vertexShaderSrc :=
            "#version "+ GLversionStr +" core 
"+
            "layout (location = 0) in vec3 aPos;"+
            "void main() {"+
                "gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);"+
            "}x00"

        vertexShader := gl.CreateShader(gl.VERTEX_SHADER)
        csource, free := gl.Strs(vertexShaderSrc)
        gl.ShaderSource(vertexShader, 1, csource, nil)
        free()
        gl.CompileShader(vertexShader)
        var status int32
        gl.GetShaderiv(vertexShader, gl.COMPILE_STATUS, &status)
        if status == gl.FALSE {
            var logLength int32
            gl.GetShaderiv(vertexShader, gl.INFO_LOG_LENGTH, &logLength)
            log := strings.Repeat("x00", int(logLength+1))
            gl.GetShaderInfoLog(vertexShader, logLength, nil, gl.Str(log))
            panic("Failed to compile vertex shader:
" + log)
        }

        fragmentShaderSrc :=
            "#version "+ GLversionStr +" core 
"+
            "out vec4 FragColor;"+
            "void main() {"+
                "FragColor = vec4(1, 0.5, 0.2, 1);"+
            "}x00"

        fragmentShader := gl.CreateShader(gl.FRAGMENT_SHADER)
        csource, free = gl.Strs(fragmentShaderSrc)
        gl.ShaderSource(fragmentShader, 1, csource, nil)
        free()
        gl.CompileShader(fragmentShader)
        gl.GetShaderiv(fragmentShader, gl.COMPILE_STATUS, &status)
        if status == gl.FALSE {
            var logLength int32
            gl.GetShaderiv(fragmentShader, gl.INFO_LOG_LENGTH, &logLength)
            log := strings.Repeat("x00", int(logLength+1))
            gl.GetShaderInfoLog(fragmentShader, logLength, nil, gl.Str(log))
            panic("Failed to compile fragment shader:
" + log)
        }

        shaderPrg := gl.CreateProgram()
        gl.AttachShader(shaderPrg, vertexShader)
        gl.AttachShader(shaderPrg, fragmentShader)
        gl.LinkProgram(shaderPrg)
        gl.GetProgramiv(shaderPrg, gl.LINK_STATUS, &status)
        if status == gl.FALSE {
            panic("Error in Linker, error logging not yet implemented.")
        }
        gl.DeleteShader(vertexShader)
        gl.DeleteShader(fragmentShader)

        vertices := []float32{
            -.5, -.5, 0,
            .5, -.5, 0,
            0,  .5, 0,
        }

        var vertexBuffer uint32
        gl.GenBuffers(1, &vertexBuffer)
        gl.BindBuffer(gl.ARRAY_BUFFER, vertexBuffer)
        var vertexArray uint32
        gl.GenVertexArrays(1, &vertexArray)
        gl.BindVertexArray(vertexArray)

        gl.BufferData(gl.ARRAY_BUFFER, len(vertices)*4, gl.Ptr(vertices), gl.STATIC_DRAW)
        gl.VertexAttribPointer(0, 3, gl.FLOAT, false, 3*4, nil)
        gl.EnableVertexAttribArray(0)
        gl.BindVertexArray(0)

        EventPoll:
        for {
            for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
                switch event.(type) {
                    case *sdl.QuitEvent:
                        break EventPoll
                }
            }
            gl.ClearColor(0, 0, 0, 0)
            gl.Clear(gl.COLOR_BUFFER_BIT)
            gl.BindVertexArray(vertexArray)
            gl.DrawArrays(gl.TRIANGLES, 0, 3)
            window.GLSwap()
        }
    }
答案

您的代码从不调用glUseProgram,这意味着您尝试在没有当前程序对象的情况下进行绘制,这在核心配置文件OpenGL中无效。

以上是关于[go + SDL + OpenGL + MacOS示例=在DrawArrays()之后我得到了INVALID_OPERATION的主要内容,如果未能解决你的问题,请参考以下文章

SDL2+OpenGL 绘制多边形

SDL2+OpenGL 绘制立体图形

SDL_Renderer opengl 冲突

SDL/opengl 多重采样不起作用

SDL 无法创建 OpenGL 窗口

SDL/C++ OpenGL 程序,如何阻止 SDL 捕获 SIGINT