Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.julian.ogl.main;
- import org.joml.Matrix4f;
- import org.joml.Vector3f;
- public class Camera {
- enum Camera_Movement{
- FORWARD,
- BACKWARD,
- LEFT,
- RIGHT
- };
- //defaults
- Vector3f POSITION = new Vector3f(0.0f, 0.0f, 0.0f);
- Vector3f UP = new Vector3f(0.0f, 1.0f, 0.0f);
- Vector3f FRONT = new Vector3f(0.0f, 0.0f, -1.0f);
- float YAW = -90.0f;
- float PITCH = 0.0f;
- float SPEED = 2.5f;
- float SENSITIVITY = 0.1f;
- float ZOOM = 90.0f;
- //attributes
- Vector3f Position;
- Vector3f Front;
- Vector3f Up;
- Vector3f Right;
- Vector3f WorldUp;
- //euler angles
- float Yaw;
- float Pitch;
- //camera options
- float MovementSpeed;
- float MouseSensitivity;
- float Zoom;
- public void printVectorData() {
- System.out.println("Position: " + Position);
- System.out.println("Front: " + Front);
- System.out.println("Up: " + Up);
- System.out.println("Right: " + Right);
- System.out.println("WorldUp: " + WorldUp);
- }
- private void updateCameraVectors() {
- Vector3f front = new Vector3f(0.0f, 0.0f, 0.0f);
- front.x = (float) (Math.cos(Yaw) * Math.cos(Pitch));
- front.y = (float) Math.sin(Pitch);
- front.z = (float) (Math.sin(Yaw) * Math.cos(Pitch));
- Front = front.normalize(new Vector3f());
- Right = Front.cross(WorldUp, new Vector3f()).normalize(new Vector3f());
- Up = Right.cross(Front, new Vector3f()).normalize(new Vector3f());
- }
- public Matrix4f getViewMatrix() {
- return new Matrix4f().lookAt(Position, Position.add(Front, new Vector3f()), Up);
- }
- public void ProcessMouseScroll(float yoffset) {
- Zoom -= yoffset;
- if(Zoom < 1.0f)
- Zoom = 1.0f;
- if(Zoom > 90.0f)
- Zoom = 90.0f;
- }
- public void ProcessMouseMovement(float xoffset, float yoffset, boolean constrainPitch) {
- xoffset *= MouseSensitivity;
- yoffset *= MouseSensitivity;
- Yaw += xoffset;
- Pitch += yoffset;
- //make sure screen doesnt get flipped when out of bounds
- if(constrainPitch) {
- if(Pitch > 89.0f)
- Pitch = 89.0f;
- if(Pitch < -89.0f)
- Pitch = -89.0f;
- }
- updateCameraVectors();
- }
- public void ProcessKeyboard(Camera_Movement direction, float delta) {
- float velocity = MovementSpeed * delta;
- if (direction == Camera_Movement.FORWARD)
- Position = Position.add(Front.mul(velocity, new Vector3f()), new Vector3f());
- if (direction == Camera_Movement.BACKWARD)
- Position = Position.sub(Front.mul(velocity, new Vector3f()), new Vector3f());
- if (direction == Camera_Movement.LEFT)
- Position = Position.sub(Right.mul(velocity, new Vector3f()), new Vector3f());
- if (direction == Camera_Movement.RIGHT)
- Position = Position.add(Right.mul(velocity, new Vector3f()), new Vector3f());
- }
- public Camera(Vector3f position) {
- Position = position;
- Front = FRONT;
- WorldUp = UP;
- Yaw = YAW;
- Pitch = PITCH;
- MovementSpeed = SPEED;
- Zoom = ZOOM;
- updateCameraVectors();
- }
- public Camera() {
- Position = POSITION;
- Front = FRONT;
- WorldUp = UP;
- Yaw = YAW;
- Pitch = PITCH;
- MovementSpeed = SPEED;
- Zoom = ZOOM;
- updateCameraVectors();
- }
- public Camera(Vector3f position, Vector3f up, float yaw, float pitch) {
- Position = position;
- WorldUp = up;
- Yaw = yaw;
- Pitch = pitch;
- updateCameraVectors();
- }
- }
RAW Paste Data