| 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 |
#define LIGHT_BOUNCE_C |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
#include "q3map2.h" |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
void RadFreeLights( void ){ |
| 49 |
light_t *light, *next; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
for ( light = lights; light; light = next ) |
| 54 |
{ |
| 55 |
next = light->next; |
| 56 |
if ( light->w != NULL ) { |
| 57 |
FreeWinding( light->w ); |
| 58 |
} |
| 59 |
free( light ); |
| 60 |
} |
| 61 |
numLights = 0; |
| 62 |
lights = NULL; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
static void RadClipWindingEpsilon( radWinding_t *in, vec3_t normal, vec_t dist, |
| 74 |
vec_t epsilon, radWinding_t *front, radWinding_t *back, clipWork_t *cw ){ |
| 75 |
vec_t *dists; |
| 76 |
int *sides; |
| 77 |
int counts[ 3 ]; |
| 78 |
vec_t dot; |
| 79 |
int i, j, k; |
| 80 |
radVert_t *v1, *v2, mid; |
| 81 |
int maxPoints; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
dists = cw->dists; |
| 86 |
sides = cw->sides; |
| 87 |
|
| 88 |
|
| 89 |
counts[ 0 ] = counts[ 1 ] = counts[ 2 ] = 0; |
| 90 |
|
| 91 |
|
| 92 |
for ( i = 0; i < in->numVerts; i++ ) |
| 93 |
{ |
| 94 |
dot = DotProduct( in->verts[ i ].xyz, normal ); |
| 95 |
dot -= dist; |
| 96 |
dists[ i ] = dot; |
| 97 |
if ( dot > epsilon ) { |
| 98 |
sides[ i ] = SIDE_FRONT; |
| 99 |
} |
| 100 |
else if ( dot < -epsilon ) { |
| 101 |
sides[ i ] = SIDE_BACK; |
| 102 |
} |
| 103 |
else{ |
| 104 |
sides[ i ] = SIDE_ON; |
| 105 |
} |
| 106 |
counts[ sides[ i ] ]++; |
| 107 |
} |
| 108 |
sides[ i ] = sides[ 0 ]; |
| 109 |
dists[ i ] = dists[ 0 ]; |
| 110 |
|
| 111 |
|
| 112 |
front->numVerts = back->numVerts = 0; |
| 113 |
|
| 114 |
|
| 115 |
if ( counts[ 0 ] == 0 ) { |
| 116 |
memcpy( back, in, sizeof( radWinding_t ) ); |
| 117 |
return; |
| 118 |
} |
| 119 |
if ( counts[ 1 ] == 0 ) { |
| 120 |
memcpy( front, in, sizeof( radWinding_t ) ); |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
maxPoints = in->numVerts + 4; |
| 126 |
|
| 127 |
|
| 128 |
for ( i = 0; i < in->numVerts; i++ ) |
| 129 |
{ |
| 130 |
|
| 131 |
v1 = &in->verts[ i ]; |
| 132 |
|
| 133 |
if ( sides[ i ] == SIDE_ON ) { |
| 134 |
memcpy( &front->verts[ front->numVerts++ ], v1, sizeof( radVert_t ) ); |
| 135 |
memcpy( &back->verts[ back->numVerts++ ], v1, sizeof( radVert_t ) ); |
| 136 |
continue; |
| 137 |
} |
| 138 |
|
| 139 |
if ( sides[ i ] == SIDE_FRONT ) { |
| 140 |
memcpy( &front->verts[ front->numVerts++ ], v1, sizeof( radVert_t ) ); |
| 141 |
} |
| 142 |
|
| 143 |
if ( sides[ i ] == SIDE_BACK ) { |
| 144 |
memcpy( &back->verts[ back->numVerts++ ], v1, sizeof( radVert_t ) ); |
| 145 |
} |
| 146 |
|
| 147 |
if ( sides[ i + 1 ] == SIDE_ON || sides[ i + 1 ] == sides[ i ] ) { |
| 148 |
continue; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
v2 = &in->verts[ ( i + 1 ) % in->numVerts ]; |
| 153 |
|
| 154 |
dot = dists[ i ] / ( dists[ i ] - dists[ i + 1 ] ); |
| 155 |
|
| 156 |
|
| 157 |
for ( j = 0; j < 4; j++ ) |
| 158 |
{ |
| 159 |
|
| 160 |
if ( j < 4 ) { |
| 161 |
for ( k = 0; k < MAX_LIGHTMAPS; k++ ) |
| 162 |
mid.color[ k ][ j ] = v1->color[ k ][ j ] + dot * ( v2->color[ k ][ j ] - v1->color[ k ][ j ] ); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
if ( j < 3 ) { |
| 167 |
mid.xyz[ j ] = v1->xyz[ j ] + dot * ( v2->xyz[ j ] - v1->xyz[ j ] ); |
| 168 |
mid.normal[ j ] = v1->normal[ j ] + dot * ( v2->normal[ j ] - v1->normal[ j ] ); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
if ( j < 2 ) { |
| 173 |
mid.st[ j ] = v1->st[ j ] + dot * ( v2->st[ j ] - v1->st[ j ] ); |
| 174 |
for ( k = 0; k < MAX_LIGHTMAPS; k++ ) |
| 175 |
mid.lightmap[ k ][ j ] = v1->lightmap[ k ][ j ] + dot * ( v2->lightmap[ k ][ j ] - v1->lightmap[ k ][ j ] ); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
VectorNormalize( mid.normal, mid.normal ); |
| 181 |
|
| 182 |
|
| 183 |
memcpy( &front->verts[ front->numVerts++ ], &mid, sizeof( radVert_t ) ); |
| 184 |
memcpy( &back->verts[ back->numVerts++ ], &mid, sizeof( radVert_t ) ); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
if ( front->numVerts > maxPoints ) { |
| 189 |
Error( "RadClipWindingEpsilon: points exceeded estimate" ); |
| 190 |
} |
| 191 |
if ( front->numVerts > MAX_POINTS_ON_WINDING ) { |
| 192 |
Error( "RadClipWindingEpsilon: MAX_POINTS_ON_WINDING" ); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
float Modulo1IfNegative( float f ){ |
| 212 |
return f < 0.0f ? f - floor( f ) : f; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
qboolean RadSampleImage( byte *pixels, int width, int height, float st[ 2 ], float color[ 4 ] ){ |
| 224 |
int x, y; |
| 225 |
|
| 226 |
|
| 227 |
color[ 0 ] = color[ 1 ] = color[ 2 ] = color[ 3 ] = 255; |
| 228 |
|
| 229 |
|
| 230 |
if ( pixels == NULL || width < 1 || height < 1 ) { |
| 231 |
return qfalse; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
x = ( (float) width * Modulo1IfNegative( st[ 0 ] ) ) + 0.5f; |
| 236 |
x %= width; |
| 237 |
y = ( (float) height * Modulo1IfNegative( st[ 1 ] ) ) + 0.5f; |
| 238 |
y %= height; |
| 239 |
|
| 240 |
|
| 241 |
pixels += ( y * width * 4 ) + ( x * 4 ); |
| 242 |
VectorCopy( pixels, color ); |
| 243 |
color[ 3 ] = pixels[ 3 ]; |
| 244 |
|
| 245 |
if ( texturesRGB ) { |
| 246 |
color[0] = Image_LinearFloatFromsRGBFloat( color[0] * ( 1.0 / 255.0 ) ) * 255.0; |
| 247 |
color[1] = Image_LinearFloatFromsRGBFloat( color[1] * ( 1.0 / 255.0 ) ) * 255.0; |
| 248 |
color[2] = Image_LinearFloatFromsRGBFloat( color[2] * ( 1.0 / 255.0 ) ) * 255.0; |
| 249 |
} |
| 250 |
|
| 251 |
return qtrue; |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
#define MAX_SAMPLES 150 |
| 263 |
#define SAMPLE_GRANULARITY 6 |
| 264 |
|
| 265 |
static void RadSample( int lightmapNum, bspDrawSurface_t *ds, rawLightmap_t *lm, shaderInfo_t *si, radWinding_t *rw, vec3_t average, vec3_t gradient, int *style ){ |
| 266 |
int i, j, k, l, v, x, y, samples, avgcolor, f_superSample; |
| 267 |
vec3_t color, mins, maxs; |
| 268 |
vec4_t textureColor; |
| 269 |
|
| 270 |
float bf; |
| 271 |
vec3_t blend; |
| 272 |
float st[ 2 ], lightmap[ 2 ], *radLuxel; |
| 273 |
radVert_t *rv[ 3 ]; |
| 274 |
|
| 275 |
if (!bouncing) |
| 276 |
Sys_Printf( "BUG: RadSample: !bouncing shouldn't happen\n" ); |
| 277 |
|
| 278 |
|
| 279 |
ClearBounds( mins, maxs ); |
| 280 |
VectorClear( average ); |
| 281 |
VectorClear( gradient ); |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
if ( rw == NULL || rw->numVerts < 3 ) { |
| 286 |
return; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
samples = 0; |
| 291 |
|
| 292 |
|
| 293 |
if ( lm == NULL || lm->radLuxels[ lightmapNum ] == NULL || bouncing == qfalse ) { |
| 294 |
for ( samples = 0; samples < rw->numVerts; samples++ ) |
| 295 |
{ |
| 296 |
|
| 297 |
if ( !RadSampleImage( si->lightImage->pixels, si->lightImage->width, si->lightImage->height, rw->verts[ samples ].st, textureColor ) ) { |
| 298 |
VectorCopy( si->averageColor, textureColor ); |
| 299 |
textureColor[ 3 ] = 255.0f; |
| 300 |
} |
| 301 |
avgcolor = ( textureColor[ 0 ] + textureColor[ 1 ] + textureColor[ 2 ] ) / 3; |
| 302 |
for ( i = 0; i < 3; i++ ) |
| 303 |
color[ i ] = ( ( textureColor[ i ] * bounceColorRatio + ( avgcolor * ( 1 - bounceColorRatio ) ) ) / 255 ) * ( rw->verts[ samples ].color[ lightmapNum ][ i ] / 255.0f ); |
| 304 |
|
| 305 |
|
| 306 |
AddPointToBounds( color, mins, maxs ); |
| 307 |
VectorAdd( average, color, average ); |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
*style = ds->vertexStyles[ lightmapNum ]; |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
else |
| 319 |
{ |
| 320 |
f_superSample = (float)superSample; |
| 321 |
|
| 322 |
for ( v = 1; v < ( rw->numVerts - 1 ) && samples < MAX_SAMPLES; v++ ) |
| 323 |
{ |
| 324 |
|
| 325 |
rv[ 0 ] = &rw->verts[ 0 ]; |
| 326 |
rv[ 1 ] = &rw->verts[ v ]; |
| 327 |
rv[ 2 ] = &rw->verts[ v + 1 ]; |
| 328 |
|
| 329 |
|
| 330 |
for ( i = 1; i < SAMPLE_GRANULARITY && samples < MAX_SAMPLES; i++ ) |
| 331 |
{ |
| 332 |
for ( j = 1; j < SAMPLE_GRANULARITY && samples < MAX_SAMPLES; j++ ) |
| 333 |
{ |
| 334 |
for ( k = 1; k < SAMPLE_GRANULARITY && samples < MAX_SAMPLES; k++ ) |
| 335 |
{ |
| 336 |
|
| 337 |
blend[ 0 ] = i; |
| 338 |
blend[ 1 ] = j; |
| 339 |
blend[ 2 ] = k; |
| 340 |
bf = ( 1.0f / ( blend[ 0 ] + blend[ 1 ] + blend[ 2 ] ) ); |
| 341 |
VectorScale( blend, bf, blend ); |
| 342 |
|
| 343 |
|
| 344 |
st[ 0 ] = st[ 1 ] = 0.0f; |
| 345 |
lightmap[ 0 ] = lightmap[ 1 ] = 0.0f; |
| 346 |
|
| 347 |
for ( l = 0; l < 3; l++ ) |
| 348 |
{ |
| 349 |
st[ 0 ] += ( rv[ l ]->st[ 0 ] * blend[ l ] ); |
| 350 |
st[ 1 ] += ( rv[ l ]->st[ 1 ] * blend[ l ] ); |
| 351 |
lightmap[ 0 ] += ( rv[ l ]->lightmap[ lightmapNum ][ 0 ] * blend[ l ] ); |
| 352 |
lightmap[ 1 ] += ( rv[ l ]->lightmap[ lightmapNum ][ 1 ] * blend[ l ] ); |
| 353 |
|
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
x = lightmap[ 0 ] / f_superSample; |
| 359 |
y = lightmap[ 1 ] / f_superSample; |
| 360 |
|
| 361 |
if ( x < 0 ) { |
| 362 |
x = 0; |
| 363 |
} |
| 364 |
else if ( x >= lm->w ) { |
| 365 |
x = lm->w - 1; |
| 366 |
} |
| 367 |
if ( y < 0 ) { |
| 368 |
y = 0; |
| 369 |
} |
| 370 |
else if ( y >= lm->h ) { |
| 371 |
y = lm->h - 1; |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
radLuxel = RAD_LUXEL( lightmapNum, x, y ); |
| 376 |
|
| 377 |
|
| 378 |
if ( radLuxel[ 0 ] < 0.0f ) { |
| 379 |
continue; |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
samples++; |
| 384 |
|
| 385 |
|
| 386 |
if ( !RadSampleImage( si->lightImage->pixels, si->lightImage->width, si->lightImage->height, st, textureColor ) ) { |
| 387 |
VectorCopy( si->averageColor, textureColor ); |
| 388 |
textureColor[ 3 ] = 255; |
| 389 |
} |
| 390 |
avgcolor = ( textureColor[ 0 ] + textureColor[ 1 ] + textureColor[ 2 ] ) / 3; |
| 391 |
for ( i = 0; i < 3; i++ ){ |
| 392 |
color[ i ] = ( ( textureColor[ i ] * bounceColorRatio + ( avgcolor * ( 1 - bounceColorRatio ) ) ) / 255 ) * ( radLuxel[ i ] / 255 ); |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
} |
| 402 |
AddPointToBounds( color, mins, maxs ); |
| 403 |
VectorAdd( average, color, average ); |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
} |
| 408 |
} |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
*style = ds->lightmapStyles[ lightmapNum ]; |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
if ( samples <= 0 ) { |
| 418 |
return; |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
VectorScale( average, ( 1.0 / samples ), average ); |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
for ( i = 0; i < 3; i++ ) |
| 434 |
gradient[ i ] = ( maxs[ i ] - mins[ i ] ) * maxs[ i ]; |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
#define RADIOSITY_MAX_GRADIENT 0.75f //% 0.25f |
| 445 |
#define RADIOSITY_VALUE 500.0f |
| 446 |
#define RADIOSITY_MIN 0.0001f |
| 447 |
#define RADIOSITY_CLIP_EPSILON 0.125f |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
static void RadSubdivideDiffuseLight( int lightmapNum, bspDrawSurface_t *ds, rawLightmap_t *lm, shaderInfo_t *si, |
| 452 |
float scale, float subdivide, radWinding_t *rw, clipWork_t *cw ){ |
| 453 |
int i, style = 0; |
| 454 |
float dist, area, value; |
| 455 |
vec3_t mins, maxs, normal, d1, d2, cross, color, gradient; |
| 456 |
light_t *light, *splash; |
| 457 |
winding_t *w, *splash_w; |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
if ( rw == NULL || rw->numVerts < 3 ) { |
| 462 |
return; |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
ClearBounds( mins, maxs ); |
| 467 |
for ( i = 0; i < rw->numVerts; i++ ) |
| 468 |
AddPointToBounds( rw->verts[ i ].xyz, mins, maxs ); |
| 469 |
|
| 470 |
|
| 471 |
for ( i = 0; i < 3; i++ ) |
| 472 |
{ |
| 473 |
if ( maxs[ i ] - mins[ i ] > subdivide ) { |
| 474 |
radWinding_t front, back; |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
VectorClear( normal ); |
| 479 |
normal[ i ] = 1; |
| 480 |
dist = ( maxs[ i ] + mins[ i ] ) * 0.5f; |
| 481 |
|
| 482 |
|
| 483 |
RadClipWindingEpsilon( rw, normal, dist, RADIOSITY_CLIP_EPSILON, &front, &back, cw ); |
| 484 |
|
| 485 |
|
| 486 |
RadSubdivideDiffuseLight( lightmapNum, ds, lm, si, scale, subdivide, &front, cw ); |
| 487 |
RadSubdivideDiffuseLight( lightmapNum, ds, lm, si, scale, subdivide, &back, cw ); |
| 488 |
return; |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
area = 0.0f; |
| 494 |
for ( i = 2; i < rw->numVerts; i++ ) |
| 495 |
{ |
| 496 |
VectorSubtract( rw->verts[ i - 1 ].xyz, rw->verts[ 0 ].xyz, d1 ); |
| 497 |
VectorSubtract( rw->verts[ i ].xyz, rw->verts[ 0 ].xyz, d2 ); |
| 498 |
CrossProduct( d1, d2, cross ); |
| 499 |
area += 0.5f * VectorLength( cross ); |
| 500 |
} |
| 501 |
if ( area < 1.0f || area > 20000000.0f ) { |
| 502 |
return; |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
if ( bouncing ) { |
| 507 |
|
| 508 |
RadSample( lightmapNum, ds, lm, si, rw, color, gradient, &style ); |
| 509 |
|
| 510 |
|
| 511 |
if ( subdivide > minDiffuseSubdivide && |
| 512 |
( gradient[ 0 ] > RADIOSITY_MAX_GRADIENT || gradient[ 1 ] > RADIOSITY_MAX_GRADIENT || gradient[ 2 ] > RADIOSITY_MAX_GRADIENT ) ) { |
| 513 |
RadSubdivideDiffuseLight( lightmapNum, ds, lm, si, scale, ( subdivide / 2.0f ), rw, cw ); |
| 514 |
return; |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
w = AllocWinding( rw->numVerts ); |
| 520 |
w->numpoints = rw->numVerts; |
| 521 |
VectorClear( normal ); |
| 522 |
for ( i = 0; i < rw->numVerts; i++ ) |
| 523 |
{ |
| 524 |
VectorCopy( rw->verts[ i ].xyz, w->p[ i ] ); |
| 525 |
VectorAdd( normal, rw->verts[ i ].normal, normal ); |
| 526 |
} |
| 527 |
VectorScale( normal, ( 1.0f / rw->numVerts ), normal ); |
| 528 |
if ( VectorNormalize( normal, normal ) == 0.0f ) { |
| 529 |
return; |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
if ( bouncing && VectorLength( color ) < RADIOSITY_MIN ) { |
| 534 |
return; |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
numDiffuseLights++; |
| 543 |
switch ( ds->surfaceType ) |
| 544 |
{ |
| 545 |
case MST_PLANAR: |
| 546 |
numBrushDiffuseLights++; |
| 547 |
break; |
| 548 |
|
| 549 |
case MST_TRIANGLE_SOUP: |
| 550 |
numTriangleDiffuseLights++; |
| 551 |
break; |
| 552 |
|
| 553 |
case MST_PATCH: |
| 554 |
numPatchDiffuseLights++; |
| 555 |
break; |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
light = safe_malloc0( sizeof( *light ) ); |
| 560 |
|
| 561 |
|
| 562 |
ThreadLock(); |
| 563 |
light->next = lights; |
| 564 |
lights = light; |
| 565 |
ThreadUnlock(); |
| 566 |
|
| 567 |
|
| 568 |
light->flags = LIGHT_AREA_DEFAULT; |
| 569 |
light->type = EMIT_AREA; |
| 570 |
light->si = si; |
| 571 |
light->fade = 1.0f; |
| 572 |
light->w = w; |
| 573 |
|
| 574 |
|
| 575 |
light->falloffTolerance = falloffTolerance; |
| 576 |
|
| 577 |
|
| 578 |
if ( bouncing == qfalse ) { |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
value = si->value; |
| 584 |
light->photons = value * area * areaScale; |
| 585 |
light->add = value * formFactorValueScale * areaScale; |
| 586 |
VectorCopy( si->color, light->color ); |
| 587 |
VectorScale( light->color, light->add, light->emitColor ); |
| 588 |
light->style = noStyles ? LS_NORMAL : si->lightStyle; |
| 589 |
if ( light->style < LS_NORMAL || light->style >= LS_NONE ) { |
| 590 |
light->style = LS_NORMAL; |
| 591 |
} |
| 592 |
|
| 593 |
|
| 594 |
VectorAdd( mins, maxs, light->origin ); |
| 595 |
VectorScale( light->origin, 0.5f, light->origin ); |
| 596 |
|
| 597 |
|
| 598 |
VectorCopy( normal, light->normal ); |
| 599 |
VectorMA( light->origin, 1.0f, light->normal, light->origin ); |
| 600 |
light->dist = DotProduct( light->origin, normal ); |
| 601 |
|
| 602 |
if ( si->backsplashFraction > 0 && !( si->compileFlags & C_SKY ) ) { |
| 603 |
|
| 604 |
|
| 605 |
qboolean bsArea; |
| 606 |
|
| 607 |
switch ( si->backsplashType ) |
| 608 |
{ |
| 609 |
case 1: |
| 610 |
bsArea = qfalse; |
| 611 |
break; |
| 612 |
case 2: |
| 613 |
bsArea = qtrue; |
| 614 |
break; |
| 615 |
default: |
| 616 |
bsArea = backsplashArea; |
| 617 |
} |
| 618 |
|
| 619 |
if ( bsArea ) { |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
splash = safe_malloc( sizeof( *splash ) ); |
| 632 |
memset( splash, 0, sizeof( *splash ) ); |
| 633 |
ThreadLock(); |
| 634 |
splash->next = lights; |
| 635 |
lights = splash; |
| 636 |
ThreadUnlock(); |
| 637 |
|
| 638 |
|
| 639 |
splash->flags = LIGHT_AREA_DEFAULT; |
| 640 |
splash->type = EMIT_AREA; |
| 641 |
splash->photons = light->photons * 7.0f * si->backsplashFraction; |
| 642 |
splash->add = light->add * 7.0f * si->backsplashFraction; |
| 643 |
splash->fade = 1.0f; |
| 644 |
splash->si = si; |
| 645 |
VectorCopy( si->color, splash->color ); |
| 646 |
VectorScale( splash->color, splash->add, splash->emitColor ); |
| 647 |
splash->falloffTolerance = falloffTolerance; |
| 648 |
splash->style = noStyles ? LS_NORMAL : si->lightStyle; |
| 649 |
|
| 650 |
if ( splash->style < LS_NORMAL || splash->style >= LS_NONE ) { |
| 651 |
splash->style = LS_NORMAL; |
| 652 |
} |
| 653 |
|
| 654 |
|
| 655 |
splash_w = AllocWinding( rw->numVerts ); |
| 656 |
splash_w->numpoints = rw->numVerts; |
| 657 |
for ( i = 0; i < rw->numVerts; i++ ) |
| 658 |
{ |
| 659 |
VectorMA( rw->verts[rw->numVerts - 1 - i].xyz, si->backsplashDistance, normal, splash_w->p[ i ] ); |
| 660 |
} |
| 661 |
|
| 662 |
splash->w = splash_w; |
| 663 |
|
| 664 |
VectorMA( light->origin, si->backsplashDistance, normal, splash->origin ); |
| 665 |
VectorNegate( normal, splash->normal ); |
| 666 |
splash->dist = DotProduct( splash->origin, splash->normal ); |
| 667 |
} |
| 668 |
else { |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
splash = safe_malloc0( sizeof( *splash ) ); |
| 673 |
|
| 674 |
splash->next = lights; |
| 675 |
lights = splash; |
| 676 |
|
| 677 |
|
| 678 |
splash->flags = LIGHT_Q3A_DEFAULT; |
| 679 |
splash->type = EMIT_POINT; |
| 680 |
splash->photons = light->photons * si->backsplashFraction; |
| 681 |
|
| 682 |
splash->fade = 1.0f; |
| 683 |
splash->si = si; |
| 684 |
VectorMA( light->origin, si->backsplashDistance, normal, splash->origin ); |
| 685 |
VectorCopy( si->color, splash->color ); |
| 686 |
|
| 687 |
splash->falloffTolerance = falloffTolerance; |
| 688 |
splash->style = noStyles ? LS_NORMAL : light->style; |
| 689 |
|
| 690 |
|
| 691 |
numPointLights++; |
| 692 |
} |
| 693 |
} |
| 694 |
} |
| 695 |
else |
| 696 |
{ |
| 697 |
|
| 698 |
value = RADIOSITY_VALUE * si->bounceScale * 0.375f; |
| 699 |
light->photons = value * area * bounceScale; |
| 700 |
light->add = value * formFactorValueScale * bounceScale; |
| 701 |
VectorCopy( color, light->color ); |
| 702 |
VectorScale( light->color, light->add, light->emitColor ); |
| 703 |
light->style = noStyles ? LS_NORMAL : style; |
| 704 |
if ( light->style < LS_NORMAL || light->style >= LS_NONE ) { |
| 705 |
light->style = LS_NORMAL; |
| 706 |
} |
| 707 |
|
| 708 |
|
| 709 |
WindingCenter( w, light->origin ); |
| 710 |
|
| 711 |
|
| 712 |
VectorCopy( normal, light->normal ); |
| 713 |
VectorMA( light->origin, 1.0f, light->normal, light->origin ); |
| 714 |
light->dist = DotProduct( light->origin, normal ); |
| 715 |
} |
| 716 |
|
| 717 |
if (light->photons < 0 || light->add < 0 || light->color[0] < 0 || light->color[1] < 0 || light->color[2] < 0) |
| 718 |
Sys_Printf( "BUG: RadSubdivideDiffuseLight created a darkbulb\n" ); |
| 719 |
|
| 720 |
|
| 721 |
if ( si->compileFlags & C_FOG || si->twoSided ) { |
| 722 |
light->flags |= LIGHT_TWOSIDED; |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
} |
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
void RadLightForTriangles( int num, int lightmapNum, rawLightmap_t *lm, shaderInfo_t *si, float scale, float subdivide, clipWork_t *cw ){ |
| 738 |
int i, j, k, v; |
| 739 |
bspDrawSurface_t *ds; |
| 740 |
float *radVertexLuxel; |
| 741 |
radWinding_t rw; |
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
ds = &bspDrawSurfaces[ num ]; |
| 746 |
|
| 747 |
|
| 748 |
rw.numVerts = 3; |
| 749 |
for ( i = 0; i < ds->numIndexes; i += 3 ) |
| 750 |
{ |
| 751 |
|
| 752 |
for ( j = 0; j < 3; j++ ) |
| 753 |
{ |
| 754 |
|
| 755 |
v = ds->firstVert + bspDrawIndexes[ ds->firstIndex + i + j ]; |
| 756 |
|
| 757 |
|
| 758 |
memcpy( &rw.verts[ j ], &yDrawVerts[ v ], sizeof( bspDrawVert_t ) ); |
| 759 |
|
| 760 |
|
| 761 |
for ( k = 0; k < MAX_LIGHTMAPS; k++ ) |
| 762 |
{ |
| 763 |
radVertexLuxel = RAD_VERTEX_LUXEL( k, ds->firstVert + bspDrawIndexes[ ds->firstIndex + i + j ] ); |
| 764 |
VectorCopy( radVertexLuxel, rw.verts[ j ].color[ k ] ); |
| 765 |
rw.verts[ j ].color[ k ][ 3 ] = yDrawVerts[ v ].color[ k ][ 3 ]; |
| 766 |
} |
| 767 |
} |
| 768 |
|
| 769 |
|
| 770 |
RadSubdivideDiffuseLight( lightmapNum, ds, lm, si, scale, subdivide, &rw, cw ); |
| 771 |
} |
| 772 |
} |
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
#define PLANAR_EPSILON 0.1f |
| 782 |
|
| 783 |
void RadLightForPatch( int num, int lightmapNum, rawLightmap_t *lm, shaderInfo_t *si, float scale, float subdivide, clipWork_t *cw ){ |
| 784 |
int i, x, y, v, t, pw[ 5 ], r; |
| 785 |
bspDrawSurface_t *ds; |
| 786 |
surfaceInfo_t *info; |
| 787 |
bspDrawVert_t *bogus; |
| 788 |
bspDrawVert_t *dv[ 4 ]; |
| 789 |
mesh_t src, *subdivided, *mesh; |
| 790 |
float *radVertexLuxel; |
| 791 |
float dist; |
| 792 |
vec4_t plane; |
| 793 |
qboolean planar; |
| 794 |
radWinding_t rw; |
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
ds = &bspDrawSurfaces[ num ]; |
| 799 |
info = &surfaceInfos[ num ]; |
| 800 |
|
| 801 |
|
| 802 |
bogus = safe_malloc( ds->numVerts * sizeof( bspDrawVert_t ) ); |
| 803 |
memcpy( bogus, &yDrawVerts[ ds->firstVert ], ds->numVerts * sizeof( bspDrawVert_t ) ); |
| 804 |
for ( i = 0; i < ds->numVerts; i++ ) |
| 805 |
bogus[ i ].color[ 0 ][ 0 ] = i; |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
src.width = ds->patchWidth; |
| 810 |
src.height = ds->patchHeight; |
| 811 |
src.verts = bogus; |
| 812 |
|
| 813 |
subdivided = SubdivideMesh2( src, info->patchIterations ); |
| 814 |
PutMeshOnCurve( *subdivided ); |
| 815 |
|
| 816 |
mesh = RemoveLinearMeshColumnsRows( subdivided ); |
| 817 |
FreeMesh( subdivided ); |
| 818 |
free( bogus ); |
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
for ( i = 0; i < ( mesh->width * mesh->height ); i++ ) |
| 824 |
{ |
| 825 |
dv[ 0 ] = &mesh->verts[ i ]; |
| 826 |
if ( dv[ 0 ]->color[ 0 ][ 0 ] >= ds->numVerts ) { |
| 827 |
dv[ 0 ]->color[ 0 ][ 0 ] = ds->numVerts - 1; |
| 828 |
} |
| 829 |
} |
| 830 |
|
| 831 |
|
| 832 |
for ( y = 0; y < ( mesh->height - 1 ); y++ ) |
| 833 |
{ |
| 834 |
for ( x = 0; x < ( mesh->width - 1 ); x++ ) |
| 835 |
{ |
| 836 |
|
| 837 |
pw[ 0 ] = x + ( y * mesh->width ); |
| 838 |
pw[ 1 ] = x + ( ( y + 1 ) * mesh->width ); |
| 839 |
pw[ 2 ] = x + 1 + ( ( y + 1 ) * mesh->width ); |
| 840 |
pw[ 3 ] = x + 1 + ( y * mesh->width ); |
| 841 |
pw[ 4 ] = x + ( y * mesh->width ); |
| 842 |
|
| 843 |
|
| 844 |
r = ( x + y ) & 1; |
| 845 |
|
| 846 |
|
| 847 |
dv[ 0 ] = &mesh->verts[ pw[ r + 0 ] ]; |
| 848 |
dv[ 1 ] = &mesh->verts[ pw[ r + 1 ] ]; |
| 849 |
dv[ 2 ] = &mesh->verts[ pw[ r + 2 ] ]; |
| 850 |
dv[ 3 ] = &mesh->verts[ pw[ r + 3 ] ]; |
| 851 |
|
| 852 |
|
| 853 |
planar = PlaneFromPoints( plane, dv[ 0 ]->xyz, dv[ 1 ]->xyz, dv[ 2 ]->xyz ); |
| 854 |
if ( planar ) { |
| 855 |
dist = DotProduct( dv[ 1 ]->xyz, plane ) - plane[ 3 ]; |
| 856 |
if ( fabs( dist ) > PLANAR_EPSILON ) { |
| 857 |
planar = qfalse; |
| 858 |
} |
| 859 |
} |
| 860 |
|
| 861 |
|
| 862 |
if ( planar ) { |
| 863 |
rw.numVerts = 4; |
| 864 |
for ( v = 0; v < 4; v++ ) |
| 865 |
{ |
| 866 |
|
| 867 |
memcpy( &rw.verts[ v ], dv[ v ], sizeof( bspDrawVert_t ) ); |
| 868 |
|
| 869 |
|
| 870 |
for ( i = 0; i < MAX_LIGHTMAPS; i++ ) |
| 871 |
{ |
| 872 |
radVertexLuxel = RAD_VERTEX_LUXEL( i, ds->firstVert + dv[ v ]->color[ 0 ][ 0 ] ); |
| 873 |
VectorCopy( radVertexLuxel, rw.verts[ v ].color[ i ] ); |
| 874 |
rw.verts[ v ].color[ i ][ 3 ] = dv[ v ]->color[ i ][ 3 ]; |
| 875 |
} |
| 876 |
} |
| 877 |
|
| 878 |
|
| 879 |
RadSubdivideDiffuseLight( lightmapNum, ds, lm, si, scale, subdivide, &rw, cw ); |
| 880 |
} |
| 881 |
|
| 882 |
|
| 883 |
else |
| 884 |
{ |
| 885 |
rw.numVerts = 3; |
| 886 |
for ( t = 0; t < 2; t++ ) |
| 887 |
{ |
| 888 |
for ( v = 0; v < 3 + t; v++ ) |
| 889 |
{ |
| 890 |
|
| 891 |
if ( v == 1 && t == 1 ) { |
| 892 |
v++; |
| 893 |
} |
| 894 |
|
| 895 |
|
| 896 |
memcpy( &rw.verts[ v ], dv[ v ], sizeof( bspDrawVert_t ) ); |
| 897 |
|
| 898 |
|
| 899 |
for ( i = 0; i < MAX_LIGHTMAPS; i++ ) |
| 900 |
{ |
| 901 |
radVertexLuxel = RAD_VERTEX_LUXEL( i, ds->firstVert + dv[ v ]->color[ 0 ][ 0 ] ); |
| 902 |
VectorCopy( radVertexLuxel, rw.verts[ v ].color[ i ] ); |
| 903 |
rw.verts[ v ].color[ i ][ 3 ] = dv[ v ]->color[ i ][ 3 ]; |
| 904 |
} |
| 905 |
} |
| 906 |
|
| 907 |
|
| 908 |
RadSubdivideDiffuseLight( lightmapNum, ds, lm, si, scale, subdivide, &rw, cw ); |
| 909 |
} |
| 910 |
} |
| 911 |
} |
| 912 |
} |
| 913 |
|
| 914 |
|
| 915 |
FreeMesh( mesh ); |
| 916 |
} |
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
void RadLight( int num ){ |
| 927 |
int lightmapNum; |
| 928 |
float scale, subdivide; |
| 929 |
int contentFlags, surfaceFlags, compileFlags; |
| 930 |
bspDrawSurface_t *ds; |
| 931 |
surfaceInfo_t *info; |
| 932 |
rawLightmap_t *lm; |
| 933 |
shaderInfo_t *si; |
| 934 |
clipWork_t cw; |
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
ds = &bspDrawSurfaces[ num ]; |
| 939 |
info = &surfaceInfos[ num ]; |
| 940 |
lm = info->lm; |
| 941 |
si = info->si; |
| 942 |
scale = si->bounceScale; |
| 943 |
|
| 944 |
|
| 945 |
contentFlags = surfaceFlags = compileFlags = 0; |
| 946 |
ApplySurfaceParm( "nodraw", &contentFlags, &surfaceFlags, &compileFlags ); |
| 947 |
|
| 948 |
|
| 949 |
ApplySurfaceParm( "trans", &contentFlags, &surfaceFlags, &compileFlags ); |
| 950 |
|
| 951 |
|
| 952 |
if ( scale <= 0.0f || ( si->compileFlags & C_SKY ) || si->autosprite || |
| 953 |
( bspShaders[ ds->shaderNum ].contentFlags & contentFlags ) || ( bspShaders[ ds->shaderNum ].surfaceFlags & surfaceFlags ) || |
| 954 |
( si->compileFlags & compileFlags ) ) { |
| 955 |
return; |
| 956 |
} |
| 957 |
|
| 958 |
|
| 959 |
if ( si->lightSubdivide ) { |
| 960 |
subdivide = si->lightSubdivide; |
| 961 |
} |
| 962 |
else{ |
| 963 |
subdivide = diffuseSubdivide; |
| 964 |
} |
| 965 |
|
| 966 |
|
| 967 |
numDiffuseSurfaces++; |
| 968 |
|
| 969 |
|
| 970 |
for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ ) |
| 971 |
{ |
| 972 |
|
| 973 |
if ( ds->lightmapStyles[ lightmapNum ] != LS_NONE && ds->lightmapStyles[ lightmapNum ] != LS_UNUSED ) { |
| 974 |
switch ( ds->surfaceType ) |
| 975 |
{ |
| 976 |
case MST_PLANAR: |
| 977 |
case MST_TRIANGLE_SOUP: |
| 978 |
RadLightForTriangles( num, lightmapNum, lm, si, scale, subdivide, &cw ); |
| 979 |
break; |
| 980 |
|
| 981 |
case MST_PATCH: |
| 982 |
RadLightForPatch( num, lightmapNum, lm, si, scale, subdivide, &cw ); |
| 983 |
break; |
| 984 |
|
| 985 |
default: |
| 986 |
break; |
| 987 |
} |
| 988 |
} |
| 989 |
} |
| 990 |
} |
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
int iterations = 0; |
| 1000 |
|
| 1001 |
void RadCreateDiffuseLights( void ){ |
| 1002 |
|
| 1003 |
Sys_FPrintf( SYS_VRB, "--- RadCreateDiffuseLights ---\n" ); |
| 1004 |
numDiffuseSurfaces = 0; |
| 1005 |
numDiffuseLights = 0; |
| 1006 |
numBrushDiffuseLights = 0; |
| 1007 |
numTriangleDiffuseLights = 0; |
| 1008 |
numPatchDiffuseLights = 0; |
| 1009 |
numAreaLights = 0; |
| 1010 |
|
| 1011 |
|
| 1012 |
RunThreadsOnIndividual( numBSPDrawSurfaces, qtrue, RadLight ); |
| 1013 |
|
| 1014 |
|
| 1015 |
if ( dump ) { |
| 1016 |
char dumpName[ 1024 ], ext[ 64 ]; |
| 1017 |
FILE *file; |
| 1018 |
light_t *light; |
| 1019 |
|
| 1020 |
strcpy( dumpName, source ); |
| 1021 |
StripExtension( dumpName ); |
| 1022 |
sprintf( ext, "_bounce_%03d.map", iterations ); |
| 1023 |
strcat( dumpName, ext ); |
| 1024 |
file = fopen( dumpName, "wb" ); |
| 1025 |
Sys_Printf( "Writing %s...\n", dumpName ); |
| 1026 |
if ( file ) { |
| 1027 |
for ( light = lights; light; light = light->next ) |
| 1028 |
{ |
| 1029 |
fprintf( file, |
| 1030 |
"{\n" |
| 1031 |
"\"classname\" \"light\"\n" |
| 1032 |
"\"light\" \"%d\"\n" |
| 1033 |
"\"origin\" \"%.0f %.0f %.0f\"\n" |
| 1034 |
"\"_color\" \"%.3f %.3f %.3f\"\n" |
| 1035 |
"}\n", |
| 1036 |
|
| 1037 |
(int) light->add, |
| 1038 |
|
| 1039 |
light->origin[ 0 ], |
| 1040 |
light->origin[ 1 ], |
| 1041 |
light->origin[ 2 ], |
| 1042 |
|
| 1043 |
light->color[ 0 ], |
| 1044 |
light->color[ 1 ], |
| 1045 |
light->color[ 2 ] ); |
| 1046 |
} |
| 1047 |
fclose( file ); |
| 1048 |
} |
| 1049 |
} |
| 1050 |
|
| 1051 |
|
| 1052 |
iterations++; |
| 1053 |
|
| 1054 |
|
| 1055 |
Sys_Printf( "%8d diffuse surfaces\n", numDiffuseSurfaces ); |
| 1056 |
Sys_FPrintf( SYS_VRB, "%8d total diffuse lights\n", numDiffuseLights ); |
| 1057 |
Sys_FPrintf( SYS_VRB, "%8d brush diffuse lights\n", numBrushDiffuseLights ); |
| 1058 |
Sys_FPrintf( SYS_VRB, "%8d patch diffuse lights\n", numPatchDiffuseLights ); |
| 1059 |
Sys_FPrintf( SYS_VRB, "%8d triangle diffuse lights\n", numTriangleDiffuseLights ); |
| 1060 |
} |