From d34fd9341c61d13677e029cb187d4dacff5e44ea Mon Sep 17 00:00:00 2001
From: Normand Briere <nbriere@noware.ca>
Date: Fri, 26 Oct 2018 20:31:01 -0400
Subject: [PATCH] Much better AO.

---
 CameraPane.java |   42 +++++++++++++++++++++++++++++++-----------
 1 files changed, 31 insertions(+), 11 deletions(-)

diff --git a/CameraPane.java b/CameraPane.java
index b6d1421..f2d438e 100644
--- a/CameraPane.java
+++ b/CameraPane.java
@@ -7693,7 +7693,7 @@
         
         if (renderCamera != lightCamera)
         for (int count = parentcam.GetTransformCount(); --count>=0;)
-            LA.matConcat(parentcam.toParent, matrix, matrix);
+            LA.matConcat(matrix, parentcam.toParent, matrix);
         
 //            LA.matConcat(renderCamera.toScreen, renderCamera.toParent, matrix);
             
@@ -7709,7 +7709,7 @@
             
         if (renderCamera != lightCamera)
         for (int count = parentcam.GetTransformCount(); --count>=0;)
-            LA.matConcat(matrix, parentcam.fromParent, matrix);
+            LA.matConcat(parentcam.fromParent, matrix, matrix);
 
 //            LA.matConcat(renderCamera.fromParent, renderCamera.fromScreen, matrix);
             
@@ -8915,7 +8915,7 @@
 //        if (parentcam != renderCamera) // not a light
         if (cam != lightCamera)
             for (int count = parentcam.GetTransformCount(); --count>=0;)
-                LA.matConcat(parentcam.toParent, matrix, matrix);
+                LA.matConcat(matrix, parentcam.toParent, matrix);
             
             for (int j = 0; j < 4; j++)
             {
@@ -8930,7 +8930,7 @@
 //        if (parentcam != renderCamera) // not a light
         if (cam != lightCamera)
             for (int count = parentcam.GetTransformCount(); --count>=0;)
-                LA.matConcat(matrix, parentcam.fromParent, matrix);
+                LA.matConcat(parentcam.fromParent, matrix, matrix);
         
             //LA.matConcat(cam.fromScreen, parentcam.fromParent, matrix);
             
@@ -15115,13 +15115,19 @@
             gl.glFlush();
             
             /**/
-            gl.glReadPixels(0, 0, OCCLUSION_SIZE, OCCLUSION_SIZE, GL.GL_BGRA, GL.GL_UNSIGNED_INT_8_8_8_8_REV, occlusionsizebuffer);
+            gl.glReadPixels(0, 0, OCCLUSION_SIZE, OCCLUSION_SIZE, GL.GL_DEPTH_COMPONENT, GL.GL_FLOAT, occlusionsizebuffer);
 
-            int[] pixels = occlusionsizebuffer.array();
+            float[] pixels = occlusionsizebuffer.array();
 
             double r = 0, g = 0, b = 0;
 
             double count = 0;
+            
+        gl.glGetDoublev(gl.GL_PROJECTION_MATRIX, tempmat2, 0);
+                                
+            float mindepth = 1;
+            
+            double FACTOR = 1;
 
             for (int i = 0; i < pixels.length; i++)
             {
@@ -15206,7 +15212,7 @@
 
                 double scale = ray.z; // 1; // cos
 
-                int p = pixels[newindex];
+                float depth = pixels[newindex];
 
                 /*
                 int newindex2 = (x + 1) * OCCLUSION_SIZE + y;
@@ -15230,10 +15236,23 @@
                 scale = (1 - modu) * modv;
                  */
 
-                r += ((p >> 16) & 0xFF) * scale / 255;
-                g += ((p >> 8) & 0xFF) * scale / 255;
-                b += (p & 0xFF) * scale / 255;
+                //r += ((p >> 16) & 0xFF) * scale / 255;
+                //g += ((p >> 8) & 0xFF) * scale / 255;
+                //b += (p & 0xFF) * scale / 255;
+                
+                if (mindepth > depth)
+                {
+                    mindepth = depth;
+                }
 
+                double z_eye = tempmat2[3*4 + 2] / (depth * -2.0 + 1.0 - tempmat2[2*4 + 2]);
+                
+                double factor = 1 - Math.exp(-z_eye * z_eye / FACTOR);
+                
+                r += factor * scale;
+                g += factor * scale;
+                b += factor * scale;
+                
                 count += scale;
             }
             
@@ -15407,7 +15426,8 @@
     static IntBuffer texturesizebuffer = IntBuffer.allocate(TEX_SIZE * TEX_SIZE);
     static IntBuffer selectsizebuffer = IntBuffer.allocate(SELECT_SIZE * SELECT_SIZE);
     static java.nio.FloatBuffer pointselectsizebuffer = java.nio.FloatBuffer.allocate(SELECT_SIZE * SELECT_SIZE);
-    static IntBuffer occlusionsizebuffer = IntBuffer.allocate(OCCLUSION_SIZE * OCCLUSION_SIZE);
+    //static IntBuffer occlusionsizebuffer = IntBuffer.allocate(OCCLUSION_SIZE * OCCLUSION_SIZE);
+    static java.nio.FloatBuffer occlusionsizebuffer = java.nio.FloatBuffer.allocate(OCCLUSION_SIZE * OCCLUSION_SIZE);
     static BufferedImage bufimage = new BufferedImage(TEX_SIZE, TEX_SIZE, BufferedImage.TYPE_INT_RGB);
     static BufferedImage textest = new cBufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
     static java.util.Vector<BufferedImage> billboards = new java.util.Vector<BufferedImage>();

--
Gitblit v1.6.2