查看: 918|回复: 2

关于GraphicsContents的问题

[复制链接]

该用户从未签到

发表于 2011-12-13 16:20:54 | 显示全部楼层 |阅读模式
代码如下
  1.         osg::Vec3 v1(0.0,0.0,0.0);
  2.         osg::Vec3 v2(1.0,0.0,0.0);
  3.         osg::Vec3 v3(0.0,1.0,0.0);
  4.         osg::Vec3 v4(0.0,0.0,1.0);

  5.         osg::ref_ptr<osg::Geometry> t1 = new osg::Geometry;
  6.         osg::ref_ptr<osg::Geometry> t2 = new osg::Geometry;
  7.         osg::ref_ptr<osg::Geometry> t3 = new osg::Geometry;
  8.         osg::ref_ptr<osg::Geometry> t4 = new osg::Geometry;

  9.         osg::ref_ptr<osg::Vec3Array> tv1 = new osg::Vec3Array;
  10.         osg::ref_ptr<osg::Vec3Array> tv2 = new osg::Vec3Array;
  11.         osg::ref_ptr<osg::Vec3Array> tv3 = new osg::Vec3Array;
  12.         osg::ref_ptr<osg::Vec3Array> tv4 = new osg::Vec3Array;
  13.         tv1->push_back(v1);tv1->push_back(v2);tv1->push_back(v3);
  14.         tv2->push_back(v1);tv2->push_back(v3);tv2->push_back(v4);
  15.         tv3->push_back(v1);tv3->push_back(v2);tv3->push_back(v4);
  16.         tv4->push_back(v2);tv4->push_back(v3);tv4->push_back(v4);

  17.         t1->setVertexArray(tv1.get());t2->setVertexArray(tv2.get());
  18.         t3->setVertexArray(tv3.get());t4->setVertexArray(tv4.get());
  19.        
  20.         t1->addPrimitiveSet(new osg::DrawArrays(osg::DrawArrays::TRIANGLES,0,3));
  21.         t2->addPrimitiveSet(new osg::DrawArrays(osg::DrawArrays::TRIANGLES,0,3));
  22.         t3->addPrimitiveSet(new osg::DrawArrays(osg::DrawArrays::TRIANGLES,0,3));
  23.         t4->addPrimitiveSet(new osg::DrawArrays(osg::DrawArrays::TRIANGLES,0,3));


  24.         osg::ref_ptr<osg::Geode> trian = new osg::Geode;
  25.         trian->addDrawable(t1);trian->addDrawable(t2);
  26.         trian->addDrawable(t3);trian->addDrawable(t4);
  27.         osgUtil::SmoothingVisitor visitor;
  28.         visitor.smooth(*t1);visitor.smooth(*t2);
  29.         visitor.smooth(*t3);visitor.smooth(*t4);
  30.         osg::ref_ptr<osg::PolygonMode> line = new osg::PolygonMode;
  31.        
  32.         osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
  33.         traits->windowDecoration = true;
  34.         traits->x = 100;
  35.         traits->y = 100;
  36.         traits->width = 800;
  37.         traits->height = 600;
  38.         traits->doubleBuffer = true;
  39.         traits->sharedContext = 0;

  40.         osg::DisplaySettings* ds = osg::DisplaySettings::instance();
  41.         traits->alpha = ds->getMinimumNumAccumAlphaBits();
  42.         traits->stencil = ds->getMinimumNumStencilBits();
  43.         traits->sampleBuffers = ds->getMultiSamples();
  44.         traits->samples = ds->getNumMultiSamples();
  45.        
  46.         osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
  47.        
  48.         gc->setClearColor(osg::Vec4(0.2,0.5,0.0,1.0));
  49.         gc->setClearMask(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

  50.         osg::ref_ptr<osg::Camera> camera = new osg::Camera;
  51.         osg::Vec3 eye(0.0,2.0,2.0);
  52.         osg::Vec3 center(0,-0.5,-0.5);
  53.         osg::Vec3 up(0.0,1.0,0.0);
  54.         osgViewer::Viewer viewer;
  55.         viewer.setSceneData(trian.get());
  56.        
  57.        
  58.         camera->setViewMatrixAsLookAt(eye,center,up);
  59.         camera->setProjectionMatrixAsPerspective(90.0,2.0,4.0,10.0);
  60.        

  61.         camera->setGraphicsContext(gc.get());
  62.         camera->setViewport(new osg::Viewport(10,10,700,500));
  63.         camera->setClearColor(osg::Vec4(0.1,0.1,0.0,1.0));
  64.         CameraEventCallBack* ecb = new CameraEventCallBack;
  65.         //camera->setEventCallback(ecb);
  66.         camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF);
  67.         viewer.setCamera(camera.get());
  68.        
  69.        
  70.        
  71.         viewer.setSceneData(trian.get());
  72.         viewer.setCamera(camera);

  73.        
  74.         return viewer.run();
复制代码
使用上面的代码之后不能显示场景图形,显示效果如图:

显示效果

显示效果

即使调整窗口和视图的大小也还是不能显示
当去除
  1. camera->setGraphicsContext(gc.get());
复制代码
这句代码后是可以全屏显示四面体的。望指教,谢谢。

该用户从未签到

 楼主| 发表于 2011-12-13 17:09:43 | 显示全部楼层
难道大家都没遇到过这个问题……

该用户从未签到

发表于 2011-12-16 12:48:13 | 显示全部楼层
代码不全,无法判断什么;参考osgcamera例子就可以了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表