From 60c9c7c1997e1783826e207c27833350d8faf3a2 Mon Sep 17 00:00:00 2001 From: Simon Dietz Date: Wed, 1 Jan 2025 11:07:58 +0100 Subject: [PATCH 01/15] Feat: Added texture creation option --- .../processing/ProcessingTextureLoader.java | 8 ++++++++ src/main/java/engine/resources/TextureLoader.java | 5 +++++ .../java/engine/resources/TextureManager.java | 15 ++++++++++----- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/main/java/engine/processing/ProcessingTextureLoader.java b/src/main/java/engine/processing/ProcessingTextureLoader.java index b1daa208..de760cde 100644 --- a/src/main/java/engine/processing/ProcessingTextureLoader.java +++ b/src/main/java/engine/processing/ProcessingTextureLoader.java @@ -1,5 +1,7 @@ package engine.processing; +import java.awt.Image; + import engine.resources.Texture; import engine.resources.TextureLoader; import processing.core.PApplet; @@ -24,4 +26,10 @@ 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); + } } diff --git a/src/main/java/engine/resources/TextureLoader.java b/src/main/java/engine/resources/TextureLoader.java index 6ea10108..d33df6a6 100644 --- a/src/main/java/engine/resources/TextureLoader.java +++ b/src/main/java/engine/resources/TextureLoader.java @@ -1,6 +1,11 @@ package engine.resources; +import java.awt.Image; + public interface TextureLoader { Texture loadTexture(String filePath); + + Texture createTexture(Image image); + } diff --git a/src/main/java/engine/resources/TextureManager.java b/src/main/java/engine/resources/TextureManager.java index bc70210e..4512de38 100644 --- a/src/main/java/engine/resources/TextureManager.java +++ b/src/main/java/engine/resources/TextureManager.java @@ -1,5 +1,6 @@ package engine.resources; +import java.awt.Image; import java.util.HashMap; import java.util.Map; @@ -7,7 +8,7 @@ public class TextureManager { private static TextureManager instance; - private TextureLoader imageLoader; + private TextureLoader textureLoader; private final Map resourceCache = new HashMap<>(); @@ -21,7 +22,7 @@ public static TextureManager getInstance() { } public void setTextureLoader(TextureLoader loader) { - this.imageLoader = loader; + this.textureLoader = loader; } public Texture loadTexture(String path) { @@ -29,11 +30,11 @@ public Texture loadTexture(String 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; @@ -42,4 +43,8 @@ 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); + } } From c9f561a4a0ee4eab615121034639e1c423132a47 Mon Sep 17 00:00:00 2001 From: Simon Dietz Date: Wed, 1 Jan 2025 22:39:57 +0100 Subject: [PATCH 02/15] Fix FlyByCamera Issue --- .../engine/application/BasicApplication.java | 3 +-- .../processing/ProcessingMouseInput.java | 24 +++++++++++++------ 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/main/java/engine/application/BasicApplication.java b/src/main/java/engine/application/BasicApplication.java index 3c3dab78..aa70cff2 100644 --- a/src/main/java/engine/application/BasicApplication.java +++ b/src/main/java/engine/application/BasicApplication.java @@ -105,8 +105,8 @@ public void update() { lastZ = input.isKeyPressed(Key.Z); } - timer.update(); + input.update(); fpsGraph.update(timer); debugInfoUpdater.update(timer, activeScene, input); @@ -139,7 +139,6 @@ public void render(Graphics g) { renderUi(g); renderDebugUi(g); - g.enableDepthTest(); } diff --git a/src/main/java/engine/processing/ProcessingMouseInput.java b/src/main/java/engine/processing/ProcessingMouseInput.java index 4799451a..aa89eb23 100644 --- a/src/main/java/engine/processing/ProcessingMouseInput.java +++ b/src/main/java/engine/processing/ProcessingMouseInput.java @@ -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; @@ -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 @@ -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() { From bfaa799e49f6756d8b6e3b4615b45b497dbfad3c Mon Sep 17 00:00:00 2001 From: Simon Dietz Date: Thu, 2 Jan 2025 11:56:13 +0100 Subject: [PATCH 03/15] feat(math): add new constructor and fix `set` method in `Plane` class - Added a new constructor to initialize the `Plane` with a normal vector and a distance. The normal vector is automatically normalized during initialization. - Fixed the `set` method to normalize both the normal vector and the distance parameter (`D`) consistently, ensuring correctness when updating plane parameters. --- src/main/java/math/Plane.java | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main/java/math/Plane.java b/src/main/java/math/Plane.java index ae8d5b28..a8403e42 100644 --- a/src/main/java/math/Plane.java +++ b/src/main/java/math/Plane.java @@ -31,6 +31,20 @@ public Plane() { this.distance = 0; } + /** + * Constructs a plane with a given normal vector and distance. + * + *

The normal vector is automatically normalized during this operation. + * + * @param normal the normal vector of the plane. + * @param distance the distance of the plane from the origin along its normal vector. + */ + public Plane(Vector3f normal, float distance) { + this.normal = new Vector3f(normal); + this.normal.normalizeLocal(); + this.distance = distance; + } + /** * Sets the plane parameters using its coefficients. * @@ -43,9 +57,10 @@ public Plane() { * @param d the distance from the origin along the plane's normal vector. */ public void set(float a, float b, float c, float d) { - this.normal.set(a, b, c); - normal.normalizeLocal(); - this.distance = d; + float length = (float) Math.sqrt(a * a + b * b + c * c); + if (length == 0) throw new IllegalArgumentException("Plane normal cannot be zero."); + this.normal.set(a / length, b / length, c / length); + this.distance = d / length; // Normalize the distance as well } /** From b51065b0c394fb3d0fd82cede493391a466dfa6f Mon Sep 17 00:00:00 2001 From: Simon Dietz Date: Thu, 2 Jan 2025 14:18:42 +0100 Subject: [PATCH 04/15] Feat: flip() --- src/main/java/math/Plane.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/java/math/Plane.java b/src/main/java/math/Plane.java index a8403e42..78c07fdd 100644 --- a/src/main/java/math/Plane.java +++ b/src/main/java/math/Plane.java @@ -45,6 +45,13 @@ public Plane(Vector3f normal, float distance) { this.distance = distance; } + /** Flips the orientation of the plane by inverting its normal and distance. */ + public void flip() { + normal.negateLocal(); + normal.normalizeLocal(); // Ensure the normal vector is normalized after negation + distance = distance == 0 ? 0 : -distance; // Avoid -0 + } + /** * Sets the plane parameters using its coefficients. * @@ -101,4 +108,9 @@ public Vector3f getNormal() { public float getDistance() { return distance; } + + @Override + public String toString() { + return "Plane [normal=" + normal + ", distance=" + distance + "]"; + } } From e4efde01edc4815031b056c7d4ec628dc1e67357 Mon Sep 17 00:00:00 2001 From: Simon Dietz Date: Thu, 2 Jan 2025 14:25:13 +0100 Subject: [PATCH 05/15] Added JavaDocs for drawImage methods in Graphics2D interface - Added detailed JavaDocs for the drawImage methods, including descriptions of parameters and behavior. - Provided explanations for both methods: one for drawing an image at a specified location without scaling, and the other for drawing and scaling the image to a specified width and height. --- src/main/java/workspace/ui/Graphics2D.java | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/main/java/workspace/ui/Graphics2D.java b/src/main/java/workspace/ui/Graphics2D.java index a1cbf700..029d2bfc 100644 --- a/src/main/java/workspace/ui/Graphics2D.java +++ b/src/main/java/workspace/ui/Graphics2D.java @@ -237,7 +237,31 @@ public interface Graphics2D { */ void clear(math.Color color); + /** + * Draws an image at the specified coordinates (x, y). + * + *

This method renders the provided image at the given position (x, y) on the screen, using the + * image's natural dimensions without scaling. + * + * @param image The {@link Image} object to render. + * @param x The x-coordinate where the image's top-left corner will be placed. + * @param y The y-coordinate where the image's top-left corner will be placed. + */ void drawImage(Image image, float x, float y); + /** + * Draws an image at the specified coordinates (x, y) and scales it to the specified width and + * height. + * + *

This method renders the provided image at the given position (x, y) on the screen, scaling + * the image to fit the specified width and height. The aspect ratio of the image may be distorted + * if the aspect ratio of the width and height differs from the image's natural dimensions. + * + * @param image The {@link Image} object to render. + * @param x The x-coordinate where the image's top-left corner will be placed. + * @param y The y-coordinate where the image's top-left corner will be placed. + * @param width The width to scale the image to. + * @param height The height to scale the image to. + */ void drawImage(Image image, float x, float y, float width, float height); } From b0569ad29505376df41e7c524a467ce2915c78d0 Mon Sep 17 00:00:00 2001 From: Simon Dietz Date: Thu, 2 Jan 2025 14:28:52 +0100 Subject: [PATCH 06/15] Added clear --- src/main/java/workspace/GraphicsPImpl.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/workspace/GraphicsPImpl.java b/src/main/java/workspace/GraphicsPImpl.java index 35a234bf..3284d7ca 100644 --- a/src/main/java/workspace/GraphicsPImpl.java +++ b/src/main/java/workspace/GraphicsPImpl.java @@ -391,8 +391,17 @@ public void setMaterial(Material material) { this.texture = null; - // Extract material properties math.Color color = material.getColor(); + // Apply material properties + setColor(color != null ? color : math.Color.WHITE); // Default to white + + if (!material.isUseLighting()) { + g.noLights(); + return; + } + + // Extract material properties + float[] ambient = material.getAmbient(); float[] diffuse = material.getDiffuse(); float[] specular = material.getSpecular(); @@ -415,9 +424,6 @@ public void setMaterial(Material material) { "Warning: Material specular property is null or incomplete. Using default."); } - // Apply material properties - setColor(color != null ? color : math.Color.WHITE); // Default to white - // Calculate and apply ambient color math.Color ambientColor = new math.Color(this.ambientColor); ambientColor.multLocal(ambient[0], ambient[1], ambient[2], 1.0f); From 7a4f4b2ba7f7d3597727a2b5143f156fb9ef7e8d Mon Sep 17 00:00:00 2001 From: Simon Dietz Date: Thu, 2 Jan 2025 16:32:44 +0100 Subject: [PATCH 07/15] Add method to add individual UV coordinates to Mesh class Description: Introduced addUvCoordinate(float u, float v) to allow appending individual UV coordinates to the mesh. This method facilitates flexible texture mapping by enabling incremental additions of u and v components. Updated JavaDocs to provide clear documentation of the method's purpose and usage. --- src/main/java/mesh/Mesh3D.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main/java/mesh/Mesh3D.java b/src/main/java/mesh/Mesh3D.java index ede95ebd..e9daa5d5 100644 --- a/src/main/java/mesh/Mesh3D.java +++ b/src/main/java/mesh/Mesh3D.java @@ -272,6 +272,21 @@ public Face3D getFaceAt(int index) { return faces.get(index); } + /** + * Adds a UV coordinate to the mesh. + * + *

This method appends a new UV coordinate, specified by the parameters {@code u} and {@code + * v}, to the list of UV coordinates for this mesh. UV coordinates are used for mapping textures + * to the surface of the mesh, where {@code u} and {@code v} typically represent the horizontal + * and vertical texture coordinates, respectively. + * + * @param u The horizontal component of the UV coordinate. + * @param v The vertical component of the UV coordinate. + */ + public void addUvCoordinate(float u, float v) { + uvs.add(new Vector2f(u, v)); + } + /** * Sets the UV coordinates for this mesh. * From 758b02eda1267e242c56c17a0f72309bc8d6a331 Mon Sep 17 00:00:00 2001 From: Simon Dietz Date: Thu, 2 Jan 2025 17:39:24 +0100 Subject: [PATCH 08/15] Feat: Added texture creation by size and set pixels option --- src/main/java/engine/processing/ProcessingTexture.java | 7 +++++++ .../java/engine/processing/ProcessingTextureLoader.java | 6 ++++++ src/main/java/engine/resources/Texture.java | 2 ++ src/main/java/engine/resources/TextureLoader.java | 2 ++ src/main/java/engine/resources/TextureManager.java | 4 ++++ 5 files changed, 21 insertions(+) diff --git a/src/main/java/engine/processing/ProcessingTexture.java b/src/main/java/engine/processing/ProcessingTexture.java index 585e6b31..07cb644c 100644 --- a/src/main/java/engine/processing/ProcessingTexture.java +++ b/src/main/java/engine/processing/ProcessingTexture.java @@ -36,6 +36,13 @@ 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; } diff --git a/src/main/java/engine/processing/ProcessingTextureLoader.java b/src/main/java/engine/processing/ProcessingTextureLoader.java index de760cde..60fd4991 100644 --- a/src/main/java/engine/processing/ProcessingTextureLoader.java +++ b/src/main/java/engine/processing/ProcessingTextureLoader.java @@ -32,4 +32,10 @@ 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); + } } diff --git a/src/main/java/engine/resources/Texture.java b/src/main/java/engine/resources/Texture.java index 53857bc4..7bbe3a1b 100644 --- a/src/main/java/engine/resources/Texture.java +++ b/src/main/java/engine/resources/Texture.java @@ -11,4 +11,6 @@ public interface Texture { void unbind(); void delete(); + + void setPixels(int[] pixels); } diff --git a/src/main/java/engine/resources/TextureLoader.java b/src/main/java/engine/resources/TextureLoader.java index d33df6a6..b58a5e1e 100644 --- a/src/main/java/engine/resources/TextureLoader.java +++ b/src/main/java/engine/resources/TextureLoader.java @@ -8,4 +8,6 @@ public interface TextureLoader { Texture createTexture(Image image); + Texture createTexture(int width, int height); + } diff --git a/src/main/java/engine/resources/TextureManager.java b/src/main/java/engine/resources/TextureManager.java index 4512de38..82f79c4a 100644 --- a/src/main/java/engine/resources/TextureManager.java +++ b/src/main/java/engine/resources/TextureManager.java @@ -47,4 +47,8 @@ public void unloadImage(String path) { public Texture createTexture(Image image) { return textureLoader.createTexture(image); } + + public Texture createTexture(int width, int height) { + return textureLoader.createTexture(width, height); + } } From 4a069bec7a7e491d236711ee90eb7245387011cf Mon Sep 17 00:00:00 2001 From: Simon Dietz Date: Thu, 2 Jan 2025 17:55:52 +0100 Subject: [PATCH 09/15] feat: add Mathf.inverseLerp method for calculating normalized interpolation Added the `Mathf.inverseLerp` method to compute the normalized position of a value within a specified range. This method is useful for linear interpolation, providing a way to map values proportionally between two points. Includes validation to prevent division by zero. --- src/main/java/math/Mathf.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/main/java/math/Mathf.java b/src/main/java/math/Mathf.java index f1cda93e..589f209f 100644 --- a/src/main/java/math/Mathf.java +++ b/src/main/java/math/Mathf.java @@ -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. + * + *

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. * From 19f9839c8d5d093d91312f21927030674e6752a4 Mon Sep 17 00:00:00 2001 From: Simon Dietz Date: Thu, 2 Jan 2025 20:50:09 +0100 Subject: [PATCH 10/15] Add FilterMode enum for texture sampling modes - Introduced FilterMode enum with POINT, LINEAR, BILINEAR, and TRILINEAR modes. - Provides different texture filtering options for improved control over texture quality and performance. --- .../java/engine/resources/FilterMode.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/main/java/engine/resources/FilterMode.java diff --git a/src/main/java/engine/resources/FilterMode.java b/src/main/java/engine/resources/FilterMode.java new file mode 100644 index 00000000..dd8a8387 --- /dev/null +++ b/src/main/java/engine/resources/FilterMode.java @@ -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. + * + *

The available filter modes are: + * + *

    + *
  • POINT: A basic, nearest-neighbor sampling method where the texture pixel + * closest to the screen pixel is selected. + *
  • LINEAR: A linear interpolation method that smooths between the two nearest + * texture pixels to create a blend. + *
  • BILINEAR: A more advanced version of linear interpolation that considers + * the four nearest texture pixels, interpolating in both x and y directions. + *
  • TRILINEAR: An extension of bilinear filtering that interpolates between + * multiple mipmap levels, providing smoother transitions between textures at different + * distances from the viewer. + *
+ * + * Each mode offers a different trade-off between performance and visual quality. + * + * @see OpenGL Wiki on Texture Filtering + */ +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 +} From 295bb9429a671cb873332e60153de0fa5b92012a Mon Sep 17 00:00:00 2001 From: Simon Dietz Date: Thu, 2 Jan 2025 20:54:04 +0100 Subject: [PATCH 11/15] Feat: filterMode --- .../engine/processing/ProcessingTexture.java | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/main/java/engine/processing/ProcessingTexture.java b/src/main/java/engine/processing/ProcessingTexture.java index 07cb644c..b5499bca 100644 --- a/src/main/java/engine/processing/ProcessingTexture.java +++ b/src/main/java/engine/processing/ProcessingTexture.java @@ -1,5 +1,6 @@ package engine.processing; +import engine.resources.FilterMode; import engine.resources.Texture; import processing.core.PImage; @@ -7,8 +8,11 @@ public class ProcessingTexture implements Texture { private final PImage image; + private FilterMode filterMode; + public ProcessingTexture(PImage image) { this.image = image; + this.filterMode = FilterMode.BILINEAR; } @Override @@ -38,12 +42,22 @@ public void delete() { @Override public void setPixels(int[] pixels) { - image.loadPixels(); - image.pixels = pixels; - image.updatePixels(); + 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; + } } From a6f029547b783d35ce7d65267d7c2f879c185d58 Mon Sep 17 00:00:00 2001 From: Simon Dietz Date: Thu, 2 Jan 2025 20:54:37 +0100 Subject: [PATCH 12/15] Feat: Handling filter modes of textures --- src/main/java/workspace/GraphicsPImpl.java | 66 ++++++++++++++++++++-- 1 file changed, 60 insertions(+), 6 deletions(-) diff --git a/src/main/java/workspace/GraphicsPImpl.java b/src/main/java/workspace/GraphicsPImpl.java index 3284d7ca..64009d73 100644 --- a/src/main/java/workspace/GraphicsPImpl.java +++ b/src/main/java/workspace/GraphicsPImpl.java @@ -6,6 +6,7 @@ import engine.processing.LightRendererImpl; import engine.processing.ProcessingTexture; import engine.render.Material; +import engine.resources.FilterMode; import engine.resources.Image; import engine.resources.Texture; import engine.scene.camera.Camera; @@ -47,7 +48,7 @@ public class GraphicsPImpl implements Graphics { public static int vertexCount = 0; - private PImage texture; + private ProcessingTexture texture; @Override public void setAmbientColor(math.Color ambientColor) { @@ -133,6 +134,62 @@ private void applyTransform(Matrix4f transform) { matrix[15]); } + /** + * Applies the specified texture to the current rendering context, setting the appropriate texture + * sampling mode based on the filter mode of the texture. + * + *

This method checks the texture's filter mode and applies the corresponding texture sampling + * mode. It also ensures that the texture is properly initialized before usage. + * + *

The available filter modes are mapped to the following sampling modes: + * + *

    + *
  • POINT -> 2 + *
  • LINEAR -> 3 + *
  • BILINEAR -> 4 + *
  • TRILINEAR -> 5 + *
+ * + * If an unexpected filter mode is encountered, a warning is logged, and the default BILINEAR mode + * (sampling mode 4) is applied. + * + * @throws IllegalArgumentException if the filter mode is unexpected and no fallback is defined. + * @see processing.opengl.Texture + */ + private void applyTexture() { + if (texture == null || texture.getImage() == null) { + return; // Ensure texture is properly initialized before applying it. + } + + FilterMode filterMode = texture.getFilterMode(); + int textureSampling; + + switch (filterMode) { + case POINT: + textureSampling = 2; + break; + case LINEAR: + textureSampling = 3; + break; + case BILINEAR: + textureSampling = 4; + break; + case TRILINEAR: + textureSampling = 5; + break; + default: + // Log a warning for unexpected filter modes (in case of future extensions). + System.err.println("Warning: Unexpected filter mode value: " + filterMode); + textureSampling = 4; // Default to BILINEAR if undefined + break; + } + + // Apply the texture with the corresponding sampling mode + ((PGraphicsOpenGL) g).textureSampling(textureSampling); + g.texture(texture.getImage()); + g.textureMode(PApplet.NORMAL); + } + private void drawMeshFaces(Mesh3D mesh) { for (Face3D f : mesh.getFaces()) { if (f.indices.length == 3) { @@ -143,10 +200,7 @@ private void drawMeshFaces(Mesh3D mesh) { g.beginShape(PApplet.POLYGON); } - if (texture != null) { - g.texture(texture); - g.textureMode(PApplet.NORMAL); - } + applyTexture(); int[] indices = f.indices; for (int i = 0; i < indices.length; i++) { @@ -447,7 +501,7 @@ public void bindTexture(Texture texture, int unit) { // TODO Auto-generated meth // g.textureMode(PApplet.NORMAL); // } ProcessingTexture texture2 = (ProcessingTexture) texture; - this.texture = texture2.getImage(); + this.texture = texture2; } @Override From 520ca8b06202281e2245a87a84888639f8b481df Mon Sep 17 00:00:00 2001 From: Simon Dietz Date: Thu, 2 Jan 2025 20:55:02 +0100 Subject: [PATCH 13/15] Feat: filterMode --- src/main/java/engine/resources/Texture.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/engine/resources/Texture.java b/src/main/java/engine/resources/Texture.java index 7bbe3a1b..d4015345 100644 --- a/src/main/java/engine/resources/Texture.java +++ b/src/main/java/engine/resources/Texture.java @@ -13,4 +13,8 @@ public interface Texture { void delete(); void setPixels(int[] pixels); + + FilterMode getFilterMode(); + + void setFilterMode(FilterMode filterMode); } From 10230d9541b7373413bdedd2fff1c0f03abc8278 Mon Sep 17 00:00:00 2001 From: Simon Dietz Date: Thu, 2 Jan 2025 21:14:55 +0100 Subject: [PATCH 14/15] Added Texture2D facade for user friendly texture creation --- src/main/java/engine/resources/Texture.java | 2 + src/main/java/engine/resources/Texture2D.java | 55 +++++++++++++++++++ src/main/java/workspace/GraphicsPImpl.java | 2 +- 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/main/java/engine/resources/Texture2D.java diff --git a/src/main/java/engine/resources/Texture.java b/src/main/java/engine/resources/Texture.java index d4015345..f4e8998c 100644 --- a/src/main/java/engine/resources/Texture.java +++ b/src/main/java/engine/resources/Texture.java @@ -17,4 +17,6 @@ public interface Texture { FilterMode getFilterMode(); void setFilterMode(FilterMode filterMode); + + Texture getBackendTexture(); } diff --git a/src/main/java/engine/resources/Texture2D.java b/src/main/java/engine/resources/Texture2D.java new file mode 100644 index 00000000..c8822caa --- /dev/null +++ b/src/main/java/engine/resources/Texture2D.java @@ -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; + } +} diff --git a/src/main/java/workspace/GraphicsPImpl.java b/src/main/java/workspace/GraphicsPImpl.java index 64009d73..12ceadf2 100644 --- a/src/main/java/workspace/GraphicsPImpl.java +++ b/src/main/java/workspace/GraphicsPImpl.java @@ -500,7 +500,7 @@ public void bindTexture(Texture texture, int unit) { // TODO Auto-generated meth // if (unit == 1) { // g.textureMode(PApplet.NORMAL); // } - ProcessingTexture texture2 = (ProcessingTexture) texture; + ProcessingTexture texture2 = (ProcessingTexture) texture.getBackendTexture(); this.texture = texture2; } From ec2bbd66a9093267c0b123b52fff9512e4537f0f Mon Sep 17 00:00:00 2001 From: Simon Dietz Date: Thu, 2 Jan 2025 21:19:01 +0100 Subject: [PATCH 15/15] Supporting backend texture. --- src/main/java/engine/processing/ProcessingTexture.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/engine/processing/ProcessingTexture.java b/src/main/java/engine/processing/ProcessingTexture.java index b5499bca..3712dcba 100644 --- a/src/main/java/engine/processing/ProcessingTexture.java +++ b/src/main/java/engine/processing/ProcessingTexture.java @@ -60,4 +60,9 @@ public FilterMode getFilterMode() { public void setFilterMode(FilterMode filterMode) { this.filterMode = filterMode; } + + @Override + public Texture getBackendTexture() { + return this; + } }