| 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 = bmodel->firstSurface + i; |
| 96 |
|
| 97 |
if ( *surf->data == SF_FACE ) { |
| 98 |
((srfSurfaceFace_t *)surf->data)->dlightBits = mask; |
| 99 |
} else if ( *surf->data == SF_GRID ) { |
| 100 |
((srfGridMesh_t *)surf->data)->dlightBits = mask; |
| 101 |
} else if ( *surf->data == SF_TRIANGLES ) { |
| 102 |
((srfTriangles_t *)surf->data)->dlightBits = mask; |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
extern cvar_t *r_ambientScale; |
| 117 |
extern cvar_t *r_directedScale; |
| 118 |
extern cvar_t *r_debugLight; |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
static void R_SetupEntityLightingGrid( trRefEntity_t *ent ) { |
| 127 |
vec3_t lightOrigin; |
| 128 |
int pos[3]; |
| 129 |
int i, j; |
| 130 |
byte *gridData; |
| 131 |
float frac[3]; |
| 132 |
int gridStep[3]; |
| 133 |
vec3_t direction; |
| 134 |
float totalFactor; |
| 135 |
|
| 136 |
if ( ent->e.renderfx & RF_LIGHTING_ORIGIN ) { |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
VectorCopy( ent->e.lightingOrigin, lightOrigin ); |
| 141 |
} else { |
| 142 |
VectorCopy( ent->e.origin, lightOrigin ); |
| 143 |
} |
| 144 |
|
| 145 |
VectorSubtract( lightOrigin, tr.world->lightGridOrigin, lightOrigin ); |
| 146 |
for ( i = 0 ; i < 3 ; i++ ) { |
| 147 |
float v; |
| 148 |
|
| 149 |
v = lightOrigin[i]*tr.world->lightGridInverseSize[i]; |
| 150 |
pos[i] = floor( v ); |
| 151 |
frac[i] = v - pos[i]; |
| 152 |
if ( pos[i] < 0 ) { |
| 153 |
pos[i] = 0; |
| 154 |
} else if ( pos[i] > tr.world->lightGridBounds[i] - 1 ) { |
| 155 |
pos[i] = tr.world->lightGridBounds[i] - 1; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
VectorClear( ent->ambientLight ); |
| 160 |
VectorClear( ent->directedLight ); |
| 161 |
VectorClear( direction ); |
| 162 |
|
| 163 |
assert( tr.world->lightGridData ); |
| 164 |
|
| 165 |
|
| 166 |
gridStep[0] = 8; |
| 167 |
gridStep[1] = 8 * tr.world->lightGridBounds[0]; |
| 168 |
gridStep[2] = 8 * tr.world->lightGridBounds[0] * tr.world->lightGridBounds[1]; |
| 169 |
gridData = tr.world->lightGridData + pos[0] * gridStep[0] |
| 170 |
+ pos[1] * gridStep[1] + pos[2] * gridStep[2]; |
| 171 |
|
| 172 |
totalFactor = 0; |
| 173 |
for ( i = 0 ; i < 8 ; i++ ) { |
| 174 |
float factor; |
| 175 |
byte *data; |
| 176 |
int lat, lng; |
| 177 |
vec3_t normal; |
| 178 |
#if idppc |
| 179 |
float d0, d1, d2, d3, d4, d5; |
| 180 |
#endif |
| 181 |
factor = 1.0; |
| 182 |
data = gridData; |
| 183 |
for ( j = 0 ; j < 3 ; j++ ) { |
| 184 |
if ( i & (1<<j) ) { |
| 185 |
if ( pos[j] + 1 > tr.world->lightGridBounds[j] - 1 ) { |
| 186 |
break; |
| 187 |
} |
| 188 |
factor *= frac[j]; |
| 189 |
data += gridStep[j]; |
| 190 |
} else { |
| 191 |
factor *= (1.0f - frac[j]); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
if ( j != 3 ) { |
| 196 |
continue; |
| 197 |
} |
| 198 |
if ( !(data[0]+data[1]+data[2]) ) { |
| 199 |
continue; |
| 200 |
} |
| 201 |
totalFactor += factor; |
| 202 |
#if idppc |
| 203 |
d0 = data[0]; d1 = data[1]; d2 = data[2]; |
| 204 |
d3 = data[3]; d4 = data[4]; d5 = data[5]; |
| 205 |
|
| 206 |
ent->ambientLight[0] += factor * d0; |
| 207 |
ent->ambientLight[1] += factor * d1; |
| 208 |
ent->ambientLight[2] += factor * d2; |
| 209 |
|
| 210 |
ent->directedLight[0] += factor * d3; |
| 211 |
ent->directedLight[1] += factor * d4; |
| 212 |
ent->directedLight[2] += factor * d5; |
| 213 |
#else |
| 214 |
ent->ambientLight[0] += factor * data[0]; |
| 215 |
ent->ambientLight[1] += factor * data[1]; |
| 216 |
ent->ambientLight[2] += factor * data[2]; |
| 217 |
|
| 218 |
ent->directedLight[0] += factor * data[3]; |
| 219 |
ent->directedLight[1] += factor * data[4]; |
| 220 |
ent->directedLight[2] += factor * data[5]; |
| 221 |
#endif |
| 222 |
lat = data[7]; |
| 223 |
lng = data[6]; |
| 224 |
lat *= (FUNCTABLE_SIZE/256); |
| 225 |
lng *= (FUNCTABLE_SIZE/256); |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
normal[0] = tr.sinTable[(lat+(FUNCTABLE_SIZE/4))&FUNCTABLE_MASK] * tr.sinTable[lng]; |
| 232 |
normal[1] = tr.sinTable[lat] * tr.sinTable[lng]; |
| 233 |
normal[2] = tr.sinTable[(lng+(FUNCTABLE_SIZE/4))&FUNCTABLE_MASK]; |
| 234 |
|
| 235 |
VectorMA( direction, factor, normal, direction ); |
| 236 |
} |
| 237 |
|
| 238 |
if ( totalFactor > 0 && totalFactor < 0.99 ) { |
| 239 |
totalFactor = 1.0f / totalFactor; |
| 240 |
VectorScale( ent->ambientLight, totalFactor, ent->ambientLight ); |
| 241 |
VectorScale( ent->directedLight, totalFactor, ent->directedLight ); |
| 242 |
} |
| 243 |
|
| 244 |
VectorScale( ent->ambientLight, r_ambientScale->value, ent->ambientLight ); |
| 245 |
VectorScale( ent->directedLight, r_directedScale->value, ent->directedLight ); |
| 246 |
|
| 247 |
VectorNormalize2( direction, ent->lightDir ); |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
static void LogLight( trRefEntity_t *ent ) { |
| 257 |
int max1, max2; |
| 258 |
|
| 259 |
if ( !(ent->e.renderfx & RF_FIRST_PERSON ) ) { |
| 260 |
return; |
| 261 |
} |
| 262 |
|
| 263 |
max1 = ent->ambientLight[0]; |
| 264 |
if ( ent->ambientLight[1] > max1 ) { |
| 265 |
max1 = ent->ambientLight[1]; |
| 266 |
} else if ( ent->ambientLight[2] > max1 ) { |
| 267 |
max1 = ent->ambientLight[2]; |
| 268 |
} |
| 269 |
|
| 270 |
max2 = ent->directedLight[0]; |
| 271 |
if ( ent->directedLight[1] > max2 ) { |
| 272 |
max2 = ent->directedLight[1]; |
| 273 |
} else if ( ent->directedLight[2] > max2 ) { |
| 274 |
max2 = ent->directedLight[2]; |
| 275 |
} |
| 276 |
|
| 277 |
ri.Printf( PRINT_ALL, "amb:%i dir:%i\n", max1, max2 ); |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
void R_SetupEntityLighting( const trRefdef_t *refdef, trRefEntity_t *ent ) { |
| 289 |
int i; |
| 290 |
dlight_t *dl; |
| 291 |
float power; |
| 292 |
vec3_t dir; |
| 293 |
float d; |
| 294 |
vec3_t lightDir; |
| 295 |
vec3_t lightOrigin; |
| 296 |
|
| 297 |
|
| 298 |
if ( ent->lightingCalculated ) { |
| 299 |
return; |
| 300 |
} |
| 301 |
ent->lightingCalculated = qtrue; |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
if ( ent->e.renderfx & RF_LIGHTING_ORIGIN ) { |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
VectorCopy( ent->e.lightingOrigin, lightOrigin ); |
| 311 |
} else { |
| 312 |
VectorCopy( ent->e.origin, lightOrigin ); |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
if ( !(refdef->rdflags & RDF_NOWORLDMODEL ) |
| 317 |
&& tr.world->lightGridData ) { |
| 318 |
R_SetupEntityLightingGrid( ent ); |
| 319 |
} else { |
| 320 |
ent->ambientLight[0] = ent->ambientLight[1] = |
| 321 |
ent->ambientLight[2] = tr.identityLight * 150; |
| 322 |
ent->directedLight[0] = ent->directedLight[1] = |
| 323 |
ent->directedLight[2] = tr.identityLight * 150; |
| 324 |
VectorCopy( tr.sunDirection, ent->lightDir ); |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
if ( 1 ) { |
| 329 |
|
| 330 |
ent->ambientLight[0] += tr.identityLight * 32; |
| 331 |
ent->ambientLight[1] += tr.identityLight * 32; |
| 332 |
ent->ambientLight[2] += tr.identityLight * 32; |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
d = VectorLength( ent->directedLight ); |
| 339 |
VectorScale( ent->lightDir, d, lightDir ); |
| 340 |
|
| 341 |
for ( i = 0 ; i < refdef->num_dlights ; i++ ) { |
| 342 |
dl = &refdef->dlights[i]; |
| 343 |
VectorSubtract( dl->origin, lightOrigin, dir ); |
| 344 |
d = VectorNormalize( dir ); |
| 345 |
|
| 346 |
power = DLIGHT_AT_RADIUS * ( dl->radius * dl->radius ); |
| 347 |
if ( d < DLIGHT_MINIMUM_RADIUS ) { |
| 348 |
d = DLIGHT_MINIMUM_RADIUS; |
| 349 |
} |
| 350 |
d = power / ( d * d ); |
| 351 |
|
| 352 |
VectorMA( ent->directedLight, d, dl->color, ent->directedLight ); |
| 353 |
VectorMA( lightDir, d, dir, lightDir ); |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
for ( i = 0 ; i < 3 ; i++ ) { |
| 358 |
if ( ent->ambientLight[i] > tr.identityLightByte ) { |
| 359 |
ent->ambientLight[i] = tr.identityLightByte; |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
if ( r_debugLight->integer ) { |
| 364 |
LogLight( ent ); |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
((byte *)&ent->ambientLightInt)[0] = ri.ftol(ent->ambientLight[0]); |
| 369 |
((byte *)&ent->ambientLightInt)[1] = ri.ftol(ent->ambientLight[1]); |
| 370 |
((byte *)&ent->ambientLightInt)[2] = ri.ftol(ent->ambientLight[2]); |
| 371 |
((byte *)&ent->ambientLightInt)[3] = 0xff; |
| 372 |
|
| 373 |
|
| 374 |
VectorNormalize( lightDir ); |
| 375 |
ent->lightDir[0] = DotProduct( lightDir, ent->e.axis[0] ); |
| 376 |
ent->lightDir[1] = DotProduct( lightDir, ent->e.axis[1] ); |
| 377 |
ent->lightDir[2] = DotProduct( lightDir, ent->e.axis[2] ); |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
int R_LightForPoint( vec3_t point, vec3_t ambientLight, vec3_t directedLight, vec3_t lightDir ) |
| 386 |
{ |
| 387 |
trRefEntity_t ent; |
| 388 |
|
| 389 |
if ( tr.world->lightGridData == NULL ) |
| 390 |
return qfalse; |
| 391 |
|
| 392 |
Com_Memset(&ent, 0, sizeof(ent)); |
| 393 |
VectorCopy( point, ent.e.origin ); |
| 394 |
R_SetupEntityLightingGrid( &ent ); |
| 395 |
VectorCopy(ent.ambientLight, ambientLight); |
| 396 |
VectorCopy(ent.directedLight, directedLight); |
| 397 |
VectorCopy(ent.lightDir, lightDir); |
| 398 |
|
| 399 |
return qtrue; |
| 400 |
} |