From 6ed65dcb597fb2153cef75bf1845978f1115658c Mon Sep 17 00:00:00 2001
From: Normand Briere <nbriere@noware.ca>
Date: Fri, 14 Dec 2018 22:53:55 -0500
Subject: [PATCH] Repair shadow, normal push + attractor mesh.

---
 CameraPane.java |  260 +++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 187 insertions(+), 73 deletions(-)

diff --git a/CameraPane.java b/CameraPane.java
index 3e6f8d9..a5a5ed5 100644
--- a/CameraPane.java
+++ b/CameraPane.java
@@ -228,7 +228,7 @@
     void PushMatrix(double[][] matrix, int count)
     {
         matrixdepth++;
-     //   GraphreeD.tracein(matrix);
+     //   GrafreeD.tracein(matrix);
         if (matrix == null)
             return; // Identity
         
@@ -253,13 +253,13 @@
     
     void PushMatrix(double[][] matrix)
     {
-    //    GraphreeD.tracein(matrix);
+    //    GrafreeD.tracein(matrix);
         PushMatrix(matrix,1);
     }
     
     void PushMatrix()
     {
-    //    GraphreeD.tracein(null);
+    //    GrafreeD.tracein(null);
         if (matrixdepth++ < MAXSTACK - 1)
         {
             currentGL.glPushMatrix();
@@ -274,7 +274,7 @@
     {
         --matrixdepth;
         
-    //    GraphreeD.traceout(inverse);
+    //    GrafreeD.traceout(inverse);
         if (inverse == null)
             return; // Identity
         
@@ -292,7 +292,7 @@
 
     void PopMatrix()
     {
-    //    GraphreeD.traceout(null);
+    //    GrafreeD.traceout(null);
         // inverse == null??
         if (--matrixdepth < MAXSTACK - 1)
         {
@@ -491,7 +491,7 @@
         System.err.println("LIVE = " + isLIVE());
         
         if (!isLIVE()) // save sound
-            GraphreeD.savesound = true; // wav.save();
+            GrafreeD.savesound = true; // wav.save();
      //   else
             repaint(); // start loop // may 2013
     }
@@ -2000,10 +2000,12 @@
          */
     TextureData ReduceTexture(TextureData texturedata, int resolution) // String name)
     {
+        int pixelformat = texturedata.getPixelFormat();
+        
         int stride = 1;
-        if (texturedata.getPixelFormat() == GetGL().GL_RGB || texturedata.getPixelFormat() == GetGL().GL_BGR) // || texturedata.getPixelFormat() == GL.GL_LUMINANCE)
+        if (pixelformat == GetGL().GL_RGB || pixelformat == GetGL().GL_BGR) // || texturedata.getPixelFormat() == GL.GL_LUMINANCE)
             stride = 3;
-        if (texturedata.getPixelFormat() == GetGL().GL_RGBA || texturedata.getPixelFormat() == GetGL().GL_BGRA) // || texturedata.getPixelFormat() == GL.GL_LUMINANCE_ALPHA)
+        if (pixelformat == GetGL().GL_RGBA || pixelformat == GetGL().GL_BGRA) // || texturedata.getPixelFormat() == GL.GL_LUMINANCE_ALPHA)
             stride = 4;
 
         int width = texturedata.getWidth();
@@ -6333,7 +6335,7 @@
         {
             bumpdepth--;
 
-          //  GraphreeD.trace("POP " + tex + "(" + pigmentdepth + ")" + " : " + pigmentstack[pigmentdepth] + " vs " + texture);
+          //  GrafreeD.trace("POP " + tex + "(" + pigmentdepth + ")" + " : " + pigmentstack[pigmentdepth] + " vs " + texture);
             if (bumpstack[bumpdepth] != (texture!=null?texture.texture:null))
             {
             //    assert (bumpstack[bumpdepth] == texture);
@@ -6363,7 +6365,7 @@
         {
             pigmentdepth--;
 
-          //  GraphreeD.trace("POP " + tex + "(" + pigmentdepth + ")" + " : " + pigmentstack[pigmentdepth] + " vs " + texture);
+          //  GrafreeD.trace("POP " + tex + "(" + pigmentdepth + ")" + " : " + pigmentstack[pigmentdepth] + " vs " + texture);
             if (pigmentstack[pigmentdepth] != (texture!=null?texture.texture:null))
             {
             //    assert (pigmentstack[pigmentdepth] == texture);
@@ -6694,12 +6696,12 @@
                 
         if (bump)
         {
-    //        GraphreeD.trace("PUSH BUMP " + tex  + "(" + bumpdepth + ")" + " : " + texture);
+    //        GrafreeD.trace("PUSH BUMP " + tex  + "(" + bumpdepth + ")" + " : " + texture);
             bumpstack[bumpdepth++] = texture!=null?texture.texture:null;
         }
         else
         {
-    //        GraphreeD.trace("PUSH PIGMENT " + tex  + "(" + pigmentdepth + ")" + " : " + texture);
+    //        GrafreeD.trace("PUSH PIGMENT " + tex  + "(" + pigmentdepth + ")" + " : " + texture);
             pigmentstack[pigmentdepth++] = texture!=null?texture.texture:null;
         }
         
@@ -7442,6 +7444,8 @@
             //gl.glFrustum(-0.5*scale, 0.5*scale, -0.5*scale, 0.5*scale, 1, 100);
             //glu.gluPerspective(lightshaper_fovy, 1, lightshaper_zNear, lightshaper_zFar);
             double scale = lightCamera.SCALE / lightCamera.Distance();
+// PATCH FILLE AUX JEANS           
+            //scale *= lightCamera.shaper_fovy / 25;
             gl.glScaled(2 * scale, 2 * scale, -scale);
             gl.glTranslated(0, 0, lightCamera.DECAL);
 
@@ -7590,7 +7594,8 @@
     
     float[] options1 = new float[]{1000, 0.00001f, 20, 0, 0}; // focus, aperture, Shadow blur, aniso, anisoV
     float[] options2 = new float[]{0, 1, 0, 0}; // fog density, intensity, elevation
-    float[] options3 = new float[]{1, 1, 1, 1}; // fog color. image intensity
+    float[] options3 = new float[]{1, 1, 1, 0}; // fog color
+    float[] options4 = new float[]{1, 0, 1, 0}; // image intensity, subsurface, lightsheen
 
     static int imagecount = 0; // movie generation
     
@@ -7628,14 +7633,14 @@
         
 //System.out.println("start frame");
         gl.glClear(gl.GL_ACCUM_BUFFER_BIT);
-        for (jitter = 0; jitter < ACSIZE; jitter++) //, GraphreeD.wav.cursor += LIVE ? 735 : 0)
+        for (jitter = 0; jitter < ACSIZE; jitter++) //, GrafreeD.wav.cursor += LIVE ? 735 : 0)
         {
             framecount++;
             
             if (CameraPane.tickcount > 0)
                 CameraPane.tickcount--;
             
-//            GraphreeD.wav.cursor += 735; // 44100 Hz / 120 Hz * 2 (for 16 bits)
+//            GrafreeD.wav.cursor += 735; // 44100 Hz / 120 Hz * 2 (for 16 bits)
 //            restartframe = true;
             if (options1[2] > 100 && (jitter%2==0))
             {
@@ -7690,7 +7695,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);
             
@@ -7706,7 +7711,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);
             
@@ -7923,7 +7928,7 @@
             
             if (!BOXMODE)
             {
-                System.out.println("image: " + fullname + " (" + (GraphreeD.wav.cursor / 735 / 4) + ")");
+                System.out.println("image: " + fullname + " (wav cursor=" + (GrafreeD.wav.cursor / 735 / 4) + ")");
             }
     
             if (!BOXMODE)
@@ -7951,7 +7956,7 @@
         {
             if (ACSIZE > 1)
             {
-           //     System.err.println("image #" + (GraphreeD.wav.cursor / 735 / 4));
+           //     System.err.println("image #" + (GrafreeD.wav.cursor / 735 / 4));
             }
         }
         
@@ -7961,7 +7966,7 @@
             ABORTED = false;
         }
         else
-            GraphreeD.wav.cursor += 735 * ACSIZE;
+            GrafreeD.wav.cursor += 735 * ACSIZE;
         
         if (false)
         {
@@ -8631,11 +8636,11 @@
 
     public void display(GLAutoDrawable drawable)
     {
-        if (GraphreeD.savesound && GraphreeD.hassound)
+        if (GrafreeD.savesound && GrafreeD.hassound)
         {
-            GraphreeD.wav.save();
-            GraphreeD.savesound = false;
-            GraphreeD.hassound = false;
+            GrafreeD.wav.save();
+            GrafreeD.savesound = false;
+            GrafreeD.hassound = false;
         }
 //        if (DEBUG_SELECTION)
 //        {
@@ -8765,7 +8770,7 @@
             Object3D theobject = object;
             Object3D theparent = object.parent;
             object.parent = null;
-            object = (Object3D)GraphreeD.clone(object);
+            object = (Object3D)GrafreeD.clone(object);
             object.Stripify();
             if (theobject.selection == null || theobject.selection.Size() == 0)
                 theobject.PreprocessOcclusion(this);
@@ -8784,7 +8789,7 @@
             if (!IsFrozen())
             {
                 // dec 2012
-                if (!(!flash && !lightMode && drawMode == DEFAULT && ANTIALIAS > 0))
+                if (!ambientOcclusion && !(!flash && !lightMode && drawMode == DEFAULT && ANTIALIAS > 0))
                 {
             framecount++;
                     shadowbuffer.display();
@@ -8912,7 +8917,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++)
             {
@@ -8927,7 +8932,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);
             
@@ -9089,6 +9094,8 @@
             //gl.glFrustum(-0.5*scale, 0.5*scale, -0.5*scale, 0.5*scale, 1, 100);
             //glu.gluPerspective(lightshaper_fovy, 1, lightshaper_zNear, lightshaper_zFar);
             double scale = lightCamera.SCALE / lightCamera.Distance();
+// PATCH FILLE AUX JEANS           
+            //scale *= lightCamera.shaper_fovy / 25;
             gl.glScaled(2 * scale, 2 * scale, -scale);
             gl.glTranslated(0, 0, lightCamera.DECAL);
 
@@ -9228,6 +9235,8 @@
         {
             //glu.gluPerspective(lightshaper_fovy, 1, lightshaper_zNear, lightshaper_zFar);
             double scale = lightCamera.SCALE / lightCamera.Distance();
+// PATCH FILLE AUX JEANS            
+            //scale *= lightCamera.shaper_fovy / 25;
             gl.glScaled(2 * scale, 2 * scale, -scale);
             gl.glTranslated(0, 0, lightCamera.DECAL);
         //System.out.println("DECAL = " + LIGHTDECAL + "; SCALE = " + LIGHTSCALE);
@@ -9379,6 +9388,7 @@
 
         gl.glProgramEnvParameter4fvARB(gl.GL_FRAGMENT_PROGRAM_ARB, 63, options2, 0);
         gl.glProgramEnvParameter4fvARB(gl.GL_FRAGMENT_PROGRAM_ARB, 62, options3, 0);
+        gl.glProgramEnvParameter4fvARB(gl.GL_FRAGMENT_PROGRAM_ARB, 61, options4, 0);
 
         options2[0] *= renderCamera.Distance();
 
@@ -9660,8 +9670,8 @@
             e.printStackTrace();
         }
         
-        if (GraphreeD.RENDERME > 0)
-            GraphreeD.RENDERME--; // mechante magouille
+        if (GrafreeD.RENDERME > 0)
+            GrafreeD.RENDERME--; // mechante magouille
         
         ONESTEP = false;
     }
@@ -9734,7 +9744,7 @@
                 BindTextures(DEFAULT_TEXTURES, 2);
             }
             //System.out.println("--> " + stackdepth);
-//            GraphreeD.traceon();
+//            GrafreeD.traceon();
         
         // DRAW
         object.draw(this, /*(Composite)*/ object, false, false);
@@ -9750,32 +9760,46 @@
                         selectedpoint.radius = radius;
                         selectedpoint.recalculate();
                         selectedpoint.material = new cMaterial();
-                        selectedpoint.material.color = 0.25f;
+                        selectedpoint.material.color = 0.15f; // Yellow
                         selectedpoint.material.modulation = 0.75f;
                         
-                        debugpoint.radius = radius;
-                        debugpoint.recalculate();
-                        debugpoint.material = new cMaterial();
-                        debugpoint.material.color = 0.25f;
-                        debugpoint.material.modulation = 0.75f;
+                        debugpointG.radius = radius;
+                        debugpointG.recalculate();
+                        debugpointG.material = new cMaterial();
+                        debugpointG.material.color = 0.25f; // Green
+                        debugpointG.material.modulation = 0.75f;
                         
-                        debugpoint2.radius = radius;
-                        debugpoint2.recalculate();
-                        debugpoint2.material = new cMaterial();
-                        debugpoint2.material.color = 0.75f;
-                        debugpoint2.material.modulation = 0.75f;
+                        debugpointP.radius = radius;
+                        debugpointP.recalculate();
+                        debugpointP.material = new cMaterial();
+                        debugpointP.material.color = 0.75f; // Purple
+                        debugpointP.material.modulation = 0.75f;
+                        
+                        debugpointC.radius = radius;
+                        debugpointC.recalculate();
+                        debugpointC.material = new cMaterial();
+                        debugpointC.material.color = 0.5f; // Cyan
+                        debugpointC.material.modulation = 0.75f;
+                        
+                        debugpointR.radius = radius;
+                        debugpointR.recalculate();
+                        debugpointR.material = new cMaterial();
+                        debugpointR.material.color = 0f; // Red
+                        debugpointR.material.modulation = 0.75f;
                         
                         InitPoints(radius);
                     }
                     selectedpoint.draw(this, /*(Composite)*/ null, false, false);
-                    //debugpoint.draw(this, /*(Composite)*/ null, false);
-                    //debugpoint2.draw(this, /*(Composite)*/ null, false);
-                    DrawPoints(this);
+                    debugpointG.draw(this, /*(Composite)*/ null, false,false);
+                    debugpointP.draw(this, /*(Composite)*/ null, false,false);
+                    debugpointC.draw(this, /*(Composite)*/ null, false,false);
+                    debugpointR.draw(this, /*(Composite)*/ null, false,false);
+                    // DrawPoints(this);
                 }
                 
        //         debugstuff.draw(this, /*(Composite)*/ null, false);
             }
-//            GraphreeD.traceoff();
+//            GrafreeD.traceoff();
             //System.out.println(stackdepth);
             if (drawMode == 0)
             {
@@ -9807,12 +9831,14 @@
 
             if (checker != null && drawMode == DEFAULT)
             {
-               // BindTexture(IMMORTAL_TEXTURE);
+                //BindTexture(IMMORTAL_TEXTURE);
+                BindTextures(checker.GetTextures(), checker.texres);
                 // NEAREST
                 GetGL().glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST); // GL.GL_LINEAR);
                 DrawChecker(gl);
                 //checker.Draw(this, null, false);
               //  ReleaseTexture(IMMORTAL_TEXTURE);
+                ReleaseTextures(checker.GetTextures());
             }
 
             if (object.parent != null)
@@ -10343,11 +10369,13 @@
                 "PARAM params6   = program.env[6];" + // bump, noise, borderfade, fog punchthrough
                 "PARAM params7   = program.env[7];" + // noise power, opacity power
                 "PARAM options0   = program.env[63];" + // fog density, intensity, elevation
-                "PARAM options1   = program.env[62];" + // fog rgb color, image intensity
+                "PARAM options1   = program.env[62];" + // fog rgb color
+                "PARAM options2   = program.env[61];" + // image intensity, subsurface, lightsheen
                 "PARAM pointlight   = program.env[127];" + // ...
                 "PARAM zero     = { 0.0, 0.0, 0.0, 0.0 };" +
                 "PARAM halfhalf = { 0.25, 0.25, 0.25, 1.0 };" +
                 "PARAM half     = { 0.5, 0.5, 0.5, 1.0 };" +
+                "PARAM threequarter = { 0.75, 0.75, 0.75, 1.0 };" +
                 "PARAM two      = { 2.0, 2.0, 2.0, 1.0 };" +
                 "PARAM ten      = { 10, 10, 10, 1.0 };" +
                 "PARAM one3rd  = { 0.5, 0.33333333333, 0.333333333, 1.0 };" +
@@ -10368,6 +10396,7 @@
                 "PARAM lodbias = { 10,10,10,10 };" + // 20, -2, -20, 1.0 };" +
                 "PARAM infinity  = { 100000000, 100000000, 100000000, 1.0 };" +
                 "PARAM one2048th = { 0.00048828125, 0.00048828125, 0.00048828125, 1.0 };" +
+                "PARAM ninetenth = { 0.9, 0.9, 0.9, 1.0 };" +
                 "PARAM almostone = { 0.999, 0.999, 0.999, 1.0 };" +
                 "PARAM c256 = { 256, 256, 256, 1.0 };" +
                 "PARAM c256i = { 0.00390625, 0.00390625, 0.00390625, 1.0 };" +
@@ -10408,6 +10437,10 @@
                 "TEMP R1;" +
                 "TEMP R2;" +
                 "TEMP R3;" +
+                "TEMP min;" +
+                "TEMP max;" +
+                "TEMP average;" +
+                "TEMP saturation;" +
                 "TEMP keep1;" +
                 "TEMP keep2;" +
                 "TEMP keep3;" +
@@ -10938,10 +10971,40 @@
 
                 // skin?
                 // Saturation for skin
-                /**/ // c'est ici
-    (Skinshader?                "DP3 temp.x, final,one;" +
+                /**/
+   (Skinshader?
+                "DP3 average.x, final,one;" +
+                "MUL average, one3rd.xxxx,average.xxxx;" +
+
+                "MIN min.x, final.x,final.y;" +
+                "MIN min.x, min.x,final.z;" +
+
+                "MAX max.x, final.x,final.y;" +
+                "MAX max.x, max.x,final.z;" +
+                "MOV max, max.xxxx;" +
+                
+                "SUB saturation, max, final;" +
+                
+                "ADD temp.x, max.x, one10th.x;" +
+                "RCP temp.x, temp.x;" +
+                "MUL temp.x, temp.x, half.x;" +
+                "MUL saturation, saturation, temp.xxxx;" +
+                
+            "DP3 ndotl.x, normald, light;" +
+            "MAX ndotl.x, ndotl.x, -ndotl.x;" +
+                
+                "SUB temp.x, one.x, ndotl.x;" +
+                "ADD temp.x, temp.x, options2.z;" + // lightsheen
+                "ADD temp.y, one.y, options2.y;" + // sursurface
+                "MUL temp.x, temp.x, temp.y;" +
+                                
+                "MUL saturation, saturation, temp.xxxx;" +
+                "SUB_SAT temp, max, saturation;" +
+/**
+                "DP3 temp.x, final,one;" +
                 "MUL R2, one3rd.xxxx,temp.xxxx;" +
-                "SUB temp, final,R2;" +
+                "SUB temp, final, R2;" +
+                  
             // using light angle
             "DP3 ndotl.x, normald,light;" +
             //"SLT ndotl.y, ndotl.x, zero.x;" +
@@ -10954,7 +11017,6 @@
             // using light intensity
             "MOV ndotl.z, R2.x;" +
             "MUL ndotl.z, ndotl.z, ndotl.z;" + // tuning...
-                
 // june 2014
                 "MAD R1.x, ndotl.z,slope.y,one.x;" +
 //                "SUB ndotl.x, one.x, ndotl.x;" +
@@ -10966,6 +11028,7 @@
 //"MUL R2.y, shadow.x,R2.y;" + // avril 2014
                 
         "MAD temp.x, R2.y, ndotl.y, temp.x;" + // red shift (skin)
+/**/
                 
              //   "ADD final, R2,temp;" +
                 "MOV final, temp;"
@@ -11055,7 +11118,7 @@
                 /**/
                 // HDR
                 "MOV temp.z, final.a;" +
-                "MUL final, final,options1.w;" +
+                "MUL final, final,options2.x;" +
                 "MOV final.a, temp.z;" +
                 /**/
                 
@@ -11092,10 +11155,17 @@
               "MOV final.z, zero.x;" +
               "MOV final.a, one.w;":""
               ) +
+                /*
     (NORMALdebug?"MOV final.x, normal.x;" +
               "MOV final.y, normal.y;" +
               "MOV final.z, normal.z;" +
               "MOV final.a, one.w;":""
+              ) +
+                */
+    (NORMALdebug?"SUB final.x, one.x, final.x;" +
+              "SUB final.y, one.x, final.y;" +
+              "SUB final.z, one.x, final.z;" +
+              "MOV final.a, final.a;":""
               ) +
 //          "MOV final, bumpmap;" +
                 "MOV result.color, final;" +
@@ -11241,6 +11311,7 @@
     String Shadow(String depth, String shadow)
     {
         return "MAX temp.x, ndotl.x, one64th.x;" +
+               "MIN temp.x, temp.x, ninetenth.x;" +
                 /**/
                 // Sine
                 "MUL temp.y, temp.x, temp.x;" +
@@ -12793,7 +12864,9 @@
         {
             mouseMode |= ZOOM;
         }
-        if ((modifiers & META) == META)
+        
+        boolean capsLocked = Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK);
+        if (capsLocked || (modifiers & META) == META)
         {
             mouseMode |= VR; // BACKFORTH;
         }
@@ -12807,6 +12880,7 @@
         }
         if ((modifiers & SHIFT) == SHIFT || forcetranslate)
         {
+            mouseMode &= ~VR;
             mouseMode |= TRANSLATE;
         }
     //    if ((modifiers & SHIFT_META) == SHIFT_META)
@@ -13196,7 +13270,8 @@
                 FlipTransform();
                 break;
             case ENTER:
-                object.editWindow.ScreenFit(); // Edit();
+                // object.editWindow.ScreenFit(); // Edit();
+                ToggleLive();
                 break;
             case DELETE:
                 ClearSelection();
@@ -14373,7 +14448,7 @@
         gl.glPushAttrib(GL.GL_ALL_ATTRIB_BITS);
         gl.glPushMatrix();
         gl.glLoadIdentity();
-        PushMatrix(checker.toParent);
+        //PushMatrix(checker.toParent);
 
         gl.glMatrixMode(GL.GL_TEXTURE);
         gl.glPushMatrix();
@@ -14396,8 +14471,8 @@
 
         gl.glNormal3f(0.0f, 0.0f, 1.0f);
             
-        float step = 0.1666f; //0.25f;
-        float stepv = step * 1652 / 998;
+        float step = 2; // 0.1666f; //0.25f;
+        float stepv = 2; // step * 1652 / 998;
 
         int i0 = 0;
         /*
@@ -14467,15 +14542,27 @@
 
                 //float u = (i+1)/2;
                 //float v = (j+1)/2;
-                gl.glTexCoord2f((i + 1) / 2, (j + 1) / 2); // (1,0) // (i+1+step)/2,(j+1)/2);
+                if (checker.flipV)
+                        gl.glTexCoord2f((i + 1) / 2, 1 - (j + 1) / 2);
+                else
+                        gl.glTexCoord2f((i + 1) / 2, (j + 1) / 2); // (1,0) // (i+1+step)/2,(j+1)/2);
                 gl.glVertex3f(i, j, -0.5f);
 
+                if (checker.flipV)
+                gl.glTexCoord2f((i + 1 + step) / 2, 1 - (j + 1) / 2); // (1,1) // (i+1+step)/2,(j+1+step)/2);
+                else
                 gl.glTexCoord2f((i + 1 + step) / 2, (j + 1) / 2); // (1,1) // (i+1+step)/2,(j+1+step)/2);
                 gl.glVertex3f(i + step, j, -0.5f);
 
+                if (checker.flipV)
+                gl.glTexCoord2f((i + 1 + step) / 2, 1 - (j + 1 + stepv) / 2); // (0,1) //(i+1)/2,(j+1+step)/2);
+                else
                 gl.glTexCoord2f((i + 1 + step) / 2, (j + 1 + stepv) / 2); // (0,1) //(i+1)/2,(j+1+step)/2);
                 gl.glVertex3f(i + step, j + stepv, -0.5f);
 
+                if (checker.flipV)
+                gl.glTexCoord2f((i + 1) / 2, 1 - (j + 1 + stepv) / 2); // (0,0) //(i+1)/2,(j+1)/2);
+                else
                 gl.glTexCoord2f((i + 1) / 2, (j + 1 + stepv) / 2); // (0,0) //(i+1)/2,(j+1)/2);
                 gl.glVertex3f(i, j + stepv, -0.5f);
             }
@@ -14487,7 +14574,7 @@
         gl.glMatrixMode(GL.GL_PROJECTION);
         gl.glPopMatrix();
         gl.glMatrixMode(GL.GL_MODELVIEW);
-        PopMatrix(null); // checker.toParent); // null);
+        //PopMatrix(null); // checker.toParent); // null);
         gl.glPopMatrix();
         PopTextureMatrix(checker.toParent);
         gl.glMatrixMode(GL.GL_TEXTURE);
@@ -14657,6 +14744,11 @@
 //                System.err.println("view = " + view[8] + " " + view[9] + " " + view[10] + " " + view[11]);
 //                System.err.println("view = " + view[12] + " " + view[13] + " " + view[14] + " " + view[15]);
                 
+                // Will fit the mesh !!!
+                selectedpoint.toParent[0][0] = 0.0001;
+                selectedpoint.toParent[1][1] = 0.0001;
+                selectedpoint.toParent[2][2] = 0.0001;
+                
                 glu.gluUnProject(x,y,depth,view,0,tempmat2,0,viewport,0,selectedpoint.toParent[3],0);
                 
 //                if (object.selection != null && object.selection.Size() > 0)
@@ -14700,16 +14792,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) GraphreeD.clone(selectedpoint);
+                        previousselectedpoint = (Sphere) GrafreeD.clone(selectedpoint);
                     }
                 }
                 
                 if (!movingcamera && !PAINTMODE)
                     object.editWindow.ScreenFitPoint(); // fev 2014
                 
-                if (PAINTMODE && GraphreeD.clipboard.size() == 1) // object.editWindow.copy.selection.Size() > 0)
+                if (PAINTMODE && GrafreeD.clipboard.size() == 1) // object.editWindow.copy.selection.Size() > 0)
                 {
-                    Object3D paintobj = GraphreeD.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++);
                     
@@ -15045,13 +15137,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++)
             {
@@ -15136,7 +15234,7 @@
 
                 double scale = ray.z; // 1; // cos
 
-                int p = pixels[newindex];
+                float depth = pixels[newindex];
 
                 /*
                 int newindex2 = (x + 1) * OCCLUSION_SIZE + y;
@@ -15160,10 +15258,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;
             }
             
@@ -15279,10 +15390,12 @@
     int AAbuffersize = 0;
     
     //double[] selectedpoint = new double[3];
-    static Sphere selectedpoint = new Sphere();
+    static Superellipsoid selectedpoint = new Superellipsoid();
     static Sphere previousselectedpoint = null;
-    static Sphere debugpoint = new Sphere();
-    static Sphere debugpoint2 = new Sphere();
+    static Sphere debugpointG = new Sphere();
+    static Sphere debugpointP = new Sphere();
+    static Sphere debugpointC = new Sphere();
+    static Sphere debugpointR = new Sphere();
     
     static Sphere debugpoints[] = new Sphere[8];
  
@@ -15335,7 +15448,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