在PyOpenGL调用中使用偏移到缓冲区

问题描述 投票:1回答:1

在OpenGL中,indicesglDrawElements参数有两种不同的含义,具体取决于你是否使用GL_ELEMENT_ARRAY_BUFFER

如果你有一个VBO绑定,那么它是在缓冲区中启动的偏移量,而不是缓冲区本身。

使用PyOpenGL时,如何在glDrawElements调用中指定要开始的偏移量?如何在glMultiDrawElements调用中指定多个起始偏移?

python numpy opengl ctypes pyopengl
1个回答
0
投票

在以下示例中,使用6个索引的列表,其可以形成由2个三角形基元组成的四边形。

indices = [0, 1, 2, 0, 2, 3]

由于传递给OpenGL函数的数据必须由相干缓冲区中的固定大小单元组成,因此必须将值列表存储到浮点数组中。可以通过ctypes库或numpy.array库创建相干阵列缓冲区。 在调用glDrawElementsglMultiDrawElements时,数组元素的类型必须与值类型枚举器常量匹配,该常量是设置的:

ctypes.c_ubyte   /  numpy.uint8     <->    GL_UNSIGNED_BYTE
ctypes.c_ushort  /  numpy.uint16    <->    GL_UNSIGNED_SHORT
ctypes.c_uint    /  numpy.uint32    <->    GL_UNSIGNED_INT

使用ctypes

import ctypes

indexArray = (ctypes.c_uint * 6)(*indices)

使用numpy

import numpy

indexArray = numpy.array(indices, dtype=numpy.uint32)

对于使用索引缓冲区和glDrawElements,有不同的机会。

使用Legacy OpenGLcompatibility profile xontext),缓冲区可以直接传递给glDrawElements。指向数组数据的指针传递给函数。

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, indexArray)

如果在顶点数组对象中声明了命名元素数组缓冲区对象,则glDrawElements的最后一个参数将被视为缓冲区对象的数据存储中的字节偏移量。

glBindVertexArray(vao)

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexArray, GL_STATIC_DRAW)

如果应该绘制索引,从缓冲区的第一个元素开始,那么最后一个参数可以是None,相当于ctypes.c_void_p(0)

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, None)

如果绘图不应以第一个索引开头,则必须计算起始索引的字节偏移量。例如对于3*4类型的缓冲区,GL_UNSIGNED_INT将开始设置为3索引:

glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, ctypes.c_void_p(3 * 4))

glMultiDrawElements的使用非常相似。

使用compatibility profile xontext,缓冲区指针可以直接传递给OpenGL函数。

必须生成索引数组:

使用ctypes

indexArray1 = (ctypes.c_uint * 3)(0, 1, 2)
indexArray2 = (ctypes.c_uint * 3)(0, 2, 3)

使用numpy

indexArray1 = numpy.array([0, 1, 2], dtype=numpy.uint32)
indexArray2 = numpy.array([0, 2, 3], dtype=numpy.uint32)

指向缓冲区的指针必须安排到一个指针数组:

使用ctypes,指向索引数据数组的指针由ctypes.addressof()获取:

indexPtr = (ctypes.c_void_p * 2)(ctypes.addressof(indexArray1),ctypes.addressof(indexArray2))

使用numpy,指向索引数据数组的指针由numpy.ndarray.ctypes获取:

indexPtr = numpy.array([indexArray1.ctypes.data, indexArray2.ctypes.data], dtype=numpy.intp)

这个指针数组可以传递给OpenGL函数:

counts   = [3, 3]
glMultiDrawElements(GL_TRIANGLES, counts, GL_UNSIGNED_INT, indexPtr, 2)

如果使用具有命名元素数组缓冲区的顶点数组对象,

glBindVertexArray(vao)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo)

然后将index参数视为指向字节偏移数组的指针。在下面,将一个带有2个偏移量的数组传递给该函数。 0表示数组中的第一个索引,3 * 4表示第三个索引。

使用ctypes

indexPtr = (ctypes.c_void_p * 2)(0, 3 * 4)
counts   = [3, 3]
glMultiDrawElements(GL_TRIANGLES, counts, GL_UNSIGNED_INT, indexPtr, 2)

使用numpy

indexPtr = np.array([0, 3*4], dtype=numpy.intp)
counts   = [3, 3]
glMultiDrawElements(GL_TRIANGLES, counts, GL_UNSIGNED_INT, indexPtr, 2)
© www.soinside.com 2019 - 2024. All rights reserved.