1/****************************************************************************
2**
3** Copyright (C) 2014 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 "scenemodifier.h"
52
53#include <QtCore/QDebug>
54
55SceneModifier::SceneModifier(Qt3DCore::QEntity *rootEntity)
56 : m_rootEntity(rootEntity)
57{
58
59 // Torus shape data
60 //! [0]
61 m_torus = new Qt3DExtras::QTorusMesh();
62 m_torus->setRadius(1.0f);
63 m_torus->setMinorRadius(0.4f);
64 m_torus->setRings(100);
65 m_torus->setSlices(20);
66 //! [0]
67
68 // TorusMesh Transform
69 //! [1]
70 Qt3DCore::QTransform *torusTransform = new Qt3DCore::QTransform();
71 torusTransform->setScale(2.0f);
72 torusTransform->setRotation(QQuaternion::fromAxisAndAngle(axis: QVector3D(0.0f, 1.0f, 0.0f), angle: 25.0f));
73 torusTransform->setTranslation(QVector3D(5.0f, 4.0f, 0.0f));
74 //! [1]
75
76 //! [2]
77 Qt3DExtras::QPhongMaterial *torusMaterial = new Qt3DExtras::QPhongMaterial();
78 torusMaterial->setDiffuse(QColor(QRgb(0xbeb32b)));
79 //! [2]
80
81 // Torus
82 //! [3]
83 m_torusEntity = new Qt3DCore::QEntity(m_rootEntity);
84 m_torusEntity->addComponent(comp: m_torus);
85 m_torusEntity->addComponent(comp: torusMaterial);
86 m_torusEntity->addComponent(comp: torusTransform);
87 //! [3]
88
89 // Cone shape data
90 Qt3DExtras::QConeMesh *cone = new Qt3DExtras::QConeMesh();
91 cone->setTopRadius(0.5);
92 cone->setBottomRadius(1);
93 cone->setLength(3);
94 cone->setRings(50);
95 cone->setSlices(20);
96
97 // ConeMesh Transform
98 Qt3DCore::QTransform *coneTransform = new Qt3DCore::QTransform();
99 coneTransform->setScale(1.5f);
100 coneTransform->setRotation(QQuaternion::fromAxisAndAngle(axis: QVector3D(1.0f, 0.0f, 0.0f), angle: 45.0f));
101 coneTransform->setTranslation(QVector3D(0.0f, 4.0f, -1.5));
102
103 Qt3DExtras::QPhongMaterial *coneMaterial = new Qt3DExtras::QPhongMaterial();
104 coneMaterial->setDiffuse(QColor(QRgb(0x928327)));
105
106 // Cone
107 m_coneEntity = new Qt3DCore::QEntity(m_rootEntity);
108 m_coneEntity->addComponent(comp: cone);
109 m_coneEntity->addComponent(comp: coneMaterial);
110 m_coneEntity->addComponent(comp: coneTransform);
111
112 // Cylinder shape data
113 Qt3DExtras::QCylinderMesh *cylinder = new Qt3DExtras::QCylinderMesh();
114 cylinder->setRadius(1);
115 cylinder->setLength(3);
116 cylinder->setRings(100);
117 cylinder->setSlices(20);
118
119 // CylinderMesh Transform
120 Qt3DCore::QTransform *cylinderTransform = new Qt3DCore::QTransform();
121 cylinderTransform->setScale(1.5f);
122 cylinderTransform->setRotation(QQuaternion::fromAxisAndAngle(axis: QVector3D(1.0f, 0.0f, 0.0f), angle: 45.0f));
123 cylinderTransform->setTranslation(QVector3D(-5.0f, 4.0f, -1.5));
124
125 Qt3DExtras::QPhongMaterial *cylinderMaterial = new Qt3DExtras::QPhongMaterial();
126 cylinderMaterial->setDiffuse(QColor(QRgb(0x928327)));
127
128 // Cylinder
129 m_cylinderEntity = new Qt3DCore::QEntity(m_rootEntity);
130 m_cylinderEntity->addComponent(comp: cylinder);
131 m_cylinderEntity->addComponent(comp: cylinderMaterial);
132 m_cylinderEntity->addComponent(comp: cylinderTransform);
133
134 // Cuboid shape data
135 Qt3DExtras::QCuboidMesh *cuboid = new Qt3DExtras::QCuboidMesh();
136
137 // CuboidMesh Transform
138 Qt3DCore::QTransform *cuboidTransform = new Qt3DCore::QTransform();
139 cuboidTransform->setScale(4.0f);
140 cuboidTransform->setTranslation(QVector3D(5.0f, -4.0f, 0.0f));
141
142 Qt3DExtras::QPhongMaterial *cuboidMaterial = new Qt3DExtras::QPhongMaterial();
143 cuboidMaterial->setDiffuse(QColor(QRgb(0x665423)));
144
145 //Cuboid
146 m_cuboidEntity = new Qt3DCore::QEntity(m_rootEntity);
147 m_cuboidEntity->addComponent(comp: cuboid);
148 m_cuboidEntity->addComponent(comp: cuboidMaterial);
149 m_cuboidEntity->addComponent(comp: cuboidTransform);
150
151 // Plane shape data
152 Qt3DExtras::QPlaneMesh *planeMesh = new Qt3DExtras::QPlaneMesh();
153 planeMesh->setWidth(2);
154 planeMesh->setHeight(2);
155
156 // Plane mesh transform
157 Qt3DCore::QTransform *planeTransform = new Qt3DCore::QTransform();
158 planeTransform->setScale(1.3f);
159 planeTransform->setRotation(QQuaternion::fromAxisAndAngle(axis: QVector3D(1.0f, 0.0f, 0.0f), angle: 45.0f));
160 planeTransform->setTranslation(QVector3D(0.0f, -4.0f, 0.0f));
161
162 Qt3DExtras::QPhongMaterial *planeMaterial = new Qt3DExtras::QPhongMaterial();
163 planeMaterial->setDiffuse(QColor(QRgb(0xa69929)));
164
165 // Plane
166 m_planeEntity = new Qt3DCore::QEntity(m_rootEntity);
167 m_planeEntity->addComponent(comp: planeMesh);
168 m_planeEntity->addComponent(comp: planeMaterial);
169 m_planeEntity->addComponent(comp: planeTransform);
170
171 // Sphere shape data
172 Qt3DExtras::QSphereMesh *sphereMesh = new Qt3DExtras::QSphereMesh();
173 sphereMesh->setRings(20);
174 sphereMesh->setSlices(20);
175 sphereMesh->setRadius(2);
176
177 // Sphere mesh transform
178 Qt3DCore::QTransform *sphereTransform = new Qt3DCore::QTransform();
179
180 sphereTransform->setScale(1.3f);
181 sphereTransform->setTranslation(QVector3D(-5.0f, -4.0f, 0.0f));
182
183 Qt3DExtras::QPhongMaterial *sphereMaterial = new Qt3DExtras::QPhongMaterial();
184 sphereMaterial->setDiffuse(QColor(QRgb(0xa69929)));
185
186 // Sphere
187 m_sphereEntity = new Qt3DCore::QEntity(m_rootEntity);
188 m_sphereEntity->addComponent(comp: sphereMesh);
189 m_sphereEntity->addComponent(comp: sphereMaterial);
190 m_sphereEntity->addComponent(comp: sphereTransform);
191}
192
193SceneModifier::~SceneModifier()
194{
195}
196
197//! [4]
198void SceneModifier::enableTorus(bool enabled)
199{
200 m_torusEntity->setEnabled(enabled);
201}
202//! [4]
203
204void SceneModifier::enableCone(bool enabled)
205{
206 m_coneEntity->setEnabled(enabled);
207}
208
209void SceneModifier::enableCylinder(bool enabled)
210{
211 m_cylinderEntity->setEnabled(enabled);
212}
213
214void SceneModifier::enableCuboid(bool enabled)
215{
216 m_cuboidEntity->setEnabled(enabled);
217}
218
219void SceneModifier::enablePlane(bool enabled)
220{
221 m_planeEntity->setEnabled(enabled);
222}
223
224void SceneModifier::enableSphere(bool enabled)
225{
226 m_sphereEntity->setEnabled(enabled);
227}
228

source code of qt3d/examples/qt3d/basicshapes-cpp/scenemodifier.cpp