10 #define MAX_POLYSETS 64
12 #if defined (__linux__) || defined (__APPLE__)
13 #define _strcmpi Q_stricmp
14 #define filelength Q_filelength
15 #define strlwr strlower
22 char polysetNames[MAX_POLYSETS][256];
23 char shaders[MAX_POLYSETS][256];
25 char *buffer, *curpos;
30 static int P3DProcess();
31 static int P3DGetToken( int restOfLine );
33 static char s_token[1024];
40 int P3DLoad( const char *filename )
42 FILE *fp = fopen( filename, "rb" );
47 memset( &p3d, 0, sizeof( p3d ) );
49 p3d.len = filelength( fileno( fp ) );
51 p3d.curpos = p3d.buffer = malloc( p3d.len );
53 if ( fread( p3d.buffer, p3d.len, 1, fp ) != 1 )
77 int CharIsTokenDelimiter( int ch )
84 int P3DSkipToToken( const char *name )
86 while ( P3DGetToken( 0 ) )
88 if ( !_strcmpi( s_token, name ) )
99 int P3DGetToken( int restOfLine )
103 if ( p3d.buffer == 0 )
106 if ( ( p3d.curpos - p3d.buffer ) == p3d.len )
110 while ( ( ( p3d.curpos - p3d.buffer ) < p3d.len ) &&
111 ( *p3d.curpos <= 32 ) )
116 while ( ( p3d.curpos - p3d.buffer ) < p3d.len )
118 s_token[i] = *p3d.curpos;
123 if ( ( CharIsTokenDelimiter( s_token[i-1] ) && !restOfLine ) ||
124 ( ( s_token[i-1] == '\n' ) ) )
136 int P3DGetNextPair( char **psetName, char **associatedShader )
138 if ( s_curpair < p3d.numPairs )
140 *psetName = p3d.polysetNames[s_curpair];
141 *associatedShader = p3d.shaders[s_curpair];
149 int P3DSkipToTokenInBlock( const char *name )
153 while ( P3DGetToken( 0 ) )
155 if ( !_strcmpi( s_token, "}" ) )
157 else if ( !_strcmpi( s_token, "{" ) )
160 if ( !_strcmpi( s_token, name ) )
175 ** Nothing fancy here.
182 // first token should be a string
183 P3DGetToken( 1 ); // Voodoo Ascii File
185 // skip to the first Obj declaration
186 while ( P3DGetToken( 0 ) )
188 if ( !_strcmpi( s_token, "Obj" ) )
192 if ( P3DSkipToToken( "Text" ) )
194 if ( P3DSkipToTokenInBlock( "TMap" ) )
198 if ( !P3DSkipToToken( "Path" ) )
201 if ( !P3DGetToken( 1 ) )
204 while ( s_token[j] != 0 )
206 if ( s_token[j] == '\\' )
209 p3d.shaders[p3d.numPairs][k] = '/';
213 p3d.shaders[p3d.numPairs][k] = s_token[j];
218 p3d.shaders[p3d.numPairs][k] = 0;
221 // strip off any explicit extensions
223 if ( ( p = strrchr( p3d.shaders[p3d.numPairs], '/' ) ) != 0 )
237 // skip to the end of the Object and grab its name
239 if ( !P3DSkipToToken( "Name" ) )
242 if ( P3DGetToken( 0 ) )
244 // strip off leading 'Obj_' if it exists
245 if ( strstr( s_token, "Obj_" ) == s_token )
246 strcpy( p3d.polysetNames[p3d.numPairs], s_token + strlen( "Obj_" ) );
248 strcpy( p3d.polysetNames[p3d.numPairs], s_token );
250 // strip off trailing unused color information
251 // if ( strrchr( p3d.polysetNames[p3d.numPairs], '_' ) != 0 )
252 // *strrchr( p3d.polysetNames[p3d.numPairs], '_' ) = 0;
271 void SkinFromP3D( const char *file )
274 char *psetName, *associatedShader;
277 ** a P3D file contains a list of polysets, each with a list of associated
278 ** texture names that constitute it's
283 ** polyset -> polyset
284 ** texture -> texture.SHADER becomes polyset's shader
286 sprintf( filename, "%s/%s", g_cddir, file );
288 if ( !P3DLoad( filename ) )
289 Error( "unable to load '%s'", filename );
291 while ( P3DGetNextPair( &psetName, &associatedShader ) )
295 // find the polyset in the object that this particular pset/shader pair
296 // corresponds to and append the shader to it
297 for ( i = 0; i < g_data.model.numSurfaces; i++ )
299 if ( !_strcmpi( g_data.surfData[i].header.name, psetName) )
303 if ( strstr( associatedShader, gamedir + 1 ) )
305 p = strstr( associatedShader, gamedir + 1 ) + strlen( gamedir ) - 1;
309 p = associatedShader;
312 strcpy( g_data.surfData[i].shaders[g_data.surfData[i].header.numShaders].name, p );
314 g_data.surfData[i].header.numShaders++;