Enable glowing point texture caching.
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / renderer / raster / shapes / basic / GlowingPoint.java
1 /*
2  * Sixth 3D engine. Copyright ©2012-2016, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 3 of the GNU Lesser General Public License
6  * or later as published by the Free Software Foundation.
7  *
8  */
9
10 package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic;
11
12 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
13 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
14 import eu.svjatoslav.sixth.e3d.renderer.raster.texture.Texture;
15
16 import java.util.Collections;
17 import java.util.Set;
18 import java.util.WeakHashMap;
19
20 public class GlowingPoint extends ForwardOrientedTexture {
21
22     private static final int TEXTURE_SIZE = 50;
23     private final Color color;
24
25     private static final Set<GlowingPoint> glowingPoints = Collections.newSetFromMap(new WeakHashMap<GlowingPoint, Boolean>());
26
27     public GlowingPoint(final Point3D point, final double pointSize,
28                         final Color color) {
29         super(point, pointSize, getTexture(color));
30         this.color = color;
31
32         synchronized (glowingPoints){
33             glowingPoints.add(this);
34         }
35     }
36
37     private static Texture getTexture(final Color color) {
38         // attempt to reuse texture from existing glowing point of the same color
39         synchronized (glowingPoints) {
40             for (GlowingPoint glowingPoint : glowingPoints)
41                 if (color.equals(glowingPoint.color))
42                     return glowingPoint.texture;
43         }
44
45         // existing texture not found, creating new one
46         return createTexture(color);
47     }
48
49     private static Texture createTexture(final Color color) {
50         final Texture texture = new Texture(TEXTURE_SIZE, TEXTURE_SIZE, 1);
51         for (int x = 0; x < TEXTURE_SIZE; x++)
52             for (int y = 0; y < TEXTURE_SIZE; y++) {
53                 int address = texture.primaryBitmap.getAddress(x, y);
54
55                 final int distance = (int) Math
56                         .sqrt((((TEXTURE_SIZE / 2) - x) * ((TEXTURE_SIZE / 2) - x))
57                                 + (((TEXTURE_SIZE / 2) - y) * ((TEXTURE_SIZE / 2) - y)));
58
59                 int alpha = 255 - ((270 * distance) / (TEXTURE_SIZE / 2));
60                 if (alpha < 0)
61                     alpha = 0;
62
63                 texture.primaryBitmap.bytes[address] = (byte) alpha;
64                 address++;
65                 texture.primaryBitmap.bytes[address] = (byte) color.b;
66                 address++;
67                 texture.primaryBitmap.bytes[address] = (byte) color.g;
68                 address++;
69                 texture.primaryBitmap.bytes[address] = (byte) color.r;
70             }
71
72         return texture;
73     }
74
75 }