2 * Sixth 3D engine. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic;
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;
11 import java.util.Collections;
13 import java.util.WeakHashMap;
15 import static java.lang.Math.pow;
16 import static java.lang.Math.sqrt;
18 public class GlowingPoint extends ForwardOrientedTexture {
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;
24 public GlowingPoint(final Point3D point, final double pointSize,
26 super(point, computeScale(pointSize), getTexture(color));
29 synchronized (glowingPoints) {
30 glowingPoints.add(this);
34 private static double computeScale(double pointSize) {
35 return pointSize / ((double) (TEXTURE_RESOLUTION_PIXELS / 50f));
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;
46 // existing texture not found, creating new one
47 return createTexture(color);
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;
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);
58 final int distanceFromCenter = (int) sqrt(pow (halfResolution - x, 2) + pow (halfResolution - y, 2));
60 int alpha = 255 - ((270 * distanceFromCenter) / halfResolution);
64 texture.primaryBitmap.bytes[address] = (byte) alpha;
66 texture.primaryBitmap.bytes[address] = (byte) color.b;
68 texture.primaryBitmap.bytes[address] = (byte) color.g;
70 texture.primaryBitmap.bytes[address] = (byte) color.r;