Search code examples
pythonopenglctypesmemcpypyopengl

How to use OpenGL's glMapBuffer in Python?


I want to try updating a VBO in PyOpenGL using glMapBuffer, which returns a ctypes pointer to the mapped memory. Now, almost all the examples for this function are in C and use memcpy, which does apparently not exist in Python as such.

So, how can I update the data using this pointer?


Solution

  • Use from_address from the ctypes to get a ctypes type instance using the memory returned by glMapBuffer.

    The following example maps the buffer to an array of numberOfFloats floats:

    map_data = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY)
    map_array = (GLfloat * numberOfFloats).from_address(map_data)
    
    // copy new data
    // [...]
    
    glUnmapBuffer(GL_ARRAY_BUFFER)
    

    The elements of map_array can be accessed by subscription:

    map_array[i] = value
    

    Data can be copied form one ctypes array to another by ctypes.memmove(dst, src, count).