forked from raduprv/Eternal-Lands
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calstripper.c
273 lines (230 loc) · 8.03 KB
/
calstripper.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
#include <stdio.h>
#include <string.h>
#include "calstripper.h"
#define SQ(a) ((a)*(a))
#define EPSILON 0.0001
int is_frame_equal(cafkeyframe *a, cafkeyframe *b){
float norm;
norm= SQ(a->tx-b->tx)+SQ(a->ty-b->ty)+SQ(a->tz-b->tz)+
SQ(a->rx-b->rx)+SQ(a->ry-b->ry)+SQ(a->rz-b->rz)+SQ(a->rw-b->rw);
return (norm<EPSILON);
}
int is_still(caftrack *tr){
int i,b=1;
for(i=1;i<tr->keyframes;i++)
b&=is_frame_equal(&tr->frames[0],&tr->frames[i]);
return b;
}
int blend(caftrack *tr, caftrack *fr, cafanim *bl){
int kf;
int bltr=-1;
if (!bl) return 0;
//search bone in bl
for(kf=0;kf<bl->header.tracks;kf++)
if (bl->tracks[kf].bone==tr->bone) {bltr=kf; break;}
if(bltr==-1) return 0;
for(kf=1;kf<tr->keyframes;kf++){
tr->frames[kf].rx=bl->tracks[bltr].frames[0].rx+(fr->frames[kf].rx-fr->frames[kf-1].rx);
tr->frames[kf].ry=bl->tracks[bltr].frames[0].ry+(fr->frames[kf].ry-fr->frames[kf-1].ry);
tr->frames[kf].rz=bl->tracks[bltr].frames[0].rz+(fr->frames[kf].rz-fr->frames[kf-1].rz);
tr->frames[kf].rw=bl->tracks[bltr].frames[0].rw+(fr->frames[kf].rw-fr->frames[kf-1].rw);
tr->frames[kf].tx=bl->tracks[bltr].frames[0].tx+(fr->frames[kf].tx-fr->frames[kf-1].tx);
tr->frames[kf].ty=bl->tracks[bltr].frames[0].ty+(fr->frames[kf].ty-fr->frames[kf-1].ty);
tr->frames[kf].tz=bl->tracks[bltr].frames[0].tz+(fr->frames[kf].tz-fr->frames[kf-1].tz);
}
//assuming frame[0].time is 0
memcpy(&tr->frames[0],&bl->tracks[bltr].frames[0],sizeof(cafkeyframe));
return 1;
}
int hardblend(caftrack *tr, caftrack *fr, cafanim *bl){
int kf;
int bltr=-1;
float time;
if (!bl) return 0;
//search bone in bl
for(kf=0;kf<bl->header.tracks;kf++)
if (bl->tracks[kf].bone==tr->bone) {bltr=kf; break;}
if(bltr==-1) return 0;
for(kf=0;kf<tr->keyframes;kf++){
time=fr->frames[kf].time;
if(kf==0||kf==tr->keyframes-1) tr->frames[kf]=bl->tracks[bltr].frames[0];
else tr->frames[kf]=fr->frames[kf];
tr->frames[kf].time=time;
}
//assuming frame[0].time is 0
return 1;
}
void striptracks(cafanim *from, cafanim *to, csfdata *csf, cafanim *bl){
int i,ctr=0,trs,tbl=0;
trs=from->header.tracks;
printf("Choose bone processing [enter or kbfs+enter]\n");
printf("---------------------------------------------------\n");
printf("BONE MOVING ACTION\n");
printf("---------------------------------------------------\n");
for(i=0;i<from->header.tracks;i++){
char cmd[32];
int st = is_still(&from->tracks[i]);
printf("%-15s %4s %sKeep/Blend/Force/%sStrip -->",csf->bones[from->tracks[i].bone].name,
(st) ? ("N"):("Y"),(st) ? (""):("*"),(!st) ? (""):("*"));
fflush(stdout);
fgets(cmd,32,stdin);
if(cmd[0]=='\n') cmd[0] = (st) ? ('s'):('k');
switch(cmd[0]){
case 'K':
case 'k':
memcpy(&to->tracks[ctr],&from->tracks[i],sizeof(caftrack));
ctr++;
break;
case 'B':
case 'b':
if(!bl) {i--; continue;}
memcpy(&to->tracks[ctr],&from->tracks[i],sizeof(caftrack));
if(!blend(&to->tracks[ctr],&from->tracks[i],bl)) {
printf("\nNot Blended, wrong bones, kept\n");
ctr++;
break;
}
ctr++;
tbl++;
break;
case 'F':
case 'f':
if(!bl) {i--; continue;}
memcpy(&to->tracks[ctr],&from->tracks[i],sizeof(caftrack));
if(!hardblend(&to->tracks[ctr],&from->tracks[i],bl)) {
printf("\nNot Blended, wrong bones, kept\n");
ctr++;
break;
}
ctr++;
tbl++;
break;
case 'S':
case 's':
trs--;
break;
default:
i--;
continue;
}
}
memcpy(&to->header,&from->header,sizeof(cafheader));
to->header.tracks=trs;
printf("\n\nTracks Stripped: %i\n",from->header.tracks-to->header.tracks);
printf("Tracks Blended: %2i\n",tbl);
printf("Tracks Kept: %4i\n",to->header.tracks);
}
//CSF FUNCS
int read_csf(FILE *fc, csfdata *csf){
int cb,ck;
//read header
fread(&csf->header,sizeof(csfheader),1,fc);
if(csf->header.magic[0]!='C'
||csf->header.magic[1]!='S'||csf->header.magic[2]!='F'
||csf->header.magic[3]!='\0') {
printf("\nError in csf header %i %c %c %c %i\n",csf->header.version,csf->header.magic[0],csf->header.magic[1],csf->header.magic[2],csf->header.magic[3]);
return 0;
} else printf("Csf Version: %i\n",csf->header.version);
//read bones
for(cb=0;cb<csf->header.bones;cb++){
fread(&csf->bones[cb].name_len,sizeof(csf->bones[cb].name_len),1,fc);
fread(&csf->bones[cb].name,1,csf->bones[cb].name_len,fc);
fread(&csf->bones[cb].data,sizeof(csf->bones[cb].data),1,fc);
fread(&csf->bones[cb].children,sizeof(int32_t),csf->bones[cb].data.num_children,fc);
}
return 1;
}
void print_bone_table(csfdata *csf){
int cb;
printf("\nBONES\n------------------------------\n");
printf(" NAME ID \n");
printf("------------------------------\n");
for(cb=0;cb<csf->header.bones;cb++){
printf("%-22s %2i\n",csf->bones[cb].name, cb);
}
printf("------------------------------\n");
}
//CAF FUNCS
int read_caf(FILE *fc, cafanim *caf){
int ct,ck;
//read header
fread(&caf->header,sizeof(cafheader),1,fc);
if(caf->header.magic[0]!='C'
||caf->header.magic[1]!='A'||caf->header.magic[2]!='F'
||caf->header.magic[3]!='\0') {
printf("\nError in caf header %i %c %c %c %i\n",caf->header.version,caf->header.magic[0],caf->header.magic[1],caf->header.magic[2],caf->header.magic[3]);
return 0;
} else printf("Caf Version: %i\n",caf->header.version);
//read tracks
for(ct=0;ct<caf->header.tracks;ct++){
fread(&caf->tracks[ct].bone,sizeof(caf->tracks[ct].bone),1,fc);
fread(&caf->tracks[ct].keyframes,sizeof(caf->tracks[ct].keyframes),1,fc);
for(ck=0;ck<caf->tracks[ct].keyframes;ck++){
fread(&caf->tracks[ct].frames[ck],sizeof(cafkeyframe),1,fc);
}
}
return 1;
}
int write_caf(FILE *fo, cafanim *caf){
int ct,ck;
//write header
fwrite(&caf->header,sizeof(cafheader),1,fo);
//write tracks
for(ct=0;ct<caf->header.tracks;ct++){
fwrite(&caf->tracks[ct].bone,sizeof(caf->tracks[ct].bone),1,fo);
fwrite(&caf->tracks[ct].keyframes,sizeof(caf->tracks[ct].keyframes),1,fo);
for(ck=0;ck<caf->tracks[ct].keyframes;ck++){
fwrite(&caf->tracks[ct].frames[ck],sizeof(cafkeyframe),1,fo);
}
}
return 1;
}
void helptext(){
printf("\nEternal Lands cal3d animation tool\n");
printf("\nUsage: calstripper source.caf dest.caf source.csf [blend.caf]\n");
printf("------------------------------------------------------------\n");
printf("For each bone in source.caf you can choose to:\n");
printf(" - (K)eep the source keyframes (default if bone is moving)\n");
printf(" - (S)trip the source keyframes (default if bone is not moving)\n");
printf(" - (B)lend the source keyframes with blend.caf (if specified).\n");
printf(" By blending you keep the bone in blend.caf and add the rotations\n");
printf(" and traslations of the corresponding bone in source.caf\n");
printf(" - (F)orce blending. The first and last source keyframes are\n");
printf(" substituted with the corresponding ones in blend.caf\n");
printf("\nOutput is written in dest.caf\n");
printf("source.csf is the skeleton file, needed to get bone names\n\n");
}
int main (int argc, char *argv[]) {
FILE *fc,*fo,*fs,*fb;
//static here is to prevent stack overflow in windows (-_-')
static cafanim caf_in,caf_to,caf_blend;
static csfdata csf;
if (argc<4) {
helptext();
return 0;
}
printf("Reading Skeleton...");
fs = fopen(argv[3],"rb");
if(!fs) { printf("\nCan't open %s\n",argv[3]); return 0;}
if(!read_csf(fs,&csf)) return 0;
fclose(fs);
if(argc>=5){
printf("Reading Blending Animation...");
fb = fopen(argv[4],"rb");
if(!fb) { printf("\nCan't open %s\n",argv[4]); return 0;}
if(!read_caf(fb,&caf_blend)) return 0;
fclose(fb);
} else printf("No Blending Animation given...\n");
printf("Reading Source Animation...");
fc = fopen(argv[1],"rb");
if(!fc) { printf("\nCan't open %s\n",argv[1]); return 0;}
if(!read_caf(fc,&caf_in)) return 0;
fclose(fc);
print_bone_table(&csf);
striptracks(&caf_in,&caf_to,&csf,(argc>=5) ? (&caf_blend):(NULL));
fo = fopen(argv[2],"wb");
if(!fo) { printf("\nCan't open %s\n",argv[2]); return 0;}
if(!write_caf(fo,&caf_to)) return 0;
fclose(fo);
return 0;
}