Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Camera.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*!
  2. * \file src/Camera.cpp
  3. * \brief Camera, View Frustum
  4. *
  5. * \author Mongoose
  6. * \author xythobuz
  7. */
  8. #include <limits>
  9. #include "imgui/imgui.h"
  10. #include "global.h"
  11. #include "RunTime.h"
  12. #include "World.h"
  13. #include "system/Shader.h"
  14. #include "system/Sound.h"
  15. #include "system/Window.h"
  16. #include "Camera.h"
  17. #include <glm/gtc/epsilon.hpp>
  18. #include <glm/gtc/matrix_transform.hpp>
  19. #include <glm/gtc/quaternion.hpp>
  20. #include <glm/gtx/quaternion.hpp>
  21. static bool equal(float a, float b) {
  22. return glm::epsilonEqual(a, b, std::numeric_limits<float>::epsilon());
  23. }
  24. static bool equal(glm::vec2 a, float b) {
  25. return equal(a.x, b) && equal(a.y, b);
  26. }
  27. static bool equal(glm::vec3 a, float b) {
  28. return equal(a.x, b) && equal(a.y, b) && equal(a.z, b);
  29. }
  30. // ----------------------------------------------------------------------------
  31. const static float fov = 45.0f;
  32. const static float nearDist = 0.1f;
  33. const static float farDist = 75000.0f;
  34. const static float maxSpeed = 3072.0f;
  35. const static float controllerDeadZone = 0.33f;
  36. const static float controllerViewFactor = glm::pi<float>();
  37. const static float rotationAngleClamp = glm::pi<float>() * 2.0f;
  38. const static float rotationAngleVertMax = glm::pi<float>() / 2.0f;
  39. const static float runFactor = 2.5f;
  40. const static glm::vec3 rightUnit(1.0f, 0.0f, 0.0f);
  41. const static glm::vec3 upUnit(0.0f, 1.0f, 0.0f);
  42. const static glm::vec3 dirUnit(0.0f, 0.0f, -1.0f);
  43. glm::vec3 Camera::pos(0.0f, 0.0f, 0.0f);
  44. glm::vec2 Camera::rot(glm::pi<float>(), 0.0f);
  45. glm::vec3 Camera::posSpeed(0.0f, 0.0f, 0.0f);
  46. glm::vec2 Camera::rotSpeed(0.0f, 0.0f);
  47. glm::mat4 Camera::projection(1.0f);
  48. glm::mat4 Camera::view(1.0f);
  49. float Camera::rotationDeltaX = 0.75f;
  50. float Camera::rotationDeltaY = 0.75f;
  51. bool Camera::updateViewFrustum = true;
  52. bool Camera::dirty = true;
  53. bool Camera::showOverlay = false;
  54. bool Camera::movingFaster = false;
  55. bool Camera::keepInRoom = false;
  56. int Camera::room = -1;
  57. void Camera::reset() {
  58. pos = glm::vec3(0.0f, 0.0f, 0.0f);
  59. rot = glm::vec2(glm::pi<float>(), 0.0f);
  60. posSpeed = glm::vec3(0.0f, 0.0f, 0.0f);
  61. rotSpeed = glm::vec2(0.0f, 0.0f);
  62. dirty = true;
  63. projection = glm::mat4(1.0f);
  64. view = glm::mat4(1.0f);
  65. room = -1;
  66. setSize(Window::getSize());
  67. }
  68. void Camera::setSize(glm::i32vec2 s) {
  69. projection = glm::perspective(fov, float(s.x) / float(s.y), nearDist, farDist);
  70. }
  71. void Camera::handleAction(ActionEvents action, bool isFinished) {
  72. float factor = 1.0f;
  73. if (isFinished)
  74. factor = -1.0f;
  75. if (action == forwardAction) {
  76. posSpeed += dirUnit * maxSpeed * factor;
  77. } else if (action == backwardAction) {
  78. posSpeed -= dirUnit * maxSpeed * factor;
  79. } else if (action == leftAction) {
  80. posSpeed -= rightUnit * maxSpeed * factor;
  81. } else if (action == rightAction) {
  82. posSpeed += rightUnit * maxSpeed * factor;
  83. } else if (action == jumpAction) {
  84. posSpeed += upUnit * maxSpeed * factor;
  85. } else if (action == crouchAction) {
  86. posSpeed -= upUnit * maxSpeed * factor;
  87. } else if (action == walkAction) {
  88. movingFaster = !isFinished;
  89. } else {
  90. return;
  91. }
  92. dirty = true;
  93. }
  94. void Camera::handleMouseMotion(int x, int y) {
  95. if ((x != 0) || (y != 0))
  96. dirty = true;
  97. while (x > 0) {
  98. rot.x -= rotationDeltaX;
  99. x--;
  100. }
  101. while (x < 0) {
  102. rot.x += rotationDeltaX;
  103. x++;
  104. }
  105. while (y > 0) {
  106. if (rot.y > -rotationAngleVertMax) {
  107. rot.y -= rotationDeltaY;
  108. }
  109. y--;
  110. }
  111. while (y < 0) {
  112. if (rot.y < rotationAngleVertMax) {
  113. rot.y += rotationDeltaY;
  114. }
  115. y++;
  116. }
  117. }
  118. void Camera::handleControllerAxis(float value, KeyboardButton axis) {
  119. if (glm::epsilonEqual(value, 0.0f, controllerDeadZone))
  120. value = 0.0f;
  121. if (axis == leftXAxis) {
  122. posSpeed.x = maxSpeed * value;
  123. } else if (axis == leftYAxis) {
  124. posSpeed.z = maxSpeed * value;
  125. } else if (axis == rightXAxis) {
  126. rotSpeed.x = -controllerViewFactor * value;
  127. } else if (axis == rightYAxis) {
  128. rotSpeed.y = -controllerViewFactor * value;
  129. } else {
  130. return;
  131. }
  132. dirty = true;
  133. }
  134. bool Camera::update() {
  135. if ((!dirty) && equal(posSpeed, 0.0f) && equal(rotSpeed, 0.0f))
  136. return false;
  137. while (rot.x > rotationAngleClamp)
  138. rot.x -= rotationAngleClamp;
  139. while (rot.x < -rotationAngleClamp)
  140. rot.x += rotationAngleClamp;
  141. while (rot.y > rotationAngleClamp)
  142. rot.y -= rotationAngleClamp;
  143. while (rot.y < -rotationAngleClamp)
  144. rot.y += rotationAngleClamp;
  145. float dT = RunTime::getLastFrameTime();
  146. glm::vec2 newRot = rot + rotSpeed * dT;
  147. if ((newRot.y > -rotationAngleVertMax) && (newRot.y < rotationAngleVertMax))
  148. rot = newRot;
  149. else
  150. rotSpeed = glm::vec2(0.0f, 0.0f);
  151. static glm::quat quatZ = glm::angleAxis(glm::pi<float>(), glm::vec3(0.0f, 0.0f, 1.0f));
  152. glm::quat quatY = glm::angleAxis(rot.x, glm::vec3(0.0f, 1.0f, 0.0f));
  153. glm::quat quatX = glm::angleAxis(rot.y, glm::vec3(1.0f, 0.0f, 0.0f));
  154. glm::quat quaternion = quatZ * quatY * quatX;
  155. float factor = movingFaster ? runFactor : 1.0f;
  156. glm::vec3 clampedSpeed = posSpeed * factor;
  157. if (glm::length(clampedSpeed) > (maxSpeed * factor)) {
  158. clampedSpeed = glm::normalize(clampedSpeed) * maxSpeed * factor;
  159. }
  160. glm::vec3 newPos = pos + (quaternion * clampedSpeed * dT);
  161. if (keepInRoom) {
  162. if ((room < 0) || (room >= World::sizeRoom())) {
  163. keepInRoom = false;
  164. pos = newPos;
  165. } else if (World::getRoom(room).getBoundingBox().inBox(newPos)) {
  166. pos = newPos;
  167. }
  168. } else {
  169. pos = newPos;
  170. }
  171. glm::mat4 translate = glm::translate(glm::mat4(1.0f), pos);
  172. glm::mat4 rotate = glm::toMat4(quaternion);
  173. view = glm::inverse(translate * rotate);
  174. if (updateViewFrustum)
  175. calculateFrustumPlanes();
  176. glm::vec3 at(0.0f, 0.0f, -1.0f);
  177. glm::vec3 up(0.0f, -1.0f, 0.0f);
  178. Sound::listenAt(pos, quaternion * at, quaternion * up);
  179. dirty = false;
  180. return updateViewFrustum;
  181. }
  182. void Camera::displayUI() {
  183. if (!showOverlay)
  184. return;
  185. if (ImGui::Begin("Camera Look-At Overlay", &showOverlay, ImVec2(0, 0), -1.0f,
  186. ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize
  187. | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings
  188. | ImGuiWindowFlags_AlwaysAutoResize)) {
  189. // TODO
  190. }
  191. ImGui::End();
  192. }
  193. // ----------------------------------------------------------------------------
  194. class FrustumPlane {
  195. public:
  196. FrustumPlane() : normal(glm::vec3(0.0f, 0.0f, 0.0f)), d(0.0f) { }
  197. void set(glm::vec3 v1, glm::vec3 v2, glm::vec3 v3) {
  198. normal = glm::normalize(glm::cross(v3 - v2, v1 - v2));
  199. d = -glm::dot(normal, v2);
  200. }
  201. float distance(glm::vec3 p) {
  202. return d + glm::dot(normal, p);
  203. }
  204. private:
  205. glm::vec3 normal;
  206. float d;
  207. };
  208. // ----------------------------------------------------------------------------
  209. #define NEAR 0
  210. #define FAR 1
  211. #define TOP 2
  212. #define BOTTOM 3
  213. #define LEFT 4
  214. #define RIGHT 5
  215. #define NTL 0
  216. #define NBL 1
  217. #define NBR 2
  218. #define NTR 3
  219. #define FTL 4
  220. #define FBL 5
  221. #define FBR 6
  222. #define FTR 7
  223. static FrustumPlane planes[6];
  224. static glm::vec3 frustumColors[6] = {
  225. glm::vec3(1.0f, 0.0f, 0.0f), // NEAR, red
  226. glm::vec3(0.0f, 1.0f, 0.0f), // FAR, green
  227. glm::vec3(0.0f, 0.0f, 1.0f), // TOP, blue
  228. glm::vec3(1.0f, 1.0f, 0.0f), // BOTTOM, yellow
  229. glm::vec3(0.0f, 1.0f, 1.0f), // LEFT, light-blue
  230. glm::vec3(1.0f, 0.0f, 1.0f) // RIGHT, pink
  231. };
  232. static glm::vec3 frustumVertices[8];
  233. static std::vector<glm::vec3> vertexBuffer;
  234. static std::vector<glm::vec3> colorBuffer;
  235. static std::vector<unsigned short> indexBuffer;
  236. static std::vector<glm::vec3> vertexPointBuffer;
  237. static std::vector<glm::vec3> colorPointBuffer;
  238. void Camera::calculateFrustumPlanes() {
  239. vertexBuffer.clear();
  240. vertexPointBuffer.clear();
  241. colorPointBuffer.clear();
  242. glm::mat4 combo = projection * view;
  243. // Calculate frustum corners to display them
  244. glm::mat4 inverse = glm::inverse(combo);
  245. frustumVertices[NTL] = glm::vec3(1.0f, 1.0f, 0.0f);
  246. frustumVertices[NTR] = glm::vec3(-1.0f, 1.0f, 0.0f);
  247. frustumVertices[NBL] = glm::vec3(1.0f, -1.0f, 0.0f);
  248. frustumVertices[NBR] = glm::vec3(-1.0f, -1.0f, 0.0f);
  249. frustumVertices[FTL] = glm::vec3(1.0f, 1.0f, 1.0f);
  250. frustumVertices[FTR] = glm::vec3(-1.0f, 1.0f, 1.0f);
  251. frustumVertices[FBL] = glm::vec3(1.0f, -1.0f, 1.0f);
  252. frustumVertices[FBR] = glm::vec3(-1.0f, -1.0f, 1.0f);
  253. for (int i = 0; i < 8; i++) {
  254. glm::vec4 t = inverse * glm::vec4(frustumVertices[i], 1.0f);
  255. frustumVertices[i] = glm::vec3(t) / t.w;
  256. }
  257. // Set planes used for frustum culling
  258. planes[TOP].set(frustumVertices[NTR], frustumVertices[NTL], frustumVertices[FTL]);
  259. planes[BOTTOM].set(frustumVertices[NBL], frustumVertices[NBR], frustumVertices[FBR]);
  260. planes[LEFT].set(frustumVertices[NTL], frustumVertices[NBL], frustumVertices[FBL]);
  261. planes[RIGHT].set(frustumVertices[NBR], frustumVertices[NTR], frustumVertices[FBR]);
  262. planes[NEAR].set(frustumVertices[NTL], frustumVertices[NTR], frustumVertices[NBR]);
  263. planes[FAR].set(frustumVertices[FTR], frustumVertices[FTL], frustumVertices[FBL]);
  264. // Near
  265. vertexBuffer.push_back(frustumVertices[NTL]);
  266. vertexBuffer.push_back(frustumVertices[NTR]);
  267. vertexBuffer.push_back(frustumVertices[NBR]);
  268. vertexBuffer.push_back(frustumVertices[NBL]);
  269. // Far
  270. vertexBuffer.push_back(frustumVertices[FTR]);
  271. vertexBuffer.push_back(frustumVertices[FTL]);
  272. vertexBuffer.push_back(frustumVertices[FBL]);
  273. vertexBuffer.push_back(frustumVertices[FBR]);
  274. // Top
  275. vertexBuffer.push_back(frustumVertices[NTR]);
  276. vertexBuffer.push_back(frustumVertices[NTL]);
  277. vertexBuffer.push_back(frustumVertices[FTL]);
  278. vertexBuffer.push_back(frustumVertices[FTR]);
  279. // Bottom
  280. vertexBuffer.push_back(frustumVertices[NBL]);
  281. vertexBuffer.push_back(frustumVertices[NBR]);
  282. vertexBuffer.push_back(frustumVertices[FBR]);
  283. vertexBuffer.push_back(frustumVertices[FBL]);
  284. // Left
  285. vertexBuffer.push_back(frustumVertices[NTL]);
  286. vertexBuffer.push_back(frustumVertices[NBL]);
  287. vertexBuffer.push_back(frustumVertices[FBL]);
  288. vertexBuffer.push_back(frustumVertices[FTL]);
  289. // Right
  290. vertexBuffer.push_back(frustumVertices[NBR]);
  291. vertexBuffer.push_back(frustumVertices[NTR]);
  292. vertexBuffer.push_back(frustumVertices[FTR]);
  293. vertexBuffer.push_back(frustumVertices[FBR]);
  294. // Position indicator
  295. vertexPointBuffer.push_back(getPosition());
  296. colorPointBuffer.push_back(glm::vec3(1.0f, 1.0f, 1.0f));
  297. // Lazy initialization of frustum plane color buffer
  298. if (colorBuffer.size() == 0) {
  299. for (int i = 0; i < 6; i++) {
  300. for (int j = 0; j < 4; j++) {
  301. colorBuffer.push_back(frustumColors[i]);
  302. }
  303. }
  304. }
  305. if (indexBuffer.size() == 0) {
  306. for (int i = 0; i < 6; i++) {
  307. indexBuffer.push_back(4 * i);
  308. indexBuffer.push_back((4 * i) + 1);
  309. indexBuffer.push_back((4 * i) + 2);
  310. indexBuffer.push_back((4 * i) + 3);
  311. indexBuffer.push_back((4 * i) + 2);
  312. indexBuffer.push_back(4 * i);
  313. }
  314. }
  315. }
  316. bool Camera::boxInFrustum(BoundingBox b) {
  317. for (int i = 0; i < 6; i++) {
  318. int out = 0, in = 0;
  319. for (int c = 0; (c < 8) && ((in == 0) || (out == 0)); c++) {
  320. if (planes[i].distance(b.getCorner(c)) >= 0)
  321. out++;
  322. else
  323. in++;
  324. }
  325. if (in == 0)
  326. return false;
  327. }
  328. return true;
  329. }
  330. void Camera::displayFrustum(glm::mat4 MVP) {
  331. Shader::set2DState(true, false);
  332. Shader::drawGL(vertexBuffer, colorBuffer, indexBuffer, MVP);
  333. Shader::drawGL(vertexPointBuffer, colorPointBuffer, MVP, gl::GL_POINTS);
  334. Shader::set2DState(false, false);
  335. }