The nodes in OSG mainly use callbacks (CallBack) to complete the work that the user temporarily needs to perform per frame. The timing of the callback function being invoked
is divided into update callbacks (CallBack) and human-Computer interaction time Callbacks (Event CallBack). The former in each frame the system traverses the
The current node, which is triggered by an interactive event, such as manipulating the keyboard, the mouse, closing the window, changing the window size, and so on. Callback Class base class
is Osg::nodecallback (), the main function is as follows:
//virtual function, callback function main operation in this function, subclasses should be rewritten, the corresponding operation has been completedvoid operator() (node* Node, nodevisitor*nv);//Add (delete) a successor callback object for the current update callbackvoidAddnestedcallback (nodecallback*NC);voidRemovenestedcallback (nodecallback*NC);//directly set/Get a recent callbackvoid(nodecallback*NC); Nodecallback*Getnestedcallback ();//call the next update callback in the nearvoidTraverse (node* node,nodevisitor* nv);
In the node class, complete the callback function settings and get:
// set/Get update callback for a node void Setupdatecallback (nodecallback* ); Nodecallback* getupdatecallback (); // Set/Get event callback for a node void Seteventcallback (nodecallback*); Nodecallback* geteventcallback ();
For Addnestedcallback (... ) function, the source code is as follows:
void addnestedcallback (nodecallback* nc) { if (NC) { if (_nestedcallback.valid ()) { nc->addnestedcallback (_nestedcallback. Get()); = nc; } Else { = nc;}} }
The
uses a ref_ptr<nodecallback> _nestedcallback in the Nodecallback class to store the next callback object, using a list of links to form
A sequence of callback objects, and when you want to add a neighboring callback, That is, when you call Addnestedcallback (nodecallback* NC), you use recursion to merge the sequence of two
, respectively, with THIS,NC as a header, for example: this->callback1->callback2- >callback3->null, Nc->callback4
->callback5->null. After merging, the new sequence is This->nc->callback1->callback4->callback2->callback5->callback3
->null. As for Removenestedcallback (...), it is relatively simple, as follows:
void removenestedcallback (nodecallback* nc) { if (NC) { if (_nestedcallback==nc) { = _nestedcallback->getnestedcallback (); } Else if (_nestedcallback.valid ()) { _nestedcallback-Removenestedcallback (NC); }}}
where the Traverse () function, whose function is to invoke the next adjacent callback function on the current node, has the following code:
void Nodecallback::traverse (node* node,nodevisitor* nv) { // If there are subsequent callback objects, call, overload operator "()" to implement if (_nestedcallback.valid ()) (*_nestedcallback) (NODE,NV); // after the callback operation is complete, access the node Else NV->traverse (*node);}
An example: using callbacks to implement rotation animations
#include <osg/Quat>#include<osg/PositionAttitudeTransform>#include<osg/io_utils>#include<osgDB/ReadFile>#include<osgViewer/Viewer>#include<iostream>classRotatecallback: Publicosg::nodecallback{ Public: Rotatecallback (): _rotatez (0.0) {} virtualvoidoperator () (Osg::node* node, osg::nodevisitor*NV) {osg::P ositionattitudetransform* Pat =dynamic_cast<OSG::P ositionattitudetransform*>(node); if(PAT) {OSG::VEC3 VEC (0,0,1); Osg::quat Quat=Osg::quat (OSG::D Egreestoradians (_rotatez), Osg::z_axis); Pat-setattitude (Quat); _rotatez+=0.10; } Traverse (node, NV); }Private:Double_rotatez;}; classInfocallback: Publicosg::nodecallback{ Public: Virtualvoidoperator () (Osg::node* node, osg::nodevisitor*NV) {osg::P ositionattitudetransform* Pat =dynamic_cast<OSG::P ositionattitudetransform*>(node); if(PAT) {DoubleAngle =0.0; OSG::VEC3 axis; Pat-getattitude (). Getrotate (angle, axis); Std::cout<<"Node is rotate around the axis ("<< Axis <<"), "<<osg::radianstodegrees (angle) <<"degrees"<<Std::endl; } Traverse (node, NV); } };intMainintargcChar**argv) {osg::argumentparser argument (&argc, argv); Osg::node* model =osgdb::readnodefiles (argument); if(!model) Model= Osgdb::readnodefile ("COW.OSG") ; Osg::ref_ptr<OSG::P ositionattitudetransform> Pat =NewOSG::P ositionattitudetransform (); Pat-AddChild (model); Pat->setupdatecallback (Newrotatecallback ()); Pat->addupdatecallback (Newinfocallback ()); Osgviewer::viewer Viewer; Viewer.setscenedata (PAT.Get() ); returnViewer.run ();}
OSG node updates and event callbacks