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. JAVA学习笔记--方法中的参数调用是引用调用or值调用

    文献来源:<JAVA核心技术卷Ⅰ>,第4章第5节 (没有相关书籍的可看传送门) ps:测试demo因为偷懒,用的是String对象 结论:Java使用的是对象的值引用.即将任何对象所在内存 ...

  3. ArcGIS案例学习笔记-点集中最近点对和最远点对

    ArcGIS案例学习笔记-点集中最近点对和最远点对 联系方式:谢老师,135-4855-4328,xiexiaokui@qq.com 目的:对于点图层,查找最近的点对和最远的点对 数据: 方法: 1. ...

  4. 《Cocos2d-x游戏开发实战精解》学习笔记3--在Cocos2d-x中播放声音

    <Cocos2d-x游戏开发实战精解>学习笔记1--在Cocos2d中显示图像 <Cocos2d-x游戏开发实战精解>学习笔记2--在Cocos2d-x中显示一行文字 之前的内 ...

  5. JavaScript学习笔记——JS中的变量复制、参数传递和作用域链

    今天在看书的过程中,又发现了自己目前对Javascript存在的一个知识模糊点:JS的作用域链,所以就通过查资料看书对作用域链相关的内容进行了学习.今天学习笔记主要有这样几个关键字:变量.参数传递.执 ...

  6. 电磁兼容性设计学习笔记--PCB中地的布局

    http://bbs.ednchina.com/BLOG_ARTICLE_3010439.HTM PCB上元器件的布局对整个PCB板的电磁兼容性影响很大,所以从事硬件电路设计的工程师很有必要学习PCB ...

  7. Python学习笔记6-Python中re(正则表达式)模块学习

    今天学习了Python中有关正则表达式的知识.关于正则表达式的语法,不作过多解释,网上有许多学习的资料.这里主要介绍Python中常用的正则表达式处理函数. re.match re.match 尝试从 ...

  8. scala学习笔记-类型参数中协变(+)、逆变(-)、类型上界(<:)和类型下界(>:)的使用

    转载自  fineqtbull   http://fineqtbull.iteye.com/blog/477994 有位je上的同学来短信向我问起了Scala类型参数中协变.逆变.类型上界和类型下界的 ...

  9. 设计模式学习笔记——java中常用的设计模式

    单例设计模式(Singleton Pattern) 观察者模式(Observer Pattern) 工厂模式(Factory Pattern) 策略模式(Strategy Pattern) 适配器模式 ...

随机推荐

  1. HDU 2746 Cyclic Nacklace

    Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  2. POJ3080 Blue Jeans —— 暴力枚举 + KMP / strstr()

    题目链接:https://vjudge.net/problem/POJ-3080 Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total ...

  3. Java中的Lock

    同步机制是Java并发编程中最重要的机制之一,锁是用来控制多个线程访问共享资源的方式,一般来说,一个锁能防止多个线程同时访问共享资源(但是也有例外,比如读写锁).Java中可以使用synchroniz ...

  4. hdu2552

    点击打开链接 思路: 1.tan(a+b) = ( tan(a) + tan(b) ) / (1 – tan(a) * tan(b) ) 2.tan( atan(x) ) = x arctan(1/s ...

  5. java中wait和notify

    在JAVA中,是没有类似于PV操作.进程互斥等相关的方法的.JAVA的进程同步是通过synchronized()来实现的,需要说明的是,JAVA的synchronized()方法类似于操作系统概念中的 ...

  6. 书写优雅的shell脚本(二)- `dirname $0`

    在命令行状态下单纯执行 $ cd `dirname $0` 是毫无意义的.因为他返回当前路径的".". 这个命令写在脚本文件里才有作用,他返回这个脚本文件放置的目录,并可以根据这个 ...

  7. [laravel]phpunit

    step1.install phpunit composer.json require中增加 "phpunit/phpunit":"4.0.*" 执行 comp ...

  8. linux--memcache的安装和使用(转)

    memcache是高性能,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度.据说官方所说,其用户包括twitter.digg.flickr等,都是些互联网大腕呀.目前用memca ...

  9. 单选框 复选框 隐藏之后,绑定的change事件在ie中失效的问题

    有时候需要对单选框和复选框进行美化,就需要在<input type="radio">和<input type="checkbox">元素 ...

  10. 未能加载文件或程序集“Microsoft.ReportViewer.WebForms, Version=9.0.0.0, Culture=neutral, PublicKeyT

    VS2008开发的网站,本地测试没问题,上传到服务器就出错,提示: 引用内容未能加载文件或程序集“Microsoft.ReportViewer.WebForms, Version=9.0.0.0, C ...