|
| 1 | +package engine.scene.camera; |
| 2 | + |
| 3 | +import engine.components.Transform; |
| 4 | +import math.Mathf; |
| 5 | +import math.Vector3f; |
| 6 | + |
| 7 | +/** |
| 8 | + * A camera that simulates perspective projection, commonly used in 3D graphics. It uses a field of |
| 9 | + * view (FOV), near and far clipping planes, and an aspect ratio to create a 3D projection of the |
| 10 | + * world onto a 2D screen. The camera has a transform (position, rotation, scale) and a target that |
| 11 | + * it looks at. This class provides methods for manipulating and querying these properties. |
| 12 | + */ |
| 13 | +public class PerspectiveCamera implements Camera { |
| 14 | + |
| 15 | + /** The camera's transform (position, rotation, scale) */ |
| 16 | + private final Transform transform = new Transform(); |
| 17 | + |
| 18 | + /** The field of view (in radians), default is 60 degrees (PI/3) */ |
| 19 | + private float fov = Mathf.PI / 3f; |
| 20 | + |
| 21 | + /** The near clipping plane distance */ |
| 22 | + private float nearPlane = 0.1f; |
| 23 | + |
| 24 | + /** The far clipping plane distance */ |
| 25 | + private float farPlane = 5000; |
| 26 | + |
| 27 | + /** The aspect ratio of the camera (width/height) */ |
| 28 | + private float aspectRatio = 16f / 9f; |
| 29 | + |
| 30 | + /** The target point the camera is looking at */ |
| 31 | + private Vector3f target; |
| 32 | + |
| 33 | + /** |
| 34 | + * Creates a new PerspectiveCamera with default values. The default field of view is 60 degrees, |
| 35 | + * near plane is 0.1, far plane is 5000, and the aspect ratio is 16:9. |
| 36 | + */ |
| 37 | + public PerspectiveCamera() { |
| 38 | + target = new Vector3f(); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Gets the target point the camera is currently looking at. |
| 43 | + * |
| 44 | + * @return The target point. |
| 45 | + */ |
| 46 | + @Override |
| 47 | + public Vector3f getTarget() { |
| 48 | + return target; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Sets the target point for the camera to look at. |
| 53 | + * |
| 54 | + * @param target The target point in world space. |
| 55 | + */ |
| 56 | + @Override |
| 57 | + public void setTarget(Vector3f target) { |
| 58 | + this.target = target; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Gets the transform (position, rotation, and scale) of the camera. |
| 63 | + * |
| 64 | + * @return The camera's transform. |
| 65 | + */ |
| 66 | + @Override |
| 67 | + public Transform getTransform() { |
| 68 | + return transform; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Gets the field of view of the camera in radians. |
| 73 | + * |
| 74 | + * @return The field of view (in radians). |
| 75 | + */ |
| 76 | + @Override |
| 77 | + public float getFieldOfView() { |
| 78 | + return fov; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Sets the field of view of the camera in radians. |
| 83 | + * |
| 84 | + * @param fov The desired field of view (in radians). |
| 85 | + */ |
| 86 | + @Override |
| 87 | + public void setFieldOfView(float fov) { |
| 88 | + this.fov = fov; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Gets the near clipping plane distance of the camera. |
| 93 | + * |
| 94 | + * @return The near clipping plane distance. |
| 95 | + */ |
| 96 | + @Override |
| 97 | + public float getNearPlane() { |
| 98 | + return nearPlane; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Sets the near clipping plane distance of the camera. |
| 103 | + * |
| 104 | + * @param nearPlane The near clipping plane distance. |
| 105 | + */ |
| 106 | + @Override |
| 107 | + public void setNearPlane(float nearPlane) { |
| 108 | + this.nearPlane = nearPlane; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Gets the far clipping plane distance of the camera. |
| 113 | + * |
| 114 | + * @return The far clipping plane distance. |
| 115 | + */ |
| 116 | + @Override |
| 117 | + public float getFarPlane() { |
| 118 | + return farPlane; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Sets the far clipping plane distance of the camera. |
| 123 | + * |
| 124 | + * @param farPlane The far clipping plane distance. |
| 125 | + */ |
| 126 | + @Override |
| 127 | + public void setFarPlane(float farPlane) { |
| 128 | + this.farPlane = farPlane; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Gets the aspect ratio of the camera (width/height). |
| 133 | + * |
| 134 | + * @return The aspect ratio. |
| 135 | + */ |
| 136 | + @Override |
| 137 | + public float getAspectRatio() { |
| 138 | + return aspectRatio; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Sets the aspect ratio of the camera (width/height). |
| 143 | + * |
| 144 | + * @param aspectRatio The desired aspect ratio. |
| 145 | + */ |
| 146 | + @Override |
| 147 | + public void setAspectRatio(float aspectRatio) { |
| 148 | + this.aspectRatio = aspectRatio; |
| 149 | + } |
| 150 | +} |
0 commit comments