Search code examples
pythonopengl3dpyopengl

PyOpengl loaded vertex data does not display via VBO


I'm trying to display a STL file using pyopengl VBO but When i try it small amount of vertex list, it works fine. but When trying it with very long data which is loaded from STL file with exact same code, it displays nothing.

for example, with simple vertex data as list

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 ]

but with big amount of data is not showing anything.

test = [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 ]

does someone know about this issue?

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()

Solution

  • See OpenGL Development on OS X and create an Core profile OpenGL 3.3 Context:

    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)
    

    Using a core profile context, you need to create a Vertex Array Object:

    VAO = glGenVertexArrays(1)
    glBindVertexArray(VAO)
    

    Complete example:

    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()