Normand Briere
2019-10-06 9cf2c36d644d0aaa28797f57a3e71591d8e66973
Object3D.java
....@@ -25,9 +25,12 @@
2525 // Use GetUUID for backward compatibility with null.
2626 private UUID uuid = UUID.randomUUID();
2727
28
- // TEMPORARY for mocap undo. No need to be transient.
28
+ // TEMPORARY for versions. No need to be transient.
2929 mocap.reader.BVHReader.BVHResult savebvh;
3030 Object3D saveskeleton;
31
+
32
+ // FileObject
33
+ Object3D savefilecontent;
3134 //
3235
3336 String skyboxname;
....@@ -37,6 +40,8 @@
3740 int versionindex = -1;
3841
3942 java.util.Hashtable<java.util.UUID, Object3D> versiontable; // = new java.util.Hashtable<java.util.UUID, Object3D>();
43
+
44
+ transient int tabIndex; // Tabs can change between sessions.
4045
4146 ScriptNode scriptnode;
4247
....@@ -97,7 +102,7 @@
97102 other.softtouched = softtouched;
98103
99104 other.random = random;
100
- other.link2master = link2master;
105
+ other.link2master = Link2Support();
101106 other.transformcount = transformcount;
102107 other.marked = marked;
103108 other.skip = skip;
....@@ -116,11 +121,12 @@
116121 {
117122 int count = 0;
118123
119
- for (int i = versionlist.length; --i >= 0;)
120
- {
121
- if (versionlist[i] != null)
122
- count++;
123
- }
124
+ if (versionlist != null)
125
+ for (int i = versionlist.length; --i >= 0;)
126
+ {
127
+ if (versionlist[i] != null)
128
+ count++;
129
+ }
124130
125131 return count;
126132 }
....@@ -136,6 +142,7 @@
136142 projectedVertices[i] = new cVector2(); // Others
137143 }
138144 projectedVertices[0].x = 100; // bump
145
+ projectedVertices[1].y = 1000; // punchthrough. only for png
139146 }
140147
141148 void MinMax(cVector minima, cVector maxima)
....@@ -215,7 +222,7 @@
215222 return;
216223
217224 transientsupport = support;
218
- transientlink2master = link2master;
225
+ transientlink2master = Link2Support();
219226
220227 support = null;
221228 link2master = false;
....@@ -628,7 +635,10 @@
628635 transient boolean keepdontselect;
629636 boolean dontselect = false;
630637 boolean hide = false;
631
- boolean link2master = false; // performs reset support/master at each frame
638
+
639
+ boolean link2master = false; // performs reset support/master at each frame (cannot rename due to serialization)
640
+ boolean link2support = false; // (cannot rename due to serialization)
641
+
632642 boolean marked = false; // animation node
633643 boolean skip = false; // centroid issue
634644 boolean skipmocap = false; // mocap data
....@@ -636,6 +646,9 @@
636646 boolean random = false;
637647 boolean speedup = false;
638648 boolean rewind = false;
649
+
650
+ // Option to sort triangles, e.g. for transparency.
651
+ boolean sort = false;
639652
640653 float NORMALPUSH = 0;
641654
....@@ -654,7 +667,7 @@
654667 {
655668 sorted = true;
656669
657
- for (int i=0; i<size()-1; i++)
670
+ for (int i=0; i<Size()-1; i++)
658671 {
659672 Object3D obji = get(i);
660673 Object3D objj = get(i+1);
....@@ -678,7 +691,7 @@
678691 {
679692 sorted = true;
680693
681
- for (int i=0; i<size()-1; i++)
694
+ for (int i=0; i<Size()-1; i++)
682695 {
683696 Object3D obji = get(i);
684697 Object3D objj = get(i+1);
....@@ -1015,7 +1028,7 @@
10151028
10161029 void Step()
10171030 {
1018
- // marde pour serialization de Texture
1031
+ // patch pour serialization de Texture
10191032 resetmaxcount();
10201033 resettransformcount();
10211034 resetstep();
....@@ -1129,6 +1142,8 @@
11291142 (Globals.DrawMode() == iCameraPane.SHADOW || !Globals.RENDERSHADOW || !Globals.COMPUTESHADOWWHENLIVE) &&
11301143 currentframe != Globals.framecount)
11311144 {
1145
+ Globals.lighttouched = true;
1146
+
11321147 currentframe = Globals.framecount;
11331148
11341149 // System.err.println("transformcount = " + transformcount);
....@@ -1208,24 +1223,28 @@
12081223 {
12091224 // return true;
12101225
1211
- if (material == null || material.multiply)
1212
- return true;
1226
+// if (material == null || material.multiply)
1227
+// return true;
1228
+//
1229
+// if (projectedVertices != null && projectedVertices.length > 2 && projectedVertices[2].y >= 10000)
1230
+// return false;
1231
+//
1232
+// // Transparent objects are dynamic because we have to sort the triangles.
1233
+// return material.opacity > 0.99;
12131234
1214
- if (projectedVertices != null && projectedVertices.length > 2 && projectedVertices[2].y >= 10000)
1215
- return false;
1216
-
1217
- // Transparent objects are dynamic because we have to sort the triangles.
1218
- return material.opacity > 0.99;
1235
+ return !sort;
12191236 }
12201237
12211238 boolean IsOpaque()
12221239 {
12231240 // return true;
12241241
1225
- if (material == null || material.multiply)
1226
- return true;
1242
+// if (material == null || material.multiply)
1243
+// return true;
1244
+//
1245
+// return material.opacity > 0.99;
12271246
1228
- return material.opacity > 0.99;
1247
+ return !sort;
12291248 }
12301249
12311250 Object3D()
....@@ -1314,6 +1333,30 @@
13141333 return false;
13151334 }
13161335
1336
+ void ResetUUIDs()
1337
+ {
1338
+ if (blockloop)
1339
+ {
1340
+ return;
1341
+ }
1342
+
1343
+ this.uuid = null;
1344
+
1345
+ blockloop = true;
1346
+
1347
+ for (int i = 0; i < Size(); i++)
1348
+ {
1349
+ Object3D obj = (Object3D) Children().get(i);
1350
+
1351
+ if (obj != null)
1352
+ {
1353
+ obj.ResetUUIDs();
1354
+ }
1355
+ }
1356
+
1357
+ blockloop = false;
1358
+ }
1359
+
13171360 boolean IsInfinite()
13181361 {
13191362 if (blockloop)
....@@ -3262,7 +3305,10 @@
32623305 {
32633306 if (bRep != null)
32643307 {
3308
+ //bRep.GenerateNormals2(crease); // in-place doesn't work. it gives wrong normals (diamond artifact).
32653309 bRep.GenerateNormals(crease);
3310
+ if (!bRep.trimmed)
3311
+ bRep.MergeNormals();
32663312 Touch();
32673313 }
32683314 }
....@@ -3375,7 +3421,7 @@
33753421 }
33763422 }
33773423
3378
- public void Scale(int scale)
3424
+ public void Scale(float scale)
33793425 {
33803426 Object3D obj = this;
33813427
....@@ -3678,6 +3724,7 @@
36783724 if (bRep != null)
36793725 {
36803726 //bRep.RemoveOneTriangle();
3727
+ System.out.println();
36813728 System.out.println("Reducing " + this);
36823729 if (name != null && name.contains("lockpickstraps"))
36833730 name = name;
....@@ -4008,6 +4055,7 @@
40084055
40094056 void RevertMeshes()
40104057 {
4058
+ // BLOCKLOOP
40114059 if (this instanceof cMesh)
40124060 {
40134061 ((cMesh)this).Revert();
....@@ -4038,11 +4086,6 @@
40384086 Touch();
40394087 }
40404088
4041
- ResetRecur();
4042
- }
4043
-
4044
- void ResetRecur()
4045
- {
40464089 for (int i = 0; i < size(); i++)
40474090 {
40484091 Object3D child = (Object3D) get(i); // reserve(i);
....@@ -4065,11 +4108,6 @@
40654108 Step();
40664109 Touch();
40674110
4068
- StepRecur();
4069
- }
4070
-
4071
- void StepRecur()
4072
- {
40734111 for (int i = 0; i < size(); i++)
40744112 {
40754113 Object3D child = (Object3D) get(i); // reserve(i);
....@@ -4256,8 +4294,8 @@
42564294 Touch();
42574295 }
42584296
4259
- transient cVector min = new cVector();
4260
- transient cVector max = new cVector();
4297
+ transient cVector min;
4298
+ transient cVector max;
42614299
42624300 void getBounds(cVector minima, cVector maxima, boolean xform)
42634301 {
....@@ -4273,7 +4311,7 @@
42734311 if (blockloop)
42744312 return;
42754313
4276
- if (min == null) // ???
4314
+ if (min == null)
42774315 {
42784316 min = new cVector();
42794317 max = new cVector();
....@@ -4314,7 +4352,7 @@
43144352
43154353 if (bRep != null)
43164354 {
4317
- bRep.getBounds(minima,maxima,this);
4355
+ bRep.getBounds(minima, maxima, xform?this:null);
43184356 }
43194357
43204358 if (false) // xform)
....@@ -6123,7 +6161,7 @@
61236161 boolean NeedSupport()
61246162 {
61256163 return
6126
- CameraPane.SUPPORT && (!CameraPane.movingcamera || (!Globals.FREEZEONMOVE && Globals.isLIVE())) && link2master && /*live &&*/ support != null
6164
+ CameraPane.SUPPORT && (!CameraPane.movingcamera || (!Globals.FREEZEONMOVE && Globals.isLIVE())) && Link2Support() && /*live &&*/ support != null
61276165 // PROBLEM with CROWD!!
61286166 && (Globals.DrawMode() == iCameraPane.SHADOW || !Globals.RENDERSHADOW || Globals.CROWD);
61296167 }
....@@ -6146,6 +6184,15 @@
61466184 return live && bRep != null;
61476185 }
61486186
6187
+ boolean Link2Support()
6188
+ {
6189
+ return link2master || link2support;
6190
+ }
6191
+
6192
+ static cVector minima = new cVector();
6193
+ static cVector maxima = new cVector();
6194
+ static javax.vecmath.Point3d center = new javax.vecmath.Point3d();
6195
+
61496196 void Draw(iCameraPane display, Object3D /*Composite*/ root, boolean selected, boolean blocked)
61506197 {
61516198 Invariants(); // june 2013
....@@ -6153,6 +6200,29 @@
61536200 if (support != null)
61546201 {
61556202 // System.err.println("Draw " + this + " Frame # " + ((Mocap)((Merge)support).object).frame);
6203
+ }
6204
+
6205
+ if (false) // live && Link2Support() && support == null && !this.marked) // project on ground
6206
+ {
6207
+ getBounds(minima, maxima, true);
6208
+ center.x = (minima.x + maxima.x) / 2;
6209
+ center.y = 10000; // (minima.y + maxima.y) / 2;
6210
+ center.z = (minima.z + maxima.z) / 2;
6211
+
6212
+ Ray ray = new Ray(center, new Vector3d(0,-1,0));
6213
+
6214
+ IntersectResult res = new IntersectResult();
6215
+ res.t = Double.POSITIVE_INFINITY;
6216
+
6217
+ if (Grafreed.grafreed.universe.intersect(ray, res))
6218
+ {
6219
+ double resx = ray.eyePoint.x + ray.viewDirection.x * res.t;
6220
+ double resy = ray.eyePoint.y + ray.viewDirection.y * res.t;
6221
+ double resz = ray.eyePoint.z + ray.viewDirection.z * res.t;
6222
+
6223
+ LA.matTranslate(toParent, 0, resy - minima.y, 0);
6224
+ LA.matInvert(toParent, fromParent);
6225
+ }
61566226 }
61576227
61586228 if (display.DrawMode() == iCameraPane.SELECTION &&
....@@ -6207,7 +6277,7 @@
62076277 support = support;
62086278
62096279 boolean usecalllists = !IsDynamic() &&
6210
- IsStatic() && GetBRep() != null && (!CameraPane.SUPPORT || support == null || !link2master); // !(this instanceof cSpring) && !(this instanceof BezierPatch);
6280
+ IsStatic() && GetBRep() != null && (!CameraPane.SUPPORT || support == null || !Link2Support()); // !(this instanceof cSpring) && !(this instanceof BezierPatch);
62116281 //boolean usecalllists = false; //!IsLive(); // IsStatic() && GetBRep() != null && (!CameraPane.SUPPORT || support == null) && !link2master; // !(this instanceof cSpring) && !(this instanceof BezierPatch);
62126282
62136283 //usecalllists &= display.DrawMode() == display.DEFAULT; // Don't compute list in shadow pass.
....@@ -6218,7 +6288,8 @@
62186288 bRep.displaylist = 0;
62196289 }
62206290 // usecalllists &= !(parent instanceof RandomNode);
6221
- // usecalllists = false;
6291
+ if (CameraPane.BOXMODE) // Too dynamic
6292
+ usecalllists = false;
62226293
62236294 if (display.DrawMode() == display.SHADOW)
62246295 //GetBRep() != null)
....@@ -6232,7 +6303,7 @@
62326303
62336304 if (!selectmode && //display.DrawMode() != display.SELECTION &&
62346305 //(touched || (bRep != null && bRep.displaylist <= 0)))
6235
- (Globals.isLIVE() && Globals.COMPUTESHADOWWHENLIVE || touched && Globals.COMPUTESHADOWWHENLIVE)) // || (bRep != null && bRep.displaylist <= 0)))
6306
+ ((Globals.isLIVE() && Globals.COMPUTESHADOWWHENLIVE) || touched)) // || (bRep != null && bRep.displaylist <= 0)))
62366307 {
62376308 Globals.lighttouched = true;
62386309 } // all panes...
....@@ -6274,8 +6345,8 @@
62746345 Globals.lighttouched = true; // all panes...
62756346 }
62766347
6277
- touched = GetBRep() == null; // this instanceof Composite || this instanceof FileObject; // false;
6278
- //touched = false;
6348
+ //touched = GetBRep() == null; // this instanceof Composite || this instanceof FileObject; // false;
6349
+ touched = false;
62796350
62806351 if (this instanceof Texture || this instanceof TextureNode)
62816352 {
....@@ -6314,7 +6385,7 @@
63146385 {
63156386 if (display.DrawMode() == iCameraPane.SHADOW)
63166387 {
6317
- if (!link2master // tricky to cull in shadow mode.
6388
+ if (!Link2Support() // tricky to cull in shadow mode.
63186389 && GetBRep().FrustumCull(this, null, display.LightCamera(), true))
63196390 {
63206391 //System.out.print("CULLED");
....@@ -6422,6 +6493,7 @@
64226493 boolean failedPigment = false;
64236494 boolean failedBump = false;
64246495
6496
+ CameraPane.lastObject = this;
64256497 try
64266498 {
64276499 display.BindPigmentTexture(tex, texres);
....@@ -6463,16 +6535,16 @@
64636535
64646536 assert (!(this instanceof Composite));
64656537 {
6466
- for (int i = 0; i < size(); i++)
6467
- {
6468
- Object3D child = (Object3D) reserve(i);
6469
- if (child == null)
6470
- continue;
6471
-
6472
- child.draw(display, root, selected, blocked);
6473
-
6474
- release(i);
6475
- }
6538
+// CRASH MOCAP!! for (int i = 0; i < size(); i++)
6539
+// {
6540
+// Object3D child = (Object3D) reserve(i);
6541
+// if (child == null)
6542
+// continue;
6543
+//
6544
+// child.draw(display, root, selected, blocked);
6545
+//
6546
+// release(i);
6547
+// }
64766548 }
64776549 }
64786550
....@@ -6786,7 +6858,8 @@
67866858
67876859 //javax.media.opengl.GL gl = display.GetGL();
67886860
6789
- if (CameraPane.BOXMODE && !selected) // || CameraPane.movingcamera)
6861
+ if (CameraPane.BOXMODE && //!Link2Support()) //
6862
+ !selected) // || CameraPane.movingcamera)
67906863 {
67916864 int fc = bRep.FaceCount();
67926865 int vc = bRep.VertexCount();
....@@ -6948,7 +7021,7 @@
69487021 facescompare[k] = new FaceCompare(k);
69497022 }
69507023
6951
- center = new cVector();
7024
+ centertriangle = new cVector();
69527025 }
69537026 else
69547027 {
....@@ -7071,7 +7144,7 @@
70717144 */
70727145 }
70737146
7074
- transient cVector center;
7147
+ transient cVector centertriangle;
70757148
70767149 class FaceCompare implements Comparable
70777150 {
....@@ -7100,14 +7173,14 @@
71007173 Vertex q = bRep.GetVertex(face.q);
71017174 Vertex r = bRep.GetVertex(face.r);
71027175
7103
- center.set(p);
7104
- center.add(q);
7105
- center.add(r);
7106
- center.mul(1.0/3);
7176
+ centertriangle.set(p);
7177
+ centertriangle.add(q);
7178
+ centertriangle.add(r);
7179
+ centertriangle.mul(1.0/3);
71077180
7108
- center.sub(Globals.theRenderer.EyeCamera().location);
7181
+ centertriangle.sub(Globals.theRenderer.EyeCamera().location);
71097182
7110
- distance = center.dot(center);
7183
+ distance = centertriangle.dot(centertriangle);
71117184 }
71127185
71137186 return distance;
....@@ -8282,15 +8355,15 @@
82828355 } // + super.toString();
82838356 //return name + " (" + (SizeOf.deepSizeOf(this)/1024) + "K) " + this.getClass().getName();
82848357
8285
-// if (!Globals.ADVANCED)
8286
-// return objname;
8358
+ if (!Globals.ADVANCED)
8359
+ return objname;
82878360
8288
- return objname; // + " " + System.identityHashCode(this); // + GetUUID()
8361
+ return objname + " " + System.identityHashCode(this) + " " + GetUUID();
82898362 }
82908363
82918364 public int hashCode()
82928365 {
8293
- // Fuck Vector...
8366
+ // Do not use Vector...
82948367 return System.identityHashCode(this);
82958368 }
82968369
....@@ -8530,8 +8603,8 @@
85308603 private static cVector2 qq2 = new cVector2();
85318604 private static cVector2 rr2 = new cVector2();
85328605 private static cVector2 ss2 = new cVector2();
8533
- private static cVector edge1 = new cVector();
8534
- private static cVector edge2 = new cVector();
8606
+// private static cVector edge1 = new cVector();
8607
+// private static cVector edge2 = new cVector();
85358608 //private static cVector norm = new cVector();
85368609 /*transient private*/ int hitSomething;
85378610 static final int hitCenter = 1;
....@@ -8729,7 +8802,275 @@
87298802
87308803 Touch();
87318804 }
8805
+
8806
+ static Vertex s1 = new Vertex();
8807
+ static Vertex s2 = new Vertex();
8808
+ static Vertex s3 = new Vertex();
87328809
8810
+ boolean intersectMesh(Ray ray, IntersectResult result)
8811
+ {
8812
+ boolean success = false;
8813
+
8814
+ if (bRep.stripified)
8815
+ {
8816
+ int[] strips = bRep.getRawIndices();
8817
+
8818
+ // TRIANGLE STRIP ARRAY
8819
+ if (bRep.trimmed)
8820
+ {
8821
+ float[] v = bRep.getRawVertices();
8822
+
8823
+ int count3 = 0;
8824
+
8825
+ if (v.length > 0)
8826
+ {
8827
+ for (int i = 0; i < strips.length; i++)
8828
+ {
8829
+ s1.set(v[count3], v[count3 + 1], v[count3 + 2]);
8830
+ count3 += 3;
8831
+
8832
+ s2.set(v[count3], v[count3 + 1], v[count3 + 2]);
8833
+ count3 += 3;
8834
+
8835
+ for (int j = 0; j < strips[i] - 2; j++)
8836
+ {
8837
+ s3.set(v[count3], v[count3 + 1], v[count3 + 2]);
8838
+ count3 += 3;
8839
+
8840
+ if (j%2 == 0)
8841
+ success |= intersectTriangle(ray, result, s1, s2, s3);
8842
+ else
8843
+ success |= intersectTriangle(ray, result, s1, s3, s2);
8844
+
8845
+ s1.set(s2);
8846
+ s2.set(s3);
8847
+ }
8848
+ }
8849
+ }
8850
+
8851
+ assert count3 == v.length;
8852
+ }
8853
+ else // !trimmed
8854
+ {
8855
+ int count = 0;
8856
+ for (int i = 0; i < strips.length; i++)
8857
+ {
8858
+ Vertex p = bRep.GetVertex(bRep.indices[count++]);
8859
+ Vertex q = bRep.GetVertex(bRep.indices[count++]);
8860
+
8861
+ for (int j = 0; j < strips[i] - 2; j++)
8862
+ {
8863
+ Vertex r = bRep.GetVertex(bRep.indices[count++]);
8864
+
8865
+ if (j%2 == 0)
8866
+ success |= intersectTriangle(ray, result, p, q, r);
8867
+ else
8868
+ success |= intersectTriangle(ray, result, p, r, q);
8869
+
8870
+ p = q;
8871
+ q = r;
8872
+ }
8873
+ }
8874
+ }
8875
+ } else // catch (Error e)
8876
+ {
8877
+ int facecount = bRep.FaceCount();
8878
+ for (int i = 0; i < facecount; i++)
8879
+ {
8880
+ Face face = bRep.GetFace(i);
8881
+
8882
+ Vertex p = bRep.GetVertex(face.p);
8883
+ Vertex q = bRep.GetVertex(face.q);
8884
+ Vertex r = bRep.GetVertex(face.r);
8885
+
8886
+ success |= intersectTriangle(ray, result, p, q, r);
8887
+ }
8888
+ }
8889
+
8890
+ return success;
8891
+ }
8892
+
8893
+ static cVector eye = new cVector();
8894
+ static cVector dir = new cVector();
8895
+
8896
+ transient BBox bbox = null;
8897
+
8898
+ boolean intersect(Ray ray, IntersectResult result)
8899
+ {
8900
+ double eyex = ray.eyePoint.x;
8901
+ double eyey = ray.eyePoint.y;
8902
+ double eyez = ray.eyePoint.z;
8903
+
8904
+ double dirx = ray.viewDirection.x;
8905
+ double diry = ray.viewDirection.y;
8906
+ double dirz = ray.viewDirection.z;
8907
+
8908
+ if (this.fromParent != null && !(this instanceof TextureNode))
8909
+ {
8910
+ eye.x = eyex;
8911
+ eye.y = eyey;
8912
+ eye.z = eyez;
8913
+ dir.x = dirx;
8914
+ dir.y = diry;
8915
+ dir.z = dirz;
8916
+
8917
+ LA.xformPos(eye, this.fromParent, eye);
8918
+ LA.xformDir(dir, this.fromParent, dir);
8919
+
8920
+ ray.eyePoint.x = eye.x;
8921
+ ray.eyePoint.y = eye.y;
8922
+ ray.eyePoint.z = eye.z;
8923
+
8924
+ ray.viewDirection.x = dir.x;
8925
+ ray.viewDirection.y = dir.y;
8926
+ ray.viewDirection.z = dir.z;
8927
+ }
8928
+
8929
+ boolean success = false;
8930
+
8931
+ boolean touch = false;
8932
+
8933
+ if (bRep != null && Link2Support())
8934
+ {
8935
+ if (bbox == null)
8936
+ {
8937
+ bbox = new BBox();
8938
+
8939
+ cVector min = new cVector();
8940
+ cVector max = new cVector();
8941
+
8942
+ this.getBounds(min, max, true);
8943
+
8944
+ bbox.min.x = min.x;
8945
+ bbox.min.y = min.y;
8946
+ bbox.min.z = min.z;
8947
+
8948
+ bbox.max.x = max.x;
8949
+ bbox.max.y = max.y;
8950
+ bbox.max.z = max.z;
8951
+ }
8952
+
8953
+ if (true) // NOT WORKING bbox.intersect(ray, result))
8954
+ {
8955
+ success |= intersectMesh(ray, result);
8956
+ }
8957
+ else
8958
+ {
8959
+ //this.hide = true;
8960
+ touch = true;
8961
+ }
8962
+ }
8963
+
8964
+ for (int i=0; i<size(); i++)
8965
+ {
8966
+ Object3D child = (Object3D) reserve(i);
8967
+
8968
+ if (child == null)
8969
+ continue;
8970
+
8971
+ success |= child.intersect(ray, result);
8972
+ release(i);
8973
+ }
8974
+
8975
+ ray.eyePoint.x = eyex;
8976
+ ray.eyePoint.y = eyey;
8977
+ ray.eyePoint.z = eyez;
8978
+
8979
+ ray.viewDirection.x = dirx;
8980
+ ray.viewDirection.y = diry;
8981
+ ray.viewDirection.z = dirz;
8982
+
8983
+// if (touch)
8984
+// this.Touch(); // refresh display list
8985
+
8986
+ return success;
8987
+ }
8988
+
8989
+ static Vector3d edge1 = new Vector3d();
8990
+ static Vector3d edge2 = new Vector3d();
8991
+ static Vector3d P = new Vector3d();
8992
+ static Vector3d T = new Vector3d();
8993
+ static Vector3d Q = new Vector3d();
8994
+
8995
+ private boolean intersectTriangle(Ray ray, IntersectResult result, Vertex v1, Vertex v2, Vertex v3)
8996
+ {
8997
+ if (false)
8998
+ {
8999
+ result.t = 0;
9000
+ return true;
9001
+ }
9002
+
9003
+ /*
9004
+ Fast, Minimum Storage Ray/Triangle Intersection, Moller et al.
9005
+
9006
+ Reference: http://www.cs.virginia.edu/~gfx/Courses/2003/ImageSynthesis/papers/Acceleration/Fast%20MinimumStorage%20RayTriangle%20Intersection.pdf
9007
+ */
9008
+
9009
+ // Calculate edges of the triangle
9010
+ edge1.set(v2.x - v1.x, v2.y - v1.y, v2.z - v1.z);
9011
+ edge2.set(v3.x - v1.x, v3.y - v1.y, v3.z - v1.z);
9012
+
9013
+ // Calculate the determinant (U parameter)
9014
+ P.cross(ray.viewDirection, edge2);
9015
+ double det = edge1.dot(P);
9016
+
9017
+ if (det > -1e-9 && det < 1e-9)
9018
+ {
9019
+ return false;
9020
+ } // Ray lies in plane of triangle
9021
+
9022
+ double invDet = 1 / det;
9023
+
9024
+ // Calculate distance from vertex1 to ray origin
9025
+ T.set(ray.eyePoint.x - v1.x, ray.eyePoint.y - v1.y, ray.eyePoint.z - v1.z);
9026
+
9027
+ double U = (T.dot(P)) * invDet; // Calculate U parameter
9028
+
9029
+ if (U < 0.f || U > 1.f)
9030
+ {
9031
+ return false;
9032
+ } // Intersection lies outside of the triangle
9033
+
9034
+ // Calculate V parameter
9035
+ Q.cross(T, edge1);
9036
+
9037
+ double V = ray.viewDirection.dot(Q) * invDet;
9038
+
9039
+ if (V < 0.f || (U + V) > 1.f)
9040
+ {
9041
+ return false;
9042
+ } // Intersection lies outside of the triangle
9043
+
9044
+ double t = edge2.dot(Q) * invDet;
9045
+
9046
+ if (t > 1e-9) // Triangle and ray intersects
9047
+ {
9048
+ //result.isIntersected = true;
9049
+ //result.id = id;
9050
+
9051
+ if (false) // isShadow)
9052
+ {
9053
+ result.t = t;
9054
+ return true;
9055
+ } else if (t < result.t)
9056
+ {
9057
+ result.object = this;
9058
+
9059
+ result.t = t;
9060
+
9061
+ //result.p.x = ray.eyePoint.x + ray.viewDirection.x * t;
9062
+ //result.p.y = ray.eyePoint.y + ray.viewDirection.y * t;
9063
+ //result.p.z = ray.eyePoint.z + ray.viewDirection.z * t;
9064
+
9065
+ result.n.cross(edge1, edge2);
9066
+ result.n.normalize();
9067
+ }
9068
+
9069
+ return true;
9070
+ }
9071
+
9072
+ return false;
9073
+ }
87339074
87349075 public int Size()
87359076 {