Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

138 rindas
3.0 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 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://www.wtfpl.net/ 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. vec3 Camera::GetPosition()
  62. {
  63. return m_position;
  64. }
  65. mat4 const &Camera::GetViewMatrix()
  66. {
  67. return m_view_matrix;
  68. }
  69. mat4 const &Camera::GetProjMatrix()
  70. {
  71. return m_proj_matrix;
  72. }
  73. void Camera::ForceSceneUpdate()
  74. {
  75. Scene::GetDefault()->SetViewMatrix(m_view_matrix);
  76. Scene::GetDefault()->SetProjMatrix(m_proj_matrix);
  77. }
  78. void Camera::TickGame(float seconds)
  79. {
  80. WorldEntity::TickGame(seconds);
  81. #if 0
  82. /* Hackish keyboard support */
  83. float updown = 2.f * (Input::GetButtonState('w')
  84. + Input::GetButtonState('z')
  85. - Input::GetButtonState('s'));
  86. float rightleft = 2.f * (Input::GetButtonState('d')
  87. - Input::GetButtonState('q')
  88. - Input::GetButtonState('a'));
  89. float pgupdown = 2.f * (Input::GetButtonState('r')
  90. - Input::GetButtonState('f'));
  91. /* Hackish stick support */
  92. static Stick *stick = NULL;
  93. if (!stick)
  94. stick = Input::TrackStick();
  95. if (stick && stick->GetAxisCount() >= 2)
  96. {
  97. rightleft += 2.f * stick->GetAxis(0) * std::abs(stick->GetAxis(0));
  98. updown += -2.f * stick->GetAxis(1) * std::abs(stick->GetAxis(1));
  99. }
  100. m_position += vec3(rightleft, pgupdown, -updown) * 200.f * seconds;
  101. m_target += vec3(rightleft, 0, -updown) * 200.f * seconds;
  102. #endif
  103. m_view_matrix = mat4::lookat(m_position, m_target, m_up)
  104. * mat4(m_rotation);
  105. }
  106. void Camera::TickDraw(float seconds)
  107. {
  108. WorldEntity::TickDraw(seconds);
  109. ForceSceneUpdate();
  110. }
  111. } /* namespace lol */