Advertisement
Guest User

Untitled

a guest
Sep 19th, 2020
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.48 KB | None | 0 0
  1. package com.julian.ogl.main;
  2.  
  3. import org.joml.Matrix4f;
  4. import org.joml.Vector3f;
  5.  
  6. public class Camera {
  7.    
  8.     enum Camera_Movement{
  9.         FORWARD,
  10.         BACKWARD,
  11.         LEFT,
  12.         RIGHT
  13.     };
  14.    
  15.     //defaults
  16.     Vector3f POSITION = new Vector3f(0.0f, 0.0f, 0.0f);
  17.     Vector3f UP = new Vector3f(0.0f, 1.0f, 0.0f);
  18.     Vector3f FRONT = new Vector3f(0.0f, 0.0f, -1.0f);
  19.     float YAW = -90.0f;
  20.     float PITCH = 0.0f;
  21.     float SPEED = 2.5f;
  22.     float SENSITIVITY = 0.1f;
  23.     float ZOOM = 45.0f;
  24.    
  25.     //attributes
  26.     Vector3f Position;
  27.     Vector3f Front;
  28.     Vector3f Up;
  29.     Vector3f Right;
  30.     Vector3f WorldUp;
  31.    
  32.     //euler angles
  33.     float Yaw;
  34.     float Pitch;
  35.    
  36.     //camera options
  37.     float MovementSpeed;
  38.     float MouseSensitivity;
  39.     float Zoom;
  40.    
  41.     private void updateCameraVectors() {
  42.         Vector3f front = new Vector3f(0.0f, 0.0f, 0.0f);
  43.         front.x = (float) (Math.cos(Math.toRadians(Yaw) * Math.cos(Math.toRadians(Pitch))));
  44.         front.y = (float) Math.sin(Math.toRadians(Pitch));
  45.         front.z = (float) (Math.sin(Math.toRadians(Yaw)) * Math.cos(Math.toRadians(Pitch)));
  46.         Front = Front.normalize();
  47.        
  48.         Vector3f fron = Front;
  49.         Vector3f wup = WorldUp;
  50.        
  51.         Right = fron.cross(wup).normalize();
  52.         Vector3f r = Right;
  53.         Up = r.cross(fron).normalize();
  54.     }
  55.    
  56.     //print Vector data only added because this issue
  57.     public void printVectorData() {
  58.         System.out.println("Position: " + Position);
  59.         System.out.println("Front: " + Front);
  60.         System.out.println("Up: " + Up);
  61.         System.out.println("Right: " + Right);
  62.         System.out.println("WorldUp: " + WorldUp);
  63.     }
  64.    
  65.     public Matrix4f getViewMatrix() {
  66.         Matrix4f viewMatrix = new Matrix4f();
  67.         Vector3f pos = Position;
  68.         Vector3f up = Up;
  69.         Vector3f front = Front;
  70.         viewMatrix = viewMatrix.lookAt(pos, pos.add(front), up);
  71.         System.out.println(Position);
  72.         return viewMatrix;
  73.     }
  74.    
  75.     public void ProcessMouseScroll(float yoffset) {
  76.         Zoom -= yoffset;
  77.         if(Zoom < 1.0f)
  78.             Zoom = 1.0f;
  79.         if(Zoom > 45.0f)
  80.             Zoom = 45.0f;
  81.     }
  82.    
  83.     public void ProcessMouseMovement(float xoffset, float yoffset, boolean constrainPitch) {
  84.         xoffset *= MouseSensitivity;
  85.         yoffset *= MouseSensitivity;
  86.        
  87.         Yaw += xoffset;
  88.         Pitch += yoffset;
  89.        
  90.         //make sure screen doesnt get flipped when out of bounds
  91.         if(constrainPitch) {
  92.             if(Pitch > 89.0f)
  93.                 Pitch = 89.0f;
  94.             if(Pitch < -89.0f)
  95.                 Pitch = -89.0f;
  96.         }
  97.        
  98.         updateCameraVectors();
  99.     }
  100.    
  101.     public void ProcessKeyboard(Camera_Movement direction, float delta) {
  102.         float velocity = MovementSpeed * delta;
  103.         Vector3f front = Front;
  104.         Vector3f right = Right;
  105.         Vector3f fmul = front.mul(velocity);
  106.         Vector3f rmul = right.mul(velocity);
  107.         if (direction == Camera_Movement.FORWARD)
  108.             Position.add(fmul);
  109.         if (direction == Camera_Movement.BACKWARD)
  110.             Position.sub(fmul);
  111.         if (direction == Camera_Movement.LEFT)
  112.             Position.sub(rmul);
  113.         if (direction == Camera_Movement.RIGHT)
  114.             Position.add(rmul);
  115.     }
  116.    
  117.     public Camera(Vector3f position) {
  118.         Position = position;
  119.         Front = FRONT;
  120.         WorldUp = UP;
  121.         Yaw = YAW;
  122.         Pitch = PITCH;
  123.         MovementSpeed = SPEED;
  124.         Zoom = ZOOM;
  125.        
  126.         updateCameraVectors();
  127.     }
  128.    
  129.     public Camera() {
  130.         Position = POSITION;
  131.         Front = FRONT;
  132.         WorldUp = UP;
  133.         Yaw = YAW;
  134.         Pitch = PITCH;
  135.         MovementSpeed = SPEED;
  136.         Zoom = ZOOM;
  137.        
  138.         updateCameraVectors();
  139.     }
  140.    
  141.     public Camera(Vector3f position, Vector3f up, float yaw, float pitch) {
  142.         Position = position;
  143.         WorldUp = up;
  144.         Yaw = yaw;
  145.         Pitch = pitch;
  146.         updateCameraVectors();
  147.     }
  148.  
  149. }
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement