PyOpenGL MacO 上的 OpenGL 版本

PyOpenGL OpenGL Version on MacOs

我被告知要从其他 Whosebug posts 将旧版配置文件切换到核心配置文件,但我似乎找不到这样做的方法。所以我提出了一个更新的错误 post 来帮助我找到一个方法。

代码:

import glfw, numpy
from OpenGL.GL import *
import OpenGL.GL.shaders


def main():
    if not glfw.init():
        return

    window = glfw.create_window(800,600,"My OpenGL Window", None, None)

    if not window:
        glfw.terminate()
        return

    glfw.make_context_current(window)

    triangle = [-0.5, -0.5, 0.0,
                0.5, -0.5, 0.0,
                0.0, 0.5, 0.0]

    triangle = numpy.array(triangle, dtype = numpy.float32)

    vertex_shader = """
    #version 460
    in vec3 position;

    void main()
    {
        gl_Position = position;
    }

    """

    fragment_shader = """
    #version 460
    void main()
    {
        gl_FragColor = vec4(1.0f,0.0f,0.0f,1.0f);
    }
    """

    shader = OpenGL.GL.shaders.compileProgram(OpenGL.GL.shaders.compileShader(vertex_shader, GL_VERTEX_SHADER),
                                              OpenGL.GL.shaders.compileShader(fragment_shader,GL_FRAGMENT_SHADER))

    VBO = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, VBO)
    glBufferData(GL_ARRAY_BUFFER, 36, triangle, GL_STATIC_DRAW)

    position = glGetAttribLocation(shader, "position")
    glVertexAttribPoint(position, 3, GL_FLOAT, GL_FALSE, 0, None)
    glEnableVertexAttribArray(position)

    glUseProgram(shader)



    glClearColor(0.2, 0.3, 0.2, 1.0)
    while not glfw.window_should_close(window):
        glfw.poll_events()

        glClear(GL_COLOR_BUFFER_BIT)

        glDrawArrays(GL_TRIANGLES, 0, 3)

        glfw.swap_buffers(window)

    glfw.terminate()

if __name__ == "__main__":
    main()

我一直收到这个错误

Traceback (most recent call last):

File "/Users/Datboi/Desktop/Python/Opengl/main.py", line 71, in <module>
    main()
File "/Users/Datboi/Desktop/Python/Opengl/main.py", line 43, in main
    shader = OpenGL.GL.shaders.compileProgram(OpenGL.GL.shaders.compileShader(vertex_shader, GL_VERTEX_SHADER),
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/OpenGL/GL/shaders.py", line 226, in compileShader
    shaderType,
RuntimeError: ('Shader compile failure (0): b"ERROR: 0:2: \'\' :  version \'460\' is not supported\n"', [b'\n    #version 460\n    in vec3 position;\n    \n    void main()\n    {\n        gl_Position = position;\n    }\n\n    '], GL_VERTEX_SHADER)

我似乎找不到解决此问题的方法。我是 OpenGL/PyOpenGL 的新手,找不到任何问过大致相同问题的 post

ERROR: 0:2: \'\' : version \'460\' is not supported

是因为您在创建window之前没有指定OpenGL版本造成的。由于您的系统仅支持 OpenGL verison 4.1,因此您必须添加以下代码行。另见 OpenGL Development on OS X

glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 4)
glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 1)
glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, GL_TRUE)
glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
window = glfw.create_window(800,600,"My OpenGL Window", None, None)

如果你解决了那个问题,那么你将面临下一个问题:

虽然gl_Position is a Homogeneous coordinates,顶点着色器输入变量position的类型是vec3

gl_Position = position 的分配导致错误:

assignment of incompatible types

您可以通过更改顶点着色器来解决此问题。要么改变分配

#version 410 core
in vec3 position;

void main()
{
    gl_Position = vec4(position, 1.0);
}

改变输入变量的类型:

#version 410 core
in vec4 position;

void main()
{
    gl_Position = position;
}

由于核心版本 4.60 中弃用了 gl_FragColor,因此您必须声明一个片段着色器输出变量,而不是使用已弃用的内置输出变量 gl_FragColor:

 #version 410 core

 out vec4 frag_color; 

 void main()
 {
    frag_color = vec4(1.0f,0.0f,0.0f,1.0f);
 }

此外,您的程序中有错字。它必须是 glVertexAttribPointer 而不是 glVertexAttribPoint.

注意,在核心配置文件中 OpenGL Context, you have to use a Vertex Array Object,因为默认顶点数组对象 0 在核心配置文件中无效。

ok im getting this error now

kages/OpenGL/GL/shaders.py", line 108, in check_validate glGetProgramInfoLog( self ), RuntimeError: Validation failure (0): b'Validation Failed: No vertex array object bound.\n'

在 OpenGL 上下文变为当前上下文后创建并绑定顶点数组对象:

glfw.make_context_current(window)

VAO = glGenVertexArrays(1)
glBindVertexArray(VAO)

shader = OpenGL.GL.shaders.compileProgram(OpenGL.GL.shaders.compileShader(vertex_shader, GL_VERTEX_SHADER),
                                          OpenGL.GL.shaders.compileShader(fragment_shader,GL_FRAGMENT_SHADER))