Debian Codegraph @

tests/auto/render/gltfplugins/tst_gltfplugins.cpp

raw symbols SCIP
1 // Copyright (C) 2016 The Qt Company Ltd and/or its subsidiary(-ies).
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4 #include <QtTest/qtest.h>
5 #include <QtCore/qtemporarydir.h>
6 #include <QtGui/qimage.h>
7
8 #include <private/qsceneimporter_p.h>
9 #include <private/qsceneexportfactory_p.h>
10 #include <private/qsceneexporter_p.h>
11 #include <private/qsceneimportfactory_p.h>
12 #include <private/qsceneimporter_p.h>
13
14 #include <Qt3DCore/qentity.h>
15 #include <Qt3DCore/qtransform.h>
16
17 #include <Qt3DRender/qcamera.h>
18 #include <Qt3DRender/qcameralens.h>
19 #include <Qt3DRender/qtextureimage.h>
20 #include <Qt3DRender/qspotlight.h>
21 #include <Qt3DRender/qdirectionallight.h>
22 #include <Qt3DRender/qpointlight.h>
23 #include <Qt3DCore/qattribute.h>
24 #include <Qt3DCore/qbuffer.h>
25 #include <Qt3DRender/qeffect.h>
26 #include <Qt3DRender/qshaderprogram.h>
27 #include <Qt3DRender/qtechnique.h>
28 #include <Qt3DRender/qparameter.h>
29 #include <Qt3DRender/qgraphicsapifilter.h>
30 #include <Qt3DRender/qfilterkey.h>
31 #include <Qt3DRender/qtexture.h>
32 #include <Qt3DRender/qcolormask.h>
33 #include <Qt3DRender/qblendequation.h>
34 #include <Qt3DRender/qgeometryrenderer.h>
35
36 #include <Qt3DExtras/qconemesh.h>
37 #include <Qt3DExtras/qcuboidmesh.h>
38 #include <Qt3DExtras/qcylindermesh.h>
39 #include <Qt3DExtras/qplanemesh.h>
40 #include <Qt3DExtras/qspheremesh.h>
41 #include <Qt3DExtras/qtorusmesh.h>
42 #include <Qt3DExtras/qt3dwindow.h>
43 #include <Qt3DExtras/qphongmaterial.h>
44 #include <Qt3DExtras/qphongalphamaterial.h>
45 #include <Qt3DExtras/qdiffusemapmaterial.h>
46 #include <Qt3DExtras/qdiffusespecularmapmaterial.h>
47 #include <Qt3DExtras/qnormaldiffusemapmaterial.h>
48 #include <Qt3DExtras/qnormaldiffusemapalphamaterial.h>
49 #include <Qt3DExtras/qnormaldiffusespecularmapmaterial.h>
50 #include <Qt3DExtras/qgoochmaterial.h>
51 #include <Qt3DExtras/qpervertexcolormaterial.h>
52 #include <Qt3DExtras/qforwardrenderer.h>
53
54 //#define VISUAL_CHECK 5000 // The value indicates the time for visual check in ms
55 //#define PRESERVE_EXPORT // Uncomment to preserve export directory contents for analysis
56
57 class tst_gltfPlugins : public QObject
58 {
59 Q_OBJECT
60
61 private Q_SLOTS:
62
63 void initTestCase();
64 void init();
65 void cleanup();
66 void exportAndImport_data();
67 void exportAndImport();
68
69 private:
70 void createTestScene();
71 Qt3DCore::QEntity *findCameraChild(Qt3DCore::QEntity *entity,
72 Qt3DRender::QCameraLens::ProjectionType type);
73 void walkEntity(Qt3DCore::QEntity *entity, int depth);
74 void createAndAddEntity(const QString &name,
75 Qt3DCore::QComponent *comp1 = nullptr,
76 Qt3DCore::QComponent *comp2 = nullptr,
77 Qt3DCore::QComponent *comp3 = nullptr,
78 Qt3DCore::QEntity *parent = nullptr);
79 void addPositionAttributeToGeometry(Qt3DCore::QGeometry *geometry,
80 Qt3DCore::QBuffer *buffer, int count);
81 void addIndexAttributeToGeometry(Qt3DCore::QGeometry *geometry,
82 Qt3DCore::QBuffer *buffer, int count);
83 void addColorAttributeToGeometry(Qt3DCore::QGeometry *geometry,
84 Qt3DCore::QBuffer *buffer, int count);
85 Qt3DCore::QEntity *findChildEntity(Qt3DCore::QEntity *entity, const QString &name);
86 Qt3DCore::QTransform *transformComponent(Qt3DCore::QEntity *entity);
87 Qt3DRender::QAbstractLight *lightComponent(Qt3DCore::QEntity *entity);
88 Qt3DRender::QCameraLens *cameraComponent(Qt3DCore::QEntity *entity);
89 Qt3DRender::QGeometryRenderer *meshComponent(Qt3DCore::QEntity *entity);
90 Qt3DRender::QMaterial *materialComponent(Qt3DCore::QEntity *entity);
91 void compareComponents(Qt3DCore::QComponent *c1, Qt3DCore::QComponent *c2);
92 Qt3DCore::QAttribute *findAttribute(const QString &name,
93 Qt3DCore::QAttribute::AttributeType type,
94 Qt3DCore::QGeometry *geometry);
95 void compareAttributes(Qt3DCore::QAttribute *a1, Qt3DCore::QAttribute *a2);
96 void compareParameters(const QList<Qt3DRender::QParameter *> &params1,
97 const QList<Qt3DRender::QParameter *> &params2);
98 void compareRenderPasses(const QList<Qt3DRender::QRenderPass *> &passes1,
99 const QList<Qt3DRender::QRenderPass *> &passes2);
100 void compareFilterKeys(const QList<Qt3DRender::QFilterKey *> &keys1,
101 const QList<Qt3DRender::QFilterKey *> &keys2);
102 QUrl getTextureUrl(Qt3DRender::QAbstractTexture *tex);
103 Qt3DRender::QGeometryRenderer *createCustomCube();
104 Qt3DRender::QEffect *createOnTopEffect();
105
106 QTemporaryDir *m_exportDir;
107 #ifdef VISUAL_CHECK
108 Qt3DExtras::Qt3DWindow *m_view1;
109 Qt3DExtras::Qt3DWindow *m_view2;
110 #endif
111 Qt3DCore::QEntity *m_sceneRoot1;
112 Qt3DCore::QEntity *m_sceneRoot2;
113 QHash<QString, Qt3DCore::QEntity *> m_entityMap;
114 };
115
116 void tst_gltfPlugins::initTestCase()
117 {
118 #ifndef VISUAL_CHECK
119 // QEffect doesn't get registered unless aspects are initialized, generating warnings in
120 // material comparisons.
121 qRegisterMetaType<Qt3DRender::QEffect *>();
122 #endif
123 }
124
125 void tst_gltfPlugins::init()
126 {
127 m_sceneRoot1 = m_sceneRoot2 = nullptr;
128 m_exportDir = new QTemporaryDir;
129 #ifdef VISUAL_CHECK
130 m_view1 = new Qt3DExtras::Qt3DWindow;
131 m_view1->setTitle(QStringLiteral("Original scene"));
132 m_view2 = new Qt3DExtras::Qt3DWindow;
133 m_view2->setTitle(QStringLiteral("Imported scene"));
134 #endif
135 }
136
137 void tst_gltfPlugins::cleanup()
138 {
139 delete m_sceneRoot1;
140 delete m_sceneRoot2;
141 m_entityMap.clear();
142 #ifdef VISUAL_CHECK
143 delete m_view1;
144 delete m_view2;
145 #endif
146 delete m_exportDir;
147 // Make sure the slate is clean for the next case
148 QCoreApplication::processEvents();
149 QTest::qWait(0);
150 }
151
152 void tst_gltfPlugins::walkEntity(Qt3DCore::QEntity *entity, int depth)
153 {
154 QString indent;
155 indent.fill(' ', depth * 2);
156 qDebug().noquote() << indent << "Entity:" << entity << "Components:" << entity->components();
157 for (auto child : entity->children()) {
158 if (auto childEntity = qobject_cast<Qt3DCore::QEntity *>(child))
159 walkEntity(childEntity, depth + 1);
160 }
161 }
162
163 void tst_gltfPlugins::createAndAddEntity(const QString &name,
164 Qt3DCore::QComponent *comp1,
165 Qt3DCore::QComponent *comp2,
166 Qt3DCore::QComponent *comp3,
167 Qt3DCore::QEntity *parent)
168 {
169 Qt3DCore::QEntity *parentEntity = parent ? parent : m_sceneRoot1;
170 Qt3DCore::QEntity *entity = new Qt3DCore::QEntity(parentEntity);
171 entity->setObjectName(name);
172 if (comp1) {
173 entity->addComponent(comp1);
174 comp1->setObjectName(comp1->metaObject()->className());
175 }
176 if (comp2) {
177 entity->addComponent(comp2);
178 comp2->setObjectName(comp2->metaObject()->className());
179 }
180 if (comp3) {
181 entity->addComponent(comp3);
182 comp3->setObjectName(comp3->metaObject()->className());
183 }
184
185 m_entityMap.insert(name, entity);
186 }
187
188 void tst_gltfPlugins::createTestScene()
189 {
190 #ifdef VISUAL_CHECK
191 m_view1->defaultFrameGraph()->setClearColor(Qt::lightGray);
192 m_view2->defaultFrameGraph()->setClearColor(Qt::lightGray);
193 #endif
194 m_sceneRoot1 = new Qt3DCore::QEntity();
195 m_sceneRoot1->setObjectName(QStringLiteral("Scene root"));
196 m_sceneRoot2 = new Qt3DCore::QEntity();
197 m_sceneRoot2->setObjectName(QStringLiteral("Imported scene parent"));
198
199 // Perspective camera
200 {
201 Qt3DRender::QCamera *camera = new Qt3DRender::QCamera(m_sceneRoot1);
202 camera->setProjectionType(Qt3DRender::QCameraLens::PerspectiveProjection);
203 camera->setViewCenter(QVector3D(0.0f, 1.5f, 0.0f));
204 camera->setPosition(QVector3D(0.0f, 3.5f, 15.0f));
205 camera->setNearPlane(0.001f);
206 camera->setFarPlane(10000.0f);
207 camera->setObjectName(QStringLiteral("Main camera"));
208 camera->transform()->setObjectName(QStringLiteral("Main camera transform"));
209 camera->lens()->setObjectName(QStringLiteral("Main camera lens"));
210 camera->setFieldOfView(30.0f);
211 camera->setAspectRatio(1.0f);
212 m_entityMap.insert(camera->objectName(), camera);
213 }
214 // Ortho camera
215 {
216 Qt3DCore::QEntity *camera = new Qt3DCore::QEntity(m_sceneRoot1);
217 camera->setObjectName(QStringLiteral("Ortho camera"));
218
219 Qt3DCore::QTransform *transform = new Qt3DCore::QTransform;
220 transform->setTranslation(QVector3D(0.0f, 0.0f, -15.0f));
221 transform->setObjectName(QStringLiteral("Ortho camera transform"));
222 camera->addComponent(transform);
223
224 Qt3DRender::QCameraLens *lens = new Qt3DRender::QCameraLens;
225 lens->setProjectionType(Qt3DRender::QCameraLens::OrthographicProjection);
226 lens->setNearPlane(0.001f);
227 lens->setFarPlane(10000.0f);
228 lens->setRight(7.0f);
229 lens->setLeft(-7.0f);
230 lens->setTop(5.0f);
231 lens->setBottom(-5.0f);
232 lens->setObjectName(QStringLiteral("Ortho camera lens"));
233 camera->addComponent(lens);
234
235 m_entityMap.insert(camera->objectName(), camera);
236 #ifdef VISUAL_CHECK
237 m_view1->defaultFrameGraph()->setCamera(camera);
238 #endif
239 }
240
241 // Point light
242 {
243 Qt3DRender::QPointLight *light = new Qt3DRender::QPointLight;
244 light->setColor(QColor("#FFDDAA"));
245 light->setIntensity(0.9f);
246 light->setConstantAttenuation(0.03f);
247 light->setLinearAttenuation(0.04f);
248 light->setQuadraticAttenuation(0.01f);
249 Qt3DCore::QTransform *transform = new Qt3DCore::QTransform;
250 transform->setTranslation(QVector3D(-6.0f, 2.0f, 3.0f));
251 createAndAddEntity(QStringLiteral("Point Light"), light, transform);
252 }
253 // Directional light
254 {
255 Qt3DRender::QDirectionalLight *light = new Qt3DRender::QDirectionalLight;
256 light->setColor(QColor("#BBCCEE"));
257 light->setIntensity(0.75f);
258 light->setWorldDirection(QVector3D(-1.0f, -1.0f, -1.0f));
259 createAndAddEntity(QStringLiteral("Directional Light"), light);
260 }
261 // Spot light
262 {
263 Qt3DRender::QSpotLight *light = new Qt3DRender::QSpotLight;
264 light->setColor(QColor("#5599DD"));
265 light->setIntensity(2.0f);
266 light->setConstantAttenuation(0.03f);
267 light->setLinearAttenuation(0.04f);
268 light->setQuadraticAttenuation(0.01f);
269 light->setLocalDirection(QVector3D(0.0f, -1.0f, -1.0f));
270 light->setCutOffAngle(30.0f);
271 Qt3DCore::QTransform *transform = new Qt3DCore::QTransform;
272 transform->setTranslation(QVector3D(0.0f, 5.0f, 0.0f));
273 createAndAddEntity(QStringLiteral("Spot Light"), light, transform);
274 }
275 // Cube with DiffuseMap
276 {
277 Qt3DExtras::QDiffuseMapMaterial *material = new Qt3DExtras::QDiffuseMapMaterial();
278 Qt3DRender::QTextureImage *diffuseTextureImage = new Qt3DRender::QTextureImage();
279 material->diffuse()->addTextureImage(diffuseTextureImage);
280 material->setAmbient(QColor("#000088"));
281 material->setSpecular(QColor("#FFFF00"));
282 material->setShininess(30.0);
283 material->setTextureScale(2.0f);
284 diffuseTextureImage->setSource(QUrl(QStringLiteral("qrc:/qtlogo.png")));
285 Qt3DCore::QTransform *transform = new Qt3DCore::QTransform;
286 transform->setScale(0.75f);
287 transform->setTranslation(QVector3D(2.0f, 1.0f, -1.0f));
288 Qt3DExtras::QCuboidMesh *mesh = new Qt3DExtras::QCuboidMesh;
289 mesh->setXExtent(1.2f);
290 mesh->setYExtent(1.1f);
291 mesh->setZExtent(0.9f);
292 mesh->setYZMeshResolution(QSize(2, 2));
293 mesh->setYZMeshResolution(QSize(2, 3));
294 mesh->setYZMeshResolution(QSize(3, 2));
295 createAndAddEntity(QStringLiteral("Cube with DiffuseMap"), mesh, material, transform);
296 }
297 // Cone with PhongAlpha
298 {
299 Qt3DExtras::QPhongAlphaMaterial *material = new Qt3DExtras::QPhongAlphaMaterial();
300 material->setAlpha(0.6f);
301 material->setAmbient(QColor("#550000"));
302 material->setDiffuse(QColor("#00FFFF"));
303 material->setSpecular(QColor("#FFFF00"));
304 material->setShininess(20.0f);
305 material->setSourceRgbArg(Qt3DRender::QBlendEquationArguments::Source1Color);
306 material->setSourceAlphaArg(Qt3DRender::QBlendEquationArguments::Source1Alpha);
307 material->setDestinationRgbArg(Qt3DRender::QBlendEquationArguments::DestinationColor);
308 material->setDestinationAlphaArg(Qt3DRender::QBlendEquationArguments::DestinationAlpha);
309 material->setBlendFunctionArg(Qt3DRender::QBlendEquation::ReverseSubtract);
310 Qt3DCore::QTransform *transform = new Qt3DCore::QTransform;
311 transform->setTranslation(QVector3D(2.0f, 1.0f, 1.0f));
312 transform->setRotation(Qt3DCore::QTransform::fromAxisAndAngle(1.0f, 0.0f, 0.0f, -20.0f));
313 Qt3DExtras::QConeMesh *mesh = new Qt3DExtras::QConeMesh;
314 mesh->setRings(2);
315 mesh->setSlices(16);
316 mesh->setTopRadius(0.5f);
317 mesh->setBottomRadius(1.5f);
318 mesh->setLength(0.9f);
319 createAndAddEntity(QStringLiteral("Cone with PhongAlpha"), mesh, material, transform);
320 }
321 // Cylinder with Phong
322 {
323 Qt3DExtras::QPhongMaterial *material = new Qt3DExtras::QPhongMaterial();
324 material->setAmbient(QColor("#220022"));
325 material->setDiffuse(QColor("#6633AA"));
326 material->setSpecular(QColor("#66AA33"));
327 material->setShininess(50.0f);
328 Qt3DCore::QTransform *transform = new Qt3DCore::QTransform;
329 transform->setTranslation(QVector3D(0.0f, 1.0f, 1.0f));
330 transform->setRotation(Qt3DCore::QTransform::fromAxisAndAngle(0.0f, 0.0f, 1.0f, -45.0f));
331 Qt3DExtras::QCylinderMesh *mesh = new Qt3DExtras::QCylinderMesh;
332 mesh->setRadius(0.5f);
333 mesh->setRings(3);
334 mesh->setLength(1.2f);
335 mesh->setSlices(16);
336 createAndAddEntity(QStringLiteral("Cylinder with Phong"), mesh, material, transform);
337 }
338 // Plane with DiffuseSpecularMap
339 {
340 Qt3DExtras::QDiffuseSpecularMapMaterial *material =
341 new Qt3DExtras::QDiffuseSpecularMapMaterial();
342 Qt3DRender::QTextureImage *diffuseTextureImage = new Qt3DRender::QTextureImage();
343 material->diffuse()->addTextureImage(diffuseTextureImage);
344 diffuseTextureImage->setSource(QUrl(QStringLiteral("qrc:/qtlogo.png")));
345 Qt3DRender::QTextureImage *specularTextureImage = new Qt3DRender::QTextureImage();
346 material->specular()->addTextureImage(specularTextureImage);
347 specularTextureImage->setSource(QUrl(QStringLiteral("qrc:/qtlogo_specular.png")));
348
349 material->setAmbient(QColor("#0000FF"));
350 material->setTextureScale(3.0f);
351 material->setShininess(15.0f);
352
353 Qt3DCore::QTransform *transform = new Qt3DCore::QTransform;
354 transform->setTranslation(QVector3D(-1.0f, 1.0f, 1.0f));
355 transform->setRotation(Qt3DCore::QTransform::fromAxisAndAngle(1.0f, 0.0f, 0.0f, 45.0f));
356
357 Qt3DExtras::QPlaneMesh *mesh = new Qt3DExtras::QPlaneMesh;
358 mesh->setMeshResolution(QSize(3, 3));
359 mesh->setHeight(1.5f);
360 mesh->setWidth(1.2f);
361 createAndAddEntity(QStringLiteral("Plane with DiffuseSpecularMap"),
362 mesh, material, transform);
363 }
364 // Sphere with NormalDiffuseMap
365 {
366 Qt3DExtras::QNormalDiffuseMapMaterial *material =
367 new Qt3DExtras::QNormalDiffuseMapMaterial();
368
369 Qt3DRender::QTextureImage *normalTextureImage = new Qt3DRender::QTextureImage();
370 material->normal()->addTextureImage(normalTextureImage);
371 normalTextureImage->setSource(QUrl(QStringLiteral("qrc:/qtlogo_normal.png")));
372
373 Qt3DRender::QTextureImage *diffuseTextureImage = new Qt3DRender::QTextureImage();
374 material->diffuse()->addTextureImage(diffuseTextureImage);
375 diffuseTextureImage->setSource(QUrl(QStringLiteral("qrc:/qtlogo.png")));
376
377 material->setAmbient(QColor("#000044"));
378 material->setSpecular(QColor("#0000CC"));
379 material->setShininess(9.0f);
380 material->setTextureScale(4.0f);
381
382 Qt3DCore::QTransform *transform = new Qt3DCore::QTransform;
383 transform->setTranslation(QVector3D(0.0f, 1.0f, -10.0f));
384
385 Qt3DExtras::QSphereMesh *mesh = new Qt3DExtras::QSphereMesh;
386 mesh->setRadius(2.0f);
387 mesh->setRings(16);
388 mesh->setSlices(16);
389 mesh->setGenerateTangents(true);
390 createAndAddEntity(QStringLiteral("Sphere with NormalDiffuseMap"),
391 mesh, material, transform);
392 }
393 // Sphere with NormalDiffuseMapAlpha
394 {
395 Qt3DExtras::QNormalDiffuseMapAlphaMaterial *material =
396 new Qt3DExtras::QNormalDiffuseMapAlphaMaterial();
397
398 Qt3DRender::QTextureImage *normalTextureImage = new Qt3DRender::QTextureImage();
399 material->normal()->addTextureImage(normalTextureImage);
400 normalTextureImage->setSource(QUrl(QStringLiteral("qrc:/qtlogo_normal.png")));
401
402 Qt3DRender::QTextureImage *diffuseTextureImage = new Qt3DRender::QTextureImage();
403 material->diffuse()->addTextureImage(diffuseTextureImage);
404 diffuseTextureImage->setSource(QUrl(QStringLiteral("qrc:/qtlogo_with_alpha.png")));
405
406 material->setAmbient(QColor("#000044"));
407 material->setSpecular(QColor("#0000CC"));
408 material->setShininess(9.0f);
409 material->setTextureScale(4.0f);
410
411 Qt3DCore::QTransform *transform = new Qt3DCore::QTransform;
412 transform->setTranslation(QVector3D(4.0f, 1.0f, -10.0f));
413
414 Qt3DExtras::QSphereMesh *mesh = new Qt3DExtras::QSphereMesh;
415 mesh->setRadius(2.0f);
416 mesh->setRings(16);
417 mesh->setSlices(16);
418 mesh->setGenerateTangents(true);
419 createAndAddEntity(QStringLiteral("Sphere with NormalDiffuseMapAlpha"),
420 mesh, material, transform);
421 }
422 // Sphere with NormalDiffuseSpecularMap
423 {
424 Qt3DExtras::QNormalDiffuseSpecularMapMaterial *material =
425 new Qt3DExtras::QNormalDiffuseSpecularMapMaterial();
426
427 Qt3DRender::QTextureImage *normalTextureImage = new Qt3DRender::QTextureImage();
428 material->normal()->addTextureImage(normalTextureImage);
429 normalTextureImage->setSource(QUrl(QStringLiteral("qrc:/qtlogo_normal.png")));
430
431 Qt3DRender::QTextureImage *diffuseTextureImage = new Qt3DRender::QTextureImage();
432 material->diffuse()->addTextureImage(diffuseTextureImage);
433 diffuseTextureImage->setSource(QUrl(QStringLiteral("qrc:/qtlogo.png")));
434
435 Qt3DRender::QTextureImage *specularTextureImage = new Qt3DRender::QTextureImage();
436 material->specular()->addTextureImage(specularTextureImage);
437 specularTextureImage->setSource(QUrl(QStringLiteral("qrc:/qtlogo_specular.png")));
438
439 material->setAmbient(QColor("#000044"));
440 material->setShininess(9.0f);
441 material->setTextureScale(4.0f);
442
443 Qt3DCore::QTransform *transform = new Qt3DCore::QTransform;
444 transform->setTranslation(QVector3D(-4.0f, 1.0f, -10.0f));
445
446 Qt3DExtras::QSphereMesh *mesh = new Qt3DExtras::QSphereMesh;
447 mesh->setRadius(2.0f);
448 mesh->setRings(16);
449 mesh->setSlices(16);
450 mesh->setGenerateTangents(true);
451 createAndAddEntity(QStringLiteral("Sphere with NormalDiffuseSpecularMap"),
452 mesh, material, transform);
453 }
454 // Torus with Gooch
455 {
456 Qt3DExtras::QGoochMaterial *material = new Qt3DExtras::QGoochMaterial();
457
458 material->setDiffuse(QColor("#333333"));
459 material->setSpecular(QColor("#550055"));
460 material->setCool(QColor("#0055AA"));
461 material->setWarm(QColor("#FF3300"));
462 material->setAlpha(0.2f);
463 material->setBeta(0.4f);
464 material->setShininess(22.0f);
465
466 Qt3DCore::QTransform *transform = new Qt3DCore::QTransform;
467 transform->setTranslation(QVector3D(0.0f, 4.0f, -10.0f));
468
469 Qt3DExtras::QTorusMesh *mesh = new Qt3DExtras::QTorusMesh;
470 mesh->setRadius(1.0f);
471 mesh->setMinorRadius(0.5f);
472 mesh->setRings(16);
473 mesh->setSlices(16);
474 createAndAddEntity(QStringLiteral("Torus with Gooch"), mesh, material, transform);
475 }
476 // Custom cube with per-vertex colors
477 {
478 Qt3DExtras::QPerVertexColorMaterial *material = new Qt3DExtras::QPerVertexColorMaterial();
479 Qt3DCore::QTransform *transform = new Qt3DCore::QTransform;
480 transform->setTranslation(QVector3D(4.0f, 3.0f, -15.0f));
481 transform->setRotation(Qt3DCore::QTransform::fromAxisAndAngle(1.0f, 1.0f, 1.0f, 270.0f));
482
483 Qt3DRender::QGeometryRenderer *boxMesh = createCustomCube();
484 Qt3DCore::QBuffer *colorDataBuffer =
485 new Qt3DCore::QBuffer(boxMesh->geometry());
486 QByteArray colorBufferData;
487 colorBufferData.resize(8 * 4 * sizeof(float));
488
489 float *cPtr = reinterpret_cast<float *>(colorBufferData.data());
490 for (int i = 0; i < 8; i++) {
491 cPtr[i * 4] = float(i) / 8.0f;
492 cPtr[i * 4 + 1] = float(8 - i) / 8.0f;
493 cPtr[i * 4 + 2] = float((i + 4) % 8) / 8.0f;
494 cPtr[i * 4 + 3] = 1.0f;
495 }
496
497 colorDataBuffer->setData(colorBufferData);
498
499 addColorAttributeToGeometry(boxMesh->geometry(), colorDataBuffer, 8);
500
501 createAndAddEntity(QStringLiteral("Custom cube with per-vertex colors"),
502 boxMesh, material, transform);
503 }
504 // Child cylinder with Phong
505 {
506 Qt3DCore::QEntity *parentEntity = findChildEntity(m_sceneRoot1,
507 QStringLiteral("Cylinder with Phong"));
508 Qt3DExtras::QPhongMaterial *material = new Qt3DExtras::QPhongMaterial();
509 material->setAmbient(QColor("#333333"));
510 material->setDiffuse(QColor("#88FF00"));
511 material->setSpecular(QColor("#000088"));
512 material->setShininess(150.0f);
513 Qt3DCore::QTransform *transform = new Qt3DCore::QTransform;
514 transform->setTranslation(QVector3D(0.0f, 4.0f, 0.0f));
515 Qt3DExtras::QCylinderMesh *mesh = new Qt3DExtras::QCylinderMesh;
516 mesh->setRadius(0.25f);
517 mesh->setRings(3);
518 mesh->setLength(1.5f);
519 mesh->setSlices(16);
520 createAndAddEntity(QStringLiteral("Child with Phong"),
521 mesh, material, transform, parentEntity);
522 }
523 // Cube with custom material
524 {
525 Qt3DRender::QMaterial *material = new Qt3DRender::QMaterial;
526 material->setEffect(createOnTopEffect());
527 material->addParameter(new Qt3DRender::QParameter(QStringLiteral("globalOffset"),
528 QVector3D(-3.0f, 0.0f, 3.0f)));
529 material->addParameter(new Qt3DRender::QParameter(QStringLiteral("extraYOffset"), 3));
530 material->effect()->addParameter(new Qt3DRender::QParameter(QStringLiteral("handleColor"),
531 QColor(Qt::magenta)));
532 material->effect()->addParameter(new Qt3DRender::QParameter(QStringLiteral("reverseOffset"),
533 QVariant::fromValue(true)));
534
535 Qt3DCore::QTransform *transform = new Qt3DCore::QTransform;
536 transform->setTranslation(QVector3D(0.0f, 2.0f, -40.0f));
537 transform->setRotation(Qt3DCore::QTransform::fromAxisAndAngle(1.0f, 2.0f, 3.0f, 90.0f));
538 Qt3DRender::QGeometryRenderer *boxMesh = createCustomCube();
539 Qt3DCore::QBuffer *offsetBuffer =
540 new Qt3DCore::QBuffer(boxMesh->geometry());
541 QByteArray offsetBufferData;
542 offsetBufferData.resize(8 * 3 * sizeof(float));
543
544 float *oPtr = reinterpret_cast<float *>(offsetBufferData.data());
545 for (int i = 0; i < 8; i++) {
546 oPtr[i * 3] = float(i) / 4.0f;
547 oPtr[i * 3 + 1] = float(8 - i) / 4.0f + 2.0f;
548 oPtr[i * 3 + 2] = float((i + 4) % 8) / 4.0f;
549 }
550
551 offsetBuffer->setData(offsetBufferData);
552
553 Qt3DCore::QAttribute *customAttribute = new Qt3DCore::QAttribute();
554 customAttribute->setAttributeType(Qt3DCore::QAttribute::VertexAttribute);
555 customAttribute->setBuffer(offsetBuffer);
556 customAttribute->setVertexBaseType(Qt3DCore::QAttribute::Float);
557 customAttribute->setVertexSize(3);
558 customAttribute->setByteOffset(0);
559 customAttribute->setByteStride(0);
560 customAttribute->setCount(8);
561 customAttribute->setName(QStringLiteral("vertexOffset"));
562
563 boxMesh->geometry()->addAttribute(customAttribute);
564
565 createAndAddEntity(QStringLiteral("Custom cube with on-top material"),
566 boxMesh, material, transform);
567 }
568
569 #ifdef VISUAL_CHECK
570 m_view1->setGeometry(30, 30, 400, 400);
571 m_view1->setRootEntity(m_sceneRoot1);
572 m_view1->show();
573
574 m_view2->setGeometry(450, 30, 400, 400);
575 m_view2->setRootEntity(m_sceneRoot2);
576 m_view2->show();
577
578 QTest::qWaitForWindowExposed(m_view1);
579 QTest::qWaitForWindowExposed(m_view2);
580 #endif
581 }
582
583 void tst_gltfPlugins::addPositionAttributeToGeometry(Qt3DCore::QGeometry *geometry,
584 Qt3DCore::QBuffer *buffer, int count)
585 {
586 Qt3DCore::QAttribute *posAttribute = new Qt3DCore::QAttribute();
587 posAttribute->setAttributeType(Qt3DCore::QAttribute::VertexAttribute);
588 posAttribute->setBuffer(buffer);
589 posAttribute->setVertexBaseType(Qt3DCore::QAttribute::Float);
590 posAttribute->setVertexSize(3);
591 posAttribute->setByteOffset(0);
592 posAttribute->setByteStride(0);
593 posAttribute->setCount(count);
594 posAttribute->setName(Qt3DCore::QAttribute::defaultPositionAttributeName());
595
596 geometry->addAttribute(posAttribute);
597 }
598
599 void tst_gltfPlugins::addIndexAttributeToGeometry(Qt3DCore::QGeometry *geometry,
600 Qt3DCore::QBuffer *buffer, int count)
601 {
602 Qt3DCore::QAttribute *indexAttribute = new Qt3DCore::QAttribute();
603 indexAttribute->setAttributeType(Qt3DCore::QAttribute::IndexAttribute);
604 indexAttribute->setBuffer(buffer);
605 indexAttribute->setVertexBaseType(Qt3DCore::QAttribute::UnsignedShort);
606 indexAttribute->setVertexSize(1);
607 indexAttribute->setByteOffset(0);
608 indexAttribute->setByteStride(0);
609 indexAttribute->setCount(count);
610
611 geometry->addAttribute(indexAttribute);
612 }
613
614 void tst_gltfPlugins::addColorAttributeToGeometry(Qt3DCore::QGeometry *geometry,
615 Qt3DCore::QBuffer *buffer, int count)
616 {
617 Qt3DCore::QAttribute *colorAttribute = new Qt3DCore::QAttribute();
618 colorAttribute->setAttributeType(Qt3DCore::QAttribute::VertexAttribute);
619 colorAttribute->setBuffer(buffer);
620 colorAttribute->setVertexBaseType(Qt3DCore::QAttribute::Float);
621 colorAttribute->setVertexSize(4);
622 colorAttribute->setByteOffset(0);
623 colorAttribute->setByteStride(0);
624 colorAttribute->setCount(count);
625 colorAttribute->setName(Qt3DCore::QAttribute::defaultColorAttributeName());
626
627 geometry->addAttribute(colorAttribute);
628 }
629
630 Qt3DCore::QEntity *tst_gltfPlugins::findChildEntity(Qt3DCore::QEntity *entity, const QString &name)
631 {
632 for (auto child : entity->children()) {
633 if (auto childEntity = qobject_cast<Qt3DCore::QEntity *>(child)) {
634 if (childEntity->objectName() == name)
635 return childEntity;
636 if (auto foundEntity = findChildEntity(childEntity, name))
637 return foundEntity;
638 }
639 }
640 return nullptr;
641 }
642
643 Qt3DCore::QTransform *tst_gltfPlugins::transformComponent(Qt3DCore::QEntity *entity)
644 {
645 for (auto component : entity->components()) {
646 if (auto castedComponent = qobject_cast<Qt3DCore::QTransform *>(component))
647 return castedComponent;
648 }
649 return nullptr;
650 }
651
652 Qt3DRender::QAbstractLight *tst_gltfPlugins::lightComponent(Qt3DCore::QEntity *entity)
653 {
654 for (auto component : entity->components()) {
655 if (auto castedComponent = qobject_cast<Qt3DRender::QAbstractLight *>(component))
656 return castedComponent;
657 }
658 return nullptr;
659 }
660
661 Qt3DRender::QCameraLens *tst_gltfPlugins::cameraComponent(Qt3DCore::QEntity *entity)
662 {
663 for (auto component : entity->components()) {
664 if (auto castedComponent = qobject_cast<Qt3DRender::QCameraLens *>(component))
665 return castedComponent;
666 }
667 return nullptr;
668 }
669
670 Qt3DRender::QGeometryRenderer *tst_gltfPlugins::meshComponent(Qt3DCore::QEntity *entity)
671 {
672 for (auto component : entity->components()) {
673 if (auto castedComponent = qobject_cast<Qt3DRender::QGeometryRenderer *>(component))
674 return castedComponent;
675 }
676 return nullptr;
677 }
678
679 Qt3DRender::QMaterial *tst_gltfPlugins::materialComponent(Qt3DCore::QEntity *entity)
680 {
681 for (auto component : entity->components()) {
682 if (auto castedComponent = qobject_cast<Qt3DRender::QMaterial *>(component))
683 return castedComponent;
684 }
685 return nullptr;
686 }
687
688 void tst_gltfPlugins::compareComponents(Qt3DCore::QComponent *c1, Qt3DCore::QComponent *c2)
689 {
690 // Make sure component classes are the same and the non-pointer properties are the same
691 QCOMPARE((c1 == nullptr), (c2 == nullptr));
692 if (c1) {
693 // Transform names are lost in export, as the transform is just part of the node item
694 if (!qobject_cast<Qt3DCore::QTransform *>(c1))
695 QCOMPARE(c1->objectName(), c2->objectName());
696 QCOMPARE(c1->metaObject()->className(), c2->metaObject()->className());
697 // Meshes are all imported as generic meshes
698 if (auto mesh1 = qobject_cast<Qt3DRender::QGeometryRenderer *>(c1)) {
699 auto mesh2 = qobject_cast<Qt3DRender::QGeometryRenderer *>(c2);
700 QVERIFY(mesh2 != nullptr);
701 auto geometry1 = mesh1->geometry();
702 if (!geometry1 && mesh1->view())
703 geometry1 = mesh1->view()->geometry();
704 auto geometry2 = mesh2->geometry();
705 if (!geometry2 && mesh2->view())
706 geometry2 = mesh2->view()->geometry();
707 // Check that attributes match.
708 compareAttributes(
709 findAttribute(Qt3DCore::QAttribute::defaultPositionAttributeName(),
710 Qt3DCore::QAttribute::VertexAttribute,
711 geometry1),
712 findAttribute(Qt3DCore::QAttribute::defaultPositionAttributeName(),
713 Qt3DCore::QAttribute::VertexAttribute,
714 geometry2));
715 compareAttributes(
716 findAttribute(Qt3DCore::QAttribute::defaultNormalAttributeName(),
717 Qt3DCore::QAttribute::VertexAttribute,
718 geometry1),
719 findAttribute(Qt3DCore::QAttribute::defaultNormalAttributeName(),
720 Qt3DCore::QAttribute::VertexAttribute,
721 geometry2));
722 compareAttributes(
723 findAttribute(Qt3DCore::QAttribute::defaultTangentAttributeName(),
724 Qt3DCore::QAttribute::VertexAttribute,
725 geometry1),
726 findAttribute(Qt3DCore::QAttribute::defaultTangentAttributeName(),
727 Qt3DCore::QAttribute::VertexAttribute,
728 geometry2));
729 compareAttributes(
730 findAttribute(Qt3DCore::QAttribute::defaultTextureCoordinateAttributeName(),
731 Qt3DCore::QAttribute::VertexAttribute,
732 geometry1),
733 findAttribute(Qt3DCore::QAttribute::defaultTextureCoordinateAttributeName(),
734 Qt3DCore::QAttribute::VertexAttribute,
735 geometry2));
736 compareAttributes(
737 findAttribute(Qt3DCore::QAttribute::defaultColorAttributeName(),
738 Qt3DCore::QAttribute::VertexAttribute,
739 geometry1),
740 findAttribute(Qt3DCore::QAttribute::defaultColorAttributeName(),
741 Qt3DCore::QAttribute::VertexAttribute,
742 geometry2));
743 compareAttributes(
744 findAttribute(QStringLiteral(""),
745 Qt3DCore::QAttribute::IndexAttribute,
746 geometry1),
747 findAttribute(QStringLiteral(""),
748 Qt3DCore::QAttribute::IndexAttribute,
749 geometry2));
750 } else {
751 int count = c1->metaObject()->propertyCount();
752 for (int i = 0; i < count; i++) {
753 auto property = c1->metaObject()->property(i);
754 auto v1 = c1->property(property.name());
755 auto v2 = c2->property(property.name());
756 const auto v1TypeId = v1.metaType().id();
757 if (v1TypeId == QMetaType::Bool) {
758 QCOMPARE(v1.toBool(), v2.toBool());
759 } else if (v1TypeId == QMetaType::QColor) {
760 QCOMPARE(v1.value<QColor>(), v2.value<QColor>());
761 } else if (v1TypeId == QMetaType::QVector3D) {
762 QCOMPARE(v1.value<QVector3D>(), v2.value<QVector3D>());
763 } else if (v1TypeId == QMetaType::QMatrix4x4) {
764 QCOMPARE(v1.value<QMatrix4x4>(), v2.value<QMatrix4x4>());
765 } else if (QMetaType::canConvert(v1.metaType(), QMetaType(QMetaType::Float))) {
766 QVERIFY(qFuzzyCompare(v1.toFloat(), v2.toFloat()));
767 }
768 }
769 if (QString::fromLatin1(c1->metaObject()->className())
770 .endsWith(QStringLiteral("Qt3DRender::QMaterial"))) {
771 auto m1 = qobject_cast<Qt3DRender::QMaterial *>(c1);
772 auto m2 = qobject_cast<Qt3DRender::QMaterial *>(c2);
773 QVERIFY(m1);
774 QVERIFY(m2);
775 auto e1 = m1->effect();
776 auto e2 = m2->effect();
777 QVERIFY(e1);
778 QVERIFY(e2);
779 QCOMPARE(e1->objectName(), e2->objectName());
780 QCOMPARE(e1->techniques().size(), e2->techniques().size());
781
782 compareParameters(m1->parameters(), m2->parameters());
783 compareParameters(e1->parameters(), e2->parameters());
784
785 for (auto t1 : e1->techniques()) {
786 bool techMatch = false;
787 for (auto t2 : e2->techniques()) {
788 if (t1->objectName() == t2->objectName()) {
789 techMatch = true;
790 compareParameters(t1->parameters(), t2->parameters());
791 compareFilterKeys(t1->filterKeys(), t2->filterKeys());
792 compareRenderPasses(t1->renderPasses(), t2->renderPasses());
793 QCOMPARE(t1->graphicsApiFilter()->api(),
794 t2->graphicsApiFilter()->api());
795 QCOMPARE(t1->graphicsApiFilter()->profile(),
796 t2->graphicsApiFilter()->profile());
797 QCOMPARE(t1->graphicsApiFilter()->minorVersion(),
798 t2->graphicsApiFilter()->minorVersion());
799 QCOMPARE(t1->graphicsApiFilter()->majorVersion(),
800 t2->graphicsApiFilter()->majorVersion());
801 QCOMPARE(t1->graphicsApiFilter()->extensions(),
802 t2->graphicsApiFilter()->extensions());
803 QCOMPARE(t1->graphicsApiFilter()->vendor(),
804 t2->graphicsApiFilter()->vendor());
805 }
806 }
807 QVERIFY(techMatch);
808 }
809 }
810 }
811 }
812 }
813
814 Qt3DCore::QAttribute *tst_gltfPlugins::findAttribute(const QString &name,
815 Qt3DCore::QAttribute::AttributeType type,
816 Qt3DCore::QGeometry *geometry)
817 {
818 for (auto att : geometry->attributes()) {
819 if ((type == Qt3DCore::QAttribute::IndexAttribute && type == att->attributeType())
820 || name == att->name()) {
821 return att;
822 }
823 }
824 return nullptr;
825 }
826
827 void tst_gltfPlugins::compareAttributes(Qt3DCore::QAttribute *a1, Qt3DCore::QAttribute *a2)
828 {
829 QCOMPARE(a1 == nullptr, a2 == nullptr);
830 if (a1) {
831 QCOMPARE(a1->attributeType(), a2->attributeType());
832 QCOMPARE(a1->vertexBaseType(), a2->vertexBaseType());
833 QCOMPARE(a1->vertexSize(), a2->vertexSize());
834 QCOMPARE(a1->count(), a2->count());
835 }
836 }
837
838 void tst_gltfPlugins::compareParameters(const QList<Qt3DRender::QParameter *> &params1,
839 const QList<Qt3DRender::QParameter *> &params2)
840 {
841 QCOMPARE(params1.size(), params2.size());
842 for (auto p1 : params1) {
843 bool pMatch = false;
844 for (auto p2 : params2) {
845 if (p1->name() == p2->name()) {
846 pMatch = true;
847 if (p1->value().metaType().id() == QMetaType::QColor) {
848 // Colors are imported as QVector4Ds
849 QColor color = p1->value().value<QColor>();
850 QVector4D vec = p2->value().value<QVector4D>();
851 QCOMPARE(color.redF(), vec.x());
852 QCOMPARE(color.greenF(), vec.y());
853 QCOMPARE(color.blueF(), vec.z());
854 QCOMPARE(color.alphaF(), vec.w());
855 } else if (p1->value().canConvert<Qt3DRender::QAbstractTexture *>()) {
856 QUrl u1 = getTextureUrl(p1->value().value<Qt3DRender::QAbstractTexture *>());
857 QUrl u2 = getTextureUrl(p2->value().value<Qt3DRender::QAbstractTexture *>());
858 QCOMPARE(u1.fileName(), u2.fileName());
859 } else {
860 QCOMPARE(p1->value(), p2->value());
861 }
862 }
863 }
864 QVERIFY(pMatch);
865 }
866 }
867
868 void tst_gltfPlugins::compareRenderPasses(const QList<Qt3DRender::QRenderPass *> &passes1,
869 const QList<Qt3DRender::QRenderPass *> &passes2)
870 {
871 QCOMPARE(passes1.size(), passes2.size());
872 for (auto pass1 : passes1) {
873 bool passMatch = false;
874 for (auto pass2 : passes2) {
875 if (pass1->objectName() == pass2->objectName()) {
876 passMatch = true;
877 compareFilterKeys(pass1->filterKeys(), pass2->filterKeys());
878 compareParameters(pass1->parameters(), pass2->parameters());
879
880 const QList<Qt3DRender::QRenderState *> states1 = pass1->renderStates();
881 const QList<Qt3DRender::QRenderState *> states2 = pass2->renderStates();
882 QCOMPARE(states1.size(), states2.size());
883 for (auto state1 : states1) {
884 bool stateMatch = false;
885 for (auto state2 : states2) {
886 if (state1->metaObject()->className()
887 == state2->metaObject()->className()) {
888 stateMatch = true;
889 }
890 }
891 QVERIFY(stateMatch);
892 }
893
894 QCOMPARE(pass1->shaderProgram()->vertexShaderCode(),
895 pass2->shaderProgram()->vertexShaderCode());
896 QCOMPARE(pass1->shaderProgram()->fragmentShaderCode(),
897 pass2->shaderProgram()->fragmentShaderCode());
898 }
899 }
900 QVERIFY(passMatch);
901 }
902 }
903
904 void tst_gltfPlugins::compareFilterKeys(const QList<Qt3DRender::QFilterKey *> &keys1,
905 const QList<Qt3DRender::QFilterKey *> &keys2)
906 {
907 QCOMPARE(keys1.size(), keys2.size());
908 for (auto k1 : keys1) {
909 bool kMatch = false;
910 for (auto k2 : keys2) {
911 if (k1->name() == k2->name()) {
912 kMatch = true;
913 QCOMPARE(k1->value(), k2->value());
914 }
915 }
916 QVERIFY(kMatch);
917 }
918 }
919
920 QUrl tst_gltfPlugins::getTextureUrl(Qt3DRender::QAbstractTexture *tex)
921 {
922 QUrl url;
923 if (tex->textureImages().size()) {
924 Qt3DRender::QTextureImage *img =
925 qobject_cast<Qt3DRender::QTextureImage *>(
926 tex->textureImages().at(0));
927 if (img)
928 url = img->source();
929 }
930 return url;
931 }
932
933 Qt3DRender::QGeometryRenderer *tst_gltfPlugins::createCustomCube()
934 {
935 Qt3DRender::QGeometryRenderer *boxMesh = new Qt3DRender::QGeometryRenderer;
936 Qt3DCore::QGeometry *boxGeometry = new Qt3DCore::QGeometry(boxMesh);
937 Qt3DCore::QBuffer *boxDataBuffer =
938 new Qt3DCore::QBuffer(boxGeometry);
939 Qt3DCore::QBuffer *indexDataBuffer =
940 new Qt3DCore::QBuffer(boxGeometry);
941 QByteArray vertexBufferData;
942 QByteArray indexBufferData;
943
944 vertexBufferData.resize(8 * 3 * sizeof(float));
945 indexBufferData.resize(12 * 3 * sizeof(ushort));
946
947 float dimension = 1.0f;
948
949 float *vPtr = reinterpret_cast<float *>(vertexBufferData.data());
950 vPtr[0] = -dimension; vPtr[1] = -dimension; vPtr[2] = -dimension;
951 vPtr[3] = dimension; vPtr[4] = -dimension; vPtr[5] = -dimension;
952 vPtr[6] = dimension; vPtr[7] = -dimension; vPtr[8] = dimension;
953 vPtr[9] = -dimension; vPtr[10] = -dimension; vPtr[11] = dimension;
954 vPtr[12] = -dimension; vPtr[13] = dimension; vPtr[14] = -dimension;
955 vPtr[15] = dimension; vPtr[16] = dimension; vPtr[17] = -dimension;
956 vPtr[18] = dimension; vPtr[19] = dimension; vPtr[20] = dimension;
957 vPtr[21] = -dimension; vPtr[22] = dimension; vPtr[23] = dimension;
958
959 ushort *iPtr = reinterpret_cast<ushort *>(indexBufferData.data());
960 iPtr[0] = 2; iPtr[1] = 0; iPtr[2] = 1;
961 iPtr[3] = 2; iPtr[4] = 3; iPtr[5] = 0;
962 iPtr[6] = 1; iPtr[7] = 6; iPtr[8] = 2;
963 iPtr[9] = 1; iPtr[10] = 5; iPtr[11] = 6;
964 iPtr[12] = 2; iPtr[13] = 7; iPtr[14] = 3;
965 iPtr[15] = 2; iPtr[16] = 6; iPtr[17] = 7;
966 iPtr[18] = 6; iPtr[19] = 5; iPtr[20] = 4;
967 iPtr[21] = 6; iPtr[22] = 4; iPtr[23] = 7;
968 iPtr[24] = 7; iPtr[25] = 0; iPtr[26] = 3;
969 iPtr[27] = 7; iPtr[28] = 4; iPtr[29] = 0;
970 iPtr[30] = 4; iPtr[31] = 1; iPtr[32] = 0;
971 iPtr[33] = 4; iPtr[34] = 5; iPtr[35] = 1;
972
973 boxDataBuffer->setData(vertexBufferData);
974 indexDataBuffer->setData(indexBufferData);
975
976 addPositionAttributeToGeometry(boxGeometry, boxDataBuffer, 8);
977 addIndexAttributeToGeometry(boxGeometry, indexDataBuffer, 36);
978
979 boxMesh->setInstanceCount(1);
980 boxMesh->setIndexOffset(0);
981 boxMesh->setFirstInstance(0);
982 boxMesh->setVertexCount(36);
983 boxMesh->setPrimitiveType(Qt3DRender::QGeometryRenderer::Triangles);
984 boxMesh->setGeometry(boxGeometry);
985
986 return boxMesh;
987 }
988
989 Qt3DRender::QEffect *tst_gltfPlugins::createOnTopEffect()
990 {
991 Qt3DRender::QEffect *effect = new Qt3DRender::QEffect;
992
993 Qt3DRender::QTechnique *technique = new Qt3DRender::QTechnique();
994 technique->graphicsApiFilter()->setProfile(Qt3DRender::QGraphicsApiFilter::NoProfile);
995 technique->graphicsApiFilter()->setApi(Qt3DRender::QGraphicsApiFilter::OpenGL);
996 technique->graphicsApiFilter()->setMajorVersion(2);
997 technique->graphicsApiFilter()->setMinorVersion(1);
998
999 Qt3DRender::QTechnique *techniqueCore = new Qt3DRender::QTechnique();
1000 techniqueCore->graphicsApiFilter()->setProfile(Qt3DRender::QGraphicsApiFilter::CoreProfile);
1001 techniqueCore->graphicsApiFilter()->setApi(Qt3DRender::QGraphicsApiFilter::OpenGL);
1002 techniqueCore->graphicsApiFilter()->setMajorVersion(3);
1003 techniqueCore->graphicsApiFilter()->setMinorVersion(1);
1004
1005 Qt3DRender::QTechnique *techniqueES2 = new Qt3DRender::QTechnique();
1006 techniqueES2->graphicsApiFilter()->setApi(Qt3DRender::QGraphicsApiFilter::OpenGLES);
1007 techniqueES2->graphicsApiFilter()->setMajorVersion(2);
1008 techniqueES2->graphicsApiFilter()->setMinorVersion(0);
1009 techniqueES2->graphicsApiFilter()->setProfile(Qt3DRender::QGraphicsApiFilter::NoProfile);
1010
1011 Qt3DRender::QFilterKey *filterkey1 = new Qt3DRender::QFilterKey(effect);
1012 Qt3DRender::QFilterKey *filterkey2 = new Qt3DRender::QFilterKey();
1013 filterkey1->setName(QStringLiteral("renderingStyle"));
1014 filterkey1->setValue(QStringLiteral("forward"));
1015 filterkey2->setName(QStringLiteral("dummyKey"));
1016 filterkey2->setValue(QStringLiteral("dummyValue"));
1017
1018 Qt3DRender::QParameter *parameter1 = new Qt3DRender::QParameter(QStringLiteral("handleColor"),
1019 QColor(Qt::yellow));
1020 Qt3DRender::QParameter *parameter2 = new Qt3DRender::QParameter(QStringLiteral("customAlpha"),
1021 1.0f);
1022 Qt3DRender::QParameter *parameter3 = new Qt3DRender::QParameter(QStringLiteral("handleColor"),
1023 QColor(Qt::blue));
1024 Qt3DRender::QTexture2D *texture = new Qt3DRender::QTexture2D;
1025 Qt3DRender::QParameter *parameter4 =
1026 new Qt3DRender::QParameter(QStringLiteral("customTexture"), texture);
1027 Qt3DRender::QTextureImage *ti = new Qt3DRender::QTextureImage();
1028 parameter4->value().value<Qt3DRender::QAbstractTexture *>()->addTextureImage(ti);
1029 ti->setSource(QUrl(QStringLiteral("qrc:/qtlogo.png")));
1030
1031 technique->addFilterKey(filterkey1);
1032 technique->addFilterKey(filterkey2);
1033 techniqueES2->addFilterKey(filterkey1);
1034 techniqueES2->addFilterKey(filterkey2);
1035 techniqueCore->addFilterKey(filterkey1);
1036
1037 technique->addParameter(parameter1);
1038 technique->addParameter(parameter2);
1039 technique->addParameter(parameter4);
1040 techniqueES2->addParameter(parameter1);
1041 techniqueES2->addParameter(parameter2);
1042
1043 Qt3DRender::QShaderProgram *shader = new Qt3DRender::QShaderProgram();
1044 Qt3DRender::QShaderProgram *shaderES2 = new Qt3DRender::QShaderProgram();
1045 shader->setVertexShaderCode(Qt3DRender::QShaderProgram::loadSource(
1046 QUrl(QStringLiteral("qrc:/ontopmaterial.vert"))));
1047 shader->setFragmentShaderCode(Qt3DRender::QShaderProgram::loadSource(
1048 QUrl(QStringLiteral("qrc:/ontopmaterial.frag"))));
1049 shaderES2->setVertexShaderCode(Qt3DRender::QShaderProgram::loadSource(
1050 QUrl(QStringLiteral("qrc:/ontopmaterialES2.vert"))));
1051 shaderES2->setFragmentShaderCode(Qt3DRender::QShaderProgram::loadSource(
1052 QUrl(QStringLiteral("qrc:/ontopmaterialES2.frag"))));
1053 shader->setObjectName(QStringLiteral("Basic shader"));
1054 shaderES2->setObjectName(QStringLiteral("ES2 shader"));
1055
1056 Qt3DRender::QRenderPass *renderPass = new Qt3DRender::QRenderPass();
1057 Qt3DRender::QRenderPass *renderPassES2 = new Qt3DRender::QRenderPass();
1058 renderPass->setShaderProgram(shader);
1059 renderPassES2->setShaderProgram(shaderES2);
1060 renderPass->addFilterKey(filterkey2);
1061 renderPass->addParameter(parameter3);
1062 Qt3DRender::QColorMask *cmask = new Qt3DRender::QColorMask;
1063 cmask->setRedMasked(false);
1064 renderPass->addRenderState(cmask);
1065 Qt3DRender::QBlendEquation *be = new Qt3DRender::QBlendEquation;
1066 be->setBlendFunction(Qt3DRender::QBlendEquation::Subtract);
1067 renderPass->addRenderState(be);
1068 technique->addRenderPass(renderPassES2);
1069 techniqueES2->addRenderPass(renderPassES2);
1070 techniqueCore->addRenderPass(renderPass);
1071 technique->setObjectName(QStringLiteral("Basic technique"));
1072 techniqueES2->setObjectName(QStringLiteral("ES2 technique"));
1073 techniqueCore->setObjectName(QStringLiteral("Core technique"));
1074 renderPass->setObjectName(QStringLiteral("Basic pass"));
1075 renderPassES2->setObjectName(QStringLiteral("ES2 pass"));
1076
1077 effect->addTechnique(technique);
1078 effect->addTechnique(techniqueES2);
1079 effect->addTechnique(techniqueCore);
1080 effect->setObjectName(QStringLiteral("OnTopEffect"));
1081
1082 return effect;
1083 }
1084
1085 Qt3DCore::QEntity *tst_gltfPlugins::findCameraChild(Qt3DCore::QEntity *entity,
1086 Qt3DRender::QCameraLens::ProjectionType type)
1087 {
1088 for (auto child : entity->children()) {
1089 if (auto childEntity = qobject_cast<Qt3DCore::QEntity *>(child)) {
1090 for (auto component : childEntity->components()) {
1091 if (auto cameraLens = qobject_cast<Qt3DRender::QCameraLens *>(component)) {
1092 if (cameraLens->projectionType() == type)
1093 return childEntity;
1094 }
1095 }
1096 if (auto cameraEntity = findCameraChild(childEntity, type))
1097 return cameraEntity;
1098 }
1099 }
1100 return nullptr;
1101 }
1102
1103 void tst_gltfPlugins::exportAndImport_data()
1104 {
1105 QTest::addColumn<bool>("compactJson");
1106
1107 QTest::newRow("No options") << false;
1108 #ifndef VISUAL_CHECK
1109 QTest::newRow("Compact json") << true;
1110 #endif
1111 }
1112
1113 void tst_gltfPlugins::exportAndImport()
1114 {
1115 QSKIP("Crashes in JSON parser - see QTBUG-88588");
1116 QFETCH(bool, compactJson);
1117
1118 createTestScene();
1119
1120 #ifdef PRESERVE_EXPORT
1121 m_exportDir->setAutoRemove(false);
1122 qDebug() << "Export Directory:" << m_exportDir->path();
1123 #endif
1124
1125 const QString sceneName = QStringLiteral("MyGLTFScene");
1126 const QString exportDir = m_exportDir->path();
1127
1128 // Export the created scene using GLTF export plugin
1129 QStringList keys = Qt3DRender::QSceneExportFactory::keys();
1130 for (const QString &key : keys) {
1131 Qt3DRender::QSceneExporter *exporter =
1132 Qt3DRender::QSceneExportFactory::create(key, QStringList());
1133 if (exporter != nullptr && key == QStringLiteral("gltfexport")) {
1134 QVariantHash options;
1135 options.insert(QStringLiteral("compactJson"), QVariant(compactJson));
1136 exporter->exportScene(m_sceneRoot1, exportDir, sceneName, options);
1137 break;
1138 }
1139 }
1140
1141 QCoreApplication::processEvents();
1142
1143 // Import the exported scene using GLTF import plugin
1144 Qt3DCore::QEntity *importedScene = nullptr;
1145 keys = Qt3DRender::QSceneImportFactory::keys();
1146 for (auto key : keys) {
1147 Qt3DRender::QSceneImporter *importer =
1148 Qt3DRender::QSceneImportFactory::create(key, QStringList());
1149 if (importer != nullptr && key == QStringLiteral("gltf")) {
1150 QString sceneSource = exportDir;
1151 if (!sceneSource.endsWith(QLatin1Char('/')))
1152 sceneSource.append(QLatin1Char('/'));
1153 sceneSource += sceneName;
1154 sceneSource += QLatin1Char('/');
1155 sceneSource += sceneName;
1156 sceneSource += QStringLiteral(".qgltf");
1157 importer->setSource(QUrl::fromLocalFile(sceneSource));
1158 importedScene = importer->scene();
1159 break;
1160 }
1161 }
1162
1163 importedScene->setParent(m_sceneRoot2);
1164
1165 // Compare contents of the original scene and the exported one.
1166 for (auto it = m_entityMap.begin(), end = m_entityMap.end(); it != end; ++it) {
1167 QString name = it.key();
1168 Qt3DCore::QEntity *exportedEntity = it.value();
1169 Qt3DCore::QEntity *importedEntity = findChildEntity(importedScene, name);
1170 QVERIFY(importedEntity != nullptr);
1171 if (importedEntity) {
1172 qDebug() << importedEntity->objectName();
1173 compareComponents(transformComponent(exportedEntity),
1174 transformComponent(importedEntity));
1175 compareComponents(lightComponent(exportedEntity),
1176 lightComponent(importedEntity));
1177 compareComponents(cameraComponent(exportedEntity),
1178 cameraComponent(importedEntity));
1179 compareComponents(meshComponent(exportedEntity),
1180 meshComponent(importedEntity));
1181 compareComponents(materialComponent(exportedEntity),
1182 materialComponent(importedEntity));
1183 Qt3DRender::QCamera *exportedCamera =
1184 qobject_cast<Qt3DRender::QCamera *>(exportedEntity);
1185 if (exportedCamera) {
1186 Qt3DRender::QCamera *importedCamera =
1187 qobject_cast<Qt3DRender::QCamera *>(importedEntity);
1188 QVERIFY(importedCamera != nullptr);
1189 QCOMPARE(exportedCamera->position(), importedCamera->position());
1190 QCOMPARE(exportedCamera->upVector(), importedCamera->upVector());
1191 QCOMPARE(exportedCamera->viewCenter(), importedCamera->viewCenter());
1192 }
1193 }
1194 }
1195
1196
1197 #ifdef VISUAL_CHECK
1198 qDebug() << "Dumping original entity tree:";
1199 walkEntity(m_sceneRoot1, 0);
1200 qDebug() << "Dumping imported entity tree:";
1201 walkEntity(importedScene, 0);
1202
1203 // Find the camera to actually show the scene
1204 m_view2->defaultFrameGraph()->setCamera(
1205 findCameraChild(m_sceneRoot2, Qt3DRender::QCameraLens::OrthographicProjection));
1206 QTest::qWait(VISUAL_CHECK);
1207
1208 m_view1->defaultFrameGraph()->setCamera(
1209 findCameraChild(m_sceneRoot1, Qt3DRender::QCameraLens::PerspectiveProjection));
1210 m_view2->defaultFrameGraph()->setCamera(
1211 findCameraChild(m_sceneRoot2, Qt3DRender::QCameraLens::PerspectiveProjection));
1212 QTest::qWait(VISUAL_CHECK);
1213 #endif
1214 }
1215
1216 QTEST_MAIN(tst_gltfPlugins)
1217
1218 #include "tst_gltfplugins.moc"