Skip to content

Commit

Permalink
Merge pull request #59 from ArtifactForms/working2
Browse files Browse the repository at this point in the history
Working2
  • Loading branch information
ArtifactForms authored Jan 2, 2025
2 parents d904467 + ec2bbd6 commit b334fda
Show file tree
Hide file tree
Showing 14 changed files with 356 additions and 28 deletions.
3 changes: 1 addition & 2 deletions src/main/java/engine/application/BasicApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ public void update() {

lastZ = input.isKeyPressed(Key.Z);
}

timer.update();
input.update();
fpsGraph.update(timer);
debugInfoUpdater.update(timer, activeScene, input);

Expand Down Expand Up @@ -139,7 +139,6 @@ public void render(Graphics g) {
renderUi(g);
renderDebugUi(g);


g.enableDepthTest();
}

Expand Down
24 changes: 17 additions & 7 deletions src/main/java/engine/processing/ProcessingMouseInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ public class ProcessingMouseInput implements MouseInput {
private final PApplet applet;

private float mouseWheelDelta = 0;

private float mouseX;
private float mouseY;
private float pMouseX;
private float pMouseY;

private Robot robot;

Expand Down Expand Up @@ -51,32 +56,32 @@ public float getScreenHeight() {

@Override
public float getMouseX() {
return applet.mouseX;
return mouseX;
}

@Override
public float getMouseY() {
return applet.mouseY;
return mouseY;
}

@Override
public float getLastMouseX() {
return applet.pmouseX;
return pMouseX;
}

@Override
public float getLastMouseY() {
return applet.pmouseY;
return pMouseY;
}

@Override
public float getMouseDeltaX() {
return applet.mouseX - applet.pmouseX;
return mouseX - pMouseX;
}

@Override
public float getMouseDeltaY() {
return applet.mouseY - applet.pmouseY;
return mouseY - pMouseY;
}

@Override
Expand All @@ -87,7 +92,12 @@ public float getMouseWheelDelta() {
}

@Override
public void updateMouseState() {}
public void updateMouseState() {
this.mouseX = applet.mouseX;
this.mouseY = applet.mouseY;
this.pMouseX = applet.pmouseX;
this.pMouseY = applet.pmouseY;
}

@Override
public void center() {
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/engine/processing/ProcessingTexture.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package engine.processing;

import engine.resources.FilterMode;
import engine.resources.Texture;
import processing.core.PImage;

public class ProcessingTexture implements Texture {

private final PImage image;

private FilterMode filterMode;

public ProcessingTexture(PImage image) {
this.image = image;
this.filterMode = FilterMode.BILINEAR;
}

@Override
Expand Down Expand Up @@ -36,7 +40,29 @@ public void delete() {
// Processing handles memory management automatically
}

@Override
public void setPixels(int[] pixels) {
image.loadPixels();
image.pixels = pixels;
image.updatePixels();
}

public PImage getImage() {
return image;
}

@Override
public FilterMode getFilterMode() {
return filterMode;
}

@Override
public void setFilterMode(FilterMode filterMode) {
this.filterMode = filterMode;
}

@Override
public Texture getBackendTexture() {
return this;
}
}
14 changes: 14 additions & 0 deletions src/main/java/engine/processing/ProcessingTextureLoader.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package engine.processing;

import java.awt.Image;

import engine.resources.Texture;
import engine.resources.TextureLoader;
import processing.core.PApplet;
Expand All @@ -24,4 +26,16 @@ public Texture loadTexture(String filePath) {
ProcessingTexture texture = new ProcessingTexture(image);
return texture;
}

@Override
public Texture createTexture(Image image) {
PImage pImage = new PImage(image);
return new ProcessingTexture(pImage);
}

@Override
public Texture createTexture(int width, int height) {
PImage pImage = new PImage(width, height);
return new ProcessingTexture(pImage);
}
}
51 changes: 51 additions & 0 deletions src/main/java/engine/resources/FilterMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package engine.resources;

/**
* Enum representing the various filter modes available for texture sampling. These filter modes
* determine how textures are sampled and filtered when applied to 3D models or surfaces, affecting
* the appearance of textures when viewed at different distances or angles.
*
* <p>The available filter modes are:
*
* <ul>
* <li><strong>POINT:</strong> A basic, nearest-neighbor sampling method where the texture pixel
* closest to the screen pixel is selected.
* <li><strong>LINEAR:</strong> A linear interpolation method that smooths between the two nearest
* texture pixels to create a blend.
* <li><strong>BILINEAR:</strong> A more advanced version of linear interpolation that considers
* the four nearest texture pixels, interpolating in both x and y directions.
* <li><strong>TRILINEAR:</strong> An extension of bilinear filtering that interpolates between
* multiple mipmap levels, providing smoother transitions between textures at different
* distances from the viewer.
* </ul>
*
* Each mode offers a different trade-off between performance and visual quality.
*
* @see <a href="https://www.opengl.org/wiki/Texture_Filtering">OpenGL Wiki on Texture Filtering</a>
*/
public enum FilterMode {
/**
* Nearest-neighbor filtering, where the closest texel (texture pixel) is chosen. Produces a
* blocky appearance when viewed from a distance.
*/
POINT,

/**
* Linear interpolation between two nearest texels, offering smoother transitions compared to
* POINT.
*/
LINEAR,

/**
* Bilinear interpolation that considers the four nearest texels, interpolating in both x and y
* directions. Provides smoother results than LINEAR.
*/
BILINEAR,

/**
* Trilinear interpolation that blends between multiple mipmap levels in addition to performing
* bilinear interpolation on each level. It smooths transitions between textures at varying
* distances from the camera.
*/
TRILINEAR
}
8 changes: 8 additions & 0 deletions src/main/java/engine/resources/Texture.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@ public interface Texture {
void unbind();

void delete();

void setPixels(int[] pixels);

FilterMode getFilterMode();

void setFilterMode(FilterMode filterMode);

Texture getBackendTexture();
}
55 changes: 55 additions & 0 deletions src/main/java/engine/resources/Texture2D.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package engine.resources;

public class Texture2D implements Texture {

private Texture texture;

public Texture2D(int width, int height) {
texture = TextureManager.getInstance().createTexture(width, height);
}

@Override
public int getWidth() {
return texture.getWidth();
}

@Override
public int getHeight() {
return texture.getHeight();
}

@Override
public void bind(int unit) {
texture.bind(unit);
}

@Override
public void unbind() {
texture.unbind();
}

@Override
public void delete() {
texture.delete();
}

@Override
public void setPixels(int[] pixels) {
texture.setPixels(pixels);
}

@Override
public FilterMode getFilterMode() {
return texture.getFilterMode();
}

@Override
public void setFilterMode(FilterMode filterMode) {
texture.setFilterMode(filterMode);
}

@Override
public Texture getBackendTexture() {
return texture;
}
}
7 changes: 7 additions & 0 deletions src/main/java/engine/resources/TextureLoader.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package engine.resources;

import java.awt.Image;

public interface TextureLoader {

Texture loadTexture(String filePath);

Texture createTexture(Image image);

Texture createTexture(int width, int height);

}
19 changes: 14 additions & 5 deletions src/main/java/engine/resources/TextureManager.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package engine.resources;

import java.awt.Image;
import java.util.HashMap;
import java.util.Map;

public class TextureManager {

private static TextureManager instance;

private TextureLoader imageLoader;
private TextureLoader textureLoader;

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

Expand All @@ -21,19 +22,19 @@ public static TextureManager getInstance() {
}

public void setTextureLoader(TextureLoader loader) {
this.imageLoader = loader;
this.textureLoader = loader;
}

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

if (imageLoader == null) {
throw new IllegalStateException("ImageLoader is not set!");
if (textureLoader == null) {
throw new IllegalStateException("TextureLoader is not set.");
}

Texture texture = imageLoader.loadTexture(path);
Texture texture = textureLoader.loadTexture(path);
resourceCache.put(path, texture);

return texture;
Expand All @@ -42,4 +43,12 @@ public Texture loadTexture(String path) {
public void unloadImage(String path) {
resourceCache.remove(path); // Optionally handle cleanup for backend-specific resources
}

public Texture createTexture(Image image) {
return textureLoader.createTexture(image);
}

public Texture createTexture(int width, int height) {
return textureLoader.createTexture(width, height);
}
}
23 changes: 23 additions & 0 deletions src/main/java/math/Mathf.java
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,29 @@ public static float lerp(float from, float to, float t) {
return from + (to - from) * clamp01(t);
}

/**
* Calculates the interpolation factor of a value within a specified range.
*
* <p>The `inverseLerp` function computes the normalized position of a value `t` within the range
* defined by `from` and `to`. It determines how far `t` lies between `from` and `to` on a scale
* from 0 to 1. If `t` is less than `from`, it returns a negative value. If `t` is greater than
* `to`, it returns a value greater than 1.
*
* @param from The start of the range.
* @param to The end of the range.
* @param t The value to normalize within the range.
* @return The normalized position of `t` in the range `[from, to]`.
* @throws IllegalArgumentException if `from` equals `to`, as this would result in division by
* zero.
*/
public static float inverseLerp(float from, float to, float t) {
if (from == to) {
throw new IllegalArgumentException(
"The start and end of the range cannot be the same (division by zero).");
}
return (t - from) / (to - from);
}

/**
* Returns the next power of two greater than or equal to the given value.
*
Expand Down
Loading

0 comments on commit b334fda

Please sign in to comment.