4 //=================================================================
6 static void OrderSurfaces( void );
7 static void LoadBase( const char *filename );
8 static int LoadModelFile( const char *filename, polyset_t **ppsets, int *pnumpolysets );
10 #define MAX_SURFACE_TRIS (SHADER_MAX_INDEXES / 3)
11 #define MAX_SURFACE_VERTS SHADER_MAX_VERTEXES
13 #define MD3_TYPE_UNKNOWN 0
14 #define MD3_TYPE_BASE3DS 1
15 #define MD3_TYPE_SPRITE 2
16 #define MD3_TYPE_ASE 3
18 #define MAX_ANIM_FRAMES 512
19 #define MAX_ANIM_SURFACES 32
29 polyset_t *surfaces[MAX_ANIM_SURFACES];
31 } ObjectAnimationFrame_t;
45 //================================================================
50 md3Shader_t shaders[MD3_MAX_SHADERS];
51 // all verts (xyz_normal)
52 float *verts[MD3_MAX_FRAMES];
54 baseTriangle_t baseTriangles[MD3_MAX_TRIANGLES];
56 // the triangles will be sorted so that they form long generalized tristrips
57 int orderedTriangles[MD3_MAX_TRIANGLES][3];
58 int lodTriangles[MD3_MAX_TRIANGLES][3];
59 baseVertex_t baseVertexes[MD3_MAX_VERTS];
65 int skinwidth, skinheight;
67 md3SurfaceData_t surfData[MD3_MAX_SURFACES];
69 md3Tag_t tags[MD3_MAX_FRAMES][MD3_MAX_TAGS];
70 md3Frame_t frames[MD3_MAX_FRAMES];
73 float scale_up; // set by $scale
74 vec3_t adjust; // set by $origin
76 int fixedwidth, fixedheight; // set by $skinsize
80 int lowerSkipFrameStart, lowerSkipFrameEnd;
86 int type; // MD3_TYPE_BASE, MD3_TYPE_OLDBASE, MD3_TYPE_ASE, or MD3_TYPE_SPRITE
92 // the command list holds counts, the count * 3 xyz, st, normal indexes
93 // that are valid for every frame
95 char g_modelname[1024];
97 //==============================================================
104 void ClearModel (void)
108 g_data.type = MD3_TYPE_UNKNOWN;
110 for ( i = 0; i < MD3_MAX_SURFACES; i++ )
112 memset( &g_data.surfData[i].header, 0, sizeof( g_data.surfData[i].header ) );
113 memset( &g_data.surfData[i].shaders, 0, sizeof( g_data.surfData[i].shaders ) );
114 memset( &g_data.surfData[i].verts, 0, sizeof( g_data.surfData[i].verts ) );
117 memset( g_data.tags, 0, sizeof( g_data.tags ) );
119 for ( i = 0; i < g_data.model.numSurfaces; i++ )
123 for ( j = 0; j < g_data.surfData[i].header.numShaders; j++ )
125 memset( &g_data.surfData[i].shaders[j], 0, sizeof( g_data.surfData[i].shaders[j] ) );
128 memset (&g_data.model, 0, sizeof(g_data.model));
129 memset (g_cddir, 0, sizeof(g_cddir));
132 g_data.scale_up = 1.0;
133 memset( &g_data.model, 0, sizeof( g_data.model ) );
134 VectorCopy (vec3_origin, g_data.adjust);
135 g_data.fixedwidth = g_data.fixedheight = 0;
136 g_skipmodel = qfalse;
140 ** void WriteModelSurface( FILE *modelouthandle, md3SurfaceData_t *pSurfData )
142 ** This routine assumes that the file position has been adjusted
143 ** properly prior to entry to point at the beginning of the surface.
145 ** Since surface header information is completely relative, we can't
146 ** just randomly seek to an arbitrary surface location right now. Is
147 ** this something we should add?
149 void WriteModelSurface( FILE *modelouthandle, md3SurfaceData_t *pSurfData )
151 md3Surface_t *pSurf = &pSurfData->header;
152 md3Shader_t *pShader = pSurfData->shaders;
153 baseVertex_t *pBaseVertex = pSurfData->baseVertexes;
154 float **verts = pSurfData->verts;
156 short xyznormals[MD3_MAX_VERTS][4];
158 float base_st[MD3_MAX_VERTS][2];
159 md3Surface_t surftemp;
163 if ( strstr( pSurf->name, "tag_" ) == pSurf->name )
167 // write out the header
170 surftemp.ident = LittleLong( MD3_IDENT );
171 surftemp.flags = LittleLong( pSurf->flags );
172 surftemp.numFrames = LittleLong( pSurf->numFrames );
173 surftemp.numShaders = LittleLong( pSurf->numShaders );
175 surftemp.ofsShaders = LittleLong( pSurf->ofsShaders );
177 surftemp.ofsTriangles = LittleLong( pSurf->ofsTriangles );
178 surftemp.numTriangles = LittleLong( pSurf->numTriangles );
180 surftemp.ofsSt = LittleLong( pSurf->ofsSt );
181 surftemp.ofsXyzNormals = LittleLong( pSurf->ofsXyzNormals );
182 surftemp.ofsEnd = LittleLong( pSurf->ofsEnd );
184 SafeWrite( modelouthandle, &surftemp, sizeof( surftemp ) );
188 printf( "surface '%s'\n", pSurf->name );
189 printf( "...num shaders: %d\n", pSurf->numShaders );
195 for ( i = 0; i < pSurf->numShaders; i++ )
197 md3Shader_t shadertemp;
200 printf( "......'%s'\n", pShader[i].name );
202 shadertemp = pShader[i];
203 shadertemp.shaderIndex = LittleLong( shadertemp.shaderIndex );
204 SafeWrite( modelouthandle, &shadertemp, sizeof( shadertemp ) );
208 // write out the triangles
210 for ( i = 0 ; i < pSurf->numTriangles ; i++ )
212 for (j = 0 ; j < 3 ; j++)
214 int ivalue = LittleLong( pSurfData->orderedTriangles[i][j] );
215 pSurfData->orderedTriangles[i][j] = ivalue;
219 SafeWrite( modelouthandle, pSurfData->orderedTriangles, pSurf->numTriangles * sizeof( g_data.surfData[0].orderedTriangles[0] ) );
223 printf( "\n...num verts: %d\n", pSurf->numVerts );
224 printf( "...TEX COORDINATES\n" );
228 // write out the texture coordinates
230 for ( i = 0; i < pSurf->numVerts ; i++) {
231 base_st[i][0] = LittleFloat( pBaseVertex[i].st[0] );
232 base_st[i][1] = LittleFloat( pBaseVertex[i].st[1] );
234 printf( "......%d: %f,%f\n", i, base_st[i][0], base_st[i][1] );
236 SafeWrite( modelouthandle, base_st, pSurf->numVerts * sizeof(base_st[0]));
239 // write the xyz_normal
242 printf( "...XYZNORMALS\n" );
243 for ( f = 0; f < g_data.model.numFrames; f++ )
245 for (j=0 ; j< pSurf->numVerts; j++)
249 for (k=0 ; k < 3 ; k++)
251 value = ( short ) ( verts[f][j*6+k] / MD3_XYZ_SCALE );
252 xyznormals[j][k] = LittleShort( value );
254 NormalToLatLong( &verts[f][j*6+3], (byte *)&xyznormals[j][3] );
256 SafeWrite( modelouthandle, xyznormals, pSurf->numVerts * sizeof( short ) * 4 );
261 ** void WriteModelFile( FILE *modelouthandle )
264 ** header sizeof( md3Header_t )
265 ** frames sizeof( md3Frame_t ) * numFrames
266 ** tags sizeof( md3Tag_t ) * numFrames * numTags
267 ** surfaces surfaceSum
269 void WriteModelFile( FILE *modelouthandle )
273 md3Header_t modeltemp;
275 int numRealSurfaces = 0;
276 int numFrames = g_data.model.numFrames;
278 // compute offsets for all surfaces, sum their total size
279 for ( i = 0; i < g_data.model.numSurfaces; i++ )
281 if ( strstr( g_data.surfData[i].header.name, "tag_" ) != g_data.surfData[i].header.name )
283 md3Surface_t *psurf = &g_data.surfData[i].header;
285 if ( psurf->numTriangles == 0 || psurf->numVerts == 0 )
289 // the triangle and vertex split threshold is controlled by a parameter
290 // to $base, a la $base blah.3ds 1900, where "1900" determines the number
291 // of triangles to split on
293 else if ( psurf->numVerts > MAX_SURFACE_VERTS )
295 Error( "too many vertices\n" );
298 psurf->numFrames = numFrames;
300 psurf->ofsShaders = sizeof( md3Surface_t );
302 if ( psurf->numTriangles > MAX_SURFACE_TRIS )
304 Error( "too many faces\n" );
307 psurf->ofsTriangles = psurf->ofsShaders + psurf->numShaders * sizeof( md3Shader_t );
309 psurf->ofsSt = psurf->ofsTriangles + psurf->numTriangles * sizeof( md3Triangle_t );
310 psurf->ofsXyzNormals = psurf->ofsSt + psurf->numVerts * sizeof( md3St_t );
311 psurf->ofsEnd = psurf->ofsXyzNormals + psurf->numFrames * psurf->numVerts * ( sizeof( short ) * 4 );
313 surfaceSum += psurf->ofsEnd;
319 g_data.model.ident = MD3_IDENT;
320 g_data.model.version = MD3_VERSION;
322 g_data.model.ofsFrames = sizeof(md3Header_t);
323 g_data.model.ofsTags = g_data.model.ofsFrames + numFrames*sizeof(md3Frame_t);
324 g_data.model.ofsSurfaces = g_data.model.ofsTags + numFrames*g_data.model.numTags*sizeof(md3Tag_t);
325 g_data.model.ofsEnd = g_data.model.ofsSurfaces + surfaceSum;
328 // write out the model header
330 modeltemp = g_data.model;
331 modeltemp.ident = LittleLong( modeltemp.ident );
332 modeltemp.version = LittleLong( modeltemp.version );
333 modeltemp.numFrames = LittleLong( modeltemp.numFrames );
334 modeltemp.numTags = LittleLong( modeltemp.numTags );
335 modeltemp.numSurfaces = LittleLong( numRealSurfaces );
336 modeltemp.ofsFrames = LittleLong( modeltemp.ofsFrames );
337 modeltemp.ofsTags = LittleLong( modeltemp.ofsTags );
338 modeltemp.ofsSurfaces = LittleLong( modeltemp.ofsSurfaces );
339 modeltemp.ofsEnd = LittleLong( modeltemp.ofsEnd );
341 SafeWrite (modelouthandle, &modeltemp, sizeof(modeltemp));
344 // write out the frames
346 for (i=0 ; i < numFrames ; i++)
352 // compute localOrigin and radius
354 g_data.frames[i].localOrigin[0] =
355 g_data.frames[i].localOrigin[1] =
356 g_data.frames[i].localOrigin[2] = 0;
358 for ( j = 0; j < 8; j++ )
360 tmpVec[0] = g_data.frames[i].bounds[(j&1)!=0][0];
361 tmpVec[1] = g_data.frames[i].bounds[(j&2)!=0][1];
362 tmpVec[2] = g_data.frames[i].bounds[(j&4)!=0][2];
364 if ( VectorLength( tmpVec ) > maxRadius )
365 maxRadius = VectorLength( tmpVec );
368 g_data.frames[i].radius = LittleFloat( maxRadius );
371 for (j=0 ; j<3 ; j++) {
372 g_data.frames[i].bounds[0][j] = LittleFloat( g_data.frames[i].bounds[0][j] );
373 g_data.frames[i].bounds[1][j] = LittleFloat( g_data.frames[i].bounds[1][j] );
374 g_data.frames[i].localOrigin[j] = LittleFloat( g_data.frames[i].localOrigin[j] );
377 fseek (modelouthandle, g_data.model.ofsFrames, SEEK_SET);
378 SafeWrite( modelouthandle, g_data.frames, numFrames * sizeof(g_data.frames[0]) );
381 // write out the tags
383 fseek( modelouthandle, g_data.model.ofsTags, SEEK_SET );
384 for (f=0 ; f<g_data.model.numFrames; f++)
388 for ( t = 0; t < g_data.model.numTags; t++ )
390 g_data.tags[f][t].origin[0] = LittleFloat(g_data.tags[f][t].origin[0]);
391 g_data.tags[f][t].origin[1] = LittleFloat(g_data.tags[f][t].origin[1]);
392 g_data.tags[f][t].origin[2] = LittleFloat(g_data.tags[f][t].origin[2]);
394 for (j=0 ; j<3 ; j++)
396 g_data.tags[f][t].axis[0][j] = LittleFloat(g_data.tags[f][t].axis[0][j]);
397 g_data.tags[f][t].axis[1][j] = LittleFloat(g_data.tags[f][t].axis[1][j]);
398 g_data.tags[f][t].axis[2][j] = LittleFloat(g_data.tags[f][t].axis[2][j]);
401 SafeWrite( modelouthandle, g_data.tags[f], g_data.model.numTags * sizeof(md3Tag_t) );
405 // write out the surfaces
407 fseek( modelouthandle, g_data.model.ofsSurfaces, SEEK_SET );
408 for ( i = 0; i < g_data.model.numSurfaces; i++ )
410 WriteModelSurface( modelouthandle, &g_data.surfData[i] );
420 void FinishModel ( int type )
422 FILE *modelouthandle;
423 FILE *defaultSkinHandle;
427 if (!g_data.model.numFrames)
431 // build generalized triangle strips
435 if ( type == TYPE_PLAYER )
437 sprintf( name, "%s%s", writedir, g_modelname );
438 *strrchr( name, '.' ) = 0;
439 strcat( name, "_default.skin" );
441 defaultSkinHandle = fopen( name, "wt" );
442 for ( i = 0; i < g_data.model.numSurfaces; i++ )
444 fprintf( defaultSkinHandle, "%s,%s\n", g_data.surfData[i].header.name, g_data.surfData[i].shaders[0].name );
446 fclose( defaultSkinHandle );
449 sprintf (name, "%s%s", writedir, g_modelname);
452 // copy the model and its shaders to release directory tree
453 // if doing a release build
457 md3SurfaceData_t *pSurf;
459 ReleaseFile( g_modelname );
461 for ( i = 0; i < g_data.model.numSurfaces; i++ ) {
462 pSurf = &g_data.surfData[i];
463 for ( j = 0; j < g_data.model.numSkins; j++ ) {
464 ReleaseShader( pSurf->shaders[j].name );
471 // write the model output file
473 printf ("saving to %s\n", name);
475 modelouthandle = SafeOpenWrite (name);
477 WriteModelFile (modelouthandle);
479 printf ("%4d surfaces\n", g_data.model.numSurfaces);
480 printf ("%4d frames\n", g_data.model.numFrames);
481 printf ("%4d tags\n", g_data.model.numTags);
482 printf ("file size: %d\n", (int)ftell (modelouthandle) );
483 printf ("---------------------\n");
485 fclose (modelouthandle);
491 ** Reorders triangles in all the surfaces.
493 static void OrderSurfaces( void )
496 extern qboolean g_stripify;
498 // go through each surface and find best strip/fans possible
499 for ( s = 0; s < g_data.model.numSurfaces; s++ )
501 int mesh[MD3_MAX_TRIANGLES][3];
504 printf( "stripifying surface %d/%d with %d tris\n", s, g_data.model.numSurfaces, g_data.surfData[s].header.numTriangles );
506 for ( i = 0; i < g_data.surfData[s].header.numTriangles; i++ )
508 mesh[i][0] = g_data.surfData[s].lodTriangles[i][0];
509 mesh[i][1] = g_data.surfData[s].lodTriangles[i][1];
510 mesh[i][2] = g_data.surfData[s].lodTriangles[i][2];
515 OrderMesh( mesh, // input
516 g_data.surfData[s].orderedTriangles, // output
517 g_data.surfData[s].header.numTriangles );
521 memcpy( g_data.surfData[s].orderedTriangles, mesh, sizeof( int ) * 3 * g_data.surfData[s].header.numTriangles );
528 ===============================================================
532 ===============================================================
536 CopyTrianglesToBaseTriangles
540 static void CopyTrianglesToBaseTriangles(triangle_t *ptri, int numtri, baseTriangle_t *bTri )
543 // int width, height, iwidth, iheight, swidth;
544 // float s_scale, t_scale;
546 // vec3_t mins, maxs;
551 // find bounds of all the verts on the base frame
553 ClearBounds (mins, maxs);
555 for (i=0 ; i<numtri ; i++)
556 for (j=0 ; j<3 ; j++)
557 AddPointToBounds (ptri[i].verts[j], mins, maxs);
559 for (i=0 ; i<3 ; i++)
561 mins[i] = floor(mins[i]);
562 maxs[i] = ceil(maxs[i]);
565 width = maxs[0] - mins[0];
566 height = maxs[2] - mins[2];
568 if (!g_data.fixedwidth)
571 if (width*scale >= 150)
572 scale = 150.0 / width;
573 if (height*scale >= 190)
574 scale = 190.0 / height;
576 s_scale = t_scale = scale;
578 iwidth = ceil(width*s_scale);
579 iheight = ceil(height*t_scale);
586 iwidth = g_data.fixedwidth / 2;
587 iheight = g_data.fixedheight;
589 s_scale = (float)(iwidth-4) / width;
590 t_scale = (float)(iheight-4) / height;
593 // make the width a multiple of 4; some hardware requires this, and it ensures
594 // dword alignment for each scan
596 g_data.skinwidth = (swidth + 3) & ~3;
597 g_data.skinheight = iheight;
600 for (i=0; i<numtri ; i++, ptri++, bTri++)
604 for (j=0 ; j<3 ; j++)
606 pbasevert = ptri->verts[j];
608 VectorCopy( ptri->verts[j], bTri->v[j].xyz);
609 VectorCopy( ptri->normals[j], bTri->v[j].normal );
611 bTri->v[j].st[0] = ptri->texcoords[j][0];
612 bTri->v[j].st[1] = ptri->texcoords[j][1];
617 static void BuildBaseFrame( const char *filename, ObjectAnimationFrame_t *pOAF )
619 baseTriangle_t *bTri;
623 // calculate the base triangles
624 for ( i = 0; i < g_data.model.numSurfaces; i++ )
626 CopyTrianglesToBaseTriangles( pOAF->surfaces[i]->triangles,
627 pOAF->surfaces[i]->numtriangles,
628 g_data.surfData[i].baseTriangles );
630 strcpy( g_data.surfData[i].header.name, pOAF->surfaces[i]->name );
632 g_data.surfData[i].header.numTriangles = pOAF->surfaces[i]->numtriangles;
633 g_data.surfData[i].header.numVerts = 0;
636 if ( strstr( filename, gamedir + 1 ) )
638 strcpy( shaderName, strstr( filename, gamedir + 1 ) + strlen( gamedir ) - 1 );
642 strcpy( shaderName, filename );
645 if ( strrchr( shaderName, '/' ) )
646 *( strrchr( shaderName, '/' ) + 1 ) = 0;
649 strcpy( shaderName, pOAF->surfaces[i]->materialname );
651 strcpy( g_data.surfData[i].shaders[g_data.surfData[i].header.numShaders].name, pOAF->surfaces[i]->materialname );
653 g_data.surfData[i].header.numShaders++;
657 // compute unique vertices for each polyset
659 for ( i = 0; i < g_data.model.numSurfaces; i++ )
663 for ( t = 0; t < pOAF->surfaces[i]->numtriangles; t++ )
665 bTri = &g_data.surfData[i].baseTriangles[t];
667 for (j=0 ; j<3 ; j++)
674 for ( k = 0; k < g_data.surfData[i].header.numVerts; k++ )
676 if ( ( g_data.surfData[i].baseVertexes[k].st[0] == bVert->st[0] ) &&
677 ( g_data.surfData[i].baseVertexes[k].st[1] == bVert->st[1] ) &&
678 ( VectorCompare (bVert->xyz, g_data.surfData[i].baseVertexes[k].xyz) ) &&
679 ( VectorCompare (bVert->normal, g_data.surfData[i].baseVertexes[k].normal) ) )
681 break; // this vertex is already in the base vertex list
685 if (k == g_data.surfData[i].header.numVerts) { // new index
686 g_data.surfData[i].baseVertexes[g_data.surfData[i].header.numVerts] = *bVert;
687 g_data.surfData[i].header.numVerts++;
692 g_data.surfData[i].lodTriangles[t][j] = k;
700 for ( i = 0; i < g_data.model.numSurfaces; i++ )
702 if ( strstr( pOAF->surfaces[i]->name, "tag_" ) == pOAF->surfaces[i]->name )
704 if ( pOAF->surfaces[i]->numtriangles != 1 )
706 Error( "tag polysets must consist of only one triangle" );
708 if ( strstr( filename, "_flash.md3" ) && !strcmp( pOAF->surfaces[i]->name, "tag_parent" ) )
710 printf( "found tag '%s'\n", pOAF->surfaces[i]->name );
711 g_data.model.numTags++;
717 static int LoadModelFile( const char *filename, polyset_t **psets, int *numpolysets )
721 const char *frameFile;
723 printf ("---------------------\n");
724 if ( filename[1] != ':' )
726 frameFile = filename;
727 sprintf( file1, "%s/%s", g_cddir, frameFile );
731 strcpy( file1, filename );
734 time1 = FileTime (file1);
736 Error ("%s doesn't exist", file1);
739 // load the base triangles
741 *psets = Polyset_LoadSets( file1, numpolysets, g_data.maxSurfaceTris );
746 Polyset_SnapSets( *psets, *numpolysets );
748 if ( strstr( file1, ".3ds" ) || strstr( file1, ".3DS" ) )
749 return MD3_TYPE_BASE3DS;
751 Error( "Unknown model file type" );
753 return MD3_TYPE_UNKNOWN;
761 void Cmd_Base( void )
766 sprintf( filename, "%s/%s", g_cddir, token );
767 LoadBase( filename );
770 static void LoadBase( const char *filename )
775 ObjectAnimationFrame_t oaf;
777 // determine polyset splitting threshold
778 if ( TokenAvailable() )
781 g_data.maxSurfaceTris = atoi( token );
785 g_data.maxSurfaceTris = MAX_SURFACE_TRIS - 1;
788 g_data.type = LoadModelFile( filename, &psets, &numpolysets );
790 Polyset_ComputeNormals( psets, numpolysets );
792 g_data.model.numSurfaces = numpolysets;
794 memset( &oaf, 0, sizeof( oaf ) );
796 for ( i = 0; i < numpolysets; i++ )
798 oaf.surfaces[i] = &psets[i];
799 oaf.numSurfaces = numpolysets;
802 BuildBaseFrame( filename, &oaf );
804 free( psets[0].triangles );
812 $spritebase xorg yorg width height
814 Generate a single square for the model
817 void Cmd_SpriteBase (void)
819 float xl, yl, width, height;
821 g_data.type = MD3_TYPE_SPRITE;
830 height = atof(token);
832 // if (g_skipmodel || g_release || g_archive)
835 printf ("---------------------\n");
837 g_data.surfData[0].verts[0] = ( float * ) calloc( 1, sizeof( float ) * 6 * 4 );
839 g_data.surfData[0].header.numVerts = 4;
841 g_data.surfData[0].verts[0][0+0] = 0;
842 g_data.surfData[0].verts[0][0+1] = -xl;
843 g_data.surfData[0].verts[0][0+2] = yl + height;
845 g_data.surfData[0].verts[0][0+3] = -1;
846 g_data.surfData[0].verts[0][0+4] = 0;
847 g_data.surfData[0].verts[0][0+5] = 0;
848 g_data.surfData[0].baseVertexes[0].st[0] = 0;
849 g_data.surfData[0].baseVertexes[0].st[1] = 0;
852 g_data.surfData[0].verts[0][6+0] = 0;
853 g_data.surfData[0].verts[0][6+1] = -xl - width;
854 g_data.surfData[0].verts[0][6+2] = yl + height;
856 g_data.surfData[0].verts[0][6+3] = -1;
857 g_data.surfData[0].verts[0][6+4] = 0;
858 g_data.surfData[0].verts[0][6+5] = 0;
859 g_data.surfData[0].baseVertexes[1].st[0] = 1;
860 g_data.surfData[0].baseVertexes[1].st[1] = 0;
863 g_data.surfData[0].verts[0][12+0] = 0;
864 g_data.surfData[0].verts[0][12+1] = -xl - width;
865 g_data.surfData[0].verts[0][12+2] = yl;
867 g_data.surfData[0].verts[0][12+3] = -1;
868 g_data.surfData[0].verts[0][12+4] = 0;
869 g_data.surfData[0].verts[0][12+5] = 0;
870 g_data.surfData[0].baseVertexes[2].st[0] = 1;
871 g_data.surfData[0].baseVertexes[2].st[1] = 1;
874 g_data.surfData[0].verts[0][18+0] = 0;
875 g_data.surfData[0].verts[0][18+1] = -xl;
876 g_data.surfData[0].verts[0][18+2] = yl;
878 g_data.surfData[0].verts[0][18+3] = -1;
879 g_data.surfData[0].verts[0][18+4] = 0;
880 g_data.surfData[0].verts[0][18+5] = 0;
881 g_data.surfData[0].baseVertexes[3].st[0] = 0;
882 g_data.surfData[0].baseVertexes[3].st[1] = 1;
884 g_data.surfData[0].lodTriangles[0][0] = 0;
885 g_data.surfData[0].lodTriangles[0][1] = 1;
886 g_data.surfData[0].lodTriangles[0][2] = 2;
888 g_data.surfData[0].lodTriangles[1][0] = 2;
889 g_data.surfData[0].lodTriangles[1][1] = 3;
890 g_data.surfData[0].lodTriangles[1][2] = 0;
892 g_data.model.numSurfaces = 1;
894 g_data.surfData[0].header.numTriangles = 2;
895 g_data.surfData[0].header.numVerts = 4;
897 g_data.model.numFrames = 1;
901 ===========================================================================
905 ===========================================================================
913 void GrabFrame (const char *frame)
921 const char *framefile;
923 qboolean parentTagExists = qfalse;
928 // the frame 'run1' will be looked for as either
929 // run.1 or run1.tri, so the new alias sequence save
930 // feature an be used
931 if ( frame[1] != ':' )
933 // framefile = FindFrameFile (frame);
935 sprintf (file1, "%s/%s",g_cddir, framefile);
939 strcpy( file1, frame );
941 printf ("grabbing %s\n", file1);
943 if (g_data.model.numFrames >= MD3_MAX_FRAMES)
944 Error ("model.numFrames >= MD3_MAX_FRAMES");
945 fr = &g_data.frames[g_data.model.numFrames];
947 strcpy (fr->name, frame);
949 psets = Polyset_LoadSets( file1, &numpolysets, g_data.maxSurfaceTris );
954 Polyset_SnapSets( psets, numpolysets );
957 // compute vertex normals
959 Polyset_ComputeNormals( psets, numpolysets );
962 // flip everything to compensate for the alias coordinate system
963 // and perform global scale and adjust
965 for ( i = 0; i < g_data.model.numSurfaces; i++ )
967 triangle_t *ptri = psets[i].triangles;
970 for ( t = 0; t < psets[i].numtriangles; t++ )
973 for ( j = 0; j < 3; j++ )
977 for ( k = 0 ; k < 3 ; k++ ) {
978 ptri[t].verts[j][k] = ptri[t].verts[j][k] * g_data.scale_up +
981 if ( ptri[t].verts[j][k] > 1023 ||
982 ptri[t].verts[j][k] < -1023 )
984 Error( "Model extents too large" );
992 // find and count tags, locate parent tag
994 for ( i = 0; i < numpolysets; i++ )
996 if ( strstr( psets[i].name, "tag_" ) == psets[i].name )
998 if ( strstr( psets[i].name, "tag_parent" ) == psets[i].name )
1000 if ( strstr( psets[i].name, "tag_parent" ) )
1004 if ( parentTagExists )
1005 Error( "Multiple parent tags not allowed" );
1007 memcpy( tri[0], psets[i].triangles[0].verts[0], sizeof( float ) * 3 );
1008 memcpy( tri[1], psets[i].triangles[0].verts[1], sizeof( float ) * 3 );
1009 memcpy( tri[2], psets[i].triangles[0].verts[2], sizeof( float ) * 3 );
1011 MD3_ComputeTagFromTri( &tagParent, tri );
1012 strcpy( tagParent.name, psets[i].name );
1013 g_data.tags[g_data.model.numFrames][numtags] = tagParent;
1014 parentTagExists = qtrue;
1021 if ( strcmp( psets[i].name, g_data.surfData[i].header.name ) )
1023 Error( "Mismatched surfaces from base('%s') to frame('%s') in model '%s'\n", g_data.surfData[i].header.name, psets[i].name, g_modelname );
1027 if ( numtags != g_data.model.numTags )
1029 Error( "mismatched number of tags in frame(%d) vs. base(%d)", numtags, g_data.model.numTags );
1032 if ( numpolysets != g_data.model.numSurfaces )
1034 Error( "mismatched number of surfaces in frame(%d) vs. base(%d)", numpolysets-numtags, g_data.model.numSurfaces );
1038 // prepare to accumulate bounds and normals
1040 ClearBounds( fr->bounds[0], fr->bounds[1] );
1043 // store the frame's vertices in the same order as the base. This assumes the
1044 // triangles and vertices in this frame are in exactly the same order as in the
1047 for ( i = 0, tagcount = 0; i < numpolysets; i++ )
1050 triangle_t *pTris = psets[i].triangles;
1052 strcpy( g_data.surfData[i].header.name, psets[i].name );
1055 // parent tag adjust
1057 if ( parentTagExists ) {
1058 for ( t = 0; t < psets[i].numtriangles; t++ )
1060 for ( j = 0; j < 3 ; j++ )
1064 VectorSubtract( pTris[t].verts[j], tagParent.origin, tmp );
1066 pTris[t].verts[j][0] = DotProduct( tmp, tagParent.axis[0] );
1067 pTris[t].verts[j][1] = DotProduct( tmp, tagParent.axis[1] );
1068 pTris[t].verts[j][2] = DotProduct( tmp, tagParent.axis[2] );
1070 VectorCopy( pTris[t].normals[j], tmp );
1071 pTris[t].normals[j][0] = DotProduct( tmp, tagParent.axis[0] );
1072 pTris[t].normals[j][1] = DotProduct( tmp, tagParent.axis[1] );
1073 pTris[t].normals[j][2] = DotProduct( tmp, tagParent.axis[2] );
1081 if ( strstr( psets[i].name, "tag_" ) == psets[i].name )
1083 md3Tag_t *pTag = &g_data.tags[g_data.model.numFrames][tagcount];
1086 strcpy( pTag->name, psets[i].name );
1088 memcpy( tri[0], pTris[0].verts[0], sizeof( float ) * 3 );
1089 memcpy( tri[1], pTris[0].verts[1], sizeof( float ) * 3 );
1090 memcpy( tri[2], pTris[0].verts[2], sizeof( float ) * 3 );
1092 MD3_ComputeTagFromTri( pTag, tri );
1097 if ( g_data.surfData[i].verts[g_data.model.numFrames] )
1098 free( g_data.surfData[i].verts[g_data.model.numFrames] );
1099 frameXyz = g_data.surfData[i].verts[g_data.model.numFrames] = calloc( 1, sizeof( float ) * 6 * g_data.surfData[i].header.numVerts );
1100 frameNormals = frameXyz + 3;
1102 for ( t = 0; t < psets[i].numtriangles; t++ )
1104 for ( j = 0; j < 3 ; j++ )
1108 index = g_data.surfData[i].baseTriangles[t].v[j].index;
1109 frameXyz[index*6+0] = pTris[t].verts[j][0];
1110 frameXyz[index*6+1] = pTris[t].verts[j][1];
1111 frameXyz[index*6+2] = pTris[t].verts[j][2];
1112 frameNormals[index*6+0] = pTris[t].normals[j][0];
1113 frameNormals[index*6+1] = pTris[t].normals[j][1];
1114 frameNormals[index*6+2] = pTris[t].normals[j][2];
1115 AddPointToBounds (&frameXyz[index*6], fr->bounds[0], fr->bounds[1] );
1121 g_data.model.numFrames++;
1123 // only free the first triangle array, all of the psets in this array share the
1124 // same triangle pool!!!
1125 // free( psets[0].triangles );
1129 //===========================================================================
1138 void Cmd_Frame (void)
1140 while (TokenAvailable())
1145 if (g_release || g_archive)
1147 g_data.model.numFrames = 1; // don't skip the writeout
1162 void SkinFrom3DS( const char *filename )
1169 _3DS_LoadPolysets( filename, &psets, &numPolysets, g_verbose );
1171 for ( i = 0; i < numPolysets; i++ )
1174 if ( strstr( filename, gamedir + 1 ) )
1176 strcpy( name, strstr( filename, gamedir + 1 ) + strlen( gamedir ) - 1 );
1180 strcpy( name, filename );
1183 if ( strrchr( name, '/' ) )
1184 *( strrchr( name, '/' ) + 1 ) = 0;
1186 strcpy( name, psets[i].materialname );
1187 strcpy( g_data.surfData[i].shaders[g_data.surfData[i].header.numShaders].name, name );
1189 g_data.surfData[i].header.numShaders++;
1192 free( psets[0].triangles );
1196 void Cmd_Skin (void)
1198 char skinfile[1024];
1200 if ( g_data.type == MD3_TYPE_BASE3DS )
1204 sprintf( skinfile, "%s/%s", g_cddir, token );
1206 if ( strstr( token, ".3ds" ) || strstr( token, ".3DS" ) )
1208 SkinFrom3DS( skinfile );
1212 Error( "Unknown file format for $skin '%s'\n", skinfile );
1217 Error( "invalid model type while processing $skin" );
1220 g_data.model.numSkins++;
1228 This routine is also called for $oldskin
1231 void Cmd_SpriteShader()
1234 strcpy( g_data.surfData[0].shaders[g_data.surfData[0].header.numShaders].name, token );
1235 g_data.surfData[0].header.numShaders++;
1236 g_data.model.numSkins++;
1244 void Cmd_Origin (void)
1246 // rotate points into frame of reference so model points down the
1248 // FIXME: use alias native coordinate system
1250 g_data.adjust[1] = -atof (token);
1253 g_data.adjust[0] = atof (token);
1256 g_data.adjust[2] = -atof (token);
1265 void Cmd_ScaleUp (void)
1268 g_data.scale_up = atof (token);
1269 if (g_skipmodel || g_release || g_archive)
1272 printf ("Scale up: %f\n", g_data.scale_up);
1280 Set a skin size other than the default
1284 void Cmd_Skinsize (void)
1287 g_data.fixedwidth = atoi(token);
1289 g_data.fixedheight = atoi(token);
1296 Begin creating a model of the given name
1299 void Cmd_Modelname (void)
1301 FinishModel ( TYPE_UNKNOWN );
1305 strcpy (g_modelname, token);
1306 StripExtension (g_modelname);
1307 strcat (g_modelname, ".md3");
1308 strcpy (g_data.model.name, g_modelname);
1319 Error ("$cd command without a $modelname");
1324 sprintf ( g_cddir, "%s%s", gamedir, token);
1326 // if -only was specified and this cd doesn't match,
1327 // skip the model (you only need to match leading chars,
1328 // so you could regrab all monsters with -only models/monsters)
1331 if (strncmp(token, g_only, strlen(g_only)))
1333 g_skipmodel = qtrue;
1334 printf ("skipping %s\n", token);
1338 void Convert3DStoMD3( const char *file )
1342 SkinFrom3DS( file );
1344 strcpy( g_data.model.name, g_modelname );
1346 FinishModel( TYPE_UNKNOWN );
1353 void Cmd_3DSConvert()
1357 FinishModel( TYPE_UNKNOWN );
1362 sprintf( file, "%s%s", gamedir, token );
1363 strcpy( g_modelname, token );
1364 if ( strrchr( g_modelname, '.' ) )
1365 *strrchr( g_modelname, '.' ) = 0;
1366 strcat( g_modelname, ".md3" );
1368 if ( FileTime( file ) == -1 )
1369 Error( "%s doesn't exist", file );
1371 if ( TokenAvailable() )
1374 g_data.scale_up = atof( token );
1377 Convert3DStoMD3( file );
1380 static void ConvertASE( const char *filename, int type, qboolean grabAnims );
1385 void Cmd_ASEConvert( qboolean grabAnims )
1387 char filename[1024];
1388 int type = TYPE_ITEM;
1390 FinishModel( TYPE_UNKNOWN );
1394 sprintf( filename, "%s%s", gamedir, token );
1396 strcpy (g_modelname, token);
1397 StripExtension (g_modelname);
1398 strcat (g_modelname, ".md3");
1399 strcpy (g_data.model.name, g_modelname);
1401 if ( !strstr( filename, ".ase" ) && !strstr( filename, ".ASE" ) )
1402 strcat( filename, ".ASE" );
1404 g_data.maxSurfaceTris = MAX_SURFACE_TRIS - 1;
1406 while ( TokenAvailable() )
1409 if ( !strcmp( token, "-origin" ) )
1411 if ( !TokenAvailable() )
1412 Error( "missing parameter for -origin" );
1414 g_data.aseAdjust[1] = -atof( token );
1416 if ( !TokenAvailable() )
1417 Error( "missing parameter for -origin" );
1419 g_data.aseAdjust[0] = atof (token);
1421 if ( !TokenAvailable() )
1422 Error( "missing parameter for -origin" );
1424 g_data.aseAdjust[2] = -atof (token);
1426 else if ( !strcmp( token, "-lod" ) )
1428 if ( !TokenAvailable() )
1429 Error( "No parameter for -lod" );
1431 g_data.currentLod = atoi( token );
1432 if ( g_data.currentLod > MD3_MAX_LODS - 1 )
1434 Error( "-lod parameter too large! (%d)\n", g_data.currentLod );
1437 if ( !TokenAvailable() )
1438 Error( "No second parameter for -lod" );
1440 g_data.lodBias = atof( token );
1442 else if ( !strcmp( token, "-maxtris" ) )
1444 if ( !TokenAvailable() )
1445 Error( "No parameter for -maxtris" );
1447 g_data.maxSurfaceTris = atoi( token );
1449 else if ( !strcmp( token, "-playerparms" ) )
1451 if ( !TokenAvailable() )
1452 Error( "missing skip start parameter for -playerparms" );
1454 g_data.lowerSkipFrameStart = atoi( token );
1457 if ( !TokenAvailable() )
1458 Error( "missing skip end parameter for -playerparms" );
1460 g_data.lowerSkipFrameEnd = atoi( token );
1463 if ( !TokenAvailable() )
1464 Error( "missing upper parameter for -playerparms" );
1466 g_data.maxUpperFrames = atoi( token );
1468 g_data.lowerSkipFrameEnd = g_data.maxUpperFrames - 1;
1471 if ( !TokenAvailable() )
1472 Error( "missing head parameter for -playerparms" );
1474 g_data.maxHeadFrames = atoi( token );
1476 g_data.maxHeadFrames = 1;
1478 if ( type != TYPE_ITEM )
1479 Error( "invalid argument" );
1483 else if ( !strcmp( token, "-weapon" ) )
1485 if ( type != TYPE_ITEM )
1486 Error( "invalid argument" );
1492 g_data.type = MD3_TYPE_ASE;
1494 if ( type == TYPE_WEAPON && grabAnims )
1496 Error( "can't grab anims with weapon models" );
1498 if ( type == TYPE_PLAYER && !grabAnims )
1500 Error( "player models must be converted with $aseanimconvert" );
1503 if ( type == TYPE_WEAPON )
1505 ConvertASE( filename, type, qfalse );
1506 ConvertASE( filename, TYPE_HAND, qtrue );
1510 ConvertASE( filename, type, grabAnims );
1514 static int GetSurfaceAnimations( SurfaceAnimation_t sanims[MAX_ANIM_SURFACES],
1522 int numValidSurfaces;
1526 if ( ( numSurfaces = ASE_GetNumSurfaces() ) > MAX_ANIM_SURFACES )
1528 Error( "Too many surfaces in ASE" );
1531 for ( numValidSurfaces = 0, i = 0; i < numSurfaces; i++ )
1533 polyset_t *splitSets;
1535 const char *surfaceName = ASE_GetSurfaceName( i );
1540 // Error( "Missing animation frames in model" );
1543 if ( strstr( surfaceName, "tag_" ) ||
1544 !strcmp( part, "any" ) ||
1545 ( strstr( surfaceName, part ) == surfaceName ) )
1548 // skip this if it's an inappropriate tag
1549 if ( strcmp( part, "any" ) )
1551 // ignore non-"tag_head" tags if this is the head
1552 if ( !strcmp( part, "h_" ) && strstr( surfaceName, "tag_" ) && strcmp( surfaceName, "tag_head" ) )
1554 // ignore "tag_head" if this is the legs
1555 if ( !strcmp( part, "l_" ) && !strcmp( surfaceName, "tag_head" ) )
1557 // ignore "tag_weapon" if this is the legs
1558 if ( !strcmp( part, "l_" ) && !strcmp( surfaceName, "tag_weapon" ) )
1562 if ( ( sanims[numValidSurfaces].frames = ASE_GetSurfaceAnimation( i, &sanims[numValidSurfaces].numFrames, skipFrameStart, skipFrameEnd, maxFrames ) ) != 0 )
1564 splitSets = Polyset_SplitSets( sanims[numValidSurfaces].frames, sanims[numValidSurfaces].numFrames, &numNewFrames, g_data.maxSurfaceTris );
1566 if ( numFrames == -1 )
1567 numFrames = sanims[numValidSurfaces].numFrames;
1568 else if ( numFrames != sanims[numValidSurfaces].numFrames )
1569 Error( "Different number of animation frames on surfaces" );
1571 if ( sanims[numValidSurfaces].frames != splitSets )
1575 // free old data if we split the surfaces
1576 for ( j = 0; j < sanims[numValidSurfaces].numFrames; j++ )
1578 free( sanims[numValidSurfaces].frames[j].triangles );
1579 free( sanims[numValidSurfaces].frames );
1582 sanims[numValidSurfaces].frames = splitSets;
1583 sanims[numValidSurfaces].numFrames = numNewFrames;
1585 Polyset_SnapSets( sanims[numValidSurfaces].frames, sanims[numValidSurfaces].numFrames );
1586 Polyset_ComputeNormals( sanims[numValidSurfaces].frames, sanims[numValidSurfaces].numFrames );
1593 return numValidSurfaces;
1596 static int SurfaceOrderToFrameOrder( SurfaceAnimation_t sanims[], ObjectAnimationFrame_t oanims[], int numSurfaces )
1602 ** we have the data here arranged in surface order, now we need to convert it to
1605 for ( i = 0, s = 0; i < numSurfaces; i++ )
1609 if ( sanims[i].frames )
1611 if ( numFrames == -1 )
1612 numFrames = sanims[i].numFrames;
1613 else if ( numFrames != sanims[i].numFrames )
1614 Error( "numFrames != sanims[i].numFrames (%d != %d)\n", numFrames, sanims[i].numFrames );
1616 for ( j = 0; j < sanims[i].numFrames; j++ )
1618 oanims[j].surfaces[s] = &sanims[i].frames[j];
1619 oanims[j].numSurfaces = numSurfaces;
1628 static void WriteMD3( const char *_filename, ObjectAnimationFrame_t oanims[], int numFrames )
1630 char filename[1024];
1632 strcpy( filename, _filename );
1633 if ( strchr( filename, '.' ) )
1634 *strchr( filename, '.' ) = 0;
1635 strcat( filename, ".md3" );
1638 static void BuildAnimationFromOAFs( const char *filename, ObjectAnimationFrame_t oanims[], int numFrames, int type )
1640 int f, i, j, tagcount;
1642 float *frameNormals;
1644 g_data.model.numSurfaces = oanims[0].numSurfaces;
1645 g_data.model.numFrames = numFrames;
1646 if ( g_data.model.numFrames < 0)
1647 Error ("model.numFrames < 0");
1648 if ( g_data.model.numFrames >= MD3_MAX_FRAMES)
1649 Error ("model.numFrames >= MD3_MAX_FRAMES");
1652 BuildBaseFrame( filename, &oanims[0] );
1654 // build animation frames
1655 for ( f = 0; f < numFrames; f++ )
1657 ObjectAnimationFrame_t *pOAF = &oanims[f];
1658 qboolean parentTagExists = qfalse;
1663 fr = &g_data.frames[f];
1665 strcpy( fr->name, "(from ASE)" );
1667 // scale and adjust frame
1668 for ( i = 0; i < pOAF->numSurfaces; i++ )
1670 triangle_t *pTris = pOAF->surfaces[i]->triangles;
1673 for ( t = 0; t < pOAF->surfaces[i]->numtriangles; t++ )
1675 for ( j = 0; j < 3; j++ )
1680 for ( k = 0 ; k < 3 ; k++ ) {
1681 pTris[t].verts[j][k] = pTris[t].verts[j][k] * g_data.scale_up +
1682 g_data.aseAdjust[k];
1684 if ( pTris[t].verts[j][k] > 1023 ||
1685 pTris[t].verts[j][k] < -1023 )
1687 Error( "Model extents too large" );
1695 // find and count tags, locate parent tag
1697 for ( i = 0; i < pOAF->numSurfaces; i++ )
1699 if ( strstr( pOAF->surfaces[i]->name, "tag_" ) == pOAF->surfaces[i]->name )
1701 // ignore parent tags when grabbing a weapon model and this is the flash portion
1702 if ( !strcmp( pOAF->surfaces[i]->name, "tag_parent" ) && strstr( filename, "_flash.md3" ) )
1706 else if ( !strstr( filename, "_hand.md3" ) && (
1707 ( !strcmp( pOAF->surfaces[i]->name, "tag_parent" ) && !strstr( filename, "_flash.md3" ) ) ||
1708 ( !strcmp( pOAF->surfaces[i]->name, "tag_torso" ) && ( strstr( filename, "upper_" ) || strstr( filename, "upper.md3" ) ) ) ||
1709 ( !strcmp( pOAF->surfaces[i]->name, "tag_head" ) && ( strstr( filename, "head.md3" ) || strstr( filename, "head_" ) ) ) ||
1710 ( !strcmp( pOAF->surfaces[i]->name, "tag_flash" ) && strstr( filename, "_flash.md3" ) )||
1711 ( !strcmp( pOAF->surfaces[i]->name, "tag_weapon" ) && type == TYPE_WEAPON ) ) )
1715 if ( parentTagExists )
1716 Error( "Multiple parent tags not allowed" );
1718 memcpy( tri[0], pOAF->surfaces[i]->triangles[0].verts[0], sizeof( float ) * 3 );
1719 memcpy( tri[1], pOAF->surfaces[i]->triangles[0].verts[1], sizeof( float ) * 3 );
1720 memcpy( tri[2], pOAF->surfaces[i]->triangles[0].verts[2], sizeof( float ) * 3 );
1722 MD3_ComputeTagFromTri( &tagParent, tri );
1723 strcpy( tagParent.name, "tag_parent" );
1724 g_data.tags[f][numtags] = tagParent;
1725 parentTagExists = qtrue;
1731 memcpy( tri[0], pOAF->surfaces[i]->triangles[0].verts[0], sizeof( float ) * 3 );
1732 memcpy( tri[1], pOAF->surfaces[i]->triangles[0].verts[1], sizeof( float ) * 3 );
1733 memcpy( tri[2], pOAF->surfaces[i]->triangles[0].verts[2], sizeof( float ) * 3 );
1735 MD3_ComputeTagFromTri( &g_data.tags[f][numtags], tri );
1736 strcpy( g_data.tags[f][numtags].name, pOAF->surfaces[i]->name );
1737 if ( strstr( g_data.tags[f][numtags].name, "tag_flash" ) )
1738 * ( strstr( g_data.tags[f][numtags].name, "tag_flash" ) + strlen( "tag_flash" ) ) = 0;
1744 if ( strcmp( pOAF->surfaces[i]->name, g_data.surfData[i].header.name ) )
1746 Error( "Mismatched surfaces from base('%s') to frame('%s') in model '%s'\n", g_data.surfData[i].header.name, pOAF->surfaces[i]->name, filename );
1750 if ( numtags != g_data.model.numTags )
1752 Error( "mismatched number of tags in frame(%d) vs. base(%d)", numtags, g_data.model.numTags );
1756 // prepare to accumulate bounds and normals
1758 ClearBounds( fr->bounds[0], fr->bounds[1] );
1761 // store the frame's vertices in the same order as the base. This assumes the
1762 // triangles and vertices in this frame are in exactly the same order as in the
1765 for ( i = 0, tagcount = 0; i < pOAF->numSurfaces; i++ )
1768 triangle_t *pTris = pOAF->surfaces[i]->triangles;
1771 // parent tag adjust
1773 if ( parentTagExists )
1775 for ( t = 0; t < pOAF->surfaces[i]->numtriangles; t++ )
1777 for ( j = 0; j < 3 ; j++ )
1781 VectorSubtract( pTris[t].verts[j], tagParent.origin, tmp );
1783 pTris[t].verts[j][0] = DotProduct( tmp, tagParent.axis[0] );
1784 pTris[t].verts[j][1] = DotProduct( tmp, tagParent.axis[1] );
1785 pTris[t].verts[j][2] = DotProduct( tmp, tagParent.axis[2] );
1787 VectorCopy( pTris[t].normals[j], tmp );
1788 pTris[t].normals[j][0] = DotProduct( tmp, tagParent.axis[0] );
1789 pTris[t].normals[j][1] = DotProduct( tmp, tagParent.axis[1] );
1790 pTris[t].normals[j][2] = DotProduct( tmp, tagParent.axis[2] );
1798 if ( strstr( pOAF->surfaces[i]->name, "tag_" ) == pOAF->surfaces[i]->name )
1800 md3Tag_t *pTag = &g_data.tags[f][tagcount];
1803 strcpy( pTag->name, pOAF->surfaces[i]->name );
1805 memcpy( tri[0], pTris[0].verts[0], sizeof( float ) * 3 );
1806 memcpy( tri[1], pTris[0].verts[1], sizeof( float ) * 3 );
1807 memcpy( tri[2], pTris[0].verts[2], sizeof( float ) * 3 );
1809 MD3_ComputeTagFromTri( pTag, tri );
1814 if ( g_data.surfData[i].verts[f] )
1815 free( g_data.surfData[i].verts[f] );
1816 frameXyz = g_data.surfData[i].verts[f] = calloc( 1, sizeof( float ) * 6 * g_data.surfData[i].header.numVerts );
1817 frameNormals = frameXyz + 3;
1819 for ( t = 0; t < pOAF->surfaces[i]->numtriangles; t++ )
1821 for ( j = 0; j < 3 ; j++ )
1825 index = g_data.surfData[i].baseTriangles[t].v[j].index;
1826 frameXyz[index*6+0] = pTris[t].verts[j][0];
1827 frameXyz[index*6+1] = pTris[t].verts[j][1];
1828 frameXyz[index*6+2] = pTris[t].verts[j][2];
1829 frameNormals[index*6+0] = pTris[t].normals[j][0];
1830 frameNormals[index*6+1] = pTris[t].normals[j][1];
1831 frameNormals[index*6+2] = pTris[t].normals[j][2];
1832 AddPointToBounds (&frameXyz[index*6], fr->bounds[0], fr->bounds[1] );
1839 if ( strstr( filename, gamedir + 1 ) )
1841 strcpy( g_modelname, strstr( filename, gamedir + 1 ) + strlen( gamedir ) - 1 );
1845 strcpy( g_modelname, filename );
1848 FinishModel( type );
1852 static void ConvertASE( const char *filename, int type, qboolean grabAnims )
1857 SurfaceAnimation_t surfaceAnimations[MAX_ANIM_SURFACES];
1858 ObjectAnimationFrame_t objectAnimationFrames[MAX_ANIM_FRAMES];
1859 char outfilename[1024];
1862 ** load ASE into memory
1864 ASE_Load( filename, g_verbose, grabAnims );
1869 if ( type == TYPE_ITEM )
1871 numSurfaces = GetSurfaceAnimations( surfaceAnimations, "any", -1, -1, -1 );
1873 if ( numSurfaces <= 0 )
1874 Error( "numSurfaces <= 0" );
1876 numFrames = SurfaceOrderToFrameOrder( surfaceAnimations, objectAnimationFrames, numSurfaces );
1878 if ( numFrames <= 0 )
1879 Error( "numFrames <= 0" );
1881 strcpy( outfilename, filename );
1882 if ( strrchr( outfilename, '.' ) )
1883 *( strrchr( outfilename, '.' ) + 1 ) = 0;
1884 strcat( outfilename, "md3" );
1885 BuildAnimationFromOAFs( outfilename, objectAnimationFrames, numFrames, type );
1888 for ( i = 0; i < numSurfaces; i++ )
1890 if ( surfaceAnimations[i].frames )
1892 for ( j = 0; j < surfaceAnimations[i].numFrames; j++ )
1894 free( surfaceAnimations[i].frames[j].triangles );
1896 free( surfaceAnimations[i].frames );
1897 surfaceAnimations[i].frames = 0;
1901 else if ( type == TYPE_PLAYER )
1903 qboolean tagTorso = qfalse;
1904 qboolean tagHead = qfalse;
1905 qboolean tagWeapon = qfalse;
1908 // verify that all necessary tags exist
1910 numSurfaces = ASE_GetNumSurfaces();
1911 for ( i = 0; i < numSurfaces; i++ )
1913 if ( !strcmp( ASE_GetSurfaceName( i ), "tag_head" ) )
1917 if ( !strcmp( ASE_GetSurfaceName( i ), "tag_torso" ) )
1921 if ( !strcmp( ASE_GetSurfaceName( i ), "tag_weapon" ) )
1929 Error( "Missing tag_weapon!" );
1933 Error( "Missing tag_torso!" );
1937 Error( "Missing tag_weapon!" );
1940 // get all upper body surfaces
1941 numSurfaces = GetSurfaceAnimations( surfaceAnimations, "u_", -1, -1, g_data.maxUpperFrames );
1942 numFrames = SurfaceOrderToFrameOrder( surfaceAnimations, objectAnimationFrames, numSurfaces );
1943 strcpy( outfilename, filename );
1944 if ( strrchr( outfilename, '/' ) )
1945 *( strrchr( outfilename, '/' ) + 1 ) = 0;
1947 if ( g_data.currentLod == 0 )
1949 strcat( outfilename, "upper.md3" );
1955 sprintf( temp, "upper_%d.md3", g_data.currentLod );
1956 strcat( outfilename, temp );
1959 BuildAnimationFromOAFs( outfilename, objectAnimationFrames, numFrames, type );
1962 for ( i = 0; i < numSurfaces; i++ )
1964 if ( surfaceAnimations[i].frames )
1966 for ( j = 0; j < surfaceAnimations[i].numFrames; j++ )
1968 free( surfaceAnimations[i].frames[j].triangles );
1970 free( surfaceAnimations[i].frames );
1971 surfaceAnimations[i].frames = 0;
1975 // get lower body surfaces
1976 numSurfaces = GetSurfaceAnimations( surfaceAnimations, "l_", g_data.lowerSkipFrameStart, g_data.lowerSkipFrameEnd, -1 );
1977 numFrames = SurfaceOrderToFrameOrder( surfaceAnimations, objectAnimationFrames, numSurfaces );
1978 strcpy( outfilename, filename );
1979 if ( strrchr( outfilename, '/' ) )
1980 *( strrchr( outfilename, '/' ) + 1 ) = 0;
1982 if ( g_data.currentLod == 0 )
1984 strcat( outfilename, "lower.md3" );
1990 sprintf( temp, "lower_%d.md3", g_data.currentLod );
1991 strcat( outfilename, temp );
1993 BuildAnimationFromOAFs( outfilename, objectAnimationFrames, numFrames, type );
1996 for ( i = 0; i < numSurfaces; i++ )
1998 if ( surfaceAnimations[i].frames )
2000 for ( j = 0; j < surfaceAnimations[i].numFrames; j++ )
2002 free( surfaceAnimations[i].frames[j].triangles );
2004 free( surfaceAnimations[i].frames );
2005 surfaceAnimations[i].frames = 0;
2009 // get head surfaces
2010 numSurfaces = GetSurfaceAnimations( surfaceAnimations, "h_", -1, -1, g_data.maxHeadFrames );
2011 numFrames = SurfaceOrderToFrameOrder( surfaceAnimations, objectAnimationFrames, numSurfaces );
2012 strcpy( outfilename, filename );
2013 if ( strrchr( outfilename, '/' ) )
2014 *( strrchr( outfilename, '/' ) + 1 ) = 0;
2016 if ( g_data.currentLod == 0 )
2018 strcat( outfilename, "head.md3" );
2024 sprintf( temp, "head_%d.md3", g_data.currentLod );
2025 strcat( outfilename, temp );
2027 BuildAnimationFromOAFs( outfilename, objectAnimationFrames, numFrames, type );
2030 for ( i = 0; i < numSurfaces; i++ )
2032 if ( surfaceAnimations[i].frames )
2034 for ( j = 0; j < surfaceAnimations[i].numFrames; j++ )
2036 free( surfaceAnimations[i].frames[j].triangles );
2038 free( surfaceAnimations[i].frames );
2039 surfaceAnimations[i].frames = 0;
2043 else if ( type == TYPE_WEAPON )
2045 // get the weapon surfaces
2046 numSurfaces = GetSurfaceAnimations( surfaceAnimations, "w_", -1, -1, -1 );
2047 numFrames = SurfaceOrderToFrameOrder( surfaceAnimations, objectAnimationFrames, numSurfaces );
2049 strcpy( outfilename, filename );
2050 if ( strrchr( outfilename, '.' ) )
2051 *( strrchr( outfilename, '.' ) + 1 ) = 0;
2052 strcat( outfilename, "md3" );
2053 BuildAnimationFromOAFs( outfilename, objectAnimationFrames, numFrames, type );
2056 for ( i = 0; i < numSurfaces; i++ )
2058 if ( surfaceAnimations[i].frames )
2060 for ( j = 0; j < surfaceAnimations[i].numFrames; j++ )
2062 free( surfaceAnimations[i].frames[j].triangles );
2064 free( surfaceAnimations[i].frames );
2065 surfaceAnimations[i].frames = 0;
2069 // get the flash surfaces
2070 numSurfaces = GetSurfaceAnimations( surfaceAnimations, "f_", -1, -1, -1 );
2071 numFrames = SurfaceOrderToFrameOrder( surfaceAnimations, objectAnimationFrames, numSurfaces );
2073 strcpy( outfilename, filename );
2074 if ( strrchr( outfilename, '.' ) )
2075 *strrchr( outfilename, '.' ) = 0;
2076 strcat( outfilename, "_flash.md3" );
2077 BuildAnimationFromOAFs( outfilename, objectAnimationFrames, numFrames, TYPE_ITEM );
2080 for ( i = 0; i < numSurfaces; i++ )
2082 if ( surfaceAnimations[i].frames )
2084 for ( j = 0; j < surfaceAnimations[i].numFrames; j++ )
2086 free( surfaceAnimations[i].frames[j].triangles );
2088 free( surfaceAnimations[i].frames );
2089 surfaceAnimations[i].frames = 0;
2093 else if ( type == TYPE_HAND )
2095 // get the hand tags
2096 numSurfaces = GetSurfaceAnimations( surfaceAnimations, "tag_", -1, -1, -1 );
2097 numFrames = SurfaceOrderToFrameOrder( surfaceAnimations, objectAnimationFrames, numSurfaces );
2099 strcpy( outfilename, filename );
2100 if ( strrchr( outfilename, '.' ) )
2101 *strrchr( outfilename, '.' ) = 0;
2102 strcat( outfilename, "_hand.md3" );
2103 BuildAnimationFromOAFs( outfilename, objectAnimationFrames, numFrames, TYPE_HAND );
2106 for ( i = 0; i < numSurfaces; i++ )
2108 if ( surfaceAnimations[i].frames )
2110 for ( j = 0; j < surfaceAnimations[i].numFrames; j++ )
2112 free( surfaceAnimations[i].frames[j].triangles );
2114 free( surfaceAnimations[i].frames );
2115 surfaceAnimations[i].frames = 0;
2121 Error( "Unknown type passed to ConvertASE()" );
2124 g_data.currentLod = 0;
2126 g_data.maxHeadFrames = 0;
2127 g_data.maxUpperFrames = 0;
2128 g_data.lowerSkipFrameStart = 0;
2129 g_data.lowerSkipFrameEnd = 0;
2130 VectorCopy( vec3_origin, g_data.aseAdjust );
2132 // unload ASE from memory