Normand Briere
2018-07-01 89c1ad67bc65d24ceadfa9e95f8c5515283f1e97
Mocap.java
....@@ -26,6 +26,456 @@
2626 public class Mocap extends Object3D
2727 {
2828 static final long serialVersionUID = 7437391692559853707L;
29
+
30
+ void Fade()
31
+ {
32
+ if (currentbones == null || CameraPane.fullreset)
33
+ return;
34
+
35
+ cVector temp = new cVector();
36
+
37
+ int numframes = bvh.animation.getNumFrames();
38
+
39
+ int b;
40
+ float[] data;
41
+ Quat4d quatstart = new Quat4d();
42
+ Quat4d quatend = new Quat4d();
43
+ Quat4d quat = new Quat4d();
44
+ Matrix4d mat4d = new Matrix4d();
45
+ double[][] rot = new double[4][4];
46
+ for (b=numbones; --b>=0;) // HIP ROTATION: 0;)
47
+ {
48
+ // int hipindex = get(0)._index;
49
+
50
+// assert(get(0).get(0)._isHip);
51
+
52
+ // if (b == hipindex)
53
+ // continue;
54
+
55
+ data = bvh.animation.getBoneData(b);
56
+
57
+ int dof = 3; // data.length/numframes;
58
+
59
+ if (b == 0)
60
+ {
61
+ dof = 6;
62
+ }
63
+ // assert(dof == 3);
64
+
65
+ int boneframe3 = (b-1)*3; // dof;
66
+
67
+ // System.err.println("Bone #" + b + ": dof = " + dof);
68
+ int fadein = 45; // 120;
69
+
70
+ // if (b == hipindex)
71
+ // fadein *= 5;
72
+
73
+ if (fadein > numframes)
74
+ fadein = 0; // numframes;
75
+
76
+ for (int fi=fadein; --fi>=0;)
77
+ //for (int f=numframes; --f>=0;)
78
+ {
79
+ int f = fi + GetFirstFrame();
80
+
81
+ int f3 = f*dof;
82
+
83
+ float k = fi;
84
+
85
+ k /= fadein - 1;
86
+
87
+ if (Float.isNaN(k))
88
+ k = 1; // 0/0
89
+
90
+ k = (float) (-Math.cos(k*Math.PI));
91
+ k += 1;
92
+ k /= 2;
93
+
94
+ int start = 3;
95
+ int end = 0;
96
+
97
+ if (dof == 6)
98
+ {
99
+ start = 6;
100
+ end = 3;
101
+ }
102
+
103
+ if (b == 0)
104
+ {
105
+ for (int i=start; --i>=end;)
106
+ {
107
+ //data[f3 + i] += pos[i] - data[frame3 + i];
108
+// data[f3 + i] = k*data[f3+i] + (1-k)*
109
+// currentbones[boneframe3 + i-end];
110
+// data[f3 + i] = (float)CurveAngle(data[f3+i], currenthip[i], 1-k);
111
+ }
112
+
113
+ // TODO: translation
114
+
115
+// _t1.setIdentity();
116
+// _t2.setIdentity();
117
+// _t2.rotZ(currentbones[boneframe3 + start-1-end]);
118
+// _t1.mul(_t2);
119
+// _t2.setIdentity();
120
+// if (b == 0)
121
+// {
122
+// _t2.rotY(currentbones[boneframe3 + start-2-end]);
123
+// _t1.mul(_t2);
124
+// _t2.setIdentity();
125
+// _t2.rotX(currentbones[boneframe3 + start-3-end]);
126
+// }
127
+// else
128
+// {
129
+// _t2.rotX(currentbones[boneframe3 + start-2-end]);
130
+// _t1.mul(_t2);
131
+// _t2.setIdentity();
132
+// _t2.rotY(currentbones[boneframe3 + start-3-end]);
133
+// }
134
+// _t1.mul(_t2);
135
+// _t2.setIdentity();
136
+//
137
+// _t1.get(mat4d);
138
+//
139
+// mat4d.get(quatstart);
140
+//
141
+// _t1.setIdentity();
142
+// _t2.setIdentity();
143
+// _t2.rotZ(data[f3 + start-1]);
144
+// _t1.mul(_t2);
145
+// _t2.setIdentity();
146
+// if (b == 0)
147
+// {
148
+// _t2.rotY(data[f3 + start-2]);
149
+// _t1.mul(_t2);
150
+// _t2.setIdentity();
151
+// _t2.rotX(data[f3 + start-3]);
152
+// }
153
+// else
154
+// {
155
+// _t2.rotX(data[f3 + start-2]);
156
+// _t1.mul(_t2);
157
+// _t2.setIdentity();
158
+// _t2.rotY(data[f3 + start-3]);
159
+// }
160
+//
161
+// _t1.mul(_t2);
162
+// _t2.setIdentity();
163
+//
164
+// _t1.get(mat4d);
165
+//
166
+// mat4d.get(quatend);
167
+//
168
+// double dot = quatstart.x*quatend.x +
169
+// quatstart.y*quatend.y +
170
+// quatstart.z*quatend.z +
171
+// quatstart.w*quatend.w
172
+// ;
173
+//
174
+// if (dot < 0)
175
+// {
176
+// quatend.x *= -1;
177
+// quatend.y *= -1;
178
+// quatend.z *= -1;
179
+// quatend.w *= -1;
180
+// }
181
+//
182
+// // k = 1-k;
183
+//
184
+// quat.x = (1-k)*quatstart.x + k*quatend.x;
185
+// quat.y = (1-k)*quatstart.y + k*quatend.y;
186
+// quat.z = (1-k)*quatstart.z + k*quatend.z;
187
+// quat.w = (1-k)*quatstart.w + k*quatend.w;
188
+//
189
+// quat.normalize();
190
+//
191
+// mat4d.set(quat);
192
+//
193
+// cJ3D.SetTransform(rot, mat4d);
194
+//
195
+// matrixToEulerXYZ(rot, temp);
196
+//
197
+// data[f3 + start-1] = -(float)temp.z;
198
+//
199
+// if (b == 0)
200
+// {
201
+// data[f3 + start-2] = -(float)temp.y;
202
+// data[f3 + start-3] = -(float)temp.x;
203
+// }
204
+// else
205
+// {
206
+// data[f3 + start-2] = -(float)temp.x;
207
+// data[f3 + start-3] = -(float)temp.y;
208
+// }
209
+ }
210
+ else
211
+ {
212
+ for (int i=start; --i>=end;)
213
+ {
214
+ //data[f3 + i] += pos[i] - data[frame3 + i];
215
+// data[f3 + i] = k*data[f3+i] + (1-k)*
216
+// currentbones[boneframe3 + i-end];
217
+ data[f3 + i] = (float)CurveAngle(data[f3+i], currentbones[boneframe3 + i/*-end*/], 1-k);
218
+ }
219
+ }
220
+ }
221
+ }
222
+ }
223
+
224
+ // Updates the toParent matrix to keep the same position and orientation
225
+ // before resetting the mocap data.
226
+ void SetGlobalTransform()
227
+ {
228
+ //SetCurrentBones(frame);
229
+
230
+ cVector temp = new cVector();
231
+ cVector pos = new cVector();
232
+ cVector poship = new cVector();
233
+
234
+ double angleY = 0;
235
+ double angleYhip = 0;
236
+
237
+ Object3D hip = get(0);
238
+
239
+ if (hip.get(0).toParent == null)
240
+ {
241
+ hip.get(0).toParent = LA.newMatrix();
242
+ hip.get(0).fromParent = LA.newMatrix();
243
+ }
244
+
245
+ LA.matConcat(toParent, hip.get(0).toParent, matrix);
246
+ poship.x = matrix[3][0];
247
+ poship.y = matrix[3][1];
248
+ poship.z = matrix[3][2];
249
+
250
+ temp.x = 1;
251
+ temp.y = 0;
252
+ temp.z = 0;
253
+
254
+ LA.xformDir(temp, matrix, temp);
255
+
256
+ angleYhip = Math.atan2(-temp.z, temp.x);
257
+
258
+ LA.matIdentity(toParent);
259
+ LA.matYRotate(toParent, angleYhip);
260
+ LA.matTranslate(toParent, poship.x, poship.y, poship.z);
261
+
262
+// LA.matConcat(toParent, hip.get(0).toParent, toParent);
263
+
264
+ CameraPane.debugpoint.toParent[3][0] = poship.x;
265
+ CameraPane.debugpoint.toParent[3][1] = poship.y;
266
+ CameraPane.debugpoint.toParent[3][2] = poship.z;
267
+
268
+ LA.matInvert(toParent, fromParent);
269
+
270
+ LA.matIdentity(hip.get(0).toParent);
271
+ LA.matIdentity(hip.get(0).fromParent);
272
+
273
+// if (true)
274
+// return;
275
+
276
+ // Updates hip.get(0).toParent
277
+ setPose(hip, GetFirstFrame(), bvh.animation.getBoneData(hip._index));
278
+
279
+ // A = toParent; B = hip.get(0).toParent
280
+ // A'*B = A
281
+ // A' = A * B-1
282
+
283
+ poship.x = hip.get(0).toParent[3][0];
284
+ poship.y = hip.get(0).toParent[3][1];
285
+ poship.z = hip.get(0).toParent[3][2];
286
+
287
+ // AT*AR = A'T*BT*A'R*BR
288
+ //
289
+ LA.matInvert(hip.get(0).toParent, matrix);
290
+
291
+ //LA.matIdentity(matrix);
292
+ //LA.matTranslate(matrix, -poship.x, -poship.y, -poship.z);
293
+
294
+ LA.matConcat(toParent, matrix, toParent);
295
+
296
+// poship.x = hip.get(0).toParent[3][0];
297
+// poship.y = hip.get(0).toParent[3][1];
298
+// poship.z = hip.get(0).toParent[3][2];
299
+//
300
+// temp.x = 1;
301
+// temp.y = 0;
302
+// temp.z = 0;
303
+//
304
+// LA.xformDir(temp, hip.get(0).toParent, temp);
305
+//
306
+// angleYhip = Math.atan2(-temp.z, temp.x);
307
+
308
+ LA.matConcat(toParent, hip.get(0).toParent, matrix);
309
+ pos.x = hip.get(0).toParent[3][0];
310
+ pos.y = hip.get(0).toParent[3][1];
311
+ pos.z = hip.get(0).toParent[3][2];
312
+ LA.xformPos(new cVector(), hip.get(0).toParent, pos);
313
+ LA.xformPos(pos, toParent, pos);
314
+
315
+// temp.x = 1;
316
+// temp.y = 0;
317
+// temp.z = 0;
318
+//
319
+// LA.xformDir(temp, toParent, temp);
320
+//
321
+// angleY = Math.atan2(-temp.z, temp.x);
322
+//
323
+// LA.matIdentity(toParent);
324
+// LA.matYRotate(toParent, angleY - angleYhip);
325
+// LA.matTranslate(toParent, pos.x - poship.x, pos.y - poship.y, pos.z - poship.z);
326
+
327
+ CameraPane.debugpoint2.toParent[3][0] = pos.x;
328
+ CameraPane.debugpoint2.toParent[3][1] = pos.y;
329
+ CameraPane.debugpoint2.toParent[3][2] = pos.z;
330
+
331
+ CameraPane.debugpoint3.toParent[3][0] = poship.x;
332
+ CameraPane.debugpoint3.toParent[3][1] = poship.y;
333
+ CameraPane.debugpoint3.toParent[3][2] = poship.z;
334
+
335
+ poship.x = toParent[3][0];
336
+ poship.y = toParent[3][1];
337
+ poship.z = toParent[3][2];
338
+
339
+ CameraPane.debugpoint4.toParent[3][0] = poship.x;
340
+ CameraPane.debugpoint4.toParent[3][1] = poship.y;
341
+ CameraPane.debugpoint4.toParent[3][2] = poship.z;
342
+
343
+ LA.matInvert(toParent, fromParent);
344
+ }
345
+
346
+ void LoadData()
347
+ {
348
+ float[] thedata = bvh.animation.getBoneData(0);
349
+
350
+// thedata[0] = 0;
351
+// thedata[1] = 0;
352
+// thedata[2] = 0;
353
+// thedata[3] = 0;
354
+// thedata[4] = 0;
355
+// thedata[5] = 0;
356
+
357
+ if (true)
358
+ return;
359
+
360
+ for (int b=numbones; --b>=0;)
361
+ //int b = 0;
362
+ {
363
+ thedata = bvh.animation.getBoneData(b);
364
+
365
+ int numframes = bvh.animation.getNumFrames();
366
+
367
+ int dof = 6; // thedata.length/numframes;
368
+
369
+ if (b > 0)
370
+ dof = 3;
371
+
372
+ if (dof != thedata.length/numframes)
373
+ {
374
+ System.err.println("Bone #" + b + ": dof = " + dof + " ---> " + (thedata.length/numframes));
375
+ dof = thedata.length/numframes;
376
+ }
377
+
378
+ //for (int f=numframes; --f>=offset;)
379
+ int firstframe = GetFirstFrame();
380
+ for (int f=0; f<numframes-firstframe; f++)
381
+ {
382
+ int f3 = f*dof;
383
+
384
+ //for (int i=3/*dof*/; --i>=0;)
385
+ for (int i=dof; --i>=0;)
386
+ {
387
+ //thedata[f3 + i] += thedata[i] - thedata[offset*dof + i];
388
+ thedata[f3 + i] = thedata[f3 + firstframe*dof + i];
389
+ }
390
+ }
391
+ }
392
+ }
393
+
394
+ void ResetZero()
395
+ {
396
+ for (int b=numbones; --b>=0;)
397
+ {
398
+ float[] thedata = bvh.animation.getBoneData(b);
399
+
400
+ int numframes = bvh.animation.getNumFrames();
401
+
402
+ int dof = 6;
403
+
404
+ if (b > 0)
405
+ dof = 3;
406
+
407
+ for (int i=dof; --i>=0;)
408
+ {
409
+ thedata[i] = 0;
410
+ }
411
+ }
412
+ }
413
+
414
+ void SetCurrentBones(int frame)
415
+ {
416
+ if (frame == 0)
417
+ {
418
+ //frame = 1;
419
+ }
420
+
421
+ if (currentbones == null)
422
+ {
423
+ currentbones = new double[(numbones-1)*3];
424
+ currenthip = new double[6];
425
+ }
426
+
427
+ for (int b=numbones; --b>=1;) // HIP ROTATION: 0;)
428
+ //int b = 0;
429
+ {
430
+ float[] data = bvh.animation.getBoneData(b);
431
+
432
+ // int numframes = bvh.animation.getNumFrames();
433
+
434
+ int dof = 6; // data.length/numframes;
435
+
436
+ if (b > 0)
437
+ dof = 3;
438
+
439
+ //assert(dof == 3);
440
+
441
+ // System.err.println("Bone #" + b + ": dof = " + dof);
442
+ int theframe = // 1; // baseframe;
443
+ frame;
444
+
445
+ int frame3 = theframe*dof;
446
+ int bone3 = (b-1)*dof;
447
+
448
+ int start = 3;
449
+ int end = 0;
450
+
451
+ if (dof == 6)
452
+ {
453
+ start = 6;
454
+ //end = 3;
455
+ }
456
+
457
+ for (int i=start; --i>=end;)
458
+ {
459
+ currentbones[bone3 + i/*-end*/] = data[frame3 + i];
460
+ }
461
+ }
462
+
463
+ float[] data = bvh.animation.getBoneData(0);
464
+
465
+ int dof = 6;
466
+
467
+ int theframe = frame;
468
+
469
+ int frame3 = theframe*dof;
470
+
471
+ int start = 6;
472
+ int end = 0;
473
+
474
+ for (int i=start; --i>=end;)
475
+ {
476
+ currenthip[i] = data[frame3 + i];
477
+ }
478
+ }
29479
30480 void WriteTo(java.io.Writer writer) throws Exception
31481 {
....@@ -705,7 +1155,7 @@
7051155 // skip first node: translation
7061156 // Object3D b = input.get(0);
7071157
708
- setPose(input, f, data.getBoneData(input._index), false); //, null);
1158
+ setPose(input, f, data.getBoneData(input._index)); //, false); //, null);
7091159 }
7101160
7111161 //mocap.figure.Bone[] children = b.getChildren();
....@@ -921,7 +1371,7 @@
9211371 //groundz += sourcenode.parent.toParent[3][2] - posz;
9221372 }
9231373
924
- boolean footcontact = CameraPane.FOOTCONTACT; // false;
1374
+ boolean footcontact = false; // CameraPane.FOOTCONTACT; // false;
9251375
9261376 if (footcontact && ground != -123456)
9271377 {
....@@ -1220,6 +1670,7 @@
12201670 src.x = 0;
12211671 src.y = 0;
12221672 src.z = 0;
1673
+ LA.xformPos(src, get(0).get(0).toParent, src);
12231674 LA.xformPos(src, get(0).toParent, src);
12241675 LA.xformPos(src, toParent, src);
12251676 // LA.xformPos(src, rotsourcenode.toParent, src);
....@@ -1563,6 +2014,7 @@
15632014 toParent = LA.newMatrix();
15642015 fromParent = LA.newMatrix();
15652016 }
2017
+
15662018 LA.matConcat(toParent, rotsourcenode.toParent, toParent);
15672019 LA.matInvert(toParent, fromParent);
15682020
....@@ -1698,7 +2150,7 @@
16982150 }
16992151
17002152 if (!smoothed)
1701
- for (int i=4; --i>=0;)
2153
+ for (int i=0; --i>=0;)
17022154 SmoothAnimData(); // much reduces shakiness
17032155
17042156 smoothed = true;
....@@ -1780,12 +2232,26 @@
17802232 if (!live) // aout 2013
17812233 return;
17822234
1783
- cVector centroid = new cVector();
1784
- cVector floor = new cVector();
1785
-
1786
- Object3D sourcenode = GetFileRoot();
2235
+ //bvh.skeleton.setPose(f, bvh.animation.getBoneData(bvh.skeleton.getIndex()), null);
2236
+ try
2237
+ {
2238
+ SetPose(GetSkeleton(), bvh.animation, f);
2239
+ SetCurrentBones(frame);
2240
+ }
2241
+ catch (Exception e)
2242
+ {
2243
+ e.printStackTrace();
2244
+ }
17872245
1788
- boolean success; // = sourcenode.getCentroid(centroid);
2246
+ if (true)
2247
+ return;
2248
+
2249
+ cVector centroid = new cVector();
2250
+ cVector floor = new cVector();
2251
+
2252
+ Object3D sourcenode = GetFileRoot();
2253
+
2254
+ boolean success; // = sourcenode.getCentroid(centroid);
17892255 // if (!success)
17902256 // {
17912257 // //assert(success);
....@@ -1793,16 +2259,6 @@
17932259 // System.out.println("No centroid for " + sourcenode);
17942260 // }
17952261
1796
- //bvh.skeleton.setPose(f, bvh.animation.getBoneData(bvh.skeleton.getIndex()), null);
1797
- try
1798
- {
1799
- SetPose(GetSkeleton(), bvh.animation, f);
1800
- }
1801
- catch (Exception e)
1802
- {
1803
- e.printStackTrace();
1804
- }
1805
-
18062262 if (sourcenode.parent == null && sourcenode.fileparent == null)
18072263 {
18082264 // No GFD
....@@ -2050,7 +2506,7 @@
20502506 * @param frame
20512507 * Frame number
20522508 */
2053
- public void setPose(Object3D bone, int frame, float[] data, boolean fade) //, Point3d offsetTrans)
2509
+ public void setPose(Object3D bone, int frame, float[] data) //, boolean fade) //, Point3d offsetTrans)
20542510 {
20552511 if (!live) // aout 2013
20562512 return;
....@@ -2058,6 +2514,33 @@
20582514 cVector temp = new cVector();
20592515 cVector temp0 = new cVector();
20602516 double pos[] = new double[3];
2517
+ double angleY = 0;
2518
+ double sinY = 0;
2519
+ double cosY = 0;
2520
+
2521
+ if (false) // bone._isRoot && firsttimeafterload && frame > 0)
2522
+ {
2523
+ bone.get(0).GlobalTransformInv();
2524
+
2525
+ temp.x = bone.get(0).globalTransform[3][0];
2526
+ temp.y = bone.get(0).globalTransform[3][1];
2527
+ temp.z = bone.get(0).globalTransform[3][2];
2528
+
2529
+ pos[0] = temp.x;
2530
+ pos[1] = temp.y;
2531
+ pos[2] = temp.z;
2532
+
2533
+ temp.x = 1;
2534
+ temp.y = 0;
2535
+ temp.z = 0;
2536
+
2537
+ LA.xformDir(temp, bone.get(0).globalTransform, temp);
2538
+
2539
+ sinY = temp.z;
2540
+ cosY = temp.x;
2541
+
2542
+ angleY = Math.atan2(-sinY, cosY);
2543
+ }
20612544
20622545 // Object3D obj = get(0);
20632546 // Object3D gp = this.parent;
....@@ -2187,15 +2670,15 @@
21872670 // set rotation
21882671 //if (bone._rotationEnabled)
21892672 if (//frame > 0 &&
2190
- !bone.skipmocap &&
2191
- !bone.name.contains("head") &&
2673
+ !bone.skipmocap //&&
2674
+ //!bone.name.contains("head") &&
21922675 // !bone.name.contains("rFoot") &&
21932676 // !bone.name.contains("lFoot") &&
2194
- !bone.name.contains("Pinky") &&
2195
- !bone.name.contains("Ring") &&
2196
- !bone.name.contains("Mid") &&
2197
- !bone.name.contains("Index") &&
2198
- !bone.name.contains("Thumb")
2677
+// !bone.name.contains("Pinky") &&
2678
+// !bone.name.contains("Ring") &&
2679
+// !bone.name.contains("Mid") &&
2680
+// !bone.name.contains("Index") &&
2681
+// !bone.name.contains("Thumb")
21992682 )
22002683 {
22012684 //_rotTG.setTransform(_t1);
....@@ -2228,15 +2711,17 @@
22282711 // CROWD
22292712 //cJ3D.GetTranslation(bone, _trans0);
22302713 //_trans.add(_trans0);
2231
- _transTF.setIdentity();
2714
+ //_transTF.setIdentity();
2715
+ _transTF.set(_t1);
22322716 _transTF.setTranslation(_trans);
22332717 // System.err.println("Translate: " + _transTF);
22342718 //_transTG.setTransform(_transTF);
2235
- cJ3D.ResetTransform(bone, _transTF, true); // translation node
2719
+ cJ3D.ResetTransform(bone.get(0), _transTF, true); // translation node
2720
+ //cJ3D.ResetTransform(bone, _transTF, true); // translation node
22362721 }
22372722 }
22382723
2239
- if (bone._isRoot && firsttimeafterload && frame > 0)
2724
+ if (false) // bone._isRoot && firsttimeafterload && frame > 0)
22402725 {
22412726 assert(bone == get(0));
22422727
....@@ -2252,17 +2737,17 @@
22522737 // LA.matConcat(originmatrixinv, tempmatrixinv, fromParent); // ABC * (B'*C')^-1
22532738 //
22542739
2255
- Invariants();
2256
- bone.Invariants();
2257
- bone.get(0).Invariants();
2258
- // if (origin == null)
2259
- // origin = null;
2260
- origin.Invariants();
2261
- origin0.Invariants();
2740
+ Invariants();
2741
+ bone.Invariants();
2742
+ bone.get(0).Invariants();
2743
+ // if (origin == null)
2744
+ // origin = null;
2745
+ origin.Invariants();
2746
+ origin0.Invariants();
22622747
2263
-
2264
- LA.matConcat(bone.get(0).fromParent, bone.fromParent, tempmatrix); // B' * C'
2265
- LA.matConcat(origin.toParent, tempmatrix, toParent);
2748
+
2749
+//?? LA.matConcat(bone.get(0).fromParent, bone.fromParent, tempmatrix); // B' * C'
2750
+//?? LA.matConcat(origin.toParent, tempmatrix, toParent);
22662751
22672752 // matrixToEulerXYZ(toParent, temp);
22682753 // matrixToEulerXYZ(bone.get(0).toParent, temp);
....@@ -2296,7 +2781,7 @@
22962781 //
22972782 // mat4d.get(quat);
22982783
2299
- if (true) // hip correction
2784
+ if (false) // hip correction
23002785 {
23012786 _t2.setIdentity();
23022787
....@@ -2337,13 +2822,13 @@
23372822 //????? LA.matConcat(bone.get(0).toParent, bone.toParent, tempmatrix); // B' * C'
23382823 // LA.matConcat(origin.fromParent, tempmatrix, fromParent);
23392824
2340
- LA.matInvert(toParent, fromParent);
2825
+//?? LA.matInvert(toParent, fromParent);
23412826
2342
- Invariants();
2343
- bone.Invariants();
2344
- bone.get(0).Invariants();
2345
- origin.Invariants();
2346
- origin0.Invariants();
2827
+ Invariants();
2828
+ bone.Invariants();
2829
+ bone.get(0).Invariants();
2830
+ origin.Invariants();
2831
+ origin0.Invariants();
23472832
23482833 // LA.matInvert(fromParent, tempmatrix);
23492834 //
....@@ -2364,33 +2849,39 @@
23642849 // temp.x = data[0];
23652850 // temp.y = data[1];
23662851 // temp.z = data[2];
2367
- LA.xformPos(temp, origin0.toParent, temp0);
2852
+ LA.xformPos(temp, origin0.toParent, temp0); // NU
23682853
2369
-// bone.GlobalTransformInv();
2370
-
2371
- temp0.x = bone.globalTransform[3][0];
2372
- temp0.y = bone.globalTransform[3][1];
2373
- temp0.z = bone.globalTransform[3][2];
2374
-
2854
+// bone.get(0).GlobalTransformInv();
2855
+//
2856
+// temp0.x = bone.get(0).globalTransform[3][0];
2857
+// temp0.y = bone.get(0).globalTransform[3][1];
2858
+// temp0.z = bone.get(0).globalTransform[3][2];
2859
+//
23752860 // MEGA TEST // june 2014: necessary for precision? otherwise comment works
2376
-fromParent[3][0] = 0;
2377
-fromParent[3][1] = 0;
2378
-fromParent[3][2] = 0;
2379
-LA.matInvert(fromParent, toParent);
2861
+//fromParent[3][0] = 0;
2862
+//fromParent[3][1] = 0;
2863
+//fromParent[3][2] = 0;
2864
+//LA.matInvert(fromParent, toParent);
23802865
2381
- LA.xformPos(temp0, fromParent, temp);
2866
+ LA.xformPos(temp0, fromParent, temp); // NU
23822867 //LA.xformPos(temp, bone.fromParent, temp);
23832868 // float[] data;
23842869
2385
- pos[0] = temp.x;
2386
- pos[1] = temp.y;
2387
- pos[2] = temp.z;
2870
+// pos[0] = temp.x;
2871
+// pos[1] = temp.y;
2872
+// pos[2] = temp.z;
2873
+//
2874
+// temp.x = 1;
2875
+// temp.y = 0;
2876
+// temp.z = 0;
2877
+//
2878
+// LA.xformDir(temp, bone.get(0).globalTransform, temp);
23882879
23892880 // CameraPane.selectedpoint.toParent[3][0] = temp0.x;
23902881 // CameraPane.selectedpoint.toParent[3][1] = temp0.y;
23912882 // CameraPane.selectedpoint.toParent[3][2] = temp0.z;
23922883
2393
- int numframes = bvh.animation.getNumFrames();
2884
+ int numframes = bvh.animation.getNumFrames();
23942885
23952886 int b = 0;
23962887 {
....@@ -2398,12 +2889,12 @@
23982889
23992890 int dof = 6; /// data.length/numframes;
24002891
2401
- // july 2014
2402
- if (dof != data.length/numframes)
2403
- {
2404
- System.err.println("Bone #" + b + ": dof = " + dof + " ---> " + (data.length/numframes));
2405
- dof = data.length/numframes;
2406
- }
2892
+ // july 2014
2893
+ if (dof != data.length/numframes)
2894
+ {
2895
+ System.err.println("Bone #" + b + ": dof = " + dof + " ---> " + (data.length/numframes));
2896
+ dof = data.length/numframes;
2897
+ }
24072898
24082899
24092900 int theframe = 0; // baseframe;
....@@ -2455,17 +2946,26 @@
24552946 // }
24562947 // }
24572948
2458
- data[f3 + i] += pos[i] - data[frame3 + i];
2949
+ data[f3 + i] += /*pos[i]*/ - data[frame3 + i];
24592950
24602951 // if (f < 3)
24612952 // {
24622953 // System.out.print(data[f3 + i] + " ");
24632954 // }
24642955 }
2465
-// if (f < 3)
2466
-// {
2467
-// System.out.println();
2468
-// }
2956
+
2957
+ double t1 = data[f3 + 0];
2958
+ double t2 = data[f3 + 2];
2959
+
2960
+ data[f3 + 0] = (float)(cosY*t1 - sinY*t2);
2961
+ data[f3 + 2] = (float)(sinY*t1 + cosY*t2);
2962
+
2963
+ for (int i=3/*dof*/; --i>=0;)
2964
+ {
2965
+ data[f3 + i] += pos[i];
2966
+ }
2967
+
2968
+ data[f3 + 4] += angleY - data[frame3 + 4];
24692969 }
24702970 }
24712971
....@@ -2479,186 +2979,17 @@
24792979 // temp.y = data[1];
24802980 // temp.z = data[2];
24812981
2482
- if (fade && currentbones != null && !CameraPane.fullreset)
2483
- {
2484
- Quat4d quatstart = new Quat4d();
2485
- Quat4d quatend = new Quat4d();
2486
- Quat4d quat = new Quat4d();
2487
- Matrix4d mat4d = new Matrix4d();
2488
- double[][] rot = new double[4][4];
2489
-
2490
- for (b=numbones; --b>=1;) // HIP ROTATION: 0;)
2491
- {
2492
- int hipindex = get(0)._index;
2493
-
2494
-// assert(get(0).get(0)._isHip);
2495
-
2496
- // if (b == hipindex)
2497
- // continue;
2498
-
2499
- data = bvh.animation.getBoneData(b);
2500
-
2501
- int dof = 3; // data.length/numframes;
2502
-
2503
- // assert(dof == 3);
2504
-
2505
- int boneframe3 = b*3; // dof;
2506
-
2507
- // System.err.println("Bone #" + b + ": dof = " + dof);
2508
- int fadein = 45; // 120;
2509
-
2510
- // if (b == hipindex)
2511
- // fadein *= 5;
2512
-
2513
- if (fadein > numframes)
2514
- fadein = 0; // numframes;
2515
-
2516
- for (int fi=fadein; --fi>=0;)
2517
- //for (int f=numframes; --f>=0;)
2518
- {
2519
- int f = fi + GetFirstFrame();
2520
-
2521
- int f3 = f*dof;
2522
-
2523
- float k = fi;
2524
-
2525
- k /= fadein - 1;
2526
-
2527
- if (Float.isNaN(k))
2528
- k = 1; // 0/0
2529
-
2530
- k = (float) (-Math.cos(k*Math.PI));
2531
- k += 1;
2532
- k /= 2;
2533
-
2534
- int start = 3;
2535
- int end = 0;
2536
-
2537
- if (dof == 6)
2538
- {
2539
- start = 6;
2540
- end = 3;
2541
- }
2542
-
2543
- if (b == 0)
2544
- {
2545
- _t1.setIdentity();
2546
- _t2.setIdentity();
2547
- _t2.rotZ(currentbones[boneframe3 + start-1-end]);
2548
- _t1.mul(_t2);
2549
- _t2.setIdentity();
2550
- if (b == 0)
2551
- {
2552
- _t2.rotY(currentbones[boneframe3 + start-2-end]);
2553
- _t1.mul(_t2);
2554
- _t2.setIdentity();
2555
- _t2.rotX(currentbones[boneframe3 + start-3-end]);
2556
- }
2557
- else
2558
- {
2559
- _t2.rotX(currentbones[boneframe3 + start-2-end]);
2560
- _t1.mul(_t2);
2561
- _t2.setIdentity();
2562
- _t2.rotY(currentbones[boneframe3 + start-3-end]);
2563
- }
2564
- _t1.mul(_t2);
2565
- _t2.setIdentity();
2566
-
2567
- _t1.get(mat4d);
2568
-
2569
- mat4d.get(quatstart);
2570
-
2571
- _t1.setIdentity();
2572
- _t2.setIdentity();
2573
- _t2.rotZ(data[f3 + start-1]);
2574
- _t1.mul(_t2);
2575
- _t2.setIdentity();
2576
- if (b == 0)
2577
- {
2578
- _t2.rotY(data[f3 + start-2]);
2579
- _t1.mul(_t2);
2580
- _t2.setIdentity();
2581
- _t2.rotX(data[f3 + start-3]);
2582
- }
2583
- else
2584
- {
2585
- _t2.rotX(data[f3 + start-2]);
2586
- _t1.mul(_t2);
2587
- _t2.setIdentity();
2588
- _t2.rotY(data[f3 + start-3]);
2589
- }
2590
-
2591
- _t1.mul(_t2);
2592
- _t2.setIdentity();
2593
-
2594
- _t1.get(mat4d);
2595
-
2596
- mat4d.get(quatend);
2597
-
2598
- double dot = quatstart.x*quatend.x +
2599
- quatstart.y*quatend.y +
2600
- quatstart.z*quatend.z +
2601
- quatstart.w*quatend.w
2602
- ;
2603
-
2604
- if (dot < 0)
2605
- {
2606
- quatend.x *= -1;
2607
- quatend.y *= -1;
2608
- quatend.z *= -1;
2609
- quatend.w *= -1;
2610
- }
2611
-
2612
- // k = 1-k;
2613
-
2614
- quat.x = (1-k)*quatstart.x + k*quatend.x;
2615
- quat.y = (1-k)*quatstart.y + k*quatend.y;
2616
- quat.z = (1-k)*quatstart.z + k*quatend.z;
2617
- quat.w = (1-k)*quatstart.w + k*quatend.w;
2618
-
2619
- quat.normalize();
2620
-
2621
- mat4d.set(quat);
2622
-
2623
- cJ3D.SetTransform(rot, mat4d);
2624
-
2625
- matrixToEulerXYZ(rot, temp);
2626
-
2627
- data[f3 + start-1] = -(float)temp.z;
2628
-
2629
- if (b == 0)
2630
- {
2631
- data[f3 + start-2] = -(float)temp.y;
2632
- data[f3 + start-3] = -(float)temp.x;
2633
- }
2634
- else
2635
- {
2636
- data[f3 + start-2] = -(float)temp.x;
2637
- data[f3 + start-3] = -(float)temp.y;
2638
- }
2639
- }
2640
- else
2641
- {
2642
- for (int i=start; --i>=end;)
2643
- {
2644
- //data[f3 + i] += pos[i] - data[frame3 + i];
2645
- // data[f3 + i] = k*data[f3+i] + (1-k)*
2646
- // currentbones[boneframe3 + i-end];
2647
- data[f3 + i] = (float)CurveAngle(data[f3+i], currentbones[boneframe3 + i-end], 1-k);
2648
- }
2649
- }
2650
-
2651
- }
2652
- }
2653
-
2654
- }
2982
+// if (fade && currentbones != null && !CameraPane.fullreset)
2983
+// {
2984
+// Fade();
2985
+// }
26552986 }
2656
-
2987
+
26572988 if (false) // doesn't change anything bone._isRoot)
26582989 {
26592990 // Reset orientation
26602991 _t2.setIdentity();
2661
-
2992
+
26622993 double tx = bone.toParent[3][0];
26632994 double ty = bone.toParent[3][1];
26642995 double tz = bone.toParent[3][2];
....@@ -2667,9 +2998,9 @@
26672998 temp.y = 0;
26682999 temp.z = 0;
26693000 LA.xformDir(temp, bone.toParent, temp);
2670
-
3001
+
26713002 double angle = Math.atan2(-temp.z, temp.x);
2672
-
3003
+
26733004 _t2.rotY(angle);
26743005 // _t1.setIdentity();
26753006 // _t1.rotX(temp.x);
....@@ -2682,56 +3013,21 @@
26823013 // _t1.setIdentity();
26833014 // _t1.rotZ(temp.z);
26843015 // _t2.mul(_t1);
2685
-
3016
+
26863017 cJ3D.ResetTransform(bone, _t2, false);
2687
-
3018
+
26883019 bone.toParent[3][0] = tx;
26893020 bone.toParent[3][1] = ty;
26903021 bone.toParent[3][2] = tz;
26913022 }
26923023 }
26933024
2694
- if (currentbones == null)
2695
- currentbones = new double[numbones*3];
2696
-
2697
- for (int b=numbones; --b>=1;) // 0;)
2698
- //int b = 0;
2699
- {
2700
- data = bvh.animation.getBoneData(b);
2701
-
2702
- int numframes = bvh.animation.getNumFrames();
2703
-
2704
- int dof = 6; // data.length/numframes;
2705
-
2706
- if (b > 0)
2707
- dof = 3;
2708
-
2709
- //assert(dof == 3);
2710
-
2711
- // System.err.println("Bone #" + b + ": dof = " + dof);
2712
- int theframe = // 1; // baseframe;
2713
- frame;
2714
-
2715
- int frame3 = theframe*dof;
2716
- int bone3 = b*3; // dof;
2717
-
2718
- int start = 3;
2719
- int end = 0;
2720
-
2721
- if (dof == 6)
2722
- {
2723
- start = 6;
2724
- end = 3;
2725
- }
2726
- for (int i=start; --i>=end;)
2727
- {
2728
- currentbones[bone3 + i-end] = data[frame3 + i];
2729
- }
2730
- }
3025
+ // SetCurrentBones(frame);
27313026 }
27323027 }
27333028
2734
- static int offset = 1; // 5; // 10; // 5; // 100; // skip initial "T"
3029
+ // skip initial "T"
3030
+ static int offset = 1; // 5; // 10; // 5; // 100;
27353031
27363032 void ReadBVH()
27373033 {
....@@ -2767,7 +3063,7 @@
27673063
27683064 // assert(obj.name.equals("hip"));
27693065
2770
- Object3D rot = hip.get(0);
3066
+ //Object3D rot = hip.get(0);
27713067
27723068 // assert(obj.name == null); // .equals("hip#"));
27733069
....@@ -2777,8 +3073,8 @@
27773073 hip.fromParent = LA.newMatrix();
27783074 }
27793075
2780
- hip.toParent[3][0] = hip.toParent[3][1] = hip.toParent[3][2] = 0;
2781
- hip.fromParent[3][0] = hip.fromParent[3][1] = hip.fromParent[3][2] = 0;
3076
+ //hip.toParent[3][0] = hip.toParent[3][1] = hip.toParent[3][2] = 0;
3077
+ //hip.fromParent[3][0] = hip.fromParent[3][1] = hip.fromParent[3][2] = 0;
27823078
27833079 if (toParent == null)
27843080 {
....@@ -2790,25 +3086,28 @@
27903086 LA.matIdentity(fromParent);
27913087 // LA.matIdentity(hip.toParent);
27923088 // LA.matIdentity(hip.fromParent);
2793
- if (rot.toParent == null)
2794
- {
2795
- // june 2014
2796
- rot.toParent = LA.newMatrix();
2797
- rot.fromParent = LA.newMatrix();
2798
- }
2799
- LA.matIdentity(rot.toParent);
2800
- LA.matIdentity(rot.fromParent);
3089
+// if (rot.toParent == null)
3090
+// {
3091
+// // june 2014
3092
+// rot.toParent = LA.newMatrix();
3093
+// rot.fromParent = LA.newMatrix();
3094
+// }
3095
+// LA.matIdentity(rot.toParent);
3096
+// LA.matIdentity(rot.fromParent);
28013097
28023098 // july 2014
28033099 goalx = goalz = 0;
28043100 targetx = targetz = 0;
28053101
2806
- SetPositionDelta(true, true, true, false);
3102
+ //SetPositionDelta(true, true, true, false);
3103
+// LoadData();
3104
+// Rewind();
28073105
28083106 baseframe = frame = 0; // T pose. No offset.
28093107
28103108 // aout 2013 endframe = 0; // june 2013
28113109
3110
+ //ResetZero();
28123111 setPose(frame);
28133112 }
28143113
....@@ -2840,7 +3139,7 @@
28403139 baseframe += step;
28413140
28423141 //frame = baseframe;
2843
- frame += step;
3142
+ //frame += step;
28443143
28453144 // if (frame != baseframe)
28463145 // {
....@@ -2880,7 +3179,10 @@
28803179 {
28813180 bvh = ((Mocap)support.get(mocapsupport++ % support.Size())).bvh;
28823181
2883
- SetPositionDelta(false, true, true, true);
3182
+ // SetPositionDelta(false, true, true, true);
3183
+ LoadData();
3184
+ Rewind();
3185
+ Fade();
28843186 }
28853187 catch (Exception e)
28863188 {
....@@ -2889,13 +3191,17 @@
28893191 }
28903192 }
28913193 }
3194
+
3195
+ frame = GetFirstFrame();
28923196 }
3197
+ else
3198
+ frame += step;
28933199
28943200 //SetPositionDelta(false);
28953201
2896
- if (frame >= bvh.animation.getNumFrames())
2897
- //baseframe =
2898
- frame = GetFirstFrame(); // 0; // offset; // initial point
3202
+// if (frame >= bvh.animation.getNumFrames())
3203
+// //baseframe =
3204
+// frame = GetFirstFrame(); // 0; // offset; // initial point
28993205
29003206 //System.err.println("frame = " + frame);
29013207 if (lastframe != 0)
....@@ -3193,6 +3499,8 @@
31933499 //static
31943500 transient
31953501 double[] currentbones;
3502
+ double[] currenthip;
3503
+
31963504 transient boolean inbetween;
31973505
31983506 void SetPositionDelta(boolean reset, boolean rewind, boolean load, boolean fade)
....@@ -3203,38 +3511,9 @@
32033511 //assert(dim == numframes*6);
32043512
32053513 if (load)
3206
- for (int b=numbones; --b>=0;)
3207
- //int b = 0;
3208
- {
3209
- float[] thedata = bvh.animation.getBoneData(b);
3210
-
3211
- int numframes = bvh.animation.getNumFrames();
3212
-
3213
- int dof = 6; // thedata.length/numframes;
3214
-
3215
- if (b > 0)
3216
- dof = 3;
3217
-
3218
- if (dof != thedata.length/numframes)
3219
- {
3220
- System.err.println("Bone #" + b + ": dof = " + dof + " ---> " + (thedata.length/numframes));
3221
- dof = thedata.length/numframes;
3222
- }
3223
-
3224
- //for (int f=numframes; --f>=offset;)
3225
- int firstframe = GetFirstFrame();
3226
- for (int f=0; f<numframes-firstframe; f++)
3227
- {
3228
- int f3 = f*dof;
3229
-
3230
- //for (int i=3/*dof*/; --i>=0;)
3231
- for (int i=dof; --i>=0;)
3232
- {
3233
- //thedata[f3 + i] += thedata[i] - thedata[offset*dof + i];
3234
- thedata[f3 + i] = thedata[f3 + firstframe*dof + i];
3235
- }
3236
- }
3237
- }
3514
+ {
3515
+ LoadData();
3516
+ }
32383517
32393518 // assert(thedata[0] == 0);
32403519 // assert(thedata[1] == 0);
....@@ -3244,11 +3523,11 @@
32443523 // assert(thedata[7] == 0);
32453524 // assert(thedata[8] == 0);
32463525
3247
- Object3D obj = get(0);
3526
+ Object3D hip = get(0);
32483527
32493528 // assert(obj.name.equals("hip"));
32503529
3251
- Object3D rot = obj.get(0);
3530
+ Object3D rot = hip.get(0);
32523531
32533532 // if (toParent == null)
32543533 // {
....@@ -3260,7 +3539,7 @@
32603539 // // assert(obj.name == null); // .equals("hip#"));
32613540
32623541 Invariants();
3263
- obj.Invariants();
3542
+ hip.Invariants();
32643543 rot.Invariants();
32653544
32663545 if (origin == null)
....@@ -3281,16 +3560,16 @@
32813560 fromParent = LA.newMatrix();
32823561 }
32833562
3284
- if (obj.toParent == null) // june 2014
3563
+ if (hip.toParent == null) // june 2014
32853564 {
3286
- obj.toParent = LA.newMatrix();
3287
- obj.fromParent = LA.newMatrix();
3565
+ hip.toParent = LA.newMatrix();
3566
+ hip.fromParent = LA.newMatrix();
32883567 }
32893568
3290
- LA.matConcat(toParent, obj.toParent, origin0.toParent); // AB
3569
+ LA.matConcat(toParent, hip.toParent, origin0.toParent); // AB
32913570 Object3D gp = this.parent;
32923571 this.parent = null;
3293
- obj.GlobalTransformInv();
3572
+ hip.GlobalTransformInv();
32943573 this.parent = gp;
32953574
32963575 if (rot.toParent == null)
....@@ -3313,41 +3592,41 @@
33133592 LA.matInvert(origin.toParent, origin.fromParent);
33143593
33153594 Invariants();
3316
- obj.Invariants();
3595
+ hip.Invariants();
33173596 rot.Invariants();
33183597 origin.Invariants();
33193598 origin0.Invariants();
33203599
3321
- firsttimeafterload = true;
3600
+ firsttimeafterload = false; // true;
33223601
33233602 if (rewind)
33243603 Rewind();
33253604
3326
- setPose(obj, frame, bvh.animation.getBoneData(obj._index), fade);
3605
+ setPose(hip, frame, bvh.animation.getBoneData(hip._index)); //, fade);
33273606
3328
- if (true)
3329
- return;
3607
+ if (true)
3608
+ return;
33303609
33313610 double pos[] = new double[6];
33323611
33333612 if (!reset)
33343613 {
3335
- assert(obj.tx == obj.toParent[3][0]);
3336
- assert(obj.ty == obj.toParent[3][1]);
3337
- assert(obj.tz == obj.toParent[3][2]);
3614
+ assert(hip.tx == hip.toParent[3][0]);
3615
+ assert(hip.ty == hip.toParent[3][1]);
3616
+ assert(hip.tz == hip.toParent[3][2]);
33383617
3339
- pos[0] = obj.tx; // obj.toParent[3][0];
3340
- pos[1] = obj.ty; // obj.toParent[3][1];
3341
- pos[2] = obj.tz; // obj.toParent[3][2];
3342
- pos[3] = obj.rz;
3343
- pos[4] = obj.ry;
3344
- pos[5] = obj.rx;
3618
+ pos[0] = hip.tx; // obj.toParent[3][0];
3619
+ pos[1] = hip.ty; // obj.toParent[3][1];
3620
+ pos[2] = hip.tz; // obj.toParent[3][2];
3621
+ pos[3] = hip.rz;
3622
+ pos[4] = hip.ry;
3623
+ pos[5] = hip.rx;
33453624 }
33463625
33473626 cVector temp = new cVector();
3348
- temp.x = obj.globalTransform[3][0];
3349
- temp.y = obj.globalTransform[3][1];
3350
- temp.z = obj.globalTransform[3][2];
3627
+ temp.x = hip.globalTransform[3][0];
3628
+ temp.y = hip.globalTransform[3][1];
3629
+ temp.z = hip.globalTransform[3][2];
33513630 // LA.xformPos(temp, origin0.toParent, temp);
33523631 // System.err.println("POS0 = " + temp.x + ", " + temp.y + ", " + temp.z + ";");
33533632
....@@ -3431,7 +3710,7 @@
34313710 }
34323711
34333712 // Rewind();
3434
- setPose(obj, frame, bvh.animation.getBoneData(obj._index), true);
3713
+ setPose(hip, frame, bvh.animation.getBoneData(hip._index)); // , true);
34353714 }
34363715
34373716 void Rewind()