| 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 |
static float ProjectRadius( float r, vec3_t location ) |
| 27 |
{ |
| 28 |
float pr; |
| 29 |
float dist; |
| 30 |
float c; |
| 31 |
vec3_t p; |
| 32 |
float projected[4]; |
| 33 |
|
| 34 |
c = DotProduct( tr.viewParms.or.axis[0], tr.viewParms.or.origin ); |
| 35 |
dist = DotProduct( tr.viewParms.or.axis[0], location ) - c; |
| 36 |
|
| 37 |
if ( dist <= 0 ) |
| 38 |
return 0; |
| 39 |
|
| 40 |
p[0] = 0; |
| 41 |
p[1] = fabs( r ); |
| 42 |
p[2] = -dist; |
| 43 |
|
| 44 |
projected[0] = p[0] * tr.viewParms.projectionMatrix[0] + |
| 45 |
p[1] * tr.viewParms.projectionMatrix[4] + |
| 46 |
p[2] * tr.viewParms.projectionMatrix[8] + |
| 47 |
tr.viewParms.projectionMatrix[12]; |
| 48 |
|
| 49 |
projected[1] = p[0] * tr.viewParms.projectionMatrix[1] + |
| 50 |
p[1] * tr.viewParms.projectionMatrix[5] + |
| 51 |
p[2] * tr.viewParms.projectionMatrix[9] + |
| 52 |
tr.viewParms.projectionMatrix[13]; |
| 53 |
|
| 54 |
projected[2] = p[0] * tr.viewParms.projectionMatrix[2] + |
| 55 |
p[1] * tr.viewParms.projectionMatrix[6] + |
| 56 |
p[2] * tr.viewParms.projectionMatrix[10] + |
| 57 |
tr.viewParms.projectionMatrix[14]; |
| 58 |
|
| 59 |
projected[3] = p[0] * tr.viewParms.projectionMatrix[3] + |
| 60 |
p[1] * tr.viewParms.projectionMatrix[7] + |
| 61 |
p[2] * tr.viewParms.projectionMatrix[11] + |
| 62 |
tr.viewParms.projectionMatrix[15]; |
| 63 |
|
| 64 |
|
| 65 |
pr = projected[1] / projected[3]; |
| 66 |
|
| 67 |
if ( pr > 1.0f ) |
| 68 |
pr = 1.0f; |
| 69 |
|
| 70 |
return pr; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
static int R_CullModel( mdvModel_t *model, trRefEntity_t *ent ) { |
| 79 |
vec3_t bounds[2]; |
| 80 |
mdvFrame_t *oldFrame, *newFrame; |
| 81 |
int i; |
| 82 |
|
| 83 |
|
| 84 |
newFrame = model->frames + ent->e.frame; |
| 85 |
oldFrame = model->frames + ent->e.oldframe; |
| 86 |
|
| 87 |
|
| 88 |
if ( !ent->e.nonNormalizedAxes ) |
| 89 |
{ |
| 90 |
if ( ent->e.frame == ent->e.oldframe ) |
| 91 |
{ |
| 92 |
switch ( R_CullLocalPointAndRadius( newFrame->localOrigin, newFrame->radius ) ) |
| 93 |
{ |
| 94 |
case CULL_OUT: |
| 95 |
tr.pc.c_sphere_cull_md3_out++; |
| 96 |
return CULL_OUT; |
| 97 |
|
| 98 |
case CULL_IN: |
| 99 |
tr.pc.c_sphere_cull_md3_in++; |
| 100 |
return CULL_IN; |
| 101 |
|
| 102 |
case CULL_CLIP: |
| 103 |
tr.pc.c_sphere_cull_md3_clip++; |
| 104 |
break; |
| 105 |
} |
| 106 |
} |
| 107 |
else |
| 108 |
{ |
| 109 |
int sphereCull, sphereCullB; |
| 110 |
|
| 111 |
sphereCull = R_CullLocalPointAndRadius( newFrame->localOrigin, newFrame->radius ); |
| 112 |
if ( newFrame == oldFrame ) { |
| 113 |
sphereCullB = sphereCull; |
| 114 |
} else { |
| 115 |
sphereCullB = R_CullLocalPointAndRadius( oldFrame->localOrigin, oldFrame->radius ); |
| 116 |
} |
| 117 |
|
| 118 |
if ( sphereCull == sphereCullB ) |
| 119 |
{ |
| 120 |
if ( sphereCull == CULL_OUT ) |
| 121 |
{ |
| 122 |
tr.pc.c_sphere_cull_md3_out++; |
| 123 |
return CULL_OUT; |
| 124 |
} |
| 125 |
else if ( sphereCull == CULL_IN ) |
| 126 |
{ |
| 127 |
tr.pc.c_sphere_cull_md3_in++; |
| 128 |
return CULL_IN; |
| 129 |
} |
| 130 |
else |
| 131 |
{ |
| 132 |
tr.pc.c_sphere_cull_md3_clip++; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
for (i = 0 ; i < 3 ; i++) { |
| 140 |
bounds[0][i] = oldFrame->bounds[0][i] < newFrame->bounds[0][i] ? oldFrame->bounds[0][i] : newFrame->bounds[0][i]; |
| 141 |
bounds[1][i] = oldFrame->bounds[1][i] > newFrame->bounds[1][i] ? oldFrame->bounds[1][i] : newFrame->bounds[1][i]; |
| 142 |
} |
| 143 |
|
| 144 |
switch ( R_CullLocalBox( bounds ) ) |
| 145 |
{ |
| 146 |
case CULL_IN: |
| 147 |
tr.pc.c_box_cull_md3_in++; |
| 148 |
return CULL_IN; |
| 149 |
case CULL_CLIP: |
| 150 |
tr.pc.c_box_cull_md3_clip++; |
| 151 |
return CULL_CLIP; |
| 152 |
case CULL_OUT: |
| 153 |
default: |
| 154 |
tr.pc.c_box_cull_md3_out++; |
| 155 |
return CULL_OUT; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
int R_ComputeLOD( trRefEntity_t *ent ) { |
| 167 |
float radius; |
| 168 |
float flod, lodscale; |
| 169 |
float projectedRadius; |
| 170 |
mdvFrame_t *frame; |
| 171 |
mdrHeader_t *mdr; |
| 172 |
mdrFrame_t *mdrframe; |
| 173 |
int lod; |
| 174 |
|
| 175 |
if ( tr.currentModel->numLods < 2 ) |
| 176 |
{ |
| 177 |
|
| 178 |
lod = 0; |
| 179 |
} |
| 180 |
else |
| 181 |
{ |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
if(tr.currentModel->type == MOD_MDR) |
| 186 |
{ |
| 187 |
int frameSize; |
| 188 |
mdr = (mdrHeader_t *) tr.currentModel->modelData; |
| 189 |
frameSize = (size_t) (&((mdrFrame_t *)0)->bones[mdr->numBones]); |
| 190 |
|
| 191 |
mdrframe = (mdrFrame_t *) ((byte *) mdr + mdr->ofsFrames + frameSize * ent->e.frame); |
| 192 |
|
| 193 |
radius = RadiusFromBounds(mdrframe->bounds[0], mdrframe->bounds[1]); |
| 194 |
} |
| 195 |
else |
| 196 |
{ |
| 197 |
|
| 198 |
frame = tr.currentModel->mdv[0]->frames; |
| 199 |
|
| 200 |
frame += ent->e.frame; |
| 201 |
|
| 202 |
radius = RadiusFromBounds( frame->bounds[0], frame->bounds[1] ); |
| 203 |
} |
| 204 |
|
| 205 |
if ( ( projectedRadius = ProjectRadius( radius, ent->e.origin ) ) != 0 ) |
| 206 |
{ |
| 207 |
lodscale = r_lodscale->value; |
| 208 |
if (lodscale > 20) lodscale = 20; |
| 209 |
flod = 1.0f - projectedRadius * lodscale; |
| 210 |
} |
| 211 |
else |
| 212 |
{ |
| 213 |
|
| 214 |
flod = 0; |
| 215 |
} |
| 216 |
|
| 217 |
flod *= tr.currentModel->numLods; |
| 218 |
lod = ri.ftol(flod); |
| 219 |
|
| 220 |
if ( lod < 0 ) |
| 221 |
{ |
| 222 |
lod = 0; |
| 223 |
} |
| 224 |
else if ( lod >= tr.currentModel->numLods ) |
| 225 |
{ |
| 226 |
lod = tr.currentModel->numLods - 1; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
lod += r_lodbias->integer; |
| 231 |
|
| 232 |
if ( lod >= tr.currentModel->numLods ) |
| 233 |
lod = tr.currentModel->numLods - 1; |
| 234 |
if ( lod < 0 ) |
| 235 |
lod = 0; |
| 236 |
|
| 237 |
return lod; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
int R_ComputeFogNum( mdvModel_t *model, trRefEntity_t *ent ) { |
| 247 |
int i, j; |
| 248 |
fog_t *fog; |
| 249 |
mdvFrame_t *mdvFrame; |
| 250 |
vec3_t localOrigin; |
| 251 |
|
| 252 |
if ( tr.refdef.rdflags & RDF_NOWORLDMODEL ) { |
| 253 |
return 0; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
mdvFrame = model->frames + ent->e.frame; |
| 258 |
VectorAdd( ent->e.origin, mdvFrame->localOrigin, localOrigin ); |
| 259 |
for ( i = 1 ; i < tr.world->numfogs ; i++ ) { |
| 260 |
fog = &tr.world->fogs[i]; |
| 261 |
for ( j = 0 ; j < 3 ; j++ ) { |
| 262 |
if ( localOrigin[j] - mdvFrame->radius >= fog->bounds[1][j] ) { |
| 263 |
break; |
| 264 |
} |
| 265 |
if ( localOrigin[j] + mdvFrame->radius <= fog->bounds[0][j] ) { |
| 266 |
break; |
| 267 |
} |
| 268 |
} |
| 269 |
if ( j == 3 ) { |
| 270 |
return i; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
return 0; |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
void R_AddMD3Surfaces( trRefEntity_t *ent ) { |
| 284 |
int i; |
| 285 |
mdvModel_t *model; |
| 286 |
mdvSurface_t *surface; |
| 287 |
void *drawSurf; |
| 288 |
shader_t *shader; |
| 289 |
int cull; |
| 290 |
int lod; |
| 291 |
int fogNum; |
| 292 |
int cubemapIndex; |
| 293 |
qboolean personalModel; |
| 294 |
|
| 295 |
|
| 296 |
personalModel = (ent->e.renderfx & RF_THIRD_PERSON) && !(tr.viewParms.isPortal |
| 297 |
|| (tr.viewParms.flags & (VPF_SHADOWMAP | VPF_DEPTHSHADOW))); |
| 298 |
|
| 299 |
if ( ent->e.renderfx & RF_WRAP_FRAMES ) { |
| 300 |
ent->e.frame %= tr.currentModel->mdv[0]->numFrames; |
| 301 |
ent->e.oldframe %= tr.currentModel->mdv[0]->numFrames; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
if ( (ent->e.frame >= tr.currentModel->mdv[0]->numFrames) |
| 311 |
|| (ent->e.frame < 0) |
| 312 |
|| (ent->e.oldframe >= tr.currentModel->mdv[0]->numFrames) |
| 313 |
|| (ent->e.oldframe < 0) ) { |
| 314 |
ri.Printf( PRINT_DEVELOPER, "R_AddMD3Surfaces: no such frame %d to %d for '%s'\n", |
| 315 |
ent->e.oldframe, ent->e.frame, |
| 316 |
tr.currentModel->name ); |
| 317 |
ent->e.frame = 0; |
| 318 |
ent->e.oldframe = 0; |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
lod = R_ComputeLOD( ent ); |
| 325 |
|
| 326 |
model = tr.currentModel->mdv[lod]; |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
cull = R_CullModel ( model, ent ); |
| 333 |
if ( cull == CULL_OUT ) { |
| 334 |
return; |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
if ( !personalModel || r_shadows->integer > 1 ) { |
| 341 |
R_SetupEntityLighting( &tr.refdef, ent ); |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
fogNum = R_ComputeFogNum( model, ent ); |
| 348 |
|
| 349 |
cubemapIndex = R_CubemapForPoint(ent->e.origin); |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
surface = model->surfaces; |
| 355 |
for ( i = 0 ; i < model->numSurfaces ; i++ ) { |
| 356 |
|
| 357 |
if ( ent->e.customShader ) { |
| 358 |
shader = R_GetShaderByHandle( ent->e.customShader ); |
| 359 |
} else if ( ent->e.customSkin > 0 && ent->e.customSkin < tr.numSkins ) { |
| 360 |
skin_t *skin; |
| 361 |
int j; |
| 362 |
|
| 363 |
skin = R_GetSkinByHandle( ent->e.customSkin ); |
| 364 |
|
| 365 |
|
| 366 |
shader = tr.defaultShader; |
| 367 |
for ( j = 0 ; j < skin->numSurfaces ; j++ ) { |
| 368 |
|
| 369 |
if ( !strcmp( skin->surfaces[j].name, surface->name ) ) { |
| 370 |
shader = skin->surfaces[j].shader; |
| 371 |
break; |
| 372 |
} |
| 373 |
} |
| 374 |
if (shader == tr.defaultShader) { |
| 375 |
ri.Printf( PRINT_DEVELOPER, "WARNING: no shader for surface %s in skin %s\n", surface->name, skin->name); |
| 376 |
} |
| 377 |
else if (shader->defaultShader) { |
| 378 |
ri.Printf( PRINT_DEVELOPER, "WARNING: shader %s in skin %s not found\n", shader->name, skin->name); |
| 379 |
} |
| 380 |
} else if ( surface->numShaderIndexes <= 0 ) { |
| 381 |
shader = tr.defaultShader; |
| 382 |
} else { |
| 383 |
shader = tr.shaders[ surface->shaderIndexes[ ent->e.skinNum % surface->numShaderIndexes ] ]; |
| 384 |
} |
| 385 |
|
| 386 |
if ( model->numVaoSurfaces > 0 ) { |
| 387 |
drawSurf = &model->vaoSurfaces[i]; |
| 388 |
} else { |
| 389 |
drawSurf = surface; |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
if ( !personalModel |
| 396 |
&& r_shadows->integer == 2 |
| 397 |
&& fogNum == 0 |
| 398 |
&& !(ent->e.renderfx & ( RF_NOSHADOW | RF_DEPTHHACK ) ) |
| 399 |
&& shader->sort == SS_OPAQUE ) { |
| 400 |
R_AddDrawSurf( drawSurf, tr.shadowShader, 0, qfalse, qfalse, 0 ); |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
if ( r_shadows->integer == 3 |
| 405 |
&& fogNum == 0 |
| 406 |
&& (ent->e.renderfx & RF_SHADOW_PLANE ) |
| 407 |
&& shader->sort == SS_OPAQUE ) { |
| 408 |
R_AddDrawSurf( drawSurf, tr.projectionShadowShader, 0, qfalse, qfalse, 0 ); |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
if ( !personalModel ) { |
| 413 |
R_AddDrawSurf( drawSurf, shader, fogNum, qfalse, qfalse, cubemapIndex ); |
| 414 |
} |
| 415 |
|
| 416 |
surface++; |
| 417 |
} |
| 418 |
|
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|