類名 | 作用 |
---|---|
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 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();
}
?