Commit b481b40b authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

RF: glvolume/glimageobject decoupled from canvas

parent 8a60306c
......@@ -309,7 +309,7 @@ class Annotations(props.HasProperties):
if obj.zmax is not None and zpos > obj.zmax: continue
try:
obj.draw2D(zpos, axes)
obj.draw2D(self.canvas, zpos, axes)
except Exception as e:
log.warning(e, exc_info=True)
......@@ -343,7 +343,7 @@ class Annotations(props.HasProperties):
try:
with glroutines.enabled(features):
obj.draw3D(xform)
obj.draw3D(self.canvas, xform)
except Exception as e:
log.warning(e, exc_info=True)
......@@ -478,7 +478,7 @@ class AnnotationObject(globject.GLSimpleObject, props.HasProperties):
Any other arguments are ignored.
"""
globject.GLSimpleObject.__init__(self, annot.canvas, False)
globject.GLSimpleObject.__init__(self, False)
self.annot = annot
self.creation = time.time()
......@@ -585,12 +585,11 @@ class AnnotationObject(globject.GLSimpleObject, props.HasProperties):
raise NotImplementedError()
def __draw(self, vertices, xform=None):
def __draw(self, canvas, vertices, xform=None):
"""Used by the default :meth:`draw2D` and :meth:`draw3D`
implementations.
"""
shader = self.annot.defaultShader
canvas = self.canvas
mvpmat = canvas.mvpMatrix
colour = list(self.colour[:3]) + [self.alpha / 100.0]
......@@ -618,7 +617,7 @@ class AnnotationObject(globject.GLSimpleObject, props.HasProperties):
shader.draw(prim, off, length)
def draw2D(self, zpos, axes, xform=None):
def draw2D(self, canvas, zpos, axes, xform=None):
"""Draw this annotation on a 2D plane. This method implements a
default routine used by most annotation types - the annotation is
drawn using vertices returned by the overridden :meth:`vertices2D`
......@@ -628,10 +627,10 @@ class AnnotationObject(globject.GLSimpleObject, props.HasProperties):
routine.
"""
vertices = self.vertices2D(zpos, axes)
self.__draw(vertices, xform)
self.__draw(canvas, vertices, xform)
def draw3D(self, xform=None):
def draw3D(self, canvas, xform=None):
"""Draw this annotation in 3D. This method implements a
default routine used by most annotation types - the annotation is
drawn using vertices returned by the overridden :meth:`vertices3D`
......@@ -641,7 +640,7 @@ class AnnotationObject(globject.GLSimpleObject, props.HasProperties):
routine.
"""
vertices = self.vertices3D()
self.__draw(vertices, xform)
self.__draw(canvas, vertices, xform)
class Point(AnnotationObject):
......@@ -902,9 +901,8 @@ class BorderMixin:
"""Whether to draw a border around the rectangle. """
def draw2D(self, zpos, axes):
def draw2D(self, canvas, zpos, axes):
shader = self.annot.defaultShader
canvas = self.canvas
mvpmat = canvas.mvpMatrix
vertices = self.vertices2D(zpos, axes)
......@@ -1300,11 +1298,10 @@ class VoxelSelection(AnnotationObject):
return verts, texs
def draw2D(self, zpos, axes):
def draw2D(self, canvas, zpos, axes):
"""Draw a :class:`.VoxelSelection` annotation. """
shader = self.__shader
canvas = self.canvas
texture = self.texture
mvpmat = canvas.mvpMatrix
colour = list(self.colour[:3]) + [self.alpha / 100.0]
......@@ -1391,14 +1388,13 @@ class TextAnnotation(AnnotationObject):
self.__text = None
def draw2D(self, zpos, axes):
def draw2D(self, canvas, zpos, axes):
"""Draw this ``TextAnnotation``. """
if self.colour is not None: colour = self.colour[:3]
else: colour = [1, 1, 1]
text = self.__text
canvas = self.annot.canvas
opts = canvas.opts
text.text = self.text
text.off = self.off
......
......@@ -173,13 +173,13 @@ def preDraw(self):
self.shader.setVertParam('modCoordXform', modCoordXform)
def draw2D(self, zpos, axes, xform=None):
def draw2D(self, canvas, zpos, axes, xform=None):
"""Draws a 2D slice of the image at the given Z location. """
shader = self.shader
bbox = self.canvas.viewport
projmat = self.canvas.projectionMatrix
viewmat = self.canvas.viewMatrix
bbox = canvas.viewport
projmat = canvas.projectionMatrix
viewmat = canvas.viewMatrix
vertices, _, texCoords = self.generateVertices2D(
zpos, axes, bbox=bbox)
......@@ -199,17 +199,18 @@ def draw2D(self, zpos, axes, xform=None):
gl.glDrawArrays(gl.GL_TRIANGLES, 0, 6)
def draw3D(self, xform=None):
def draw3D(self, canvas, xform=None):
"""Draws the image in 3D on the canvas.
:arg self: The :class:`.GLVolume` object which is managing the image
to be drawn.
:arg canvas: The canvas being drawn on
:arg xform: A 4*4 transformation matrix to be applied to the vertex
data.
"""
opts = self.opts
canvas = self.canvas
display = self.display
shader = self.shader
shape = self.image.shape
......@@ -306,15 +307,15 @@ def draw3D(self, xform=None):
def drawAll(self, axes, zposes, xforms):
def drawAll(self, canvas, axes, zposes, xforms):
"""Draws mutltiple slices of the given image at the given Z position,
applying the corresponding transformation to each of the slices.
"""
nslices = len(zposes)
shader = self.shader
projmat = self.canvas.projectionMatrix
viewmat = self.canvas.viewMatrix
projmat = canvas.projectionMatrix
viewmat = canvas.viewMatrix
vertices = np.zeros((nslices * 6, 3), dtype=np.float32)
texCoords = np.zeros((nslices * 6, 3), dtype=np.float32)
indices = np.arange(nslices * 6, dtype=np.uint32)
......
......@@ -187,12 +187,14 @@ def preDraw(self):
self.shader.set('modCoordXform', modCoordXform)
def draw2D(self, zpos, axes, xform=None):
def draw2D(self, canvas, zpos, axes, xform=None):
"""Draws the specified 2D slice from the specified image on the canvas.
:arg self: The :class:`.GLVolume` object which is managing the image
to be drawn.
:arg canvas: The canvas being drawn on
:arg zpos: World Z position of slice to be drawn.
:arg axes: x, y, z axis indices.
......@@ -202,8 +204,8 @@ def draw2D(self, zpos, axes, xform=None):
"""
shader = self.shader
bbox = self.canvas.viewport
mvpmat = self.canvas.mvpMatrix
bbox = canvas.viewport
mvpmat = canvas.mvpMatrix
vertices, voxCoords, texCoords = self.generateVertices2D(
zpos, axes, bbox=bbox)
......@@ -222,19 +224,20 @@ def draw2D(self, zpos, axes, xform=None):
gl.glDrawArrays(gl.GL_TRIANGLES, 0, 6)
def draw3D(self, xform=None):
def draw3D(self, canvas, xform=None):
"""Draws the image in 3D on the canvas.
:arg self: The :class:`.GLVolume` object which is managing the image
to be drawn.
:arg canvas: The canvas being drawn on
:arg xform: A 4*4 transformation matrix to be applied to the vertex
data.
"""
ovl = self.overlay
opts = self.opts
canvas = self.canvas
shader = self.shader
copts = canvas.opts
bbox = canvas.viewport
......@@ -284,12 +287,12 @@ def draw3D(self, xform=None):
self.drawClipPlanes(xform=xform)
def drawAll(self, axes, zposes, xforms):
def drawAll(self, canvas, axes, zposes, xforms):
"""Draws all of the specified slices. """
nslices = len(zposes)
shader = self.shader
mvpmat = self.canvas.mvpMatrix
mvpmat = canvas.mvpMatrix
vertices = np.zeros((nslices * 6, 3), dtype=np.float32)
voxCoords = np.zeros((nslices * 6, 3), dtype=np.float32)
texCoords = np.zeros((nslices * 6, 3), dtype=np.float32)
......
......@@ -52,7 +52,7 @@ class GLImageObject(globject.GLObject):
"""
def __init__(self, overlay, overlayList, displayCtx, canvas, threedee):
def __init__(self, overlay, overlayList, displayCtx, threedee):
"""Create a ``GLImageObject``.
:arg image: A :class:`.Nifti` object.
......@@ -62,13 +62,11 @@ class GLImageObject(globject.GLObject):
:arg displayCtx: The :class:`.DisplayContext` object managing the
scene.
:arg canvas: The canvas doing the drawing.
:arg threedee: Set up for 2D or 3D rendering.
"""
globject.GLObject.__init__(
self, overlay, overlayList, displayCtx, canvas, threedee)
self, overlay, overlayList, displayCtx, threedee)
self.__name = 'GLImageObject_{}'.format(self.name)
......@@ -175,7 +173,7 @@ class GLImageObject(globject.GLObject):
return res
def frontFace(self):
def frontFace(self, canvas):
"""Convenience method for 2D rendering.
Image slices are generally drawn onto a 2D plane which is parallel to
......@@ -199,7 +197,7 @@ class GLImageObject(globject.GLObject):
numInverts = 0
copts = self.canvas.opts
copts = canvas.opts
if copts.invertX: numInverts += 1
if copts.invertY: numInverts += 1
......
......@@ -186,7 +186,7 @@ class GLVolume(glimageobject.GLImageObject):
"""
def __init__(self, image, overlayList, displayCtx, canvas, threedee):
def __init__(self, image, overlayList, displayCtx, threedee):
"""Create a ``GLVolume`` object.
:arg image: An :class:`.Image` object.
......@@ -196,8 +196,6 @@ class GLVolume(glimageobject.GLImageObject):
:arg displayCtx: The :class:`.DisplayContext` object managing the
scene.
:arg canvas: The canvas doing the drawing.
:arg threedee: Set up for 2D or 3D rendering.
"""
......@@ -205,7 +203,6 @@ class GLVolume(glimageobject.GLImageObject):
image,
overlayList,
displayCtx,
canvas,
threedee)
# Create an image texture, clip texture, and a colour map texture
......@@ -686,21 +683,21 @@ class GLVolume(glimageobject.GLImageObject):
fslgl.glvolume_funcs.preDraw(self)
def draw2D(self, *args, **kwargs):
def draw2D(self, canvas, *args, **kwargs):
"""Calls the version dependent ``draw2D`` function. """
with glroutines.enabled((gl.GL_CULL_FACE)):
gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_FILL)
gl.glCullFace(gl.GL_BACK)
gl.glFrontFace(self.frontFace())
fslgl.glvolume_funcs.draw2D(self, *args, **kwargs)
gl.glFrontFace(self.frontFace(canvas))
fslgl.glvolume_funcs.draw2D(self, canvas, *args, **kwargs)
def draw3D(self, xform=None):
def draw3D(self, canvas, xform=None):
"""Calls the version dependent ``draw3D`` function. """
opts = self.opts
w, h = self.canvas.GetScaledSize()
w, h = canvas.GetScaledSize()
res = self.opts.resolution / 100.0
sw = int(np.ceil(w * res))
sh = int(np.ceil(h * res))
......@@ -728,7 +725,7 @@ class GLVolume(glimageobject.GLImageObject):
gl.glPolygonMode(gl.GL_FRONT_AND_BACK, gl.GL_FILL)
gl.glFrontFace(gl.GL_CCW)
gl.glCullFace(gl.GL_BACK)
fslgl.glvolume_funcs.draw3D(self, xform)
fslgl.glvolume_funcs.draw3D(self, canvas, xform)
if self.opts.resolution != 100:
gl.glViewport(0, 0, w, h)
......
Supports Markdown
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