2 Copyright (C) 1999-2007 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
23 #include "globaldefs.h"
32 #define MAX_POLYSETS 64
34 #if GDEF_OS_LINUX || GDEF_OS_MACOS
36 #define _strcmpi Q_stricmp
37 #define strlwr strlower
38 #define filelength(f) Q_filelength(f)
40 #define filelength(f) filelength(fileno(f))
47 char polysetNames[MAX_POLYSETS][256];
48 char shaders[MAX_POLYSETS][256];
50 char *buffer, *curpos;
55 static int P3DProcess();
56 static int P3DGetToken( int restOfLine );
58 static char s_token[1024];
65 int P3DLoad( const char *filename ){
66 FILE *fp = fopen( filename, "rb" );
72 memset( &p3d, 0, sizeof( p3d ) );
74 p3d.len = filelength( fp );
76 p3d.curpos = p3d.buffer = malloc( p3d.len );
78 if ( fread( p3d.buffer, p3d.len, 1, fp ) != 1 ) {
99 int CharIsTokenDelimiter( int ch ){
106 int P3DSkipToToken( const char *name ){
107 while ( P3DGetToken( 0 ) )
109 if ( !_strcmpi( s_token, name ) ) {
121 int P3DGetToken( int restOfLine ){
124 if ( p3d.buffer == 0 ) {
128 if ( ( p3d.curpos - p3d.buffer ) == p3d.len ) {
133 while ( ( ( p3d.curpos - p3d.buffer ) < p3d.len ) &&
134 ( *p3d.curpos <= 32 ) )
139 while ( ( p3d.curpos - p3d.buffer ) < p3d.len )
141 s_token[i] = *p3d.curpos;
146 if ( ( CharIsTokenDelimiter( s_token[i - 1] ) && !restOfLine ) ||
147 ( ( s_token[i - 1] == '\n' ) ) ) {
158 int P3DGetNextPair( char **psetName, char **associatedShader ){
159 if ( s_curpair < p3d.numPairs ) {
160 *psetName = p3d.polysetNames[s_curpair];
161 *associatedShader = p3d.shaders[s_curpair];
169 int P3DSkipToTokenInBlock( const char *name ){
172 while ( P3DGetToken( 0 ) )
174 if ( !_strcmpi( s_token, "}" ) ) {
177 else if ( !_strcmpi( s_token, "{" ) ) {
181 if ( !_strcmpi( s_token, name ) ) {
196 ** Nothing fancy here.
202 // first token should be a string
203 P3DGetToken( 1 ); // Voodoo Ascii File
205 // skip to the first Obj declaration
206 while ( P3DGetToken( 0 ) )
208 if ( !_strcmpi( s_token, "Obj" ) ) {
211 if ( P3DSkipToToken( "Text" ) ) {
212 if ( P3DSkipToTokenInBlock( "TMap" ) ) {
215 if ( !P3DSkipToToken( "Path" ) ) {
219 if ( !P3DGetToken( 1 ) ) {
223 while ( s_token[j] != 0 )
225 if ( s_token[j] == '\\' ) {
227 p3d.shaders[p3d.numPairs][k] = '/';
231 p3d.shaders[p3d.numPairs][k] = s_token[j];
236 p3d.shaders[p3d.numPairs][k] = 0;
239 // strip off any explicit extensions
241 if ( ( p = strrchr( p3d.shaders[p3d.numPairs], '/' ) ) != 0 ) {
253 // skip to the end of the Object and grab its name
255 if ( !P3DSkipToToken( "Name" ) ) {
259 if ( P3DGetToken( 0 ) ) {
260 // strip off leading 'Obj_' if it exists
261 if ( strstr( s_token, "Obj_" ) == s_token ) {
262 strcpy( p3d.polysetNames[p3d.numPairs], s_token + strlen( "Obj_" ) );
265 strcpy( p3d.polysetNames[p3d.numPairs], s_token );
268 // strip off trailing unused color information
269 // if ( strrchr( p3d.polysetNames[p3d.numPairs], '_' ) != 0 )
270 // *strrchr( p3d.polysetNames[p3d.numPairs], '_' ) = 0;
289 void SkinFromP3D( const char *file ){
291 char *psetName, *associatedShader;
294 ** a P3D file contains a list of polysets, each with a list of associated
295 ** texture names that constitute it's
300 ** polyset -> polyset
301 ** texture -> texture.SHADER becomes polyset's shader
303 sprintf( filename, "%s/%s", g_cddir, file );
305 if ( !P3DLoad( filename ) ) {
306 Error( "unable to load '%s'", filename );
309 while ( P3DGetNextPair( &psetName, &associatedShader ) )
313 // find the polyset in the object that this particular pset/shader pair
314 // corresponds to and append the shader to it
315 for ( i = 0; i < g_data.model.numSurfaces; i++ )
317 if ( !_strcmpi( g_data.surfData[i].header.name, psetName ) ) {
320 if ( strstr( associatedShader, gamedir + 1 ) ) {
321 p = strstr( associatedShader, gamedir + 1 ) + strlen( gamedir ) - 1;
325 p = associatedShader;
328 strcpy( g_data.surfData[i].shaders[g_data.surfData[i].header.numShaders].name, p );
330 g_data.surfData[i].header.numShaders++;