2 * Sixth 3D engine. Copyright ©2012-2017, 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 final Color color;
25 private static final Set<GlowingPoint> glowingPoints = Collections.newSetFromMap(new WeakHashMap<GlowingPoint, Boolean>());
27 public GlowingPoint(final Point3D point, final double pointSize,
29 super(point, pointSize, getTexture(color));
32 synchronized (glowingPoints){
33 glowingPoints.add(this);
37 private static Texture getTexture(final Color color) {
38 // attempt to reuse texture from existing glowing point of the same color
39 synchronized (glowingPoints) {
40 for (GlowingPoint glowingPoint : glowingPoints)
41 if (color.equals(glowingPoint.color))
42 return glowingPoint.texture;
45 // existing texture not found, creating new one
46 return createTexture(color);
49 private static Texture createTexture(final Color color) {
50 final Texture texture = new Texture(TEXTURE_SIZE, TEXTURE_SIZE, 1);
51 for (int x = 0; x < TEXTURE_SIZE; x++)
52 for (int y = 0; y < TEXTURE_SIZE; y++) {
53 int address = texture.primaryBitmap.getAddress(x, y);
55 final int distance = (int) Math
56 .sqrt((((TEXTURE_SIZE / 2) - x) * ((TEXTURE_SIZE / 2) - x))
57 + (((TEXTURE_SIZE / 2) - y) * ((TEXTURE_SIZE / 2) - y)));
59 int alpha = 255 - ((270 * distance) / (TEXTURE_SIZE / 2));
63 texture.primaryBitmap.bytes[address] = (byte) alpha;
65 texture.primaryBitmap.bytes[address] = (byte) color.b;
67 texture.primaryBitmap.bytes[address] = (byte) color.g;
69 texture.primaryBitmap.bytes[address] = (byte) color.r;