外国工业设计网站,安仁网络推广软件定制开发,网站设计公司域名服务器建设,竞价推广代理若该文为原创文章#xff0c;未经允许不得转载 本文章博客地址#xff1a;https://blog.csdn.net/qq21497936/article/details/143932273 各位读者#xff0c;知识无穷而人力有穷#xff0c;要么改需求#xff0c;要么找专业人士#xff0c;要么自己研究
长沙红胖子Qt…若该文为原创文章未经允许不得转载 本文章博客地址https://blog.csdn.net/qq21497936/article/details/143932273 各位读者知识无穷而人力有穷要么改需求要么找专业人士要么自己研究
长沙红胖子Qt长沙创微智科博文大全开发技术集合包含Qt实用技术、树莓派、三维、OpenCV、OpenGL、ffmpeg、OSG、单片机、软硬结合等等持续更新中…
OSG开发专栏点击传送门
上一篇《OSG开发笔记三十二深入理解相机视口、制作支持与主视图同步变换旋转的相机HUD》 下一篇《OSG开发笔记三十四 OsgUtil::Simplifier简化几何体提升显示性能和渲染效率》 前言 前面的相机hud可以单独显示图形继续深入研究相机hud技术就是子视图了实现该功能的直接技术是从相机技术。 本篇描述osg从相机技术 Demo 相机视口的关键调用
是否清除颜色深度缓存清除
pCamera-setClearMask(GL_DEPTH_BUFFER_BIT);如果不清除颜色缓存渲染的窗口中若无内容则将其他窗口渲染的内容显示到当前窗口。
设置渲染顺序最后渲染
// 设置POST渲染顺序(最后渲染)
pCamera-setRenderOrder(osg::Camera::POST_RENDER);后渲染的优先级比较高最后显示显示优先级最高。
设置是否接受事件不接受
// 设置为不接收事件,始终得不到焦点
pCamera-setAllowEventFocus(false);设置视口大小
// 视口就是引擎三维的区域但是注意区别于屏幕的坐标系屏幕是左上为00而三维引擎是左下为00
pSlaveFrontCamera-setViewport(0,0,rect().width() / 4,rect().height() / 4);设置从相机故过程
步骤一新建相机
osg::ref_ptrosg::Camera pSlaveFrontCamera new osg::Camera;步骤二设置上下文
pSlaveFrontCamera-setGraphicsContext(_pViewer-getWindow());步骤三设置视图区域
// 视口就是引擎三维的区域但是注意区别于屏幕的坐标系屏幕是左上为00而三维引擎是左下为00
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_ptrosg::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_ptrosg::Group pGroup new osg::Group;// 绘制盒体立方体、长方体{osg::ref_ptrosg::Geode pGeode new osg::Geode;// 创建专门指明精细度的类osg::TessellationHints并设置对应精细度osg::ref_ptrosg::TessellationHints pHints new osg::TessellationHints;pHints-setDetailRatio(0.5);// 绘制几何类型(几何体)qreal width 5.0f;// 函数1pGeode-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);}
#endifpGroup-addChild(pGeode);}// 创建多视口相机{
#if 0// 这里改用了自己窗口已经创建的这块废掉了但是保留基本的核心思想是一样的osg::ref_ptrosg::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_ptrosg::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-sharedContext 0;}LOG pTraits-x pTraits-y pTraits-width pTraits-height;osg::ref_ptrosg::GraphicsContext pGraphicsContext osg::GraphicsContext::createGraphicsContext(pTraits.get());if(!pGraphicsContext-valid()){LOG if(!pGraphicsContext-valid());return pGroup.get();}
#endifdouble angle 15.0f;#if 1// 前osg::ref_ptrosg::Camera pSlaveFrontCamera new osg::Camera;pSlaveFrontCamera-setGraphicsContext(_pViewer-getWindow());// 视口就是引擎三维的区域但是注意区别于屏幕的坐标系屏幕是左上为00而三维引擎是左下为00pSlaveFrontCamera-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);
#endif#if 1// 后osg::ref_ptrosg::Camera pSlaveBehindCamera new osg::Camera;pSlaveBehindCamera-setGraphicsContext(_pViewer-getWindow());// 视口就是引擎三维的区域但是注意区别于屏幕的坐标系屏幕是左上为00而三维引擎是左下为00pSlaveBehindCamera-setViewport(0,rect().width() / 4 * 3,rect().width() / 4,rect().height() / 4);pSlaveBehindCamera-setRenderOrder(osg::Camera::POST_RENDER);_pViewer-addSlave(pSlaveBehindCamera,osg::Matrix::translate(0, 0, 0),osg::Matrix::rotate(osg::DegreesToRadians(30), 1.0, 0.0, 0.0),true);
#endif#if 0// 左
// osg::ref_ptrosg::Camera pSlaveLeftCamera new osg::Camera;
// pSlaveLeftCamera-setGraphicsContext(_pViewer-getWindow());osg::ref_ptrHudRotateCamera pSlaveLeftCamera new HudRotateCamera;pSlaveLeftCamera-setGraphicsContext(_pViewer-getWindow());pSlaveLeftCamera-setMasterCamera(_pViewer-getCamera());pSlaveLeftCamera-setViewport(0,rect().height() / 8 * 3,rect().width() / 4,rect().height() / 4);pSlaveLeftCamera-setRenderOrder(osg::Camera::POST_RENDER);#if 0_pViewer-addSlave(pSlaveLeftCamera,osg::Matrix(),osg::Matrix::rotate(osg::DegreesToRadians(angle), 0.0, 0.0, 1.0),true);
#endif
#if 0// 设置相机位置观察目标点和方向osg::Vec3f vec3Eye osg::Vec3f(100, 100, 0);osg::Vec3f vec3Center osg::Vec3f(0, 0, 0);osg::Vec3f vec3Up osg::Vec3f(0, 1, 0);pSlaveLeftCamera-setViewMatrixAsLookAt(vec3Eye, vec3Center, vec3Up);_pViewer-addSlave(pSlaveLeftCamera);
#endif
#if 1// 设置slave相机的位置和方向osg::ref_ptrosg::MatrixTransform transform new osg::MatrixTransform();// 设置平移矩阵根据需要进行调整osg::Matrix matrix;matrix.makeTranslate(0, 0, 0); // 这里的x, y, z是你想要平移到的位置transform-setMatrix(matrix);transform-addChild(pGroup);_pViewer-addSlave(pSlaveLeftCamera,osg::Matrix(),osg::Matrix::rotate(osg::DegreesToRadians(15.0f), 0.0, 1.0, 1.0),true);pSlaveLeftCamera-setProjectionMatrixAsPerspective(100.0f, 1.0, 1.0f, 100.0f);
#endif#endif#if 0// 右osg::ref_ptrosg::Camera pSlaveRightCamera new osg::Camera;pSlaveRightCamera-setGraphicsContext(_pViewer-getWindow());pSlaveRightCamera-setViewport(rect().width() / 4 * 3,rect().height() / 8 * 3,rect().width() / 4,rect().height() / 4);pSlaveRightCamera-setRenderOrder(osg::Camera::POST_RENDER);_pViewer-addSlave(pSlaveRightCamera,osg::Matrix(),osg::Matrix::rotate(osg::DegreesToRadians(-angle), 0.0, 0.0, 1.0),true);
#endif#if 0// 上osg::ref_ptrosg::Camera pSlaveUpCamera new osg::Camera;pSlaveUpCamera-setGraphicsContext(_pViewer-getWindow());pSlaveUpCamera-setViewport(rect().width() / 8 * 3,0,rect().width() / 4,rect().height() / 4);pSlaveUpCamera-setRenderOrder(osg::Camera::POST_RENDER);_pViewer-addSlave(pSlaveUpCamera,osg::Matrix(),osg::Matrix::rotate(osg::DegreesToRadians(angle), 1.0, 0.0, 0.0),true);
#endif#if 0// 下osg::ref_ptrosg::Camera pSlaveDownCamera new osg::Camera;pSlaveDownCamera-setGraphicsContext(_pViewer-getWindow());pSlaveDownCamera-setViewport(rect().height() / 8 * 3,rect().height() / 8 * 6,rect().width() / 4,rect().height() / 4);pSlaveDownCamera-setRenderOrder(osg::Camera::POST_RENDER);_pViewer-addSlave(pSlaveDownCamera,osg::Matrix(),osg::Matrix::rotate(osg::DegreesToRadians(-angle), 1.0, 0.0, 0.0),true);
#endif}return pGroup.get();
}工程模板v1.36.0 入坑
入坑一设置相机就崩溃
问题 解决过程 定位到不设置相机就不崩溃然后这里是笔者自己造的Qt与OSG结合的使用了位置这里也可以查看实际打印的创建的区域坐标和大小确实也是不对 那直接把场景里面的gc赋值给他测试是可以的修改的地方有点多因为这个QtOSG是笔者根据源码原理进行调整渲染的与直接编译出来的qtosg还是有点区别总之一句话就是Qt渲染窗口里面已经有这个osg::ref_ptrosg::GraphicsContext了不用去额外建立了 删除以下代码 然后再调整相机代码还有从Qt渲染窗口里面增加拿到这个内容上下文的函数就好了。
解决 新增获取函数原本不能获取 这里实际大小为 所以外面代码直接用窗口的宽高好了笔者是铺满的这里是要缩小放前面那就是改为4/1吧 入坑二左视图没有
问题 左视图应该显示但是没显示
解决过程 改成一样的 然后不偏移试试 偏移一个小角度试试 所以是Y轴的中心不对但是我们也没有改测试绕x轴 然后绕z轴发现就z轴没有偏移 尝试单独设置添加相机的视口是无效的: 尝试单独修改同步旋转的相机去修改视口也是无效 继续尝试 是否与内置相机的视口有关系测试也无关 解决主技术方向未解决 从原始从相机技术方面暂时没有解决因为也尝试了更改矩阵、修改相机视角观看位置都没什么变化。 可以确认的是应该是相机旋转的中心不对并不是场景中心不对所以鼠标拽托中间还是在旋转而视角旋转则x和y轴存在偏移。X和y存在偏移就是左右和里外若是与屏幕有关也是上下和左右所以这里这么分析推断也不对。 代码全部放出读者有兴趣可以提供协助一起探讨。
规避解决方法 直接在相机中修改偏移旋转然后当作结点加入是可以解决而且还不能是从相机需要addChild进入 这时候拉伸有问题 变形了 终于外挂一个东西解决 但是鼠标中键按下偏移中心点会都向右理论上反向180°的y轴应该向左但是还是向右因为是场景偏移我们规避是对场景下的相机进行旋转所以实际是移动场景相机了相机里面的正反对外无效。
官方从相机示例也存在问题怀疑是osg3.4.0源码bug 为了再次深入论证是否代码问题笔者又用官方的demo实现
#include osg/Camera
#include osg/Group
#include osg/Geode
#include osg/ShapeDrawable
#include osgViewer/Viewer
#include osg/GraphicsContextint main(int argc, char *argv[])
{osg::ref_ptrosg::Group pGroup new osg::Group;// 绘制盒体立方体、长方体{osg::ref_ptrosg::Geode pGeode new osg::Geode;// 创建专门指明精细度的类osg::TessellationHints并设置对应精细度osg::ref_ptrosg::TessellationHints pHints new osg::TessellationHints;pHints-setDetailRatio(0.5);// 绘制几何类型(几何体)double width 5.0f;// 函数1pGeode-addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(0, 0, 0), width), pHints));// 设置关闭光照OFF同时旋转都能看到了光照关闭法向量不起作用{osg::StateSet *pStateSet pGeode-getOrCreateStateSet();pStateSet-setMode(GL_LIGHTING, osg::StateAttribute::ON);}pGroup-addChild(pGeode);}osg::GraphicsContext::WindowingSystemInterface * wsi osg::GraphicsContext::getWindowingSystemInterface();if(!wsi){return 0;}osg::ref_ptrosg::GraphicsContext::Traits traits new osg::GraphicsContext::Traits;traits-x 0;traits-y 0;traits-width 800;traits-height 600;traits-windowDecoration false;traits-doubleBuffer true;traits-sharedContext 0;osg::ref_ptrosg::GraphicsContext gc osg::GraphicsContext::createGraphicsContext(traits);if(!gc.valid()){return 0;}osg::ref_ptrosgViewer::Viewer viewer new osgViewer::Viewer;osg::ref_ptrosg::Camera master new osg::Camera;master-setGraphicsContext(gc);master-setViewport(0, 0, 800, 600);viewer-addSlave(master.get());osg::ref_ptrosg::Camera leftcam new osg::Camera;leftcam-setGraphicsContext(gc);leftcam-setViewport(0, 0, 800 / 2, 600 / 2);leftcam-setRenderOrder(osg::Camera::POST_RENDER);viewer-addSlave(leftcam.get(),osg::Matrix(),osg::Matrix::rotate(osg::DegreesToRadians(15.0), 0.0, 1.0, 0.0),true);viewer-setSceneData(pGroup);viewer-run();return 0;
}改进后相机代码 osg::ref_ptrosgViewer::Viewer viewer new osgViewer::Viewer;osg::ref_ptrosg::Camera master new osg::Camera;master-setGraphicsContext(gc);master-setViewport(0, 0, 800, 800);viewer-addSlave(master.get());{osg::ref_ptrosg::Camera leftcam new osg::Camera;leftcam-setGraphicsContext(gc);leftcam-setViewport(0, 0, 100, 100);leftcam-setRenderOrder(osg::Camera::POST_RENDER);viewer-addSlave(leftcam.get(),osg::Matrix(),osg::Matrix::rotate(osg::DegreesToRadians(15.0), 1.0, 0.0, 0.0),true);}{osg::ref_ptrosg::Camera leftcam new osg::Camera;leftcam-setGraphicsContext(gc);leftcam-setViewport(100, 0, 100, 100);leftcam-setRenderOrder(osg::Camera::POST_RENDER);viewer-addSlave(leftcam.get(),osg::Matrix(),osg::Matrix::rotate(osg::DegreesToRadians(15.0), 0.0, 1.0, 0.0),true);}{osg::ref_ptrosg::Camera leftcam new osg::Camera;leftcam-setGraphicsContext(gc);leftcam-setViewport(200, 0, 100, 100);leftcam-setRenderOrder(osg::Camera::POST_RENDER);viewer-addSlave(leftcam.get(),osg::Matrix(),osg::Matrix::rotate(osg::DegreesToRadians(15.0), 0.0, 0.0, 1.0),true);}{osg::ref_ptrosg::Camera leftcam new osg::Camera;leftcam-setGraphicsContext(gc);leftcam-setViewport(300, 0, 100, 100);leftcam-setRenderOrder(osg::Camera::POST_RENDER);viewer-addSlave(leftcam.get(),osg::Matrix(),osg::Matrix::rotate(osg::DegreesToRadians(180.0), 0.0, 0.0, 1.0),true);}所以是osg3.4.0的源码这块就有问题 上一篇《OSG开发笔记三十二深入理解相机视口、制作支持与主视图同步变换旋转的相机HUD》 下一篇《OSG开发笔记三十四 OsgUtil::Simplifier简化几何体提升显示性能和渲染效率》 本文章博客地址https://blog.csdn.net/qq21497936/article/details/143932273