glDrawElements method not drawing with indices and vertices

Question:

My desired output of my program would be a simple quad being drawn onto the screen, instead nothing is being drawn on my screen. The relevant methods of my program are below.

What is wrong in my code causing this issue?

I’m trying to draw a quad on the screen using the glDrawElements method like this:

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)
Asked By: Andreas Sabelfeld

||

Answers:

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). 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).

EDIT: I was wrong, thanks to @Rabbid76 for clarification. 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.

Answered By: Florian Echtler

The type of the lase argument of glDrawElements is const GLvoid *. So the argument must be None or ctypes.c_void_p(0), but not 0:

glDrawElements(GL_TRIANGLES, model.get_vertex_count(), GL_UNSIGNED_INT, 0)

glDrawElements(GL_TRIANGLES, model.get_vertex_count(), GL_UNSIGNED_INT, None)
Answered By: Rabbid76