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 <QGuiApplication> |
52 | |
53 | #include <Qt3DCore/QEntity> |
54 | #include <Qt3DRender/QCamera> |
55 | #include <Qt3DRender/QCameraLens> |
56 | #include <Qt3DCore/QTransform> |
57 | #include <Qt3DCore/QAspectEngine> |
58 | |
59 | #include <Qt3DInput/QInputAspect> |
60 | |
61 | #include <Qt3DRender/QRenderAspect> |
62 | #include <Qt3DExtras/QForwardRenderer> |
63 | #include <Qt3DExtras/QPhongMaterial> |
64 | #include <Qt3DExtras/QCylinderMesh> |
65 | #include <Qt3DExtras/QSphereMesh> |
66 | #include <Qt3DExtras/QTorusMesh> |
67 | |
68 | #include <QPropertyAnimation> |
69 | |
70 | #include "qt3dwindow.h" |
71 | #include "orbittransformcontroller.h" |
72 | #include "qorbitcameracontroller.h" |
73 | |
74 | Qt3DCore::QEntity *createScene() |
75 | { |
76 | // Root entity |
77 | Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity; |
78 | |
79 | // Material |
80 | Qt3DRender::QMaterial *material = new Qt3DExtras::QPhongMaterial(rootEntity); |
81 | |
82 | // Torus |
83 | Qt3DCore::QEntity *torusEntity = new Qt3DCore::QEntity(rootEntity); |
84 | Qt3DExtras::QTorusMesh *torusMesh = new Qt3DExtras::QTorusMesh; |
85 | torusMesh->setRadius(5); |
86 | torusMesh->setMinorRadius(1); |
87 | torusMesh->setRings(100); |
88 | torusMesh->setSlices(20); |
89 | |
90 | Qt3DCore::QTransform *torusTransform = new Qt3DCore::QTransform; |
91 | torusTransform->setScale3D(QVector3D(1.5, 1, 0.5)); |
92 | torusTransform->setRotation(QQuaternion::fromAxisAndAngle(axis: QVector3D(1, 0, 0), angle: 45.0f)); |
93 | |
94 | torusEntity->addComponent(comp: torusMesh); |
95 | torusEntity->addComponent(comp: torusTransform); |
96 | torusEntity->addComponent(comp: material); |
97 | |
98 | // Sphere |
99 | Qt3DCore::QEntity *sphereEntity = new Qt3DCore::QEntity(rootEntity); |
100 | Qt3DExtras::QSphereMesh *sphereMesh = new Qt3DExtras::QSphereMesh; |
101 | sphereMesh->setRadius(3); |
102 | sphereMesh->setGenerateTangents(true); |
103 | |
104 | Qt3DCore::QTransform *sphereTransform = new Qt3DCore::QTransform; |
105 | OrbitTransformController *controller = new OrbitTransformController(sphereTransform); |
106 | controller->setTarget(sphereTransform); |
107 | controller->setRadius(20.0f); |
108 | |
109 | QPropertyAnimation *sphereRotateTransformAnimation = new QPropertyAnimation(sphereTransform); |
110 | sphereRotateTransformAnimation->setTargetObject(controller); |
111 | sphereRotateTransformAnimation->setPropertyName("angle" ); |
112 | sphereRotateTransformAnimation->setStartValue(QVariant::fromValue(value: 0)); |
113 | sphereRotateTransformAnimation->setEndValue(QVariant::fromValue(value: 360)); |
114 | sphereRotateTransformAnimation->setDuration(10000); |
115 | sphereRotateTransformAnimation->setLoopCount(-1); |
116 | sphereRotateTransformAnimation->start(); |
117 | |
118 | sphereEntity->addComponent(comp: sphereMesh); |
119 | sphereEntity->addComponent(comp: sphereTransform); |
120 | sphereEntity->addComponent(comp: material); |
121 | |
122 | return rootEntity; |
123 | } |
124 | |
125 | int main(int argc, char* argv[]) |
126 | { |
127 | QGuiApplication app(argc, argv); |
128 | Qt3DExtras::Qt3DWindow view; |
129 | |
130 | Qt3DCore::QEntity *scene = createScene(); |
131 | |
132 | // Camera |
133 | Qt3DRender::QCamera *camera = view.camera(); |
134 | camera->lens()->setPerspectiveProjection(fieldOfView: 45.0f, aspect: 16.0f/9.0f, nearPlane: 0.1f, farPlane: 1000.0f); |
135 | camera->setPosition(QVector3D(0, 0, 40.0f)); |
136 | camera->setViewCenter(QVector3D(0, 0, 0)); |
137 | |
138 | // For camera controls |
139 | Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(scene); |
140 | camController->setLinearSpeed( 50.0f ); |
141 | camController->setLookSpeed( 180.0f ); |
142 | camController->setCamera(camera); |
143 | |
144 | view.setRootEntity(scene); |
145 | view.show(); |
146 | |
147 | return app.exec(); |
148 | } |
149 | |