Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package render;
- import org.joml.Matrix4f;
- import org.joml.Vector3f;
- import org.joml.Vector4f;
- import lombok.Getter;
- import lombok.Setter;
- @Getter
- public class YPRCamera {
- private @Setter Vector3f position = new Vector3f(); /// < Camera position.
- private Vector3f view = new Vector3f(0, 0, -1); /// < Direction of the line of sight.
- private Vector3f up = new Vector3f(0, 1, 0); /// < Direction of up.
- /**
- * Sets the position, view and up for the camera.
- *
- * The method will also normalize the view and up vectors.
- *
- * @param x
- * X coordinate to the camera position.
- * @param y
- * Y coordinate to the camera position.
- * @param z
- * Z coordinate to the camera position.
- * @param vX
- * X coordinate to the viewing vector.
- * @param vY
- * Y coordinate to the viewing vector.
- * @param vZ
- * Z coordinate to the viewing vector.
- * @param upX
- * X coordinate to the up vector.
- * @param upY
- * Y coordinate to the up vector.
- * @param upZ
- * Z coordinate to the up vector.
- */
- public void positionCamera(float x, float y, float z, float vX, float vY, float vZ, float upX, float upY, float upZ) {
- position.set(x, y, z);
- view.set(vX, vY, vZ);
- up.set(upX, upY, upZ);
- up.normalize();
- view.normalize();
- }
- /**
- * Sets the position, view and up for the camera.
- *
- * The method will also normalize the view and up vectors.
- *
- * @param pos
- * Position vector.
- * @param view
- * View vector.
- * @param up
- * Up vector.
- */
- void positionCamera(Vector3f pos, Vector3f view, Vector3f up) {
- this.position = pos;
- this.view = view;
- this.up = up;
- up.normalize();
- view.normalize();
- }
- /**
- * Sets the position for the camera.
- *
- * @param x
- * X coordinate to the camera position.
- * @param y
- * Y coordinate to the camera position.
- * @param z
- * Z coordinate to the camera position.
- */
- void setPosition(float x, float y, float z) {
- position.x = x;
- position.y = y;
- position.z = z;
- }
- /**
- * Sets the view vector for the camera.
- *
- * The method will also normalize the view vector.
- *
- * @param x
- * X coordinate to the viewing vector.
- * @param y
- * Y coordinate to the viewing vector.
- * @param z
- * Z coordinate to the viewing vector.
- */
- void setView(float x, float y, float z) {
- view.x = x;
- view.y = y;
- view.z = z;
- view.normalize();
- }
- /**
- * Sets the view vector for the camera.
- *
- * The method will also normalize the view vector.
- *
- * @param view
- * View vector.
- */
- void setView(Vector3f view) {
- this.view = view;
- view.normalize();
- }
- /**
- * Sets the up vector for the camera.
- *
- * The method will also normalize the up vector.
- *
- * @param x
- * X coordinate to the up vector.
- * @param y
- * Y coordinate to the up vector.
- * @param z
- * Z coordinate to the up vector.
- */
- void setUp(float x, float y, float z) {
- up.x = x;
- up.y = y;
- up.z = z;
- up.normalize();
- }
- /**
- * Sets the up vector for the camera.
- *
- * The method will also normalize the up vector.
- *
- * @param up
- * Up vector.
- */
- void setUp(Vector3f up) {
- this.up = up;
- up.normalize();
- }
- /**
- * Sets the up vector for the camera in the positive y direction.
- *
- * This method will also normalize the up vector and keep it orthogonal to the viewing vector.
- *
- */
- void setUpViaY() {
- Vector3f Y = new Vector3f(0, 1, 0);
- Vector3f V = view;
- Vector3f P = V.mul(Y.dot(V) / V.lengthSquared(), new Vector3f());
- up = Y.sub(P).normalize();
- }
- /**
- * Adds to the current pitch of the camera.
- *
- * @param num
- * The degree measurement of the amount to add to the pitch of the camera.
- */
- public void addPitch(float num) {
- // Calculate the right vector.
- Vector3f mn = view.cross(up, new Vector3f());
- // Create the rotation matrix for rotation around the right vector by an angle of num.
- Matrix4f rot = new Matrix4f().rotate((float) Math.toRadians(num), mn);
- // Convert the view and up vectors to 4D.
- Vector4f View4 = new Vector4f(view, 1);
- Vector4f Up4 = new Vector4f(up, 1);
- // Rotate the view and up vectors.
- View4.mul(rot);
- Up4.mul(rot);
- // Convert the transformed view and up vectors to 3D.
- up = new Vector3f(Up4.x, Up4.y, Up4.z).normalize();
- view = new Vector3f(View4.x, View4.y, View4.z).normalize();
- }
- /**
- * Adds to the current yaw of the camera.
- *
- * @param num
- * The degree measurement of the amount to add to the yaw of the camera.
- */
- public void addYaw(float num) {
- // Create the rotation matrix for rotation around the up vector by an angle of num.
- Matrix4f rot = new Matrix4f().rotate((float) Math.toRadians(num), up);
- // Convert the view vector to 4D.
- Vector4f View4 = new Vector4f(view, 1);
- // Rotate the view vector.
- View4.mul(rot);
- // Convert the transformed view vector to 3D.
- view = new Vector3f(View4.x, View4.y, View4.z).normalize();
- }
- /**
- * Adds to the current roll of the camera.
- *
- * @param num
- * The degree measurement of the amount to add to the roll of the camera.
- */
- public void addRoll(float num) {
- // Create the rotation matrix for rotation around the view vector by an angle of num.
- Matrix4f rot = new Matrix4f().rotate((float) Math.toRadians(num), view);
- // Convert the up vector to 4D.
- Vector4f Up4 = new Vector4f(up, 1);
- // Rotate the up vector.
- Up4.mul(rot);
- // Convert the transformed up vector to 3D.
- up = new Vector3f(Up4.x, Up4.y, Up4.z).normalize();
- }
- /**
- * @return The lookat matrix for the current position, line of sight, and up vectors.
- */
- Matrix4f lookAt() {
- return new Matrix4f().lookAt(position, position.add(view, new Vector3f()), up);
- }
- /**
- * Moves the camera forward.
- *
- * @param num
- * Amount to move the camera forward.
- */
- public void moveForward(float num) {
- float mod = view.lengthSquared();
- if (mod < 0.0001f)
- return;
- position.add(view.div(mod, new Vector3f()).mul(num));
- }
- /**
- * Moves the camera right.
- *
- * @param num
- * Amount to move the camera to the right.
- */
- public void moveRight(float num) {
- Vector3f mn = view.cross(up, new Vector3f());
- float mod = mn.lengthSquared();
- if (mod < 0.0001f)
- return;
- position.add(mn.div(mod).mul(num));
- }
- /**
- * Moves the camera up.
- *
- * @param num
- * Amount to move the camera up.
- */
- public void moveUp(float num) {
- float mod = up.lengthSquared();
- if (mod < 0.0001f)
- return;
- position.add(up.div(mod, new Vector3f()).mul(num));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment