Use MatrixTransform in OSG to translate/rotate/scale the model

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.