git.s-ol.nu ~forks/DiligentTools / a6339f1
GLTF Loader: added camera loading assiduous 10 months ago
2 changed file(s) with 83 addition(s) and 11 deletion(s). Raw diff Collapse all Expand all
216216 std::vector<Node*> Joints;
217217 };
218218
219 struct Camera
220 {
221 enum class Projection
222 {
223 Unknown,
224 Perspective,
225 Orthographic
226 } Type = Projection::Unknown;
227
228 std::string Name;
229
230 union
231 {
232 struct PerspectiveCamera
233 {
234 float AspectRatio = 0;
235 float YFov = 0;
236 float ZFar = 0;
237 float ZNear = 0;
238 } Perspective;
239 struct OrthographicCamera
240 {
241 float XMag;
242 float YMag;
243 float ZFar;
244 float ZNear;
245 } Orthographic;
246 };
247
248 float4x4 matrix;
249 };
219250
220251 struct Node
221252 {
225256
226257 std::vector<std::unique_ptr<Node>> Children;
227258
228 float4x4 Matrix;
229 std::unique_ptr<Mesh> pMesh;
230 Skin* pSkin = nullptr;
231 Int32 SkinIndex = -1;
232 float3 Translation;
233 float3 Scale = float3{1, 1, 1};
234 Quaternion Rotation;
235 BoundBox BVH;
236 BoundBox AABB;
259 float4x4 Matrix;
260 std::unique_ptr<Mesh> pMesh;
261 std::unique_ptr<Camera> pCamera;
262 Skin* pSkin = nullptr;
263 Int32 SkinIndex = -1;
264 float3 Translation;
265 float3 Scale = float3{1, 1, 1};
266 Quaternion Rotation;
267 BoundBox BVH;
268 BoundBox AABB;
237269
238270 bool IsValidBVH = false;
239271
222222
223223 void Node::UpdateTransforms()
224224 {
225 const auto NodeTransform = (pMesh || pCamera) ? GetMatrix() : float4x4::Identity();
225226 if (pMesh)
226227 {
227 pMesh->Transforms.matrix = GetMatrix();
228 pMesh->Transforms.matrix = NodeTransform;
228229 if (pSkin != nullptr)
229230 {
230231 // Update join matrices
238239 pSkin->InverseBindMatrices[i] * JointNode->GetMatrix() * InverseTransform;
239240 }
240241 }
242 }
243
244 if (pCamera)
245 {
246 pCamera->matrix = NodeTransform;
241247 }
242248
243249 for (auto& child : Children)
310316 }
311317
312318 // Node contains mesh data
313 if (gltf_node.mesh > -1)
319 if (gltf_node.mesh >= 0)
314320 {
315321 const tinygltf::Mesh& gltf_mesh = gltf_model.meshes[gltf_node.mesh];
316322 std::unique_ptr<Mesh> pNewMesh{new Mesh{pDevice, NewNode->Matrix}};
551557 NewNode->pMesh = std::move(pNewMesh);
552558 }
553559
560 // Node contains camera
561 if (gltf_node.camera >= 0)
562 {
563 const auto& gltf_cam = gltf_model.cameras[gltf_node.camera];
564
565 std::unique_ptr<Camera> pNewCamera{new Camera{}};
566 pNewCamera->Name = gltf_cam.name;
567
568 if (gltf_cam.type == "perspective")
569 {
570 pNewCamera->Type = Camera::Projection::Perspective;
571 pNewCamera->Perspective.AspectRatio = static_cast<float>(gltf_cam.perspective.aspectRatio);
572 pNewCamera->Perspective.YFov = static_cast<float>(gltf_cam.perspective.yfov);
573 pNewCamera->Perspective.ZNear = static_cast<float>(gltf_cam.perspective.znear);
574 pNewCamera->Perspective.ZFar = static_cast<float>(gltf_cam.perspective.zfar);
575 }
576 else if (gltf_cam.type == "orthographic")
577 {
578 pNewCamera->Type = Camera::Projection::Orthographic;
579 pNewCamera->Orthographic.XMag = static_cast<float>(gltf_cam.orthographic.xmag);
580 pNewCamera->Orthographic.YMag = static_cast<float>(gltf_cam.orthographic.ymag);
581 pNewCamera->Orthographic.ZNear = static_cast<float>(gltf_cam.orthographic.znear);
582 pNewCamera->Orthographic.ZFar = static_cast<float>(gltf_cam.orthographic.zfar);
583 }
584 else
585 {
586 UNEXPECTED("Unexpected camera type: ", gltf_cam.type);
587 pNewCamera.reset();
588 }
589
590 if (pNewCamera)
591 NewNode->pCamera = std::move(pNewCamera);
592 }
593
554594 LinearNodes.push_back(NewNode.get());
555595 if (parent)
556596 {