当前位置: 首页 > news >正文

使用Qt3D绘制机械手场景

文章目录

  • 1.前言
  • 2.效果
  • 3.实现过程
    • 3.1.场景代码
    • 3.2.自定义模型的渲染
    • 3.3.绘制直线或者网格
  • 4.有待解决的一些问题
    • 4.1.线宽的设置、背面消隐(culling)的设置
    • 4.2.法线的问题

1.前言

之前是使用Coin3D来绘制机械手场景的【Qt利用Coin3D(OpenInventor)进行3d绘图】。
后来需要在HarmonyOS显示这个机械手模型,但是想要编译Coin3D到HarmonyOS的话,显然太难了。
然后尝试使用OpenGL原生的函数来绘制,但是HarmonyOS对很多函数都不支持,一查,发现HarmonyOS支持OpenglES。那就麻烦了。
最后,一番兜兜转转,还是用回Qt3D吧。

2.效果

效果也还行,但是某些功能还得研究一下。
在这里插入图片描述点、线、面、灯光等各种功能都ok。
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/8c52fb4533714e9588d87d3ac7d45ca5.png在这里插入图片描述

3.实现过程

主要是参考了Qt自带的例程以及网上的一些资料。

3.1.场景代码

创建场景、摄像机、灯光等这些基本元素的代码,是参考Qt自带的例程实现的。比如下面的【Qt 3D Simple C++ Example】在这里插入图片描述

3.2.自定义模型的渲染

【Qt3DExample】

3.3.绘制直线或者网格

【how-do-i-draw-a-simple-line-in-Qt3D】

int drawOriginGrid(const int rows,const int cols,const float step,const QColor& color,Qt3DCore::QEntity *_rootEntity)
{auto *geometry = new Qt3DRender::QGeometry(_rootEntity);QList<QVector3D> vertexList;// 横线for(int i = -rows; i <= rows; i++){vertexList << QVector3D(cols  * step, 0, i * step);vertexList << QVector3D(-cols * step, 0, i * step);}// 竖线for(int i = -cols; i <= cols; i++){vertexList << QVector3D(i * step, 0, rows * step);vertexList << QVector3D(i * step, 0, -rows * step);}QByteArray bufferBytes;bufferBytes.resize(3 * vertexList.length() * sizeof(float));float *positions = reinterpret_cast<float*>(bufferBytes.data());for(int i = 0; i < vertexList.length(); i++){QVector3D vertex = vertexList.at(i);*positions++ = vertex.x();*positions++ = vertex.y();*positions++ = vertex.z();}auto *buf = new Qt3DRender::QBuffer(geometry);buf->setData(bufferBytes);auto *positionAttribute = new Qt3DRender::QAttribute(geometry);positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);positionAttribute->setVertexSize(3);positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);positionAttribute->setBuffer(buf);positionAttribute->setByteStride(3 * sizeof(float));positionAttribute->setCount(vertexList.length());geometry->addAttribute(positionAttribute); // We add the vertices in the geometry// connectivity between verticesQByteArray indexBytes;indexBytes.resize(2 * vertexList.length() * sizeof(unsigned int));unsigned int *indices = reinterpret_cast<unsigned int*>(indexBytes.data());for(int i = 0; i < vertexList.length(); i++){*indices++ = i;}auto *indexBuffer = new Qt3DRender::QBuffer(geometry);indexBuffer->setData(indexBytes);auto *indexAttribute = new Qt3DRender::QAttribute(geometry);indexAttribute->setVertexBaseType(Qt3DRender::QAttribute::UnsignedInt);indexAttribute->setAttributeType(Qt3DRender::QAttribute::IndexAttribute);indexAttribute->setBuffer(indexBuffer);indexAttribute->setCount(vertexList.length());geometry->addAttribute(indexAttribute); // We add the indices linking the points in the geometry// meshauto *line = new Qt3DRender::QGeometryRenderer(_rootEntity);line->setGeometry(geometry);line->setPrimitiveType(Qt3DRender::QGeometryRenderer::Lines);auto *material = new Qt3DExtras::QPhongMaterial(_rootEntity);material->setAmbient(color);material->setDiffuse(color);// entityauto *lineEntity = new Qt3DCore::QEntity(_rootEntity);lineEntity->addComponent(line);lineEntity->addComponent(material);return 0;
}

4.有待解决的一些问题

4.1.线宽的设置、背面消隐(culling)的设置

有资料说可以通过QRenderStateSet来设置,但是我设置了并没有得到有效的效果

    Qt3DExtras::Qt3DWindow *view = new Qt3DExtras::Qt3DWindow();view->defaultFrameGraph()->setClearColor(QColor(QRgb(0x2b2b2b)));if(0){using namespace Qt3DRender;qDebug() << "frameGraph:" << view->defaultFrameGraph() << view->activeFrameGraph();Qt3DRender::QRenderSurfaceSelector *surSel;auto childNodeList = view->activeFrameGraph()->childNodes();foreach (Qt3DCore::QNode * childNode, childNodeList) {qDebug() << "child:" << childNode;if(qobject_cast<Qt3DRender::QRenderSurfaceSelector*>(childNode)){surSel = qobject_cast<Qt3DRender::QRenderSurfaceSelector*>(childNode);}}qDebug() << surSel->childNodes() << surSel->childNodes()[0]->childNodes()<< surSel->childNodes()[0]->childNodes()[0]->childNodes();qDebug() << surSel->childNodes()[0]->childNodes()[0]->childNodes()[0];((Qt3DRender::QClearBuffers*)(surSel->childNodes()[0]->childNodes()[0]->childNodes()[0]))->setBuffers(QClearBuffers::ColorDepthBuffer);QRenderStateSet *renderStateSet = new QRenderStateSet(surSel->childNodes()[0]->childNodes()[0]);QCullFace *cullFace = new QCullFace();cullFace->setMode(QCullFace::NoCulling);// renderStateSet->addRenderState(cullFace);QLineWidth *lineWidth = new QLineWidth();lineWidth->setValue(100);// renderStateSet->addRenderState(lineWidth);QPointSize *ptSize = new QPointSize();ptSize->setValue(100);// renderStateSet->addRenderState(ptSize);qDebug() << surSel->childNodes()[0]->childNodes()[0]->childNodes();}

4.2.法线的问题

目前好像是支持点法线,不支持面法线,假如使用index模式来渲染面片时,会渲染出奇怪的效果
在这里插入图片描述
在这里插入图片描述

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 论文阅读:基于生物神经元的模拟游戏世界感知与学习
  • 详细介绍Linux iftop 的结果如何查看
  • 昇思25天学习打卡营第XX天|Diffusion扩散模型
  • log4j2漏洞练习(未完成)
  • MyBatis入门如何使用操作数据库及常见错误(yml配置)
  • 删除链表的倒数第N个结点(LeetCode)
  • java中的tcp
  • SpringBoot集成GraalVM创建高性能原生镜像
  • 60个常见的 Linux 指令
  • 淘宝的商品信息缓存体系是如何构建的?
  • 基于多种机器学习的豆瓣电影评分预测与多维度可视化【可加系统】
  • 孟德尔随机化、R语言,报错,如何解决?
  • 【达梦数据库】通过线程pid定位会话SQL
  • Python——继承
  • vue2学习 -- 核心语法
  • SegmentFault for Android 3.0 发布
  • 2017-09-12 前端日报
  • 2017届校招提前批面试回顾
  • Android 架构优化~MVP 架构改造
  • mysql innodb 索引使用指南
  • Mysql数据库的条件查询语句
  • SpingCloudBus整合RabbitMQ
  • Spring Boot快速入门(一):Hello Spring Boot
  • springMvc学习笔记(2)
  • 技术胖1-4季视频复习— (看视频笔记)
  • 漫谈开发设计中的一些“原则”及“设计哲学”
  • 七牛云 DV OV EV SSL 证书上线,限时折扣低至 6.75 折!
  • 使用agvtool更改app version/build
  • TPG领衔财团投资轻奢珠宝品牌APM Monaco
  • ​ 全球云科技基础设施:亚马逊云科技的海外服务器网络如何演进
  • ​​​【收录 Hello 算法】10.4 哈希优化策略
  • ​【已解决】npm install​卡主不动的情况
  • # 职场生活之道:善于团结
  • #《AI中文版》V3 第 1 章 概述
  • #中国IT界的第一本漂流日记 传递IT正能量# 【分享得“IT漂友”勋章】
  • (2024,Vision-LSTM,ViL,xLSTM,ViT,ViM,双向扫描)xLSTM 作为通用视觉骨干
  • (k8s)Kubernetes 从0到1容器编排之旅
  • (搬运以学习)flask 上下文的实现
  • (二)PySpark3:SparkSQL编程
  • (附源码)php投票系统 毕业设计 121500
  • (附源码)springboot太原学院贫困生申请管理系统 毕业设计 101517
  • (附源码)流浪动物保护平台的设计与实现 毕业设计 161154
  • (四)linux文件内容查看
  • (四)图像的%2线性拉伸
  • (续)使用Django搭建一个完整的项目(Centos7+Nginx)
  • (转) Android中ViewStub组件使用
  • . ./ bash dash source 这五种执行shell脚本方式 区别
  • .NET Core 版本不支持的问题
  • .net core 使用js,.net core 使用javascript,在.net core项目中怎么使用javascript
  • .net core 微服务_.NET Core 3.0中用 Code-First 方式创建 gRPC 服务与客户端
  • .NET gRPC 和RESTful简单对比
  • .net 程序发生了一个不可捕获的异常
  • .NET 分布式技术比较
  • .net之微信企业号开发(一) 所使用的环境与工具以及准备工作
  • [2021]Zookeeper getAcl命令未授权访问漏洞概述与解决