Open Source Tomb Raider Engine
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Camera.cpp 11KB

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