forked from UberGames/MD3View
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathg2export.cpp
More file actions
executable file
·433 lines (343 loc) · 14.4 KB
/
g2export.cpp
File metadata and controls
executable file
·433 lines (343 loc) · 14.4 KB
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
/*
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" // stuff needed for caller program, standar headers etc
#include "oddbits.h"
//
#include "matrix4.h"
#include "mdx_format.h"
#include "matcomp.h"
//
#include "g2export.h"
#define BASEDIRNAME "base"
extern char qdir[];//1024];
extern char gamedir[];//1024]; // q:\quake\baseef
extern LPCSTR Filename_QUAKEBASEONLY(LPCSTR psFullPathedName);
extern LPCSTR G2Exporter_Surface_GetName (int iSurfaceIndex);
extern LPCSTR G2Exporter_Surface_GetShaderName(int iSurfaceIndex);
extern int G2Exporter_Surface_GetNumVerts (int iSurfaceIndex, int iLOD);
extern int G2Exporter_Surface_GetNumTris (int iSurfaceIndex, int iLOD);
extern int G2Exporter_Surface_GetTriIndex (int iSurfaceIndex, int iTriangleIndex, int iTriVert, int iLOD);
extern vec3_t* G2Exporter_Surface_GetVertNormal(int iSurfaceIndex, int iVertIndex, int iLOD);
extern vec3_t* G2Exporter_Surface_GetVertCoords(int iSurfaceIndex, int iVertIndex, int iLOD);
extern vec2_t* G2Exporter_Surface_GetTexCoords (int iSurfaceIndex, int iVertIndex, int iLOD);
static LPCSTR Filename_StripQuakeBase(LPCSTR psFullPathedName)
{
static char sTemp[1024];
strcpy(sTemp,Filename_QUAKEBASEONLY(psFullPathedName));
int iQuakeLen = strlen(sTemp);
strcpy(sTemp,&psFullPathedName[iQuakeLen]);
return &sTemp[0];
}
static void ForwardSlash(char *psLocalName)
{
while (*psLocalName)
{
if (*psLocalName == '\\')
*psLocalName = '/';
psLocalName++;
}
}
// saves clogging the param stack...
//
int giNumLODs;
int giNumSurfaces;
int giNumTags;
static char *ExportGhoul2FromMD3_Main(FILE *fhGLM, /*FILE *fhGLA,*/ LPCSTR psFullPathedNameGLM)
{
char *psErrMess = NULL;
const int iMallocSize = 1024*1024*20; // 20MB should be enough for one model... :-)
byte *pbBuffer = (byte *) malloc(iMallocSize);
if (pbBuffer)
{
// filename used in both files...
//
char sLocalNameGLA[MAX_PATH];
strcpy( sLocalNameGLA,Filename_WithoutExt(Filename_StripQuakeBase(psFullPathedNameGLM)));
ForwardSlash(sLocalNameGLA);
// start writing...
//
byte *at = pbBuffer;
// write GLM Header...
//
mdxmHeader_t *pMDXMHeader = (mdxmHeader_t *) at;
at += sizeof(mdxmHeader_t);
{// GLM brace-match for skipping...
pMDXMHeader->ident = MDXM_IDENT;
pMDXMHeader->version = MDXM_VERSION;
strcpy( pMDXMHeader->name,Filename_StripQuakeBase(psFullPathedNameGLM));
ForwardSlash(pMDXMHeader->name);
strcpy( pMDXMHeader->animName,sDEFAULT_GLA_NAME);//sLocalNameGLA);
pMDXMHeader->animIndex = 0; // ingame use only
pMDXMHeader->numBones = 1; // ... and a fake one at that.
pMDXMHeader->numLODs = giNumLODs;
// pMDXMHeader->ofsLODs = ?????????????????????????????????????????????
pMDXMHeader->numSurfaces = giNumSurfaces + giNumTags;
// pMDXMHeader->ofsSurfHierarchy= ?????????????????????????????????????????????
// pMDXMHeader->ofsEnd = ?????????????????????????????????????????????
// for hierarchiy purposes, I'm going to just write out the first surface as the parent,
// then make every other surface a child of that one...
//
// G2 surfaces come from MD3 meshes first, then the MD3 tags (since G2 uses tag surfaces)
//
mdxmHierarchyOffsets_t *pHierarchyOffsets = (mdxmHierarchyOffsets_t *) at;
at += sizeof(mdxmHierarchyOffsets_t) * pMDXMHeader->numSurfaces;
pMDXMHeader->ofsSurfHierarchy = at - (byte *) pMDXMHeader;
for (int iSurfaceIndex = 0; iSurfaceIndex < pMDXMHeader->numSurfaces; iSurfaceIndex++)
{
// Note: bool bSurfaceIsTag == (iSurfaceIndex < giNumSurfaces)
//
mdxmSurfHierarchy_t *pSurfHierarchy = (mdxmSurfHierarchy_t *) at;
//
// store this offset...
//
pHierarchyOffsets->offsets[iSurfaceIndex] = (byte *)pSurfHierarchy - (byte *)pHierarchyOffsets;
// fill in surf hierarchy struct...
//
strcpy( pSurfHierarchy->name, G2Exporter_Surface_GetName(iSurfaceIndex));
pSurfHierarchy->flags = 0;
if ( iSurfaceIndex >= giNumSurfaces)
{
pSurfHierarchy->flags |= G2SURFACEFLAG_ISBOLT;
}
if (!strnicmp(&pSurfHierarchy->name[strlen(pSurfHierarchy->name)-4],"_off",4))
{
pSurfHierarchy->flags |= G2SURFACEFLAG_OFF;
}
strlwr(pSurfHierarchy->name);
strcpy( pSurfHierarchy->shader, G2Exporter_Surface_GetShaderName(iSurfaceIndex));
pSurfHierarchy->shaderIndex = 0; // ingame use only
pSurfHierarchy->parentIndex = iSurfaceIndex?0:-1;
pSurfHierarchy->numChildren = iSurfaceIndex?0:pMDXMHeader->numSurfaces-1;
if (!iSurfaceIndex)
{
for (int i=0; i<pSurfHierarchy->numChildren; i++)
{
pSurfHierarchy->childIndexes[i] = i+1;
}
}
int iThisHierarchySize = (int)( &((mdxmSurfHierarchy_t *)0)->childIndexes[ pSurfHierarchy->numChildren ] );
at += iThisHierarchySize;
}
// write out LODs...
//
pMDXMHeader->ofsLODs = at - (byte *) pMDXMHeader;
for (int iLODIndex = 0; iLODIndex < pMDXMHeader->numLODs; iLODIndex++)
{
mdxmLOD_t *pLOD = (mdxmLOD_t *) at;
at += sizeof(mdxmLOD_t);
mdxmLODSurfOffset_t *pLODSurfOffsets = (mdxmLODSurfOffset_t *) at;
at += sizeof(mdxmLODSurfOffset_t) * pMDXMHeader->numSurfaces;
for (int iSurfaceIndex = 0; iSurfaceIndex < pMDXMHeader->numSurfaces; iSurfaceIndex++)
{
mdxmSurface_t *pSurface = (mdxmSurface_t *) at;
at += sizeof(mdxmSurface_t);
//
// store this offset...
//
pLODSurfOffsets->offsets[iSurfaceIndex] = (byte *)pSurface - (byte *) pLODSurfOffsets;
//
// fill in this surface struct...
//
pSurface->ident = 0; // ingame-use only, defaulted to 0 here
pSurface->thisSurfaceIndex = iSurfaceIndex;
pSurface->ofsHeader = (byte *)pMDXMHeader - (byte *)pSurface; // offset back to main header
pSurface->numVerts = G2Exporter_Surface_GetNumVerts(iSurfaceIndex, iLODIndex);
pSurface->numTriangles = G2Exporter_Surface_GetNumTris(iSurfaceIndex, iLODIndex);
// pSurface->maxVertBoneWeights= 1; // easy, eh? :-)
//
// write out triangles...
//
pSurface->ofsTriangles = at - (byte *)pSurface;
for (int iTriangleIndex = 0; iTriangleIndex < pSurface->numTriangles; iTriangleIndex++)
{
mdxmTriangle_t *pTriangle = (mdxmTriangle_t *) at;
at += sizeof(mdxmTriangle_t);
for (int i=0; i<3; i++)
{
pTriangle->indexes[i] = G2Exporter_Surface_GetTriIndex(iSurfaceIndex,iTriangleIndex,i,iLODIndex);
}
}
//
// write out verts...(when exporting from MD3 these are all weighted to only 1 bone)
//
pSurface->ofsVerts = at - (byte *)pSurface;
mdxmVertex_t *pVerts = (mdxmVertex_t *) at;
mdxmVertexTexCoord_t *pVertTexCoords = (mdxmVertexTexCoord_t *) &pVerts[pSurface->numVerts];
at = (unsigned char *) &pVertTexCoords[pSurface->numVerts]; // skip over all this vert-writing...
for (int iVertIndex = 0; iVertIndex < pSurface->numVerts; iVertIndex++)
{
mdxmVertex_t *pVert = &pVerts[iVertIndex];
memcpy(pVert->normal,G2Exporter_Surface_GetVertNormal(iSurfaceIndex,iVertIndex,iLODIndex),sizeof(vec3_t));
memcpy(pVert->vertCoords,G2Exporter_Surface_GetVertCoords(iSurfaceIndex,iVertIndex,iLODIndex),sizeof(vec3_t));
// memcpy(pVert->texCoords,G2Exporter_Surface_GetTexCoords (iSurfaceIndex,iVertIndex,iLODIndex),sizeof(vec2_t));
memcpy(pVertTexCoords[iVertIndex].texCoords,G2Exporter_Surface_GetTexCoords (iSurfaceIndex,iVertIndex,iLODIndex),sizeof(vec2_t));
memset(pVert->BoneWeightings,0,sizeof(pVert->BoneWeightings));
int iNumWeights = 1;
pVert->uiNmWeightsAndBoneIndexes = (iNumWeights-1)<<30;
int iWeight = 1023; // highest weighting currently allowed (1.000f as 10 bit fixed point)
pVert->BoneWeightings[0] = iWeight&0xFF; // float->byte
// the 2-bit pairs at 20,22,24,26 are the top 2 bits of each weighting (leaving bits 28&29 free)...
//
pVert->uiNmWeightsAndBoneIndexes |= (iWeight>>8)<<((0*2)+20);
pVert->BoneWeightings[0] = 255; // 1.0f weighting
// int iWeightTest = G2_GetVertWeights( pVert );
// int iBoneIndex = G2_GetVertBoneIndex( pVert, 0 );
// float fBoneWeight = G2_GetVertBoneWeight( pVert, 0 );
}
// remaining surface struct fields...
//
pSurface->numBoneReferences = 1;
pSurface->ofsBoneReferences = at - (byte *) pSurface;
int *piBonesUsed = (int *) at;
at += pSurface->numBoneReferences * sizeof(int);
piBonesUsed[0] = 0; // the one and only bone ref
pSurface->ofsEnd = at - (byte *) pSurface;
}
pLOD->ofsEnd = at - (byte *) pLOD;
}
pMDXMHeader->ofsEnd = at - (byte *) pMDXMHeader;
}
/*
// now create GLA file...
//
mdxaHeader_t *pMDXAHeader = (mdxaHeader_t *) at;
at += sizeof(mdxaHeader_t);
{// for brace-skipping...
pMDXAHeader->ident = MDXA_IDENT;
pMDXAHeader->version = MDXA_VERSION;
strncpy(pMDXAHeader->name,sLocalNameGLA,sizeof(pMDXAHeader->name));
pMDXAHeader->name[sizeof(pMDXAHeader->name)-1]='\0';
pMDXAHeader->fScale = 1.0f;
pMDXAHeader->numFrames = 1; // inherently, when doing MD3 to G2 files
// pMDXAHeader->ofsFrames = ??????????????????
pMDXAHeader->numBones = 1; // inherently, when doing MD3 to G2 files
// pMDXAHeader->ofsCompBonePool= ??????????????????
// pMDXAHeader->ofsSkel = ??????????????????
// pMDXAHeader->ofsEnd = ??????????????????
// write out bone hierarchy...
//
mdxaSkelOffsets_t * pSkelOffsets = (mdxaSkelOffsets_t *) at;
at += (int)( &((mdxaSkelOffsets_t *)0)->offsets[ pMDXAHeader->numBones ] );
pMDXAHeader->ofsSkel = at - (byte *) pMDXAHeader;
for (int iSkelIndex = 0; iSkelIndex < pMDXAHeader->numBones; iSkelIndex++)
{
mdxaSkel_t *pSkel = (mdxaSkel_t *) at;
pSkelOffsets->offsets[iSkelIndex] = (byte *) pSkel - (byte *) pSkelOffsets;
// setup flags...
//
pSkel->flags = 0;
strcpy( pSkel->name, "Generated by MD3View"); // doesn't matter what this is called I believe.
pSkel->parent= -1; // index of bone that is parent to this one, -1 = NULL/root
Matrix4 BasePose;
BasePose.Identity();
BasePose.To3x4(pSkel->BasePoseMat.matrix);
Matrix4 BasePoseInverse;
BasePoseInverse.Inverse(BasePose);
BasePoseInverse.To3x4(pSkel->BasePoseMatInv.matrix);
pSkel->numChildren = 0; // inherently, when doing MD3 to G2 files
int iThisSkelSize = (int)( &((mdxaSkel_t *)0)->children[ pSkel->numChildren ] );
at += iThisSkelSize;
}
// write out frames...
//
pMDXAHeader->ofsFrames = at - (byte *) pMDXAHeader;
int iFrameSize = (int)( &((mdxaFrame_t *)0)->boneIndexes[ pMDXAHeader->numBones ] );
for (int i=0; i<pMDXAHeader->numFrames; i++)
{
mdxaFrame_t *pFrame = (mdxaFrame_t *) at;
at += iFrameSize; // variable sized struct
pFrame->boneIndexes[0] = 0; // inherently, when doing MD3 to G2 files
}
// now write out compressed bone pool...
//
pMDXAHeader->ofsCompBonePool = at - (byte *) pMDXAHeader;
for (int iCompBoneIndex = 0; iCompBoneIndex < 1; iCompBoneIndex++)
{
mdxaCompBone_t *pCompBone = (mdxaCompBone_t *) at;
at += sizeof(mdxaCompBone_t);
float matThis[3][4];
Matrix4 Bone;
Bone.Identity();
Bone.To3x4(matThis);
byte Comp[sizeof(mdxaCompBone_t)];
MC_Compress(matThis,Comp);
memcpy(pCompBone->Comp,Comp,sizeof(Comp));
}
// int iPoolBytesUsed = (CompressedBones.size()*MC_COMP_BYTES) + (iStats_NumBoneRefs*sizeof(int));
// printf("( Compressed bone bytes: %d, using pool = %d. Saving = %d bytes )\n",iOldBoneSize,iPoolBytesUsed, iOldBoneSize - iPoolBytesUsed);
// done...
//
pMDXAHeader->ofsEnd = at - (byte *) pMDXAHeader;
}
*/
// write 'em out to disk...
//
// int iGLMSize = fwrite(pMDXMHeader,1,(byte *)pMDXAHeader - (byte *)pMDXMHeader, fhGLM);
int iGLMSize = fwrite(pMDXMHeader,1,pMDXMHeader->ofsEnd, fhGLM);
assert(iGLMSize == pMDXMHeader->ofsEnd);
// int iGLASize = fwrite(pMDXAHeader,1,at - (byte *)pMDXAHeader, fhGLA);
// assert(iGLASize == pMDXAHeader->ofsEnd);
// and finally...
//
free(pbBuffer);
}
else
{
psErrMess = va("Error! Unable to allocate %d bytes for workspace!\n",iMallocSize);
}
return psErrMess;
}
// assumes 'psFullPathedFilename' is full-pathed filenam with ".glm" on the end,
// ppsFullPathedNameGLA is optional feedback param if you want to know what the GLA name was.
//
// return = NULL for no error, else string of error for caller to display...
//
char *ExportGhoul2FromMD3(LPCSTR psFullPathedFilename, int iNumLODs, int iNumSurfaces, int iNumTags,
LPCSTR *ppsFullPathedNameGLA /* = NULL */
)
{
char *psErrorMess = NULL;
giNumLODs = iNumLODs;
giNumSurfaces = iNumSurfaces;
giNumTags = iNumTags;
FILE *fhGLM = fopen(psFullPathedFilename,"wb");
if ( fhGLM)
{
static char sNameGLA[MAX_PATH];
// strcpy(sNameGLA, Filename_WithoutExt(psFullPathedFilename) );
// strcat(sNameGLA, ".gla");
strcpy(sNameGLA,sDEFAULT_GLA_NAME);
// FILE *fhGLA = fopen(sNameGLA,"wb");
// if (fhGLA)
{
if ( ppsFullPathedNameGLA)
{
*ppsFullPathedNameGLA = &sNameGLA[0];
}
psErrorMess = ExportGhoul2FromMD3_Main(fhGLM, /*fhGLA,*/ psFullPathedFilename);
// fclose(fhGLA);
}
// else
// {
// psErrorMess = va("Error: Unable to open file '%s'!\n");
// }
fclose(fhGLM);
}
else
{
psErrorMess = va("Error: Unable to open file '%s'!\n");
}
return psErrorMess;
}
/////////////////// eof ////////////////////