如何使VBO与Python和PyOpenGL一起使用 [英] How to get VBOs to work with Python and PyOpenGL
问题描述
以下Python程序应在窗口的右上象限中绘制一个白色三角形.
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.
我还尝试将缓冲区数据提交为NumPy数组:
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)
也没有绘制三角形. PyOpenGL ...是不是可以绘制VBO?
Also no triangle is drawn. PyOpenGL... Y U NO draw VBOs ?
我的系统:Python 2.7.3; OpenGL 4.2.0; Linux Mint Maya 64位
My system: Python 2.7.3 ; OpenGL 4.2.0 ; Linux Mint Maya 64 bit
推荐答案
好的,我刚刚找到了它:
Okay, I just found it:
glVertexPointer
调用的第四个参数必须为None
,代表NULL指针
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.
这篇关于如何使VBO与Python和PyOpenGL一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!