Open Source Tomb Raider Engine
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Camera.cpp 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 <stdio.h>
  35. #include <math.h>
  36. #include <MatMath.h>
  37. #include <Camera.h>
  38. #ifdef DEBUG_MEMORY
  39. #include <memory_test.h>
  40. #endif
  41. unsigned int Camera::mCounter = 0;
  42. ////////////////////////////////////////////////////////////
  43. // Constructors
  44. ////////////////////////////////////////////////////////////
  45. Camera::Camera()
  46. {
  47. mId = ++mCounter;
  48. mFlags = 0;
  49. mViewDistance = 14.0;
  50. mTranslateDelta = 256.0;
  51. mRotateDelta = HEL_DEG_TO_RAD(15.0);
  52. mRotateDelta2 = HEL_DEG_TO_RAD(5.0);
  53. mFlags &= Camera_FlyMode;
  54. reset();
  55. }
  56. Camera::~Camera()
  57. {
  58. }
  59. ////////////////////////////////////////////////////////////
  60. // Public Accessors
  61. ////////////////////////////////////////////////////////////
  62. unsigned int Camera::getId()
  63. {
  64. return mId;
  65. }
  66. void Camera::getPosition(vec3_t pos)
  67. {
  68. pos[0] = mPos[0];
  69. pos[1] = mPos[1];
  70. pos[2] = mPos[2];
  71. }
  72. void Camera::getUp(vec3_t up)
  73. {
  74. up[0] = mUp[0];
  75. up[1] = mUp[1];
  76. up[2] = mUp[2];
  77. }
  78. void Camera::getTarget(vec3_t target)
  79. {
  80. target[0] = mTarget[0];
  81. target[1] = mTarget[1];
  82. target[2] = mTarget[2];
  83. }
  84. float Camera::getYaw()
  85. {
  86. return HEL_RAD_TO_DEG(mTheta);
  87. }
  88. double Camera::getRadianYaw()
  89. {
  90. return mTheta;
  91. }
  92. double Camera::getRadianPitch()
  93. {
  94. return mTheta2;
  95. }
  96. ////////////////////////////////////////////////////////////
  97. // Public Mutators
  98. ////////////////////////////////////////////////////////////
  99. void Camera::rotate(float angle, float x, float y, float z)
  100. {
  101. Quaternion t, n;
  102. Matrix matrix;
  103. double side[4] = { 1, 0, 0, 1 };
  104. double up[4] = { 0, 1, 0, 1 };
  105. double look[4] = { 0, 0, -1, 1 };
  106. unsigned int i;
  107. matrix_t m;
  108. t.set(angle, x, y, z);
  109. n = mQ * t;
  110. n.normalize();
  111. n.getMatrix(m);
  112. matrix.setMatrix(m);
  113. matrix.multiply4d(side, mSide);
  114. matrix.multiply4d(look, mTarget);
  115. matrix.multiply4d(up, mUp);
  116. for (i = 0; i < 3; ++i)
  117. {
  118. mSide[i] += mPos[i];
  119. mTarget[i] += mPos[i];
  120. mUp[i] += mPos[i];
  121. }
  122. mQ = n;
  123. }
  124. void Camera::translate(float x, float y, float z)
  125. {
  126. int i;
  127. double result[4];
  128. double v[4];
  129. matrix_t m;
  130. Matrix matrix;
  131. v[0] = x;
  132. v[1] = y;
  133. v[2] = -z;
  134. v[3] = 1;
  135. m[0] = mSide[0] - mPos[0];
  136. m[1] = mUp[0] - mPos[0];
  137. m[2] = mTarget[0] - mPos[0];
  138. m[3] = 0;
  139. m[4] = mSide[1] - mPos[1];
  140. m[5] = mUp[1] - mPos[1];
  141. m[6] = mTarget[1] - mPos[1];
  142. m[7] = 0;
  143. m[8] = mSide[2] - mPos[2];
  144. m[9] = mUp[2] - mPos[2];
  145. m[10] = mTarget[2] - mPos[2];
  146. m[11] = 0;
  147. m[12] = 0;
  148. m[13] = 0;
  149. m[14] = 0;
  150. m[15] = 1;
  151. matrix.setMatrix(m);
  152. matrix.multiply4d(v, result);
  153. for (i = 0; i < 3; ++i)
  154. {
  155. mSide[i] += result[i];
  156. mUp[i] += result[i];
  157. mTarget[i] += result[i];
  158. mPos[i] += result[i];
  159. }
  160. mPos[0] = x;
  161. mPos[1] = y;
  162. mPos[2] = z;
  163. }
  164. void Camera::reset()
  165. {
  166. mTheta = 0.0;
  167. mTheta2 = 0.0;
  168. mPos[0] = 0.0;
  169. mPos[1] = 0.0;
  170. mPos[2] = 0.0;
  171. mTarget[0] = 0.0;
  172. mTarget[1] = 0.0;
  173. mTarget[2] = mViewDistance;
  174. mSide[0] = 1.0;
  175. mSide[1] = 0.0;
  176. mSide[2] = 0.0;
  177. mUp[0] = 0.0;
  178. mUp[1] = 1.0;
  179. mUp[2] = 0.0;
  180. mQ.setIdentity();
  181. translate(0.0, 0.0, 0.0);
  182. }
  183. void Camera::setSensitivityY(float angle)
  184. {
  185. mRotateDelta2 = HEL_DEG_TO_RAD(angle);
  186. }
  187. void Camera::setSensitivityX(float angle)
  188. {
  189. mRotateDelta = HEL_DEG_TO_RAD(angle);
  190. }
  191. ////////
  192. void Camera::command(enum camera_command cmd)
  193. {
  194. switch (cmd)
  195. {
  196. case CAMERA_MOVE_FORWARD:
  197. if (mFlags & Camera_FlyMode)
  198. {
  199. mPos[2] += (mTranslateDelta * cos(mTheta));
  200. }
  201. mPos[0] += (mTranslateDelta * sin(mTheta));
  202. mPos[1] += (mTranslateDelta * sin(mTheta2));
  203. break;
  204. case CAMERA_MOVE_BACKWARD:
  205. if (mFlags & Camera_FlyMode)
  206. {
  207. mPos[2] -= (mTranslateDelta * cos(mTheta));
  208. }
  209. mPos[0] -= (mTranslateDelta * sin(mTheta));
  210. mPos[1] -= (mTranslateDelta * sin(mTheta2));
  211. break;
  212. case CAMERA_MOVE_LEFT:
  213. mPos[0] -= (mTranslateDelta * sin(mTheta - 90.0f));
  214. mPos[2] -= (mTranslateDelta * cos(mTheta - 90.0f));
  215. break;
  216. case CAMERA_MOVE_RIGHT:
  217. mPos[0] -= (mTranslateDelta * sin(mTheta + 90.0f));
  218. mPos[2] -= (mTranslateDelta * cos(mTheta + 90.0f));
  219. break;
  220. case CAMERA_ROTATE_UP:
  221. if (mTheta2 < (M_PI / 2)) {
  222. mTheta2 += mRotateDelta2;
  223. rotate(mTheta2, 1.0, 0.0, 0.0);
  224. }
  225. break;
  226. case CAMERA_ROTATE_DOWN:
  227. if (mTheta2 > -(M_PI / 2)) {
  228. mTheta2 -= mRotateDelta2;
  229. rotate(mTheta2, 1.0, 0.0, 0.0);
  230. }
  231. break;
  232. case CAMERA_ROTATE_RIGHT:
  233. mTheta += mRotateDelta;
  234. rotate(mTheta, 0.0, 1.0, 0.0);
  235. break;
  236. case CAMERA_ROTATE_LEFT:
  237. mTheta -= mRotateDelta;
  238. rotate(mTheta, 0.0, 1.0, 0.0);
  239. break;
  240. case CAMERA_MOVE_UP:
  241. mPos[1] -= mTranslateDelta / 2.0;
  242. mTarget[1] -= mTranslateDelta / 2.0;
  243. break;
  244. case CAMERA_MOVE_DOWN:
  245. mPos[1] += mTranslateDelta / 2.0;
  246. mTarget[1] += mTranslateDelta / 2.0;
  247. break;
  248. case CAMERA_SPEED_UP:
  249. ++mTranslateDelta;
  250. break;
  251. case CAMERA_SPEED_DOWN:
  252. if (--mTranslateDelta < 0.0)
  253. mTranslateDelta = 1.0;
  254. break;
  255. default:
  256. ;
  257. }
  258. }
  259. //! \fixme Mostly invalid for QUAT_CAM (can rotate on XYZ)
  260. bool Camera::isBehind(int x, int z)
  261. {
  262. double bTheta, bCameraX, bCameraZ, Distance;
  263. // Set up a "virtual camera" a huge distance behind us
  264. bTheta = mTheta + HEL_PI;
  265. if (bTheta > HEL_PI)
  266. bTheta -= HEL_2_PI;
  267. // 64k is a fair distance away...
  268. bCameraX = (65536.0 * sin(bTheta)) + mPos[0];
  269. bCameraZ = (65536.0 * cos(bTheta)) + mPos[2];
  270. bCameraX -= x;
  271. bCameraZ -= z;
  272. Distance = sqrt((bCameraX * bCameraX) + (bCameraZ * bCameraZ));
  273. return (Distance < 65536.0);
  274. }
  275. void Camera::setSpeed(float s)
  276. {
  277. mTranslateDelta = s;
  278. }
  279. void Camera::update()
  280. {
  281. mTarget[2] = (mViewDistance * cos(mTheta)) + mPos[2];
  282. mTarget[0] = (mViewDistance * sin(mTheta)) + mPos[0];
  283. mTarget[1] = (mViewDistance * sin(mTheta2)) + mPos[1]; // + height_offset;
  284. }
  285. void Camera::setPosition(vec3_t pos)
  286. {
  287. mPos[0] = pos[0];
  288. mPos[1] = pos[1];
  289. mPos[2] = pos[2];
  290. }
  291. void Camera::setUp(vec3_t up)
  292. {
  293. mUp[0] = up[0];
  294. mUp[1] = up[1];
  295. mUp[2] = up[2];
  296. }
  297. void Camera::setTarget(vec3_t target)
  298. {
  299. mTarget[0] = target[0];
  300. mTarget[1] = target[1];
  301. mTarget[2] = target[2];
  302. }
  303. ////////////////////////////////////////////////////////////
  304. // Private Accessors
  305. ////////////////////////////////////////////////////////////
  306. ////////////////////////////////////////////////////////////
  307. // Private Mutators
  308. ////////////////////////////////////////////////////////////