Possibility to retrieve objects by group. Reuse glowing point textures.
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / renderer / raster / shapes / basic / GlowingPoint.java
index 2a11748..4dd454f 100644 (file)
@@ -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<GlowingPoint> glowingPoints = Collections.newSetFromMap(new WeakHashMap<GlowingPoint, Boolean>());
+
     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++) {