| 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 |
#include <string.h> |
| 27 |
|
| 28 |
trGlobals_t tr; |
| 29 |
|
| 30 |
static float s_flipMatrix[16] = { |
| 31 |
|
| 32 |
|
| 33 |
0, 0, -1, 0, |
| 34 |
-1, 0, 0, 0, |
| 35 |
0, 1, 0, 0, |
| 36 |
0, 0, 0, 1 |
| 37 |
}; |
| 38 |
|
| 39 |
|
| 40 |
refimport_t ri; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
surfaceType_t entitySurface = SF_ENTITY; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
int R_CullLocalBox (vec3_t bounds[2]) { |
| 54 |
int i, j; |
| 55 |
vec3_t transformed[8]; |
| 56 |
float dists[8]; |
| 57 |
vec3_t v; |
| 58 |
cplane_t *frust; |
| 59 |
int anyBack; |
| 60 |
int front, back; |
| 61 |
|
| 62 |
if ( r_nocull->integer ) { |
| 63 |
return CULL_CLIP; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
for (i = 0 ; i < 8 ; i++) { |
| 68 |
v[0] = bounds[i&1][0]; |
| 69 |
v[1] = bounds[(i>>1)&1][1]; |
| 70 |
v[2] = bounds[(i>>2)&1][2]; |
| 71 |
|
| 72 |
VectorCopy( tr.or.origin, transformed[i] ); |
| 73 |
VectorMA( transformed[i], v[0], tr.or.axis[0], transformed[i] ); |
| 74 |
VectorMA( transformed[i], v[1], tr.or.axis[1], transformed[i] ); |
| 75 |
VectorMA( transformed[i], v[2], tr.or.axis[2], transformed[i] ); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
anyBack = 0; |
| 80 |
for (i = 0 ; i < 4 ; i++) { |
| 81 |
frust = &tr.viewParms.frustum[i]; |
| 82 |
|
| 83 |
front = back = 0; |
| 84 |
for (j = 0 ; j < 8 ; j++) { |
| 85 |
dists[j] = DotProduct(transformed[j], frust->normal); |
| 86 |
if ( dists[j] > frust->dist ) { |
| 87 |
front = 1; |
| 88 |
if ( back ) { |
| 89 |
break; |
| 90 |
} |
| 91 |
} else { |
| 92 |
back = 1; |
| 93 |
} |
| 94 |
} |
| 95 |
if ( !front ) { |
| 96 |
|
| 97 |
return CULL_OUT; |
| 98 |
} |
| 99 |
anyBack |= back; |
| 100 |
} |
| 101 |
|
| 102 |
if ( !anyBack ) { |
| 103 |
return CULL_IN; |
| 104 |
} |
| 105 |
|
| 106 |
return CULL_CLIP; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
int R_CullLocalPointAndRadius( vec3_t pt, float radius ) |
| 113 |
{ |
| 114 |
vec3_t transformed; |
| 115 |
|
| 116 |
R_LocalPointToWorld( pt, transformed ); |
| 117 |
|
| 118 |
return R_CullPointAndRadius( transformed, radius ); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
int R_CullPointAndRadius( vec3_t pt, float radius ) |
| 125 |
{ |
| 126 |
int i; |
| 127 |
float dist; |
| 128 |
cplane_t *frust; |
| 129 |
qboolean mightBeClipped = qfalse; |
| 130 |
|
| 131 |
if ( r_nocull->integer ) { |
| 132 |
return CULL_CLIP; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
for (i = 0 ; i < 4 ; i++) |
| 137 |
{ |
| 138 |
frust = &tr.viewParms.frustum[i]; |
| 139 |
|
| 140 |
dist = DotProduct( pt, frust->normal) - frust->dist; |
| 141 |
if ( dist < -radius ) |
| 142 |
{ |
| 143 |
return CULL_OUT; |
| 144 |
} |
| 145 |
else if ( dist <= radius ) |
| 146 |
{ |
| 147 |
mightBeClipped = qtrue; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
if ( mightBeClipped ) |
| 152 |
{ |
| 153 |
return CULL_CLIP; |
| 154 |
} |
| 155 |
|
| 156 |
return CULL_IN; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
void R_LocalNormalToWorld (vec3_t local, vec3_t world) { |
| 167 |
world[0] = local[0] * tr.or.axis[0][0] + local[1] * tr.or.axis[1][0] + local[2] * tr.or.axis[2][0]; |
| 168 |
world[1] = local[0] * tr.or.axis[0][1] + local[1] * tr.or.axis[1][1] + local[2] * tr.or.axis[2][1]; |
| 169 |
world[2] = local[0] * tr.or.axis[0][2] + local[1] * tr.or.axis[1][2] + local[2] * tr.or.axis[2][2]; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
void R_LocalPointToWorld (vec3_t local, vec3_t world) { |
| 179 |
world[0] = local[0] * tr.or.axis[0][0] + local[1] * tr.or.axis[1][0] + local[2] * tr.or.axis[2][0] + tr.or.origin[0]; |
| 180 |
world[1] = local[0] * tr.or.axis[0][1] + local[1] * tr.or.axis[1][1] + local[2] * tr.or.axis[2][1] + tr.or.origin[1]; |
| 181 |
world[2] = local[0] * tr.or.axis[0][2] + local[1] * tr.or.axis[1][2] + local[2] * tr.or.axis[2][2] + tr.or.origin[2]; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
void R_WorldToLocal (vec3_t world, vec3_t local) { |
| 191 |
local[0] = DotProduct(world, tr.or.axis[0]); |
| 192 |
local[1] = DotProduct(world, tr.or.axis[1]); |
| 193 |
local[2] = DotProduct(world, tr.or.axis[2]); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
void R_TransformModelToClip( const vec3_t src, const float *modelMatrix, const float *projectionMatrix, |
| 203 |
vec4_t eye, vec4_t dst ) { |
| 204 |
int i; |
| 205 |
|
| 206 |
for ( i = 0 ; i < 4 ; i++ ) { |
| 207 |
eye[i] = |
| 208 |
src[0] * modelMatrix[ i + 0 * 4 ] + |
| 209 |
src[1] * modelMatrix[ i + 1 * 4 ] + |
| 210 |
src[2] * modelMatrix[ i + 2 * 4 ] + |
| 211 |
1 * modelMatrix[ i + 3 * 4 ]; |
| 212 |
} |
| 213 |
|
| 214 |
for ( i = 0 ; i < 4 ; i++ ) { |
| 215 |
dst[i] = |
| 216 |
eye[0] * projectionMatrix[ i + 0 * 4 ] + |
| 217 |
eye[1] * projectionMatrix[ i + 1 * 4 ] + |
| 218 |
eye[2] * projectionMatrix[ i + 2 * 4 ] + |
| 219 |
eye[3] * projectionMatrix[ i + 3 * 4 ]; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
void R_TransformClipToWindow( const vec4_t clip, const viewParms_t *view, vec4_t normalized, vec4_t window ) { |
| 230 |
normalized[0] = clip[0] / clip[3]; |
| 231 |
normalized[1] = clip[1] / clip[3]; |
| 232 |
normalized[2] = ( clip[2] + clip[3] ) / ( 2 * clip[3] ); |
| 233 |
|
| 234 |
window[0] = 0.5f * ( 1.0f + normalized[0] ) * view->viewportWidth; |
| 235 |
window[1] = 0.5f * ( 1.0f + normalized[1] ) * view->viewportHeight; |
| 236 |
window[2] = normalized[2]; |
| 237 |
|
| 238 |
window[0] = (int) ( window[0] + 0.5 ); |
| 239 |
window[1] = (int) ( window[1] + 0.5 ); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
void myGlMultMatrix( const float *a, const float *b, float *out ) { |
| 250 |
int i, j; |
| 251 |
|
| 252 |
for ( i = 0 ; i < 4 ; i++ ) { |
| 253 |
for ( j = 0 ; j < 4 ; j++ ) { |
| 254 |
out[ i * 4 + j ] = |
| 255 |
a [ i * 4 + 0 ] * b [ 0 * 4 + j ] |
| 256 |
+ a [ i * 4 + 1 ] * b [ 1 * 4 + j ] |
| 257 |
+ a [ i * 4 + 2 ] * b [ 2 * 4 + j ] |
| 258 |
+ a [ i * 4 + 3 ] * b [ 3 * 4 + j ]; |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
void R_RotateForEntity( const trRefEntity_t *ent, const viewParms_t *viewParms, |
| 273 |
orientationr_t *or ) { |
| 274 |
float glMatrix[16]; |
| 275 |
vec3_t delta; |
| 276 |
float axisLength; |
| 277 |
|
| 278 |
if ( ent->e.reType != RT_MODEL ) { |
| 279 |
*or = viewParms->world; |
| 280 |
return; |
| 281 |
} |
| 282 |
|
| 283 |
VectorCopy( ent->e.origin, or->origin ); |
| 284 |
|
| 285 |
VectorCopy( ent->e.axis[0], or->axis[0] ); |
| 286 |
VectorCopy( ent->e.axis[1], or->axis[1] ); |
| 287 |
VectorCopy( ent->e.axis[2], or->axis[2] ); |
| 288 |
|
| 289 |
glMatrix[0] = or->axis[0][0]; |
| 290 |
glMatrix[4] = or->axis[1][0]; |
| 291 |
glMatrix[8] = or->axis[2][0]; |
| 292 |
glMatrix[12] = or->origin[0]; |
| 293 |
|
| 294 |
glMatrix[1] = or->axis[0][1]; |
| 295 |
glMatrix[5] = or->axis[1][1]; |
| 296 |
glMatrix[9] = or->axis[2][1]; |
| 297 |
glMatrix[13] = or->origin[1]; |
| 298 |
|
| 299 |
glMatrix[2] = or->axis[0][2]; |
| 300 |
glMatrix[6] = or->axis[1][2]; |
| 301 |
glMatrix[10] = or->axis[2][2]; |
| 302 |
glMatrix[14] = or->origin[2]; |
| 303 |
|
| 304 |
glMatrix[3] = 0; |
| 305 |
glMatrix[7] = 0; |
| 306 |
glMatrix[11] = 0; |
| 307 |
glMatrix[15] = 1; |
| 308 |
|
| 309 |
myGlMultMatrix( glMatrix, viewParms->world.modelMatrix, or->modelMatrix ); |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
VectorSubtract( viewParms->or.origin, or->origin, delta ); |
| 314 |
|
| 315 |
|
| 316 |
if ( ent->e.nonNormalizedAxes ) { |
| 317 |
axisLength = VectorLength( ent->e.axis[0] ); |
| 318 |
if ( !axisLength ) { |
| 319 |
axisLength = 0; |
| 320 |
} else { |
| 321 |
axisLength = 1.0f / axisLength; |
| 322 |
} |
| 323 |
} else { |
| 324 |
axisLength = 1.0f; |
| 325 |
} |
| 326 |
|
| 327 |
or->viewOrigin[0] = DotProduct( delta, or->axis[0] ) * axisLength; |
| 328 |
or->viewOrigin[1] = DotProduct( delta, or->axis[1] ) * axisLength; |
| 329 |
or->viewOrigin[2] = DotProduct( delta, or->axis[2] ) * axisLength; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
void R_RotateForViewer (void) |
| 340 |
{ |
| 341 |
float viewerMatrix[16]; |
| 342 |
vec3_t origin; |
| 343 |
|
| 344 |
Com_Memset (&tr.or, 0, sizeof(tr.or)); |
| 345 |
tr.or.axis[0][0] = 1; |
| 346 |
tr.or.axis[1][1] = 1; |
| 347 |
tr.or.axis[2][2] = 1; |
| 348 |
VectorCopy (tr.viewParms.or.origin, tr.or.viewOrigin); |
| 349 |
|
| 350 |
|
| 351 |
VectorCopy( tr.viewParms.or.origin, origin ); |
| 352 |
|
| 353 |
viewerMatrix[0] = tr.viewParms.or.axis[0][0]; |
| 354 |
viewerMatrix[4] = tr.viewParms.or.axis[0][1]; |
| 355 |
viewerMatrix[8] = tr.viewParms.or.axis[0][2]; |
| 356 |
viewerMatrix[12] = -origin[0] * viewerMatrix[0] + -origin[1] * viewerMatrix[4] + -origin[2] * viewerMatrix[8]; |
| 357 |
|
| 358 |
viewerMatrix[1] = tr.viewParms.or.axis[1][0]; |
| 359 |
viewerMatrix[5] = tr.viewParms.or.axis[1][1]; |
| 360 |
viewerMatrix[9] = tr.viewParms.or.axis[1][2]; |
| 361 |
viewerMatrix[13] = -origin[0] * viewerMatrix[1] + -origin[1] * viewerMatrix[5] + -origin[2] * viewerMatrix[9]; |
| 362 |
|
| 363 |
viewerMatrix[2] = tr.viewParms.or.axis[2][0]; |
| 364 |
viewerMatrix[6] = tr.viewParms.or.axis[2][1]; |
| 365 |
viewerMatrix[10] = tr.viewParms.or.axis[2][2]; |
| 366 |
viewerMatrix[14] = -origin[0] * viewerMatrix[2] + -origin[1] * viewerMatrix[6] + -origin[2] * viewerMatrix[10]; |
| 367 |
|
| 368 |
viewerMatrix[3] = 0; |
| 369 |
viewerMatrix[7] = 0; |
| 370 |
viewerMatrix[11] = 0; |
| 371 |
viewerMatrix[15] = 1; |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
myGlMultMatrix( viewerMatrix, s_flipMatrix, tr.or.modelMatrix ); |
| 376 |
|
| 377 |
tr.viewParms.world = tr.or; |
| 378 |
|
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
static void R_SetFarClip( void ) |
| 385 |
{ |
| 386 |
float farthestCornerDistance = 0; |
| 387 |
int i; |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
if ( tr.refdef.rdflags & RDF_NOWORLDMODEL ) { |
| 392 |
tr.viewParms.zFar = 2048; |
| 393 |
return; |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
farthestCornerDistance = 0; |
| 400 |
for ( i = 0; i < 8; i++ ) |
| 401 |
{ |
| 402 |
vec3_t v; |
| 403 |
vec3_t vecTo; |
| 404 |
float distance; |
| 405 |
|
| 406 |
if ( i & 1 ) |
| 407 |
{ |
| 408 |
v[0] = tr.viewParms.visBounds[0][0]; |
| 409 |
} |
| 410 |
else |
| 411 |
{ |
| 412 |
v[0] = tr.viewParms.visBounds[1][0]; |
| 413 |
} |
| 414 |
|
| 415 |
if ( i & 2 ) |
| 416 |
{ |
| 417 |
v[1] = tr.viewParms.visBounds[0][1]; |
| 418 |
} |
| 419 |
else |
| 420 |
{ |
| 421 |
v[1] = tr.viewParms.visBounds[1][1]; |
| 422 |
} |
| 423 |
|
| 424 |
if ( i & 4 ) |
| 425 |
{ |
| 426 |
v[2] = tr.viewParms.visBounds[0][2]; |
| 427 |
} |
| 428 |
else |
| 429 |
{ |
| 430 |
v[2] = tr.viewParms.visBounds[1][2]; |
| 431 |
} |
| 432 |
|
| 433 |
VectorSubtract( v, tr.viewParms.or.origin, vecTo ); |
| 434 |
|
| 435 |
distance = vecTo[0] * vecTo[0] + vecTo[1] * vecTo[1] + vecTo[2] * vecTo[2]; |
| 436 |
|
| 437 |
if ( distance > farthestCornerDistance ) |
| 438 |
{ |
| 439 |
farthestCornerDistance = distance; |
| 440 |
} |
| 441 |
} |
| 442 |
tr.viewParms.zFar = sqrt( farthestCornerDistance ); |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
void R_SetupFrustum (viewParms_t *dest, float xmin, float xmax, float ymax, float zProj, float stereoSep) |
| 454 |
{ |
| 455 |
vec3_t ofsorigin; |
| 456 |
float oppleg, adjleg, length; |
| 457 |
int i; |
| 458 |
|
| 459 |
if(stereoSep == 0 && xmin == -xmax) |
| 460 |
{ |
| 461 |
|
| 462 |
VectorCopy(dest->or.origin, ofsorigin); |
| 463 |
|
| 464 |
length = sqrt(xmax * xmax + zProj * zProj); |
| 465 |
oppleg = xmax / length; |
| 466 |
adjleg = zProj / length; |
| 467 |
|
| 468 |
VectorScale(dest->or.axis[0], oppleg, dest->frustum[0].normal); |
| 469 |
VectorMA(dest->frustum[0].normal, adjleg, dest->or.axis[1], dest->frustum[0].normal); |
| 470 |
|
| 471 |
VectorScale(dest->or.axis[0], oppleg, dest->frustum[1].normal); |
| 472 |
VectorMA(dest->frustum[1].normal, -adjleg, dest->or.axis[1], dest->frustum[1].normal); |
| 473 |
} |
| 474 |
else |
| 475 |
{ |
| 476 |
|
| 477 |
|
| 478 |
VectorMA(dest->or.origin, stereoSep, dest->or.axis[1], ofsorigin); |
| 479 |
|
| 480 |
oppleg = xmax + stereoSep; |
| 481 |
length = sqrt(oppleg * oppleg + zProj * zProj); |
| 482 |
VectorScale(dest->or.axis[0], oppleg / length, dest->frustum[0].normal); |
| 483 |
VectorMA(dest->frustum[0].normal, zProj / length, dest->or.axis[1], dest->frustum[0].normal); |
| 484 |
|
| 485 |
oppleg = xmin + stereoSep; |
| 486 |
length = sqrt(oppleg * oppleg + zProj * zProj); |
| 487 |
VectorScale(dest->or.axis[0], -oppleg / length, dest->frustum[1].normal); |
| 488 |
VectorMA(dest->frustum[1].normal, -zProj / length, dest->or.axis[1], dest->frustum[1].normal); |
| 489 |
} |
| 490 |
|
| 491 |
length = sqrt(ymax * ymax + zProj * zProj); |
| 492 |
oppleg = ymax / length; |
| 493 |
adjleg = zProj / length; |
| 494 |
|
| 495 |
VectorScale(dest->or.axis[0], oppleg, dest->frustum[2].normal); |
| 496 |
VectorMA(dest->frustum[2].normal, adjleg, dest->or.axis[2], dest->frustum[2].normal); |
| 497 |
|
| 498 |
VectorScale(dest->or.axis[0], oppleg, dest->frustum[3].normal); |
| 499 |
VectorMA(dest->frustum[3].normal, -adjleg, dest->or.axis[2], dest->frustum[3].normal); |
| 500 |
|
| 501 |
for (i=0 ; i<4 ; i++) { |
| 502 |
dest->frustum[i].type = PLANE_NON_AXIAL; |
| 503 |
dest->frustum[i].dist = DotProduct (ofsorigin, dest->frustum[i].normal); |
| 504 |
SetPlaneSignbits( &dest->frustum[i] ); |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
void R_SetupProjection(viewParms_t *dest, float zProj, qboolean computeFrustum) |
| 514 |
{ |
| 515 |
float xmin, xmax, ymin, ymax; |
| 516 |
float width, height, stereoSep = r_stereoSeparation->value; |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
if(stereoSep != 0) |
| 524 |
{ |
| 525 |
if(dest->stereoFrame == STEREO_LEFT) |
| 526 |
stereoSep = zProj / stereoSep; |
| 527 |
else if(dest->stereoFrame == STEREO_RIGHT) |
| 528 |
stereoSep = zProj / -stereoSep; |
| 529 |
else |
| 530 |
stereoSep = 0; |
| 531 |
} |
| 532 |
|
| 533 |
ymax = zProj * tan(dest->fovY * M_PI / 360.0f); |
| 534 |
ymin = -ymax; |
| 535 |
|
| 536 |
xmax = zProj * tan(dest->fovX * M_PI / 360.0f); |
| 537 |
xmin = -xmax; |
| 538 |
|
| 539 |
width = xmax - xmin; |
| 540 |
height = ymax - ymin; |
| 541 |
|
| 542 |
dest->projectionMatrix[0] = 2 * zProj / width; |
| 543 |
dest->projectionMatrix[4] = 0; |
| 544 |
dest->projectionMatrix[8] = (xmax + xmin + 2 * stereoSep) / width; |
| 545 |
dest->projectionMatrix[12] = 2 * zProj * stereoSep / width; |
| 546 |
|
| 547 |
dest->projectionMatrix[1] = 0; |
| 548 |
dest->projectionMatrix[5] = 2 * zProj / height; |
| 549 |
dest->projectionMatrix[9] = ( ymax + ymin ) / height; |
| 550 |
dest->projectionMatrix[13] = 0; |
| 551 |
|
| 552 |
dest->projectionMatrix[3] = 0; |
| 553 |
dest->projectionMatrix[7] = 0; |
| 554 |
dest->projectionMatrix[11] = -1; |
| 555 |
dest->projectionMatrix[15] = 0; |
| 556 |
|
| 557 |
|
| 558 |
if(computeFrustum) |
| 559 |
R_SetupFrustum(dest, xmin, xmax, ymax, zProj, stereoSep); |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
void R_SetupProjectionZ(viewParms_t *dest) |
| 570 |
{ |
| 571 |
float zNear, zFar, depth; |
| 572 |
|
| 573 |
zNear = r_znear->value; |
| 574 |
zFar = dest->zFar; |
| 575 |
depth = zFar - zNear; |
| 576 |
|
| 577 |
dest->projectionMatrix[2] = 0; |
| 578 |
dest->projectionMatrix[6] = 0; |
| 579 |
dest->projectionMatrix[10] = -( zFar + zNear ) / depth; |
| 580 |
dest->projectionMatrix[14] = -2 * zFar * zNear / depth; |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
void R_MirrorPoint (vec3_t in, orientation_t *surface, orientation_t *camera, vec3_t out) { |
| 589 |
int i; |
| 590 |
vec3_t local; |
| 591 |
vec3_t transformed; |
| 592 |
float d; |
| 593 |
|
| 594 |
VectorSubtract( in, surface->origin, local ); |
| 595 |
|
| 596 |
VectorClear( transformed ); |
| 597 |
for ( i = 0 ; i < 3 ; i++ ) { |
| 598 |
d = DotProduct(local, surface->axis[i]); |
| 599 |
VectorMA( transformed, d, camera->axis[i], transformed ); |
| 600 |
} |
| 601 |
|
| 602 |
VectorAdd( transformed, camera->origin, out ); |
| 603 |
} |
| 604 |
|
| 605 |
void R_MirrorVector (vec3_t in, orientation_t *surface, orientation_t *camera, vec3_t out) { |
| 606 |
int i; |
| 607 |
float d; |
| 608 |
|
| 609 |
VectorClear( out ); |
| 610 |
for ( i = 0 ; i < 3 ; i++ ) { |
| 611 |
d = DotProduct(in, surface->axis[i]); |
| 612 |
VectorMA( out, d, camera->axis[i], out ); |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
void R_PlaneForSurface (surfaceType_t *surfType, cplane_t *plane) { |
| 623 |
srfTriangles_t *tri; |
| 624 |
srfPoly_t *poly; |
| 625 |
drawVert_t *v1, *v2, *v3; |
| 626 |
vec4_t plane4; |
| 627 |
|
| 628 |
if (!surfType) { |
| 629 |
Com_Memset (plane, 0, sizeof(*plane)); |
| 630 |
plane->normal[0] = 1; |
| 631 |
return; |
| 632 |
} |
| 633 |
switch (*surfType) { |
| 634 |
case SF_FACE: |
| 635 |
*plane = ((srfSurfaceFace_t *)surfType)->plane; |
| 636 |
return; |
| 637 |
case SF_TRIANGLES: |
| 638 |
tri = (srfTriangles_t *)surfType; |
| 639 |
v1 = tri->verts + tri->indexes[0]; |
| 640 |
v2 = tri->verts + tri->indexes[1]; |
| 641 |
v3 = tri->verts + tri->indexes[2]; |
| 642 |
PlaneFromPoints( plane4, v1->xyz, v2->xyz, v3->xyz ); |
| 643 |
VectorCopy( plane4, plane->normal ); |
| 644 |
plane->dist = plane4[3]; |
| 645 |
return; |
| 646 |
case SF_POLY: |
| 647 |
poly = (srfPoly_t *)surfType; |
| 648 |
PlaneFromPoints( plane4, poly->verts[0].xyz, poly->verts[1].xyz, poly->verts[2].xyz ); |
| 649 |
VectorCopy( plane4, plane->normal ); |
| 650 |
plane->dist = plane4[3]; |
| 651 |
return; |
| 652 |
default: |
| 653 |
Com_Memset (plane, 0, sizeof(*plane)); |
| 654 |
plane->normal[0] = 1; |
| 655 |
return; |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
qboolean R_GetPortalOrientations( drawSurf_t *drawSurf, int entityNum, |
| 670 |
orientation_t *surface, orientation_t *camera, |
| 671 |
vec3_t pvsOrigin, qboolean *mirror ) { |
| 672 |
int i; |
| 673 |
cplane_t originalPlane, plane; |
| 674 |
trRefEntity_t *e; |
| 675 |
float d; |
| 676 |
vec3_t transformed; |
| 677 |
|
| 678 |
|
| 679 |
R_PlaneForSurface( drawSurf->surface, &originalPlane ); |
| 680 |
|
| 681 |
|
| 682 |
if ( entityNum != REFENTITYNUM_WORLD ) { |
| 683 |
tr.currentEntityNum = entityNum; |
| 684 |
tr.currentEntity = &tr.refdef.entities[entityNum]; |
| 685 |
|
| 686 |
|
| 687 |
R_RotateForEntity( tr.currentEntity, &tr.viewParms, &tr.or ); |
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
R_LocalNormalToWorld( originalPlane.normal, plane.normal ); |
| 692 |
plane.dist = originalPlane.dist + DotProduct( plane.normal, tr.or.origin ); |
| 693 |
|
| 694 |
|
| 695 |
originalPlane.dist = originalPlane.dist + DotProduct( originalPlane.normal, tr.or.origin ); |
| 696 |
} else { |
| 697 |
plane = originalPlane; |
| 698 |
} |
| 699 |
|
| 700 |
VectorCopy( plane.normal, surface->axis[0] ); |
| 701 |
PerpendicularVector( surface->axis[1], surface->axis[0] ); |
| 702 |
CrossProduct( surface->axis[0], surface->axis[1], surface->axis[2] ); |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
for ( i = 0 ; i < tr.refdef.num_entities ; i++ ) { |
| 708 |
e = &tr.refdef.entities[i]; |
| 709 |
if ( e->e.reType != RT_PORTALSURFACE ) { |
| 710 |
continue; |
| 711 |
} |
| 712 |
|
| 713 |
d = DotProduct( e->e.origin, originalPlane.normal ) - originalPlane.dist; |
| 714 |
if ( d > 64 || d < -64) { |
| 715 |
continue; |
| 716 |
} |
| 717 |
|
| 718 |
|
| 719 |
VectorCopy( e->e.oldorigin, pvsOrigin ); |
| 720 |
|
| 721 |
|
| 722 |
if ( e->e.oldorigin[0] == e->e.origin[0] && |
| 723 |
e->e.oldorigin[1] == e->e.origin[1] && |
| 724 |
e->e.oldorigin[2] == e->e.origin[2] ) { |
| 725 |
VectorScale( plane.normal, plane.dist, surface->origin ); |
| 726 |
VectorCopy( surface->origin, camera->origin ); |
| 727 |
VectorSubtract( vec3_origin, surface->axis[0], camera->axis[0] ); |
| 728 |
VectorCopy( surface->axis[1], camera->axis[1] ); |
| 729 |
VectorCopy( surface->axis[2], camera->axis[2] ); |
| 730 |
|
| 731 |
*mirror = qtrue; |
| 732 |
return qtrue; |
| 733 |
} |
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
d = DotProduct( e->e.origin, plane.normal ) - plane.dist; |
| 738 |
VectorMA( e->e.origin, -d, surface->axis[0], surface->origin ); |
| 739 |
|
| 740 |
|
| 741 |
VectorCopy( e->e.oldorigin, camera->origin ); |
| 742 |
AxisCopy( e->e.axis, camera->axis ); |
| 743 |
VectorSubtract( vec3_origin, camera->axis[0], camera->axis[0] ); |
| 744 |
VectorSubtract( vec3_origin, camera->axis[1], camera->axis[1] ); |
| 745 |
|
| 746 |
|
| 747 |
if ( e->e.oldframe ) { |
| 748 |
|
| 749 |
if ( e->e.frame ) { |
| 750 |
|
| 751 |
d = (tr.refdef.time/1000.0f) * e->e.frame; |
| 752 |
VectorCopy( camera->axis[1], transformed ); |
| 753 |
RotatePointAroundVector( camera->axis[1], camera->axis[0], transformed, d ); |
| 754 |
CrossProduct( camera->axis[0], camera->axis[1], camera->axis[2] ); |
| 755 |
} else { |
| 756 |
|
| 757 |
d = sin( tr.refdef.time * 0.003f ); |
| 758 |
d = e->e.skinNum + d * 4; |
| 759 |
VectorCopy( camera->axis[1], transformed ); |
| 760 |
RotatePointAroundVector( camera->axis[1], camera->axis[0], transformed, d ); |
| 761 |
CrossProduct( camera->axis[0], camera->axis[1], camera->axis[2] ); |
| 762 |
} |
| 763 |
} |
| 764 |
else if ( e->e.skinNum ) { |
| 765 |
d = e->e.skinNum; |
| 766 |
VectorCopy( camera->axis[1], transformed ); |
| 767 |
RotatePointAroundVector( camera->axis[1], camera->axis[0], transformed, d ); |
| 768 |
CrossProduct( camera->axis[0], camera->axis[1], camera->axis[2] ); |
| 769 |
} |
| 770 |
*mirror = qfalse; |
| 771 |
return qtrue; |
| 772 |
} |
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
return qfalse; |
| 786 |
} |
| 787 |
|
| 788 |
static qboolean IsMirror( const drawSurf_t *drawSurf, int entityNum ) |
| 789 |
{ |
| 790 |
int i; |
| 791 |
cplane_t originalPlane, plane; |
| 792 |
trRefEntity_t *e; |
| 793 |
float d; |
| 794 |
|
| 795 |
|
| 796 |
R_PlaneForSurface( drawSurf->surface, &originalPlane ); |
| 797 |
|
| 798 |
|
| 799 |
if ( entityNum != REFENTITYNUM_WORLD ) |
| 800 |
{ |
| 801 |
tr.currentEntityNum = entityNum; |
| 802 |
tr.currentEntity = &tr.refdef.entities[entityNum]; |
| 803 |
|
| 804 |
|
| 805 |
R_RotateForEntity( tr.currentEntity, &tr.viewParms, &tr.or ); |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
R_LocalNormalToWorld( originalPlane.normal, plane.normal ); |
| 810 |
plane.dist = originalPlane.dist + DotProduct( plane.normal, tr.or.origin ); |
| 811 |
|
| 812 |
|
| 813 |
originalPlane.dist = originalPlane.dist + DotProduct( originalPlane.normal, tr.or.origin ); |
| 814 |
} |
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
for ( i = 0 ; i < tr.refdef.num_entities ; i++ ) |
| 820 |
{ |
| 821 |
e = &tr.refdef.entities[i]; |
| 822 |
if ( e->e.reType != RT_PORTALSURFACE ) { |
| 823 |
continue; |
| 824 |
} |
| 825 |
|
| 826 |
d = DotProduct( e->e.origin, originalPlane.normal ) - originalPlane.dist; |
| 827 |
if ( d > 64 || d < -64) { |
| 828 |
continue; |
| 829 |
} |
| 830 |
|
| 831 |
|
| 832 |
if ( e->e.oldorigin[0] == e->e.origin[0] && |
| 833 |
e->e.oldorigin[1] == e->e.origin[1] && |
| 834 |
e->e.oldorigin[2] == e->e.origin[2] ) |
| 835 |
{ |
| 836 |
return qtrue; |
| 837 |
} |
| 838 |
|
| 839 |
return qfalse; |
| 840 |
} |
| 841 |
return qfalse; |
| 842 |
} |
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
static qboolean SurfIsOffscreen( const drawSurf_t *drawSurf, vec4_t clipDest[128] ) { |
| 850 |
float shortest = 100000000; |
| 851 |
int entityNum; |
| 852 |
int numTriangles; |
| 853 |
shader_t *shader; |
| 854 |
int fogNum; |
| 855 |
int dlighted; |
| 856 |
vec4_t clip, eye; |
| 857 |
int i; |
| 858 |
unsigned int pointAnd = (unsigned int)~0; |
| 859 |
|
| 860 |
R_RotateForViewer(); |
| 861 |
|
| 862 |
R_DecomposeSort( drawSurf->sort, &entityNum, &shader, &fogNum, &dlighted ); |
| 863 |
RB_BeginSurface( shader, fogNum ); |
| 864 |
rb_surfaceTable[ *drawSurf->surface ]( drawSurf->surface ); |
| 865 |
|
| 866 |
assert( tess.numVertexes < 128 ); |
| 867 |
|
| 868 |
for ( i = 0; i < tess.numVertexes; i++ ) |
| 869 |
{ |
| 870 |
int j; |
| 871 |
unsigned int pointFlags = 0; |
| 872 |
|
| 873 |
R_TransformModelToClip( tess.xyz[i], tr.or.modelMatrix, tr.viewParms.projectionMatrix, eye, clip ); |
| 874 |
|
| 875 |
for ( j = 0; j < 3; j++ ) |
| 876 |
{ |
| 877 |
if ( clip[j] >= clip[3] ) |
| 878 |
{ |
| 879 |
pointFlags |= (1 << (j*2)); |
| 880 |
} |
| 881 |
else if ( clip[j] <= -clip[3] ) |
| 882 |
{ |
| 883 |
pointFlags |= ( 1 << (j*2+1)); |
| 884 |
} |
| 885 |
} |
| 886 |
pointAnd &= pointFlags; |
| 887 |
} |
| 888 |
|
| 889 |
|
| 890 |
if ( pointAnd ) |
| 891 |
{ |
| 892 |
return qtrue; |
| 893 |
} |
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
numTriangles = tess.numIndexes / 3; |
| 901 |
|
| 902 |
for ( i = 0; i < tess.numIndexes; i += 3 ) |
| 903 |
{ |
| 904 |
vec3_t normal; |
| 905 |
float len; |
| 906 |
|
| 907 |
VectorSubtract( tess.xyz[tess.indexes[i]], tr.viewParms.or.origin, normal ); |
| 908 |
|
| 909 |
len = VectorLengthSquared( normal ); |
| 910 |
if ( len < shortest ) |
| 911 |
{ |
| 912 |
shortest = len; |
| 913 |
} |
| 914 |
|
| 915 |
if ( DotProduct( normal, tess.normal[tess.indexes[i]] ) >= 0 ) |
| 916 |
{ |
| 917 |
numTriangles--; |
| 918 |
} |
| 919 |
} |
| 920 |
if ( !numTriangles ) |
| 921 |
{ |
| 922 |
return qtrue; |
| 923 |
} |
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
if ( IsMirror( drawSurf, entityNum ) ) |
| 928 |
{ |
| 929 |
return qfalse; |
| 930 |
} |
| 931 |
|
| 932 |
if ( shortest > (tess.shader->portalRange*tess.shader->portalRange) ) |
| 933 |
{ |
| 934 |
return qtrue; |
| 935 |
} |
| 936 |
|
| 937 |
return qfalse; |
| 938 |
} |
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
qboolean R_MirrorViewBySurface (drawSurf_t *drawSurf, int entityNum) { |
| 948 |
vec4_t clipDest[128]; |
| 949 |
viewParms_t newParms; |
| 950 |
viewParms_t oldParms; |
| 951 |
orientation_t surface, camera; |
| 952 |
|
| 953 |
|
| 954 |
if (tr.viewParms.isPortal) { |
| 955 |
ri.Printf( PRINT_DEVELOPER, "WARNING: recursive mirror/portal found\n" ); |
| 956 |
return qfalse; |
| 957 |
} |
| 958 |
|
| 959 |
if ( r_noportals->integer || (r_fastsky->integer == 1) ) { |
| 960 |
return qfalse; |
| 961 |
} |
| 962 |
|
| 963 |
|
| 964 |
if ( SurfIsOffscreen( drawSurf, clipDest ) ) { |
| 965 |
return qfalse; |
| 966 |
} |
| 967 |
|
| 968 |
|
| 969 |
oldParms = tr.viewParms; |
| 970 |
|
| 971 |
newParms = tr.viewParms; |
| 972 |
newParms.isPortal = qtrue; |
| 973 |
if ( !R_GetPortalOrientations( drawSurf, entityNum, &surface, &camera, |
| 974 |
newParms.pvsOrigin, &newParms.isMirror ) ) { |
| 975 |
return qfalse; |
| 976 |
} |
| 977 |
|
| 978 |
R_MirrorPoint (oldParms.or.origin, &surface, &camera, newParms.or.origin ); |
| 979 |
|
| 980 |
VectorSubtract( vec3_origin, camera.axis[0], newParms.portalPlane.normal ); |
| 981 |
newParms.portalPlane.dist = DotProduct( camera.origin, newParms.portalPlane.normal ); |
| 982 |
|
| 983 |
R_MirrorVector (oldParms.or.axis[0], &surface, &camera, newParms.or.axis[0]); |
| 984 |
R_MirrorVector (oldParms.or.axis[1], &surface, &camera, newParms.or.axis[1]); |
| 985 |
R_MirrorVector (oldParms.or.axis[2], &surface, &camera, newParms.or.axis[2]); |
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
R_RenderView (&newParms); |
| 991 |
|
| 992 |
tr.viewParms = oldParms; |
| 993 |
|
| 994 |
return qtrue; |
| 995 |
} |
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
int R_SpriteFogNum( trRefEntity_t *ent ) { |
| 1005 |
int i, j; |
| 1006 |
fog_t *fog; |
| 1007 |
|
| 1008 |
if ( tr.refdef.rdflags & RDF_NOWORLDMODEL ) { |
| 1009 |
return 0; |
| 1010 |
} |
| 1011 |
|
| 1012 |
if ( ent->e.renderfx & RF_CROSSHAIR ) { |
| 1013 |
return 0; |
| 1014 |
} |
| 1015 |
|
| 1016 |
for ( i = 1 ; i < tr.world->numfogs ; i++ ) { |
| 1017 |
fog = &tr.world->fogs[i]; |
| 1018 |
for ( j = 0 ; j < 3 ; j++ ) { |
| 1019 |
if ( ent->e.origin[j] - ent->e.radius >= fog->bounds[1][j] ) { |
| 1020 |
break; |
| 1021 |
} |
| 1022 |
if ( ent->e.origin[j] + ent->e.radius <= fog->bounds[0][j] ) { |
| 1023 |
break; |
| 1024 |
} |
| 1025 |
} |
| 1026 |
if ( j == 3 ) { |
| 1027 |
return i; |
| 1028 |
} |
| 1029 |
} |
| 1030 |
|
| 1031 |
return 0; |
| 1032 |
} |
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
static ID_INLINE void R_Radix( int offset, int size, drawSurf_t *source, drawSurf_t *dest ) |
| 1048 |
{ |
| 1049 |
int count[ 256 ] = { 0 }; |
| 1050 |
int index[ 256 ]; |
| 1051 |
int i; |
| 1052 |
unsigned char *sortKey = NULL; |
| 1053 |
unsigned char *end = NULL; |
| 1054 |
|
| 1055 |
sortKey = ( (unsigned char *)&source[ 0 ].sort ) + offset; |
| 1056 |
end = sortKey + ( size * sizeof( drawSurf_t ) ); |
| 1057 |
for( ; sortKey < end; sortKey += sizeof( drawSurf_t ) ) |
| 1058 |
++count[ *sortKey ]; |
| 1059 |
|
| 1060 |
index[ 0 ] = 0; |
| 1061 |
|
| 1062 |
for( i = 1; i < 256; ++i ) |
| 1063 |
index[ i ] = index[ i - 1 ] + count[ i - 1 ]; |
| 1064 |
|
| 1065 |
sortKey = ( (unsigned char *)&source[ 0 ].sort ) + offset; |
| 1066 |
for( i = 0; i < size; ++i, sortKey += sizeof( drawSurf_t ) ) |
| 1067 |
dest[ index[ *sortKey ]++ ] = source[ i ]; |
| 1068 |
} |
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
static void R_RadixSort( drawSurf_t *source, int size ) |
| 1078 |
{ |
| 1079 |
static drawSurf_t scratch[ MAX_DRAWSURFS ]; |
| 1080 |
#ifdef Q3_LITTLE_ENDIAN |
| 1081 |
R_Radix( 0, size, source, scratch ); |
| 1082 |
R_Radix( 1, size, scratch, source ); |
| 1083 |
R_Radix( 2, size, source, scratch ); |
| 1084 |
R_Radix( 3, size, scratch, source ); |
| 1085 |
#else |
| 1086 |
R_Radix( 3, size, source, scratch ); |
| 1087 |
R_Radix( 2, size, scratch, source ); |
| 1088 |
R_Radix( 1, size, source, scratch ); |
| 1089 |
R_Radix( 0, size, scratch, source ); |
| 1090 |
#endif |
| 1091 |
} |
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
void R_AddDrawSurf( surfaceType_t *surface, shader_t *shader, |
| 1101 |
int fogIndex, int dlightMap ) { |
| 1102 |
int index; |
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
index = tr.refdef.numDrawSurfs & DRAWSURF_MASK; |
| 1107 |
|
| 1108 |
|
| 1109 |
tr.refdef.drawSurfs[index].sort = (shader->sortedIndex << QSORT_SHADERNUM_SHIFT) |
| 1110 |
| tr.shiftedEntityNum | ( fogIndex << QSORT_FOGNUM_SHIFT ) | (int)dlightMap; |
| 1111 |
tr.refdef.drawSurfs[index].surface = surface; |
| 1112 |
tr.refdef.numDrawSurfs++; |
| 1113 |
} |
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
void R_DecomposeSort( unsigned sort, int *entityNum, shader_t **shader, |
| 1121 |
int *fogNum, int *dlightMap ) { |
| 1122 |
*fogNum = ( sort >> QSORT_FOGNUM_SHIFT ) & 31; |
| 1123 |
*shader = tr.sortedShaders[ ( sort >> QSORT_SHADERNUM_SHIFT ) & (MAX_SHADERS-1) ]; |
| 1124 |
*entityNum = ( sort >> QSORT_REFENTITYNUM_SHIFT ) & REFENTITYNUM_MASK; |
| 1125 |
*dlightMap = sort & 3; |
| 1126 |
} |
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
void R_SortDrawSurfs( drawSurf_t *drawSurfs, int numDrawSurfs ) { |
| 1134 |
shader_t *shader; |
| 1135 |
int fogNum; |
| 1136 |
int entityNum; |
| 1137 |
int dlighted; |
| 1138 |
int i; |
| 1139 |
|
| 1140 |
|
| 1141 |
if ( numDrawSurfs < 1 ) { |
| 1142 |
|
| 1143 |
R_AddDrawSurfCmd( drawSurfs, numDrawSurfs ); |
| 1144 |
return; |
| 1145 |
} |
| 1146 |
|
| 1147 |
|
| 1148 |
R_RadixSort( drawSurfs, numDrawSurfs ); |
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
for ( i = 0 ; i < numDrawSurfs ; i++ ) { |
| 1153 |
R_DecomposeSort( (drawSurfs+i)->sort, &entityNum, &shader, &fogNum, &dlighted ); |
| 1154 |
|
| 1155 |
if ( shader->sort > SS_PORTAL ) { |
| 1156 |
break; |
| 1157 |
} |
| 1158 |
|
| 1159 |
|
| 1160 |
if ( shader->sort == SS_BAD ) { |
| 1161 |
ri.Error (ERR_DROP, "Shader '%s'with sort == SS_BAD", shader->name ); |
| 1162 |
} |
| 1163 |
|
| 1164 |
|
| 1165 |
if ( R_MirrorViewBySurface( (drawSurfs+i), entityNum) ) { |
| 1166 |
|
| 1167 |
if ( r_portalOnly->integer ) { |
| 1168 |
return; |
| 1169 |
} |
| 1170 |
break; |
| 1171 |
} |
| 1172 |
} |
| 1173 |
|
| 1174 |
R_AddDrawSurfCmd( drawSurfs, numDrawSurfs ); |
| 1175 |
} |
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
void R_AddEntitySurfaces (void) { |
| 1183 |
trRefEntity_t *ent; |
| 1184 |
shader_t *shader; |
| 1185 |
|
| 1186 |
if ( !r_drawentities->integer ) { |
| 1187 |
return; |
| 1188 |
} |
| 1189 |
|
| 1190 |
for ( tr.currentEntityNum = 0; |
| 1191 |
tr.currentEntityNum < tr.refdef.num_entities; |
| 1192 |
tr.currentEntityNum++ ) { |
| 1193 |
ent = tr.currentEntity = &tr.refdef.entities[tr.currentEntityNum]; |
| 1194 |
|
| 1195 |
ent->needDlights = qfalse; |
| 1196 |
|
| 1197 |
|
| 1198 |
tr.shiftedEntityNum = tr.currentEntityNum << QSORT_REFENTITYNUM_SHIFT; |
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
if ( (ent->e.renderfx & RF_FIRST_PERSON) && tr.viewParms.isPortal) { |
| 1206 |
continue; |
| 1207 |
} |
| 1208 |
|
| 1209 |
|
| 1210 |
switch ( ent->e.reType ) { |
| 1211 |
case RT_PORTALSURFACE: |
| 1212 |
break; |
| 1213 |
case RT_SPRITE: |
| 1214 |
case RT_BEAM: |
| 1215 |
case RT_LIGHTNING: |
| 1216 |
case RT_RAIL_CORE: |
| 1217 |
case RT_RAIL_RINGS: |
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
if ( (ent->e.renderfx & RF_THIRD_PERSON) && !tr.viewParms.isPortal) { |
| 1222 |
continue; |
| 1223 |
} |
| 1224 |
shader = R_GetShaderByHandle( ent->e.customShader ); |
| 1225 |
R_AddDrawSurf( &entitySurface, shader, R_SpriteFogNum( ent ), 0 ); |
| 1226 |
break; |
| 1227 |
|
| 1228 |
case RT_MODEL: |
| 1229 |
|
| 1230 |
R_RotateForEntity( ent, &tr.viewParms, &tr.or ); |
| 1231 |
|
| 1232 |
tr.currentModel = R_GetModelByHandle( ent->e.hModel ); |
| 1233 |
if (!tr.currentModel) { |
| 1234 |
R_AddDrawSurf( &entitySurface, tr.defaultShader, 0, 0 ); |
| 1235 |
} else { |
| 1236 |
switch ( tr.currentModel->type ) { |
| 1237 |
case MOD_MESH: |
| 1238 |
R_AddMD3Surfaces( ent ); |
| 1239 |
break; |
| 1240 |
case MOD_MDR: |
| 1241 |
R_MDRAddAnimSurfaces( ent ); |
| 1242 |
break; |
| 1243 |
case MOD_IQM: |
| 1244 |
R_AddIQMSurfaces( ent ); |
| 1245 |
break; |
| 1246 |
case MOD_BRUSH: |
| 1247 |
R_AddBrushModelSurfaces( ent ); |
| 1248 |
break; |
| 1249 |
case MOD_BAD: |
| 1250 |
if ( (ent->e.renderfx & RF_THIRD_PERSON) && !tr.viewParms.isPortal) { |
| 1251 |
break; |
| 1252 |
} |
| 1253 |
R_AddDrawSurf( &entitySurface, tr.defaultShader, 0, 0 ); |
| 1254 |
break; |
| 1255 |
default: |
| 1256 |
ri.Error( ERR_DROP, "R_AddEntitySurfaces: Bad modeltype" ); |
| 1257 |
break; |
| 1258 |
} |
| 1259 |
} |
| 1260 |
break; |
| 1261 |
default: |
| 1262 |
ri.Error( ERR_DROP, "R_AddEntitySurfaces: Bad reType" ); |
| 1263 |
} |
| 1264 |
} |
| 1265 |
|
| 1266 |
} |
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
void R_GenerateDrawSurfs( void ) { |
| 1275 |
R_AddWorldSurfaces (); |
| 1276 |
|
| 1277 |
R_AddPolygonSurfaces(); |
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
R_SetFarClip(); |
| 1287 |
|
| 1288 |
|
| 1289 |
R_SetupProjectionZ (&tr.viewParms); |
| 1290 |
|
| 1291 |
R_AddEntitySurfaces (); |
| 1292 |
} |
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
void R_DebugPolygon( int color, int numPoints, float *points ) { |
| 1300 |
int i; |
| 1301 |
|
| 1302 |
GL_State( GLS_DEPTHMASK_TRUE | GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE ); |
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
qglColor3f( color&1, (color>>1)&1, (color>>2)&1 ); |
| 1307 |
qglBegin( GL_POLYGON ); |
| 1308 |
for ( i = 0 ; i < numPoints ; i++ ) { |
| 1309 |
qglVertex3fv( points + i * 3 ); |
| 1310 |
} |
| 1311 |
qglEnd(); |
| 1312 |
|
| 1313 |
|
| 1314 |
GL_State( GLS_POLYMODE_LINE | GLS_DEPTHMASK_TRUE | GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE ); |
| 1315 |
qglDepthRange( 0, 0 ); |
| 1316 |
qglColor3f( 1, 1, 1 ); |
| 1317 |
qglBegin( GL_POLYGON ); |
| 1318 |
for ( i = 0 ; i < numPoints ; i++ ) { |
| 1319 |
qglVertex3fv( points + i * 3 ); |
| 1320 |
} |
| 1321 |
qglEnd(); |
| 1322 |
qglDepthRange( 0, 1 ); |
| 1323 |
} |
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
void R_DebugGraphics( void ) { |
| 1333 |
if ( tr.refdef.rdflags & RDF_NOWORLDMODEL ) { |
| 1334 |
return; |
| 1335 |
} |
| 1336 |
if ( !r_debugSurface->integer ) { |
| 1337 |
return; |
| 1338 |
} |
| 1339 |
|
| 1340 |
R_IssuePendingRenderCommands(); |
| 1341 |
|
| 1342 |
GL_Bind( tr.whiteImage); |
| 1343 |
GL_Cull( CT_FRONT_SIDED ); |
| 1344 |
ri.CM_DrawDebugSurface( R_DebugPolygon ); |
| 1345 |
} |
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
void R_RenderView (viewParms_t *parms) { |
| 1357 |
int firstDrawSurf; |
| 1358 |
int numDrawSurfs; |
| 1359 |
|
| 1360 |
if ( parms->viewportWidth <= 0 || parms->viewportHeight <= 0 ) { |
| 1361 |
return; |
| 1362 |
} |
| 1363 |
|
| 1364 |
tr.viewCount++; |
| 1365 |
|
| 1366 |
tr.viewParms = *parms; |
| 1367 |
tr.viewParms.frameSceneNum = tr.frameSceneNum; |
| 1368 |
tr.viewParms.frameCount = tr.frameCount; |
| 1369 |
|
| 1370 |
firstDrawSurf = tr.refdef.numDrawSurfs; |
| 1371 |
|
| 1372 |
tr.viewCount++; |
| 1373 |
|
| 1374 |
|
| 1375 |
R_RotateForViewer (); |
| 1376 |
|
| 1377 |
R_SetupProjection(&tr.viewParms, r_zproj->value, qtrue); |
| 1378 |
|
| 1379 |
R_GenerateDrawSurfs(); |
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
numDrawSurfs = tr.refdef.numDrawSurfs; |
| 1385 |
if ( numDrawSurfs > MAX_DRAWSURFS ) { |
| 1386 |
numDrawSurfs = MAX_DRAWSURFS; |
| 1387 |
} |
| 1388 |
|
| 1389 |
R_SortDrawSurfs( tr.refdef.drawSurfs + firstDrawSurf, numDrawSurfs - firstDrawSurf ); |
| 1390 |
|
| 1391 |
|
| 1392 |
R_DebugGraphics(); |
| 1393 |
} |
| 1394 |
|
| 1395 |
|
| 1396 |
|