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

QT3D学习笔记——圆台、圆锥

类名作用
Qt3DWindow3D渲染窗口容器
QEntity场景中的实体(对象或容器)
QCamera控制观察视角
QPointLight点光源
QConeMesh圆锥几何网格
QTransform控制实体的位置/旋转/缩放
QPhongMaterialPhong光照材质(定义颜色、反光等)
QFirstPersonCameraController第一人称相机控制器(WASD+鼠标控制)

Qt3DWindow(3D 窗口)

├─ RootEntity (QEntity 根实体)
│   ├─ Camera (QCamera 相机)
│   │   ├─ Lens (镜头:透视投影参数)
│   │   └─ Controller (控制器:WASD+鼠标控制)
│   │
│   ├─ Light (光源)
│   │   ├─ QPointLight(点光源:颜色/强度)
│   │   └─ QTransform (变换:光源位置)

│   │
│   └─ Cone (3d 圆锥实体)

│       ├─ QConeMesh (网格:几何图形)

│       ├─ QPhongMaterial (材质:颜色/反光)
│       └─ QTransform (变换:位置/旋转)

│   
└─ FrameGraph (渲染管线:背景色/渲染设置)

//#include "mainwindow.h"//#include "FrustumWidget.h"
#include <QApplication>
#include <QtCore/QObject>
#include <QGuiApplication>#include <Qt3DRender/qcamera.h>
#include <Qt3DCore/qentity.h>
#include <Qt3DRender/qcameralens.h>#include <QtWidgets/QApplication>
#include <QtWidgets/QWidget>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QCommandLinkButton>
#include <QtGui/QScreen>#include <Qt3DInput/QInputAspect>#include <Qt3DExtras/qtorusmesh.h>
#include <Qt3DRender/qmesh.h>
#include <Qt3DRender/qtechnique.h>
#include <Qt3DRender/qmaterial.h>
#include <Qt3DRender/qeffect.h>
#include <Qt3DRender/qtexture.h>
#include <Qt3DRender/qrenderpass.h>
#include <Qt3DRender/qsceneloader.h>
#include <Qt3DRender/qpointlight.h>#include <Qt3DCore/qtransform.h>
#include <Qt3DCore/qaspectengine.h>#include <Qt3DRender/qrenderaspect.h>
#include <Qt3DExtras/qforwardrenderer.h>#include <Qt3DExtras/qt3dwindow.h>
#include <Qt3DExtras/qfirstpersoncameracontroller.h>
#include <Qt3DExtras/QPhongMaterial>
#include <Qt3DExtras/QConeMesh>
#include <QText2DEntity>
// 创建坐标轴线
Qt3DCore::QEntity* createAxisLine(Qt3DCore::QEntity* parent,const QVector3D& start,const QVector3D& end,const QColor& color) {Qt3DCore::QEntity* axis = new Qt3DCore::QEntity(parent);// 顶点数据QByteArray vertexData;vertexData.resize(2 * 3 * sizeof(float)); // 2 points xyzfloat* ptr = reinterpret_cast<float*>(vertexData.data());*ptr++ = start.x(); *ptr++ = start.y(); *ptr++ = start.z();*ptr++ = end.x();   *ptr++ = end.y();   *ptr++ = end.z();// 几何体Qt3DCore::QGeometry* geometry = new Qt3DCore::QGeometry(axis);Qt3DCore::QBuffer* vertexBuffer = new Qt3DCore::QBuffer(geometry);vertexBuffer->setData(vertexData);Qt3DCore::QAttribute* positionAttr = new Qt3DCore::QAttribute(vertexBuffer,  // 直接传递缓冲区,不传递geometryQt3DCore::QAttribute::defaultPositionAttributeName(),Qt3DCore::QAttribute::Float,3,  // 每个顶点的分量数(xyz=3)2   // 顶点数量(起点+终点=2));geometry->addAttribute(positionAttr);  // 手动添加到geometry// 渲染器Qt3DRender::QGeometryRenderer* line = new Qt3DRender::QGeometryRenderer(axis);line->setGeometry(geometry);line->setPrimitiveType(Qt3DRender::QGeometryRenderer::Lines);// 材质Qt3DExtras::QPhongMaterial* material = new Qt3DExtras::QPhongMaterial(axis);material->setDiffuse(color);material->setAmbient(color);axis->addComponent(line);axis->addComponent(material);return axis;
}// 添加坐标轴标签(需要Qt3DExtras模块)
void addAxisLabel(Qt3DCore::QEntity* parent,const QString& text,const QVector3D& position) {Qt3DExtras::QText2DEntity* label = new Qt3DExtras::QText2DEntity(parent);label->setText(text);label->setColor(Qt::black);label->setHeight(20);label->setWidth(30);Qt3DCore::QTransform* transform = new Qt3DCore::QTransform(label);transform->setTranslation(position);label->addComponent(transform);
}
int main(int argc, char *argv[])
{QApplication a(argc, argv);//MainWindow w;//w.show();Qt3DExtras::Qt3DWindow *view = new Qt3DExtras::Qt3DWindow();//3D的渲染窗口,提供3D场景的显示容器view->defaultFrameGraph()->setClearColor(QColor(Qt::black));//设置视图背景色QWidget *widget = QWidget::createWindowContainer(view);QSize screenSize = view->screen()->size();widget->setMinimumSize(QSize(200, 100));widget->setMaximumSize(screenSize);widget->setWindowTitle(QStringLiteral("Basic shapes"));//3D中的基本实体类,代表场景中的一个对象或容器。Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity();view->setRootEntity(rootEntity);//管理输入设备的抽象层,启用输入处理(如鼠标、键盘事件)Qt3DInput::QInputAspect *input = new Qt3DInput::QInputAspect;view->registerAspect(input);//控制3D场景的观察视角Qt3DRender::QCamera *cameraEntity = view->camera();cameraEntity->lens()->setPerspectiveProjection(45.0f, 16.0f / 9.0f, 0.1f, 1000.0f);cameraEntity->setUpVector(QVector3D(0, 1, 0));//定义相机的“向上”方向(通常为Y轴 (0,1,0))。cameraEntity->setPosition(QVector3D(0, 10.0f, 10.0f));//视点:观察者所处的位置。cameraEntity->setViewCenter(QVector3D(0, 0, 0));//观察目标点//高光控制Qt3DCore::QEntity *lightEntity = new Qt3DCore::QEntity(rootEntity);//点光源组件Qt3DRender::QPointLight *light = new Qt3DRender::QPointLight(lightEntity);light->setColor("white");light->setIntensity(1);lightEntity->addComponent(light);//定义3d实体的位置/旋转/缩放Qt3DCore::QTransform *lightTransform = new Qt3DCore::QTransform(lightEntity);lightTransform->setTranslation(cameraEntity->position());lightEntity->addComponent(lightTransform);//启用第一人称相机控制(通过WASD和鼠标移动相机)Qt3DExtras::QFirstPersonCameraController *camController = new Qt3DExtras::QFirstPersonCameraController(rootEntity);camController->setCamera(cameraEntity);// 第一个圆锥(底部圆锥)Qt3DCore::QEntity *m_coneEntity = new Qt3DCore::QEntity(rootEntity);Qt3DExtras::QConeMesh *cone = new Qt3DExtras::QConeMesh();cone->setTopRadius(0.5);cone->setBottomRadius(0.5);cone->setLength(1);  // 高度为1cone->setRings(50);cone->setSlices(20);Qt3DCore::QTransform *coneTransform = new Qt3DCore::QTransform();coneTransform->setScale(1.0);//coneTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1.0f, 0.0f, 0.0f), 45.0f));coneTransform->setTranslation(QVector3D(0.0f, 0.0f, 0.0f));  // 底部圆锥位于原点Qt3DExtras::QPhongMaterial *coneMaterial = new Qt3DExtras::QPhongMaterial();coneMaterial->setDiffuse(QColor(Qt::white));m_coneEntity->addComponent(cone);m_coneEntity->addComponent(coneMaterial);m_coneEntity->addComponent(coneTransform);// 第二个圆锥(顶部圆锥)Qt3DCore::QEntity *m_coneEntity1 = new Qt3DCore::QEntity(rootEntity);Qt3DExtras::QConeMesh *cone1 = new Qt3DExtras::QConeMesh();cone1->setTopRadius(0.5);cone1->setBottomRadius(0.5);cone1->setLength(1);  // 高度同样为1cone1->setRings(50);cone1->setSlices(20);Qt3DCore::QTransform *coneTransform1 = new Qt3DCore::QTransform();coneTransform1->setScale(1.0);//coneTransform1->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1.0f, 0.0f, 0.0f), 45.0f));// 将第二个圆锥放在第一个圆锥的顶部// Y轴偏移 = 第一个圆锥的高度 + 第二个圆锥高度的一半(因为旋转后中心点在几何中心)coneTransform1->setTranslation(QVector3D(0.0f, 1.0f, 0.0f));Qt3DExtras::QPhongMaterial *coneMaterial1 = new Qt3DExtras::QPhongMaterial();coneMaterial1->setDiffuse(QColor(Qt::white));m_coneEntity1->addComponent(cone1);m_coneEntity1->addComponent(coneMaterial1);m_coneEntity1->addComponent(coneTransform1);widget->show();widget->resize(500, 500);return a.exec();
}

 

http://www.xdnf.cn/news/13015.html

相关文章:

  • Xcode 16.2 版本 pod init 报错
  • 关键领域软件测试的突围之路:如何破解安全与效率的平衡难题
  • 【1】跨越技术栈鸿沟:字节跳动开源TRAE AI编程IDE的实战体验
  • C++--stack和queue的使用及其模拟实现
  • 【Android】Android 开发 ADB 常用指令
  • FreeRTOS队列
  • CMS内容管理系统的设计与实现:多站点模式的实现
  • React核心概念:State是什么?如何用useState管理组件自己的数据?
  • 总结html标签---表单标签
  • 接地气的方式认识JVM(二)
  • MinIO Docker 部署:仅开放一个端口
  • 数据结构-线性表
  • Fabric V2.5 通用溯源系统——增加图片上传与下载功能
  • 使用LangGraph和LangSmith构建多智能体人工智能系统
  • 【java面试】微服务篇
  • 【中间件】Web服务、消息队列、缓存与微服务治理:Nginx、Kafka、Redis、Nacos 详解
  • CSP-38th
  • ARM SMMUv3 STE表和CD表数据格式分析(三)
  • 职位竞聘BA商业推理测评管理人员TAS倍智题库天翼云益丰等企业
  • Secs/Gem第十一讲(基于secs4net项目的ChatGpt介绍)
  • @Import原理与实战
  • VBA经典应用69例应用8:利用VBA,预设某个程序在晚上21点运行
  • 浮点数精度问题(CSP38思考)
  • (新)Gateway网关+基于Nacos配置动态路由
  • 【Ftrace专栏】function graph的trace输出格式使用
  • NumPy数组属性
  • 《最短路(Floyd)》题集
  • Qwen3开源最新Embedding模型
  • Cesium快速入门到精通系列教程八:时间系统
  • 【术语扫盲】评估指标Precision、Recall、F1-score、Support是什么含义?