Qt文档阅读笔记-最简单的动态3D圆环实例
生活随笔
收集整理的這篇文章主要介紹了
Qt文档阅读笔记-最简单的动态3D圆环实例
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
程序的邏輯如下:
正常顯示邏輯:
1.場景中要存在一個根實體;
2.為根實體加載材質(zhì);
3.在根實體下添加其他實體;
4.為其他實體添加額外的數(shù)據(jù)(比如畫圓環(huán)等);
5.放置攝像機(jī),設(shè)置前景等屬性。
攝像機(jī)視覺方面的邏輯:
1.放置攝像機(jī),設(shè)置前景等屬性;
2.創(chuàng)建攝像機(jī)軌道控制類,設(shè)置移動速度等屬性;
物體實現(xiàn)動態(tài)功能的邏輯:
1.創(chuàng)建一個類,這個類完成對某一3D模型“動態(tài)”數(shù)據(jù)提取;
2.生成一個單位矩陣,并對這個單位矩陣進(jìn)行操作(旋轉(zhuǎn),轉(zhuǎn)化等);
3.把這個單位矩陣放到目標(biāo)物體上;
4.使用QPropertyAnimation對某一屬性名,實現(xiàn)動態(tài)效果;
?
程序運(yùn)行截圖如下:
?
源碼如下:
animationcontroller.h
#ifndef ANIMATIONCONTROLLER_H #define ANIMATIONCONTROLLER_H#include <QObject> #include <QMatrix4x4>namespace Qt3DCore { class QTransform; }class AnimationController : public QObject {Q_OBJECTQ_PROPERTY(Qt3DCore::QTransform* target READ target WRITE setTarget NOTIFY targetChanged)Q_PROPERTY(float radius READ radius WRITE setRadius NOTIFY radiusChanged)Q_PROPERTY(float angle READ angle WRITE setAngle NOTIFY angleChanged) public:AnimationController(QObject *parent = 0);void setTarget(Qt3DCore::QTransform *target);Qt3DCore::QTransform *target()const;void setRadius(float radius);float radius()const;void setAngle(float angle);float angle()const;signals:void targetChanged();void radiusChanged();void angleChanged();protected:void updateMatrix();private:QMatrix4x4 m_matrix;Qt3DCore::QTransform *m_target;float m_radius;float m_angle; };#endif // ANIMATIONCONTROLLER_Hanimationcontroller.cpp
#include "animationcontroller.h" #include <QTransform>AnimationController::AnimationController(QObject *parent): QObject(parent), m_target(nullptr), m_matrix(), m_radius(1.0f), m_angle(0.0f){}void AnimationController::setTarget(Qt3DCore::QTransform *target) {if(m_target != target){m_target = target;emit targetChanged();} }Qt3DCore::QTransform *AnimationController::target() const {return m_target; }void AnimationController::setRadius(float radius) {if(!qFuzzyCompare(radius, m_radius)){m_radius = radius;updateMatrix();emit radiusChanged();} }float AnimationController::radius() const {return m_radius; }void AnimationController::setAngle(float angle) {if(!qFuzzyCompare(angle, m_angle)){m_angle = angle;updateMatrix();emit angleChanged();} }float AnimationController::angle() const {return m_angle; }void AnimationController::updateMatrix() {m_matrix.setToIdentity();m_matrix.rotate(m_angle, QVector3D(1.0f, 0.0f, 0.0f));m_matrix.translate(m_radius, 0.0f, 0.0f);m_target->setMatrix(m_matrix); }main.cpp
#include <Qt3DCore> #include <QApplication> #include <Qt3DRender> #include <Qt3DInput> #include <Qt3DExtras> #include <QPropertyAnimation> #include "animationcontroller.h"Qt3DCore::QEntity *createScene(){// Root entityQt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity;// MaterialQt3DRender::QMaterial *material = new Qt3DExtras::QPhongMaterial(rootEntity);static_cast<Qt3DExtras::QPhongMaterial*>(material)->setAmbient(QColor(Qt::blue));Qt3DCore::QEntity *torusEntity = new Qt3DCore::QEntity(rootEntity);// TorusQt3DExtras::QTorusMesh *torusMesh = new Qt3DExtras::QTorusMesh;torusMesh->setRadius(10);torusMesh->setMinorRadius(1);torusMesh->setRings(100);torusMesh->setSlices(20);Qt3DCore::QTransform *sphereTransform = new Qt3DCore::QTransform;AnimationController *animationController = new AnimationController(sphereTransform);animationController->setTarget(sphereTransform);animationController->setRadius(20.0f);QPropertyAnimation *rotateAnimation = new QPropertyAnimation(sphereTransform);rotateAnimation->setTargetObject(animationController);rotateAnimation->setPropertyName("angle");rotateAnimation->setStartValue(QVariant::fromValue(0));rotateAnimation->setEndValue(QVariant::fromValue(360));rotateAnimation->setDuration(10000);rotateAnimation->setLoopCount(-1);rotateAnimation->start();torusEntity->addComponent(torusMesh);torusEntity->addComponent(sphereTransform);torusEntity->addComponent(material);return rootEntity;}int main(int argc, char *argv[]) {QApplication a(argc, argv);Qt3DExtras::Qt3DWindow view;Qt3DCore::QEntity *scene = createScene();Qt3DRender::QCamera *camera = view.camera();camera->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f);camera->setPosition(QVector3D(0, 0, 40.0f));camera->setViewCenter(QVector3D(0, 0, 0));Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(scene);camController->setLinearSpeed(50.0f);camController->setLookSpeed(180.0f);camController->setCamera(camera);view.setRootEntity(scene);view.show();return a.exec(); }?
總結(jié)
以上是生活随笔為你收集整理的Qt文档阅读笔记-最简单的动态3D圆环实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt工作笔记-对QImage的进一步认识
- 下一篇: C++ STL list构造