-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmd3IO.cpp
executable file
·2204 lines (1864 loc) · 56.5 KB
/
md3IO.cpp
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
/*
Copyright (C) 2010 Matthew Baranowski, Sander van Rossen & Raven software.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "system.h"
#include "ndictionary.h"
#include "md3gl.h"
#include "md3view.h"
#include "DiskIO.h"
#include "oddbits.h"
#include "matcomp.h"
#include <stddef.h> // for offsetof macro
#include "mdx_format.h"
#include "g2export.h"
#include "matrix4.h"
#pragma warning (disable : 4786) //ident trunc
#include <list>
using namespace std;
// if enabled, this'll make the g2 model have same orientation as MD3,
// if disabled, the model will have all that weird 90-degree bollocks applied to it.
//
//#define PERFECT_CONVERSION // this is now runtime, controlled by "bool g_bPerfect"
extern bool g_bPerfect;
#ifndef M_PI
#define M_PI 3.14159265358979323846 // matches value in gcc v2 math.h
#endif
#define DEG2RAD( a ) ( ( (a) * M_PI ) / 180.0F )
#define RAD2DEG( a ) ( ( (a) * 180.0f ) / M_PI )
/*
** NormalToLatLong
**
** We use two byte encoded normals in some space critical applications.
** Lat = 0 at (1,0,0) to 360 (-1,0,0), encoded in 8-bit sine table format
** Lng = 0 at (0,0,1) to 180 (0,0,-1), encoded in 8-bit sine table format
**
*/
void NormalToLatLong( const vec3_t normal, byte bytes[2] )
{
// check for singularities
if ( normal[0] == 0 && normal[1] == 0 ) {
if ( normal[2] > 0 ) {
bytes[0] = 0;
bytes[1] = 0; // lat = 0, long = 0
} else {
bytes[0] = 128;
bytes[1] = 0; // lat = 0, long = 128
}
} else {
int a, b;
a = RAD2DEG( atan2( normal[1], normal[0] ) ) * (255.0f / 360.0f );
a &= 0xff;
b = RAD2DEG( acos( normal[2] ) ) * ( 255.0f / 360.0f );
b &= 0xff;
bytes[0] = b; // longitude
bytes[1] = a; // lattitude
}
}
double VectorLength( const vec3_t v )
{
double length;
length = sqrt (v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
return length;
}
void ClearBounds (vec3_t mins, vec3_t maxs)
{
mins[0] = mins[1] = mins[2] = 99999;
maxs[0] = maxs[1] = maxs[2] = -99999;
}
void AddPointToBounds( const vec3_t v, vec3_t mins, vec3_t maxs )
{
int i;
vec_t val;
for (i=0 ; i<3 ; i++)
{
val = v[i];
if (val < mins[i])
mins[i] = val;
if (val > maxs[i])
maxs[i] = val;
}
}
#define OUTPOS() //OutputDebugString(va("0x%X\n",filesize(fHandle)))
list <string> FilesWritten;
bool bAutoOverWrite = false;
bool bAskThem = true;
gl_model* gpModel=NULL;
bool ExportThisModel( LPCSTR psFilename, gl_model* pModel, bool bExportAsMD3)
{
bool bG2MultiLODExportHasErrors = false;
bool bG2ForceSingleLODExport = false;
// this compiler is being really gay and insisting I move these up here to avoid their initialising being
// skipped by the first 'goto' commands, even though they're not used later. Duh!!!
//
int iOutLODCount = 1;
int iOutLOD;
gpModel = pModel; // so ghoul2 exporter code can get at the model data
// OutputDebugString(va("#### Exporting: %s\n",psFilename));
if (!pModel) // call with NULL pModel to display info on stuff written
{
string str;
str = "Files written:\n\n";
list <string>::iterator it;
STL_ITERATE(it,FilesWritten)
{
str += (*it).c_str();
str += "\n";
}
str += "\n\nNo errors";
InfoBox(str.c_str());
return true;
}
StartWait();
FILE *fHandle = NULL;
bool bOneFrameMD3ExportHack = false;
if (!bExportAsMD3) // and hence Ghoul2...
{
if (pModel->modelType != MODTYPE_MD3)
{
ErrorBox("Error! Currently, this program will only export loaded MD3's as Ghoul2 models");
goto error;
}
// and if it's an MD3, then it must only be one frame since there's no skeletal information...
//
if (pModel->iNumFrames != 1)
{
if (!GetYesNo( va("Only MD3 models with 1 frame can be exported as Ghoul2, this model has %d frames!\n\nExport just the first frame? ( 'NO' will abort export )",pModel->iNumFrames)))
{
goto error;
}
bOneFrameMD3ExportHack = true;
}
// because of multiple-LOD-from-multiple-model issues I now need to pre-scan some stuff to check for
// mismatching surfaces etc...
//
if (pModel->pModel_LOD1 || pModel->pModel_LOD2)
{
// multiple models loaded, need to run checks...
//
char sExportSingleLODPrompt[4096]="Unknown";//{0};
//
bool bMisMatchingSurfacenameQuestionAlreadyAsked = false;
int iLOD;
for (iLOD=0; iLOD<3; iLOD++)
{
gl_model* pMod = NULL;
switch (iLOD)
{
//case 0: pMod = pModel; break;
case 1: pMod = pModel->pModel_LOD1; break;
case 2: pMod = pModel->pModel_LOD2; break;
}
if (pMod)
{
if (pModel->modelType != MODTYPE_MD3)
{
sprintf(sExportSingleLODPrompt,"LOD %d is not an MD3 model",iLOD);
bG2MultiLODExportHasErrors = true;
break;
}
if (pMod->iNumMeshes > pModel->iNumMeshes)
{
sprintf(sExportSingleLODPrompt,"LOD %d has more surfaces than LOD 0",iLOD);
bG2MultiLODExportHasErrors = true;
break;
}
if (pMod->iNumMeshes < pModel->iNumMeshes)
{
// this is ok because I'll generate fake surfaces, but let's just see if the surfaces
// so far match, and if not, issue a warning...
//
if (!bMisMatchingSurfacenameQuestionAlreadyAsked)
{
for (int iMesh=0; iMesh<(int)pMod->iNumMeshes; iMesh++)
{
LPCSTR pLOD0MeshName = pModel->pMeshes[iMesh].sName;
LPCSTR pLODXMeshName = pModel->pMeshes[iMesh].sName;
if (stricmp(pLOD0MeshName,pLODXMeshName))
{
bG2ForceSingleLODExport = !GetYesNo(va("Multi-LOD-export issue:\n\nLOD %d has less surfaces than LOD 0 (which isn't a problem, I can insert extra dummy surfaces), but the common surface names also mismatch, surface %d is called \"%s\" in this LOD, and \"%s\" in the original\n\nDo you still want to go ahead and export as multi-LOD?\n\n('NO' will export single-LOD only)",
iLOD, iMesh, pLODXMeshName, pLOD0MeshName));
bMisMatchingSurfacenameQuestionAlreadyAsked = true;
break;
}
}
}
}
}
}
// Ok, do we need to offer them a prompt to export single-LOD only?
//
if (iLOD != 3)
{
if (bG2MultiLODExportHasErrors)
{
bG2ForceSingleLODExport = GetYesNo(va("Multi-LOD-export failure:\n\n%s\n\nDo you want to go ahead and export as a single-LOD model?",sExportSingleLODPrompt));
if (!bG2ForceSingleLODExport) // since we CANNOT export multi-lods because of the errtype, exit if they say 'no'.
goto error;
}
}
}
}
if (pModel == mdview.baseModel)
{
// OutputDebugString("!!!! First model\n");
// the only model, or the first in a sequence if exporting several...
//
FilesWritten.clear();
if (bAskThem)
{
//bAskThem = true;
bAutoOverWrite = false;
}
}
switch (pModel->modelType) // type of current model, not export format
{
case MODTYPE_MD3: iOutLODCount = 1; break;
case MODTYPE_MDR: iOutLODCount = pModel->Q3ModelStuff.md4->numLODs; break;
default: ErrorBox("casedefault: Unhandled model type"); goto error;
}
char sFilename[MAX_PATH];
for (iOutLOD=0; iOutLOD<iOutLODCount; iOutLOD++)
{
strcpy(sFilename,psFilename);
char *p;
while ((p=strchr(sFilename,'\\'))!=NULL)
*p='/';
if (iOutLOD)
{
char *p=strrchr(sFilename,'.');
assert(p);
if (p)
{
strcpy(p,va("_%d.md3",iOutLOD));
}
}
bool bGoAhead = true;
if (FileExists(sFilename))
{
if (bAskThem)
{
bGoAhead = GetYesNo(va("Output file \"%s\" already exists, overwrite?",sFilename));
if (bAskThem)
{
bAskThem =!GetYesNo("Apply this decision to all future queries?");
}
if (!bAskThem)
{
bAutoOverWrite = bGoAhead;
}
}
else
{
bGoAhead = bAutoOverWrite;
}
RestoreWait();
}
if (bGoAhead)
{
if (bExportAsMD3)
{
fHandle = fopen(sFilename,"wb");
if (fHandle)
{
FilesWritten.push_back(sFilename);
//
// fuck this, it's tedious not having a structure to work with (and for rewinding to field offsets during
// writing), so...
//
typedef struct
{
char ID[4];
int iVersion;
char sName[68];
int iNumFrames;
int iNumTags;
int iNumMeshes;
int iMaxSkinNum;
int iHeadSize;
int iOffsetToTags; // from file-0
int iOffsetToMeshes; //
int iFileSize;
} MD3Head, *lpMD3Head;
MD3Head Header;
strncpy(Header.ID,"IDP3",4);
Header.iVersion = 15;
strcpy(Header.sName,Filename_WithoutPath(Filename_WithoutExt(sFilename)));
Header.iNumFrames = bOneFrameMD3ExportHack ? 1 : pModel->iNumFrames;
Header.iNumTags = pModel->iNumTags;
Header.iMaxSkinNum = 0; // ? (always seems to be 0 in models I've tried)
Header.iHeadSize = sizeof(Header);
Header.iOffsetToTags = sizeof(Header) + (Header.iNumFrames * sizeof md3BoundFrame_t);
Header.iOffsetToMeshes = Header.iOffsetToTags + (Header.iNumFrames * Header.iNumTags * sizeof(md3Tag_t)/*BoneFrame_t*/);
Header.iFileSize = NULL;
md4LOD_t *pLOD = NULL;
switch (pModel->modelType)
{
case MODTYPE_MD3:
Header.iNumMeshes = pModel->iNumMeshes;
break;
case MODTYPE_MDR:
{
pLOD = (md4LOD_t *) ((byte *)pModel->Q3ModelStuff.md4 + pModel->Q3ModelStuff.md4->ofsLODs);
for ( int i=0; i<iOutLOD; i++)
{
pLOD = (md4LOD_t*)( (byte *)pLOD + pLOD->ofsEnd );
}
Header.iNumMeshes = pLOD->numSurfaces;
break;
}
default:
ErrorBox("casedefault: Unhandled model type");
goto error;
}
fwrite(&Header,sizeof(Header),1,fHandle);
OUTPOS();
// write the boundary frames...
//
md3BoundFrame_t *pBoundFrames = NULL; // MDRs will have to come back and fill this in later during freeze
long lBoundFrameWriteSeekPos = ftell(fHandle);
int iFrame = 0;
for (iFrame = 0; iFrame<Header.iNumFrames; iFrame++)
{
md3BoundFrame_t BoundFrame;
switch (pModel->modelType)
{
case MODTYPE_MD3:
// just write out the original one from when the model was loaded in...
//
BoundFrame = pModel->pMD3BoundFrames[iFrame];
break;
case MODTYPE_MDR:
if (iFrame==0)
pBoundFrames = new md3BoundFrame_t[Header.iNumFrames];
// and setup this entry ready for freeze-code boundary-determiner later...
//
ClearBounds(pBoundFrames[iFrame].bounds[0], pBoundFrames[iFrame].bounds[1]);
// for the moment, just write out zeroes...
//
memset(&BoundFrame,0,sizeof(BoundFrame));
break;
default:
ErrorBox("casedefault: Unhandled model type");
goto error;
}
fwrite(&BoundFrame, sizeof(BoundFrame),1,fHandle);
}
OUTPOS();
// write tags... (MDR already uses MD3 tags in this app)
//
for (iFrame = 0; iFrame<Header.iNumFrames; iFrame++)
{
for (int iTag = 0; iTag<Header.iNumTags; iTag++)
{
md3Tag_t* pTag = &pModel->tags[iFrame][iTag];
fwrite(pTag, sizeof(*pTag),1,fHandle);
}
}
OUTPOS();
// write meshes...
//
for (int iMesh = 0; iMesh<Header.iNumMeshes; iMesh++)
{
gl_mesh *pMesh = NULL; // for writing from MD3 source
md4Surface_t *pSurface = NULL; // " " " MDR "
typedef struct
{
char ID[4];
char sName[68];
int iNumMeshFrames;
int iNumSkins;
int iNumVertices;
int iNumTriangles;
int iOffsetToTriangles;
int iHeadSize;
int iOffsetToTexVecs;
int iOffsetToVertices;
int iMeshSize;
} MD3MeshHead, *lpMD3MeshHead;
MD3MeshHead MeshHeader;
strncpy(MeshHeader.ID,"IDP3",4);
switch (pModel->modelType)
{
case MODTYPE_MD3:
pMesh = &pModel->pMeshes[ iMesh ];
strcpy(MeshHeader.sName, pMesh->sName);
MeshHeader.iNumMeshFrames = bOneFrameMD3ExportHack ? 1 : pMesh->iNumMeshFrames; // same as global count, but convenient
MeshHeader.iNumSkins = pMesh->iNumSkins;
MeshHeader.iNumVertices = pMesh->iNumVertices;
MeshHeader.iNumTriangles = pMesh->iNumTriangles;
break;
case MODTYPE_MDR:
{
pSurface = (md4Surface_t *)( (byte *)pLOD + pLOD->ofsSurfaces );
for (int iSurf=0; iSurf<iMesh; iSurf++)
{
pSurface = (md4Surface_t *)( (byte *)pSurface + pSurface->ofsEnd );
}
strcpy(MeshHeader.sName, pSurface->name);
MeshHeader.iNumMeshFrames = pModel->iNumFrames; // same as global count, but convenient
MeshHeader.iNumSkins = 1;
MeshHeader.iNumVertices = pSurface->numVerts;
MeshHeader.iNumTriangles = pSurface->numTriangles;
break;
}
default:
ErrorBox("casedefault: Unhandled model type");
goto error;
}
typedef struct
{
short x,y,z,normal;
}MD3MeshVert;
MeshHeader.iOffsetToTriangles = sizeof(MeshHeader) + MeshHeader.iNumSkins * sizeof(MeshHeader.sName);
MeshHeader.iHeadSize = sizeof(MeshHeader);
MeshHeader.iOffsetToTexVecs = MeshHeader.iOffsetToTriangles + (MeshHeader.iNumTriangles * sizeof(TriVec));
MeshHeader.iOffsetToVertices = MeshHeader.iOffsetToTexVecs + (MeshHeader.iNumVertices * sizeof(TexVec));
MeshHeader.iMeshSize = MeshHeader.iOffsetToVertices + (MeshHeader.iNumVertices * MeshHeader.iNumMeshFrames * sizeof(MD3MeshVert));
fwrite(&MeshHeader,sizeof(MeshHeader),1,fHandle);
OUTPOS();
// this is one of these leftover crap things, AFAIK it's always 1, that's all that's stored anyway, so...
//
for (int iSkin=0; iSkin<MeshHeader.iNumSkins; iSkin++)
{
char sName[68];
switch (pModel->modelType)
{
case MODTYPE_MD3:
strcpy(sName, pMesh->sTextureName);
break;
case MODTYPE_MDR:
strcpy(sName, pSurface->shader);
break;
default:
ErrorBox("casedefault: Unhandled model type");
goto error;
}
fwrite(sName,sizeof(sName),1,fHandle);
}
OUTPOS();
shaderCommands_t* pTess = NULL;
// write triangles...
//
for (int iTri=0; iTri<MeshHeader.iNumTriangles; iTri++)
{
TriVec Tri;
switch (pModel->modelType)
{
case MODTYPE_MD3:
memcpy(Tri,pMesh->triangles[iTri],sizeof(Tri)); // stupid compiler won't do an assign
break;
case MODTYPE_MDR:
pTess = Freeze_Surface( pSurface, 0 ); // frame 0 is fine for working out tri indexes and (next) tex coords
memcpy(Tri,&pTess->indexes[iTri*3],sizeof(Tri));
break;
default:
ErrorBox("casedefault: Unhandled model type");
goto error;
}
put32(Tri[0],fHandle);
put32(Tri[1],fHandle);
put32(Tri[2],fHandle);
}
OUTPOS();
// write tex coords...
//
for (int iVert=0; iVert<MeshHeader.iNumVertices; iVert++)
{
float u,v;
switch (pModel->modelType)
{
case MODTYPE_MD3:
u = pMesh->textureCoord[iVert][0];
v = pMesh->textureCoord[iVert][1];
break;
case MODTYPE_MDR:
assert(pTess);
u = pTess->texCoords[iVert][0][0];
v = pTess->texCoords[iVert][0][1];
break;
default:
ErrorBox("casedefault: Unhandled model type");
goto error;
}
putFloat(u,fHandle);
putFloat(v,fHandle);
}
OUTPOS();
// actual mesh data...
//
for (int iFrame=0; iFrame<MeshHeader.iNumMeshFrames; iFrame++)
{
for (int iVert=0; iVert<MeshHeader.iNumVertices; iVert++)
{
short x,y,z,normal;
switch (pModel->modelType)
{
case MODTYPE_MD3:
x = (short)((pMesh->meshFrames[iFrame].pVerts[iVert][0])*64);
y = (short)((pMesh->meshFrames[iFrame].pVerts[iVert][1])*64);
z = (short)((pMesh->meshFrames[iFrame].pVerts[iVert][2])*64);
normal = pMesh->meshFrames[iFrame].pNormalIndexes[iVert];
break;
case MODTYPE_MDR:
if (iVert==0)
pTess = Freeze_Surface( pSurface, iFrame );
x = (short)((pTess->xyz[iVert][0])*64);
y = (short)((pTess->xyz[iVert][1])*64);
z = (short)((pTess->xyz[iVert][2])*64);
byte thing[2];
NormalToLatLong( pTess->normal[iVert], thing );
normal = *(short*)thing; /////xxxxxxxxxxxxx this needs testing in-game
// extra stuff needed for MDR source, we need to fill in the stuff about boundary frames
// here, which MD3 didn't need to do because it just writes out whatever it found when
// the model was loaded in...
//
assert(pBoundFrames);
if (pBoundFrames)
{
AddPointToBounds (pTess->xyz[iVert], pBoundFrames[iFrame].bounds[0], pBoundFrames[iFrame].bounds[1]);
}
break;
default:
ErrorBox("casedefault: Unhandled model type");
goto error;
}
put16(x,fHandle);
put16(y,fHandle);
put16(z,fHandle);
put16(normal,fHandle);
}
}
OUTPOS();
}
// if we were writing from an MDR source, we need to go back and write out the newly-generated boundary frames
// (writing from MD3s will have used the frames found during original load)
//
if (pBoundFrames)
{
// do some final processing...
//
for (int iFrame=0; iFrame<Header.iNumFrames; iFrame++)
{
pBoundFrames[iFrame].localOrigin[0] =
pBoundFrames[iFrame].localOrigin[1] =
pBoundFrames[iFrame].localOrigin[2] = 0;
strcpy(pBoundFrames[iFrame].name,"MD3View_Raven"); // creator ( name[16] !!! )
// work out largest radius from mins/maxs... (for remaining field in boundframe struct)
//
vec3_t tmpVec;
float maxRadius = 0;
for (int j=0; j<8; j++)
{
tmpVec[0] = pBoundFrames[iFrame].bounds[(j&1)!=0][0];
tmpVec[1] = pBoundFrames[iFrame].bounds[(j&2)!=0][1];
tmpVec[2] = pBoundFrames[iFrame].bounds[(j&4)!=0][2];
if (VectorLength( tmpVec ) > maxRadius )
maxRadius = VectorLength( tmpVec );
}
pBoundFrames[iFrame].radius = maxRadius;
}
// now write all boundframes out...
//
fseek(fHandle,lBoundFrameWriteSeekPos,SEEK_SET);
fwrite(pBoundFrames,sizeof(md3BoundFrame_t),Header.iNumFrames,fHandle);
delete [] pBoundFrames;
pBoundFrames = NULL;
}
// now go back and write in the file size...
//
fseek(fHandle,offsetof(MD3Head,iFileSize), SEEK_SET);
put32(filesize(fHandle),fHandle);
fclose(fHandle);
}
else
{
ErrorBox(va("Error: Unable to write to '%s'!\n",sFilename));
}
}
else
{
// exporting an MD3 model as a ghoul2 model, so we first need to ask how many LODs there are from the special LOD-models loaded,
// even though they aren't part of the same MD3 model...
//
int iActualNumLODs = iOutLODCount;
if (pModel->pModel_LOD1) iActualNumLODs++;
if (pModel->pModel_LOD2) iActualNumLODs++;
LPCSTR psNameGLA = NULL;
iActualNumLODs = bG2ForceSingleLODExport?1:iActualNumLODs;
char *psErrMess = ExportGhoul2FromMD3(sFilename, iActualNumLODs, pModel->iNumMeshes, pModel->iNumTags,
&psNameGLA
);
if (!psErrMess)
{
FilesWritten.push_back(sFilename);
FilesWritten.push_back(psNameGLA);
}
else
{
ErrorBox(psErrMess);
}
}
}
}
EndWait();
return true;
error:
EndWait();
if (fHandle)
{
fclose(fHandle);
fHandle = NULL;
list <string>::iterator it;
STL_ITERATE(it,FilesWritten)
{
remove((*it).c_str());
}
}
return false;
}
// ghoul2 exported interface functions...
//
LPCSTR G2Exporter_Surface_GetName(int iSurfaceIndex)
{
// always assume LOD0 here, so ignore other loaded MD3 models...
//
switch (gpModel->modelType)
{
case MODTYPE_MD3:
{
if (iSurfaceIndex < (int)gpModel->iNumMeshes)
{
// standard surface...
//
gl_mesh *pMesh =&gpModel->pMeshes[iSurfaceIndex];
return pMesh->sName;
}
else
{
// tag surface...
//
static char sTemp[1024];
Tag* pTag = &gpModel->tags[0][iSurfaceIndex - gpModel->iNumMeshes];
//
// prepend name with a '*', and remove "tag_" from name start if present...
//
strcpy(sTemp,"*");
strcat(sTemp, (!strnicmp(pTag->Name,"tag_",4)) ? &pTag->Name[4] : pTag->Name);
return sTemp;
}
}
default:
{
static bool bAlreadyReported = false;
if (!bAlreadyReported)
{
bAlreadyReported = true;
assert(0);
ErrorBox(va("G2Exporter_Surface_GetName(): Error! modeltype %d has no case handler!\n",gpModel->modelType));
}
}
break;
}
return "Error";
}
LPCSTR G2Exporter_Surface_GetShaderName(int iSurfaceIndex)
{
// always assume LOD0 here, so ignore other loaded MD3 models...
//
switch (gpModel->modelType)
{
case MODTYPE_MD3:
{
if (iSurfaceIndex < (int)gpModel->iNumMeshes)
{
// standard surface...
//
gl_mesh *pMesh =&gpModel->pMeshes[iSurfaceIndex];
return pMesh->sTextureName;
}
else
{
// tag surface...
//
return "[NoMaterial]";
}
}
default:
{
static bool bAlreadyReported = false;
if (!bAlreadyReported)
{
bAlreadyReported = true;
assert(0);
ErrorBox(va("G2Exporter_Surface_GetShaderName(): Error! modeltype %d has no case handler!\n",gpModel->modelType));
}
}
break;
}
return "Error";
}
static gl_model *Model_AccountForLOD(gl_model* pModel, int iLOD)
{
gl_model *pMod = pModel;
switch (iLOD)
{
case 0:
break;
case 1:
pMod = pMod->pModel_LOD1?pMod->pModel_LOD1:pMod;
break;
case 2:
pMod = pMod->pModel_LOD2?pMod->pModel_LOD2:pMod;
break;
default:
assert(0);
}
return pMod;
}
int G2Exporter_Surface_GetNumVerts(int iSurfaceIndex, int iLOD)
{
gl_model* pModel = Model_AccountForLOD(gpModel, iLOD);
switch (pModel->modelType)
{
case MODTYPE_MD3:
{
if (iSurfaceIndex < (int)gpModel->iNumMeshes) // gpModel, not pModel, to cope with models of differing # meshes
{
if (iSurfaceIndex < (int)pModel->iNumMeshes) // now check surface is legal for THIS LOD model...
{
// standard surface...
//
gl_mesh *pMesh =&pModel->pMeshes[iSurfaceIndex];
return pMesh->iNumVertices;
}
else
{
// surface was a legal index for LOD 0, but not for this one, so return fake surface info...
//
return 1;
}
}
else
{
// tag surface...
//
return 3; // for one triangle
}
}
default:
{
static bool bAlreadyReported = false;
if (!bAlreadyReported)
{
bAlreadyReported = true;
assert(0);
ErrorBox(va("G2Exporter_Surface_GetNumVerts(): Error! modeltype %d has no case handler!\n",pModel->modelType));
}
}
break;
}
return 0;
}
int G2Exporter_Surface_GetNumTris(int iSurfaceIndex, int iLOD)
{
gl_model* pModel = Model_AccountForLOD(gpModel, iLOD);
switch (pModel->modelType)
{
case MODTYPE_MD3:
{
if (iSurfaceIndex < (int)gpModel->iNumMeshes) // gpModel, not pModel, to cope with models of differing # meshes
{
if (iSurfaceIndex < (int)pModel->iNumMeshes) // now check surface is legal for THIS LOD model...
{
// standard surface...
//
gl_mesh *pMesh =&pModel->pMeshes[iSurfaceIndex];
return pMesh->iNumTriangles;
}
else
{
// surface was a legal index for LOD 0, but not for this one, so return fake surface info...
//
return 1;
}
}
else
{
// tag surface...
//
return 1; // for one triangle
}
}
default:
{
static bool bAlreadyReported = false;
if (!bAlreadyReported)
{
bAlreadyReported = true;
assert(0);
ErrorBox(va("G2Exporter_Surface_GetNumTris(): Error! modeltype %d has no case handler!\n",pModel->modelType));
}
}
break;
}
return 0;
}
int G2Exporter_Surface_GetTriIndex(int iSurfaceIndex, int iTriangleIndex, int iTriVert, int iLOD)
{
gl_model* pModel = Model_AccountForLOD(gpModel, iLOD);
switch (pModel->modelType)
{
case MODTYPE_MD3:
{
if (iSurfaceIndex < (int)gpModel->iNumMeshes) // gpModel, not pModel, to cope with models of differing # meshes
{
if (iSurfaceIndex < (int)pModel->iNumMeshes) // now check surface is legal for THIS LOD model...
{
// standard surface...
//
gl_mesh *pMesh = &pModel->pMeshes[iSurfaceIndex];
TriVec *pTriIndexes= &pMesh->triangles[iTriangleIndex];
return pTriIndexes[0][iTriVert];
}
else
{
// surface was a legal index for LOD 0, but not for this one, so return fake surface info...
//
return 0; // only one vert in a fake surface, so index is always zero
}
}
else
{