Improved code readability
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / renderer / raster / shapes / basic / GlowingPoint.java
1 /*
2  * Sixth 3D engine. Author: Svjatoslav Agejenko. 
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic;
6
7 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
8 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
9 import eu.svjatoslav.sixth.e3d.renderer.raster.texture.Texture;
10
11 import java.util.Collections;
12 import java.util.Set;
13 import java.util.WeakHashMap;
14
15 import static java.lang.Math.pow;
16 import static java.lang.Math.sqrt;
17
18 public class GlowingPoint extends ForwardOrientedTexture {
19
20     private static final int TEXTURE_RESOLUTION_PIXELS = 100;
21     private static final Set<GlowingPoint> glowingPoints = Collections.newSetFromMap(new WeakHashMap<>());
22     private final Color color;
23
24     public GlowingPoint(final Point3D point, final double pointSize,
25                         final Color color) {
26         super(point, computeScale(pointSize), getTexture(color));
27         this.color = color;
28
29         synchronized (glowingPoints) {
30             glowingPoints.add(this);
31         }
32     }
33
34     private static double computeScale(double pointSize) {
35         return pointSize / ((double) (TEXTURE_RESOLUTION_PIXELS / 50f));
36     }
37
38     private static Texture getTexture(final Color color) {
39         // attempt to reuse texture from existing glowing point of the same color
40         synchronized (glowingPoints) {
41             for (GlowingPoint glowingPoint : glowingPoints)
42                 if (color.equals(glowingPoint.color))
43                     return glowingPoint.texture;
44         }
45
46         // existing texture not found, creating new one
47         return createTexture(color);
48     }
49
50     private static Texture createTexture(final Color color) {
51         final Texture texture = new Texture(TEXTURE_RESOLUTION_PIXELS, TEXTURE_RESOLUTION_PIXELS, 1);
52         int halfResolution = TEXTURE_RESOLUTION_PIXELS / 2;
53
54         for (int x = 0; x < TEXTURE_RESOLUTION_PIXELS; x++)
55             for (int y = 0; y < TEXTURE_RESOLUTION_PIXELS; y++) {
56                 int address = texture.primaryBitmap.getAddress(x, y);
57
58                 final int distanceFromCenter = (int) sqrt(pow (halfResolution - x, 2) + pow (halfResolution - y, 2));
59
60                 int alpha = 255 - ((270 * distanceFromCenter) / halfResolution);
61                 if (alpha < 0)
62                     alpha = 0;
63
64                 texture.primaryBitmap.bytes[address] = (byte) alpha;
65                 address++;
66                 texture.primaryBitmap.bytes[address] = (byte) color.b;
67                 address++;
68                 texture.primaryBitmap.bytes[address] = (byte) color.g;
69                 address++;
70                 texture.primaryBitmap.bytes[address] = (byte) color.r;
71             }
72
73         return texture;
74     }
75
76 }