2
0

MirrorCamera.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // ----------------------------------------------------------------
  2. // From Game Programming in C++ by Sanjay Madhav
  3. // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
  4. //
  5. // Released under the BSD License
  6. // See LICENSE in root directory for full details.
  7. // ----------------------------------------------------------------
  8. #include "MirrorCamera.h"
  9. #include "Actor.h"
  10. #include "Game.h"
  11. #include "Renderer.h"
  12. #include "LevelLoader.h"
  13. MirrorCamera::MirrorCamera(Actor* owner)
  14. :CameraComponent(owner)
  15. ,mHorzDist(150.0f)
  16. ,mVertDist(200.0f)
  17. ,mTargetDist(400.0f)
  18. {
  19. }
  20. void MirrorCamera::Update(float deltaTime)
  21. {
  22. CameraComponent::Update(deltaTime);
  23. // Compute ideal position
  24. Vector3 idealPos = ComputeCameraPos();
  25. // Target is target dist in front of owning actor
  26. Vector3 target = mOwner->GetPosition() -
  27. mOwner->GetForward() * mTargetDist;
  28. // Use actual position here, not ideal
  29. Matrix4 view = Matrix4::CreateLookAt(idealPos, target,
  30. Vector3::UnitZ);
  31. Game* game = mOwner->GetGame();
  32. game->GetRenderer()->SetMirrorView(view);
  33. }
  34. void MirrorCamera::SnapToIdeal()
  35. {
  36. Vector3 idealPos = ComputeCameraPos();
  37. // Compute target and view
  38. Vector3 target = mOwner->GetPosition() -
  39. mOwner->GetForward() * mTargetDist;
  40. // Use actual position here, not ideal
  41. Matrix4 view = Matrix4::CreateLookAt(idealPos, target,
  42. Vector3::UnitZ);
  43. Game* game = mOwner->GetGame();
  44. game->GetRenderer()->SetMirrorView(view);
  45. }
  46. void MirrorCamera::LoadProperties(const rapidjson::Value& inObj)
  47. {
  48. CameraComponent::LoadProperties(inObj);
  49. JsonHelper::GetFloat(inObj, "horzDist", mHorzDist);
  50. JsonHelper::GetFloat(inObj, "vertDist", mVertDist);
  51. JsonHelper::GetFloat(inObj, "targetDist", mTargetDist);
  52. }
  53. void MirrorCamera::SaveProperties(rapidjson::Document::AllocatorType& alloc, rapidjson::Value& inObj) const
  54. {
  55. CameraComponent::SaveProperties(alloc, inObj);
  56. JsonHelper::AddFloat(alloc, inObj, "horzDist", mHorzDist);
  57. JsonHelper::AddFloat(alloc, inObj, "vertDist", mVertDist);
  58. JsonHelper::AddFloat(alloc, inObj, "targetDist", mTargetDist);
  59. }
  60. Vector3 MirrorCamera::ComputeCameraPos() const
  61. {
  62. // Set camera position in front of
  63. Vector3 cameraPos = mOwner->GetPosition();
  64. cameraPos += mOwner->GetForward() * mHorzDist;
  65. cameraPos += Vector3::UnitZ * mVertDist;
  66. return cameraPos;
  67. }