From 33504fc9a180903aace77613264550754fba5706 Mon Sep 17 00:00:00 2001
From: Normand Briere <nbriere@noware.ca>
Date: Thu, 15 Aug 2019 12:03:58 -0400
Subject: [PATCH] Shadow RGB

---
 CameraPane.java |  199 +++++++++++++++++++++++++++++++++++++------------
 1 files changed, 148 insertions(+), 51 deletions(-)

diff --git a/CameraPane.java b/CameraPane.java
index bd26db2..ebdca60 100644
--- a/CameraPane.java
+++ b/CameraPane.java
@@ -110,7 +110,11 @@
     //private Mat4f spotlightTransform = new Mat4f();
     //private Mat4f spotlightInverseTransform = new Mat4f();
     static GLContext glcontext = null;
-    /*static*/ com.sun.opengl.util.texture.Texture cubemap;
+    /*static*/ com.sun.opengl.util.texture.Texture cubemap; // Either custom or rgb
+    /*static*/ com.sun.opengl.util.texture.Texture cubemapcustom;
+    /*static*/ com.sun.opengl.util.texture.Texture cubemaprgb;
+    boolean transformMode;
+    
     boolean reverseUP = false;
     static boolean frozen = false;
     boolean enablebackspace = false; // patch for back buffer refresh
@@ -173,7 +177,7 @@
     static boolean doublesided = false; // true; // reversed normals are awful for conformance
     boolean anisotropy = true;
     boolean softshadow = true; // slower but better false;
-    boolean opacityhalo = false;
+    boolean opacityhalo = false; // reverse the halo effect (e.g. glass)
     
     boolean macromode = false;
     
@@ -188,15 +192,16 @@
     
     private File defaultDirectory = javax.swing.filechooser.FileSystemView.getFileSystemView().getDefaultDirectory();
 
-    public void LoadSkybox(String name, String ext, boolean mipmap) throws GLException
+    public com.sun.opengl.util.texture.Texture LoadSkybox(String name, String ext, boolean mipmap) throws GLException
     {
         try
         {
-            cubemap = LoadCubemap(getClass().getClassLoader(), name, ext, mipmap);
+            return LoadCubemap(getClass().getClassLoader(), name, ext, mipmap);
         } catch (IOException e)
         {
             System.out.println("NAME = " + name);
             e.printStackTrace(); // throw new RuntimeException(e);
+            return null;
         }
     }
     
@@ -2512,7 +2517,7 @@
                     com.sun.opengl.util.texture.TextureIO.newTextureData(
                     getClass().getClassLoader().getResourceAsStream(name),
                     true,
-                    com.sun.opengl.util.texture.TextureIO.PNG);
+                    GetFormat(name)); // com.sun.opengl.util.texture.TextureIO.PNG);
         } catch (java.io.IOException e)
         {
             throw new javax.media.opengl.GLException(e);
@@ -8358,7 +8363,7 @@
 //            else
 //                if (!texname.startsWith("/"))
 //                    texname = "/Users/nbriere/Textures/" + texname;
-            if (!FileExists(texname))
+            if (!FileExists(texname) && !texname.startsWith("@"))
             {
                     texname = fallbackTextureName;
             }
@@ -8441,6 +8446,15 @@
                                 new Exception().printStackTrace();
                         } else
                         {
+                        if (texname.startsWith("@"))
+                        {
+                //            texturecache = textures.get(texname); // suspicious
+                            if (texturecache == null)
+                                texturecache = new CacheTexture(GetResourceTexture(texname.substring(1), bump),resolution);
+                            else
+                                new Exception().printStackTrace();
+                        } else
+                        {
                             if (textureon)
                             {
                                 String cachename = texname;
@@ -8500,6 +8514,7 @@
                                     texturecache = new CacheTexture(texturedata,resolution);
         //texture = GetTexture(tex, bump);
                             }
+                        }
                         }
                     //}
                 }
@@ -8768,10 +8783,12 @@
 
         gl.glGetIntegerv(GL.GL_MAX_TEXTURE_STACK_DEPTH, temp, 0);
         MAXSTACK = temp[0];
-        System.out.println("GL_MAX_TEXTURE_STACK_DEPTH = " + MAXSTACK);
+        if (Globals.DEBUG)
+            System.out.println("GL_MAX_TEXTURE_STACK_DEPTH = " + MAXSTACK);
         gl.glGetIntegerv(GL.GL_MAX_MODELVIEW_STACK_DEPTH, temp, 0);
         MAXSTACK = temp[0];
-        System.out.println("GL_MAX_MODELVIEW_STACK_DEPTH = " + MAXSTACK);
+        if (Globals.DEBUG)
+            System.out.println("GL_MAX_MODELVIEW_STACK_DEPTH = " + MAXSTACK);
 
         // Use debug pipeline
         //drawable.setGL(new DebugGL(gl)); //
@@ -8779,7 +8796,8 @@
         gl = drawable.getGL(); //
 
         GL gl3 = getGL();
-        System.out.println("INIT GL IS: " + gl.getClass().getName());
+        if (Globals.DEBUG)
+            System.out.println("INIT GL IS: " + gl.getClass().getName());
 
 
         //float pos[] = { 100, 100, 100, 0 };
@@ -9220,6 +9238,8 @@
 
     void LoadEnvy(int which)
     {
+        assert(false);
+        
         String name;
         String ext;
 
@@ -9316,10 +9336,9 @@
     double[] light2camera = new double[16];
     
     //int newenvy = -1;
-    boolean envyoff = true; // false;
-    String skyboxname = "";
-    String skyboxext;
-    String loadedskyboxname = "";
+    //boolean envyoff = false;
+    
+    String loadedskyboxname;
     
     cVector light0 = new cVector(0, 0, 0); // 1,3,2);
     //float[] light0 = { 0,0,0 };
@@ -10842,7 +10861,7 @@
 
         if (wait)
         {
-            Sleep(500);
+            Sleep(200); // blocks everything
 
             wait = false;
         }
@@ -11065,10 +11084,34 @@
 //
 //        newenvy = -1;
         
-        if (!skyboxname.equals(this.loadedskyboxname))
+        if (transformMode) // object.skyboxname != null && object.skyboxname.equals("cubemaps/default-skyboxes/rgb"))
         {
-            LoadSkybox(skyboxname + "/", skyboxext, false);
-            loadedskyboxname = skyboxname;
+            if (cubemaprgb == null)
+            {
+                cubemaprgb = LoadSkybox("cubemaps/default-skyboxes/rgb" + "/", "jpg", false);
+            }
+            
+            cubemap = cubemaprgb;
+        }
+        else
+        {
+            if (object.skyboxname != null)
+            {
+                if (!object.skyboxname.equals(this.loadedskyboxname))
+                {
+                    if (cubemap != null && cubemap != cubemaprgb)
+                        cubemap.dispose();
+                    cubemapcustom = LoadSkybox(object.skyboxname + "/", object.skyboxext, false);
+                    loadedskyboxname = object.skyboxname;
+                }
+            }
+            else
+            {
+                cubemapcustom = null;
+                loadedskyboxname = null;
+            }
+            
+            cubemap = cubemapcustom;
         }
         
         ratio = ((double) getWidth()) / getHeight();
@@ -11372,7 +11415,7 @@
 
 //            if (cam != lightCamera)
             //for (int count = parentcam.GetTransformCount(); --count>=0;)
-                LA.xformDir(lightposition, parentcam.GlobalTransform(), lightposition); // may 2013
+                LA.xformDir(lightposition, parentcam.GlobalTransformInv(), lightposition); // may 2013
         }
         
         LA.xformDir(lightposition, cam.toScreen, lightposition);
@@ -12446,7 +12489,7 @@
 
         //gl.glProgramEnvParameter4fvARB(GL.GL_FRAGMENT_PROGRAM_ARB, 127, lightParams, 0);
 
-        String program0 =
+        String programmin =
                 // Min shader
                 "!!ARBfp1.0\n" +
                 "PARAM zero123      = { 0.0, 1.0, 2, 1.25 };" +
@@ -12513,7 +12556,7 @@
                 "MOV result.color, temp;" +
                 "END";
         
-        String program =
+        String programmax =
                 "!!ARBfp1.0\n" +
                 
                 //"OPTION ARB_fragment_program_shadow;" +
@@ -13038,7 +13081,7 @@
            // display shadow only (fakedepth == 0)
          "SUB temp.x, half.x, shadow.x;" +
          "MOV temp.y, -params5.z;" + // params6.x;" +
-         "SLT temp.z, temp.y, -one2048th.x;" +
+         "SLT temp.z, temp.y, -c256i.x;" +
          "SUB temp.y, one.x, temp.z;" +
          "MUL temp.x, temp.x, temp.y;" +
          "KIL temp.x;" +
@@ -13369,6 +13412,13 @@
             //once = true;
         }
 
+        String program = programmax;
+        
+        if (Globals.MINSHADER)
+        {
+            program = programmin;
+        }
+        
         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);
@@ -14618,9 +14668,9 @@
         MODIFIERS |= COMMAND;
         /**/
         if((mod&SHIFT) == SHIFT)
-        manipCamera.RotatePosition(0, -speed);
-        else
         manipCamera.BackForth(0, -speed*delta, 0); // getWidth());
+        else
+        manipCamera.RotatePosition(0, -speed);
         /**/
         if ((mod & SHIFT) == SHIFT)
         {
@@ -14639,9 +14689,9 @@
         MODIFIERS |= COMMAND;
         /**/
         if((mod&SHIFT) == SHIFT)
-        manipCamera.RotatePosition(0, speed);
-        else
         manipCamera.BackForth(0, speed*delta, 0); // getWidth());
+        else
+        manipCamera.RotatePosition(0, speed);
         /**/
         if ((mod & SHIFT) == SHIFT)
         {
@@ -15362,7 +15412,7 @@
                 OCCLUSION_CULLING ^= true;
                 System.out.println("OCCLUSION CULLING = " + OCCLUSION_CULLING);
                 break;
-            case '0': envyoff ^= true; repaint(); break;
+            //case '0': envyoff ^= true; repaint(); break;
             case '1':
             case '2':
             case '3':
@@ -15372,7 +15422,7 @@
             case '7':
             case '8':
             case '9':
-                if (envyoff)
+                if (true) // envyoff)
                 {
                     BGcolor = (key - '1')/8.f;
                 }
@@ -16084,7 +16134,7 @@
                                 gr.drawLine(X, Y, info.bounds.width/2, info.bounds.height/2);
                             break;
                             case Object3D.hitScale: gr.setColor(Color.cyan);
-                                gr.drawLine(X, Y, 0, 0);
+                                gr.drawLine(X, Y, info.bounds.width/2, info.bounds.height/2);
                             break;
                         }
 
@@ -16570,6 +16620,8 @@
     private /*static*/ boolean firstime;
     private /*static*/ cVector newView = new cVector();
     private static final String[] suffixes = {"posx", "negx", "posy", "negy", "posz", "negz"};
+    private static final String[] suffixes2 = {"east", "west", "top", "bottom", "north", "south"};
+    private static final String[] suffixes3 = {"ft", "bk", "up", "dn", "rt", "lf"};
     private static final int[] targets = {GL.GL_TEXTURE_CUBE_MAP_POSITIVE_X,
                                              GL.GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
                                              GL.GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
@@ -16582,22 +16634,56 @@
     {
         com.sun.opengl.util.texture.Texture cubemap = TextureIO.newTexture(GL.GL_TEXTURE_CUBE_MAP);
 
+        int usedsuf = 0;
+        
         for (int i = 0; i < suffixes.length; i++)
         {
-            String resourceName = basename + suffixes[i] + "." + suffix;
-            TextureData data = TextureIO.newTextureData(scope.getResourceAsStream(resourceName),
-                    mipmapped,
-                    FileUtil.getFileSuffix(resourceName));
-            if (data == null)
+            String[] suffixe = suffixes;
+            String[] fallback = suffixes2;
+            String[] fallfallback = suffixes3;
+            
+            for (int c=usedsuf; --c>=0;)
             {
-                throw new IOException("Unable to load texture " + resourceName);
+//                String[] temp = suffixe;
+//                suffixe = fallback;
+//                fallback = fallfallback;
+//                fallfallback = temp;
             }
+            
+            String resourceName = basename + suffixe[i] + "." + suffix;
+            TextureData data;
+            
+            try
+            {
+                data = TextureIO.newTextureData(scope.getResourceAsStream(resourceName),
+                        mipmapped,
+                        FileUtil.getFileSuffix(resourceName));
+            }
+            catch (Exception e)
+            {
+                try
+                {
+                    resourceName = basename + fallback[i] + "." + suffix;
+                    data = TextureIO.newTextureData(scope.getResourceAsStream(resourceName),
+                            mipmapped,
+                            FileUtil.getFileSuffix(resourceName));
+                }
+                catch (Exception e2)
+                {
+                    resourceName = basename + fallfallback[i] + "." + suffix;
+                    data = TextureIO.newTextureData(scope.getResourceAsStream(resourceName),
+                            mipmapped,
+                            FileUtil.getFileSuffix(resourceName));
+                }
+            }
+            
             //System.out.println("Target = " + targets[i]);
             cubemap.updateImage(data, targets[i]);
         }
 
         return cubemap;
     }
+    
     int bigsphere = -1;
 
     float BGcolor = 0.5f;
@@ -16606,7 +16692,9 @@
     
     private void DrawSkyBox(GL gl, float ratio)
     {
-        if (envyoff || cubemap == null)
+        if (//envyoff ||
+            WIREFRAME ||
+                cubemap == null)
         {
             gl.glClearColor(BGcolor, BGcolor, BGcolor, 1);
             gl.glClear(gl.GL_COLOR_BUFFER_BIT);
@@ -16623,14 +16711,14 @@
         gl.glLoadIdentity();
         gl.glScalef(1,ratio,1);
 
-        colorV[0] = 2;
-        colorV[1] = 2;
-        colorV[2] = 2;
-        colorV[3] = 1;
-        gl.glDisable(gl.GL_COLOR_MATERIAL);
-        gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_AMBIENT, colorV, 0);
-        
-        gl.glLightModelfv(gl.GL_LIGHT_MODEL_AMBIENT, ambientLight, 0);
+//        colorV[0] = 2;
+//        colorV[1] = 2;
+//        colorV[2] = 2;
+//        colorV[3] = 1;
+//        gl.glDisable(gl.GL_COLOR_MATERIAL);
+//        gl.glMaterialfv(gl.GL_FRONT_AND_BACK, gl.GL_AMBIENT, colorV, 0);
+//        
+//        gl.glLightModelfv(gl.GL_LIGHT_MODEL_AMBIENT, ambientLight, 0);
         
         //gl.glActiveTexture(GL.GL_TEXTURE1);
         //gl.glDisable(GL.GL_TEXTURE_CUBE_MAP);
@@ -16644,7 +16732,7 @@
         // GL_NORMAL_MAP texgen mode. Temporarily enabling lighting
         // causes the normals to be sent down. Thanks to Ken Dyke.
         //gl.glEnable(GL.GL_LIGHTING);
-        gl.glEnable(GL.GL_LIGHTING);
+        gl.glDisable(GL.GL_LIGHTING);
 
         gl.glTexGeni(GL.GL_S, GL.GL_TEXTURE_GEN_MODE, GL.GL_NORMAL_MAP);
         gl.glTexGeni(GL.GL_T, GL.GL_TEXTURE_GEN_MODE, GL.GL_NORMAL_MAP);
@@ -16663,6 +16751,7 @@
         {
             gl.glScalef(1.0f, -1.0f, 1.0f);
         }
+        gl.glScalef(-1.0f, 1.0f, 1.0f);
         gl.glMultMatrixd(viewrot_1, 0);
         gl.glTranslatef(0, 0, 0.5f); // (float)lightCamera.Distance());  // 0.5f);
         //viewer.updateInverseRotation(gl);
@@ -16703,7 +16792,8 @@
         gl.glDisable(GL.GL_TEXTURE_GEN_R);
 
         cubemap.disable();
-        ////cubemap.unbind();
+        //cubemap.dispose();
+        
         if (CULLFACE)
         {
             gl.glEnable(gl.GL_CULL_FACE);
@@ -17467,10 +17557,14 @@
             gl.glFlush();
             
             /**/
-            gl.glReadPixels(0, 0, OCCLUSION_SIZE, OCCLUSION_SIZE, GL.GL_DEPTH_COMPONENT, GL.GL_FLOAT, occlusionsizebuffer);
+            gl.glReadPixels(0, 0, OCCLUSION_SIZE, OCCLUSION_SIZE, GL.GL_DEPTH_COMPONENT, GL.GL_FLOAT, occlusiondepthbuffer);
 
-            float[] pixels = occlusionsizebuffer.array();
+            float[] depths = occlusiondepthbuffer.array();
 
+            gl.glReadPixels(0, 0, OCCLUSION_SIZE, OCCLUSION_SIZE, GL.GL_BGRA, GL.GL_UNSIGNED_INT_8_8_8_8_REV, occlusioncolorbuffer);
+
+            int[] pixels = selectsizebuffer.array();
+            
             double r = 0, g = 0, b = 0;
 
             double count = 0;
@@ -17481,7 +17575,7 @@
             
             double FACTOR = 1;
 
-            for (int i = 0; i < pixels.length; i++)
+            for (int i = 0; i < depths.length; i++)
             {
                 int x = i / OCCLUSION_SIZE - OCCLUSION_SIZE / 2;
                 int y = i % OCCLUSION_SIZE - OCCLUSION_SIZE / 2;
@@ -17564,7 +17658,7 @@
 
                 double scale = ray.z; // 1; // cos
 
-                float depth = pixels[newindex];
+                float depth = depths[newindex];
 
                 /*
                 int newindex2 = (x + 1) * OCCLUSION_SIZE + y;
@@ -17761,11 +17855,14 @@
     static IntBuffer AAbuffer; // = IntBuffer.allocate(MAX_SIZE*MAX_SIZE);
     static IntBuffer bigAAbuffer;
     static java.nio.FloatBuffer histogram = BufferUtil.newFloatBuffer(HISTOGRAM_SIZE * 3);
-    static IntBuffer texturesizebuffer = IntBuffer.allocate(TEX_SIZE * TEX_SIZE);
+    //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 java.nio.FloatBuffer occlusionsizebuffer = java.nio.FloatBuffer.allocate(OCCLUSION_SIZE * OCCLUSION_SIZE);
+    static java.nio.FloatBuffer occlusiondepthbuffer = java.nio.FloatBuffer.allocate(OCCLUSION_SIZE * OCCLUSION_SIZE);
+    
+    static IntBuffer occlusioncolorbuffer = IntBuffer.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