Changes between Version 6 and Version 7 of Support/Tutorials/FindingNodes

Show
Ignore:
Timestamp:
11/19/08 11:46:51 (6 years ago)
Author:
osg (IP: 85.145.66.238)
Comment:

Added explanation of difference between osg::Switch and osgSim::MultiSwitch?

Legend:

Unmodified
Added
Removed
Modified
  • Support/Tutorials/FindingNodes

    v6 v7  
    216216}}} 
    217217 
    218 Since we want to use the damaged state for the second model, we'll use the findNodeVisitor class to get a handle to the multiSwitch that controls the scond tank's state. This node visitor should be initiated from the group that contains the second tank. This section of code demonstrates how to declare and initialize a findNodeVisitor instance and initiate the traversal. After the traversal is complete we can retrieve the handle to the first node in our list of nodes whose names matched the string we were looking for. This will give us a handle to the multiSwitch we want to control. 
     218Since we want to use the damaged state for the second model, we'll use the findNodeVisitor class to get a handle to the multiSwitch that controls the scond tank's state. This node visitor should be initiated from the group that contains the second tank. This section of code demonstrates how to declare and initialize a findNodeVisitor instance and initiate the traversal. After the traversal is complete we can retrieve the handle to the first node in our list of nodes whose names matched the string we were looking for. This will give us a handle to the !MultiSwitch we want to control. 
    219219 
    220220{{{ 
     
    234234}}} 
    235235 
    236 == Updating a switch node == 
    237  
    238 Once we have a valid handle to a switch node, the next trick will be to change from one model state to the next. This is done using the setSingleChildOn method. The setSingleChildOn() method takes two arguments - an unsigned integer corresponding to the index of the switchSet to manipulate and an unsigned integer cooresponding to the position you want to set. In the tank example, there is only one switchSet. It can be set to undamaged and damaged states as follows: 
    239  
    240 {{{ 
    241        // make sure it's a valid handle. If it is, set the first (only)  
    242        // multi-switch: 
     236== Updating a osg::Switch versus a osgSim::!MultiSwitch node == 
     237 
     238Once we have a valid handle to a switch node, the next trick will be to change from one model state to the next. This is done using the setSingleChildOn method. The setSingleChildOn() method takes two arguments - an unsigned integer corresponding to the index of the switchSet to manipulate and an unsigned integer corresponding to the position you want to set. In the tank example, there is only one switchSet. It can be set to undamaged and damaged states as follows: 
     239 
     240{{{ 
     241       // make sure it's a valid handle. If it is, set the first (only) multi-switch: 
     242       // assuming a single switch set with values {1, 0}, activeSwitchSet 0 
    243243       if (tankSwitch) 
    244244       { 
    245           //tankSwitch->setSingleChildOn(0,false); // good model 
    246           tankSwitch->setSingleChildOn(0,true); // bad model 
    247        } 
     245          //tankSwitch->setSingleChildOn(0,0); // good model 
     246          tankSwitch->setSingleChildOn(0,1); // bad model 
     247       } 
     248       // result: switch set modified to {0, 1} 
     249}}} 
     250 
     251The core osg library supports a Switch node. The osgSim nodekit adds the !MultiSwitch node. The fundamental difference is that the !MultiSwitch node has an extra level of indirection. A osg::Switch node has a single switchset (a list of booleans of which children are on or off). The !MultiSwitch node has a collection of switchsets, which allow for predefined switch configurations. Typical usage of a !MultiSwitch node would be done as follows: 
     252 
     253{{{ 
     254       // make sure it's a valid handle. If it is, set the first (only) multi-switch. 
     255       // assuming two switch sets with values {1, 0} and {0, 1}, activeSwitchSet 0 
     256       if (tankSwitch) 
     257       { 
     258          //tankSwitch->setActiveSwitchSet(0);  // good model 
     259          tankSwitch->setActiveSwitchSet(1); // bad model 
     260       } 
     261       // result: switch sets unchanged and activeSwitchSet 1 
     262}}} 
     263 
     264The examples above do not really justify the usage of a !MultiSwitch, because they can simply be implemented using a common osg::Switch. However when loading !OpenFlight files only the more generic !MultiSwitch nodes will be generated in the scenegraph. The usage of a standard Switch node would be as follows: 
     265 
     266{{{ 
     267       osg::Switch* tankSwitch = dynamic_cast <osg::Switch*> (findNode.getFirst()); 
     268 
     269       // make sure it's a valid handle. If it is, set the first (only) multi-switch. 
     270       // assuming an osg Switch with two children 
     271       if (tankSwitch) 
     272       { 
     273          //tankSwitch->setSingleChildOn(0);  // good model 
     274          tankSwitch->setSingleChildOn(1);// bad model 
     275       } 
     276       // result: second child on 
    248277}}} 
    249278