From a4176a2e53a8b0475d69091c49529e87bb505c52 Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Tue, 13 Jun 2017 10:53:20 +0300 Subject: [PATCH] Possibility to retrieve objects by group. Reuse glowing point textures. --- .../sixth/e3d/renderer/raster/Color.java | 21 +++++++++++++++++++ .../shapes/basic/ForwardOrientedTexture.java | 4 ++++ .../raster/shapes/basic/GlowingPoint.java | 16 ++++++++++++-- .../base/AbstractCompositeShape.java | 7 +++++++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/Color.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/Color.java index 7081837..e21de2d 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/Color.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/Color.java @@ -135,4 +135,25 @@ public final class Color { return toAwtColor().getRGB(); } + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Color color = (Color) o; + + if (r != color.r) return false; + if (g != color.g) return false; + if (b != color.b) return false; + return a == color.a; + } + + @Override + public int hashCode() { + int result = r; + result = 31 * result + g; + result = 31 * result + b; + result = 31 * result + a; + return result; + } } diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/ForwardOrientedTexture.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/ForwardOrientedTexture.java index 3025596..798dcef 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/ForwardOrientedTexture.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/ForwardOrientedTexture.java @@ -105,4 +105,8 @@ public class ForwardOrientedTexture extends AbstractCoordinateShape { this.scale = scale * SIZE_MULTIPLIER; } + public Point3D getLocation(){ + return coordinates[0].coordinate; + } + } diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/GlowingPoint.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/GlowingPoint.java index 2a11748..4dd454f 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/GlowingPoint.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/GlowingPoint.java @@ -13,21 +13,33 @@ import eu.svjatoslav.sixth.e3d.geometry.Point3D; import eu.svjatoslav.sixth.e3d.renderer.raster.Color; import eu.svjatoslav.sixth.e3d.renderer.raster.texture.Texture; +import java.util.Collections; +import java.util.Set; +import java.util.WeakHashMap; + public class GlowingPoint extends ForwardOrientedTexture { private static final int TEXTURE_SIZE = 50; private final Color color; + private static final Set glowingPoints = Collections.newSetFromMap(new WeakHashMap()); + public GlowingPoint(final Point3D point, final double pointSize, final Color color) { super(point, pointSize, getTexture(color)); this.color = color; + } - getTexture(color); + private static Texture getTexture(final Color color) { + // attempt to reuse texture from existing glowing point of the same color + for (GlowingPoint glowingPoint : glowingPoints) + if (color.equals(glowingPoint.color)) return glowingPoint.texture; + // existing texture not found, creating new one + return createTexture(color); } - public static Texture getTexture(final Color color) { + private static Texture createTexture(final Color color) { final Texture texture = new Texture(TEXTURE_SIZE, TEXTURE_SIZE, 1); for (int x = 0; x < TEXTURE_SIZE; x++) for (int y = 0; y < TEXTURE_SIZE; y++) { diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/base/AbstractCompositeShape.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/base/AbstractCompositeShape.java index 54729ae..99f21c3 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/base/AbstractCompositeShape.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/base/AbstractCompositeShape.java @@ -25,6 +25,7 @@ import eu.svjatoslav.sixth.e3d.renderer.raster.slicer.Slicer; import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; /** * In order to get perspective correct textures, large textured polygons are @@ -119,6 +120,12 @@ public class AbstractCompositeShape extends AbstractShape { } } + public List getGroup(final String groupIdentifier) { + return originalSubShapes.stream().filter( + subShape -> subShape.matchesGroup(groupIdentifier)) + .collect(Collectors.toList()); + } + private void resliceIfNeeded() { final double proposedSliceFactor = relativityTracker -- 2.20.1