Updated readability of the code.
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / renderer / octree / raytracer / RayTracer.java
index 5137b5a..8f7923f 100755 (executable)
@@ -1,15 +1,10 @@
 /*
- * Sixth 3D engine. Copyright ©2012-2017, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 3 of the GNU Lesser General Public License
- * or later as published by the Free Software Foundation.
- *
+ * Sixth 3D engine. Author: Svjatoslav Agejenko. 
+ * This project is released under Creative Commons Zero (CC0) license.
  */
-
 package eu.svjatoslav.sixth.e3d.renderer.octree.raytracer;
 
-import eu.svjatoslav.sixth.e3d.gui.View;
+import eu.svjatoslav.sixth.e3d.gui.ViewPanel;
 import eu.svjatoslav.sixth.e3d.renderer.octree.OctreeVolume;
 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
 import eu.svjatoslav.sixth.e3d.renderer.raster.texture.Texture;
@@ -21,20 +16,20 @@ public class RayTracer implements Runnable {
     private static final int PROGRESS_UPDATE_FREQUENCY_MILLIS = 1000;
     private final Camera camera;
     private final Texture texture;
-    private final View view;
-    private OctreeVolume octreeVolume;
-    private Vector<LightSource> lights;
+    private final ViewPanel viewPanel;
+    private final OctreeVolume octreeVolume;
+    private final Vector<LightSource> lights;
     private int computedLights;
 
     public RayTracer(final Texture texture, final OctreeVolume octreeVolume,
                      final Vector<LightSource> lights, final Camera camera,
-                     final View view) {
+                     final ViewPanel viewPanel) {
 
         this.texture = texture;
         this.octreeVolume = octreeVolume;
         this.lights = lights;
         this.camera = camera;
-        this.view = view;
+        this.viewPanel = viewPanel;
     }
 
     @Override
@@ -56,25 +51,25 @@ public class RayTracer implements Runnable {
         final CameraView cameraView = camera.getCameraView();
 
         // calculate vertical vectors
-        final double x1p = cameraView.downLeft.x - cameraView.upLeft.x;
-        final double y1p = cameraView.downLeft.y - cameraView.upLeft.y;
-        final double z1p = cameraView.downLeft.z - cameraView.upLeft.z;
+        final double x1p = cameraView.bottomLeft.x - cameraView.topLeft.x;
+        final double y1p = cameraView.bottomLeft.y - cameraView.topLeft.y;
+        final double z1p = cameraView.bottomLeft.z - cameraView.topLeft.z;
 
-        final double x2p = cameraView.downRight.x - cameraView.upRight.x;
-        final double y2p = cameraView.downRight.y - cameraView.upRight.y;
-        final double z2p = cameraView.downRight.z - cameraView.upRight.z;
+        final double x2p = cameraView.bottomRight.x - cameraView.topRight.x;
+        final double y2p = cameraView.bottomRight.y - cameraView.topRight.y;
+        final double z2p = cameraView.bottomRight.z - cameraView.topRight.z;
 
         long nextBitmapUpdate = System.currentTimeMillis()
                 + PROGRESS_UPDATE_FREQUENCY_MILLIS;
 
         for (int y = 0; y < height; y++) {
-            final double cx1 = cameraView.upLeft.x + ((x1p * y) / height);
-            final double cy1 = cameraView.upLeft.y + ((y1p * y) / height);
-            final double cz1 = cameraView.upLeft.z + ((z1p * y) / height);
+            final double cx1 = cameraView.topLeft.x + ((x1p * y) / height);
+            final double cy1 = cameraView.topLeft.y + ((y1p * y) / height);
+            final double cz1 = cameraView.topLeft.z + ((z1p * y) / height);
 
-            final double cx2 = cameraView.upRight.x + ((x2p * y) / height);
-            final double cy2 = cameraView.upRight.y + ((y2p * y) / height);
-            final double cz2 = cameraView.upRight.z + ((z2p * y) / height);
+            final double cx2 = cameraView.topRight.x + ((x2p * y) / height);
+            final double cy2 = cameraView.topRight.y + ((y2p * y) / height);
+            final double cz2 = cameraView.topRight.z + ((z2p * y) / height);
 
             // calculate horisontal vector
             final double x3p = cx2 - cx1;
@@ -86,11 +81,11 @@ public class RayTracer implements Runnable {
                 final double cy3 = cy1 + ((y3p * x) / width);
                 final double cz3 = cz1 + ((z3p * x) / width);
 
-                final Ray r = new Ray(cameraView.camCenter.x,
-                        cameraView.camCenter.y, cameraView.camCenter.z, cx3
-                        - cameraView.camCenter.x, cy3
-                        - cameraView.camCenter.y, cz3
-                        - cameraView.camCenter.z);
+                final Ray r = new Ray(cameraView.cameraCenter.x,
+                        cameraView.cameraCenter.y, cameraView.cameraCenter.z, cx3
+                        - cameraView.cameraCenter.x, cy3
+                        - cameraView.cameraCenter.y, cz3
+                        - cameraView.cameraCenter.z);
                 final int c = traceRay(r);
 
                 final Color color = new Color(c);
@@ -101,42 +96,35 @@ public class RayTracer implements Runnable {
                 nextBitmapUpdate = System.currentTimeMillis()
                         + PROGRESS_UPDATE_FREQUENCY_MILLIS;
                 texture.resetResampledBitmapCache();
-                view.repaintDuringNextViewUpdate();
+                viewPanel.repaintDuringNextViewUpdate();
             }
         }
 
         texture.resetResampledBitmapCache();
-        view.repaintDuringNextViewUpdate();
-        // System.out.println("Raytracing done.");
-        // System.out.println("New lights computed:" + computedLights);
-    }
-
-    public int traceLight(final LightSource l, final int cubeX,
-                          final int cubeY, final int cubeZ) {
-        return 0;
+        viewPanel.repaintDuringNextViewUpdate();
     }
 
-    public int traceRay(final Ray r) {
+    private int traceRay(final Ray ray) {
 
-        final int re = octreeVolume.traceCell(0, 0, 0,
-                octreeVolume.masterCellSize, 0, r);
+        final int intersectingCell = octreeVolume.traceCell(0, 0, 0,
+                octreeVolume.masterCellSize, 0, ray);
 
-        if (re != -1) {
+        if (intersectingCell != -1) {
             // if lightening not computed, compute it
-            if (octreeVolume.ce3[re] == -1)
+            if (octreeVolume.ce3[intersectingCell] == -1)
                 // if cell is larger than 1
-                if (r.hitCellSize > 1) {
+                if (ray.hitCellSize > 1) {
                     // break it up
-                    octreeVolume.breakSolidCell(re);
-                    return traceRay(r);
+                    octreeVolume.breakSolidCell(intersectingCell);
+                    return traceRay(ray);
                 } else {
                     computedLights++;
                     float red = 30, green = 30, blue = 30;
 
                     for (final LightSource l : lights) {
-                        final int xDist = (l.x - r.hitCellX);
-                        final int yDist = (l.y - r.hitCellY);
-                        final int zDist = (l.z - r.hitCellZ);
+                        final double xDist = (l.location.x - ray.hitCellX);
+                        final double yDist = (l.location.y - ray.hitCellY);
+                        final double zDist = (l.location.z - ray.hitCellZ);
 
                         double newRed = 0, newGreen = 0, newBlue = 0;
                         double tempRed, tempGreen, tempBlue;
@@ -145,12 +133,12 @@ public class RayTracer implements Runnable {
                                 + (yDist * yDist) + (zDist * zDist));
                         distance = (distance / 3) + 1;
 
-                        final Ray r1 = new Ray(r.hitCellX, r.hitCellY
-                                - (float) 1.5, r.hitCellZ,
+                        final Ray r1 = new Ray(ray.hitCellX, ray.hitCellY
+                                - (float) 1.5, ray.hitCellZ,
 
-                                (float) l.x - (float) r.hitCellX, l.y
-                                - (r.hitCellY - (float) 1.5), (float) l.z
-                                - (float) r.hitCellZ);
+                                (float) l.location.x - (float) ray.hitCellX, l.location.y
+                                - (ray.hitCellY - (float) 1.5), (float) l.location.z
+                                - (float) ray.hitCellZ);
 
                         final int rt1 = octreeVolume.traceCell(0, 0, 0,
                                 octreeVolume.masterCellSize, 0, r1);
@@ -161,12 +149,12 @@ public class RayTracer implements Runnable {
                             newBlue = (l.color.b * l.brightness) / distance;
                         }
 
-                        final Ray r2 = new Ray(r.hitCellX - (float) 1.5,
-                                r.hitCellY, r.hitCellZ,
+                        final Ray r2 = new Ray(ray.hitCellX - (float) 1.5,
+                                ray.hitCellY, ray.hitCellZ,
 
-                                l.x - (r.hitCellX - (float) 1.5), (float) l.y
-                                - (float) r.hitCellY, (float) l.z
-                                - (float) r.hitCellZ);
+                                l.location.x - (ray.hitCellX - (float) 1.5), (float) l.location.y
+                                - (float) ray.hitCellY, (float) l.location.z
+                                - (float) ray.hitCellZ);
 
                         final int rt2 = octreeVolume.traceCell(0, 0, 0,
                                 octreeVolume.masterCellSize, 0, r2);
@@ -184,12 +172,12 @@ public class RayTracer implements Runnable {
                                 newBlue = tempBlue;
                         }
 
-                        final Ray r3 = new Ray(r.hitCellX, r.hitCellY,
-                                r.hitCellZ - (float) 1.5,
+                        final Ray r3 = new Ray(ray.hitCellX, ray.hitCellY,
+                                ray.hitCellZ - (float) 1.5,
 
-                                (float) l.x - (float) r.hitCellX, (float) l.y
-                                - (float) r.hitCellY, l.z
-                                - (r.hitCellZ - (float) 1.5));
+                                (float) l.location.x - (float) ray.hitCellX, (float) l.location.y
+                                - (float) ray.hitCellY, l.location.z
+                                - (ray.hitCellZ - (float) 1.5));
 
                         final int rt3 = octreeVolume.traceCell(0, 0, 0,
                                 octreeVolume.masterCellSize, 0, r3);
@@ -206,12 +194,12 @@ public class RayTracer implements Runnable {
                                 newBlue = tempBlue;
                         }
 
-                        final Ray r4 = new Ray(r.hitCellX, r.hitCellY
-                                + (float) 1.5, r.hitCellZ,
+                        final Ray r4 = new Ray(ray.hitCellX, ray.hitCellY
+                                + (float) 1.5, ray.hitCellZ,
 
-                                (float) l.x - (float) r.hitCellX, l.y
-                                - (r.hitCellY + (float) 1.5), (float) l.z
-                                - (float) r.hitCellZ);
+                                (float) l.location.x - (float) ray.hitCellX, l.location.y
+                                - (ray.hitCellY + (float) 1.5), (float) l.location.z
+                                - (float) ray.hitCellZ);
 
                         final int rt4 = octreeVolume.traceCell(0, 0, 0,
                                 octreeVolume.masterCellSize, 0, r4);
@@ -228,12 +216,12 @@ public class RayTracer implements Runnable {
                                 newBlue = tempBlue;
                         }
 
-                        final Ray r5 = new Ray(r.hitCellX + (float) 1.5,
-                                r.hitCellY, r.hitCellZ,
+                        final Ray r5 = new Ray(ray.hitCellX + (float) 1.5,
+                                ray.hitCellY, ray.hitCellZ,
 
-                                l.x - (r.hitCellX + (float) 1.5), (float) l.y
-                                - (float) r.hitCellY, (float) l.z
-                                - (float) r.hitCellZ);
+                                l.location.x - (ray.hitCellX + (float) 1.5), (float) l.location.y
+                                - (float) ray.hitCellY, (float) l.location.z
+                                - (float) ray.hitCellZ);
 
                         final int rt5 = octreeVolume.traceCell(0, 0, 0,
                                 octreeVolume.masterCellSize, 0, r5);
@@ -250,12 +238,12 @@ public class RayTracer implements Runnable {
                                 newBlue = tempBlue;
                         }
 
-                        final Ray r6 = new Ray(r.hitCellX, r.hitCellY,
-                                r.hitCellZ + (float) 1.5,
+                        final Ray r6 = new Ray(ray.hitCellX, ray.hitCellY,
+                                ray.hitCellZ + (float) 1.5,
 
-                                (float) l.x - (float) r.hitCellX, (float) l.y
-                                - (float) r.hitCellY, l.z
-                                - (r.hitCellZ + (float) 1.5));
+                                (float) l.location.x - (float) ray.hitCellX, (float) l.location.y
+                                - (float) ray.hitCellY, l.location.z
+                                - (ray.hitCellZ + (float) 1.5));
 
                         final int rt6 = octreeVolume.traceCell(0, 0, 0,
                                 octreeVolume.masterCellSize, 0, r6);
@@ -277,7 +265,7 @@ public class RayTracer implements Runnable {
 
                     }
 
-                    final int cellColor = octreeVolume.ce2[re];
+                    final int cellColor = octreeVolume.ce2[intersectingCell];
 
                     red = (red * ((cellColor & 0xFF0000) >> 16)) / 255;
                     green = (green * ((cellColor & 0xFF00) >> 8)) / 255;
@@ -290,13 +278,13 @@ public class RayTracer implements Runnable {
                     if (blue > 255)
                         blue = 255;
 
-                    octreeVolume.ce3[re] = (((int) red) << 16)
+                    octreeVolume.ce3[intersectingCell] = (((int) red) << 16)
                             + (((int) green) << 8) + ((int) blue);
 
                 }
-            if (octreeVolume.ce3[re] == 0)
-                return octreeVolume.ce2[re];
-            return octreeVolume.ce3[re];
+            if (octreeVolume.ce3[intersectingCell] == 0)
+                return octreeVolume.ce2[intersectingCell];
+            return octreeVolume.ce3[intersectingCell];
         }
 
         // return (200 << 16) + (200 << 8) + 255;