I want to call glDrawElements on the same element buffer object more than once, where each draw call accesses different parts of the element buffer object. So, out of many ways possible, in C/C++, one of them was to set the void * indices
argument. I tried a similar thing in PyQt5 using QOpenGLWidget, but I get the error
Traceback (most recent call last):
File "path/to/dir/scratch.py", line 305, in paintGL
self.renderer.draw(self.m_gl)
File "path/to/dir/scratch.py", line 257, in draw
obj.drawCall(gl)
File "path/to/dir/scratch.py", line 217, in drawCall
gl.glDrawElements(gl.GL_TRIANGLES, 3, self.idxdtype, 2) # tri2.
TypeError: glDrawElements(self, int, int, int, PYQT_OPENGL_ARRAY): array must be a sequence or a buffer
What does the type PYQT_OPENGL_ARRAY
mean? I cannot seem to find any hints here or on SO. Whereas this post looks like a possible duplicate, the question firstly refers to PyOpenGL and besides the suggested answer ctypes.c_void_p(int)
gives the error
Traceback (most recent call last):
File "path/to/dir/scratch.py", line 305, in paintGL
self.renderer.draw(self.m_gl)
File "path/to/dir/scratch.py", line 257, in draw
obj.drawCall(gl)
File "path/to/dir/scratch.py", line 217, in drawCall
gl.glDrawElements(gl.GL_TRIANGLES, 3, self.idxdtype, ctypes.c_void_p(2)) # tri2.
TypeError: a 1-dimensional buffer is required
The draw call looks like this. I am aware that I can directly use the entire element buffer to draw the two triangles, but I want to be able to use this functionality of void * indices
def drawCall(self, gl):
self.shaderProgram.bind()
self.vao.bind()
self.shaderProgram.setUniformValue(self.u_ColorIdx, self.color)
self.shaderProgram.setUniformValue(self.u_MVPidx, self.mvpMatrix)
# tri1 would be in the y-ve region and colored red.
gl.glDrawElements(gl.GL_TRIANGLES, 3, self.idxdtype, None) # tri1
# tri2 would be in the y+ve region and colored green.
self.shaderProgram.setUniformValue(self.u_ColorIdx, QtGui.QVector4D(0.0, 0.7, 0.3, 1.0))
gl.glDrawElements(gl.GL_TRIANGLES, 3, self.idxdtype, ctypes.c_void_p(2)) # tri2.
# I am aware that I might as well rotate tri1 by 180 degrees around z-axis,
# and set u_color uniform value to green to achieve the desired effect. But I want
# to do it using indices. It seems better for a large number of objects.
self.shaderProgram.release()
self.vao.release()
Finally, a link to the entire example code.
Edit: I am not looking for answers that suggest the usage of PyOpenGL/ModernGL to set the buffer objects.