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