| 1 | // Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB). |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include "boxentity.h" |
| 5 | #include <Qt3DRender/QGeometryRenderer> |
| 6 | |
| 7 | #include <qmath.h> |
| 8 | |
| 9 | BoxEntity::BoxEntity(QNode *parent) |
| 10 | : Qt3DCore::QEntity(parent) |
| 11 | , m_transform(new Qt3DCore::QTransform()) |
| 12 | , m_mesh(new Qt3DExtras::QCuboidMesh()) |
| 13 | , m_material(new Qt3DExtras::QPhongMaterial()) |
| 14 | , m_angle(0.0f) |
| 15 | , m_radius(1.0f) |
| 16 | { |
| 17 | connect(m_material, SIGNAL(diffuseChanged(QColor)), |
| 18 | this, SIGNAL(diffuseColorChanged(QColor))); |
| 19 | m_material->setAmbient(Qt::gray); |
| 20 | m_material->setSpecular(Qt::white); |
| 21 | m_material->setShininess(150.0f); |
| 22 | |
| 23 | addComponent(m_transform); |
| 24 | addComponent(m_mesh); |
| 25 | addComponent(m_material); |
| 26 | } |
| 27 | |
| 28 | void BoxEntity::setDiffuseColor(const QColor &diffuseColor) |
| 29 | { |
| 30 | m_material->setDiffuse(diffuseColor); |
| 31 | } |
| 32 | |
| 33 | void BoxEntity::setAngle(float arg) |
| 34 | { |
| 35 | if (m_angle == arg) |
| 36 | return; |
| 37 | |
| 38 | m_angle = arg; |
| 39 | emit angleChanged(); |
| 40 | updateTransformation(); |
| 41 | } |
| 42 | |
| 43 | void BoxEntity::setRadius(float arg) |
| 44 | { |
| 45 | if (m_radius == arg) |
| 46 | return; |
| 47 | |
| 48 | m_radius = arg; |
| 49 | emit radiusChanged(); |
| 50 | updateTransformation(); |
| 51 | } |
| 52 | |
| 53 | QColor BoxEntity::diffuseColor() |
| 54 | { |
| 55 | return m_material->diffuse(); |
| 56 | } |
| 57 | |
| 58 | float BoxEntity::angle() const |
| 59 | { |
| 60 | return m_angle; |
| 61 | } |
| 62 | |
| 63 | float BoxEntity::radius() const |
| 64 | { |
| 65 | return m_radius; |
| 66 | } |
| 67 | |
| 68 | void BoxEntity::updateTransformation() |
| 69 | { |
| 70 | m_transform->setTranslation(QVector3D(qCos(m_angle) * m_radius, |
| 71 | 1.0f, |
| 72 | qSin(m_angle) * m_radius)); |
| 73 | } |
| 74 |