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 "entity.h"
52
53#include <QGuiApplication>
54
55#include <QPropertyAnimation>
56#include <QUrl>
57#include <QTimer>
58#include <Qt3DCore/QEntity>
59#include <Qt3DRender/QCamera>
60#include <Qt3DCore/QTransform>
61#include <Qt3DCore/qaspectengine.h>
62#include <Qt3DInput/QInputAspect>
63#include <Qt3DRender/QParameter>
64#include <Qt3DExtras/QCylinderMesh>
65#include <Qt3DRender/QRenderAspect>
66#include <Qt3DRender/QCameraSelector>
67#include <Qt3DExtras/QPhongMaterial>
68#include <Qt3DExtras/QForwardRenderer>
69#include <qmath.h>
70#include <Qt3DExtras/qt3dwindow.h>
71#include <Qt3DExtras/qfirstpersoncameracontroller.h>
72
73using namespace Qt3DCore;
74using namespace Qt3DRender;
75
76int main(int ac, char **av)
77{
78 QGuiApplication app(ac, av);
79 Qt3DExtras::Qt3DWindow view;
80 view.defaultFrameGraph()->setClearColor(Qt::black);
81
82 QEntity *root = new QEntity();
83
84 // Mesh
85 auto *mesh = new Qt3DExtras::QCylinderMesh(root);
86 mesh->setRings(50.0f);
87 mesh->setSlices(30.0f);
88 mesh->setRadius(2.5f);
89 mesh->setLength(5.0f);
90
91 // Material
92 auto phongMaterial = new Qt3DExtras::QPhongMaterial(root);
93 auto effect = phongMaterial->effect();
94
95 // Camera
96 QCamera *cameraEntity = view.camera();
97 cameraEntity->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f);
98 cameraEntity->setPosition(QVector3D(0, 250.0f, 50.0f));
99 cameraEntity->setUpVector(QVector3D(0, 1, 0));
100 cameraEntity->setViewCenter(QVector3D(0, 0, 0));
101
102 // For camera controls
103 Qt3DExtras::QFirstPersonCameraController *camController = new Qt3DExtras::QFirstPersonCameraController(root);
104 camController->setCamera(cameraEntity);
105
106 const double radius = 100.0;
107 const int max = 1000;
108 const double det = 1.0 / max;
109
110 // Scene
111 for (int i = 0; i < max; i++) {
112 Entity *e = new Entity(effect, root);
113 e->addComponent(mesh);
114 const double angle = M_PI * 2.0 * double(i) * det * 10.;
115
116 e->setDiffuseColor(QColor(int(qFabs(qCos(angle)) * 255.0), 204, 75));
117 e->setPosition(QVector3D(float(radius * qCos(angle)),
118 float(-200.0 * i * det),
119 float(radius * qSin(angle))));
120 e->setTheta(30.0f * i);
121 e->setPhi(45.0f * i);
122
123 QPropertyAnimation *animX = new QPropertyAnimation(e, QByteArrayLiteral("theta"));
124 animX->setDuration(2400 * (i + 1));
125 animX->setStartValue(QVariant::fromValue(i * 30.0f));
126 animX->setEndValue(QVariant::fromValue((i + 1) * 390.0f));
127 animX->setLoopCount(-1);
128 animX->start();
129
130 QPropertyAnimation *animZ = new QPropertyAnimation(e, QByteArrayLiteral("phi"));
131 animZ->setDuration(2400 * (i + 1));
132 animZ->setStartValue(QVariant::fromValue(i * 20.0f));
133 animZ->setEndValue(QVariant::fromValue((i + 1) * 380.0f));
134 animZ->setLoopCount(-1);
135 animZ->start();
136 }
137
138 view.setRootEntity(root);
139 view.show();
140
141 if (app.arguments().contains(("--bench")))
142 QTimer::singleShot(25 * 1000, &app, &QCoreApplication::quit);
143
144 return app.exec();
145}
146