|
模型Node节点同时是几个PositionAttitudeTransform变换的子节点,模型Node下包含一个DOF节点,当几个PositionAttitudeTransform变换时,模型Node随之发生不同的变换,当其下的DOF节点则随着最后一次PositionAttitudeTransform的变换而全部变换,有没有解决该问题的办法啊?附上相关代码,希望高手们帮忙分析解决下,谢谢:
osg::Group* root = new osg::Group();
osg::Group* tankOneGroup = NULL;
osg::Group* tankTwoGroup = NULL;
osg::Group* tankThreeGroup = NULL;
osgViewer::Viewer viewer;
// Load the models from the same file
tankOneGroup = dynamic_cast<osg::Group*>
(osgDB::readNodeFile("../NPS_Data/Models/t72-tank/t72-tank_des.flt"));
tankTwoGroup = tankOneGroup;
tankThreeGroup = tankOneGroup;
// findNodeVisitor will turn osgSim:OFTransform nodes
// animation field to false
tankOneGroup->accept( *(new findNodeVisitor("")) );
// add the first tank as a child of the root node
root->addChild(tankOneGroup);
// declare a transform for positioning the second tank
osg:ositionAttitudeTransform* tankTwoPAT =
new osg::PositionAttitudeTransform();
// move the second tank five units right, five units forward
tankTwoPAT->setPosition( osg::Vec3(5,5,0) );
// add the tank as a child of its transform to the scene
tankTwoPAT->addChild(tankTwoGroup);
root->addChild(tankTwoPAT);
// declare a transform for positioning the third tank
osg::PositionAttitudeTransform* tankThreePAT =
new osg::PositionAttitudeTransform();
// move the third tank ten units right
tankThreePAT->setPosition( osg::Vec3(10,0,0) );
// rotate the tank model 22.5 degrees to the left
// (to demonstrate that rotation of the turret will be
// relative to the tank's heading)
tankThreePAT->setAttitude( osg:uat(3.14159/8.0, osg::Vec3(0,0,1) ));
// add the tank as a child of its transform to the scene
tankThreePAT->addChild(tankThreeGroup);
root->addChild(tankThreePAT);
// Declare an instance of 'findNodeVisitor' class and set its
findNodeVisitor findNode("gun");
// Initiate traversal of this findNodeVisitor instance starting
// from tankTwoGroup, searching all its children. Build a list
// of nodes whose names matched the searchForName string above.
tankTwoGroup->accept(findNode);
// Declare a switch type and assign it to the first node
// in our list of matching nodes.
osgSim::DOFTransform * tankDOF =
dynamic_cast<osgSim::DOFTransform *> (findNode.getFirst()) ;
if (tankDOF)
{
tankDOF->setCurrentHPR( osg::Vec3(-3.14159/2.0,0.0,0.0) );
}
// Declare an instance of 'findNodeVisitor'; set the name to search
// for to "turret"
findNodeVisitor findTurretNode("turret");
// Initiate a traversal starting from the subtree that represents
// the third tank model we loaded.
tankThreeGroup->accept(findTurretNode);
// Make sure we found a node and it's the correct type
osgSim::DOFTransform * turretDOF =
dynamic_cast<osgSim::DOFTransform *> (findTurretNode.getFirst()) ;
// if it's a valid DOF node, set the heading of the turret
// to 22.5 degrees right relative to the tank's heading.
if (turretDOF)
{
turretDOF->setCurrentHPR( osg::Vec3(-3.14159/4.0,0.0,0.0) );
}
// Declare an instance of 'findNodeVisitor'; set the name to search
// for to "gun"
findNodeVisitor findGunNode("gun");
// Initiate a traversal from the subtree that represents the third
// tank model we loaded.
tankThreeGroup->accept(findGunNode);
// Make sure we found a node and it's the correct type
osgSim::DOFTransform * gunDOF =
dynamic_cast<osgSim::DOFTransform *> (findGunNode.getFirst()) ;
// If the node we found is a valid DOF transform, set the pitch
// to 22.5 degrees up relative to the turret's pitch.
if (gunDOF)
{
gunDOF->setCurrentHPR( osg::Vec3(0.0,3.14159/8.0,0.0) );
}
viewer.addEventHandler(new osgViewer::StatsHandler);
viewer.setCameraManipulator(new osgGA::TrackballManipulator());
viewer.setSceneData( root );
viewer.realize();
while( !viewer.done() )
{
viewer.frame();
}
以上显示结果是坦克模型的大炮姿态一样。 |
|