|
楼主 |
发表于 2008-7-22 09:44:00
|
显示全部楼层
的确是osg::ref_ptr<osg::MatrixTransform> tran=dynamic_cast<osg::MatrixTransform*> (viewer->getSceneData());没有正确匹配,问题出在哪里呢?
我试着重新写一个简单的例子,问题还是出在匹配上,帮看看问题在哪呀~~~
回调类,从《快速入门指导》里复制过来的:- class RotateCB : public osg::NodeCallback
- {
- public:
- RotateCB() : _angle( 0. ) {}
- virtual void operator()( osg::Node* node,
- osg::NodeVisitor* nv )
- {
- // Normally, check to make sure we have an update
- // visitor, not necessary in this simple example.
- osg::MatrixTransform* mtLeft = dynamic_cast<osg::MatrixTransform*>( node );//应该没匹配到
- osg::Matrix mR, mT;
- mT.makeTranslate( -6., 0., 0. );
- mR.makeRotate( _angle, osg::Vec3( 0., 0., 1. ) );
- mtLeft->setMatrix( mR * mT );//debug的时候程序就卡在这里,mtLeft的指针为空,应该是上面没匹配到
- // Increment the angle for the next from.
- _angle += 0.01;
- // Continue traversing so that OSG can process
- // any other nodes with callbacks.
- traverse( node, nv );
- }
- protected:
- double _angle;
- };
复制代码 创建一个三角锥体:- osg::ref_ptr<osg::Node> createSceneGraph(int a,int b,int h)
- {
- // Create an object to store geometry in.
- osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
- // Create an array of four vertices.
- osg::ref_ptr<osg::Vec3Array> v = new osg::Vec3Array;
- geom->setVertexArray( v.get() );
- v->push_back( osg::Vec3( 0.f, -1.f, 0.f ) );
- v->push_back( osg::Vec3( -1.f, 0.f, -1.f ) );
- v->push_back( osg::Vec3( 1.f, 0.f, -1.f ) );
- v->push_back( osg::Vec3( 1.f, 0.f, 1.f ) );
- v->push_back( osg::Vec3( -1.f, 0.f, 1.f ) );
- v->push_back( osg::Vec3( -1.f, 0.f, -1.f ) );
- // Create an array of four colors.
- osg::ref_ptr<osg::Vec4Array> c = new osg::Vec4Array;
- geom->setColorArray( c.get() );
- geom->setColorBinding( osg::Geometry::BIND_PER_VERTEX );
- c->push_back( osg::Vec4( 1.f, 0.f, 0.f, 1.f ) );
- c->push_back( osg::Vec4( 0.f, 1.f, 0.f, 1.f ) );
- c->push_back( osg::Vec4( 0.f, 0.f, 1.f, 1.f ) );
- c->push_back( osg::Vec4( 1.f, 1.f, 0.f, 1.f ) );
- c->push_back( osg::Vec4( 1.f, 0.f, 1.f, 1.f ) );
- c->push_back( osg::Vec4( 0.f, 1.f, 0.f, 1.f ) );
- // Draw a four-vertex quad from the stored data.
- geom->addPrimitiveSet(new osg::DrawArrays( osg::PrimitiveSet::POLYGON, a,b ) );
- osg::ref_ptr<osg::Geode> geode = new osg::Geode;
- geode->addDrawable( geom.get() );
- return geode.get();
- }
复制代码 主函数,创建场景:- #include <osg/Geode>
- #include <osg/Geometry>
- #include <osgViewer/Viewer>
- #include <osgGA/TrackballManipulator>
- #include <osg/Camera>
- #include <osg/NodeCallback>
- #include <osg/Group>
- #include <osg/MatrixTransform>
- #include <osgDB/ReadFile>
- #include <osg/Notify>
- int main( int, char ** )
- {
- osg::ref_ptr<osg::Group> root =new osg::Group;
- root->addChild(createSceneGraph(0,6,-1).get());//画三角锥体的上面部分
- root->addChild(createSceneGraph(1,4,1).get());//画三角锥体的下面部分(就是那个正方型的盖子)
- osg::StateSet* state = root->getOrCreateStateSet();
- state->setMode( GL_LIGHTING,osg::StateAttribute::OFF |osg::StateAttribute::PROTECTED );
- root->setDataVariance( osg::Object::DYNAMIC );//将节点设为DYNAMIC告诉OSG我要在运行的时候更改它
- root->setUpdateCallback(new RotateCB);//设置回调函数,不用的时候可以正常运行
- // Create the viewer and set its scene data to our scene
- // graph created above.
- osgViewer::Viewer viewer;
- viewer.setSceneData(root.get());
- if (!viewer.getSceneData())
- return 1;
- // Loop and render. OSG calls RotateCB::operator()
- // during the update traversal.
- viewer.getCamera()->setProjectionMatrixAsPerspective(80., 1., 1., 100. );
- // Create a matrix to specify a distance from the viewpoint.
- osg::Matrix trans;
- trans.makeTranslate( 0., 0., -12. );
- // Rotation angle (in radians)
- double angle=0.0;
- while (!viewer.done())
- {
- // Create the rotation matrix.
- osg::Matrix rot;
- rot.makeRotate( angle, osg::Vec3( 1., 0., 0. ) );
- angle += 0.01;
- // Set the view matrix (the concatenation of the rotation and
- // translation matrices).
- viewer.getCamera()->setViewMatrix( rot * trans );
- // Draw the next frame.
- viewer.frame();
- }
- }
复制代码 以上代码在VC2005SP1+OSG2.4+Platform SDK 2003下编译通过。
[ 本帖最后由 Sailent 于 2008-7-22 09:53 编辑 ] |
|