OpenSceneGraph: Callbacks www.openscenegraph.org

Christopher Jaynes Center for Visualization and Virtual Environments

Dept. of Computer Science University of Kentucky

GSN'03 - Portland, ME

Application Callbacks

• Goal • Setup callback to update a node of a • Approach • Example uses solar system example (see previous lectures) • Setup callback to manage planet position as it orbits the sun • Tutorial demonstrates how to use a callback to continually update nodes • Occurs as part of the update traversal performed once each frame.

GSN'03 - Portland, ME Openscenegraph callbacks

• Developer can interact with a scene graph using callbacks.

• Callbacks: user-defined functions that are automatically executed depending on the type of traversal (update, cull, draw) being performed.

• Associated with individual nodes or with selected types (or subtypes) of nodes.

• During each traversal of a scene graph if a node is encountered that has a user-defined callback associated with it, that callback is executed.

• See chapter four of David Eberly's '3D Design' and SGI's Chapter Four of the 'Performer Programmer's Guide'.

GSN'03 - Portland, ME

Callback

Update traversal

User defined function

User data

GSN'03 - Portland, ME Creating a Callback

• code associated with update callbacks happens once per frame

• before the cull traversal • One approach: code could inserted in the main simulation loop between the viewer.update() and viewer.frame() calls. • However: callbacks provide an interface that is easier to update and maintain.

• Code that takes advantage of callbacks can also be more efficient when a multithreaded processing mode is used.

GSN'03 - Portland, ME

Accessing User Data

• Callbacks often need access to user defined data • Typically program only has access to a node pointer and a nodeVisitor during the traversal • Make use of the userData member of the node class • pointer to an instance of a user defined class containing any data fields the user wants to associate with a particular node. • This user defined class is that it must be derived from the osg::Referenced class. • The referenced class helps users manage memory by providing smart pointers. • Keep a count of the number of references • Deleted when their reference count reaches zero.

GSN'03 - Portland, ME User Data

• We will define an object class to manage planetary data • Extends referenced class

class planetDataType : public osg::Referenced { public: // ... (public methods to follow) ... protected: !!!osgSim::DOFTransform* planetNode; !!! !!!double rotation; !!!double tilt; double orbitRadius; double planetRadius; };

GSN'03 - Portland, ME

Updating user data object

• We’ll now define a few methods that operate on the user defined data

void tankDataType::updatePlanetOrbit() { !!!orbit += 0.01; !!!planetNode->setCurrentHPR( osg::Vec3(orbit,0,0) ); }

GSN'03 - Portland, ME Defining up the callback `

• Geometric transform must be applied to transform node to position object class planetNodeCallback : public osg::NodeCallback { public: !!!virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) !!!{ !!!!!!osg::ref_ptr planetData = !!!!!!!!!dynamic_cast (node->getUserData() ); !!!!!!if(planetData) !!!!!!{ !!!!!!!!!planetData->updateRotation(); !!!!!!} !!!!!!traverse(node, nv); !!!} }; GSN'03 - Portland, ME

• Now we must install the callback object

// .. code to initialize variables, load model, build scene ... planetDataType* planetData = new planetDataType(planetNode); planetNode->setUserData( planetData ); planetNode->setUpdateCallback(new planetNodeCallback);

GSN'03 - Portland, ME Simulation loop

• Enter the simulation loop where keyboard events are handled • Scenegraph is parsed in the manner defined by osg

while( !viewer.done() ) { !!!// wait for all cull and draw threads to complete. !!!viewer.sync(); !!!// Initiate scene graph traversal to update nodes. !!!// Animation nodes will require update. Additionally, !!!// any node for which an 'update' callback has been !!!// set up will also be updated. More information on !!!// settting up callbacks to follow.

!!!viewer.update(); !!!// initiate the cull and draw traversals of the scene. !!!viewer.frame(); }

GSN'03 - Portland, ME