MovableCamera.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include "Device.h"
  23. #include "Mat3.h"
  24. #include "Types.h"
  25. #include "MovableCamera.h"
  26. #include "InputManager.h"
  27. namespace crown
  28. {
  29. MovableCamera::MovableCamera(const Vec3& position, bool visible, float fov, float aspect, bool active, float speed, float sensibility) :
  30. Camera(position, visible, fov, aspect, active),
  31. mSpeed(speed),
  32. mMouseSensibility(sensibility),
  33. mUpPressed(false),
  34. mRightPressed(false),
  35. mDownPressed(false),
  36. mLeftPressed(false)
  37. {
  38. GetInputManager()->RegisterKeyboardListener(this);
  39. mRotFactor = Vec2(0.0f, 0.0f);
  40. mAngleX = 0.0f;
  41. mAngleY = 0.0f;
  42. GetInputManager()->set_cursor_relative_xy(Vec2(0.5f, 0.5f));
  43. }
  44. MovableCamera::~MovableCamera()
  45. {
  46. }
  47. float MovableCamera::GetMouseSensibility() const
  48. {
  49. return mMouseSensibility;
  50. }
  51. void MovableCamera::SetActive(bool active)
  52. {
  53. Camera::SetActive(active);
  54. GetInputManager()->set_cursor_relative_xy(Vec2(0.5f, 0.5f));
  55. }
  56. void MovableCamera::SetMouseSensibility(float sensibility)
  57. {
  58. mMouseSensibility = sensibility;
  59. }
  60. float MovableCamera::GetSpeed() const
  61. {
  62. return mSpeed;
  63. }
  64. void MovableCamera::SetSpeed(float speed)
  65. {
  66. mSpeed = speed;
  67. }
  68. void MovableCamera::Render()
  69. {
  70. if (!mActive)
  71. {
  72. return;
  73. }
  74. SetViewByMouse();
  75. if (mUpPressed)
  76. {
  77. MoveForward();
  78. UpdateViewMatrix();
  79. }
  80. if (mRightPressed)
  81. {
  82. StrafeRight();
  83. UpdateViewMatrix();
  84. }
  85. if (mDownPressed)
  86. {
  87. MoveBackward();
  88. UpdateViewMatrix();
  89. }
  90. if (mLeftPressed)
  91. {
  92. StrafeLeft();
  93. UpdateViewMatrix();
  94. }
  95. Camera::Render();
  96. }
  97. void MovableCamera::KeyPressed(const KeyboardEvent& event)
  98. {
  99. switch (event.key)
  100. {
  101. case 'w':
  102. {
  103. mUpPressed = true;
  104. break;
  105. }
  106. case 'a':
  107. {
  108. mLeftPressed = true;
  109. break;
  110. }
  111. case 's':
  112. {
  113. mDownPressed = true;
  114. break;
  115. }
  116. case 'd':
  117. {
  118. mRightPressed = true;
  119. break;
  120. }
  121. default:
  122. {
  123. break;
  124. }
  125. }
  126. }
  127. void MovableCamera::KeyReleased(const KeyboardEvent& event)
  128. {
  129. switch (event.key)
  130. {
  131. case 'w':
  132. {
  133. mUpPressed = false;
  134. break;
  135. }
  136. case 'a':
  137. {
  138. mLeftPressed = false;
  139. break;
  140. }
  141. case 's':
  142. {
  143. mDownPressed = false;
  144. break;
  145. }
  146. case 'd':
  147. {
  148. mRightPressed = false;
  149. break;
  150. }
  151. default:
  152. {
  153. break;
  154. }
  155. }
  156. }
  157. void MovableCamera::MoveForward()
  158. {
  159. mPosition += mLookAt * mSpeed;
  160. }
  161. void MovableCamera::MoveBackward()
  162. {
  163. mPosition -= mLookAt * mSpeed;
  164. }
  165. void MovableCamera::StrafeLeft()
  166. {
  167. Vec3 left = mUp.cross(mLookAt);
  168. left.normalize();
  169. mPosition += left * mSpeed;
  170. }
  171. void MovableCamera::StrafeRight()
  172. {
  173. Vec3 left = mUp.cross(mLookAt);
  174. left.normalize();
  175. mPosition -= left * mSpeed;
  176. }
  177. void MovableCamera::SetViewByMouse()
  178. {
  179. static Vec2 lastPos = GetInputManager()->get_cursor_relative_xy();
  180. Vec2 currentPos = GetInputManager()->get_cursor_relative_xy();
  181. GetInputManager()->set_cursor_relative_xy(Vec2(0.5f, 0.5f));
  182. if (lastPos == currentPos)
  183. {
  184. return;
  185. }
  186. Vec2 delta = lastPos - currentPos;
  187. GetInputManager()->set_cursor_relative_xy(Vec2(0.5f, 0.5f));
  188. lastPos = GetInputManager()->get_cursor_relative_xy();
  189. mAngleX += delta.y * mMouseSensibility;
  190. mAngleY += delta.x * mMouseSensibility;
  191. mAngleX = math::clamp_to_range(-89.999f * math::DEG_TO_RAD, 89.999f * math::DEG_TO_RAD, mAngleX);
  192. mAngleY = math::fmod(mAngleY, math::TWO_PI);
  193. Vec3 right(1, 0, 0);
  194. Vec3 look;
  195. look.x = 0.0f;
  196. look.y = math::sin(mAngleX);
  197. look.z = -math::cos(mAngleX);
  198. Vec3 up = right.cross(look);
  199. up.normalize();
  200. Mat3 m;
  201. m.build_rotation_y(mAngleY);
  202. look = m * look;
  203. mUp = m * up;
  204. Camera::SetLookAt(look);
  205. }
  206. } // namespace crown