FirstPersonCamera.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "FirstPersonCamera.h"
  2. FirstPersonCamera::FirstPersonCamera()
  3. : _pitchNode(NULL), _rootNode(NULL)
  4. {
  5. }
  6. FirstPersonCamera::~FirstPersonCamera()
  7. {
  8. SAFE_RELEASE(_pitchNode);
  9. SAFE_RELEASE(_rootNode);
  10. }
  11. void FirstPersonCamera::initialize(float nearPlane, float farPlane, float fov)
  12. {
  13. SAFE_RELEASE(_pitchNode);
  14. SAFE_RELEASE(_rootNode);
  15. _rootNode = Node::create("FirstPersonCamera_root");
  16. _pitchNode = Node::create("FirstPersonCamera_pitch");
  17. _rootNode->addChild(_pitchNode);
  18. float aspectRatio = Game::getInstance()->getAspectRatio();
  19. assert(aspectRatio > 0.0f);
  20. Camera* camera = Camera::createPerspective(fov, aspectRatio, nearPlane, farPlane);
  21. _pitchNode->setCamera(camera);
  22. SAFE_RELEASE(camera);
  23. }
  24. Node* FirstPersonCamera::getRootNode()
  25. {
  26. return _rootNode;
  27. }
  28. Camera* FirstPersonCamera::getCamera()
  29. {
  30. if (_pitchNode)
  31. return _pitchNode->getCamera();
  32. return NULL;
  33. }
  34. void FirstPersonCamera::setPosition(const Vector3& position)
  35. {
  36. _rootNode->setTranslation(position);
  37. }
  38. void FirstPersonCamera::moveForward(float amount)
  39. {
  40. Vector3 v = _pitchNode->getForwardVectorWorld();
  41. v.normalize().scale(amount);
  42. _rootNode->translate(v);
  43. }
  44. void FirstPersonCamera::moveBackward(float amount)
  45. {
  46. moveForward(-amount);
  47. }
  48. void FirstPersonCamera::moveLeft(float amount)
  49. {
  50. _rootNode->translateLeft(amount);
  51. }
  52. void FirstPersonCamera::moveRight(float amount)
  53. {
  54. _rootNode->translateLeft(-amount);
  55. }
  56. void FirstPersonCamera::moveUp(float amount)
  57. {
  58. _rootNode->translateUp(amount);
  59. }
  60. void FirstPersonCamera::moveDown(float amount)
  61. {
  62. _rootNode->translateUp(-amount);
  63. }
  64. void FirstPersonCamera::rotate(float yaw, float pitch)
  65. {
  66. _rootNode->rotateY(-yaw);
  67. _pitchNode->rotateX(pitch);
  68. }