initial commit
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / renderer / raster / shapes / composite / Galaxy.java
1 /*
2  * Sixth - System for data storage, computation, exploration and interaction.
3  * Copyright ©2012-2016, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 3 of the GNU Lesser General Public License
7  * or later as published by the Free Software Foundation.
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.geometry.Transform;
14 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
15 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.ForwardOrientedTexture;
16 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.GlowingPoint;
17 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.base.AbstractCompositeShape;
18 import eu.svjatoslav.sixth.e3d.renderer.raster.texture.Texture;
19
20 import java.util.Random;
21 import java.util.Vector;
22
23 public class Galaxy extends AbstractCompositeShape {
24
25     private Vector<Texture> textures;
26
27     public Galaxy(final int size, final int tailCount, final int starsCount,
28                   Transform transform) {
29
30         super(transform);
31
32         Point3D location = new Point3D();
33
34         createReusableTextures();
35
36         final double angle1 = Math.random() * 10;
37         final double angle2 = Math.random() * 10;
38
39         final double angleSin1 = Math.sin(angle1);
40         final double angleCos1 = Math.cos(angle1);
41         final double angleSin2 = Math.sin(angle2);
42         final double angleCos2 = Math.cos(angle2);
43
44         Random random = new Random();
45
46         for (int i = 1; i < starsCount; i++) {
47             final double b = Math.random() * 10;
48
49             final double s = (b * b) / 30;
50
51             final double v1 = (Math.random() * (11.5 - b)) / 3;
52             final double v1p = v1 / 2;
53
54             final double ane = ((Math.random() * (s / 2)) / tailCount) * 2;
55             final double sba = ((2 * Math.PI) / tailCount)
56                     * random.nextInt(tailCount);
57
58             final double x = (((Math.sin((b - sba) + ane) * s) + (Math.random() * v1)) - v1p)
59                     * size;
60             final double z = (((Math.cos((b - sba) + ane) * s) + (Math.random() * v1)) - v1p)
61                     * size;
62             final double y = ((Math.random() * v1) - v1p) * size;
63
64             final double x1 = (x * angleCos1) + (z * angleSin1);
65             final double z1 = (z * angleCos1) - (x * angleSin1);
66
67             final double y1 = (y * angleCos2) + (z1 * angleSin2);
68             final double z2 = (z1 * angleCos2) - (y * angleSin2);
69
70             final Point3D point3d = new Point3D(x1 + location.x, y1
71                     + location.y, z2 + location.z);
72
73             addStar(point3d);
74         }
75     }
76
77     public void addStar(final Point3D point3d) {
78
79         final Texture texture = textures.get((int) (Math.random() * textures
80                 .size()));
81
82         addShape(new ForwardOrientedTexture(point3d, 10, texture));
83     }
84
85     public void createReusableTextures() {
86         textures = new Vector<>();
87
88         for (int i = 0; i < 30; i++) {
89             final GlowingPoint glowingPoint = new GlowingPoint(null, 1,
90                     new Color(Math.random() + 0.5, Math.random() + 0.5,
91                             Math.random() + 0.5, 255));
92
93             textures.add(glowingPoint.texture);
94
95         }
96     }
97
98 }