1 /* -------------------------------------------------------------------------------
3 Copyright (C) 1999-2007 id Software, Inc. and contributors.
4 For a list of contributors, see the accompanying CONTRIBUTORS file.
6 This file is part of GtkRadiant.
8 GtkRadiant is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 GtkRadiant is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GtkRadiant; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 ----------------------------------------------------------------------------------
24 This code has been altered significantly from its original form, to support
25 several games based on the Quake III Arena engine, in the form of "Q3Map2."
27 ------------------------------------------------------------------------------- */
32 #define SURFACE_EXTRA_C
41 /* -------------------------------------------------------------------------------
43 ydnar: srf file module
45 ------------------------------------------------------------------------------- */
47 typedef struct surfaceExtra_s
49 mapDrawSurface_t *mds;
53 int castShadows, recvShadows;
60 #define GROW_SURFACE_EXTRAS 1024
62 int numSurfaceExtras = 0;
63 int maxSurfaceExtras = 0;
64 surfaceExtra_t *surfaceExtras;
65 surfaceExtra_t seDefault = { NULL, NULL, -1, 0, WORLDSPAWN_CAST_SHADOWS, WORLDSPAWN_RECV_SHADOWS, 0, 0, { 0, 0, 0 } };
71 allocates a new extra storage
74 static surfaceExtra_t *AllocSurfaceExtra( void )
80 if( numSurfaceExtras >= maxSurfaceExtras )
82 /* reallocate more room */
83 maxSurfaceExtras += GROW_SURFACE_EXTRAS;
84 se = safe_malloc( maxSurfaceExtras * sizeof( surfaceExtra_t ) );
85 if( surfaceExtras != NULL )
87 memcpy( se, surfaceExtras, numSurfaceExtras * sizeof( surfaceExtra_t ) );
88 free( surfaceExtras );
94 se = &surfaceExtras[ numSurfaceExtras ];
96 memcpy( se, &seDefault, sizeof( surfaceExtra_t ) );
105 SetDefaultSampleSize()
106 sets the default lightmap sample size
109 void SetDefaultSampleSize( int sampleSize )
111 seDefault.sampleSize = sampleSize;
118 stores extra (q3map2) data for the specific numbered drawsurface
121 void SetSurfaceExtra( mapDrawSurface_t *ds, int num )
127 if( ds == NULL || num < 0 )
130 /* get a new extra */
131 se = AllocSurfaceExtra();
133 /* copy out the relevant bits */
135 se->si = ds->shaderInfo;
136 se->parentSurfaceNum = ds->parent != NULL ? ds->parent->outputNum : -1;
137 se->entityNum = ds->entityNum;
138 se->castShadows = ds->castShadows;
139 se->recvShadows = ds->recvShadows;
140 se->sampleSize = ds->sampleSize;
141 se->longestCurve = ds->longestCurve;
142 VectorCopy( ds->lightmapAxis, se->lightmapAxis );
145 //% Sys_FPrintf( SYS_VRB, "SetSurfaceExtra(): entityNum = %d\n", ds->entityNum );
152 getter functions for extra surface data
155 static surfaceExtra_t *GetSurfaceExtra( int num )
157 if( num < 0 || num >= numSurfaceExtras )
159 return &surfaceExtras[ num ];
163 shaderInfo_t *GetSurfaceExtraShaderInfo( int num )
165 surfaceExtra_t *se = GetSurfaceExtra( num );
170 int GetSurfaceExtraParentSurfaceNum( int num )
172 surfaceExtra_t *se = GetSurfaceExtra( num );
173 return se->parentSurfaceNum;
177 int GetSurfaceExtraEntityNum( int num )
179 surfaceExtra_t *se = GetSurfaceExtra( num );
180 return se->entityNum;
184 int GetSurfaceExtraCastShadows( int num )
186 surfaceExtra_t *se = GetSurfaceExtra( num );
187 return se->castShadows;
191 int GetSurfaceExtraRecvShadows( int num )
193 surfaceExtra_t *se = GetSurfaceExtra( num );
194 return se->recvShadows;
198 int GetSurfaceExtraSampleSize( int num )
200 surfaceExtra_t *se = GetSurfaceExtra( num );
201 return se->sampleSize;
205 float GetSurfaceExtraLongestCurve( int num )
207 surfaceExtra_t *se = GetSurfaceExtra( num );
208 return se->longestCurve;
212 void GetSurfaceExtraLightmapAxis( int num, vec3_t lightmapAxis )
214 surfaceExtra_t *se = GetSurfaceExtra( num );
215 VectorCopy( se->lightmapAxis, lightmapAxis );
222 WriteSurfaceExtraFile()
223 writes out a surface info file (<map>.srf)
226 void WriteSurfaceExtraFile( const char *path )
228 char srfPath[ 1024 ];
235 if( path == NULL || path[ 0 ] == '\0' )
239 Sys_Printf( "--- WriteSurfaceExtraFile ---\n" );
242 strcpy( srfPath, path );
243 StripExtension( srfPath );
244 strcat( srfPath, ".srf" );
245 Sys_Printf( "Writing %s\n", srfPath );
246 sf = fopen( srfPath, "w" );
248 Error( "Error opening %s for writing", srfPath );
250 /* lap through the extras list */
251 for( i = -1; i < numSurfaceExtras; i++ )
254 se = GetSurfaceExtra( i );
256 /* default or surface num? */
258 fprintf( sf, "default" );
260 fprintf( sf, "%d", i );
262 /* valid map drawsurf? */
263 if( se->mds == NULL )
267 fprintf( sf, " // %s V: %d I: %d %s\n",
268 surfaceTypes[ se->mds->type ],
271 (se->mds->planar ? "planar" : "") );
275 fprintf( sf, "{\n" );
279 fprintf( sf, "\tshader %s\n", se->si->shader );
281 /* parent surface number */
282 if( se->parentSurfaceNum != seDefault.parentSurfaceNum )
283 fprintf( sf, "\tparent %d\n", se->parentSurfaceNum );
286 if( se->entityNum != seDefault.entityNum )
287 fprintf( sf, "\tentity %d\n", se->entityNum );
290 if( se->castShadows != seDefault.castShadows || se == &seDefault )
291 fprintf( sf, "\tcastShadows %d\n", se->castShadows );
294 if( se->recvShadows != seDefault.recvShadows || se == &seDefault )
295 fprintf( sf, "\treceiveShadows %d\n", se->recvShadows );
297 /* lightmap sample size */
298 if( se->sampleSize != seDefault.sampleSize || se == &seDefault )
299 fprintf( sf, "\tsampleSize %d\n", se->sampleSize );
302 if( se->longestCurve != seDefault.longestCurve || se == &seDefault )
303 fprintf( sf, "\tlongestCurve %f\n", se->longestCurve );
305 /* lightmap axis vector */
306 if( VectorCompare( se->lightmapAxis, seDefault.lightmapAxis ) == qfalse )
307 fprintf( sf, "\tlightmapAxis ( %f %f %f )\n", se->lightmapAxis[ 0 ], se->lightmapAxis[ 1 ], se->lightmapAxis[ 2 ] );
310 fprintf( sf, "}\n\n" );
320 LoadSurfaceExtraFile()
321 reads a surface info file (<map>.srf)
324 void LoadSurfaceExtraFile( const char *path )
326 char srfPath[ 1024 ];
328 int surfaceNum, size;
333 if( path == NULL || path[ 0 ] == '\0' )
337 strcpy( srfPath, path );
338 StripExtension( srfPath );
339 strcat( srfPath, ".srf" );
340 Sys_Printf( "Loading %s\n", srfPath );
341 size = LoadFile( srfPath, (void**) &buffer );
344 Sys_Printf( "WARNING: Unable to find surface file %s, using defaults.\n", srfPath );
349 ParseFromMemory( (char *) buffer, size );
354 /* test for end of file */
355 if( !GetToken( qtrue ) )
359 if( !Q_stricmp( token, "default" ) )
365 surfaceNum = atoi( token );
366 if( surfaceNum < 0 || surfaceNum > MAX_MAP_DRAW_SURFS )
367 Error( "ReadSurfaceExtraFile(): %s, line %d: bogus surface num %d", srfPath, scriptline, surfaceNum );
368 while( surfaceNum >= numSurfaceExtras )
369 se = AllocSurfaceExtra();
370 se = &surfaceExtras[ surfaceNum ];
373 /* handle { } section */
374 if( !GetToken( qtrue ) || strcmp( token, "{" ) )
375 Error( "ReadSurfaceExtraFile(): %s, line %d: { not found", srfPath, scriptline );
378 if( !GetToken( qtrue ) )
380 if( !strcmp( token, "}" ) )
384 if( !Q_stricmp( token, "shader" ) )
387 se->si = ShaderInfoForShader( token );
390 /* parent surface number */
391 else if( !Q_stricmp( token, "parent" ) )
394 se->parentSurfaceNum = atoi( token );
398 else if( !Q_stricmp( token, "entity" ) )
401 se->entityNum = atoi( token );
405 else if( !Q_stricmp( token, "castShadows" ) )
408 se->castShadows = atoi( token );
412 else if( !Q_stricmp( token, "receiveShadows" ) )
415 se->recvShadows = atoi( token );
418 /* lightmap sample size */
419 else if( !Q_stricmp( token, "sampleSize" ) )
422 se->sampleSize = atoi( token );
426 else if( !Q_stricmp( token, "longestCurve" ) )
429 se->longestCurve = atof( token );
432 /* lightmap axis vector */
433 else if( !Q_stricmp( token, "lightmapAxis" ) )
434 Parse1DMatrix( 3, se->lightmapAxis );
436 /* ignore all other tokens on the line */
437 while( TokenAvailable() )
442 /* free the buffer */