-
Notifications
You must be signed in to change notification settings - Fork 22
/
assiqe.c
1293 lines (1107 loc) · 36.3 KB
/
assiqe.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <assert.h>
#include <assimp/cimport.h>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include "getopt.h"
int verbose = 0;
int need_to_bake_skin = 0;
int save_all_bones = 0;
int dolowprec = 0;
int dostatic = 0; // export without skeleton
int dorigid = 0; // export rigid (non-deformed) nodes as bones too
int domesh = 1; // export mesh
int doanim = 0; // export animations
int dobone = 0; // export skeleton
int doflip = 1; // export flipped (quake-style clockwise winding) triangles
int doaxis = 0; // flip bone axis from X to Y to match blender
int dounscale = 0; // remove scaling from bind pose
int dohips = 0; // reparent thighs to pelvis (see zo_hom_marche)
char *only_one_node = NULL;
int list_all_meshes = 0;
int list_all_positions = 0;
#define MAX_UVMAP 4
#define FIRST_UVMAP 0
#define MAX_COL 4
#define FIRST_COL 4
// We use %.9g to print floats with 9 digits of precision which
// is enough to represent a 32-bit float accurately, while still
// shortening if possible to save space for all those 0s and 1s.
#define EPSILON 0.00001
#define NEAR_0(x) (fabs((x)) < EPSILON)
#define NEAR_1(x) (NEAR_0((x)-1))
#define KILL_0(x) (NEAR_0((x)) ? 0 : (x))
#define KILL_N(x,n) (NEAR_0((x)-(n)) ? (n) : (x))
#define KILL(x) KILL_0(KILL_N(KILL_N(x, 1), -1))
#define LOWP(x) (roundf(x*32768)/32768)
int fix_hips(int verbose);
void unfix_hips(void);
static struct aiMatrix4x4 yup_to_zup = {
1, 0, 0, 0,
0, 0, -1, 0,
0, 1, 0, 0,
0, 0, 0, 1
};
static struct aiMatrix4x4 axis_x_to_y = {
0, 1, 0, 0,
-1, 0, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
};
double aiDeterminant(struct aiMatrix4x4 *m)
{
return (double)
m->a1*m->b2*m->c3*m->d4 - m->a1*m->b2*m->c4*m->d3 +
m->a1*m->b3*m->c4*m->d2 - m->a1*m->b3*m->c2*m->d4 +
m->a1*m->b4*m->c2*m->d3 - m->a1*m->b4*m->c3*m->d2 -
m->a2*m->b3*m->c4*m->d1 + m->a2*m->b3*m->c1*m->d4 -
m->a2*m->b4*m->c1*m->d3 + m->a2*m->b4*m->c3*m->d1 -
m->a2*m->b1*m->c3*m->d4 + m->a2*m->b1*m->c4*m->d3 +
m->a3*m->b4*m->c1*m->d2 - m->a3*m->b4*m->c2*m->d1 +
m->a3*m->b1*m->c2*m->d4 - m->a3*m->b1*m->c4*m->d2 +
m->a3*m->b2*m->c4*m->d1 - m->a3*m->b2*m->c1*m->d4 -
m->a4*m->b1*m->c2*m->d3 + m->a4*m->b1*m->c3*m->d2 -
m->a4*m->b2*m->c3*m->d1 + m->a4*m->b2*m->c1*m->d3 -
m->a4*m->b3*m->c1*m->d2 + m->a4*m->b3*m->c2*m->d1;
}
void aiInverseMatrix(struct aiMatrix4x4 *p, struct aiMatrix4x4 *m)
{
double det = aiDeterminant(m);
assert(det != 0.0);
double invdet = 1.0 / det;
p->a1= invdet * (m->b2*(m->c3*m->d4-m->c4*m->d3) + m->b3*(m->c4*m->d2-m->c2*m->d4) + m->b4*(m->c2*m->d3-m->c3*m->d2));
p->a2=-invdet * (m->a2*(m->c3*m->d4-m->c4*m->d3) + m->a3*(m->c4*m->d2-m->c2*m->d4) + m->a4*(m->c2*m->d3-m->c3*m->d2));
p->a3= invdet * (m->a2*(m->b3*m->d4-m->b4*m->d3) + m->a3*(m->b4*m->d2-m->b2*m->d4) + m->a4*(m->b2*m->d3-m->b3*m->d2));
p->a4=-invdet * (m->a2*(m->b3*m->c4-m->b4*m->c3) + m->a3*(m->b4*m->c2-m->b2*m->c4) + m->a4*(m->b2*m->c3-m->b3*m->c2));
p->b1=-invdet * (m->b1*(m->c3*m->d4-m->c4*m->d3) + m->b3*(m->c4*m->d1-m->c1*m->d4) + m->b4*(m->c1*m->d3-m->c3*m->d1));
p->b2= invdet * (m->a1*(m->c3*m->d4-m->c4*m->d3) + m->a3*(m->c4*m->d1-m->c1*m->d4) + m->a4*(m->c1*m->d3-m->c3*m->d1));
p->b3=-invdet * (m->a1*(m->b3*m->d4-m->b4*m->d3) + m->a3*(m->b4*m->d1-m->b1*m->d4) + m->a4*(m->b1*m->d3-m->b3*m->d1));
p->b4= invdet * (m->a1*(m->b3*m->c4-m->b4*m->c3) + m->a3*(m->b4*m->c1-m->b1*m->c4) + m->a4*(m->b1*m->c3-m->b3*m->c1));
p->c1= invdet * (m->b1*(m->c2*m->d4-m->c4*m->d2) + m->b2*(m->c4*m->d1-m->c1*m->d4) + m->b4*(m->c1*m->d2-m->c2*m->d1));
p->c2=-invdet * (m->a1*(m->c2*m->d4-m->c4*m->d2) + m->a2*(m->c4*m->d1-m->c1*m->d4) + m->a4*(m->c1*m->d2-m->c2*m->d1));
p->c3= invdet * (m->a1*(m->b2*m->d4-m->b4*m->d2) + m->a2*(m->b4*m->d1-m->b1*m->d4) + m->a4*(m->b1*m->d2-m->b2*m->d1));
p->c4=-invdet * (m->a1*(m->b2*m->c4-m->b4*m->c2) + m->a2*(m->b4*m->c1-m->b1*m->c4) + m->a4*(m->b1*m->c2-m->b2*m->c1));
p->d1=-invdet * (m->b1*(m->c2*m->d3-m->c3*m->d2) + m->b2*(m->c3*m->d1-m->c1*m->d3) + m->b3*(m->c1*m->d2-m->c2*m->d1));
p->d2= invdet * (m->a1*(m->c2*m->d3-m->c3*m->d2) + m->a2*(m->c3*m->d1-m->c1*m->d3) + m->a3*(m->c1*m->d2-m->c2*m->d1));
p->d3=-invdet * (m->a1*(m->b2*m->d3-m->b3*m->d2) + m->a2*(m->b3*m->d1-m->b1*m->d3) + m->a3*(m->b1*m->d2-m->b2*m->d1));
p->d4= invdet * (m->a1*(m->b2*m->c3-m->b3*m->c2) + m->a2*(m->b3*m->c1-m->b1*m->c3) + m->a3*(m->b1*m->c2-m->b2*m->c1));
}
void aiComposeMatrix(struct aiMatrix4x4 *m, struct aiVector3D *scale, struct aiQuaternion *q, struct aiVector3D *pos)
{
struct aiMatrix4x4 smat;
aiIdentityMatrix4(m);
m->a1 = 1.0f - 2.0f * (q->y * q->y + q->z * q->z);
m->a2 = 2.0f * (q->x * q->y - q->z * q->w);
m->a3 = 2.0f * (q->x * q->z + q->y * q->w);
m->b1 = 2.0f * (q->x * q->y + q->z * q->w);
m->b2 = 1.0f - 2.0f * (q->x * q->x + q->z * q->z);
m->b3 = 2.0f * (q->y * q->z - q->x * q->w);
m->c1 = 2.0f * (q->x * q->z - q->y * q->w);
m->c2 = 2.0f * (q->y * q->z + q->x * q->w);
m->c3 = 1.0f - 2.0f * (q->x * q->x + q->y * q->y);
aiIdentityMatrix4(&smat);
smat.a1 = scale->x;
smat.b2 = scale->y;
smat.c3 = scale->z;
aiMultiplyMatrix4(m, &smat);
m->a4 = pos->x; m->b4 = pos->y; m->c4 = pos->z;
}
void aiNormalizeQuaternion(struct aiQuaternion *q)
{
const float mag = sqrt(q->x*q->x + q->y*q->y + q->z*q->z + q->w*q->w);
if (mag)
{
const float invMag = 1.0f/mag;
q->x *= invMag;
q->y *= invMag;
q->z *= invMag;
q->w *= invMag;
}
}
void print_matrix(struct aiMatrix4x4 *m)
{
printf("matrix %g %g %g %g %g %g %g %g %g (det=%g)\n",
m->a1, m->a2, m->a3,
m->b1, m->b2, m->b3,
m->c1, m->c2, m->c3,
aiDeterminant(m));
}
int is_identity_matrix(struct aiMatrix4x4 *m)
{
return
NEAR_1(m->a1) && NEAR_0(m->a2) && NEAR_0(m->a3) &&
NEAR_0(m->b1) && NEAR_1(m->b2) && NEAR_0(m->b3) &&
NEAR_0(m->c1) && NEAR_0(m->c2) && NEAR_1(m->c3) &&
NEAR_0(m->a4) && NEAR_0(m->b4) && NEAR_0(m->c4);
}
char basename[1024];
int numtags = 0;
char **taglist = NULL;
int numuntags = 0;
char *untaglist[100];
#define MAXBLEND 12
#define MIN(a,b) ((a)<(b)?(a):(b))
struct vb {
int b[MAXBLEND];
float w[MAXBLEND];
int n;
};
struct material {
struct aiMaterial *material;
char *name;
char *shader;
};
struct material matlist[10000];
int nummats = 0;
struct bone {
struct aiNode *node;
char *name;
char *clean_name;
int parent;
int number; // for iqe export
int isbone;
int isskin;
int isrigid;
char *reason; // reason for selecting
float unscale[3]; // inverse of scaling factor in bind pose
// scratch matrices for inverse bind pose and absolute bind pose
struct aiMatrix4x4 invpose; // inv(parent * pose)
struct aiMatrix4x4 abspose; // (parent * pose)
// current pose in matrix and decomposed form
struct aiMatrix4x4 pose;
struct aiVector3D translate;
struct aiQuaternion rotate;
struct aiVector3D scale;
};
struct bone bonelist[10000];
int numbones = 0;
int find_bone(char *name)
{
int i;
for (i = 0; i < numbones; i++)
if (!strcmp(name, bonelist[i].name))
return i;
return -1;
}
char *get_base_name(char *s)
{
char *p = strrchr(s, '/');
if (!p) p = strrchr(s, '\\');
if (!p) return s;
return p + 1;
}
char *clean_node_name(char *p)
{
static char buf[200];
if (strstr(p, "node-") == p)
p += 5;
strcpy(buf, p);
for (p = buf; *p; p++) {
*p = tolower(*p);
if (*p == ' ') *p = '_';
}
return strdup(buf); // leak like a sieve
}
char *clean_material_name(char *p)
{
static char buf[200];
strcpy(buf, p);
p = strstr(buf, "-material");
if (p) *p = 0;
for (p = buf; *p; p++) {
*p = tolower(*p);
if (*p == ' ') *p = '_';
if (*p == '#') *p = '_';
}
return strdup(buf); // leak like a sieve
}
char *find_material(struct aiMaterial *material)
{
struct aiString str;
char shader[500], *p;
char *name;
int i;
for (i = 0; i < nummats; i++)
if (matlist[i].material == material)
return matlist[i].shader;
aiGetMaterialString(material, AI_MATKEY_NAME, &str);
name = str.data;
strcpy(shader, clean_material_name(name));
strcat(shader, "+");
if (!aiGetMaterialString(material, AI_MATKEY_TEXTURE_DIFFUSE(0), &str))
strcat(shader, get_base_name(str.data));
else
strcat(shader, "unknown");
p = strrchr(shader, '.');
if (p) *p = 0;
p = shader; while (*p) { *p = tolower(*p); p++; }
matlist[nummats].name = name;
matlist[nummats].material = material;
matlist[nummats].shader = strdup(shader);
return matlist[nummats++].shader;
}
// --- figure out which bones are part of armature ---
void build_bone_list_from_nodes(struct aiNode *node, int parent, char *clean_name)
{
int i;
// inherit clean names for auto-inserted nodes
if (!strstr(node->mName.data, "$ColladaAutoName$"))
clean_name = clean_node_name((char*)node->mName.data);
bonelist[numbones].name = node->mName.data;
bonelist[numbones].clean_name = clean_name;
bonelist[numbones].parent = parent;
bonelist[numbones].reason = "<none>";
bonelist[numbones].isbone = 0;
bonelist[numbones].isskin = 0;
bonelist[numbones].isrigid = 0;
bonelist[numbones].node = node;
// these are set in calc_bind_pose and/or apply_initial_frame
aiIdentityMatrix4(&bonelist[numbones].pose);
aiIdentityMatrix4(&bonelist[numbones].abspose);
aiIdentityMatrix4(&bonelist[numbones].invpose);
parent = numbones++;
for (i = 0; i < node->mNumChildren; i++)
build_bone_list_from_nodes(node->mChildren[i], parent, clean_name);
}
void apply_initial_frame(void)
{
int i;
for (i = 0; i < numbones; i++) {
// restore original transformation
bonelist[i].pose = bonelist[i].node->mTransformation;
// ... and update rotate/translate/scale
aiDecomposeMatrix(&bonelist[i].pose, &bonelist[i].scale, &bonelist[i].rotate, &bonelist[i].translate);
}
}
// recalculate abspose from local pose matrix
void calc_abs_pose(void)
{
int i;
for (i = 0; i < numbones; i++) {
bonelist[i].abspose = bonelist[i].pose;
if (bonelist[i].parent >= 0) {
bonelist[i].abspose = bonelist[bonelist[i].parent].abspose;
aiMultiplyMatrix4(&bonelist[i].abspose, &bonelist[i].pose);
}
}
}
void calc_bind_pose(void)
{
// we now (in the single mesh / non-baking case) have our bind pose
// our invpose is set to the inv_bind_pose matrix
// compute forward abspose and pose matrices here
int i;
for (i = 0; i < numbones; i++) {
if (bonelist[i].isskin) {
// skinned and boned, invpose is our reference
aiInverseMatrix(&bonelist[i].abspose, &bonelist[i].invpose);
bonelist[i].pose = bonelist[i].abspose;
if (bonelist[i].parent >= 0) {
struct aiMatrix4x4 m = bonelist[bonelist[i].parent].invpose;
aiMultiplyMatrix4(&m, &bonelist[i].pose);
bonelist[i].pose = m;
}
} else {
// not skinned, so no invpose. pose is our reference
bonelist[i].pose = bonelist[i].node->mTransformation;
bonelist[i].abspose = bonelist[i].pose;
if (bonelist[i].parent >= 0) {
bonelist[i].abspose = bonelist[bonelist[i].parent].abspose;
aiMultiplyMatrix4(&bonelist[i].abspose, &bonelist[i].pose);
}
aiInverseMatrix(&bonelist[i].invpose, &bonelist[i].abspose);
}
}
// compute translate/rotate/scale
for (i = 0; i < numbones; i++)
if (bonelist[i].isbone)
aiDecomposeMatrix(&bonelist[i].pose, &bonelist[i].scale, &bonelist[i].rotate, &bonelist[i].translate);
}
void mark_bone_parents(int i)
{
while (i >= 0) {
if (!bonelist[i].isbone) {
bonelist[i].reason = "parent";
bonelist[i].isbone = 1;
}
i = bonelist[i].parent;
}
}
void mark_tags(void)
{
int i, k;
for (k = 0; k < numtags; k++) {
for (i = 0; i < numbones; i++) {
if (!strcmp(taglist[k], bonelist[i].clean_name)) {
fprintf(stderr, "marking tag %s\n", taglist[k]);
bonelist[i].reason = "tagged";
bonelist[i].isbone = 1;
break;
}
}
}
}
void unmark_tags(void)
{
int i, k;
for (k = 0; k < numuntags; k++) {
for (i = 0; i < numbones; i++) {
if (!strcmp(untaglist[k], bonelist[i].clean_name)) {
fprintf(stderr, "unmarking tag %s\n", untaglist[k]);
bonelist[i].reason = "untagged";
bonelist[i].isbone = 0;
break;
}
}
}
}
void mark_skinned_bones(const struct aiScene *scene)
{
int i, k, a, b;
for (i = 0; i < numbones; i++) {
struct aiNode *node = bonelist[i].node;
if (only_one_node && strcmp(bonelist[i].clean_name, only_one_node))
continue;
for (k = 0; k < node->mNumMeshes; k++) {
struct aiMesh *mesh = scene->mMeshes[node->mMeshes[k]];
for (a = 0; a < mesh->mNumBones; a++) {
b = find_bone(mesh->mBones[a]->mName.data);
if (!bonelist[b].isbone) {
bonelist[b].reason = "skinned";
bonelist[b].invpose = mesh->mBones[a]->mOffsetMatrix;
bonelist[b].isbone = 1;
bonelist[b].isskin = 1;
} else if (!need_to_bake_skin) {
if (memcmp(&bonelist[b].invpose, &mesh->mBones[a]->mOffsetMatrix, sizeof bonelist[b].invpose))
need_to_bake_skin = 1;
}
}
}
}
}
void mark_animated_bones(const struct aiScene *scene)
{
int i, k, b;
for (i = 0; i < scene->mNumAnimations; i++) {
const struct aiAnimation *anim = scene->mAnimations[i];
for (k = 0; k < anim->mNumChannels; k++) {
b = find_bone(anim->mChannels[k]->mNodeName.data);
bonelist[b].reason = "animated";
bonelist[b].isbone = 1;
}
}
}
void mark_rigid_bones(const struct aiScene *scene)
{
int i, k;
for (i = 0; i < numbones; i++) {
struct aiNode *node = bonelist[i].node;
for (k = 0; k < node->mNumMeshes; k++) {
struct aiMesh *mesh = scene->mMeshes[node->mMeshes[k]];
if (mesh->mNumBones == 0 && !is_identity_matrix(&node->mTransformation)) {
bonelist[i].isrigid = 1;
}
}
if (bonelist[i].isrigid) {
bonelist[i].reason = "rigid";
bonelist[i].isbone = 1;
}
}
}
int build_bone_list(const struct aiScene *scene)
{
int number;
int i;
build_bone_list_from_nodes(scene->mRootNode, -1, "SCENE");
if (dohips) fix_hips(0);
// we always need the bind pose
if (doanim || domesh || dorigid)
mark_skinned_bones(scene);
if (doanim || save_all_bones)
mark_animated_bones(scene);
if (dorigid)
mark_rigid_bones(scene);
mark_tags(); // mark special bones named on command line as "tags" to attach stuff
unmark_tags(); // remove named bones from list
// select all parents of selected bones
for (i = 0; i < numbones; i++) {
if (bonelist[i].isbone)
mark_bone_parents(i);
}
// select all otherwise 'dead' children of selected bones
if (save_all_bones) {
for (i = 0; i < numbones; i++) {
if (!bonelist[i].isbone)
if (bonelist[i].parent >= 0 && bonelist[bonelist[i].parent].isbone)
bonelist[i].isbone = 1;
}
}
if (save_all_bones > 1) {
for (i = 0; i < numbones; i++) {
bonelist[i].reason = "useless";
bonelist[i].isbone = 1;
}
}
// skip root node if it has 1 child and identity transform
int count = 0;
for (i = 0; i < numbones; i++)
if (bonelist[i].isbone && bonelist[i].parent == 0)
count++;
if (count == 1 && is_identity_matrix(&bonelist[0].node->mTransformation)) {
bonelist[0].reason = "useless root node";
bonelist[0].isbone = 0;
bonelist[0].number = -1;
}
if (verbose)
for (i = 0; i < numbones; i++)
if (bonelist[i].isbone)
fprintf(stderr, "selecting %s bone %s\n", bonelist[i].reason, bonelist[i].clean_name);
// assign IQE numbers to bones
number = 0;
for (i = 0; i < numbones; i++)
if (bonelist[i].isbone)
bonelist[i].number = number++;
if (dohips) unfix_hips();
calc_bind_pose();
return number;
}
// --- export poses and animation frames ---
void export_pm(FILE *out, int i)
{
struct aiMatrix4x4 m = bonelist[i].pose;
fprintf(out, "pm %.9g %.9g %.9g %.9g %.9g %.9g %.9g %.9g %.9g %.9g %.9g %.9g\n",
KILL(m.a4), KILL(m.b4), KILL(m.c4),
(m.a1), (m.a2), (m.a3),
(m.b1), (m.b2), (m.b3),
(m.c1), (m.c2), (m.c3));
}
void export_pq(FILE *out, int i)
{
struct aiQuaternion rotate = bonelist[i].rotate;
struct aiVector3D scale = bonelist[i].scale;
struct aiVector3D translate = bonelist[i].translate;
if (dolowprec) {
if (KILL(scale.x) == 1 && KILL(scale.y) == 1 && KILL(scale.z) == 1)
fprintf(out, "pq %.9g %.9g %.9g %.9g %.9g %.9g %.9g\n",
LOWP(translate.x), LOWP(translate.y), LOWP(translate.z),
LOWP(rotate.x), LOWP(rotate.y), LOWP(rotate.z), LOWP(rotate.w));
else
fprintf(out, "pq %.9g %.9g %.9g %.9g %.9g %.9g %.9g %.9g %.9g %.9g\n",
LOWP(translate.x), LOWP(translate.y), LOWP(translate.z),
LOWP(rotate.x), LOWP(rotate.y), LOWP(rotate.z), LOWP(rotate.w),
LOWP(scale.x), LOWP(scale.y), LOWP(scale.z));
} else {
if (KILL(scale.x) == 1 && KILL(scale.y) == 1 && KILL(scale.z) == 1)
fprintf(out, "pq %.9g %.9g %.9g %.9g %.9g %.9g %.9g\n",
KILL(translate.x), KILL(translate.y), KILL(translate.z),
(rotate.x), (rotate.y), (rotate.z), (rotate.w));
else
fprintf(out, "pq %.9g %.9g %.9g %.9g %.9g %.9g %.9g %.9g %.9g %.9g\n",
KILL(translate.x), KILL(translate.y), KILL(translate.z),
(rotate.x), (rotate.y), (rotate.z), (rotate.w),
KILL(scale.x), KILL(scale.y), KILL(scale.z));
}
}
int saved_parents[1000];
struct {
char *name; char *parent; int parent_id;
} hiplist[] = {
{ "bip01_l_thigh", "bip01_pelvis", -1 },
{ "bip01_r_thigh", "bip01_pelvis", -1 },
// { "bip01_l_foot", "bip01_l_calf", -1 },
// { "bip01_r_foot", "bip01_r_calf", -1 },
{ NULL, NULL, 0 }
};
int fix_hips(int verbose)
{
int i, k, p, fixed = 0;
for (k = 0; hiplist[k].parent; k++)
hiplist[k].parent_id = -1;
for (i = 0; i < numbones; i++) {
saved_parents[i] = bonelist[i].parent;
for (k = 0; hiplist[k].parent; k++)
if (!strcmp(bonelist[i].clean_name, hiplist[k].parent))
hiplist[k].parent_id = i;
p = bonelist[i].parent;
for (k = 0; hiplist[k].parent; k++) {
if (!strcmp(bonelist[i].clean_name, hiplist[k].name)) {
if (p >= 0 && strcmp(bonelist[p].clean_name, hiplist[k].parent)) {
if (verbose)
fprintf(stderr, "fixing %s -> %s (was connected to %s)\n",
bonelist[i].clean_name,
bonelist[hiplist[k].parent_id].clean_name,
bonelist[p].clean_name);
fixed = 1;
bonelist[i].parent = hiplist[k].parent_id;
}
}
}
}
return fixed;
}
void unfix_hips(void)
{
int i;
for (i = 0; i < numbones; i++)
bonelist[i].parent = saved_parents[i];
}
void fix_pose(void)
{
int i;
calc_abs_pose();
if (dohips) fix_hips(0);
for (i = 0; i < numbones; i++) {
if (bonelist[i].isbone) {
// remove scaling factor in absolute pose
if (dounscale < 0) {
struct aiVector3D apos, ascale;
struct aiQuaternion arot;
aiDecomposeMatrix(&bonelist[i].abspose, &ascale, &arot, &apos);
bonelist[i].unscale[0] = ascale.x;
bonelist[i].unscale[1] = ascale.y;
bonelist[i].unscale[2] = ascale.z;
if (KILL(ascale.x) != 1 || KILL(ascale.y) != 1 || KILL(ascale.z) != 1)
fprintf(stderr, "unscaling %s: %g %g %g\n", bonelist[i].name, ascale.x, ascale.y, ascale.z);
}
if (dounscale) {
float x = bonelist[i].unscale[0];
float y = bonelist[i].unscale[1];
float z = bonelist[i].unscale[2];
if (KILL(x) != 1 || KILL(y) != 1 || KILL(z) != 1) {
bonelist[i].abspose.a1 /= x; bonelist[i].abspose.b1 /= x; bonelist[i].abspose.c1 /= x;
bonelist[i].abspose.a2 /= y; bonelist[i].abspose.b2 /= y; bonelist[i].abspose.c2 /= y;
bonelist[i].abspose.a3 /= z; bonelist[i].abspose.b3 /= z; bonelist[i].abspose.c3 /= z;
}
}
// flip axis in absolute pose
if (doaxis)
aiMultiplyMatrix4(&bonelist[i].abspose, &axis_x_to_y);
// ...and invert so we can recalculate the local poses
aiInverseMatrix(&bonelist[i].invpose, &bonelist[i].abspose);
// ...and recalculate the local pose
bonelist[i].pose = bonelist[i].abspose;
if (bonelist[i].parent >= 0) {
struct aiMatrix4x4 m = bonelist[bonelist[i].parent].invpose;
aiMultiplyMatrix4(&m, &bonelist[i].pose);
bonelist[i].pose = m;
}
// ...and make sure we have it in decomposed form
aiDecomposeMatrix(&bonelist[i].pose, &bonelist[i].scale, &bonelist[i].rotate, &bonelist[i].translate);
}
}
if (dohips) unfix_hips();
}
void export_pose(FILE *out)
{
int i;
if (doaxis || dounscale || dohips)
fix_pose();
for (i = 0; i < numbones; i++)
if (bonelist[i].isbone)
//export_pm(out, i);
export_pq(out, i);
}
void export_bone_list(FILE *out)
{
int i, n;
for (n = i = 0; i < numbones; i++) if (bonelist[i].isbone) n++;
if (dounscale) fprintf(stderr, "removing scaling factors from bind pose\n");
if (doaxis) fprintf(stderr, "flipping bone axis from x to y\n");
if (dohips) {
fprintf(stderr, "patching skeleton hierarchy\n");
dohips = fix_hips(1);
}
fprintf(stderr, "exporting skeleton: %d bones\n", n);
fprintf(out, "\n");
for (i = 0; i < numbones; i++) {
if (bonelist[i].isbone) {
if (bonelist[i].parent >= 0)
fprintf(out, "joint \"%s\" %d\n",
bonelist[i].clean_name,
bonelist[bonelist[i].parent].number);
else
fprintf(out, "joint \"%s\" -1\n", bonelist[i].clean_name);
}
}
if (dohips)
unfix_hips();
fprintf(out, "\n");
if (dounscale) dounscale = -1;
export_pose(out);
if (dounscale) dounscale = 1;
}
void export_static_animation(FILE *out, const struct aiScene *scene)
{
fprintf(stderr, "exporting animation: static rest pose\n");
fprintf(out, "\n");
fprintf(out, "\nanimation \"%s\"\n", basename);
fprintf(out, "framerate 30\n");
fprintf(out, "frame\n");
apply_initial_frame();
export_pose(out);
}
int animation_length(const struct aiAnimation *anim)
{
int i, len = 0;
for (i = 0; i < anim->mNumChannels; i++) {
struct aiNodeAnim *chan = anim->mChannels[i];
if (chan->mNumPositionKeys > len) len = chan->mNumPositionKeys;
if (chan->mNumRotationKeys > len) len = chan->mNumRotationKeys;
if (chan->mNumScalingKeys > len) len = chan->mNumScalingKeys;
}
return len;
}
void export_frame(FILE *out, const struct aiAnimation *anim, int frame)
{
int i;
// start with fresh matrices
apply_initial_frame();
for (i = 0; i < anim->mNumChannels; i++) {
struct aiNodeAnim *chan = anim->mChannels[i];
int a = find_bone(chan->mNodeName.data);
int tframe = MIN(frame, chan->mNumPositionKeys - 1);
int rframe = MIN(frame, chan->mNumRotationKeys - 1);
int sframe = MIN(frame, chan->mNumScalingKeys - 1);
bonelist[a].translate = chan->mPositionKeys[tframe].mValue;
bonelist[a].rotate = chan->mRotationKeys[rframe].mValue;
bonelist[a].scale = chan->mScalingKeys[sframe].mValue;
#ifdef HACK_MATRIX_KEY
bonelist[a].pose = chan->mRotationKeys[rframe].mMatrixValue;
#endif
}
#ifndef HACK_MATRIX_KEY
// translate/rotate/scale have changed: recompute pose
for (i = 0; i < numbones; i++) {
if (bonelist[i].isbone) {
// make sure we're not hit by precision issues in decomposematrix
aiNormalizeQuaternion(&bonelist[i].rotate);
aiComposeMatrix(&bonelist[i].pose, &bonelist[i].scale, &bonelist[i].rotate, &bonelist[i].translate);
}
}
#endif
fprintf(out, "\n");
fprintf(out, "frame %d\n", frame);
export_pose(out);
}
void export_animations(FILE *out, const struct aiScene *scene)
{
int i, k, len;
for (i = 0; i < scene->mNumAnimations; i++) {
const struct aiAnimation *anim = scene->mAnimations[i];
if (scene->mNumAnimations > 1)
fprintf(out, "\nanimation \"%s,%02d\"\n", basename, i);
else
fprintf(out, "\nanimation \"%s\"\n", basename);
fprintf(out, "framerate 30\n");
len = animation_length(anim);
fprintf(stderr, "exporting animation %d: %d frames\n", i+1, len);
for (k = 0; k < len; k++)
export_frame(out, anim, k);
}
if (scene->mNumAnimations == 0)
export_static_animation(out, scene);
}
/*
* For multi-mesh models, sometimes each mesh has its own inv_bind_matrix set
* for each bone. To export to IQE we must have only one inv_bind_matrix per
* bone. We can bake the mesh by animating it to the initial frame.
* Once this is done, set the inv_bind_matrix to be the inverse of the forward
* bind_matrix of this pose.
*/
void bake_mesh_skin(const struct aiMesh *mesh)
{
int i, k, b;
struct aiMatrix3x3 mat3;
struct aiMatrix4x4 bonemat[1000], mat;
struct aiVector3D *outpos, *outnorm;
if (mesh->mNumBones == 0)
return;
outpos = malloc(mesh->mNumVertices * sizeof *outpos);
outnorm = malloc(mesh->mNumVertices * sizeof *outnorm);
memset(outpos, 0, mesh->mNumVertices * sizeof *outpos);
memset(outnorm, 0, mesh->mNumVertices * sizeof *outpos);
calc_abs_pose();
for (i = 0; i < mesh->mNumBones; i++) {
b = find_bone(mesh->mBones[i]->mName.data);
bonemat[i] = bonelist[b].abspose;
aiMultiplyMatrix4(&bonemat[i], &mesh->mBones[i]->mOffsetMatrix);
}
for (k = 0; k < mesh->mNumBones; k++) {
struct aiBone *bone = mesh->mBones[k];
b = find_bone(mesh->mBones[k]->mName.data);
mat = bonemat[k];
mat3.a1 = mat.a1; mat3.a2 = mat.a2; mat3.a3 = mat.a3;
mat3.b1 = mat.b1; mat3.b2 = mat.b2; mat3.b3 = mat.b3;
mat3.c1 = mat.c1; mat3.c2 = mat.c2; mat3.c3 = mat.c3;
for (i = 0; i < bone->mNumWeights; i++) {
struct aiVertexWeight vw = bone->mWeights[i];
int v = vw.mVertexId;
float w = vw.mWeight;
struct aiVector3D srcpos = mesh->mVertices[v];
struct aiVector3D srcnorm = mesh->mNormals[v];
aiTransformVecByMatrix4(&srcpos, &mat);
aiTransformVecByMatrix3(&srcnorm, &mat3);
outpos[v].x += srcpos.x * w;
outpos[v].y += srcpos.y * w;
outpos[v].z += srcpos.z * w;
outnorm[v].x += srcnorm.x * w;
outnorm[v].y += srcnorm.y * w;
outnorm[v].z += srcnorm.z * w;
}
}
memcpy(mesh->mVertices, outpos, mesh->mNumVertices * sizeof *outpos);
memcpy(mesh->mNormals, outnorm, mesh->mNumVertices * sizeof *outnorm);
free(outpos);
free(outnorm);
}
void bake_scene_skin(const struct aiScene *scene)
{
int i;
fprintf(stderr, "baking skin to recreate base pose in multi-mesh model\n");
for (i = 0; i < scene->mNumMeshes; i++)
bake_mesh_skin(scene->mMeshes[i]);
}
void export_custom_vertexarrays(FILE *out, const struct aiScene *scene)
{
int i, t, first = 1;
int seen[10] = {0};
for (i = 0; i < scene->mNumMeshes; i++) {
struct aiMesh *mesh = scene->mMeshes[i];
for (t = 1; t < MAX_UVMAP; t++) {
int custom = FIRST_UVMAP + t - 1;
if (mesh->mTextureCoords[t]) {
if (!seen[custom]) {
if (first) { fprintf(out, "\n"); first = 0; }
fprintf(out, "vertexarray custom%d float 2 \"uvmap.%d\"\n", custom, t);
seen[custom] = 1;
}
}
}
for (t = 1; t < MAX_COL; t++) {
int custom = FIRST_COL + t - 1;
if (mesh->mColors[t]) {
if (!seen[custom]) {
if (first) { fprintf(out, "\n"); first = 0; }
fprintf(out, "vertexarray custom%d ubyte 4 \"color.%d\"\n", custom, t);
seen[custom] = 1;
}
}
}
}
}
/*
* Export meshes. Group them by materials. Also apply the node transform
* to the vertices. IQE does not have a concept of per-group transforms.
*
* If we are exporting a rigged model, we have to skip any meshes which
* are not deformed by the armature. If we are exporting a non-rigged model,
* we have to pre-transform all meshes.
*
* TODO: turn non-rigged meshes into rigged meshes by hooking them up to
* a synthesized bone for its node.
*/
void export_node(FILE *out, const struct aiScene *scene, const struct aiNode *node,
struct aiMatrix4x4 mat, char *clean_name)
{
struct aiMatrix3x3 mat3;
int i, a, k, t;
aiMultiplyMatrix4(&mat, &node->mTransformation);
mat3.a1 = mat.a1; mat3.a2 = mat.a2; mat3.a3 = mat.a3;
mat3.b1 = mat.b1; mat3.b2 = mat.b2; mat3.b3 = mat.b3;
mat3.c1 = mat.c1; mat3.c2 = mat.c2; mat3.c3 = mat.c3;
if (!strstr(node->mName.data, "$ColladaAutoName$"))
clean_name = clean_node_name((char*)node->mName.data);
if (only_one_node && strcmp(clean_name, only_one_node))
goto skip_mesh;
for (i = 0; i < node->mNumMeshes; i++) {
struct aiMesh *mesh = scene->mMeshes[node->mMeshes[i]];
struct aiMaterial *material = scene->mMaterials[mesh->mMaterialIndex];
if (mesh->mNumBones == 0 && dobone && !dorigid) {
if (verbose)
fprintf(stderr, "skipping rigid mesh %d in node %s (no bones)\n", i, clean_name);
continue;
}
fprintf(stderr, "exporting mesh %s[%d]: %d vertices, %d faces\n",
clean_name, i, mesh->mNumVertices, mesh->mNumFaces);
fprintf(out, "\n");
fprintf(out, "mesh \"%s\"\n", clean_name);
fprintf(out, "material \"%s\"\n", find_material(material));
struct vb *vb = (struct vb*) malloc(mesh->mNumVertices * sizeof(*vb));
memset(vb, 0, mesh->mNumVertices * sizeof(*vb));
// A rigidly animated node -- insert fake blend index/weights
if (mesh->mNumBones == 0 && dobone) {
a = find_bone((char*)node->mName.data);
if (verbose)
fprintf(stderr, "\trigid bone %d for mesh in node %s (no bones)\n", bonelist[a].number, node->mName.data);
for (k = 0; k < mesh->mNumVertices; k++) {
vb[k].b[0] = bonelist[a].number;
vb[k].w[0] = 1;
vb[k].n = 1;
}
}
// Assemble blend index/weight array
for (k = 0; k < mesh->mNumBones; k++) {
struct aiBone *bone = mesh->mBones[k];
a = find_bone(bone->mName.data);
for (t = 0; t < bone->mNumWeights; t++) {
struct aiVertexWeight *w = mesh->mBones[k]->mWeights + t;
int idx = w->mVertexId;
if (vb[idx].n < MAXBLEND) {
vb[idx].b[vb[idx].n] = bonelist[a].number;
vb[idx].w[vb[idx].n] = w->mWeight;