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 import static java.lang.Math.pow;
21 import static java.lang.Math.sqrt;
23 public class GlowingPoint extends ForwardOrientedTexture {
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;
29 public GlowingPoint(final Point3D point, final double pointSize,
31 super(point, computeScale(pointSize), getTexture(color));
34 synchronized (glowingPoints) {
35 glowingPoints.add(this);
39 private static double computeScale(double pointSize) {
40 return pointSize / ((double) (TEXTURE_RESOLUTION_PIXELS / 50f));
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;
51 // existing texture not found, creating new one
52 return createTexture(color);
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;
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);
63 final int distanceFromCenter = (int) sqrt(pow (halfResolution - x, 2) + pow (halfResolution - y, 2));
65 int alpha = 255 - ((270 * distanceFromCenter) / halfResolution);
69 texture.primaryBitmap.bytes[address] = (byte) alpha;
71 texture.primaryBitmap.bytes[address] = (byte) color.b;
73 texture.primaryBitmap.bytes[address] = (byte) color.g;
75 texture.primaryBitmap.bytes[address] = (byte) color.r;