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