-
Notifications
You must be signed in to change notification settings - Fork 0
/
dpmodel.c
2673 lines (2448 loc) · 67.2 KB
/
dpmodel.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
// converter for .smd files (and a .txt script) to .dpm
// written by Ashley Rose Hale (LadyHavoc) but placed into public domain
//
// disclaimer: Ashley Rose Hale (LadyHavoc) is not not responsible if this code blinds you with
// its horrible design, sets your house on fire, makes you cry,
// or anything else - use at your own risk.
//
// Yes, this is perhaps my second worst code ever (next to zmodel).
// Thanks to Jalisk0 for the HalfLife2 .SMD bone weighting support
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#ifndef M_PI
#define M_PI 3.1415926535
#endif
#if _MSC_VER
#pragma warning (disable : 4244)
#define strcasecmp _stricmp
#endif
#define MAX_FILEPATH 1024
#define MAX_NAME 32
#define MAX_FRAMES 65536
#define MAX_TRIS 65536
#define MAX_VERTS (MAX_TRIS * 3)
#define MAX_BONES 256
#define MAX_SHADERS 256
#define MAX_FILESIZE (64*1024*1024)
#define MAX_INFLUENCES 16
#define MAX_ATTACHMENTS MAX_BONES
// model format related flags
#define DPMBONEFLAG_ATTACH 1
#if 1
#define EPSILON_VERTEX 0.1
#define EPSILON_NORMAL 0.001
#define EPSILON_TEXCOORD 0.0001
#else
#define EPSILON_VERTEX 0
#define EPSILON_NORMAL 0
#define EPSILON_TEXCOORD 0
#endif
static char texturedir_name[MAX_FILEPATH];
static char outputdir_name[MAX_FILEPATH];
static char model_name[MAX_FILEPATH];
static char scene_name[MAX_FILEPATH];
static char model_name_uppercase[MAX_FILEPATH];
static char scene_name_uppercase[MAX_FILEPATH];
static char model_name_lowercase[MAX_FILEPATH];
static char scene_name_lowercase[MAX_FILEPATH];
static FILE *headerfile = NULL;
static FILE *qcheaderfile = NULL;
static FILE *qhheaderfile = NULL;
static FILE *animinfofile = NULL;
static FILE *framegroupsfile = NULL;
static double modelorigin[3] = {0, 0, 0}, modelrotate[3] = {0, 0, 0}, modelscale[3] = {1, 1, 1};
static int modelinvert = 0;
static double sceneframerate = 10;
static int sceneloop = 0;
static int sceneframegroups = 0;
// this makes it keep all bones, not removing unused ones (as they might be used for attachments)
static int keepallbones = 1;
void stringtouppercase(char *in, char *out)
{
// cleanup name
while (*in)
{
*out = *in++;
// force lowercase
if (*out >= 'a' && *out <= 'z')
*out += 'A' - 'a';
out++;
}
*out++ = 0;
}
void stringtolowercase(char *in, char *out)
{
// cleanup name
while (*in)
{
*out = *in++;
// force lowercase
if (*out >= 'A' && *out <= 'Z')
*out += 'a' - 'A';
out++;
}
*out++ = 0;
}
void cleancopyname(char *out, char *in, int size)
{
char *end = out + size - 1;
// cleanup name
while (out < end)
{
*out = *in++;
if (!*out)
break;
// force lowercase
//if (*out >= 'A' && *out <= 'Z')
// *out += 'a' - 'A';
// convert backslash to slash
if (*out == '\\')
*out = '/';
out++;
}
end++;
while (out < end)
*out++ = 0; // pad with nulls
}
void chopextension(char *text)
{
char *temp;
if (!*text)
return;
temp = text;
while (*temp)
{
if (*temp == '\\')
*temp = '/';
temp++;
}
temp = text + strlen(text) - 1;
while (temp >= text)
{
if (*temp == '.') // found an extension
{
// clear extension
*temp++ = 0;
while (*temp)
*temp++ = 0;
break;
}
if (*temp == '/') // no extension but hit path
break;
temp--;
}
}
void makepath(char *text)
{
char *temp;
if (!*text)
return;
temp = text;
while (*temp)
{
if (*temp == '\\')
*temp = '/';
temp++;
}
temp = text + strlen(text) - 1;
while (temp >= text)
{
if (*temp == '/') // found path
{
// clear filename
temp++;
while (*temp)
*temp++ = 0;
break;
}
temp--;
}
}
// LadyHavoc: taken from DarkPlaces
typedef enum qboolean_e {false = 0, true = 1} qboolean;
#define MAX_TOKEN_CHARS 1024
char com_token[MAX_TOKEN_CHARS];
int COM_ParseToken(const char **datapointer, int returnnewline)
{
int len;
const char *data = *datapointer;
len = 0;
com_token[0] = 0;
if (!data)
{
*datapointer = NULL;
return false;
}
// skip whitespace
skipwhite:
// line endings:
// UNIX: \n
// Mac: \r
// Windows: \r\n
for (;*data <= ' ' && ((*data != '\n' && *data != '\r') || !returnnewline);data++)
{
if (*data == 0)
{
// end of file
*datapointer = NULL;
return false;
}
}
// handle Windows line ending
if (data[0] == '\r' && data[1] == '\n')
data++;
if (data[0] == '/' && data[1] == '/')
{
// comment
while (*data && *data != '\n' && *data != '\r')
data++;
goto skipwhite;
}
else if (data[0] == '/' && data[1] == '*')
{
// comment
data++;
while (*data && (data[0] != '*' || data[1] != '/'))
data++;
data += 2;
goto skipwhite;
}
else if (*data == '\"')
{
// quoted string
for (data++;*data != '\"';data++)
{
if (*data == '\\' && data[1] == '"' )
data++;
if (!*data || len >= (int)sizeof(com_token) - 1)
{
com_token[0] = 0;
*datapointer = NULL;
return false;
}
com_token[len++] = *data;
}
com_token[len] = 0;
*datapointer = data+1;
return true;
}
else if (*data == '\'')
{
// quoted string
for (data++;*data != '\'';data++)
{
if (*data == '\\' && data[1] == '\'' )
data++;
if (!*data || len >= (int)sizeof(com_token) - 1)
{
com_token[0] = 0;
*datapointer = NULL;
return false;
}
com_token[len++] = *data;
}
com_token[len] = 0;
*datapointer = data+1;
return true;
}
else if (*data == '\r')
{
// translate Mac line ending to UNIX
com_token[len++] = '\n';
com_token[len] = 0;
*datapointer = data;
return true;
}
else if (*data == '\n' || *data == '{' || *data == '}' || *data == ')' || *data == '(' || *data == ']' || *data == '[' || *data == '\'' || *data == ':' || *data == ',' || *data == ';')
{
// single character
com_token[len++] = *data++;
com_token[len] = 0;
*datapointer = data;
return true;
}
else
{
// regular word
for (;*data > ' ' && *data != '{' && *data != '}' && *data != ')' && *data != '(' && *data != ']' && *data != '[' && *data != '\'' && *data != ':' && *data != ',' && *data != ';' && *data != '\'' && *data != '"';data++)
{
if (len >= (int)sizeof(com_token) - 1)
{
com_token[0] = 0;
*datapointer = NULL;
return false;
}
com_token[len++] = *data;
}
com_token[len] = 0;
*datapointer = data;
return true;
}
}
void *readfile(char *filename, int *filesize)
{
FILE *file;
void *mem;
size_t size;
if (!filename[0])
{
printf("readfile: tried to open empty filename\n");
return NULL;
}
if (!(file = fopen(filename, "rb")))
return NULL;
fseek(file, 0, SEEK_END);
if (!(size = ftell(file)))
{
fclose(file);
return NULL;
}
if (!(mem = malloc(size + 1)))
{
fclose(file);
return NULL;
}
((unsigned char *)mem)[size] = 0; // 0 byte added on the end
fseek(file, 0, SEEK_SET);
if (fread(mem, 1, size, file) < size)
{
fclose(file);
free(mem);
return NULL;
}
fclose(file);
if (filesize) // can be passed NULL...
*filesize = size;
printf( "READ: %s of size %i\n", filename, (int)size );
return mem;
}
void writefile(char *filename, void *buffer, int size)
{
int size1;
FILE *file;
file = fopen(filename, "wb");
if (!file)
{
printf("unable to open file \"%s\" for writing\n", filename);
return;
}
size1 = fwrite(buffer, 1, size, file);
fclose(file);
if (size1 < size)
{
printf("unable to write file \"%s\"\n", filename);
return;
}
}
double VectorDistance(const double *v1, const double *v2)
{
return sqrt((v2[0]-v1[0])*(v2[0]-v1[0])+(v2[1]-v1[1])*(v2[1]-v1[1])+(v2[2]-v1[2])*(v2[2]-v1[2]));
}
double VectorDistance2D(const double *v1, const double *v2)
{
return sqrt((v2[0]-v1[0])*(v2[0]-v1[0])+(v2[1]-v1[1])*(v2[1]-v1[1]));
}
char *scriptbytes, *scriptend;
int scriptsize;
/*
int scriptfiles = 0;
char *scriptfile[256];
struct
{
double framerate;
int noloop;
int skip;
}
scriptfileinfo[256];
int parsefilenames(void)
{
char *in, *out, *name;
scriptfiles = 0;
in = scriptbytes;
out = scriptfilebuffer;
while (*in && *in <= ' ')
in++;
while (*in &&
while (scriptfiles < 256)
{
scriptfile[scriptfiles++] = name;
while (*in && *in != '\n' && *in != '\r')
in++;
if (!*in)
break;
while (*b > ' ')
b++;
while (*b && *b <= ' ')
*b++ = 0;
if (*b == 0)
break;
}
return scriptfiles;
}
*/
const char *tokenpos;
typedef struct bonepose_s
{
double m[3][4];
}
bonepose_t;
typedef struct bone_s
{
char name[MAX_NAME];
int parent; // parent of this bone
int flags;
int users; // used to determine if a bone is used to avoid saving out unnecessary bones
int defined;
}
bone_t;
typedef struct frame_s
{
int defined;
char name[MAX_NAME];
double mins[3], maxs[3], yawradius, allradius; // clipping
int numbones;
bonepose_t *bones;
}
frame_t;
typedef struct tripoint_s
{
int shadernum;
double texcoord[2];
int numinfluences;
double influenceorigin[MAX_INFLUENCES][3];
double influencenormal[MAX_INFLUENCES][3];
int influencebone[MAX_INFLUENCES];
float influenceweight[MAX_INFLUENCES];
// these are used for comparing against other tripoints (which are relative to other bones)
double originalorigin[3];
double originalnormal[3];
}
tripoint;
typedef struct triangle_s
{
int shadernum;
int v[3];
}
triangle;
typedef struct attachment_s
{
char name[MAX_NAME];
char parentname[MAX_NAME];
bonepose_t matrix;
}
attachment;
int numattachments = 0;
attachment attachments[MAX_ATTACHMENTS];
int numframes = 0;
int framegroup = 0;
frame_t frames[MAX_FRAMES];
int numbones = 0;
bone_t bones[MAX_BONES]; // master bone list
int numshaders = 0;
char shaders[MAX_SHADERS][32];
int numtriangles = 0;
triangle triangles[MAX_TRIS];
int numverts = 0;
tripoint vertices[MAX_VERTS];
//these are used while processing things
bonepose_t bonematrix[MAX_BONES];
char *modelfile;
int vertremap[MAX_VERTS];
double wrapangles(double f)
{
while (f < M_PI)
f += M_PI * 2;
while (f >= M_PI)
f -= M_PI * 2;
return f;
}
bonepose_t computebonematrix(double x, double y, double z, double a, double b, double c)
{
bonepose_t out;
double sr, sp, sy, cr, cp, cy;
sy = sin(c);
cy = cos(c);
sp = sin(b);
cp = cos(b);
sr = sin(a);
cr = cos(a);
out.m[0][0] = cp*cy;
out.m[1][0] = cp*sy;
out.m[2][0] = -sp;
out.m[0][1] = sr*sp*cy+cr*-sy;
out.m[1][1] = sr*sp*sy+cr*cy;
out.m[2][1] = sr*cp;
out.m[0][2] = (cr*sp*cy+-sr*-sy);
out.m[1][2] = (cr*sp*sy+-sr*cy);
out.m[2][2] = cr*cp;
out.m[0][3] = x;
out.m[1][3] = y;
out.m[2][3] = z;
return out;
}
bonepose_t concattransform(bonepose_t in1, bonepose_t in2)
{
bonepose_t out;
out.m[0][0] = in1.m[0][0] * in2.m[0][0] + in1.m[0][1] * in2.m[1][0] + in1.m[0][2] * in2.m[2][0];
out.m[0][1] = in1.m[0][0] * in2.m[0][1] + in1.m[0][1] * in2.m[1][1] + in1.m[0][2] * in2.m[2][1];
out.m[0][2] = in1.m[0][0] * in2.m[0][2] + in1.m[0][1] * in2.m[1][2] + in1.m[0][2] * in2.m[2][2];
out.m[0][3] = in1.m[0][0] * in2.m[0][3] + in1.m[0][1] * in2.m[1][3] + in1.m[0][2] * in2.m[2][3] + in1.m[0][3];
out.m[1][0] = in1.m[1][0] * in2.m[0][0] + in1.m[1][1] * in2.m[1][0] + in1.m[1][2] * in2.m[2][0];
out.m[1][1] = in1.m[1][0] * in2.m[0][1] + in1.m[1][1] * in2.m[1][1] + in1.m[1][2] * in2.m[2][1];
out.m[1][2] = in1.m[1][0] * in2.m[0][2] + in1.m[1][1] * in2.m[1][2] + in1.m[1][2] * in2.m[2][2];
out.m[1][3] = in1.m[1][0] * in2.m[0][3] + in1.m[1][1] * in2.m[1][3] + in1.m[1][2] * in2.m[2][3] + in1.m[1][3];
out.m[2][0] = in1.m[2][0] * in2.m[0][0] + in1.m[2][1] * in2.m[1][0] + in1.m[2][2] * in2.m[2][0];
out.m[2][1] = in1.m[2][0] * in2.m[0][1] + in1.m[2][1] * in2.m[1][1] + in1.m[2][2] * in2.m[2][1];
out.m[2][2] = in1.m[2][0] * in2.m[0][2] + in1.m[2][1] * in2.m[1][2] + in1.m[2][2] * in2.m[2][2];
out.m[2][3] = in1.m[2][0] * in2.m[0][3] + in1.m[2][1] * in2.m[1][3] + in1.m[2][2] * in2.m[2][3] + in1.m[2][3];
return out;
}
void transform(double in[3], bonepose_t matrix, double out[3])
{
out[0] = in[0] * matrix.m[0][0] + in[1] * matrix.m[0][1] + in[2] * matrix.m[0][2] + matrix.m[0][3];
out[1] = in[0] * matrix.m[1][0] + in[1] * matrix.m[1][1] + in[2] * matrix.m[1][2] + matrix.m[1][3];
out[2] = in[0] * matrix.m[2][0] + in[1] * matrix.m[2][1] + in[2] * matrix.m[2][2] + matrix.m[2][3];
}
void transformnormal(double in[3], bonepose_t matrix, double out[3])
{
out[0] = in[0] * matrix.m[0][0] + in[1] * matrix.m[0][1] + in[2] * matrix.m[0][2];
out[1] = in[0] * matrix.m[1][0] + in[1] * matrix.m[1][1] + in[2] * matrix.m[1][2];
out[2] = in[0] * matrix.m[2][0] + in[1] * matrix.m[2][1] + in[2] * matrix.m[2][2];
}
void inversetransform(double in[3], bonepose_t matrix, double out[3])
{
double temp[3];
temp[0] = in[0] - matrix.m[0][3];
temp[1] = in[1] - matrix.m[1][3];
temp[2] = in[2] - matrix.m[2][3];
out[0] = temp[0] * matrix.m[0][0] + temp[1] * matrix.m[1][0] + temp[2] * matrix.m[2][0];
out[1] = temp[0] * matrix.m[0][1] + temp[1] * matrix.m[1][1] + temp[2] * matrix.m[2][1];
out[2] = temp[0] * matrix.m[0][2] + temp[1] * matrix.m[1][2] + temp[2] * matrix.m[2][2];
}
/*
void rotate(double in[3], bonepose_t matrix, double out[3])
{
out[0] = in[0] * matrix.m[0][0] + in[1] * matrix.m[0][1] + in[2] * matrix.m[0][2];
out[1] = in[0] * matrix.m[1][0] + in[1] * matrix.m[1][1] + in[2] * matrix.m[1][2];
out[2] = in[0] * matrix.m[2][0] + in[1] * matrix.m[2][1] + in[2] * matrix.m[2][2];
}
*/
void inverserotate(double in[3], bonepose_t matrix, double out[3])
{
out[0] = in[0] * matrix.m[0][0] + in[1] * matrix.m[1][0] + in[2] * matrix.m[2][0];
out[1] = in[0] * matrix.m[0][1] + in[1] * matrix.m[1][1] + in[2] * matrix.m[2][1];
out[2] = in[0] * matrix.m[0][2] + in[1] * matrix.m[1][2] + in[2] * matrix.m[2][2];
}
int parsenodes(void)
{
int num, parent;
char line[1024], name[1024];
memset(bones, 0, sizeof(bones));
numbones = 0;
while (COM_ParseToken(&tokenpos, true))
{
// if this is the end keyword, we're done with this section of the file
if (!strcasecmp(com_token, "end"))
break;
//parse this line read by tokens
//get bone number
//we already read the first token, so use it
if (com_token[0] <= ' ')
{
printf("error in nodes, expecting bone number in line:%s\n", line);
return 0;
}
num = atoi( com_token );
//get bone name
if (!COM_ParseToken(&tokenpos, true) || com_token[0] < ' ')
{
printf("error in nodes, expecting bone name in line:%s\n", line);
return 0;
}
cleancopyname(name, com_token, MAX_NAME);//printf( "bone name: %s\n", name );
//get parent number
if (!COM_ParseToken(&tokenpos, true) || com_token[0] <= ' ')
{
printf("error in nodes, expecting parent number in line:%s\n", line);
return 0;
}
parent = atoi( com_token );
if (num < 0 || num >= MAX_BONES)
{
printf("invalid bone number %i\n", num);
return 0;
}
if (parent >= num)
{
printf("bone's parent >= bone's number\n");
return 0;
}
if (parent < -1)
{
printf("bone's parent < -1\n");
return 0;
}
if (parent >= 0 && !bones[parent].defined)
{
printf("bone's parent bone has not been defined\n");
return 0;
}
memcpy(bones[num].name, name, MAX_NAME);
bones[num].defined = 1;
bones[num].parent = parent;
if (num >= numbones)
numbones = num + 1;
// skip any trailing parameters (might be a later version of smd)
while (COM_ParseToken(&tokenpos, true) && com_token[0] != '\n');
}
// skip any trailing parameters (might be a later version of smd)
while (COM_ParseToken(&tokenpos, true) && com_token[0] != '\n');
return 1;
}
void animinfo_write(int base, int num, float framerate, int isanim)
{
int frame;
if(animinfofile && isanim)
fprintf(animinfofile, "%i %i %g // %s %s\n", base, num, framerate, model_name_lowercase, scene_name_lowercase);
if (qhheaderfile)
fprintf(qhheaderfile, "vector anim_%s_%s = '%i %i %g';\n", model_name_lowercase, scene_name_lowercase, base, num, framerate);
if (num >= 0 && qcheaderfile)
fprintf(qcheaderfile, "$frame");
for (frame = base;frame < num + base;frame++)
{
if (headerfile)
fprintf(headerfile, "#define MODEL_%s_%s_%i %i\n", model_name_uppercase, scene_name_uppercase, frame - base, frame);
if (qcheaderfile)
fprintf(qcheaderfile, " %s_%s_%i", model_name_lowercase, scene_name_lowercase, frame - base + 1);
}
if (headerfile)
{
fprintf(headerfile, "#define MODEL_%s_%s_START %i\n", model_name_uppercase, scene_name_uppercase, base);
fprintf(headerfile, "#define MODEL_%s_%s_END %i\n", model_name_uppercase, scene_name_uppercase, num + base);
fprintf(headerfile, "#define MODEL_%s_%s_LENGTH %i\n", model_name_uppercase, scene_name_uppercase, num);
fprintf(headerfile, "#define MODEL_%s_%s_FRAMERATE %g\n", model_name_uppercase, scene_name_uppercase, framerate);
fprintf(headerfile, "\n");
}
if (qcheaderfile)
fprintf(qcheaderfile, "\n");
}
int parseskeleton(void)
{
char line[1024], temp[1024];
int i, frame, num;
double x, y, z, a, b, c;
int baseframe;
chopextension(scene_name);
stringtouppercase(scene_name, scene_name_uppercase);
stringtolowercase(scene_name, scene_name_lowercase);
printf("parsing scene %s\n", scene_name);
baseframe = numframes;
frame = baseframe;
while (COM_ParseToken(&tokenpos, true))
{
// if this is the end keyword, we're done with this section of the file
if (!strcasecmp(com_token, "end"))
break;
//parse this line read by tokens
//get opening line token
//we already read the first token, so use it
if (com_token[0] <= ' ')
{
printf("error in parseskeleton, script line:%s\n", line);
return 0;
}
if (!strcasecmp(com_token, "time"))
{
//get the time value
if (!COM_ParseToken(&tokenpos, true) || com_token[0] <= ' ')
{
printf("error in parseskeleton, expecting time value in line:%s\n", line);
return 0;
}
i = atoi( com_token );
if (i < 0)
{
printf("invalid time %i\n", i);
return 0;
}
frame = baseframe + i;
if (frame >= MAX_FRAMES)
{
printf("only %i frames supported currently\n", MAX_FRAMES);
return 0;
}
if (frames[frame].defined)
{
printf("warning: duplicate frame\n");
free(frames[frame].bones);
}
sprintf(temp, "%s_%i", scene_name, i);
if (strlen(temp) > 31)
{
printf("error: frame name \"%s\" is longer than 31 characters\n", temp);
return 0;
}
cleancopyname(frames[frame].name, temp, MAX_NAME);
frames[frame].numbones = numbones + numattachments + 1;
frames[frame].bones = malloc(frames[frame].numbones * sizeof(bonepose_t));
memset(frames[frame].bones, 0, frames[frame].numbones * sizeof(bonepose_t));
frames[frame].bones[frames[frame].numbones - 1].m[0][1] = 35324;
frames[frame].defined = 1;
if (numframes < frame + 1)
numframes = frame + 1;
}
else
{
//the token was bone number
num = atoi( com_token );
//get x, y, z tokens
if (!COM_ParseToken(&tokenpos, true) || com_token[0] <= ' ')
{
printf("error in parseskeleton, expecting 'x' value in line:%s\n", line);
return 0;
}
x = atof( com_token );
if (!COM_ParseToken(&tokenpos, true) || com_token[0] <= ' ')
{
printf("error in parseskeleton, expecting 'y' value in line:%s\n", line);
return 0;
}
y = atof( com_token );
if (!COM_ParseToken(&tokenpos, true) || com_token[0] <= ' ')
{
printf("error in parseskeleton, expecting 'z' value in line:%s\n", line);
return 0;
}
z = atof( com_token );
//get a, b, c tokens
if (!COM_ParseToken(&tokenpos, true) || com_token[0] <= ' ')
{
printf("error in parseskeleton, expecting 'a' value in line:%s\n", line);
return 0;
}
a = atof( com_token );
if (!COM_ParseToken(&tokenpos, true) || com_token[0] <= ' ')
{
printf("error in parseskeleton, expecting 'b' value in line:%s\n", line);
return 0;
}
b = atof( com_token );
if (!COM_ParseToken(&tokenpos, true) || com_token[0] <= ' ')
{
printf("error in parseskeleton, expecting 'c' value in line:%s\n", line);
return 0;
}
c = atof( com_token );
if (num < 0 || num >= numbones)
{
printf("error: invalid bone number: %i\n", num);
return 0;
}
if (!bones[num].defined)
{
printf("error: bone %i not defined\n", num);
return 0;
}
// LadyHavoc: compute matrix
frames[frame].bones[num] = computebonematrix(x * modelscale[0], y * modelscale[1], z * modelscale[2], a, b, c);
}
// skip any trailing parameters (might be a later version of smd)
while (COM_ParseToken(&tokenpos, true) && com_token[0] != '\n');
}
// skip any trailing parameters (might be a later version of smd)
while (COM_ParseToken(&tokenpos, true) && com_token[0] != '\n');
for (frame = 0;frame < numframes;frame++)
{
if (!frames[frame].defined)
{
if (frame < 1)
{
printf("error: no first frame\n");
return 0;
}
if (!frames[frame - 1].defined)
{
printf("error: no previous frame to duplicate\n");
return 0;
}
sprintf(temp, "%s_%i", scene_name, frame - baseframe);
if (strlen(temp) > 31)
{
printf("error: frame name \"%s\" is longer than 31 characters\n", temp);
return 0;
}
printf("frame %s missing, duplicating previous frame %s with new name %s\n", temp, frames[frame - 1].name, temp);
frames[frame].defined = 1;
cleancopyname(frames[frame].name, temp, MAX_NAME);
frames[frame].numbones = numbones + numattachments + 1;
frames[frame].bones = malloc(frames[frame].numbones * sizeof(bonepose_t));
memcpy(frames[frame].bones, frames[frame - 1].bones, frames[frame].numbones * sizeof(bonepose_t));
frames[frame].bones[frames[frame].numbones - 1].m[0][1] = 35324;
printf("duplicate frame named %s\n", frames[frame].name);
}
}
if (!headerfile)
{
sprintf(temp, "%s%s.h", outputdir_name, model_name);
headerfile = fopen(temp, "w");
if (headerfile)
{
fprintf(headerfile, "/*\n");
fprintf(headerfile, "Generated header file for %s\n", model_name);
fprintf(headerfile, "This file contains frame number definitions for use in code referencing the model, to make code more readable and maintainable.\n");
fprintf(headerfile, "*/\n");
fprintf(headerfile, "\n");
fprintf(headerfile, "#ifndef MODEL_%s_H\n", model_name_uppercase);
fprintf(headerfile, "#define MODEL_%s_H\n", model_name_uppercase);
fprintf(headerfile, "\n");
}
}
if (!qcheaderfile)
{
sprintf(temp, "%s%s.qc", outputdir_name, model_name);
qcheaderfile = fopen(temp, "w");
if (qcheaderfile)
{
fprintf(qcheaderfile, "/*\n");
fprintf(qcheaderfile, "Generated header file for %s\n", model_name);
fprintf(qcheaderfile, "This file contains frame number definitions for use in code referencing the model, simply copy and paste into your qc file.\n");
fprintf(qcheaderfile, "*/\n");
fprintf(qcheaderfile, "\n");
}
}
if (!qhheaderfile)
{
sprintf(temp, "%s%s.qh", outputdir_name, model_name);
qhheaderfile = fopen(temp, "w");
if (qhheaderfile)
{
fprintf(qhheaderfile, "/*\n");
fprintf(qhheaderfile, "Generated header file for %s\n", model_name);
fprintf(qhheaderfile, "This file contains animation definitions for use in code referencing the model, simply add this file to your progs.src before use.\n");
fprintf(qhheaderfile, "*/\n");
fprintf(qhheaderfile, "\n");
}
}
if (!animinfofile)
{
// you can never usefully have both formats
sprintf(temp, "%s%s.dpm.animinfo", outputdir_name, model_name);
animinfofile = fopen(temp, "w");
// do not write a start comment to the animinfo file, as the QC code cannot skip it
}
if (!framegroupsfile)
{
// you can never usefully have both formats
sprintf(temp, "%s%s.dpm.framegroups", outputdir_name, model_name);
framegroupsfile = fopen(temp, "w");
if (framegroupsfile)
{
fprintf(framegroupsfile, "/*\n");
fprintf(framegroupsfile, "Generated framegroups file for %s\n", model_name);
fprintf(framegroupsfile, "Used by DarkPlaces to simulate frame groups in DPM models.\n");
fprintf(framegroupsfile, "*/\n");
fprintf(framegroupsfile, "\n");
}
}
if(numframes > 1)
{
if(sceneframegroups)
{
// real framegroups entry
if(framegroupsfile)
fprintf(framegroupsfile, "%i %i %g %i %s\n", baseframe, numframes - baseframe, sceneframerate, sceneloop, scene_name_lowercase);
// dummy animinfo entry
animinfo_write(framegroup, 1, sceneframerate / (numframes - baseframe), true);
++framegroup;
}
else // if(!sceneframegroups)
{
// real animinfo entry
animinfo_write(framegroup, numframes - baseframe, sceneframerate, true);
// dummy framegroups entries
for(i = 0; i < numframes - baseframe; ++i)
if(framegroupsfile)
fprintf(framegroupsfile, "%i %i %g %i %s\n", baseframe + i, 1, 1.0, 0, scene_name_lowercase);
framegroup += numframes - baseframe;
}
}
else
animinfo_write(baseframe, numframes - baseframe, sceneframerate, false); // reference pose
return 1;
}
/*
int sentinelcheckframes(char *filename, int fileline)
{
int i;
for (i = 0;i < numframes;i++)
{
if (frames[i].defined && frames[i].bones)
{
if (frames[i].bones[frames[i].numbones - 1].m[0][1] != 35324)
{
printf("sentinelcheckframes: error on frame %s detected at %s:%i\n", frames[i].name, filename, fileline);
exit(1);
}
}
}
return 1;
}
*/
int freeframes(void)
{
int i;
//sentinelcheckframes(__FILE__, __LINE__);
//printf("no errors were detected\n");
for (i = 0;i < numframes;i++)
{
if (frames[i].defined && frames[i].bones)
{
//fprintf(stdout, "freeing %s\n", frames[i].name);
//fflush(stdout);
//if (frames[i].bones[frames[i].numbones - 1].m[0][1] != 35324)
// printf("freeframes: error on frame %s\n", frames[i].name);
//else
free(frames[i].bones);
}
}
numframes = 0;
return 1;