Galaxy and glowing point visual improvements.
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / renderer / raster / shapes / composite / Galaxy.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.composite;
11
12 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
13 import eu.svjatoslav.sixth.e3d.math.Transform;
14 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
15 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.GlowingPoint;
16 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.base.AbstractCompositeShape;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Random;
21
22 import static java.lang.Math.*;
23
24 public class Galaxy extends AbstractCompositeShape {
25
26     private static List<Color> colors;
27
28     public Galaxy(final int galaxySize, final int tailCount, final int starsCount,
29                   Transform transform) {
30
31         super(transform);
32
33         ensureColorsAreInitialized();
34
35         final double angle1 = random() * 10;
36         final double angle2 = random() * 10;
37
38         final double angleSin1 = sin(angle1);
39         final double angleCos1 = cos(angle1);
40         final double angleSin2 = sin(angle2);
41         final double angleCos2 = cos(angle2);
42
43         Random random = new Random();
44
45         double starSize = galaxySize / 70d;
46
47         for (int i = 1; i < starsCount; i++) {
48             final double b = random() * 10;
49
50             final double s = (b * b) / 30;
51
52             final double v1 = (random() * (11.5 - b)) / 3;
53             final double v1p = v1 / 2;
54
55             final double ane = ((random() * (s / 2)) / tailCount) * 2;
56             final double sba = ((2 * PI) / tailCount)
57                     * random.nextInt(tailCount);
58
59             final double x = (((sin((b - sba) + ane) * s) + (random() * v1)) - v1p) * galaxySize;
60             final double z = (((cos((b - sba) + ane) * s) + (random() * v1)) - v1p) * galaxySize;
61             final double y = ((random() * v1) - v1p) * galaxySize;
62
63             final double x1 = (x * angleCos1) + (z * angleSin1);
64             final double z1 = (z * angleCos1) - (x * angleSin1);
65
66             final double y1 = (y * angleCos2) + (z1 * angleSin2);
67             final double z2 = (z1 * angleCos2) - (y * angleSin2);
68
69             addStar(new Point3D(x1, y1, z2), starSize);
70         }
71     }
72
73     private void addStar(final Point3D starLocation, double size) {
74         addShape(new GlowingPoint(starLocation, size, colors.get((int) (random() * colors.size()))));
75     }
76
77     private synchronized void ensureColorsAreInitialized() {
78         if (colors != null) return;
79
80         colors = new ArrayList<>();
81
82         for (int i = 0; i < 30; i++)
83             colors.add(
84                     new Color(
85                             random() + 0.5,
86                             random() + 0.5,
87                             random() + 0.5,
88                             255));
89     }
90
91 }