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
21 ----------------------------------------------------------------------------------
23 This code has been altered significantly from its original form, to support
24 several games based on the Quake III Arena engine, in the form of "Q3Map2."
26 ------------------------------------------------------------------------------- */
31 #define SURFACE_EXTRA_C
40 /* -------------------------------------------------------------------------------
42 ydnar: srf file module
44 ------------------------------------------------------------------------------- */
46 typedef struct surfaceExtra_s
48 mapDrawSurface_t *mds;
52 int castShadows, recvShadows;
59 #define GROW_SURFACE_EXTRAS 1024
61 int numSurfaceExtras = 0;
62 int maxSurfaceExtras = 0;
63 surfaceExtra_t *surfaceExtras;
64 surfaceExtra_t seDefault = { NULL, NULL, -1, 0, WORLDSPAWN_CAST_SHADOWS, WORLDSPAWN_RECV_SHADOWS, 0, 0, { 0, 0, 0 } };
70 allocates a new extra storage
73 static surfaceExtra_t *AllocSurfaceExtra( void )
79 if( numSurfaceExtras >= maxSurfaceExtras )
81 /* reallocate more room */
82 maxSurfaceExtras += GROW_SURFACE_EXTRAS;
83 se = safe_malloc( maxSurfaceExtras * sizeof( surfaceExtra_t ) );
84 if( surfaceExtras != NULL )
86 memcpy( se, surfaceExtras, numSurfaceExtras * sizeof( surfaceExtra_t ) );
87 free( surfaceExtras );
93 se = &surfaceExtras[ numSurfaceExtras ];
95 memcpy( se, &seDefault, sizeof( surfaceExtra_t ) );
104 SetDefaultSampleSize()
105 sets the default lightmap sample size
108 void SetDefaultSampleSize( int sampleSize )
110 seDefault.sampleSize = sampleSize;
117 stores extra (q3map2) data for the specific numbered drawsurface
120 void SetSurfaceExtra( mapDrawSurface_t *ds, int num )
126 if( ds == NULL || num < 0 )
129 /* get a new extra */
130 se = AllocSurfaceExtra();
132 /* copy out the relevant bits */
134 se->si = ds->shaderInfo;
135 se->parentSurfaceNum = ds->parent != NULL ? ds->parent->outputNum : -1;
136 se->entityNum = ds->entityNum;
137 se->castShadows = ds->castShadows;
138 se->recvShadows = ds->recvShadows;
139 se->sampleSize = ds->sampleSize;
140 se->longestCurve = ds->longestCurve;
141 VectorCopy( ds->lightmapAxis, se->lightmapAxis );
144 //% Sys_FPrintf( SYS_VRB, "SetSurfaceExtra(): entityNum = %d\n", ds->entityNum );
151 getter functions for extra surface data
154 static surfaceExtra_t *GetSurfaceExtra( int num )
156 if( num < 0 || num >= numSurfaceExtras )
158 return &surfaceExtras[ num ];
162 shaderInfo_t *GetSurfaceExtraShaderInfo( int num )
164 surfaceExtra_t *se = GetSurfaceExtra( num );
169 int GetSurfaceExtraParentSurfaceNum( int num )
171 surfaceExtra_t *se = GetSurfaceExtra( num );
172 return se->parentSurfaceNum;
176 int GetSurfaceExtraEntityNum( int num )
178 surfaceExtra_t *se = GetSurfaceExtra( num );
179 return se->entityNum;
183 int GetSurfaceExtraCastShadows( int num )
185 surfaceExtra_t *se = GetSurfaceExtra( num );
186 return se->castShadows;
190 int GetSurfaceExtraRecvShadows( int num )
192 surfaceExtra_t *se = GetSurfaceExtra( num );
193 return se->recvShadows;
197 int GetSurfaceExtraSampleSize( int num )
199 surfaceExtra_t *se = GetSurfaceExtra( num );
200 return se->sampleSize;
204 float GetSurfaceExtraLongestCurve( int num )
206 surfaceExtra_t *se = GetSurfaceExtra( num );
207 return se->longestCurve;
211 void GetSurfaceExtraLightmapAxis( int num, vec3_t lightmapAxis )
213 surfaceExtra_t *se = GetSurfaceExtra( num );
214 VectorCopy( se->lightmapAxis, lightmapAxis );
221 WriteSurfaceExtraFile()
222 writes out a surface info file (<map>.srf)
225 void WriteSurfaceExtraFile( const char *path )
227 char srfPath[ 1024 ];
234 if( path == NULL || path[ 0 ] == '\0' )
238 Sys_Printf( "--- WriteSurfaceExtraFile ---\n" );
241 strcpy( srfPath, path );
242 StripExtension( srfPath );
243 strcat( srfPath, ".srf" );
244 Sys_Printf( "Writing %s\n", srfPath );
245 sf = fopen( srfPath, "w" );
247 Error( "Error opening %s for writing", srfPath );
249 /* lap through the extras list */
250 for( i = -1; i < numSurfaceExtras; i++ )
253 se = GetSurfaceExtra( i );
255 /* default or surface num? */
257 fprintf( sf, "default" );
259 fprintf( sf, "%d", i );
261 /* valid map drawsurf? */
262 if( se->mds == NULL )
266 fprintf( sf, " // %s V: %d I: %d %s\n",
267 surfaceTypes[ se->mds->type ],
270 (se->mds->planar ? "planar" : "") );
274 fprintf( sf, "{\n" );
278 fprintf( sf, "\tshader %s\n", se->si->shader );
280 /* parent surface number */
281 if( se->parentSurfaceNum != seDefault.parentSurfaceNum )
282 fprintf( sf, "\tparent %d\n", se->parentSurfaceNum );
285 if( se->entityNum != seDefault.entityNum )
286 fprintf( sf, "\tentity %d\n", se->entityNum );
289 if( se->castShadows != seDefault.castShadows || se == &seDefault )
290 fprintf( sf, "\tcastShadows %d\n", se->castShadows );
293 if( se->recvShadows != seDefault.recvShadows || se == &seDefault )
294 fprintf( sf, "\treceiveShadows %d\n", se->recvShadows );
296 /* lightmap sample size */
297 if( se->sampleSize != seDefault.sampleSize || se == &seDefault )
298 fprintf( sf, "\tsampleSize %d\n", se->sampleSize );
301 if( se->longestCurve != seDefault.longestCurve || se == &seDefault )
302 fprintf( sf, "\tlongestCurve %f\n", se->longestCurve );
304 /* lightmap axis vector */
305 if( VectorCompare( se->lightmapAxis, seDefault.lightmapAxis ) == qfalse )
306 fprintf( sf, "\tlightmapAxis ( %f %f %f )\n", se->lightmapAxis[ 0 ], se->lightmapAxis[ 1 ], se->lightmapAxis[ 2 ] );
309 fprintf( sf, "}\n\n" );
319 LoadSurfaceExtraFile()
320 reads a surface info file (<map>.srf)
323 void LoadSurfaceExtraFile( const char *path )
325 char srfPath[ 1024 ];
327 int surfaceNum, size;
332 if( path == NULL || path[ 0 ] == '\0' )
336 strcpy( srfPath, path );
337 StripExtension( srfPath );
338 strcat( srfPath, ".srf" );
339 Sys_Printf( "Loading %s\n", srfPath );
340 size = LoadFile( srfPath, (void**) &buffer );
343 Sys_Printf( "WARNING: Unable to find surface file %s, using defaults.\n", srfPath );
348 ParseFromMemory( buffer, size );
353 /* test for end of file */
354 if( !GetToken( qtrue ) )
358 if( !Q_stricmp( token, "default" ) )
364 surfaceNum = atoi( token );
365 if( surfaceNum < 0 || surfaceNum > MAX_MAP_DRAW_SURFS )
366 Error( "ReadSurfaceExtraFile(): %s, line %d: bogus surface num %d", srfPath, scriptline, surfaceNum );
367 while( surfaceNum >= numSurfaceExtras )
368 se = AllocSurfaceExtra();
369 se = &surfaceExtras[ surfaceNum ];
372 /* handle { } section */
373 if( !GetToken( qtrue ) || strcmp( token, "{" ) )
374 Error( "ReadSurfaceExtraFile(): %s, line %d: { not found", srfPath, scriptline );
377 if( !GetToken( qtrue ) )
379 if( !strcmp( token, "}" ) )
383 if( !Q_stricmp( token, "shader" ) )
386 se->si = ShaderInfoForShader( token );
389 /* parent surface number */
390 else if( !Q_stricmp( token, "parent" ) )
393 se->parentSurfaceNum = atoi( token );
397 else if( !Q_stricmp( token, "entity" ) )
400 se->entityNum = atoi( token );
404 else if( !Q_stricmp( token, "castShadows" ) )
407 se->castShadows = atoi( token );
411 else if( !Q_stricmp( token, "receiveShadows" ) )
414 se->recvShadows = atoi( token );
417 /* lightmap sample size */
418 else if( !Q_stricmp( token, "sampleSize" ) )
421 se->sampleSize = atoi( token );
425 else if( !Q_stricmp( token, "longestCurve" ) )
428 se->longestCurve = atof( token );
431 /* lightmap axis vector */
432 else if( !Q_stricmp( token, "lightmapAxis" ) )
433 Parse1DMatrix( 3, se->lightmapAxis );
435 /* ignore all other tokens on the line */
436 while( TokenAvailable() )
441 /* free the buffer */