PyOpengl 加载的顶点数据不通过 VBO 显示

PyOpengl loaded vertex data does not display via VBO

我正在尝试使用 pyopengl VBO 显示 STL 文件,但是当我尝试使用少量顶点列表时,它工作正常。但是当用完全相同的代码从 STL 文件加载的非常长的数据尝试它时,它什么都不显示。

例如, 以简单的顶点数据作为列表

测试 = [0.5, -0.5, 0.0, 1.0, 0.0, 0.0, -0.5, -0.5, 0.0, 0.0, 1.0, 0.0, 0.0,0.5,0.0,0.0,0.0,1.0]

但是数据量很大,什么也没有显示。

测试 = [0.5, -0.5, 1111.0, 1.0, 0.0, 0.0, -0.5, -11111.5, 0.0, 0.0, 1.0, 0.0, 0.0,0.5,0.0,0.0,0.0,1.0]

有人知道这个问题吗?

import glfw

from OpenGL.GL import *

import OpenGL.GL.shaders as shaders

import numpy

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

    window = glfw.create_window(800, 600, "vbo test", None, None)

    if not window:
        glfw.terminate()
        return
    glfw.make_context_current(window)


    test = [0.5, -0.5, 0.0,        1.0, 0.0, 0.0,
            -0.5, -0.5, 0.0,      0.0, 1.0, 0.0,
            0.0,0.5, 0.0,       0.0, 0.0, 1.0 ]


    triangle = numpy.array(list(map(float,test)), dtype=numpy.float32)

    vertex_shader_source = """
                            #version 330 core
                            in vec3 vPosition;
                            in vec3 vColor;

                            out vec3 color;

                            uniform mat4 ModelViewProjectionMatrix;

                            void main()
                            {
                                color = vColor;

                                gl_Position = vec4(vPosition,1.0f) ;
                            }
                            """
    fragment_shader_cource = """
                            #version 330 core
                            in vec3 color;
                            
                            out vec4 outColor;
                            void main()
                            {
                                outColor = vec4(color,1.0f);
                            }
                             """



    vertex_shader = shaders.compileShader(vertex_shader_source, GL_VERTEX_SHADER)
    fragment_shader = shaders.compileShader(fragment_shader_cource, GL_FRAGMENT_SHADER)
    shader = shaders.compileProgram(vertex_shader, fragment_shader)

    VBO = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, VBO)
    glBufferData(GL_ARRAY_BUFFER, triangle.itemsize * triangle.size, triangle, GL_STATIC_DRAW)

    position = glGetAttribLocation(shader, "vPosition")
    glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 4*6, ctypes.c_void_p(0))
    glEnableVertexAttribArray(position)

    color = glGetAttribLocation(shader,"vColor")
    glVertexAttribPointer(color,3,GL_FLOAT,GL_FALSE, 4*6, ctypes.c_void_p(4*3))
    glEnableVertexAttribArray(color)
    glUseProgram(0)
    glUseProgram(shader)
    glClearColor(0, 0, 0, 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()

查看 OpenGL Development on OS X 并创建核心配置文件 OpenGL 3.3 上下文:

glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
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, "vbo test", None, None)

使用核心配置文件上下文,您需要创建 Vertex Array Object:

VAO = glGenVertexArrays(1)
glBindVertexArray(VAO)

完整示例:

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

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

    glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
    glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
    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, "vbo test", None, None)

    if not window:
        glfw.terminate()
        return
    glfw.make_context_current(window)


    test = [0.5, -0.5, 0.0,        1.0, 0.0, 0.0,
            -0.5, -0.5, 0.0,      0.0, 1.0, 0.0,
            0.0,0.5, 0.0,       0.0, 0.0, 1.0 ]


    triangle = numpy.array(list(map(float,test)), dtype=numpy.float32)

    vertex_shader_source = """
                            #version 330 core
                            in vec3 vPosition;
                            in vec3 vColor;

                            out vec3 color;

                            uniform mat4 ModelViewProjectionMatrix;

                            void main()
                            {
                                color = vColor;

                                gl_Position = vec4(vPosition,1.0f) ;
                            }
                            """
    fragment_shader_cource = """
                            #version 330 core
                            in vec3 color;
                            
                            out vec4 outColor;
                            void main()
                            {
                                outColor = vec4(color,1.0f);
                            }
                             """

    VAO = glGenVertexArrays(1)
    glBindVertexArray(VAO)

    vertex_shader = shaders.compileShader(vertex_shader_source, GL_VERTEX_SHADER)
    fragment_shader = shaders.compileShader(fragment_shader_cource, GL_FRAGMENT_SHADER)
    shader = shaders.compileProgram(vertex_shader, fragment_shader)

    VBO = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, VBO)
    glBufferData(GL_ARRAY_BUFFER, triangle.itemsize * triangle.size, triangle, GL_STATIC_DRAW)

    position = glGetAttribLocation(shader, "vPosition")
    glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 4*6, ctypes.c_void_p(0))
    glEnableVertexAttribArray(position)

    color = glGetAttribLocation(shader,"vColor")
    glVertexAttribPointer(color,3,GL_FLOAT,GL_FALSE, 4*6, ctypes.c_void_p(4*3))
    glEnableVertexAttribArray(color)
    glUseProgram(0)
    glUseProgram(shader)
    glClearColor(0, 0, 0, 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()