1/****************************************************************************
2**
3** Copyright (C) 2016 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 <QGuiApplication>
52
53#include <Qt3DRender/qcamera.h>
54#include <Qt3DExtras/qcylindermesh.h>
55#include <Qt3DExtras/qspheremesh.h>
56#include <Qt3DExtras/qphongmaterial.h>
57
58#include <Qt3DCore/qentity.h>
59#include <Qt3DCore/qtransform.h>
60
61#include <Qt3DExtras/qt3dwindow.h>
62#include <Qt3DExtras/qorbitcameracontroller.h>
63
64#include <QTimer>
65
66class ComponentSwapper : public QObject
67{
68 Q_OBJECT
69public:
70 ComponentSwapper(Qt3DCore::QEntity *entity,
71 Qt3DCore::QComponent *component1,
72 Qt3DCore::QComponent *component2,
73 QObject * parent = nullptr)
74 : QObject(parent)
75 , m_entity(entity)
76 , m_component1(component1)
77 , m_component2(component2)
78 , m_currentComponent(component1)
79 {
80 // Set initial state
81 m_entity->addComponent(comp: m_component1);
82 }
83
84public slots:
85 void swapComponents()
86 {
87 if (m_currentComponent == m_component1) {
88 m_entity->removeComponent(comp: m_component1);
89 m_entity->addComponent(comp: m_component2);
90 m_currentComponent = m_component2;
91 } else {
92 m_entity->removeComponent(comp: m_component2);
93 m_entity->addComponent(comp: m_component1);
94 m_currentComponent = m_component1;
95 }
96 }
97
98private:
99 Qt3DCore::QEntity *m_entity;
100 Qt3DCore::QComponent *m_component1;
101 Qt3DCore::QComponent *m_component2;
102 Qt3DCore::QComponent *m_currentComponent;
103};
104
105
106int main(int argc, char **argv)
107{
108 QGuiApplication app(argc, argv);
109 Qt3DExtras::Qt3DWindow view;
110
111 // Root entity
112 Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity();
113
114 // Camera
115 Qt3DRender::QCamera *camera = view.camera();
116 camera->setPosition(QVector3D(0, 0, 20.0f));
117 camera->setViewCenter(QVector3D(0, 0, 0));
118
119 // For camera controls
120 Qt3DExtras::QOrbitCameraController *cameraController = new Qt3DExtras::QOrbitCameraController(rootEntity);
121 cameraController->setCamera(camera);
122
123 // Cylinder mesh data
124 Qt3DExtras::QCylinderMesh *cylinderMesh = new Qt3DExtras::QCylinderMesh();
125 cylinderMesh->setRadius(1);
126 cylinderMesh->setLength(3);
127 cylinderMesh->setRings(5);
128 cylinderMesh->setSlices(40);
129
130 // Sphere mesh data
131 Qt3DExtras::QSphereMesh *sphereMesh = new Qt3DExtras::QSphereMesh();
132 sphereMesh->setRings(20);
133 sphereMesh->setSlices(40);
134
135 // Transform for cylinder
136 Qt3DCore::QTransform *transform = new Qt3DCore::QTransform;
137 transform->setScale(1.5f);
138 transform->setRotation(QQuaternion::fromAxisAndAngle(axis: QVector3D(1, 0, 0), angle: 45.0f));
139
140 // Material
141 Qt3DExtras::QPhongMaterial *material = new Qt3DExtras::QPhongMaterial(rootEntity);
142 material->setDiffuse(Qt::red);
143
144 // Cylinder
145 Qt3DCore::QEntity *cylinder = new Qt3DCore::QEntity(rootEntity);
146 cylinder->addComponent(comp: transform);
147 cylinder->addComponent(comp: material);
148
149 // Set root object of the scene
150 view.setRootEntity(rootEntity);
151 view.show();
152
153 ComponentSwapper *swapper = new ComponentSwapper(cylinder, cylinderMesh, sphereMesh);
154 QTimer *timer = new QTimer;
155 QObject::connect(sender: timer, SIGNAL(timeout()), receiver: swapper, SLOT(swapComponents()));
156 timer->start(msec: 2000);
157
158 return app.exec();
159}
160
161#include "main.moc"
162

source code of qt3d/tests/manual/component-changes/main.cpp