From 9cf2c36d644d0aaa28797f57a3e71591d8e66973 Mon Sep 17 00:00:00 2001
From: Normand Briere <nbriere@noware.ca>
Date: Sun, 06 Oct 2019 10:56:18 -0400
Subject: [PATCH] Save image helper.

---
 Object3D.java |  410 +++++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 359 insertions(+), 51 deletions(-)

diff --git a/Object3D.java b/Object3D.java
index 3a1cb6e..0843864 100644
--- a/Object3D.java
+++ b/Object3D.java
@@ -41,6 +41,8 @@
         
     java.util.Hashtable<java.util.UUID, Object3D> versiontable; // = new java.util.Hashtable<java.util.UUID, Object3D>();
     
+    transient int tabIndex; // Tabs can change between sessions.
+    
     ScriptNode scriptnode;
 
     void deepCopyNode(Object3D other)
@@ -100,7 +102,7 @@
         other.softtouched = softtouched;
         
         other.random = random;
-        other.link2master = link2master;
+        other.link2master = Link2Support();
         other.transformcount = transformcount;
         other.marked = marked;
         other.skip = skip;
@@ -140,7 +142,7 @@
                     projectedVertices[i] = new cVector2(); // Others
                 }
                 projectedVertices[0].x = 100; // bump
-                projectedVertices[1].y = 5000; // punchthrough. only for png
+                projectedVertices[1].y = 1000; // punchthrough. only for png
         }
 
     void MinMax(cVector minima, cVector maxima)
@@ -220,7 +222,7 @@
             return;
 
         transientsupport = support;
-        transientlink2master = link2master;
+        transientlink2master = Link2Support();
 
         support = null;
         link2master = false;
@@ -633,7 +635,10 @@
     transient boolean keepdontselect;
     boolean dontselect = false;
     boolean hide = false;
-    boolean link2master = false; // performs reset support/master at each frame
+    
+    boolean link2master = false; // performs reset support/master at each frame (cannot rename due to serialization)
+    boolean link2support = false; // (cannot rename due to serialization)
+    
     boolean marked = false; // animation node
     boolean skip = false; // centroid issue
     boolean skipmocap = false; // mocap data
@@ -641,6 +646,9 @@
     boolean random = false;
     boolean speedup = false;
     boolean rewind = false;
+    
+    // Option to sort triangles, e.g. for transparency.
+    boolean sort = false;
     
     float NORMALPUSH = 0;
     
@@ -659,7 +667,7 @@
         {
             sorted = true;
             
-            for (int i=0; i<size()-1; i++)
+            for (int i=0; i<Size()-1; i++)
             {
                 Object3D obji = get(i);
                 Object3D objj = get(i+1);
@@ -683,7 +691,7 @@
         {
             sorted = true;
             
-            for (int i=0; i<size()-1; i++)
+            for (int i=0; i<Size()-1; i++)
             {
                 Object3D obji = get(i);
                 Object3D objj = get(i+1);
@@ -1020,7 +1028,7 @@
     
     void Step()
     {
-        // marde pour serialization de Texture
+        // patch pour serialization de Texture
         resetmaxcount();
         resettransformcount();
         resetstep();
@@ -1134,6 +1142,8 @@
                 (Globals.DrawMode() == iCameraPane.SHADOW || !Globals.RENDERSHADOW || !Globals.COMPUTESHADOWWHENLIVE) &&
                 currentframe != Globals.framecount)
         {
+            Globals.lighttouched = true;
+            
             currentframe = Globals.framecount;
             
 //            System.err.println("transformcount = " + transformcount);
@@ -1213,24 +1223,28 @@
     {
 //        return true;
         
-        if (material == null || material.multiply)
-            return true;
+//        if (material == null || material.multiply)
+//            return true;
+//        
+//        if (projectedVertices != null && projectedVertices.length > 2 && projectedVertices[2].y >= 10000)
+//            return false;
+//        
+//        // Transparent objects are dynamic because we have to sort the triangles.
+//        return material.opacity > 0.99;
         
-        if (projectedVertices != null && projectedVertices.length > 2 && projectedVertices[2].y >= 10000)
-            return false;
-        
-        // Transparent objects are dynamic because we have to sort the triangles.
-        return material.opacity > 0.99;
+        return !sort;
     }
     
     boolean IsOpaque()
     {
 //        return true;
         
-        if (material == null || material.multiply)
-            return true;
+//        if (material == null || material.multiply)
+//            return true;
+//        
+//        return material.opacity > 0.99;
         
-        return material.opacity > 0.99;
+        return !sort;
     }
     
     Object3D()
@@ -3407,7 +3421,7 @@
         }
     }
     
-    public void Scale(int scale)
+    public void Scale(float scale)
     {
         Object3D obj = this;
         
@@ -4041,6 +4055,7 @@
     
     void RevertMeshes()
     {
+        // BLOCKLOOP
         if (this instanceof cMesh)
         {
             ((cMesh)this).Revert();
@@ -4071,11 +4086,6 @@
             Touch();
         }
         
-        ResetRecur();
-    }
-    
-    void ResetRecur()
-    {
         for (int i = 0; i < size(); i++)
         {
             Object3D child = (Object3D) get(i); // reserve(i);
@@ -4098,11 +4108,6 @@
         Step();
         Touch();
         
-        StepRecur();
-    }
-    
-    void StepRecur()
-    {
         for (int i = 0; i < size(); i++)
         {
             Object3D child = (Object3D) get(i); // reserve(i);
@@ -4289,8 +4294,8 @@
         Touch();
     }
     
-    transient cVector min = new cVector();
-    transient cVector max = new cVector();
+    transient cVector min;
+    transient cVector max;
     
     void getBounds(cVector minima, cVector maxima, boolean xform)
     {
@@ -4306,7 +4311,7 @@
         if (blockloop)
             return;
         
-        if (min == null) // ???
+        if (min == null)
         {
             min = new cVector();
             max = new cVector();
@@ -4347,7 +4352,7 @@
         
         if (bRep != null)
         {
-            bRep.getBounds(minima,maxima,this);
+            bRep.getBounds(minima, maxima, xform?this:null);
         }
 
         if (false) // xform)
@@ -6156,7 +6161,7 @@
     boolean NeedSupport()
     {
         return
-                CameraPane.SUPPORT && (!CameraPane.movingcamera || (!Globals.FREEZEONMOVE && Globals.isLIVE())) && link2master && /*live &&*/ support != null
+                CameraPane.SUPPORT && (!CameraPane.movingcamera || (!Globals.FREEZEONMOVE && Globals.isLIVE())) && Link2Support() && /*live &&*/ support != null
                  // PROBLEM with CROWD!!
                     && (Globals.DrawMode() == iCameraPane.SHADOW || !Globals.RENDERSHADOW || Globals.CROWD);
     }
@@ -6179,6 +6184,15 @@
         return live && bRep != null;
     }
     
+    boolean Link2Support()
+    {
+        return link2master || link2support;
+    }
+    
+    static cVector minima = new cVector();
+    static cVector maxima = new cVector();
+    static javax.vecmath.Point3d center = new javax.vecmath.Point3d();
+    
     void Draw(iCameraPane display, Object3D /*Composite*/ root, boolean selected, boolean blocked)
     {
         Invariants(); // june 2013
@@ -6186,6 +6200,29 @@
         if (support != null)
         {
         //    System.err.println("Draw " + this + " Frame # " + ((Mocap)((Merge)support).object).frame);
+        }
+        
+        if (false) // live && Link2Support() && support == null && !this.marked) // project on ground
+        {
+            getBounds(minima, maxima, true);
+            center.x = (minima.x + maxima.x) / 2;
+            center.y = 10000; // (minima.y + maxima.y) / 2;
+            center.z = (minima.z + maxima.z) / 2;
+
+            Ray ray = new Ray(center, new Vector3d(0,-1,0));
+
+            IntersectResult res = new IntersectResult();
+            res.t = Double.POSITIVE_INFINITY;
+
+            if (Grafreed.grafreed.universe.intersect(ray, res))
+            {
+                double resx = ray.eyePoint.x + ray.viewDirection.x * res.t;
+                double resy = ray.eyePoint.y + ray.viewDirection.y * res.t;
+                double resz = ray.eyePoint.z + ray.viewDirection.z * res.t;
+                
+                LA.matTranslate(toParent, 0, resy - minima.y, 0);
+                LA.matInvert(toParent, fromParent);
+            }
         }
         
         if (display.DrawMode() == iCameraPane.SELECTION &&
@@ -6240,7 +6277,7 @@
             support = support;
         
         boolean usecalllists = !IsDynamic() &&
-                                    IsStatic() && GetBRep() != null && (!CameraPane.SUPPORT || support == null || !link2master); // !(this instanceof cSpring) && !(this instanceof BezierPatch);
+                                    IsStatic() && GetBRep() != null && (!CameraPane.SUPPORT || support == null || !Link2Support()); // !(this instanceof cSpring) && !(this instanceof BezierPatch);
         //boolean usecalllists = false; //!IsLive(); // IsStatic() && GetBRep() != null && (!CameraPane.SUPPORT || support == null) && !link2master; // !(this instanceof cSpring) && !(this instanceof BezierPatch);
         
         //usecalllists &= display.DrawMode() == display.DEFAULT; // Don't compute list in shadow pass.
@@ -6251,7 +6288,8 @@
             bRep.displaylist = 0;
         }
    //     usecalllists &= !(parent instanceof RandomNode);
-   //     usecalllists = false;
+        if (CameraPane.BOXMODE) // Too dynamic
+            usecalllists = false;
 
         if (display.DrawMode() == display.SHADOW)
             //GetBRep() != null)
@@ -6265,7 +6303,7 @@
         
         if (!selectmode && //display.DrawMode() != display.SELECTION &&
                 //(touched || (bRep != null && bRep.displaylist <= 0)))
-                (Globals.isLIVE() && Globals.COMPUTESHADOWWHENLIVE || touched && Globals.COMPUTESHADOWWHENLIVE)) // || (bRep != null && bRep.displaylist <= 0)))
+                ((Globals.isLIVE() && Globals.COMPUTESHADOWWHENLIVE) || touched)) // || (bRep != null && bRep.displaylist <= 0)))
         {
             Globals.lighttouched = true;
         } // all panes...
@@ -6307,8 +6345,8 @@
             Globals.lighttouched = true; // all panes...
         }
 
-        touched = GetBRep() == null; // this instanceof Composite || this instanceof FileObject; // false;
-        //touched = false;
+        //touched = GetBRep() == null; // this instanceof Composite || this instanceof FileObject; // false;
+        touched = false;
 
         if (this instanceof Texture || this instanceof TextureNode)
         {
@@ -6347,7 +6385,7 @@
         {
             if (display.DrawMode() == iCameraPane.SHADOW)
             {
-                if (!link2master // tricky to cull in shadow mode.
+                if (!Link2Support() // tricky to cull in shadow mode.
                          && GetBRep().FrustumCull(this, null, display.LightCamera(), true))
                 {
                     //System.out.print("CULLED");
@@ -6455,6 +6493,7 @@
     boolean failedPigment = false;
     boolean failedBump = false;
     
+    CameraPane.lastObject = this;
     try
     {
         display.BindPigmentTexture(tex, texres);
@@ -6819,7 +6858,8 @@
 
         //javax.media.opengl.GL gl = display.GetGL();
 
-        if (CameraPane.BOXMODE && !selected) // || CameraPane.movingcamera)
+        if (CameraPane.BOXMODE && //!Link2Support()) //
+                                !selected) // || CameraPane.movingcamera)
         {
             int fc = bRep.FaceCount();
             int vc = bRep.VertexCount();
@@ -6981,7 +7021,7 @@
                         facescompare[k] = new FaceCompare(k);
                     }
                     
-                    center = new cVector();
+                    centertriangle = new cVector();
                 }
                 else
                 {
@@ -7104,7 +7144,7 @@
          */
     }
 
-    transient cVector center;
+    transient cVector centertriangle;
     
     class FaceCompare implements Comparable
     {
@@ -7133,14 +7173,14 @@
                 Vertex q = bRep.GetVertex(face.q);
                 Vertex r = bRep.GetVertex(face.r);
 
-                center.set(p);
-                center.add(q);
-                center.add(r);
-                center.mul(1.0/3);
+                centertriangle.set(p);
+                centertriangle.add(q);
+                centertriangle.add(r);
+                centertriangle.mul(1.0/3);
                 
-                center.sub(Globals.theRenderer.EyeCamera().location);
+                centertriangle.sub(Globals.theRenderer.EyeCamera().location);
                 
-                distance = center.dot(center);
+                distance = centertriangle.dot(centertriangle);
             }
             
             return distance;
@@ -8323,7 +8363,7 @@
 
     public int hashCode()
     {
-        // Fuck Vector...
+        // Do not use Vector...
         return System.identityHashCode(this);
     }
     
@@ -8563,8 +8603,8 @@
     private static cVector2 qq2 = new cVector2();
     private static cVector2 rr2 = new cVector2();
     private static cVector2 ss2 = new cVector2();
-    private static cVector edge1 = new cVector();
-    private static cVector edge2 = new cVector();
+//    private static cVector edge1 = new cVector();
+//    private static cVector edge2 = new cVector();
     //private static cVector norm = new cVector();
     /*transient private*/ int hitSomething;
     static final int hitCenter = 1;
@@ -8762,7 +8802,275 @@
 
         Touch();
     }
+    
+    static Vertex s1 = new Vertex();
+    static Vertex s2 = new Vertex();
+    static Vertex s3 = new Vertex();
 
+    boolean intersectMesh(Ray ray, IntersectResult result)
+    {
+        boolean success = false;
+        
+        if (bRep.stripified)
+        {
+            int[] strips = bRep.getRawIndices();
+            
+                // TRIANGLE STRIP ARRAY
+                if (bRep.trimmed)
+                {
+                    float[] v = bRep.getRawVertices();
+
+                    int count3 = 0;
+
+                    if (v.length > 0)
+                    {
+                        for (int i = 0; i < strips.length; i++)
+                        {
+                            s1.set(v[count3], v[count3 + 1], v[count3 + 2]);
+                            count3 += 3;
+                            
+                            s2.set(v[count3], v[count3 + 1], v[count3 + 2]);
+                            count3 += 3;
+                            
+                            for (int j = 0; j < strips[i] - 2; j++)
+                            {
+                                s3.set(v[count3], v[count3 + 1], v[count3 + 2]);
+                                count3 += 3;
+                                
+                                if (j%2 == 0)
+                                      success |= intersectTriangle(ray, result, s1, s2, s3);
+                                else
+                                      success |= intersectTriangle(ray, result, s1, s3, s2);
+
+                                s1.set(s2);
+                                s2.set(s3);
+                            }
+                        }
+                    }
+
+                    assert count3 == v.length;
+                }
+                else // !trimmed
+                {
+                    int count = 0;
+                    for (int i = 0; i < strips.length; i++)
+                    {
+                        Vertex p = bRep.GetVertex(bRep.indices[count++]);
+                        Vertex q = bRep.GetVertex(bRep.indices[count++]);
+                        
+                        for (int j = 0; j < strips[i] - 2; j++)
+                        {
+                            Vertex r = bRep.GetVertex(bRep.indices[count++]);
+
+                            if (j%2 == 0)
+                                  success |= intersectTriangle(ray, result, p, q, r);
+                            else
+                                  success |= intersectTriangle(ray, result, p, r, q);
+                            
+                            p = q;
+                            q = r;
+                        }
+                    }
+                }
+        } else // catch (Error e)
+        {
+            int facecount = bRep.FaceCount();
+            for (int i = 0; i < facecount; i++)
+            {
+                Face face = bRep.GetFace(i);
+
+                Vertex p = bRep.GetVertex(face.p);
+                Vertex q = bRep.GetVertex(face.q);
+                Vertex r = bRep.GetVertex(face.r);
+
+                success |= intersectTriangle(ray, result, p, q, r);
+            }
+        }
+        
+        return success;
+    }
+    
+    static cVector eye = new cVector();
+    static cVector dir = new cVector();
+    
+    transient BBox bbox = null;
+    
+    boolean intersect(Ray ray, IntersectResult result)
+    {
+        double eyex = ray.eyePoint.x;
+        double eyey = ray.eyePoint.y;
+        double eyez = ray.eyePoint.z;
+        
+        double dirx = ray.viewDirection.x;
+        double diry = ray.viewDirection.y;
+        double dirz = ray.viewDirection.z;
+        
+        if (this.fromParent != null && !(this instanceof TextureNode))
+        {
+            eye.x = eyex;
+            eye.y = eyey;
+            eye.z = eyez;
+            dir.x = dirx;
+            dir.y = diry;
+            dir.z = dirz;
+            
+            LA.xformPos(eye, this.fromParent, eye);
+            LA.xformDir(dir, this.fromParent, dir);
+            
+            ray.eyePoint.x = eye.x;
+            ray.eyePoint.y = eye.y;
+            ray.eyePoint.z = eye.z;
+
+            ray.viewDirection.x = dir.x;
+            ray.viewDirection.y = dir.y;
+            ray.viewDirection.z = dir.z;
+        }
+        
+        boolean success = false;
+        
+        boolean touch = false;
+        
+        if (bRep != null && Link2Support())
+        {
+            if (bbox == null)
+            {
+                bbox = new BBox();
+                
+                cVector min = new cVector();
+                cVector max = new cVector();
+                
+                this.getBounds(min, max, true);
+                
+                bbox.min.x = min.x;
+                bbox.min.y = min.y;
+                bbox.min.z = min.z;
+                
+                bbox.max.x = max.x;
+                bbox.max.y = max.y;
+                bbox.max.z = max.z;
+            }
+            
+            if (true) // NOT WORKING bbox.intersect(ray, result))
+            {
+                success |= intersectMesh(ray, result);
+            }
+            else
+            {
+                //this.hide = true;
+                touch = true;
+            }
+        }
+        
+        for (int i=0; i<size(); i++)
+        {
+            Object3D child = (Object3D) reserve(i);
+            
+            if (child == null)
+                continue;
+
+            success |= child.intersect(ray, result);
+            release(i);
+        }
+        
+        ray.eyePoint.x = eyex;
+        ray.eyePoint.y = eyey;
+        ray.eyePoint.z = eyez;
+        
+        ray.viewDirection.x = dirx;
+        ray.viewDirection.y = diry;
+        ray.viewDirection.z = dirz;
+        
+//        if (touch)
+//            this.Touch(); // refresh display list
+            
+        return success;
+    }
+
+    static Vector3d edge1 = new Vector3d();
+    static Vector3d edge2 = new Vector3d();
+    static Vector3d P = new Vector3d();
+    static Vector3d T = new Vector3d();
+    static Vector3d Q = new Vector3d();
+    
+    private boolean intersectTriangle(Ray ray, IntersectResult result, Vertex v1, Vertex v2, Vertex v3)
+    {
+        if (false)
+        {
+            result.t = 0;
+            return true;
+        }
+        
+        /*
+        Fast, Minimum Storage Ray/Triangle Intersection, Moller et al.
+        
+        Reference: http://www.cs.virginia.edu/~gfx/Courses/2003/ImageSynthesis/papers/Acceleration/Fast%20MinimumStorage%20RayTriangle%20Intersection.pdf
+         */
+
+        // Calculate edges of the triangle
+        edge1.set(v2.x - v1.x, v2.y - v1.y, v2.z - v1.z);
+        edge2.set(v3.x - v1.x, v3.y - v1.y, v3.z - v1.z);
+
+        // Calculate the determinant (U parameter)
+        P.cross(ray.viewDirection, edge2);
+        double det = edge1.dot(P);
+
+        if (det > -1e-9 && det < 1e-9)
+        {
+            return false;
+        }	// Ray lies in plane of triangle
+
+        double invDet = 1 / det;
+
+        // Calculate distance from vertex1 to ray origin
+        T.set(ray.eyePoint.x - v1.x, ray.eyePoint.y - v1.y, ray.eyePoint.z - v1.z);
+
+        double U = (T.dot(P)) * invDet;	// Calculate U parameter
+
+        if (U < 0.f || U > 1.f)
+        {
+            return false;
+        }	// Intersection lies outside of the triangle
+
+        // Calculate V parameter
+        Q.cross(T, edge1);
+
+        double V = ray.viewDirection.dot(Q) * invDet;
+
+        if (V < 0.f || (U + V) > 1.f)
+        {
+            return false;
+        }	// Intersection lies outside of the triangle
+
+        double t = edge2.dot(Q) * invDet;
+
+        if (t > 1e-9)	// Triangle and ray intersects
+        {
+            //result.isIntersected = true;
+            //result.id = id;
+
+            if (false) // isShadow)
+            {
+                result.t = t;
+                return true;
+            } else if (t < result.t)
+            {
+                result.object = this;
+
+                result.t = t;
+
+                //result.p.x = ray.eyePoint.x + ray.viewDirection.x * t;
+                //result.p.y = ray.eyePoint.y + ray.viewDirection.y * t;
+                //result.p.z = ray.eyePoint.z + ray.viewDirection.z * t;
+
+                result.n.cross(edge1, edge2);
+                result.n.normalize();
+            }
+            
+            return true;
+        }
+        
+        return false;
+    }
 
     public int Size()
     {

--
Gitblit v1.6.2