camera.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Copyright 2013 Dario Manesku. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include <bx/timer.h>
  6. #include <bx/math.h>
  7. #include "camera.h"
  8. #include "entry/entry.h"
  9. #include "entry/cmd.h"
  10. #include "entry/input.h"
  11. #include <bx/allocator.h>
  12. int cmdMove(CmdContext* /*_context*/, void* /*_userData*/, int _argc, char const* const* _argv)
  13. {
  14. if (_argc > 1)
  15. {
  16. if (0 == bx::strCmp(_argv[1], "forward") )
  17. {
  18. cameraSetKeyState(CAMERA_KEY_FORWARD, true);
  19. return 0;
  20. }
  21. else if (0 == bx::strCmp(_argv[1], "left") )
  22. {
  23. cameraSetKeyState(CAMERA_KEY_LEFT, true);
  24. return 0;
  25. }
  26. else if (0 == bx::strCmp(_argv[1], "right") )
  27. {
  28. cameraSetKeyState(CAMERA_KEY_RIGHT, true);
  29. return 0;
  30. }
  31. else if (0 == bx::strCmp(_argv[1], "backward") )
  32. {
  33. cameraSetKeyState(CAMERA_KEY_BACKWARD, true);
  34. return 0;
  35. }
  36. else if (0 == bx::strCmp(_argv[1], "up") )
  37. {
  38. cameraSetKeyState(CAMERA_KEY_UP, true);
  39. return 0;
  40. }
  41. else if (0 == bx::strCmp(_argv[1], "down") )
  42. {
  43. cameraSetKeyState(CAMERA_KEY_DOWN, true);
  44. return 0;
  45. }
  46. }
  47. return 1;
  48. }
  49. static void cmd(const void* _userData)
  50. {
  51. cmdExec( (const char*)_userData);
  52. }
  53. static const InputBinding s_camBindings[] =
  54. {
  55. { entry::Key::KeyW, entry::Modifier::None, 0, cmd, "move forward" },
  56. { entry::Key::GamepadUp, entry::Modifier::None, 0, cmd, "move forward" },
  57. { entry::Key::KeyA, entry::Modifier::None, 0, cmd, "move left" },
  58. { entry::Key::GamepadLeft, entry::Modifier::None, 0, cmd, "move left" },
  59. { entry::Key::KeyS, entry::Modifier::None, 0, cmd, "move backward" },
  60. { entry::Key::GamepadDown, entry::Modifier::None, 0, cmd, "move backward" },
  61. { entry::Key::KeyD, entry::Modifier::None, 0, cmd, "move right" },
  62. { entry::Key::GamepadRight, entry::Modifier::None, 0, cmd, "move right" },
  63. { entry::Key::KeyQ, entry::Modifier::None, 0, cmd, "move down" },
  64. { entry::Key::GamepadShoulderL, entry::Modifier::None, 0, cmd, "move down" },
  65. { entry::Key::KeyE, entry::Modifier::None, 0, cmd, "move up" },
  66. { entry::Key::GamepadShoulderR, entry::Modifier::None, 0, cmd, "move up" },
  67. INPUT_BINDING_END
  68. };
  69. struct Camera
  70. {
  71. struct MouseCoords
  72. {
  73. int32_t m_mx;
  74. int32_t m_my;
  75. int32_t m_mz;
  76. };
  77. Camera()
  78. {
  79. reset();
  80. entry::MouseState mouseState;
  81. update(0.0f, mouseState, true);
  82. cmdAdd("move", cmdMove);
  83. inputAddBindings("camBindings", s_camBindings);
  84. }
  85. ~Camera()
  86. {
  87. inputRemoveBindings("camBindings");
  88. }
  89. void reset()
  90. {
  91. m_mouseNow.m_mx = 0;
  92. m_mouseNow.m_my = 0;
  93. m_mouseNow.m_mz = 0;
  94. m_mouseLast.m_mx = 0;
  95. m_mouseLast.m_my = 0;
  96. m_mouseLast.m_mz = 0;
  97. m_eye.x = 0.0f;
  98. m_eye.y = 0.0f;
  99. m_eye.z = -35.0f;
  100. m_at.x = 0.0f;
  101. m_at.y = 0.0f;
  102. m_at.z = -1.0f;
  103. m_up.x = 0.0f;
  104. m_up.y = 1.0f;
  105. m_up.z = 0.0f;
  106. m_horizontalAngle = 0.01f;
  107. m_verticalAngle = 0.0f;
  108. m_mouseSpeed = 0.0020f;
  109. m_gamepadSpeed = 0.04f;
  110. m_moveSpeed = 30.0f;
  111. m_keys = 0;
  112. m_mouseDown = false;
  113. }
  114. void setKeyState(uint8_t _key, bool _down)
  115. {
  116. m_keys &= ~_key;
  117. m_keys |= _down ? _key : 0;
  118. }
  119. void update(float _deltaTime, const entry::MouseState& _mouseState, bool _reset)
  120. {
  121. if (_reset)
  122. {
  123. m_mouseLast.m_mx = _mouseState.m_mx;
  124. m_mouseLast.m_my = _mouseState.m_my;
  125. m_mouseLast.m_mz = _mouseState.m_mz;
  126. m_mouseNow = m_mouseLast;
  127. m_mouseDown = false;
  128. return;
  129. }
  130. if (!m_mouseDown)
  131. {
  132. m_mouseLast.m_mx = _mouseState.m_mx;
  133. m_mouseLast.m_my = _mouseState.m_my;
  134. }
  135. m_mouseDown = !!_mouseState.m_buttons[entry::MouseButton::Right];
  136. if (m_mouseDown)
  137. {
  138. m_mouseNow.m_mx = _mouseState.m_mx;
  139. m_mouseNow.m_my = _mouseState.m_my;
  140. }
  141. m_mouseLast.m_mz = m_mouseNow.m_mz;
  142. m_mouseNow.m_mz = _mouseState.m_mz;
  143. const float deltaZ = float(m_mouseNow.m_mz - m_mouseLast.m_mz);
  144. if (m_mouseDown)
  145. {
  146. const int32_t deltaX = m_mouseNow.m_mx - m_mouseLast.m_mx;
  147. const int32_t deltaY = m_mouseNow.m_my - m_mouseLast.m_my;
  148. m_horizontalAngle += m_mouseSpeed * float(deltaX);
  149. m_verticalAngle -= m_mouseSpeed * float(deltaY);
  150. m_mouseLast.m_mx = m_mouseNow.m_mx;
  151. m_mouseLast.m_my = m_mouseNow.m_my;
  152. }
  153. entry::GamepadHandle handle = { 0 };
  154. m_horizontalAngle += m_gamepadSpeed * inputGetGamepadAxis(handle, entry::GamepadAxis::RightX)/32768.0f;
  155. m_verticalAngle -= m_gamepadSpeed * inputGetGamepadAxis(handle, entry::GamepadAxis::RightY)/32768.0f;
  156. const int32_t gpx = inputGetGamepadAxis(handle, entry::GamepadAxis::LeftX);
  157. const int32_t gpy = inputGetGamepadAxis(handle, entry::GamepadAxis::LeftY);
  158. m_keys |= gpx < -16834 ? CAMERA_KEY_LEFT : 0;
  159. m_keys |= gpx > 16834 ? CAMERA_KEY_RIGHT : 0;
  160. m_keys |= gpy < -16834 ? CAMERA_KEY_FORWARD : 0;
  161. m_keys |= gpy > 16834 ? CAMERA_KEY_BACKWARD : 0;
  162. const bx::Vec3 direction =
  163. {
  164. bx::cos(m_verticalAngle) * bx::sin(m_horizontalAngle),
  165. bx::sin(m_verticalAngle),
  166. bx::cos(m_verticalAngle) * bx::cos(m_horizontalAngle),
  167. };
  168. const bx::Vec3 right =
  169. {
  170. bx::sin(m_horizontalAngle - bx::kPiHalf),
  171. 0.0f,
  172. bx::cos(m_horizontalAngle - bx::kPiHalf),
  173. };
  174. const bx::Vec3 up = bx::cross(right, direction);
  175. m_eye = bx::mad(direction, deltaZ * _deltaTime * m_moveSpeed, m_eye);
  176. if (m_keys & CAMERA_KEY_FORWARD)
  177. {
  178. m_eye = bx::mad(direction, _deltaTime * m_moveSpeed, m_eye);
  179. setKeyState(CAMERA_KEY_FORWARD, false);
  180. }
  181. if (m_keys & CAMERA_KEY_BACKWARD)
  182. {
  183. m_eye = bx::mad(direction, -_deltaTime * m_moveSpeed, m_eye);
  184. setKeyState(CAMERA_KEY_BACKWARD, false);
  185. }
  186. if (m_keys & CAMERA_KEY_LEFT)
  187. {
  188. m_eye = bx::mad(right, _deltaTime * m_moveSpeed, m_eye);
  189. setKeyState(CAMERA_KEY_LEFT, false);
  190. }
  191. if (m_keys & CAMERA_KEY_RIGHT)
  192. {
  193. m_eye = bx::mad(right, -_deltaTime * m_moveSpeed, m_eye);
  194. setKeyState(CAMERA_KEY_RIGHT, false);
  195. }
  196. if (m_keys & CAMERA_KEY_UP)
  197. {
  198. m_eye = bx::mad(up, _deltaTime * m_moveSpeed, m_eye);
  199. setKeyState(CAMERA_KEY_UP, false);
  200. }
  201. if (m_keys & CAMERA_KEY_DOWN)
  202. {
  203. m_eye = bx::mad(up, -_deltaTime * m_moveSpeed, m_eye);
  204. setKeyState(CAMERA_KEY_DOWN, false);
  205. }
  206. m_at = bx::add(m_eye, direction);
  207. m_up = bx::cross(right, direction);
  208. }
  209. void getViewMtx(float* _viewMtx)
  210. {
  211. bx::mtxLookAt(_viewMtx, bx::load<bx::Vec3>(&m_eye.x), bx::load<bx::Vec3>(&m_at.x), bx::load<bx::Vec3>(&m_up.x) );
  212. }
  213. void setPosition(const bx::Vec3& _pos)
  214. {
  215. m_eye = _pos;
  216. }
  217. void setVerticalAngle(float _verticalAngle)
  218. {
  219. m_verticalAngle = _verticalAngle;
  220. }
  221. void setHorizontalAngle(float _horizontalAngle)
  222. {
  223. m_horizontalAngle = _horizontalAngle;
  224. }
  225. MouseCoords m_mouseNow;
  226. MouseCoords m_mouseLast;
  227. bx::Vec3 m_eye;
  228. bx::Vec3 m_at;
  229. bx::Vec3 m_up;
  230. float m_horizontalAngle;
  231. float m_verticalAngle;
  232. float m_mouseSpeed;
  233. float m_gamepadSpeed;
  234. float m_moveSpeed;
  235. uint8_t m_keys;
  236. bool m_mouseDown;
  237. };
  238. static Camera* s_camera = NULL;
  239. void cameraCreate()
  240. {
  241. s_camera = BX_NEW(entry::getAllocator(), Camera);
  242. }
  243. void cameraDestroy()
  244. {
  245. BX_DELETE(entry::getAllocator(), s_camera);
  246. s_camera = NULL;
  247. }
  248. void cameraSetPosition(const bx::Vec3& _pos)
  249. {
  250. s_camera->setPosition(_pos);
  251. }
  252. void cameraSetHorizontalAngle(float _horizontalAngle)
  253. {
  254. s_camera->setHorizontalAngle(_horizontalAngle);
  255. }
  256. void cameraSetVerticalAngle(float _verticalAngle)
  257. {
  258. s_camera->setVerticalAngle(_verticalAngle);
  259. }
  260. void cameraSetKeyState(uint8_t _key, bool _down)
  261. {
  262. s_camera->setKeyState(_key, _down);
  263. }
  264. void cameraGetViewMtx(float* _viewMtx)
  265. {
  266. s_camera->getViewMtx(_viewMtx);
  267. }
  268. bx::Vec3 cameraGetPosition()
  269. {
  270. return s_camera->m_eye;
  271. }
  272. bx::Vec3 cameraGetAt()
  273. {
  274. return s_camera->m_at;
  275. }
  276. void cameraUpdate(float _deltaTime, const entry::MouseState& _mouseState, bool _reset)
  277. {
  278. s_camera->update(_deltaTime, _mouseState, _reset);
  279. }