Normand Briere
2019-09-24 767be784dc7fe293bf5c5ee6507df242526be3ed
Object3D.java
....@@ -25,30 +25,106 @@
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;
3437 String skyboxext;
3538
36
- Object3D versionlist[];
39
+ Object3D[] versionlist;
3740 int versionindex = -1;
3841
3942 java.util.Hashtable<java.util.UUID, Object3D> versiontable; // = new java.util.Hashtable<java.util.UUID, Object3D>();
4043
4144 ScriptNode scriptnode;
4245
46
+ void deepCopyNode(Object3D other)
47
+ {
48
+ other.skyboxname = skyboxname;
49
+ other.skyboxext = skyboxext;
50
+
51
+ if (toParent != null)
52
+ {
53
+ other.toParent = LA.newMatrix();
54
+ other.fromParent = LA.newMatrix();
55
+ LA.matCopy(toParent, other.toParent);
56
+ LA.matCopy(fromParent, other.fromParent);
57
+ if (toParentMarked != null)
58
+ {
59
+ other.toParentMarked = LA.newMatrix();
60
+ other.fromParentMarked = LA.newMatrix();
61
+ LA.matCopy(toParentMarked, other.toParentMarked);
62
+ LA.matCopy(fromParentMarked, other.fromParentMarked);
63
+ }
64
+ }
65
+ else
66
+ {
67
+ if (other.toParent == null)
68
+// assert(other.toParent == null);
69
+// new Exception().printStackTrace();
70
+ System.err.println("null parent: " + other);
71
+ }
72
+
73
+ /*
74
+ double ident[][] = LA.newMatrix();
75
+ if (bRep == null)
76
+ other.bRep = null;
77
+ else
78
+ other.bRep = new BoundaryRep(bRep, ident);
79
+ */
80
+ // Really new...
81
+ //other.editWindow = null;
82
+ if (name == null)
83
+ other.name = null;
84
+ else
85
+ other.name = new String(name);
86
+
87
+ if (material != null)
88
+ {
89
+ other.material = new cMaterial(material);
90
+ } else
91
+ {
92
+ other.material = null;
93
+ }
94
+
95
+ other.GetTextures().name = GetTextures().name;
96
+
97
+ CopyExtraMaterial(other);
98
+
99
+ other.touched = touched;
100
+ other.softtouched = softtouched;
101
+
102
+ other.random = random;
103
+ other.link2master = link2master;
104
+ other.transformcount = transformcount;
105
+ other.marked = marked;
106
+ other.skip = skip;
107
+ other.count = count;
108
+ other.flipV = flipV;
109
+ other.live = live;
110
+ other.rewind = rewind;
111
+ other.hide = hide;
112
+ other.texres = texres;
113
+ other.speedup = speedup;
114
+ other.height = height;
115
+ other.depth = depth;
116
+ }
117
+
43118 int VersionCount()
44119 {
45120 int count = 0;
46121
47
- for (int i = versionlist.length; --i >= 0;)
48
- {
49
- if (versionlist[i] != null)
50
- count++;
51
- }
122
+ if (versionlist != null)
123
+ for (int i = versionlist.length; --i >= 0;)
124
+ {
125
+ if (versionlist[i] != null)
126
+ count++;
127
+ }
52128
53129 return count;
54130 }
....@@ -64,6 +140,7 @@
64140 projectedVertices[i] = new cVector2(); // Others
65141 }
66142 projectedVertices[0].x = 100; // bump
143
+ projectedVertices[1].y = 1000; // punchthrough. only for png
67144 }
68145
69146 void MinMax(cVector minima, cVector maxima)
....@@ -190,8 +267,39 @@
190267 }
191268 }
192269
270
+ boolean HasBigData()
271
+ {
272
+ if (blockloop)
273
+ return false;
274
+
275
+ if (bRep != null)
276
+ {
277
+ return true;
278
+ }
279
+
280
+ blockloop = true;
281
+
282
+ for (int i = 0; i < Size(); i++)
283
+ {
284
+ Object3D child = (Object3D) get(i);
285
+ if (child == null)
286
+ continue;
287
+ if (child.HasBigData())
288
+ {
289
+ blockloop = false;
290
+ return true;
291
+ }
292
+ }
293
+
294
+ blockloop = false;
295
+ return false;
296
+ }
297
+
193298 void ExtractBigData(java.util.Hashtable<java.util.UUID, Object3D> hashtable)
194299 {
300
+ if (blockloop)
301
+ return;
302
+
195303 Object3D o;
196304
197305 boolean isnew = false;
....@@ -215,7 +323,7 @@
215323 hashtable.put(GetUUID(), o);
216324 }
217325
218
- if (!blockloop)
326
+ //if (!blockloop)
219327 {
220328 blockloop = true;
221329
....@@ -227,7 +335,7 @@
227335 blockloop = false;
228336 }
229337
230
- if (isnew)
338
+ //if (isnew)
231339 ExtractBigData(o);
232340 }
233341
....@@ -338,15 +446,15 @@
338446
339447 void RestoreBigData(java.util.Hashtable<java.util.UUID, Object3D> hashtable)
340448 {
449
+ if (blockloop)
450
+ return;
451
+
341452 if (!hashtable.containsKey(GetUUID()))
342453 return;
343454
344455 Object3D o = hashtable.get(GetUUID());
345456
346457 RestoreBigData(o);
347
-
348
- if (blockloop)
349
- return;
350458
351459 blockloop = true;
352460
....@@ -363,6 +471,7 @@
363471 void RestoreBigData(Object3D o)
364472 {
365473 //System.err.println("RestoreBigData : " + this + " <-- " + o);
474
+ Grafreed.Assert(this.bRep == null);
366475
367476 this.bRep = o.bRep;
368477 if (this.support != null && o.transientrep != null)
....@@ -524,7 +633,7 @@
524633 transient boolean keepdontselect;
525634 boolean dontselect = false;
526635 boolean hide = false;
527
- boolean link2master = false; // performs reset support/master at each frame
636
+ boolean link2master = false; // performs reset support/master at each frame (cannot rename due to serialization)
528637 boolean marked = false; // animation node
529638 boolean skip = false; // centroid issue
530639 boolean skipmocap = false; // mocap data
....@@ -532,6 +641,9 @@
532641 boolean random = false;
533642 boolean speedup = false;
534643 boolean rewind = false;
644
+
645
+ // Option to sort triangles, e.g. for transparency.
646
+ boolean sort = false;
535647
536648 float NORMALPUSH = 0;
537649
....@@ -550,7 +662,7 @@
550662 {
551663 sorted = true;
552664
553
- for (int i=0; i<size()-1; i++)
665
+ for (int i=0; i<Size()-1; i++)
554666 {
555667 Object3D obji = get(i);
556668 Object3D objj = get(i+1);
....@@ -574,7 +686,7 @@
574686 {
575687 sorted = true;
576688
577
- for (int i=0; i<size()-1; i++)
689
+ for (int i=0; i<Size()-1; i++)
578690 {
579691 Object3D obji = get(i);
580692 Object3D objj = get(i+1);
....@@ -911,7 +1023,7 @@
9111023
9121024 void Step()
9131025 {
914
- // marde pour serialization de Texture
1026
+ // patch pour serialization de Texture
9151027 resetmaxcount();
9161028 resettransformcount();
9171029 resetstep();
....@@ -1104,24 +1216,28 @@
11041216 {
11051217 // return true;
11061218
1107
- if (material == null || material.multiply)
1108
- return true;
1219
+// if (material == null || material.multiply)
1220
+// return true;
1221
+//
1222
+// if (projectedVertices != null && projectedVertices.length > 2 && projectedVertices[2].y >= 10000)
1223
+// return false;
1224
+//
1225
+// // Transparent objects are dynamic because we have to sort the triangles.
1226
+// return material.opacity > 0.99;
11091227
1110
- if (projectedVertices != null && projectedVertices.length > 2 && projectedVertices[2].y >= 10000)
1111
- return false;
1112
-
1113
- // Transparent objects are dynamic because we have to sort the triangles.
1114
- return material.opacity > 0.99;
1228
+ return !sort;
11151229 }
11161230
11171231 boolean IsOpaque()
11181232 {
11191233 // return true;
11201234
1121
- if (material == null || material.multiply)
1122
- return true;
1235
+// if (material == null || material.multiply)
1236
+// return true;
1237
+//
1238
+// return material.opacity > 0.99;
11231239
1124
- return material.opacity > 0.99;
1240
+ return !sort;
11251241 }
11261242
11271243 Object3D()
....@@ -1199,8 +1315,10 @@
11991315
12001316 // will share the geometry
12011317 assert (!(this instanceof Composite));
1202
- return deepCopy(); // Never called for Composite
1203
-
1318
+
1319
+ Object3D obj = deepCopy(); // Never called for Composite
1320
+ obj.count = 2;
1321
+ return obj;
12041322 }
12051323
12061324 boolean HasLoops()
....@@ -1208,6 +1326,30 @@
12081326 return false;
12091327 }
12101328
1329
+ void ResetUUIDs()
1330
+ {
1331
+ if (blockloop)
1332
+ {
1333
+ return;
1334
+ }
1335
+
1336
+ this.uuid = null;
1337
+
1338
+ blockloop = true;
1339
+
1340
+ for (int i = 0; i < Size(); i++)
1341
+ {
1342
+ Object3D obj = (Object3D) Children().get(i);
1343
+
1344
+ if (obj != null)
1345
+ {
1346
+ obj.ResetUUIDs();
1347
+ }
1348
+ }
1349
+
1350
+ blockloop = false;
1351
+ }
1352
+
12111353 boolean IsInfinite()
12121354 {
12131355 if (blockloop)
....@@ -1278,78 +1420,16 @@
12781420 return;
12791421
12801422 blockloop = true;
1423
+
1424
+ other.parent = parent;
1425
+
12811426 //System.out.println("COPY " + this + " to " + other);
12821427 //new Exception().printStackTrace();
1428
+ deepCopyNode(other);
12831429
1284
- other.parent = parent;
1285
- if (toParent != null)
1286
- {
1287
- other.toParent = LA.newMatrix();
1288
- other.fromParent = LA.newMatrix();
1289
- LA.matCopy(toParent, other.toParent);
1290
- LA.matCopy(fromParent, other.fromParent);
1291
- if (toParentMarked != null)
1292
- {
1293
- other.toParentMarked = LA.newMatrix();
1294
- other.fromParentMarked = LA.newMatrix();
1295
- LA.matCopy(toParentMarked, other.toParentMarked);
1296
- LA.matCopy(fromParentMarked, other.fromParentMarked);
1297
- }
1298
- }
1299
- else
1300
- {
1301
- if (other.toParent == null)
1302
-// assert(other.toParent == null);
1303
-// new Exception().printStackTrace();
1304
- System.err.println("null parent: " + other);
1305
- }
1306
- /*
1307
- double ident[][] = LA.newMatrix();
1308
- if (bRep == null)
1309
- other.bRep = null;
1310
- else
1311
- other.bRep = new BoundaryRep(bRep, ident);
1312
- */
1313
- // Really new...
13141430 other.bRep = bRep; // Share the geometry
13151431
13161432 other.support = support; // Share the support
1317
-
1318
- //other.editWindow = null;
1319
- if (name == null)
1320
- other.name = null;
1321
- else
1322
- other.name = new String(name);
1323
-
1324
- if (material != null)
1325
- {
1326
- other.material = new cMaterial(material);
1327
- } else
1328
- {
1329
- other.material = null;
1330
- }
1331
-
1332
- other.GetTextures().name = GetTextures().name;
1333
-
1334
- CopyExtraMaterial(other);
1335
-
1336
- other.touched = touched;
1337
- other.softtouched = softtouched;
1338
-
1339
- other.random = random;
1340
- other.link2master = link2master;
1341
- other.transformcount = transformcount;
1342
- other.marked = marked;
1343
- other.skip = skip;
1344
- other.count = count;
1345
- other.flipV = flipV;
1346
- other.live = live;
1347
- other.rewind = rewind;
1348
- other.hide = hide;
1349
- other.texres = texres;
1350
- other.speedup = speedup;
1351
- other.height = height;
1352
- other.depth = depth;
13531433
13541434 // aout 2013 if (/*displaylist != -1 &&*/other.displaylist != displaylist)
13551435 // {
....@@ -1405,17 +1485,24 @@
14051485 if ((mask & GEOMETRY) != 0)
14061486 {
14071487 if (bRep != null)
1408
- bRep.overwriteThis(other.bRep==null?other.transientrep:other.bRep);
1409
- else
1410
- assert(other.bRep == null);
1411
-
1412
- if (bRep != null)
14131488 {
1489
+ bRep.overwriteThis(other.bRep==null?other.transientrep:other.bRep);
14141490 CameraPane.RemoveList(bRep.displaylist);
14151491 bRep.displaylist = 0; // june 2013 -1;
14161492 }
14171493 else
1418
- bRep = bRep;
1494
+ {
1495
+ //assert(other.bRep == null);
1496
+ bRep = other.bRep;
1497
+ }
1498
+
1499
+// if (bRep != null)
1500
+// {
1501
+// CameraPane.RemoveList(bRep.displaylist);
1502
+// bRep.displaylist = 0; // june 2013 -1;
1503
+// }
1504
+// else
1505
+// bRep = bRep;
14191506 }
14201507
14211508 /* Use a MASK = GEO/MAT
....@@ -3211,7 +3298,10 @@
32113298 {
32123299 if (bRep != null)
32133300 {
3301
+ //bRep.GenerateNormals2(crease); // in-place doesn't work. it gives wrong normals (diamond artifact).
32143302 bRep.GenerateNormals(crease);
3303
+ if (!bRep.trimmed)
3304
+ bRep.MergeNormals();
32153305 Touch();
32163306 }
32173307 }
....@@ -3322,6 +3412,18 @@
33223412 obj.fromParent[3][0] = obj.fromParent[3][1] = obj.fromParent[3][2] = 0;
33233413 }
33243414 }
3415
+ }
3416
+
3417
+ public void Scale(int scale)
3418
+ {
3419
+ Object3D obj = this;
3420
+
3421
+ obj.toParent[0][0] = obj.toParent[1][1] = obj.toParent[2][2] = scale;
3422
+ obj.toParent[0][1] = obj.toParent[1][0] = obj.toParent[2][0] = 0;
3423
+ obj.toParent[0][2] = obj.toParent[1][2] = obj.toParent[2][1] = 0;
3424
+ obj.fromParent[0][0] = obj.fromParent[1][1] = obj.fromParent[2][2] = 1/scale;
3425
+ obj.fromParent[0][1] = obj.fromParent[1][0] = obj.fromParent[2][0] = 0;
3426
+ obj.fromParent[0][2] = obj.fromParent[1][2] = obj.fromParent[2][1] = 0;
33253427 }
33263428
33273429 public void TextureRatioTransform(int axis)
....@@ -3615,6 +3717,7 @@
36153717 if (bRep != null)
36163718 {
36173719 //bRep.RemoveOneTriangle();
3720
+ System.out.println();
36183721 System.out.println("Reducing " + this);
36193722 if (name != null && name.contains("lockpickstraps"))
36203723 name = name;
....@@ -3945,6 +4048,7 @@
39454048
39464049 void RevertMeshes()
39474050 {
4051
+ // BLOCKLOOP
39484052 if (this instanceof cMesh)
39494053 {
39504054 ((cMesh)this).Revert();
....@@ -3975,11 +4079,6 @@
39754079 Touch();
39764080 }
39774081
3978
- ResetRecur();
3979
- }
3980
-
3981
- void ResetRecur()
3982
- {
39834082 for (int i = 0; i < size(); i++)
39844083 {
39854084 Object3D child = (Object3D) get(i); // reserve(i);
....@@ -4002,11 +4101,6 @@
40024101 Step();
40034102 Touch();
40044103
4005
- StepRecur();
4006
- }
4007
-
4008
- void StepRecur()
4009
- {
40104104 for (int i = 0; i < size(); i++)
40114105 {
40124106 Object3D child = (Object3D) get(i); // reserve(i);
....@@ -4193,8 +4287,8 @@
41934287 Touch();
41944288 }
41954289
4196
- transient cVector min = new cVector();
4197
- transient cVector max = new cVector();
4290
+ transient cVector min;
4291
+ transient cVector max;
41984292
41994293 void getBounds(cVector minima, cVector maxima, boolean xform)
42004294 {
....@@ -4210,15 +4304,15 @@
42104304 if (blockloop)
42114305 return;
42124306
4213
- if (min == null) // ???
4307
+ if (min == null)
42144308 {
42154309 min = new cVector();
42164310 max = new cVector();
42174311 }
42184312
4219
- for (int i = 0; i<size(); i++)
4313
+ for (int i = 0; i<Size(); i++)
42204314 {
4221
- Object3D child = (Object3D) reserve(i);
4315
+ Object3D child = (Object3D) get(i); //reserve(i);
42224316 if (child == null)
42234317 continue;
42244318
....@@ -4237,21 +4331,21 @@
42374331 if (child.hide && !(child instanceof Merge) || child.skip)
42384332 //if (child.hide)
42394333 {
4240
- release(i);
4334
+ //release(i);
42414335 continue;
42424336 }
42434337
42444338 blockloop = true;
42454339 child.getBounds(min, max, true); // xform);
42464340 blockloop = false;
4247
- release(i);
4341
+ //release(i);
42484342
42494343 MinMax(minima, maxima);
42504344 }
42514345
42524346 if (bRep != null)
42534347 {
4254
- bRep.getBounds(minima,maxima,this);
4348
+ bRep.getBounds(minima, maxima, xform?this:null);
42554349 }
42564350
42574351 if (false) // xform)
....@@ -6083,6 +6177,10 @@
60836177 return live && bRep != null;
60846178 }
60856179
6180
+ static cVector minima = new cVector();
6181
+ static cVector maxima = new cVector();
6182
+ static javax.vecmath.Point3d center = new javax.vecmath.Point3d();
6183
+
60866184 void Draw(iCameraPane display, Object3D /*Composite*/ root, boolean selected, boolean blocked)
60876185 {
60886186 Invariants(); // june 2013
....@@ -6090,6 +6188,29 @@
60906188 if (support != null)
60916189 {
60926190 // System.err.println("Draw " + this + " Frame # " + ((Mocap)((Merge)support).object).frame);
6191
+ }
6192
+
6193
+ if (live && link2master && support == null && !this.marked) // project on ground
6194
+ {
6195
+ getBounds(minima, maxima, true);
6196
+ center.x = (minima.x + maxima.x) / 2;
6197
+ center.y = 10000; // (minima.y + maxima.y) / 2;
6198
+ center.z = (minima.z + maxima.z) / 2;
6199
+
6200
+ Ray ray = new Ray(center, new Vector3d(0,-1,0));
6201
+
6202
+ IntersectResult res = new IntersectResult();
6203
+ res.t = Double.POSITIVE_INFINITY;
6204
+
6205
+ if (Grafreed.grafreed.universe.intersect(ray, res))
6206
+ {
6207
+ double resx = ray.eyePoint.x + ray.viewDirection.x * res.t;
6208
+ double resy = ray.eyePoint.y + ray.viewDirection.y * res.t;
6209
+ double resz = ray.eyePoint.z + ray.viewDirection.z * res.t;
6210
+
6211
+ LA.matTranslate(toParent, 0, resy - minima.y, 0);
6212
+ LA.matInvert(toParent, fromParent);
6213
+ }
60936214 }
60946215
60956216 if (display.DrawMode() == iCameraPane.SELECTION &&
....@@ -6211,8 +6332,8 @@
62116332 Globals.lighttouched = true; // all panes...
62126333 }
62136334
6214
- touched = GetBRep() == null; // this instanceof Composite || this instanceof FileObject; // false;
6215
- //touched = false;
6335
+ //touched = GetBRep() == null; // this instanceof Composite || this instanceof FileObject; // false;
6336
+ touched = false;
62166337
62176338 if (this instanceof Texture || this instanceof TextureNode)
62186339 {
....@@ -6313,6 +6434,20 @@
63136434 {
63146435 drawSelf(display, root, selected, blocked);
63156436 }
6437
+
6438
+// if (!(this instanceof Composite))
6439
+// {
6440
+// for (int i = 0; i < size(); i++)
6441
+// {
6442
+// Object3D child = (Object3D) reserve(i);
6443
+// if (child == null)
6444
+// continue;
6445
+//
6446
+// child.draw(display, root, selected, blocked);
6447
+//
6448
+// release(i);
6449
+// }
6450
+// }
63166451 } else
63176452 {
63186453 /*
....@@ -6345,13 +6480,14 @@
63456480 boolean failedPigment = false;
63466481 boolean failedBump = false;
63476482
6483
+ CameraPane.lastObject = this;
63486484 try
63496485 {
63506486 display.BindPigmentTexture(tex, texres);
63516487 }
63526488 catch (Exception e)
63536489 {
6354
- System.err.println("FAILED: " + this);
6490
+ // System.err.println("FAILED: " + this);
63556491 failedPigment = true;
63566492 }
63576493
....@@ -6382,6 +6518,20 @@
63826518 display.CallList(bRep.displaylist);
63836519 // june 2013 drawSelf(display, root, selected);
63846520 }
6521
+ }
6522
+
6523
+ assert (!(this instanceof Composite));
6524
+ {
6525
+// CRASH MOCAP!! for (int i = 0; i < size(); i++)
6526
+// {
6527
+// Object3D child = (Object3D) reserve(i);
6528
+// if (child == null)
6529
+// continue;
6530
+//
6531
+// child.draw(display, root, selected, blocked);
6532
+//
6533
+// release(i);
6534
+// }
63856535 }
63866536 }
63876537
....@@ -6435,16 +6585,17 @@
64356585
64366586 void CallList(iCameraPane display, Object3D /*Composite*/ root, boolean selected, boolean blocked)
64376587 {
6438
- if (GetBRep() == null)
6439
- {
6440
- drawSelf(display, root, selected, blocked);
6441
- } else
6588
+ if (GetBRep() != null)
64426589 {
64436590 DrawNode(display, root, selected);
64446591 if (this instanceof BezierPatch)
64456592 {
64466593 //drawSelf(display, root, selected);
64476594 }
6595
+ }
6596
+ else
6597
+ {
6598
+ drawSelf(display, root, selected, blocked);
64486599 }
64496600 }
64506601
....@@ -6856,7 +7007,7 @@
68567007 facescompare[k] = new FaceCompare(k);
68577008 }
68587009
6859
- center = new cVector();
7010
+ centertriangle = new cVector();
68607011 }
68617012 else
68627013 {
....@@ -6979,7 +7130,7 @@
69797130 */
69807131 }
69817132
6982
- transient cVector center;
7133
+ transient cVector centertriangle;
69837134
69847135 class FaceCompare implements Comparable
69857136 {
....@@ -7008,14 +7159,14 @@
70087159 Vertex q = bRep.GetVertex(face.q);
70097160 Vertex r = bRep.GetVertex(face.r);
70107161
7011
- center.set(p);
7012
- center.add(q);
7013
- center.add(r);
7014
- center.mul(1.0/3);
7162
+ centertriangle.set(p);
7163
+ centertriangle.add(q);
7164
+ centertriangle.add(r);
7165
+ centertriangle.mul(1.0/3);
70157166
7016
- center.sub(Globals.theRenderer.EyeCamera().location);
7167
+ centertriangle.sub(Globals.theRenderer.EyeCamera().location);
70177168
7018
- distance = center.dot(center);
7169
+ distance = centertriangle.dot(centertriangle);
70197170 }
70207171
70217172 return distance;
....@@ -8190,10 +8341,10 @@
81908341 } // + super.toString();
81918342 //return name + " (" + (SizeOf.deepSizeOf(this)/1024) + "K) " + this.getClass().getName();
81928343
8193
-// if (!Globals.ADVANCED)
8194
-// return objname;
8344
+ if (!Globals.ADVANCED)
8345
+ return objname;
81958346
8196
- return objname + " " + System.identityHashCode(this); // + GetUUID()
8347
+ return objname + " " + System.identityHashCode(this) + " " + GetUUID();
81978348 }
81988349
81998350 public int hashCode()
....@@ -8438,8 +8589,8 @@
84388589 private static cVector2 qq2 = new cVector2();
84398590 private static cVector2 rr2 = new cVector2();
84408591 private static cVector2 ss2 = new cVector2();
8441
- private static cVector edge1 = new cVector();
8442
- private static cVector edge2 = new cVector();
8592
+// private static cVector edge1 = new cVector();
8593
+// private static cVector edge2 = new cVector();
84438594 //private static cVector norm = new cVector();
84448595 /*transient private*/ int hitSomething;
84458596 static final int hitCenter = 1;
....@@ -8637,7 +8788,275 @@
86378788
86388789 Touch();
86398790 }
8791
+
8792
+ static Vertex s1 = new Vertex();
8793
+ static Vertex s2 = new Vertex();
8794
+ static Vertex s3 = new Vertex();
86408795
8796
+ boolean intersectMesh(Ray ray, IntersectResult result)
8797
+ {
8798
+ boolean success = false;
8799
+
8800
+ if (bRep.stripified)
8801
+ {
8802
+ int[] strips = bRep.getRawIndices();
8803
+
8804
+ // TRIANGLE STRIP ARRAY
8805
+ if (bRep.trimmed)
8806
+ {
8807
+ float[] v = bRep.getRawVertices();
8808
+
8809
+ int count3 = 0;
8810
+
8811
+ if (v.length > 0)
8812
+ {
8813
+ for (int i = 0; i < strips.length; i++)
8814
+ {
8815
+ s1.set(v[count3], v[count3 + 1], v[count3 + 2]);
8816
+ count3 += 3;
8817
+
8818
+ s2.set(v[count3], v[count3 + 1], v[count3 + 2]);
8819
+ count3 += 3;
8820
+
8821
+ for (int j = 0; j < strips[i] - 2; j++)
8822
+ {
8823
+ s3.set(v[count3], v[count3 + 1], v[count3 + 2]);
8824
+ count3 += 3;
8825
+
8826
+ if (j%2 == 0)
8827
+ success |= intersectTriangle(ray, result, s1, s2, s3);
8828
+ else
8829
+ success |= intersectTriangle(ray, result, s1, s3, s2);
8830
+
8831
+ s1.set(s2);
8832
+ s2.set(s3);
8833
+ }
8834
+ }
8835
+ }
8836
+
8837
+ assert count3 == v.length;
8838
+ }
8839
+ else // !trimmed
8840
+ {
8841
+ int count = 0;
8842
+ for (int i = 0; i < strips.length; i++)
8843
+ {
8844
+ Vertex p = bRep.GetVertex(bRep.indices[count++]);
8845
+ Vertex q = bRep.GetVertex(bRep.indices[count++]);
8846
+
8847
+ for (int j = 0; j < strips[i] - 2; j++)
8848
+ {
8849
+ Vertex r = bRep.GetVertex(bRep.indices[count++]);
8850
+
8851
+ if (j%2 == 0)
8852
+ success |= intersectTriangle(ray, result, p, q, r);
8853
+ else
8854
+ success |= intersectTriangle(ray, result, p, r, q);
8855
+
8856
+ p = q;
8857
+ q = r;
8858
+ }
8859
+ }
8860
+ }
8861
+ } else // catch (Error e)
8862
+ {
8863
+ int facecount = bRep.FaceCount();
8864
+ for (int i = 0; i < facecount; i++)
8865
+ {
8866
+ Face face = bRep.GetFace(i);
8867
+
8868
+ Vertex p = bRep.GetVertex(face.p);
8869
+ Vertex q = bRep.GetVertex(face.q);
8870
+ Vertex r = bRep.GetVertex(face.r);
8871
+
8872
+ success |= intersectTriangle(ray, result, p, q, r);
8873
+ }
8874
+ }
8875
+
8876
+ return success;
8877
+ }
8878
+
8879
+ static cVector eye = new cVector();
8880
+ static cVector dir = new cVector();
8881
+
8882
+ transient BBox bbox = null;
8883
+
8884
+ boolean intersect(Ray ray, IntersectResult result)
8885
+ {
8886
+ double eyex = ray.eyePoint.x;
8887
+ double eyey = ray.eyePoint.y;
8888
+ double eyez = ray.eyePoint.z;
8889
+
8890
+ double dirx = ray.viewDirection.x;
8891
+ double diry = ray.viewDirection.y;
8892
+ double dirz = ray.viewDirection.z;
8893
+
8894
+ if (this.fromParent != null && !(this instanceof TextureNode))
8895
+ {
8896
+ eye.x = eyex;
8897
+ eye.y = eyey;
8898
+ eye.z = eyez;
8899
+ dir.x = dirx;
8900
+ dir.y = diry;
8901
+ dir.z = dirz;
8902
+
8903
+ LA.xformPos(eye, this.fromParent, eye);
8904
+ LA.xformDir(dir, this.fromParent, dir);
8905
+
8906
+ ray.eyePoint.x = eye.x;
8907
+ ray.eyePoint.y = eye.y;
8908
+ ray.eyePoint.z = eye.z;
8909
+
8910
+ ray.viewDirection.x = dir.x;
8911
+ ray.viewDirection.y = dir.y;
8912
+ ray.viewDirection.z = dir.z;
8913
+ }
8914
+
8915
+ boolean success = false;
8916
+
8917
+ boolean touch = false;
8918
+
8919
+ if (bRep != null && link2master)
8920
+ {
8921
+ if (bbox == null)
8922
+ {
8923
+ bbox = new BBox();
8924
+
8925
+ cVector min = new cVector();
8926
+ cVector max = new cVector();
8927
+
8928
+ this.getBounds(min, max, true);
8929
+
8930
+ bbox.min.x = min.x;
8931
+ bbox.min.y = min.y;
8932
+ bbox.min.z = min.z;
8933
+
8934
+ bbox.max.x = max.x;
8935
+ bbox.max.y = max.y;
8936
+ bbox.max.z = max.z;
8937
+ }
8938
+
8939
+ if (true) // NOT WORKING bbox.intersect(ray, result))
8940
+ {
8941
+ success |= intersectMesh(ray, result);
8942
+ }
8943
+ else
8944
+ {
8945
+ //this.hide = true;
8946
+ touch = true;
8947
+ }
8948
+ }
8949
+
8950
+ for (int i=0; i<size(); i++)
8951
+ {
8952
+ Object3D child = (Object3D) reserve(i);
8953
+
8954
+ if (child == null)
8955
+ continue;
8956
+
8957
+ success |= child.intersect(ray, result);
8958
+ release(i);
8959
+ }
8960
+
8961
+ ray.eyePoint.x = eyex;
8962
+ ray.eyePoint.y = eyey;
8963
+ ray.eyePoint.z = eyez;
8964
+
8965
+ ray.viewDirection.x = dirx;
8966
+ ray.viewDirection.y = diry;
8967
+ ray.viewDirection.z = dirz;
8968
+
8969
+// if (touch)
8970
+// this.Touch(); // refresh display list
8971
+
8972
+ return success;
8973
+ }
8974
+
8975
+ static Vector3d edge1 = new Vector3d();
8976
+ static Vector3d edge2 = new Vector3d();
8977
+ static Vector3d P = new Vector3d();
8978
+ static Vector3d T = new Vector3d();
8979
+ static Vector3d Q = new Vector3d();
8980
+
8981
+ private boolean intersectTriangle(Ray ray, IntersectResult result, Vertex v1, Vertex v2, Vertex v3)
8982
+ {
8983
+ if (false)
8984
+ {
8985
+ result.t = 0;
8986
+ return true;
8987
+ }
8988
+
8989
+ /*
8990
+ Fast, Minimum Storage Ray/Triangle Intersection, Moller et al.
8991
+
8992
+ Reference: http://www.cs.virginia.edu/~gfx/Courses/2003/ImageSynthesis/papers/Acceleration/Fast%20MinimumStorage%20RayTriangle%20Intersection.pdf
8993
+ */
8994
+
8995
+ // Calculate edges of the triangle
8996
+ edge1.set(v2.x - v1.x, v2.y - v1.y, v2.z - v1.z);
8997
+ edge2.set(v3.x - v1.x, v3.y - v1.y, v3.z - v1.z);
8998
+
8999
+ // Calculate the determinant (U parameter)
9000
+ P.cross(ray.viewDirection, edge2);
9001
+ double det = edge1.dot(P);
9002
+
9003
+ if (det > -1e-9 && det < 1e-9)
9004
+ {
9005
+ return false;
9006
+ } // Ray lies in plane of triangle
9007
+
9008
+ double invDet = 1 / det;
9009
+
9010
+ // Calculate distance from vertex1 to ray origin
9011
+ T.set(ray.eyePoint.x - v1.x, ray.eyePoint.y - v1.y, ray.eyePoint.z - v1.z);
9012
+
9013
+ double U = (T.dot(P)) * invDet; // Calculate U parameter
9014
+
9015
+ if (U < 0.f || U > 1.f)
9016
+ {
9017
+ return false;
9018
+ } // Intersection lies outside of the triangle
9019
+
9020
+ // Calculate V parameter
9021
+ Q.cross(T, edge1);
9022
+
9023
+ double V = ray.viewDirection.dot(Q) * invDet;
9024
+
9025
+ if (V < 0.f || (U + V) > 1.f)
9026
+ {
9027
+ return false;
9028
+ } // Intersection lies outside of the triangle
9029
+
9030
+ double t = edge2.dot(Q) * invDet;
9031
+
9032
+ if (t > 1e-9) // Triangle and ray intersects
9033
+ {
9034
+ //result.isIntersected = true;
9035
+ //result.id = id;
9036
+
9037
+ if (false) // isShadow)
9038
+ {
9039
+ result.t = t;
9040
+ return true;
9041
+ } else if (t < result.t)
9042
+ {
9043
+ result.object = this;
9044
+
9045
+ result.t = t;
9046
+
9047
+ //result.p.x = ray.eyePoint.x + ray.viewDirection.x * t;
9048
+ //result.p.y = ray.eyePoint.y + ray.viewDirection.y * t;
9049
+ //result.p.z = ray.eyePoint.z + ray.viewDirection.z * t;
9050
+
9051
+ result.n.cross(edge1, edge2);
9052
+ result.n.normalize();
9053
+ }
9054
+
9055
+ return true;
9056
+ }
9057
+
9058
+ return false;
9059
+ }
86419060
86429061 public int Size()
86439062 {