8.5. Scene GraphsA SCENE GRAPH is a hierarchical data structure containing a description of a scene to be rendered. Rather than explicitly coding a scene to be rendered as OpenGL API calls, an application builds a scene graph, then calls the scene graph rendering engine to do the actual rendering by traversing the data structure. In this way, a scene graph allows a developer to focus on what to draw, rather than on the details of how to draw. In typical terminology, the term scene graph is often used to describe the toolkit for both the data structure itself and the rendering engine that traverses the data structure to render the scene. Scene graphs make it possible for a developer of visualization applications to leverage the performance of OpenGL without necessarily being an expert in all the details of OpenGL; the scene graph itself encapsulates OpenGL best practices. Because a scene graph is a toolkit layer on top of OpenGL, it raises the programming abstractions closer to the application domain. As a tree, a scene graph consists of nodes that have one or more children. Since OpenGL state often has a hierarchical nature (such as the model-view matrix stack), that state can easily be mapped to nodes in the tree. Some of the attributes and benefits of scene graphs:
The typical application code for a scene graph-based rendering application is conceptually not much more than at startup, build the scenegraph (loading files from disc, etc) do forever { update the camera position and other time-varying entities in the scene (such as vehicles or particle effects) draw the scene } Although a scene graph allows a visualization developer to focus on what to draw rather than on the details of how to draw it, that does not mean the developer is isolated from direct OpenGL control: not at allthe developer could define custom rendering methods. But for many common rendering tasks, the scene graph renderer already provides a complete solution and acts as a reusable toolkit for leveraging that capability. As expected from a well-designed toolkit, there are no inviolate rules; if you as the developer really need to do something out of the ordinary, the toolkit provides the avenues necessary. The palette of nodes available for constructing the scene graph provides ways to apply attributes to be inherited by the children of that node. Here are some examples (certainly not an exhaustive list) of scene graph nodes.
By helping the developer focus on "what" rather than "how," a scene graph can simplify the use of shaders. For example, using the hierarchical nature of a scene graph, a developer can compose a scene that has a shader with default values for uniform variables near the root of the scene graph, so it affects much of the scene. Alternative values for uniform variables attached at lower nodes can specialize the shader for a particular piece of geometry. The scene graph rendering engine can take over the tasks of determining if or when to compile or link the components of the shader and when to actually send updates for uniform variables to the pipeline, depending on visibility. Several OpenGL scene graph products are currently available. OpenGL Performer (http://www.sgi.com/products/software/performer/), a commercial product available from SGI, has recently included support for OpenGL 2.0 and the OpenGL Shading Language. OpenSceneGraph (or OSG, http://www.openscenegraph.org/) is an open source project that also has recently included support for OpenGL 2.0 and the OpenGL Shading Language. OpenSG (http://www.opensg.org) is another open source OpenGL scene graph. Depending on your application, any one of these could save you a great deal of time and application development effort. |