Advertisement
Guest User

Untitled

a guest
Mar 1st, 2015
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.22 KB | None | 0 0
  1. #include "../cse452.h"
  2. #include "Camera.h"
  3. #include <cmath>
  4. #include <FL/Fl.H>
  5.  
  6. Camera::Camera()
  7. {
  8. k = nearClippingPlane / (double)farClippingPlane;
  9. theta_w = M_PI / 6.0; //atan(width / (2.0 * nearClippingPlane));
  10. theta_h = M_PI / 6.0; //atan(height / (2.0 * nearClippingPlane));
  11.  
  12. updateD();
  13. updateSxyz();
  14. updateSxy();
  15. updateR();
  16. updateTp();
  17. updateT();
  18. }
  19.  
  20. Camera::~Camera() {
  21. // destroy your data here
  22. }
  23.  
  24. void Camera::updateD() {
  25. // Perspective transformation matrix
  26. D = Matrix4(
  27. Vector4(1, 0, 0, 0),
  28. Vector4(0, 1, 0, 0),
  29. Vector4(0, 0, 1 / (k - 1.0), k / (k - 1.0)),
  30. Vector4(0, 0, -1, 0)
  31. );
  32. }
  33.  
  34. void Camera::updateSxyz() {
  35. // Transforms to box
  36. Sxyz = Matrix4(
  37. Vector4(1 / farClippingPlane, 0, 0, 0),
  38. Vector4(0, 1 / farClippingPlane, 0, 0),
  39. Vector4(0, 0, 1 / farClippingPlane, 0),
  40. Vector4(0, 0, 0, 1)
  41. );
  42.  
  43. Sxyz_inv = Matrix4(
  44. Vector4(farClippingPlane, 0, 0, 0),
  45. Vector4(0, farClippingPlane, 0, 0),
  46. Vector4(0, 0, farClippingPlane, 0),
  47. Vector4(0, 0, 0, 1)
  48. );
  49.  
  50. if (!((Sxyz * Sxyz_inv).approxEqual(Matrix4::identity()))) {
  51. std::cout << "Sxyz inverse is incorrect." << std::endl;
  52. }
  53. }
  54.  
  55. void Camera::updateSxy() {
  56. // Scaling matrix for the x, y worldview to camera
  57. Sxy = Matrix4(
  58. Vector4(1 / tan(theta_w / 2.0), 0, 0, 0),
  59. Vector4(0, 1 / tan(theta_h / 2.0), 0, 0),
  60. Vector4(0, 0, 1, 0),
  61. Vector4(0, 0, 0, 1)
  62. );
  63.  
  64. Sxy_inv = Matrix4(
  65. Vector4(tan(theta_w / 2.0), 0, 0, 0),
  66. Vector4(0, tan(theta_h / 2.0), 0, 0),
  67. Vector4(0, 0, 1, 0),
  68. Vector4(0, 0, 0, 1)
  69. );
  70.  
  71. if (!((Sxy * Sxy_inv).approxEqual(Matrix4::identity()))) {
  72. std::cout << "Sxy inverse is incorrect." << std::endl;
  73. }
  74. }
  75.  
  76. void Camera::updateR() {
  77. // Rotation
  78. R = Matrix4(
  79. Vector4(-u[0], -u[1], -u[2], 0),
  80. Vector4(v[0], v[1], v[2], 0),
  81. Vector4(-n[0],- n[1], -n[2], 0),
  82. Vector4(0, 0, 0, 1)
  83. );
  84.  
  85. //R_inv = R.transpose();
  86. R_inv = Matrix4(
  87. Vector4(-u[0], v[0], -n[0], 0),
  88. Vector4(-u[1], v[1], -n[1], 0),
  89. Vector4(-u[2], v[2], -n[2], 0),
  90. Vector4(0, 0, 0, 1));
  91.  
  92. if (!(R_inv == R.transpose())) {
  93. std::cout << "R inverse is incorrect." << std::endl;
  94. }
  95. }
  96.  
  97. void Camera::updateTp() {
  98. // Transformation matrix for position of camera
  99. Tp = Matrix4(
  100. Vector4(0, 0, 0, eye[0]),
  101. Vector4(0, 0, 0, eye[1]),
  102. Vector4(0, 0, 0, eye[2]),
  103. Vector4(0, 0, 0, 1)
  104. );
  105. }
  106.  
  107. void Camera::updateT() {
  108. // Translation matrix
  109. T = Matrix4(
  110. Vector4(1, 0, 0, -eye[0]),
  111. Vector4(0, 1, 0, -eye[1]),
  112. Vector4(0, 0, 1, -eye[2]),
  113. Vector4(0, 0, 0, 1)
  114. );
  115.  
  116. T_inv = Matrix4(
  117. Vector4(1, 0, 0, eye[0]),
  118. Vector4(0, 1, 0, eye[1]),
  119. Vector4(0, 0, 1, eye[2]),
  120. Vector4(0, 0, 0, 1)
  121. );
  122.  
  123. if (!((T * T_inv).approxEqual(Matrix4::identity()))) {
  124. std::cout << "T inverse is incorrect." << std::endl;
  125. }
  126. }
  127.  
  128. // The following three should be unit length and orthogonal to each other
  129. // u vector
  130. Vector3 Camera::getRight() const
  131. {
  132. return u;
  133. }
  134.  
  135. // v vector
  136. Vector3 Camera::getUp() const
  137. {
  138. return v;
  139. }
  140.  
  141. // - n vector
  142. Vector3 Camera::getLook() const
  143. {
  144. return -n;
  145. }
  146.  
  147. double Camera::getSkew() const
  148. {
  149. // Change this to implement the extra credit
  150. return skew;
  151. }
  152.  
  153. double Camera::getAspectRatioScale() const
  154. {
  155. // Change this to implement the extra credit
  156. return aspectRatioScale;
  157. }
  158.  
  159. Point3 Camera::getProjectionCenter() const
  160. {
  161. // Change this to implement the extra credit
  162. return projCenter;
  163. }
  164.  
  165. Matrix4 Camera::getProjection() const
  166. {
  167. // return the current projection and scale matrix
  168. return D*Sxyz;
  169. }
  170.  
  171. Matrix4 Camera::getWorldToCamera() const
  172. {
  173. return R*T;
  174. }
  175.  
  176. Matrix4 Camera::getRotationFromXYZ() const
  177. {
  178. // return just the rotation matrix
  179. return R; // world -> cam
  180. }
  181.  
  182. Matrix4 Camera::getRotationToXYZ() const
  183. {
  184. // return just the rotation matrix
  185. return R_inv; // cam -> world
  186. }
  187.  
  188. Matrix4 Camera::getCameraToWorld() const
  189. {
  190. // return the current camera to world matrix
  191. // This is the inverse of the rotation, translation, and scale
  192. return Sxyz_inv * Sxy_inv * R_inv * T_inv;
  193. }
  194.  
  195. int Camera::getWidth() const
  196. {
  197. // return the current image width
  198. return width;
  199. }
  200.  
  201. int Camera::getHeight() const
  202. {
  203. // return the current image height
  204. return height;
  205. }
  206.  
  207. Point3 Camera::getEye() const
  208. {
  209. // return the current eye location
  210. return eye;
  211. }
  212.  
  213. double Camera::getZoom() const
  214. {
  215. return theta_h;
  216. }
  217.  
  218. void Camera::setFrom(const Point3& from)
  219. {
  220. // set the current viewpoint (eye point)
  221. eye = from;
  222. updateT();
  223. }
  224.  
  225. void Camera::setAt(const Point3& at)
  226. {
  227. // set the point the camera is looking at
  228. // calling this requires that the from (or eye) point already be valid
  229. look = eye - at;
  230. n = -look;
  231. n.normalize();
  232.  
  233. v = v - ((v * -n) / (-n * -n))*-n;
  234.  
  235. u = cross(v, n);
  236. u.normalize();
  237. updateR();
  238. }
  239.  
  240. void Camera::setLook(const Vector3& l)
  241. {
  242. // set the direction the camera is looking at
  243. look = l;
  244. look.normalize();
  245. n = -look;
  246. v = v - ((v*look) / (look*look))*look;
  247. u = v^n;
  248. u.normalize();
  249. v.normalize();
  250. n.normalize();
  251. updateR();
  252. }
  253.  
  254. void Camera::setUp(const Vector3& up)
  255. {
  256. // set the upwards direction of the camera
  257. v = up;
  258. v.normalize();
  259. look = look - ((look*v) / (v*v)) *v;
  260. n = -look;
  261. u = v^n;
  262. u.normalize();
  263. v.normalize();
  264. n.normalize();
  265. updateR();
  266. }
  267.  
  268. void Camera::setWidthHeight(int w, int h)
  269. {
  270. // set the current width and height of the film plane
  271. width = w;
  272. height = h;
  273. theta_w = 2.0 * atan(width / (2.0 * nearClippingPlane));
  274. theta_h = 2.0 * atan(height / (2.0 * nearClippingPlane));
  275.  
  276. updateSxy();
  277. }
  278.  
  279. void Camera::setZoom(double z)
  280. {
  281. // set field of view (specified in degrees)
  282. theta_h = (2.0*M_PI*z)/360;
  283. theta_w = 2.0*atan(tan(theta_h / 2.0)*(width / height));
  284. updateSxy();
  285. }
  286.  
  287. void Camera::setNearFar(double n, double f)
  288. {
  289. // set the near and far clipping planes
  290. nearClippingPlane = n;
  291. farClippingPlane = f;
  292. k = n / f;
  293. theta_w = 2.0 * atan(width / (2.0 * nearClippingPlane));
  294. theta_h = 2.0 * atan(height / (2.0 * nearClippingPlane));
  295. updateSxyz();
  296. updateD();
  297. updateSxy();
  298. }
  299.  
  300. void Camera::setSkew( double d )
  301. {
  302. skew = d;
  303. }
  304.  
  305. void Camera::setAspectRatioScale( double d )
  306. {
  307. aspectRatioScale = d;
  308. }
  309.  
  310. void Camera::setProjectionCenter( const Point3 &p )
  311. {
  312. projCenter = p;
  313. }
  314.  
  315. void Camera::moveForward(double dist) {
  316. // move the camera forward (in the viewing direction)
  317. // by the amount dist
  318. eye = eye + n*dist;
  319. updateTp();
  320. updateT();
  321. }
  322.  
  323. void Camera::moveSideways(double dist) {
  324. // move the camera sideways (orthogonal to the viewing direction)
  325. // by the amount dist
  326. eye = eye + -getRight()*dist;
  327. updateTp();
  328. updateT();
  329. }
  330.  
  331. void Camera::moveVertical(double dist) {
  332. // move the camera vertically (along the up vector)
  333. // by the amount dist
  334. eye = eye + getUp()*dist;
  335. updateTp();
  336. updateT();
  337. }
  338.  
  339. void Camera::rotateYaw(double angle) {
  340. // rotate the camera left/right (around the up vector)
  341. look = look*cos(angle) - getRight()*sin(angle);
  342. look.normalize();
  343. n = -look;
  344. n.normalize();
  345. u = v^n;
  346. u.normalize();
  347. updateTp();
  348. updateT();
  349. updateR();
  350. }
  351.  
  352. void Camera::rotatePitch(double angle) {
  353. // rotate the camera up/down (pitch angle)
  354. look = look*cos(angle) - getUp()*sin(angle);
  355. look.normalize();
  356. n = -look;
  357. n.normalize();
  358. u = v^n;
  359. u.normalize();
  360. updateTp();
  361. updateT();
  362. updateR();
  363. }
  364.  
  365. void Camera::rotateAroundAtPoint(int axis, double angle, double focusDist)
  366. {
  367.  
  368. }
  369.  
  370.  
  371. void Camera::moveKeyboard( )
  372. {
  373. // you may change key controls for the interactive
  374. // camera controls here, make sure you document your changes
  375. // in your README file
  376.  
  377. if (Fl::event_key('w'))
  378. moveForward(+0.05);
  379. if (Fl::event_key('s'))
  380. moveForward(-0.05);
  381. if (Fl::event_key('a'))
  382. moveSideways(-0.05);
  383. if (Fl::event_key('d'))
  384. moveSideways(+0.05);
  385. if (Fl::event_key(FL_Up))
  386. moveVertical(+0.05);
  387. if (Fl::event_key(FL_Down))
  388. moveVertical(-0.05);
  389. if (Fl::event_key(FL_Left))
  390. rotateYaw(+0.05);
  391. if (Fl::event_key(FL_Right))
  392. rotateYaw(-0.05);
  393. if (Fl::event_key(FL_Page_Up))
  394. rotatePitch(+0.05);
  395. if (Fl::event_key(FL_Page_Down))
  396. rotatePitch(-0.05);
  397. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement