How to get VBOs to work with Python and PyOpenGL

巧了我就是萌 提交于 2019-12-10 12:46:05

问题


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:


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.



来源:https://stackoverflow.com/questions/13179565/how-to-get-vbos-to-work-with-python-and-pyopengl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!