OpenSceneGraph

Index

Source Code, Examples, Skeletons

License: GNU GPL 2 Requires: OSG 2.x

OSG 2.x Example - Airplane

Download OSG 2.x example (airplane)

These aircraft models originated from the FlightGear simulator.
This example demonstrates:

  • Using Visitor pattern to find a node by name.
  • Splicing a transform node to articulate part of a model (to animate propeller).
  • Local rotation using matrix multiplication with a rotation matrix.
  • Rotating a light source.

CMake skeleton

Jean-Sébastien Guay's CMake skeleton for compiling an OSG 2.x app. Put your C++ files in a src/ subdir, then customize lines with TODO.

Download CMake skeleton for OSG 2.x

Fundamentals of OSG Programming

Object Ownership

  • new osg::Class without a matching delete is an OSG programming idiom.
  • Internally, OSG is based on smart pointers with intrusive reference-counts (OSG::Referenced).
  • Passing an OSG object to an OSG API function transfers ownership to OSG. The graph will own what was attached by addChild().
  • osg::ref_ptr
    If an app needs to hold onto an OSG object after passing it to an OSG API function, the app must store it inside a osg::ref_ptr in order to increment its reference-count (to avoid unexpected deletion).

Matrix, Transform

Rotating a matrix can be done by making a rotation matrix, then doing matrix multiplication. Eg, to animate rotating an aircraft by 1 degree every frame, pre-compute a 1 degree rotation matrix from an identity matrix, then multiply it by a transform's matrix every frame. The rotation matrix can remain unchanged until the angle (rotation rate) has to change.

class
{
   osg::ref_ptr<osg::MatrixTransform> mTransform;       // rotated every frame
   osg::ref_ptr<osg::Matrix>          mRotationMatrix;  // coefficient of matrix multiplication
};

// Make a rotation matrix:
mRotationMatrix.makeRotate( degree, osg::Vec3f( 1.0f, 0.0f, 0.0f ) );  // X axis

// Rotate matrix every frame (matrix of OSG::MatrixTransform):
...
osg::Matrix m = mTransform->getMatrix();
m = mRotationMatrix * m;
mTransform->setMatrix( m );

Matrix Multiplication, Rotating Around Which Axis

Rotation of a matrix can be either in object space or fixed space.

Long way: Read a linear algebra textbook and study premultiply or postmultiply.
Short way: Run your program: if rotation is wrong, swap the coefficients.

m = mRotationMatrix * m;  // rotate around local axis
m = m * mRotationMatrix;  // rotate around fixed axis

Blending

Blending in OSG is similar to OpenGL, with the addition of using OSG's transparent bin (essentially another rendering pass) for transparent primitives.

// Enable blending, select transparent bin.
stateSet->setMode( GL_BLEND, osg::StateAttribute::ON );
stateSet->setRenderingHint( osg::StateSet::TRANSPARENT_BIN );

// Enable depth test so that an opaque polygon will occlude a transparent one behind it.
stateSet->setMode( GL_DEPTH_TEST, osg::StateAttribute::ON );

// Conversely, disable writing to depth buffer so that
// a transparent polygon will allow polygons behind it to shine thru.
// OSG renders transparent polygons after opaque ones.
osg::Depth* depth = new osg::Depth;
depth->setWriteMask( false );
stateSet->setAttributeAndModes( depth, osg::StateAttribute::ON );

// Disable conflicting modes.
stateSet->setMode( GL_LIGHTING, osg::StateAttribute::OFF );

Converting GIS Satellite Images for OSG

1st install GDAL. 2nd install VirtualPlanetBuilder.

Go to USGS Seamless Data Distribution System to get satellite imagery. In Firefox, allow usgs.gov to open popups. Pick a location, outline a small square, download. A page will popup, select options, check Orthoimagery (near bottom of popup). Not all locations have Orthoimagery data.

-rw------- 1 jimb jimb     39214 2007-11-26 13:52 84233928.aux
-rw------- 1 jimb jimb      4099 2007-11-26 13:52 84233928.prj
-rw------- 1 jimb jimb        95 2007-11-26 13:52 84233928.tfw
-rw------- 1 jimb jimb 143794609 2007-11-26 13:52 84233928.tif
-rw------- 1 jimb jimb     23160 2007-11-26 13:52 meta1.html
-rw------- 1 jimb jimb     15558 2007-11-26 13:52 Metadata.xml

To preview the TIF file, use this command to create a smaller TIF file (the TIF file is too gigantic for viewing).

gdal_translate -outsize 5% 5% orig.tif small.tif

This is the command to generate a scene graph from the satellite imagery. The same .TIF file is used as the DEM (height-map) and the texture. .ive is in face OSG's native binary format. -a neatly stores a multitude of auxillary files in the single .osga file.

osgdem -d 84233928.tif -t 84233928.tif -o sfbay.ive -a sfbay.osga -l 8 -v 0.08

Links

OSG Programming Links

   index    e.m.a.i.l
©2007,2008 Jim Brooks
Text logo courtesy of cooltext.com
Last modified: Wed Apr 2 11:04:46 EDT 2008