fsleyes.gl.textures.rendertexture¶
This module provides the RenderTexture and
GLObjectRenderTexture classes, which are Texture2D
sub-classes intended to be used as targets for off-screen rendering.
These classes are used by the SliceCanvas and
LightBoxCanvas classes for off-screen rendering, and in various
other situations throughout FSLeyes. See also the
RenderTextureStack, which uses RenderTexture instances.
-
class
fsleyes.gl.textures.rendertexture.RenderTexture(name, rttype='cds', **kwargs)[source]¶ Bases:
fsleyes.gl.textures.texture2d.Texture2DThe
RenderTextureclass is a 2D RGBA texture which manages a frame buffer, and a render buffer or aDepthTexturewhich is used as the depth attachment.A
RenderTextureis intended to be used as a target for off-screen rendering. Using aRenderTexture(texin the example below) as the rendering target is easy:# Set the texture size in pixels tex.shape = 1024, 768 # Bind the texture/frame buffer, and configure # the viewport for orthoghraphic display. lo = (0.0, 0.0, 0.0) hi = (1.0, 1.0, 1.0) with tex.target(0, 1, lo, hi): # ... # draw the scene # ...
The contents of the
RenderTexturecan later be drawn to the screen via theTexture2D.draw()orTexture2D.drawOnBounds()methods.A
RenderTexturecan be configured in one of three ways:Using a
RGBA8Texture2D(theRenderTextureitself) as the colour attachment, and no depth or stencil attachments.As above for the colour attachment, and a
DepthTextureas the depth attachment.As above for the colour attachment, and a
DEPTH24_STENCIL8renderbuffer as the combined depth+stencil attachment.
These are the only available options due to limitations in different GL implementations. You can choose which option you wish to use via the
rttypeargument to__init__.If you choose option #2, a
DepthTextureinstance will be used as the depth attachment. The resulting depth values created after drawing to theRenderTexturemay be used in subsequent processing. You can either access the depth texture directly via thedepthTexture()method, or you can draw the contents of theRenderTexture, with depth information, by passing theuseDepthflag to either thedraw()ordrawOnBounds()methods.-
__init__(name, rttype='cds', **kwargs)[source]¶ Create a
RenderTexture. All keyword arguments are passed through to theTexture2D.__init__()method.- Parameters
name – Unique name for this texture
rttype –
RenderTextureconfiguration. If provided, must be passed as a keyword argument. Valid values are:'c': Only a colour attachment is configured'cd': Colour and depth attachments areconfigured
'cds'Colour, depth, and stencil attachmentsare configured. This is the default.
Note
A rendering target must have been set for the GL context before a frame buffer can be created … in other words, call
context.SetCurrentbefore creating aRenderTexture.
-
__initShader()¶ Called by
__init__()if thisRenderTexturewas configured to use a colour and depth texture. Compiles vertex/fragment shader programs which pass the colour and depth values through.These shaders are used if the
draw()orTexture2D.drawOnBounds()methods are used with theuseDepth=Trueargument.
-
destroy()[source]¶ Must be called when this
RenderTextureis no longer needed. Destroys the frame buffer and render buffer, and callsTexture2D.destroy().
-
property
data¶ Returns the data that has been passed to the
set()method.
-
property
depthTexture¶ Returns the
DepthTextureinstance used as the depth buffer. ReturnsNoneif thisRenderTexturewas not configured with'cd'(see__init__()).
-
property
shape¶ Return a tuple containing the texture data shape.
-
renderViewport(xax, yax, lo, hi)[source]¶ Context manager which sets and restores the viewport via
setRenderViewport()andrestoreViewport().
-
setRenderViewport(xax, yax, lo, hi)[source]¶ Configures the GL viewport for a 2D orthographic display. See the
routines.show2D()function.The existing viewport settings are cached, and can be restored via the
restoreViewport()method.- Parameters
xax – The display coordinate system axis which corresponds to the horizontal screen axis.
yax – The display coordinate system axis which corresponds to the vertical screen axis.
lo – A tuple containing the minimum
(x, y, z)display coordinates.hi – A tuple containing the maximum
(x, y, z)display coordinates.
-
restoreViewport()[source]¶ Restores the GL viewport settings which were saved via a prior call to
setRenderViewport().
-
target(*args, **kwargs)[source]¶ Context manager which binds and unbinds this
RenderTextureas the render target, viabindAsRenderTarget()andunbindAsRenderTarget().If any arguments are provided, the viewport is also set and restored via
setRenderViewport()andrestoreViewport().
-
bindAsRenderTarget()[source]¶ Configures the frame buffer and render buffer of this
RenderTextureas the targets for rendering.The existing farme buffer and render buffer are cached, and can be restored via the
unbindAsRenderTarget()method.
-
unbindAsRenderTarget()[source]¶ Restores the frame buffer and render buffer which were saved via a prior call to
bindAsRenderTarget().
-
doRefresh()[source]¶ Overrides
Texture2D.doRefresh(). Calls the base-class implementation, and ensures that the frame buffer and render buffer of thisRenderTextureare configured correctly.
-
draw(*args, **kwargs)[source]¶ Overrides
Texture2D.draw(). Calls that method, optionally using the information in the depth texture.- Parameters
useDepth – Must be passed as a keyword argument. Defaults to
False. IfTrue, and thisRenderTexturewas configured to use a depth texture, the texture is rendered with depth information using a fragment program
A
RuntimeErrorwill be raised ifuseDepth is True, but thisRenderTexturewas not configured appropriately (the'cd'setting in__init__()).
-
__module__= 'fsleyes.gl.textures.rendertexture'¶
-
class
fsleyes.gl.textures.rendertexture.GLObjectRenderTexture(name, globj, xax, yax, maxResolution=2048)[source]¶ Bases:
fsleyes.gl.textures.rendertexture.RenderTextureThe
GLObjectRenderTextureis aRenderTextureintended to be used for renderingGLObjectinstances off-screen.The advantage of using a
GLObjectRenderTextureover aRenderTextureis that aGLObjectRenderTexturewill automatically adjust its size to suit the resolution of theGLObject- see theGLObject.getDataResolution()method.In order to accomplish this, the
setAxes()method must be called whenever the display orientation changes, so that the render texture size can be re-calculated.-
__init__(name, globj, xax, yax, maxResolution=2048)[source]¶ Create a
GLObjectRenderTexture.- Parameters
name – A unique name for this
GLObjectRenderTexture.globj – The
GLObjectinstance which is to be rendered.xax – Index of the display coordinate system axis to be used as the horizontal render texture axis.
yax – Index of the display coordinate system axis to be used as the vertical render texture axis.
maxResolution – Maximum resolution in pixels, along either the horizontal or vertical axis, for this
GLObjectRenderTexture.
-
destroy()[source]¶ Must be called when this
GLObjectRenderTextureis no longer needed. Removes the update listener from theGLObject, and callsRenderTexture.destroy().
-
__module__= 'fsleyes.gl.textures.rendertexture'¶
-
setAxes(xax, yax)[source]¶ This method must be called when the display orientation of the
GLObjectchanges. It updates the size of thisGLObjectRenderTextureso that the resolution and aspect ratio of theGLOBjectare maintained.
-
property
shape¶ Return a tuple containing the texture data shape.
-
__updateShape(*a)¶ Updates the size of this
GLObjectRenderTexture, basing it on the resolution returned by theGLObject.getDataResolution()method. If that method returnsNone, a default resolution is used.
-