Galaxy and glowing point visual improvements.
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / renderer / raster / shapes / basic / GlowingPoint.java
1 /*
2  * Sixth 3D engine. Copyright ©2012-2018, 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 import static java.lang.Math.pow;
21 import static java.lang.Math.sqrt;
22
23 public class GlowingPoint extends ForwardOrientedTexture {
24
25     private static final int TEXTURE_RESOLUTION_PIXELS = 100;
26     private static final Set<GlowingPoint> glowingPoints = Collections.newSetFromMap(new WeakHashMap<>());
27     private final Color color;
28
29     public GlowingPoint(final Point3D point, final double pointSize,
30                         final Color color) {
31         super(point, computeScale(pointSize), getTexture(color));
32         this.color = color;
33
34         synchronized (glowingPoints) {
35             glowingPoints.add(this);
36         }
37     }
38
39     private static double computeScale(double pointSize) {
40         return pointSize / ((double) (TEXTURE_RESOLUTION_PIXELS / 50f));
41     }
42
43     private static Texture getTexture(final Color color) {
44         // attempt to reuse texture from existing glowing point of the same color
45         synchronized (glowingPoints) {
46             for (GlowingPoint glowingPoint : glowingPoints)
47                 if (color.equals(glowingPoint.color))
48                     return glowingPoint.texture;
49         }
50
51         // existing texture not found, creating new one
52         return createTexture(color);
53     }
54
55     private static Texture createTexture(final Color color) {
56         final Texture texture = new Texture(TEXTURE_RESOLUTION_PIXELS, TEXTURE_RESOLUTION_PIXELS, 1);
57         int halfResolution = TEXTURE_RESOLUTION_PIXELS / 2;
58
59         for (int x = 0; x < TEXTURE_RESOLUTION_PIXELS; x++)
60             for (int y = 0; y < TEXTURE_RESOLUTION_PIXELS; y++) {
61                 int address = texture.primaryBitmap.getAddress(x, y);
62
63                 final int distanceFromCenter = (int) sqrt(pow (halfResolution - x, 2) + pow (halfResolution - y, 2));
64
65                 int alpha = 255 - ((270 * distanceFromCenter) / halfResolution);
66                 if (alpha < 0)
67                     alpha = 0;
68
69                 texture.primaryBitmap.bytes[address] = (byte) alpha;
70                 address++;
71                 texture.primaryBitmap.bytes[address] = (byte) color.b;
72                 address++;
73                 texture.primaryBitmap.bytes[address] = (byte) color.g;
74                 address++;
75                 texture.primaryBitmap.bytes[address] = (byte) color.r;
76             }
77
78         return texture;
79     }
80
81 }