-
Notifications
You must be signed in to change notification settings - Fork 2
/
tyrlite.c
494 lines (400 loc) · 11.9 KB
/
tyrlite.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/* Copyright (C) 1996-1997 Id Software, Inc.
This program 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.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
See file, 'COPYING', for details.
*/
/*
* tyrlite/tyrlite.c
* Modifications by Kevin Shanahan, 1999-2000
*/
#include "tyrlite.h"
#include <limits.h>
#define __min(a,b) (((a) < (b)) ? (a) : (b))
#define __max(a,b) (((a) > (b)) ? (a) : (b))
extern int numbounces;
int numhits[MAX_MAP_ENTITIES];
float scaledist = 1.0;
float scalecos = 0.5;
float rangescale = 0.5;
int worldminlight = 0;
vec3_t minlight_color = { 255, 255, 255 }; /* defaults to white light */
int sunlight = 0;
vec3_t sunlight_color = { 255, 255, 255 }; /* defaults to white light */
vec3_t sunmangle = { 0, 0, 16384 }; /* defaults to straight down */
byte *filebase;
byte *file_p;
byte *file_end;
dmodel_t *bspmodel;
int bspfileface; /* next surface to dispatch */
vec3_t bsp_origin;
qboolean extrasamples;
qboolean compress_ents;
qboolean facecounter;
qboolean colored;
qboolean nominlimit;
qboolean force;
qboolean makelit;
qboolean nolightface[MAX_MAP_FACES];
vec3_t faceoffset[MAX_MAP_FACES];
extern int num_lights;
//++ [js] new feature
qboolean external;
qboolean nodefault;
char extfilename[PATH_MAX];
tex_col_list tc_list;
// get number of lines in text
long getNumLines(FILE* file)
{
long numlines = 0;
char line [1024];
char* txt = NULL;
while (NULL != fgets(line, 1024, file))
{
txt = strchr(line,'\n');
if (txt != NULL)
{
numlines++;
}
else break;
}
fseek(file,0,SEEK_SET);
return numlines;
}
// file parser
void ParseDefFile(char* filename) {
long num = 0;
int i = 0;
int r,g,b;
char name[64];
char line [1024];
FILE* file = fopen(filename,"rt");
if (file != NULL)
{
num = __max(0,getNumLines(file));
num = __min(num , MAX_ENTRYNUM);
tc_list.num = num ;
tc_list.entries = (tex_col*) malloc(sizeof(tex_col) * num);
while (fgets(line,1024,file) != NULL)
{
if (line[strlen(line)-1] == '\n') line[strlen(line)-1] = '\0';
if (strlen(line) > 0)
{
sscanf(line, "%s %d %d %d", name ,&r,&g,&b);
if (strlen(name) > 0 )
{
strcpy(tc_list.entries[i].name,name);
tc_list.entries[i].red = __min(__max(r,1),255);
tc_list.entries[i].green = __min(__max(g,1),255);
tc_list.entries[i].blue = __min(__max(b,1),255);
i++;
}
}
if (i >= num) break;
}
num = i;
tc_list.num = num ;
printf("Loaded %d entries from file : %s\n", num, filename);
}
else printf("Unable to open file named : %s \n", filename);
}
//-- [js] new feature
byte *GetFileSpace (int size)
{
byte *buf;
LOCK;
file_p = (byte *)(((long)file_p + 3)&~3);
buf = file_p;
file_p += size;
UNLOCK;
if (file_p > file_end)
Error ("GetFileSpace: overrun");
return buf;
}
void LightThread (void *junk)
{
int i;
while (1)
{
LOCK;
i = bspfileface++;
UNLOCK;
if (!facecounter)
printf("Lighting face %i of %i\r", i, numfaces);
if (i >= numfaces)
{
printf("\n\nMHColour Completed.\n\n");
return;
}
if (!makelit)
LightFace (i, nolightface[i], faceoffset[i]);
else LightFaceLIT (i, nolightface[i], faceoffset[i]);
}
}
void LightThread2 (void *junk)
{
int i, j;
j = bspfileface;
while (1)
{
LOCK;
i = j++;
UNLOCK;
if (!facecounter)
printf("Checking face %i of %i\r", i, numfaces);
if (i >= numfaces)
{
printf ("\n");
return;
}
TestLightFace (i, nolightface[i], faceoffset[i]);
}
}
void FindFaceOffsets( void )
{
extern int nummodels;
extern dmodel_t dmodels[MAX_MAP_MODELS];
int i,j;
entity_t *ent;
char name[ 20 ];
char *classname;
vec3_t org;
memset( nolightface, 0, sizeof( nolightface ) );
for( j = dmodels[ 0 ].firstface; j < dmodels[ 0 ].numfaces; j++ )
nolightface[ j ] = 0;
for( i = 1; i < nummodels; i++ )
{
sprintf( name, "*%d", i );
ent = FindEntityWithKeyPair( "model", name );
if ( !ent )
Error("FindFaceOffsets: Couldn't find entity for model %s.\n",
name );
classname = ValueForKey ( ent, "classname" );
if ( !strncmp( classname, "rotate_", 7 ) )
{
int start;
int end;
GetVectorForKey(ent, "origin", org);
start = dmodels[ i ].firstface;
end = start + dmodels[ i ].numfaces;
for( j = start; j < end; j++ )
{
nolightface[ j ] = 300;
faceoffset[ j ][ 0 ] = org[ 0 ];
faceoffset[ j ][ 1 ] = org[ 1 ];
faceoffset[ j ][ 2 ] = org[ 2 ];
}
}
}
}
/*
* =============
* LightWorld
* =============
*/
void LightWorld (void)
{
int i;
int j;
int max;
float mody;
int num_colours;
extern int num_clights;
extern int numlighttex;
CheckTex ();
printf ("\n");
filebase = file_p = dlightdata;
file_end = filebase + MAX_MAP_LIGHTING;
for (i = 0; i < num_entities; i++)
{
if (!strcmp (entities[i].classname, "light"))
{
entities[i].lightcolour[0] = 0;
entities[i].lightcolour[1] = 0;
entities[i].lightcolour[2] = 0;
}
}
// since we're not running on an alpha box, we can safely do it this way...
if (numlighttex)
LightThread2 (NULL);
else
printf ("Skipping Texture lighting - no faces modify light colour in this BSP!\n");
// normalise the lightcolours to a base max of 255 and set any one with r/g/b of
// 0 each to an r/g/b value of 255 each
num_colours = 0;
printf ("\nChecking all light sources for potential effectiveness...\n");
for (i = 0; i < num_entities; i++)
{
printf ("- Checking entity %i of %i\r", i + 1, num_entities);
if (entities[i].light)
{
// hopefully this should never happen, but just in case...
if (entities[i].lightcolour[0] == 0 &&
entities[i].lightcolour[1] == 0 &&
entities[i].lightcolour[2] == 0)
{
// a faint orange tinge keeps white lights from looking dull.
entities[i].lightcolour[0] = 255;
entities[i].lightcolour[1] = 225;
entities[i].lightcolour[2] = 200;
// the light is white, so we don't need to do anything more
continue;
}
if (entities[i].lightcolour[0] == entities[i].lightcolour[1] &&
entities[i].lightcolour[1] == entities[i].lightcolour[2])
{
// a faint orange tinge keeps white lights from looking dull.
entities[i].lightcolour[0] = 255;
entities[i].lightcolour[1] = 225;
entities[i].lightcolour[2] = 200;
// the light is white, so we don't need to do anything more
continue;
}
num_colours++;
max = 0;
for (j = 0; j < 3; j++)
if (entities[i].lightcolour[j] > max)
max = entities[i].lightcolour[j];
// this condition will happen for any flame, torch or globe light.
if (max == 255)
continue;
// use 275 here instead of 255 because coloured lights can seem darker than white
// ones
for (j = 0; j < 3; j++)
entities[i].lightcolour[j] = (275 * entities[i].lightcolour[j]) / max;
}
}
printf ("- %i extra Light sources were coloured by Texture lighting\n",
num_colours - num_clights);
printf ("- A total of %i Light sources out of %i have now been coloured\n",
num_colours, num_lights);
if (num_colours < (num_lights / 4))
DecisionTime ("I suggest you don't continue, especially if you got the first warning too");
printf ("\n");
if (!num_colours)
Error ("This BSP contains no light colour modifying data!");
LightThread (NULL);
lightdatasize = file_p - filebase;
//printf ("lightdatasize: %i\n", lightdatasize);
}
/*
* ==================
* main
* light modelfile
* ==================
*/
int main (int argc, char **argv)
{
int i;
double start;
double end;
char source[1024];
//init_log("tyrlite.log");
printf ("--------------------\n");
printf ("JSColour Version 1.0\n");
printf ("Based on MHColour 0.5\n");
printf ("Based on Tyrlite 0.8\n");
printf ("--------------------\n");
force = false;
makelit = false;
external = false; // [js] new feature
nodefault = false; // [js] new feature
for (i=1 ; i<argc ; i++)
{
if (!strcmp(argv[i],"-extra"))
{
extrasamples = true;
printf ("extra sampling enabled\n");
}
else if (!strcmp(argv[i],"-dist") && argc > i)
{
scaledist = atof (argv[i+1]);
i++;
}
else if (!strcmp(argv[i],"-range") && argc > i)
{
rangescale = atof (argv[i+1]);
i++;
}
else if (!strcmp(argv[i],"-light") && argc > i)
{
worldminlight = atof (argv[i+1]);
i++;
}
else if (!strcmp(argv[i],"-force"))
{
force = true;
printf ("Forcing colouring regardless of potential effectiveness\n");
}
else if (!strcmp (argv[i], "-lit"))
{
makelit = true;
printf ("Making a LIT file\n");
}
//++ [js] new feature
else if (!strcmp (argv[i], "-external") && argc > i)
{
FILE* f = NULL;
strcpy(extfilename, argv[i+1]);
f = fopen(extfilename,"rt");
if ( f != NULL)
{
external = true;
printf ("Using external definition file : %s\n",extfilename);
fclose(f);
}
else printf ("No such file : %s Ignoring this option\n",extfilename);
i++;
}
else if (!strcmp (argv[i], "-nodefault"))
{
nodefault = true;
printf ("Ignoring built-in color definition list\n");
}
//-- [js] new feature
else if (argv[i][0] == '-')
Error ("Unknown option \"%s\"", argv[i]);
else
break;
}
// set the options we always want
nominlimit = false;
colored = true;
compress_ents = false;
//++ [js] new feature
if (external == true)
ParseDefFile(extfilename);
//-- [js] new feature
if (i != argc - 1)
Error ("usage: jscolour [-light num] [-extra] [-force] [-dist num] \n\t\t [-range num] [-nodefault] [-external file] bspfile"); // [js] new feature
//Error ("usage: mhcolour [-light num] [-extra] [-force] [-dist num] [-range num] bspfile");
InitThreads ();
start = I_FloatTime ();
numbounces = 0;
strcpy (source, argv[i]);
StripExtension (source);
DefaultExtension (source, ".bsp");
LoadBSPFile (source);
LoadEntities ();
MakeTnodes (&dmodels[0]);
FindFaceOffsets();
LightWorld ();
WriteEntitiesToString ();
if (colored)
WriteBSPFile (source, BSP_COLORED_VERSION);
else
WriteBSPFile (source, BSP_OLD_VERSION);
end = I_FloatTime ();
printf ("%0.1f seconds elapsed\n", end-start);
free(tc_list.entries); // [js] new feature
//close_log();
return 0;
}