tterrag1098

Untitled

Oct 11th, 2019
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.52 KB | None | 0 0
  1. package render;
  2.  
  3. import org.joml.Matrix4f;
  4. import org.joml.Vector3f;
  5. import org.joml.Vector4f;
  6.  
  7. import lombok.Getter;
  8. import lombok.Setter;
  9.  
  10. @Getter
  11. public class YPRCamera {
  12.  
  13. private @Setter Vector3f position = new Vector3f(); /// < Camera position.
  14. private Vector3f view = new Vector3f(0, 0, -1); /// < Direction of the line of sight.
  15. private Vector3f up = new Vector3f(0, 1, 0); /// < Direction of up.
  16.  
  17. /**
  18. * Sets the position, view and up for the camera.
  19. *
  20. * The method will also normalize the view and up vectors.
  21. *
  22. * @param x
  23. * X coordinate to the camera position.
  24. * @param y
  25. * Y coordinate to the camera position.
  26. * @param z
  27. * Z coordinate to the camera position.
  28. * @param vX
  29. * X coordinate to the viewing vector.
  30. * @param vY
  31. * Y coordinate to the viewing vector.
  32. * @param vZ
  33. * Z coordinate to the viewing vector.
  34. * @param upX
  35. * X coordinate to the up vector.
  36. * @param upY
  37. * Y coordinate to the up vector.
  38. * @param upZ
  39. * Z coordinate to the up vector.
  40. */
  41. public void positionCamera(float x, float y, float z, float vX, float vY, float vZ, float upX, float upY, float upZ) {
  42. position.set(x, y, z);
  43. view.set(vX, vY, vZ);
  44. up.set(upX, upY, upZ);
  45.  
  46. up.normalize();
  47. view.normalize();
  48. }
  49.  
  50. /**
  51. * Sets the position, view and up for the camera.
  52. *
  53. * The method will also normalize the view and up vectors.
  54. *
  55. * @param pos
  56. * Position vector.
  57. * @param view
  58. * View vector.
  59. * @param up
  60. * Up vector.
  61. */
  62. void positionCamera(Vector3f pos, Vector3f view, Vector3f up) {
  63. this.position = pos;
  64. this.view = view;
  65. this.up = up;
  66.  
  67. up.normalize();
  68. view.normalize();
  69. }
  70.  
  71. /**
  72. * Sets the position for the camera.
  73. *
  74. * @param x
  75. * X coordinate to the camera position.
  76. * @param y
  77. * Y coordinate to the camera position.
  78. * @param z
  79. * Z coordinate to the camera position.
  80. */
  81. void setPosition(float x, float y, float z) {
  82. position.x = x;
  83. position.y = y;
  84. position.z = z;
  85. }
  86.  
  87. /**
  88. * Sets the view vector for the camera.
  89. *
  90. * The method will also normalize the view vector.
  91. *
  92. * @param x
  93. * X coordinate to the viewing vector.
  94. * @param y
  95. * Y coordinate to the viewing vector.
  96. * @param z
  97. * Z coordinate to the viewing vector.
  98. */
  99. void setView(float x, float y, float z) {
  100. view.x = x;
  101. view.y = y;
  102. view.z = z;
  103. view.normalize();
  104. }
  105.  
  106. /**
  107. * Sets the view vector for the camera.
  108. *
  109. * The method will also normalize the view vector.
  110. *
  111. * @param view
  112. * View vector.
  113. */
  114. void setView(Vector3f view) {
  115. this.view = view;
  116. view.normalize();
  117. }
  118.  
  119. /**
  120. * Sets the up vector for the camera.
  121. *
  122. * The method will also normalize the up vector.
  123. *
  124. * @param x
  125. * X coordinate to the up vector.
  126. * @param y
  127. * Y coordinate to the up vector.
  128. * @param z
  129. * Z coordinate to the up vector.
  130. */
  131. void setUp(float x, float y, float z) {
  132. up.x = x;
  133. up.y = y;
  134. up.z = z;
  135. up.normalize();
  136. }
  137.  
  138. /**
  139. * Sets the up vector for the camera.
  140. *
  141. * The method will also normalize the up vector.
  142. *
  143. * @param up
  144. * Up vector.
  145. */
  146. void setUp(Vector3f up) {
  147. this.up = up;
  148. up.normalize();
  149. }
  150.  
  151. /**
  152. * Sets the up vector for the camera in the positive y direction.
  153. *
  154. * This method will also normalize the up vector and keep it orthogonal to the viewing vector.
  155. *
  156. */
  157. void setUpViaY() {
  158. Vector3f Y = new Vector3f(0, 1, 0);
  159. Vector3f V = view;
  160. Vector3f P = V.mul(Y.dot(V) / V.lengthSquared(), new Vector3f());
  161. up = Y.sub(P).normalize();
  162. }
  163.  
  164. /**
  165. * Adds to the current pitch of the camera.
  166. *
  167. * @param num
  168. * The degree measurement of the amount to add to the pitch of the camera.
  169. */
  170. public void addPitch(float num) {
  171. // Calculate the right vector.
  172. Vector3f mn = view.cross(up, new Vector3f());
  173.  
  174. // Create the rotation matrix for rotation around the right vector by an angle of num.
  175. Matrix4f rot = new Matrix4f().rotate((float) Math.toRadians(num), mn);
  176.  
  177. // Convert the view and up vectors to 4D.
  178. Vector4f View4 = new Vector4f(view, 1);
  179. Vector4f Up4 = new Vector4f(up, 1);
  180.  
  181. // Rotate the view and up vectors.
  182. View4.mul(rot);
  183. Up4.mul(rot);
  184.  
  185. // Convert the transformed view and up vectors to 3D.
  186. up = new Vector3f(Up4.x, Up4.y, Up4.z).normalize();
  187. view = new Vector3f(View4.x, View4.y, View4.z).normalize();
  188. }
  189.  
  190. /**
  191. * Adds to the current yaw of the camera.
  192. *
  193. * @param num
  194. * The degree measurement of the amount to add to the yaw of the camera.
  195. */
  196. public void addYaw(float num) {
  197. // Create the rotation matrix for rotation around the up vector by an angle of num.
  198. Matrix4f rot = new Matrix4f().rotate((float) Math.toRadians(num), up);
  199.  
  200. // Convert the view vector to 4D.
  201. Vector4f View4 = new Vector4f(view, 1);
  202.  
  203. // Rotate the view vector.
  204. View4.mul(rot);
  205.  
  206. // Convert the transformed view vector to 3D.
  207. view = new Vector3f(View4.x, View4.y, View4.z).normalize();
  208. }
  209.  
  210. /**
  211. * Adds to the current roll of the camera.
  212. *
  213. * @param num
  214. * The degree measurement of the amount to add to the roll of the camera.
  215. */
  216. public void addRoll(float num) {
  217. // Create the rotation matrix for rotation around the view vector by an angle of num.
  218. Matrix4f rot = new Matrix4f().rotate((float) Math.toRadians(num), view);
  219.  
  220. // Convert the up vector to 4D.
  221. Vector4f Up4 = new Vector4f(up, 1);
  222.  
  223. // Rotate the up vector.
  224. Up4.mul(rot);
  225.  
  226. // Convert the transformed up vector to 3D.
  227. up = new Vector3f(Up4.x, Up4.y, Up4.z).normalize();
  228. }
  229.  
  230. /**
  231. * @return The lookat matrix for the current position, line of sight, and up vectors.
  232. */
  233. Matrix4f lookAt() {
  234. return new Matrix4f().lookAt(position, position.add(view, new Vector3f()), up);
  235. }
  236.  
  237. /**
  238. * Moves the camera forward.
  239. *
  240. * @param num
  241. * Amount to move the camera forward.
  242. */
  243. public void moveForward(float num) {
  244. float mod = view.lengthSquared();
  245. if (mod < 0.0001f)
  246. return;
  247.  
  248. position.add(view.div(mod, new Vector3f()).mul(num));
  249. }
  250.  
  251. /**
  252. * Moves the camera right.
  253. *
  254. * @param num
  255. * Amount to move the camera to the right.
  256. */
  257. public void moveRight(float num) {
  258. Vector3f mn = view.cross(up, new Vector3f());
  259.  
  260. float mod = mn.lengthSquared();
  261. if (mod < 0.0001f)
  262. return;
  263.  
  264. position.add(mn.div(mod).mul(num));
  265. }
  266.  
  267. /**
  268. * Moves the camera up.
  269. *
  270. * @param num
  271. * Amount to move the camera up.
  272. */
  273. public void moveUp(float num) {
  274. float mod = up.lengthSquared();
  275. if (mod < 0.0001f)
  276. return;
  277.  
  278. position.add(up.div(mod, new Vector3f()).mul(num));
  279. }
  280. }
Advertisement
Add Comment
Please, Sign In to add comment