How to get VBOs to work with Python and PyOpenGL

2019-06-17 05:13发布

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

1条回答
smile是对你的礼貌
2楼-- · 2019-06-17 05:34

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.

查看更多
登录 后发表回答