8b37068369d16ef1b4a1c7bedf6f571cdfa409aa
[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 public class GlowingPoint extends ForwardOrientedTexture {
21
22     private static final int TEXTURE_SIZE = 50;
23     private static final Set<GlowingPoint> glowingPoints = Collections.newSetFromMap(new WeakHashMap<GlowingPoint, Boolean>());
24     private final Color color;
25
26     public GlowingPoint(final Point3D point, final double pointSize,
27                         final Color color) {
28         super(point, pointSize, getTexture(color));
29         this.color = color;
30
31         synchronized (glowingPoints) {
32             glowingPoints.add(this);
33         }
34     }
35
36     private static Texture getTexture(final Color color) {
37         // attempt to reuse texture from existing glowing point of the same color
38         synchronized (glowingPoints) {
39             for (GlowingPoint glowingPoint : glowingPoints)
40                 if (color.equals(glowingPoint.color))
41                     return glowingPoint.texture;
42         }
43
44         // existing texture not found, creating new one
45         return createTexture(color);
46     }
47
48     private static Texture createTexture(final Color color) {
49         final Texture texture = new Texture(TEXTURE_SIZE, TEXTURE_SIZE, 1);
50         for (int x = 0; x < TEXTURE_SIZE; x++)
51             for (int y = 0; y < TEXTURE_SIZE; y++) {
52                 int address = texture.primaryBitmap.getAddress(x, y);
53
54                 final int distance = (int) Math
55                         .sqrt((((TEXTURE_SIZE / 2) - x) * ((TEXTURE_SIZE / 2) - x))
56                                 + (((TEXTURE_SIZE / 2) - y) * ((TEXTURE_SIZE / 2) - y)));
57
58                 int alpha = 255 - ((270 * distance) / (TEXTURE_SIZE / 2));
59                 if (alpha < 0)
60                     alpha = 0;
61
62                 texture.primaryBitmap.bytes[address] = (byte) alpha;
63                 address++;
64                 texture.primaryBitmap.bytes[address] = (byte) color.b;
65                 address++;
66                 texture.primaryBitmap.bytes[address] = (byte) color.g;
67                 address++;
68                 texture.primaryBitmap.bytes[address] = (byte) color.r;
69             }
70
71         return texture;
72     }
73
74 }