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