Code cleanup and formatting.
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / renderer / octree / raytracer / RayTracer.java
index 5137b5a..79ae21c 100755 (executable)
@@ -1,5 +1,5 @@
 /*
- * Sixth 3D engine. Copyright ©2012-2017, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ * Sixth 3D engine. Copyright ©2012-2018, 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
@@ -9,7 +9,7 @@
 
 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 +21,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 final ViewPanel viewPanel;
     private OctreeVolume octreeVolume;
     private 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
@@ -101,42 +101,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);
+        viewPanel.repaintDuringNextViewUpdate();
     }
 
-    public int traceLight(final LightSource l, final int cubeX,
-                          final int cubeY, final int cubeZ) {
-        return 0;
-    }
-
-    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 int xDist = (l.x - ray.hitCellX);
+                        final int yDist = (l.y - ray.hitCellY);
+                        final int zDist = (l.z - ray.hitCellZ);
 
                         double newRed = 0, newGreen = 0, newBlue = 0;
                         double tempRed, tempGreen, tempBlue;
@@ -145,12 +138,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.x - (float) ray.hitCellX, l.y
+                                - (ray.hitCellY - (float) 1.5), (float) l.z
+                                - (float) ray.hitCellZ);
 
                         final int rt1 = octreeVolume.traceCell(0, 0, 0,
                                 octreeVolume.masterCellSize, 0, r1);
@@ -161,12 +154,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.x - (ray.hitCellX - (float) 1.5), (float) l.y
+                                - (float) ray.hitCellY, (float) l.z
+                                - (float) ray.hitCellZ);
 
                         final int rt2 = octreeVolume.traceCell(0, 0, 0,
                                 octreeVolume.masterCellSize, 0, r2);
@@ -184,12 +177,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.x - (float) ray.hitCellX, (float) l.y
+                                - (float) ray.hitCellY, l.z
+                                - (ray.hitCellZ - (float) 1.5));
 
                         final int rt3 = octreeVolume.traceCell(0, 0, 0,
                                 octreeVolume.masterCellSize, 0, r3);
@@ -206,12 +199,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.x - (float) ray.hitCellX, l.y
+                                - (ray.hitCellY + (float) 1.5), (float) l.z
+                                - (float) ray.hitCellZ);
 
                         final int rt4 = octreeVolume.traceCell(0, 0, 0,
                                 octreeVolume.masterCellSize, 0, r4);
@@ -228,12 +221,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.x - (ray.hitCellX + (float) 1.5), (float) l.y
+                                - (float) ray.hitCellY, (float) l.z
+                                - (float) ray.hitCellZ);
 
                         final int rt5 = octreeVolume.traceCell(0, 0, 0,
                                 octreeVolume.masterCellSize, 0, r5);
@@ -250,12 +243,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.x - (float) ray.hitCellX, (float) l.y
+                                - (float) ray.hitCellY, l.z
+                                - (ray.hitCellZ + (float) 1.5));
 
                         final int rt6 = octreeVolume.traceCell(0, 0, 0,
                                 octreeVolume.masterCellSize, 0, r6);
@@ -277,7 +270,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 +283,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;