OpenSceneGraph
3.0
Beginner
s
Guide
Create
high-performance
virtual
reality applications
with
OpenSceneGraph,
oneof the
best
3D
graphics engines
Rui
Wang
Xuelei
Qian
Ti.i;H;:r.c.HE
1
INf-OV
i!,"A'fiOt
JSi..ii»L10 I HG< UN IVt-!! V,ITATSB
I III,J OTH L K HANNOVERJ
[
" U » i i SH ; Nopen
source
i:3m»ft«i!f *«fHM'«m:t>cmmamBIRMINGHAM -MUM8AI TlBAJB Hannover
Table
of
Contents
Preface
1Chapter
1:The
Journey
intoOpenSceneGraph
7A
quick
overview ofrendering
middleware 8Scene
graphs
8The Birth and
development
of OSG 9Components
10Why
OSG? 12WhousesOSG? 13
Havea
quick
taste 14Timeforaction
-say "Hello World" OSG
style
14Live in
community
15Summary
17Chapter
2:Compilation
and
Installationof
OpenSceneGraph
19
System requirements
20Using
the installer 20Timeforaction-
installing
OSG 21Running
utilities 26Timeforaction-
playing
withosgviewer
26Using
theproject
wizard 29Time foraction-
creating
yoursolution withoneclick 29
Prebuilts
making
trouble? 30Cross-platform building
31Starting
CMake 31Time for action
-running
CMake in GUI mode 32Setting
upoptions
35Generating packages using
Visual Studio 37Time for action-
building
withaVisualStudiosolution 37Table
of
ContentsTime for action-
building
witha UNIX makefile 38Configuring
environment variables 40Summary
41Chapter
3:Creating
Your First OSGProgram
43
Constructing
yourownprojects
44Time for action-
building
applications
with CMake 44Using
a root node 46Time for action-
improving
the "Hello World"example
47Understanding
memorymanagement
48ref_ptr<>
andReferenced
classes 48Collecting garbage:
why
and how 50Tracing
themanaged
entities 52Timefor action-
monitoring
countedobjects
52Parsing
command-linearguments
55Time for action-
reading
the model filename from the command line 55Tracing
with the notifier 57Redirecting
the notifier 57Time for
action-saving
thelog
file 58Summary
60Chapter
4:Building Geometry
Models61
How
OpenGL
drawsobjects
62Geode and Drawable classes 62
Rendering
basicshapes
63Time for action-
quickly creating simple objects
64Storing
arraydata 66Vertices andvertex
attributes
66Specifying drawing
types 68Time for action-
drawing
acoloredquad
68Indexing primitives
72Time for action-
drawing
an octahedron 73Using
polygonal techniques
77Time for action-
tessellating
apolygon
78Rereading geometry
attributes 81Customizing
aprimitive
functor 82Time for action-
collecting triangle
faces 82Implementing
yourowndrawables 86Using OpenGL drawing
calls 87Time for action-
creating
the famousOpenGL
teapot
87Summary
91Chapter
5:Managing
SceneGraph
93The
Group
interface 94Managing
parent nodes 94Time for action-
adding
modelstothescenegraph
96Traversing
thescenegraph
98Transformation
nodes 99Understanding
the matrix 100The MatrixTransform class 101
Time for action
-performing
translations of child nodes 101Switch nodes 104
Time foraction-
switching
between the normal anddamaged
Cessna 105Level-of-detail
nodes 107Time for action-
constructing
a LODCessna 108Proxy
andpaging
nodes 110Time for action-
loading
a modelatruntime 110Customizing
yourown NodeKits 112Timefor action-
animating
the switch node 113The visitor
design pattern
116Visiting
scenegraph
structures 117Time for action-
analyzing
theCessna structure 118Summary
121Chapter
6:Creating
RealisticRendering
Effects
123
Encapsulating
theOpenGL
statemachine 124Attributes and modes 124
Time for action-
setting polygon
modes of different nodes 126Inheriting
renderstates 128Timefor action-
lighting
theglider
ornot 129Playing
with fixed-function effects 131Time for action
-applying
simple fog
tomodels 134Lights
andlight
sources 136Timefor action
-creating light
sourcesin thescene 137The
Image
class 140The basis oftexture
mapping
141Time for action-
loading
andapplying
2Dtextures 143Handling rendering
order 146Time for action-
achieving
the translucent effect 148Understanding graphics
shaders 152Using uniforms
153Time for action-
implementing
a cartooncow 154Table
of
ContentsTime for action-
generating
a Beziercurve 158Summary
162Chapter
7:Viewing
the World
163
From worldto screen 164
The Camera class 165
Rendering
orderof
cameras 167Timefor action-
creating
anHUD camera 168Using
asingle
viewer 170Digging
into the simulationloop
170Time for action
-customizing
the simulationloop
172Using
acomposite
viewer 175Timefor action-
rendering
more scenes atonetime 176Changing
global display settings
179Time for action-
enabling global multisampling
180Stereo visualization 182
Time for action-
rendering anaglyph
stereo scenes 183Rendering
totextures 184Frame
buffer, pixel buffer,
and FBO 185Time
for
action-drawing
aircraftson a loaded terrain 186Summary
192Chapter
8:Animating
SceneObjects
193
Taking
referencestofunctions
193List of callbacks 194
Time foraction-
switching
nodes in theupdate
traversal 195Avoiding
conflicting
modifications 198Time
for
action-drawing
ageometry
dynamically
199Understanding
ease motions 203Animating
thetransformation
nodes 205Time for action-
making
useof the animationpath
205Changing
rendering
states 208Time
for
action-fading
in 209Playing
moviesontextures 214Time for action-
rendering
aflashing spotlight
215Creating
complex
key-frame
animations 218Channels and animation managers 220
Time for
action
-managing
animation channels221
Loading
andrendering
characters 225Time for action-
creating
anddriving
a charactersystem
225Summary
228Chapter
9:Interacting
with Outside Elements
231Variousevents 232
Handling
mouseandkeyboard inputs
233Time for action-
driving
the Cessna 234Adding
customized events 239Timeforaction-
creating
a usertimer 239Picking objects
243Intersection 243
Time for action-
clicking
andselecting
geometries
245Windows, graphics
contexts,
andcameras 249The Traits class 250
Time for action-
configuring
the traits ofarendering
window 251Integrating
OSG intoawindow 254Time for action-
attaching
OSGwithawindow handlein Win32 255
Summary
260Chapter
10:Saving
andLoading
Files 263Understanding file
I/O plugins
264Discovery
ofspecified
extension 265Supported
file formats 266The
pseudo-loader
270Timeforaction-
reading
files fromthe Internet 271Configuring third-party dependencies
272Time for action-
adding
libcurlsupport
for OSG 273Writing
yourownplugins
276Handling
the datastream 278Timefor action-
designing
andparsing
a newfile format 279Serializing
OSG nativescenes 283Creating serializers
284Time for action-
creating
serializers for user-defined classes 285Summary
289Chapter
11:Developing
Visual
Components
291Creating
billboardsina scene 292Time for action-
creating
bannersfacing
you 292Creating
texts 296Time for action
-writing
descriptions
for the Cessna 297Creating
3D texts 300Timeforaction-
creating
texts in the world space 301Creating
particle
animations 303Table
of
ContentsTime for action-
receiving
andcasting
shadows 311Implementing special
effects
315Time
for action-drawing
the outline of models 316Playing
withmoreNodeKits 318Summary
319Chapter
12:Improving Rendering Efficiency
321OpenThreads
basics 322Time
for
action-using
aseparate
data receiver thread 322Understanding
multithreadedrendering
328Time for action-
switching
between differentthreading
models 328Dynamic
sceneculling
335Occluders and occludees 336
Time
for
action-adding
occludersto acomplex
scene 337Improving
yourapplication
341Time for action-
sharing
textureswithacustomized callback 343Paging huge
scenedata 347Making
use of thequad-tree
348Time foraction-
building
aquad-tree
for massiverendering
348Summary
357Appendix:
Pop
Quiz
Answers35_9_
Chapter
2 359Dependencies
ofosgviewer
359The difference between
ALL_BUILD
and 'build all' 359Chapter
3 360Configuring
OSGpath options yourselves
360Releaseasmart
pointer
360Chapter
4 360Results of different
primitive types
360Optimizing
indexedgeometries
360Chapter
5 361Fast
dynamic casting
361Matrix
multiplications
361Chapter
6 361Lights
withoutsources 361Replacements
of built-in uniforms 361Chapter
7 362Changing
model positions
in the HUDcamera 362 Anotherway todisplay
thesame scene in differentviews 362Chapter
8 362Choosing
thealpha
setter and the callback 363Chapter
9 363Handling
eventswithin nodes 363Globaland node-related events 363
Chapter
10 363Getting
rid ofpseudo-loaders
363Understanding
the inheritance relations 364Chapter
11 364Text