Guest User

Untitled

a guest
Sep 20th, 2020
26
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 = 90.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.     public void printVectorData() {
  42.         System.out.println("Position: " + Position);
  43.         System.out.println("Front: " + Front);
  44.         System.out.println("Up: " + Up);
  45.         System.out.println("Right: " + Right);
  46.         System.out.println("WorldUp: " + WorldUp);
  47.     }
  48.    
  49.     private void updateCameraVectors() {
  50.         Vector3f front = new Vector3f(0.0f, 0.0f, 0.0f);
  51.         front.x = (float) (Math.cos(Yaw) * Math.cos(Pitch));
  52.         front.y = (float) Math.sin(Pitch);
  53.         front.z = (float) (Math.sin(Yaw) * Math.cos(Pitch));
  54.         Front = front.normalize(new Vector3f());
  55.        
  56.         Right = Front.cross(WorldUp, new Vector3f()).normalize(new Vector3f());
  57.         Up = Right.cross(Front, new Vector3f()).normalize(new Vector3f());
  58.     }
  59.    
  60.     public Matrix4f getViewMatrix() {
  61.         return new Matrix4f().lookAt(Position, Position.add(Front, new Vector3f()), Up);
  62.     }
  63.    
  64.     public void ProcessMouseScroll(float yoffset) {
  65.         Zoom -= yoffset;
  66.         if(Zoom < 1.0f)
  67.             Zoom = 1.0f;
  68.         if(Zoom > 90.0f)
  69.             Zoom = 90.0f;
  70.     }
  71.    
  72.     public void ProcessMouseMovement(float xoffset, float yoffset, boolean constrainPitch) {
  73.         xoffset *= MouseSensitivity;
  74.         yoffset *= MouseSensitivity;
  75.        
  76.         Yaw += xoffset;
  77.         Pitch += yoffset;
  78.        
  79.         //make sure screen doesnt get flipped when out of bounds
  80.         if(constrainPitch) {
  81.             if(Pitch > 89.0f)
  82.                 Pitch = 89.0f;
  83.             if(Pitch < -89.0f)
  84.                 Pitch = -89.0f;
  85.         }
  86.        
  87.         updateCameraVectors();
  88.     }
  89.    
  90.     public void ProcessKeyboard(Camera_Movement direction, float delta) {
  91.         float velocity = MovementSpeed * delta;
  92.         if (direction == Camera_Movement.FORWARD)
  93.             Position = Position.add(Front.mul(velocity, new Vector3f()), new Vector3f());
  94.         if (direction == Camera_Movement.BACKWARD)
  95.             Position = Position.sub(Front.mul(velocity, new Vector3f()), new Vector3f());
  96.         if (direction == Camera_Movement.LEFT)
  97.             Position = Position.sub(Right.mul(velocity, new Vector3f()), new Vector3f());
  98.         if (direction == Camera_Movement.RIGHT)
  99.             Position = Position.add(Right.mul(velocity, new Vector3f()), new Vector3f());
  100.     }
  101.    
  102.     public Camera(Vector3f position) {
  103.         Position = position;
  104.         Front = FRONT;
  105.         WorldUp = UP;
  106.         Yaw = YAW;
  107.         Pitch = PITCH;
  108.         MovementSpeed = SPEED;
  109.         Zoom = ZOOM;
  110.        
  111.         updateCameraVectors();
  112.     }
  113.    
  114.     public Camera() {
  115.         Position = POSITION;
  116.         Front = FRONT;
  117.         WorldUp = UP;
  118.         Yaw = YAW;
  119.         Pitch = PITCH;
  120.         MovementSpeed = SPEED;
  121.         Zoom = ZOOM;
  122.        
  123.         updateCameraVectors();
  124.     }
  125.    
  126.     public Camera(Vector3f position, Vector3f up, float yaw, float pitch) {
  127.         Position = position;
  128.         WorldUp = up;
  129.         Yaw = yaw;
  130.         Pitch = pitch;
  131.         updateCameraVectors();
  132.     }
  133.  
  134. }
  135.  
RAW Paste Data