Normand Briere
2019-09-17 54adfcbf93eb477bedeec45409f36cf7e102b790
Object3D.java
....@@ -662,7 +662,7 @@
662662 {
663663 sorted = true;
664664
665
- for (int i=0; i<size()-1; i++)
665
+ for (int i=0; i<Size()-1; i++)
666666 {
667667 Object3D obji = get(i);
668668 Object3D objj = get(i+1);
....@@ -686,7 +686,7 @@
686686 {
687687 sorted = true;
688688
689
- for (int i=0; i<size()-1; i++)
689
+ for (int i=0; i<Size()-1; i++)
690690 {
691691 Object3D obji = get(i);
692692 Object3D objj = get(i+1);
....@@ -4048,6 +4048,7 @@
40484048
40494049 void RevertMeshes()
40504050 {
4051
+ // BLOCKLOOP
40514052 if (this instanceof cMesh)
40524053 {
40534054 ((cMesh)this).Revert();
....@@ -4078,11 +4079,6 @@
40784079 Touch();
40794080 }
40804081
4081
- ResetRecur();
4082
- }
4083
-
4084
- void ResetRecur()
4085
- {
40864082 for (int i = 0; i < size(); i++)
40874083 {
40884084 Object3D child = (Object3D) get(i); // reserve(i);
....@@ -4296,8 +4292,8 @@
42964292 Touch();
42974293 }
42984294
4299
- transient cVector min = new cVector();
4300
- transient cVector max = new cVector();
4295
+ transient cVector min;
4296
+ transient cVector max;
43014297
43024298 void getBounds(cVector minima, cVector maxima, boolean xform)
43034299 {
....@@ -4313,7 +4309,7 @@
43134309 if (blockloop)
43144310 return;
43154311
4316
- if (min == null) // ???
4312
+ if (min == null)
43174313 {
43184314 min = new cVector();
43194315 max = new cVector();
....@@ -4354,7 +4350,7 @@
43544350
43554351 if (bRep != null)
43564352 {
4357
- bRep.getBounds(minima,maxima,this);
4353
+ bRep.getBounds(minima, maxima, xform?this:null);
43584354 }
43594355
43604356 if (false) // xform)
....@@ -8571,8 +8567,8 @@
85718567 private static cVector2 qq2 = new cVector2();
85728568 private static cVector2 rr2 = new cVector2();
85738569 private static cVector2 ss2 = new cVector2();
8574
- private static cVector edge1 = new cVector();
8575
- private static cVector edge2 = new cVector();
8570
+// private static cVector edge1 = new cVector();
8571
+// private static cVector edge2 = new cVector();
85768572 //private static cVector norm = new cVector();
85778573 /*transient private*/ int hitSomething;
85788574 static final int hitCenter = 1;
....@@ -8770,7 +8766,269 @@
87708766
87718767 Touch();
87728768 }
8769
+
8770
+ static Vertex s1 = new Vertex();
8771
+ static Vertex s2 = new Vertex();
8772
+ static Vertex s3 = new Vertex();
87738773
8774
+ boolean intersectMesh(Ray ray, IntersectResult result)
8775
+ {
8776
+ boolean success = false;
8777
+
8778
+ if (bRep.stripified)
8779
+ {
8780
+ int[] strips = bRep.getRawIndices();
8781
+
8782
+ // TRIANGLE STRIP ARRAY
8783
+ if (bRep.trimmed)
8784
+ {
8785
+ float[] v = bRep.getRawVertices();
8786
+
8787
+ int count3 = 0;
8788
+
8789
+ if (v.length > 0)
8790
+ {
8791
+ for (int i = 0; i < strips.length; i++)
8792
+ {
8793
+ s1.set(v[count3], v[count3 + 1], v[count3 + 2]);
8794
+ count3 += 3;
8795
+
8796
+ s2.set(v[count3], v[count3 + 1], v[count3 + 2]);
8797
+ count3 += 3;
8798
+
8799
+ for (int j = 0; j < strips[i] - 2; j++)
8800
+ {
8801
+ s3.set(v[count3], v[count3 + 1], v[count3 + 2]);
8802
+ count3 += 3;
8803
+
8804
+ if (j%2 == 0)
8805
+ success |= intersectTriangle(ray, result, s1, s2, s3);
8806
+ else
8807
+ success |= intersectTriangle(ray, result, s1, s3, s2);
8808
+
8809
+ s1.set(s2);
8810
+ s2.set(s3);
8811
+ }
8812
+ }
8813
+ }
8814
+
8815
+ assert count3 == v.length;
8816
+ }
8817
+ else // !trimmed
8818
+ {
8819
+ int count = 0;
8820
+ for (int i = 0; i < strips.length; i++)
8821
+ {
8822
+ Vertex p = bRep.GetVertex(bRep.indices[count++]);
8823
+ Vertex q = bRep.GetVertex(bRep.indices[count++]);
8824
+
8825
+ for (int j = 0; j < strips[i] - 2; j++)
8826
+ {
8827
+ Vertex r = bRep.GetVertex(bRep.indices[count++]);
8828
+
8829
+ if (j%2 == 0)
8830
+ success |= intersectTriangle(ray, result, p, q, r);
8831
+ else
8832
+ success |= intersectTriangle(ray, result, p, r, q);
8833
+
8834
+ p = q;
8835
+ q = r;
8836
+ }
8837
+ }
8838
+ }
8839
+ } else // catch (Error e)
8840
+ {
8841
+ int facecount = bRep.FaceCount();
8842
+ for (int i = 0; i < facecount; i++)
8843
+ {
8844
+ Face face = bRep.GetFace(i);
8845
+
8846
+ Vertex p = bRep.GetVertex(face.p);
8847
+ Vertex q = bRep.GetVertex(face.q);
8848
+ Vertex r = bRep.GetVertex(face.r);
8849
+
8850
+ success |= intersectTriangle(ray, result, p, q, r);
8851
+ }
8852
+ }
8853
+
8854
+ return success;
8855
+ }
8856
+
8857
+ static cVector eye = new cVector();
8858
+ static cVector dir = new cVector();
8859
+
8860
+ transient BBox bbox = null;
8861
+
8862
+ boolean intersect(Ray ray, IntersectResult result)
8863
+ {
8864
+ double eyex = ray.eyePoint.x;
8865
+ double eyey = ray.eyePoint.y;
8866
+ double eyez = ray.eyePoint.z;
8867
+
8868
+ double dirx = ray.viewDirection.x;
8869
+ double diry = ray.viewDirection.y;
8870
+ double dirz = ray.viewDirection.z;
8871
+
8872
+ if (this.fromParent != null)
8873
+ {
8874
+ eye.x = eyex;
8875
+ eye.y = eyey;
8876
+ eye.z = eyez;
8877
+ dir.x = dirx;
8878
+ dir.y = diry;
8879
+ dir.z = dirz;
8880
+
8881
+ LA.xformPos(eye, this.fromParent, eye);
8882
+ LA.xformDir(dir, this.fromParent, dir);
8883
+
8884
+ ray.eyePoint.x = eye.x;
8885
+ ray.eyePoint.y = eye.y;
8886
+ ray.eyePoint.z = eye.z;
8887
+
8888
+ ray.viewDirection.x = dir.x;
8889
+ ray.viewDirection.y = dir.y;
8890
+ ray.viewDirection.z = dir.z;
8891
+ }
8892
+
8893
+ boolean success = false;
8894
+
8895
+ boolean touch = false;
8896
+
8897
+ if (bRep != null)
8898
+ {
8899
+ if (bbox == null)
8900
+ {
8901
+ bbox = new BBox();
8902
+
8903
+ cVector min = new cVector();
8904
+ cVector max = new cVector();
8905
+
8906
+ this.getBounds(min, max, false);
8907
+
8908
+ bbox.min.x = min.x;
8909
+ bbox.min.y = min.y;
8910
+ bbox.min.z = min.z;
8911
+
8912
+ bbox.max.x = max.x;
8913
+ bbox.max.y = max.y;
8914
+ bbox.max.z = max.z;
8915
+ }
8916
+
8917
+ if (bbox.intersect(ray, result))
8918
+ {
8919
+ success |= intersectMesh(ray, result);
8920
+ }
8921
+ else
8922
+ {
8923
+ //this.hide = true;
8924
+ touch = true;
8925
+ }
8926
+ }
8927
+
8928
+ for (int i=0; i<size(); i++)
8929
+ {
8930
+ Object3D child = (Object3D) reserve(i);
8931
+
8932
+ if (child == null)
8933
+ continue;
8934
+
8935
+ success |= child.intersect(ray, result);
8936
+ release(i);
8937
+ }
8938
+
8939
+ ray.eyePoint.x = eyex;
8940
+ ray.eyePoint.y = eyey;
8941
+ ray.eyePoint.z = eyez;
8942
+
8943
+ ray.viewDirection.x = dirx;
8944
+ ray.viewDirection.y = diry;
8945
+ ray.viewDirection.z = dirz;
8946
+
8947
+// if (touch)
8948
+// this.Touch(); // refresh display list
8949
+
8950
+ return success;
8951
+ }
8952
+
8953
+ static Vector3d edge1 = new Vector3d();
8954
+ static Vector3d edge2 = new Vector3d();
8955
+ static Vector3d P = new Vector3d();
8956
+ static Vector3d T = new Vector3d();
8957
+ static Vector3d Q = new Vector3d();
8958
+
8959
+ private boolean intersectTriangle(Ray ray, IntersectResult result, Vertex v1, Vertex v2, Vertex v3)
8960
+ {
8961
+ /*
8962
+ Fast, Minimum Storage Ray/Triangle Intersection, Moller et al.
8963
+
8964
+ Reference: http://www.cs.virginia.edu/~gfx/Courses/2003/ImageSynthesis/papers/Acceleration/Fast%20MinimumStorage%20RayTriangle%20Intersection.pdf
8965
+ */
8966
+
8967
+ // Calculate edges of the triangle
8968
+ edge1.set(v2.x - v1.x, v2.y - v1.y, v2.z - v1.z);
8969
+ edge2.set(v3.x - v1.x, v3.y - v1.y, v3.z - v1.z);
8970
+
8971
+ // Calculate the determinant (U parameter)
8972
+ P.cross(ray.viewDirection, edge2);
8973
+ double det = edge1.dot(P);
8974
+
8975
+ if (det > -1e-9 && det < 1e-9)
8976
+ {
8977
+ return false;
8978
+ } // Ray lies in plane of triangle
8979
+
8980
+ double invDet = 1 / det;
8981
+
8982
+ // Calculate distance from vertex1 to ray origin
8983
+ T.set(ray.eyePoint.x - v1.x, ray.eyePoint.y - v1.y, ray.eyePoint.z - v1.z);
8984
+
8985
+ double U = (T.dot(P)) * invDet; // Calculate U parameter
8986
+
8987
+ if (U < 0.f || U > 1.f)
8988
+ {
8989
+ return false;
8990
+ } // Intersection lies outside of the triangle
8991
+
8992
+ // Calculate V parameter
8993
+ Q.cross(T, edge1);
8994
+
8995
+ double V = ray.viewDirection.dot(Q) * invDet;
8996
+
8997
+ if (V < 0.f || (U + V) > 1.f)
8998
+ {
8999
+ return false;
9000
+ } // Intersection lies outside of the triangle
9001
+
9002
+ double t = edge2.dot(Q) * invDet;
9003
+
9004
+ if (t > 1e-9) // Triangle and ray intersects
9005
+ {
9006
+ //result.isIntersected = true;
9007
+ //result.id = id;
9008
+
9009
+ if (false) // isShadow)
9010
+ {
9011
+ result.t = t;
9012
+ return true;
9013
+ } else if (t < result.t)
9014
+ {
9015
+ result.object = this;
9016
+
9017
+ result.t = t;
9018
+
9019
+ //result.p.x = ray.eyePoint.x + ray.viewDirection.x * t;
9020
+ //result.p.y = ray.eyePoint.y + ray.viewDirection.y * t;
9021
+ //result.p.z = ray.eyePoint.z + ray.viewDirection.z * t;
9022
+
9023
+ result.n.cross(edge1, edge2);
9024
+ result.n.normalize();
9025
+ }
9026
+
9027
+ return true;
9028
+ }
9029
+
9030
+ return false;
9031
+ }
87749032
87759033 public int Size()
87769034 {