5

如何在 PyOpenGL python 绑定到 OpenGL 中使用 glBufferData()?

当我运行以下代码时

import sys
from OpenGL.GL import *
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtOpenGL import *

class SimpleTestWidget(QGLWidget):

    def __init__(self):
        QGLWidget.__init__(self)

    def initializeGL(self):
        self._vertexBuffer = glGenBuffers(1)
        glBindBuffer(GL_ARRAY_BUFFER, self._vertexBuffer)
        vertices = [0.5, 0.5, -0.5, 0.5, -0.5, -0.5, 0.5, -0.5]
        glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW)    # Error

    def paintGL(self):
        glViewport(0, 0, self.width(), self.height())
        glClearColor(0.0, 1.0, 0.0, 1.0)
        glClear(GL_COLOR_BUFFER_BIT)

        glEnableClientState(GL_VERTEX_ARRAY)
        glBindBuffer(GL_ARRAY_BUFFER, self._vertexBuffer)
        glVertexPointer(2, GL_FLOAT, 0, 0)
        glColor3f(1.0, 0.0, 0.0)
        glDrawArrays(GL_QUADS, 0, 4)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = SimpleTestWidget()
    w.show()
    app.exec_()

然后调用 glBufferData() 会导致错误消息

Haven't implemented type-inference for lists yet

该代码应该在绿色背景上绘制一个红色矩形。

4

2 回答 2

8

作为一种解决方法,在支持列表之前,将顶点作为 numpy 数组传递:

vertices = numpy.array([0.5, 0.5, -0.5, 0.5, -0.5, -0.5, 0.5, -0.5], 
                       dtype='float32')

glVertexPointer 调用应该是glVertexPointer(2, GL_FLOAT, 0, None)

于 2012-06-20T20:24:57.387 回答
3

您还可以使用数组模块的数组对象:

from array import array
vert=[0.0,0.0,0.0,
      1.0,0.0,0.0,
      1.0,1.0,0.0,
      0.0,1.0,0.0]

ar=array("f",vert)
glBufferData(GL_ARRAY_BUFFER, ar.tostring(), GL_STATIC_DRAW)

https://docs.python.org/2/library/array.html

于 2015-09-14T15:27:51.187 回答