I’m trying to learn OpenGL and Qt5 in parallel, and decide to try and use primarily Qt5-functions (with a few exceptions when I’ve been unable to get the Qt5-functions to work). I’ve gotten the basics down for my “Hello cubes”, but am now having issues as I try to handle instancing in the vertex buffer instead of individual draw calls.
The project in full can be found at https://github.com/anderslindho/graphy, where the master branch contains a stable non-instance buffer approach, and the instancing branch contains my latest attempt at instancing.
The core snippets that I have changed are:
(lines 160–176 in opengl.py)
for i, j, k in itertools.product(
range(0, 100, 2),
range(0, 100, 2),
range(0, 100, 2)
):
translation = pyrr.Vector3([0.0, 0.0, 0.0])
translation.x = i + self.instance_offset
translation.y = j + self.instance_offset
translation.z = k + self.instance_offset
self.instance_array.append(translation)
self.len_of_instance_array = len(self.instance_array)
self.instance_array = np.array(self.instance_array, dtype=np.float32).flatten()
self.instance_vbo.create()
self.instance_vbo.bind()
self.instance_vbo.allocate(self.instance_array, self.instance_array.nbytes)
(lines 192–196 in opengl.py)
self.glEnableVertexAttribArray(2)
self.glVertexAttribPointer(
2, 3, gl.GL_FLOAT, gl.GL_FALSE, 0, VoidPtr(0)
)
self.glVertexAttribDivisor(2, 1)
…and the draw call is:
(lines 99–117 in opengl.py)
def render(self) -> None:
self.program.bind()
vao_binder = QOpenGLVertexArrayObject.Binder(self.vao)
# TODO: switch to Qt functions (doesn't work)
gl.glUniformMatrix4fv(self.projection_loc, 1, gl.GL_FALSE, self.projection)
view = self.camera.get_view_matrix()
gl.glUniformMatrix4fv(self.camera_loc, 1, gl.GL_FALSE, view)
self.glDrawElementsInstanced(
gl.GL_TRIANGLES,
len(self.shape.indices),
gl.GL_UNSIGNED_INT,
VoidPtr(0),
self.len_of_instance_array
)
self.program.release()
vao_binder = None
I should also add that I’ve had some issues with some of Qt5’s OpenGL-functions, which is why the draw-call uses OpenGL.GL
functions, and I’ve had issues with null pointers which is why I have resorted to VoidPtr()
(despite having read that None
works for others, which confuses me a little).
Any and all help appreciated.