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

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