Debian Codegraph @

tests/manual/mesh-morphing/main.cpp

raw symbols SCIP
1 // Copyright (C) 2016 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include <QGuiApplication>
5 #include <QPropertyAnimation>
6
7 #include <Qt3DInput/QInputAspect>
8
9 #include <Qt3DRender/qcamera.h>
10 #include <Qt3DExtras/qcylindermesh.h>
11 #include <Qt3DExtras/QPhongMaterial>
12 #include <Qt3DExtras/QMorphPhongMaterial>
13 #include <Qt3DAnimation/QVertexBlendAnimation>
14 #include <Qt3DAnimation/QMorphTarget>
15 #include <Qt3DExtras/QCylinderGeometry>
16
17 #include <Qt3DCore/qentity.h>
18 #include <Qt3DCore/qtransform.h>
19 #include <Qt3DCore/qaspectengine.h>
20
21 #include <Qt3DExtras/qt3dwindow.h>
22 #include <Qt3DExtras/QForwardRenderer>
23 #include <Qt3DExtras/qfirstpersoncameracontroller.h>
24
25 #include <QtGui/qquaternion.h>
26
27 int main(int argc, char **argv)
28 {
29 QGuiApplication app(argc, argv);
30 Qt3DExtras::Qt3DWindow view;
31
32 // Root entity
33 Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity();
34
35 // Camera
36 Qt3DRender::QCamera *camera = view.camera();
37 camera->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f);
38 camera->setPosition(QVector3D(0, 2, 20.0f));
39 camera->setUpVector(QVector3D(0, 1, 0));
40 camera->setViewCenter(QVector3D(0, 0, 0));
41
42 // For camera controls
43 Qt3DExtras::QFirstPersonCameraController *cameraController
44 = new Qt3DExtras::QFirstPersonCameraController(rootEntity);
45 cameraController->setCamera(camera);
46
47 view.defaultFrameGraph()->setCamera(camera);
48 view.defaultFrameGraph()->setClearColor(Qt::gray);
49
50 // Transform for mesh
51 Qt3DCore::QTransform *transform = new Qt3DCore::QTransform;
52 transform->setTranslation(QVector3D(0,-1,-1));
53
54 // Base mesh to morph
55 Qt3DExtras::QCylinderMesh *mesh = new Qt3DExtras::QCylinderMesh(rootEntity);
56 mesh->setRings(6);
57 mesh->setSlices(20);
58
59 // create morh targets from geometry
60 Qt3DExtras::QCylinderGeometry *cylinder1 = new Qt3DExtras::QCylinderGeometry(rootEntity);
61 Qt3DExtras::QCylinderGeometry *cylinder2 = new Qt3DExtras::QCylinderGeometry(rootEntity);
62 Qt3DExtras::QCylinderGeometry *cylinder3 = new Qt3DExtras::QCylinderGeometry(rootEntity);
63
64 cylinder1->setRings(6);
65 cylinder1->setSlices(20);
66 cylinder1->setLength(2.0f);
67 cylinder1->setRadius(1.0f);
68
69 cylinder2->setRings(6);
70 cylinder2->setSlices(20);
71 cylinder2->setLength(1.0f);
72 cylinder2->setRadius(5.0f);
73
74 cylinder3->setRings(6);
75 cylinder3->setSlices(20);
76 cylinder3->setLength(9.0f);
77 cylinder3->setRadius(1.0f);
78
79 QStringList attributes;
80 attributes.push_back(Qt3DCore::QAttribute::defaultPositionAttributeName());
81 attributes.push_back(Qt3DCore::QAttribute::defaultNormalAttributeName());
82
83 const QList<Qt3DAnimation::QMorphTarget*> morphTargets
84 = { Qt3DAnimation::QMorphTarget::fromGeometry(cylinder1, attributes),
85 Qt3DAnimation::QMorphTarget::fromGeometry(cylinder2, attributes),
86 Qt3DAnimation::QMorphTarget::fromGeometry(cylinder3, attributes),
87 Qt3DAnimation::QMorphTarget::fromGeometry(cylinder1, attributes) };
88
89 Qt3DAnimation::QVertexBlendAnimation *animation = new Qt3DAnimation::QVertexBlendAnimation;
90 const QList<float> times = { 0.0f, 5.0f, 8.0f, 12.0f };
91
92 animation->setTargetPositions(times);
93 animation->setTarget(mesh);
94 animation->setMorphTargets(morphTargets);
95
96 // Material
97 Qt3DExtras::QMorphPhongMaterial *material = new Qt3DExtras::QMorphPhongMaterial(rootEntity);
98 material->setDiffuse(Qt::red);
99
100 QObject::connect(animation, &Qt3DAnimation::QVertexBlendAnimation::interpolatorChanged,
101 material, &Qt3DExtras::QMorphPhongMaterial::setInterpolator);
102
103 // Cylinder
104 Qt3DCore::QEntity *morphingEntity = new Qt3DCore::QEntity(rootEntity);
105 morphingEntity->addComponent(mesh);
106 morphingEntity->addComponent(transform);
107 morphingEntity->addComponent(material);
108
109 // Cylinder shape data
110 Qt3DExtras::QCylinderMesh *cylinderMesh = new Qt3DExtras::QCylinderMesh();
111 cylinderMesh->setRadius(1);
112 cylinderMesh->setLength(3);
113 cylinderMesh->setRings(100);
114 cylinderMesh->setSlices(20);
115
116 // Transform for cylinder
117 Qt3DCore::QTransform *cylinderTransform = new Qt3DCore::QTransform;
118 cylinderTransform->setScale(0.5f);
119 cylinderTransform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1, 0, 0), 45.0f));
120
121 // Material
122 Qt3DExtras::QPhongMaterial *cylinderMaterial = new Qt3DExtras::QPhongMaterial(rootEntity);
123 cylinderMaterial->setDiffuse(Qt::red);
124
125 // Cylinder
126 Qt3DCore::QEntity *cylinder = new Qt3DCore::QEntity(rootEntity);
127 cylinder->addComponent(cylinderMesh);
128 cylinder->addComponent(cylinderTransform);
129 cylinder->addComponent(cylinderMaterial);
130
131 QPropertyAnimation* anim = new QPropertyAnimation(animation);
132 anim->setDuration(5000);
133 anim->setEndValue(QVariant::fromValue(12.0f));
134 anim->setStartValue(QVariant::fromValue(0.0f));
135 anim->setLoopCount(-1);
136 anim->setTargetObject(animation);
137 anim->setPropertyName("position");
138 anim->start();
139
140 // Set root object of the scene
141 view.setRootEntity(rootEntity);
142 view.show();
143
144 return app.exec();
145 }