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