Skip to content

Commit 188f232

Browse files
Merge pull request #56 from ArtifactForms/working2
Working2
2 parents 809a853 + aae734f commit 188f232

20 files changed

+1894
-460
lines changed

src/main/java/engine/components/FlyByCameraControl.java

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,12 @@ public void update(float tpf) {
7878
float mouseY = input.getMouseDeltaY() * mouseSensitivity * tpf;
7979

8080
handleRotation(mouseX, mouseY);
81-
updateTarget();
8281

8382
Vector3f velocity = calculateVelocity();
8483
if (velocity.length() > 0) {
8584
applyMovement(velocity, tpf);
8685
}
87-
86+
updateTarget();
8887
input.center();
8988
}
9089

@@ -147,7 +146,6 @@ private void applyMovement(Vector3f velocity, float tpf) {
147146
Vector3f position = camera.getTransform().getPosition();
148147
position.addLocal(velocity.mult(moveSpeed * tpf));
149148
camera.getTransform().setPosition(position);
150-
updateTarget();
151149
}
152150

153151
/**
@@ -180,4 +178,46 @@ public void onAttach() {
180178
public void onDetach() {
181179
// Not used yet
182180
}
181+
182+
/**
183+
* Returns the current movement speed of the camera.
184+
*
185+
* @return The movement speed in units per second.
186+
*/
187+
public float getMoveSpeed() {
188+
return moveSpeed;
189+
}
190+
191+
/**
192+
* Sets the movement speed of the camera.
193+
*
194+
* @param moveSpeed The new movement speed in units per second.
195+
*/
196+
public void setMoveSpeed(float moveSpeed) {
197+
this.moveSpeed = moveSpeed;
198+
}
199+
200+
/**
201+
* Returns the current mouse sensitivity used for camera rotation.
202+
*
203+
* <p>The mouse sensitivity determines how much the camera rotates based on mouse movement. Higher
204+
* sensitivity values result in larger rotations for smaller mouse movements.
205+
*
206+
* @return The current mouse sensitivity.
207+
*/
208+
public float getMouseSensitivity() {
209+
return mouseSensitivity;
210+
}
211+
212+
/**
213+
* Sets the mouse sensitivity used for camera rotation.
214+
*
215+
* <p>The mouse sensitivity determines how much the camera rotates based on mouse movement. Higher
216+
* sensitivity values result in larger rotations for smaller mouse movements.
217+
*
218+
* @param mouseSensitivity The new mouse sensitivity value.
219+
*/
220+
public void setMouseSensitivity(float mouseSensitivity) {
221+
this.mouseSensitivity = mouseSensitivity;
222+
}
183223
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import engine.input.KeyInput;
77
import engine.input.MouseInput;
88
import engine.resources.ResourceManager;
9+
import engine.resources.TextureManager;
910
import processing.core.PApplet;
1011
import workspace.GraphicsPImpl;
1112
import workspace.ui.Graphics;
@@ -31,6 +32,7 @@ public void settings() {
3132
public void setup() {
3233
Graphics g = new GraphicsPImpl(this);
3334
ResourceManager.getInstance().setImageLoader(new ProcessingImageLoader(this));
35+
TextureManager.getInstance().setTextureLoader(new ProcessingTextureLoader(this));
3436
container.setGraphics(g);
3537
getSurface().setTitle(settings.getTitle());
3638
setupInput();
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package engine.processing;
2+
3+
import engine.resources.Texture;
4+
import processing.core.PImage;
5+
6+
public class ProcessingTexture implements Texture {
7+
8+
private final PImage image;
9+
10+
public ProcessingTexture(PImage image) {
11+
this.image = image;
12+
}
13+
14+
@Override
15+
public int getWidth() {
16+
return image.width;
17+
}
18+
19+
@Override
20+
public int getHeight() {
21+
return image.height;
22+
}
23+
24+
@Override
25+
public void bind(int unit) {
26+
// Processing doesn't use texture units in the same way, just bind globally
27+
image.loadPixels();
28+
}
29+
30+
@Override
31+
public void unbind() {
32+
// No specific unbind operation for Processing
33+
}
34+
35+
@Override
36+
public void delete() {
37+
// Processing handles memory management automatically
38+
}
39+
40+
public PImage getImage() {
41+
return image;
42+
}
43+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package engine.processing;
2+
3+
import engine.resources.Texture;
4+
import engine.resources.TextureLoader;
5+
import processing.core.PApplet;
6+
import processing.core.PImage;
7+
8+
public class ProcessingTextureLoader implements TextureLoader {
9+
10+
private final PApplet parent;
11+
12+
public ProcessingTextureLoader(PApplet parent) {
13+
this.parent = parent;
14+
}
15+
16+
@Override
17+
public Texture loadTexture(String filePath) {
18+
PImage image =
19+
parent.loadImage(
20+
ProcessingTextureLoader.class
21+
.getClassLoader()
22+
.getResource("images/" + filePath)
23+
.getPath());
24+
ProcessingTexture texture = new ProcessingTexture(image);
25+
return texture;
26+
}
27+
}

src/main/java/engine/render/Material.java

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package engine.render;
22

3+
import engine.resources.Texture;
34
import math.Color;
45
import workspace.ui.Graphics;
56

@@ -67,6 +68,10 @@ public class Material {
6768
/** Shininess factor for specular highlights. */
6869
private final float shininess;
6970

71+
private Texture normalTexture;
72+
73+
private Texture diffuseTexture;
74+
7075
/**
7176
* Constructor to set the base color of the material.
7277
*
@@ -83,11 +88,13 @@ private Material(Builder builder) {
8388
this.diffuse = builder.diffuse;
8489
this.specular = builder.specular;
8590
this.shininess = builder.shininess;
91+
this.normalTexture = builder.normalTexture;
92+
this.diffuseTexture = builder.diffuseTexture;
8693
}
8794

8895
/**
89-
* Builder class to facilitate the creation of custom materials with specific lighting and shader
90-
* properties.
96+
* Builder class to facilitate the creation of custom materials with specific lighting, shader and
97+
* texture properties.
9198
*/
9299
public static class Builder {
93100

@@ -103,6 +110,10 @@ public static class Builder {
103110

104111
private float shininess = 10.0f;
105112

113+
private Texture diffuseTexture = null;
114+
115+
private Texture normalTexture = null;
116+
106117
/**
107118
* Sets the base color of the material.
108119
*
@@ -163,6 +174,28 @@ public Builder setUseLighting(boolean useLighting) {
163174
return this;
164175
}
165176

177+
/**
178+
* Sets the normal texture of the material.
179+
*
180+
* @param normalTexture The normal texture, can be null
181+
* @return The builder instance for chaining
182+
*/
183+
public Builder setNormalTexture(Texture normalTexture) {
184+
this.normalTexture = normalTexture;
185+
return this;
186+
}
187+
188+
/**
189+
* Sets the diffuse texture of the material.
190+
*
191+
* @param diffuseTexture The diffuse texture, can be null.
192+
* @return The builder instance for chaining
193+
*/
194+
public Builder setDiffuseTexture(Texture diffuseTexture) {
195+
this.diffuseTexture = diffuseTexture;
196+
return this;
197+
}
198+
166199
/**
167200
* Builds and returns the Material instance with the set properties.
168201
*
@@ -180,6 +213,13 @@ public Material build() {
180213
*/
181214
public void apply(Graphics g) {
182215
g.setMaterial(this);
216+
217+
if (diffuseTexture != null) {
218+
g.bindTexture(diffuseTexture, 0); // Bind to texture unit 0
219+
}
220+
if (normalTexture != null) {
221+
g.bindTexture(normalTexture, 1); // Bind to texture unit 1
222+
}
183223
}
184224

185225
/**
@@ -240,4 +280,12 @@ public float[] getSpecular() {
240280
public float getShininess() {
241281
return shininess;
242282
}
283+
284+
public Texture getDiffuseTexture() {
285+
return diffuseTexture;
286+
}
287+
288+
public Texture getNormalTexture() {
289+
return normalTexture;
290+
}
243291
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package engine.resources;
2+
3+
public interface Texture {
4+
5+
int getWidth();
6+
7+
int getHeight();
8+
9+
void bind(int unit); // Bind to a specific texture unit
10+
11+
void unbind();
12+
13+
void delete();
14+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package engine.resources;
2+
3+
public interface TextureLoader {
4+
5+
Texture loadTexture(String filePath);
6+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package engine.resources;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class TextureManager {
7+
8+
private static TextureManager instance;
9+
10+
private TextureLoader imageLoader;
11+
12+
private final Map<String, Texture> resourceCache = new HashMap<>();
13+
14+
private TextureManager() {}
15+
16+
public static TextureManager getInstance() {
17+
if (instance == null) {
18+
instance = new TextureManager();
19+
}
20+
return instance;
21+
}
22+
23+
public void setTextureLoader(TextureLoader loader) {
24+
this.imageLoader = loader;
25+
}
26+
27+
public Texture loadTexture(String path) {
28+
if (resourceCache.containsKey(path)) {
29+
return resourceCache.get(path); // Return cached resource
30+
}
31+
32+
if (imageLoader == null) {
33+
throw new IllegalStateException("ImageLoader is not set!");
34+
}
35+
36+
Texture texture = imageLoader.loadTexture(path);
37+
resourceCache.put(path, texture);
38+
39+
return texture;
40+
}
41+
42+
public void unloadImage(String path) {
43+
resourceCache.remove(path); // Optionally handle cleanup for backend-specific resources
44+
}
45+
}

src/main/java/math/Mathf.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,15 @@ public class Mathf {
9494
* `numberOfColumns` is less than or equal to zero.
9595
*/
9696
public static int toOneDimensionalIndex(int rowIndex, int colIndex, int numberOfColumns) {
97-
if (rowIndex < 0 || colIndex < 0) throw new IllegalArgumentException();
98-
99-
if (numberOfColumns <= 0) throw new IllegalArgumentException();
97+
if (numberOfColumns <= 0) {
98+
throw new IllegalArgumentException("NumberOfColumns must be greater than zero.");
99+
}
100+
if (rowIndex < 0) {
101+
throw new IllegalArgumentException("rowIndex must be non-negative");
102+
}
103+
if (colIndex < 0 || colIndex >= numberOfColumns) {
104+
throw new IllegalArgumentException("colIndex is out of bounds");
105+
}
100106

101107
return rowIndex * numberOfColumns + colIndex;
102108
}
@@ -890,4 +896,17 @@ public static float normalizeAngle(float angle) {
890896

891897
return angle;
892898
}
899+
900+
/**
901+
* Compares two floating-point numbers for equality within a given tolerance (epsilon).
902+
*
903+
* @param a The first number.
904+
* @param b The second number.
905+
* @param epsilon The tolerance within which the two numbers are considered equal.
906+
* @return True if the absolute difference between the two numbers is less than or equal to
907+
* epsilon, otherwise false.
908+
*/
909+
public static boolean equals(float a, float b, float epsilon) {
910+
return Math.abs(a - b) <= epsilon;
911+
}
893912
}

0 commit comments

Comments
 (0)