1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the Qt3D module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL3$
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 http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or later as published by the Free
28** Software Foundation and appearing in the file LICENSE.GPL included in
29** the packaging of this file. Please review the following information to
30** ensure the GNU General Public License version 2.0 requirements will be
31** met: http://www.gnu.org/licenses/gpl-2.0.html.
32**
33** $QT_END_LICENSE$
34**
35****************************************************************************/
36
37#include <QGuiApplication>
38#include <QPropertyAnimation>
39
40#include <Qt3DInput/QInputAspect>
41
42#include <Qt3DRender/qcamera.h>
43#include <Qt3DExtras/qcylindermesh.h>
44#include <Qt3DExtras/QPhongMaterial>
45#include <Qt3DExtras/QMorphPhongMaterial>
46#include <Qt3DAnimation/QVertexBlendAnimation>
47#include <Qt3DAnimation/QMorphTarget>
48#include <Qt3DExtras/QCylinderGeometry>
49
50#include <Qt3DCore/qentity.h>
51#include <Qt3DCore/qtransform.h>
52#include <Qt3DCore/qaspectengine.h>
53
54#include <Qt3DExtras/qt3dwindow.h>
55#include <Qt3DExtras/QForwardRenderer>
56#include <Qt3DExtras/qfirstpersoncameracontroller.h>
57
58int main(int argc, char **argv)
59{
60 QGuiApplication app(argc, argv);
61 Qt3DExtras::Qt3DWindow view;
62
63 // Root entity
64 Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity();
65
66 // Camera
67 Qt3DRender::QCamera *camera = view.camera();
68 camera->lens()->setPerspectiveProjection(fieldOfView: 45.0f, aspect: 16.0f/9.0f, nearPlane: 0.1f, farPlane: 1000.0f);
69 camera->setPosition(QVector3D(0, 2, 20.0f));
70 camera->setUpVector(QVector3D(0, 1, 0));
71 camera->setViewCenter(QVector3D(0, 0, 0));
72
73 // For camera controls
74 Qt3DExtras::QFirstPersonCameraController *cameraController
75 = new Qt3DExtras::QFirstPersonCameraController(rootEntity);
76 cameraController->setCamera(camera);
77
78 view.defaultFrameGraph()->setCamera(camera);
79 view.defaultFrameGraph()->setClearColor(Qt::gray);
80
81 // Transform for mesh
82 Qt3DCore::QTransform *transform = new Qt3DCore::QTransform;
83 transform->setTranslation(QVector3D(0,-1,-1));
84
85 // Base mesh to morph
86 Qt3DExtras::QCylinderMesh *mesh = new Qt3DExtras::QCylinderMesh(rootEntity);
87 mesh->setRings(6);
88 mesh->setSlices(20);
89
90 // create morh targets from geometry
91 Qt3DExtras::QCylinderGeometry *cylinder1 = new Qt3DExtras::QCylinderGeometry(rootEntity);
92 Qt3DExtras::QCylinderGeometry *cylinder2 = new Qt3DExtras::QCylinderGeometry(rootEntity);
93 Qt3DExtras::QCylinderGeometry *cylinder3 = new Qt3DExtras::QCylinderGeometry(rootEntity);
94
95 cylinder1->setRings(6);
96 cylinder1->setSlices(20);
97 cylinder1->setLength(2.0f);
98 cylinder1->setRadius(1.0f);
99
100 cylinder2->setRings(6);
101 cylinder2->setSlices(20);
102 cylinder2->setLength(1.0f);
103 cylinder2->setRadius(5.0f);
104
105 cylinder3->setRings(6);
106 cylinder3->setSlices(20);
107 cylinder3->setLength(9.0f);
108 cylinder3->setRadius(1.0f);
109
110 QStringList attributes;
111 attributes.push_back(t: Qt3DRender::QAttribute::defaultPositionAttributeName());
112 attributes.push_back(t: Qt3DRender::QAttribute::defaultNormalAttributeName());
113
114 QVector<Qt3DAnimation::QMorphTarget*> morphTargets;
115 morphTargets.push_back(t: Qt3DAnimation::QMorphTarget::fromGeometry(geometry: cylinder1, attributes));
116 morphTargets.push_back(t: Qt3DAnimation::QMorphTarget::fromGeometry(geometry: cylinder2, attributes));
117 morphTargets.push_back(t: Qt3DAnimation::QMorphTarget::fromGeometry(geometry: cylinder3, attributes));
118 morphTargets.push_back(t: morphTargets.first());
119
120 Qt3DAnimation::QVertexBlendAnimation *animation = new Qt3DAnimation::QVertexBlendAnimation;
121 QVector<float> times;
122 times.push_back(t: 0.0f);
123 times.push_back(t: 5.0f);
124 times.push_back(t: 8.0f);
125 times.push_back(t: 12.0f);
126
127 animation->setTargetPositions(times);
128 animation->setTarget(mesh);
129 animation->setMorphTargets(morphTargets);
130
131 // Material
132 Qt3DExtras::QMorphPhongMaterial *material = new Qt3DExtras::QMorphPhongMaterial(rootEntity);
133 material->setDiffuse(Qt::red);
134
135 QObject::connect(sender: animation, signal: &Qt3DAnimation::QVertexBlendAnimation::interpolatorChanged,
136 receiver: material, slot: &Qt3DExtras::QMorphPhongMaterial::setInterpolator);
137
138 // Cylinder
139 Qt3DCore::QEntity *morphingEntity = new Qt3DCore::QEntity(rootEntity);
140 morphingEntity->addComponent(comp: mesh);
141 morphingEntity->addComponent(comp: transform);
142 morphingEntity->addComponent(comp: material);
143
144 // Cylinder shape data
145 Qt3DExtras::QCylinderMesh *cylinderMesh = new Qt3DExtras::QCylinderMesh();
146 cylinderMesh->setRadius(1);
147 cylinderMesh->setLength(3);
148 cylinderMesh->setRings(100);
149 cylinderMesh->setSlices(20);
150
151 // Transform for cylinder
152 Qt3DCore::QTransform *cylinderTransform = new Qt3DCore::QTransform;
153 cylinderTransform->setScale(0.5f);
154 cylinderTransform->setRotation(QQuaternion::fromAxisAndAngle(axis: QVector3D(1, 0, 0), angle: 45.0f));
155
156 // Material
157 Qt3DExtras::QPhongMaterial *cylinderMaterial = new Qt3DExtras::QPhongMaterial(rootEntity);
158 cylinderMaterial->setDiffuse(Qt::red);
159
160 // Cylinder
161 Qt3DCore::QEntity *cylinder = new Qt3DCore::QEntity(rootEntity);
162 cylinder->addComponent(comp: cylinderMesh);
163 cylinder->addComponent(comp: cylinderTransform);
164 cylinder->addComponent(comp: cylinderMaterial);
165
166 QPropertyAnimation* anim = new QPropertyAnimation(animation);
167 anim->setDuration(5000);
168 anim->setEndValue(QVariant::fromValue(value: 12.0f));
169 anim->setStartValue(QVariant::fromValue(value: 0.0f));
170 anim->setLoopCount(-1);
171 anim->setTargetObject(animation);
172 anim->setPropertyName("position");
173 anim->start();
174
175 // Set root object of the scene
176 view.setRootEntity(rootEntity);
177 view.show();
178
179 return app.exec();
180}
181

source code of qt3d/tests/manual/mesh-morphing/main.cpp