Advertisement
DuskFall

Code

Nov 22nd, 2017
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. Transform class
  2.  
  3. package model;
  4.  
  5. import cameras.Camera;
  6. import cameras.CameraFP;
  7. import math.Matrix4f;
  8. import math.Vector3f;
  9.  
  10. public class Transform
  11. {
  12. private static Camera camera;
  13. private static float zNear;
  14. private static float zFar;
  15. private static float width;
  16. private static float height;
  17. private static float fov;
  18.  
  19. private Vector3f translation;
  20. private Vector3f rotation;
  21. private Vector3f scale;
  22.  
  23. public Transform()
  24. {
  25. setTranslation(new Vector3f(0,0,0));
  26. setRotation(new Vector3f(0,0,0));
  27. setScale(new Vector3f(1,1,1));
  28. }
  29.  
  30. public Matrix4f getTransformation()
  31. {
  32. Matrix4f translationMatrix = new Matrix4f().setTranslation(translation.getX(), translation.getY(), translation.getZ());
  33. Matrix4f rotationMatrix = new Matrix4f().setRotation(rotation.getX(), rotation.getY(), rotation.getZ());
  34. Matrix4f scaleMatrix = new Matrix4f().setScale(scale.getX(), scale.getY(), scale.getZ());
  35.  
  36. return translationMatrix.multiply(rotationMatrix.multiply(scaleMatrix));
  37. }
  38.  
  39. public static void setProjection(float fov, float width,float height, float zNear, float zFar)
  40. {
  41. Transform.fov = fov;
  42. Transform.width = width;
  43. Transform.height = height;
  44. Transform.zNear = zNear;
  45. Transform.zFar = zFar;
  46. }
  47. public Matrix4f getProjectedTransformation()
  48. {
  49. Matrix4f transformationMatrix = getTransformation();
  50. Matrix4f projectionMatrix = new Matrix4f().setProjection(fov, width, height, zNear, zFar);
  51.  
  52. Matrix4f cameraRotation = new Matrix4f().setCamera(camera.getForward(), camera.getUp());
  53. Matrix4f cameraTranslation = new Matrix4f().setTranslation(-camera.getPosition().getX(), -camera.getPosition().getY(), -camera.getPosition().getZ());
  54.  
  55. return projectionMatrix.multiply(cameraRotation.multiply(cameraTranslation.multiply(transformationMatrix)));
  56.  
  57. }
  58.  
  59. public Vector3f getTranslation()
  60. {
  61. return translation;
  62. }
  63.  
  64. public void setTranslation(Vector3f translation)
  65. {
  66. this.translation = translation;
  67. }
  68. public void setTranslation(float x, float y, float z)
  69. {
  70. this.translation = new Vector3f(x, y, z);
  71. }
  72.  
  73. public Vector3f getRotation() {
  74. return rotation;
  75. }
  76.  
  77. public void setRotation(Vector3f rotation) {
  78. this.rotation = rotation;
  79. }
  80.  
  81. public void setRotation(float x, float y, float z)
  82. {
  83. this.rotation = new Vector3f(x, y, z);
  84. }
  85.  
  86. public void setScale(float x, float y, float z)
  87. {
  88. this.scale = new Vector3f(x, y, z);
  89. }
  90.  
  91. public Vector3f getScale() {
  92. return scale;
  93. }
  94.  
  95. public void setScale(Vector3f scale) {
  96. this.scale = scale;
  97. }
  98.  
  99. public static Camera getCamera() {
  100. return camera;
  101. }
  102.  
  103. public void setCamera(Camera camera) {
  104. Transform.camera = camera;
  105. }
  106. }
  107.  
  108. Shader bind and update methods
  109.  
  110. InterfaceShader shader = InterfaceShader.getInstance();
  111. shader.bind();
  112. shader.setColour(this.getActingColour());
  113. shader.setLocation(this.getLocation());
  114. shader.setSize(this.getBounds());
  115. shader.setGradient(this.getShouldGradient());
  116. Transform t = new Transform();
  117. t.setTranslation(this.location.getX(),this.location.getY(),this.getZDepth());
  118. shader.setViewMatrix(t.getProjectedTransformation());
  119. shader.updateUniforms();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement