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 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /* -*- Mode: C++; tab-width: 3; indent-tabs-mode: t; c-basic-offset: 3 -*- */
  2. /*================================================================
  3. *
  4. * Project : Freyja
  5. * Author : Mongoose
  6. * Website : http://www.westga.edu/~stu7440/
  7. * Email : stu7440@westga.edu
  8. * Object : Camera
  9. * License : No use w/o permission (C) 2001 Mongoose
  10. * Comments: OpenGL camera class for Freyja
  11. *
  12. * This file was generated using Mongoose's C++
  13. * template generator script. <stu7440@westga.edu>
  14. *
  15. *-- History -------------------------------------------------
  16. *
  17. * 2002.12.16:
  18. * Mongoose - Removed perspective setting and OpenGL dependency
  19. * API changes to reflect new direction of this object:
  20. * Removing outdated algorithms and code
  21. * And refactoring the class in general
  22. *
  23. * 2001.06.06:
  24. * Mongoose - Moving GLU code into here to setup break up
  25. * into Camera base class, DynamicCamera,
  26. * and GLUCamera child classes
  27. *
  28. * 2001.06.04:
  29. * Mongoose - Quaternion based compile option
  30. *
  31. * 2001.05.18:
  32. * Mongoose - Created
  33. =================================================================*/
  34. #include <math.h>
  35. #include "hel/math.h"
  36. #include "Camera.h"
  37. #ifdef DEBUG_MEMEORY
  38. # include "memeory_test.h"
  39. #endif
  40. unsigned int Camera::mCounter = 0;
  41. ////////////////////////////////////////////////////////////
  42. // Constructors
  43. ////////////////////////////////////////////////////////////
  44. Camera::Camera()
  45. {
  46. mId = ++mCounter;
  47. mFlags = 0;
  48. mViewDistance = 14.0;
  49. mTranslateDelta = 256.0;
  50. mRotateDelta = helDegToRad(15.0);
  51. mRotateDelta2 = helDegToRad(5.0);
  52. mFlags &= Camera_FlyMode;
  53. reset();
  54. }
  55. Camera::~Camera()
  56. {
  57. }
  58. ////////////////////////////////////////////////////////////
  59. // Public Accessors
  60. ////////////////////////////////////////////////////////////
  61. unsigned int Camera::getId()
  62. {
  63. return mId;
  64. }
  65. void Camera::getPosition(vec3_t pos)
  66. {
  67. pos[0] = mPos[0];
  68. pos[1] = mPos[1];
  69. pos[2] = mPos[2];
  70. }
  71. void Camera::getUp(vec3_t up)
  72. {
  73. up[0] = mUp[0];
  74. up[1] = mUp[1];
  75. up[2] = mUp[2];
  76. }
  77. void Camera::getTarget(vec3_t target)
  78. {
  79. target[0] = mTarget[0];
  80. target[1] = mTarget[1];
  81. target[2] = mTarget[2];
  82. }
  83. float Camera::getYaw()
  84. {
  85. return helRadToDeg(mTheta);
  86. }
  87. double Camera::getRadianYaw()
  88. {
  89. return mTheta;
  90. }
  91. double Camera::getRadianPitch()
  92. {
  93. return mTheta2;
  94. }
  95. ////////////////////////////////////////////////////////////
  96. // Public Mutators
  97. ////////////////////////////////////////////////////////////
  98. void Camera::rotate(float angle, float x, float y, float z)
  99. {
  100. Quaternion t, n;
  101. Matrix matrix;
  102. double side[4] = { 1, 0, 0, 1 };
  103. double up[4] = { 0, 1, 0, 1 };
  104. double look[4] = { 0, 0, -1, 1 };
  105. unsigned int i;
  106. matrix_t m;
  107. t.set(angle, x, y, z);
  108. n = mQ * t;
  109. n.normalize();
  110. n.getMatrix(m);
  111. matrix.setMatrix(m);
  112. matrix.multiply4d(side, mSide);
  113. matrix.multiply4d(look, mTarget);
  114. matrix.multiply4d(up, mUp);
  115. for (i = 0; i < 3; ++i)
  116. {
  117. mSide[i] += mPos[i];
  118. mTarget[i] += mPos[i];
  119. mUp[i] += mPos[i];
  120. }
  121. mQ = n;
  122. }
  123. void Camera::translate(float x, float y, float z)
  124. {
  125. int i;
  126. double result[4];
  127. double v[4];
  128. matrix_t m;
  129. Matrix matrix;
  130. v[0] = x;
  131. v[1] = y;
  132. v[2] = -z;
  133. v[3] = 1;
  134. m[0] = mSide[0] - mPos[0];
  135. m[1] = mUp[0] - mPos[0];
  136. m[2] = mTarget[0] - mPos[0];
  137. m[3] = 0;
  138. m[4] = mSide[1] - mPos[1];
  139. m[5] = mUp[1] - mPos[1];
  140. m[6] = mTarget[1] - mPos[1];
  141. m[7] = 0;
  142. m[8] = mSide[2] - mPos[2];
  143. m[9] = mUp[2] - mPos[2];
  144. m[10] = mTarget[2] - mPos[2];
  145. m[11] = 0;
  146. m[12] = 0;
  147. m[13] = 0;
  148. m[14] = 0;
  149. m[15] = 1;
  150. matrix.setMatrix(m);
  151. matrix.multiply4d(v, result);
  152. for (i = 0; i < 3; ++i)
  153. {
  154. mSide[i] += result[i];
  155. mUp[i] += result[i];
  156. mTarget[i] += result[i];
  157. mPos[i] += result[i];
  158. }
  159. mPos[0] = x;
  160. mPos[1] = y;
  161. mPos[2] = z;
  162. }
  163. void Camera::reset()
  164. {
  165. mTheta = 0.0;
  166. mTheta2 = 0.0;
  167. mPos[0] = 0.0;
  168. mPos[1] = 0.0;
  169. mPos[2] = 0.0;
  170. mTarget[0] = 0.0;
  171. mTarget[1] = 0.0;
  172. mTarget[2] = mViewDistance;
  173. mSide[0] = 1.0;
  174. mSide[1] = 0.0;
  175. mSide[2] = 0.0;
  176. mUp[0] = 0.0;
  177. mUp[1] = 1.0;
  178. mUp[2] = 0.0;
  179. mQ.setIdentity();
  180. translate(0.0, 0.0, 0.0);
  181. }
  182. void Camera::setSensitivityY(float angle)
  183. {
  184. mRotateDelta2 = helDegToRad(angle);
  185. }
  186. void Camera::setSensitivityX(float angle)
  187. {
  188. mRotateDelta = helDegToRad(angle);
  189. }
  190. ////////
  191. void Camera::command(enum camera_command cmd)
  192. {
  193. switch (cmd)
  194. {
  195. case CAMERA_MOVE_FORWARD:
  196. if (mFlags & Camera_FlyMode)
  197. {
  198. mPos[2] += (mTranslateDelta * cos(mTheta));
  199. }
  200. mPos[0] += (mTranslateDelta * sin(mTheta));
  201. mPos[1] += (mTranslateDelta * sin(mTheta2));
  202. break;
  203. case CAMERA_MOVE_BACKWARD:
  204. if (mFlags & Camera_FlyMode)
  205. {
  206. mPos[2] -= (mTranslateDelta * cos(mTheta));
  207. }
  208. mPos[0] -= (mTranslateDelta * sin(mTheta));
  209. mPos[1] -= (mTranslateDelta * sin(mTheta2));
  210. break;
  211. case CAMERA_MOVE_LEFT:
  212. #ifdef OBSOLETE
  213. //mPos[0] -= (mTranslateDelta * sin(mTheta2+MTK_PI_OVER_4));
  214. mPos[1] -= (mTranslateDelta * cos(mTheta));
  215. if (mFlags & Camera_FlyMode)
  216. {
  217. mPos[2] -= (mTranslateDelta * cos(mTheta2+MTK_PI_OVER_4));
  218. }
  219. #else
  220. mPos[0] -= (mTranslateDelta * sin(mTheta - 90.0f));
  221. mPos[2] -= (mTranslateDelta * cos(mTheta - 90.0f));
  222. #endif
  223. break;
  224. case CAMERA_MOVE_RIGHT:
  225. #ifdef OBSOLETE // I have no idea when it got out of hand
  226. //mPos[0] += (mTranslateDelta * sin(mTheta2+MTK_PI_OVER_4));
  227. mPos[1] += (mTranslateDelta * cos(mTheta));
  228. if (mFlags & Camera_FlyMode)
  229. {
  230. mPos[2] += (mTranslateDelta * cos(mTheta2+MTK_PI_OVER_4));
  231. }
  232. #else
  233. mPos[0] -= (mTranslateDelta * sin(mTheta + 90.0f));
  234. mPos[2] -= (mTranslateDelta * cos(mTheta + 90.0f));
  235. #endif
  236. break;
  237. case CAMERA_ROTATE_UP:
  238. mTheta2 += mRotateDelta2;
  239. rotate(mTheta2, 1.0, 0.0, 0.0);
  240. break;
  241. case CAMERA_ROTATE_DOWN:
  242. mTheta2 -= mRotateDelta2;
  243. rotate(mTheta2, 1.0, 0.0, 0.0);
  244. break;
  245. case CAMERA_ROTATE_RIGHT:
  246. mTheta += mRotateDelta;
  247. rotate(mTheta, 0.0, 1.0, 0.0);
  248. break;
  249. case CAMERA_ROTATE_LEFT:
  250. mTheta -= mRotateDelta;
  251. rotate(mTheta, 0.0, 1.0, 0.0);
  252. break;
  253. case CAMERA_MOVE_UP:
  254. mPos[1] -= mTranslateDelta / 2.0;
  255. mTarget[1] -= mTranslateDelta / 2.0;
  256. break;
  257. case CAMERA_MOVE_DOWN:
  258. mPos[1] += mTranslateDelta / 2.0;
  259. mTarget[1] += mTranslateDelta / 2.0;
  260. break;
  261. case CAMERA_SPEED_UP:
  262. ++mTranslateDelta;
  263. break;
  264. case CAMERA_SPEED_DOWN:
  265. if (--mTranslateDelta < 0.0)
  266. mTranslateDelta = 1.0;
  267. default:
  268. ;
  269. }
  270. }
  271. // FIXME: Mostly invalid for QUAT_CAM (can rotate on XYZ)
  272. bool Camera::isBehind(int x, int z)
  273. {
  274. double bTheta, bCameraX, bCameraZ, Distance;
  275. // Set up a "virtual camera" a huge distance behind us
  276. bTheta = mTheta + HEL_PI;
  277. if (bTheta > HEL_PI)
  278. bTheta -= HEL_2_PI;
  279. // 64k is a fair distance away...
  280. bCameraX = (65536.0 * sin(bTheta)) + mPos[0];
  281. bCameraZ = (65536.0 * cos(bTheta)) + mPos[2];
  282. bCameraX -= x;
  283. bCameraZ -= z;
  284. Distance = sqrt((bCameraX * bCameraX) + (bCameraZ * bCameraZ));
  285. return (Distance < 65536.0);
  286. }
  287. void Camera::setSpeed(float s)
  288. {
  289. mTranslateDelta = s;
  290. }
  291. void Camera::update()
  292. {
  293. mTarget[2] = (mViewDistance * cos(mTheta)) + mPos[2];
  294. mTarget[0] = (mViewDistance * sin(mTheta)) + mPos[0];
  295. mTarget[1] = (mViewDistance * sin(mTheta2)) + mPos[1]; // + height_offset;
  296. }
  297. void Camera::setPosition(vec3_t pos)
  298. {
  299. mPos[0] = pos[0];
  300. mPos[1] = pos[1];
  301. mPos[2] = pos[2];
  302. }
  303. void Camera::setUp(vec3_t up)
  304. {
  305. mUp[0] = up[0];
  306. mUp[1] = up[1];
  307. mUp[2] = up[2];
  308. }
  309. void Camera::setTarget(vec3_t target)
  310. {
  311. mTarget[0] = target[0];
  312. mTarget[1] = target[1];
  313. mTarget[2] = target[2];
  314. }
  315. ////////////////////////////////////////////////////////////
  316. // Private Accessors
  317. ////////////////////////////////////////////////////////////
  318. ////////////////////////////////////////////////////////////
  319. // Private Mutators
  320. ////////////////////////////////////////////////////////////