在PyOpenGL调用中在缓冲区中使用偏移量 [英] Using offsets into the buffer in PyOpenGL calls

查看:87
本文介绍了在PyOpenGL调用中在缓冲区中使用偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在OpenGL中, glDrawElements 具有两种不同的含义,具体取决于您是否使用GL_ELEMENT_ARRAY_BUFFER.

In OpenGL the indices parameter for glDrawElements has two different meanings, depending on if you are using GL_ELEMENT_ARRAY_BUFFER or not.

如果绑定了VBO,则它是从该缓冲区开始的偏移量,而不是缓冲区本身.

If you have a VBO bound, then it is the offset to start in that buffer, rather than the buffer itself.

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

When using PyOpenGL, how can you specify the offset to start at in a glDrawElements call? How can you specify multiple start offsets in a glMultiDrawElements call?

推荐答案

在以下示例中,使用了6个索引的列表,这些索引可能形成由2个三角形图元组成的四边形.

In the following examples a list of 6 indices is used, which may form a quad which consists of 2 triangle primitives.

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

由于传递给OpenGL函数的数据必须由相干缓冲区中的固定大小单位组成,因此值列表必须存储到浮点数组中. 可以通过 ctypes 库或

Since the data which is passed to the OpenGL functions has to consist of fixed size units in a coherent buffer, the list of values has to be stored to an array of floats. Coherent array buffers can be created either by ctypes library or numpy.array library.
The type of the array elements has to match value type enumerator constant, which is set, at the call of glDrawElements or glMultiDrawElements:

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 有不同的机会.

For using an index buffer and glDrawElements there are different opportunities.

使用旧版OpenGL (

Using Legacy OpenGL (compatibility profile xontext), the buffer can be directly passed to glDrawElements. The pointer to the array data is passed to the function.

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, indexArray)

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

If named element array buffer object is stated in the vertex array object, then the last parameter of glDrawElements is treated as a byte offset into the buffer object's data store.

glBindVertexArray(vao)

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

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

If the indices should be drawn, starting at the 1st element of the buffer, then the last parameter can be None, which is equivalent to ctypes.c_void_p(0):

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, None)

如果图形不应该从第一个索引开始,则必须计算起始索引的字节偏移量.例如3*4GL_UNSIGNED_INT类型的缓冲区的起始位置设置为3索引:

If the drawing should not start with the first index, then the byte offset of the start index has to be calculated. e.g. 3*4 sets the start to the 3 index, for a buffer of type GL_UNSIGNED_INT:

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

使用 glMultiDrawElements 是非常相似.

The use of glMultiDrawElements is very similar.

使用兼容性配置文件xontext ,缓冲区指针可以直接传递给OpenGL功能.

Using a compatibility profile xontext, the buffer pointers can be directly passed to the OpenGL function.

要生成索引数组:

使用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)

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

The pointers to the buffers have to be arranged to an array of pointers:

使用ctypes指向索引数据数组的指针是通过 ctypes.addressof() :

Using ctypes the pointer to the index data arrays is get by ctypes.addressof():

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

使用numpy指向索引数据数组的指针是通过 numpy.ndarray.ctypes :

Using numpy the pointer to the index data arrays is get by numpy.ndarray.ctypes:

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

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

This array of pointer can be passed to the OpenGL function:

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

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

If a vertex array object with an named element array buffer is used,

glBindVertexArray(vao)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo)

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

then the index parameter is treated as a pointer to an array of byte offsets. In the following an array with 2 offset is passed to the function. 0 identifies the 1st index in the array and 3*4 the 3rd index.

使用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)

这篇关于在PyOpenGL调用中在缓冲区中使用偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆