How to get VBOs to work with Python and PyOpenGL
Asked Answered
K

1

12

The following Python program should draw a white triangle in the upper right quadrant of the window.

import pygame
from OpenGL.GL import *
from ctypes import *

pygame.init ()
screen = pygame.display.set_mode ((800,600), pygame.OPENGL|pygame.DOUBLEBUF, 24)
glViewport (0, 0, 800, 600)
glClearColor (0.0, 0.5, 0.5, 1.0)
glEnableClientState (GL_VERTEX_ARRAY)

vertices = [ 0.0, 1.0, 0.0,  0.0, 0.0, 0.0,  1.0, 1.0, 0.0 ]
vbo = glGenBuffers (1)
glBindBuffer (GL_ARRAY_BUFFER, vbo)
glBufferData (GL_ARRAY_BUFFER, len(vertices)*4, (c_float*len(vertices))(*vertices), GL_STATIC_DRAW)

running = True
while running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    glClear (GL_COLOR_BUFFER_BIT)

    glBindBuffer (GL_ARRAY_BUFFER, vbo)
    glVertexPointer (3, GL_FLOAT, 0, 0)

    glDrawArrays (GL_TRIANGLES, 0, 3)

    pygame.display.flip ()

It won't throw any errors, but unfortunately it doesn't draw the triangle.

I also tried to submit the buffer data as a NumPy-array:

glBufferData (GL_ARRAY_BUFFER, len(vertices)*4, np.array (vertices, dtype="float32"), GL_STATIC_DRAW)

Also no triangle is drawn. PyOpenGL... Y U NO draw VBOs ?

My system: Python 2.7.3 ; OpenGL 4.2.0 ; Linux Mint Maya 64 bit

Korykorzybski answered 1/11, 2012 at 14:48 Comment(0)
K
11

Okay, I just found it:

The 4th parameter of the glVertexPointer call must be None representing a NULL pointer

glVertexPointer (3, GL_FLOAT, 0, None)

I swear, I searched for hours last night and didn't see that.

Korykorzybski answered 1/11, 2012 at 15:15 Comment(4)
The second hit on Google for "Vertex Buffer Object".Katerinekates
Oh that saved me from some gray hair. It's the same with GL.glVertexAttribPointer(0, 4, GL.GL_FLOAT, GL.GL_FALSE, 0, None). Passing 0 as the last parameter probably works in C/C++ code, but apparently not in PyOpenGL. It has to be None. I spent several hours debugging before i found this. I was already going insane. You saved me, thanks a lot!Glenglencoe
The final parameter is of type GLvoid* and must be converted to a ctypes.c_void_p(offset), see linkAverroism
Hey Adrian. I spent about 2 hours a day for a week but could not find out this last thing which was how to use void from the ctypes module. Everything makes sense now. I have made many awesome 3d models for a game and programmed 90 % of it already but it was using the fixed function pipeline and it was laggy but now, with some optimizations, I can use the modern OpenGL pipeline too and I know that it will boost the performance alot. Thanks alot ! I wish I could do more than just saying thanks...Royalroyalist

© 2022 - 2024 — McMap. All rights reserved.