Advertisement
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 = 45.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;
- private void updateCameraVectors() {
- Vector3f front = new Vector3f(0.0f, 0.0f, 0.0f);
- front.x = (float) (Math.cos(Math.toRadians(Yaw) * Math.cos(Math.toRadians(Pitch))));
- front.y = (float) Math.sin(Math.toRadians(Pitch));
- front.z = (float) (Math.sin(Math.toRadians(Yaw)) * Math.cos(Math.toRadians(Pitch)));
- Front = Front.normalize();
- Vector3f fron = Front;
- Vector3f wup = WorldUp;
- Right = fron.cross(wup).normalize();
- Vector3f r = Right;
- Up = r.cross(fron).normalize();
- }
- //print Vector data only added because this issue
- 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);
- }
- public Matrix4f getViewMatrix() {
- Matrix4f viewMatrix = new Matrix4f();
- Vector3f pos = Position;
- Vector3f up = Up;
- Vector3f front = Front;
- viewMatrix = viewMatrix.lookAt(pos, pos.add(front), up);
- System.out.println(Position);
- return viewMatrix;
- }
- public void ProcessMouseScroll(float yoffset) {
- Zoom -= yoffset;
- if(Zoom < 1.0f)
- Zoom = 1.0f;
- if(Zoom > 45.0f)
- Zoom = 45.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;
- Vector3f front = Front;
- Vector3f right = Right;
- Vector3f fmul = front.mul(velocity);
- Vector3f rmul = right.mul(velocity);
- if (direction == Camera_Movement.FORWARD)
- Position.add(fmul);
- if (direction == Camera_Movement.BACKWARD)
- Position.sub(fmul);
- if (direction == Camera_Movement.LEFT)
- Position.sub(rmul);
- if (direction == Camera_Movement.RIGHT)
- Position.add(rmul);
- }
- 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();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement