No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

camera.cpp 2.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://sam.zoy.org/projects/COPYING.WTFPL for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <cstring>
  14. #include <cstdlib>
  15. #include "core.h"
  16. #if defined _WIN32 || defined _XBOX
  17. # define strcasecmp _stricmp
  18. # undef near
  19. # undef far
  20. #endif
  21. namespace lol
  22. {
  23. Camera::Camera(vec3 const &position, vec3 const &target, vec3 const &up)
  24. : m_target(target),
  25. m_up(up)
  26. {
  27. m_gamegroup = GAMEGROUP_BEFORE;
  28. m_drawgroup = DRAWGROUP_CAMERA;
  29. /* Create a default perspective */
  30. SetPerspective(45.f, 800.f, 600.f, -1000.f, 1000.f);
  31. SetPosition(position);
  32. }
  33. Camera::~Camera()
  34. {
  35. }
  36. void Camera::SetPosition(vec3 const &pos)
  37. {
  38. m_position = pos;
  39. }
  40. void Camera::SetRotation(quat const &rot)
  41. {
  42. m_rotation = rot;
  43. }
  44. void Camera::SetOrtho(float width, float height, float near, float far)
  45. {
  46. m_proj_matrix = mat4::ortho(width, height, near, far);
  47. }
  48. void Camera::SetPerspective(float fov, float width, float height,
  49. float near, float far)
  50. {
  51. m_proj_matrix = mat4::perspective(fov, width, height, near, far);
  52. }
  53. void Camera::SetTarget(vec3 const &pos)
  54. {
  55. m_target = pos;
  56. }
  57. vec3 Camera::GetTarget()
  58. {
  59. return m_target;
  60. }
  61. mat4 const &Camera::GetViewMatrix()
  62. {
  63. return m_view_matrix;
  64. }
  65. mat4 const &Camera::GetProjMatrix()
  66. {
  67. return m_proj_matrix;
  68. }
  69. void Camera::TickGame(float seconds)
  70. {
  71. WorldEntity::TickGame(seconds);
  72. /* Hackish keyboard support */
  73. float updown = 2.f * (Input::GetButtonState('w')
  74. + Input::GetButtonState('z')
  75. - Input::GetButtonState('s'));
  76. float rightleft = 2.f * (Input::GetButtonState('d')
  77. - Input::GetButtonState('q')
  78. - Input::GetButtonState('a'));
  79. float pgupdown = 2.f * (Input::GetButtonState('r')
  80. - Input::GetButtonState('f'));
  81. #if 0
  82. /* Hackish stick support */
  83. static Stick *stick = NULL;
  84. if (!stick)
  85. stick = Input::TrackStick();
  86. if (stick && stick->GetAxisCount() >= 2)
  87. {
  88. rightleft += 2.f * stick->GetAxis(0) * std::abs(stick->GetAxis(0));
  89. updown += -2.f * stick->GetAxis(1) * std::abs(stick->GetAxis(1));
  90. }
  91. #endif
  92. m_position += vec3(rightleft, pgupdown, -updown) * 200.f * seconds;
  93. m_target += vec3(rightleft, 0, -updown) * 200.f * seconds;
  94. m_view_matrix = mat4::lookat(m_position, m_target, m_up)
  95. * mat4(m_rotation);
  96. }
  97. void Camera::TickDraw(float seconds)
  98. {
  99. WorldEntity::TickDraw(seconds);
  100. Scene::GetDefault()->SetViewMatrix(m_view_matrix);
  101. Scene::GetDefault()->SetProjMatrix(m_proj_matrix);
  102. }
  103. } /* namespace lol */