Hi Goutham, The root nodes of subgraphs your load will not typically be a MatrixTransform so a dynamic_cast<osg::MatrixTransform*> of that subgraph will return NULL.
Have a look the cessna.osg and cow.osg in a text editor to see what nodes are at the top. Robert. On 17 March 2015 at 17:24, Goutham Mudide <[email protected]> wrote: > Hi, > > I am trying to move loaded object dynamically but to do so i need to get > matrix 4x4 for that specific object. > > In below code i loaded cessna and cow models into scene and i want to > rotate only cow model?? > > I am trying this exercise based on driving the Cessna example from > Openscenegraph 3.0 BignnersGuide pgno 234. > > > > #include <osg/MatrixTransform> > #include <osgDB/ReadFile> > #include <osgGA/GUIEventHandler> > #include <osgViewer/Viewer> > #include <osgViewer/ViewerEventHandlers> > #include <osg/io_utils> > > osg::ref_ptr<osg::Group> root = new osg::Group; > class ModelController : public osgGA::GUIEventHandler > { > public: > ModelController( osg::MatrixTransform* node ) > : _model(node) > {} > virtual bool handle( const osgGA::GUIEventAdapter& ea, > osgGA::GUIActionAdapter& aa ); > protected: > osg::ref_ptr<osg::MatrixTransform> _model; > }; > > > > bool ModelController::handle( const osgGA::GUIEventAdapter& ea, > osgGA::GUIActionAdapter& aa ) > { > if ( !_model ) > { > std::cout<<"Not MODEL"<<std::endl; > return false; > } > osg::Matrix matrix = _model->getMatrix(); > std::cout<<"MATRIX+"<<_model->getMatrix()<<std::endl; > switch ( ea.getEventType() ) > { > case osgGA::GUIEventAdapter::KEYDOWN: > switch ( ea.getKey() ) > { > case 'a': case 'A': > matrix *= osg::Matrix::rotate(-0.1f, osg::Z_AXIS); > break; > case 'd': case 'D': > matrix *= osg::Matrix::rotate(0.1f, osg::Z_AXIS); > break; > case 'w': case 'W': > matrix *= osg::Matrix::rotate(-0.1f, osg::X_AXIS); > break; > case 's': case 'S': > matrix *= osg::Matrix::rotate(0.1f, osg::X_AXIS); > break; > default: > break; > } > _model->setMatrix( matrix ); > break; > default: > break; > } > return false; > } > > int main(int argc,char *argv[]) > { > osg::ref_ptr<osg::Node> model = osgDB::readNodeFile( "cessna.osg"); > osg::ref_ptr<osg::Node> model1 = osgDB::readNodeFile( "cow.osg"); > > > > osg::ref_ptr<osg::MatrixTransform> mt = new osg::MatrixTransform; > mt->addChild( model.get() ); > mt->addChild(model1.get()); > > > root->addChild( mt.get() ); > > osg::MatrixTransform *mt1 = dynamic_cast <osg::MatrixTransform*> > (model1.get()); > osg::ref_ptr<ModelController> ctrler =new ModelController(mt1); > > osgViewer::Viewer viewer; > viewer.addEventHandler( ctrler.get() ); > viewer.getCamera()->setViewMatrixAsLookAt( > osg::Vec3(0.0f,-100.0f,0.0f), osg::Vec3(), osg::Z_AXIS ); > viewer.setSceneData( root.get() ); > viewer.addEventHandler(new osgViewer::WindowSizeHandler); > return viewer.run(); > } > > Thank you! > > Cheers, > Goutham > > ------------------ > Read this topic online here: > http://forum.openscenegraph.org/viewtopic.php?p=63139#63139 > > > > > _______________________________________________ > osg-users mailing list > [email protected] > http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org > >
_______________________________________________ osg-users mailing list [email protected] http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

