Open Source Tomb Raider Engine
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Camera.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 <glbinding/gl/gl33.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. int Camera::room = -1;
  56. void Camera::reset() {
  57. pos = glm::vec3(0.0f, 0.0f, 0.0f);
  58. rot = glm::vec2(glm::pi<float>(), 0.0f);
  59. posSpeed = glm::vec3(0.0f, 0.0f, 0.0f);
  60. rotSpeed = glm::vec2(0.0f, 0.0f);
  61. dirty = true;
  62. projection = glm::mat4(1.0f);
  63. view = glm::mat4(1.0f);
  64. room = -1;
  65. setSize(Window::getSize());
  66. }
  67. void Camera::setSize(glm::i32vec2 s) {
  68. //! \fixme TODO instead of mirroring the Y axis in the shader, scale with -1 here
  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. glm::quat quatY = glm::angleAxis(rot.x, glm::vec3(0.0f, 1.0f, 0.0f));
  152. glm::quat quatX = glm::angleAxis(rot.y, glm::vec3(1.0f, 0.0f, 0.0f));
  153. glm::quat quaternion = quatY * quatX;
  154. glm::vec3 clampedSpeed;
  155. if (movingFaster) {
  156. clampedSpeed = posSpeed * runFactor;
  157. if (glm::length(clampedSpeed) > (maxSpeed * runFactor)) {
  158. clampedSpeed = glm::normalize(clampedSpeed) * maxSpeed * runFactor;
  159. }
  160. } else {
  161. clampedSpeed = posSpeed;
  162. if (glm::length(clampedSpeed) > maxSpeed) {
  163. clampedSpeed = glm::normalize(clampedSpeed) * maxSpeed;
  164. }
  165. }
  166. pos += quaternion * clampedSpeed * dT;
  167. glm::mat4 translate = glm::translate(glm::mat4(1.0f), pos);
  168. glm::mat4 rotate = glm::toMat4(quaternion);
  169. view = glm::inverse(translate * rotate);
  170. if (updateViewFrustum)
  171. calculateFrustumPlanes();
  172. glm::vec3 at(0.0f, 0.0f, -1.0f);
  173. glm::vec3 up(0.0f, -1.0f, 0.0f);
  174. Sound::listenAt(pos, quaternion * at, quaternion * up);
  175. dirty = false;
  176. return updateViewFrustum;
  177. }
  178. void Camera::displayUI() {
  179. if (!showOverlay)
  180. return;
  181. if (ImGui::Begin("Camera Look-At Overlay", &showOverlay, ImVec2(0, 0), -1.0f,
  182. ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize
  183. | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings
  184. | ImGuiWindowFlags_AlwaysAutoResize)) {
  185. // TODO
  186. }
  187. ImGui::End();
  188. }
  189. // ----------------------------------------------------------------------------
  190. class FrustumPlane {
  191. public:
  192. FrustumPlane() : normal(glm::vec3(0.0f, 0.0f, 0.0f)), d(0.0f) { }
  193. void set(glm::vec3 v1, glm::vec3 v2, glm::vec3 v3) {
  194. normal = glm::normalize(glm::cross(v3 - v2, v1 - v2));
  195. d = -glm::dot(normal, v2);
  196. }
  197. float distance(glm::vec3 p) {
  198. return d + glm::dot(normal, p);
  199. }
  200. private:
  201. glm::vec3 normal;
  202. float d;
  203. };
  204. // ----------------------------------------------------------------------------
  205. #define NEAR 0
  206. #define FAR 1
  207. #define TOP 2
  208. #define BOTTOM 3
  209. #define LEFT 4
  210. #define RIGHT 5
  211. #define NTL 0
  212. #define NBL 1
  213. #define NBR 2
  214. #define NTR 3
  215. #define FTL 4
  216. #define FBL 5
  217. #define FBR 6
  218. #define FTR 7
  219. static FrustumPlane planes[6];
  220. static glm::vec3 frustumColors[6] = {
  221. glm::vec3(1.0f, 0.0f, 0.0f), // NEAR, red
  222. glm::vec3(0.0f, 1.0f, 0.0f), // FAR, green
  223. glm::vec3(0.0f, 0.0f, 1.0f), // TOP, blue
  224. glm::vec3(1.0f, 1.0f, 0.0f), // BOTTOM, yellow
  225. glm::vec3(0.0f, 1.0f, 1.0f), // LEFT, light-blue
  226. glm::vec3(1.0f, 0.0f, 1.0f) // RIGHT, pink
  227. };
  228. static glm::vec3 frustumVertices[8];
  229. static ShaderBuffer vertexBuffer;
  230. static ShaderBuffer colorBuffer;
  231. static ShaderBuffer indexBuffer;
  232. static ShaderBuffer vertexPointBuffer;
  233. static ShaderBuffer colorPointBuffer;
  234. void Camera::calculateFrustumPlanes() {
  235. glm::mat4 combo = projection * view;
  236. // Calculate frustum corners to display them
  237. glm::mat4 inverse = glm::inverse(combo);
  238. frustumVertices[NTL] = glm::vec3(1.0f, 1.0f, 0.0f);
  239. frustumVertices[NTR] = glm::vec3(-1.0f, 1.0f, 0.0f);
  240. frustumVertices[NBL] = glm::vec3(1.0f, -1.0f, 0.0f);
  241. frustumVertices[NBR] = glm::vec3(-1.0f, -1.0f, 0.0f);
  242. frustumVertices[FTL] = glm::vec3(1.0f, 1.0f, 1.0f);
  243. frustumVertices[FTR] = glm::vec3(-1.0f, 1.0f, 1.0f);
  244. frustumVertices[FBL] = glm::vec3(1.0f, -1.0f, 1.0f);
  245. frustumVertices[FBR] = glm::vec3(-1.0f, -1.0f, 1.0f);
  246. for (int i = 0; i < 8; i++) {
  247. glm::vec4 t = inverse * glm::vec4(frustumVertices[i], 1.0f);
  248. frustumVertices[i] = glm::vec3(t) / t.w;
  249. frustumVertices[i].y *= -1.0f;
  250. }
  251. // Set planes used for frustum culling
  252. planes[TOP].set(frustumVertices[NTR], frustumVertices[NTL], frustumVertices[FTL]);
  253. planes[BOTTOM].set(frustumVertices[NBL], frustumVertices[NBR], frustumVertices[FBR]);
  254. planes[LEFT].set(frustumVertices[NTL], frustumVertices[NBL], frustumVertices[FBL]);
  255. planes[RIGHT].set(frustumVertices[NBR], frustumVertices[NTR], frustumVertices[FBR]);
  256. planes[NEAR].set(frustumVertices[NTL], frustumVertices[NTR], frustumVertices[NBR]);
  257. planes[FAR].set(frustumVertices[FTR], frustumVertices[FTL], frustumVertices[FBL]);
  258. std::vector<glm::vec3> verts;
  259. // Near
  260. verts.push_back(frustumVertices[NTL]);
  261. verts.push_back(frustumVertices[NTR]);
  262. verts.push_back(frustumVertices[NBR]);
  263. verts.push_back(frustumVertices[NBL]);
  264. // Far
  265. verts.push_back(frustumVertices[FTR]);
  266. verts.push_back(frustumVertices[FTL]);
  267. verts.push_back(frustumVertices[FBL]);
  268. verts.push_back(frustumVertices[FBR]);
  269. // Top
  270. verts.push_back(frustumVertices[NTR]);
  271. verts.push_back(frustumVertices[NTL]);
  272. verts.push_back(frustumVertices[FTL]);
  273. verts.push_back(frustumVertices[FTR]);
  274. // Bottom
  275. verts.push_back(frustumVertices[NBL]);
  276. verts.push_back(frustumVertices[NBR]);
  277. verts.push_back(frustumVertices[FBR]);
  278. verts.push_back(frustumVertices[FBL]);
  279. // Left
  280. verts.push_back(frustumVertices[NTL]);
  281. verts.push_back(frustumVertices[NBL]);
  282. verts.push_back(frustumVertices[FBL]);
  283. verts.push_back(frustumVertices[FTL]);
  284. // Right
  285. verts.push_back(frustumVertices[NBR]);
  286. verts.push_back(frustumVertices[NTR]);
  287. verts.push_back(frustumVertices[FTR]);
  288. verts.push_back(frustumVertices[FBR]);
  289. vertexBuffer.bufferData(verts);
  290. verts.clear();
  291. std::vector<glm::vec3> cols;
  292. verts.push_back(getPosition());
  293. cols.push_back(glm::vec3(1.0f, 1.0f, 1.0f));
  294. vertexPointBuffer.bufferData(verts);
  295. colorPointBuffer.bufferData(cols);
  296. if (colorBuffer.getSize() == 0) {
  297. cols.clear();
  298. for (int i = 0; i < 6; i++) {
  299. for (int j = 0; j < 4; j++) {
  300. cols.push_back(frustumColors[i]);
  301. }
  302. }
  303. colorBuffer.bufferData(cols);
  304. }
  305. if (indexBuffer.getSize() == 0) {
  306. std::vector<unsigned short> inds;
  307. for (int i = 0; i < 6; i++) {
  308. inds.push_back(4 * i);
  309. inds.push_back((4 * i) + 1);
  310. inds.push_back((4 * i) + 2);
  311. inds.push_back((4 * i) + 3);
  312. inds.push_back((4 * i) + 2);
  313. inds.push_back(4 * i);
  314. }
  315. indexBuffer.bufferData(inds);
  316. }
  317. }
  318. bool Camera::boxInFrustum(BoundingBox b) {
  319. for (int i = 0; i < 6; i++) {
  320. int out = 0, in = 0;
  321. for (int c = 0; (c < 8) && ((in == 0) || (out == 0)); c++) {
  322. if (planes[i].distance(b.getCorner(c)) < 0)
  323. out++;
  324. else
  325. in++;
  326. }
  327. if (in == 0)
  328. return false;
  329. }
  330. return true;
  331. }
  332. void Camera::displayFrustum(glm::mat4 MVP) {
  333. Shader::set2DState(true, false);
  334. Shader::drawGL(vertexBuffer, colorBuffer, indexBuffer, MVP);
  335. Shader::drawGL(vertexPointBuffer, colorPointBuffer, MVP, gl::GL_POINTS);
  336. Shader::set2DState(false, false);
  337. }