1. #pragma comment(lib, "osg.lib")
  2. #pragma comment(lib, "osgDB.lib")
  3. #pragma comment(lib, "osgViewer.lib")
  4. #include  "osgViewer/Viewer"
  5. #include  "osgDB/ReadFile"
  6. #include  "osg/Node"
  7. #include "osg/Shape"
  8. #include "osg/Geode"
  9. #include "osg/ShapeDrawable"
  10. int main(){
  11. //初始化视景器
  12. osg::ref_ptr<osgViewer::Viewer> viewer=new osgViewer::Viewer;
  13. //初始化场景根节点
  14. osg::ref_ptr<osg::Group> root=new osg::Group;
  15. //场景数据
  16. osg::ref_ptr<osg::Node>  node=osgDB::readNodeFile("glider.osg");
  17. root->addChild(node);
  18. //将场景数据加入视景器中
  19. viewer->setSceneData(root);
  20. //得到相机默认的参数设置
  21. osg::Vec3d eye,center,up;
  22. viewer->getCamera()->getViewMatrixAsLookAt(eye,center,up);
  23. //将相机参数打印出来
  24. printf("init eye: %f,%f,%f\n",eye._v[0],eye._v[1],eye._v[2]);
  25. printf("init center: %f,%f,%f\n",center._v[0],center._v[1],center._v[2]);
  26. printf("init up: %f,%f,%f\n",up._v[0],up._v[1],up._v[2]);
  27. //修改相机参数
  28. eye=osg::Vec3d(0.0,-10.0,0.0);
  29. center=osg::Vec3d(0.0,0.0,0.0);
  30. up=osg::Vec3d(0.0,0.0,1.0);
  31. //将参数设置给相机,并立即获取相机参数
  32. viewer->getCamera()->setViewMatrixAsLookAt(eye,center,up);
  33. viewer->getCamera()->getViewMatrixAsLookAt(eye,center,up);
  34. //将参数打印出来
  35. printf("eye: %f,%f,%f new\n",eye._v[0],eye._v[1],eye._v[2]);
  36. printf("center: %f,%f,%f new\n",center._v[0],center._v[1],center._v[2]);
  37. printf("up: %f,%f,%f new\n",up._v[0],up._v[1],up._v[2]);
  38. //仿真循环,注意不要使用viewer->run(),如果使用这个参数,上面关于相机的所有更改都会无效
  39. while(!viewer->done())
  40. {
  41. viewer->frame();
  42. }
  43. return 1;
  44. }

结果如图:

上面的代码显示初始的相机参数是:

这个参数默认是看不到东西的,因为OSG中默认的坐标系是这个样子的:

这样,如果eye在原点,center在z轴负半轴的情况下是看不到东西,所以如果不自己手动设置参数是看不到的东西的。如下面的代码将设置相机位置的代码注释掉:

  1. #pragma comment(lib, "osg.lib")
  2. #pragma comment(lib, "osgDB.lib")
  3. #pragma comment(lib, "osgViewer.lib")
  4. #include  "osgViewer/Viewer"
  5. #include  "osgDB/ReadFile"
  6. #include  "osg/Node"
  7. #include "osg/Shape"
  8. #include "osg/Geode"
  9. #include "osg/ShapeDrawable"
  10. int main(){
  11. //初始化视景器
  12. osg::ref_ptr<osgViewer::Viewer> viewer=new osgViewer::Viewer;
  13. //初始化场景根节点
  14. osg::ref_ptr<osg::Group> root=new osg::Group;
  15. //场景数据
  16. osg::ref_ptr<osg::Node>  node=osgDB::readNodeFile("glider.osg");
  17. root->addChild(node);
  18. //将场景数据加入视景器中
  19. viewer->setSceneData(root);
  20. //得到相机默认的参数设置
  21. osg::Vec3d eye,center,up;
  22. viewer->getCamera()->getViewMatrixAsLookAt(eye,center,up);
  23. //将相机参数打印出来
  24. printf("init eye: %f,%f,%f\n",eye._v[0],eye._v[1],eye._v[2]);
  25. printf("init center: %f,%f,%f\n",center._v[0],center._v[1],center._v[2]);
  26. printf("init up: %f,%f,%f\n",up._v[0],up._v[1],up._v[2]);
  27. ////修改相机参数
  28. //eye=osg::Vec3d(0.0,-10.0,0.0);
  29. //center=osg::Vec3d(0.0,0.0,0.0);
  30. //up=osg::Vec3d(0.0,0.0,1.0);
  31. ////将参数设置给相机,并立即获取相机参数
  32. //viewer->getCamera()->setViewMatrixAsLookAt(eye,center,up);
  33. //viewer->getCamera()->getViewMatrixAsLookAt(eye,center,up);
  34. ////将参数打印出来
  35. //printf("eye: %f,%f,%f new\n",eye._v[0],eye._v[1],eye._v[2]);
  36. //printf("center: %f,%f,%f new\n",center._v[0],center._v[1],center._v[2]);
  37. //printf("up: %f,%f,%f new\n",up._v[0],up._v[1],up._v[2]);
  38. //仿真循环,注意不要使用viewer->run(),如果使用这个参数,上面关于相机的所有更改都会无效
  39. while(!viewer->done())
  40. {
  41. viewer->frame();
  42. }
  43. return 1;
  44. }

在这种情况下的效果如下图,什么都看不到:

使用下面这一组测试数据后:

  1. //修改相机参数
  2. eye=osg::Vec3d(-10.0,0.0,0.0);
  3. center=osg::Vec3d(0.0,0.0,0.0);
  4. up=osg::Vec3d(0.0,0.0,1.0);

即从x轴负半轴看去,看到的应该是飞机的正面,效果如下图:

正常。在这种情况下场景中是没有漫游器的,也不存在默认的漫游器这个说法,如果需要实现场景漫游,自己加个漫游器就可以了。

注意:如果直接使用:

  1. viewer->run();

怎么修改相机参数都是无效的,
      即使加上

  1. viewer->setCameraManipulator(NULL);

也是一样,因为在

  1. viewer->run();

这个函数里会对场景中是否存在漫游器进行判断,如果没有漫游器,它会自己添加一个TrackballManipulator漫游器。

run函数的实现如下:

  1. int Viewer::run()
  2. {
  3. if (!getCameraManipulator() && getCamera()->getAllowEventFocus())
  4. {
  5. setCameraManipulator(new osgGA::TrackballManipulator());
  6. }
  7. setReleaseContextAtEndOfFrameHint(false);
  8. return ViewerBase::run();
  9. }

很容易看出问题所在了。

OSG中相机参数的更改的更多相关文章

  1. 【学习笔记】OSG中相机参数的更改

    #pragma comment(lib, "osg.lib") #pragma comment(lib, "osgDB.lib") #pragma commen ...

  2. osg设置相机参数,包括初始位置

    严重注意!!!以下设置必须在viewer.realize();之后,否则不起作用!!!! 设置相机的位置,可以通过CameraManipulator(一般是osgGA::TrackballManipu ...

  3. Three.js 中 相机的常用参数含义

    Three.js 中相机常用的参数有up.position和lookAt. position是指相机所在的位置,将人头比作相机的话,那么position就是人头的中心的位置: up类似于人的脖子可以调 ...

  4. OSG使用更新回调来更改模型

    OSG使用更新回调来更改模型 转自:http://blog.sina.com.cn/s/blog_668aae7801017gl7.html 使用回调类实现对场景图形节点的更新.本节将讲解如何使用回调 ...

  5. OSG中的示例程序简介

    OSG中的示例程序简介 转自:http://www.cnblogs.com/indif/archive/2011/05/13/2045136.html 1.example_osganimate一)演示 ...

  6. OSG中的示例程序简介(转)

    OSG中的示例程序简介 1.example_osganimate一)演示了路径动画的使用 (AnimationPath.AnimationPathCallback),路径动画回调可以作用在Camera ...

  7. osg中的视点控制

    osg中的视点控制 osg的视点控制基类是CameraManipulator, 它是一个虚基类, 有用的方法都跟home有关. 在这个类里面有三个重要的成员变量: osg::Vec3d _homeEy ...

  8. ArcEngine中License权限等级更改的问题

    曾经认为自己对于ArcGIS 开发许可问题比较理解了,并小结在<ArcEngine10.x开发的许可问题>中. 01.权限问题 今天在调用GP时失败(插值式开发,使用的是他人框架),因为需 ...

  9. Android AVD创建及设置中各参数详解

    设置AVD时有些参数比较模糊,特地找了篇文章,大家参考下! 本文根据如下的模拟器安装做一些解释: Name:自定义虚拟的名称,不能有空格或者其他非法字符,否则不能创建,即Creat AVD不能高亮点击 ...

随机推荐

  1. 【游戏框架】Phaser

    PhaserDesktop and Mobile HTML5 game framework Phaser Examples

  2. android 自动化(1)

    学习android自动化测试要感谢一个朋友耐心的指导 环境搭建:(需要java JDK 以及android SDK) JDK:http://www.oracle.com/technetwork/jav ...

  3. Win32中目录的操作

    1 系统和当前目录 1.1 获取Windows目录 UINT GetWindowsDirectory( LPTSTR lpBuffer, //BUFF的地址  UINT uSize    //BUFF ...

  4. PHP的哲学:Simple Is Hard

    原帖地址 PHP框架的繁荣是正确的发展方向吗?   作者 正文 poshboytl    发表时间:2009-01-19   最后修改:2009-04-06   做ROR有一年了, 感觉非常好.配合敏 ...

  5. OpenStack Havana 部署在Ubuntu 12.04 Server 【OVS+GRE】(二)——网络节点的安装

    序:OpenStack Havana 部署在Ubuntu 12.04 Server [OVS+GRE] 网络节点: 1.安装前更新系统 安装好ubuntu 12.04 Server 64bits后,进 ...

  6. hdoj 1276 士兵队列训练问题【模拟】

    士兵队列训练问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  7. 365. Water and Jug Problem

    莫名奇妙找了个奇怪的规律. 每次用大的减小的,然后差值和小的再减,减减减减减减到差值=0为止.(较小的数 和 差值 相等为止,这么说更确切) 然后看能不能整除就行了. 有些特殊情况. 看答案是用GCD ...

  8. IntelliJ IDEA自用快捷键 转载

    最常用快捷键- 未分类 command Binding Description defeat - Ctrl+/ 代码提示 No - Ctrl+Alt+L 格式化代码   - Ctrl+B 快速打开光标 ...

  9. 53个要点提高php效率

    用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“函数”(译注:PHP手册中说 ...

  10. java读取图片的(尺寸、拍摄日期、标记)等EXIF信息

    1.metadata-extractor是 处理图片EXIF信息的开源项目,最新代码及下载地址:https://github.com/drewnoakes/metadata-extractor 2.本 ...