Skip to content

Commit b334fda

Browse files
Merge pull request #59 from ArtifactForms/working2
Working2
2 parents d904467 + ec2bbd6 commit b334fda

File tree

14 files changed

+356
-28
lines changed

14 files changed

+356
-28
lines changed

src/main/java/engine/application/BasicApplication.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ public void update() {
105105

106106
lastZ = input.isKeyPressed(Key.Z);
107107
}
108-
109108
timer.update();
109+
input.update();
110110
fpsGraph.update(timer);
111111
debugInfoUpdater.update(timer, activeScene, input);
112112

@@ -139,7 +139,6 @@ public void render(Graphics g) {
139139
renderUi(g);
140140
renderDebugUi(g);
141141

142-
143142
g.enableDepthTest();
144143
}
145144

src/main/java/engine/processing/ProcessingMouseInput.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ public class ProcessingMouseInput implements MouseInput {
1010
private final PApplet applet;
1111

1212
private float mouseWheelDelta = 0;
13+
14+
private float mouseX;
15+
private float mouseY;
16+
private float pMouseX;
17+
private float pMouseY;
1318

1419
private Robot robot;
1520

@@ -51,32 +56,32 @@ public float getScreenHeight() {
5156

5257
@Override
5358
public float getMouseX() {
54-
return applet.mouseX;
59+
return mouseX;
5560
}
5661

5762
@Override
5863
public float getMouseY() {
59-
return applet.mouseY;
64+
return mouseY;
6065
}
6166

6267
@Override
6368
public float getLastMouseX() {
64-
return applet.pmouseX;
69+
return pMouseX;
6570
}
6671

6772
@Override
6873
public float getLastMouseY() {
69-
return applet.pmouseY;
74+
return pMouseY;
7075
}
7176

7277
@Override
7378
public float getMouseDeltaX() {
74-
return applet.mouseX - applet.pmouseX;
79+
return mouseX - pMouseX;
7580
}
7681

7782
@Override
7883
public float getMouseDeltaY() {
79-
return applet.mouseY - applet.pmouseY;
84+
return mouseY - pMouseY;
8085
}
8186

8287
@Override
@@ -87,7 +92,12 @@ public float getMouseWheelDelta() {
8792
}
8893

8994
@Override
90-
public void updateMouseState() {}
95+
public void updateMouseState() {
96+
this.mouseX = applet.mouseX;
97+
this.mouseY = applet.mouseY;
98+
this.pMouseX = applet.pmouseX;
99+
this.pMouseY = applet.pmouseY;
100+
}
91101

92102
@Override
93103
public void center() {

src/main/java/engine/processing/ProcessingTexture.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package engine.processing;
22

3+
import engine.resources.FilterMode;
34
import engine.resources.Texture;
45
import processing.core.PImage;
56

67
public class ProcessingTexture implements Texture {
78

89
private final PImage image;
910

11+
private FilterMode filterMode;
12+
1013
public ProcessingTexture(PImage image) {
1114
this.image = image;
15+
this.filterMode = FilterMode.BILINEAR;
1216
}
1317

1418
@Override
@@ -36,7 +40,29 @@ public void delete() {
3640
// Processing handles memory management automatically
3741
}
3842

43+
@Override
44+
public void setPixels(int[] pixels) {
45+
image.loadPixels();
46+
image.pixels = pixels;
47+
image.updatePixels();
48+
}
49+
3950
public PImage getImage() {
4051
return image;
4152
}
53+
54+
@Override
55+
public FilterMode getFilterMode() {
56+
return filterMode;
57+
}
58+
59+
@Override
60+
public void setFilterMode(FilterMode filterMode) {
61+
this.filterMode = filterMode;
62+
}
63+
64+
@Override
65+
public Texture getBackendTexture() {
66+
return this;
67+
}
4268
}

src/main/java/engine/processing/ProcessingTextureLoader.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package engine.processing;
22

3+
import java.awt.Image;
4+
35
import engine.resources.Texture;
46
import engine.resources.TextureLoader;
57
import processing.core.PApplet;
@@ -24,4 +26,16 @@ public Texture loadTexture(String filePath) {
2426
ProcessingTexture texture = new ProcessingTexture(image);
2527
return texture;
2628
}
29+
30+
@Override
31+
public Texture createTexture(Image image) {
32+
PImage pImage = new PImage(image);
33+
return new ProcessingTexture(pImage);
34+
}
35+
36+
@Override
37+
public Texture createTexture(int width, int height) {
38+
PImage pImage = new PImage(width, height);
39+
return new ProcessingTexture(pImage);
40+
}
2741
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package engine.resources;
2+
3+
/**
4+
* Enum representing the various filter modes available for texture sampling. These filter modes
5+
* determine how textures are sampled and filtered when applied to 3D models or surfaces, affecting
6+
* the appearance of textures when viewed at different distances or angles.
7+
*
8+
* <p>The available filter modes are:
9+
*
10+
* <ul>
11+
* <li><strong>POINT:</strong> A basic, nearest-neighbor sampling method where the texture pixel
12+
* closest to the screen pixel is selected.
13+
* <li><strong>LINEAR:</strong> A linear interpolation method that smooths between the two nearest
14+
* texture pixels to create a blend.
15+
* <li><strong>BILINEAR:</strong> A more advanced version of linear interpolation that considers
16+
* the four nearest texture pixels, interpolating in both x and y directions.
17+
* <li><strong>TRILINEAR:</strong> An extension of bilinear filtering that interpolates between
18+
* multiple mipmap levels, providing smoother transitions between textures at different
19+
* distances from the viewer.
20+
* </ul>
21+
*
22+
* Each mode offers a different trade-off between performance and visual quality.
23+
*
24+
* @see <a href="https://www.opengl.org/wiki/Texture_Filtering">OpenGL Wiki on Texture Filtering</a>
25+
*/
26+
public enum FilterMode {
27+
/**
28+
* Nearest-neighbor filtering, where the closest texel (texture pixel) is chosen. Produces a
29+
* blocky appearance when viewed from a distance.
30+
*/
31+
POINT,
32+
33+
/**
34+
* Linear interpolation between two nearest texels, offering smoother transitions compared to
35+
* POINT.
36+
*/
37+
LINEAR,
38+
39+
/**
40+
* Bilinear interpolation that considers the four nearest texels, interpolating in both x and y
41+
* directions. Provides smoother results than LINEAR.
42+
*/
43+
BILINEAR,
44+
45+
/**
46+
* Trilinear interpolation that blends between multiple mipmap levels in addition to performing
47+
* bilinear interpolation on each level. It smooths transitions between textures at varying
48+
* distances from the camera.
49+
*/
50+
TRILINEAR
51+
}

src/main/java/engine/resources/Texture.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,12 @@ public interface Texture {
1111
void unbind();
1212

1313
void delete();
14+
15+
void setPixels(int[] pixels);
16+
17+
FilterMode getFilterMode();
18+
19+
void setFilterMode(FilterMode filterMode);
20+
21+
Texture getBackendTexture();
1422
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package engine.resources;
2+
3+
public class Texture2D implements Texture {
4+
5+
private Texture texture;
6+
7+
public Texture2D(int width, int height) {
8+
texture = TextureManager.getInstance().createTexture(width, height);
9+
}
10+
11+
@Override
12+
public int getWidth() {
13+
return texture.getWidth();
14+
}
15+
16+
@Override
17+
public int getHeight() {
18+
return texture.getHeight();
19+
}
20+
21+
@Override
22+
public void bind(int unit) {
23+
texture.bind(unit);
24+
}
25+
26+
@Override
27+
public void unbind() {
28+
texture.unbind();
29+
}
30+
31+
@Override
32+
public void delete() {
33+
texture.delete();
34+
}
35+
36+
@Override
37+
public void setPixels(int[] pixels) {
38+
texture.setPixels(pixels);
39+
}
40+
41+
@Override
42+
public FilterMode getFilterMode() {
43+
return texture.getFilterMode();
44+
}
45+
46+
@Override
47+
public void setFilterMode(FilterMode filterMode) {
48+
texture.setFilterMode(filterMode);
49+
}
50+
51+
@Override
52+
public Texture getBackendTexture() {
53+
return texture;
54+
}
55+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
package engine.resources;
22

3+
import java.awt.Image;
4+
35
public interface TextureLoader {
46

57
Texture loadTexture(String filePath);
8+
9+
Texture createTexture(Image image);
10+
11+
Texture createTexture(int width, int height);
12+
613
}

src/main/java/engine/resources/TextureManager.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package engine.resources;
22

3+
import java.awt.Image;
34
import java.util.HashMap;
45
import java.util.Map;
56

67
public class TextureManager {
78

89
private static TextureManager instance;
910

10-
private TextureLoader imageLoader;
11+
private TextureLoader textureLoader;
1112

1213
private final Map<String, Texture> resourceCache = new HashMap<>();
1314

@@ -21,19 +22,19 @@ public static TextureManager getInstance() {
2122
}
2223

2324
public void setTextureLoader(TextureLoader loader) {
24-
this.imageLoader = loader;
25+
this.textureLoader = loader;
2526
}
2627

2728
public Texture loadTexture(String path) {
2829
if (resourceCache.containsKey(path)) {
2930
return resourceCache.get(path); // Return cached resource
3031
}
3132

32-
if (imageLoader == null) {
33-
throw new IllegalStateException("ImageLoader is not set!");
33+
if (textureLoader == null) {
34+
throw new IllegalStateException("TextureLoader is not set.");
3435
}
3536

36-
Texture texture = imageLoader.loadTexture(path);
37+
Texture texture = textureLoader.loadTexture(path);
3738
resourceCache.put(path, texture);
3839

3940
return texture;
@@ -42,4 +43,12 @@ public Texture loadTexture(String path) {
4243
public void unloadImage(String path) {
4344
resourceCache.remove(path); // Optionally handle cleanup for backend-specific resources
4445
}
46+
47+
public Texture createTexture(Image image) {
48+
return textureLoader.createTexture(image);
49+
}
50+
51+
public Texture createTexture(int width, int height) {
52+
return textureLoader.createTexture(width, height);
53+
}
4554
}

src/main/java/math/Mathf.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,29 @@ public static float lerp(float from, float to, float t) {
645645
return from + (to - from) * clamp01(t);
646646
}
647647

648+
/**
649+
* Calculates the interpolation factor of a value within a specified range.
650+
*
651+
* <p>The `inverseLerp` function computes the normalized position of a value `t` within the range
652+
* defined by `from` and `to`. It determines how far `t` lies between `from` and `to` on a scale
653+
* from 0 to 1. If `t` is less than `from`, it returns a negative value. If `t` is greater than
654+
* `to`, it returns a value greater than 1.
655+
*
656+
* @param from The start of the range.
657+
* @param to The end of the range.
658+
* @param t The value to normalize within the range.
659+
* @return The normalized position of `t` in the range `[from, to]`.
660+
* @throws IllegalArgumentException if `from` equals `to`, as this would result in division by
661+
* zero.
662+
*/
663+
public static float inverseLerp(float from, float to, float t) {
664+
if (from == to) {
665+
throw new IllegalArgumentException(
666+
"The start and end of the range cannot be the same (division by zero).");
667+
}
668+
return (t - from) / (to - from);
669+
}
670+
648671
/**
649672
* Returns the next power of two greater than or equal to the given value.
650673
*

0 commit comments

Comments
 (0)