[英]glDrawElements method not drawing with indices and vertices
I'm trying to draw a quad on the screen using the glDrawElements method like this:我正在尝试使用 glDrawElements 方法在屏幕上绘制一个四边形,如下所示:
glDrawElements(GL_TRIANGLES, model.get_vertex_count(), GL_UNSIGNED_INT, 0)
To get the indices I've written this method:为了获取索引,我编写了这个方法:
@classmethod
def bind_indices_buffer(cls, indices: list[int]):
indices = numpy.array(indices, dtype=numpy.uint32) # convert the data into an unsigned integer array
vbo_id = glGenBuffers(1)
cls.__vbos.append(vbo_id)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo_id)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices, GL_STATIC_DRAW)
And for the vertices:对于顶点:
@classmethod
def store_data_in_attribute_list(cls, attribute_number: int, data: list[float]):
data = numpy.array(data, dtype='float32') # convert the data into a float32 array
vbo_id = glGenBuffers(1) # create 1 VBO buffer
cls.__vbos.append(vbo_id) # appends it to the VBO list in the class
glBindBuffer(GL_ARRAY_BUFFER, vbo_id) # binds the buffer to use it
glBufferData(GL_ARRAY_BUFFER, data, GL_STATIC_DRAW) # specifies the buffer, data and usage
glVertexAttribPointer(attribute_number, 3, GL_FLOAT, False, 0, None) # put the VBO into a VAO
glBindBuffer(GL_ARRAY_BUFFER, 0)
If you want to see the rest of the code, I have pasted it into my last question, these snippets are the only changes如果您想查看代码的 rest,我已将其粘贴到我的最后一个问题中,这些片段是唯一的变化
The type of the lase argument of glDrawElements
is const GLvoid *
. glDrawElements
的最后一个参数的类型是const GLvoid *
。 So the argument must be None
or ctypes.c_void_p(0)
, but not 0:所以参数必须是None
或ctypes.c_void_p(0)
,但不是 0:
glDrawElements(GL_TRIANGLES, model.get_vertex_count(), GL_UNSIGNED_INT, 0)
glDrawElements(GL_TRIANGLES, model.get_vertex_count(), GL_UNSIGNED_INT, None)
I admit my OpenGL is a bit rusty, but IIRC glDrawElements
expects a pointer to a list of indices in the last parameter (cf. https://registry.khronos.org/OpenGL-Refpages/gl4/html/glDrawElements.xhtml , and I wouldn't really know how to handle this in Python).我承认我的 OpenGL 有点生锈,但 IIRC glDrawElements
期望在最后一个参数中有一个指向索引列表的指针(参见https://registry.khronos.org/OpenGL-Refpages/gl4/html/glDrawElements.xhtml ,和我真的不知道如何在 Python 中处理这个)。 If you just want to draw all vertices, try glDrawArrays( GL_TRIANGLES, 0, model.get_vertex_count() )
instead (cf. https://registry.khronos.org/OpenGL-Refpages/gl4/html/glDrawArrays.xhtml ).如果您只想绘制所有顶点,请尝试glDrawArrays( GL_TRIANGLES, 0, model.get_vertex_count() )
(参见https://registry.khronos.org/OpenGL-Refpages/gl4/html/glDrawArrays.xhtml )。
EDIT: I was wrong, thanks to @Rabbid76 for clarification.编辑:我错了,感谢@Rabbid76 的澄清。 glDrawArrays
will just pull the given number of vertices from the enabled arrays in order (vertex[0], vertex[1], vertex[2], ...) while glDrawElements
will use the index array to look them up (vertex[index[0]], vertex[index[1]], ...) etc. glDrawArrays
将按顺序从启用的 arrays 中拉出给定数量的顶点(vertex[0]、vertex[1]、vertex[2]、...),而glDrawElements
将使用索引数组查找它们(vertex[index [0]], 顶点[索引[1]], ...) 等
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.