| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#include "tr_local.h" |
| 25 |
|
| 26 |
#define DLIGHT_AT_RADIUS 16 |
| 27 |
|
| 28 |
|
| 29 |
#define DLIGHT_MINIMUM_RADIUS 16 |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
void R_TransformDlights( int count, dlight_t *dl, orientationr_t *or) { |
| 43 |
int i; |
| 44 |
vec3_t temp; |
| 45 |
|
| 46 |
for ( i = 0 ; i < count ; i++, dl++ ) { |
| 47 |
VectorSubtract( dl->origin, or->origin, temp ); |
| 48 |
dl->transformed[0] = DotProduct( temp, or->axis[0] ); |
| 49 |
dl->transformed[1] = DotProduct( temp, or->axis[1] ); |
| 50 |
dl->transformed[2] = DotProduct( temp, or->axis[2] ); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
void R_DlightBmodel( bmodel_t *bmodel ) { |
| 62 |
int i, j; |
| 63 |
dlight_t *dl; |
| 64 |
int mask; |
| 65 |
msurface_t *surf; |
| 66 |
|
| 67 |
|
| 68 |
R_TransformDlights( tr.refdef.num_dlights, tr.refdef.dlights, &tr.or ); |
| 69 |
|
| 70 |
mask = 0; |
| 71 |
for ( i=0 ; i<tr.refdef.num_dlights ; i++ ) { |
| 72 |
dl = &tr.refdef.dlights[i]; |
| 73 |
|
| 74 |
|
| 75 |
for ( j = 0 ; j < 3 ; j++ ) { |
| 76 |
if ( dl->transformed[j] - bmodel->bounds[1][j] > dl->radius ) { |
| 77 |
break; |
| 78 |
} |
| 79 |
if ( bmodel->bounds[0][j] - dl->transformed[j] > dl->radius ) { |
| 80 |
break; |
| 81 |
} |
| 82 |
} |
| 83 |
if ( j < 3 ) { |
| 84 |
continue; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
mask |= 1 << i; |
| 89 |
} |
| 90 |
|
| 91 |
tr.currentEntity->needDlights = (mask != 0); |
| 92 |
|
| 93 |
|
| 94 |
for ( i = 0 ; i < bmodel->numSurfaces ; i++ ) { |
| 95 |
surf = tr.world->surfaces + bmodel->firstSurface + i; |
| 96 |
|
| 97 |
switch(*surf->data) |
| 98 |
{ |
| 99 |
case SF_FACE: |
| 100 |
case SF_GRID: |
| 101 |
case SF_TRIANGLES: |
| 102 |
((srfBspSurface_t *)surf->data)->dlightBits = mask; |
| 103 |
break; |
| 104 |
|
| 105 |
default: |
| 106 |
break; |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
extern cvar_t *r_ambientScale; |
| 121 |
extern cvar_t *r_directedScale; |
| 122 |
extern cvar_t *r_debugLight; |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
static void R_SetupEntityLightingGrid( trRefEntity_t *ent, world_t *world ) { |
| 131 |
vec3_t lightOrigin; |
| 132 |
int pos[3]; |
| 133 |
int i, j; |
| 134 |
byte *gridData; |
| 135 |
float frac[3]; |
| 136 |
int gridStep[3]; |
| 137 |
vec3_t direction; |
| 138 |
float totalFactor; |
| 139 |
|
| 140 |
if ( ent->e.renderfx & RF_LIGHTING_ORIGIN ) { |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
VectorCopy( ent->e.lightingOrigin, lightOrigin ); |
| 145 |
} else { |
| 146 |
VectorCopy( ent->e.origin, lightOrigin ); |
| 147 |
} |
| 148 |
|
| 149 |
VectorSubtract( lightOrigin, world->lightGridOrigin, lightOrigin ); |
| 150 |
for ( i = 0 ; i < 3 ; i++ ) { |
| 151 |
float v; |
| 152 |
|
| 153 |
v = lightOrigin[i]*world->lightGridInverseSize[i]; |
| 154 |
pos[i] = floor( v ); |
| 155 |
frac[i] = v - pos[i]; |
| 156 |
if ( pos[i] < 0 ) { |
| 157 |
pos[i] = 0; |
| 158 |
} else if ( pos[i] > world->lightGridBounds[i] - 1 ) { |
| 159 |
pos[i] = world->lightGridBounds[i] - 1; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
VectorClear( ent->ambientLight ); |
| 164 |
VectorClear( ent->directedLight ); |
| 165 |
VectorClear( direction ); |
| 166 |
|
| 167 |
assert( world->lightGridData ); |
| 168 |
|
| 169 |
|
| 170 |
gridStep[0] = 8; |
| 171 |
gridStep[1] = 8 * world->lightGridBounds[0]; |
| 172 |
gridStep[2] = 8 * world->lightGridBounds[0] * world->lightGridBounds[1]; |
| 173 |
gridData = world->lightGridData + pos[0] * gridStep[0] |
| 174 |
+ pos[1] * gridStep[1] + pos[2] * gridStep[2]; |
| 175 |
|
| 176 |
totalFactor = 0; |
| 177 |
for ( i = 0 ; i < 8 ; i++ ) { |
| 178 |
float factor; |
| 179 |
byte *data; |
| 180 |
int lat, lng; |
| 181 |
vec3_t normal; |
| 182 |
#if idppc |
| 183 |
float d0, d1, d2, d3, d4, d5; |
| 184 |
#endif |
| 185 |
factor = 1.0; |
| 186 |
data = gridData; |
| 187 |
for ( j = 0 ; j < 3 ; j++ ) { |
| 188 |
if ( i & (1<<j) ) { |
| 189 |
if ( pos[j] + 1 > world->lightGridBounds[j] - 1 ) { |
| 190 |
break; |
| 191 |
} |
| 192 |
factor *= frac[j]; |
| 193 |
data += gridStep[j]; |
| 194 |
} else { |
| 195 |
factor *= (1.0f - frac[j]); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
if ( j != 3 ) { |
| 200 |
continue; |
| 201 |
} |
| 202 |
|
| 203 |
if (world->lightGrid16) |
| 204 |
{ |
| 205 |
uint16_t *data16 = world->lightGrid16 + (int)(data - world->lightGridData) / 8 * 6; |
| 206 |
if (!(data16[0]+data16[1]+data16[2]+data16[3]+data16[4]+data16[5])) { |
| 207 |
continue; |
| 208 |
} |
| 209 |
} |
| 210 |
else |
| 211 |
{ |
| 212 |
if (!(data[0]+data[1]+data[2]+data[3]+data[4]+data[5]) ) { |
| 213 |
continue; |
| 214 |
} |
| 215 |
} |
| 216 |
totalFactor += factor; |
| 217 |
#if idppc |
| 218 |
d0 = data[0]; d1 = data[1]; d2 = data[2]; |
| 219 |
d3 = data[3]; d4 = data[4]; d5 = data[5]; |
| 220 |
|
| 221 |
ent->ambientLight[0] += factor * d0; |
| 222 |
ent->ambientLight[1] += factor * d1; |
| 223 |
ent->ambientLight[2] += factor * d2; |
| 224 |
|
| 225 |
ent->directedLight[0] += factor * d3; |
| 226 |
ent->directedLight[1] += factor * d4; |
| 227 |
ent->directedLight[2] += factor * d5; |
| 228 |
#else |
| 229 |
if (world->lightGrid16) |
| 230 |
{ |
| 231 |
|
| 232 |
uint16_t *data16 = world->lightGrid16 + (int)(data - world->lightGridData) / 8 * 6; |
| 233 |
|
| 234 |
ent->ambientLight[0] += factor * data16[0] / 257.0f; |
| 235 |
ent->ambientLight[1] += factor * data16[1] / 257.0f; |
| 236 |
ent->ambientLight[2] += factor * data16[2] / 257.0f; |
| 237 |
|
| 238 |
ent->directedLight[0] += factor * data16[3] / 257.0f; |
| 239 |
ent->directedLight[1] += factor * data16[4] / 257.0f; |
| 240 |
ent->directedLight[2] += factor * data16[5] / 257.0f; |
| 241 |
} |
| 242 |
else |
| 243 |
{ |
| 244 |
ent->ambientLight[0] += factor * data[0]; |
| 245 |
ent->ambientLight[1] += factor * data[1]; |
| 246 |
ent->ambientLight[2] += factor * data[2]; |
| 247 |
|
| 248 |
ent->directedLight[0] += factor * data[3]; |
| 249 |
ent->directedLight[1] += factor * data[4]; |
| 250 |
ent->directedLight[2] += factor * data[5]; |
| 251 |
} |
| 252 |
#endif |
| 253 |
lat = data[7]; |
| 254 |
lng = data[6]; |
| 255 |
lat *= (FUNCTABLE_SIZE/256); |
| 256 |
lng *= (FUNCTABLE_SIZE/256); |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
normal[0] = tr.sinTable[(lat+(FUNCTABLE_SIZE/4))&FUNCTABLE_MASK] * tr.sinTable[lng]; |
| 263 |
normal[1] = tr.sinTable[lat] * tr.sinTable[lng]; |
| 264 |
normal[2] = tr.sinTable[(lng+(FUNCTABLE_SIZE/4))&FUNCTABLE_MASK]; |
| 265 |
|
| 266 |
VectorMA( direction, factor, normal, direction ); |
| 267 |
} |
| 268 |
|
| 269 |
if ( totalFactor > 0 && totalFactor < 0.99 ) { |
| 270 |
totalFactor = 1.0f / totalFactor; |
| 271 |
VectorScale( ent->ambientLight, totalFactor, ent->ambientLight ); |
| 272 |
VectorScale( ent->directedLight, totalFactor, ent->directedLight ); |
| 273 |
} |
| 274 |
|
| 275 |
VectorScale( ent->ambientLight, r_ambientScale->value, ent->ambientLight ); |
| 276 |
VectorScale( ent->directedLight, r_directedScale->value, ent->directedLight ); |
| 277 |
|
| 278 |
VectorNormalize2( direction, ent->lightDir ); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
static void LogLight( trRefEntity_t *ent ) { |
| 288 |
int max1, max2; |
| 289 |
|
| 290 |
if ( !(ent->e.renderfx & RF_FIRST_PERSON ) ) { |
| 291 |
return; |
| 292 |
} |
| 293 |
|
| 294 |
max1 = ent->ambientLight[0]; |
| 295 |
if ( ent->ambientLight[1] > max1 ) { |
| 296 |
max1 = ent->ambientLight[1]; |
| 297 |
} else if ( ent->ambientLight[2] > max1 ) { |
| 298 |
max1 = ent->ambientLight[2]; |
| 299 |
} |
| 300 |
|
| 301 |
max2 = ent->directedLight[0]; |
| 302 |
if ( ent->directedLight[1] > max2 ) { |
| 303 |
max2 = ent->directedLight[1]; |
| 304 |
} else if ( ent->directedLight[2] > max2 ) { |
| 305 |
max2 = ent->directedLight[2]; |
| 306 |
} |
| 307 |
|
| 308 |
ri.Printf( PRINT_ALL, "amb:%i dir:%i\n", max1, max2 ); |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
void R_SetupEntityLighting( const trRefdef_t *refdef, trRefEntity_t *ent ) { |
| 320 |
int i; |
| 321 |
dlight_t *dl; |
| 322 |
float power; |
| 323 |
vec3_t dir; |
| 324 |
float d; |
| 325 |
vec3_t lightDir; |
| 326 |
vec3_t lightOrigin; |
| 327 |
|
| 328 |
|
| 329 |
if ( ent->lightingCalculated ) { |
| 330 |
return; |
| 331 |
} |
| 332 |
ent->lightingCalculated = qtrue; |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
if ( ent->e.renderfx & RF_LIGHTING_ORIGIN ) { |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
VectorCopy( ent->e.lightingOrigin, lightOrigin ); |
| 342 |
} else { |
| 343 |
VectorCopy( ent->e.origin, lightOrigin ); |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
if ( !(refdef->rdflags & RDF_NOWORLDMODEL ) |
| 348 |
&& tr.world->lightGridData ) { |
| 349 |
R_SetupEntityLightingGrid( ent, tr.world ); |
| 350 |
} else { |
| 351 |
ent->ambientLight[0] = ent->ambientLight[1] = |
| 352 |
ent->ambientLight[2] = tr.identityLight * 150; |
| 353 |
ent->directedLight[0] = ent->directedLight[1] = |
| 354 |
ent->directedLight[2] = tr.identityLight * 150; |
| 355 |
VectorCopy( tr.sunDirection, ent->lightDir ); |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
if ( 1 ) { |
| 360 |
|
| 361 |
ent->ambientLight[0] += tr.identityLight * 32; |
| 362 |
ent->ambientLight[1] += tr.identityLight * 32; |
| 363 |
ent->ambientLight[2] += tr.identityLight * 32; |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
d = VectorLength( ent->directedLight ); |
| 370 |
VectorScale( ent->lightDir, d, lightDir ); |
| 371 |
|
| 372 |
for ( i = 0 ; i < refdef->num_dlights ; i++ ) { |
| 373 |
dl = &refdef->dlights[i]; |
| 374 |
VectorSubtract( dl->origin, lightOrigin, dir ); |
| 375 |
d = VectorNormalize( dir ); |
| 376 |
|
| 377 |
power = DLIGHT_AT_RADIUS * ( dl->radius * dl->radius ); |
| 378 |
if ( d < DLIGHT_MINIMUM_RADIUS ) { |
| 379 |
d = DLIGHT_MINIMUM_RADIUS; |
| 380 |
} |
| 381 |
d = power / ( d * d ); |
| 382 |
|
| 383 |
VectorMA( ent->directedLight, d, dl->color, ent->directedLight ); |
| 384 |
VectorMA( lightDir, d, dir, lightDir ); |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
{ |
| 391 |
float r, g, b, max; |
| 392 |
|
| 393 |
r = ent->ambientLight[0]; |
| 394 |
g = ent->ambientLight[1]; |
| 395 |
b = ent->ambientLight[2]; |
| 396 |
|
| 397 |
max = MAX(MAX(r, g), b); |
| 398 |
|
| 399 |
if (max > 255.0f) |
| 400 |
{ |
| 401 |
max = 255.0f / max; |
| 402 |
ent->ambientLight[0] *= max; |
| 403 |
ent->ambientLight[1] *= max; |
| 404 |
ent->ambientLight[2] *= max; |
| 405 |
} |
| 406 |
|
| 407 |
r = ent->directedLight[0]; |
| 408 |
g = ent->directedLight[1]; |
| 409 |
b = ent->directedLight[2]; |
| 410 |
|
| 411 |
max = MAX(MAX(r, g), b); |
| 412 |
|
| 413 |
if (max > 255.0f) |
| 414 |
{ |
| 415 |
max = 255.0f / max; |
| 416 |
ent->directedLight[0] *= max; |
| 417 |
ent->directedLight[1] *= max; |
| 418 |
ent->directedLight[2] *= max; |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
|
| 423 |
if ( r_debugLight->integer ) { |
| 424 |
LogLight( ent ); |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
((byte *)&ent->ambientLightInt)[0] = ri.ftol(ent->ambientLight[0]); |
| 429 |
((byte *)&ent->ambientLightInt)[1] = ri.ftol(ent->ambientLight[1]); |
| 430 |
((byte *)&ent->ambientLightInt)[2] = ri.ftol(ent->ambientLight[2]); |
| 431 |
((byte *)&ent->ambientLightInt)[3] = 0xff; |
| 432 |
|
| 433 |
|
| 434 |
VectorNormalize( lightDir ); |
| 435 |
ent->modelLightDir[0] = DotProduct( lightDir, ent->e.axis[0] ); |
| 436 |
ent->modelLightDir[1] = DotProduct( lightDir, ent->e.axis[1] ); |
| 437 |
ent->modelLightDir[2] = DotProduct( lightDir, ent->e.axis[2] ); |
| 438 |
|
| 439 |
VectorCopy(lightDir, ent->lightDir); |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
int R_LightForPoint( vec3_t point, vec3_t ambientLight, vec3_t directedLight, vec3_t lightDir ) |
| 448 |
{ |
| 449 |
trRefEntity_t ent; |
| 450 |
|
| 451 |
if ( tr.world->lightGridData == NULL ) |
| 452 |
return qfalse; |
| 453 |
|
| 454 |
Com_Memset(&ent, 0, sizeof(ent)); |
| 455 |
VectorCopy( point, ent.e.origin ); |
| 456 |
R_SetupEntityLightingGrid( &ent, tr.world ); |
| 457 |
VectorCopy(ent.ambientLight, ambientLight); |
| 458 |
VectorCopy(ent.directedLight, directedLight); |
| 459 |
VectorCopy(ent.lightDir, lightDir); |
| 460 |
|
| 461 |
return qtrue; |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
int R_LightDirForPoint( vec3_t point, vec3_t lightDir, vec3_t normal, world_t *world ) |
| 466 |
{ |
| 467 |
trRefEntity_t ent; |
| 468 |
|
| 469 |
if ( world->lightGridData == NULL ) |
| 470 |
return qfalse; |
| 471 |
|
| 472 |
Com_Memset(&ent, 0, sizeof(ent)); |
| 473 |
VectorCopy( point, ent.e.origin ); |
| 474 |
R_SetupEntityLightingGrid( &ent, world ); |
| 475 |
|
| 476 |
if (DotProduct(ent.lightDir, normal) > 0.2f) |
| 477 |
VectorCopy(ent.lightDir, lightDir); |
| 478 |
else |
| 479 |
VectorCopy(normal, lightDir); |
| 480 |
|
| 481 |
return qtrue; |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
int R_CubemapForPoint( vec3_t point ) |
| 486 |
{ |
| 487 |
int cubemapIndex = -1; |
| 488 |
|
| 489 |
if (r_cubeMapping->integer && tr.numCubemaps) |
| 490 |
{ |
| 491 |
int i; |
| 492 |
vec_t shortest = (float)WORLD_SIZE * (float)WORLD_SIZE; |
| 493 |
|
| 494 |
for (i = 0; i < tr.numCubemaps; i++) |
| 495 |
{ |
| 496 |
vec3_t diff; |
| 497 |
vec_t length; |
| 498 |
|
| 499 |
VectorSubtract(point, tr.cubemaps[i].origin, diff); |
| 500 |
length = DotProduct(diff, diff); |
| 501 |
|
| 502 |
if (shortest > length) |
| 503 |
{ |
| 504 |
shortest = length; |
| 505 |
cubemapIndex = i; |
| 506 |
} |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
return cubemapIndex + 1; |
| 511 |
} |