类名 | 作用 |
---|---|
Qt3DWindow |
3D渲染窗口容器 |
QEntity |
场景中的实体(对象或容器) |
QCamera |
控制观察视角 |
QPointLight |
点光源 |
QConeMesh |
圆锥几何网格 |
QTransform |
控制实体的位置/旋转/缩放 |
QPhongMaterial |
Phong光照材质(定义颜色、反光等) |
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 xyz
float* 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, // 直接传递缓冲区,不传递geometry
Qt3DCore::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); // 高度为1
cone->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); // 高度同样为1
cone1->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();
}