Use MatrixTransform in OSG to translate/rotate/scale the model
Transferred from: http://www.cnblogs.com/kekec/archive/2011/08/15/2139893.html#undefined
MatrixTransform is inherited from Transform-group, so you can hook the node object underneath it.
By setting its matrix, the model transformation of its subnodes is realized.
--The local coordinate system is understood (the local coordinate system is also called the inertial coordinate system, its relative position with the model is constant during the transformation process)
The following code:
//Create a cylindrical bodyDoubleR =0.5;Doubleh =3.0; osg::vec3 ORGINPT (0.0,0.0,0.0); Osg::ref_ptr<osg::Geode> Cylindergeode =Newosg::geode;osg::ref_ptr<osg::Cylinder> Geocylinder =NewOsg::cylinder (ORGINPT, R, h); Osg::ref_ptr<osg::ShapeDrawable> cylinderdrawable =NewOsg::shapedrawable (Geocylinder.Get()); Cylinderdrawable->setcolor (OSG::VEC4 (1.0f,0.0f,0.0f,1.0f)); Cylindergeode->adddrawable (cylinderdrawable.Get()); //-- The following actions are for the local coordinate system--//shift the cylinder first (20.0, -12.0, -35.0)//then rotate the z-axis direction to the vector n direction. The z-axis of the local coordinate system is consistent with the n vector//Next, translate the rotated model along the z-direction 0.5*h Length (global coordinate system, equivalent to 0.5*h length in n direction)//Finally, the model is magnified twice timesOSG::VEC3 N (1.0,1.0, -1.0); Osg::vec3 Z (0.0,0.0,1.0); N.normalize (); Osg::ref_ptr<osg::MatrixTransform> MT =NewOSG::MATRIXTRANSFORM;MT->setmatrix (Osg::matrix::scale (OSG::VEC3 (2.0,2.0,2.0))*osg::matrix::translate (OSG::VEC3 (0,0,0.5*H)) *Osg::matrix::rotate (z, N)*osg::matrix::translate (OSG::VEC3 (20.0, -12.0, -35.0)) ; MT->addchild (Cylindergeode);
Based on the above features, the three-dimensional coordinates of the changed model can be calculated.
For the containment relationship (MatrixTransform A contains a matrixtransform b,matrixtransform B contains a model)
So, the new coordinates of the model (X, Y, Z) = (z/y) * B.matrix * A.MATRIX;
Use MatrixTransform in OSG to translate/rotate/scale the model