| 1 | // Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include "entity.h" |
| 5 | |
| 6 | #include <Qt3DRender/QMaterial> |
| 7 | #include <Qt3DRender/QParameter> |
| 8 | #include <Qt3DCore/QTransform> |
| 9 | #include <QMatrix4x4> |
| 10 | |
| 11 | Entity::Entity(Qt3DRender::QEffect *effect, Qt3DCore::QNode *parent) |
| 12 | : QEntity(parent) |
| 13 | , m_transform(new Qt3DCore::QTransform()) |
| 14 | , m_material(new Qt3DRender::QMaterial()) |
| 15 | , m_diffuseColorParam(new Qt3DRender::QParameter()) |
| 16 | { |
| 17 | m_diffuseColorParam->setName(QLatin1String("kd")); |
| 18 | m_material->addParameter(m_diffuseColorParam); |
| 19 | m_material->setEffect(effect); |
| 20 | |
| 21 | addComponent(m_transform); |
| 22 | addComponent(m_material); |
| 23 | } |
| 24 | |
| 25 | void Entity::updateTransform() |
| 26 | { |
| 27 | QMatrix4x4 m; |
| 28 | m.translate(m_position); |
| 29 | m.rotate(m_phi, QVector3D(1.0f, 0.0f, 0.0f)); |
| 30 | m.rotate(m_theta, QVector3D(0.0f, 0.0f, 1.0f)); |
| 31 | m_transform->setMatrix(m); |
| 32 | } |
| 33 | |
| 34 | float Entity::theta() const |
| 35 | { |
| 36 | return m_theta; |
| 37 | } |
| 38 | |
| 39 | float Entity::phi() const |
| 40 | { |
| 41 | return m_phi; |
| 42 | } |
| 43 | |
| 44 | QVector3D Entity::position() const |
| 45 | { |
| 46 | return m_position; |
| 47 | } |
| 48 | |
| 49 | QColor Entity::diffuseColor() const |
| 50 | { |
| 51 | return m_diffuseColorParam->value().value<QColor>(); |
| 52 | } |
| 53 | |
| 54 | void Entity::setTheta(float theta) |
| 55 | { |
| 56 | if (qFuzzyCompare(m_theta, theta)) |
| 57 | return; |
| 58 | |
| 59 | m_theta = theta; |
| 60 | const bool wasBlocked = blockNotifications(true); |
| 61 | emit thetaChanged(theta); |
| 62 | blockNotifications(wasBlocked); |
| 63 | updateTransform(); |
| 64 | } |
| 65 | |
| 66 | void Entity::setPhi(float phi) |
| 67 | { |
| 68 | if (qFuzzyCompare(m_phi, phi)) |
| 69 | return; |
| 70 | |
| 71 | m_phi = phi; |
| 72 | const bool wasBlocked = blockNotifications(true); |
| 73 | emit phiChanged(phi); |
| 74 | blockNotifications(wasBlocked); |
| 75 | updateTransform(); |
| 76 | } |
| 77 | |
| 78 | void Entity::setPosition(QVector3D position) |
| 79 | { |
| 80 | if (m_position == position) |
| 81 | return; |
| 82 | |
| 83 | m_position = position; |
| 84 | emit positionChanged(position); |
| 85 | updateTransform(); |
| 86 | } |
| 87 | |
| 88 | void Entity::setDiffuseColor(QColor diffuseColor) |
| 89 | { |
| 90 | if (m_diffuseColorParam->value().value<QColor>() == diffuseColor) |
| 91 | return; |
| 92 | |
| 93 | m_diffuseColorParam->setValue(QVariant::fromValue(diffuseColor)); |
| 94 | const bool wasBlocked = blockNotifications(true); |
| 95 | emit diffuseColorChanged(diffuseColor); |
| 96 | blockNotifications(wasBlocked); |
| 97 | } |