summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSean Harmer <sh@theharmers.co.uk>2018-07-16 14:32:08 +0100
committerSean Harmer <sean.harmer@kdab.com>2018-08-02 08:57:15 +0000
commit80a51b60c2f501a726b98e20eaba7f9551bee720 (patch)
tree43608ce96dc4725daac5cb2b332cae981fbce299 /src
parent8b9f4b0936b131dad72f0ea8da1347957af20c6e (diff)
Add support for loading specific animations from glTF 2 files
Can now specify source urls to QAnimationClipLoader with query parameters for animationIndex or animationName. Add a new manual test to demonstrate/test this by loading the 2nd animation from the Rigged-Simple.gltf file. Will followup with the same support for Qt 3D native json animation files. Change-Id: Icb66073f29b8471fe06e2e2e9c43720567dc9ee5 Reviewed-by: Paul Lemire <paul.lemire@kdab.com>
Diffstat (limited to 'src')
-rw-r--r--src/animation/backend/animationclip.cpp31
-rw-r--r--src/animation/backend/gltfimporter.cpp19
-rw-r--r--src/animation/backend/gltfimporter_p.h7
-rw-r--r--src/animation/frontend/qanimationcliploader.cpp8
4 files changed, 53 insertions, 12 deletions
diff --git a/src/animation/backend/animationclip.cpp b/src/animation/backend/animationclip.cpp
index e8f8f4552..cc0a91ce0 100644
--- a/src/animation/backend/animationclip.cpp
+++ b/src/animation/backend/animationclip.cpp
@@ -50,9 +50,13 @@
#include <QtCore/qjsonarray.h>
#include <QtCore/qjsondocument.h>
#include <QtCore/qjsonobject.h>
+#include <QtCore/qurlquery.h>
QT_BEGIN_NAMESPACE
+#define ANIMATION_INDEX_KEY QLatin1String("animationIndex")
+#define ANIMATION_NAME_KEY QLatin1String("animationName")
+
namespace Qt3DAnimation {
namespace Animation {
@@ -210,15 +214,36 @@ void AnimationClip::loadAnimationFromUrl()
return;
}
+ // Extract the animationName or animationIndex from the url query parameters.
+ // If both present, animationIndex wins.
+ int animationIndex = -1;
+ QString animationName;
+ if (m_source.hasQuery()) {
+ QUrlQuery query(m_source);
+ if (query.hasQueryItem(ANIMATION_INDEX_KEY)) {
+ bool ok = false;
+ int i = query.queryItemValue(ANIMATION_INDEX_KEY).toInt(&ok);
+ if (ok)
+ animationIndex = i;
+ }
+
+ if (animationIndex == -1 && query.hasQueryItem(ANIMATION_NAME_KEY)) {
+ animationName = query.queryItemValue(ANIMATION_NAME_KEY);
+ }
+
+ qCDebug(Jobs) << "animationIndex =" << animationIndex;
+ qCDebug(Jobs) << "animationName =" << animationName;
+ }
+
// TODO: Convert to plugins
// Load glTF or "native"
if (filePath.endsWith(QLatin1String("gltf"))) {
qCDebug(Jobs) << "Loading glTF animation from" << filePath;
GLTFImporter gltf;
gltf.load(&file);
- // TODO: Allow loading of a named animation from a file containing many
- m_name = gltf.animations().first().name;
- m_channels = gltf.createAnimationData();
+ auto nameAndChannels = gltf.createAnimationData(animationIndex, animationName);
+ m_name = nameAndChannels.name;
+ m_channels = nameAndChannels.channels;
} else if (filePath.endsWith(QLatin1String("json"))) {
// Native format
QByteArray animationData = file.readAll();
diff --git a/src/animation/backend/gltfimporter.cpp b/src/animation/backend/gltfimporter.cpp
index d9c0cb9a6..e91dd5661 100644
--- a/src/animation/backend/gltfimporter.cpp
+++ b/src/animation/backend/gltfimporter.cpp
@@ -466,16 +466,15 @@ QHash<int, int> GLTFImporter::createNodeIndexToJointIndexMap(const Skin &skin) c
return nodeIndexToJointIndexMap;
}
-QVector<Qt3DAnimation::Animation::Channel> GLTFImporter::createAnimationData(const QString &animationName) const
+GLTFImporter::AnimationNameAndChannels GLTFImporter::createAnimationData(int animationIndex, const QString &animationName) const
{
- QVector<Qt3DAnimation::Animation::Channel> channels;
+ AnimationNameAndChannels nameAndChannels;
if (m_animations.isEmpty()) {
qCWarning(Jobs) << "File does not contain any animation data";
- return channels;
+ return nameAndChannels;
}
- int animationIndex = 0;
- if (!animationName.isEmpty()) {
+ if (animationIndex == -1 && !animationName.isEmpty()) {
for (int i = 0; i < m_animations.size(); ++i) {
if (m_animations[i].name == animationName) {
animationIndex = i;
@@ -483,7 +482,13 @@ QVector<Qt3DAnimation::Animation::Channel> GLTFImporter::createAnimationData(con
}
}
}
+
+ if (animationIndex >= m_animations.size()) {
+ qCWarning(Jobs) << "Invalid animation index. Skipping.";
+ return nameAndChannels;
+ }
const Animation &animation = m_animations[animationIndex];
+ nameAndChannels.name = animation.name;
// Create node index to joint index lookup tables for each skin
QVector<QHash<int, int>> nodeIndexToJointIndexMaps;
@@ -621,11 +626,11 @@ QVector<Qt3DAnimation::Animation::Channel> GLTFImporter::createAnimationData(con
} // case 4
}
- channels.push_back(outputChannel);
+ nameAndChannels.channels.push_back(outputChannel);
++channelIndex;
}
- return channels;
+ return nameAndChannels;
}
GLTFImporter::RawData GLTFImporter::accessorData(int accessorIndex, int index) const
diff --git a/src/animation/backend/gltfimporter_p.h b/src/animation/backend/gltfimporter_p.h
index ae7674fe7..e31f8d078 100644
--- a/src/animation/backend/gltfimporter_p.h
+++ b/src/animation/backend/gltfimporter_p.h
@@ -182,7 +182,12 @@ public:
bool load(QIODevice *ioDev);
const QVector<Animation> animations() const { return m_animations; }
- QVector<Qt3DAnimation::Animation::Channel> createAnimationData(const QString &animationName = QString()) const;
+ struct AnimationNameAndChannels
+ {
+ QString name;
+ QVector<Qt3DAnimation::Animation::Channel> channels;
+ };
+ AnimationNameAndChannels createAnimationData(int animationIndex, const QString &animationName = QString()) const;
private:
static Qt3DRender::QAttribute::VertexBaseType accessorTypeFromJSON(int componentType);
diff --git a/src/animation/frontend/qanimationcliploader.cpp b/src/animation/frontend/qanimationcliploader.cpp
index be23f49a3..423ca136c 100644
--- a/src/animation/frontend/qanimationcliploader.cpp
+++ b/src/animation/frontend/qanimationcliploader.cpp
@@ -77,7 +77,13 @@ void QAnimationClipLoaderPrivate::setStatus(QAnimationClipLoader::Status status)
/*!
\property Qt3DAnimation::QAnimationClipLoader::source
- Holds the source URL from which to load the animation clip.
+ Holds the source URL from which to load the animation clip. Currently
+ glTF2 and the native Qt 3D json animation file formats are supported.
+
+ In the case where a file contains multiple animations, it is possible
+ to select which animation should be loaded by way of query parameters
+ on the source url. The accepted query parameters are animationIndex and
+ animationName. If both are specified, animationName is ignored.
*/
/*!
\class Qt3DAnimation::QAnimationClipLoader