OpenGLContext.passes.rendervisitor
Basic Rendering code implemented as a Visitor class
The RenderVisitor class provides a basic implementation
of rendering a Context/scenegraph via a Visitor pattern.
It provides support for setting up lights, backgrounds,
viewpoints, transforms, and selection-names. (Those last
two are only applicable to scenegraph-based applications).
A few helper classes provide post-node-traversal
resetting mechanisms.
Classes
class _TransformPopToken(
object
):
Pop matrix off model-view-stack
This is part of the visitor API used by the Transform method
it restores the model-view matrix stack after a Transforming
node's children are finished being visited.
This version is used when the model-view matrix stack
has not overflowed, which allows us to use the more efficient
glPopMatrix call.
class GroupPopToken(
object
):
class RenderVisitor(
Visitor
):
OpenGL Rendering visitor/traversal object for scenegraphs
This class does the "normal" setup and rendering for
"normal" scenegraph nodes, Context(s) and SceneGraph(s)
themselves. It is the base class of most RenderPass
objects (with the notable exception of Transparent
rendering passes (which do no traversal at the moment)).
Context(
self
,
node
)
Render the Context object
calls:
ContextRenderSetup(node)
ContextSetupDisplay(node)
ContextSetupBindables(node)
ContextRenderSetup(
self
,
node
)
Set up the context for rendering prior to scene rendering
This method runs even when there is a scenegraph
It sets the rendering mode to GL_RENDER, and loads
identity matrices for the projection and model
view matrices.
ContextSetupBindables(
self
,
node
)
Set up the bindable objects (viewpoints, backgrounds, lights)
If the Context object has a non-null scenegraph, as
returned by getSceneGraph(), then this method will
do nothing, as the scenegraph will be responsible for
setting up the bindable nodes.
If the Context does not have the scenegraph, then we
look for a Context.SetupBindables method, and call
that with context.SetupBindables(self)
If the Context does not have a SetupBindables method
or a scenegraph, we call:
context.Viewpoint ( self )
context.Background( self )
context.Lights ( self )
ContextSetupDisplay(
self
,
node
)
Establishes rendering parameters for context's rendering pass
This method runs even when there is a scenegraph
(Re-)establishes common rendering parameters which
may have been left in unexpected states. If the Context
object has a SetupDisplay method, we call that. Otherwise
we enable depth testing, set the depth test to GL_LESS,
enable face-culling, and set face-culling to cull back-faces.
Rendering(
self
,
node
)
Render a rendering node (Shape)
At the moment, this just calls node.Render( self )
SceneGraph(
self
,
node
)
Render a scenegraph object (SetupBindables)
This method is only called if the Context returns
a non-null value from getSceneGraph(), as it only
is called when a Scenegraph instance is encountered.
If a Scenegraph is active, we do not use the code in
ContextSetupBindables, so the Scenegraph is responsible
for setting up all of the bindable nodes it wishes
to have rendered.
calls:
SceneGraphCamera(node)
SceneGraphLights(node)
SceneGraphBackground(node)
SceneGraphBackground(
self
,
node
)
Render background for a scenegraph
The background paths found by the OverallPass are used to
transform the individual background objects to their appropriate
positions in the scene. In general, only the rotation
of the path will affect a background node, as they are
rendered around the viewpoint, rather than around a
particular object-space position.
This method relies on the pre-scanning pass implemented
by the renderpass.OverallPass object. That is not
a particularly desirable dependency, but eliminating it
will likely be quite messy.
SceneGraphCamera(
self
,
node
)
Setup the camera/viewpoint from the scenegraph
XXX
At the moment, there is no Viewpoint support in
OpenGLContext, so this method merely calls the
Context's Viewpoint method.
SceneGraphDefaultlight(
self
,
lightID
)
Create the default headlight
VRML browsers generally have a default lighting scheme
where, if there are no lights in the scene, a headlight
is created pointing from the viewpoint forward into the scene.
The light paths found by the OverallPass are used to
transform the individual light objects to their appropriate
positions in the scene.
XXX
This code is not quite right, instead of creating
a headlight, it is just creating a simple light pointing
forward from 0,0,10. What we want is a light that always
points forward from the current viewpoint in the current
view direction.
SceneGraphLights(
self
,
node
)
Render lights for a scenegraph
The default implementation limits you to eight active
lights, despite the fact that many OpenGL renderers
support more than this. There have been problems with
support for calculating light IDs beyond eight, so I
have limited the set for now.
This method relies on the pre-scanning pass implemented
by the renderpass.OverallPass object. That is not
a particularly desirable dependency, but eliminating it
will likely be quite messy.
Transform(
self
,
node
)
Render a transform object, return a finalisation token
This method attempts to catch model-view matrix overflows
and gracefully handle them. It returns "tokens" which reset
the model-view matrix to the previous condition. This is
part of the Visitor API.
Note:
Most Transforming nodes are also Grouping nodes,
so both the Transform method and the Grouping method
will be run for those nodes.
class TransformOverflowToken(
object
):
Restore previous matrix in model-view-stack overflow situations
This is part of the visitor API used by the Transform method
it restores the model-view matrix stack after a Transforming
node's children are finished being visited.
This version is used when the model-view matrix stack
has overflowed, using the more expensive glLoadMatrixd
call to do the restoration.