summaryrefslogtreecommitdiffstats
path: root/AssetLoader
diff options
context:
space:
mode:
authorassiduous <assiduous@diligentgraphics.com>2020-12-10 22:52:07 +0000
committerassiduous <assiduous@diligentgraphics.com>2020-12-10 22:52:07 +0000
commit8cc4d58920cd6bdab0b7fb328d7d3922176d1f40 (patch)
tree708766855cd54b0fb99e38e4c2098c7b1efda0ea /AssetLoader
parentGLTFLoader: updated model initialization API; few minor improvements to loading (diff)
downloadDiligentTools-8cc4d58920cd6bdab0b7fb328d7d3922176d1f40.tar.gz
DiligentTools-8cc4d58920cd6bdab0b7fb328d7d3922176d1f40.zip
GLTF Loader: added
Diffstat (limited to 'AssetLoader')
-rw-r--r--AssetLoader/interface/GLTFLoader.hpp7
-rw-r--r--AssetLoader/src/GLTFLoader.cpp27
2 files changed, 26 insertions, 8 deletions
diff --git a/AssetLoader/interface/GLTFLoader.hpp b/AssetLoader/interface/GLTFLoader.hpp
index 604c838..81c7a96 100644
--- a/AssetLoader/interface/GLTFLoader.hpp
+++ b/AssetLoader/interface/GLTFLoader.hpp
@@ -348,9 +348,6 @@ struct Model
/// Optional resource cache usage info.
ResourceCacheUseInfo* pCacheInfo = nullptr;
-
- /// Optional transform to apply to the model
- const float4x4* pTransform = nullptr;
};
Model(IRenderDevice* pDevice,
IDeviceContext* pContext,
@@ -362,6 +359,8 @@ struct Model
void PrepareGPUResources(IRenderDevice* pDevice, IDeviceContext* pCtx);
+ void Transform(const float4x4& Matrix);
+
IBuffer* GetBuffer(BUFFER_ID BuffId)
{
return Buffers[BuffId].pBuffer;
@@ -415,7 +414,7 @@ private:
void LoadMaterials(const tinygltf::Model& gltf_model);
void LoadAnimations(const tinygltf::Model& gltf_model);
void CalculateBoundingBox(Node* node, const Node* parent);
- void GetSceneDimensions();
+ void CalculateSceneDimensions();
Node* FindNode(Node* parent, Uint32 index);
Node* NodeFromIndex(uint32_t index);
diff --git a/AssetLoader/src/GLTFLoader.cpp b/AssetLoader/src/GLTFLoader.cpp
index a76bb7a..1adc4a6 100644
--- a/AssetLoader/src/GLTFLoader.cpp
+++ b/AssetLoader/src/GLTFLoader.cpp
@@ -204,7 +204,10 @@ Mesh::Mesh(IRenderDevice* pDevice, const float4x4& matrix)
float4x4 Node::LocalMatrix() const
{
- return Matrix * float4x4::Scale(Scale) * Rotation.ToMatrix() * float4x4::Translation(Translation);
+ // Translation, rotation, and scale properties and local space transformation are
+ // mutually exclusive in GLTF.
+ // We, however, may use non-trivial Matrix with TRS to apply transform to a model.
+ return float4x4::Scale(Scale) * Rotation.ToMatrix() * float4x4::Translation(Translation) * Matrix;
}
float4x4 Node::GetMatrix() const
@@ -271,6 +274,9 @@ void Model::LoadNode(IRenderDevice* pDevice,
NewNode->SkinIndex = gltf_node.skin;
NewNode->Matrix = float4x4::Identity();
+ // Any node can define a local space transformation either by supplying a matrix property,
+ // or any of translation, rotation, and scale properties (also known as TRS properties).
+
// Generate local node matrix
//float3 Translation;
if (gltf_node.translation.size() == 3)
@@ -307,7 +313,7 @@ void Model::LoadNode(IRenderDevice* pDevice,
if (gltf_node.mesh > -1)
{
const tinygltf::Mesh& gltf_mesh = gltf_model.meshes[gltf_node.mesh];
- std::unique_ptr<Mesh> pNewMesh{new Mesh(pDevice, NewNode->Matrix)};
+ std::unique_ptr<Mesh> pNewMesh{new Mesh{pDevice, NewNode->Matrix}};
for (size_t j = 0; j < gltf_mesh.primitives.size(); j++)
{
const tinygltf::Primitive& primitive = gltf_mesh.primitives[j];
@@ -1765,7 +1771,7 @@ void Model::LoadFromFile(IRenderDevice* pDevice,
PrepareGPUResources(pDevice, pContext);
}
- GetSceneDimensions();
+ CalculateSceneDimensions();
}
void Model::CalculateBoundingBox(Node* node, const Node* parent)
@@ -1795,7 +1801,7 @@ void Model::CalculateBoundingBox(Node* node, const Node* parent)
}
}
-void Model::GetSceneDimensions()
+void Model::CalculateSceneDimensions()
{
// Calculate binary volume hierarchy for all nodes in the scene
for (auto* node : LinearNodes)
@@ -1896,6 +1902,19 @@ void Model::UpdateAnimation(Uint32 index, float time)
}
}
+void Model::Transform(const float4x4& Matrix)
+{
+ for (auto& root_node : Nodes)
+ root_node->Matrix *= Matrix;
+
+ for (auto* node : LinearNodes)
+ {
+ if (node->pMesh)
+ node->Update();
+ }
+
+ CalculateSceneDimensions();
+}
Node* Model::FindNode(Node* parent, Uint32 index)
{