Qt 5.15.2 三维显示功能

Qt 5.15.2 三维显示功能

三维显示效果:
在这里插入图片描述

.pro项目文件

QT       += core gui opengl 3dcore 3drender 3dinput 3dextrasgreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++17# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0SOURCES += \dialog_fbx2glb.cpp \dialog_osgb_to_b3dm.cpp \main.cpp \mainwindow.cpp \scene.cppHEADERS += \dialog_fbx2glb.h \dialog_osgb_to_b3dm.h \mainwindow.h \scene.hFORMS += \dialog_fbx2glb.ui \dialog_osgb_to_b3dm.ui \mainwindow.ui# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += targetDISTFILES += \images/211.ico \images/Clear2.ico \images/File.ico \images/Open.ico \images/opendir.png \images/openfile.png

Scene.h

#ifndef SCENE_H
#define SCENE_H#include <QObject>
#include <QWidget>#include <Qt3DCore/QEntity>
#include <Qt3DRender/QMesh>
#include <Qt3DRender/QCamera>
#include <Qt3DRender/QPointLight>
#include <Qt3DRender/QDirectionalLight>
#include <Qt3DCore/QTransform>
#include <Qt3DWindow>
#include <QOrbitCameraController>
#include <QFirstPersonCameraController>#include <Qt3DExtras/QForwardRenderer>
#include <Qt3DExtras/QPhongMaterial>
#include <Qt3DExtras/QTorusMesh>
#include <Qt3DExtras/QSphereMesh>#include <QKeyEvent>
#include <QTransform>
#include <QComponent>
#include <QQuaternion>
#include <QInputAspect>class Scene : public QObject
{Q_OBJECT
public:Qt3DCore::QEntity *rootEntity;Qt3DCore::QEntity *model;Qt3DCore::QTransform *transform;Qt3DRender::QDirectionalLight *light;//explicit Scene(Qt3DExtras::Qt3DWindow *, QObject *parent=nullptr);void SetupMesh(Qt3DRender::QMesh *mesh);void StartScene();void Camera(Qt3DExtras::Qt3DWindow *view);void setCameraCenterPoint(QVector3D center);Qt3DCore::QEntity* findChildQEntity(QString objName,Qt3DCore::QEntity* rootEnt);QVector3D computCenterPoint(Qt3DCore::QEntity* first);void initOrbitCameraController(Qt3DCore::QEntity *ent);void initFirstPersonCameraController(Qt3DCore::QEntity *ent);void init3d(Qt3DExtras::Qt3DWindow *view);void NewScene(Qt3DRender::QMesh *);void NewScene2(Qt3DRender::QMesh *mesh);void SetupTransformRoot(Qt3DCore::QEntity* model);void SetupTransform(Qt3DCore::QEntity* modelPrivate);void SetupMaterial(Qt3DCore::QEntity* model);void SetupLighting();//添加新实体Qt3DCore::QEntity* addMeshEntity(Qt3DRender::QMesh *mesh);Qt3DCore::QEntity* addEntity(Qt3DCore::QComponent *comp);//Qt3DCore::QEntity* addNewEntity();//void KeyControls(QKeyEvent *event);void wheelControls(QWheelEvent *event);void mouseControls(QMouseEvent *event);void mousePressControls(QMouseEvent *event);void paintALL();public slots:void LightXChanged(int angle);void LightYChanged(int angle);void LightZChanged(int angle);private:Qt3DExtras::Qt3DWindow *view;unsigned int backgroudColor=0x42f4bc;    //0xffffff;  0x42f4bc//QPoint m_qpRotPosOld;int m_iScloe; //滚动放大缩小尺寸//旋转double m_dRotX;double m_dRotY;double m_dRotZ;//位移double m_dMoveX;double m_dMoveY;double m_dMoveZ;//};#endif // SCENE_H

scene.cpp

#include "scene.h"
#include <cmath>Scene::Scene(Qt3DExtras::Qt3DWindow *view, QObject *parent) : QObject (parent)
{this->view=view;rootEntity = new Qt3DCore::QEntity();SetupTransformRoot(rootEntity);//view->setRootEntity(rootEntity);view->defaultFrameGraph()->setClearColor(QColor(QRgb(this->backgroudColor)));   //0x42f4bcCamera(view);StartScene();//this->initOrbitCameraController(rootEntity);//this->initFirstPersonCameraController();
}void Scene::Camera(Qt3DExtras::Qt3DWindow *view)
{//其中camera是新建的camera实体,lens表示了镜头,这个函数有着四个参数,//第一个参数是视觉域,   45.0f//第二个是分辨率(这里选择了16比9),  16.0f/9.0f//第三个参数是近面摆放位置,   0.1f//最后一个是远面放置位置,    3000.0f//后两个参数的选择当中要特别注意,只有在远近面之间的物体才会显示,所以要是想全部显示出所加入的实体,//那么近面就要足够近,远面就要足够远。// Camera//Qt3DRender::QCamera *cameraEntity = view->camera();Qt3DRender::QCamera *camera = view->camera();camera->lens()->setPerspectiveProjection(45.0f,16.0f/9.0f, 0.1f, 3000.0f);   //近面板0.1f,远面板1000.0fcamera->setViewCenter(QVector3D(-789, -2343, 10));    //0, 0, 0camera->setPosition(QVector3D(-789, -2343, 200));   //0,0,40  -200  5000  相机高度Qt3DInput::QInputAspect *input = new Qt3DInput::QInputAspect();view->registerAspect(input);//input->setCamera(cameraEntity);//使摄像机能左右转动
}void Scene::setCameraCenterPoint(QVector3D center)
{Qt3DRender::QCamera *camera = view->camera();camera->lens()->setPerspectiveProjection(45.0f,16.0f/9.0f, 0.1f, 3000.0f);   //近面板0.1f,远面板1000.0fcamera->setViewCenter(QVector3D(center.x()*1.5, center.y()*1.5, abs(center.z())));    //0, 0, 0camera->setPosition(QVector3D(center.x()*1.5, center.y()*1.5, abs(center.z())*1.5+50));   //0,0,40  -200  5000  相机高度qDebug()<<"camera point="<<camera->position();//Qt3DInput::QInputAspect *input = new Qt3DInput::QInputAspect();view->registerAspect(input);
}Qt3DCore::QEntity* Scene::findChildQEntity(QString objName,Qt3DCore::QEntity* rootEnt)
{for (Qt3DCore::QNode* childNode : rootEnt->childNodes()){QString itemName="";Qt3DCore::QEntity* testObj=dynamic_cast<Qt3DCore::QEntity*>(childNode);if(testObj){itemName=testObj->objectName();if(objName==itemName){return testObj;}}}return nullptr;
}QVector3D Scene::computCenterPoint(Qt3DCore::QEntity* first)
{QVector3D center(0, 0, 0);if(first!=nullptr){// Now iterate over all the components and calculate the center pointint totalSize=0;//for (Qt3DCore::QNode* childNode : first->childNodes()){Qt3DCore::QTransform* tranobj=dynamic_cast<Qt3DCore::QTransform*>(childNode);if(tranobj){center += tranobj->translation();totalSize+=1;}Qt3DRender::QMesh* meshobj=dynamic_cast<Qt3DRender::QMesh*>(childNode);if(meshobj){center += meshobj->geometry()->minExtent();center += meshobj->geometry()->maxExtent();totalSize+=2;}}//center /= totalSize; // compute the center point//定位相机//scene->setCameraCenterPoint(center);//}return center;
}void Scene::init3d(Qt3DExtras::Qt3DWindow *view)
{delete model;//view->defaultFrameGraph()->setClearColor(QColor(QRgb(this->backgroudColor)));Camera(view);this->StartScene();
}void Scene::StartScene()
{model = new Qt3DCore::QEntity(rootEntity);model->setObjectName("救生圈场景模型");//救生圈场景 模型   这个尺寸大小影响显示Qt3DExtras::QTorusMesh *torusMesh = new Qt3DExtras::QTorusMesh(model);torusMesh->setRadius(5);torusMesh->setMinorRadius(1);torusMesh->setRings(100);torusMesh->setSlices(20);model->addComponent(torusMesh);/*Qt3DExtras::QSphereMesh *sphereMesh = new Qt3DExtras::QSphereMesh(model);sphereMesh->setRadius(5);model->addComponent(sphereMesh);QString filename="C:/data/obj/Tile_+005_+006_OBJ/Tile_+005_+006/Tile_+005_+006.obj";//filename="D:\\data\\data3d\fbx\\HCZ.fbx";Qt3DRender::QMesh *mesh = new Qt3DRender::QMesh(model);mesh->setSource(QUrl::fromLocalFile(filename));mesh->setMeshName("objMesh");//mesh->setSource(QUrl("qrc:/data/obj/Tile_+005_+006_OBJ/Tile_+005_+006/Tile_+005_+006.obj"));//mesh->setSource(QUrl(filename));   //file:///model->addComponent(mesh);*///SetupTransform(model);SetupMaterial(model);SetupLighting();}void Scene::NewScene(Qt3DRender::QMesh *mesh)
{delete model;model = new Qt3DCore::QEntity(rootEntity);SetupMesh(mesh);SetupTransform(model);SetupMaterial(model);SetupLighting();
}
void Scene::NewScene2(Qt3DRender::QMesh *mesh)
{delete model;model = new Qt3DCore::QEntity(rootEntity);SetupMesh(mesh);SetupTransform(model);SetupMaterial(model);SetupLighting();
}
Qt3DCore::QEntity* Scene::addMeshEntity(Qt3DRender::QMesh *mesh)
{Qt3DCore::QEntity* newEnt = new Qt3DCore::QEntity(rootEntity);newEnt->addComponent(mesh);return newEnt;
}Qt3DCore::QEntity* Scene::addEntity(Qt3DCore::QComponent *comp)
{Qt3DCore::QEntity* newEnt = new Qt3DCore::QEntity(rootEntity);newEnt->addComponent(comp);return newEnt;
}Qt3DCore::QEntity* Scene::addNewEntity()
{return new Qt3DCore::QEntity(rootEntity);
}void Scene::SetupMesh(Qt3DRender::QMesh *mesh)
{model->addComponent(mesh);
}void Scene::SetupTransformRoot(Qt3DCore::QEntity* model)
{transform = new Qt3DCore::QTransform(model);transform->setScale3D(QVector3D(1, 1, 1));transform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1, 0, 0), 0));//transform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(0, 1, 0), 0));//transform->setScale(1.0f);//transform->setTranslation(QVector3D(-700.0f,-2100.0f,10.0f));model->addComponent(transform);
}
void Scene::SetupTransform(Qt3DCore::QEntity* modelPrivate)
{Qt3DCore::QTransform* tf = new Qt3DCore::QTransform(modelPrivate);tf->setScale3D(QVector3D(1, 1, 1));tf->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(0, 0, 0), 0));//tf->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(0, 1, 0), 0));//tf->setScale(1.0f);//tf->setTranslation(QVector3D(-700.0f,-2100.0f,10.0f));modelPrivate->addComponent(tf);
}void Scene::SetupMaterial(Qt3DCore::QEntity* model)
{Qt3DRender::QMaterial *material = new Qt3DExtras::QPhongMaterial(model);model->addComponent(material);
}void Scene::SetupLighting()
{Qt3DCore::QEntity *lightEntity = new Qt3DCore::QEntity(rootEntity);light = new Qt3DRender::QDirectionalLight(lightEntity);light->setColor("white");light->setIntensity(1);light->setWorldDirection(QVector3D(0,0,-1));lightEntity->addComponent(light);
}//接管相机的鼠标操作 盘旋操作
void Scene::initOrbitCameraController(Qt3DCore::QEntity *ent)
{Qt3DExtras::QOrbitCameraController *controller = new Qt3DExtras::QOrbitCameraController(ent);controller->setCamera(view->camera());}
//接管相机的鼠标操作  第一人操作
void Scene::initFirstPersonCameraController(Qt3DCore::QEntity *ent)
{Qt3DExtras::QFirstPersonCameraController *controller=new Qt3DExtras::QFirstPersonCameraController(ent);controller->setCamera(view->camera());
}void Scene::paintALL()
{//对rootEntity 整体操作transform->setTranslation(QVector3D(m_dMoveX,m_dMoveY,m_iScloe));transform->setRotation(QQuaternion(sqrt(m_dRotX*m_dRotX+m_dRotY*m_dRotY),m_dRotX,m_dRotY,m_dRotZ));}//mouse wheel +/-  放大/缩小 功能
void Scene::wheelControls(QWheelEvent *event)
{float scale=2;if(event->angleDelta().x() >0 || event->angleDelta().y()>0){//放大//scale=1.1f;transform->setTranslation(QVector3D(transform->translation().x(),transform->translation().y(),transform->translation().z()+scale));}else{   //缩小//scale=0.9f;transform->setTranslation(QVector3D(transform->translation().x(),transform->translation().y(),transform->translation().z()-scale));}/*if (event->delta() < 0) {//m_iScloe--;m_iScloe=event->delta();this->paintALL();}else if (event->delta() > 0) {//m_iScloe++;m_iScloe=event->delta();this->paintALL();}*/}void Scene::mousePressControls(QMouseEvent *event)
{if(event->button()==Qt::LeftButton){}
}void Scene::mouseControls(QMouseEvent *event)
{QPoint pos = event->pos();//左键按下+move=>旋转if( event->buttons() & Qt::LeftButton){if (pos.x()  > m_qpRotPosOld.x()){m_dRotX += 1;}else if(pos.x()  < m_qpRotPosOld.x()){m_dRotX -= 1;}if(pos.y()  > m_qpRotPosOld.y()){m_dRotY += 1;}else if(pos.y()  < m_qpRotPosOld.y()){m_dRotY -= 1;}//this->paintALL();}if( event->buttons() & Qt::RightButton)             //右键{   //右键按下+move=>移动if (pos.x()  > m_qpRotPosOld.x()){m_dMoveX += 1;}else if(pos.x()  < m_qpRotPosOld.x()){m_dMoveX -= 1;}if(pos.y()  > m_qpRotPosOld.y()){m_dMoveY -= 1;}else if(pos.y()  < m_qpRotPosOld.y()){m_dMoveY += 1;}//this->paintALL();}//rotateif(event->button() & Qt::RightButton){   //mouse right press and moveing//if(event->type()==QEvent::MouseMove){//event->MouseButtonPressint dy=pos.y()-m_qpRotPosOld.y();int dx=pos.x()-m_qpRotPosOld.x();if(dy>0){transform->setRotationY(transform->rotationY()+1);}else if(dy<0){transform->setRotationY(transform->rotationY()-1);}//if(dx>0){transform->setRotationX(transform->rotationX()+1);}else if(dx<0){transform->setRotationX(transform->rotationX()-1);}}}/**///moveif(event->button() & Qt::LeftButton){   //mouse right press and moveing//if(event->type()==QEvent::MouseMove){//event->MouseButtonPressint dy=pos.y()-m_qpRotPosOld.y();int dx=pos.x()-m_qpRotPosOld.x();if(dy>0){transform->setTranslation(QVector3D(transform->translation().x(),transform->translation().y()-2,transform->translation().z()));}else if(dy<0){transform->setTranslation(QVector3D(transform->translation().x(),transform->translation().y()+2,transform->translation().z()));}if(dx>0){transform->setTranslation(QVector3D(transform->translation().x()-2,transform->translation().y(),transform->translation().z()));}else if(dx<0){transform->setTranslation(QVector3D(transform->translation().x()+2,transform->translation().y(),transform->translation().z()));}}}//旋转显示窗口//glTranslatef(m_dMoveX, m_dMoveY, m_iScloe);//glRotatef(sqrt(m_dRotX*m_dRotX+m_dRotY*m_dRotY), m_dRotY,m_dRotX,m_dRotZ);m_qpRotPosOld = pos;
}void Scene::KeyControls(QKeyEvent *event){if (event->modifiers().testFlag(Qt::ControlModifier)){//ctrl+方向键if(event->key()==Qt::Key_Up){transform->setRotationX(transform->rotationX()-6);}if(event->key()==Qt::Key_Down){transform->setRotationX(transform->rotationX()+6);}if(event->key()==Qt::Key_Left){transform->setRotationY(transform->rotationY()-6);}if(event->key()==Qt::Key_Right){transform->setRotationY(transform->rotationY()+6);}}else if (event->modifiers().testFlag(Qt::ShiftModifier)) {//shift+方向键 上/下if(event->key()==Qt::Key_Up){  //放大transform->setTranslation(QVector3D(transform->translation().x(),transform->translation().y(),transform->translation().z()-2));}if(event->key()==Qt::Key_Down){  //缩小transform->setTranslation(QVector3D(transform->translation().x(),transform->translation().y(),transform->translation().z()+2));}}else{//方向键  moveif(event->key()==Qt::Key_Up){transform->setTranslation(QVector3D(transform->translation().x(),transform->translation().y()+1,transform->translation().z()));}if(event->key()==Qt::Key_Down){transform->setTranslation(QVector3D(transform->translation().x(),transform->translation().y()-1,transform->translation().z()));}if(event->key()==Qt::Key_Left){transform->setTranslation(QVector3D(transform->translation().x()-1,transform->translation().y(),transform->translation().z()));}if(event->key()==Qt::Key_Right){transform->setTranslation(QVector3D(transform->translation().x()+1,transform->translation().y(),transform->translation().z()));}}
}void Scene::LightXChanged(int angle)
{light->setWorldDirection(QVector3D(angle,light->worldDirection().y(),light->worldDirection().z()));
}void Scene::LightYChanged(int angle)
{light->setWorldDirection(QVector3D(light->worldDirection().x(),angle,light->worldDirection().z()));
}void Scene::LightZChanged(int angle)
{light->setWorldDirection(QVector3D(light->worldDirection().x(),light->worldDirection().y(),angle));
}

本blog地址:https://blog.csdn.net/hsg77

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/258879.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Matlab论文插图绘制模板第129期—函数网格曲面图

在之前的文章中&#xff0c;分享了Matlab函数折线图的绘制模板&#xff1a; 函数三维折线图&#xff1a; 进一步&#xff0c;再来分享一下函数网格曲面图。 先来看一下成品效果&#xff1a; 特别提示&#xff1a;本期内容『数据代码』已上传资源群中&#xff0c;加群的朋友请自…

基于SSM的社区管理系统

末尾获取源码 开发语言&#xff1a;Java Java开发工具&#xff1a;JDK1.8 后端框架&#xff1a;SSM 前端&#xff1a;Vue 数据库&#xff1a;MySQL5.7和Navicat管理工具结合 服务器&#xff1a;Tomcat8.5 开发软件&#xff1a;IDEA / Eclipse 是否Maven项目&#xff1a;是 目录…

AI+RPA+数据+机器人”,塑造多维发展的“数字员工”

世界正进入数字经济快速发展的时期。 2021年12月&#xff0c;中央网络安全和信息化委员会印发《“十四五”国家信息化规划》&#xff0c;提出加快建设数字中国&#xff0c;大力发展数字经济的总体目标&#xff1b;2022年1月&#xff0c;央行印发《金融科技发展规划&#xff08…

SpringAOP专栏二《原理篇》

上一篇SpringAOP专栏一《使用教程篇》-CSDN博客介绍了SpringAop如何使用&#xff0c;这一篇文章就会介绍Spring AOP 的底层实现原理&#xff0c;并通过源代码解析来详细阐述其实现过程。 前言 Spring AOP 的实现原理是基于动态代理和字节码操作的。不了解动态代理和字节码操作…

专业的合封芯片企业,合封芯片的赋能者——宇凡微

一、引言 随着科技的快速发展&#xff0c;芯片封装技术已经成为电子设备制造中不可或缺的一环。其中&#xff0c;合封芯片以其高效、节能、小型化的特点&#xff0c;逐渐成为市场主流。 在这篇文章中&#xff0c;我们将深入了解一家专业的合封芯片企业——宇凡微&#xff0c;…

Proteus仿真--基于ADC0808设计的调温报警器

本文介绍基于ADC0808实现的调温报警器设计&#xff08;完整仿真源文件及代码见文末链接&#xff09; 温度调节使用滑动变阻器模拟实现&#xff0c;ADC0808采集信号并输出在LCD上面显示&#xff0c;报警系统是LED灯和蜂鸣器实现声光电报警 仿真图如下 仿真运行视频 Proteus仿真…

利用乔拓云SAAS系统,快速、高效搭建小程序

a-service&#xff0c;软件即服务&#xff09;系统来搭建他们的微信小程序。SAAS系统作为一种创新的软件应用模式&#xff0c;将软件作为一种服务提供给用户&#xff0c;为用户提供了更高效、更便捷的解决方案。本文将探讨为什么越来越多的商家选择使用乔拓云这种SAAS系统搭建小…

ubuntu18.04配置cuda+cudnn+anconda+pytorch-gpu+pycharm

一、显卡驱动安装 执行nvidia-smi查看安装情况 二、cuda安装 cuda官网下载cuda_11.6.2_510.47.03_linux.run&#xff0c;安装执行 sudo sh cuda_11.6.2_510.47.03_linux.run提升安装项&#xff0c;驱动不用安装&#xff0c;即第一项&#xff08;Driver&#xff09;&#xff…

因小失大,一个普通的postman多接口顺序执行,让项目瘫痪了一天

Runner的使用 postman不仅可以单独运行某个接口&#xff0c;postman的 Runner模块可以运行多个接口&#xff0c;可以实现真正意义上的自动化接口测试 Runner的主要功能如下 按顺序调用接口&#xff0c;执行用例&#xff1b; 批量发送请求&#xff1b; 对接口数据进行参数化…

轻松操纵SQL:Druid解析器实践

一、背景 在BI&#xff08;Business Intelligence&#xff09;场景中&#xff0c;用户会频繁使用SQL查询语句&#xff0c;但在平台运作过程中&#xff0c;面临着权限管理、多数据源处理和表校验等多种挑战。 例如&#xff0c;用户可能不清楚自身是否具备对特定表&#xff08;…

2023-12-08 LeetCode每日一题(出租车的最大盈利)

2023-12-08每日一题 一、题目编号 2008. 出租车的最大盈利二、题目链接 点击跳转到题目位置 三、题目描述 你驾驶出租车行驶在一条有 n 个地点的路上。这 n 个地点从近到远编号为 1 到 n &#xff0c;你想要从 1 开到 n &#xff0c;通过接乘客订单盈利。你只能沿着编号递…

PSP - 计算蛋白质复合物链间接触的残基与面积

欢迎关注我的CSDN&#xff1a;https://spike.blog.csdn.net/ 本文地址&#xff1a;https://spike.blog.csdn.net/article/details/134884889 在蛋白质复合物中&#xff0c;通过链间距离&#xff0c;可以计算出在接触面的残基与接触面的面积&#xff0c;使用 BioPython 库 与 SA…