Removed ViewListener interface. Renamed View to ViewPanel.
[sixth-3d-demos.git] / src / main / java / eu / svjatoslav / sixth / e3d / examples / OctreeDemo.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.math.Transform;
13 import eu.svjatoslav.sixth.e3d.gui.ViewContext;
14 import eu.svjatoslav.sixth.e3d.gui.ViewFrame;
15 import eu.svjatoslav.sixth.e3d.gui.humaninput.WorldNavigationTracker;
16 import eu.svjatoslav.sixth.e3d.renderer.octree.OctreeVolume;
17 import eu.svjatoslav.sixth.e3d.renderer.octree.raytracer.Camera;
18 import eu.svjatoslav.sixth.e3d.renderer.octree.raytracer.LightSource;
19 import eu.svjatoslav.sixth.e3d.renderer.octree.raytracer.RayTracer;
20 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
21 import eu.svjatoslav.sixth.e3d.renderer.raster.ShapeCollection;
22 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.GlowingPoint;
23 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.line.LineAppearance;
24 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.LightSourceMarker;
25 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.solid.SolidPolygonRectangularBox;
26 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.textcanvas.TextCanvas;
27 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.wireframe.Grid3D;
28
29 import java.awt.event.KeyEvent;
30 import java.util.Vector;
31
32 public class OctreeDemo extends WorldNavigationTracker {
33
34     private static final double magnification = 5;
35     private final LineAppearance gridAppearance = new LineAppearance(40, new Color(255,
36             0, 0, 60));
37     private final Vector<LightSource> lights = new Vector<>();
38     private OctreeVolume octreeVolume;
39     private ShapeCollection shapeCollection;
40     private ViewContext context;
41
42     public static void main(final String[] args) {
43         new OctreeDemo().init();
44     }
45
46     private void addLight(final Point3D location, final Color color,
47                           final float brightness) {
48         shapeCollection.addShape(new LightSourceMarker(new Point3D(location)
49                 .scaleUp(magnification), color));
50
51         final LightSource lightSource = new LightSource(location, color,
52                 brightness);
53
54         lights.add(lightSource);
55     }
56
57     private void dotSpiral() {
58         for (double i = 0; i < 20; i = i + .1) {
59
60             double w = 1;
61             double h = 1;
62
63             final double x = Math.sin(i) * 20f * h;
64             final double y = Math.cos(i) * 20f * w;
65             final double c1 = (Math.cos(i * 3f) * 100) + 127;
66             final double c2 = (Math.cos(i * 5.3332f) * 100f) + 127;
67             final double c3 = (Math.cos(i * 1.342f) * 100f) + 127;
68
69             putPixel((int) x, (int) y, (int) (i * 4f), new Color((int) c1,
70                     (int) c2, (int) c3, 255));
71         }
72     }
73
74     private void fractal(final int x, final int y, final int z, final int size,
75                          final int step) {
76         final double c1 = (Math.cos(y / 7f) * 100f) + 127;
77         final double c2 = (Math.cos(x / 10f) * 100f) + 127;
78         final double c3 = (Math.cos(z / 12f) * 100f) + 127;
79
80         putRect(x - size, y - size, z - size, x + size, y + size, z + size,
81                 new Color((int) c1, (int) c2, (int) c3, 100));
82
83         if (size > 1) {
84             fractal(x, y - (size * 3), z, size / 2, step + 1);
85             fractal(x + (size * 3), y, z, size / 2, step + 1);
86             fractal(x, y, z + (size * 3), size / 2, step + 1);
87         }
88     }
89
90     private void init() {
91
92         final ViewFrame viewFrame = new ViewFrame();
93         context = viewFrame.getViewPanel().getContext();
94
95         context.getAvatar().setLocation(new Point3D(0, -30, -300));
96
97         octreeVolume = new OctreeVolume();
98
99         shapeCollection = context.getRootShapeCollection();
100
101         shapeCollection.addShape(new Grid3D(
102                 new Point3D(-10000, -10000, -10000), new Point3D(10000, 10000,
103                 10000), 4000, gridAppearance));
104
105         // yellow light
106         addLight(new Point3D(20, -450, 240), new Color(255, 255, 255, 255), 100);
107
108         // red light
109         addLight(new Point3D(-150, -116, 141), new Color(255, 0, 0, 255), 10);
110
111         dotSpiral();
112
113         // arbitrary rectangles
114         putRect(-10, -10, -10, 10, 10, -20, new Color(200, 255, 200, 100));
115         putRect(-3, 0, -30, 12, 3, 300, new Color(255, 200, 200, 100));
116         putRect(-20, 20, -20, 20, 80, 20, new Color(255, 200, 255, 100));
117
118         tiledFloor();
119
120         fractal(-50, 20, 100, 32, 1);
121
122         final TextCanvas message = new TextCanvas(new Transform(new Point3D(
123                 -10, 20, -180)), "Press \"r\" to raytrace current wiew",
124                 Color.WHITE, Color.PURPLE);
125         shapeCollection.addShape(message);
126
127         context.getKeyboardFocusTracker().setFocusOwner(this);
128         context.getViewPanel().repaintDuringNextViewUpdate();
129     }
130
131     @Override
132     public void keyPressed(final KeyEvent event, final ViewContext viewContext) {
133
134         if ('r' == event.getKeyChar()) {
135             raytrace();
136             return;
137         }
138         super.keyPressed(event, viewContext);
139     }
140
141     private void putPixel(final int x, final int y, final int z,
142                           final Color color) {
143         shapeCollection.addShape(new GlowingPoint(new Point3D(x, y, z)
144                 .scaleUp(magnification), 3 * magnification, color));
145         octreeVolume.putCell(x, y, z, color);
146
147     }
148
149     private void putRect(final int x1, final int y1, final int z1, final int x2,
150                          final int y2, final int z2, final Color color) {
151
152         shapeCollection
153                 .addShape(new SolidPolygonRectangularBox(
154                         new Point3D(x1, y1, z1).scaleUp(magnification),
155                         new Point3D(x2, y2, z2).scaleUp(magnification), color));
156
157         octreeVolume.fillRect3D(x1, y1, z1, x2, y2, z2, color);
158     }
159
160     private void raytrace() {
161         // create and add camera object to scene
162         final Camera camera = new Camera(context.getAvatar(), magnification);
163         shapeCollection.addShape(camera);
164
165         // initialize and start Raytracer in a separate thread
166         final RayTracer rayTracer = new RayTracer(camera.getTexture(),
167                 octreeVolume, lights, camera, context.getViewPanel());
168         final Thread thread = new Thread(rayTracer);
169         thread.start();
170     }
171
172     private void tiledFloor() {
173         final int step = 40;
174         final int size = step - 15;
175         for (int x = -200; x < 200; x += step)
176             for (int z = -200; z < 200; z += step)
177                 putRect(x, 100, z, x + size, 110, z + size, new Color(255, 255,
178                         255, 100));
179     }
180
181 }