11

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

broepi
  • 221
  • 2
  • 8

1 Answers1

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.

broepi
  • 221
  • 2
  • 8
  • 1
    The second hit on Google for ["Vertex Buffer Object".](http://www.songho.ca/opengl/gl_vbo.html) – Nicol Bolas Nov 01 '12 at 15:54
  • 1
    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! – Jiří Stránský Aug 17 '13 at 22:35
  • 1
    The final parameter is of type GLvoid* and must be converted to a ctypes.c_void_p(offset), see [link](https://bitbucket.org/tartley/gltutpy/src/tip/t02.playing-with-colors/VertexColors.py) – Adrian Sep 16 '14 at 12:12
  • 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... – Mudit Bhatia Jul 07 '21 at 10:59