Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. //
  2. // projection.cpp
  3. // Lab8_TODO
  4. //
  5. // Copyright © 2016 CGIS. All rights reserved.
  6. //
  7.  
  8. #include "projection.h"
  9.  
  10. namespace egc {
  11. mat4 defineViewTransformMatrix(int startX, int startY, int width, int height)
  12. {
  13. mat4 viewTransformMatrix;
  14. mat4 T1 = translate(1.0f, -1.0f, 0.0f);
  15. mat4 M;
  16. M.at(1, 1) = -1.0f;
  17. mat4 S = scale(width / 2, height / 2, 1);
  18. mat4 T2 = translate(startX, startY, 1);
  19.  
  20. viewTransformMatrix = T2 * S * M * T1;
  21. return viewTransformMatrix;
  22. }
  23.  
  24. mat4 defineCameraMatrix(Camera myCamera)
  25. {
  26. mat4 cameraMatrix;
  27.  
  28. vec3 e = myCamera.cameraPosition;
  29. vec3 g = myCamera.cameraTarget - e;
  30. vec3 t = myCamera.cameraUp;
  31.  
  32. vec3 w = g;
  33. w.normalize();
  34.  
  35. vec3 u = crossProduct(t, w);
  36. u.normalize();
  37.  
  38. vec3 v = crossProduct(w, u);
  39.  
  40. mat4 coord;
  41.  
  42. coord.at(0, 0) = u.x;
  43. coord.at(0, 1) = u.y;
  44. coord.at(0, 2) = u.z;
  45.  
  46. coord.at(1, 0) = v.x;
  47. coord.at(1, 1) = v.y;
  48. coord.at(1, 2) = v.z;
  49.  
  50. coord.at(2, 0) = w.x;
  51. coord.at(2, 1) = w.y;
  52. coord.at(2, 2) = w.z;
  53.  
  54. mat4 T = translate(-e);
  55.  
  56. cameraMatrix = coord * T;
  57. return cameraMatrix;
  58. }
  59.  
  60. mat4 definePerspectiveProjectionMatrix(float fov, float aspect, float zNear, float zFar)
  61. {
  62. float tan = tanf(fov/2);
  63. mat4 perspectiveMatrix;
  64. perspectiveMatrix.at(0, 0) /= aspect * tan;
  65. perspectiveMatrix.at(1, 1) /= tan;
  66. perspectiveMatrix.at(2, 2) = (zFar + zNear) / (zNear - zFar);
  67. perspectiveMatrix.at(2, 3) = (2 * zFar * zNear) / (zFar - zNear);
  68. perspectiveMatrix.at(3, 2) = 1;
  69. perspectiveMatrix.at(3, 3) = 0;
  70.  
  71. return perspectiveMatrix;
  72. }
  73.  
  74. void perspectiveDivide(vec4 &inputVertex)
  75. {
  76. inputVertex.x /= inputVertex.w;
  77. inputVertex.y /= inputVertex.w;
  78. inputVertex.z /= inputVertex.w;
  79. inputVertex.w /= inputVertex.w;
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement