diff options
author | Miikka Heikkinen <miikka.heikkinen@qt.io> | 2017-01-11 14:10:24 +0200 |
---|---|---|
committer | Miikka Heikkinen <miikka.heikkinen@qt.io> | 2017-01-18 13:10:28 +0000 |
commit | cef6d5406267787d22deee75600ee9d3653a3109 (patch) | |
tree | 8e03434b1556ab642133b0a6ba85cce0e012f97f | |
parent | d7fb7425775d4e9191626fcf62f4198c84843090 (diff) |
GLTF Export QCamera properly
QCamera position, upVector, and viewCenter properties were not properly
preserved when doing GLTF export/import.
Change-Id: I6d81633cf29d6e347935c56605b46923d3e1aa03
Reviewed-by: Tomi Korpipää <tomi.korpipaa@qt.io>
Reviewed-by: Antti Määttä <antti.maatta@qt.io>
-rw-r--r-- | src/plugins/sceneparsers/gltf/gltfimporter.cpp | 26 | ||||
-rw-r--r-- | src/plugins/sceneparsers/gltf/gltfimporter.h | 2 | ||||
-rw-r--r-- | src/plugins/sceneparsers/gltfexport/gltfexporter.cpp | 13 | ||||
-rw-r--r-- | src/plugins/sceneparsers/gltfexport/gltfexporter.h | 3 | ||||
-rw-r--r-- | tests/auto/render/gltfplugins/tst_gltfplugins.cpp | 12 |
5 files changed, 47 insertions, 9 deletions
diff --git a/src/plugins/sceneparsers/gltf/gltfimporter.cpp b/src/plugins/sceneparsers/gltf/gltfimporter.cpp index e5434da3f..c026594bf 100644 --- a/src/plugins/sceneparsers/gltf/gltfimporter.cpp +++ b/src/plugins/sceneparsers/gltf/gltfimporter.cpp @@ -213,6 +213,9 @@ #define KEY_EFFECT QLatin1String("effect") #define KEY_EFFECTS QLatin1String("effects") #define KEY_PROPERTIES QLatin1String("properties") +#define KEY_POSITION QLatin1String("position") +#define KEY_UPVECTOR QLatin1String("upVector") +#define KEY_VIEW_CENTER QLatin1String("viewCenter") QT_BEGIN_NAMESPACE @@ -380,7 +383,8 @@ Qt3DCore::QEntity* GLTFImporter::node(const QString &id) const auto translation = jsonObj.value(KEY_TRANSLATION); const auto scale = jsonObj.value(KEY_SCALE); Qt3DCore::QTransform *trans = nullptr; - QCameraLens* cameraLens = nullptr; + QCameraLens *cameraLens = nullptr; + QCamera *cameraEntity = nullptr; // If the node contains no meshes, results will still be null here. // If the node has camera and transform, promote it to QCamera, as that makes it more @@ -389,10 +393,10 @@ Qt3DCore::QEntity* GLTFImporter::node(const QString &id) if (!cameraVal.isUndefined() && (!matrix.isUndefined() || !rotation.isUndefined() || !translation.isUndefined() || !scale.isUndefined())) { - auto camera = new QCamera; - trans = camera->transform(); - cameraLens = camera->lens(); - result = camera; + cameraEntity = new QCamera; + trans = cameraEntity->transform(); + cameraLens = cameraEntity->lens(); + result = cameraEntity; } else { result = new QEntity; } @@ -460,7 +464,7 @@ Qt3DCore::QEntity* GLTFImporter::node(const QString &id) const bool newLens = cameraLens == nullptr; if (newLens) cameraLens = new QCameraLens; - if (!fillCameraLens(*cameraLens, cameraVal.toString())) { + if (!fillCamera(*cameraLens, cameraEntity, cameraVal.toString())) { qCWarning(GLTFImporterLog, "failed to build camera: %ls on node %ls", qUtf16PrintableImpl(cameraVal.toString()), qUtf16PrintableImpl(id)); } else if (newLens) { @@ -925,7 +929,7 @@ QMaterial* GLTFImporter::material(const QString &id) return mat; } -bool GLTFImporter::fillCameraLens(QCameraLens &lens, const QString &id) const +bool GLTFImporter::fillCamera(QCameraLens &lens, QCamera *cameraEntity, const QString &id) const { const auto jsonVal = m_json.object().value(KEY_CAMERAS).toObject().value(id); if (Q_UNLIKELY(jsonVal.isUndefined())) { @@ -973,6 +977,14 @@ bool GLTFImporter::fillCameraLens(QCameraLens &lens, const QString &id) const qUtf16PrintableImpl(id), qUtf16PrintableImpl(camTy)); return false; } + if (cameraEntity) { + if (jsonObj.contains(KEY_POSITION)) + cameraEntity->setPosition(jsonArrToVec3(jsonObj.value(KEY_POSITION).toArray())); + if (jsonObj.contains(KEY_UPVECTOR)) + cameraEntity->setUpVector(jsonArrToVec3(jsonObj.value(KEY_UPVECTOR).toArray())); + if (jsonObj.contains(KEY_VIEW_CENTER)) + cameraEntity->setViewCenter(jsonArrToVec3(jsonObj.value(KEY_VIEW_CENTER).toArray())); + } renameFromJson(jsonObj, &lens); return true; } diff --git a/src/plugins/sceneparsers/gltf/gltfimporter.h b/src/plugins/sceneparsers/gltf/gltfimporter.h index 8f6b9b356..e97148600 100644 --- a/src/plugins/sceneparsers/gltf/gltfimporter.h +++ b/src/plugins/sceneparsers/gltf/gltfimporter.h @@ -147,7 +147,7 @@ private: Qt3DCore::QEntity *defaultScene(); QMaterial *material(const QString &id); - bool fillCameraLens(QCameraLens &lens, const QString &id) const; + bool fillCamera(QCameraLens &lens, QCamera *cameraEntity, const QString &id) const; void parse(); void cleanup(); diff --git a/src/plugins/sceneparsers/gltfexport/gltfexporter.cpp b/src/plugins/sceneparsers/gltfexport/gltfexporter.cpp index befe7770d..7332fa6b5 100644 --- a/src/plugins/sceneparsers/gltfexport/gltfexporter.cpp +++ b/src/plugins/sceneparsers/gltfexport/gltfexporter.cpp @@ -961,6 +961,7 @@ void GLTFExporter::parseCameras() { qCDebug(GLTFExporterLog, "Parsing cameras..."); int cameraCount = 0; + for (auto it = m_cameraMap.constBegin(); it != m_cameraMap.constEnd(); ++it) { QCameraLens *camera = it.value(); CameraInfo c; @@ -984,7 +985,12 @@ void GLTFExporter::parseCameras() c.zfar = camera->farPlane(); // GLTF cameras point in -Z by default, the rest is in the - // node matrix, so no separate look-at params given here. + // node matrix, so no separate look-at params given here, unless it's actually QCamera. + QCamera *cameraEntity = nullptr; + const QVector<QEntity *> entities = camera->entities(); + if (entities.size() == 1) + cameraEntity = qobject_cast<QCamera *>(entities.at(0)); + c.cameraEntity = cameraEntity; m_cameraInfo.insert(camera, c); if (GLTFExporterLog().isDebugEnabled()) { @@ -1259,6 +1265,11 @@ bool GLTFExporter::saveScene() camera["type"] = QStringLiteral("orthographic"); camera["orthographic"] = proj; } + if (camInfo.cameraEntity) { + camera["position"] = vec2jsvec(camInfo.cameraEntity->position()); + camera["upVector"] = vec2jsvec(camInfo.cameraEntity->upVector()); + camera["viewCenter"] = vec2jsvec(camInfo.cameraEntity->viewCenter()); + } camera["name"] = camInfo.originalName; cameras[camInfo.name] = camera; } diff --git a/src/plugins/sceneparsers/gltfexport/gltfexporter.h b/src/plugins/sceneparsers/gltfexport/gltfexporter.h index e43bfe29c..07d852fd4 100644 --- a/src/plugins/sceneparsers/gltfexport/gltfexporter.h +++ b/src/plugins/sceneparsers/gltfexport/gltfexporter.h @@ -72,6 +72,7 @@ class QTransform; namespace Qt3DRender { +class QCamera; class QCameraLens; class QMaterial; class QGeometryRenderer; @@ -196,6 +197,8 @@ private: // Orthographic properties float xmag; float ymag; + + QCamera *cameraEntity; }; struct LightInfo { diff --git a/tests/auto/render/gltfplugins/tst_gltfplugins.cpp b/tests/auto/render/gltfplugins/tst_gltfplugins.cpp index 3ab10f17a..a800fc1fa 100644 --- a/tests/auto/render/gltfplugins/tst_gltfplugins.cpp +++ b/tests/auto/render/gltfplugins/tst_gltfplugins.cpp @@ -241,6 +241,7 @@ void tst_gltfPlugins::createTestScene() camera->lens()->setObjectName(QStringLiteral("Main camera lens")); camera->setFieldOfView(30.0f); camera->setAspectRatio(1.0f); + m_entityMap.insert(camera->objectName(), camera); } // Ortho camera { @@ -263,6 +264,7 @@ void tst_gltfPlugins::createTestScene() lens->setObjectName(QStringLiteral("Ortho camera lens")); camera->addComponent(lens); + m_entityMap.insert(camera->objectName(), camera); #ifdef VISUAL_CHECK m_view1->defaultFrameGraph()->setCamera(camera); #endif @@ -1208,6 +1210,16 @@ void tst_gltfPlugins::exportAndImport() meshComponent(importedEntity)); compareComponents(materialComponent(exportedEntity), materialComponent(importedEntity)); + Qt3DRender::QCamera *exportedCamera = + qobject_cast<Qt3DRender::QCamera *>(exportedEntity); + if (exportedCamera) { + Qt3DRender::QCamera *importedCamera = + qobject_cast<Qt3DRender::QCamera *>(importedEntity); + QVERIFY(importedCamera != nullptr); + QCOMPARE(exportedCamera->position(), importedCamera->position()); + QCOMPARE(exportedCamera->upVector(), importedCamera->upVector()); + QCOMPARE(exportedCamera->viewCenter(), importedCamera->viewCenter()); + } } } |