initial commit
[sixth-3d-demos.git] / src / main / java / eu / svjatoslav / sixth / e3d / examples / SphereDemo.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.examples;
11
12 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
13 import eu.svjatoslav.sixth.e3d.gui.ViewContext;
14 import eu.svjatoslav.sixth.e3d.gui.ViewFrame;
15 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
16 import eu.svjatoslav.sixth.e3d.renderer.raster.ShapeCollection;
17 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.line.LineAppearance;
18 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.solidpolygon.SolidPolygon;
19 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.wireframe.WireframeSphere;
20
21 public class SphereDemo {
22
23     private static final double WAVE_FREQUENCY = 50d;
24     private static final double WAVE_AMPLITUDE = 50d;
25     private static final Color SQUARE_PLATE_COLOR = new Color("88F7");
26
27     private static void makeSquarePlate(final ShapeCollection shapeCollection,
28                                         final double y, final double x, final double z) {
29         final Point3D p1 = new Point3D(x, y, z);
30         final Point3D p2 = new Point3D(x + 20, y, z);
31         final Point3D p3 = new Point3D(x, y, z + 20);
32         final Point3D p4 = new Point3D(x + 20, y, z + 20);
33         final SolidPolygon polygon1 = new SolidPolygon(p1, p2, p3, SQUARE_PLATE_COLOR);
34         final SolidPolygon polygon2 = new SolidPolygon(p4, p2, p3, SQUARE_PLATE_COLOR);
35         shapeCollection.addShape(polygon1);
36         shapeCollection.addShape(polygon2);
37     }
38
39     /**
40      * @param surfaceElevation surface total elevation
41      */
42     private static void makeWobblySurface(final ShapeCollection shapeCollection,
43                                           final double surfaceElevation) {
44         for (double x = -500; x < 500; x += 20)
45             for (double z = -500; z < 500; z += 20) {
46
47                 // use Pythagorean theorem to compute distance from the center
48                 final double distanceFromCenter = Math.sqrt((x * x) + (z * z));
49
50                 double plateElevation = Math.sin(distanceFromCenter / WAVE_FREQUENCY) * WAVE_AMPLITUDE;
51
52                 makeSquarePlate(shapeCollection, plateElevation + surfaceElevation, x,
53                         z);
54             }
55     }
56
57     public static void main(final String[] args) {
58
59         final ViewFrame viewFrame = new ViewFrame();
60         final ViewContext context = viewFrame.getView().getContext();
61
62         final ShapeCollection geometryCollection = context
63                 .getRootShapeCollection();
64
65         final LineAppearance appearance = new LineAppearance(4, new Color(255,
66                 0, 0, 30));
67
68         // add sphere
69         geometryCollection.addShape(new WireframeSphere(new Point3D(0, 0, 0),
70                 100, appearance));
71
72         // create floor
73         makeWobblySurface(geometryCollection, 200);
74         makeWobblySurface(geometryCollection, -200);
75
76         context.getAvatar().setLocation(new Point3D(0, 0, -340));
77
78     }
79 }