- #pragma comment(lib, "osg.lib")
- #pragma comment(lib, "osgDB.lib")
- #pragma comment(lib, "osgViewer.lib")
-
- #include "osgViewer/Viewer"
- #include "osgDB/ReadFile"
- #include "osg/Node"
- #include "osg/Shape"
- #include "osg/Geode"
- #include "osg/ShapeDrawable"
-
- int main(){
-
-
- osg::ref_ptr<osgViewer::Viewer> viewer=new osgViewer::Viewer;
-
-
- osg::ref_ptr<osg::Group> root=new osg::Group;
-
-
- osg::ref_ptr<osg::Node> node=osgDB::readNodeFile("glider.osg");
-
- root->addChild(node);
-
-
- viewer->setSceneData(root);
-
-
- osg::Vec3d eye,center,up;
- viewer->getCamera()->getViewMatrixAsLookAt(eye,center,up);
-
-
- printf("init eye: %f,%f,%f\n",eye._v[0],eye._v[1],eye._v[2]);
- printf("init center: %f,%f,%f\n",center._v[0],center._v[1],center._v[2]);
- printf("init up: %f,%f,%f\n",up._v[0],up._v[1],up._v[2]);
-
-
- eye=osg::Vec3d(0.0,-10.0,0.0);
- center=osg::Vec3d(0.0,0.0,0.0);
- up=osg::Vec3d(0.0,0.0,1.0);
-
-
- viewer->getCamera()->setViewMatrixAsLookAt(eye,center,up);
- viewer->getCamera()->getViewMatrixAsLookAt(eye,center,up);
-
-
- printf("eye: %f,%f,%f new\n",eye._v[0],eye._v[1],eye._v[2]);
- printf("center: %f,%f,%f new\n",center._v[0],center._v[1],center._v[2]);
- printf("up: %f,%f,%f new\n",up._v[0],up._v[1],up._v[2]);
-
-
- while(!viewer->done())
- {
- viewer->frame();
- }
-
-
- return 1;
- }
结果如图:
上面的代码显示初始的相机参数是:
这个参数默认是看不到东西的,因为OSG中默认的坐标系是这个样子的:
这样,如果eye在原点,center在z轴负半轴的情况下是看不到东西,所以如果不自己手动设置参数是看不到的东西的。如下面的代码将设置相机位置的代码注释掉:
- #pragma comment(lib, "osg.lib")
- #pragma comment(lib, "osgDB.lib")
- #pragma comment(lib, "osgViewer.lib")
-
- #include "osgViewer/Viewer"
- #include "osgDB/ReadFile"
- #include "osg/Node"
- #include "osg/Shape"
- #include "osg/Geode"
- #include "osg/ShapeDrawable"
-
- int main(){
-
-
- osg::ref_ptr<osgViewer::Viewer> viewer=new osgViewer::Viewer;
-
-
- osg::ref_ptr<osg::Group> root=new osg::Group;
-
-
- osg::ref_ptr<osg::Node> node=osgDB::readNodeFile("glider.osg");
-
- root->addChild(node);
-
-
- viewer->setSceneData(root);
-
-
- osg::Vec3d eye,center,up;
- viewer->getCamera()->getViewMatrixAsLookAt(eye,center,up);
-
-
- printf("init eye: %f,%f,%f\n",eye._v[0],eye._v[1],eye._v[2]);
- printf("init center: %f,%f,%f\n",center._v[0],center._v[1],center._v[2]);
- printf("init up: %f,%f,%f\n",up._v[0],up._v[1],up._v[2]);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- while(!viewer->done())
- {
- viewer->frame();
- }
-
-
- return 1;
- }
在这种情况下的效果如下图,什么都看不到:
使用下面这一组测试数据后:
- eye=osg::Vec3d(-10.0,0.0,0.0);
- center=osg::Vec3d(0.0,0.0,0.0);
- up=osg::Vec3d(0.0,0.0,1.0);
即从x轴负半轴看去,看到的应该是飞机的正面,效果如下图:
正常。在这种情况下场景中是没有漫游器的,也不存在默认的漫游器这个说法,如果需要实现场景漫游,自己加个漫游器就可以了。
注意:如果直接使用:
怎么修改相机参数都是无效的,
即使加上
- viewer->setCameraManipulator(NULL);
也是一样,因为在
这个函数里会对场景中是否存在漫游器进行判断,如果没有漫游器,它会自己添加一个TrackballManipulator漫游器。
run函数的实现如下:
- int Viewer::run()
- {
- if (!getCameraManipulator() && getCamera()->getAllowEventFocus())
- {
- setCameraManipulator(new osgGA::TrackballManipulator());
- }
-
- setReleaseContextAtEndOfFrameHint(false);
-
- return ViewerBase::run();
- }
很容易看出问题所在了。
OSG中相机参数的更改
原文:http://www.cnblogs.com/lauzhishuai/p/5517669.html