Version 7 (modified by robert, 7 years ago)

--

Where in the world am I?

To compute where a particular node is the scene graph is, in world coordinates, you need to acquire the accumulated 4x4 matrix from the root of the scene graph down to your node. osg::Node contains a convenience method, Node::getWorldMatrices(..), that walks the parental chain from your node to the root(s) of your scene graph, returning a list of matrices. A list of matrices is returned rather than a single matrix as the OpenSceneGraph supports sharing of nodes, so they may have multiple parents, and if the parent chain has separate transforms in them the different paths will result in different matrices - in this case you node is effectively in more than one place at once! Multiple parents would be typical in cases such as a wheel subgraph that is reused four times on a car, each with a transform above it to place it in the appropriate place.

To use this feature use code such as:

 
   osg::MatrixList worldMatrices = myNode->getWorldMatrices();

   for(osg::MatrixList::iterator itr = worldMatrices.begin();
       itr != worldMatrices.end();
       ++itr)
   {
       osg::Matrix& matrix = *itr;
       osg::Vec3 center = myNode->getBound().center() * matrix;
       std::cout<<"Node "<<myNode->getName()<<" center = "<<center<<" worldMatrix = "<<matrix<<std::end;
   }