1/****************************************************************************
2**
3** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB).
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt3D module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "entity.h"
52
53#include <Qt3DExtras/QCylinderMesh>
54#include <Qt3DRender/QMaterial>
55#include <Qt3DRender/QParameter>
56#include <Qt3DCore/QTransform>
57#include <QMatrix4x4>
58
59Entity::Entity(Qt3DRender::QEffect *effect, Qt3DCore::QNode *parent)
60 : QEntity(parent)
61 , m_transform(new Qt3DCore::QTransform())
62 , m_material(new Qt3DRender::QMaterial())
63 , m_diffuseColorParam(new Qt3DRender::QParameter())
64{
65 m_diffuseColorParam->setName(QLatin1String("kd"));
66 m_material->addParameter(parameter: m_diffuseColorParam);
67 m_material->setEffect(effect);
68
69 addComponent(comp: m_transform);
70 addComponent(comp: m_material);
71}
72
73void Entity::updateTransform()
74{
75 QMatrix4x4 m;
76 m.translate(vector: m_position);
77 m.rotate(angle: m_phi, vector: QVector3D(1.0f, 0.0f, 0.0f));
78 m.rotate(angle: m_theta, vector: QVector3D(0.0f, 0.0f, 1.0f));
79 m_transform->setMatrix(m);
80}
81
82float Entity::theta() const
83{
84 return m_theta;
85}
86
87float Entity::phi() const
88{
89 return m_phi;
90}
91
92QVector3D Entity::position() const
93{
94 return m_position;
95}
96
97QColor Entity::diffuseColor() const
98{
99 return m_diffuseColorParam->value().value<QColor>();
100}
101
102void Entity::setTheta(float theta)
103{
104 if (qFuzzyCompare(p1: m_theta, p2: theta))
105 return;
106
107 m_theta = theta;
108 const bool wasBlocked = blockNotifications(block: true);
109 emit thetaChanged(theta);
110 blockNotifications(block: wasBlocked);
111 updateTransform();
112}
113
114void Entity::setPhi(float phi)
115{
116 if (qFuzzyCompare(p1: m_phi, p2: phi))
117 return;
118
119 m_phi = phi;
120 const bool wasBlocked = blockNotifications(block: true);
121 emit phiChanged(phi);
122 blockNotifications(block: wasBlocked);
123 updateTransform();
124}
125
126void Entity::setPosition(QVector3D position)
127{
128 if (m_position == position)
129 return;
130
131 m_position = position;
132 emit positionChanged(position);
133 updateTransform();
134}
135
136void Entity::setDiffuseColor(QColor diffuseColor)
137{
138 if (m_diffuseColorParam->value().value<QColor>() == diffuseColor)
139 return;
140
141 m_diffuseColorParam->setValue(QVariant::fromValue(value: diffuseColor));
142 const bool wasBlocked = blockNotifications(block: true);
143 emit diffuseColorChanged(diffuseColor);
144 blockNotifications(block: wasBlocked);
145}
146

source code of qt3d/tests/manual/bigscene-cpp/entity.cpp