Debian Codegraph @

tools/quake3/q3map2/surface_extra.c

raw symbols SCIP
1 /* -------------------------------------------------------------------------------
2
3 Copyright (C) 1999-2007 id Software, Inc. and contributors.
4 For a list of contributors, see the accompanying CONTRIBUTORS file.
5
6 This file is part of GtkRadiant.
7
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.
12
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.
17
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
21
22 ----------------------------------------------------------------------------------
23
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."
26
27 ------------------------------------------------------------------------------- */
28
29
30
31 /* marker */
32 #define SURFACE_EXTRA_C
33
34
35
36 /* dependencies */
37 #include "q3map2.h"
38
39
40
41 /* -------------------------------------------------------------------------------
42
43 ydnar: srf file module
44
45 ------------------------------------------------------------------------------- */
46
47 typedef struct surfaceExtra_s
48 {
49 mapDrawSurface_t *mds;
50 shaderInfo_t *si;
51 int parentSurfaceNum;
52 int entityNum;
53 int castShadows, recvShadows;
54 int sampleSize;
55 float longestCurve;
56 vec3_t lightmapAxis;
57 }
58 surfaceExtra_t;
59
60 #define GROW_SURFACE_EXTRAS 1024
61
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 } };
66
67
68
69 /*
70 AllocSurfaceExtra()
71 allocates a new extra storage
72 */
73
74 static surfaceExtra_t *AllocSurfaceExtra( void ){
75 surfaceExtra_t *se;
76
77
78 /* enough space? */
79 if ( numSurfaceExtras >= maxSurfaceExtras ) {
80 /* reallocate more room */
81 maxSurfaceExtras += GROW_SURFACE_EXTRAS;
82 se = safe_malloc( maxSurfaceExtras * sizeof( surfaceExtra_t ) );
83 if ( surfaceExtras != NULL ) {
84 memcpy( se, surfaceExtras, numSurfaceExtras * sizeof( surfaceExtra_t ) );
85 free( surfaceExtras );
86 }
87 surfaceExtras = se;
88 }
89
90 /* add another */
91 se = &surfaceExtras[ numSurfaceExtras ];
92 numSurfaceExtras++;
93 memcpy( se, &seDefault, sizeof( surfaceExtra_t ) );
94
95 /* return it */
96 return se;
97 }
98
99
100
101 /*
102 SetDefaultSampleSize()
103 sets the default lightmap sample size
104 */
105
106 void SetDefaultSampleSize( int sampleSize ){
107 seDefault.sampleSize = sampleSize;
108 }
109
110
111
112 /*
113 SetSurfaceExtra()
114 stores extra (q3map2) data for the specific numbered drawsurface
115 */
116
117 void SetSurfaceExtra( mapDrawSurface_t *ds, int num ){
118 surfaceExtra_t *se;
119
120
121 /* dummy check */
122 if ( ds == NULL || num < 0 ) {
123 return;
124 }
125
126 /* get a new extra */
127 se = AllocSurfaceExtra();
128
129 /* copy out the relevant bits */
130 se->mds = ds;
131 se->si = ds->shaderInfo;
132 se->parentSurfaceNum = ds->parent != NULL ? ds->parent->outputNum : -1;
133 se->entityNum = ds->entityNum;
134 se->castShadows = ds->castShadows;
135 se->recvShadows = ds->recvShadows;
136 se->sampleSize = ds->sampleSize;
137 se->longestCurve = ds->longestCurve;
138 VectorCopy( ds->lightmapAxis, se->lightmapAxis );
139
140 /* debug code */
141 //% Sys_FPrintf( SYS_VRB, "SetSurfaceExtra(): entityNum = %d\n", ds->entityNum );
142 }
143
144
145
146 /*
147 GetSurfaceExtra*()
148 getter functions for extra surface data
149 */
150
151 static surfaceExtra_t *GetSurfaceExtra( int num ){
152 if ( num < 0 || num >= numSurfaceExtras ) {
153 return &seDefault;
154 }
155 return &surfaceExtras[ num ];
156 }
157
158
159 shaderInfo_t *GetSurfaceExtraShaderInfo( int num ){
160 surfaceExtra_t *se = GetSurfaceExtra( num );
161 return se->si;
162 }
163
164
165 int GetSurfaceExtraParentSurfaceNum( int num ){
166 surfaceExtra_t *se = GetSurfaceExtra( num );
167 return se->parentSurfaceNum;
168 }
169
170
171 int GetSurfaceExtraEntityNum( int num ){
172 surfaceExtra_t *se = GetSurfaceExtra( num );
173 return se->entityNum;
174 }
175
176
177 int GetSurfaceExtraCastShadows( int num ){
178 surfaceExtra_t *se = GetSurfaceExtra( num );
179 return se->castShadows;
180 }
181
182
183 int GetSurfaceExtraRecvShadows( int num ){
184 surfaceExtra_t *se = GetSurfaceExtra( num );
185 return se->recvShadows;
186 }
187
188
189 int GetSurfaceExtraSampleSize( int num ){
190 surfaceExtra_t *se = GetSurfaceExtra( num );
191 return se->sampleSize;
192 }
193
194
195 float GetSurfaceExtraLongestCurve( int num ){
196 surfaceExtra_t *se = GetSurfaceExtra( num );
197 return se->longestCurve;
198 }
199
200
201 void GetSurfaceExtraLightmapAxis( int num, vec3_t lightmapAxis ){
202 surfaceExtra_t *se = GetSurfaceExtra( num );
203 VectorCopy( se->lightmapAxis, lightmapAxis );
204 }
205
206
207
208
209 /*
210 WriteSurfaceExtraFile()
211 writes out a surface info file (<map>.srf)
212 */
213
214 void WriteSurfaceExtraFile( const char *surfaceFilePath ){
215 FILE *sf;
216 surfaceExtra_t *se;
217 int i;
218
219
220 /* dummy check */
221 if ( surfaceFilePath == NULL || surfaceFilePath[ 0 ] == '\0' ) {
222 return;
223 }
224
225 /* note it */
226 Sys_Printf( "--- WriteSurfaceExtraFile ---\n" );
227
228 /* open the file */
229 Sys_Printf( "Writing %s\n", surfaceFilePath );
230 sf = fopen( surfaceFilePath, "w" );
231 if ( sf == NULL ) {
232 Error( "Error opening %s for writing", surfaceFilePath );
233 }
234
235 /* lap through the extras list */
236 for ( i = -1; i < numSurfaceExtras; i++ )
237 {
238 /* get extra */
239 se = GetSurfaceExtra( i );
240
241 /* default or surface num? */
242 if ( i < 0 ) {
243 fprintf( sf, "default" );
244 }
245 else{
246 fprintf( sf, "%d", i );
247 }
248
249 /* valid map drawsurf? */
250 if ( se->mds == NULL ) {
251 fprintf( sf, "\n" );
252 }
253 else
254 {
255 fprintf( sf, " // %s V: %d I: %d %s\n",
256 surfaceTypes[ se->mds->type ],
257 se->mds->numVerts,
258 se->mds->numIndexes,
259 ( se->mds->planar ? "planar" : "" ) );
260 }
261
262 /* open braces */
263 fprintf( sf, "{\n" );
264
265 /* shader */
266 if ( se->si != NULL ) {
267 fprintf( sf, "\tshader %s\n", se->si->shader );
268 }
269
270 /* parent surface number */
271 if ( se->parentSurfaceNum != seDefault.parentSurfaceNum ) {
272 fprintf( sf, "\tparent %d\n", se->parentSurfaceNum );
273 }
274
275 /* entity number */
276 if ( se->entityNum != seDefault.entityNum ) {
277 fprintf( sf, "\tentity %d\n", se->entityNum );
278 }
279
280 /* cast shadows */
281 if ( se->castShadows != seDefault.castShadows || se == &seDefault ) {
282 fprintf( sf, "\tcastShadows %d\n", se->castShadows );
283 }
284
285 /* recv shadows */
286 if ( se->recvShadows != seDefault.recvShadows || se == &seDefault ) {
287 fprintf( sf, "\treceiveShadows %d\n", se->recvShadows );
288 }
289
290 /* lightmap sample size */
291 if ( se->sampleSize != seDefault.sampleSize || se == &seDefault ) {
292 fprintf( sf, "\tsampleSize %d\n", se->sampleSize );
293 }
294
295 /* longest curve */
296 if ( se->longestCurve != seDefault.longestCurve || se == &seDefault ) {
297 fprintf( sf, "\tlongestCurve %f\n", se->longestCurve );
298 }
299
300 /* lightmap axis vector */
301 if ( VectorCompare( se->lightmapAxis, seDefault.lightmapAxis ) == qfalse ) {
302 fprintf( sf, "\tlightmapAxis ( %f %f %f )\n", se->lightmapAxis[ 0 ], se->lightmapAxis[ 1 ], se->lightmapAxis[ 2 ] );
303 }
304
305 /* close braces */
306 fprintf( sf, "}\n\n" );
307 }
308
309 /* close the file */
310 fclose( sf );
311 }
312
313
314
315 /*
316 LoadSurfaceExtraFile()
317 reads a surface info file (<map>.srf)
318 */
319
320 void LoadSurfaceExtraFile( const char *surfaceFilePath ){
321 surfaceExtra_t *se;
322 int surfaceNum, size;
323 byte *buffer;
324
325
326 /* dummy check */
327 if ( surfaceFilePath == NULL || surfaceFilePath[ 0 ] == '\0' ) {
328 return;
329 }
330
331 /* load the file */
332 Sys_Printf( "Loading %s\n", surfaceFilePath );
333 size = LoadFile( surfaceFilePath, (void**) &buffer );
334 if ( size <= 0 ) {
335 Sys_FPrintf( SYS_WRN, "WARNING: Unable to find surface file %s, using defaults.\n", surfaceFilePath );
336 return;
337 }
338
339 /* parse the file */
340 ParseFromMemory( (char*) buffer, size );
341
342 /* tokenize it */
343 while ( 1 )
344 {
345 /* test for end of file */
346 if ( !GetToken( qtrue ) ) {
347 break;
348 }
349
350 /* default? */
351 if ( !Q_stricmp( token, "default" ) ) {
352 se = &seDefault;
353 }
354
355 /* surface number */
356 else
357 {
358 surfaceNum = atoi( token );
359 if ( surfaceNum < 0 || surfaceNum > MAX_MAP_DRAW_SURFS ) {
360 Error( "ReadSurfaceExtraFile(): %s, line %d: bogus surface num %d", surfaceFilePath, scriptline, surfaceNum );
361 }
362 while ( surfaceNum >= numSurfaceExtras )
363 se = AllocSurfaceExtra();
364 se = &surfaceExtras[ surfaceNum ];
365 }
366
367 /* handle { } section */
368 if ( !GetToken( qtrue ) || strcmp( token, "{" ) ) {
369 Error( "ReadSurfaceExtraFile(): %s, line %d: { not found", surfaceFilePath, scriptline );
370 }
371 while ( 1 )
372 {
373 if ( !GetToken( qtrue ) ) {
374 break;
375 }
376 if ( !strcmp( token, "}" ) ) {
377 break;
378 }
379
380 /* shader */
381 if ( !Q_stricmp( token, "shader" ) ) {
382 GetToken( qfalse );
383 se->si = ShaderInfoForShader( token );
384 }
385
386 /* parent surface number */
387 else if ( !Q_stricmp( token, "parent" ) ) {
388 GetToken( qfalse );
389 se->parentSurfaceNum = atoi( token );
390 }
391
392 /* entity number */
393 else if ( !Q_stricmp( token, "entity" ) ) {
394 GetToken( qfalse );
395 se->entityNum = atoi( token );
396 }
397
398 /* cast shadows */
399 else if ( !Q_stricmp( token, "castShadows" ) ) {
400 GetToken( qfalse );
401 se->castShadows = atoi( token );
402 }
403
404 /* recv shadows */
405 else if ( !Q_stricmp( token, "receiveShadows" ) ) {
406 GetToken( qfalse );
407 se->recvShadows = atoi( token );
408 }
409
410 /* lightmap sample size */
411 else if ( !Q_stricmp( token, "sampleSize" ) ) {
412 GetToken( qfalse );
413 se->sampleSize = atoi( token );
414 }
415
416 /* longest curve */
417 else if ( !Q_stricmp( token, "longestCurve" ) ) {
418 GetToken( qfalse );
419 se->longestCurve = atof( token );
420 }
421
422 /* lightmap axis vector */
423 else if ( !Q_stricmp( token, "lightmapAxis" ) ) {
424 Parse1DMatrix( 3, se->lightmapAxis );
425 }
426
427 /* ignore all other tokens on the line */
428 while ( TokenAvailable() )
429 GetToken( qfalse );
430 }
431 }
432
433 /* free the buffer */
434 free( buffer );
435 }