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