From 98896326eb94666451b7e419becfb5d721840313 Mon Sep 17 00:00:00 2001
From: Normand Briere <nbriere@noware.ca>
Date: Mon, 10 Jun 2019 20:36:03 -0400
Subject: [PATCH] Fix shadow in light mode

---
 CameraPane.java |  199 ++++++++++++++++++++++++++++++++-----------------
 1 files changed, 129 insertions(+), 70 deletions(-)

diff --git a/CameraPane.java b/CameraPane.java
index e5811ae..337c107 100644
--- a/CameraPane.java
+++ b/CameraPane.java
@@ -1628,7 +1628,7 @@
             
         cColor.HSBtoRGB(display.color, display.saturation, 1, display.modelParams0);
 
-        float[] colorV = GrafreeD.colorV;
+        float[] colorV = Grafreed.colorV;
 
         /**/
         if (display.DrawMode() == display.DEFAULT) // && display.RENDERPROGRAM == 0)
@@ -2140,7 +2140,7 @@
         System.err.println("LIVE = " + Globals.isLIVE());
         
         if (!Globals.isLIVE()) // save sound
-            GrafreeD.savesound = true; // wav.save();
+            Grafreed.savesound = true; // wav.save();
      //   else
             repaint(); // start loop // may 2013
     }
@@ -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;
         
@@ -8075,12 +8075,16 @@
         {
             String texname = tex;
             
-            String[] split = tex.split("Textures");
-            if (split.length > 1)
-                texname = "/Users/nbriere/Textures" + split[split.length-1];
-            else
-                if (!texname.startsWith("/"))
-                    texname = "/Users/nbriere/Textures/" + texname;
+//            String[] split = tex.split("Textures");
+//            if (split.length > 1)
+//                texname = "/Users/nbriere/Textures" + split[split.length-1];
+//            else
+//                if (!texname.startsWith("/"))
+//                    texname = "/Users/nbriere/Textures/" + texname;
+            if (!new File(tex).exists())
+            {
+                    texname = javax.swing.filechooser.FileSystemView.getFileSystemView().getDefaultDirectory() + "/Textures/" + texname;
+            }
             
             if (CACHETEXTURE)
                 texture = textures.get(texname); // TEXTURE CACHE
@@ -8180,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);
                             }
@@ -8302,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);
                 
@@ -8320,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)
         {
@@ -9538,7 +9544,7 @@
             
             if (!BOXMODE)
             {
-                System.out.println("image: " + fullname + " (wav cursor=" + (GrafreeD.wav.cursor / 735 / 4) + ")");
+                System.out.println("image: " + fullname + " (wav cursor=" + (Grafreed.wav.cursor / 735 / 4) + ")");
             }
     
             if (!BOXMODE)
@@ -9576,7 +9582,7 @@
             ABORTED = false;
         }
         else
-            GrafreeD.wav.cursor += 735 * ACSIZE;
+            Grafreed.wav.cursor += 735 * ACSIZE;
         
         if (false)
         {
@@ -10239,11 +10245,11 @@
 
     public void display(GLAutoDrawable drawable)
     {
-        if (GrafreeD.savesound && GrafreeD.hassound)
+        if (Grafreed.savesound && Grafreed.hassound)
         {
-            GrafreeD.wav.save();
-            GrafreeD.savesound = false;
-            GrafreeD.hassound = false;
+            Grafreed.wav.save();
+            Grafreed.savesound = false;
+            Grafreed.hassound = false;
         }
 //        if (DEBUG_SELECTION)
 //        {
@@ -10373,7 +10379,7 @@
             Object3D theobject = object;
             Object3D theparent = object.parent;
             object.parent = null;
-            object = (Object3D)GrafreeD.clone(object);
+            object = (Object3D)Grafreed.clone(object);
             object.Stripify();
             if (theobject.selection == null || theobject.selection.Size() == 0)
                 theobject.PreprocessOcclusion(this);
@@ -10386,13 +10392,13 @@
             ambientOcclusion = false;
         }
 
-        if (Globals.lighttouched && DrawMode() == DEFAULT && !lightMode) // && !FROZEN)
+        if (Globals.lighttouched && DrawMode() == DEFAULT) // && !lightMode) // && !FROZEN)
         {
             //if (RENDERSHADOW) // ?
             if (!IsFrozen())
             {
                 // dec 2012
-                if (!ambientOcclusion && !(!flash && !lightMode && DrawMode() == DEFAULT && ANTIALIAS > 0))
+                if (!ambientOcclusion && !(!flash && DrawMode() == DEFAULT && ANTIALIAS > 0))
                 {
             Globals.framecount++;
                     shadowbuffer.display();
@@ -10794,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);
@@ -11279,8 +11294,8 @@
             e.printStackTrace();
         }
         
-        if (GrafreeD.RENDERME > 0)
-            GrafreeD.RENDERME--; // mechante magouille
+        if (Grafreed.RENDERME > 0)
+            Grafreed.RENDERME--; // mechante magouille
         
         Globals.ONESTEP = false;
     }
@@ -11350,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();
@@ -11441,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);
@@ -11523,7 +11552,7 @@
             return;
         }
 
-        String string = obj.GetToolTip();
+        String string = obj.toString(); //.GetToolTip();
         
         GL gl = GetGL();
         
@@ -12473,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;" +
@@ -12754,7 +12783,7 @@
               "MUL final.y, fragment.texcoord[0].x, c256;" +
               "FLR final.x, final.y;" +
               "SUB final.y, final.y, final.x;" +
-              //"MUL final.x, final.x, c256i;" +
+              "MUL final.x, final.x, c256i;" +
               "MOV final.z, zero.x;" +
               "MOV final.a, one.w;":""
               ) +
@@ -12762,7 +12791,7 @@
               "MUL final.y, fragment.texcoord[0].y, c256;" +
               "FLR final.x, final.y;" +
               "SUB final.y, final.y, final.x;" +
-              //"MUL final.x, final.x, c256i;" +
+              "MUL final.x, final.x, c256i;" +
               "MOV final.z, zero.x;" +
               "MOV final.a, one.w;":""
               ) +
@@ -12805,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);
 
@@ -12938,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;" +
@@ -12957,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 + ";" +
                 "";
     }
@@ -13515,7 +13552,7 @@
     public void mousePressed(MouseEvent e)
     {
         //System.out.println("mousePressed: " + e);
-        clickStart(e.getX(), e.getY(), e.getModifiersEx());
+        clickStart(e.getX(), e.getY(), e.getModifiers(), e.getModifiersEx());
     }
 
     static long prevtime = 0;
@@ -13591,8 +13628,8 @@
       //      mode |= META;
         //}
         
-        SetMouseMode(WHEEL | e.getModifiersEx());
-        drag(anchorX, anchorY + e.getUnitsToScroll()*8, 0);
+        SetMouseMode(e.getModifiers(), WHEEL | e.getModifiersEx());
+        drag(anchorX, anchorY + e.getUnitsToScroll()*8, 0, 0);
         anchorX = ax;
         anchorY = ay;
         prevX = px;
@@ -13652,6 +13689,10 @@
 //            wasliveok = true;
 //            waslive = false;
             
+            // May 2019 Forget it:
+            if (true)
+                    return;
+            
             // source == timer
             if (mouseDown)
             {
@@ -13690,7 +13731,7 @@
     
     javax.swing.Timer timer = new javax.swing.Timer(350, this);
 
-    void clickStart(int x, int y, int modifiers)
+    void clickStart(int x, int y, int modifiers, int modifiersex)
     {
         if (!wasliveok)
             return;
@@ -13707,7 +13748,7 @@
 //        touched = true; // main DL
         if (isRenderer)
         {
-            SetMouseMode(modifiers);
+            SetMouseMode(modifiers, modifiersex);
         }
 
         selectX = anchorX = x;
@@ -13720,7 +13761,7 @@
         clicked = true;
         hold = false;
 
-        if (((modifiers & ~1024) & ~0) == 0) // Single or multiple selection
+        if (((modifiersex & ~1024) & ~0) == 0) // Single or multiple selection
         // june 2013 means CTRL_CLICK: if (((modifiers & ~1024) & ~128) == 0) // Single or multiple selection
         {
       //  System.out.println("RESTART II " + modifiers);
@@ -13751,7 +13792,7 @@
             info.camera = renderCamera;
             info.x = x;
             info.y = y;
-            info.modifiers = modifiers;
+            info.modifiers = modifiersex;
             editObj = object.doEditClick(info, 0);
             if (!editObj)
             {
@@ -13780,7 +13821,7 @@
             clickEnd(e.getX(), e.getY(), e.getModifiersEx());
         }
         else
-            drag(e.getX(), e.getY(), e.getModifiersEx());
+            drag(e.getX(), e.getY(), e.getModifiers(), e.getModifiersEx());
         
     //try { Thread.sleep(1); } catch (Exception ex) {}
     }
@@ -14017,7 +14058,7 @@
                     {
                         Globals.lighttouched = true;
                     }
-                    drag(X, (mouseMode != 0) ? Y : anchorY, MODIFIERS);
+                    drag(X, (mouseMode != 0) ? Y : anchorY, 0, MODIFIERS);
                 }
             //else
             }
@@ -14117,7 +14158,7 @@
     int X, Y;
     boolean SX, SY;
 
-    void drag(int x, int y, int modifiers)
+    void drag(int x, int y, int modifiers, int modifiersex)
     {
         if (IsFrozen())
         {
@@ -14126,17 +14167,17 @@
 
         drag = true; // NEW
         
-        boolean continuous = (modifiers & COMMAND) == COMMAND;
+        boolean continuous = (modifiersex & COMMAND) == COMMAND;
 
         X = x;
         Y = y;
         // floating state for animation
-        MODIFIERS = modifiers;
-        modifiers &= ~1024;
+        MODIFIERS = modifiersex;
+        modifiersex &= ~1024;
         if (false) // modifiers != 0)
         {
             //new Exception().printStackTrace();
-            System.out.println("mouseDragged: " + modifiers);
+            System.out.println("mouseDragged: " + modifiersex);
             System.out.println("SHIFT = " + SHIFT);
             System.out.println("CONTROL = " + COMMAND);
             System.out.println("META = " + META);
@@ -14156,7 +14197,7 @@
                 info.camera = renderCamera;
                 info.x = x;
                 info.y = y;
-                object.editWindow.copy.doEditDrag(info);
+                object.editWindow.copy.doEditDrag(info, (modifiers & MouseEvent.BUTTON3_MASK) != 0);
             } else
             {
                 if (x < startX)
@@ -14333,6 +14374,7 @@
     public void mouseReleased(MouseEvent e)
     {
         movingcamera = false;
+        X = Y = 0;
         //System.out.println("mouseReleased: " + e);
         clickEnd(e.getX(), e.getY(), e.getModifiersEx());
     }
@@ -14355,9 +14397,9 @@
         boolean control = ((modifiers & CTRL) != 0); // june 2013: for point selection
         boolean command = ((modifiers & COMMAND) != 0); // june 2013: for multiple selection
         
-        if (control || command || IsFrozen())
+// No delay        if (control || command || IsFrozen())
             timeout = true;
-        else
+// ?? May 2019        else
        // timer.setDelay((modifiers & 128) != 0?0:350);
         mouseDown = false;
     if (!control && !command) // june 2013
@@ -14467,7 +14509,7 @@
         System.out.println("keyReleased: " + e);
     }
 
-    void SetMouseMode(int modifiers)
+    void SetMouseMode(int modifiers, int modifiersex)
     {
         //System.out.println("SetMouseMode = " + modifiers);
         //modifiers &= ~1024;
@@ -14479,25 +14521,25 @@
         //if (modifiers == 0) // || (modifiers == (1024 | CONTROL)))
         //	return;
         //System.out.println("SetMode = " + modifiers);
-        if ((modifiers & WHEEL) == WHEEL)
+        if ((modifiersex & WHEEL) == WHEEL)
         {
             mouseMode |= ZOOM;
         }
         
         boolean capsLocked = Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK);
-        if (capsLocked || (modifiers & META) == META)
+        if (capsLocked) // || (modifiers & META) == META)
         {
             mouseMode |= VR; // BACKFORTH;
         }
-        if ((modifiers & CTRLCLICK) == CTRLCLICK)
+        if ((modifiersex & CTRLCLICK) == CTRLCLICK)
         {
             mouseMode |= SELECT;
         }
-        if ((modifiers & COMMAND) == COMMAND)
+        if ((modifiersex & COMMAND) == COMMAND)
         {
             mouseMode |= SELECT;
         }
-        if ((modifiers & SHIFT) == SHIFT || forcetranslate)
+        if ((modifiersex & SHIFT) == SHIFT || forcetranslate || (modifiers & MouseEvent.BUTTON3_MASK) != 0)
         {
             mouseMode &= ~VR;
             mouseMode |= TRANSLATE;
@@ -14526,7 +14568,7 @@
 
         if (isRenderer) //
         {
-            SetMouseMode(modifiers);
+            SetMouseMode(0, modifiers);
         }
 
         Globals.theRenderer.keyPressed(key);
@@ -14862,7 +14904,7 @@
                 //RESIZETEXTURE ^= true;
                 //break;
             case 'z':
-                RENDERSHADOW ^= true;
+                Globals.RENDERSHADOW ^= true;
                 Globals.lighttouched = true;
                 repaint();
                 break;
@@ -14986,7 +15028,7 @@
         //mode = ROTATE;
         if ((MODIFIERS & COMMAND) == 0) // VR??
         {
-            SetMouseMode(modifiers);
+            SetMouseMode(0, modifiers);
         }
     }
 
@@ -15122,7 +15164,7 @@
     {
         //System.out.println("processMouseMotionEvent: " + mouseMode + " " + e.getModifiers() + " " + e.getModifiersEx() + " " + e.getButton());
         //if (e.getButton() == MouseEvent.NOBUTTON && (mouseMode & SELECT) == 0)
-        if ((e.getModifiers() & MouseEvent.BUTTON1_MASK) == 0 && (mouseMode & SELECT) == 0)
+        if ((e.getModifiers() & MouseEvent.BUTTON1_MASK) == 0 && (e.getModifiers() & MouseEvent.BUTTON3_MASK) == 0 && (mouseMode & SELECT) == 0)
         {
             mouseMoved(e);
         } else
@@ -15341,7 +15383,9 @@
         info.bounds.setBounds(0, 0, (int) (width * zoom), (int) (height * zoom));
         //Image img = CreateImage(width, height);
         //System.out.println("width = " + width + "; height = " + height + "\n");
+        
         Graphics gr = g; // img.getGraphics();
+        
         if (!hasMarquee)
         {
             if (Xmin < Xmax) // !locked)
@@ -15439,14 +15483,28 @@
             if (!isRenderer)
             {
                 object.drawEditHandles(info, 0);
+                
+                if (drag && (X != 0 || Y != 0) && object.selection.Size() > 0)
+                {
+                    switch (object.selection.get(0).hitSomething)
+                    {
+                        case Object3D.hitCenter: gr.setColor(Color.pink); break;
+                        case Object3D.hitRotate: gr.setColor(Color.green); break;
+                        case Object3D.hitScale: gr.setColor(Color.cyan); break;
+                    }
+                    
+                    gr.drawLine(X, Y, info.bounds.width/2, info.bounds.height/2);
+                }
             }
         }
+        
         if (isRenderer)
         {
         //gr.setColor(Color.black);
         //gr.drawRect(info.bounds.x - 1, info.bounds.y - 1, info.bounds.width + 1, info.bounds.height + 1);
         //gr.drawRect(info.bounds.x - 2, info.bounds.y - 2, info.bounds.width + 3, info.bounds.height + 3);
         }
+        
         if (hasMarquee)
         {
             gr.setXORMode(Color.white);
@@ -15559,6 +15617,7 @@
     public boolean mouseDown(Event evt, int x, int y)
     {
         System.out.println("mouseDown: " + evt);
+        System.exit(0);
         /*
         locked = true;
         drag = false;
@@ -15602,7 +15661,7 @@
         {
             keyPressed(0, modifiers);
         }
-        clickStart(x, y, modifiers);
+ //       clickStart(x, y, modifiers);
         return true;
     }
 
@@ -15720,7 +15779,7 @@
         {
             keyReleased(0, 0);
         }
-        drag(x, y, modifiers);
+        drag(x, y, 0, modifiers);
         return true;
     }
 
@@ -16418,16 +16477,16 @@
                             System.out.println("; fromto " + sel + " " + Trunk(previousselectedpoint.toParent[3][0]) + " " + Trunk(previousselectedpoint.toParent[3][2]) + " " + Trunk(selectedpoint.toParent[3][0]) + " " + Trunk(selectedpoint.toParent[3][2]));
                         }
                         
-                        previousselectedpoint = (Sphere) GrafreeD.clone(selectedpoint);
+                        previousselectedpoint = (Sphere) Grafreed.clone(selectedpoint);
                     }
                 }
                 
                 if (!movingcamera && !PAINTMODE)
                     object.editWindow.ScreenFitPoint(); // fev 2014
                 
-                if (PAINTMODE && GrafreeD.clipboard.size() == 1) // object.editWindow.copy.selection.Size() > 0)
+                if (PAINTMODE && Grafreed.clipboard.size() == 1) // object.editWindow.copy.selection.Size() > 0)
                 {
-                    Object3D paintobj = GrafreeD.clipboard.get(0); // object.editWindow.copy.selection.elementAt(0);
+                    Object3D paintobj = Grafreed.clipboard.get(0); // object.editWindow.copy.selection.elementAt(0);
                     
                     Object3D group = new Object3D("inst" + paintcount++);
                     
@@ -16583,7 +16642,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);
@@ -16593,7 +16652,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