From cd87df088b1850ba0c90beb6c866c443e4eaf3b9 Mon Sep 17 00:00:00 2001
From: Normand Briere <nbriere@noware.ca>
Date: Sat, 08 Jun 2019 23:56:42 -0400
Subject: [PATCH] Fix shadow and bump issue.

---
 CameraPane.java |   67 +++++++++++++++++++++++++--------
 1 files changed, 50 insertions(+), 17 deletions(-)

diff --git a/CameraPane.java b/CameraPane.java
index 830f914..0d3d46b 100644
--- a/CameraPane.java
+++ b/CameraPane.java
@@ -8022,7 +8022,7 @@
         }
     }
 
-    /*boolean*/ public void BindTextures(cTexture tex, int resolution) // INTERFACE
+    /*boolean*/ public void BindTextures(cTexture tex, int resolution) throws Exception // INTERFACE
     {
         if (// DrawMode() != 0 || /*tex == null ||*/
                 ambientOcclusion ) // || !textureon)
@@ -8067,7 +8067,7 @@
         return; // true;
     }
     
-    CacheTexture GetCacheTexture(String tex, boolean bump, int resolution)
+    CacheTexture GetCacheTexture(String tex, boolean bump, int resolution) throws Exception
     {
         CacheTexture/*com.sun.opengl.util.texture.Texture*/ texture = null;
         
@@ -8184,7 +8184,9 @@
                                 texturedata = GetFileTexture(cachename, processbump, resolution);                                
                                 
                                 
-                                if (texturedata != null)
+                                if (texturedata == null)
+                                    throw new Exception();
+                                    
                                     texture = new CacheTexture(com.sun.opengl.util.texture.TextureIO.newTexture(texturedata),resolution);
         //texture = GetTexture(tex, bump);
                             }
@@ -8306,7 +8308,7 @@
         return texture;
     }
     
-    com.sun.opengl.util.texture.Texture GetTexture(String tex, boolean bump, int resolution)
+    com.sun.opengl.util.texture.Texture GetTexture(String tex, boolean bump, int resolution) throws Exception
     {
         CacheTexture texture = GetCacheTexture(tex, bump, resolution);
                 
@@ -8324,14 +8326,14 @@
         return texture!=null?texture.texture:null;
     }
     
-    public com.sun.opengl.util.texture.TextureData GetTextureData(String tex, boolean bump, int resolution)
+    public com.sun.opengl.util.texture.TextureData GetTextureData(String tex, boolean bump, int resolution) throws Exception
     {
         CacheTexture texture = GetCacheTexture(tex, bump, resolution);
                 
         return texture!=null?texture.texturedata:null;
     }
     
-    boolean BindTexture(String tex, boolean bump, int resolution)
+    boolean BindTexture(String tex, boolean bump, int resolution) throws Exception
     {
         if (/*tex == null ||*/ ambientOcclusion ) // || !textureon)
         {
@@ -10798,7 +10800,16 @@
             // Bump noise
             gl.glActiveTexture(GL.GL_TEXTURE6);
             //gl.glBindTexture(GL.GL_TEXTURE_2D, bump_noise);
-            BindTexture(NOISE_TEXTURE, false, 2);
+            
+    try
+    {
+        BindTexture(NOISE_TEXTURE, false, 2);
+    }
+    catch (Exception e)
+    {
+        System.err.println("FAILED: " + NOISE_TEXTURE);
+    }
+           
 
             gl.glActiveTexture(GL.GL_TEXTURE0);
             gl.glEnable(GL.GL_TEXTURE_2D);
@@ -11354,7 +11365,14 @@
 
                 usedtextures.clear();
             
-                BindTextures(DEFAULT_TEXTURES, 2);
+    try
+    {
+        BindTextures(DEFAULT_TEXTURES, 2);
+    }
+    catch (Exception e)
+    {
+        System.err.println("FAILED: " + DEFAULT_TEXTURES);
+    }
             }
             //System.out.println("--> " + stackdepth);
 //            GrafreeD.traceon();
@@ -11445,7 +11463,14 @@
             if (checker != null && DrawMode() == DEFAULT)
             {
                 //BindTexture(IMMORTAL_TEXTURE);
-                BindTextures(checker.GetTextures(), checker.texres);
+    try
+    {
+        BindTextures(checker.GetTextures(), checker.texres);
+    }
+    catch (Exception e)
+    {
+        System.err.println("FAILED: " + checker.GetTextures());
+    }
                 // NEAREST
                 GetGL().glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST); // GL.GL_LINEAR);
                 DrawChecker(gl);
@@ -12477,8 +12502,8 @@
 
            // display shadow only (bump == 0)
          "SUB temp.x, half.x, shadow.x;" +
-         "MOV temp.y, -params6.x;" +
-         "SLT temp.z, temp.y, zero.x;" +
+         "MOV temp.y, -params5.z;" + // params6.x;" +
+         "SLT temp.z, temp.y, -one2048th.x;" +
          "SUB temp.y, one.x, temp.z;" +
          "MUL temp.x, temp.x, temp.y;" +
          "KIL temp.x;" +
@@ -12809,7 +12834,7 @@
             //once = true;
         }
 
-        System.out.print("Program #" + mode + "; length = " + program.length());
+        System.out.print("Program #" + mode + "; instructions = " + program.split(";").length + "; length = " + program.length());
         System.out.println(" - " + (mode >> 3) + " lights; " + ((mode & 2) == 2 ? "anisoUV " : "") + ((mode & 4) == 4 ? "SoftShadow " : ""));
         loadProgram(gl, GL.GL_FRAGMENT_PROGRAM_ARB, program);
 
@@ -12942,12 +12967,16 @@
 
                 "ADD " + depth + ".z, " + depth + ".z, temp.x;" +
                 //"SUB " + depth + ".z, " + depth + ".z, temp.x;" + // back face shadowing!
+                
+                // Compare fragment depth in light space with shadowmap.
                 "SUB temp.x, fragment.texcoord[1].z, " + depth + ".z;" +
                 "SGE temp.y, temp.x, zero.x;" +
-                "SUB " + shadow + ".y, one.x, temp.y;" +
+                "SUB " + shadow + ".y, one.x, temp.y;" + // Specular is fully occluded
+                
+                // Reverse comparison
                 "SUB temp.x, one.x, temp.x;" +
                 "MUL " + shadow + ".x, temp.x, temp.y;" +
-                "SUB " + shadow + ".x, one.x, " + shadow + ".x;" + // specular is fully occluded
+                "SUB " + shadow + ".x, one.x, " + shadow + ".x;" + // diffuse
                 "POW " + shadow + ".x, " + shadow + ".x, params5.z;" + // fake depth
 
                 "SLT " + shadow + ".z, fragment.texcoord[1].z, " + depth + ".z;" +
@@ -12961,6 +12990,10 @@
                 // No shadow for backface
                 "DP3 temp.x, normal, lightd;" +
                 "SLT temp.x, temp.x, zero.x;" + // shadoweps
+                "LRP " + shadow + ", temp.x, one, " + shadow + ";" +
+
+                // No shadow when out of frustrum
+                "SGE temp.x, " + depth + ".z, one.z;" +
                 "LRP " + shadow + ", temp.x, one, " + shadow + ";" +
                 "";
     }
@@ -14870,7 +14903,7 @@
                 //RESIZETEXTURE ^= true;
                 //break;
             case 'z':
-                RENDERSHADOW ^= true;
+                Globals.RENDERSHADOW ^= true;
                 Globals.lighttouched = true;
                 repaint();
                 break;
@@ -16592,7 +16625,7 @@
                 gl.glDisable(gl.GL_CULL_FACE);
             }
 
-            if (!RENDERSHADOW)
+            if (!Globals.RENDERSHADOW)
                 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
 
             // SB gl.glPolygonOffset(2.5f, 10);
@@ -16602,7 +16635,7 @@
             //gl.glColorMask(false, false, false, false);
 
             //render_scene_from_light_view(gl, drawable, 0, 0);
-            if (RENDERSHADOW && Globals.lighttouched && !movingcamera) // && !parent.IsFreezed())
+            if (Globals.RENDERSHADOW && Globals.lighttouched && (!movingcamera || !Globals.FREEZEONMOVE)) // && !parent.IsFreezed())
             {
                 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
 

--
Gitblit v1.6.2