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

使用QT进行3D开发建模

一、学习使用QT中的3D开发,先创建3D运行窗口;使丑丑的3D小机器人动起来;

主要程序代码:

void Form_text::init3D()
{//step1  创建一个3d 得viewQt3DExtras::Qt3DWindow* view = new Qt3DExtras::Qt3DWindow();//此颜色设置view的背景色,内部即glClearColor()的颜色view->defaultFrameGraph()->setClearColor(QColor(QRgb(0x4d4d4f)));//如果要将 3DWidnow 嵌入到widget中,需要一个widget来装它QWidget* container = QWidget::createWindowContainer(view);QSize screenSize = view->screen()->size();container->setMinimumSize(QSize(1800, 800));container->setMaximumSize(screenSize);if (!ui->widget->layout()){ui->widget->setLayout(new QVBoxLayout());  // 强制设置布局}// 将容器添加到布局中ui->widget->layout()->addWidget(container);//创建一个跟实体dPtr->mRootEntity = new Qt3DCore::QEntity;//相机相关的属性控制Qt3DRender::QCamera* camEntity = view->camera();if (!camEntity){qWarning() << "Camera is null!";return;  // 或创建新的 Camera}camEntity->lens()->setPerspectiveProjection(45.0f, 16.0f / 9.0f, 0.1f, 1000.0f);camEntity->setPosition(QVector3D(0, 0, 10.0f));camEntity->setUpVector(QVector3D(0, 1, 0));camEntity->setViewCenter(QVector3D(0, 0, 0));//加一个Camera的 轨道控制器Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(dPtr->mRootEntity);camController->setCamera(camEntity);//创建一个点光源Qt3DCore::QEntity *lightEntity = new Qt3DCore::QEntity(dPtr->mRootEntity);Qt3DRender::QPointLight *light = new Qt3DRender::QPointLight(lightEntity);light->setColor("white");light->setIntensity(1);lightEntity->addComponent(light);//将点光源 移动到 摄像机位置Qt3DCore::QTransform *lightTransform = new Qt3DCore::QTransform(lightEntity);lightTransform->setTranslation(camEntity->position());lightEntity->addComponent(lightTransform);view->setRootEntity(dPtr->mRootEntity);{// 手指动画m_fingerAnimation = new QPropertyAnimation(this);m_fingerAnimation->setTargetObject(this);m_fingerAnimation->setPropertyName("fingerAngle");m_fingerAnimation->setStartValue(0);m_fingerAnimation->setEndValue(45);m_fingerAnimation->setDuration(1000);m_fingerAnimation->setLoopCount(-1); // 无限循环m_fingerAnimation->setEasingCurve(QEasingCurve::InOutSine);// 脚趾动画m_toeAnimation = new QPropertyAnimation(this);m_toeAnimation->setTargetObject(this);m_toeAnimation->setPropertyName("toeAngle");m_toeAnimation->setStartValue(0);m_toeAnimation->setEndValue(15);m_toeAnimation->setDuration(1500);m_toeAnimation->setLoopCount(-1);m_toeAnimation->setEasingCurve(QEasingCurve::InOutSine);}addTestGeoMesh();}

2、丑丑的小机器人构思创建;

void Form_text::addTestGeoMesh()
{// 1. 创建机器人根实体Qt3DCore::QEntity* robotEntity = new Qt3DCore::QEntity(dPtr->mRootEntity);// 2. 头部(球体)Qt3DCore::QEntity* headEntity = new Qt3DCore::QEntity(robotEntity);Qt3DExtras::QSphereMesh* headMesh = new Qt3DExtras::QSphereMesh();headMesh->setRadius(1.0f);Qt3DExtras::QPhongMaterial* headMaterial = new Qt3DExtras::QPhongMaterial();headMaterial->setDiffuse(QColor("#FF9999"));Qt3DCore::QTransform* headTransform = new Qt3DCore::QTransform();headTransform->setTranslation(QVector3D(0.0f, 3.0f, 0.0f));headEntity->addComponent(headMesh);headEntity->addComponent(headMaterial);headEntity->addComponent(headTransform);// 3. 身体(立方体)Qt3DCore::QEntity* bodyEntity = new Qt3DCore::QEntity(robotEntity);Qt3DExtras::QCuboidMesh* bodyMesh = new Qt3DExtras::QCuboidMesh();bodyMesh->setXExtent(2.0f);bodyMesh->setYExtent(3.0f);bodyMesh->setZExtent(1.0f);Qt3DExtras::QPhongMaterial* bodyMaterial = new Qt3DExtras::QPhongMaterial();bodyMaterial->setDiffuse(QColor("#99CCFF"));Qt3DCore::QTransform* bodyTransform = new Qt3DCore::QTransform();bodyEntity->addComponent(bodyMesh);bodyEntity->addComponent(bodyMaterial);bodyEntity->addComponent(bodyTransform);// 4. 四肢(圆柱体)// 修改四肢创建方式,添加关节连接auto addLimb = [robotEntity](const QVector3D& position, const QColor& color, float radius, float length, bool isArm) {// 关节球体Qt3DCore::QEntity* jointEntity = new Qt3DCore::QEntity(robotEntity);Qt3DExtras::QSphereMesh* jointMesh = new Qt3DExtras::QSphereMesh();jointMesh->setRadius(radius * 1.0f); // 关节比肢体稍大Qt3DExtras::QPhongMaterial* jointMaterial = new Qt3DExtras::QPhongMaterial();jointMaterial->setDiffuse(QColor("#888888")); // 关节使用灰色Qt3DCore::QTransform* jointTransform = new Qt3DCore::QTransform();jointTransform->setTranslation(position);jointEntity->addComponent(jointMesh);jointEntity->addComponent(jointMaterial);jointEntity->addComponent(jointTransform);// 肢体作为关节的子实体Qt3DCore::QEntity* limbEntity = new Qt3DCore::QEntity(jointEntity); // 注意这里的父实体改为jointEntityQt3DExtras::QCylinderMesh* limbMesh = new Qt3DExtras::QCylinderMesh();limbMesh->setRadius(radius);limbMesh->setLength(length);Qt3DExtras::QPhongMaterial* limbMaterial = new Qt3DExtras::QPhongMaterial();limbMaterial->setDiffuse(color);Qt3DCore::QTransform* limbTransform = new Qt3DCore::QTransform();// 根据肢体类型调整位置if (isArm) {limbTransform->setTranslation(QVector3D(0, length/2, 0));limbTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(0, 0, 1), 90.0f));} else {limbTransform->setTranslation(QVector3D(0, -length/2, 0));}limbEntity->addComponent(limbMesh);limbEntity->addComponent(limbMaterial);limbEntity->addComponent(limbTransform);return jointEntity; // 返回关节实体以便添加动画};// 手臂(更细)addLimb(QVector3D(-1.5f, 1.0f, 0.0f), QColor("#FFFF00"), 0.2f, 1.5f, true);addLimb(QVector3D(1.5f, 1.0f, 0.0f), QColor("#FFFF00"), 0.2f, 1.5f, true);// 腿部(更粗)addLimb(QVector3D(-0.5f, -2.0f, 0.0f), QColor("#00FF00"), 0.5f, 1.0f, false);addLimb(QVector3D(0.5f, -2.0f, 0.0f), QColor("#00FF00"), 0.5f, 1.0f, false);// 5. 眼睛(小球体)auto addEye = [robotEntity](const QVector3D& position) {Qt3DCore::QEntity* eyeEntity = new Qt3DCore::QEntity(robotEntity);Qt3DExtras::QSphereMesh* eyeMesh = new Qt3DExtras::QSphereMesh();eyeMesh->setRadius(0.2f);Qt3DExtras::QPhongMaterial* eyeMaterial = new Qt3DExtras::QPhongMaterial();eyeMaterial->setDiffuse(QColor("#000000"));Qt3DCore::QTransform* eyeTransform = new Qt3DCore::QTransform();eyeTransform->setTranslation(position);eyeEntity->addComponent(eyeMesh);eyeEntity->addComponent(eyeMaterial);eyeEntity->addComponent(eyeTransform);};addEye(QVector3D(-0.5f, 3.5f, 0.8f));addEye(QVector3D(0.5f, 3.5f, 0.8f));// 6. 鼻子(圆锥体)Qt3DCore::QEntity* noseEntity = new Qt3DCore::QEntity(robotEntity);Qt3DExtras::QConeMesh* noseMesh = new Qt3DExtras::QConeMesh();noseMesh->setTopRadius(0.1f);noseMesh->setBottomRadius(0.3f);noseMesh->setLength(0.5f);Qt3DExtras::QPhongMaterial* noseMaterial = new Qt3DExtras::QPhongMaterial();noseMaterial->setDiffuse(QColor("#FF6666"));Qt3DCore::QTransform* noseTransform = new Qt3DCore::QTransform();noseTransform->setTranslation(QVector3D(0.0f, 3.2f, 0.9f));noseTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1, 0, 0), -90.0f));noseEntity->addComponent(noseMesh);noseEntity->addComponent(noseMaterial);noseEntity->addComponent(noseTransform);// 7. 嘴巴(扁平圆柱体)Qt3DCore::QEntity* mouthEntity = new Qt3DCore::QEntity(robotEntity);Qt3DExtras::QCylinderMesh* mouthMesh = new Qt3DExtras::QCylinderMesh();mouthMesh->setRadius(0.4f);mouthMesh->setLength(0.1f);mouthMesh->setRings(1);Qt3DExtras::QPhongMaterial* mouthMaterial = new Qt3DExtras::QPhongMaterial();mouthMaterial->setDiffuse(QColor("#FF0000"));Qt3DCore::QTransform* mouthTransform = new Qt3DCore::QTransform();mouthTransform->setTranslation(QVector3D(0.0f, 2.8f, 0.9f));mouthTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1, 0, 0), 90.0f));mouthEntity->addComponent(mouthMesh);mouthEntity->addComponent(mouthMaterial);mouthEntity->addComponent(mouthTransform);// 8. 手掌(小球体)// 修改 addHand 函数为更详细的手部设计auto addHand = [this,robotEntity](const QVector3D& basePosition, bool isLeft) {// 手掌Qt3DCore::QEntity* palmEntity = new Qt3DCore::QEntity(robotEntity);Qt3DExtras::QSphereMesh* palmMesh = new Qt3DExtras::QSphereMesh();palmMesh->setRadius(0.3f);Qt3DExtras::QPhongMaterial* palmMaterial = new Qt3DExtras::QPhongMaterial();palmMaterial->setDiffuse(QColor("#FFD700"));Qt3DCore::QTransform* palmTransform = new Qt3DCore::QTransform();palmTransform->setTranslation(basePosition);palmEntity->addComponent(palmMesh);palmEntity->addComponent(palmMaterial);palmEntity->addComponent(palmTransform);// 手指// auto addFinger = [robotEntity](const QVector3D& position, float rotationAngle, bool isThumb) {auto addFinger = [this,robotEntity](const QVector3D& position, float rotationAngle, bool isThumb) {Qt3DCore::QEntity* fingerEntity = new Qt3DCore::QEntity(robotEntity);Qt3DExtras::QCylinderMesh* fingerMesh = new Qt3DExtras::QCylinderMesh();fingerMesh->setRadius(isThumb ? 0.08f : 0.06f);fingerMesh->setLength(isThumb ? 0.4f : 0.5f);Qt3DExtras::QPhongMaterial* fingerMaterial = new Qt3DExtras::QPhongMaterial();fingerMaterial->setDiffuse(QColor("#FFD700"));Qt3DCore::QTransform* fingerTransform = new Qt3DCore::QTransform();fingerTransform->setTranslation(position);fingerTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(0, 1, 0), rotationAngle));fingerEntity->addComponent(fingerMesh);fingerEntity->addComponent(fingerMaterial);fingerEntity->addComponent(fingerTransform);// Qt3DCore::QTransform* fingerTransform = new Qt3DCore::QTransform();// fingerTransform->setTranslation(position);// fingerTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(0, 1, 0), rotationAngle));m_fingerTransforms.append(fingerTransform); // 添加到动画控制列表// 指节if (!isThumb){Qt3DCore::QEntity* fingerJointEntity = new Qt3DCore::QEntity(robotEntity);Qt3DExtras::QCylinderMesh* fingerJointMesh = new Qt3DExtras::QCylinderMesh();fingerJointMesh->setRadius(0.06f);fingerJointMesh->setLength(0.3f);Qt3DExtras::QPhongMaterial* fingerJointMaterial = new Qt3DExtras::QPhongMaterial();fingerJointMaterial->setDiffuse(QColor("#FFD700"));Qt3DCore::QTransform* fingerJointTransform = new Qt3DCore::QTransform();fingerJointTransform->setTranslation(position + QVector3D(0, 0, 0.25f));fingerJointTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(0, 1, 0), rotationAngle + 15.0f));fingerJointEntity->addComponent(fingerJointMesh);fingerJointEntity->addComponent(fingerJointMaterial);fingerJointEntity->addComponent(fingerJointTransform);// Qt3DCore::QTransform* fingerJointTransform = new Qt3DCore::QTransform();// fingerJointTransform->setTranslation(position + QVector3D(0, 0, 0.25f));// fingerJointTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(0, 1, 0), rotationAngle + 15.0f));m_fingerTransforms.append(fingerJointTransform); // 添加到动画控制列表}};// 拇指addFinger(basePosition + QVector3D(isLeft ? -0.2f : 0.2f, 0, 0.1f),isLeft ? -30.0f : 30.0f, true);// 其他手指for (int i = 0; i < 4; i++) {float offset = -0.15f + i * 0.1f;addFinger(basePosition + QVector3D(isLeft ? -0.1f : 0.1f, offset, 0.2f),isLeft ? -10.0f : 10.0f, false);}};// 更新调用方式addHand(QVector3D(-1.5f, 0.2f, 0.0f), true);  // 左手addHand(QVector3D(1.5f, 0.2f, 0.0f), false);  // 右手// 修改 addFoot 函数为更详细的脚部设计auto addFoot = [this,robotEntity](const QVector3D& basePosition, bool isLeft) {// 脚掌Qt3DCore::QEntity* footEntity = new Qt3DCore::QEntity(robotEntity);Qt3DExtras::QCuboidMesh* footMesh = new Qt3DExtras::QCuboidMesh();footMesh->setXExtent(0.8f);footMesh->setYExtent(0.2f);footMesh->setZExtent(0.4f);Qt3DExtras::QPhongMaterial* footMaterial = new Qt3DExtras::QPhongMaterial();footMaterial->setDiffuse(QColor("#8B4513"));Qt3DCore::QTransform* footTransform = new Qt3DCore::QTransform();footTransform->setTranslation(basePosition);footEntity->addComponent(footMesh);footEntity->addComponent(footMaterial);footEntity->addComponent(footTransform);// 脚趾auto addToe = [this,robotEntity](const QVector3D& position, float size) {Qt3DCore::QEntity* toeEntity = new Qt3DCore::QEntity(robotEntity);Qt3DExtras::QCylinderMesh* toeMesh = new Qt3DExtras::QCylinderMesh();toeMesh->setRadius(0.1f * size);toeMesh->setLength(0.2f * size);Qt3DExtras::QPhongMaterial* toeMaterial = new Qt3DExtras::QPhongMaterial();toeMaterial->setDiffuse(QColor("#8B4513"));Qt3DCore::QTransform* toeTransform = new Qt3DCore::QTransform();toeTransform->setTranslation(position);toeTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1, 0, 0), 15.0f));toeEntity->addComponent(toeMesh);toeEntity->addComponent(toeMaterial);toeEntity->addComponent(toeTransform);// Qt3DCore::QTransform* toeTransform = new Qt3DCore::QTransform();// toeTransform->setTranslation(position);// toeTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1, 0, 0), 15.0f));m_toeTransforms.append(toeTransform); // 添加到动画控制列表};// 五个脚趾for (int i = 0; i < 5; i++) {float offsetX = -0.3f + i * 0.15f;float size = 1.0f - i * 0.15f; // 大脚趾最大,小脚趾最小addToe(basePosition + QVector3D(offsetX, 0.0f, 0.3f), size);}};// 更新调用方式addFoot(QVector3D(-0.5f, -3.2f, 0.0f), true);  // 左脚addFoot(QVector3D(0.5f, -3.2f, 0.0f), false);  // 右脚// 10. 光源// 1. 主光源(前方)Qt3DCore::QEntity* mainLightEntity = new Qt3DCore::QEntity(dPtr->mRootEntity);Qt3DRender::QPointLight* mainLight = new Qt3DRender::QPointLight(mainLightEntity);mainLight->setColor("white");mainLight->setIntensity(0.6f);mainLight->setConstantAttenuation(1.0f);    // 衰减参数mainLight->setLinearAttenuation(0.05f);mainLight->setQuadraticAttenuation(0.01f);Qt3DCore::QTransform* mainLightTransform = new Qt3DCore::QTransform(mainLightEntity);mainLightTransform->setTranslation(QVector3D(0.0f, 5.0f, 10.0f));mainLightEntity->addComponent(mainLight);mainLightEntity->addComponent(mainLightTransform);// 2. 补光光源(后方)Qt3DCore::QEntity* fillLightEntity = new Qt3DCore::QEntity(dPtr->mRootEntity);Qt3DRender::QPointLight* fillLight = new Qt3DRender::QPointLight(fillLightEntity);fillLight->setColor("white");fillLight->setIntensity(0.3f);Qt3DCore::QTransform* fillLightTransform = new Qt3DCore::QTransform(fillLightEntity);fillLightTransform->setTranslation(QVector3D(0.0f, 5.0f, -10.0f));fillLightEntity->addComponent(fillLight);fillLightEntity->addComponent(fillLightTransform);// 3. 环境光(改用 QDirectionalLight)Qt3DRender::QDirectionalLight* ambientLight = new Qt3DRender::QDirectionalLight(dPtr->mRootEntity);ambientLight->setColor(QColor("#606060")); // 灰色环境光ambientLight->setIntensity(0.5f);dPtr->mRootEntity->addComponent(ambientLight);// 启动动画m_fingerAnimation->start();m_toeAnimation->start();}
float Form_text::fingerAngle() const { return m_currentFingerAngle; }
float Form_text::toeAngle() const { return m_currentToeAngle; }void Form_text::setFingerAngle(float angle)
{m_currentFingerAngle = angle;for (auto transform : m_fingerTransforms) {transform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(0, 1, 0), angle));}
}void Form_text::setToeAngle(float angle)
{m_currentToeAngle = angle;for (auto transform : m_toeTransforms){transform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1, 0, 0), 15 + angle));}
}

3.对应的文件.h文件

#ifndef FORM_TEXT_H
#define FORM_TEXT_H#include <QWidget>
#include <Qt3DCore>
#include <Qt3DRender>
#include <Qt3DInput>
#include <Qt3DAnimation>
#include <Qt3DLogic>
#include <Qt3DExtras>
#include <QObject>namespace Ui {
class Form_text;
}struct Form_textPrivate {  // 私有数据结构Qt3DCore::QEntity *mRootEntity = nullptr;  // 初始化为 nullptr
};class Form_text : public QWidget
{Q_OBJECT// 添加属性用于动画控制Q_PROPERTY(float fingerAngle READ fingerAngle WRITE setFingerAngle)Q_PROPERTY(float toeAngle READ toeAngle WRITE setToeAngle)
public:explicit Form_text(QWidget *parent = nullptr);~Form_text();private:Ui::Form_text *ui;void init3D();void addTestGeoMesh();std::unique_ptr<Form_textPrivate> dPtr;  // 使用智能指针管理QVector<Qt3DCore::QTransform*> m_fingerTransforms;QVector<Qt3DCore::QTransform*> m_toeTransforms;QPropertyAnimation* m_fingerAnimation;QPropertyAnimation* m_toeAnimation;float m_currentFingerAngle;float m_currentToeAngle;float fingerAngle() const;float toeAngle() const;void setFingerAngle(float angle);void setToeAngle(float angle);};#endif // FORM_TEXT_H

http://www.dtcms.com/a/390389.html

相关文章:

  • 阿里云开源DeepResearch:轻量化AI推理框架技术解析与实践指南
  • Visual Studio 2026 Insiders 重磅发布:AI 深度集成、性能飞跃、全新设计
  • 大模型初识(基础模型 业务集成+智能体Agent+Prompt提示词优化)
  • 【4/20】Node.js 入门:设置后端服务器,实现一个简单 API 端点
  • Kafka事务:构建可靠的分布式消息处理系统
  • 补环境-JS原型链检测:在Node.js中完美模拟浏览器原型环境
  • TCP端口号的作用
  • 笔记本电脑维修指南(芯片级)
  • Burpsuite进行暴力破解
  • 虚拟现实CAVE系统中的光学跟踪技术,1:1呈现CAD模型沉浸式交互
  • 2025拍照手机综合排名与场景化选购指南
  • TCP 抓包分析:tcp抓包工具、 iOS/HTTPS 流量解析全流程
  • 从电商API到数据分析的全流程教程
  • 【踩坑】ELK日志解析优化实战:解决多行合并与字段提取问题
  • 大数据高校舆情分析系统 snownlp情感分析 数据分析 可视化 Flask框架 大数据实战(源码)✅
  • 【12/20】数据库高级查询:MongoDB 聚合管道在用户数据分析中的应用,实现报告生成
  • Oceanbase tablegroup表组与负载均衡实践
  • 什么是批量剪辑矩阵源码,支持OEM!
  • RabbitMQ快速入门指南
  • 在项目中通过LangChain4j框架接入AI大模型
  • c语言9:从内存到实践深入浅出理解数组
  • sglang使用笔记
  • 本地大模型编程实战(36)使用知识图谱增强RAG(2)生成知识图谱
  • clip——手写数字识别
  • commons-numbers
  • MySqL-day4_01(内置函数、存储过程、视图)
  • 用html5写一个手机ui
  • 2.canvas学习
  • 【系统架构设计(34)】计算机网络架构与技术基础
  • 计网1.2 计算机网络体系结构与参考模型