Commit 63a4c9b9 authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

RF: Can't read depth from frame buffer, because if (e.g.) an ortho view is

open, the context will have changed, and contents of 3d canvas frame buffer no
longer available. But we can take advantage of the fact that GLVolumes are
actually rendered to an off-screen texture.
parent f284f826
......@@ -240,6 +240,12 @@ class GLVolume(glimageobject.GLImageObject):
self.smoothFilter = glfilter.Filter('smooth', texture=0)
self.smoothFilter.set(kernSize=self.opts.smoothing * 2)
# One of renderTexture1 or 2 will be be used
# as the target for the final draw, depending
# on settings. A reference to whichever one
# is used is saved to the "renderTexture"
# attribute on each draw.
self.renderTexture = None
self.renderTexture1 = textures.RenderTexture(
self.name, interp=gl.GL_LINEAR, rttype='cd')
self.renderTexture2 = textures.RenderTexture(
......@@ -759,7 +765,7 @@ class GLVolume(glimageobject.GLImageObject):
# contains the final render. Otherwise,
# rt2 contains the final render, but rt1
# contains the depth information. So we
# need to # temporarily replace rt2.depth
# need to temporarily replace rt2.depth
# with rt1.depth.
if opts.smoothing > 0:
src = self.renderTexture2
......@@ -773,6 +779,7 @@ class GLVolume(glimageobject.GLImageObject):
src.depthTexture = dep
src.draw(verts, useDepth=True)
src.depthTexture = olddep
self.renderTexture = src
def drawAll(self, *args, **kwargs):
......
......@@ -259,20 +259,39 @@ class Scene3DViewProfile(profiles.Profile):
if screen2Display is None:
return
# Transform the mouse coords into normalised device
# coordinates (NDCs, in the range [0, 1] - see
# Volume3DOpts.calculateRayCastSettings), and query
# the depth for the current fragment (this is saved
# by the glvolume 3d fragment shader).
# Retrieve the depth for the current
# fragment. Images are drawn to an off-screen
# texture (see GLVolume.draw3d), so we can get
# the depth from there.
globj = canvas.getGLObject(ovl)
tex = globj.renderTexture.depthTexture
with tex.bound():
# There's no function to read part of
# a texture in GL < 4.5, so we have
# to read the entire depth testure.
buf = gl.glGetTexImage(gl.GL_TEXTURE_2D,
0,
tex.baseFormat,
tex.textureType,
None)
# Get the mouse coords, and transform
# them into normalised device coordinates
# (NDCs, in the range [0, 1] - see
# Volume3DOpts.calculateRayCastSettings),
x, y = mousePos
w, h = canvas.GetSize()
z = gl.glReadPixels(
x, y, 1, 1, gl.GL_DEPTH_COMPONENT, gl.GL_FLOAT)
x = x / w
y = y / h
# The depth texure is stored as uint32,
# but represents floating point values in
# the range [0, 1].
buf = np.frombuffer(buf, dtype=tex.dtype).reshape(h, w)
z = buf[y, x] / 4294967295.0
x = x / w
y = y / h
# Transform NDCs into display coordinates
xyz = affine.transform((x, y, z), screen2Display)
xyz = affine.transform([x, y, z], screen2Display)
self.displayCtx.location.xyz = xyz
else:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment