Skip to content

Commit f37d56d

Browse files
Merge pull request #49 from ArtifactForms/working2
Working2
2 parents fb4f46d + 089f7f4 commit f37d56d

38 files changed

+4481
-4739
lines changed

src/main/java/engine/components/Transform.java

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ public Transform() {
5757
* @param g The graphics context to which this transformation is applied.
5858
*/
5959
public void apply(Graphics g) {
60-
g.translate(position.x, position.y, position.z);
61-
g.rotateX(rotation.x);
60+
g.scale(scale.x, scale.y, scale.z); // Scale first
61+
g.rotateX(rotation.x); // Then rotate
6262
g.rotateY(rotation.y);
6363
g.rotateZ(rotation.z);
64-
g.scale(scale.x, scale.y, scale.z);
64+
g.translate(position.x, position.y, position.z); // Translate last
6565
}
6666

6767
/**
@@ -88,6 +88,17 @@ public void rotate(Vector3f delta) {
8888
this.rotation.addLocal(delta);
8989
}
9090

91+
/**
92+
* Rotates this transformation by the given delta values for each axis (in radians).
93+
*
94+
* @param x The change in rotation around the X-axis (in radians).
95+
* @param y The change in rotation around the Y-axis (in radians).
96+
* @param z The change in rotation around the Z-axis (in radians).
97+
*/
98+
public void rotate(float x, float y, float z) {
99+
this.rotation.addLocal(x, y, z);
100+
}
101+
91102
/**
92103
* Scales this transformation by the provided scaling factors.
93104
*
@@ -208,6 +219,38 @@ public void setScale(float sx, float sy, float sz) {
208219
this.scale.set(sx, sy, sz);
209220
}
210221

222+
/**
223+
* Retrieves the forward direction of the transform, based on its current rotation.
224+
*
225+
* <p>The forward direction is calculated using the rotation values and represents the vector that
226+
* points in the direction the object is facing.
227+
*
228+
* @return A normalized {@link Vector3f} representing the forward direction of the object.
229+
*/
230+
public Vector3f getForward() {
231+
float cosY = (float) Math.cos(rotation.y);
232+
float sinY = (float) Math.sin(rotation.y);
233+
float cosX = (float) Math.cos(rotation.x);
234+
float sinX = (float) Math.sin(rotation.x);
235+
236+
return new Vector3f(cosY * cosX, sinX, sinY * cosX).normalizeLocal();
237+
}
238+
239+
/**
240+
* Retrieves the right direction of the transform, based on its current rotation.
241+
*
242+
* <p>The right direction is calculated using the rotation values and represents the vector that
243+
* points to the right of the object.
244+
*
245+
* @return A normalized {@link Vector3f} representing the right direction of the object.
246+
*/
247+
public Vector3f getRight() {
248+
float cosY = (float) Math.cos(rotation.y);
249+
float sinY = (float) Math.sin(rotation.y);
250+
251+
return new Vector3f(-sinY, 0, cosY).normalizeLocal();
252+
}
253+
211254
@Override
212255
public void update(float tpf) {}
213256

src/main/java/engine/debug/DebugInfoUpdater.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import engine.input.Input;
1010
import engine.input.Key;
1111
import engine.scene.Scene;
12+
import engine.scene.camera.Camera;
13+
import math.Mathf;
1214

1315
/**
1416
* The {@code DebugInfoUpdater} class is responsible for updating debug information displayed by a
@@ -24,9 +26,11 @@ public class DebugInfoUpdater {
2426
private static final String CATEGORY_SCENE = "Scene";
2527

2628
private static final String CATEGORY_SYSTEM = "System";
27-
29+
2830
private static final String CATEGORY_OS = "OS";
2931

32+
private static final String CATEGORY_CAMERA = "Camera";
33+
3034
private final DebugOverlay debugOverlay;
3135

3236
private final PerformanceMetrics performanceMetrics = new PerformanceMetrics();
@@ -80,6 +84,7 @@ public void update(Timer timer, Scene activeScene, Input input) {
8084
updateInputMetrics(input);
8185
if (activeScene != null) {
8286
updateSceneMetrics(activeScene);
87+
updateCameraInfo(activeScene.getActiveCamera());
8388
}
8489
updateOsMetrics();
8590
}
@@ -91,12 +96,20 @@ private String keysToString(Collection<Key> keys) {
9196
}
9297
return pressedKeys;
9398
}
94-
99+
100+
private void updateCameraInfo(Camera camera) {
101+
if (camera == null) return;
102+
setInfo(CATEGORY_CAMERA, "Aspect", camera.getAspectRatio());
103+
setInfo(CATEGORY_CAMERA, "FOV", Mathf.toDegrees(camera.getFieldOfView()));
104+
setInfo(CATEGORY_CAMERA, "Near", Mathf.toDegrees(camera.getNearPlane()));
105+
setInfo(CATEGORY_CAMERA, "Far", Mathf.toDegrees(camera.getFarPlane()));
106+
}
107+
95108
private void updateOsMetrics() {
96-
setInfo(CATEGORY_OS, "Name", osBean.getName());
97-
setInfo(CATEGORY_OS, "Arch", osBean.getArch());
98-
setInfo(CATEGORY_OS, "Processors", osBean.getAvailableProcessors());
99-
setInfo(CATEGORY_OS, "Version", osBean.getVersion());
109+
setInfo(CATEGORY_OS, "Name", osBean.getName());
110+
setInfo(CATEGORY_OS, "Arch", osBean.getArch());
111+
setInfo(CATEGORY_OS, "Processors", osBean.getAvailableProcessors());
112+
setInfo(CATEGORY_OS, "Version", osBean.getVersion());
100113
}
101114

102115
private void updateInputMetrics(Input input) {

src/main/java/engine/processing/ProcessingApplication.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import engine.input.MouseInput;
88
import processing.core.PApplet;
99
import workspace.GraphicsPImpl;
10-
import workspace.Workspace;
1110
import workspace.ui.Graphics;
1211

1312
public class ProcessingApplication extends PApplet {
@@ -18,8 +17,6 @@ public class ProcessingApplication extends PApplet {
1817

1918
private static ApplicationSettings settings;
2019

21-
private Workspace workspace;
22-
2320
@Override
2421
public void settings() {
2522
size(settings.getWidth(), settings.getHeight(), P3D);
@@ -35,10 +32,6 @@ public void setup() {
3532
container.setGraphics(g);
3633
getSurface().setTitle(settings.getTitle());
3734
setupInput();
38-
// workspace = new Workspace(this);
39-
// workspace.setLoop(true);
40-
// workspace.setGridVisible(false);
41-
// workspace.setUiVisible(false);
4235
container.initialize();
4336
noCursor();
4437
}
@@ -54,8 +47,6 @@ private void setupInput() {
5447
public void draw() {
5548
colorMode(RGB);
5649
background(0);
57-
scale(100);
58-
strokeWeight(0.01f);
5950
noLights();
6051
container.update();
6152
container.render();
Lines changed: 53 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package engine.scene.camera;
22

33
import engine.components.Transform;
4-
import math.Matrix4f;
5-
import math.Ray3f;
64
import math.Vector3f;
75

86
/**
@@ -19,60 +17,48 @@
1917
*/
2018
public interface Camera {
2119

22-
Vector3f getTarget();
23-
24-
/**
25-
* Retrieves the camera's transformation.
26-
*
27-
* <p>The transformation defines the position, rotation, and scaling of the camera in the 3D
28-
* world. This transformation is used to compute the camera's position and orientation in world
29-
* space.
30-
*
31-
* @return The {@link Transform} representing the camera's position, rotation, and scaling.
32-
*/
33-
Transform getTransform();
34-
3520
/**
36-
* Retrieves the current view matrix of the camera.
21+
* Sets the camera's target position in the world.
3722
*
38-
* <p>The view matrix is used to transform world-space coordinates into camera (view) space. It is
39-
* typically derived from the camera's position and orientation in the 3D world.
23+
* <p>The target defines the point in the 3D world that the camera is focused on. This is
24+
* typically used for creating behaviors such as following an object or maintaining a fixed view
25+
* of a specific scene element. The target can be dynamically updated during runtime to adjust the
26+
* camera's view.
4027
*
41-
* @return The view matrix as a {@link Matrix4f}.
28+
* @param target The new target for the camera to focus on, represented as a {@link Vector3f}.
4229
*/
43-
Matrix4f getViewMatrix();
30+
void setTarget(Vector3f target);
4431

4532
/**
46-
* Retrieves the current projection matrix of the camera.
33+
* Retrieves the current target position of the camera.
4734
*
48-
* <p>The projection matrix defines how a 3D scene is projected onto a 2D viewport, depending on
49-
* the camera's projection settings (perspective or orthographic).
35+
* <p>The target defines the point in the 3D world that the camera is currently focused on. This
36+
* is useful for determining the camera's orientation or calculating the view matrix. The target
37+
* remains constant unless explicitly changed using {@link #setTarget(Vector3f)}.
5038
*
51-
* @return The projection matrix as a {@link Matrix4f}.
39+
* @return The current target of the camera as a {@link Vector3f}.
5240
*/
53-
Matrix4f getProjectionMatrix();
41+
Vector3f getTarget();
5442

5543
/**
56-
* Updates the view matrix based on the current transformation.
44+
* Retrieves the camera's transformation, including its position, rotation, and scale in the 3D
45+
* world.
5746
*
58-
* <p>This method should recalculate the view matrix whenever the camera's position or orientation
59-
* has changed.
60-
*/
61-
void updateViewMatrix();
62-
63-
/**
64-
* Updates the projection matrix based on camera-specific settings.
47+
* <p>The transformation is a {@link Transform} object that encapsulates the camera's position,
48+
* rotation, and scaling within the scene. It is used to compute the camera's world space
49+
* orientation and can be updated to change the camera's location or rotation in the scene.
6550
*
66-
* <p>This method should be called whenever changes are made to parameters like the field of view,
67-
* near or far clipping planes, or aspect ratio.
51+
* @return The camera's {@link Transform} representing its position, rotation, and scaling in the
52+
* world.
6853
*/
69-
void updateProjectionMatrix();
54+
Transform getTransform();
7055

7156
/**
72-
* Retrieves the field of view (FOV) for perspective cameras.
57+
* Retrieves the field of view (FOV) of the camera, applicable for perspective projections.
7358
*
74-
* <p>The field of view determines how wide or narrow the camera's view is and only applies to
75-
* perspective projections.
59+
* <p>The field of view determines how wide or narrow the camera's perspective is, affecting how
60+
* much of the 3D scene is visible at any given time. This property is only relevant for
61+
* perspective cameras.
7662
*
7763
* @return The current field of view in degrees.
7864
*/
@@ -81,45 +67,53 @@ public interface Camera {
8167
/**
8268
* Sets the field of view (FOV) for perspective cameras.
8369
*
84-
* <p>This only has an effect on cameras configured for perspective projection.
70+
* <p>The field of view defines how wide or narrow the camera's perspective is, which influences
71+
* the perception of depth in the scene. This property only applies to perspective cameras and has
72+
* no effect on orthographic cameras.
8573
*
8674
* @param fov The desired field of view in degrees.
8775
*/
8876
void setFieldOfView(float fov);
8977

9078
/**
91-
* Retrieves the near clipping plane distance.
79+
* Retrieves the near clipping plane distance for the camera.
9280
*
93-
* <p>The near clipping plane defines the closest distance from the camera at which objects are
94-
* rendered. Objects closer than this distance will not be visible.
81+
* <p>The near clipping plane defines the closest distance at which objects will be rendered by
82+
* the camera. Any objects closer than this distance will not be visible. This value is crucial
83+
* for depth calculations and scene rendering.
9584
*
9685
* @return The near clipping plane distance.
9786
*/
9887
float getNearPlane();
9988

10089
/**
101-
* Sets the near clipping plane distance.
90+
* Sets the near clipping plane distance for the camera.
10291
*
103-
* <p>This modifies how close an object must be to the camera to be visible.
92+
* <p>The near clipping plane defines the closest visible distance in the 3D world. Objects closer
93+
* than this plane will not be rendered. Adjusting this value helps prevent issues like z-fighting
94+
* and ensures proper visibility of objects.
10495
*
10596
* @param nearPlane The desired near clipping plane distance.
10697
*/
10798
void setNearPlane(float nearPlane);
10899

109100
/**
110-
* Retrieves the far clipping plane distance.
101+
* Retrieves the far clipping plane distance for the camera.
111102
*
112-
* <p>The far clipping plane defines the furthest distance from the camera at which objects are
113-
* rendered. Objects farther away than this distance will not be visible.
103+
* <p>The far clipping plane defines the furthest distance at which objects will be rendered.
104+
* Objects farther than this distance will not be visible. This value is critical for controlling
105+
* the rendering of distant objects and depth precision.
114106
*
115107
* @return The far clipping plane distance.
116108
*/
117109
float getFarPlane();
118110

119111
/**
120-
* Sets the far clipping plane distance.
112+
* Sets the far clipping plane distance for the camera.
121113
*
122-
* <p>This modifies how far objects can be from the camera to remain visible.
114+
* <p>The far clipping plane defines the maximum visible distance for the camera. Objects beyond
115+
* this distance will not be rendered. Modifying this value affects the range of visible objects
116+
* in the scene and can help optimize performance by excluding distant geometry.
123117
*
124118
* @param farPlane The desired far clipping plane distance.
125119
*/
@@ -128,46 +122,23 @@ public interface Camera {
128122
/**
129123
* Retrieves the current aspect ratio of the camera's viewport.
130124
*
131-
* <p>The aspect ratio is defined as the ratio of the viewport's width to its height and is used
132-
* to adjust the projection matrix accordingly.
125+
* <p>The aspect ratio is the ratio of the width to the height of the camera's viewport and
126+
* determines how the scene is projected onto the screen. The aspect ratio is used to adjust the
127+
* projection matrix for accurate rendering and proper scene alignment, especially in 3D.
133128
*
134-
* @return The aspect ratio of the viewport.
129+
* @return The current aspect ratio of the camera's viewport (width divided by height).
135130
*/
136131
float getAspectRatio();
137132

138133
/**
139134
* Sets the aspect ratio for the camera's viewport.
140135
*
141-
* <p>Changing the aspect ratio should trigger an update to the projection matrix.
136+
* <p>Changing the aspect ratio of the camera will affect how the scene is projected, ensuring
137+
* that the rendered image fits the viewport properly. This adjustment is typically necessary when
138+
* resizing the window or changing the display resolution.
142139
*
143-
* @param aspectRatio The desired aspect ratio.
140+
* @param aspectRatio The desired aspect ratio for the camera's viewport (width divided by
141+
* height).
144142
*/
145143
void setAspectRatio(float aspectRatio);
146-
147-
/**
148-
* Converts 2D screen coordinates to a 3D ray in world space.
149-
*
150-
* <p>This method is essential for raycasting operations, such as determining which objects in the
151-
* scene correspond to a given 2D screen-space click.
152-
*
153-
* @param screenX The x-coordinate on the screen.
154-
* @param screenY The y-coordinate on the screen.
155-
* @param viewportWidth The width of the viewport in pixels.
156-
* @param viewportHeight The height of the viewport in pixels.
157-
* @return A {@link Ray3f} representing the computed ray in 3D world space.
158-
*/
159-
Ray3f createRay(float screenX, float screenY, int viewportWidth, int viewportHeight);
160-
161-
/**
162-
* Converts 2D screen-space coordinates to their corresponding world-space coordinates.
163-
*
164-
* <p>This is the inverse of projection and can be used for operations like object picking or
165-
* determining intersections between screen-space inputs and 3D objects in the world.
166-
*
167-
* @param screenCoords The 2D screen-space coordinates to unproject.
168-
* @param viewportWidth The width of the viewport in pixels.
169-
* @param viewportHeight The height of the viewport in pixels.
170-
* @return The corresponding 3D world-space coordinates as a {@link Vector3f}.
171-
*/
172-
Vector3f unproject(Vector3f screenCoords, int viewportWidth, int viewportHeight);
173144
}

0 commit comments

Comments
 (0)