2 * Sixth 3D engine. Copyright ©2012-2018, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
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.
10 package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic;
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;
16 import java.util.Collections;
18 import java.util.WeakHashMap;
20 public class GlowingPoint extends ForwardOrientedTexture {
22 private static final int TEXTURE_SIZE = 50;
23 private static final Set<GlowingPoint> glowingPoints = Collections.newSetFromMap(new WeakHashMap<>());
24 private final Color color;
26 public GlowingPoint(final Point3D point, final double pointSize,
28 super(point, pointSize, getTexture(color));
31 synchronized (glowingPoints) {
32 glowingPoints.add(this);
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;
44 // existing texture not found, creating new one
45 return createTexture(color);
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);
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)));
58 int alpha = 255 - ((270 * distance) / (TEXTURE_SIZE / 2));
62 texture.primaryBitmap.bytes[address] = (byte) alpha;
64 texture.primaryBitmap.bytes[address] = (byte) color.b;
66 texture.primaryBitmap.bytes[address] = (byte) color.g;
68 texture.primaryBitmap.bytes[address] = (byte) color.r;