From a6339f1cb8757a1f349083e8c5e5ee2c2c342e5d Mon Sep 17 00:00:00 2001 From: assiduous Date: Thu, 7 Jan 2021 21:36:58 -0800 Subject: GLTF Loader: added camera loading --- AssetLoader/src/GLTFLoader.cpp | 44 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) (limited to 'AssetLoader/src/GLTFLoader.cpp') diff --git a/AssetLoader/src/GLTFLoader.cpp b/AssetLoader/src/GLTFLoader.cpp index 72862b8..a48969e 100644 --- a/AssetLoader/src/GLTFLoader.cpp +++ b/AssetLoader/src/GLTFLoader.cpp @@ -223,9 +223,10 @@ float4x4 Node::GetMatrix() const void Node::UpdateTransforms() { + const auto NodeTransform = (pMesh || pCamera) ? GetMatrix() : float4x4::Identity(); if (pMesh) { - pMesh->Transforms.matrix = GetMatrix(); + pMesh->Transforms.matrix = NodeTransform; if (pSkin != nullptr) { // Update join matrices @@ -241,6 +242,11 @@ void Node::UpdateTransforms() } } + if (pCamera) + { + pCamera->matrix = NodeTransform; + } + for (auto& child : Children) { child->UpdateTransforms(); @@ -311,7 +317,7 @@ void Model::LoadNode(IRenderDevice* pDevice, } // Node contains mesh data - if (gltf_node.mesh > -1) + if (gltf_node.mesh >= 0) { const tinygltf::Mesh& gltf_mesh = gltf_model.meshes[gltf_node.mesh]; std::unique_ptr pNewMesh{new Mesh{pDevice, NewNode->Matrix}}; @@ -552,6 +558,40 @@ void Model::LoadNode(IRenderDevice* pDevice, NewNode->pMesh = std::move(pNewMesh); } + // Node contains camera + if (gltf_node.camera >= 0) + { + const auto& gltf_cam = gltf_model.cameras[gltf_node.camera]; + + std::unique_ptr pNewCamera{new Camera{}}; + pNewCamera->Name = gltf_cam.name; + + if (gltf_cam.type == "perspective") + { + pNewCamera->Type = Camera::Projection::Perspective; + pNewCamera->Perspective.AspectRatio = static_cast(gltf_cam.perspective.aspectRatio); + pNewCamera->Perspective.YFov = static_cast(gltf_cam.perspective.yfov); + pNewCamera->Perspective.ZNear = static_cast(gltf_cam.perspective.znear); + pNewCamera->Perspective.ZFar = static_cast(gltf_cam.perspective.zfar); + } + else if (gltf_cam.type == "orthographic") + { + pNewCamera->Type = Camera::Projection::Orthographic; + pNewCamera->Orthographic.XMag = static_cast(gltf_cam.orthographic.xmag); + pNewCamera->Orthographic.YMag = static_cast(gltf_cam.orthographic.ymag); + pNewCamera->Orthographic.ZNear = static_cast(gltf_cam.orthographic.znear); + pNewCamera->Orthographic.ZFar = static_cast(gltf_cam.orthographic.zfar); + } + else + { + UNEXPECTED("Unexpected camera type: ", gltf_cam.type); + pNewCamera.reset(); + } + + if (pNewCamera) + NewNode->pCamera = std::move(pNewCamera); + } + LinearNodes.push_back(NewNode.get()); if (parent) { -- cgit v1.2.3