Search code examples
pythonopenglpyopengl

Point position doesn't change even if the data in glBufferData change


code:

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

def main():
    if not glfw.init():
        raise RuntimeError('Can not initialize glfw library')
    window = glfw.create_window(500, 500, 'Demo', None, None)
    if not window:
        glfw.terminate()
        raise RuntimeError('Can not create glfw window')
    glfw.make_context_current(window)

    glClearColor(0, 0, 0, 1)
    glColor(1, 0, 0, 1)
    glPointSize(10.0)

    VBO = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, VBO)

    # The result of following two lines are looks the same
    # glBufferData(GL_ARRAY_BUFFER, np.array([0, 0, 0], dtype='float32'), GL_STATIC_DRAW)
    glBufferData(GL_ARRAY_BUFFER, np.array([999999999, 999999999, 999999999], dtype='float32'), GL_STATIC_DRAW)

    while not glfw.window_should_close(window):
        glClear(GL_COLOR_BUFFER_BIT)

        glEnableVertexAttribArray(0)
        glBindBuffer(GL_ARRAY_BUFFER, VBO)
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0)
        glDrawArrays(GL_POINTS, 0, 1)
        glDisableVertexAttribArray(0)

        glfw.swap_buffers(window)
        glfw.poll_events()

    glfw.terminate()

if __name__ == "__main__":
    main()

I'm studying OpenGL and I'm trying to follow the tutorial here. However, I found the position of the point never change even if I change the data in "glBufferData".

I don't known how this happened. Is the function glBufferData not working? Or maybe I made some low-level mistakes.


Solution

  • If a named buffer object is bound, then the 6th parameter of glVertexAttribPointer is treated as a byte offset into the buffer object's data store. However the type of the parameter is c_void_p.

    Therfore if the offset is 0, then the 6th parameter can either be None or c_void_p(0) else the offset has to be caste to c_void_p(0):

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0)

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None)
    

    Minimal example:

    import glfw
    import numpy as np
    from OpenGL.GL import *
    
    def main():
        if not glfw.init():
            raise RuntimeError('Can not initialize glfw library')
        window = glfw.create_window(500, 500, 'Demo', None, None)
        if not window:
            glfw.terminate()
            raise RuntimeError('Can not create glfw window')
        glfw.make_context_current(window)
    
        glClearColor(0, 0, 0, 1)
        glColor(1, 0, 0, 1)
        glPointSize(10.0)
    
        VBO = glGenBuffers(1)
        glBindBuffer(GL_ARRAY_BUFFER, VBO)
        glBufferData(GL_ARRAY_BUFFER, np.array([0.2, -0.2, 0, -0.2, -0.2, 0, 0, 0.2, 0], dtype='float32'), GL_STATIC_DRAW)
    
        while not glfw.window_should_close(window):
            glClear(GL_COLOR_BUFFER_BIT)
    
            glEnableVertexAttribArray(0)
            glBindBuffer(GL_ARRAY_BUFFER, VBO)
            glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None)
            glDrawArrays(GL_POINTS, 0, 3)
            glDisableVertexAttribArray(0)
    
            glfw.swap_buffers(window)
            glfw.poll_events()
    
        glfw.terminate()
    
    if __name__ == "__main__":
        main()