How are the MatrixTransform matrices of type parent and child nodes multiplied within the osg engine?

15 views
Skip to first unread message

mirr...@gmail.com

unread,
Jan 6, 2023, 2:26:42 AM1/6/23
to OpenSceneGraph Users
osg::ref_ptr<osg::Node> node = osgDB::readNodeFile("cow.osg");          osg::ref_ptr<osg::MatrixTransform> mt1 = new osg::MatrixTransform();     
osg::Matrix m;   
 m.makeTranslate(osg::Vec3(10.0f, 0.0f, 0.0f));    
m.makeRotate(45.0f, 1.0f, 0.0f, 0.0f);     
mt1->setMatrix(m); 
//
osg::ref_ptr<osg::MatrixTransform> mt2 = new osg::MatrixTransform();    
osg::Matrix t;   
t.makeTranslate(osg::Vec3(-10.0f, 0.0f, 0.0f));    
mt2->setMatrix(t);    
mt2->addChild(node.get());
//
mt1->addChild(mt2);

Robert Osfield

unread,
Jan 6, 2023, 11:52:59 AM1/6/23
to OpenSceneGraph Users
Could you please use the new Dicussion forum on github for future posts as this is now the official forum.

On Friday, 6 January 2023 at 07:26:42 UTC mirr...@gmail.com wrote:
osg::ref_ptr<osg::Node> node = osgDB::readNodeFile("cow.osg");          osg::ref_ptr<osg::MatrixTransform> mt1 = new osg::MatrixTransform();     
osg::Matrix m;   
 m.makeTranslate(osg::Vec3(10.0f, 0.0f, 0.0f));    
m.makeRotate(45.0f, 1.0f, 0.0f, 0.0f);     
mt1->setMatrix(m); 

The m.makeRotate(...) above will reset the matrix to just represent the rotation, the m.makeTranslate(..) will be lost.

The way to set the matrix transform would be:

mt1->setMatrix(osg::Matrix::rotate(45.0f, 1.0f, 0.0f, 0.0f) * osg::Matrix::translate(osg::Vec3(10.0f, 0.0f, 0.0f)));
 
Or at least that's what I've recall as I haven't used the OSG for a while :-)

//
osg::ref_ptr<osg::MatrixTransform> mt2 = new osg::MatrixTransform();    
osg::Matrix t;   
t.makeTranslate(osg::Vec3(-10.0f, 0.0f, 0.0f));    
mt2->setMatrix(t);    
mt2->addChild(node.get());
//
mt1->addChild(mt2);

The transform hierarchy in the scene graphs is that you have the camera's view matrix at the top, then nested within this the transforms in the scene graph and they are mulitplied together top down with the view matrix to create the modelview matrix.

For instance you'd have a Transform above a Car subgraph to place it into world coordinates, and in the Car subgraph you'd have Transform above each wheel to rotate it.
 
If you want to put this Car onto a Ship then this ship subgraph would have a Transform to place it in the world coordinate frame, and the Car's transform would be placed under the Ship Transform to place it relative to the Ship.

If you wanted to Ship to be sailing around the Earth and have the Earth obiting the sub you'd have an Earth subgraph that is underneath a Transform, and place the Ship underneath so it moves with the Earth as it glides gracefully around in orbit of sun.
Reply all
Reply to author
Forward
0 new messages