5a77288a5fc4a88eb2742d3b43210be1d9ea2172
[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 public class Galaxy extends AbstractCompositeShape {
23
24     private static List<Color> colors;
25
26     public Galaxy(final int size, final int tailCount, final int starsCount,
27                   Transform transform) {
28
29         super(transform);
30
31         ensureColorsAreInitialized();
32
33         final double angle1 = Math.random() * 10;
34         final double angle2 = Math.random() * 10;
35
36         final double angleSin1 = Math.sin(angle1);
37         final double angleCos1 = Math.cos(angle1);
38         final double angleSin2 = Math.sin(angle2);
39         final double angleCos2 = Math.cos(angle2);
40
41         Random random = new Random();
42
43         for (int i = 1; i < starsCount; i++) {
44             final double b = Math.random() * 10;
45
46             final double s = (b * b) / 30;
47
48             final double v1 = (Math.random() * (11.5 - b)) / 3;
49             final double v1p = v1 / 2;
50
51             final double ane = ((Math.random() * (s / 2)) / tailCount) * 2;
52             final double sba = ((2 * Math.PI) / tailCount)
53                     * random.nextInt(tailCount);
54
55             final double x = (((Math.sin((b - sba) + ane) * s) + (Math.random() * v1)) - v1p)
56                     * size;
57             final double z = (((Math.cos((b - sba) + ane) * s) + (Math.random() * v1)) - v1p)
58                     * size;
59             final double y = ((Math.random() * v1) - v1p) * size;
60
61             final double x1 = (x * angleCos1) + (z * angleSin1);
62             final double z1 = (z * angleCos1) - (x * angleSin1);
63
64             final double y1 = (y * angleCos2) + (z1 * angleSin2);
65             final double z2 = (z1 * angleCos2) - (y * angleSin2);
66
67             addStar(new Point3D(x1, y1, z2));
68         }
69     }
70
71     private void addStar(final Point3D starLocation) {
72         addShape(
73                 new GlowingPoint(starLocation, 10,
74                         colors.get((int) (Math.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                             Math.random() + 0.5,
86                             Math.random() + 0.5,
87                             Math.random() + 0.5,
88                             255));
89     }
90
91 }