Fix incorrect rendering of overlapping object in renderToArray() (#1306)

This commit adds a depth buffer in renderToArray().

This fixes the issue that overlapping objects are rendered incorrectly
when using renderToArray() on a GLViewWidget.

This might be related to issue #743.
This commit is contained in:
Allard Hendriksen 2020-10-14 04:47:49 +02:00 committed by GitHub
parent 287d564d7d
commit 9d99a3e7b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -498,6 +498,7 @@ class GLViewWidget(QtOpenGL.QGLWidget):
self.makeCurrent()
tex = None
fb = None
depth_buf = None
try:
output = np.empty((w, h, 4), dtype=np.ubyte)
fb = glfbo.glGenFramebuffers(1)
@ -516,6 +517,12 @@ class GLViewWidget(QtOpenGL.QGLWidget):
## create teture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texwidth, texwidth, 0, GL_RGBA, GL_UNSIGNED_BYTE, data.transpose((1,0,2)))
# Create depth buffer
depth_buf = glGenRenderbuffers(1)
glBindRenderbuffer(GL_RENDERBUFFER, depth_buf)
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, texwidth, texwidth)
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth_buf)
self.opts['viewport'] = (0, 0, w, h) # viewport is the complete image; this ensures that paintGL(region=...)
# is interpreted correctly.
p2 = 2 * padding
@ -545,6 +552,7 @@ class GLViewWidget(QtOpenGL.QGLWidget):
glDeleteTextures([tex])
if fb is not None:
glfbo.glDeleteFramebuffers([fb])
if depth_buf is not None:
glDeleteRenderbuffers(1, [depth_buf])
return output