以下代码来自QT官方示例:
主要添加了中文注释,XYZ轴,和材质颜色。
main.cpp
/****************************************************************************
**
** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB).
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QGuiApplication>
#include <Qt3DCore/QEntity>
#include <Qt3DRender/QCamera>
#include <Qt3DRender/QCameraLens>
#include <Qt3DCore/QTransform>
#include <Qt3DCore/QAspectEngine>
#include <Qt3DInput/QInputAspect>
#include <Qt3DRender/QRenderAspect>
#include <Qt3DExtras/QForwardRenderer>
#include <Qt3DExtras/QPhongMaterial>
#include <Qt3DExtras/QCylinderMesh>
#include <Qt3DExtras/QSphereMesh>
#include <Qt3DExtras/QTorusMesh>
#include <QPropertyAnimation>
#include <QMaterial>
#include "qt3dwindow.h"
#include "orbittransformcontroller.h"
#include "qorbitcameracontroller.h"
Qt3DCore::QEntity *createScene()
{
// Root entity
// 根实体
Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity; // 创建根实体
// Material
// 材质
//Qt3DRender::QMaterial *material = new Qt3DExtras::QPhongMaterial(rootEntity); // 创建材质
//QPhongMaterial提供默认实现的光照效果;
Qt3DExtras::QPhongMaterial *material = new Qt3DExtras::QPhongMaterial();
material->setDiffuse(QColor((Qt::red))); //设置材质颜色
Qt3DExtras::QPhongMaterial *material1 = new Qt3DExtras::QPhongMaterial();
material1->setDiffuse(QColor((Qt::blue))); //设置材质颜色
// Torus
// 创建环面
Qt3DCore::QEntity *torusEntity = new Qt3DCore::QEntity(rootEntity); // 创建环面实体
Qt3DExtras::QTorusMesh *torusMesh = new Qt3DExtras::QTorusMesh; // 创建环面网格
torusMesh->setRadius(5); // 设置半径
torusMesh->setMinorRadius(1); // 设置小半径
torusMesh->setRings(100); // 设置环数
torusMesh->setSlices(20); // 设置切片数
Qt3DCore::QTransform *torusTransform = new Qt3DCore::QTransform; // 创建环面变换
torusTransform->setScale3D(QVector3D(1.5, 1, 0.5)); // 设置缩放
torusTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1, 0, 0), 45.0f)); // 设置旋转
torusEntity->addComponent(torusMesh); // 添加环面网格
torusEntity->addComponent(torusTransform); // 添加环面变换
torusEntity->addComponent(material); // 添加材质
// Sphere
// 创建球体
Qt3DCore::QEntity *sphereEntity = new Qt3DCore::QEntity(rootEntity); // 创建球体实体
Qt3DExtras::QSphereMesh *sphereMesh = new Qt3DExtras::QSphereMesh; // 创建球体网格
sphereMesh->setRadius(3); // 设置半径
Qt3DCore::QTransform *sphereTransform = new Qt3DCore::QTransform; // 创建球体变换
OrbitTransformController *controller = new OrbitTransformController(sphereTransform); // 创建轨道变换控制器
controller->setTarget(sphereTransform); // 设置目标
controller->setRadius(20.0f); // 设置半径
QPropertyAnimation *sphereRotateTransformAnimation = new QPropertyAnimation(sphereTransform); // 创建球体旋转动画
sphereRotateTransformAnimation->setTargetObject(controller); // 设置目标对象
sphereRotateTransformAnimation->setPropertyName("angle"); // 设置属性名
sphereRotateTransformAnimation->setStartValue(QVariant::fromValue(0)); // 设置起始值
sphereRotateTransformAnimation->setEndValue(QVariant::fromValue(360)); // 设置结束值
sphereRotateTransformAnimation->setDuration(10000); // 设置持续时间
sphereRotateTransformAnimation->setLoopCount(-1); // 设置循环次数
sphereRotateTransformAnimation->start(); // 开始动画
sphereEntity->addComponent(sphereMesh); // 添加球体网格
sphereEntity->addComponent(sphereTransform); // 添加球体变换
sphereEntity->addComponent(material1); // 添加材质
// 创建坐标轴实体
Qt3DCore::QEntity *axisEntity = new Qt3DCore::QEntity(rootEntity);
// 创建 X 轴实体
Qt3DExtras::QCylinderMesh *xAxisMesh = new Qt3DExtras::QCylinderMesh();
xAxisMesh->setRadius(0.02f);
xAxisMesh->setLength(100.0f);
xAxisMesh->setRings(100);
xAxisMesh->setSlices(20);
Qt3DCore::QTransform *xAxisTransform = new Qt3DCore::QTransform();
xAxisTransform->setTranslation(QVector3D(0.5f, 0.0f, 0.0f));
xAxisTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(0.0f, 0.0f, 1.0f), 90.0f));
Qt3DExtras::QPhongMaterial *xAxisMaterial = new Qt3DExtras::QPhongMaterial();
xAxisMaterial->setDiffuse(QColor(255, 0, 0));
Qt3DCore::QEntity *xAxisEntity = new Qt3DCore::QEntity(axisEntity);
xAxisEntity->addComponent(xAxisMesh);
xAxisEntity->addComponent(xAxisMaterial);
xAxisEntity->addComponent(xAxisTransform);
// 创建 Y 轴实体
Qt3DExtras::QCylinderMesh *yAxisMesh = new Qt3DExtras::QCylinderMesh();
yAxisMesh->setRadius(0.02f);
yAxisMesh->setLength(100.0f);
yAxisMesh->setRings(100);
yAxisMesh->setSlices(20);
Qt3DCore::QTransform *yAxisTransform = new Qt3DCore::QTransform();
yAxisTransform->setTranslation(QVector3D(0.0f, 0.5f, 0.0f));
Qt3DExtras::QPhongMaterial *yAxisMaterial = new Qt3DExtras::QPhongMaterial();
yAxisMaterial->setDiffuse(QColor(0, 255, 0));
Qt3DCore::QEntity *yAxisEntity = new Qt3DCore::QEntity(axisEntity);
yAxisEntity->addComponent(yAxisMesh);
yAxisEntity->addComponent(yAxisMaterial);
yAxisEntity->addComponent(yAxisTransform);
// 创建 Z 轴实体
Qt3DExtras::QCylinderMesh *zAxisMesh = new Qt3DExtras::QCylinderMesh();
zAxisMesh->setRadius(0.02f);
zAxisMesh->setLength(100.0f);
zAxisMesh->setRings(100);
zAxisMesh->setSlices(20);
Qt3DCore::QTransform *zAxisTransform = new Qt3DCore::QTransform();
zAxisTransform->setTranslation(QVector3D(0.0f, 0.0f, 0.5f));
zAxisTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1.0f, 0.0f, 0.0f), 90.0f));
Qt3DExtras::QPhongMaterial *zAxisMaterial = new Qt3DExtras::QPhongMaterial();
zAxisMaterial->setDiffuse(QColor(0, 0, 255));
Qt3DCore::QEntity *zAxisEntity = new Qt3DCore::QEntity(axisEntity);
zAxisEntity->addComponent(zAxisMesh);
zAxisEntity->addComponent(zAxisMaterial);
zAxisEntity->addComponent(zAxisTransform);
return rootEntity; // 返回根实体
}
int main(int argc, char* argv[])
{
QGuiApplication app(argc, argv);
Qt3DExtras::Qt3DWindow view; // 创建一个Qt 3D窗口
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( 100.0f ); // 设置相机控制器线性速度
camController->setLookSpeed( 100.0f ); // 设置相机控制器视角速度
camController->setCamera(camera); // 设置相机控制器相机
view.setRootEntity(scene); // 设置根实体
view.show(); // 显示窗口
return app.exec();
}
orbittransformcontroller.h
/****************************************************************************
**
** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB).
** Copyright (C) 2016 The Qt Company Ltd and/or its subsidiary(-ies).
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#ifndef ORBITTRANSFORMCONTROLLER_H
#define ORBITTRANSFORMCONTROLLER_H
#include <QObject>
#include <QMatrix4x4>
QT_BEGIN_NAMESPACE
namespace Qt3DCore {
class QTransform;
}
class OrbitTransformController : public QObject
{
Q_OBJECT
Q_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:
OrbitTransformController(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:
Qt3DCore::QTransform *m_target;
QMatrix4x4 m_matrix;
float m_radius;//半径
float m_angle;
};
QT_END_NAMESPACE
#endif // ORBITTRANSFORMCONTROLLER_H
orbittransformcontroller.cpp
/****************************************************************************
**
** Copyright (C) 2016 Klaralvdalens Datakonsult AB (KDAB).
** Copyright (C) 2016 The Qt Company Ltd and/or its subsidiary(-ies).
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "orbittransformcontroller.h"
#include <Qt3DCore/qtransform.h>
QT_BEGIN_NAMESPACE
OrbitTransformController::OrbitTransformController(QObject *parent)
: QObject(parent)
, m_target(nullptr)
, m_matrix()
, m_radius(1.0f)
, m_angle(0.0f)
{
}
void OrbitTransformController::setTarget(Qt3DCore::QTransform *target)
{
if (m_target != target) {
m_target = target;
emit targetChanged();
}
}
Qt3DCore::QTransform *OrbitTransformController::target() const
{
return m_target;
}
void OrbitTransformController::setRadius(float radius)
{
if (!qFuzzyCompare(radius, m_radius)) {
m_radius = radius;
updateMatrix();
emit radiusChanged();
}
}
float OrbitTransformController::radius() const
{
return m_radius;
}
void OrbitTransformController::setAngle(float angle)
{
if (!qFuzzyCompare(angle, m_angle)) {
m_angle = angle;
updateMatrix();
emit angleChanged();
}
}
float OrbitTransformController::angle() const
{
return m_angle;
}
void OrbitTransformController::updateMatrix()
{
// 将矩阵重置为单位矩阵
m_matrix.setToIdentity();
// 绕y轴旋转m_angle度
m_matrix.rotate(m_angle, QVector3D(0.0f, 1.0f, 0.0f));
// 沿x轴平移m_radius
m_matrix.translate(m_radius, 0.0f, 0.0f);
// 更新目标的变换矩阵
m_target->setMatrix(m_matrix);
}
QT_END_NAMESPACE
运行结果: