2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #define MAX_ASE_MATERIALS 32
31 #define MAX_ASE_OBJECTS 64
32 #define MAX_ASE_ANIMATIONS 32
33 #define MAX_ASE_ANIMATION_FRAMES 512
35 #define VERBOSE( x ) { if ( ase.verbose ) { Sys_Printf x ; } }
49 typedef int aseFace_t[3];
59 aseVertex_t *vertexes;
60 aseTVertex_t *tvertexes;
61 aseFace_t *faces, *tfaces;
63 int currentFace, currentVertex;
69 aseMesh_t frames[MAX_ASE_ANIMATION_FRAMES];
80 ** contains the animate sequence of a single surface
81 ** using a single material
90 aseMeshAnimation_t anim;
97 aseMaterial_t materials[MAX_ASE_MATERIALS];
98 aseGeomObject_t objects[MAX_ASE_OBJECTS];
110 static char s_token[1024];
112 static char gl_filename[1024];
114 static void ASE_Process( void );
115 static void ASE_FreeGeomObject( int ndx );
117 #if defined (__linux__) || defined (__APPLE__)
119 static char* strlwr (char* string)
122 for (cp = string; *cp; ++cp)
124 if ('A' <= *cp && *cp <= 'Z')
136 void ASE_Load( const char *filename, qboolean verbose, qboolean grabAnims )
138 FILE *fp = fopen( filename, "rb" );
141 Error( "File not found '%s'", filename );
143 memset( &ase, 0, sizeof( ase ) );
145 ase.verbose = verbose;
146 ase.grabAnims = grabAnims;
147 ase.len = Q_filelength( fp );
149 ase.curpos = ase.buffer = safe_malloc( ase.len );
151 Sys_Printf( "Processing '%s'\n", filename );
153 if ( fread( ase.buffer, ase.len, 1, fp ) != 1 )
156 Error( "fread() != -1 for '%s'", filename );
161 strcpy(gl_filename, filename);
169 void ASE_Free( void )
173 for ( i = 0; i < ase.currentObject; i++ )
175 ASE_FreeGeomObject( i );
180 ** ASE_GetNumSurfaces
182 int ASE_GetNumSurfaces( void )
184 return ase.currentObject;
188 ** ASE_GetSurfaceName
190 const char *ASE_GetSurfaceName( int which )
192 aseGeomObject_t *pObject = &ase.objects[which];
194 if ( !pObject->anim.numFrames )
197 return pObject->name;
201 ** ASE_GetSurfaceAnimation
203 ** Returns an animation (sequence of polysets)
205 polyset_t *ASE_GetSurfaceAnimation( int which, int *pNumFrames, int skipFrameStart, int skipFrameEnd, int maxFrames )
207 aseGeomObject_t *pObject = &ase.objects[which];
209 int numFramesInAnimation;
213 if ( !pObject->anim.numFrames )
216 if ( pObject->anim.numFrames > maxFrames && maxFrames != -1 )
218 numFramesInAnimation = maxFrames;
222 numFramesInAnimation = pObject->anim.numFrames;
223 if ( maxFrames != -1 )
224 Sys_Printf( "WARNING: ASE_GetSurfaceAnimation maxFrames > numFramesInAnimation\n" );
227 if ( skipFrameEnd != -1 )
228 numFramesToKeep = numFramesInAnimation - ( skipFrameEnd - skipFrameStart + 1 );
230 numFramesToKeep = numFramesInAnimation;
232 *pNumFrames = numFramesToKeep;
234 psets = calloc( sizeof( polyset_t ) * numFramesToKeep, 1 );
236 for ( f = 0, i = 0; i < numFramesInAnimation; i++ )
239 aseMesh_t *pMesh = &pObject->anim.frames[i];
241 if ( skipFrameStart != -1 )
243 if ( i >= skipFrameStart && i <= skipFrameEnd )
247 strcpy( psets[f].name, pObject->name );
248 strcpy( psets[f].materialname, ase.materials[pObject->materialRef].name );
250 psets[f].triangles = calloc( sizeof( triangle_t ) * pObject->anim.frames[i].numFaces, 1 );
251 psets[f].numtriangles = pObject->anim.frames[i].numFaces;
253 for ( t = 0; t < pObject->anim.frames[i].numFaces; t++ )
257 for ( k = 0; k < 3; k++ )
259 psets[f].triangles[t].verts[k][0] = pMesh->vertexes[pMesh->faces[t][k]].x;
260 psets[f].triangles[t].verts[k][1] = pMesh->vertexes[pMesh->faces[t][k]].y;
261 psets[f].triangles[t].verts[k][2] = pMesh->vertexes[pMesh->faces[t][k]].z;
263 if ( pMesh->tvertexes && pMesh->tfaces )
265 psets[f].triangles[t].texcoords[k][0] = pMesh->tvertexes[pMesh->tfaces[t][k]].s;
266 psets[f].triangles[t].texcoords[k][1] = pMesh->tvertexes[pMesh->tfaces[t][k]].t;
278 static void ASE_FreeGeomObject( int ndx )
280 aseGeomObject_t *pObject;
283 pObject = &ase.objects[ndx];
285 for ( i = 0; i < pObject->anim.numFrames; i++ )
287 if ( pObject->anim.frames[i].vertexes )
289 free( pObject->anim.frames[i].vertexes );
291 if ( pObject->anim.frames[i].tvertexes )
293 free( pObject->anim.frames[i].tvertexes );
295 if ( pObject->anim.frames[i].faces )
297 free( pObject->anim.frames[i].faces );
299 if ( pObject->anim.frames[i].tfaces )
301 free( pObject->anim.frames[i].tfaces );
305 memset( pObject, 0, sizeof( *pObject ) );
308 static aseMesh_t *ASE_GetCurrentMesh( void )
310 aseGeomObject_t *pObject;
312 if ( ase.currentObject >= MAX_ASE_OBJECTS )
314 Error( "Too many GEOMOBJECTs" );
315 return 0; // never called
318 pObject = &ase.objects[ase.currentObject];
320 if ( pObject->anim.currentFrame >= MAX_ASE_ANIMATION_FRAMES )
322 Error( "Too many MESHes" );
326 return &pObject->anim.frames[pObject->anim.currentFrame];
329 static int CharIsTokenDelimiter( int ch )
336 static int ASE_GetToken( qboolean restOfLine )
340 if ( ase.buffer == 0 )
343 if ( ( ase.curpos - ase.buffer ) == ase.len )
347 while ( ( ( ase.curpos - ase.buffer ) < ase.len ) &&
348 ( *ase.curpos <= 32 ) )
353 while ( ( ase.curpos - ase.buffer ) < ase.len )
355 s_token[i] = *ase.curpos;
360 if ( ( CharIsTokenDelimiter( s_token[i-1] ) && !restOfLine ) ||
361 ( ( s_token[i-1] == '\n' ) || ( s_token[i-1] == '\r' ) ) )
373 static void ASE_ParseBracedBlock( void (*parser)( const char *token ) )
377 while ( ASE_GetToken( qfalse ) )
379 if ( !strcmp( s_token, "{" ) )
383 else if ( !strcmp( s_token, "}" ) )
388 else if ( indent < 0 )
389 Error( "Unexpected '}'" );
399 static void ASE_SkipEnclosingBraces( void )
403 while ( ASE_GetToken( qfalse ) )
405 if ( !strcmp( s_token, "{" ) )
409 else if ( !strcmp( s_token, "}" ) )
414 else if ( indent < 0 )
415 Error( "Unexpected '}'" );
420 static void ASE_SkipRestOfLine( void )
422 ASE_GetToken( qtrue );
425 static void ASE_KeyMAP_DIFFUSE( const char *token )
427 char fullpath[1024], bitmap[1024], modeldir[1024];
431 strcpy(filename, gl_filename);
433 if ( !strcmp( token, "*BITMAP" ) )
435 ASE_GetToken( qfalse );
437 // the purpose of this whole chunk of code below is to extract the relative path
438 // from a full path in the ASE
440 strcpy( bitmap, s_token + 1 );
441 if ( strchr( bitmap, '"' ) )
442 *strchr( bitmap, '"' ) = 0;
444 /* convert backslash to slash */
447 if ( bitmap[i] == '\\' )
452 /* remove filename from path */
453 for( i=strlen(filename); i>0; i--)
455 if(filename[i] == '/')
462 /* replaces a relative path with a full path */
463 if(bitmap[0] == '.' && bitmap[1] == '.' && bitmap[2] == '/')
465 while(bitmap[0] == '.' && bitmap[1] == '.' && bitmap[2] == '/')
467 /* remove last item from path */
468 for( i=strlen(filename); i>0; i--)
470 if(filename[i] == '/')
476 strcpy(bitmap, &bitmap[3]);
478 strcat(filename, "/");
479 strcat(filename, bitmap);
480 strcpy(bitmap, filename);
483 if ( strstr( bitmap, gamedir ) )
485 strcpy( ase.materials[ase.numMaterials].name, strstr( bitmap, gamedir ) + strlen( gamedir ) );
486 Sys_Printf("material name: \'%s\'\n", strstr( bitmap, gamedir ) + strlen( gamedir ) );
490 sprintf( ase.materials[ase.numMaterials].name, "(not converted: '%s')", bitmap );
491 Sys_Printf( "WARNING: illegal material name '%s'\n", bitmap );
499 static void ASE_KeyMATERIAL( const char *token )
501 if ( !strcmp( token, "*MAP_DIFFUSE" ) )
503 ASE_ParseBracedBlock( ASE_KeyMAP_DIFFUSE );
510 static void ASE_KeyMATERIAL_LIST( const char *token )
512 if ( !strcmp( token, "*MATERIAL_COUNT" ) )
514 ASE_GetToken( qfalse );
515 VERBOSE( ( "..num materials: %s\n", s_token ) );
516 if ( atoi( s_token ) > MAX_ASE_MATERIALS )
518 Error( "Too many materials!" );
520 ase.numMaterials = 0;
522 else if ( !strcmp( token, "*MATERIAL" ) )
524 VERBOSE( ( "..material %d ", ase.numMaterials ) );
525 ASE_ParseBracedBlock( ASE_KeyMATERIAL );
530 static void ASE_KeyMESH_VERTEX_LIST( const char *token )
532 aseMesh_t *pMesh = ASE_GetCurrentMesh();
534 if ( !strcmp( token, "*MESH_VERTEX" ) )
536 ASE_GetToken( qfalse ); // skip number
538 ASE_GetToken( qfalse );
539 pMesh->vertexes[pMesh->currentVertex].y = atof( s_token );
541 ASE_GetToken( qfalse );
542 pMesh->vertexes[pMesh->currentVertex].x = -atof( s_token );
544 ASE_GetToken( qfalse );
545 pMesh->vertexes[pMesh->currentVertex].z = atof( s_token );
547 pMesh->currentVertex++;
549 if ( pMesh->currentVertex > pMesh->numVertexes )
551 Error( "pMesh->currentVertex >= pMesh->numVertexes" );
556 Error( "Unknown token '%s' while parsing MESH_VERTEX_LIST", token );
560 static void ASE_KeyMESH_FACE_LIST( const char *token )
562 aseMesh_t *pMesh = ASE_GetCurrentMesh();
564 if ( !strcmp( token, "*MESH_FACE" ) )
566 ASE_GetToken( qfalse ); // skip face number
568 ASE_GetToken( qfalse ); // skip label
569 ASE_GetToken( qfalse ); // first vertex
570 pMesh->faces[pMesh->currentFace][0] = atoi( s_token );
572 ASE_GetToken( qfalse ); // skip label
573 ASE_GetToken( qfalse ); // second vertex
574 pMesh->faces[pMesh->currentFace][2] = atoi( s_token );
576 ASE_GetToken( qfalse ); // skip label
577 ASE_GetToken( qfalse ); // third vertex
578 pMesh->faces[pMesh->currentFace][1] = atoi( s_token );
580 ASE_GetToken( qtrue );
583 if ( ( p = strstr( s_token, "*MESH_MTLID" ) ) != 0 )
585 p += strlen( "*MESH_MTLID" ) + 1;
590 Error( "No *MESH_MTLID found for face!" );
594 pMesh->currentFace++;
598 Error( "Unknown token '%s' while parsing MESH_FACE_LIST", token );
602 static void ASE_KeyTFACE_LIST( const char *token )
604 aseMesh_t *pMesh = ASE_GetCurrentMesh();
606 if ( !strcmp( token, "*MESH_TFACE" ) )
610 ASE_GetToken( qfalse );
612 ASE_GetToken( qfalse );
614 ASE_GetToken( qfalse );
616 ASE_GetToken( qfalse );
619 pMesh->tfaces[pMesh->currentFace][0] = a;
620 pMesh->tfaces[pMesh->currentFace][1] = b;
621 pMesh->tfaces[pMesh->currentFace][2] = c;
623 pMesh->currentFace++;
627 Error( "Unknown token '%s' in MESH_TFACE", token );
631 static void ASE_KeyMESH_TVERTLIST( const char *token )
633 aseMesh_t *pMesh = ASE_GetCurrentMesh();
635 if ( !strcmp( token, "*MESH_TVERT" ) )
637 char u[80], v[80], w[80];
639 ASE_GetToken( qfalse );
641 ASE_GetToken( qfalse );
642 strcpy( u, s_token );
644 ASE_GetToken( qfalse );
645 strcpy( v, s_token );
647 ASE_GetToken( qfalse );
648 strcpy( w, s_token );
650 pMesh->tvertexes[pMesh->currentVertex].s = atof( u );
651 pMesh->tvertexes[pMesh->currentVertex].t = 1.0f - atof( v );
653 pMesh->currentVertex++;
655 if ( pMesh->currentVertex > pMesh->numTVertexes )
657 Error( "pMesh->currentVertex > pMesh->numTVertexes" );
662 Error( "Unknown token '%s' while parsing MESH_TVERTLIST", token );
666 static void ASE_KeyMESH( const char *token )
668 aseMesh_t *pMesh = ASE_GetCurrentMesh();
670 if ( !strcmp( token, "*TIMEVALUE" ) )
672 ASE_GetToken( qfalse );
674 pMesh->timeValue = atoi( s_token );
675 VERBOSE( ( ".....timevalue: %d\n", pMesh->timeValue ) );
677 else if ( !strcmp( token, "*MESH_NUMVERTEX" ) )
679 ASE_GetToken( qfalse );
681 pMesh->numVertexes = atoi( s_token );
682 VERBOSE( ( ".....TIMEVALUE: %d\n", pMesh->timeValue ) );
683 VERBOSE( ( ".....num vertexes: %d\n", pMesh->numVertexes ) );
685 else if ( !strcmp( token, "*MESH_NUMFACES" ) )
687 ASE_GetToken( qfalse );
689 pMesh->numFaces = atoi( s_token );
690 VERBOSE( ( ".....num faces: %d\n", pMesh->numFaces ) );
692 else if ( !strcmp( token, "*MESH_NUMTVFACES" ) )
694 ASE_GetToken( qfalse );
696 if ( atoi( s_token ) != pMesh->numFaces )
698 Error( "MESH_NUMTVFACES != MESH_NUMFACES" );
701 else if ( !strcmp( token, "*MESH_NUMTVERTEX" ) )
703 ASE_GetToken( qfalse );
705 pMesh->numTVertexes = atoi( s_token );
706 VERBOSE( ( ".....num tvertexes: %d\n", pMesh->numTVertexes ) );
708 else if ( !strcmp( token, "*MESH_VERTEX_LIST" ) )
710 pMesh->vertexes = calloc( sizeof( aseVertex_t ) * pMesh->numVertexes, 1 );
711 pMesh->currentVertex = 0;
712 VERBOSE( ( ".....parsing MESH_VERTEX_LIST\n" ) );
713 ASE_ParseBracedBlock( ASE_KeyMESH_VERTEX_LIST );
715 else if ( !strcmp( token, "*MESH_TVERTLIST" ) )
717 pMesh->currentVertex = 0;
718 pMesh->tvertexes = calloc( sizeof( aseTVertex_t ) * pMesh->numTVertexes, 1 );
719 VERBOSE( ( ".....parsing MESH_TVERTLIST\n" ) );
720 ASE_ParseBracedBlock( ASE_KeyMESH_TVERTLIST );
722 else if ( !strcmp( token, "*MESH_FACE_LIST" ) )
724 pMesh->faces = calloc( sizeof( aseFace_t ) * pMesh->numFaces, 1 );
725 pMesh->currentFace = 0;
726 VERBOSE( ( ".....parsing MESH_FACE_LIST\n" ) );
727 ASE_ParseBracedBlock( ASE_KeyMESH_FACE_LIST );
729 else if ( !strcmp( token, "*MESH_TFACELIST" ) )
731 pMesh->tfaces = calloc( sizeof( aseFace_t ) * pMesh->numFaces, 1 );
732 pMesh->currentFace = 0;
733 VERBOSE( ( ".....parsing MESH_TFACE_LIST\n" ) );
734 ASE_ParseBracedBlock( ASE_KeyTFACE_LIST );
736 else if ( !strcmp( token, "*MESH_NORMALS" ) )
738 ASE_ParseBracedBlock( 0 );
742 static void ASE_KeyMESH_ANIMATION( const char *token )
744 aseMesh_t *pMesh = ASE_GetCurrentMesh();
746 // loads a single animation frame
747 if ( !strcmp( token, "*MESH" ) )
749 VERBOSE( ( "...found MESH\n" ) );
750 assert( pMesh->faces == 0 );
751 assert( pMesh->vertexes == 0 );
752 assert( pMesh->tvertexes == 0 );
753 memset( pMesh, 0, sizeof( *pMesh ) );
755 ASE_ParseBracedBlock( ASE_KeyMESH );
757 if ( ++ase.objects[ase.currentObject].anim.currentFrame == MAX_ASE_ANIMATION_FRAMES )
759 Error( "Too many animation frames" );
764 Error( "Unknown token '%s' while parsing MESH_ANIMATION", token );
768 static void ASE_KeyGEOMOBJECT( const char *token )
770 if ( !strcmp( token, "*NODE_NAME" ) )
772 char *name = ase.objects[ase.currentObject].name;
774 ASE_GetToken( qtrue );
775 VERBOSE( ( " %s\n", s_token ) );
776 strcpy( ase.objects[ase.currentObject].name, s_token + 1 );
777 if ( strchr( ase.objects[ase.currentObject].name, '"' ) )
778 *strchr( ase.objects[ase.currentObject].name, '"' ) = 0;
780 if ( strstr( name, "tag" ) == name )
782 while ( strchr( name, '_' ) != strrchr( name, '_' ) )
784 *strrchr( name, '_' ) = 0;
786 while ( strrchr( name, ' ' ) )
788 *strrchr( name, ' ' ) = 0;
792 else if ( !strcmp( token, "*NODE_PARENT" ) )
794 ASE_SkipRestOfLine();
796 // ignore unused data blocks
797 else if ( !strcmp( token, "*NODE_TM" ) ||
798 !strcmp( token, "*TM_ANIMATION" ) )
800 ASE_ParseBracedBlock( 0 );
802 // ignore regular meshes that aren't part of animation
803 else if ( !strcmp( token, "*MESH" ) && !ase.grabAnims )
806 if ( strstr( ase.objects[ase.currentObject].name, "tag_" ) == ase.objects[ase.currentObject].name )
808 s_forceStaticMesh = true;
809 ASE_ParseBracedBlock( ASE_KeyMESH );
810 s_forceStaticMesh = false;
813 ASE_ParseBracedBlock( ASE_KeyMESH );
814 if ( ++ase.objects[ase.currentObject].anim.currentFrame == MAX_ASE_ANIMATION_FRAMES )
816 Error( "Too many animation frames" );
818 ase.objects[ase.currentObject].anim.numFrames = ase.objects[ase.currentObject].anim.currentFrame;
819 ase.objects[ase.currentObject].numAnimations++;
821 // ignore meshes that aren't part of animations if this object isn't a
825 ASE_ParseBracedBlock( 0 );
829 // according to spec these are obsolete
830 else if ( !strcmp( token, "*MATERIAL_REF" ) )
832 ASE_GetToken( qfalse );
834 ase.objects[ase.currentObject].materialRef = atoi( s_token );
836 // loads a sequence of animation frames
837 else if ( !strcmp( token, "*MESH_ANIMATION" ) )
841 VERBOSE( ( "..found MESH_ANIMATION\n" ) );
843 if ( ase.objects[ase.currentObject].numAnimations )
845 Error( "Multiple MESH_ANIMATIONS within a single GEOM_OBJECT" );
847 ASE_ParseBracedBlock( ASE_KeyMESH_ANIMATION );
848 ase.objects[ase.currentObject].anim.numFrames = ase.objects[ase.currentObject].anim.currentFrame;
849 ase.objects[ase.currentObject].numAnimations++;
853 ASE_SkipEnclosingBraces();
857 else if ( !strcmp( token, "*PROP_MOTIONBLUR" ) ||
858 !strcmp( token, "*PROP_CASTSHADOW" ) ||
859 !strcmp( token, "*PROP_RECVSHADOW" ) )
861 ASE_SkipRestOfLine();
865 static void ConcatenateObjects( aseGeomObject_t *pObjA, aseGeomObject_t *pObjB )
869 static void CollapseObjects( void )
872 int numObjects = ase.currentObject;
874 for ( i = 0; i < numObjects; i++ )
879 if ( strstr( ase.objects[i].name, "tag" ) == ase.objects[i].name )
884 if ( !ase.objects[i].numAnimations )
889 for ( j = i + 1; j < numObjects; j++ )
891 if ( strstr( ase.objects[j].name, "tag" ) == ase.objects[j].name )
895 if ( ase.objects[i].materialRef == ase.objects[j].materialRef )
897 if ( ase.objects[j].numAnimations )
899 ConcatenateObjects( &ase.objects[i], &ase.objects[j] );
909 static void ASE_Process( void )
911 while ( ASE_GetToken( qfalse ) )
913 if ( !strcmp( s_token, "*3DSMAX_ASCIIEXPORT" ) ||
914 !strcmp( s_token, "*COMMENT" ) )
916 ASE_SkipRestOfLine();
918 else if ( !strcmp( s_token, "*SCENE" ) )
919 ASE_SkipEnclosingBraces();
920 else if ( !strcmp( s_token, "*MATERIAL_LIST" ) )
922 VERBOSE( ("MATERIAL_LIST\n") );
924 ASE_ParseBracedBlock( ASE_KeyMATERIAL_LIST );
926 else if ( !strcmp( s_token, "*GEOMOBJECT" ) )
928 VERBOSE( ("GEOMOBJECT" ) );
930 ASE_ParseBracedBlock( ASE_KeyGEOMOBJECT );
932 if ( strstr( ase.objects[ase.currentObject].name, "Bip" ) ||
933 strstr( ase.objects[ase.currentObject].name, "ignore_" ) )
935 ASE_FreeGeomObject( ase.currentObject );
936 VERBOSE( ( "(discarding BIP/ignore object)\n" ) );
938 else if ( ( strstr( ase.objects[ase.currentObject].name, "h_" ) != ase.objects[ase.currentObject].name ) &&
939 ( strstr( ase.objects[ase.currentObject].name, "l_" ) != ase.objects[ase.currentObject].name ) &&
940 ( strstr( ase.objects[ase.currentObject].name, "u_" ) != ase.objects[ase.currentObject].name ) &&
941 ( strstr( ase.objects[ase.currentObject].name, "tag" ) != ase.objects[ase.currentObject].name ) &&
944 VERBOSE( ( "(ignoring improperly labeled object '%s')\n", ase.objects[ase.currentObject].name ) );
945 ASE_FreeGeomObject( ase.currentObject );
949 if ( ++ase.currentObject == MAX_ASE_OBJECTS )
951 Error( "Too many GEOMOBJECTs" );
955 else if ( s_token[0] )
957 Sys_Printf( "Unknown token '%s'\n", s_token );
961 if ( !ase.currentObject )
962 Error( "No animation data!" );