| 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_YDNAR_C |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
#include "q3map2.h" |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
void ColorToBytes( const float *color, byte *colorBytes, float scale ){ |
| 48 |
int i; |
| 49 |
float max, gamma; |
| 50 |
vec3_t sample; |
| 51 |
float inv, dif; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
if ( scale <= 0.0f ) { |
| 56 |
scale = 1.0f; |
| 57 |
} |
| 58 |
|
| 59 |
scale *= lightmapBrightness; |
| 60 |
|
| 61 |
|
| 62 |
VectorScale( color, scale, sample ); |
| 63 |
|
| 64 |
|
| 65 |
gamma = 1.0f / lightmapGamma; |
| 66 |
for ( i = 0; i < 3; i++ ) |
| 67 |
{ |
| 68 |
|
| 69 |
if ( sample[ i ] < 0.0f ) { |
| 70 |
sample[ i ] = 0.0f; |
| 71 |
continue; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
sample[ i ] = pow( sample[ i ] / 255.0f, gamma ) * 255.0f; |
| 76 |
} |
| 77 |
|
| 78 |
if ( lightmapExposure == 0 ) { |
| 79 |
|
| 80 |
max = sample[ 0 ]; |
| 81 |
if ( sample[ 1 ] > max ) { |
| 82 |
max = sample[ 1 ]; |
| 83 |
} |
| 84 |
if ( sample[ 2 ] > max ) { |
| 85 |
max = sample[ 2 ]; |
| 86 |
} |
| 87 |
if ( max > 255.0f ) { |
| 88 |
VectorScale( sample, ( 255.0f / max ), sample ); |
| 89 |
} |
| 90 |
} |
| 91 |
else |
| 92 |
{ |
| 93 |
inv = 1.f / lightmapExposure; |
| 94 |
|
| 95 |
|
| 96 |
max = sample[ 0 ]; |
| 97 |
if ( sample[ 1 ] > max ) { |
| 98 |
max = sample[ 1 ]; |
| 99 |
} |
| 100 |
if ( sample[ 2 ] > max ) { |
| 101 |
max = sample[ 2 ]; |
| 102 |
} |
| 103 |
|
| 104 |
dif = ( 1 - exp( -max * inv ) ) * 255; |
| 105 |
|
| 106 |
if ( max > 0 ) { |
| 107 |
dif = dif / max; |
| 108 |
} |
| 109 |
else |
| 110 |
{ |
| 111 |
dif = 0; |
| 112 |
} |
| 113 |
|
| 114 |
for ( i = 0; i < 3; i++ ) |
| 115 |
{ |
| 116 |
sample[i] *= dif; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
VectorScale( sample, ( 1.0f / lightmapCompensate ), sample ); |
| 123 |
|
| 124 |
|
| 125 |
if ( lightmapContrast != 1.0f ){ |
| 126 |
for ( i = 0; i < 3; i++ ){ |
| 127 |
sample[i] = lightmapContrast * ( sample[i] - 128 ) + 128; |
| 128 |
if ( sample[i] < 0 ){ |
| 129 |
sample[i] = 0; |
| 130 |
} |
| 131 |
} |
| 132 |
if ( ( sample[0] > 255 ) || ( sample[1] > 255 ) || ( sample[2] > 255 ) ) { |
| 133 |
max = sample[0] > sample[1] ? sample[0] : sample[1]; |
| 134 |
max = max > sample[2] ? max : sample[2]; |
| 135 |
sample[0] = sample[0] * 255 / max; |
| 136 |
sample[1] = sample[1] * 255 / max; |
| 137 |
sample[2] = sample[2] * 255 / max; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
if ( lightmapsRGB ) { |
| 143 |
sample[0] = floor( Image_sRGBFloatFromLinearFloat( sample[0] * ( 1.0 / 255.0 ) ) * 255.0 + 0.5 ); |
| 144 |
sample[1] = floor( Image_sRGBFloatFromLinearFloat( sample[1] * ( 1.0 / 255.0 ) ) * 255.0 + 0.5 ); |
| 145 |
sample[2] = floor( Image_sRGBFloatFromLinearFloat( sample[2] * ( 1.0 / 255.0 ) ) * 255.0 + 0.5 ); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
colorBytes[ 0 ] = sample[ 0 ]; |
| 150 |
colorBytes[ 1 ] = sample[ 1 ]; |
| 151 |
colorBytes[ 2 ] = sample[ 2 ]; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
void ColorToBytesNonZero( const float *color, byte *colorBytes, float scale) { |
| 161 |
int i; |
| 162 |
ColorToBytes(color, colorBytes, scale); |
| 163 |
for (i = 0; i < 3; ++i) |
| 164 |
if (colorBytes[i] == 0) |
| 165 |
colorBytes[i] = 1; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
#define MAX_SAMPLES 256 |
| 181 |
#define THETA_EPSILON 0.000001 |
| 182 |
#define EQUAL_NORMAL_EPSILON 0.01 |
| 183 |
|
| 184 |
void SmoothNormals( void ){ |
| 185 |
int i, j, k, f, cs, numVerts, numVotes, fOld, start; |
| 186 |
float shadeAngle, defaultShadeAngle, maxShadeAngle, dot, testAngle; |
| 187 |
bspDrawSurface_t *ds; |
| 188 |
shaderInfo_t *si; |
| 189 |
float *shadeAngles; |
| 190 |
byte *smoothed; |
| 191 |
vec3_t average, diff; |
| 192 |
int indexes[ MAX_SAMPLES ]; |
| 193 |
vec3_t votes[ MAX_SAMPLES ]; |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
shadeAngles = safe_malloc0( numBSPDrawVerts * sizeof( float ) ); |
| 198 |
|
| 199 |
|
| 200 |
cs = ( numBSPDrawVerts / 8 ) + 1; |
| 201 |
smoothed = safe_malloc0( cs ); |
| 202 |
|
| 203 |
|
| 204 |
defaultShadeAngle = DEG2RAD( shadeAngleDegrees ); |
| 205 |
maxShadeAngle = 0; |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
for ( i = 0; i < numBSPDrawSurfaces; i++ ) |
| 210 |
{ |
| 211 |
|
| 212 |
ds = &bspDrawSurfaces[ i ]; |
| 213 |
|
| 214 |
|
| 215 |
si = surfaceInfos[ i ].si; |
| 216 |
if ( si->shadeAngleDegrees ) { |
| 217 |
shadeAngle = DEG2RAD( si->shadeAngleDegrees ); |
| 218 |
} |
| 219 |
else{ |
| 220 |
shadeAngle = defaultShadeAngle; |
| 221 |
} |
| 222 |
if ( shadeAngle > maxShadeAngle ) { |
| 223 |
maxShadeAngle = shadeAngle; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
for ( j = 0; j < ds->numVerts; j++ ) |
| 228 |
{ |
| 229 |
f = ds->firstVert + j; |
| 230 |
shadeAngles[ f ] = shadeAngle; |
| 231 |
if ( ds->surfaceType == MST_TRIANGLE_SOUP ) { |
| 232 |
smoothed[ f >> 3 ] |= ( 1 << ( f & 7 ) ); |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
if ( trisoup && ds->surfaceType == MST_PLANAR ) { |
| 238 |
ds->surfaceType = MST_TRIANGLE_SOUP; |
| 239 |
ds->lightmapNum[ 0 ] = -3; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
if ( maxShadeAngle == 0 ) { |
| 245 |
free( shadeAngles ); |
| 246 |
free( smoothed ); |
| 247 |
return; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
fOld = -1; |
| 252 |
start = I_FloatTime(); |
| 253 |
|
| 254 |
|
| 255 |
for ( i = 0; i < numBSPDrawVerts; i++ ) |
| 256 |
{ |
| 257 |
|
| 258 |
f = 10 * i / numBSPDrawVerts; |
| 259 |
if ( f != fOld ) { |
| 260 |
fOld = f; |
| 261 |
Sys_Printf( "%i...", f ); |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
if ( smoothed[ i >> 3 ] & ( 1 << ( i & 7 ) ) ) { |
| 266 |
continue; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
VectorClear( average ); |
| 271 |
numVerts = 0; |
| 272 |
numVotes = 0; |
| 273 |
|
| 274 |
|
| 275 |
for ( j = i; j < numBSPDrawVerts && numVerts < MAX_SAMPLES; j++ ) |
| 276 |
{ |
| 277 |
|
| 278 |
if ( smoothed[ j >> 3 ] & ( 1 << ( j & 7 ) ) ) { |
| 279 |
continue; |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
if ( VectorCompare( yDrawVerts[ i ].xyz, yDrawVerts[ j ].xyz ) == qfalse ) { |
| 284 |
continue; |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
shadeAngle = ( shadeAngles[ i ] < shadeAngles[ j ] ? shadeAngles[ i ] : shadeAngles[ j ] ); |
| 289 |
|
| 290 |
|
| 291 |
dot = DotProduct( bspDrawVerts[ i ].normal, bspDrawVerts[ j ].normal ); |
| 292 |
if ( dot > 1.0 ) { |
| 293 |
dot = 1.0; |
| 294 |
} |
| 295 |
else if ( dot < -1.0 ) { |
| 296 |
dot = -1.0; |
| 297 |
} |
| 298 |
testAngle = acos( dot ) + THETA_EPSILON; |
| 299 |
if ( testAngle >= shadeAngle ) { |
| 300 |
|
| 301 |
continue; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
indexes[ numVerts++ ] = j; |
| 307 |
|
| 308 |
|
| 309 |
smoothed[ j >> 3 ] |= ( 1 << ( j & 7 ) ); |
| 310 |
|
| 311 |
|
| 312 |
for ( k = 0; k < numVotes; k++ ) |
| 313 |
{ |
| 314 |
VectorSubtract( bspDrawVerts[ j ].normal, votes[ k ], diff ); |
| 315 |
if ( fabs( diff[ 0 ] ) < EQUAL_NORMAL_EPSILON && |
| 316 |
fabs( diff[ 1 ] ) < EQUAL_NORMAL_EPSILON && |
| 317 |
fabs( diff[ 2 ] ) < EQUAL_NORMAL_EPSILON ) { |
| 318 |
break; |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
if ( k == numVotes && numVotes < MAX_SAMPLES ) { |
| 324 |
VectorAdd( average, bspDrawVerts[ j ].normal, average ); |
| 325 |
VectorCopy( bspDrawVerts[ j ].normal, votes[ numVotes ] ); |
| 326 |
numVotes++; |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
if ( numVerts < 2 ) { |
| 332 |
continue; |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
if ( VectorNormalize( average, average ) > 0 ) { |
| 337 |
|
| 338 |
for ( j = 0; j < numVerts; j++ ) |
| 339 |
VectorCopy( average, yDrawVerts[ indexes[ j ] ].normal ); |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
free( shadeAngles ); |
| 345 |
free( smoothed ); |
| 346 |
|
| 347 |
|
| 348 |
Sys_Printf( " (%i)\n", (int) ( I_FloatTime() - start ) ); |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
static qboolean CalcTangentVectors( int numVerts, bspDrawVert_t **dv, vec3_t *stv, vec3_t *ttv ){ |
| 367 |
int i; |
| 368 |
float bb, s, t; |
| 369 |
vec3_t bary; |
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
bb = ( dv[ 1 ]->st[ 0 ] - dv[ 0 ]->st[ 0 ] ) * ( dv[ 2 ]->st[ 1 ] - dv[ 0 ]->st[ 1 ] ) - ( dv[ 2 ]->st[ 0 ] - dv[ 0 ]->st[ 0 ] ) * ( dv[ 1 ]->st[ 1 ] - dv[ 0 ]->st[ 1 ] ); |
| 374 |
if ( fabs( bb ) < 0.00000001f ) { |
| 375 |
return qfalse; |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
for ( i = 0; i < numVerts; i++ ) |
| 380 |
{ |
| 381 |
|
| 382 |
s = dv[ i ]->st[ 0 ] + 10.0f; |
| 383 |
t = dv[ i ]->st[ 1 ]; |
| 384 |
bary[ 0 ] = ( ( dv[ 1 ]->st[ 0 ] - s ) * ( dv[ 2 ]->st[ 1 ] - t ) - ( dv[ 2 ]->st[ 0 ] - s ) * ( dv[ 1 ]->st[ 1 ] - t ) ) / bb; |
| 385 |
bary[ 1 ] = ( ( dv[ 2 ]->st[ 0 ] - s ) * ( dv[ 0 ]->st[ 1 ] - t ) - ( dv[ 0 ]->st[ 0 ] - s ) * ( dv[ 2 ]->st[ 1 ] - t ) ) / bb; |
| 386 |
bary[ 2 ] = ( ( dv[ 0 ]->st[ 0 ] - s ) * ( dv[ 1 ]->st[ 1 ] - t ) - ( dv[ 1 ]->st[ 0 ] - s ) * ( dv[ 0 ]->st[ 1 ] - t ) ) / bb; |
| 387 |
|
| 388 |
stv[ i ][ 0 ] = bary[ 0 ] * dv[ 0 ]->xyz[ 0 ] + bary[ 1 ] * dv[ 1 ]->xyz[ 0 ] + bary[ 2 ] * dv[ 2 ]->xyz[ 0 ]; |
| 389 |
stv[ i ][ 1 ] = bary[ 0 ] * dv[ 0 ]->xyz[ 1 ] + bary[ 1 ] * dv[ 1 ]->xyz[ 1 ] + bary[ 2 ] * dv[ 2 ]->xyz[ 1 ]; |
| 390 |
stv[ i ][ 2 ] = bary[ 0 ] * dv[ 0 ]->xyz[ 2 ] + bary[ 1 ] * dv[ 1 ]->xyz[ 2 ] + bary[ 2 ] * dv[ 2 ]->xyz[ 2 ]; |
| 391 |
|
| 392 |
VectorSubtract( stv[ i ], dv[ i ]->xyz, stv[ i ] ); |
| 393 |
VectorNormalize( stv[ i ], stv[ i ] ); |
| 394 |
|
| 395 |
|
| 396 |
s = dv[ i ]->st[ 0 ]; |
| 397 |
t = dv[ i ]->st[ 1 ] + 10.0f; |
| 398 |
bary[ 0 ] = ( ( dv[ 1 ]->st[ 0 ] - s ) * ( dv[ 2 ]->st[ 1 ] - t ) - ( dv[ 2 ]->st[ 0 ] - s ) * ( dv[ 1 ]->st[ 1 ] - t ) ) / bb; |
| 399 |
bary[ 1 ] = ( ( dv[ 2 ]->st[ 0 ] - s ) * ( dv[ 0 ]->st[ 1 ] - t ) - ( dv[ 0 ]->st[ 0 ] - s ) * ( dv[ 2 ]->st[ 1 ] - t ) ) / bb; |
| 400 |
bary[ 2 ] = ( ( dv[ 0 ]->st[ 0 ] - s ) * ( dv[ 1 ]->st[ 1 ] - t ) - ( dv[ 1 ]->st[ 0 ] - s ) * ( dv[ 0 ]->st[ 1 ] - t ) ) / bb; |
| 401 |
|
| 402 |
ttv[ i ][ 0 ] = bary[ 0 ] * dv[ 0 ]->xyz[ 0 ] + bary[ 1 ] * dv[ 1 ]->xyz[ 0 ] + bary[ 2 ] * dv[ 2 ]->xyz[ 0 ]; |
| 403 |
ttv[ i ][ 1 ] = bary[ 0 ] * dv[ 0 ]->xyz[ 1 ] + bary[ 1 ] * dv[ 1 ]->xyz[ 1 ] + bary[ 2 ] * dv[ 2 ]->xyz[ 1 ]; |
| 404 |
ttv[ i ][ 2 ] = bary[ 0 ] * dv[ 0 ]->xyz[ 2 ] + bary[ 1 ] * dv[ 1 ]->xyz[ 2 ] + bary[ 2 ] * dv[ 2 ]->xyz[ 2 ]; |
| 405 |
|
| 406 |
VectorSubtract( ttv[ i ], dv[ i ]->xyz, ttv[ i ] ); |
| 407 |
VectorNormalize( ttv[ i ], ttv[ i ] ); |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
return qtrue; |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
static void PerturbNormal( bspDrawVert_t *dv, shaderInfo_t *si, vec3_t pNormal, vec3_t stv[ 3 ], vec3_t ttv[ 3 ] ){ |
| 427 |
int i; |
| 428 |
vec4_t bump; |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
VectorCopy( dv->normal, pNormal ); |
| 433 |
|
| 434 |
|
| 435 |
if ( RadSampleImage( si->normalImage->pixels, si->normalImage->width, si->normalImage->height, dv->st, bump ) == qfalse ) { |
| 436 |
return; |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
for ( i = 0; i < 3; i++ ) |
| 441 |
bump[ i ] = ( bump[ i ] - 127.0f ) * ( 1.0f / 127.5f ); |
| 442 |
|
| 443 |
|
| 444 |
VectorMA( dv->normal, bump[ 0 ], stv[ 0 ], pNormal ); |
| 445 |
VectorMA( pNormal, bump[ 1 ], ttv[ 0 ], pNormal ); |
| 446 |
VectorMA( pNormal, bump[ 2 ], dv->normal, pNormal ); |
| 447 |
|
| 448 |
|
| 449 |
VectorNormalize( pNormal, pNormal ); |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
#define NUDGE 0.5f |
| 460 |
#define BOGUS_NUDGE -99999.0f |
| 461 |
|
| 462 |
static int MapSingleLuxel( rawLightmap_t *lm, surfaceInfo_t *info, bspDrawVert_t *dv, vec4_t plane, float pass, vec3_t stv[ 3 ], vec3_t ttv[ 3 ], vec3_t worldverts[ 3 ] ){ |
| 463 |
int i, x, y, numClusters, *clusters, pointCluster, *cluster; |
| 464 |
float *luxel, *origin, *normal, d, lightmapSampleOffset; |
| 465 |
shaderInfo_t *si; |
| 466 |
vec3_t pNormal; |
| 467 |
vec3_t vecs[ 3 ]; |
| 468 |
vec3_t nudged; |
| 469 |
vec3_t cverts[ 3 ]; |
| 470 |
vec3_t temp; |
| 471 |
vec4_t sideplane, hostplane; |
| 472 |
vec3_t origintwo; |
| 473 |
int j, next; |
| 474 |
float e; |
| 475 |
float *nudge; |
| 476 |
static float nudges[][ 2 ] = |
| 477 |
{ |
| 478 |
|
| 479 |
{ -NUDGE, 0 }, |
| 480 |
{ NUDGE, 0 }, |
| 481 |
{ 0, NUDGE }, |
| 482 |
{ 0, -NUDGE }, |
| 483 |
{ -NUDGE, NUDGE }, |
| 484 |
{ NUDGE, -NUDGE }, |
| 485 |
{ NUDGE, NUDGE }, |
| 486 |
{ -NUDGE, -NUDGE }, |
| 487 |
{ BOGUS_NUDGE, BOGUS_NUDGE } |
| 488 |
}; |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
x = dv->lightmap[ 0 ][ 0 ]; |
| 493 |
y = dv->lightmap[ 0 ][ 1 ]; |
| 494 |
if ( x < 0 ) { |
| 495 |
x = 0; |
| 496 |
} |
| 497 |
else if ( x >= lm->sw ) { |
| 498 |
x = lm->sw - 1; |
| 499 |
} |
| 500 |
if ( y < 0 ) { |
| 501 |
y = 0; |
| 502 |
} |
| 503 |
else if ( y >= lm->sh ) { |
| 504 |
y = lm->sh - 1; |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
if ( info != NULL ) { |
| 509 |
si = info->si; |
| 510 |
numClusters = info->numSurfaceClusters; |
| 511 |
clusters = &surfaceClusters[ info->firstSurfaceCluster ]; |
| 512 |
} |
| 513 |
else |
| 514 |
{ |
| 515 |
si = NULL; |
| 516 |
numClusters = 0; |
| 517 |
clusters = NULL; |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
luxel = SUPER_LUXEL( 0, x, y ); |
| 522 |
origin = SUPER_ORIGIN( x, y ); |
| 523 |
normal = SUPER_NORMAL( x, y ); |
| 524 |
cluster = SUPER_CLUSTER( x, y ); |
| 525 |
|
| 526 |
|
| 527 |
if ( ( *cluster ) == CLUSTER_OCCLUDED && lm->plane != NULL ) { |
| 528 |
return ( *cluster ); |
| 529 |
} |
| 530 |
|
| 531 |
|
| 532 |
else if ( ( *cluster ) >= 0 ) { |
| 533 |
|
| 534 |
if ( stv != NULL ) { |
| 535 |
PerturbNormal( dv, si, pNormal, stv, ttv ); |
| 536 |
} |
| 537 |
else{ |
| 538 |
VectorCopy( dv->normal, pNormal ); |
| 539 |
} |
| 540 |
|
| 541 |
|
| 542 |
VectorAdd( normal, pNormal, normal ); |
| 543 |
luxel[ 3 ] += 1.0f; |
| 544 |
return ( *cluster ); |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
if ( lm->vecs != NULL ) { |
| 553 |
|
| 554 |
VectorCopy( lm->origin, origin ); |
| 555 |
for ( i = 0; i < 3; i++ ) |
| 556 |
{ |
| 557 |
|
| 558 |
if ( i == lm->axisNum ) { |
| 559 |
continue; |
| 560 |
} |
| 561 |
origin[ i ] += ( x * lm->vecs[ 0 ][ i ] ) + ( y * lm->vecs[ 1 ][ i ] ); |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
d = DotProduct( origin, plane ) - plane[ 3 ]; |
| 566 |
|
| 567 |
vec_t c = plane[ lm->axisNum ]; |
| 568 |
d /= c ? c : 1; |
| 569 |
origin[ lm->axisNum ] -= d; |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
else{ |
| 574 |
VectorCopy( dv->xyz, origin ); |
| 575 |
} |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
if ( worldverts != NULL && lightmapTriangleCheck ) { |
| 583 |
for ( j = 0; j < 3; j++ ) |
| 584 |
{ |
| 585 |
VectorCopy( worldverts[j],cverts[j] ); |
| 586 |
} |
| 587 |
PlaneFromPoints( hostplane,cverts[0],cverts[1],cverts[2] ); |
| 588 |
|
| 589 |
for ( j = 0; j < 3; j++ ) |
| 590 |
{ |
| 591 |
for ( i = 0; i < 3; i++ ) |
| 592 |
{ |
| 593 |
|
| 594 |
next = ( i + 1 ) % 3; |
| 595 |
|
| 596 |
VectorCopy( cverts[next],temp ); |
| 597 |
VectorAdd( temp,hostplane,temp ); |
| 598 |
PlaneFromPoints( sideplane,cverts[i],cverts[ next ], temp ); |
| 599 |
|
| 600 |
|
| 601 |
e = DotProduct( origin,sideplane ); |
| 602 |
e = e - sideplane[3]; |
| 603 |
if ( e > 0 ) { |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
origin[0] -= sideplane[0] * ( e + 1 ); |
| 608 |
origin[1] -= sideplane[1] * ( e + 1 ); |
| 609 |
origin[2] -= sideplane[2] * ( e + 1 ); |
| 610 |
#ifdef DEBUG_27_1 |
| 611 |
VectorClear( origin ); |
| 612 |
#endif |
| 613 |
} |
| 614 |
} |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
if ( lm->plane != NULL ) { |
| 622 |
VectorCopy( lm->vecs[ 0 ], vecs[ 0 ] ); |
| 623 |
VectorCopy( lm->vecs[ 1 ], vecs[ 1 ] ); |
| 624 |
VectorCopy( lm->plane, vecs[ 2 ] ); |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
else |
| 629 |
{ |
| 630 |
if ( plane != NULL ) { |
| 631 |
VectorCopy( plane, vecs[ 2 ] ); |
| 632 |
} |
| 633 |
else{ |
| 634 |
VectorCopy( dv->normal, vecs[ 2 ] ); |
| 635 |
} |
| 636 |
MakeNormalVectors( vecs[ 2 ], vecs[ 0 ], vecs[ 1 ] ); |
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
if ( si != NULL ) { |
| 641 |
lightmapSampleOffset = si->lightmapSampleOffset; |
| 642 |
} |
| 643 |
else{ |
| 644 |
lightmapSampleOffset = DEFAULT_LIGHTMAP_SAMPLE_OFFSET; |
| 645 |
} |
| 646 |
if ( lm->axisNum < 0 ) { |
| 647 |
VectorMA( origin, lightmapSampleOffset, vecs[ 2 ], origin ); |
| 648 |
} |
| 649 |
else if ( vecs[ 2 ][ lm->axisNum ] < 0.0f ) { |
| 650 |
origin[ lm->axisNum ] -= lightmapSampleOffset; |
| 651 |
} |
| 652 |
else{ |
| 653 |
origin[ lm->axisNum ] += lightmapSampleOffset; |
| 654 |
} |
| 655 |
|
| 656 |
VectorCopy( origin,origintwo ); |
| 657 |
if ( lightmapExtraVisClusterNudge ) { |
| 658 |
origintwo[0] += vecs[2][0]; |
| 659 |
origintwo[1] += vecs[2][1]; |
| 660 |
origintwo[2] += vecs[2][2]; |
| 661 |
} |
| 662 |
|
| 663 |
|
| 664 |
pointCluster = ClusterForPointExtFilter( origintwo, LUXEL_EPSILON, numClusters, clusters ); |
| 665 |
|
| 666 |
|
| 667 |
luxel[ 1 ] = 0.0f; |
| 668 |
|
| 669 |
|
| 670 |
if ( pointCluster < 0 && dark == qfalse ) { |
| 671 |
|
| 672 |
nudge = nudges[ 0 ]; |
| 673 |
while ( nudge[ 0 ] > BOGUS_NUDGE && pointCluster < 0 ) |
| 674 |
{ |
| 675 |
|
| 676 |
for ( i = 0; i < 3; i++ ) |
| 677 |
{ |
| 678 |
|
| 679 |
nudged[ i ] = origintwo[ i ] + ( nudge[ 0 ] * vecs[ 0 ][ i ] ) + ( nudge[ 1 ] * vecs[ 1 ][ i ] ); |
| 680 |
} |
| 681 |
nudge += 2; |
| 682 |
|
| 683 |
|
| 684 |
pointCluster = ClusterForPointExtFilter( nudged, LUXEL_EPSILON, numClusters, clusters ); |
| 685 |
if ( pointCluster >= 0 ) { |
| 686 |
VectorCopy( nudged, origin ); |
| 687 |
} |
| 688 |
luxel[ 1 ] += 1.0f; |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
|
| 693 |
if ( pointCluster < 0 && si != NULL && dark == qfalse ) { |
| 694 |
VectorMA( dv->xyz, lightmapSampleOffset, dv->normal, nudged ); |
| 695 |
pointCluster = ClusterForPointExtFilter( nudged, LUXEL_EPSILON, numClusters, clusters ); |
| 696 |
if ( pointCluster >= 0 ) { |
| 697 |
VectorCopy( nudged, origin ); |
| 698 |
} |
| 699 |
luxel[ 1 ] += 1.0f; |
| 700 |
} |
| 701 |
|
| 702 |
|
| 703 |
if ( pointCluster < 0 ) { |
| 704 |
( *cluster ) = CLUSTER_OCCLUDED; |
| 705 |
VectorClear( origin ); |
| 706 |
VectorClear( normal ); |
| 707 |
numLuxelsOccluded++; |
| 708 |
return ( *cluster ); |
| 709 |
} |
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
if ( stv ) { |
| 716 |
PerturbNormal( dv, si, pNormal, stv, ttv ); |
| 717 |
} |
| 718 |
else{ |
| 719 |
VectorCopy( dv->normal, pNormal ); |
| 720 |
} |
| 721 |
|
| 722 |
|
| 723 |
( *cluster ) = pointCluster; |
| 724 |
VectorCopy( pNormal, normal ); |
| 725 |
|
| 726 |
|
| 727 |
luxel[ 0 ] = pass; |
| 728 |
luxel[ 3 ] = 1.0f; |
| 729 |
|
| 730 |
|
| 731 |
numLuxelsMapped++; |
| 732 |
|
| 733 |
|
| 734 |
return ( *cluster ); |
| 735 |
} |
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
static void MapTriangle_r( rawLightmap_t *lm, surfaceInfo_t *info, bspDrawVert_t *dv[ 3 ], vec4_t plane, vec3_t stv[ 3 ], vec3_t ttv[ 3 ], vec3_t worldverts[ 3 ] ){ |
| 746 |
bspDrawVert_t mid, *dv2[ 3 ]; |
| 747 |
int max; |
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
#if 0 |
| 752 |
MapSingleLuxel( lm, info, dv[ 0 ], plane, 1, stv, ttv ); |
| 753 |
MapSingleLuxel( lm, info, dv[ 1 ], plane, 1, stv, ttv ); |
| 754 |
MapSingleLuxel( lm, info, dv[ 2 ], plane, 1, stv, ttv ); |
| 755 |
#endif |
| 756 |
|
| 757 |
|
| 758 |
{ |
| 759 |
int i; |
| 760 |
float *a, *b, dx, dy, dist, maxDist; |
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
max = -1; |
| 765 |
maxDist = 0; |
| 766 |
for ( i = 0; i < 3; i++ ) |
| 767 |
{ |
| 768 |
|
| 769 |
a = dv[ i ]->lightmap[ 0 ]; |
| 770 |
b = dv[ ( i + 1 ) % 3 ]->lightmap[ 0 ]; |
| 771 |
|
| 772 |
|
| 773 |
dx = a[ 0 ] - b[ 0 ]; |
| 774 |
dy = a[ 1 ] - b[ 1 ]; |
| 775 |
dist = ( dx * dx ) + ( dy * dy ); |
| 776 |
|
| 777 |
|
| 778 |
if ( dist > maxDist ) { |
| 779 |
maxDist = dist; |
| 780 |
max = i; |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
|
| 785 |
if ( max < 0 || maxDist <= subdivideThreshold ) { |
| 786 |
return; |
| 787 |
} |
| 788 |
} |
| 789 |
|
| 790 |
|
| 791 |
LerpDrawVert( dv[ max ], dv[ ( max + 1 ) % 3 ], &mid ); |
| 792 |
MapSingleLuxel( lm, info, &mid, plane, 1, stv, ttv, worldverts ); |
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
VectorCopy( dv, dv2 ); |
| 799 |
dv2[ max ] = ∣ |
| 800 |
MapTriangle_r( lm, info, dv2, plane, stv, ttv, worldverts ); |
| 801 |
|
| 802 |
|
| 803 |
VectorCopy( dv, dv2 ); |
| 804 |
dv2[ ( max + 1 ) % 3 ] = ∣ |
| 805 |
MapTriangle_r( lm, info, dv2, plane, stv, ttv, worldverts ); |
| 806 |
} |
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
static qboolean MapTriangle( rawLightmap_t *lm, surfaceInfo_t *info, bspDrawVert_t *dv[ 3 ], qboolean mapNonAxial ){ |
| 817 |
int i; |
| 818 |
vec4_t plane; |
| 819 |
vec3_t *stv, *ttv, stvStatic[ 3 ], ttvStatic[ 3 ]; |
| 820 |
vec3_t worldverts[ 3 ]; |
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
if ( lm->plane != NULL ) { |
| 825 |
VectorCopy( lm->plane, plane ); |
| 826 |
plane[ 3 ] = lm->plane[ 3 ]; |
| 827 |
} |
| 828 |
|
| 829 |
|
| 830 |
else if ( PlaneFromPoints( plane, dv[ 0 ]->xyz, dv[ 1 ]->xyz, dv[ 2 ]->xyz ) == qfalse ) { |
| 831 |
return qfalse; |
| 832 |
} |
| 833 |
|
| 834 |
|
| 835 |
if ( info->si->normalImage != NULL && CalcTangentVectors( 3, dv, stvStatic, ttvStatic ) ) { |
| 836 |
stv = stvStatic; |
| 837 |
ttv = ttvStatic; |
| 838 |
} |
| 839 |
else |
| 840 |
{ |
| 841 |
stv = NULL; |
| 842 |
ttv = NULL; |
| 843 |
} |
| 844 |
|
| 845 |
VectorCopy( dv[ 0 ]->xyz, worldverts[ 0 ] ); |
| 846 |
VectorCopy( dv[ 1 ]->xyz, worldverts[ 1 ] ); |
| 847 |
VectorCopy( dv[ 2 ]->xyz, worldverts[ 2 ] ); |
| 848 |
|
| 849 |
|
| 850 |
MapSingleLuxel( lm, info, dv[ 0 ], plane, 1, stv, ttv, worldverts ); |
| 851 |
MapSingleLuxel( lm, info, dv[ 1 ], plane, 1, stv, ttv, worldverts ); |
| 852 |
MapSingleLuxel( lm, info, dv[ 2 ], plane, 1, stv, ttv, worldverts ); |
| 853 |
|
| 854 |
|
| 855 |
if ( mapNonAxial ) { |
| 856 |
|
| 857 |
MapTriangle_r( lm, info, dv, plane, stv, ttv, worldverts ); |
| 858 |
return qtrue; |
| 859 |
} |
| 860 |
|
| 861 |
for ( i = 0; i < 3; i++ ) |
| 862 |
{ |
| 863 |
float *a, *b; |
| 864 |
bspDrawVert_t *dv2[ 3 ]; |
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
a = dv[ i ]->lightmap[ 0 ]; |
| 869 |
b = dv[ ( i + 1 ) % 3 ]->lightmap[ 0 ]; |
| 870 |
|
| 871 |
|
| 872 |
if ( fabs( a[ 0 ] - b[ 0 ] ) < 0.01f || fabs( a[ 1 ] - b[ 1 ] ) < 0.01f ) { |
| 873 |
dv2[ 0 ] = dv[ i ]; |
| 874 |
dv2[ 1 ] = dv[ ( i + 1 ) % 3 ]; |
| 875 |
dv2[ 2 ] = dv[ ( i + 1 ) % 3 ]; |
| 876 |
|
| 877 |
|
| 878 |
MapTriangle_r( lm, info, dv2, plane, stv, ttv, worldverts ); |
| 879 |
} |
| 880 |
} |
| 881 |
|
| 882 |
return qtrue; |
| 883 |
} |
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
static void MapQuad_r( rawLightmap_t *lm, surfaceInfo_t *info, bspDrawVert_t *dv[ 4 ], vec4_t plane, vec3_t stv[ 4 ], vec3_t ttv[ 4 ] ){ |
| 894 |
bspDrawVert_t mid[ 2 ], *dv2[ 4 ]; |
| 895 |
int max; |
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
{ |
| 900 |
int i; |
| 901 |
float *a, *b, dx, dy, dist, maxDist; |
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
max = -1; |
| 906 |
maxDist = 0; |
| 907 |
for ( i = 0; i < 4; i++ ) |
| 908 |
{ |
| 909 |
|
| 910 |
a = dv[ i ]->lightmap[ 0 ]; |
| 911 |
b = dv[ ( i + 1 ) % 4 ]->lightmap[ 0 ]; |
| 912 |
|
| 913 |
|
| 914 |
dx = a[ 0 ] - b[ 0 ]; |
| 915 |
dy = a[ 1 ] - b[ 1 ]; |
| 916 |
dist = ( dx * dx ) + ( dy * dy ); |
| 917 |
|
| 918 |
|
| 919 |
if ( dist > maxDist ) { |
| 920 |
maxDist = dist; |
| 921 |
max = i; |
| 922 |
} |
| 923 |
} |
| 924 |
|
| 925 |
|
| 926 |
if ( max < 0 || maxDist <= subdivideThreshold ) { |
| 927 |
return; |
| 928 |
} |
| 929 |
} |
| 930 |
|
| 931 |
|
| 932 |
max &= 1; |
| 933 |
|
| 934 |
|
| 935 |
LerpDrawVert( dv[ max ], dv[ ( max + 1 ) % 4 ], &mid[ 0 ] ); |
| 936 |
LerpDrawVert( dv[ max + 2 ], dv[ ( max + 3 ) % 4 ], &mid[ 1 ] ); |
| 937 |
|
| 938 |
|
| 939 |
MapSingleLuxel( lm, info, &mid[ 0 ], plane, 1, stv, ttv, NULL ); |
| 940 |
MapSingleLuxel( lm, info, &mid[ 1 ], plane, 1, stv, ttv, NULL ); |
| 941 |
|
| 942 |
|
| 943 |
if ( max == 0 ) { |
| 944 |
|
| 945 |
dv2[ 0 ] = dv[ 0 ]; |
| 946 |
dv2[ 1 ] = &mid[ 0 ]; |
| 947 |
dv2[ 2 ] = &mid[ 1 ]; |
| 948 |
dv2[ 3 ] = dv[ 3 ]; |
| 949 |
MapQuad_r( lm, info, dv2, plane, stv, ttv ); |
| 950 |
|
| 951 |
|
| 952 |
dv2[ 0 ] = &mid[ 0 ]; |
| 953 |
dv2[ 1 ] = dv[ 1 ]; |
| 954 |
dv2[ 2 ] = dv[ 2 ]; |
| 955 |
dv2[ 3 ] = &mid[ 1 ]; |
| 956 |
MapQuad_r( lm, info, dv2, plane, stv, ttv ); |
| 957 |
} |
| 958 |
|
| 959 |
|
| 960 |
else |
| 961 |
{ |
| 962 |
|
| 963 |
dv2[ 0 ] = dv[ 0 ]; |
| 964 |
dv2[ 1 ] = dv[ 1 ]; |
| 965 |
dv2[ 2 ] = &mid[ 0 ]; |
| 966 |
dv2[ 3 ] = &mid[ 1 ]; |
| 967 |
MapQuad_r( lm, info, dv2, plane, stv, ttv ); |
| 968 |
|
| 969 |
|
| 970 |
dv2[ 0 ] = &mid[ 1 ]; |
| 971 |
dv2[ 1 ] = &mid[ 0 ]; |
| 972 |
dv2[ 2 ] = dv[ 2 ]; |
| 973 |
dv2[ 3 ] = dv[ 3 ]; |
| 974 |
MapQuad_r( lm, info, dv2, plane, stv, ttv ); |
| 975 |
} |
| 976 |
} |
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
#define QUAD_PLANAR_EPSILON 0.5f |
| 987 |
|
| 988 |
static qboolean MapQuad( rawLightmap_t *lm, surfaceInfo_t *info, bspDrawVert_t *dv[ 4 ] ){ |
| 989 |
float dist; |
| 990 |
vec4_t plane; |
| 991 |
vec3_t *stv, *ttv, stvStatic[ 4 ], ttvStatic[ 4 ]; |
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
if ( lm->plane != NULL ) { |
| 996 |
VectorCopy( lm->plane, plane ); |
| 997 |
plane[ 3 ] = lm->plane[ 3 ]; |
| 998 |
} |
| 999 |
|
| 1000 |
|
| 1001 |
else if ( PlaneFromPoints( plane, dv[ 0 ]->xyz, dv[ 1 ]->xyz, dv[ 2 ]->xyz ) == qfalse ) { |
| 1002 |
return qfalse; |
| 1003 |
} |
| 1004 |
|
| 1005 |
|
| 1006 |
dist = DotProduct( plane, dv[ 3 ]->xyz ) - plane[ 3 ]; |
| 1007 |
if ( fabs( dist ) > QUAD_PLANAR_EPSILON ) { |
| 1008 |
return qfalse; |
| 1009 |
} |
| 1010 |
|
| 1011 |
|
| 1012 |
if ( info->si->normalImage != NULL && CalcTangentVectors( 4, dv, stvStatic, ttvStatic ) ) { |
| 1013 |
stv = stvStatic; |
| 1014 |
ttv = ttvStatic; |
| 1015 |
} |
| 1016 |
else |
| 1017 |
{ |
| 1018 |
stv = NULL; |
| 1019 |
ttv = NULL; |
| 1020 |
} |
| 1021 |
|
| 1022 |
|
| 1023 |
MapSingleLuxel( lm, info, dv[ 0 ], plane, 1, stv, ttv, NULL ); |
| 1024 |
MapSingleLuxel( lm, info, dv[ 1 ], plane, 1, stv, ttv, NULL ); |
| 1025 |
MapSingleLuxel( lm, info, dv[ 2 ], plane, 1, stv, ttv, NULL ); |
| 1026 |
MapSingleLuxel( lm, info, dv[ 3 ], plane, 1, stv, ttv, NULL ); |
| 1027 |
|
| 1028 |
|
| 1029 |
MapQuad_r( lm, info, dv, plane, stv, ttv ); |
| 1030 |
return qtrue; |
| 1031 |
} |
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
#define VectorDivide( in, d, out ) VectorScale( in, ( 1.0f / ( d ) ), out ) //% (out)[ 0 ] = (in)[ 0 ] / (d), (out)[ 1 ] = (in)[ 1 ] / (d), (out)[ 2 ] = (in)[ 2 ] / (d) |
| 1041 |
|
| 1042 |
void MapRawLightmap( int rawLightmapNum ){ |
| 1043 |
int n, num, i, x, y, sx, sy, pw[ 5 ], r, *cluster, mapNonAxial; |
| 1044 |
float *luxel, *origin, *normal, samples, radius, pass; |
| 1045 |
rawLightmap_t *lm; |
| 1046 |
bspDrawSurface_t *ds; |
| 1047 |
surfaceInfo_t *info; |
| 1048 |
mesh_t src, *subdivided, *mesh; |
| 1049 |
bspDrawVert_t *verts, *dv[ 4 ], fake; |
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
if ( rawLightmapNum >= numRawLightmaps ) { |
| 1054 |
return; |
| 1055 |
} |
| 1056 |
|
| 1057 |
|
| 1058 |
lm = &rawLightmaps[ rawLightmapNum ]; |
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
for ( n = 0; n < lm->numLightSurfaces; n++ ) |
| 1066 |
{ |
| 1067 |
|
| 1068 |
if ( n > 0 ) { |
| 1069 |
for ( y = 0; y < lm->sh; y++ ) |
| 1070 |
{ |
| 1071 |
for ( x = 0; x < lm->sw; x++ ) |
| 1072 |
{ |
| 1073 |
|
| 1074 |
cluster = SUPER_CLUSTER( x, y ); |
| 1075 |
if ( *cluster < 0 ) { |
| 1076 |
*cluster = CLUSTER_UNMAPPED; |
| 1077 |
} |
| 1078 |
} |
| 1079 |
} |
| 1080 |
} |
| 1081 |
|
| 1082 |
|
| 1083 |
num = lightSurfaces[ lm->firstLightSurface + n ]; |
| 1084 |
ds = &bspDrawSurfaces[ num ]; |
| 1085 |
info = &surfaceInfos[ num ]; |
| 1086 |
|
| 1087 |
|
| 1088 |
if ( info->lm != lm ) { |
| 1089 |
Sys_Printf( "!" ); |
| 1090 |
continue; |
| 1091 |
} |
| 1092 |
|
| 1093 |
|
| 1094 |
switch ( ds->surfaceType ) |
| 1095 |
{ |
| 1096 |
case MST_PLANAR: |
| 1097 |
|
| 1098 |
verts = yDrawVerts + ds->firstVert; |
| 1099 |
|
| 1100 |
|
| 1101 |
for ( mapNonAxial = 0; mapNonAxial < 2; mapNonAxial++ ) |
| 1102 |
{ |
| 1103 |
for ( i = 0; i < ds->numIndexes; i += 3 ) |
| 1104 |
{ |
| 1105 |
dv[ 0 ] = &verts[ bspDrawIndexes[ ds->firstIndex + i ] ]; |
| 1106 |
dv[ 1 ] = &verts[ bspDrawIndexes[ ds->firstIndex + i + 1 ] ]; |
| 1107 |
dv[ 2 ] = &verts[ bspDrawIndexes[ ds->firstIndex + i + 2 ] ]; |
| 1108 |
MapTriangle( lm, info, dv, mapNonAxial ); |
| 1109 |
} |
| 1110 |
} |
| 1111 |
break; |
| 1112 |
|
| 1113 |
case MST_PATCH: |
| 1114 |
|
| 1115 |
src.width = ds->patchWidth; |
| 1116 |
src.height = ds->patchHeight; |
| 1117 |
src.verts = &yDrawVerts[ ds->firstVert ]; |
| 1118 |
|
| 1119 |
subdivided = SubdivideMesh2( src, info->patchIterations ); |
| 1120 |
|
| 1121 |
|
| 1122 |
PutMeshOnCurve( *subdivided ); |
| 1123 |
mesh = RemoveLinearMeshColumnsRows( subdivided ); |
| 1124 |
FreeMesh( subdivided ); |
| 1125 |
|
| 1126 |
|
| 1127 |
verts = mesh->verts; |
| 1128 |
|
| 1129 |
|
| 1130 |
#if 0 |
| 1131 |
if ( lm->plane ) { |
| 1132 |
Sys_Printf( "Planar patch: [%1.3f %1.3f %1.3f] [%1.3f %1.3f %1.3f] [%1.3f %1.3f %1.3f]\n", |
| 1133 |
lm->plane[ 0 ], lm->plane[ 1 ], lm->plane[ 2 ], |
| 1134 |
lm->vecs[ 0 ][ 0 ], lm->vecs[ 0 ][ 1 ], lm->vecs[ 0 ][ 2 ], |
| 1135 |
lm->vecs[ 1 ][ 0 ], lm->vecs[ 1 ][ 1 ], lm->vecs[ 1 ][ 2 ] ); |
| 1136 |
} |
| 1137 |
#endif |
| 1138 |
|
| 1139 |
|
| 1140 |
#if 0 |
| 1141 |
|
| 1142 |
for ( mapNonAxial = 0; mapNonAxial < 2; mapNonAxial++ ) |
| 1143 |
{ |
| 1144 |
for ( y = 0; y < ( mesh->height - 1 ); y++ ) |
| 1145 |
{ |
| 1146 |
for ( x = 0; x < ( mesh->width - 1 ); x++ ) |
| 1147 |
{ |
| 1148 |
|
| 1149 |
pw[ 0 ] = x + ( y * mesh->width ); |
| 1150 |
pw[ 1 ] = x + ( ( y + 1 ) * mesh->width ); |
| 1151 |
pw[ 2 ] = x + 1 + ( ( y + 1 ) * mesh->width ); |
| 1152 |
pw[ 3 ] = x + 1 + ( y * mesh->width ); |
| 1153 |
pw[ 4 ] = x + ( y * mesh->width ); |
| 1154 |
|
| 1155 |
|
| 1156 |
r = ( x + y ) & 1; |
| 1157 |
|
| 1158 |
|
| 1159 |
dv[ 0 ] = &verts[ pw[ r + 0 ] ]; |
| 1160 |
dv[ 1 ] = &verts[ pw[ r + 1 ] ]; |
| 1161 |
dv[ 2 ] = &verts[ pw[ r + 2 ] ]; |
| 1162 |
MapTriangle( lm, info, dv, mapNonAxial ); |
| 1163 |
|
| 1164 |
|
| 1165 |
dv[ 0 ] = &verts[ pw[ r + 0 ] ]; |
| 1166 |
dv[ 1 ] = &verts[ pw[ r + 2 ] ]; |
| 1167 |
dv[ 2 ] = &verts[ pw[ r + 3 ] ]; |
| 1168 |
MapTriangle( lm, info, dv, mapNonAxial ); |
| 1169 |
} |
| 1170 |
} |
| 1171 |
} |
| 1172 |
|
| 1173 |
#else |
| 1174 |
|
| 1175 |
for ( y = 0; y < ( mesh->height - 1 ); y++ ) |
| 1176 |
{ |
| 1177 |
for ( x = 0; x < ( mesh->width - 1 ); x++ ) |
| 1178 |
{ |
| 1179 |
|
| 1180 |
pw[ 0 ] = x + ( y * mesh->width ); |
| 1181 |
pw[ 1 ] = x + ( ( y + 1 ) * mesh->width ); |
| 1182 |
pw[ 2 ] = x + 1 + ( ( y + 1 ) * mesh->width ); |
| 1183 |
pw[ 3 ] = x + 1 + ( y * mesh->width ); |
| 1184 |
pw[ 4 ] = pw[ 0 ]; |
| 1185 |
|
| 1186 |
|
| 1187 |
r = ( x + y ) & 1; |
| 1188 |
|
| 1189 |
|
| 1190 |
dv[ 0 ] = &verts[ pw[ r + 0 ] ]; |
| 1191 |
dv[ 1 ] = &verts[ pw[ r + 1 ] ]; |
| 1192 |
dv[ 2 ] = &verts[ pw[ r + 2 ] ]; |
| 1193 |
dv[ 3 ] = &verts[ pw[ r + 3 ] ]; |
| 1194 |
if ( MapQuad( lm, info, dv ) ) { |
| 1195 |
continue; |
| 1196 |
} |
| 1197 |
|
| 1198 |
for ( mapNonAxial = 0; mapNonAxial < 2; mapNonAxial++ ) |
| 1199 |
{ |
| 1200 |
|
| 1201 |
dv[ 1 ] = &verts[ pw[ r + 1 ] ]; |
| 1202 |
dv[ 2 ] = &verts[ pw[ r + 2 ] ]; |
| 1203 |
MapTriangle( lm, info, dv, mapNonAxial ); |
| 1204 |
|
| 1205 |
|
| 1206 |
dv[ 1 ] = &verts[ pw[ r + 2 ] ]; |
| 1207 |
dv[ 2 ] = &verts[ pw[ r + 3 ] ]; |
| 1208 |
MapTriangle( lm, info, dv, mapNonAxial ); |
| 1209 |
} |
| 1210 |
} |
| 1211 |
} |
| 1212 |
|
| 1213 |
#endif |
| 1214 |
|
| 1215 |
|
| 1216 |
FreeMesh( mesh ); |
| 1217 |
break; |
| 1218 |
|
| 1219 |
default: |
| 1220 |
break; |
| 1221 |
} |
| 1222 |
} |
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
for ( y = 0; y < lm->sh; y++ ) |
| 1230 |
{ |
| 1231 |
for ( x = 0; x < lm->sw; x++ ) |
| 1232 |
{ |
| 1233 |
|
| 1234 |
luxel = SUPER_LUXEL( 0, x, y ); |
| 1235 |
normal = SUPER_NORMAL( x, y ); |
| 1236 |
cluster = SUPER_CLUSTER( x, y ); |
| 1237 |
|
| 1238 |
|
| 1239 |
if ( *cluster < 0 ) { |
| 1240 |
continue; |
| 1241 |
} |
| 1242 |
|
| 1243 |
|
| 1244 |
if ( luxel[ 3 ] > 1.0f ) { |
| 1245 |
VectorNormalize( normal, normal ); |
| 1246 |
} |
| 1247 |
|
| 1248 |
|
| 1249 |
luxel[ 3 ] = 1.0f; |
| 1250 |
} |
| 1251 |
} |
| 1252 |
|
| 1253 |
|
| 1254 |
if ( lm->plane == NULL ) { |
| 1255 |
return; |
| 1256 |
} |
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
radius = floor( superSample / 2 ); |
| 1265 |
radius = radius > 0 ? radius : 1.0f; |
| 1266 |
radius += 1.0f; |
| 1267 |
for ( pass = 2.0f; pass <= radius; pass += 1.0f ) |
| 1268 |
{ |
| 1269 |
for ( y = 0; y < lm->sh; y++ ) |
| 1270 |
{ |
| 1271 |
for ( x = 0; x < lm->sw; x++ ) |
| 1272 |
{ |
| 1273 |
|
| 1274 |
luxel = SUPER_LUXEL( 0, x, y ); |
| 1275 |
normal = SUPER_NORMAL( x, y ); |
| 1276 |
cluster = SUPER_CLUSTER( x, y ); |
| 1277 |
|
| 1278 |
|
| 1279 |
if ( *cluster != CLUSTER_UNMAPPED ) { |
| 1280 |
continue; |
| 1281 |
} |
| 1282 |
|
| 1283 |
|
| 1284 |
VectorClear( fake.xyz ); |
| 1285 |
VectorClear( fake.normal ); |
| 1286 |
fake.lightmap[ 0 ][ 0 ] = x; |
| 1287 |
fake.lightmap[ 0 ][ 1 ] = y; |
| 1288 |
samples = 0.0f; |
| 1289 |
for ( sy = ( y - 1 ); sy <= ( y + 1 ); sy++ ) |
| 1290 |
{ |
| 1291 |
if ( sy < 0 || sy >= lm->sh ) { |
| 1292 |
continue; |
| 1293 |
} |
| 1294 |
|
| 1295 |
for ( sx = ( x - 1 ); sx <= ( x + 1 ); sx++ ) |
| 1296 |
{ |
| 1297 |
if ( sx < 0 || sx >= lm->sw || ( sx == x && sy == y ) ) { |
| 1298 |
continue; |
| 1299 |
} |
| 1300 |
|
| 1301 |
|
| 1302 |
luxel = SUPER_LUXEL( 0, sx, sy ); |
| 1303 |
origin = SUPER_ORIGIN( sx, sy ); |
| 1304 |
normal = SUPER_NORMAL( sx, sy ); |
| 1305 |
cluster = SUPER_CLUSTER( sx, sy ); |
| 1306 |
|
| 1307 |
|
| 1308 |
if ( *cluster < 0 || luxel[ 0 ] >= pass ) { |
| 1309 |
continue; |
| 1310 |
} |
| 1311 |
|
| 1312 |
|
| 1313 |
VectorAdd( fake.xyz, origin, fake.xyz ); |
| 1314 |
VectorAdd( fake.normal, normal, fake.normal ); |
| 1315 |
samples += luxel[ 3 ]; |
| 1316 |
} |
| 1317 |
} |
| 1318 |
|
| 1319 |
|
| 1320 |
if ( samples == 0.0f ) { |
| 1321 |
continue; |
| 1322 |
} |
| 1323 |
|
| 1324 |
|
| 1325 |
VectorDivide( fake.xyz, samples, fake.xyz ); |
| 1326 |
|
| 1327 |
if ( VectorNormalize( fake.normal, fake.normal ) == 0.0f ) { |
| 1328 |
continue; |
| 1329 |
} |
| 1330 |
|
| 1331 |
|
| 1332 |
MapSingleLuxel( lm, NULL, &fake, lm->plane, pass, NULL, NULL, NULL ); |
| 1333 |
} |
| 1334 |
} |
| 1335 |
} |
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
for ( y = 0; y < lm->sh; y++ ) |
| 1343 |
{ |
| 1344 |
for ( x = 0; x < lm->sw; x++ ) |
| 1345 |
{ |
| 1346 |
|
| 1347 |
luxel = SUPER_LUXEL( 0, x, y ); |
| 1348 |
normal = SUPER_NORMAL( x, y ); |
| 1349 |
cluster = SUPER_CLUSTER( x, y ); |
| 1350 |
|
| 1351 |
|
| 1352 |
if ( *cluster < 0 ) { |
| 1353 |
continue; |
| 1354 |
} |
| 1355 |
|
| 1356 |
|
| 1357 |
if ( luxel[ 3 ] > 1.0f ) { |
| 1358 |
VectorNormalize( normal, normal ); |
| 1359 |
} |
| 1360 |
|
| 1361 |
|
| 1362 |
luxel[ 3 ] = 1.0f; |
| 1363 |
} |
| 1364 |
} |
| 1365 |
|
| 1366 |
|
| 1367 |
#if 0 |
| 1368 |
Sys_Printf( "\n" ); |
| 1369 |
for ( y = 0; y < lm->sh; y++ ) |
| 1370 |
{ |
| 1371 |
for ( x = 0; x < lm->sw; x++ ) |
| 1372 |
{ |
| 1373 |
vec3_t mins, maxs; |
| 1374 |
|
| 1375 |
|
| 1376 |
cluster = SUPER_CLUSTER( x, y ); |
| 1377 |
origin = SUPER_ORIGIN( x, y ); |
| 1378 |
normal = SUPER_NORMAL( x, y ); |
| 1379 |
luxel = SUPER_LUXEL( x, y ); |
| 1380 |
|
| 1381 |
if ( *cluster < 0 ) { |
| 1382 |
continue; |
| 1383 |
} |
| 1384 |
|
| 1385 |
|
| 1386 |
ClearBounds( mins, maxs ); |
| 1387 |
for ( n = 0; n < lm->numLightSurfaces; n++ ) |
| 1388 |
{ |
| 1389 |
int TOL; |
| 1390 |
info = &surfaceInfos[ lightSurfaces[ lm->firstLightSurface + n ] ]; |
| 1391 |
TOL = info->sampleSize + 2; |
| 1392 |
AddPointToBounds( info->mins, mins, maxs ); |
| 1393 |
AddPointToBounds( info->maxs, mins, maxs ); |
| 1394 |
if ( origin[ 0 ] > ( info->mins[ 0 ] - TOL ) && origin[ 0 ] < ( info->maxs[ 0 ] + TOL ) && |
| 1395 |
origin[ 1 ] > ( info->mins[ 1 ] - TOL ) && origin[ 1 ] < ( info->maxs[ 1 ] + TOL ) && |
| 1396 |
origin[ 2 ] > ( info->mins[ 2 ] - TOL ) && origin[ 2 ] < ( info->maxs[ 2 ] + TOL ) ) { |
| 1397 |
break; |
| 1398 |
} |
| 1399 |
} |
| 1400 |
|
| 1401 |
|
| 1402 |
if ( n < lm->numLightSurfaces ) { |
| 1403 |
continue; |
| 1404 |
} |
| 1405 |
|
| 1406 |
|
| 1407 |
Sys_Printf( "%6d [%2d,%2d] (%4d): XYZ(%+4.1f %+4.1f %+4.1f) LO(%+4.1f %+4.1f %+4.1f) HI(%+4.1f %+4.1f %+4.1f) <%3.0f>\n", |
| 1408 |
rawLightmapNum, x, y, *cluster, |
| 1409 |
origin[ 0 ], origin[ 1 ], origin[ 2 ], |
| 1410 |
mins[ 0 ], mins[ 1 ], mins[ 2 ], |
| 1411 |
maxs[ 0 ], maxs[ 1 ], maxs[ 2 ], |
| 1412 |
luxel[ 3 ] ); |
| 1413 |
} |
| 1414 |
} |
| 1415 |
#endif |
| 1416 |
} |
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
#define DIRT_CONE_ANGLE 88 |
| 1426 |
#define DIRT_NUM_ANGLE_STEPS 16 |
| 1427 |
#define DIRT_NUM_ELEVATION_STEPS 3 |
| 1428 |
#define DIRT_NUM_VECTORS ( DIRT_NUM_ANGLE_STEPS * DIRT_NUM_ELEVATION_STEPS ) |
| 1429 |
|
| 1430 |
static vec3_t dirtVectors[ DIRT_NUM_VECTORS ]; |
| 1431 |
static int numDirtVectors = 0; |
| 1432 |
|
| 1433 |
void SetupDirt( void ){ |
| 1434 |
int i, j; |
| 1435 |
float angle, elevation, angleStep, elevationStep; |
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
Sys_FPrintf( SYS_VRB, "--- SetupDirt ---\n" ); |
| 1440 |
|
| 1441 |
|
| 1442 |
angleStep = DEG2RAD( 360.0f / DIRT_NUM_ANGLE_STEPS ); |
| 1443 |
elevationStep = DEG2RAD( DIRT_CONE_ANGLE / DIRT_NUM_ELEVATION_STEPS ); |
| 1444 |
|
| 1445 |
|
| 1446 |
angle = 0.0f; |
| 1447 |
for ( i = 0, angle = 0.0f; i < DIRT_NUM_ANGLE_STEPS; i++, angle += angleStep ) |
| 1448 |
{ |
| 1449 |
|
| 1450 |
for ( j = 0, elevation = elevationStep * 0.5f; j < DIRT_NUM_ELEVATION_STEPS; j++, elevation += elevationStep ) |
| 1451 |
{ |
| 1452 |
dirtVectors[ numDirtVectors ][ 0 ] = sin( elevation ) * cos( angle ); |
| 1453 |
dirtVectors[ numDirtVectors ][ 1 ] = sin( elevation ) * sin( angle ); |
| 1454 |
dirtVectors[ numDirtVectors ][ 2 ] = cos( elevation ); |
| 1455 |
numDirtVectors++; |
| 1456 |
} |
| 1457 |
} |
| 1458 |
|
| 1459 |
|
| 1460 |
Sys_FPrintf( SYS_VRB, "%9d dirtmap vectors\n", numDirtVectors ); |
| 1461 |
} |
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
|
| 1466 |
|
| 1467 |
|
| 1468 |
|
| 1469 |
float DirtForSample( trace_t *trace ){ |
| 1470 |
int i; |
| 1471 |
float gatherDirt, outDirt, angle, elevation, ooDepth; |
| 1472 |
vec3_t normal, worldUp, myUp, myRt, temp, direction, displacement; |
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
if ( !dirty ) { |
| 1477 |
return 1.0f; |
| 1478 |
} |
| 1479 |
if ( trace == NULL || trace->cluster < 0 ) { |
| 1480 |
return 0.0f; |
| 1481 |
} |
| 1482 |
|
| 1483 |
|
| 1484 |
gatherDirt = 0.0f; |
| 1485 |
ooDepth = 1.0f / dirtDepth; |
| 1486 |
VectorCopy( trace->normal, normal ); |
| 1487 |
|
| 1488 |
|
| 1489 |
if ( normal[ 0 ] == 0.0f && normal[ 1 ] == 0.0f && ( normal[ 2 ] == 1.0f || normal[ 2 ] == -1.0f ) ) { |
| 1490 |
if ( normal[ 2 ] == 1.0f ) { |
| 1491 |
VectorSet( myRt, 1.0f, 0.0f, 0.0f ); |
| 1492 |
VectorSet( myUp, 0.0f, 1.0f, 0.0f ); |
| 1493 |
} |
| 1494 |
else if ( normal[ 2 ] == -1.0f ) { |
| 1495 |
VectorSet( myRt, -1.0f, 0.0f, 0.0f ); |
| 1496 |
VectorSet( myUp, 0.0f, 1.0f, 0.0f ); |
| 1497 |
} |
| 1498 |
} |
| 1499 |
else |
| 1500 |
{ |
| 1501 |
VectorSet( worldUp, 0.0f, 0.0f, 1.0f ); |
| 1502 |
CrossProduct( normal, worldUp, myRt ); |
| 1503 |
VectorNormalize( myRt, myRt ); |
| 1504 |
CrossProduct( myRt, normal, myUp ); |
| 1505 |
VectorNormalize( myUp, myUp ); |
| 1506 |
} |
| 1507 |
|
| 1508 |
|
| 1509 |
if ( dirtMode == 1 ) { |
| 1510 |
|
| 1511 |
for ( i = 0; i < numDirtVectors; i++ ) |
| 1512 |
{ |
| 1513 |
|
| 1514 |
angle = Random() * DEG2RAD( 360.0f ); |
| 1515 |
elevation = Random() * DEG2RAD( DIRT_CONE_ANGLE ); |
| 1516 |
temp[ 0 ] = cos( angle ) * sin( elevation ); |
| 1517 |
temp[ 1 ] = sin( angle ) * sin( elevation ); |
| 1518 |
temp[ 2 ] = cos( elevation ); |
| 1519 |
|
| 1520 |
|
| 1521 |
direction[ 0 ] = myRt[ 0 ] * temp[ 0 ] + myUp[ 0 ] * temp[ 1 ] + normal[ 0 ] * temp[ 2 ]; |
| 1522 |
direction[ 1 ] = myRt[ 1 ] * temp[ 0 ] + myUp[ 1 ] * temp[ 1 ] + normal[ 1 ] * temp[ 2 ]; |
| 1523 |
direction[ 2 ] = myRt[ 2 ] * temp[ 0 ] + myUp[ 2 ] * temp[ 1 ] + normal[ 2 ] * temp[ 2 ]; |
| 1524 |
|
| 1525 |
|
| 1526 |
VectorMA( trace->origin, dirtDepth, direction, trace->end ); |
| 1527 |
SetupTrace( trace ); |
| 1528 |
VectorSet(trace->color, 1.0f, 1.0f, 1.0f); |
| 1529 |
|
| 1530 |
|
| 1531 |
TraceLine( trace ); |
| 1532 |
if ( trace->opaque && !( trace->compileFlags & C_SKY ) ) { |
| 1533 |
VectorSubtract( trace->hit, trace->origin, displacement ); |
| 1534 |
gatherDirt += 1.0f - ooDepth * VectorLength( displacement ); |
| 1535 |
} |
| 1536 |
} |
| 1537 |
} |
| 1538 |
else |
| 1539 |
{ |
| 1540 |
|
| 1541 |
for ( i = 0; i < numDirtVectors; i++ ) |
| 1542 |
{ |
| 1543 |
|
| 1544 |
direction[ 0 ] = myRt[ 0 ] * dirtVectors[ i ][ 0 ] + myUp[ 0 ] * dirtVectors[ i ][ 1 ] + normal[ 0 ] * dirtVectors[ i ][ 2 ]; |
| 1545 |
direction[ 1 ] = myRt[ 1 ] * dirtVectors[ i ][ 0 ] + myUp[ 1 ] * dirtVectors[ i ][ 1 ] + normal[ 1 ] * dirtVectors[ i ][ 2 ]; |
| 1546 |
direction[ 2 ] = myRt[ 2 ] * dirtVectors[ i ][ 0 ] + myUp[ 2 ] * dirtVectors[ i ][ 1 ] + normal[ 2 ] * dirtVectors[ i ][ 2 ]; |
| 1547 |
|
| 1548 |
|
| 1549 |
VectorMA( trace->origin, dirtDepth, direction, trace->end ); |
| 1550 |
SetupTrace( trace ); |
| 1551 |
VectorSet(trace->color, 1.0f, 1.0f, 1.0f); |
| 1552 |
|
| 1553 |
|
| 1554 |
TraceLine( trace ); |
| 1555 |
if ( trace->opaque ) { |
| 1556 |
VectorSubtract( trace->hit, trace->origin, displacement ); |
| 1557 |
gatherDirt += 1.0f - ooDepth * VectorLength( displacement ); |
| 1558 |
} |
| 1559 |
} |
| 1560 |
} |
| 1561 |
|
| 1562 |
|
| 1563 |
VectorMA( trace->origin, dirtDepth, normal, trace->end ); |
| 1564 |
SetupTrace( trace ); |
| 1565 |
VectorSet(trace->color, 1.0f, 1.0f, 1.0f); |
| 1566 |
|
| 1567 |
|
| 1568 |
TraceLine( trace ); |
| 1569 |
if ( trace->opaque ) { |
| 1570 |
VectorSubtract( trace->hit, trace->origin, displacement ); |
| 1571 |
gatherDirt += 1.0f - ooDepth * VectorLength( displacement ); |
| 1572 |
} |
| 1573 |
|
| 1574 |
|
| 1575 |
if ( gatherDirt <= 0.0f ) { |
| 1576 |
return 1.0f; |
| 1577 |
} |
| 1578 |
|
| 1579 |
|
| 1580 |
outDirt = pow( gatherDirt / ( numDirtVectors + 1 ), dirtGain ); |
| 1581 |
if ( outDirt > 1.0f ) { |
| 1582 |
outDirt = 1.0f; |
| 1583 |
} |
| 1584 |
|
| 1585 |
|
| 1586 |
outDirt *= dirtScale; |
| 1587 |
if ( outDirt > 1.0f ) { |
| 1588 |
outDirt = 1.0f; |
| 1589 |
} |
| 1590 |
|
| 1591 |
|
| 1592 |
return 1.0f - outDirt; |
| 1593 |
} |
| 1594 |
|
| 1595 |
|
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
|
| 1602 |
void DirtyRawLightmap( int rawLightmapNum ){ |
| 1603 |
int i, x, y, sx, sy, *cluster; |
| 1604 |
float *origin, *normal, *dirt, *dirt2, average, samples; |
| 1605 |
rawLightmap_t *lm; |
| 1606 |
surfaceInfo_t *info; |
| 1607 |
trace_t trace; |
| 1608 |
qboolean noDirty; |
| 1609 |
|
| 1610 |
|
| 1611 |
|
| 1612 |
if ( rawLightmapNum >= numRawLightmaps ) { |
| 1613 |
return; |
| 1614 |
} |
| 1615 |
|
| 1616 |
|
| 1617 |
lm = &rawLightmaps[ rawLightmapNum ]; |
| 1618 |
|
| 1619 |
|
| 1620 |
trace.testOcclusion = qtrue; |
| 1621 |
trace.forceSunlight = qfalse; |
| 1622 |
trace.recvShadows = lm->recvShadows; |
| 1623 |
trace.numSurfaces = lm->numLightSurfaces; |
| 1624 |
trace.surfaces = &lightSurfaces[ lm->firstLightSurface ]; |
| 1625 |
trace.inhibitRadius = 0.0f; |
| 1626 |
trace.testAll = qfalse; |
| 1627 |
|
| 1628 |
|
| 1629 |
trace.twoSided = qfalse; |
| 1630 |
for ( i = 0; i < trace.numSurfaces; i++ ) |
| 1631 |
{ |
| 1632 |
|
| 1633 |
info = &surfaceInfos[ trace.surfaces[ i ] ]; |
| 1634 |
|
| 1635 |
|
| 1636 |
if ( info->si->twoSided ) { |
| 1637 |
trace.twoSided = qtrue; |
| 1638 |
break; |
| 1639 |
} |
| 1640 |
} |
| 1641 |
|
| 1642 |
noDirty = qfalse; |
| 1643 |
for ( i = 0; i < trace.numSurfaces; i++ ) |
| 1644 |
{ |
| 1645 |
|
| 1646 |
info = &surfaceInfos[ trace.surfaces[ i ] ]; |
| 1647 |
|
| 1648 |
|
| 1649 |
if ( info->si->noDirty ) { |
| 1650 |
noDirty = qtrue; |
| 1651 |
break; |
| 1652 |
} |
| 1653 |
} |
| 1654 |
|
| 1655 |
|
| 1656 |
for ( y = 0; y < lm->sh; y++ ) |
| 1657 |
{ |
| 1658 |
for ( x = 0; x < lm->sw; x++ ) |
| 1659 |
{ |
| 1660 |
|
| 1661 |
cluster = SUPER_CLUSTER( x, y ); |
| 1662 |
origin = SUPER_ORIGIN( x, y ); |
| 1663 |
normal = SUPER_NORMAL( x, y ); |
| 1664 |
dirt = SUPER_DIRT( x, y ); |
| 1665 |
|
| 1666 |
|
| 1667 |
*dirt = 0.0f; |
| 1668 |
|
| 1669 |
|
| 1670 |
if ( *cluster < 0 ) { |
| 1671 |
continue; |
| 1672 |
} |
| 1673 |
|
| 1674 |
|
| 1675 |
if ( noDirty ) { |
| 1676 |
*dirt = 1.0f; |
| 1677 |
continue; |
| 1678 |
} |
| 1679 |
|
| 1680 |
|
| 1681 |
trace.cluster = *cluster; |
| 1682 |
VectorCopy( origin, trace.origin ); |
| 1683 |
VectorCopy( normal, trace.normal ); |
| 1684 |
|
| 1685 |
|
| 1686 |
*dirt = DirtForSample( &trace ); |
| 1687 |
} |
| 1688 |
} |
| 1689 |
|
| 1690 |
|
| 1691 |
|
| 1692 |
|
| 1693 |
|
| 1694 |
for ( y = 0; y < lm->sh; y++ ) |
| 1695 |
{ |
| 1696 |
for ( x = 0; x < lm->sw; x++ ) |
| 1697 |
{ |
| 1698 |
|
| 1699 |
cluster = SUPER_CLUSTER( x, y ); |
| 1700 |
dirt = SUPER_DIRT( x, y ); |
| 1701 |
|
| 1702 |
|
| 1703 |
average = *dirt; |
| 1704 |
samples = 1.0f; |
| 1705 |
for ( sy = ( y - 1 ); sy <= ( y + 1 ); sy++ ) |
| 1706 |
{ |
| 1707 |
if ( sy < 0 || sy >= lm->sh ) { |
| 1708 |
continue; |
| 1709 |
} |
| 1710 |
|
| 1711 |
for ( sx = ( x - 1 ); sx <= ( x + 1 ); sx++ ) |
| 1712 |
{ |
| 1713 |
if ( sx < 0 || sx >= lm->sw || ( sx == x && sy == y ) ) { |
| 1714 |
continue; |
| 1715 |
} |
| 1716 |
|
| 1717 |
|
| 1718 |
cluster = SUPER_CLUSTER( sx, sy ); |
| 1719 |
dirt2 = SUPER_DIRT( sx, sy ); |
| 1720 |
if ( *cluster < 0 || *dirt2 <= 0.0f ) { |
| 1721 |
continue; |
| 1722 |
} |
| 1723 |
|
| 1724 |
|
| 1725 |
average += *dirt2; |
| 1726 |
samples += 1.0f; |
| 1727 |
} |
| 1728 |
|
| 1729 |
|
| 1730 |
if ( samples <= 0.0f ) { |
| 1731 |
break; |
| 1732 |
} |
| 1733 |
} |
| 1734 |
|
| 1735 |
|
| 1736 |
if ( samples <= 0.0f ) { |
| 1737 |
continue; |
| 1738 |
} |
| 1739 |
|
| 1740 |
|
| 1741 |
*dirt = average / samples; |
| 1742 |
} |
| 1743 |
} |
| 1744 |
} |
| 1745 |
|
| 1746 |
|
| 1747 |
|
| 1748 |
|
| 1749 |
|
| 1750 |
|
| 1751 |
|
| 1752 |
|
| 1753 |
static qboolean SubmapRawLuxel( rawLightmap_t *lm, int x, int y, float bx, float by, int *sampleCluster, vec3_t sampleOrigin, vec3_t sampleNormal ){ |
| 1754 |
int i, *cluster, *cluster2; |
| 1755 |
float *origin, *origin2, *normal; |
| 1756 |
vec3_t originVecs[ 2 ]; |
| 1757 |
|
| 1758 |
vec3_t origin_dummy = {}; |
| 1759 |
|
| 1760 |
|
| 1761 |
if ( ( x < ( lm->sw - 1 ) && bx >= 0.0f ) || ( x == 0 && bx <= 0.0f ) ) { |
| 1762 |
cluster = SUPER_CLUSTER( x, y ); |
| 1763 |
origin = SUPER_ORIGIN( x, y ); |
| 1764 |
|
| 1765 |
cluster2 = SUPER_CLUSTER( x + 1, y ); |
| 1766 |
origin2 = *cluster2 < 0 ? SUPER_ORIGIN( x, y ) : SUPER_ORIGIN( x + 1, y ); |
| 1767 |
|
| 1768 |
} |
| 1769 |
else if ( ( x > 0 && bx <= 0.0f ) || ( x == ( lm->sw - 1 ) && bx >= 0.0f ) ) { |
| 1770 |
cluster = SUPER_CLUSTER( x - 1, y ); |
| 1771 |
origin = *cluster < 0 ? SUPER_ORIGIN( x, y ) : SUPER_ORIGIN( x - 1, y ); |
| 1772 |
|
| 1773 |
cluster2 = SUPER_CLUSTER( x, y ); |
| 1774 |
origin2 = SUPER_ORIGIN( x, y ); |
| 1775 |
|
| 1776 |
} |
| 1777 |
else { |
| 1778 |
Sys_FPrintf( SYS_WRN, "WARNING: Spurious lightmap S vector\n" ); |
| 1779 |
origin = origin_dummy; |
| 1780 |
origin2 = origin_dummy; |
| 1781 |
} |
| 1782 |
|
| 1783 |
VectorSubtract( origin2, origin, originVecs[ 0 ] ); |
| 1784 |
|
| 1785 |
|
| 1786 |
|
| 1787 |
if ( ( y < ( lm->sh - 1 ) && bx >= 0.0f ) || ( y == 0 && bx <= 0.0f ) ) { |
| 1788 |
cluster = SUPER_CLUSTER( x, y ); |
| 1789 |
origin = SUPER_ORIGIN( x, y ); |
| 1790 |
|
| 1791 |
cluster2 = SUPER_CLUSTER( x, y + 1 ); |
| 1792 |
origin2 = *cluster2 < 0 ? SUPER_ORIGIN( x, y ) : SUPER_ORIGIN( x, y + 1 ); |
| 1793 |
|
| 1794 |
} |
| 1795 |
else if ( ( y > 0 && bx <= 0.0f ) || ( y == ( lm->sh - 1 ) && bx >= 0.0f ) ) { |
| 1796 |
cluster = SUPER_CLUSTER( x, y - 1 ); |
| 1797 |
origin = *cluster < 0 ? SUPER_ORIGIN( x, y ) : SUPER_ORIGIN( x, y - 1 ); |
| 1798 |
|
| 1799 |
cluster2 = SUPER_CLUSTER( x, y ); |
| 1800 |
origin2 = SUPER_ORIGIN( x, y ); |
| 1801 |
|
| 1802 |
} |
| 1803 |
else{ |
| 1804 |
Sys_FPrintf( SYS_WRN, "WARNING: Spurious lightmap T vector\n" ); |
| 1805 |
} |
| 1806 |
|
| 1807 |
VectorSubtract( origin2, origin, originVecs[ 1 ] ); |
| 1808 |
|
| 1809 |
|
| 1810 |
for ( i = 0; i < 3; i++ ) |
| 1811 |
sampleOrigin[ i ] = sampleOrigin[ i ] + ( bx * originVecs[ 0 ][ i ] ) + ( by * originVecs[ 1 ][ i ] ); |
| 1812 |
|
| 1813 |
|
| 1814 |
*sampleCluster = ClusterForPointExtFilter( sampleOrigin, ( LUXEL_EPSILON * 2 ), lm->numLightClusters, lm->lightClusters ); |
| 1815 |
if ( *sampleCluster < 0 ) { |
| 1816 |
return qfalse; |
| 1817 |
} |
| 1818 |
|
| 1819 |
|
| 1820 |
normal = SUPER_NORMAL( x, y ); |
| 1821 |
VectorCopy( normal, sampleNormal ); |
| 1822 |
|
| 1823 |
|
| 1824 |
return qtrue; |
| 1825 |
} |
| 1826 |
|
| 1827 |
|
| 1828 |
|
| 1829 |
|
| 1830 |
|
| 1831 |
|
| 1832 |
|
| 1833 |
static void SubsampleRawLuxel_r( rawLightmap_t *lm, trace_t *trace, vec3_t sampleOrigin, int x, int y, float bias, float *lightLuxel, float *lightDeluxel ){ |
| 1834 |
int b, samples, mapped, lighted; |
| 1835 |
int cluster[ 4 ]; |
| 1836 |
vec4_t luxel[ 4 ]; |
| 1837 |
vec3_t deluxel[ 4 ]; |
| 1838 |
vec3_t origin[ 4 ], normal[ 4 ]; |
| 1839 |
float biasDirs[ 4 ][ 2 ] = { { -1.0f, -1.0f }, { 1.0f, -1.0f }, { -1.0f, 1.0f }, { 1.0f, 1.0f } }; |
| 1840 |
vec3_t color, direction = { 0, 0, 0 }, total; |
| 1841 |
|
| 1842 |
|
| 1843 |
|
| 1844 |
if ( lightLuxel[ 3 ] >= lightSamples ) { |
| 1845 |
return; |
| 1846 |
} |
| 1847 |
|
| 1848 |
|
| 1849 |
VectorClear( total ); |
| 1850 |
mapped = 0; |
| 1851 |
lighted = 0; |
| 1852 |
|
| 1853 |
|
| 1854 |
for ( b = 0; b < 4; b++ ) |
| 1855 |
{ |
| 1856 |
|
| 1857 |
VectorCopy( sampleOrigin, origin[ b ] ); |
| 1858 |
|
| 1859 |
|
| 1860 |
if ( !SubmapRawLuxel( lm, x, y, ( bias * biasDirs[ b ][ 0 ] ), ( bias * biasDirs[ b ][ 1 ] ), &cluster[ b ], origin[ b ], normal[ b ] ) ) { |
| 1861 |
cluster[ b ] = -1; |
| 1862 |
continue; |
| 1863 |
} |
| 1864 |
mapped++; |
| 1865 |
|
| 1866 |
|
| 1867 |
luxel[ b ][ 3 ] = lightLuxel[ 3 ] + 1.0f; |
| 1868 |
|
| 1869 |
|
| 1870 |
trace->cluster = *cluster; |
| 1871 |
VectorCopy( origin[ b ], trace->origin ); |
| 1872 |
VectorCopy( normal[ b ], trace->normal ); |
| 1873 |
|
| 1874 |
|
| 1875 |
|
| 1876 |
LightContributionToSample( trace ); |
| 1877 |
if ( trace->forceSubsampling > 1.0f ) { |
| 1878 |
|
| 1879 |
++lighted; |
| 1880 |
++mapped; |
| 1881 |
++mapped; |
| 1882 |
} |
| 1883 |
|
| 1884 |
|
| 1885 |
VectorCopy( trace->color, luxel[ b ] ); |
| 1886 |
if ( lightDeluxel ) { |
| 1887 |
VectorCopy( trace->directionContribution, deluxel[ b ] ); |
| 1888 |
} |
| 1889 |
VectorAdd( total, trace->color, total ); |
| 1890 |
if ( ( luxel[ b ][ 0 ] + luxel[ b ][ 1 ] + luxel[ b ][ 2 ] ) > 0.0f ) { |
| 1891 |
lighted++; |
| 1892 |
} |
| 1893 |
} |
| 1894 |
|
| 1895 |
|
| 1896 |
if ( ( lightLuxel[ 3 ] + 1.0f ) < lightSamples && |
| 1897 |
( total[ 0 ] > 4.0f || total[ 1 ] > 4.0f || total[ 2 ] > 4.0f ) && |
| 1898 |
lighted != 0 && lighted != mapped ) { |
| 1899 |
for ( b = 0; b < 4; b++ ) |
| 1900 |
{ |
| 1901 |
if ( cluster[ b ] < 0 ) { |
| 1902 |
continue; |
| 1903 |
} |
| 1904 |
SubsampleRawLuxel_r( lm, trace, origin[ b ], x, y, ( bias * 0.5f ), luxel[ b ], lightDeluxel ? deluxel[ b ] : NULL ); |
| 1905 |
} |
| 1906 |
} |
| 1907 |
|
| 1908 |
|
| 1909 |
|
| 1910 |
|
| 1911 |
VectorCopy( lightLuxel, color ); |
| 1912 |
if ( lightDeluxel ) { |
| 1913 |
VectorCopy( lightDeluxel, direction ); |
| 1914 |
} |
| 1915 |
samples = 1; |
| 1916 |
for ( b = 0; b < 4; b++ ) |
| 1917 |
{ |
| 1918 |
if ( cluster[ b ] < 0 ) { |
| 1919 |
continue; |
| 1920 |
} |
| 1921 |
VectorAdd( color, luxel[ b ], color ); |
| 1922 |
if ( lightDeluxel ) { |
| 1923 |
VectorAdd( direction, deluxel[ b ], direction ); |
| 1924 |
} |
| 1925 |
samples++; |
| 1926 |
} |
| 1927 |
|
| 1928 |
|
| 1929 |
if ( samples > 0 ) { |
| 1930 |
|
| 1931 |
color[ 0 ] /= samples; |
| 1932 |
color[ 1 ] /= samples; |
| 1933 |
color[ 2 ] /= samples; |
| 1934 |
|
| 1935 |
|
| 1936 |
VectorCopy( color, lightLuxel ); |
| 1937 |
lightLuxel[ 3 ] += 1.0f; |
| 1938 |
|
| 1939 |
if ( lightDeluxel ) { |
| 1940 |
direction[ 0 ] /= samples; |
| 1941 |
direction[ 1 ] /= samples; |
| 1942 |
direction[ 2 ] /= samples; |
| 1943 |
VectorCopy( direction, lightDeluxel ); |
| 1944 |
} |
| 1945 |
} |
| 1946 |
} |
| 1947 |
|
| 1948 |
|
| 1949 |
static void GaussLikeRandom( float sigma, float *x, float *y ){ |
| 1950 |
float r; |
| 1951 |
r = Random() * 2 * Q_PI; |
| 1952 |
*x = sigma * 2.73861278752581783822 * cos( r ); |
| 1953 |
*y = sigma * 2.73861278752581783822 * sin( r ); |
| 1954 |
r = Random(); |
| 1955 |
r = 1 - sqrt( r ); |
| 1956 |
r = 1 - sqrt( r ); |
| 1957 |
*x *= r; |
| 1958 |
*y *= r; |
| 1959 |
} |
| 1960 |
static void RandomSubsampleRawLuxel( rawLightmap_t *lm, trace_t *trace, vec3_t sampleOrigin, int x, int y, float bias, float *lightLuxel, float *lightDeluxel ){ |
| 1961 |
int b, mapped; |
| 1962 |
int cluster; |
| 1963 |
vec3_t origin, normal; |
| 1964 |
vec3_t total, totaldirection; |
| 1965 |
float dx, dy; |
| 1966 |
|
| 1967 |
VectorClear( total ); |
| 1968 |
VectorClear( totaldirection ); |
| 1969 |
mapped = 0; |
| 1970 |
for ( b = 0; b < lightSamples; ++b ) |
| 1971 |
{ |
| 1972 |
|
| 1973 |
VectorCopy( sampleOrigin, origin ); |
| 1974 |
GaussLikeRandom( bias, &dx, &dy ); |
| 1975 |
|
| 1976 |
|
| 1977 |
if ( !SubmapRawLuxel( lm, x, y, dx, dy, &cluster, origin, normal ) ) { |
| 1978 |
cluster = -1; |
| 1979 |
continue; |
| 1980 |
} |
| 1981 |
mapped++; |
| 1982 |
|
| 1983 |
trace->cluster = cluster; |
| 1984 |
VectorCopy( origin, trace->origin ); |
| 1985 |
VectorCopy( normal, trace->normal ); |
| 1986 |
|
| 1987 |
LightContributionToSample( trace ); |
| 1988 |
VectorAdd( total, trace->color, total ); |
| 1989 |
if ( lightDeluxel ) { |
| 1990 |
VectorAdd( totaldirection, trace->directionContribution, totaldirection ); |
| 1991 |
} |
| 1992 |
} |
| 1993 |
|
| 1994 |
|
| 1995 |
if ( mapped > 0 ) { |
| 1996 |
|
| 1997 |
lightLuxel[ 0 ] = total[ 0 ] / mapped; |
| 1998 |
lightLuxel[ 1 ] = total[ 1 ] / mapped; |
| 1999 |
lightLuxel[ 2 ] = total[ 2 ] / mapped; |
| 2000 |
|
| 2001 |
if ( lightDeluxel ) { |
| 2002 |
lightDeluxel[ 0 ] = totaldirection[ 0 ] / mapped; |
| 2003 |
lightDeluxel[ 1 ] = totaldirection[ 1 ] / mapped; |
| 2004 |
lightDeluxel[ 2 ] = totaldirection[ 2 ] / mapped; |
| 2005 |
} |
| 2006 |
} |
| 2007 |
} |
| 2008 |
|
| 2009 |
|
| 2010 |
|
| 2011 |
|
| 2012 |
|
| 2013 |
|
| 2014 |
|
| 2015 |
|
| 2016 |
#define STACK_LL_SIZE ( SUPER_LUXEL_SIZE * 64 * 64 ) |
| 2017 |
#define LIGHT_LUXEL( x, y ) ( lightLuxels + ( ( ( ( y ) * lm->sw ) + ( x ) ) * SUPER_LUXEL_SIZE ) ) |
| 2018 |
#define LIGHT_DELUXEL( x, y ) ( lightDeluxels + ( ( ( ( y ) * lm->sw ) + ( x ) ) * SUPER_DELUXEL_SIZE ) ) |
| 2019 |
|
| 2020 |
void IlluminateRawLightmap( int rawLightmapNum ){ |
| 2021 |
int i, t, x, y, sx, sy, size, luxelFilterRadius, lightmapNum; |
| 2022 |
int *cluster, *cluster2, mapped, lighted, totalLighted; |
| 2023 |
size_t llSize, ldSize; |
| 2024 |
rawLightmap_t *lm; |
| 2025 |
surfaceInfo_t *info; |
| 2026 |
qboolean filterColor, filterDir; |
| 2027 |
float brightness; |
| 2028 |
float *origin, *normal, *dirt, *luxel, *luxel2, *deluxel, *deluxel2; |
| 2029 |
unsigned char *flag; |
| 2030 |
float *lightLuxels, *lightDeluxels, *lightLuxel, *lightDeluxel, samples, filterRadius, weight; |
| 2031 |
vec3_t color, direction, averageColor, averageDir, total, temp, temp2; |
| 2032 |
float tests[ 4 ][ 2 ] = { { 0.0f, 0 }, { 1, 0 }, { 0, 1 }, { 1, 1 } }; |
| 2033 |
trace_t trace; |
| 2034 |
float stackLightLuxels[ STACK_LL_SIZE ]; |
| 2035 |
|
| 2036 |
|
| 2037 |
|
| 2038 |
if ( rawLightmapNum >= numRawLightmaps ) { |
| 2039 |
return; |
| 2040 |
} |
| 2041 |
|
| 2042 |
|
| 2043 |
lm = &rawLightmaps[ rawLightmapNum ]; |
| 2044 |
|
| 2045 |
|
| 2046 |
trace.testOcclusion = !noTrace; |
| 2047 |
trace.forceSunlight = qfalse; |
| 2048 |
trace.recvShadows = lm->recvShadows; |
| 2049 |
trace.numSurfaces = lm->numLightSurfaces; |
| 2050 |
trace.surfaces = &lightSurfaces[ lm->firstLightSurface ]; |
| 2051 |
trace.inhibitRadius = DEFAULT_INHIBIT_RADIUS; |
| 2052 |
|
| 2053 |
|
| 2054 |
trace.twoSided = qfalse; |
| 2055 |
for ( i = 0; i < trace.numSurfaces; i++ ) |
| 2056 |
{ |
| 2057 |
|
| 2058 |
info = &surfaceInfos[ trace.surfaces[ i ] ]; |
| 2059 |
|
| 2060 |
|
| 2061 |
if ( info->si->twoSided ) { |
| 2062 |
trace.twoSided = qtrue; |
| 2063 |
break; |
| 2064 |
} |
| 2065 |
} |
| 2066 |
|
| 2067 |
|
| 2068 |
CreateTraceLightsForBounds( lm->mins, lm->maxs, lm->plane, lm->numLightClusters, lm->lightClusters, LIGHT_SURFACES, &trace ); |
| 2069 |
|
| 2070 |
|
| 2071 |
|
| 2072 |
|
| 2073 |
|
| 2074 |
|
| 2075 |
numLuxelsIlluminated += ( lm->sw * lm->sh ); |
| 2076 |
|
| 2077 |
|
| 2078 |
if ( debugSurfaces || debugAxis || debugCluster || debugOrigin || dirtDebug || normalmap ) { |
| 2079 |
|
| 2080 |
for ( y = 0; y < lm->sh; y++ ) |
| 2081 |
{ |
| 2082 |
for ( x = 0; x < lm->sw; x++ ) |
| 2083 |
{ |
| 2084 |
|
| 2085 |
cluster = SUPER_CLUSTER( x, y ); |
| 2086 |
|
| 2087 |
|
| 2088 |
if ( *cluster < 0 ) { |
| 2089 |
continue; |
| 2090 |
} |
| 2091 |
|
| 2092 |
|
| 2093 |
luxel = SUPER_LUXEL( 0, x, y ); |
| 2094 |
origin = SUPER_ORIGIN( x, y ); |
| 2095 |
normal = SUPER_NORMAL( x, y ); |
| 2096 |
|
| 2097 |
|
| 2098 |
if ( debugSurfaces ) { |
| 2099 |
VectorCopy( debugColors[ rawLightmapNum % 12 ], luxel ); |
| 2100 |
} |
| 2101 |
|
| 2102 |
|
| 2103 |
else if ( debugAxis ) { |
| 2104 |
luxel[ 0 ] = ( lm->axis[ 0 ] + 1.0f ) * 127.5f; |
| 2105 |
luxel[ 1 ] = ( lm->axis[ 1 ] + 1.0f ) * 127.5f; |
| 2106 |
luxel[ 2 ] = ( lm->axis[ 2 ] + 1.0f ) * 127.5f; |
| 2107 |
} |
| 2108 |
|
| 2109 |
|
| 2110 |
else if ( debugCluster ) { |
| 2111 |
VectorCopy( debugColors[ *cluster % 12 ], luxel ); |
| 2112 |
} |
| 2113 |
|
| 2114 |
|
| 2115 |
else if ( debugOrigin ) { |
| 2116 |
VectorSubtract( lm->maxs, lm->mins, temp ); |
| 2117 |
VectorScale( temp, ( 1.0f / 255.0f ), temp ); |
| 2118 |
VectorSubtract( origin, lm->mins, temp2 ); |
| 2119 |
luxel[ 0 ] = lm->mins[ 0 ] + ( temp[ 0 ] * temp2[ 0 ] ); |
| 2120 |
luxel[ 1 ] = lm->mins[ 1 ] + ( temp[ 1 ] * temp2[ 1 ] ); |
| 2121 |
luxel[ 2 ] = lm->mins[ 2 ] + ( temp[ 2 ] * temp2[ 2 ] ); |
| 2122 |
} |
| 2123 |
|
| 2124 |
|
| 2125 |
else if ( normalmap ) { |
| 2126 |
luxel[ 0 ] = ( normal[ 0 ] + 1.0f ) * 127.5f; |
| 2127 |
luxel[ 1 ] = ( normal[ 1 ] + 1.0f ) * 127.5f; |
| 2128 |
luxel[ 2 ] = ( normal[ 2 ] + 1.0f ) * 127.5f; |
| 2129 |
} |
| 2130 |
|
| 2131 |
|
| 2132 |
else{ |
| 2133 |
VectorClear( luxel ); |
| 2134 |
} |
| 2135 |
|
| 2136 |
|
| 2137 |
luxel[ 3 ] = 1.0f; |
| 2138 |
} |
| 2139 |
} |
| 2140 |
} |
| 2141 |
else |
| 2142 |
{ |
| 2143 |
|
| 2144 |
llSize = lm->sw * lm->sh * SUPER_LUXEL_SIZE * sizeof( float ); |
| 2145 |
ldSize = lm->sw * lm->sh * SUPER_DELUXEL_SIZE * sizeof( float ); |
| 2146 |
if ( llSize <= ( STACK_LL_SIZE * sizeof( float ) ) ) { |
| 2147 |
lightLuxels = stackLightLuxels; |
| 2148 |
} |
| 2149 |
else{ |
| 2150 |
lightLuxels = safe_malloc( llSize ); |
| 2151 |
} |
| 2152 |
if ( deluxemap ) { |
| 2153 |
lightDeluxels = safe_malloc( ldSize ); |
| 2154 |
} |
| 2155 |
else{ |
| 2156 |
lightDeluxels = NULL; |
| 2157 |
} |
| 2158 |
|
| 2159 |
|
| 2160 |
|
| 2161 |
|
| 2162 |
|
| 2163 |
for ( y = 0; y < lm->sh; y++ ) |
| 2164 |
{ |
| 2165 |
for ( x = 0; x < lm->sw; x++ ) |
| 2166 |
{ |
| 2167 |
|
| 2168 |
cluster = SUPER_CLUSTER( x, y ); |
| 2169 |
luxel = SUPER_LUXEL( 0, x, y ); |
| 2170 |
normal = SUPER_NORMAL( x, y ); |
| 2171 |
deluxel = SUPER_DELUXEL( x, y ); |
| 2172 |
|
| 2173 |
|
| 2174 |
if ( *cluster < 0 ) { |
| 2175 |
VectorClear( luxel ); |
| 2176 |
} |
| 2177 |
|
| 2178 |
|
| 2179 |
else |
| 2180 |
{ |
| 2181 |
VectorCopy( ambientColor, luxel ); |
| 2182 |
if ( deluxemap ) { |
| 2183 |
brightness = RGBTOGRAY( ambientColor ) * ( 1.0f / 255.0f ); |
| 2184 |
|
| 2185 |
|
| 2186 |
if ( brightness < 0.00390625f ) { |
| 2187 |
brightness = 0.00390625f; |
| 2188 |
} |
| 2189 |
|
| 2190 |
VectorScale( normal, brightness, deluxel ); |
| 2191 |
} |
| 2192 |
luxel[ 3 ] = 1.0f; |
| 2193 |
} |
| 2194 |
} |
| 2195 |
} |
| 2196 |
|
| 2197 |
|
| 2198 |
size = lm->sw * lm->sh * SUPER_LUXEL_SIZE * sizeof( float ); |
| 2199 |
for ( lightmapNum = 1; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ ) |
| 2200 |
{ |
| 2201 |
if ( lm->superLuxels[ lightmapNum ] != NULL ) { |
| 2202 |
memset( lm->superLuxels[ lightmapNum ], 0, size ); |
| 2203 |
} |
| 2204 |
} |
| 2205 |
|
| 2206 |
|
| 2207 |
|
| 2208 |
|
| 2209 |
|
| 2210 |
|
| 2211 |
for ( i = 0; i < trace.numLights; i++ ) |
| 2212 |
{ |
| 2213 |
|
| 2214 |
trace.light = trace.lights[ i ]; |
| 2215 |
|
| 2216 |
|
| 2217 |
for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ ) |
| 2218 |
{ |
| 2219 |
if ( lm->styles[ lightmapNum ] == trace.light->style || |
| 2220 |
lm->styles[ lightmapNum ] == LS_NONE ) { |
| 2221 |
break; |
| 2222 |
} |
| 2223 |
} |
| 2224 |
|
| 2225 |
|
| 2226 |
if ( lightmapNum >= MAX_LIGHTMAPS ) { |
| 2227 |
Sys_FPrintf( SYS_WRN, "WARNING: Hit per-surface style limit (%d)\n", MAX_LIGHTMAPS ); |
| 2228 |
continue; |
| 2229 |
} |
| 2230 |
|
| 2231 |
|
| 2232 |
memset( lightLuxels, 0, llSize ); |
| 2233 |
if ( deluxemap ) { |
| 2234 |
memset( lightDeluxels, 0, ldSize ); |
| 2235 |
} |
| 2236 |
totalLighted = 0; |
| 2237 |
|
| 2238 |
|
| 2239 |
filterRadius = lm->filterRadius > trace.light->filterRadius |
| 2240 |
? lm->filterRadius |
| 2241 |
: trace.light->filterRadius; |
| 2242 |
if ( filterRadius < 0.0f ) { |
| 2243 |
filterRadius = 0.0f; |
| 2244 |
} |
| 2245 |
|
| 2246 |
|
| 2247 |
luxelFilterRadius = lm->sampleSize != 0 ? superSample * filterRadius / lm->sampleSize : 0; |
| 2248 |
if ( luxelFilterRadius == 0 && ( filterRadius > 0.0f || filter ) ) { |
| 2249 |
luxelFilterRadius = 1; |
| 2250 |
} |
| 2251 |
|
| 2252 |
|
| 2253 |
if ( lightSamples > 1 || lightRandomSamples ) { |
| 2254 |
size = lm->sw * lm->sh * SUPER_LUXEL_SIZE * sizeof( unsigned char ); |
| 2255 |
if ( lm->superFlags == NULL ) { |
| 2256 |
lm->superFlags = safe_malloc( size ); |
| 2257 |
} |
| 2258 |
memset( (void *) lm->superFlags, 0, size ); |
| 2259 |
} |
| 2260 |
|
| 2261 |
|
| 2262 |
for ( y = 0; y < lm->sh; y++ ) |
| 2263 |
{ |
| 2264 |
for ( x = 0; x < lm->sw; x++ ) |
| 2265 |
{ |
| 2266 |
|
| 2267 |
cluster = SUPER_CLUSTER( x, y ); |
| 2268 |
if ( *cluster < 0 ) { |
| 2269 |
continue; |
| 2270 |
} |
| 2271 |
|
| 2272 |
|
| 2273 |
lightLuxel = LIGHT_LUXEL( x, y ); |
| 2274 |
lightDeluxel = LIGHT_DELUXEL( x, y ); |
| 2275 |
origin = SUPER_ORIGIN( x, y ); |
| 2276 |
normal = SUPER_NORMAL( x, y ); |
| 2277 |
flag = SUPER_FLAG( x, y ); |
| 2278 |
|
| 2279 |
|
| 2280 |
lightLuxel[ 3 ] = 1.0f; |
| 2281 |
|
| 2282 |
|
| 2283 |
trace.cluster = *cluster; |
| 2284 |
VectorCopy( origin, trace.origin ); |
| 2285 |
VectorCopy( normal, trace.normal ); |
| 2286 |
|
| 2287 |
|
| 2288 |
LightContributionToSample( &trace ); |
| 2289 |
VectorCopy( trace.color, lightLuxel ); |
| 2290 |
|
| 2291 |
|
| 2292 |
if ( deluxemap ) { |
| 2293 |
VectorCopy( trace.directionContribution, lightDeluxel ); |
| 2294 |
} |
| 2295 |
|
| 2296 |
|
| 2297 |
if ( trace.forceSubsampling > 1.0f && ( lightSamples > 1 || lightRandomSamples ) ) { |
| 2298 |
totalLighted++; |
| 2299 |
*flag |= FLAG_FORCE_SUBSAMPLING; |
| 2300 |
} |
| 2301 |
|
| 2302 |
else if ( trace.color[ 0 ] || trace.color[ 1 ] || trace.color[ 2 ] ) { |
| 2303 |
totalLighted++; |
| 2304 |
} |
| 2305 |
} |
| 2306 |
} |
| 2307 |
|
| 2308 |
|
| 2309 |
if ( totalLighted == 0 ) { |
| 2310 |
continue; |
| 2311 |
} |
| 2312 |
|
| 2313 |
|
| 2314 |
|
| 2315 |
if ( lightSamples > 1 || lightRandomSamples ) { |
| 2316 |
|
| 2317 |
for ( y = 0; y < ( lm->sh - 1 ); y++ ) |
| 2318 |
{ |
| 2319 |
for ( x = 0; x < ( lm->sw - 1 ); x++ ) |
| 2320 |
{ |
| 2321 |
|
| 2322 |
mapped = 0; |
| 2323 |
lighted = 0; |
| 2324 |
VectorClear( total ); |
| 2325 |
|
| 2326 |
|
| 2327 |
for ( t = 0; t < 4; t++ ) |
| 2328 |
{ |
| 2329 |
|
| 2330 |
sx = x + tests[ t ][ 0 ]; |
| 2331 |
sy = y + tests[ t ][ 1 ]; |
| 2332 |
|
| 2333 |
|
| 2334 |
cluster = SUPER_CLUSTER( sx, sy ); |
| 2335 |
if ( *cluster < 0 ) { |
| 2336 |
continue; |
| 2337 |
} |
| 2338 |
mapped++; |
| 2339 |
|
| 2340 |
|
| 2341 |
flag = SUPER_FLAG( sx, sy ); |
| 2342 |
if ( *flag & FLAG_FORCE_SUBSAMPLING ) { |
| 2343 |
|
| 2344 |
++lighted; |
| 2345 |
++mapped; |
| 2346 |
++mapped; |
| 2347 |
} |
| 2348 |
lightLuxel = LIGHT_LUXEL( sx, sy ); |
| 2349 |
VectorAdd( total, lightLuxel, total ); |
| 2350 |
if ( ( lightLuxel[ 0 ] + lightLuxel[ 1 ] + lightLuxel[ 2 ] ) > 0.0f ) { |
| 2351 |
lighted++; |
| 2352 |
} |
| 2353 |
} |
| 2354 |
|
| 2355 |
|
| 2356 |
if ( total[ 0 ] <= 4.0f && total[ 1 ] <= 4.0f && total[ 2 ] <= 4.0f ) { |
| 2357 |
continue; |
| 2358 |
} |
| 2359 |
|
| 2360 |
|
| 2361 |
if ( lighted != 0 && lighted != mapped ) { |
| 2362 |
for ( t = 0; t < 4; t++ ) |
| 2363 |
{ |
| 2364 |
|
| 2365 |
sx = x + tests[ t ][ 0 ]; |
| 2366 |
sy = y + tests[ t ][ 1 ]; |
| 2367 |
|
| 2368 |
|
| 2369 |
cluster = SUPER_CLUSTER( sx, sy ); |
| 2370 |
if ( *cluster < 0 ) { |
| 2371 |
continue; |
| 2372 |
} |
| 2373 |
flag = SUPER_FLAG( sx, sy ); |
| 2374 |
if ( *flag & FLAG_ALREADY_SUBSAMPLED ) { |
| 2375 |
continue; |
| 2376 |
} |
| 2377 |
lightLuxel = LIGHT_LUXEL( sx, sy ); |
| 2378 |
lightDeluxel = LIGHT_DELUXEL( sx, sy ); |
| 2379 |
origin = SUPER_ORIGIN( sx, sy ); |
| 2380 |
|
| 2381 |
|
| 2382 |
|
| 2383 |
|
| 2384 |
|
| 2385 |
|
| 2386 |
if ( lightRandomSamples ) { |
| 2387 |
RandomSubsampleRawLuxel( lm, &trace, origin, sx, sy, 0.5f * lightSamplesSearchBoxSize, lightLuxel, deluxemap ? lightDeluxel : NULL ); |
| 2388 |
} |
| 2389 |
else{ |
| 2390 |
SubsampleRawLuxel_r( lm, &trace, origin, sx, sy, 0.25f * lightSamplesSearchBoxSize, lightLuxel, deluxemap ? lightDeluxel : NULL ); |
| 2391 |
} |
| 2392 |
|
| 2393 |
*flag |= FLAG_ALREADY_SUBSAMPLED; |
| 2394 |
|
| 2395 |
|
| 2396 |
|
| 2397 |
|
| 2398 |
} |
| 2399 |
} |
| 2400 |
} |
| 2401 |
} |
| 2402 |
} |
| 2403 |
|
| 2404 |
|
| 2405 |
if ( 0 && dirty ) { |
| 2406 |
|
| 2407 |
for ( y = 0; y < lm->sh; y++ ) |
| 2408 |
{ |
| 2409 |
for ( x = 0; x < lm->sw; x++ ) |
| 2410 |
{ |
| 2411 |
|
| 2412 |
cluster = SUPER_CLUSTER( x, y ); |
| 2413 |
if ( *cluster < 0 ) { |
| 2414 |
continue; |
| 2415 |
} |
| 2416 |
|
| 2417 |
|
| 2418 |
lightLuxel = LIGHT_LUXEL( x, y ); |
| 2419 |
dirt = SUPER_DIRT( x, y ); |
| 2420 |
|
| 2421 |
|
| 2422 |
VectorScale( lightLuxel, *dirt, lightLuxel ); |
| 2423 |
} |
| 2424 |
} |
| 2425 |
} |
| 2426 |
|
| 2427 |
|
| 2428 |
if ( lm->superLuxels[ lightmapNum ] == NULL ) { |
| 2429 |
|
| 2430 |
size = lm->sw * lm->sh * SUPER_LUXEL_SIZE * sizeof( float ); |
| 2431 |
lm->superLuxels[ lightmapNum ] = safe_malloc0( size ); |
| 2432 |
} |
| 2433 |
|
| 2434 |
|
| 2435 |
if ( lightmapNum > 0 ) { |
| 2436 |
lm->styles[ lightmapNum ] = trace.light->style; |
| 2437 |
|
| 2438 |
} |
| 2439 |
|
| 2440 |
|
| 2441 |
for ( y = 0; y < lm->sh; y++ ) |
| 2442 |
{ |
| 2443 |
for ( x = 0; x < lm->sw; x++ ) |
| 2444 |
{ |
| 2445 |
|
| 2446 |
cluster = SUPER_CLUSTER( x, y ); |
| 2447 |
if ( *cluster < 0 ) { |
| 2448 |
continue; |
| 2449 |
} |
| 2450 |
origin = SUPER_ORIGIN( x, y ); |
| 2451 |
|
| 2452 |
|
| 2453 |
if ( luxelFilterRadius ) { |
| 2454 |
|
| 2455 |
VectorClear( averageColor ); |
| 2456 |
VectorClear( averageDir ); |
| 2457 |
samples = 0.0f; |
| 2458 |
|
| 2459 |
|
| 2460 |
for ( sy = ( y - luxelFilterRadius ); sy <= ( y + luxelFilterRadius ); sy++ ) |
| 2461 |
{ |
| 2462 |
if ( sy < 0 || sy >= lm->sh ) { |
| 2463 |
continue; |
| 2464 |
} |
| 2465 |
|
| 2466 |
for ( sx = ( x - luxelFilterRadius ); sx <= ( x + luxelFilterRadius ); sx++ ) |
| 2467 |
{ |
| 2468 |
if ( sx < 0 || sx >= lm->sw ) { |
| 2469 |
continue; |
| 2470 |
} |
| 2471 |
|
| 2472 |
|
| 2473 |
cluster = SUPER_CLUSTER( sx, sy ); |
| 2474 |
if ( *cluster < 0 ) { |
| 2475 |
continue; |
| 2476 |
} |
| 2477 |
lightLuxel = LIGHT_LUXEL( sx, sy ); |
| 2478 |
lightDeluxel = LIGHT_DELUXEL( sx, sy ); |
| 2479 |
|
| 2480 |
|
| 2481 |
weight = ( abs( sx - x ) == luxelFilterRadius ? 0.5f : 1.0f ); |
| 2482 |
weight *= ( abs( sy - y ) == luxelFilterRadius ? 0.5f : 1.0f ); |
| 2483 |
|
| 2484 |
|
| 2485 |
VectorScale( lightLuxel, weight, color ); |
| 2486 |
VectorAdd( averageColor, color, averageColor ); |
| 2487 |
if ( deluxemap ) { |
| 2488 |
VectorScale( lightDeluxel, weight, direction ); |
| 2489 |
VectorAdd( averageDir, direction, averageDir ); |
| 2490 |
} |
| 2491 |
samples += weight; |
| 2492 |
} |
| 2493 |
} |
| 2494 |
|
| 2495 |
|
| 2496 |
if ( samples <= 0.0f ) { |
| 2497 |
continue; |
| 2498 |
} |
| 2499 |
|
| 2500 |
|
| 2501 |
luxel = SUPER_LUXEL( lightmapNum, x, y ); |
| 2502 |
luxel[ 3 ] = 1.0f; |
| 2503 |
|
| 2504 |
|
| 2505 |
if ( trace.light->flags & LIGHT_NEGATIVE ) { |
| 2506 |
luxel[ 0 ] -= averageColor[ 0 ] / samples; |
| 2507 |
luxel[ 1 ] -= averageColor[ 1 ] / samples; |
| 2508 |
luxel[ 2 ] -= averageColor[ 2 ] / samples; |
| 2509 |
} |
| 2510 |
|
| 2511 |
|
| 2512 |
else |
| 2513 |
{ |
| 2514 |
luxel[ 0 ] += averageColor[ 0 ] / samples; |
| 2515 |
luxel[ 1 ] += averageColor[ 1 ] / samples; |
| 2516 |
luxel[ 2 ] += averageColor[ 2 ] / samples; |
| 2517 |
} |
| 2518 |
|
| 2519 |
if ( deluxemap ) { |
| 2520 |
|
| 2521 |
deluxel = SUPER_DELUXEL( x, y ); |
| 2522 |
deluxel[ 0 ] += averageDir[ 0 ] / samples; |
| 2523 |
deluxel[ 1 ] += averageDir[ 1 ] / samples; |
| 2524 |
deluxel[ 2 ] += averageDir[ 2 ] / samples; |
| 2525 |
} |
| 2526 |
} |
| 2527 |
|
| 2528 |
|
| 2529 |
else |
| 2530 |
{ |
| 2531 |
|
| 2532 |
lightLuxel = LIGHT_LUXEL( x, y ); |
| 2533 |
lightDeluxel = LIGHT_DELUXEL( x, y ); |
| 2534 |
luxel = SUPER_LUXEL( lightmapNum, x, y ); |
| 2535 |
deluxel = SUPER_DELUXEL( x, y ); |
| 2536 |
|
| 2537 |
|
| 2538 |
if ( trace.light->flags & LIGHT_NEGATIVE ) { |
| 2539 |
VectorScale( averageColor, -1.0f, averageColor ); |
| 2540 |
} |
| 2541 |
|
| 2542 |
|
| 2543 |
luxel[ 3 ] = 1.0f; |
| 2544 |
|
| 2545 |
|
| 2546 |
if ( trace.light->flags & LIGHT_NEGATIVE ) { |
| 2547 |
VectorSubtract( luxel, lightLuxel, luxel ); |
| 2548 |
} |
| 2549 |
|
| 2550 |
|
| 2551 |
else{ |
| 2552 |
VectorAdd( luxel, lightLuxel, luxel ); |
| 2553 |
} |
| 2554 |
|
| 2555 |
if ( deluxemap ) { |
| 2556 |
VectorAdd( deluxel, lightDeluxel, deluxel ); |
| 2557 |
} |
| 2558 |
} |
| 2559 |
} |
| 2560 |
} |
| 2561 |
} |
| 2562 |
|
| 2563 |
|
| 2564 |
if ( lightLuxels != stackLightLuxels ) { |
| 2565 |
free( lightLuxels ); |
| 2566 |
} |
| 2567 |
|
| 2568 |
if ( deluxemap ) { |
| 2569 |
free( lightDeluxels ); |
| 2570 |
} |
| 2571 |
} |
| 2572 |
|
| 2573 |
|
| 2574 |
FreeTraceLights( &trace ); |
| 2575 |
|
| 2576 |
|
| 2577 |
if ( floodlighty ) { |
| 2578 |
FloodlightIlluminateLightmap( lm ); |
| 2579 |
} |
| 2580 |
|
| 2581 |
if ( debugnormals ) { |
| 2582 |
for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ ) |
| 2583 |
{ |
| 2584 |
|
| 2585 |
if ( lm->superLuxels[ lightmapNum ] == NULL ) { |
| 2586 |
continue; |
| 2587 |
} |
| 2588 |
|
| 2589 |
for ( y = 0; y < lm->sh; y++ ) |
| 2590 |
{ |
| 2591 |
for ( x = 0; x < lm->sw; x++ ) |
| 2592 |
{ |
| 2593 |
|
| 2594 |
cluster = SUPER_CLUSTER( x, y ); |
| 2595 |
|
| 2596 |
|
| 2597 |
|
| 2598 |
|
| 2599 |
luxel = SUPER_LUXEL( lightmapNum, x, y ); |
| 2600 |
normal = SUPER_NORMAL( x, y ); |
| 2601 |
|
| 2602 |
luxel[0] = ( normal[0] * 127 ) + 127; |
| 2603 |
luxel[1] = ( normal[1] * 127 ) + 127; |
| 2604 |
luxel[2] = ( normal[2] * 127 ) + 127; |
| 2605 |
} |
| 2606 |
} |
| 2607 |
} |
| 2608 |
} |
| 2609 |
|
| 2610 |
|
| 2611 |
|
| 2612 |
|
| 2613 |
|
| 2614 |
if ( dirty ) { |
| 2615 |
|
| 2616 |
for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ ) |
| 2617 |
{ |
| 2618 |
|
| 2619 |
if ( lm->superLuxels[ lightmapNum ] == NULL ) { |
| 2620 |
continue; |
| 2621 |
} |
| 2622 |
|
| 2623 |
|
| 2624 |
for ( y = 0; y < lm->sh; y++ ) |
| 2625 |
{ |
| 2626 |
for ( x = 0; x < lm->sw; x++ ) |
| 2627 |
{ |
| 2628 |
|
| 2629 |
cluster = SUPER_CLUSTER( x, y ); |
| 2630 |
|
| 2631 |
|
| 2632 |
luxel = SUPER_LUXEL( lightmapNum, x, y ); |
| 2633 |
dirt = SUPER_DIRT( x, y ); |
| 2634 |
|
| 2635 |
|
| 2636 |
VectorScale( luxel, *dirt, luxel ); |
| 2637 |
|
| 2638 |
|
| 2639 |
if ( dirtDebug ) { |
| 2640 |
VectorSet( luxel, *dirt * 255.0f, *dirt * 255.0f, *dirt * 255.0f ); |
| 2641 |
} |
| 2642 |
} |
| 2643 |
} |
| 2644 |
} |
| 2645 |
} |
| 2646 |
|
| 2647 |
|
| 2648 |
|
| 2649 |
|
| 2650 |
|
| 2651 |
|
| 2652 |
for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ ) |
| 2653 |
{ |
| 2654 |
|
| 2655 |
if ( lm->superLuxels[ lightmapNum ] == NULL ) { |
| 2656 |
continue; |
| 2657 |
} |
| 2658 |
|
| 2659 |
|
| 2660 |
for ( y = 0; y < lm->sh; y++ ) |
| 2661 |
{ |
| 2662 |
for ( x = 0; x < lm->sw; x++ ) |
| 2663 |
{ |
| 2664 |
|
| 2665 |
cluster = SUPER_CLUSTER( x, y ); |
| 2666 |
luxel = SUPER_LUXEL( lightmapNum, x, y ); |
| 2667 |
deluxel = SUPER_DELUXEL( x, y ); |
| 2668 |
normal = SUPER_NORMAL( x, y ); |
| 2669 |
|
| 2670 |
|
| 2671 |
filterColor = qfalse; |
| 2672 |
filterDir = qfalse; |
| 2673 |
if ( *cluster < 0 || |
| 2674 |
( lm->splotchFix && ( luxel[ 0 ] <= ambientColor[ 0 ] || luxel[ 1 ] <= ambientColor[ 1 ] || luxel[ 2 ] <= ambientColor[ 2 ] ) ) ) { |
| 2675 |
filterColor = qtrue; |
| 2676 |
} |
| 2677 |
|
| 2678 |
if ( deluxemap && lightmapNum == 0 && ( *cluster < 0 || filter ) ) { |
| 2679 |
filterDir = qtrue; |
| 2680 |
} |
| 2681 |
|
| 2682 |
if ( !filterColor && !filterDir ) { |
| 2683 |
continue; |
| 2684 |
} |
| 2685 |
|
| 2686 |
|
| 2687 |
VectorClear( averageColor ); |
| 2688 |
VectorClear( averageDir ); |
| 2689 |
samples = 0.0f; |
| 2690 |
|
| 2691 |
|
| 2692 |
for ( sy = ( y - 1 ); sy <= ( y + 1 ); sy++ ) |
| 2693 |
{ |
| 2694 |
if ( sy < 0 || sy >= lm->sh ) { |
| 2695 |
continue; |
| 2696 |
} |
| 2697 |
|
| 2698 |
for ( sx = ( x - 1 ); sx <= ( x + 1 ); sx++ ) |
| 2699 |
{ |
| 2700 |
if ( sx < 0 || sx >= lm->sw || ( sx == x && sy == y ) ) { |
| 2701 |
continue; |
| 2702 |
} |
| 2703 |
|
| 2704 |
|
| 2705 |
cluster2 = SUPER_CLUSTER( sx, sy ); |
| 2706 |
luxel2 = SUPER_LUXEL( lightmapNum, sx, sy ); |
| 2707 |
deluxel2 = SUPER_DELUXEL( sx, sy ); |
| 2708 |
|
| 2709 |
|
| 2710 |
if ( *cluster2 < 0 || luxel2[ 3 ] == 0.0f || |
| 2711 |
( lm->splotchFix && VectorCompare( luxel2, ambientColor ) ) ) { |
| 2712 |
continue; |
| 2713 |
} |
| 2714 |
|
| 2715 |
|
| 2716 |
VectorAdd( averageColor, luxel2, averageColor ); |
| 2717 |
samples += luxel2[ 3 ]; |
| 2718 |
if ( filterDir ) { |
| 2719 |
VectorAdd( averageDir, deluxel2, averageDir ); |
| 2720 |
} |
| 2721 |
} |
| 2722 |
} |
| 2723 |
|
| 2724 |
|
| 2725 |
if ( samples <= 0.0f ) { |
| 2726 |
continue; |
| 2727 |
} |
| 2728 |
|
| 2729 |
|
| 2730 |
if ( dark ) { |
| 2731 |
if ( lightmapNum == 0 ) { |
| 2732 |
VectorMA( averageColor, 2.0f, ambientColor, averageColor ); |
| 2733 |
} |
| 2734 |
samples += 2.0f; |
| 2735 |
} |
| 2736 |
|
| 2737 |
|
| 2738 |
if ( filterColor ) { |
| 2739 |
VectorDivide( averageColor, samples, luxel ); |
| 2740 |
luxel[ 3 ] = 1.0f; |
| 2741 |
} |
| 2742 |
if ( filterDir ) { |
| 2743 |
VectorDivide( averageDir, samples, deluxel ); |
| 2744 |
} |
| 2745 |
|
| 2746 |
|
| 2747 |
if ( *cluster < 0 ) { |
| 2748 |
*cluster = CLUSTER_FLOODED; |
| 2749 |
} |
| 2750 |
} |
| 2751 |
} |
| 2752 |
} |
| 2753 |
} |
| 2754 |
|
| 2755 |
|
| 2756 |
|
| 2757 |
|
| 2758 |
|
| 2759 |
|
| 2760 |
|
| 2761 |
|
| 2762 |
#define VERTEX_NUDGE 4.0f |
| 2763 |
|
| 2764 |
void IlluminateVertexes( int num ){ |
| 2765 |
int i, x, y, z, x1, y1, z1, sx, sy, radius, maxRadius, *cluster; |
| 2766 |
int lightmapNum, numAvg; |
| 2767 |
float samples, *vertLuxel, *radVertLuxel, *luxel, dirt; |
| 2768 |
vec3_t temp, temp2, colors[ MAX_LIGHTMAPS ], avgColors[ MAX_LIGHTMAPS ]; |
| 2769 |
bspDrawSurface_t *ds; |
| 2770 |
surfaceInfo_t *info; |
| 2771 |
rawLightmap_t *lm; |
| 2772 |
bspDrawVert_t *verts; |
| 2773 |
trace_t trace; |
| 2774 |
float floodLightAmount; |
| 2775 |
vec3_t floodColor; |
| 2776 |
|
| 2777 |
|
| 2778 |
|
| 2779 |
ds = &bspDrawSurfaces[ num ]; |
| 2780 |
info = &surfaceInfos[ num ]; |
| 2781 |
lm = info->lm; |
| 2782 |
|
| 2783 |
|
| 2784 |
|
| 2785 |
|
| 2786 |
|
| 2787 |
|
| 2788 |
if ( lm == NULL || cpmaHack ) { |
| 2789 |
|
| 2790 |
trace.testOcclusion = ( cpmaHack && lm != NULL ) ? qfalse : !noTrace; |
| 2791 |
trace.forceSunlight = info->si->forceSunlight; |
| 2792 |
trace.recvShadows = info->recvShadows; |
| 2793 |
trace.numSurfaces = 1; |
| 2794 |
trace.surfaces = # |
| 2795 |
trace.inhibitRadius = DEFAULT_INHIBIT_RADIUS; |
| 2796 |
|
| 2797 |
|
| 2798 |
trace.twoSided = info->si->twoSided; |
| 2799 |
|
| 2800 |
|
| 2801 |
CreateTraceLightsForSurface( num, &trace ); |
| 2802 |
|
| 2803 |
|
| 2804 |
verts = yDrawVerts + ds->firstVert; |
| 2805 |
numAvg = 0; |
| 2806 |
memset( avgColors, 0, sizeof( avgColors ) ); |
| 2807 |
|
| 2808 |
|
| 2809 |
for ( i = 0; i < ds->numVerts; i++ ) |
| 2810 |
{ |
| 2811 |
|
| 2812 |
radVertLuxel = RAD_VERTEX_LUXEL( 0, ds->firstVert + i ); |
| 2813 |
|
| 2814 |
|
| 2815 |
if ( debugSurfaces ) { |
| 2816 |
VectorCopy( debugColors[ num % 12 ], radVertLuxel ); |
| 2817 |
} |
| 2818 |
|
| 2819 |
|
| 2820 |
else if ( debugOrigin ) { |
| 2821 |
VectorSubtract( info->maxs, info->mins, temp ); |
| 2822 |
VectorScale( temp, ( 1.0f / 255.0f ), temp ); |
| 2823 |
VectorSubtract( verts[ i ].xyz, info->mins, temp2 ); |
| 2824 |
radVertLuxel[ 0 ] = info->mins[ 0 ] + ( temp[ 0 ] * temp2[ 0 ] ); |
| 2825 |
radVertLuxel[ 1 ] = info->mins[ 1 ] + ( temp[ 1 ] * temp2[ 1 ] ); |
| 2826 |
radVertLuxel[ 2 ] = info->mins[ 2 ] + ( temp[ 2 ] * temp2[ 2 ] ); |
| 2827 |
} |
| 2828 |
|
| 2829 |
|
| 2830 |
else if ( normalmap ) { |
| 2831 |
radVertLuxel[ 0 ] = ( verts[ i ].normal[ 0 ] + 1.0f ) * 127.5f; |
| 2832 |
radVertLuxel[ 1 ] = ( verts[ i ].normal[ 1 ] + 1.0f ) * 127.5f; |
| 2833 |
radVertLuxel[ 2 ] = ( verts[ i ].normal[ 2 ] + 1.0f ) * 127.5f; |
| 2834 |
} |
| 2835 |
|
| 2836 |
else if ( info->si->noVertexLight ) { |
| 2837 |
VectorSet( radVertLuxel, 127.5f, 127.5f, 127.5f ); |
| 2838 |
} |
| 2839 |
|
| 2840 |
else if ( noVertexLighting > 0 ) { |
| 2841 |
VectorSet( radVertLuxel, 127.5f * noVertexLighting, 127.5f * noVertexLighting, 127.5f * noVertexLighting ); |
| 2842 |
} |
| 2843 |
|
| 2844 |
|
| 2845 |
else |
| 2846 |
{ |
| 2847 |
|
| 2848 |
VectorSet( radVertLuxel, -1.0f, -1.0f, -1.0f ); |
| 2849 |
|
| 2850 |
|
| 2851 |
trace.cluster = ClusterForPointExtFilter( verts[ i ].xyz, VERTEX_EPSILON, info->numSurfaceClusters, &surfaceClusters[ info->firstSurfaceCluster ] ); |
| 2852 |
if ( trace.cluster >= 0 ) { |
| 2853 |
|
| 2854 |
VectorCopy( verts[ i ].xyz, trace.origin ); |
| 2855 |
VectorCopy( verts[ i ].normal, trace.normal ); |
| 2856 |
|
| 2857 |
|
| 2858 |
if ( dirty && !bouncing ) { |
| 2859 |
dirt = DirtForSample( &trace ); |
| 2860 |
} |
| 2861 |
else{ |
| 2862 |
dirt = 1.0f; |
| 2863 |
} |
| 2864 |
|
| 2865 |
|
| 2866 |
floodLightAmount = 0.0f; |
| 2867 |
VectorClear( floodColor ); |
| 2868 |
if ( floodlighty && !bouncing ) { |
| 2869 |
floodLightAmount = floodlightIntensity * FloodLightForSample( &trace, floodlightDistance, floodlight_lowquality ); |
| 2870 |
VectorScale( floodlightRGB, floodLightAmount, floodColor ); |
| 2871 |
} |
| 2872 |
|
| 2873 |
|
| 2874 |
LightingAtSample( &trace, ds->vertexStyles, colors ); |
| 2875 |
|
| 2876 |
|
| 2877 |
for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ ) |
| 2878 |
{ |
| 2879 |
|
| 2880 |
VectorScale( colors[ lightmapNum ], dirt, colors[ lightmapNum ] ); |
| 2881 |
|
| 2882 |
|
| 2883 |
VectorAdd( colors[ lightmapNum ], floodColor, colors[ lightmapNum ] ); |
| 2884 |
|
| 2885 |
|
| 2886 |
radVertLuxel = RAD_VERTEX_LUXEL( lightmapNum, ds->firstVert + i ); |
| 2887 |
VectorCopy( colors[ lightmapNum ], radVertLuxel ); |
| 2888 |
VectorAdd( avgColors[ lightmapNum ], colors[ lightmapNum ], colors[ lightmapNum ] ); |
| 2889 |
} |
| 2890 |
} |
| 2891 |
|
| 2892 |
|
| 2893 |
radVertLuxel = RAD_VERTEX_LUXEL( 0, ds->firstVert + i ); |
| 2894 |
if ( radVertLuxel[ 0 ] <= ambientColor[ 0 ] && |
| 2895 |
radVertLuxel[ 1 ] <= ambientColor[ 1 ] && |
| 2896 |
radVertLuxel[ 2 ] <= ambientColor[ 2 ] ) { |
| 2897 |
|
| 2898 |
for ( x = 0; x < 5; x++ ) |
| 2899 |
{ |
| 2900 |
|
| 2901 |
x1 = ( ( x >> 1 ) ^ ( x & 1 ? -1 : 0 ) ) + ( x & 1 ); |
| 2902 |
|
| 2903 |
for ( y = 0; y < 5; y++ ) |
| 2904 |
{ |
| 2905 |
y1 = ( ( y >> 1 ) ^ ( y & 1 ? -1 : 0 ) ) + ( y & 1 ); |
| 2906 |
|
| 2907 |
for ( z = 0; z < 5; z++ ) |
| 2908 |
{ |
| 2909 |
z1 = ( ( z >> 1 ) ^ ( z & 1 ? -1 : 0 ) ) + ( z & 1 ); |
| 2910 |
|
| 2911 |
|
| 2912 |
trace.origin[ 0 ] = verts[ i ].xyz[ 0 ] + ( VERTEX_NUDGE * x1 ); |
| 2913 |
trace.origin[ 1 ] = verts[ i ].xyz[ 1 ] + ( VERTEX_NUDGE * y1 ); |
| 2914 |
trace.origin[ 2 ] = verts[ i ].xyz[ 2 ] + ( VERTEX_NUDGE * z1 ); |
| 2915 |
|
| 2916 |
|
| 2917 |
trace.cluster = ClusterForPointExtFilter( trace.origin, VERTEX_EPSILON, info->numSurfaceClusters, &surfaceClusters[ info->firstSurfaceCluster ] ); |
| 2918 |
if ( trace.cluster < 0 ) { |
| 2919 |
continue; |
| 2920 |
} |
| 2921 |
|
| 2922 |
|
| 2923 |
if ( dirty && !bouncing ) { |
| 2924 |
dirt = DirtForSample( &trace ); |
| 2925 |
} |
| 2926 |
else{ |
| 2927 |
dirt = 1.0f; |
| 2928 |
} |
| 2929 |
|
| 2930 |
|
| 2931 |
floodLightAmount = 0.0f; |
| 2932 |
VectorClear( floodColor ); |
| 2933 |
if ( floodlighty && !bouncing ) { |
| 2934 |
floodLightAmount = floodlightIntensity * FloodLightForSample( &trace, floodlightDistance, floodlight_lowquality ); |
| 2935 |
VectorScale( floodlightRGB, floodLightAmount, floodColor ); |
| 2936 |
} |
| 2937 |
|
| 2938 |
|
| 2939 |
LightingAtSample( &trace, ds->vertexStyles, colors ); |
| 2940 |
|
| 2941 |
|
| 2942 |
for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ ) |
| 2943 |
{ |
| 2944 |
|
| 2945 |
VectorScale( colors[ lightmapNum ], dirt, colors[ lightmapNum ] ); |
| 2946 |
|
| 2947 |
|
| 2948 |
VectorAdd( colors[ lightmapNum ], floodColor, colors[ lightmapNum ] ); |
| 2949 |
|
| 2950 |
|
| 2951 |
radVertLuxel = RAD_VERTEX_LUXEL( lightmapNum, ds->firstVert + i ); |
| 2952 |
VectorCopy( colors[ lightmapNum ], radVertLuxel ); |
| 2953 |
} |
| 2954 |
|
| 2955 |
|
| 2956 |
radVertLuxel = RAD_VERTEX_LUXEL( 0, ds->firstVert + i ); |
| 2957 |
if ( radVertLuxel[ 0 ] > ambientColor[ 0 ] || |
| 2958 |
radVertLuxel[ 1 ] > ambientColor[ 1 ] || |
| 2959 |
radVertLuxel[ 2 ] > ambientColor[ 2 ] ) { |
| 2960 |
x = y = z = 1000; |
| 2961 |
} |
| 2962 |
} |
| 2963 |
} |
| 2964 |
} |
| 2965 |
} |
| 2966 |
|
| 2967 |
|
| 2968 |
radVertLuxel = RAD_VERTEX_LUXEL( 0, ds->firstVert + i ); |
| 2969 |
if ( radVertLuxel[ 0 ] > ambientColor[ 0 ] || |
| 2970 |
radVertLuxel[ 1 ] > ambientColor[ 1 ] || |
| 2971 |
radVertLuxel[ 2 ] > ambientColor[ 2 ] ) { |
| 2972 |
numAvg++; |
| 2973 |
for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ ) |
| 2974 |
{ |
| 2975 |
radVertLuxel = RAD_VERTEX_LUXEL( lightmapNum, ds->firstVert + i ); |
| 2976 |
VectorAdd( avgColors[ lightmapNum ], radVertLuxel, avgColors[ lightmapNum ] ); |
| 2977 |
} |
| 2978 |
} |
| 2979 |
} |
| 2980 |
|
| 2981 |
|
| 2982 |
numVertsIlluminated++; |
| 2983 |
} |
| 2984 |
|
| 2985 |
|
| 2986 |
if ( numAvg > 0 ) { |
| 2987 |
for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ ) |
| 2988 |
VectorScale( avgColors[ lightmapNum ], ( 1.0f / numAvg ), avgColors[ lightmapNum ] ); |
| 2989 |
} |
| 2990 |
else |
| 2991 |
{ |
| 2992 |
VectorCopy( ambientColor, avgColors[ 0 ] ); |
| 2993 |
} |
| 2994 |
|
| 2995 |
|
| 2996 |
for ( i = 0; i < ds->numVerts; i++ ) |
| 2997 |
{ |
| 2998 |
|
| 2999 |
radVertLuxel = RAD_VERTEX_LUXEL( 0, ds->firstVert + i ); |
| 3000 |
|
| 3001 |
|
| 3002 |
if ( radVertLuxel[ 0 ] < 0.0f ) { |
| 3003 |
for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ ) |
| 3004 |
{ |
| 3005 |
radVertLuxel = RAD_VERTEX_LUXEL( lightmapNum, ds->firstVert + i ); |
| 3006 |
VectorCopy( avgColors[ lightmapNum ], radVertLuxel ); |
| 3007 |
|
| 3008 |
|
| 3009 |
|
| 3010 |
} |
| 3011 |
} |
| 3012 |
|
| 3013 |
|
| 3014 |
for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ ) |
| 3015 |
{ |
| 3016 |
|
| 3017 |
vertLuxel = VERTEX_LUXEL( lightmapNum, ds->firstVert + i ); |
| 3018 |
radVertLuxel = RAD_VERTEX_LUXEL( lightmapNum, ds->firstVert + i ); |
| 3019 |
|
| 3020 |
|
| 3021 |
if ( bouncing || bounce == 0 || !bounceOnly ) { |
| 3022 |
VectorAdd( vertLuxel, radVertLuxel, vertLuxel ); |
| 3023 |
} |
| 3024 |
if ( !info->si->noVertexLight ) { |
| 3025 |
ColorToBytes( vertLuxel, verts[ i ].color[ lightmapNum ], info->si->vertexScale ); |
| 3026 |
} |
| 3027 |
} |
| 3028 |
} |
| 3029 |
|
| 3030 |
|
| 3031 |
FreeTraceLights( &trace ); |
| 3032 |
|
| 3033 |
|
| 3034 |
return; |
| 3035 |
} |
| 3036 |
|
| 3037 |
|
| 3038 |
|
| 3039 |
|
| 3040 |
|
| 3041 |
|
| 3042 |
for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ ) |
| 3043 |
ds->vertexStyles[ lightmapNum ] = lm->styles[ lightmapNum ]; |
| 3044 |
|
| 3045 |
|
| 3046 |
maxRadius = lm->sw; |
| 3047 |
maxRadius = maxRadius > lm->sh ? maxRadius : lm->sh; |
| 3048 |
|
| 3049 |
|
| 3050 |
verts = yDrawVerts + ds->firstVert; |
| 3051 |
for ( i = 0; i < ds->numVerts; i++ ) |
| 3052 |
{ |
| 3053 |
|
| 3054 |
for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ ) |
| 3055 |
{ |
| 3056 |
|
| 3057 |
if ( lm->superLuxels[ lightmapNum ] == NULL ) { |
| 3058 |
continue; |
| 3059 |
} |
| 3060 |
|
| 3061 |
|
| 3062 |
x = verts[ i ].lightmap[ lightmapNum ][ 0 ]; |
| 3063 |
y = verts[ i ].lightmap[ lightmapNum ][ 1 ]; |
| 3064 |
if ( x < 0 ) { |
| 3065 |
x = 0; |
| 3066 |
} |
| 3067 |
else if ( x >= lm->sw ) { |
| 3068 |
x = lm->sw - 1; |
| 3069 |
} |
| 3070 |
if ( y < 0 ) { |
| 3071 |
y = 0; |
| 3072 |
} |
| 3073 |
else if ( y >= lm->sh ) { |
| 3074 |
y = lm->sh - 1; |
| 3075 |
} |
| 3076 |
|
| 3077 |
|
| 3078 |
vertLuxel = VERTEX_LUXEL( lightmapNum, ds->firstVert + i ); |
| 3079 |
radVertLuxel = RAD_VERTEX_LUXEL( lightmapNum, ds->firstVert + i ); |
| 3080 |
|
| 3081 |
|
| 3082 |
if ( normalmap ) { |
| 3083 |
radVertLuxel[ 0 ] = ( verts[ i ].normal[ 0 ] + 1.0f ) * 127.5f; |
| 3084 |
radVertLuxel[ 1 ] = ( verts[ i ].normal[ 1 ] + 1.0f ) * 127.5f; |
| 3085 |
radVertLuxel[ 2 ] = ( verts[ i ].normal[ 2 ] + 1.0f ) * 127.5f; |
| 3086 |
} |
| 3087 |
|
| 3088 |
|
| 3089 |
else if ( debugSurfaces ) { |
| 3090 |
VectorCopy( debugColors[ num % 12 ], radVertLuxel ); |
| 3091 |
} |
| 3092 |
|
| 3093 |
else if ( info->si->noVertexLight ) { |
| 3094 |
VectorSet( radVertLuxel, 127.5f, 127.5f, 127.5f ); |
| 3095 |
} |
| 3096 |
|
| 3097 |
else if ( noVertexLighting > 0 ) { |
| 3098 |
VectorSet( radVertLuxel, 127.5f * noVertexLighting, 127.5f * noVertexLighting, 127.5f * noVertexLighting ); |
| 3099 |
} |
| 3100 |
|
| 3101 |
|
| 3102 |
else |
| 3103 |
{ |
| 3104 |
|
| 3105 |
VectorClear( radVertLuxel ); |
| 3106 |
samples = 0.0f; |
| 3107 |
for ( radius = 0; radius < maxRadius && samples <= 0.0f; radius++ ) |
| 3108 |
{ |
| 3109 |
|
| 3110 |
for ( sy = ( y - radius ); sy <= ( y + radius ); sy++ ) |
| 3111 |
{ |
| 3112 |
if ( sy < 0 || sy >= lm->sh ) { |
| 3113 |
continue; |
| 3114 |
} |
| 3115 |
|
| 3116 |
for ( sx = ( x - radius ); sx <= ( x + radius ); sx++ ) |
| 3117 |
{ |
| 3118 |
if ( sx < 0 || sx >= lm->sw ) { |
| 3119 |
continue; |
| 3120 |
} |
| 3121 |
|
| 3122 |
|
| 3123 |
luxel = SUPER_LUXEL( lightmapNum, sx, sy ); |
| 3124 |
cluster = SUPER_CLUSTER( sx, sy ); |
| 3125 |
if ( *cluster < 0 ) { |
| 3126 |
continue; |
| 3127 |
} |
| 3128 |
|
| 3129 |
|
| 3130 |
|
| 3131 |
|
| 3132 |
|
| 3133 |
|
| 3134 |
VectorAdd( radVertLuxel, luxel, radVertLuxel ); |
| 3135 |
samples += luxel[ 3 ]; |
| 3136 |
} |
| 3137 |
} |
| 3138 |
} |
| 3139 |
|
| 3140 |
|
| 3141 |
if ( samples > 0.0f ) { |
| 3142 |
VectorDivide( radVertLuxel, samples, radVertLuxel ); |
| 3143 |
} |
| 3144 |
else{ |
| 3145 |
VectorCopy( ambientColor, radVertLuxel ); |
| 3146 |
} |
| 3147 |
} |
| 3148 |
|
| 3149 |
|
| 3150 |
VectorAdd( vertLuxel, radVertLuxel, vertLuxel ); |
| 3151 |
numVertsIlluminated++; |
| 3152 |
|
| 3153 |
|
| 3154 |
if ( !info->si->noVertexLight ) { |
| 3155 |
ColorToBytes( vertLuxel, verts[ i ].color[ lightmapNum ], 1.0f ); |
| 3156 |
} |
| 3157 |
} |
| 3158 |
} |
| 3159 |
} |
| 3160 |
|
| 3161 |
|
| 3162 |
|
| 3163 |
|
| 3164 |
|
| 3165 |
|
| 3166 |
|
| 3167 |
|
| 3168 |
|
| 3169 |
|
| 3170 |
|
| 3171 |
|
| 3172 |
|
| 3173 |
|
| 3174 |
|
| 3175 |
|
| 3176 |
|
| 3177 |
|
| 3178 |
void SetupBrushesFlags( unsigned int mask_any, unsigned int test_any, unsigned int mask_all, unsigned int test_all ){ |
| 3179 |
int i, j, b; |
| 3180 |
unsigned int compileFlags, allCompileFlags; |
| 3181 |
bspBrush_t *brush; |
| 3182 |
bspBrushSide_t *side; |
| 3183 |
bspShader_t *shader; |
| 3184 |
shaderInfo_t *si; |
| 3185 |
|
| 3186 |
|
| 3187 |
|
| 3188 |
Sys_FPrintf( SYS_VRB, "--- SetupBrushes ---\n" ); |
| 3189 |
|
| 3190 |
|
| 3191 |
if ( opaqueBrushes == NULL ) { |
| 3192 |
opaqueBrushes = safe_malloc( numBSPBrushes / 8 + 1 ); |
| 3193 |
} |
| 3194 |
|
| 3195 |
|
| 3196 |
memset( opaqueBrushes, 0, numBSPBrushes / 8 + 1 ); |
| 3197 |
numOpaqueBrushes = 0; |
| 3198 |
|
| 3199 |
|
| 3200 |
for ( i = 0; i < bspModels[ 0 ].numBSPBrushes; i++ ) |
| 3201 |
{ |
| 3202 |
|
| 3203 |
b = bspModels[ 0 ].firstBSPBrush + i; |
| 3204 |
brush = &bspBrushes[ b ]; |
| 3205 |
|
| 3206 |
|
| 3207 |
compileFlags = 0; |
| 3208 |
allCompileFlags = ~( 0u ); |
| 3209 |
for ( j = 0; j < brush->numSides; j++ ) |
| 3210 |
{ |
| 3211 |
|
| 3212 |
side = &bspBrushSides[ brush->firstSide + j ]; |
| 3213 |
shader = &bspShaders[ side->shaderNum ]; |
| 3214 |
|
| 3215 |
|
| 3216 |
si = ShaderInfoForShaderNull( shader->shader ); |
| 3217 |
if ( si == NULL ) { |
| 3218 |
continue; |
| 3219 |
} |
| 3220 |
|
| 3221 |
|
| 3222 |
compileFlags |= si->compileFlags; |
| 3223 |
allCompileFlags &= si->compileFlags; |
| 3224 |
} |
| 3225 |
|
| 3226 |
|
| 3227 |
if ( ( compileFlags & mask_any ) == test_any && ( allCompileFlags & mask_all ) == test_all ) { |
| 3228 |
opaqueBrushes[ b >> 3 ] |= ( 1 << ( b & 7 ) ); |
| 3229 |
numOpaqueBrushes++; |
| 3230 |
maxOpaqueBrush = i; |
| 3231 |
} |
| 3232 |
} |
| 3233 |
|
| 3234 |
|
| 3235 |
Sys_FPrintf( SYS_VRB, "%9d opaque brushes\n", numOpaqueBrushes ); |
| 3236 |
} |
| 3237 |
void SetupBrushes( void ){ |
| 3238 |
SetupBrushesFlags( C_TRANSLUCENT, 0, 0, 0 ); |
| 3239 |
} |
| 3240 |
|
| 3241 |
|
| 3242 |
|
| 3243 |
|
| 3244 |
|
| 3245 |
|
| 3246 |
|
| 3247 |
|
| 3248 |
qboolean ClusterVisible( int a, int b ){ |
| 3249 |
int leafBytes; |
| 3250 |
byte *pvs; |
| 3251 |
|
| 3252 |
|
| 3253 |
|
| 3254 |
if ( a < 0 || b < 0 ) { |
| 3255 |
return qfalse; |
| 3256 |
} |
| 3257 |
|
| 3258 |
|
| 3259 |
if ( a == b ) { |
| 3260 |
return qtrue; |
| 3261 |
} |
| 3262 |
|
| 3263 |
|
| 3264 |
if ( numBSPVisBytes <= 8 ) { |
| 3265 |
return qtrue; |
| 3266 |
} |
| 3267 |
|
| 3268 |
|
| 3269 |
|
| 3270 |
leafBytes = ( (int*) bspVisBytes )[ 1 ]; |
| 3271 |
pvs = bspVisBytes + VIS_HEADER_SIZE + ( a * leafBytes ); |
| 3272 |
|
| 3273 |
|
| 3274 |
if ( ( pvs[ b >> 3 ] & ( 1 << ( b & 7 ) ) ) ) { |
| 3275 |
return qtrue; |
| 3276 |
} |
| 3277 |
return qfalse; |
| 3278 |
} |
| 3279 |
|
| 3280 |
|
| 3281 |
|
| 3282 |
|
| 3283 |
|
| 3284 |
|
| 3285 |
|
| 3286 |
|
| 3287 |
int PointInLeafNum_r( vec3_t point, int nodenum ){ |
| 3288 |
int leafnum; |
| 3289 |
vec_t dist; |
| 3290 |
bspNode_t *node; |
| 3291 |
bspPlane_t *plane; |
| 3292 |
|
| 3293 |
|
| 3294 |
while ( nodenum >= 0 ) |
| 3295 |
{ |
| 3296 |
node = &bspNodes[ nodenum ]; |
| 3297 |
plane = &bspPlanes[ node->planeNum ]; |
| 3298 |
dist = DotProduct( point, plane->normal ) - plane->dist; |
| 3299 |
if ( dist > 0.1 ) { |
| 3300 |
nodenum = node->children[ 0 ]; |
| 3301 |
} |
| 3302 |
else if ( dist < -0.1 ) { |
| 3303 |
nodenum = node->children[ 1 ]; |
| 3304 |
} |
| 3305 |
else |
| 3306 |
{ |
| 3307 |
leafnum = PointInLeafNum_r( point, node->children[ 0 ] ); |
| 3308 |
if ( bspLeafs[ leafnum ].cluster != -1 ) { |
| 3309 |
return leafnum; |
| 3310 |
} |
| 3311 |
nodenum = node->children[ 1 ]; |
| 3312 |
} |
| 3313 |
} |
| 3314 |
|
| 3315 |
leafnum = -nodenum - 1; |
| 3316 |
return leafnum; |
| 3317 |
} |
| 3318 |
|
| 3319 |
|
| 3320 |
|
| 3321 |
|
| 3322 |
|
| 3323 |
|
| 3324 |
|
| 3325 |
|
| 3326 |
int PointInLeafNum( vec3_t point ){ |
| 3327 |
return PointInLeafNum_r( point, 0 ); |
| 3328 |
} |
| 3329 |
|
| 3330 |
|
| 3331 |
|
| 3332 |
|
| 3333 |
|
| 3334 |
|
| 3335 |
|
| 3336 |
|
| 3337 |
qboolean ClusterVisibleToPoint( vec3_t point, int cluster ){ |
| 3338 |
int pointCluster; |
| 3339 |
|
| 3340 |
|
| 3341 |
|
| 3342 |
pointCluster = ClusterForPoint( point ); |
| 3343 |
if ( pointCluster < 0 ) { |
| 3344 |
return qfalse; |
| 3345 |
} |
| 3346 |
|
| 3347 |
|
| 3348 |
return ClusterVisible( pointCluster, cluster ); |
| 3349 |
} |
| 3350 |
|
| 3351 |
|
| 3352 |
|
| 3353 |
|
| 3354 |
|
| 3355 |
|
| 3356 |
|
| 3357 |
|
| 3358 |
int ClusterForPoint( vec3_t point ){ |
| 3359 |
int leafNum; |
| 3360 |
|
| 3361 |
|
| 3362 |
|
| 3363 |
leafNum = PointInLeafNum( point ); |
| 3364 |
if ( leafNum < 0 ) { |
| 3365 |
return -1; |
| 3366 |
} |
| 3367 |
|
| 3368 |
|
| 3369 |
return bspLeafs[ leafNum ].cluster; |
| 3370 |
} |
| 3371 |
|
| 3372 |
|
| 3373 |
|
| 3374 |
|
| 3375 |
|
| 3376 |
|
| 3377 |
|
| 3378 |
|
| 3379 |
int ClusterForPointExt( vec3_t point, float epsilon ){ |
| 3380 |
int i, j, b, leafNum, cluster; |
| 3381 |
float dot; |
| 3382 |
qboolean inside; |
| 3383 |
int *brushes, numBSPBrushes; |
| 3384 |
bspLeaf_t *leaf; |
| 3385 |
bspBrush_t *brush; |
| 3386 |
bspPlane_t *plane; |
| 3387 |
|
| 3388 |
|
| 3389 |
|
| 3390 |
leafNum = PointInLeafNum( point ); |
| 3391 |
if ( leafNum < 0 ) { |
| 3392 |
return -1; |
| 3393 |
} |
| 3394 |
leaf = &bspLeafs[ leafNum ]; |
| 3395 |
|
| 3396 |
|
| 3397 |
cluster = leaf->cluster; |
| 3398 |
if ( cluster < 0 ) { |
| 3399 |
return -1; |
| 3400 |
} |
| 3401 |
|
| 3402 |
|
| 3403 |
brushes = &bspLeafBrushes[ leaf->firstBSPLeafBrush ]; |
| 3404 |
numBSPBrushes = leaf->numBSPLeafBrushes; |
| 3405 |
for ( i = 0; i < numBSPBrushes; i++ ) |
| 3406 |
{ |
| 3407 |
|
| 3408 |
b = brushes[ i ]; |
| 3409 |
if ( b > maxOpaqueBrush ) { |
| 3410 |
continue; |
| 3411 |
} |
| 3412 |
brush = &bspBrushes[ b ]; |
| 3413 |
if ( !( opaqueBrushes[ b >> 3 ] & ( 1 << ( b & 7 ) ) ) ) { |
| 3414 |
continue; |
| 3415 |
} |
| 3416 |
|
| 3417 |
|
| 3418 |
inside = qtrue; |
| 3419 |
for ( j = 0; j < brush->numSides && inside; j++ ) |
| 3420 |
{ |
| 3421 |
plane = &bspPlanes[ bspBrushSides[ brush->firstSide + j ].planeNum ]; |
| 3422 |
dot = DotProduct( point, plane->normal ); |
| 3423 |
dot -= plane->dist; |
| 3424 |
if ( dot > epsilon ) { |
| 3425 |
inside = qfalse; |
| 3426 |
} |
| 3427 |
} |
| 3428 |
|
| 3429 |
|
| 3430 |
if ( inside ) { |
| 3431 |
return -1 - b; |
| 3432 |
} |
| 3433 |
} |
| 3434 |
|
| 3435 |
|
| 3436 |
return cluster; |
| 3437 |
} |
| 3438 |
|
| 3439 |
|
| 3440 |
|
| 3441 |
|
| 3442 |
|
| 3443 |
|
| 3444 |
|
| 3445 |
|
| 3446 |
int ClusterForPointExtFilter( vec3_t point, float epsilon, int numClusters, int *clusters ){ |
| 3447 |
int i, cluster; |
| 3448 |
|
| 3449 |
|
| 3450 |
|
| 3451 |
cluster = ClusterForPointExt( point, epsilon ); |
| 3452 |
|
| 3453 |
|
| 3454 |
if ( cluster < 0 || numClusters <= 0 || clusters == NULL ) { |
| 3455 |
return cluster; |
| 3456 |
} |
| 3457 |
|
| 3458 |
|
| 3459 |
for ( i = 0; i < numClusters; i++ ) |
| 3460 |
{ |
| 3461 |
if ( cluster == clusters[ i ] || ClusterVisible( cluster, clusters[ i ] ) ) { |
| 3462 |
return cluster; |
| 3463 |
} |
| 3464 |
} |
| 3465 |
|
| 3466 |
|
| 3467 |
return -1; |
| 3468 |
} |
| 3469 |
|
| 3470 |
|
| 3471 |
|
| 3472 |
|
| 3473 |
|
| 3474 |
|
| 3475 |
|
| 3476 |
|
| 3477 |
|
| 3478 |
int ShaderForPointInLeaf( vec3_t point, int leafNum, float epsilon, int wantContentFlags, int wantSurfaceFlags, int *contentFlags, int *surfaceFlags ){ |
| 3479 |
int i, j; |
| 3480 |
float dot; |
| 3481 |
qboolean inside; |
| 3482 |
int *brushes, numBSPBrushes; |
| 3483 |
bspLeaf_t *leaf; |
| 3484 |
bspBrush_t *brush; |
| 3485 |
bspBrushSide_t *side; |
| 3486 |
bspPlane_t *plane; |
| 3487 |
bspShader_t *shader; |
| 3488 |
int allSurfaceFlags, allContentFlags; |
| 3489 |
|
| 3490 |
|
| 3491 |
|
| 3492 |
*surfaceFlags = 0; |
| 3493 |
*contentFlags = 0; |
| 3494 |
|
| 3495 |
|
| 3496 |
if ( leafNum < 0 ) { |
| 3497 |
return -1; |
| 3498 |
} |
| 3499 |
leaf = &bspLeafs[ leafNum ]; |
| 3500 |
|
| 3501 |
|
| 3502 |
brushes = &bspLeafBrushes[ leaf->firstBSPLeafBrush ]; |
| 3503 |
numBSPBrushes = leaf->numBSPLeafBrushes; |
| 3504 |
for ( i = 0; i < numBSPBrushes; i++ ) |
| 3505 |
{ |
| 3506 |
|
| 3507 |
brush = &bspBrushes[ brushes[ i ] ]; |
| 3508 |
|
| 3509 |
|
| 3510 |
inside = qtrue; |
| 3511 |
allSurfaceFlags = 0; |
| 3512 |
allContentFlags = 0; |
| 3513 |
for ( j = 0; j < brush->numSides && inside; j++ ) |
| 3514 |
{ |
| 3515 |
side = &bspBrushSides[ brush->firstSide + j ]; |
| 3516 |
plane = &bspPlanes[ side->planeNum ]; |
| 3517 |
dot = DotProduct( point, plane->normal ); |
| 3518 |
dot -= plane->dist; |
| 3519 |
if ( dot > epsilon ) { |
| 3520 |
inside = qfalse; |
| 3521 |
} |
| 3522 |
else |
| 3523 |
{ |
| 3524 |
shader = &bspShaders[ side->shaderNum ]; |
| 3525 |
allSurfaceFlags |= shader->surfaceFlags; |
| 3526 |
allContentFlags |= shader->contentFlags; |
| 3527 |
} |
| 3528 |
} |
| 3529 |
|
| 3530 |
|
| 3531 |
if ( inside ) { |
| 3532 |
|
| 3533 |
if ( wantContentFlags && !( wantContentFlags & allContentFlags ) ) { |
| 3534 |
continue; |
| 3535 |
} |
| 3536 |
if ( wantSurfaceFlags && !( wantSurfaceFlags & allSurfaceFlags ) ) { |
| 3537 |
continue; |
| 3538 |
} |
| 3539 |
|
| 3540 |
|
| 3541 |
*surfaceFlags = allSurfaceFlags; |
| 3542 |
*contentFlags = allContentFlags; |
| 3543 |
return brush->shaderNum; |
| 3544 |
} |
| 3545 |
} |
| 3546 |
|
| 3547 |
|
| 3548 |
return -1; |
| 3549 |
} |
| 3550 |
|
| 3551 |
|
| 3552 |
|
| 3553 |
|
| 3554 |
|
| 3555 |
|
| 3556 |
|
| 3557 |
|
| 3558 |
|
| 3559 |
|
| 3560 |
|
| 3561 |
qboolean ChopBounds( vec3_t mins, vec3_t maxs, vec3_t origin, vec3_t normal ){ |
| 3562 |
|
| 3563 |
return qtrue; |
| 3564 |
} |
| 3565 |
|
| 3566 |
|
| 3567 |
|
| 3568 |
|
| 3569 |
|
| 3570 |
|
| 3571 |
|
| 3572 |
|
| 3573 |
|
| 3574 |
#define LIGHT_EPSILON 0.125f |
| 3575 |
#define LIGHT_NUDGE 2.0f |
| 3576 |
|
| 3577 |
void SetupEnvelopes( qboolean forGrid, qboolean fastFlag ){ |
| 3578 |
int i, x, y, z, x1, y1, z1; |
| 3579 |
light_t *light, *light2, **owner; |
| 3580 |
bspLeaf_t *leaf; |
| 3581 |
vec3_t origin, dir, mins, maxs; |
| 3582 |
float radius, intensity; |
| 3583 |
light_t *buckets[ 256 ]; |
| 3584 |
|
| 3585 |
|
| 3586 |
|
| 3587 |
if ( lights == NULL ) { |
| 3588 |
return; |
| 3589 |
} |
| 3590 |
|
| 3591 |
|
| 3592 |
Sys_FPrintf( SYS_VRB, "--- SetupEnvelopes%s ---\n", fastFlag ? " (fast)" : "" ); |
| 3593 |
|
| 3594 |
|
| 3595 |
numLights = 0; |
| 3596 |
numCulledLights = 0; |
| 3597 |
owner = &lights; |
| 3598 |
while ( *owner != NULL ) |
| 3599 |
{ |
| 3600 |
|
| 3601 |
light = *owner; |
| 3602 |
|
| 3603 |
|
| 3604 |
if ( light->photons < 0.0f || light->add < 0.0f ) { |
| 3605 |
light->photons *= -1.0f; |
| 3606 |
light->add *= -1.0f; |
| 3607 |
light->flags |= LIGHT_NEGATIVE; |
| 3608 |
} |
| 3609 |
|
| 3610 |
|
| 3611 |
if ( light->type == EMIT_SUN ) { |
| 3612 |
|
| 3613 |
light->cluster = 0; |
| 3614 |
light->envelope = MAX_WORLD_COORD * 8.0f; |
| 3615 |
VectorSet( light->mins, MIN_WORLD_COORD * 8.0f, MIN_WORLD_COORD * 8.0f, MIN_WORLD_COORD * 8.0f ); |
| 3616 |
VectorSet( light->maxs, MAX_WORLD_COORD * 8.0f, MAX_WORLD_COORD * 8.0f, MAX_WORLD_COORD * 8.0f ); |
| 3617 |
} |
| 3618 |
|
| 3619 |
|
| 3620 |
else |
| 3621 |
{ |
| 3622 |
|
| 3623 |
light->cluster = ClusterForPointExt( light->origin, LIGHT_EPSILON ); |
| 3624 |
|
| 3625 |
|
| 3626 |
if ( light->cluster < 0 ) { |
| 3627 |
|
| 3628 |
for ( x = 0; x < 4; x++ ) |
| 3629 |
{ |
| 3630 |
|
| 3631 |
x1 = ( ( x >> 1 ) ^ ( x & 1 ? -1 : 0 ) ) + ( x & 1 ); |
| 3632 |
|
| 3633 |
for ( y = 0; y < 4; y++ ) |
| 3634 |
{ |
| 3635 |
y1 = ( ( y >> 1 ) ^ ( y & 1 ? -1 : 0 ) ) + ( y & 1 ); |
| 3636 |
|
| 3637 |
for ( z = 0; z < 4; z++ ) |
| 3638 |
{ |
| 3639 |
z1 = ( ( z >> 1 ) ^ ( z & 1 ? -1 : 0 ) ) + ( z & 1 ); |
| 3640 |
|
| 3641 |
|
| 3642 |
origin[ 0 ] = light->origin[ 0 ] + ( LIGHT_NUDGE * x1 ); |
| 3643 |
origin[ 1 ] = light->origin[ 1 ] + ( LIGHT_NUDGE * y1 ); |
| 3644 |
origin[ 2 ] = light->origin[ 2 ] + ( LIGHT_NUDGE * z1 ); |
| 3645 |
|
| 3646 |
|
| 3647 |
light->cluster = ClusterForPointExt( origin, LIGHT_EPSILON ); |
| 3648 |
if ( light->cluster < 0 ) { |
| 3649 |
continue; |
| 3650 |
} |
| 3651 |
|
| 3652 |
|
| 3653 |
VectorCopy( origin, light->origin ); |
| 3654 |
} |
| 3655 |
} |
| 3656 |
} |
| 3657 |
} |
| 3658 |
|
| 3659 |
|
| 3660 |
if ( light->cluster >= 0 ) { |
| 3661 |
|
| 3662 |
if ( fastFlag ) { |
| 3663 |
light->flags |= LIGHT_FAST_TEMP; |
| 3664 |
} |
| 3665 |
else{ |
| 3666 |
light->flags &= ~LIGHT_FAST_TEMP; |
| 3667 |
} |
| 3668 |
if ( fastpoint && ( light->type != EMIT_AREA ) ) { |
| 3669 |
light->flags |= LIGHT_FAST_TEMP; |
| 3670 |
} |
| 3671 |
if ( light->si ) { |
| 3672 |
if ( light->si->noFast ) { |
| 3673 |
light->flags &= ~( LIGHT_FAST | LIGHT_FAST_TEMP ); |
| 3674 |
} |
| 3675 |
if ( light->si->fast ) { |
| 3676 |
light->flags |= LIGHT_FAST; |
| 3677 |
} |
| 3678 |
} |
| 3679 |
|
| 3680 |
|
| 3681 |
light->envelope = 0; |
| 3682 |
|
| 3683 |
|
| 3684 |
if ( exactPointToPolygon && light->type == EMIT_AREA && light->w != NULL ) { |
| 3685 |
light->envelope = MAX_WORLD_COORD * 8.0f; |
| 3686 |
|
| 3687 |
|
| 3688 |
if ( ( light->flags & LIGHT_FAST ) || ( light->flags & LIGHT_FAST_TEMP ) ) { |
| 3689 |
|
| 3690 |
VectorScale( light->normal, -1.0f, dir ); |
| 3691 |
for ( radius = 100.0f; radius < MAX_WORLD_COORD * 8.0f; radius += 10.0f ) |
| 3692 |
{ |
| 3693 |
float factor; |
| 3694 |
|
| 3695 |
VectorMA( light->origin, radius, light->normal, origin ); |
| 3696 |
factor = PointToPolygonFormFactor( origin, dir, light->w ); |
| 3697 |
if ( factor < 0.0f ) { |
| 3698 |
factor *= -1.0f; |
| 3699 |
} |
| 3700 |
if ( ( factor * light->add ) <= light->falloffTolerance ) { |
| 3701 |
light->envelope = radius; |
| 3702 |
break; |
| 3703 |
} |
| 3704 |
} |
| 3705 |
} |
| 3706 |
|
| 3707 |
intensity = light->photons; |
| 3708 |
} |
| 3709 |
else |
| 3710 |
{ |
| 3711 |
radius = 0.0f; |
| 3712 |
intensity = light->photons; |
| 3713 |
} |
| 3714 |
|
| 3715 |
|
| 3716 |
if ( light->envelope <= 0.0f ) { |
| 3717 |
|
| 3718 |
if ( !( light->flags & LIGHT_ATTEN_DISTANCE ) ) { |
| 3719 |
light->envelope = MAX_WORLD_COORD * 8.0f; |
| 3720 |
} |
| 3721 |
|
| 3722 |
else if ( ( light->flags & LIGHT_FAST ) || ( light->flags & LIGHT_FAST_TEMP ) ) { |
| 3723 |
|
| 3724 |
if ( ( light->flags & LIGHT_ATTEN_LINEAR ) ) { |
| 3725 |
light->envelope = ( ( intensity * linearScale ) - light->falloffTolerance ) / light->fade; |
| 3726 |
} |
| 3727 |
|
| 3728 |
|
| 3729 |
|
| 3730 |
|
| 3731 |
|
| 3732 |
|
| 3733 |
|
| 3734 |
|
| 3735 |
|
| 3736 |
|
| 3737 |
else{ |
| 3738 |
light->envelope = sqrt( intensity / light->falloffTolerance ) + radius; |
| 3739 |
} |
| 3740 |
|
| 3741 |
|
| 3742 |
|
| 3743 |
|
| 3744 |
|
| 3745 |
|
| 3746 |
|
| 3747 |
} |
| 3748 |
else |
| 3749 |
{ |
| 3750 |
|
| 3751 |
if ( ( light->flags & LIGHT_ATTEN_LINEAR ) ) { |
| 3752 |
light->envelope = ( intensity * linearScale ) / light->fade; |
| 3753 |
} |
| 3754 |
|
| 3755 |
|
| 3756 |
else{ |
| 3757 |
light->envelope = MAX_WORLD_COORD * 8.0f; |
| 3758 |
} |
| 3759 |
} |
| 3760 |
} |
| 3761 |
|
| 3762 |
|
| 3763 |
{ |
| 3764 |
|
| 3765 |
ClearBounds( mins, maxs ); |
| 3766 |
|
| 3767 |
|
| 3768 |
for ( i = 0; i < numBSPLeafs; i++ ) |
| 3769 |
{ |
| 3770 |
|
| 3771 |
leaf = &bspLeafs[ i ]; |
| 3772 |
|
| 3773 |
|
| 3774 |
if ( leaf->cluster < 0 ) { |
| 3775 |
continue; |
| 3776 |
} |
| 3777 |
if ( ClusterVisible( light->cluster, leaf->cluster ) == qfalse ) { |
| 3778 |
continue; |
| 3779 |
} |
| 3780 |
|
| 3781 |
|
| 3782 |
VectorCopy( leaf->mins, origin ); |
| 3783 |
AddPointToBounds( origin, mins, maxs ); |
| 3784 |
VectorCopy( leaf->maxs, origin ); |
| 3785 |
AddPointToBounds( origin, mins, maxs ); |
| 3786 |
} |
| 3787 |
|
| 3788 |
|
| 3789 |
for ( i = 0; i < 3; i++ ) |
| 3790 |
{ |
| 3791 |
if ( mins[ i ] > light->origin[ i ] || maxs[ i ] < light->origin[ i ] ) { |
| 3792 |
|
| 3793 |
|
| 3794 |
|
| 3795 |
|
| 3796 |
AddPointToBounds( light->origin, mins, maxs ); |
| 3797 |
} |
| 3798 |
} |
| 3799 |
|
| 3800 |
|
| 3801 |
if ( light->type == EMIT_AREA || light->type == EMIT_SPOT ) { |
| 3802 |
ChopBounds( mins, maxs, light->origin, light->normal ); |
| 3803 |
} |
| 3804 |
|
| 3805 |
|
| 3806 |
VectorCopy( mins, light->mins ); |
| 3807 |
VectorCopy( maxs, light->maxs ); |
| 3808 |
|
| 3809 |
|
| 3810 |
|
| 3811 |
VectorScale( light->origin, 2, origin ); |
| 3812 |
VectorSubtract( origin, maxs, origin ); |
| 3813 |
AddPointToBounds( origin, mins, maxs ); |
| 3814 |
|
| 3815 |
VectorScale( light->origin, 2, origin ); |
| 3816 |
VectorSubtract( origin, mins, origin ); |
| 3817 |
AddPointToBounds( origin, mins, maxs ); |
| 3818 |
|
| 3819 |
|
| 3820 |
VectorSubtract( maxs, light->origin, dir ); |
| 3821 |
radius = (float) VectorLength( dir ); |
| 3822 |
|
| 3823 |
|
| 3824 |
if ( radius < light->envelope ) { |
| 3825 |
light->envelope = radius; |
| 3826 |
|
| 3827 |
} |
| 3828 |
|
| 3829 |
|
| 3830 |
} |
| 3831 |
|
| 3832 |
|
| 3833 |
if ( forGrid ) { |
| 3834 |
if ( !( light->flags & LIGHT_GRID ) ) { |
| 3835 |
light->envelope = 0.0f; |
| 3836 |
} |
| 3837 |
} |
| 3838 |
else |
| 3839 |
{ |
| 3840 |
if ( !( light->flags & LIGHT_SURFACES ) ) { |
| 3841 |
light->envelope = 0.0f; |
| 3842 |
} |
| 3843 |
} |
| 3844 |
} |
| 3845 |
|
| 3846 |
|
| 3847 |
if ( light->cluster < 0 || light->envelope <= 0.0f ) { |
| 3848 |
|
| 3849 |
|
| 3850 |
|
| 3851 |
|
| 3852 |
numCulledLights++; |
| 3853 |
*owner = light->next; |
| 3854 |
if ( light->w != NULL ) { |
| 3855 |
free( light->w ); |
| 3856 |
} |
| 3857 |
free( light ); |
| 3858 |
continue; |
| 3859 |
} |
| 3860 |
} |
| 3861 |
|
| 3862 |
|
| 3863 |
light->envelope2 = ( light->envelope * light->envelope ); |
| 3864 |
|
| 3865 |
|
| 3866 |
numLights++; |
| 3867 |
|
| 3868 |
|
| 3869 |
owner = &( ( **owner ).next ); |
| 3870 |
} |
| 3871 |
|
| 3872 |
|
| 3873 |
memset( buckets, 0, sizeof( buckets ) ); |
| 3874 |
light2 = NULL; |
| 3875 |
for ( light = lights; light != NULL; light = light2 ) |
| 3876 |
{ |
| 3877 |
|
| 3878 |
light2 = light->next; |
| 3879 |
|
| 3880 |
|
| 3881 |
light->next = buckets[ light->style ]; |
| 3882 |
buckets[ light->style ] = light; |
| 3883 |
|
| 3884 |
|
| 3885 |
if ( light->style != LS_NORMAL ) { |
| 3886 |
noCollapse = qtrue; |
| 3887 |
} |
| 3888 |
} |
| 3889 |
|
| 3890 |
|
| 3891 |
lights = NULL; |
| 3892 |
for ( i = 255; i >= 0; i-- ) |
| 3893 |
{ |
| 3894 |
light2 = NULL; |
| 3895 |
for ( light = buckets[ i ]; light != NULL; light = light2 ) |
| 3896 |
{ |
| 3897 |
light2 = light->next; |
| 3898 |
light->next = lights; |
| 3899 |
lights = light; |
| 3900 |
} |
| 3901 |
} |
| 3902 |
|
| 3903 |
|
| 3904 |
Sys_Printf( "%9d total lights\n", numLights ); |
| 3905 |
Sys_Printf( "%9d culled lights\n", numCulledLights ); |
| 3906 |
} |
| 3907 |
|
| 3908 |
|
| 3909 |
|
| 3910 |
|
| 3911 |
|
| 3912 |
|
| 3913 |
|
| 3914 |
|
| 3915 |
void CreateTraceLightsForBounds( vec3_t mins, vec3_t maxs, vec3_t normal, int numClusters, int *clusters, int flags, trace_t *trace ){ |
| 3916 |
int i; |
| 3917 |
light_t *light; |
| 3918 |
vec3_t origin, dir, nullVector = { 0.0f, 0.0f, 0.0f }; |
| 3919 |
float radius, dist, length; |
| 3920 |
|
| 3921 |
|
| 3922 |
|
| 3923 |
if ( numLights == 0 ) { |
| 3924 |
SetupEnvelopes( qfalse, fast ); |
| 3925 |
} |
| 3926 |
|
| 3927 |
|
| 3928 |
|
| 3929 |
|
| 3930 |
|
| 3931 |
trace->lights = safe_malloc( sizeof( light_t* ) * ( numLights + 1 ) ); |
| 3932 |
trace->numLights = 0; |
| 3933 |
|
| 3934 |
|
| 3935 |
VectorAdd( mins, maxs, origin ); |
| 3936 |
VectorScale( origin, 0.5f, origin ); |
| 3937 |
VectorSubtract( maxs, origin, dir ); |
| 3938 |
radius = (float) VectorLength( dir ); |
| 3939 |
|
| 3940 |
|
| 3941 |
if ( normal != NULL ) { |
| 3942 |
length = VectorLength( normal ); |
| 3943 |
} |
| 3944 |
else |
| 3945 |
{ |
| 3946 |
normal = nullVector; |
| 3947 |
length = 0; |
| 3948 |
} |
| 3949 |
|
| 3950 |
|
| 3951 |
|
| 3952 |
for ( light = lights; light; light = light->next ) |
| 3953 |
{ |
| 3954 |
|
| 3955 |
if ( light->envelope <= 0 ) { |
| 3956 |
lightsEnvelopeCulled++; |
| 3957 |
continue; |
| 3958 |
} |
| 3959 |
|
| 3960 |
|
| 3961 |
if ( !( light->flags & flags ) ) { |
| 3962 |
continue; |
| 3963 |
} |
| 3964 |
|
| 3965 |
|
| 3966 |
if ( light->type != EMIT_SUN ) { |
| 3967 |
|
| 3968 |
if ( sunOnly ) { |
| 3969 |
continue; |
| 3970 |
} |
| 3971 |
|
| 3972 |
|
| 3973 |
if ( numClusters > 0 && clusters != NULL ) { |
| 3974 |
for ( i = 0; i < numClusters; i++ ) |
| 3975 |
{ |
| 3976 |
if ( ClusterVisible( light->cluster, clusters[ i ] ) ) { |
| 3977 |
break; |
| 3978 |
} |
| 3979 |
} |
| 3980 |
|
| 3981 |
|
| 3982 |
if ( i == numClusters ) { |
| 3983 |
lightsClusterCulled++; |
| 3984 |
continue; |
| 3985 |
} |
| 3986 |
} |
| 3987 |
|
| 3988 |
|
| 3989 |
VectorSubtract( light->origin, origin, dir ); |
| 3990 |
dist = VectorLength( dir ); |
| 3991 |
dist -= light->envelope; |
| 3992 |
dist -= radius; |
| 3993 |
if ( dist > 0 ) { |
| 3994 |
lightsEnvelopeCulled++; |
| 3995 |
continue; |
| 3996 |
} |
| 3997 |
|
| 3998 |
|
| 3999 |
#if 0 |
| 4000 |
skip = qfalse; |
| 4001 |
for ( i = 0; i < 3; i++ ) |
| 4002 |
{ |
| 4003 |
if ( mins[ i ] > light->maxs[ i ] || maxs[ i ] < light->mins[ i ] ) { |
| 4004 |
skip = qtrue; |
| 4005 |
} |
| 4006 |
} |
| 4007 |
if ( skip ) { |
| 4008 |
lightsBoundsCulled++; |
| 4009 |
continue; |
| 4010 |
} |
| 4011 |
#endif |
| 4012 |
} |
| 4013 |
|
| 4014 |
|
| 4015 |
if ( length > 0.0f && trace->twoSided == qfalse ) { |
| 4016 |
|
| 4017 |
if ( !( light->flags & LIGHT_TWOSIDED ) && DotProduct( light->normal, normal ) > 0.999f ) { |
| 4018 |
lightsPlaneCulled++; |
| 4019 |
continue; |
| 4020 |
} |
| 4021 |
|
| 4022 |
|
| 4023 |
if ( DotProduct( light->origin, normal ) - DotProduct( origin, normal ) < -1.0f ) { |
| 4024 |
lightsPlaneCulled++; |
| 4025 |
continue; |
| 4026 |
} |
| 4027 |
} |
| 4028 |
|
| 4029 |
|
| 4030 |
trace->lights[ trace->numLights++ ] = light; |
| 4031 |
} |
| 4032 |
|
| 4033 |
|
| 4034 |
trace->lights[ trace->numLights ] = NULL; |
| 4035 |
} |
| 4036 |
|
| 4037 |
|
| 4038 |
|
| 4039 |
void FreeTraceLights( trace_t *trace ){ |
| 4040 |
if ( trace->lights != NULL ) { |
| 4041 |
free( trace->lights ); |
| 4042 |
} |
| 4043 |
} |
| 4044 |
|
| 4045 |
|
| 4046 |
|
| 4047 |
|
| 4048 |
|
| 4049 |
|
| 4050 |
|
| 4051 |
|
| 4052 |
void CreateTraceLightsForSurface( int num, trace_t *trace ){ |
| 4053 |
int i; |
| 4054 |
vec3_t mins, maxs, normal; |
| 4055 |
bspDrawVert_t *dv; |
| 4056 |
bspDrawSurface_t *ds; |
| 4057 |
surfaceInfo_t *info; |
| 4058 |
|
| 4059 |
|
| 4060 |
|
| 4061 |
if ( num < 0 ) { |
| 4062 |
return; |
| 4063 |
} |
| 4064 |
|
| 4065 |
|
| 4066 |
ds = &bspDrawSurfaces[ num ]; |
| 4067 |
info = &surfaceInfos[ num ]; |
| 4068 |
|
| 4069 |
|
| 4070 |
ClearBounds( mins, maxs ); |
| 4071 |
VectorCopy( bspDrawVerts[ ds->firstVert ].normal, normal ); |
| 4072 |
for ( i = 0; i < ds->numVerts; i++ ) |
| 4073 |
{ |
| 4074 |
dv = &yDrawVerts[ ds->firstVert + i ]; |
| 4075 |
AddPointToBounds( dv->xyz, mins, maxs ); |
| 4076 |
if ( !VectorCompare( dv->normal, normal ) ) { |
| 4077 |
VectorClear( normal ); |
| 4078 |
} |
| 4079 |
} |
| 4080 |
|
| 4081 |
|
| 4082 |
CreateTraceLightsForBounds( mins, maxs, normal, info->numSurfaceClusters, &surfaceClusters[ info->firstSurfaceCluster ], LIGHT_SURFACES, trace ); |
| 4083 |
} |
| 4084 |
|
| 4085 |
|
| 4086 |
|
| 4087 |
#define FLOODLIGHT_CONE_ANGLE 88 |
| 4088 |
#define FLOODLIGHT_NUM_ANGLE_STEPS 16 |
| 4089 |
#define FLOODLIGHT_NUM_ELEVATION_STEPS 4 |
| 4090 |
#define FLOODLIGHT_NUM_VECTORS ( FLOODLIGHT_NUM_ANGLE_STEPS * FLOODLIGHT_NUM_ELEVATION_STEPS ) |
| 4091 |
|
| 4092 |
static vec3_t floodVectors[ FLOODLIGHT_NUM_VECTORS ]; |
| 4093 |
static int numFloodVectors = 0; |
| 4094 |
|
| 4095 |
void SetupFloodLight( void ){ |
| 4096 |
int i, j; |
| 4097 |
float angle, elevation, angleStep, elevationStep; |
| 4098 |
const char *value; |
| 4099 |
double v1,v2,v3,v4,v5,v6; |
| 4100 |
|
| 4101 |
|
| 4102 |
Sys_FPrintf( SYS_VRB, "--- SetupFloodLight ---\n" ); |
| 4103 |
|
| 4104 |
|
| 4105 |
angleStep = DEG2RAD( 360.0f / FLOODLIGHT_NUM_ANGLE_STEPS ); |
| 4106 |
elevationStep = DEG2RAD( FLOODLIGHT_CONE_ANGLE / FLOODLIGHT_NUM_ELEVATION_STEPS ); |
| 4107 |
|
| 4108 |
|
| 4109 |
angle = 0.0f; |
| 4110 |
for ( i = 0, angle = 0.0f; i < FLOODLIGHT_NUM_ANGLE_STEPS; i++, angle += angleStep ) |
| 4111 |
{ |
| 4112 |
|
| 4113 |
for ( j = 0, elevation = elevationStep * 0.5f; j < FLOODLIGHT_NUM_ELEVATION_STEPS; j++, elevation += elevationStep ) |
| 4114 |
{ |
| 4115 |
floodVectors[ numFloodVectors ][ 0 ] = sin( elevation ) * cos( angle ); |
| 4116 |
floodVectors[ numFloodVectors ][ 1 ] = sin( elevation ) * sin( angle ); |
| 4117 |
floodVectors[ numFloodVectors ][ 2 ] = cos( elevation ); |
| 4118 |
numFloodVectors++; |
| 4119 |
} |
| 4120 |
} |
| 4121 |
|
| 4122 |
|
| 4123 |
Sys_FPrintf( SYS_VRB, "%9d numFloodVectors\n", numFloodVectors ); |
| 4124 |
|
| 4125 |
|
| 4126 |
value = ValueForKey( &entities[ 0 ], "_floodlight" ); |
| 4127 |
|
| 4128 |
if ( value[ 0 ] != '\0' ) { |
| 4129 |
v1 = v2 = v3 = 0; |
| 4130 |
v4 = floodlightDistance; |
| 4131 |
v5 = floodlightIntensity; |
| 4132 |
v6 = floodlightDirectionScale; |
| 4133 |
|
| 4134 |
sscanf( value, "%lf %lf %lf %lf %lf %lf", &v1, &v2, &v3, &v4, &v5, &v6 ); |
| 4135 |
|
| 4136 |
floodlightRGB[0] = v1; |
| 4137 |
floodlightRGB[1] = v2; |
| 4138 |
floodlightRGB[2] = v3; |
| 4139 |
|
| 4140 |
if ( VectorLength( floodlightRGB ) == 0 ) { |
| 4141 |
VectorSet( floodlightRGB,0.94,0.94,1.0 ); |
| 4142 |
} |
| 4143 |
|
| 4144 |
if ( v4 < 1 ) { |
| 4145 |
v4 = 1024; |
| 4146 |
} |
| 4147 |
if ( v5 < 1 ) { |
| 4148 |
v5 = 128; |
| 4149 |
} |
| 4150 |
if ( v6 < 0 ) { |
| 4151 |
v6 = 1; |
| 4152 |
} |
| 4153 |
|
| 4154 |
floodlightDistance = v4; |
| 4155 |
floodlightIntensity = v5; |
| 4156 |
floodlightDirectionScale = v6; |
| 4157 |
|
| 4158 |
floodlighty = qtrue; |
| 4159 |
Sys_Printf( "FloodLighting enabled via worldspawn _floodlight key.\n" ); |
| 4160 |
} |
| 4161 |
else |
| 4162 |
{ |
| 4163 |
VectorSet( floodlightRGB,0.94,0.94,1.0 ); |
| 4164 |
} |
| 4165 |
if ( colorsRGB ) { |
| 4166 |
floodlightRGB[0] = Image_LinearFloatFromsRGBFloat( floodlightRGB[0] ); |
| 4167 |
floodlightRGB[1] = Image_LinearFloatFromsRGBFloat( floodlightRGB[1] ); |
| 4168 |
floodlightRGB[2] = Image_LinearFloatFromsRGBFloat( floodlightRGB[2] ); |
| 4169 |
} |
| 4170 |
ColorNormalize( floodlightRGB,floodlightRGB ); |
| 4171 |
} |
| 4172 |
|
| 4173 |
|
| 4174 |
|
| 4175 |
|
| 4176 |
|
| 4177 |
|
| 4178 |
|
| 4179 |
float FloodLightForSample( trace_t *trace, float floodLightDistance, qboolean floodLightLowQuality ){ |
| 4180 |
int i; |
| 4181 |
float d; |
| 4182 |
float contribution; |
| 4183 |
int sub = 0; |
| 4184 |
float gatherLight, outLight; |
| 4185 |
vec3_t normal, worldUp, myUp, myRt, direction, displacement; |
| 4186 |
float dd; |
| 4187 |
int vecs = 0; |
| 4188 |
|
| 4189 |
gatherLight = 0; |
| 4190 |
|
| 4191 |
|
| 4192 |
|
| 4193 |
if ( trace == NULL || trace->cluster < 0 ) { |
| 4194 |
return 0.0f; |
| 4195 |
} |
| 4196 |
|
| 4197 |
|
| 4198 |
|
| 4199 |
dd = floodLightDistance; |
| 4200 |
VectorCopy( trace->normal, normal ); |
| 4201 |
|
| 4202 |
|
| 4203 |
if ( normal[ 0 ] == 0.0f && normal[ 1 ] == 0.0f && ( normal[ 2 ] == 1.0f || normal[ 2 ] == -1.0f ) ) { |
| 4204 |
if ( normal[ 2 ] == 1.0f ) { |
| 4205 |
VectorSet( myRt, 1.0f, 0.0f, 0.0f ); |
| 4206 |
VectorSet( myUp, 0.0f, 1.0f, 0.0f ); |
| 4207 |
} |
| 4208 |
else if ( normal[ 2 ] == -1.0f ) { |
| 4209 |
VectorSet( myRt, -1.0f, 0.0f, 0.0f ); |
| 4210 |
VectorSet( myUp, 0.0f, 1.0f, 0.0f ); |
| 4211 |
} |
| 4212 |
} |
| 4213 |
else |
| 4214 |
{ |
| 4215 |
VectorSet( worldUp, 0.0f, 0.0f, 1.0f ); |
| 4216 |
CrossProduct( normal, worldUp, myRt ); |
| 4217 |
VectorNormalize( myRt, myRt ); |
| 4218 |
CrossProduct( myRt, normal, myUp ); |
| 4219 |
VectorNormalize( myUp, myUp ); |
| 4220 |
} |
| 4221 |
|
| 4222 |
|
| 4223 |
if ( floodLightLowQuality == qtrue ) { |
| 4224 |
|
| 4225 |
for ( i = 0; i < numFloodVectors; i++ ) |
| 4226 |
if ( rand() % 10 != 0 ) { |
| 4227 |
continue; |
| 4228 |
} |
| 4229 |
} |
| 4230 |
else |
| 4231 |
{ |
| 4232 |
|
| 4233 |
for ( i = 0; i < numFloodVectors; i++ ) |
| 4234 |
{ |
| 4235 |
vecs++; |
| 4236 |
|
| 4237 |
|
| 4238 |
direction[ 0 ] = myRt[ 0 ] * floodVectors[ i ][ 0 ] + myUp[ 0 ] * floodVectors[ i ][ 1 ] + normal[ 0 ] * floodVectors[ i ][ 2 ]; |
| 4239 |
direction[ 1 ] = myRt[ 1 ] * floodVectors[ i ][ 0 ] + myUp[ 1 ] * floodVectors[ i ][ 1 ] + normal[ 1 ] * floodVectors[ i ][ 2 ]; |
| 4240 |
direction[ 2 ] = myRt[ 2 ] * floodVectors[ i ][ 0 ] + myUp[ 2 ] * floodVectors[ i ][ 1 ] + normal[ 2 ] * floodVectors[ i ][ 2 ]; |
| 4241 |
|
| 4242 |
|
| 4243 |
VectorMA( trace->origin, dd, direction, trace->end ); |
| 4244 |
|
| 4245 |
|
| 4246 |
|
| 4247 |
SetupTrace( trace ); |
| 4248 |
VectorSet(trace->color, 1.0f, 1.0f, 1.0f); |
| 4249 |
|
| 4250 |
TraceLine( trace ); |
| 4251 |
contribution = 1; |
| 4252 |
|
| 4253 |
if ( trace->compileFlags & C_SKY || trace->compileFlags & C_TRANSLUCENT ) { |
| 4254 |
contribution = 1.0f; |
| 4255 |
} |
| 4256 |
else if ( trace->opaque ) { |
| 4257 |
VectorSubtract( trace->hit, trace->origin, displacement ); |
| 4258 |
d = VectorLength( displacement ); |
| 4259 |
|
| 4260 |
|
| 4261 |
|
| 4262 |
contribution = d / dd; |
| 4263 |
if ( contribution > 1 ) { |
| 4264 |
contribution = 1.0f; |
| 4265 |
} |
| 4266 |
|
| 4267 |
|
| 4268 |
} |
| 4269 |
|
| 4270 |
gatherLight += contribution; |
| 4271 |
} |
| 4272 |
} |
| 4273 |
|
| 4274 |
|
| 4275 |
if ( gatherLight <= 0.0f ) { |
| 4276 |
return 0.0f; |
| 4277 |
} |
| 4278 |
|
| 4279 |
sub = vecs; |
| 4280 |
|
| 4281 |
if ( sub < 1 ) { |
| 4282 |
sub = 1; |
| 4283 |
} |
| 4284 |
gatherLight /= ( sub ); |
| 4285 |
|
| 4286 |
outLight = gatherLight; |
| 4287 |
if ( outLight > 1.0f ) { |
| 4288 |
outLight = 1.0f; |
| 4289 |
} |
| 4290 |
|
| 4291 |
|
| 4292 |
return outLight; |
| 4293 |
} |
| 4294 |
|
| 4295 |
|
| 4296 |
|
| 4297 |
|
| 4298 |
|
| 4299 |
|
| 4300 |
|
| 4301 |
|
| 4302 |
|
| 4303 |
|
| 4304 |
void FloodLightRawLightmapPass( rawLightmap_t *lm, vec3_t lmFloodLightRGB, float lmFloodLightIntensity, float lmFloodLightDistance, qboolean lmFloodLightLowQuality, float floodlightDirectionScale ){ |
| 4305 |
int i, x, y, *cluster; |
| 4306 |
float *origin, *normal, *floodlight, floodLightAmount; |
| 4307 |
surfaceInfo_t *info; |
| 4308 |
trace_t trace; |
| 4309 |
|
| 4310 |
|
| 4311 |
|
| 4312 |
memset( &trace,0,sizeof( trace_t ) ); |
| 4313 |
|
| 4314 |
|
| 4315 |
trace.testOcclusion = qtrue; |
| 4316 |
trace.forceSunlight = qfalse; |
| 4317 |
trace.twoSided = qtrue; |
| 4318 |
trace.recvShadows = lm->recvShadows; |
| 4319 |
trace.numSurfaces = lm->numLightSurfaces; |
| 4320 |
trace.surfaces = &lightSurfaces[ lm->firstLightSurface ]; |
| 4321 |
trace.inhibitRadius = DEFAULT_INHIBIT_RADIUS; |
| 4322 |
trace.testAll = qfalse; |
| 4323 |
trace.distance = 1024; |
| 4324 |
|
| 4325 |
|
| 4326 |
|
| 4327 |
for ( i = 0; i < trace.numSurfaces; i++ ) |
| 4328 |
{ |
| 4329 |
|
| 4330 |
info = &surfaceInfos[ trace.surfaces[ i ] ]; |
| 4331 |
|
| 4332 |
|
| 4333 |
if ( info->si->twoSided ) { |
| 4334 |
trace.twoSided = qtrue; |
| 4335 |
break; |
| 4336 |
} |
| 4337 |
} |
| 4338 |
|
| 4339 |
|
| 4340 |
for ( y = 0; y < lm->sh; y++ ) |
| 4341 |
{ |
| 4342 |
for ( x = 0; x < lm->sw; x++ ) |
| 4343 |
{ |
| 4344 |
|
| 4345 |
cluster = SUPER_CLUSTER( x, y ); |
| 4346 |
origin = SUPER_ORIGIN( x, y ); |
| 4347 |
normal = SUPER_NORMAL( x, y ); |
| 4348 |
floodlight = SUPER_FLOODLIGHT( x, y ); |
| 4349 |
|
| 4350 |
|
| 4351 |
*floodlight = 0.0f; |
| 4352 |
|
| 4353 |
|
| 4354 |
if ( *cluster < 0 ) { |
| 4355 |
continue; |
| 4356 |
} |
| 4357 |
|
| 4358 |
|
| 4359 |
trace.cluster = *cluster; |
| 4360 |
VectorCopy( origin, trace.origin ); |
| 4361 |
VectorCopy( normal, trace.normal ); |
| 4362 |
|
| 4363 |
|
| 4364 |
floodLightAmount = FloodLightForSample( &trace, lmFloodLightDistance, lmFloodLightLowQuality ) * lmFloodLightIntensity; |
| 4365 |
|
| 4366 |
|
| 4367 |
floodlight[0] += lmFloodLightRGB[0] * floodLightAmount; |
| 4368 |
floodlight[1] += lmFloodLightRGB[1] * floodLightAmount; |
| 4369 |
floodlight[2] += lmFloodLightRGB[2] * floodLightAmount; |
| 4370 |
floodlight[3] += floodlightDirectionScale; |
| 4371 |
} |
| 4372 |
} |
| 4373 |
|
| 4374 |
|
| 4375 |
return; |
| 4376 |
|
| 4377 |
#if 0 |
| 4378 |
|
| 4379 |
|
| 4380 |
for ( y = 0; y < lm->sh; y++ ) |
| 4381 |
{ |
| 4382 |
for ( x = 0; x < lm->sw; x++ ) |
| 4383 |
{ |
| 4384 |
|
| 4385 |
cluster = SUPER_CLUSTER( x, y ); |
| 4386 |
floodlight = SUPER_FLOODLIGHT( x, y ); |
| 4387 |
|
| 4388 |
|
| 4389 |
average = *floodlight; |
| 4390 |
samples = 1.0f; |
| 4391 |
for ( sy = ( y - 1 ); sy <= ( y + 1 ); sy++ ) |
| 4392 |
{ |
| 4393 |
if ( sy < 0 || sy >= lm->sh ) { |
| 4394 |
continue; |
| 4395 |
} |
| 4396 |
|
| 4397 |
for ( sx = ( x - 1 ); sx <= ( x + 1 ); sx++ ) |
| 4398 |
{ |
| 4399 |
if ( sx < 0 || sx >= lm->sw || ( sx == x && sy == y ) ) { |
| 4400 |
continue; |
| 4401 |
} |
| 4402 |
|
| 4403 |
|
| 4404 |
cluster = SUPER_CLUSTER( sx, sy ); |
| 4405 |
floodlight2 = SUPER_FLOODLIGHT( sx, sy ); |
| 4406 |
if ( *cluster < 0 || *floodlight2 <= 0.0f ) { |
| 4407 |
continue; |
| 4408 |
} |
| 4409 |
|
| 4410 |
|
| 4411 |
average += *floodlight2; |
| 4412 |
samples += 1.0f; |
| 4413 |
} |
| 4414 |
|
| 4415 |
|
| 4416 |
if ( samples <= 0.0f ) { |
| 4417 |
break; |
| 4418 |
} |
| 4419 |
} |
| 4420 |
|
| 4421 |
|
| 4422 |
if ( samples <= 0.0f ) { |
| 4423 |
continue; |
| 4424 |
} |
| 4425 |
|
| 4426 |
|
| 4427 |
*floodlight = average / samples; |
| 4428 |
} |
| 4429 |
} |
| 4430 |
#endif |
| 4431 |
} |
| 4432 |
|
| 4433 |
void FloodLightRawLightmap( int rawLightmapNum ){ |
| 4434 |
rawLightmap_t *lm; |
| 4435 |
|
| 4436 |
|
| 4437 |
if ( rawLightmapNum >= numRawLightmaps ) { |
| 4438 |
return; |
| 4439 |
} |
| 4440 |
|
| 4441 |
lm = &rawLightmaps[ rawLightmapNum ]; |
| 4442 |
|
| 4443 |
|
| 4444 |
if ( floodlighty && floodlightIntensity ) { |
| 4445 |
FloodLightRawLightmapPass( lm, floodlightRGB, floodlightIntensity, floodlightDistance, floodlight_lowquality, floodlightDirectionScale ); |
| 4446 |
} |
| 4447 |
|
| 4448 |
|
| 4449 |
if ( lm->floodlightIntensity ) { |
| 4450 |
FloodLightRawLightmapPass( lm, lm->floodlightRGB, lm->floodlightIntensity, lm->floodlightDistance, qfalse, lm->floodlightDirectionScale ); |
| 4451 |
numSurfacesFloodlighten += 1; |
| 4452 |
} |
| 4453 |
} |
| 4454 |
|
| 4455 |
void FloodlightRawLightmaps(){ |
| 4456 |
Sys_Printf( "--- FloodlightRawLightmap ---\n" ); |
| 4457 |
numSurfacesFloodlighten = 0; |
| 4458 |
RunThreadsOnIndividual( numRawLightmaps, qtrue, FloodLightRawLightmap ); |
| 4459 |
Sys_Printf( "%9d custom lightmaps floodlighted\n", numSurfacesFloodlighten ); |
| 4460 |
} |
| 4461 |
|
| 4462 |
|
| 4463 |
|
| 4464 |
|
| 4465 |
|
| 4466 |
|
| 4467 |
void FloodlightIlluminateLightmap( rawLightmap_t *lm ){ |
| 4468 |
float *luxel, *floodlight, *deluxel, *normal; |
| 4469 |
int *cluster; |
| 4470 |
float brightness; |
| 4471 |
int x, y, lightmapNum; |
| 4472 |
|
| 4473 |
|
| 4474 |
for ( lightmapNum = 0; lightmapNum < MAX_LIGHTMAPS; lightmapNum++ ) |
| 4475 |
{ |
| 4476 |
|
| 4477 |
if ( lm->superLuxels[ lightmapNum ] == NULL ) { |
| 4478 |
continue; |
| 4479 |
} |
| 4480 |
|
| 4481 |
if( lm->styles[lightmapNum] != LS_NORMAL && lm->styles[lightmapNum] != LS_NONE ) |
| 4482 |
continue; |
| 4483 |
|
| 4484 |
|
| 4485 |
for ( y = 0; y < lm->sh; y++ ) |
| 4486 |
{ |
| 4487 |
for ( x = 0; x < lm->sw; x++ ) |
| 4488 |
{ |
| 4489 |
|
| 4490 |
floodlight = SUPER_FLOODLIGHT( x, y ); |
| 4491 |
if ( !floodlight[0] && !floodlight[1] && !floodlight[2] ) { |
| 4492 |
continue; |
| 4493 |
} |
| 4494 |
|
| 4495 |
|
| 4496 |
cluster = SUPER_CLUSTER( x, y ); |
| 4497 |
|
| 4498 |
|
| 4499 |
if ( *cluster < 0 ) { |
| 4500 |
continue; |
| 4501 |
} |
| 4502 |
|
| 4503 |
|
| 4504 |
luxel = SUPER_LUXEL( lightmapNum, x, y ); |
| 4505 |
deluxel = SUPER_DELUXEL( x, y ); |
| 4506 |
|
| 4507 |
|
| 4508 |
luxel[0] += floodlight[0]; |
| 4509 |
luxel[1] += floodlight[1]; |
| 4510 |
luxel[2] += floodlight[2]; |
| 4511 |
|
| 4512 |
if ( luxel[3] == 0 ) { |
| 4513 |
luxel[3] = 1; |
| 4514 |
} |
| 4515 |
|
| 4516 |
|
| 4517 |
if ( deluxemap && floodlight[3] > 0 ) { |
| 4518 |
vec3_t lightvector; |
| 4519 |
|
| 4520 |
normal = SUPER_NORMAL( x, y ); |
| 4521 |
brightness = RGBTOGRAY( floodlight ) * ( 1.0f / 255.0f ) * floodlight[3]; |
| 4522 |
|
| 4523 |
|
| 4524 |
if ( brightness < 0.00390625f ) { |
| 4525 |
brightness = 0.00390625f; |
| 4526 |
} |
| 4527 |
|
| 4528 |
VectorScale( normal, brightness, lightvector ); |
| 4529 |
VectorAdd( deluxel, lightvector, deluxel ); |
| 4530 |
} |
| 4531 |
} |
| 4532 |
} |
| 4533 |
} |
| 4534 |
} |