fdfv232 发表于 2025-2-7 02:48:46

OSG开发笔记(三十三):同时观察物体不同角度的多视图从相机技术

前言


  前面的相机hud可以单独显示图形,继续深入研究相机hud,技术就是子视图了,实现该功能的直接技术是从相机技术。
  本篇描述osg从相机技术

 
Demo


  

  

 
相机视口的关键调用


是否清除颜色深度缓存(清除)


pCamera->setClearMask(GL_DEPTH_BUFFER_BIT);
  如果不清除颜色缓存,渲染的窗口中若无内容,则将其他窗口渲染的内容显示到当前窗口。

设置渲染顺序(最后渲染)


// 设置POST渲染顺序(最后渲染)pCamera->setRenderOrder(osg::Camera::POST_RENDER);
  后渲染的优先级比较高(最后显示,显示优先级最高)。

设置是否接受事件(不接受)


// 设置为不接收事件,始终得不到焦点pCamera->setAllowEventFocus(false);
设置视口大小


// 视口就是引擎三维的区域,但是注意区别于屏幕的坐标系(屏幕是左上为0,0,而三维引擎是左下为0,0)pSlaveFrontCamera->setViewport(0,                           0,                           rect().width() / 4,                           rect().height() / 4);
 
设置从相机故过程


步骤一:新建相机


osg::ref_ptr<osg::Camera> pSlaveFrontCamera = new osg::Camera;
步骤二:设置上下文


pSlaveFrontCamera->setGraphicsContext(_pViewer->getWindow());
步骤三:设置视图区域


// 视口就是引擎三维的区域,但是注意区别于屏幕的坐标系(屏幕是左上为0,0,而三维引擎是左下为0,0)pSlaveFrontCamera->setViewport(0,                           0,                           rect().width() / 4,                           rect().height() / 4);
步骤四:设置渲染顺序


pSlaveFrontCamera->setRenderOrder(osg::Camera::POST_RENDER);
步骤五:关键步骤添加从相机


  第二个参数是缩放矩阵,第三个参数是旋转矩阵

_pViewer->addSlave(pSlaveFrontCamera,                  osg::Matrix(),                  osg::Matrix::rotate(osg::DegreesToRadians(0.0), 0.0, 0.0, 0.0),                  true);
 
Demo关键源码


osg::ref_ptr<osg::Node> OsgWidget::getMulViewCameraNode(){    // 隐藏整个demo全局的按钮面板(没用到按键的直接隐藏,不影响此Demo)    {      ui->groupBox_pannel->setVisible(false);      ui->label_cursor->setVisible(false);      ui->label_cursor_2->setVisible(false);      ui->label_msg->setVisible(false);      ui->label_state->setVisible(false);    }    osg::ref_ptr<osg::Group> pGroup = new osg::Group;    // 绘制盒体(立方体、长方体)    {      osg::ref_ptr<osg::Geode> pGeode = new osg::Geode;      // 创建专门指明精细度的类osg::TessellationHints,并设置对应精细度      osg::ref_ptr<osg::TessellationHints> pHints = new osg::TessellationHints;      pHints->setDetailRatio(0.5);      // 绘制几何类型(几何体)      qreal width = 5.0f;      // 函数1      pGeode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(0, 0, 0), width), pHints));#if 1      // 设置关闭光照:OFF,同时旋转都能看到了(光照关闭,法向量不起作用)      {            osg::StateSet *pStateSet = pGeode->getOrCreateStateSet();            pStateSet->setMode(GL_LIGHTING, osg::StateAttribute::ON);//            pStateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);      }#endif      pGroup->addChild(pGeode);    }    // 创建多视口相机    {#if 0      // 这里改用了自己窗口已经创建的,这块废掉了,但是保留,基本的核心思想是一样的      osg::ref_ptr<osg::GraphicsContext::WindowingSystemInterface> pWindowingSystemInterface                = osg::GraphicsContext::getWindowingSystemInterface();      if(!pWindowingSystemInterface.get())      {            LOG << "if(!pWindowingSystemInterface.get())";            return pGroup.get();      }      unsigned int width = 0;      unsigned int height = 0;      pWindowingSystemInterface->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0),                                                       width,                                                       height);      osg::ref_ptr<osg::GraphicsContext::Traits> pTraits = new osg::GraphicsContext::Traits;      {            pTraits->x = 0;            pTraits->y = 0;            pTraits->width = width;            pTraits->height = height;            pTraits->windowDecoration = false;            pTraits->doubleBuffer = true;            pTraits
页: [1]
查看完整版本: OSG开发笔记(三十三):同时观察物体不同角度的多视图从相机技术