Updated readability of the code.
[sixth-3d-demos.git] / src / main / java / eu / svjatoslav / sixth / e3d / examples / RandomPolygonsDemo.java
1 /*
2  * Sixth 3D engine demos. Author: Svjatoslav Agejenko. 
3  * This project is released under Creative Commons Zero (CC0) license.
4  *
5 */
6
7 package eu.svjatoslav.sixth.e3d.examples;
8
9 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
10 import eu.svjatoslav.sixth.e3d.gui.ViewFrame;
11 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
12 import eu.svjatoslav.sixth.e3d.renderer.raster.ShapeCollection;
13 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.line.LineAppearance;
14 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.solidpolygon.SolidPolygon;
15 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.wireframe.Grid3D;
16
17 public class RandomPolygonsDemo {
18
19     private static final double POLYGON_AVERAGE_SIZE = 130;
20     private static final int POLYGON_COUNT = 1000;
21
22     private static void addRandomPolygon(final ShapeCollection geometryCollection) {
23         final Point3D polygonLocation = getRandomPoint(1000);
24
25         final Point3D point1 = new Point3D(polygonLocation);
26         point1.add(getRandomPoint(POLYGON_AVERAGE_SIZE));
27
28         final Point3D point2 = new Point3D(polygonLocation);
29         point2.add(getRandomPoint(POLYGON_AVERAGE_SIZE));
30
31         final Point3D point3 = new Point3D(polygonLocation);
32         point3.add(getRandomPoint(POLYGON_AVERAGE_SIZE));
33
34         final Color color = new Color(
35                 getColorChannelBrightness(),
36                 getColorChannelBrightness(),
37                 getColorChannelBrightness(),
38                 1);
39
40         final SolidPolygon polygon = new SolidPolygon(point1, point2, point3,
41                 color);
42         geometryCollection.addShape(polygon);
43     }
44
45     /* I don't want very dark polygons, so ensure there is at least some
46      * brightness present. */
47     private static double getColorChannelBrightness() {
48         return Math.random() * 0.7 + 0.3f;
49     }
50
51     private static Point3D getRandomPoint(final double amplitude) {
52         return new Point3D((Math.random() * amplitude * 2d) - amplitude,
53                 (Math.random() * amplitude * 2d) - amplitude, (Math.random()
54                 * amplitude * 2d)
55                 - amplitude);
56     }
57
58     public static void main(final String[] args) {
59
60         final ViewFrame viewFrame = new ViewFrame();
61
62         final ShapeCollection shapeCollection = viewFrame.getViewPanel()
63                 .getRootShapeCollection();
64
65         // add grid
66         final LineAppearance appearance = new LineAppearance(5, new Color(100,
67                 100, 255, 60));
68
69         shapeCollection.addShape(new Grid3D(new Point3D(1000, -1000, -1000),
70                 new Point3D(-1000, 1000, 1000), 300, appearance));
71
72         // add random polygons
73         for (int i = 0; i < POLYGON_COUNT; i++)
74             addRandomPolygon(shapeCollection);
75
76     }
77 }