| 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 SURFACE_C |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
#include "q3map2.h" |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
mapDrawSurface_t *AllocDrawSurface( surfaceType_t type ){ |
| 47 |
mapDrawSurface_t *ds; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
if ( type <= SURFACE_BAD || type >= NUM_SURFACE_TYPES ) { |
| 52 |
Error( "AllocDrawSurface: Invalid surface type %d specified", type ); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
if ( numMapDrawSurfs >= MAX_MAP_DRAW_SURFS ) { |
| 57 |
Error( "MAX_MAP_DRAW_SURFS (%d) exceeded", MAX_MAP_DRAW_SURFS ); |
| 58 |
} |
| 59 |
ds = &mapDrawSurfs[ numMapDrawSurfs ]; |
| 60 |
numMapDrawSurfs++; |
| 61 |
|
| 62 |
|
| 63 |
memset( ds, 0, sizeof( mapDrawSurface_t ) ); |
| 64 |
ds->type = type; |
| 65 |
ds->planeNum = -1; |
| 66 |
ds->fogNum = defaultFogNum; |
| 67 |
ds->outputNum = -1; |
| 68 |
ds->surfaceNum = numMapDrawSurfs - 1; |
| 69 |
|
| 70 |
return ds; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
void FinishSurface( mapDrawSurface_t *ds ){ |
| 81 |
mapDrawSurface_t *ds2; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
if ( ds == NULL || ds->shaderInfo == NULL || ds->type <= SURFACE_BAD || ds->type >= NUM_SURFACE_TYPES ) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
if ( ds->celShader != NULL ) { |
| 91 |
MakeCelSurface( ds, ds->celShader ); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
if ( ds->backSide ) { |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
if ( ds->shaderInfo->cloneShader != NULL && ds->shaderInfo->cloneShader[ 0 ] != '\0' ) { |
| 101 |
CloneSurface( ds, ShaderInfoForShader( ds->shaderInfo->cloneShader ) ); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
if ( ds->shaderInfo->backShader != NULL && ds->shaderInfo->backShader[ 0 ] != '\0' ) { |
| 106 |
ds2 = CloneSurface( ds, ShaderInfoForShader( ds->shaderInfo->backShader ) ); |
| 107 |
ds2->backSide = qtrue; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
mapDrawSurface_t *CloneSurface( mapDrawSurface_t *src, shaderInfo_t *si ){ |
| 119 |
mapDrawSurface_t *ds; |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
if ( src == NULL || si == NULL ) { |
| 124 |
return NULL; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
ds = AllocDrawSurface( src->type ); |
| 129 |
if ( ds == NULL ) { |
| 130 |
return NULL; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
memcpy( ds, src, sizeof( *ds ) ); |
| 135 |
|
| 136 |
|
| 137 |
ds->sideRef = NULL; |
| 138 |
|
| 139 |
|
| 140 |
ds->shaderInfo = si; |
| 141 |
|
| 142 |
|
| 143 |
if ( ds->numVerts > 0 ) { |
| 144 |
ds->verts = safe_malloc( ds->numVerts * sizeof( *ds->verts ) ); |
| 145 |
memcpy( ds->verts, src->verts, ds->numVerts * sizeof( *ds->verts ) ); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
if ( ds->numIndexes <= 0 ) { |
| 150 |
return ds; |
| 151 |
} |
| 152 |
ds->indexes = safe_malloc( ds->numIndexes * sizeof( *ds->indexes ) ); |
| 153 |
memcpy( ds->indexes, src->indexes, ds->numIndexes * sizeof( *ds->indexes ) ); |
| 154 |
|
| 155 |
|
| 156 |
return ds; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
mapDrawSurface_t *MakeCelSurface( mapDrawSurface_t *src, shaderInfo_t *si ){ |
| 167 |
mapDrawSurface_t *ds; |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
if ( src == NULL || si == NULL ) { |
| 172 |
return NULL; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
if ( ( src->shaderInfo->compileFlags & C_TRANSLUCENT ) || |
| 177 |
( src->shaderInfo->compileFlags & C_SKY ) ) { |
| 178 |
return NULL; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
ds = CloneSurface( src, si ); |
| 183 |
if ( ds == NULL ) { |
| 184 |
return NULL; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
ds->planar = qfalse; |
| 189 |
ds->planeNum = -1; |
| 190 |
ds->celShader = NULL; |
| 191 |
|
| 192 |
|
| 193 |
return ds; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
mapDrawSurface_t *MakeSkyboxSurface( mapDrawSurface_t *src ){ |
| 204 |
int i; |
| 205 |
mapDrawSurface_t *ds; |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
if ( src == NULL ) { |
| 210 |
return NULL; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
ds = CloneSurface( src, src->shaderInfo ); |
| 215 |
if ( ds == NULL ) { |
| 216 |
return NULL; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
ds->parent = src; |
| 221 |
|
| 222 |
|
| 223 |
for ( i = 0; i < ds->numVerts; i++ ) |
| 224 |
{ |
| 225 |
m4x4_transform_point( skyboxTransform, ds->verts[ i ].xyz ); |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
VectorClear( ds->lightmapVecs[ 2 ] ); |
| 234 |
|
| 235 |
|
| 236 |
return ds; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
#define TINY_AREA 1.0f |
| 247 |
|
| 248 |
qboolean IsTriangleDegenerate( bspDrawVert_t *points, int a, int b, int c ){ |
| 249 |
vec3_t v1, v2, v3; |
| 250 |
float d; |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
VectorSubtract( points[ b ].xyz, points[ a ].xyz, v1 ); |
| 255 |
VectorSubtract( points[ c ].xyz, points[ a ].xyz, v2 ); |
| 256 |
CrossProduct( v1, v2, v3 ); |
| 257 |
d = VectorLength( v3 ); |
| 258 |
|
| 259 |
|
| 260 |
if ( d < TINY_AREA ) { |
| 261 |
return qtrue; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
return qfalse; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
void ClearSurface( mapDrawSurface_t *ds ){ |
| 276 |
ds->type = SURFACE_BAD; |
| 277 |
ds->planar = qfalse; |
| 278 |
ds->planeNum = -1; |
| 279 |
ds->numVerts = 0; |
| 280 |
if ( ds->verts != NULL ) { |
| 281 |
free( ds->verts ); |
| 282 |
} |
| 283 |
ds->verts = NULL; |
| 284 |
ds->numIndexes = 0; |
| 285 |
if ( ds->indexes != NULL ) { |
| 286 |
free( ds->indexes ); |
| 287 |
} |
| 288 |
ds->indexes = NULL; |
| 289 |
numClearedSurfaces++; |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
void TidyEntitySurfaces( entity_t *e ){ |
| 300 |
int i, j, deleted; |
| 301 |
mapDrawSurface_t *out, *in = NULL; |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
Sys_FPrintf( SYS_VRB, "--- TidyEntitySurfaces ---\n" ); |
| 306 |
|
| 307 |
|
| 308 |
deleted = 0; |
| 309 |
for ( i = e->firstDrawSurf, j = e->firstDrawSurf; j < numMapDrawSurfs; i++, j++ ) |
| 310 |
{ |
| 311 |
|
| 312 |
out = &mapDrawSurfs[ i ]; |
| 313 |
|
| 314 |
|
| 315 |
for ( ; j < numMapDrawSurfs; j++ ) |
| 316 |
{ |
| 317 |
|
| 318 |
in = &mapDrawSurfs[ j ]; |
| 319 |
|
| 320 |
|
| 321 |
if ( in->type == SURFACE_FLARE || in->type == SURFACE_SHADER || |
| 322 |
( in->type != SURFACE_BAD && in->numVerts > 0 ) ) { |
| 323 |
break; |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
ClearSurface( in ); |
| 328 |
deleted++; |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
if ( i != j ) { |
| 333 |
memcpy( out, in, sizeof( mapDrawSurface_t ) ); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
numMapDrawSurfs = i; |
| 339 |
|
| 340 |
|
| 341 |
Sys_FPrintf( SYS_VRB, "%9d empty or malformed surfaces deleted\n", deleted ); |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
qboolean CalcSurfaceTextureRange( mapDrawSurface_t *ds ){ |
| 352 |
int i, j, v, size[ 2 ]; |
| 353 |
float mins[ 2 ], maxs[ 2 ]; |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
if ( ds->numVerts <= 0 ) { |
| 358 |
return qtrue; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
mins[ 0 ] = 999999; |
| 363 |
mins[ 1 ] = 999999; |
| 364 |
maxs[ 0 ] = -999999; |
| 365 |
maxs[ 1 ] = -999999; |
| 366 |
for ( i = 0; i < ds->numVerts; i++ ) |
| 367 |
{ |
| 368 |
for ( j = 0; j < 2; j++ ) |
| 369 |
{ |
| 370 |
if ( ds->verts[ i ].st[ j ] < mins[ j ] ) { |
| 371 |
mins[ j ] = ds->verts[ i ].st[ j ]; |
| 372 |
} |
| 373 |
if ( ds->verts[ i ].st[ j ] > maxs[ j ] ) { |
| 374 |
maxs[ j ] = ds->verts[ i ].st[ j ]; |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
for ( j = 0; j < 2; j++ ) |
| 381 |
ds->bias[ j ] = -floor( 0.5f * ( mins[ j ] + maxs[ j ] ) ); |
| 382 |
|
| 383 |
|
| 384 |
size[ 0 ] = ds->shaderInfo->shaderWidth; |
| 385 |
size[ 1 ] = ds->shaderInfo->shaderHeight; |
| 386 |
ds->texMins[ 0 ] = 999999; |
| 387 |
ds->texMins[ 1 ] = 999999; |
| 388 |
ds->texMaxs[ 0 ] = -999999; |
| 389 |
ds->texMaxs[ 1 ] = -999999; |
| 390 |
for ( i = 0; i < ds->numVerts; i++ ) |
| 391 |
{ |
| 392 |
for ( j = 0; j < 2; j++ ) |
| 393 |
{ |
| 394 |
v = ( (float) ds->verts[ i ].st[ j ] + ds->bias[ j ] ) * size[ j ]; |
| 395 |
if ( v < ds->texMins[ j ] ) { |
| 396 |
ds->texMins[ j ] = v; |
| 397 |
} |
| 398 |
if ( v > ds->texMaxs[ j ] ) { |
| 399 |
ds->texMaxs[ j ] = v; |
| 400 |
} |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
for ( j = 0; j < 2; j++ ) |
| 406 |
ds->texRange[ j ] = ( ds->texMaxs[ j ] - ds->texMins[ j ] ); |
| 407 |
|
| 408 |
|
| 409 |
if ( texRange == 0 ) { |
| 410 |
return qtrue; |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
for ( j = 0; j < 2; j++ ) |
| 415 |
{ |
| 416 |
if ( ds->texMins[ j ] < -texRange || ds->texMaxs[ j ] > texRange ) { |
| 417 |
return qfalse; |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
return qtrue; |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
qboolean CalcLightmapAxis( vec3_t normal, vec3_t axis ){ |
| 433 |
vec3_t absolute; |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
if ( normal[ 0 ] == 0.0f && normal[ 1 ] == 0.0f && normal[ 2 ] == 0.0f ) { |
| 438 |
VectorClear( axis ); |
| 439 |
return qfalse; |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
absolute[ 0 ] = fabs( normal[ 0 ] ); |
| 444 |
absolute[ 1 ] = fabs( normal[ 1 ] ); |
| 445 |
absolute[ 2 ] = fabs( normal[ 2 ] ); |
| 446 |
|
| 447 |
|
| 448 |
if ( absolute[ 2 ] > absolute[ 0 ] - 0.0001f && absolute[ 2 ] > absolute[ 1 ] - 0.0001f ) { |
| 449 |
if ( normal[ 2 ] > 0.0f ) { |
| 450 |
VectorSet( axis, 0.0f, 0.0f, 1.0f ); |
| 451 |
} |
| 452 |
else{ |
| 453 |
VectorSet( axis, 0.0f, 0.0f, -1.0f ); |
| 454 |
} |
| 455 |
} |
| 456 |
else if ( absolute[ 0 ] > absolute[ 1 ] - 0.0001f && absolute[ 0 ] > absolute[ 2 ] - 0.0001f ) { |
| 457 |
if ( normal[ 0 ] > 0.0f ) { |
| 458 |
VectorSet( axis, 1.0f, 0.0f, 0.0f ); |
| 459 |
} |
| 460 |
else{ |
| 461 |
VectorSet( axis, -1.0f, 0.0f, 0.0f ); |
| 462 |
} |
| 463 |
} |
| 464 |
else |
| 465 |
{ |
| 466 |
if ( normal[ 1 ] > 0.0f ) { |
| 467 |
VectorSet( axis, 0.0f, 1.0f, 0.0f ); |
| 468 |
} |
| 469 |
else{ |
| 470 |
VectorSet( axis, 0.0f, -1.0f, 0.0f ); |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
return qtrue; |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
#define PLANAR_EPSILON 0.5f //% 0.126f 0.25f |
| 486 |
|
| 487 |
void ClassifySurfaces( int numSurfs, mapDrawSurface_t *ds ){ |
| 488 |
int i, bestAxis; |
| 489 |
float dist; |
| 490 |
vec4_t plane; |
| 491 |
shaderInfo_t *si; |
| 492 |
static vec3_t axii[ 6 ] = |
| 493 |
{ |
| 494 |
{ 0, 0, -1 }, |
| 495 |
{ 0, 0, 1 }, |
| 496 |
{ -1, 0, 0 }, |
| 497 |
{ 1, 0, 0 }, |
| 498 |
{ 0, -1, 0 }, |
| 499 |
{ 0, 1, 0 } |
| 500 |
}; |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
for ( ; numSurfs > 0; numSurfs--, ds++ ) |
| 505 |
{ |
| 506 |
|
| 507 |
if ( ds->type == SURFACE_BAD || ds->numVerts <= 0 ) { |
| 508 |
continue; |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
si = ds->shaderInfo; |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
if ( ds->type != SURFACE_PATCH && ds->type != SURFACE_FACE ) { |
| 519 |
if ( ds->numVerts > SHADER_MAX_VERTEXES ) { |
| 520 |
ds->type = SURFACE_FORCED_META; |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
ClearBounds( ds->mins, ds->maxs ); |
| 530 |
for ( i = 0; i < ds->numVerts; i++ ) |
| 531 |
AddPointToBounds( ds->verts[ i ].xyz, ds->mins, ds->maxs ); |
| 532 |
|
| 533 |
|
| 534 |
if ( ds->planeNum >= 0 ) { |
| 535 |
VectorCopy( mapplanes[ ds->planeNum ].normal, plane ); |
| 536 |
plane[ 3 ] = mapplanes[ ds->planeNum ].dist; |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
else |
| 541 |
{ |
| 542 |
VectorClear( plane ); |
| 543 |
plane[ 3 ] = 0.0f; |
| 544 |
for ( i = 0; i < ds->numVerts; i++ ) |
| 545 |
{ |
| 546 |
if ( ds->verts[ i ].normal[ 0 ] != 0.0f && ds->verts[ i ].normal[ 1 ] != 0.0f && ds->verts[ i ].normal[ 2 ] != 0.0f ) { |
| 547 |
VectorCopy( ds->verts[ i ].normal, plane ); |
| 548 |
plane[ 3 ] = DotProduct( ds->verts[ i ].xyz, plane ); |
| 549 |
break; |
| 550 |
} |
| 551 |
} |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
if ( VectorLength( plane ) <= 0.0f ) { |
| 556 |
ds->planar = qfalse; |
| 557 |
ds->planeNum = -1; |
| 558 |
} |
| 559 |
else |
| 560 |
{ |
| 561 |
|
| 562 |
ds->planar = qtrue; |
| 563 |
|
| 564 |
|
| 565 |
for ( i = 0; i < ds->numVerts; i++ ) |
| 566 |
{ |
| 567 |
|
| 568 |
dist = DotProduct( ds->verts[ i ].xyz, plane ) - plane[ 3 ]; |
| 569 |
if ( fabs( dist ) > PLANAR_EPSILON ) { |
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
ds->planar = qfalse; |
| 576 |
break; |
| 577 |
} |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
|
| 582 |
if ( ds->planar ) { |
| 583 |
if ( ds->planeNum < 0 ) { |
| 584 |
ds->planeNum = FindFloatPlane( plane, plane[ 3 ], 1, &ds->verts[ 0 ].xyz ); |
| 585 |
} |
| 586 |
VectorCopy( plane, ds->lightmapVecs[ 2 ] ); |
| 587 |
} |
| 588 |
else |
| 589 |
{ |
| 590 |
ds->planeNum = -1; |
| 591 |
VectorClear( ds->lightmapVecs[ 2 ] ); |
| 592 |
|
| 593 |
|
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
if ( si->compileFlags & C_VERTEXLIT || ds->type == SURFACE_TRIANGLES || nolm == qtrue ) { |
| 602 |
VectorClear( ds->lightmapAxis ); |
| 603 |
|
| 604 |
ds->sampleSize = 0; |
| 605 |
continue; |
| 606 |
} |
| 607 |
|
| 608 |
|
| 609 |
if ( si->lightmapAxis[ 0 ] || si->lightmapAxis[ 1 ] || si->lightmapAxis[ 2 ] ) { |
| 610 |
VectorCopy( si->lightmapAxis, ds->lightmapAxis ); |
| 611 |
} |
| 612 |
else if ( ds->type == SURFACE_FORCED_META ) { |
| 613 |
VectorClear( ds->lightmapAxis ); |
| 614 |
} |
| 615 |
else if ( ds->planar ) { |
| 616 |
CalcLightmapAxis( plane, ds->lightmapAxis ); |
| 617 |
} |
| 618 |
else |
| 619 |
{ |
| 620 |
|
| 621 |
for ( bestAxis = 0; bestAxis < 6; bestAxis++ ) |
| 622 |
{ |
| 623 |
for ( i = 0; i < ds->numVerts && bestAxis < 6; i++ ) |
| 624 |
{ |
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
if ( DotProduct( ds->verts[ i ].normal, axii[ bestAxis ] ) < 0.25f ) { |
| 629 |
break; |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
if ( i == ds->numVerts ) { |
| 634 |
break; |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
|
| 639 |
if ( bestAxis < 6 ) { |
| 640 |
|
| 641 |
|
| 642 |
VectorCopy( axii[ bestAxis ], ds->lightmapAxis ); |
| 643 |
} |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
} |
| 649 |
|
| 650 |
|
| 651 |
if ( ds->shaderInfo->lightmapSampleSize > 0 ) { |
| 652 |
ds->sampleSize = ds->shaderInfo->lightmapSampleSize; |
| 653 |
} |
| 654 |
else if ( ds->sampleSize <= 0 ) { |
| 655 |
ds->sampleSize = sampleSize; |
| 656 |
|
| 657 |
} |
| 658 |
if ( ds->lightmapScale > 0.0f ) { |
| 659 |
ds->sampleSize = ds->lightmapScale * (float)ds->sampleSize; |
| 660 |
ds->lightmapScale = 0; |
| 661 |
} |
| 662 |
|
| 663 |
if ( ds->sampleSize < minSampleSize ) { |
| 664 |
ds->sampleSize = minSampleSize; |
| 665 |
} |
| 666 |
|
| 667 |
if ( ds->sampleSize < 1 ) { |
| 668 |
ds->sampleSize = 1; |
| 669 |
} |
| 670 |
|
| 671 |
if ( ds->sampleSize > 16384 ) { |
| 672 |
ds->sampleSize = 16384; |
| 673 |
} |
| 674 |
} |
| 675 |
} |
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
void ClassifyEntitySurfaces( entity_t *e ){ |
| 685 |
int i; |
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
Sys_FPrintf( SYS_VRB, "--- ClassifyEntitySurfaces ---\n" ); |
| 690 |
|
| 691 |
|
| 692 |
for ( i = e->firstDrawSurf; i < numMapDrawSurfs; i++ ) |
| 693 |
{ |
| 694 |
FinishSurface( &mapDrawSurfs[ i ] ); |
| 695 |
ClassifySurfaces( 1, &mapDrawSurfs[ i ] ); |
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
TidyEntitySurfaces( e ); |
| 700 |
} |
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
byte GetShaderIndexForPoint( indexMap_t *im, vec3_t eMins, vec3_t eMaxs, vec3_t point ){ |
| 710 |
int i, x, y; |
| 711 |
float s, t; |
| 712 |
vec3_t mins, maxs, size; |
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
if ( im == NULL ) { |
| 717 |
return 0; |
| 718 |
} |
| 719 |
|
| 720 |
|
| 721 |
#if 0 |
| 722 |
|
| 723 |
for ( i = 0; i < 3; i++ ) |
| 724 |
{ |
| 725 |
mins[ i ] = floor( eMins[ i ] + 0.1 ); |
| 726 |
maxs[ i ] = floor( eMaxs[ i ] + 0.1 ); |
| 727 |
size[ i ] = maxs[ i ] - mins[ i ]; |
| 728 |
} |
| 729 |
|
| 730 |
|
| 731 |
s = floor( point[ 0 ] + 0.1f - mins[ 0 ] ) / size[ 0 ]; |
| 732 |
t = floor( maxs[ 1 ] - point[ 1 ] + 0.1f ) / size[ 1 ]; |
| 733 |
if ( s < 0.0f ) { |
| 734 |
s = 0.0f; |
| 735 |
} |
| 736 |
else if ( s > 1.0f ) { |
| 737 |
s = 1.0f; |
| 738 |
} |
| 739 |
if ( t < 0.0f ) { |
| 740 |
t = 0.0f; |
| 741 |
} |
| 742 |
else if ( t > 1.0f ) { |
| 743 |
t = 1.0f; |
| 744 |
} |
| 745 |
|
| 746 |
|
| 747 |
x = ( im->w - 1 ) * s; |
| 748 |
y = ( im->h - 1 ) * t; |
| 749 |
#else |
| 750 |
|
| 751 |
for ( i = 0; i < 3; i++ ) |
| 752 |
{ |
| 753 |
mins[ i ] = eMins[ i ]; |
| 754 |
maxs[ i ] = eMaxs[ i ]; |
| 755 |
size[ i ] = maxs[ i ] - mins[ i ]; |
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
s = ( point[ 0 ] - mins[ 0 ] ) / size[ 0 ]; |
| 760 |
t = ( maxs[ 1 ] - point[ 1 ] ) / size[ 1 ]; |
| 761 |
|
| 762 |
|
| 763 |
x = s * im->w; |
| 764 |
y = t * im->h; |
| 765 |
if ( x < 0 ) { |
| 766 |
x = 0; |
| 767 |
} |
| 768 |
else if ( x > ( im->w - 1 ) ) { |
| 769 |
x = ( im->w - 1 ); |
| 770 |
} |
| 771 |
if ( y < 0 ) { |
| 772 |
y = 0; |
| 773 |
} |
| 774 |
else if ( y > ( im->h - 1 ) ) { |
| 775 |
y = ( im->h - 1 ); |
| 776 |
} |
| 777 |
#endif |
| 778 |
|
| 779 |
|
| 780 |
return im->pixels[ y * im->w + x ]; |
| 781 |
} |
| 782 |
|
| 783 |
|
| 784 |
#define snprintf_ignore(s, n, format, ...) do { \ |
| 785 |
size_t __n = snprintf(s, n, format, __VA_ARGS__); \ |
| 786 |
if (n >= n) {} \ |
| 787 |
} while (0) |
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
shaderInfo_t *GetIndexedShader( shaderInfo_t *parent, indexMap_t *im, int numPoints, byte *shaderIndexes ){ |
| 796 |
int i; |
| 797 |
byte minShaderIndex, maxShaderIndex; |
| 798 |
char shader[ MAX_QPATH ]; |
| 799 |
shaderInfo_t *si; |
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
if ( im == NULL || numPoints <= 0 || shaderIndexes == NULL ) { |
| 804 |
return ShaderInfoForShader( "default" ); |
| 805 |
} |
| 806 |
|
| 807 |
|
| 808 |
minShaderIndex = 255; |
| 809 |
maxShaderIndex = 0; |
| 810 |
for ( i = 0; i < numPoints; i++ ) |
| 811 |
{ |
| 812 |
if ( shaderIndexes[ i ] < minShaderIndex ) { |
| 813 |
minShaderIndex = shaderIndexes[ i ]; |
| 814 |
} |
| 815 |
if ( shaderIndexes[ i ] > maxShaderIndex ) { |
| 816 |
maxShaderIndex = shaderIndexes[ i ]; |
| 817 |
} |
| 818 |
} |
| 819 |
|
| 820 |
|
| 821 |
for ( i = 0; i < numPoints; i++ ) |
| 822 |
{ |
| 823 |
|
| 824 |
if ( shaderIndexes[ i ] < maxShaderIndex ) { |
| 825 |
shaderIndexes[ i ] = 0; |
| 826 |
} |
| 827 |
else{ |
| 828 |
shaderIndexes[ i ] = 255; |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
|
| 833 |
if ( minShaderIndex == maxShaderIndex ) { |
| 834 |
snprintf_ignore( shader, sizeof shader, "textures/%s_%d", im->shader, maxShaderIndex ); |
| 835 |
} |
| 836 |
else{ |
| 837 |
snprintf_ignore( shader, sizeof shader, "textures/%s_%dto%d", im->shader, minShaderIndex, maxShaderIndex ); |
| 838 |
} |
| 839 |
|
| 840 |
|
| 841 |
si = ShaderInfoForShader( shader ); |
| 842 |
|
| 843 |
|
| 844 |
if ( parent->globalTexture ) { |
| 845 |
si->globalTexture = qtrue; |
| 846 |
} |
| 847 |
if ( parent->forceMeta ) { |
| 848 |
si->forceMeta = qtrue; |
| 849 |
} |
| 850 |
if ( parent->nonplanar ) { |
| 851 |
si->nonplanar = qtrue; |
| 852 |
} |
| 853 |
if ( si->shadeAngleDegrees == 0.0 ) { |
| 854 |
si->shadeAngleDegrees = parent->shadeAngleDegrees; |
| 855 |
} |
| 856 |
if ( parent->tcGen && si->tcGen == qfalse ) { |
| 857 |
|
| 858 |
si->tcGen = qtrue; |
| 859 |
VectorCopy( parent->vecs[ 0 ], si->vecs[ 0 ] ); |
| 860 |
VectorCopy( parent->vecs[ 1 ], si->vecs[ 1 ] ); |
| 861 |
} |
| 862 |
if ( VectorLength( parent->lightmapAxis ) > 0.0f && VectorLength( si->lightmapAxis ) <= 0.0f ) { |
| 863 |
|
| 864 |
VectorCopy( parent->lightmapAxis, si->lightmapAxis ); |
| 865 |
} |
| 866 |
|
| 867 |
|
| 868 |
return si; |
| 869 |
} |
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
#define SNAP_FLOAT_TO_INT 8 |
| 880 |
#define SNAP_INT_TO_FLOAT ( 1.0 / SNAP_FLOAT_TO_INT ) |
| 881 |
|
| 882 |
mapDrawSurface_t *DrawSurfaceForSide( entity_t *e, brush_t *b, side_t *s, winding_t *w ){ |
| 883 |
int i, j, k; |
| 884 |
mapDrawSurface_t *ds; |
| 885 |
shaderInfo_t *si, *parent; |
| 886 |
bspDrawVert_t *dv; |
| 887 |
vec3_t texX, texY; |
| 888 |
vec_t x, y; |
| 889 |
vec3_t vTranslated; |
| 890 |
qboolean indexed; |
| 891 |
byte shaderIndexes[ 256 ]; |
| 892 |
float offsets[ 256 ]; |
| 893 |
char tempShader[ MAX_QPATH ]; |
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
if ( s->culled ) { |
| 898 |
return NULL; |
| 899 |
} |
| 900 |
|
| 901 |
|
| 902 |
if ( w->numpoints > MAX_POINTS_ON_WINDING ) { |
| 903 |
Error( "DrawSurfaceForSide: w->numpoints = %d (> %d)", w->numpoints, MAX_POINTS_ON_WINDING ); |
| 904 |
} |
| 905 |
|
| 906 |
|
| 907 |
si = s->shaderInfo; |
| 908 |
|
| 909 |
|
| 910 |
if ( si->indexed && b->im != NULL ) { |
| 911 |
|
| 912 |
indexed = qtrue; |
| 913 |
|
| 914 |
|
| 915 |
for ( i = 0; i < w->numpoints; i++ ) |
| 916 |
{ |
| 917 |
shaderIndexes[ i ] = GetShaderIndexForPoint( b->im, b->eMins, b->eMaxs, w->p[ i ] ); |
| 918 |
offsets[ i ] = b->im->offsets[ shaderIndexes[ i ] ]; |
| 919 |
|
| 920 |
} |
| 921 |
|
| 922 |
|
| 923 |
parent = si; |
| 924 |
si = GetIndexedShader( parent, b->im, w->numpoints, shaderIndexes ); |
| 925 |
} |
| 926 |
else{ |
| 927 |
indexed = qfalse; |
| 928 |
} |
| 929 |
|
| 930 |
|
| 931 |
if ( skyFixHack && si->skyParmsImageBase[ 0 ] != '\0' ) { |
| 932 |
|
| 933 |
snprintf_ignore( tempShader, sizeof tempShader, "%s_lf", si->skyParmsImageBase ); |
| 934 |
DrawSurfaceForShader( tempShader ); |
| 935 |
snprintf_ignore( tempShader, sizeof tempShader, "%s_rt", si->skyParmsImageBase ); |
| 936 |
DrawSurfaceForShader( tempShader ); |
| 937 |
snprintf_ignore( tempShader, sizeof tempShader, "%s_ft", si->skyParmsImageBase ); |
| 938 |
DrawSurfaceForShader( tempShader ); |
| 939 |
snprintf_ignore( tempShader, sizeof tempShader, "%s_bk", si->skyParmsImageBase ); |
| 940 |
DrawSurfaceForShader( tempShader ); |
| 941 |
snprintf_ignore( tempShader, sizeof tempShader, "%s_up", si->skyParmsImageBase ); |
| 942 |
DrawSurfaceForShader( tempShader ); |
| 943 |
snprintf_ignore( tempShader, sizeof tempShader, "%s_dn", si->skyParmsImageBase ); |
| 944 |
DrawSurfaceForShader( tempShader ); |
| 945 |
} |
| 946 |
|
| 947 |
|
| 948 |
ds = AllocDrawSurface( SURFACE_FACE ); |
| 949 |
ds->entityNum = b->entityNum; |
| 950 |
ds->castShadows = b->castShadows; |
| 951 |
ds->recvShadows = b->recvShadows; |
| 952 |
|
| 953 |
ds->planar = qtrue; |
| 954 |
ds->planeNum = s->planenum; |
| 955 |
VectorCopy( mapplanes[ s->planenum ].normal, ds->lightmapVecs[ 2 ] ); |
| 956 |
|
| 957 |
ds->shaderInfo = si; |
| 958 |
ds->mapBrush = b; |
| 959 |
ds->sideRef = AllocSideRef( s, NULL ); |
| 960 |
ds->fogNum = -1; |
| 961 |
ds->sampleSize = b->lightmapSampleSize; |
| 962 |
ds->lightmapScale = b->lightmapScale; |
| 963 |
ds->numVerts = w->numpoints; |
| 964 |
ds->verts = safe_malloc0( ds->numVerts * sizeof( *ds->verts ) ); |
| 965 |
|
| 966 |
|
| 967 |
ComputeAxisBase( mapplanes[ s->planenum ].normal, texX, texY ); |
| 968 |
|
| 969 |
|
| 970 |
for ( j = 0; j < w->numpoints; j++ ) |
| 971 |
{ |
| 972 |
|
| 973 |
dv = ds->verts + j; |
| 974 |
|
| 975 |
|
| 976 |
VectorCopy( w->p[ j ], dv->xyz ); |
| 977 |
if ( indexed ) { |
| 978 |
dv->xyz[ 2 ] += offsets[ j ]; |
| 979 |
} |
| 980 |
|
| 981 |
|
| 982 |
for ( i = 0 ; i < 3 ; i++ ) |
| 983 |
dv->xyz[ i ] = SNAP_INT_TO_FLOAT * floor( dv->xyz[ i ] * SNAP_FLOAT_TO_INT + 0.5f ); |
| 984 |
VectorAdd( dv->xyz, e->origin, vTranslated ); |
| 985 |
|
| 986 |
|
| 987 |
if ( flat ) { |
| 988 |
dv->st[ 0 ] = si->stFlat[ 0 ]; |
| 989 |
dv->st[ 1 ] = si->stFlat[ 1 ]; |
| 990 |
} |
| 991 |
|
| 992 |
|
| 993 |
else if ( si->tcGen ) { |
| 994 |
dv->st[ 0 ] = DotProduct( si->vecs[ 0 ], vTranslated ); |
| 995 |
dv->st[ 1 ] = DotProduct( si->vecs[ 1 ], vTranslated ); |
| 996 |
} |
| 997 |
|
| 998 |
|
| 999 |
else if ( g_bBrushPrimit == BPRIMIT_OLDBRUSHES ) { |
| 1000 |
|
| 1001 |
dv->st[ 0 ] = s->vecs[ 0 ][ 3 ] + DotProduct( s->vecs[ 0 ], vTranslated ); |
| 1002 |
dv->st[ 1 ] = s->vecs[ 1 ][ 3 ] + DotProduct( s->vecs[ 1 ], vTranslated ); |
| 1003 |
dv->st[ 0 ] /= si->shaderWidth; |
| 1004 |
dv->st[ 1 ] /= si->shaderHeight; |
| 1005 |
} |
| 1006 |
|
| 1007 |
|
| 1008 |
else |
| 1009 |
{ |
| 1010 |
|
| 1011 |
x = DotProduct( vTranslated, texX ); |
| 1012 |
y = DotProduct( vTranslated, texY ); |
| 1013 |
dv->st[ 0 ] = s->texMat[ 0 ][ 0 ] * x + s->texMat[ 0 ][ 1 ] * y + s->texMat[ 0 ][ 2 ]; |
| 1014 |
dv->st[ 1 ] = s->texMat[ 1 ][ 0 ] * x + s->texMat[ 1 ][ 1 ] * y + s->texMat[ 1 ][ 2 ]; |
| 1015 |
} |
| 1016 |
|
| 1017 |
|
| 1018 |
VectorCopy( mapplanes[ s->planenum ].normal, dv->normal ); |
| 1019 |
|
| 1020 |
|
| 1021 |
for ( k = 0; k < MAX_LIGHTMAPS; k++ ) |
| 1022 |
{ |
| 1023 |
dv->color[ k ][ 0 ] = 255; |
| 1024 |
dv->color[ k ][ 1 ] = 255; |
| 1025 |
dv->color[ k ][ 2 ] = 255; |
| 1026 |
|
| 1027 |
|
| 1028 |
dv->color[ k ][ 3 ] = ( indexed ? shaderIndexes[ j ] : 255 ); |
| 1029 |
} |
| 1030 |
} |
| 1031 |
|
| 1032 |
|
| 1033 |
ds->celShader = b->celShader; |
| 1034 |
|
| 1035 |
|
| 1036 |
if ( b->shadeAngleDegrees > 0.0f ) { |
| 1037 |
ds->shadeAngleDegrees = b->shadeAngleDegrees; |
| 1038 |
} |
| 1039 |
|
| 1040 |
|
| 1041 |
return ds; |
| 1042 |
} |
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
#define YDNAR_NORMAL_EPSILON 0.50f |
| 1052 |
|
| 1053 |
qboolean VectorCompareExt( vec3_t n1, vec3_t n2, float epsilon ){ |
| 1054 |
int i; |
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
for ( i = 0; i < 3; i++ ) |
| 1059 |
if ( fabs( n1[ i ] - n2[ i ] ) > epsilon ) { |
| 1060 |
return qfalse; |
| 1061 |
} |
| 1062 |
return qtrue; |
| 1063 |
} |
| 1064 |
|
| 1065 |
mapDrawSurface_t *DrawSurfaceForMesh( entity_t *e, parseMesh_t *p, mesh_t *mesh ){ |
| 1066 |
int i, k, numVerts; |
| 1067 |
vec4_t plane; |
| 1068 |
qboolean planar; |
| 1069 |
float dist; |
| 1070 |
mapDrawSurface_t *ds; |
| 1071 |
shaderInfo_t *si, *parent; |
| 1072 |
bspDrawVert_t *dv; |
| 1073 |
vec3_t vTranslated; |
| 1074 |
mesh_t *copy; |
| 1075 |
qboolean indexed; |
| 1076 |
byte shaderIndexes[ MAX_EXPANDED_AXIS * MAX_EXPANDED_AXIS ]; |
| 1077 |
float offsets[ MAX_EXPANDED_AXIS * MAX_EXPANDED_AXIS ]; |
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
if ( mesh == NULL ) { |
| 1082 |
mesh = &p->mesh; |
| 1083 |
} |
| 1084 |
si = p->shaderInfo; |
| 1085 |
if ( mesh == NULL || si == NULL ) { |
| 1086 |
return NULL; |
| 1087 |
} |
| 1088 |
|
| 1089 |
|
| 1090 |
numVerts = mesh->width * mesh->height; |
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
copy = CopyMesh( mesh ); |
| 1098 |
|
| 1099 |
|
| 1100 |
MakeMeshNormals( *copy ); |
| 1101 |
for ( i = 0; i < numVerts; i++ ) |
| 1102 |
VectorCopy( copy->verts[ i ].normal, mesh->verts[ i ].normal ); |
| 1103 |
|
| 1104 |
|
| 1105 |
PutMeshOnCurve( *copy ); |
| 1106 |
|
| 1107 |
|
| 1108 |
MakeMeshNormals( *copy ); |
| 1109 |
for ( i = 0; i < numVerts; i++ ) |
| 1110 |
{ |
| 1111 |
|
| 1112 |
if ( DotProduct( copy->verts[ i ].normal, mesh->verts[ i ].normal ) < 0.75f ) { |
| 1113 |
VectorCopy( copy->verts[ i ].normal, mesh->verts[ i ].normal ); |
| 1114 |
} |
| 1115 |
} |
| 1116 |
|
| 1117 |
|
| 1118 |
FreeMesh( copy ); |
| 1119 |
|
| 1120 |
|
| 1121 |
if ( si->indexed && p->im != NULL ) { |
| 1122 |
|
| 1123 |
indexed = qtrue; |
| 1124 |
|
| 1125 |
|
| 1126 |
for ( i = 0; i < numVerts; i++ ) |
| 1127 |
{ |
| 1128 |
shaderIndexes[ i ] = GetShaderIndexForPoint( p->im, p->eMins, p->eMaxs, mesh->verts[ i ].xyz ); |
| 1129 |
offsets[ i ] = p->im->offsets[ shaderIndexes[ i ] ]; |
| 1130 |
} |
| 1131 |
|
| 1132 |
|
| 1133 |
parent = si; |
| 1134 |
si = GetIndexedShader( parent, p->im, numVerts, shaderIndexes ); |
| 1135 |
} |
| 1136 |
else{ |
| 1137 |
indexed = qfalse; |
| 1138 |
} |
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
ds = AllocDrawSurface( SURFACE_PATCH ); |
| 1143 |
ds->entityNum = p->entityNum; |
| 1144 |
ds->castShadows = p->castShadows; |
| 1145 |
ds->recvShadows = p->recvShadows; |
| 1146 |
|
| 1147 |
ds->shaderInfo = si; |
| 1148 |
ds->mapMesh = p; |
| 1149 |
ds->sampleSize = p->lightmapSampleSize; |
| 1150 |
ds->lightmapScale = p->lightmapScale; |
| 1151 |
ds->patchWidth = mesh->width; |
| 1152 |
ds->patchHeight = mesh->height; |
| 1153 |
ds->numVerts = ds->patchWidth * ds->patchHeight; |
| 1154 |
ds->verts = safe_malloc( ds->numVerts * sizeof( *ds->verts ) ); |
| 1155 |
memcpy( ds->verts, mesh->verts, ds->numVerts * sizeof( *ds->verts ) ); |
| 1156 |
|
| 1157 |
ds->fogNum = -1; |
| 1158 |
ds->planeNum = -1; |
| 1159 |
|
| 1160 |
ds->longestCurve = p->longestCurve; |
| 1161 |
ds->maxIterations = p->maxIterations; |
| 1162 |
|
| 1163 |
|
| 1164 |
VectorCopy( mesh->verts[ 0 ].normal, plane ); |
| 1165 |
plane[ 3 ] = DotProduct( mesh->verts[ 0 ].xyz, plane ); |
| 1166 |
planar = qtrue; |
| 1167 |
|
| 1168 |
|
| 1169 |
if ( VectorLength( plane ) < 0.001f ) { |
| 1170 |
Sys_Printf( "DrawSurfaceForMesh: bogus plane\n" ); |
| 1171 |
} |
| 1172 |
|
| 1173 |
|
| 1174 |
for ( i = 1; i < ds->numVerts && planar; i++ ) |
| 1175 |
{ |
| 1176 |
|
| 1177 |
if ( VectorCompare( plane, mesh->verts[ i ].normal ) == qfalse ) { |
| 1178 |
planar = qfalse; |
| 1179 |
} |
| 1180 |
|
| 1181 |
|
| 1182 |
dist = DotProduct( mesh->verts[ i ].xyz, plane ) - plane[ 3 ]; |
| 1183 |
if ( fabs( dist ) > EQUAL_EPSILON ) { |
| 1184 |
planar = qfalse; |
| 1185 |
} |
| 1186 |
} |
| 1187 |
|
| 1188 |
|
| 1189 |
if ( planar ) { |
| 1190 |
|
| 1191 |
ds->planeNum = FindFloatPlane( plane, plane[ 3 ], 1, &mesh->verts[ 0 ].xyz ); |
| 1192 |
VectorCopy( plane, ds->lightmapVecs[ 2 ] ); |
| 1193 |
|
| 1194 |
|
| 1195 |
for ( i = 0; i < ds->numVerts; i++ ) |
| 1196 |
VectorCopy( plane, ds->verts[ i ].normal ); |
| 1197 |
} |
| 1198 |
|
| 1199 |
|
| 1200 |
for ( i = 0; i < ds->numVerts; i++ ) |
| 1201 |
{ |
| 1202 |
|
| 1203 |
dv = &ds->verts[ i ]; |
| 1204 |
|
| 1205 |
|
| 1206 |
if ( flat ) { |
| 1207 |
dv->st[ 0 ] = si->stFlat[ 0 ]; |
| 1208 |
dv->st[ 1 ] = si->stFlat[ 1 ]; |
| 1209 |
} |
| 1210 |
|
| 1211 |
|
| 1212 |
else if ( si->tcGen ) { |
| 1213 |
|
| 1214 |
VectorAdd( dv->xyz, e->origin, vTranslated ); |
| 1215 |
dv->st[ 0 ] = DotProduct( si->vecs[ 0 ], vTranslated ); |
| 1216 |
dv->st[ 1 ] = DotProduct( si->vecs[ 1 ], vTranslated ); |
| 1217 |
} |
| 1218 |
|
| 1219 |
|
| 1220 |
for ( k = 0; k < MAX_LIGHTMAPS; k++ ) |
| 1221 |
{ |
| 1222 |
dv->color[ k ][ 0 ] = 255; |
| 1223 |
dv->color[ k ][ 1 ] = 255; |
| 1224 |
dv->color[ k ][ 2 ] = 255; |
| 1225 |
|
| 1226 |
|
| 1227 |
dv->color[ k ][ 3 ] = ( indexed ? shaderIndexes[ i ] : 255 ); |
| 1228 |
} |
| 1229 |
|
| 1230 |
|
| 1231 |
if ( indexed ) { |
| 1232 |
dv->xyz[ 2 ] += offsets[ i ]; |
| 1233 |
} |
| 1234 |
} |
| 1235 |
|
| 1236 |
|
| 1237 |
ds->celShader = p->celShader; |
| 1238 |
|
| 1239 |
|
| 1240 |
return ds; |
| 1241 |
} |
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
mapDrawSurface_t *DrawSurfaceForFlare( int entNum, vec3_t origin, vec3_t normal, vec3_t color, const char *flareShader, int lightStyle ){ |
| 1251 |
mapDrawSurface_t *ds; |
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
if ( emitFlares == qfalse ) { |
| 1256 |
return NULL; |
| 1257 |
} |
| 1258 |
|
| 1259 |
|
| 1260 |
ds = AllocDrawSurface( SURFACE_FLARE ); |
| 1261 |
ds->entityNum = entNum; |
| 1262 |
|
| 1263 |
|
| 1264 |
if ( flareShader != NULL && flareShader[ 0 ] != '\0' ) { |
| 1265 |
ds->shaderInfo = ShaderInfoForShader( flareShader ); |
| 1266 |
} |
| 1267 |
else{ |
| 1268 |
ds->shaderInfo = ShaderInfoForShader( game->flareShader ); |
| 1269 |
} |
| 1270 |
if ( origin != NULL ) { |
| 1271 |
VectorCopy( origin, ds->lightmapOrigin ); |
| 1272 |
} |
| 1273 |
if ( normal != NULL ) { |
| 1274 |
VectorCopy( normal, ds->lightmapVecs[ 2 ] ); |
| 1275 |
} |
| 1276 |
if ( color != NULL ) { |
| 1277 |
VectorCopy( color, ds->lightmapVecs[ 0 ] ); |
| 1278 |
} |
| 1279 |
|
| 1280 |
|
| 1281 |
ds->lightStyle = lightStyle; |
| 1282 |
if ( ds->lightStyle < 0 || ds->lightStyle >= LS_NONE ) { |
| 1283 |
ds->lightStyle = LS_NORMAL; |
| 1284 |
} |
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
return ds; |
| 1290 |
} |
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
mapDrawSurface_t *DrawSurfaceForShader( char *shader ){ |
| 1300 |
int i; |
| 1301 |
shaderInfo_t *si; |
| 1302 |
mapDrawSurface_t *ds; |
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
si = ShaderInfoForShader( shader ); |
| 1307 |
|
| 1308 |
|
| 1309 |
for ( i = 0; i < numMapDrawSurfs; i++ ) |
| 1310 |
{ |
| 1311 |
|
| 1312 |
ds = &mapDrawSurfs[ i ]; |
| 1313 |
|
| 1314 |
|
| 1315 |
if ( ds->shaderInfo == si ) { |
| 1316 |
return ds; |
| 1317 |
} |
| 1318 |
} |
| 1319 |
|
| 1320 |
|
| 1321 |
ds = AllocDrawSurface( SURFACE_SHADER ); |
| 1322 |
ds->entityNum = 0; |
| 1323 |
ds->shaderInfo = ShaderInfoForShader( shader ); |
| 1324 |
|
| 1325 |
|
| 1326 |
return ds; |
| 1327 |
} |
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
static void AddSurfaceFlare( mapDrawSurface_t *ds, vec3_t entityOrigin ){ |
| 1337 |
vec3_t origin; |
| 1338 |
int i; |
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
VectorClear( origin ); |
| 1343 |
for ( i = 0; i < ds->numVerts; i++ ) |
| 1344 |
VectorAdd( origin, ds->verts[ i ].xyz, origin ); |
| 1345 |
VectorScale( origin, ( 1.0f / ds->numVerts ), origin ); |
| 1346 |
if ( entityOrigin != NULL ) { |
| 1347 |
VectorAdd( origin, entityOrigin, origin ); |
| 1348 |
} |
| 1349 |
|
| 1350 |
|
| 1351 |
VectorMA( origin, 2.0f, ds->lightmapVecs[ 2 ], origin ); |
| 1352 |
|
| 1353 |
|
| 1354 |
DrawSurfaceForFlare( ds->entityNum, origin, ds->lightmapVecs[ 2 ], ds->shaderInfo->color, ds->shaderInfo->flareShader, ds->shaderInfo->lightStyle ); |
| 1355 |
} |
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
static void SubdivideFace_r( entity_t *e, brush_t *brush, side_t *side, winding_t *w, int fogNum, float subdivisions ){ |
| 1365 |
int i; |
| 1366 |
int axis; |
| 1367 |
vec3_t bounds[ 2 ]; |
| 1368 |
const float epsilon = 0.1; |
| 1369 |
int subFloor, subCeil; |
| 1370 |
winding_t *frontWinding, *backWinding; |
| 1371 |
mapDrawSurface_t *ds; |
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
if ( w == NULL ) { |
| 1376 |
return; |
| 1377 |
} |
| 1378 |
if ( w->numpoints < 3 ) { |
| 1379 |
Error( "SubdivideFace_r: Bad w->numpoints (%d < 3)", w->numpoints ); |
| 1380 |
} |
| 1381 |
|
| 1382 |
|
| 1383 |
ClearBounds( bounds[ 0 ], bounds[ 1 ] ); |
| 1384 |
for ( i = 0; i < w->numpoints; i++ ) |
| 1385 |
AddPointToBounds( w->p[ i ], bounds[ 0 ], bounds[ 1 ] ); |
| 1386 |
|
| 1387 |
|
| 1388 |
for ( axis = 0; axis < 3; axis++ ) |
| 1389 |
{ |
| 1390 |
vec3_t planePoint = { 0, 0, 0 }; |
| 1391 |
vec3_t planeNormal = { 0, 0, 0 }; |
| 1392 |
float d; |
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
subFloor = floor( bounds[ 0 ][ axis ] / subdivisions ) * subdivisions; |
| 1397 |
subCeil = ceil( bounds[ 1 ][ axis ] / subdivisions ) * subdivisions; |
| 1398 |
planePoint[ axis ] = subFloor + subdivisions; |
| 1399 |
planeNormal[ axis ] = -1; |
| 1400 |
d = DotProduct( planePoint, planeNormal ); |
| 1401 |
|
| 1402 |
|
| 1403 |
if ( ( subCeil - subFloor ) > subdivisions ) { |
| 1404 |
|
| 1405 |
ClipWindingEpsilon( w, planeNormal, d, epsilon, &frontWinding, &backWinding ); |
| 1406 |
|
| 1407 |
|
| 1408 |
if ( frontWinding == NULL ) { |
| 1409 |
w = backWinding; |
| 1410 |
} |
| 1411 |
else if ( backWinding == NULL ) { |
| 1412 |
w = frontWinding; |
| 1413 |
} |
| 1414 |
else |
| 1415 |
{ |
| 1416 |
SubdivideFace_r( e, brush, side, frontWinding, fogNum, subdivisions ); |
| 1417 |
SubdivideFace_r( e, brush, side, backWinding, fogNum, subdivisions ); |
| 1418 |
return; |
| 1419 |
} |
| 1420 |
} |
| 1421 |
} |
| 1422 |
|
| 1423 |
|
| 1424 |
ds = DrawSurfaceForSide( e, brush, side, w ); |
| 1425 |
|
| 1426 |
|
| 1427 |
ds->fogNum = fogNum; |
| 1428 |
} |
| 1429 |
|
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
void SubdivideFaceSurfaces( entity_t *e, tree_t *tree ){ |
| 1439 |
int i, j, numBaseDrawSurfs, fogNum; |
| 1440 |
mapDrawSurface_t *ds; |
| 1441 |
brush_t *brush; |
| 1442 |
side_t *side; |
| 1443 |
shaderInfo_t *si; |
| 1444 |
winding_t *w; |
| 1445 |
float range, size, subdivisions, s2; |
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
Sys_FPrintf( SYS_VRB, "--- SubdivideFaceSurfaces ---\n" ); |
| 1450 |
|
| 1451 |
|
| 1452 |
numBaseDrawSurfs = numMapDrawSurfs; |
| 1453 |
for ( i = e->firstDrawSurf; i < numBaseDrawSurfs; i++ ) |
| 1454 |
{ |
| 1455 |
|
| 1456 |
ds = &mapDrawSurfs[ i ]; |
| 1457 |
|
| 1458 |
|
| 1459 |
if ( ds->type != SURFACE_FACE || ds->mapBrush == NULL || ds->sideRef == NULL || ds->sideRef->side == NULL ) { |
| 1460 |
continue; |
| 1461 |
} |
| 1462 |
|
| 1463 |
|
| 1464 |
brush = ds->mapBrush; |
| 1465 |
side = ds->sideRef->side; |
| 1466 |
|
| 1467 |
|
| 1468 |
si = side->shaderInfo; |
| 1469 |
if ( si == NULL ) { |
| 1470 |
continue; |
| 1471 |
} |
| 1472 |
|
| 1473 |
|
| 1474 |
if ( si->compileFlags & C_SKY ) { |
| 1475 |
continue; |
| 1476 |
} |
| 1477 |
|
| 1478 |
|
| 1479 |
ClassifySurfaces( 1, ds ); |
| 1480 |
if ( CalcSurfaceTextureRange( ds ) == qfalse ) { |
| 1481 |
|
| 1482 |
range = ( ds->texRange[ 0 ] > ds->texRange[ 1 ] ? ds->texRange[ 0 ] : ds->texRange[ 1 ] ); |
| 1483 |
size = ds->maxs[ 0 ] - ds->mins[ 0 ]; |
| 1484 |
for ( j = 1; j < 3; j++ ) |
| 1485 |
if ( ( ds->maxs[ j ] - ds->mins[ j ] ) > size ) { |
| 1486 |
size = ds->maxs[ j ] - ds->mins[ j ]; |
| 1487 |
} |
| 1488 |
subdivisions = ( size / range ) * texRange; |
| 1489 |
subdivisions = ceil( subdivisions / 2 ) * 2; |
| 1490 |
for ( j = 1; j < 8; j++ ) |
| 1491 |
{ |
| 1492 |
s2 = ceil( (float) texRange / j ); |
| 1493 |
if ( fabs( subdivisions - s2 ) <= 4.0 ) { |
| 1494 |
subdivisions = s2; |
| 1495 |
break; |
| 1496 |
} |
| 1497 |
} |
| 1498 |
} |
| 1499 |
else{ |
| 1500 |
subdivisions = si->subdivisions; |
| 1501 |
} |
| 1502 |
|
| 1503 |
|
| 1504 |
if ( si->subdivisions > 0 && si->subdivisions < subdivisions ) { |
| 1505 |
subdivisions = si->subdivisions; |
| 1506 |
} |
| 1507 |
if ( subdivisions < 1.0f ) { |
| 1508 |
continue; |
| 1509 |
} |
| 1510 |
|
| 1511 |
|
| 1512 |
fogNum = ds->fogNum; |
| 1513 |
|
| 1514 |
|
| 1515 |
w = WindingFromDrawSurf( ds ); |
| 1516 |
ClearSurface( ds ); |
| 1517 |
|
| 1518 |
|
| 1519 |
SubdivideFace_r( e, brush, side, w, fogNum, subdivisions ); |
| 1520 |
} |
| 1521 |
} |
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
|
| 1533 |
void ClipSideIntoTree_r( winding_t *w, side_t *side, node_t *node ){ |
| 1534 |
plane_t *plane; |
| 1535 |
winding_t *front, *back; |
| 1536 |
|
| 1537 |
if ( !w ) { |
| 1538 |
return; |
| 1539 |
} |
| 1540 |
|
| 1541 |
if ( node->planenum != PLANENUM_LEAF ) { |
| 1542 |
if ( side->planenum == node->planenum ) { |
| 1543 |
ClipSideIntoTree_r( w, side, node->children[0] ); |
| 1544 |
return; |
| 1545 |
} |
| 1546 |
if ( side->planenum == ( node->planenum ^ 1 ) ) { |
| 1547 |
ClipSideIntoTree_r( w, side, node->children[1] ); |
| 1548 |
return; |
| 1549 |
} |
| 1550 |
|
| 1551 |
plane = &mapplanes[ node->planenum ]; |
| 1552 |
ClipWindingEpsilonStrict( w, plane->normal, plane->dist, |
| 1553 |
ON_EPSILON, &front, &back ); |
| 1554 |
if ( !front && !back ) { |
| 1555 |
|
| 1556 |
front = CopyWinding( w ); |
| 1557 |
back = CopyWinding( w ); |
| 1558 |
} |
| 1559 |
FreeWinding( w ); |
| 1560 |
|
| 1561 |
ClipSideIntoTree_r( front, side, node->children[0] ); |
| 1562 |
ClipSideIntoTree_r( back, side, node->children[1] ); |
| 1563 |
|
| 1564 |
return; |
| 1565 |
} |
| 1566 |
|
| 1567 |
|
| 1568 |
if ( !node->opaque ) { |
| 1569 |
AddWindingToConvexHull( w, &side->visibleHull, mapplanes[ side->planenum ].normal ); |
| 1570 |
} |
| 1571 |
|
| 1572 |
FreeWinding( w ); |
| 1573 |
return; |
| 1574 |
} |
| 1575 |
|
| 1576 |
|
| 1577 |
|
| 1578 |
|
| 1579 |
|
| 1580 |
static int g_numHiddenFaces, g_numCoinFaces; |
| 1581 |
|
| 1582 |
|
| 1583 |
|
| 1584 |
|
| 1585 |
|
| 1586 |
|
| 1587 |
|
| 1588 |
|
| 1589 |
#define CULL_EPSILON 0.1f |
| 1590 |
|
| 1591 |
qboolean CullVectorCompare( const vec3_t v1, const vec3_t v2 ){ |
| 1592 |
int i; |
| 1593 |
|
| 1594 |
|
| 1595 |
for ( i = 0; i < 3; i++ ) |
| 1596 |
if ( fabs( v1[ i ] - v2[ i ] ) > CULL_EPSILON ) { |
| 1597 |
return qfalse; |
| 1598 |
} |
| 1599 |
return qtrue; |
| 1600 |
} |
| 1601 |
|
| 1602 |
|
| 1603 |
|
| 1604 |
|
| 1605 |
|
| 1606 |
|
| 1607 |
|
| 1608 |
|
| 1609 |
qboolean SideInBrush( side_t *side, brush_t *b ){ |
| 1610 |
int i, s; |
| 1611 |
plane_t *plane; |
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
if ( side->winding == NULL || side->shaderInfo == NULL ) { |
| 1616 |
return qtrue; |
| 1617 |
} |
| 1618 |
|
| 1619 |
|
| 1620 |
if ( side->culled == qtrue || ( b->compileFlags & C_TRANSLUCENT ) ) { |
| 1621 |
return qfalse; |
| 1622 |
} |
| 1623 |
|
| 1624 |
|
| 1625 |
for ( i = 0; i < b->numsides; i++ ) |
| 1626 |
{ |
| 1627 |
|
| 1628 |
if ( b->sides[ i ].compileFlags & C_NODRAW ) { |
| 1629 |
return qfalse; |
| 1630 |
} |
| 1631 |
|
| 1632 |
|
| 1633 |
plane = &mapplanes[ b->sides[ i ].planenum ]; |
| 1634 |
s = WindingOnPlaneSide( side->winding, plane->normal, plane->dist ); |
| 1635 |
if ( s == SIDE_FRONT || s == SIDE_CROSS ) { |
| 1636 |
return qfalse; |
| 1637 |
} |
| 1638 |
} |
| 1639 |
|
| 1640 |
|
| 1641 |
if ( side->shaderInfo ) { |
| 1642 |
if ( side->shaderInfo->autosprite || side->shaderInfo->polygonOffset ) { |
| 1643 |
return qfalse; |
| 1644 |
} |
| 1645 |
} |
| 1646 |
|
| 1647 |
|
| 1648 |
side->culled = qtrue; |
| 1649 |
g_numHiddenFaces++; |
| 1650 |
return qtrue; |
| 1651 |
} |
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 1655 |
|
| 1656 |
|
| 1657 |
|
| 1658 |
|
| 1659 |
void CullSides( entity_t *e ){ |
| 1660 |
int numPoints; |
| 1661 |
int i, j, k, l, first, second, dir; |
| 1662 |
winding_t *w1, *w2; |
| 1663 |
brush_t *b1, *b2; |
| 1664 |
side_t *side1, *side2; |
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
Sys_FPrintf( SYS_VRB, "--- CullSides ---\n" ); |
| 1669 |
|
| 1670 |
g_numHiddenFaces = 0; |
| 1671 |
g_numCoinFaces = 0; |
| 1672 |
|
| 1673 |
|
| 1674 |
for ( b1 = e->brushes; b1; b1 = b1->next ) |
| 1675 |
{ |
| 1676 |
|
| 1677 |
if ( b1->numsides < 1 ) { |
| 1678 |
continue; |
| 1679 |
} |
| 1680 |
|
| 1681 |
|
| 1682 |
for ( b2 = b1->next; b2; b2 = b2->next ) |
| 1683 |
{ |
| 1684 |
|
| 1685 |
if ( b2->numsides < 1 ) { |
| 1686 |
continue; |
| 1687 |
} |
| 1688 |
|
| 1689 |
|
| 1690 |
if ( b1->original == b2->original && b1->original != NULL ) { |
| 1691 |
continue; |
| 1692 |
} |
| 1693 |
|
| 1694 |
|
| 1695 |
j = 0; |
| 1696 |
for ( i = 0; i < 3; i++ ) |
| 1697 |
if ( b1->mins[ i ] > b2->maxs[ i ] || b1->maxs[ i ] < b2->mins[ i ] ) { |
| 1698 |
j++; |
| 1699 |
} |
| 1700 |
if ( j ) { |
| 1701 |
continue; |
| 1702 |
} |
| 1703 |
|
| 1704 |
|
| 1705 |
for ( i = 0; i < b1->numsides; i++ ) |
| 1706 |
SideInBrush( &b1->sides[ i ], b2 ); |
| 1707 |
for ( i = 0; i < b2->numsides; i++ ) |
| 1708 |
SideInBrush( &b2->sides[ i ], b1 ); |
| 1709 |
|
| 1710 |
|
| 1711 |
for ( i = 0; i < b1->numsides; i++ ) |
| 1712 |
{ |
| 1713 |
|
| 1714 |
side1 = &b1->sides[ i ]; |
| 1715 |
w1 = side1->winding; |
| 1716 |
if ( w1 == NULL ) { |
| 1717 |
continue; |
| 1718 |
} |
| 1719 |
numPoints = w1->numpoints; |
| 1720 |
if ( side1->shaderInfo == NULL ) { |
| 1721 |
continue; |
| 1722 |
} |
| 1723 |
|
| 1724 |
|
| 1725 |
for ( j = 0; j < b2->numsides; j++ ) |
| 1726 |
{ |
| 1727 |
|
| 1728 |
side2 = &b2->sides[ j ]; |
| 1729 |
w2 = side2->winding; |
| 1730 |
if ( w2 == NULL ) { |
| 1731 |
continue; |
| 1732 |
} |
| 1733 |
if ( side2->shaderInfo == NULL ) { |
| 1734 |
continue; |
| 1735 |
} |
| 1736 |
if ( w1->numpoints != w2->numpoints ) { |
| 1737 |
continue; |
| 1738 |
} |
| 1739 |
if ( side1->culled == qtrue && side2->culled == qtrue ) { |
| 1740 |
continue; |
| 1741 |
} |
| 1742 |
|
| 1743 |
|
| 1744 |
if ( ( side1->planenum & ~0x00000001 ) != ( side2->planenum & ~0x00000001 ) ) { |
| 1745 |
continue; |
| 1746 |
} |
| 1747 |
|
| 1748 |
|
| 1749 |
if ( side1->shaderInfo && |
| 1750 |
( side1->shaderInfo->autosprite || side1->shaderInfo->polygonOffset ) ) { |
| 1751 |
continue; |
| 1752 |
} |
| 1753 |
if ( side2->shaderInfo && |
| 1754 |
( side2->shaderInfo->autosprite || side2->shaderInfo->polygonOffset ) ) { |
| 1755 |
continue; |
| 1756 |
} |
| 1757 |
|
| 1758 |
|
| 1759 |
first = -1; |
| 1760 |
for ( k = 0; k < numPoints; k++ ) |
| 1761 |
{ |
| 1762 |
if ( VectorCompare( w1->p[ 0 ], w2->p[ k ] ) ) { |
| 1763 |
first = k; |
| 1764 |
k = numPoints; |
| 1765 |
} |
| 1766 |
} |
| 1767 |
if ( first == -1 ) { |
| 1768 |
continue; |
| 1769 |
} |
| 1770 |
|
| 1771 |
|
| 1772 |
second = -1; |
| 1773 |
dir = 0; |
| 1774 |
if ( ( first + 1 ) < numPoints ) { |
| 1775 |
second = first + 1; |
| 1776 |
} |
| 1777 |
else{ |
| 1778 |
second = 0; |
| 1779 |
} |
| 1780 |
if ( CullVectorCompare( w1->p[ 1 ], w2->p[ second ] ) ) { |
| 1781 |
dir = 1; |
| 1782 |
} |
| 1783 |
else |
| 1784 |
{ |
| 1785 |
if ( first > 0 ) { |
| 1786 |
second = first - 1; |
| 1787 |
} |
| 1788 |
else{ |
| 1789 |
second = numPoints - 1; |
| 1790 |
} |
| 1791 |
if ( CullVectorCompare( w1->p[ 1 ], w2->p[ second ] ) ) { |
| 1792 |
dir = -1; |
| 1793 |
} |
| 1794 |
} |
| 1795 |
if ( dir == 0 ) { |
| 1796 |
continue; |
| 1797 |
} |
| 1798 |
|
| 1799 |
|
| 1800 |
l = first; |
| 1801 |
for ( k = 0; k < numPoints; k++ ) |
| 1802 |
{ |
| 1803 |
if ( !CullVectorCompare( w1->p[ k ], w2->p[ l ] ) ) { |
| 1804 |
k = 100000; |
| 1805 |
} |
| 1806 |
|
| 1807 |
l += dir; |
| 1808 |
if ( l < 0 ) { |
| 1809 |
l = numPoints - 1; |
| 1810 |
} |
| 1811 |
else if ( l >= numPoints ) { |
| 1812 |
l = 0; |
| 1813 |
} |
| 1814 |
} |
| 1815 |
if ( k >= 100000 ) { |
| 1816 |
continue; |
| 1817 |
} |
| 1818 |
|
| 1819 |
|
| 1820 |
if ( !side2->culled && !( side2->compileFlags & C_TRANSLUCENT ) && !( side2->compileFlags & C_NODRAW ) ) { |
| 1821 |
side1->culled = qtrue; |
| 1822 |
g_numCoinFaces++; |
| 1823 |
} |
| 1824 |
|
| 1825 |
if ( side1->planenum == side2->planenum && side1->culled == qtrue ) { |
| 1826 |
continue; |
| 1827 |
} |
| 1828 |
|
| 1829 |
|
| 1830 |
if ( !side1->culled && !( side1->compileFlags & C_TRANSLUCENT ) && !( side1->compileFlags & C_NODRAW ) ) { |
| 1831 |
side2->culled = qtrue; |
| 1832 |
g_numCoinFaces++; |
| 1833 |
} |
| 1834 |
} |
| 1835 |
} |
| 1836 |
} |
| 1837 |
} |
| 1838 |
|
| 1839 |
|
| 1840 |
Sys_FPrintf( SYS_VRB, "%9d hidden faces culled\n", g_numHiddenFaces ); |
| 1841 |
Sys_FPrintf( SYS_VRB, "%9d coincident faces culled\n", g_numCoinFaces ); |
| 1842 |
} |
| 1843 |
|
| 1844 |
|
| 1845 |
|
| 1846 |
|
| 1847 |
|
| 1848 |
|
| 1849 |
|
| 1850 |
|
| 1851 |
|
| 1852 |
|
| 1853 |
|
| 1854 |
|
| 1855 |
|
| 1856 |
|
| 1857 |
void ClipSidesIntoTree( entity_t *e, tree_t *tree ){ |
| 1858 |
brush_t *b; |
| 1859 |
int i; |
| 1860 |
winding_t *w; |
| 1861 |
side_t *side, *newSide; |
| 1862 |
shaderInfo_t *si; |
| 1863 |
|
| 1864 |
|
| 1865 |
|
| 1866 |
CullSides( e ); |
| 1867 |
|
| 1868 |
|
| 1869 |
Sys_FPrintf( SYS_VRB, "--- ClipSidesIntoTree ---\n" ); |
| 1870 |
|
| 1871 |
|
| 1872 |
for ( b = e->brushes; b; b = b->next ) |
| 1873 |
{ |
| 1874 |
|
| 1875 |
for ( i = 0; i < b->numsides; i++ ) |
| 1876 |
{ |
| 1877 |
|
| 1878 |
side = &b->sides[ i ]; |
| 1879 |
if ( side->winding == NULL ) { |
| 1880 |
continue; |
| 1881 |
} |
| 1882 |
|
| 1883 |
|
| 1884 |
w = CopyWinding( side->winding ); |
| 1885 |
side->visibleHull = NULL; |
| 1886 |
ClipSideIntoTree_r( w, side, tree->headnode ); |
| 1887 |
|
| 1888 |
|
| 1889 |
w = side->visibleHull; |
| 1890 |
if ( w == NULL ) { |
| 1891 |
continue; |
| 1892 |
} |
| 1893 |
|
| 1894 |
|
| 1895 |
si = side->shaderInfo; |
| 1896 |
if ( si == NULL ) { |
| 1897 |
continue; |
| 1898 |
} |
| 1899 |
|
| 1900 |
|
| 1901 |
|
| 1902 |
if ( ( si->compileFlags & C_NODRAW ) && si->indexed == qfalse && !( si->compileFlags & C_FOG ) ) { |
| 1903 |
continue; |
| 1904 |
} |
| 1905 |
|
| 1906 |
|
| 1907 |
if ( si->autosprite || si->noClip ) { |
| 1908 |
w = side->winding; |
| 1909 |
} |
| 1910 |
|
| 1911 |
|
| 1912 |
DrawSurfaceForSide( e, b, side, w ); |
| 1913 |
|
| 1914 |
|
| 1915 |
if ( !( si->compileFlags & C_FOG ) ) { |
| 1916 |
continue; |
| 1917 |
} |
| 1918 |
|
| 1919 |
|
| 1920 |
w = ReverseWinding( w ); |
| 1921 |
newSide = safe_malloc( sizeof( *side ) ); |
| 1922 |
*newSide = *side; |
| 1923 |
newSide->visibleHull = w; |
| 1924 |
newSide->planenum ^= 1; |
| 1925 |
|
| 1926 |
|
| 1927 |
DrawSurfaceForSide( e, b, newSide, w ); |
| 1928 |
} |
| 1929 |
} |
| 1930 |
} |
| 1931 |
|
| 1932 |
|
| 1933 |
|
| 1934 |
|
| 1935 |
|
| 1936 |
|
| 1937 |
|
| 1938 |
|
| 1939 |
|
| 1940 |
|
| 1941 |
|
| 1942 |
|
| 1943 |
|
| 1944 |
|
| 1945 |
|
| 1946 |
int AddReferenceToLeaf( mapDrawSurface_t *ds, node_t *node ){ |
| 1947 |
drawSurfRef_t *dsr; |
| 1948 |
|
| 1949 |
|
| 1950 |
|
| 1951 |
if ( node->planenum != PLANENUM_LEAF || node->opaque ) { |
| 1952 |
return 0; |
| 1953 |
} |
| 1954 |
|
| 1955 |
|
| 1956 |
for ( dsr = node->drawSurfReferences; dsr; dsr = dsr->nextRef ) |
| 1957 |
{ |
| 1958 |
if ( dsr->outputNum == numBSPDrawSurfaces ) { |
| 1959 |
return 0; |
| 1960 |
} |
| 1961 |
} |
| 1962 |
|
| 1963 |
|
| 1964 |
dsr = safe_malloc( sizeof( *dsr ) ); |
| 1965 |
dsr->outputNum = numBSPDrawSurfaces; |
| 1966 |
dsr->nextRef = node->drawSurfReferences; |
| 1967 |
node->drawSurfReferences = dsr; |
| 1968 |
|
| 1969 |
|
| 1970 |
if ( node->skybox ) { |
| 1971 |
ds->skybox = qtrue; |
| 1972 |
} |
| 1973 |
if ( ds->shaderInfo->compileFlags & C_SKY ) { |
| 1974 |
node->sky = qtrue; |
| 1975 |
} |
| 1976 |
|
| 1977 |
|
| 1978 |
return 1; |
| 1979 |
} |
| 1980 |
|
| 1981 |
|
| 1982 |
|
| 1983 |
|
| 1984 |
|
| 1985 |
|
| 1986 |
|
| 1987 |
|
| 1988 |
int AddReferenceToTree_r( mapDrawSurface_t *ds, node_t *node, qboolean skybox ){ |
| 1989 |
int i, refs = 0; |
| 1990 |
|
| 1991 |
|
| 1992 |
|
| 1993 |
if ( node == NULL ) { |
| 1994 |
return 0; |
| 1995 |
} |
| 1996 |
|
| 1997 |
|
| 1998 |
if ( node->planenum != PLANENUM_LEAF ) { |
| 1999 |
|
| 2000 |
refs += AddReferenceToTree_r( ds, node->children[ 0 ], skybox ); |
| 2001 |
refs += AddReferenceToTree_r( ds, node->children[ 1 ], skybox ); |
| 2002 |
return refs; |
| 2003 |
} |
| 2004 |
|
| 2005 |
|
| 2006 |
if ( skybox ) { |
| 2007 |
|
| 2008 |
if ( !node->sky ) { |
| 2009 |
return 0; |
| 2010 |
} |
| 2011 |
|
| 2012 |
|
| 2013 |
for ( i = 0; i < ds->numVerts; i++ ) |
| 2014 |
AddPointToBounds( ds->verts[ i ].xyz, node->mins, node->maxs ); |
| 2015 |
} |
| 2016 |
|
| 2017 |
|
| 2018 |
return AddReferenceToLeaf( ds, node ); |
| 2019 |
} |
| 2020 |
|
| 2021 |
|
| 2022 |
|
| 2023 |
|
| 2024 |
|
| 2025 |
|
| 2026 |
|
| 2027 |
|
| 2028 |
int FilterPointIntoTree_r( vec3_t point, mapDrawSurface_t *ds, node_t *node ){ |
| 2029 |
float d; |
| 2030 |
plane_t *plane; |
| 2031 |
int refs = 0; |
| 2032 |
|
| 2033 |
|
| 2034 |
|
| 2035 |
if ( node->planenum != PLANENUM_LEAF ) { |
| 2036 |
|
| 2037 |
plane = &mapplanes[ node->planenum ]; |
| 2038 |
d = DotProduct( point, plane->normal ) - plane->dist; |
| 2039 |
|
| 2040 |
|
| 2041 |
refs = 0; |
| 2042 |
if ( d >= -ON_EPSILON ) { |
| 2043 |
refs += FilterPointIntoTree_r( point, ds, node->children[ 0 ] ); |
| 2044 |
} |
| 2045 |
if ( d <= ON_EPSILON ) { |
| 2046 |
refs += FilterPointIntoTree_r( point, ds, node->children[ 1 ] ); |
| 2047 |
} |
| 2048 |
|
| 2049 |
|
| 2050 |
return refs; |
| 2051 |
} |
| 2052 |
|
| 2053 |
|
| 2054 |
return AddReferenceToLeaf( ds, node ); |
| 2055 |
} |
| 2056 |
|
| 2057 |
|
| 2058 |
|
| 2059 |
|
| 2060 |
|
| 2061 |
|
| 2062 |
|
| 2063 |
|
| 2064 |
int FilterPointConvexHullIntoTree_r( vec3_t **points, int npoints, mapDrawSurface_t *ds, node_t *node ){ |
| 2065 |
float d, dmin, dmax; |
| 2066 |
plane_t *plane; |
| 2067 |
int refs = 0; |
| 2068 |
int i; |
| 2069 |
|
| 2070 |
if ( !points ) { |
| 2071 |
return 0; |
| 2072 |
} |
| 2073 |
|
| 2074 |
|
| 2075 |
if ( node->planenum != PLANENUM_LEAF ) { |
| 2076 |
|
| 2077 |
plane = &mapplanes[ node->planenum ]; |
| 2078 |
|
| 2079 |
dmin = dmax = DotProduct( *( points[0] ), plane->normal ) - plane->dist; |
| 2080 |
for ( i = 1; i < npoints; ++i ) |
| 2081 |
{ |
| 2082 |
d = DotProduct( *( points[i] ), plane->normal ) - plane->dist; |
| 2083 |
if ( d > dmax ) { |
| 2084 |
dmax = d; |
| 2085 |
} |
| 2086 |
if ( d < dmin ) { |
| 2087 |
dmin = d; |
| 2088 |
} |
| 2089 |
} |
| 2090 |
|
| 2091 |
|
| 2092 |
refs = 0; |
| 2093 |
if ( dmax >= -ON_EPSILON ) { |
| 2094 |
refs += FilterPointConvexHullIntoTree_r( points, npoints, ds, node->children[ 0 ] ); |
| 2095 |
} |
| 2096 |
if ( dmin <= ON_EPSILON ) { |
| 2097 |
refs += FilterPointConvexHullIntoTree_r( points, npoints, ds, node->children[ 1 ] ); |
| 2098 |
} |
| 2099 |
|
| 2100 |
|
| 2101 |
return refs; |
| 2102 |
} |
| 2103 |
|
| 2104 |
|
| 2105 |
return AddReferenceToLeaf( ds, node ); |
| 2106 |
} |
| 2107 |
|
| 2108 |
|
| 2109 |
|
| 2110 |
|
| 2111 |
|
| 2112 |
|
| 2113 |
|
| 2114 |
|
| 2115 |
int FilterWindingIntoTree_r( winding_t *w, mapDrawSurface_t *ds, node_t *node ){ |
| 2116 |
int i, refs = 0; |
| 2117 |
plane_t *p1; |
| 2118 |
vec4_t plane1; |
| 2119 |
winding_t *fat, *front, *back; |
| 2120 |
shaderInfo_t *si; |
| 2121 |
|
| 2122 |
|
| 2123 |
|
| 2124 |
si = ds->shaderInfo; |
| 2125 |
|
| 2126 |
|
| 2127 |
if ( node->parent == NULL && si != NULL && |
| 2128 |
( si->mins[ 0 ] != 0.0f || si->maxs[ 0 ] != 0.0f || |
| 2129 |
si->mins[ 1 ] != 0.0f || si->maxs[ 1 ] != 0.0f || |
| 2130 |
si->mins[ 2 ] != 0.0f || si->maxs[ 2 ] != 0.0f ) ) { |
| 2131 |
static qboolean warned = qfalse; |
| 2132 |
if ( !warned ) { |
| 2133 |
Sys_FPrintf( SYS_WRN, "WARNING: this map uses the deformVertexes move hack\n" ); |
| 2134 |
warned = qtrue; |
| 2135 |
} |
| 2136 |
|
| 2137 |
|
| 2138 |
|
| 2139 |
fat = AllocWinding( w->numpoints * 3 + 3 ); |
| 2140 |
fat->numpoints = w->numpoints * 3 + 3; |
| 2141 |
for ( i = 0; i < w->numpoints; i++ ) |
| 2142 |
{ |
| 2143 |
VectorCopy( w->p[ i ], fat->p[ i ] ); |
| 2144 |
VectorAdd( w->p[ i ], si->mins, fat->p[ i + ( w->numpoints + 1 ) ] ); |
| 2145 |
VectorAdd( w->p[ i ], si->maxs, fat->p[ i + ( w->numpoints + 1 ) * 2 ] ); |
| 2146 |
} |
| 2147 |
VectorCopy( w->p[ 0 ], fat->p[ i ] ); |
| 2148 |
VectorAdd( w->p[ 0 ], si->mins, fat->p[ i + w->numpoints ] ); |
| 2149 |
VectorAdd( w->p[ 0 ], si->maxs, fat->p[ i + w->numpoints * 2 ] ); |
| 2150 |
|
| 2151 |
|
| 2152 |
|
| 2153 |
|
| 2154 |
|
| 2155 |
|
| 2156 |
|
| 2157 |
|
| 2158 |
|
| 2159 |
FreeWinding( w ); |
| 2160 |
w = fat; |
| 2161 |
} |
| 2162 |
|
| 2163 |
|
| 2164 |
if ( node->planenum != PLANENUM_LEAF ) { |
| 2165 |
|
| 2166 |
p1 = &mapplanes[ node->planenum ]; |
| 2167 |
VectorCopy( p1->normal, plane1 ); |
| 2168 |
plane1[ 3 ] = p1->dist; |
| 2169 |
|
| 2170 |
|
| 2171 |
if ( ds->planeNum >= 0 ) { |
| 2172 |
#if 0 |
| 2173 |
plane_t *p2; |
| 2174 |
vec4_t plane2; |
| 2175 |
|
| 2176 |
|
| 2177 |
p2 = &mapplanes[ ds->planeNum ]; |
| 2178 |
VectorCopy( p2->normal, plane2 ); |
| 2179 |
plane2[ 3 ] = p2->dist; |
| 2180 |
|
| 2181 |
|
| 2182 |
vec4_t reverse; |
| 2183 |
|
| 2184 |
|
| 2185 |
VectorSubtract( vec3_origin, plane2, reverse ); |
| 2186 |
reverse[ 3 ] = -plane2[ 3 ]; |
| 2187 |
|
| 2188 |
|
| 2189 |
if ( DotProduct( plane1, plane2 ) > 0.999f && fabs( plane1[ 3 ] - plane2[ 3 ] ) < 0.001f ) { |
| 2190 |
return FilterWindingIntoTree_r( w, ds, node->children[ 0 ] ); |
| 2191 |
} |
| 2192 |
if ( DotProduct( plane1, reverse ) > 0.999f && fabs( plane1[ 3 ] - reverse[ 3 ] ) < 0.001f ) { |
| 2193 |
return FilterWindingIntoTree_r( w, ds, node->children[ 1 ] ); |
| 2194 |
} |
| 2195 |
#else |
| 2196 |
|
| 2197 |
|
| 2198 |
|
| 2199 |
if ( ds->planeNum == node->planenum ) { |
| 2200 |
return FilterWindingIntoTree_r( w, ds, node->children[ 0 ] ); |
| 2201 |
} |
| 2202 |
if ( ds->planeNum == ( node->planenum ^ 1 ) ) { |
| 2203 |
return FilterWindingIntoTree_r( w, ds, node->children[ 1 ] ); |
| 2204 |
} |
| 2205 |
#endif |
| 2206 |
} |
| 2207 |
|
| 2208 |
|
| 2209 |
ClipWindingEpsilonStrict( w, plane1, plane1[ 3 ], ON_EPSILON, &front, &back ); |
| 2210 |
|
| 2211 |
|
| 2212 |
refs = 0; |
| 2213 |
if ( front == NULL && back == NULL ) { |
| 2214 |
|
| 2215 |
|
| 2216 |
refs += FilterWindingIntoTree_r( CopyWinding( w ), ds, node->children[ 0 ] ); |
| 2217 |
refs += FilterWindingIntoTree_r( CopyWinding( w ), ds, node->children[ 1 ] ); |
| 2218 |
} |
| 2219 |
if ( front != NULL ) { |
| 2220 |
refs += FilterWindingIntoTree_r( front, ds, node->children[ 0 ] ); |
| 2221 |
} |
| 2222 |
if ( back != NULL ) { |
| 2223 |
refs += FilterWindingIntoTree_r( back, ds, node->children[ 1 ] ); |
| 2224 |
} |
| 2225 |
FreeWinding( w ); |
| 2226 |
|
| 2227 |
|
| 2228 |
return refs; |
| 2229 |
} |
| 2230 |
|
| 2231 |
|
| 2232 |
return AddReferenceToLeaf( ds, node ); |
| 2233 |
} |
| 2234 |
|
| 2235 |
|
| 2236 |
|
| 2237 |
|
| 2238 |
|
| 2239 |
|
| 2240 |
|
| 2241 |
|
| 2242 |
int FilterFaceIntoTree( mapDrawSurface_t *ds, tree_t *tree ){ |
| 2243 |
winding_t *w; |
| 2244 |
int refs = 0; |
| 2245 |
|
| 2246 |
|
| 2247 |
|
| 2248 |
w = WindingFromDrawSurf( ds ); |
| 2249 |
refs = FilterWindingIntoTree_r( w, ds, tree->headnode ); |
| 2250 |
|
| 2251 |
|
| 2252 |
return refs; |
| 2253 |
} |
| 2254 |
|
| 2255 |
|
| 2256 |
|
| 2257 |
|
| 2258 |
|
| 2259 |
|
| 2260 |
|
| 2261 |
|
| 2262 |
#define FILTER_SUBDIVISION 8 |
| 2263 |
|
| 2264 |
static int FilterPatchIntoTree( mapDrawSurface_t *ds, tree_t *tree ){ |
| 2265 |
int x, y, refs = 0; |
| 2266 |
|
| 2267 |
for ( y = 0; y + 2 < ds->patchHeight; y += 2 ) |
| 2268 |
for ( x = 0; x + 2 < ds->patchWidth; x += 2 ) |
| 2269 |
{ |
| 2270 |
vec3_t *points[9]; |
| 2271 |
points[0] = &ds->verts[( y + 0 ) * ds->patchWidth + ( x + 0 )].xyz; |
| 2272 |
points[1] = &ds->verts[( y + 0 ) * ds->patchWidth + ( x + 1 )].xyz; |
| 2273 |
points[2] = &ds->verts[( y + 0 ) * ds->patchWidth + ( x + 2 )].xyz; |
| 2274 |
points[3] = &ds->verts[( y + 1 ) * ds->patchWidth + ( x + 0 )].xyz; |
| 2275 |
points[4] = &ds->verts[( y + 1 ) * ds->patchWidth + ( x + 1 )].xyz; |
| 2276 |
points[5] = &ds->verts[( y + 1 ) * ds->patchWidth + ( x + 2 )].xyz; |
| 2277 |
points[6] = &ds->verts[( y + 2 ) * ds->patchWidth + ( x + 0 )].xyz; |
| 2278 |
points[7] = &ds->verts[( y + 2 ) * ds->patchWidth + ( x + 1 )].xyz; |
| 2279 |
points[8] = &ds->verts[( y + 2 ) * ds->patchWidth + ( x + 2 )].xyz; |
| 2280 |
refs += FilterPointConvexHullIntoTree_r( points, 9, ds, tree->headnode ); |
| 2281 |
} |
| 2282 |
|
| 2283 |
return refs; |
| 2284 |
} |
| 2285 |
|
| 2286 |
|
| 2287 |
|
| 2288 |
|
| 2289 |
|
| 2290 |
|
| 2291 |
|
| 2292 |
|
| 2293 |
static int FilterTrianglesIntoTree( mapDrawSurface_t *ds, tree_t *tree ){ |
| 2294 |
int i, refs; |
| 2295 |
winding_t *w; |
| 2296 |
|
| 2297 |
|
| 2298 |
|
| 2299 |
refs = 0; |
| 2300 |
for ( i = 0; i < ds->numIndexes; i += 3 ) |
| 2301 |
{ |
| 2302 |
|
| 2303 |
if ( ds->indexes[ i ] >= ds->numVerts || |
| 2304 |
ds->indexes[ i + 1 ] >= ds->numVerts || |
| 2305 |
ds->indexes[ i + 2 ] >= ds->numVerts ) { |
| 2306 |
Error( "Index %d greater than vertex count %d", ds->indexes[ i ], ds->numVerts ); |
| 2307 |
} |
| 2308 |
|
| 2309 |
|
| 2310 |
w = AllocWinding( 3 ); |
| 2311 |
w->numpoints = 3; |
| 2312 |
VectorCopy( ds->verts[ ds->indexes[ i ] ].xyz, w->p[ 0 ] ); |
| 2313 |
VectorCopy( ds->verts[ ds->indexes[ i + 1 ] ].xyz, w->p[ 1 ] ); |
| 2314 |
VectorCopy( ds->verts[ ds->indexes[ i + 2 ] ].xyz, w->p[ 2 ] ); |
| 2315 |
refs += FilterWindingIntoTree_r( w, ds, tree->headnode ); |
| 2316 |
} |
| 2317 |
|
| 2318 |
return refs; |
| 2319 |
} |
| 2320 |
|
| 2321 |
|
| 2322 |
|
| 2323 |
|
| 2324 |
|
| 2325 |
|
| 2326 |
|
| 2327 |
|
| 2328 |
static int FilterFoliageIntoTree( mapDrawSurface_t *ds, tree_t *tree ){ |
| 2329 |
int f, i, refs; |
| 2330 |
bspDrawVert_t *instance; |
| 2331 |
vec3_t xyz; |
| 2332 |
winding_t *w; |
| 2333 |
|
| 2334 |
|
| 2335 |
|
| 2336 |
refs = 0; |
| 2337 |
for ( f = 0; f < ds->numFoliageInstances; f++ ) |
| 2338 |
{ |
| 2339 |
|
| 2340 |
instance = ds->verts + ds->patchHeight + f; |
| 2341 |
|
| 2342 |
|
| 2343 |
for ( i = 0; i < ds->numIndexes; i += 3 ) |
| 2344 |
{ |
| 2345 |
|
| 2346 |
if ( ds->indexes[ i ] >= ds->numVerts || |
| 2347 |
ds->indexes[ i + 1 ] >= ds->numVerts || |
| 2348 |
ds->indexes[ i + 2 ] >= ds->numVerts ) { |
| 2349 |
Error( "Index %d greater than vertex count %d", ds->indexes[ i ], ds->numVerts ); |
| 2350 |
} |
| 2351 |
|
| 2352 |
|
| 2353 |
w = AllocWinding( 3 ); |
| 2354 |
w->numpoints = 3; |
| 2355 |
VectorAdd( instance->xyz, ds->verts[ ds->indexes[ i ] ].xyz, w->p[ 0 ] ); |
| 2356 |
VectorAdd( instance->xyz, ds->verts[ ds->indexes[ i + 1 ] ].xyz, w->p[ 1 ] ); |
| 2357 |
VectorAdd( instance->xyz, ds->verts[ ds->indexes[ i + 2 ] ].xyz, w->p[ 2 ] ); |
| 2358 |
refs += FilterWindingIntoTree_r( w, ds, tree->headnode ); |
| 2359 |
} |
| 2360 |
} |
| 2361 |
|
| 2362 |
return refs; |
| 2363 |
} |
| 2364 |
|
| 2365 |
|
| 2366 |
|
| 2367 |
|
| 2368 |
|
| 2369 |
|
| 2370 |
|
| 2371 |
static int FilterFlareSurfIntoTree( mapDrawSurface_t *ds, tree_t *tree ){ |
| 2372 |
return FilterPointIntoTree_r( ds->lightmapOrigin, ds, tree->headnode ); |
| 2373 |
} |
| 2374 |
|
| 2375 |
|
| 2376 |
|
| 2377 |
|
| 2378 |
|
| 2379 |
|
| 2380 |
|
| 2381 |
|
| 2382 |
void EmitDrawVerts( mapDrawSurface_t *ds, bspDrawSurface_t *out ){ |
| 2383 |
int i, k; |
| 2384 |
bspDrawVert_t *dv; |
| 2385 |
shaderInfo_t *si; |
| 2386 |
float offset; |
| 2387 |
|
| 2388 |
|
| 2389 |
|
| 2390 |
si = ds->shaderInfo; |
| 2391 |
offset = si->offset; |
| 2392 |
|
| 2393 |
|
| 2394 |
out->firstVert = numBSPDrawVerts; |
| 2395 |
out->numVerts = ds->numVerts; |
| 2396 |
for ( i = 0; i < ds->numVerts; i++ ) |
| 2397 |
{ |
| 2398 |
|
| 2399 |
IncDrawVerts(); |
| 2400 |
dv = &bspDrawVerts[ numBSPDrawVerts - 1 ]; |
| 2401 |
|
| 2402 |
|
| 2403 |
memcpy( dv, &ds->verts[ i ], sizeof( *dv ) ); |
| 2404 |
|
| 2405 |
|
| 2406 |
if ( offset != 0.0f ) { |
| 2407 |
VectorMA( dv->xyz, offset, dv->normal, dv->xyz ); |
| 2408 |
} |
| 2409 |
|
| 2410 |
|
| 2411 |
|
| 2412 |
|
| 2413 |
if ( numBSPModels > 0 ) { |
| 2414 |
AddPointToBounds( dv->xyz, bspModels[ numBSPModels ].mins, bspModels[ numBSPModels ].maxs ); |
| 2415 |
} |
| 2416 |
|
| 2417 |
|
| 2418 |
if ( debugSurfaces ) { |
| 2419 |
for ( k = 0; k < MAX_LIGHTMAPS; k++ ) |
| 2420 |
VectorCopy( debugColors[ ( ds - mapDrawSurfs ) % 12 ], dv->color[ k ] ); |
| 2421 |
} |
| 2422 |
} |
| 2423 |
} |
| 2424 |
|
| 2425 |
|
| 2426 |
|
| 2427 |
|
| 2428 |
|
| 2429 |
|
| 2430 |
|
| 2431 |
|
| 2432 |
|
| 2433 |
|
| 2434 |
int FindDrawIndexes( int numIndexes, int *indexes ){ |
| 2435 |
int i, j, numTestIndexes; |
| 2436 |
|
| 2437 |
|
| 2438 |
|
| 2439 |
if ( numIndexes < 3 || numBSPDrawIndexes < numIndexes || indexes == NULL ) { |
| 2440 |
return numBSPDrawIndexes; |
| 2441 |
} |
| 2442 |
|
| 2443 |
|
| 2444 |
numTestIndexes = 1 + numBSPDrawIndexes - numIndexes; |
| 2445 |
|
| 2446 |
|
| 2447 |
if ( numIndexes == 3 ) { |
| 2448 |
|
| 2449 |
for ( i = 0; i < numTestIndexes; i++ ) |
| 2450 |
{ |
| 2451 |
|
| 2452 |
if ( indexes[ 0 ] == bspDrawIndexes[ i ] && |
| 2453 |
indexes[ 1 ] == bspDrawIndexes[ i + 1 ] && |
| 2454 |
indexes[ 2 ] == bspDrawIndexes[ i + 2 ] ) { |
| 2455 |
numRedundantIndexes += numIndexes; |
| 2456 |
return i; |
| 2457 |
} |
| 2458 |
} |
| 2459 |
|
| 2460 |
|
| 2461 |
return numBSPDrawIndexes; |
| 2462 |
} |
| 2463 |
|
| 2464 |
|
| 2465 |
for ( i = 0; i < numTestIndexes; i++ ) |
| 2466 |
{ |
| 2467 |
|
| 2468 |
if ( indexes[ 0 ] == bspDrawIndexes[ i ] && |
| 2469 |
indexes[ 1 ] == bspDrawIndexes[ i + 1 ] && |
| 2470 |
indexes[ 2 ] == bspDrawIndexes[ i + 2 ] && |
| 2471 |
indexes[ 3 ] == bspDrawIndexes[ i + 3 ] ) { |
| 2472 |
|
| 2473 |
if ( numIndexes == 4 ) { |
| 2474 |
return i; |
| 2475 |
} |
| 2476 |
|
| 2477 |
|
| 2478 |
for ( j = 4; j < numIndexes; j++ ) |
| 2479 |
{ |
| 2480 |
if ( indexes[ j ] != bspDrawIndexes[ i + j ] ) { |
| 2481 |
break; |
| 2482 |
} |
| 2483 |
else if ( j == ( numIndexes - 1 ) ) { |
| 2484 |
numRedundantIndexes += numIndexes; |
| 2485 |
return i; |
| 2486 |
} |
| 2487 |
} |
| 2488 |
} |
| 2489 |
} |
| 2490 |
|
| 2491 |
|
| 2492 |
return numBSPDrawIndexes; |
| 2493 |
} |
| 2494 |
|
| 2495 |
|
| 2496 |
|
| 2497 |
|
| 2498 |
|
| 2499 |
|
| 2500 |
|
| 2501 |
|
| 2502 |
void EmitDrawIndexes( mapDrawSurface_t *ds, bspDrawSurface_t *out ){ |
| 2503 |
int i; |
| 2504 |
|
| 2505 |
|
| 2506 |
|
| 2507 |
out->firstIndex = FindDrawIndexes( ds->numIndexes, ds->indexes ); |
| 2508 |
out->numIndexes = ds->numIndexes; |
| 2509 |
if ( out->firstIndex == numBSPDrawIndexes ) { |
| 2510 |
|
| 2511 |
for ( i = 0; i < ds->numIndexes; i++ ) |
| 2512 |
{ |
| 2513 |
AUTOEXPAND_BY_REALLOC_BSP( DrawIndexes, 1024 ); |
| 2514 |
bspDrawIndexes[ numBSPDrawIndexes ] = ds->indexes[ i ]; |
| 2515 |
|
| 2516 |
|
| 2517 |
if ( ds->type != SURFACE_PATCH ) { |
| 2518 |
if ( bspDrawIndexes[ numBSPDrawIndexes ] < 0 || bspDrawIndexes[ numBSPDrawIndexes ] >= ds->numVerts ) { |
| 2519 |
Sys_FPrintf( SYS_WRN, "WARNING: %d %s has invalid index %d (%d)\n", |
| 2520 |
numBSPDrawSurfaces, |
| 2521 |
ds->shaderInfo->shader, |
| 2522 |
bspDrawIndexes[ numBSPDrawIndexes ], |
| 2523 |
i ); |
| 2524 |
bspDrawIndexes[ numBSPDrawIndexes ] = 0; |
| 2525 |
} |
| 2526 |
} |
| 2527 |
|
| 2528 |
|
| 2529 |
numBSPDrawIndexes++; |
| 2530 |
} |
| 2531 |
} |
| 2532 |
} |
| 2533 |
|
| 2534 |
|
| 2535 |
|
| 2536 |
|
| 2537 |
|
| 2538 |
|
| 2539 |
|
| 2540 |
|
| 2541 |
|
| 2542 |
void EmitFlareSurface( mapDrawSurface_t *ds ){ |
| 2543 |
int i; |
| 2544 |
bspDrawSurface_t *out; |
| 2545 |
|
| 2546 |
|
| 2547 |
|
| 2548 |
if ( emitFlares == qfalse && ds->type != SURFACE_SHADER ) { |
| 2549 |
return; |
| 2550 |
} |
| 2551 |
|
| 2552 |
|
| 2553 |
if ( numBSPDrawSurfaces == MAX_MAP_DRAW_SURFS ) { |
| 2554 |
Error( "MAX_MAP_DRAW_SURFS" ); |
| 2555 |
} |
| 2556 |
|
| 2557 |
|
| 2558 |
if ( numBSPDrawSurfaces == MAX_MAP_DRAW_SURFS ) { |
| 2559 |
Error( "MAX_MAP_DRAW_SURFS" ); |
| 2560 |
} |
| 2561 |
out = &bspDrawSurfaces[ numBSPDrawSurfaces ]; |
| 2562 |
ds->outputNum = numBSPDrawSurfaces; |
| 2563 |
numBSPDrawSurfaces++; |
| 2564 |
memset( out, 0, sizeof( *out ) ); |
| 2565 |
|
| 2566 |
|
| 2567 |
out->surfaceType = MST_FLARE; |
| 2568 |
out->shaderNum = EmitShader( ds->shaderInfo->shader, &ds->shaderInfo->contentFlags, &ds->shaderInfo->surfaceFlags ); |
| 2569 |
out->fogNum = ds->fogNum; |
| 2570 |
|
| 2571 |
|
| 2572 |
for ( i = 0; i < MAX_LIGHTMAPS; i++ ) |
| 2573 |
{ |
| 2574 |
out->lightmapNum[ i ] = -3; |
| 2575 |
out->lightmapStyles[ i ] = LS_NONE; |
| 2576 |
out->vertexStyles[ i ] = LS_NONE; |
| 2577 |
} |
| 2578 |
out->lightmapStyles[ 0 ] = ds->lightStyle; |
| 2579 |
out->vertexStyles[ 0 ] = ds->lightStyle; |
| 2580 |
|
| 2581 |
VectorCopy( ds->lightmapOrigin, out->lightmapOrigin ); |
| 2582 |
VectorCopy( ds->lightmapVecs[ 0 ], out->lightmapVecs[ 0 ] ); |
| 2583 |
VectorCopy( ds->lightmapVecs[ 1 ], out->lightmapVecs[ 1 ] ); |
| 2584 |
VectorCopy( ds->lightmapVecs[ 2 ], out->lightmapVecs[ 2 ] ); |
| 2585 |
|
| 2586 |
|
| 2587 |
numSurfacesByType[ ds->type ]++; |
| 2588 |
} |
| 2589 |
|
| 2590 |
|
| 2591 |
|
| 2592 |
|
| 2593 |
|
| 2594 |
|
| 2595 |
|
| 2596 |
|
| 2597 |
void EmitPatchSurface( entity_t *e, mapDrawSurface_t *ds ){ |
| 2598 |
int i, j; |
| 2599 |
bspDrawSurface_t *out; |
| 2600 |
int surfaceFlags, contentFlags; |
| 2601 |
int forcePatchMeta; |
| 2602 |
|
| 2603 |
|
| 2604 |
forcePatchMeta = IntForKey( e, "_patchMeta" ); |
| 2605 |
if ( !forcePatchMeta ) { |
| 2606 |
forcePatchMeta = IntForKey( e, "patchMeta" ); |
| 2607 |
} |
| 2608 |
|
| 2609 |
|
| 2610 |
if ( ds->backSide || ds->shaderInfo->invert ) { |
| 2611 |
bspDrawVert_t *dv1, *dv2, temp; |
| 2612 |
|
| 2613 |
|
| 2614 |
for ( i = 0; i < ds->numVerts; i++ ) |
| 2615 |
VectorScale( ds->verts[ i ].normal, -1.0f, ds->verts[ i ].normal ); |
| 2616 |
|
| 2617 |
|
| 2618 |
for ( j = 0; j < ds->patchHeight; j++ ) |
| 2619 |
{ |
| 2620 |
for ( i = 0; i < ( ds->patchWidth / 2 ); i++ ) |
| 2621 |
{ |
| 2622 |
dv1 = &ds->verts[ j * ds->patchWidth + i ]; |
| 2623 |
dv2 = &ds->verts[ j * ds->patchWidth + ( ds->patchWidth - i - 1 ) ]; |
| 2624 |
memcpy( &temp, dv1, sizeof( bspDrawVert_t ) ); |
| 2625 |
memcpy( dv1, dv2, sizeof( bspDrawVert_t ) ); |
| 2626 |
memcpy( dv2, &temp, sizeof( bspDrawVert_t ) ); |
| 2627 |
} |
| 2628 |
} |
| 2629 |
|
| 2630 |
|
| 2631 |
VectorScale( ds->lightmapVecs[ 2 ], -1.0f, ds->lightmapVecs[ 2 ] ); |
| 2632 |
} |
| 2633 |
|
| 2634 |
|
| 2635 |
if ( numBSPDrawSurfaces == MAX_MAP_DRAW_SURFS ) { |
| 2636 |
Error( "MAX_MAP_DRAW_SURFS" ); |
| 2637 |
} |
| 2638 |
out = &bspDrawSurfaces[ numBSPDrawSurfaces ]; |
| 2639 |
ds->outputNum = numBSPDrawSurfaces; |
| 2640 |
numBSPDrawSurfaces++; |
| 2641 |
memset( out, 0, sizeof( *out ) ); |
| 2642 |
|
| 2643 |
|
| 2644 |
out->surfaceType = MST_PATCH; |
| 2645 |
if ( debugSurfaces ) { |
| 2646 |
out->shaderNum = EmitShader( "debugsurfaces", NULL, NULL ); |
| 2647 |
} |
| 2648 |
else if ( patchMeta || forcePatchMeta ) { |
| 2649 |
|
| 2650 |
surfaceFlags = ds->shaderInfo->surfaceFlags; |
| 2651 |
contentFlags = ds->shaderInfo->contentFlags; |
| 2652 |
ApplySurfaceParm( "nodraw", &contentFlags, &surfaceFlags, NULL ); |
| 2653 |
ApplySurfaceParm( "pointlight", &contentFlags, &surfaceFlags, NULL ); |
| 2654 |
|
| 2655 |
|
| 2656 |
VectorClear( ds->lightmapVecs[ 2 ] ); |
| 2657 |
VectorClear( ds->lightmapAxis ); |
| 2658 |
ds->sampleSize = 0; |
| 2659 |
|
| 2660 |
|
| 2661 |
out->shaderNum = EmitShader( ds->shaderInfo->shader, &contentFlags, &surfaceFlags ); |
| 2662 |
} |
| 2663 |
else{ |
| 2664 |
out->shaderNum = EmitShader( ds->shaderInfo->shader, &ds->shaderInfo->contentFlags, &ds->shaderInfo->surfaceFlags ); |
| 2665 |
} |
| 2666 |
out->patchWidth = ds->patchWidth; |
| 2667 |
out->patchHeight = ds->patchHeight; |
| 2668 |
out->fogNum = ds->fogNum; |
| 2669 |
|
| 2670 |
|
| 2671 |
for ( i = 0; i < MAX_LIGHTMAPS; i++ ) |
| 2672 |
{ |
| 2673 |
out->lightmapNum[ i ] = -3; |
| 2674 |
out->lightmapStyles[ i ] = LS_NONE; |
| 2675 |
out->vertexStyles[ i ] = LS_NONE; |
| 2676 |
} |
| 2677 |
out->lightmapStyles[ 0 ] = LS_NORMAL; |
| 2678 |
out->vertexStyles[ 0 ] = LS_NORMAL; |
| 2679 |
|
| 2680 |
|
| 2681 |
VectorCopy( ds->lightmapOrigin, out->lightmapOrigin ); |
| 2682 |
VectorCopy( ds->bounds[ 0 ], out->lightmapVecs[ 0 ] ); |
| 2683 |
VectorCopy( ds->bounds[ 1 ], out->lightmapVecs[ 1 ] ); |
| 2684 |
VectorCopy( ds->lightmapVecs[ 2 ], out->lightmapVecs[ 2 ] ); |
| 2685 |
|
| 2686 |
|
| 2687 |
if ( ds->planar == qfalse ) { |
| 2688 |
VectorClear( out->lightmapVecs[ 2 ] ); |
| 2689 |
} |
| 2690 |
|
| 2691 |
|
| 2692 |
EmitDrawVerts( ds, out ); |
| 2693 |
EmitDrawIndexes( ds, out ); |
| 2694 |
|
| 2695 |
|
| 2696 |
numSurfacesByType[ ds->type ]++; |
| 2697 |
} |
| 2698 |
|
| 2699 |
|
| 2700 |
|
| 2701 |
|
| 2702 |
|
| 2703 |
|
| 2704 |
|
| 2705 |
|
| 2706 |
#define VERTEX_CACHE_SIZE 16 |
| 2707 |
|
| 2708 |
static void OptimizeTriangleSurface( mapDrawSurface_t *ds ){ |
| 2709 |
int i, j, k, temp, first, best, bestScore, score; |
| 2710 |
int vertexCache[ VERTEX_CACHE_SIZE + 1 ]; |
| 2711 |
int *indexes; |
| 2712 |
|
| 2713 |
|
| 2714 |
|
| 2715 |
if ( ds->numIndexes <= VERTEX_CACHE_SIZE || |
| 2716 |
ds->shaderInfo->autosprite ) { |
| 2717 |
return; |
| 2718 |
} |
| 2719 |
|
| 2720 |
|
| 2721 |
indexes = safe_malloc( ds->numIndexes * sizeof( *indexes ) ); |
| 2722 |
memcpy( indexes, ds->indexes, ds->numIndexes * sizeof( *indexes ) ); |
| 2723 |
|
| 2724 |
|
| 2725 |
for ( i = 0; i <= VERTEX_CACHE_SIZE && i < ds->numIndexes; i++ ) |
| 2726 |
vertexCache[ i ] = indexes[ i ]; |
| 2727 |
|
| 2728 |
|
| 2729 |
for ( i = 0; i < ds->numIndexes; i += 3 ) |
| 2730 |
{ |
| 2731 |
|
| 2732 |
first = -1; |
| 2733 |
best = -1; |
| 2734 |
bestScore = -1; |
| 2735 |
for ( j = 0; j < ds->numIndexes; j += 3 ) |
| 2736 |
{ |
| 2737 |
|
| 2738 |
if ( indexes[ j ] != -1 ) { |
| 2739 |
|
| 2740 |
if ( first < 0 ) { |
| 2741 |
first = j; |
| 2742 |
} |
| 2743 |
|
| 2744 |
|
| 2745 |
score = 0; |
| 2746 |
for ( k = 0; k < VERTEX_CACHE_SIZE; k++ ) |
| 2747 |
{ |
| 2748 |
if ( indexes[ j ] == vertexCache[ k ] || indexes[ j + 1 ] == vertexCache[ k ] || indexes[ j + 2 ] == vertexCache[ k ] ) { |
| 2749 |
score++; |
| 2750 |
} |
| 2751 |
} |
| 2752 |
|
| 2753 |
|
| 2754 |
if ( score > bestScore ) { |
| 2755 |
bestScore = score; |
| 2756 |
best = j; |
| 2757 |
} |
| 2758 |
|
| 2759 |
|
| 2760 |
if ( score == 3 ) { |
| 2761 |
break; |
| 2762 |
} |
| 2763 |
} |
| 2764 |
} |
| 2765 |
|
| 2766 |
|
| 2767 |
if ( best < 0 ) { |
| 2768 |
best = first; |
| 2769 |
} |
| 2770 |
|
| 2771 |
|
| 2772 |
if ( best >= 0 ) { |
| 2773 |
|
| 2774 |
for ( j = 0; j < 3; j++ ) |
| 2775 |
{ |
| 2776 |
for ( k = 0; k < VERTEX_CACHE_SIZE; k++ ) |
| 2777 |
{ |
| 2778 |
if ( indexes[ best + j ] == vertexCache[ k ] ) { |
| 2779 |
break; |
| 2780 |
} |
| 2781 |
} |
| 2782 |
|
| 2783 |
if ( k >= VERTEX_CACHE_SIZE ) { |
| 2784 |
|
| 2785 |
for ( k = VERTEX_CACHE_SIZE; k > 0; k-- ) |
| 2786 |
vertexCache[ k ] = vertexCache[ k - 1 ]; |
| 2787 |
|
| 2788 |
|
| 2789 |
vertexCache[ 0 ] = indexes[ best + j ]; |
| 2790 |
} |
| 2791 |
} |
| 2792 |
|
| 2793 |
|
| 2794 |
ds->indexes[ i ] = indexes[ best ]; |
| 2795 |
ds->indexes[ i + 1 ] = indexes[ best + 1 ]; |
| 2796 |
ds->indexes[ i + 2 ] = indexes[ best + 2 ]; |
| 2797 |
|
| 2798 |
|
| 2799 |
indexes[ best ] = -1; |
| 2800 |
indexes[ best + 1 ] = -1; |
| 2801 |
indexes[ best + 2 ] = -1; |
| 2802 |
|
| 2803 |
|
| 2804 |
while ( ds->indexes[ i ] > ds->indexes[ i + 1 ] || ds->indexes[ i ] > ds->indexes[ i + 2 ] ) |
| 2805 |
{ |
| 2806 |
temp = ds->indexes[ i ]; |
| 2807 |
ds->indexes[ i ] = ds->indexes[ i + 1 ]; |
| 2808 |
ds->indexes[ i + 1 ] = ds->indexes[ i + 2 ]; |
| 2809 |
ds->indexes[ i + 2 ] = temp; |
| 2810 |
} |
| 2811 |
} |
| 2812 |
} |
| 2813 |
|
| 2814 |
|
| 2815 |
free( indexes ); |
| 2816 |
} |
| 2817 |
|
| 2818 |
|
| 2819 |
|
| 2820 |
|
| 2821 |
|
| 2822 |
|
| 2823 |
|
| 2824 |
|
| 2825 |
void EmitTriangleSurface( mapDrawSurface_t *ds ){ |
| 2826 |
int i, temp; |
| 2827 |
bspDrawSurface_t *out; |
| 2828 |
|
| 2829 |
|
| 2830 |
if ( ds->backSide || ds->shaderInfo->invert ) { |
| 2831 |
|
| 2832 |
for ( i = 0; i < ds->numIndexes; i += 3 ) |
| 2833 |
{ |
| 2834 |
temp = ds->indexes[ i ]; |
| 2835 |
ds->indexes[ i ] = ds->indexes[ i + 1 ]; |
| 2836 |
ds->indexes[ i + 1 ] = temp; |
| 2837 |
} |
| 2838 |
|
| 2839 |
|
| 2840 |
for ( i = 0; i < ds->numVerts; i++ ) |
| 2841 |
VectorScale( ds->verts[ i ].normal, -1.0f, ds->verts[ i ].normal ); |
| 2842 |
|
| 2843 |
|
| 2844 |
VectorScale( ds->lightmapVecs[ 2 ], -1.0f, ds->lightmapVecs[ 2 ] ); |
| 2845 |
} |
| 2846 |
|
| 2847 |
|
| 2848 |
if ( numBSPDrawSurfaces == MAX_MAP_DRAW_SURFS ) { |
| 2849 |
Error( "MAX_MAP_DRAW_SURFS" ); |
| 2850 |
} |
| 2851 |
out = &bspDrawSurfaces[ numBSPDrawSurfaces ]; |
| 2852 |
ds->outputNum = numBSPDrawSurfaces; |
| 2853 |
numBSPDrawSurfaces++; |
| 2854 |
memset( out, 0, sizeof( *out ) ); |
| 2855 |
|
| 2856 |
|
| 2857 |
if ( ds->type == SURFACE_FOLIAGE ) { |
| 2858 |
out->surfaceType = MST_FOLIAGE; |
| 2859 |
} |
| 2860 |
|
| 2861 |
|
| 2862 |
|
| 2863 |
else if ( ( VectorLength( ds->lightmapAxis ) <= 0.0f && ds->planar == qfalse ) || |
| 2864 |
ds->type == SURFACE_TRIANGLES || |
| 2865 |
ds->type == SURFACE_FOGHULL || |
| 2866 |
ds->numVerts > maxLMSurfaceVerts || |
| 2867 |
debugSurfaces ) { |
| 2868 |
out->surfaceType = MST_TRIANGLE_SOUP; |
| 2869 |
} |
| 2870 |
|
| 2871 |
|
| 2872 |
else{ |
| 2873 |
out->surfaceType = MST_PLANAR; |
| 2874 |
} |
| 2875 |
|
| 2876 |
|
| 2877 |
if ( debugSurfaces ) { |
| 2878 |
out->shaderNum = EmitShader( "debugsurfaces", NULL, NULL ); |
| 2879 |
} |
| 2880 |
else{ |
| 2881 |
out->shaderNum = EmitShader( ds->shaderInfo->shader, &ds->shaderInfo->contentFlags, &ds->shaderInfo->surfaceFlags ); |
| 2882 |
} |
| 2883 |
out->patchWidth = ds->patchWidth; |
| 2884 |
out->patchHeight = ds->patchHeight; |
| 2885 |
out->fogNum = ds->fogNum; |
| 2886 |
|
| 2887 |
|
| 2888 |
if ( debugInset ) { |
| 2889 |
bspDrawVert_t *a, *b, *c; |
| 2890 |
vec3_t cent, dir; |
| 2891 |
|
| 2892 |
|
| 2893 |
|
| 2894 |
for ( i = 0; i < ds->numIndexes; i += 3 ) |
| 2895 |
{ |
| 2896 |
|
| 2897 |
a = &ds->verts[ ds->indexes[ i ] ]; |
| 2898 |
b = &ds->verts[ ds->indexes[ i + 1 ] ]; |
| 2899 |
c = &ds->verts[ ds->indexes[ i + 2 ] ]; |
| 2900 |
|
| 2901 |
|
| 2902 |
VectorCopy( a->xyz, cent ); |
| 2903 |
VectorAdd( cent, b->xyz, cent ); |
| 2904 |
VectorAdd( cent, c->xyz, cent ); |
| 2905 |
VectorScale( cent, 1.0f / 3.0f, cent ); |
| 2906 |
|
| 2907 |
|
| 2908 |
VectorSubtract( cent, a->xyz, dir ); |
| 2909 |
VectorNormalize( dir, dir ); |
| 2910 |
VectorAdd( a->xyz, dir, a->xyz ); |
| 2911 |
VectorSubtract( cent, b->xyz, dir ); |
| 2912 |
VectorNormalize( dir, dir ); |
| 2913 |
VectorAdd( b->xyz, dir, b->xyz ); |
| 2914 |
VectorSubtract( cent, c->xyz, dir ); |
| 2915 |
VectorNormalize( dir, dir ); |
| 2916 |
VectorAdd( c->xyz, dir, c->xyz ); |
| 2917 |
} |
| 2918 |
} |
| 2919 |
|
| 2920 |
|
| 2921 |
for ( i = 0; i < MAX_LIGHTMAPS; i++ ) |
| 2922 |
{ |
| 2923 |
out->lightmapNum[ i ] = -3; |
| 2924 |
out->lightmapStyles[ i ] = LS_NONE; |
| 2925 |
out->vertexStyles[ i ] = LS_NONE; |
| 2926 |
} |
| 2927 |
out->lightmapStyles[ 0 ] = LS_NORMAL; |
| 2928 |
out->vertexStyles[ 0 ] = LS_NORMAL; |
| 2929 |
|
| 2930 |
|
| 2931 |
VectorCopy( ds->lightmapOrigin, out->lightmapOrigin ); |
| 2932 |
VectorCopy( ds->lightmapVecs[ 0 ], out->lightmapVecs[ 0 ] ); |
| 2933 |
VectorCopy( ds->lightmapVecs[ 1 ], out->lightmapVecs[ 1 ] ); |
| 2934 |
VectorCopy( ds->lightmapVecs[ 2 ], out->lightmapVecs[ 2 ] ); |
| 2935 |
|
| 2936 |
|
| 2937 |
if ( ds->planar == qfalse ) { |
| 2938 |
VectorClear( out->lightmapVecs[ 2 ] ); |
| 2939 |
} |
| 2940 |
|
| 2941 |
|
| 2942 |
OptimizeTriangleSurface( ds ); |
| 2943 |
|
| 2944 |
|
| 2945 |
EmitDrawVerts( ds, out ); |
| 2946 |
EmitDrawIndexes( ds, out ); |
| 2947 |
|
| 2948 |
|
| 2949 |
numSurfacesByType[ ds->type ]++; |
| 2950 |
} |
| 2951 |
|
| 2952 |
|
| 2953 |
|
| 2954 |
|
| 2955 |
|
| 2956 |
|
| 2957 |
|
| 2958 |
|
| 2959 |
static void EmitFaceSurface( mapDrawSurface_t *ds ){ |
| 2960 |
|
| 2961 |
if ( maxAreaFaceSurface ) { |
| 2962 |
MaxAreaFaceSurface( ds ); |
| 2963 |
} |
| 2964 |
else{ |
| 2965 |
StripFaceSurface( ds ); |
| 2966 |
} |
| 2967 |
EmitTriangleSurface( ds ); |
| 2968 |
} |
| 2969 |
|
| 2970 |
|
| 2971 |
|
| 2972 |
|
| 2973 |
|
| 2974 |
|
| 2975 |
|
| 2976 |
|
| 2977 |
static void MakeDebugPortalSurfs_r( node_t *node, shaderInfo_t *si ){ |
| 2978 |
int i, k, c, s; |
| 2979 |
portal_t *p; |
| 2980 |
winding_t *w; |
| 2981 |
mapDrawSurface_t *ds; |
| 2982 |
bspDrawVert_t *dv; |
| 2983 |
|
| 2984 |
|
| 2985 |
|
| 2986 |
if ( node->planenum != PLANENUM_LEAF ) { |
| 2987 |
MakeDebugPortalSurfs_r( node->children[ 0 ], si ); |
| 2988 |
MakeDebugPortalSurfs_r( node->children[ 1 ], si ); |
| 2989 |
return; |
| 2990 |
} |
| 2991 |
|
| 2992 |
|
| 2993 |
if ( node->opaque ) { |
| 2994 |
return; |
| 2995 |
} |
| 2996 |
|
| 2997 |
|
| 2998 |
for ( c = 0, p = node->portals; p != NULL; c++, p = p->next[ s ] ) |
| 2999 |
{ |
| 3000 |
|
| 3001 |
w = p->winding; |
| 3002 |
s = ( p->nodes[ 1 ] == node ); |
| 3003 |
|
| 3004 |
|
| 3005 |
if ( w && p->nodes[ 0 ] == node ) { |
| 3006 |
|
| 3007 |
if ( PortalPassable( p ) == qfalse ) { |
| 3008 |
continue; |
| 3009 |
} |
| 3010 |
|
| 3011 |
|
| 3012 |
if ( w->numpoints > 64 ) { |
| 3013 |
Error( "MakePortalSurfs_r: w->numpoints = %d", w->numpoints ); |
| 3014 |
} |
| 3015 |
|
| 3016 |
|
| 3017 |
ds = AllocDrawSurface( SURFACE_FACE ); |
| 3018 |
ds->shaderInfo = si; |
| 3019 |
ds->planar = qtrue; |
| 3020 |
ds->sideRef = AllocSideRef( p->side, NULL ); |
| 3021 |
ds->planeNum = FindFloatPlane( p->plane.normal, p->plane.dist, 0, NULL ); |
| 3022 |
VectorCopy( p->plane.normal, ds->lightmapVecs[ 2 ] ); |
| 3023 |
ds->fogNum = -1; |
| 3024 |
ds->numVerts = w->numpoints; |
| 3025 |
ds->verts = safe_malloc0( ds->numVerts * sizeof( *ds->verts ) ); |
| 3026 |
|
| 3027 |
|
| 3028 |
for ( i = 0; i < ds->numVerts; i++ ) |
| 3029 |
{ |
| 3030 |
|
| 3031 |
dv = ds->verts + i; |
| 3032 |
|
| 3033 |
|
| 3034 |
VectorCopy( w->p[ i ], dv->xyz ); |
| 3035 |
VectorCopy( p->plane.normal, dv->normal ); |
| 3036 |
dv->st[ 0 ] = 0; |
| 3037 |
dv->st[ 1 ] = 0; |
| 3038 |
for ( k = 0; k < MAX_LIGHTMAPS; k++ ) |
| 3039 |
{ |
| 3040 |
VectorCopy( debugColors[ c % 12 ], dv->color[ k ] ); |
| 3041 |
dv->color[ k ][ 3 ] = 32; |
| 3042 |
} |
| 3043 |
} |
| 3044 |
} |
| 3045 |
} |
| 3046 |
} |
| 3047 |
|
| 3048 |
|
| 3049 |
|
| 3050 |
|
| 3051 |
|
| 3052 |
|
| 3053 |
|
| 3054 |
|
| 3055 |
void MakeDebugPortalSurfs( tree_t *tree ){ |
| 3056 |
shaderInfo_t *si; |
| 3057 |
|
| 3058 |
|
| 3059 |
|
| 3060 |
Sys_FPrintf( SYS_VRB, "--- MakeDebugPortalSurfs ---\n" ); |
| 3061 |
|
| 3062 |
|
| 3063 |
si = ShaderInfoForShader( "debugportals" ); |
| 3064 |
|
| 3065 |
|
| 3066 |
MakeDebugPortalSurfs_r( tree->headnode, si ); |
| 3067 |
} |
| 3068 |
|
| 3069 |
|
| 3070 |
|
| 3071 |
|
| 3072 |
|
| 3073 |
|
| 3074 |
|
| 3075 |
|
| 3076 |
void MakeFogHullSurfs( entity_t *e, tree_t *tree, char *shader ){ |
| 3077 |
shaderInfo_t *si; |
| 3078 |
mapDrawSurface_t *ds; |
| 3079 |
vec3_t fogMins, fogMaxs; |
| 3080 |
int i, indexes[] = |
| 3081 |
{ |
| 3082 |
0, 1, 2, 0, 2, 3, |
| 3083 |
4, 7, 5, 5, 7, 6, |
| 3084 |
1, 5, 6, 1, 6, 2, |
| 3085 |
0, 4, 5, 0, 5, 1, |
| 3086 |
2, 6, 7, 2, 7, 3, |
| 3087 |
3, 7, 4, 3, 4, 0 |
| 3088 |
}; |
| 3089 |
|
| 3090 |
|
| 3091 |
|
| 3092 |
if ( shader == NULL || shader[ 0 ] == '\0' ) { |
| 3093 |
return; |
| 3094 |
} |
| 3095 |
|
| 3096 |
|
| 3097 |
Sys_FPrintf( SYS_VRB, "--- MakeFogHullSurfs ---\n" ); |
| 3098 |
|
| 3099 |
|
| 3100 |
VectorCopy( mapMins, fogMins ); |
| 3101 |
VectorCopy( mapMaxs, fogMaxs ); |
| 3102 |
for ( i = 0; i < 3; i++ ) |
| 3103 |
{ |
| 3104 |
fogMins[ i ] -= 128; |
| 3105 |
fogMaxs[ i ] += 128; |
| 3106 |
} |
| 3107 |
|
| 3108 |
|
| 3109 |
si = ShaderInfoForShader( shader ); |
| 3110 |
|
| 3111 |
|
| 3112 |
ds = AllocDrawSurface( SURFACE_FOGHULL ); |
| 3113 |
ds->shaderInfo = si; |
| 3114 |
ds->fogNum = -1; |
| 3115 |
ds->numVerts = 8; |
| 3116 |
ds->verts = safe_malloc0( ds->numVerts * sizeof( *ds->verts ) ); |
| 3117 |
ds->numIndexes = 36; |
| 3118 |
ds->indexes = safe_malloc0( ds->numIndexes * sizeof( *ds->indexes ) ); |
| 3119 |
|
| 3120 |
|
| 3121 |
VectorSet( ds->verts[ 0 ].xyz, fogMins[ 0 ], fogMins[ 1 ], fogMins[ 2 ] ); |
| 3122 |
VectorSet( ds->verts[ 1 ].xyz, fogMins[ 0 ], fogMaxs[ 1 ], fogMins[ 2 ] ); |
| 3123 |
VectorSet( ds->verts[ 2 ].xyz, fogMaxs[ 0 ], fogMaxs[ 1 ], fogMins[ 2 ] ); |
| 3124 |
VectorSet( ds->verts[ 3 ].xyz, fogMaxs[ 0 ], fogMins[ 1 ], fogMins[ 2 ] ); |
| 3125 |
|
| 3126 |
VectorSet( ds->verts[ 4 ].xyz, fogMins[ 0 ], fogMins[ 1 ], fogMaxs[ 2 ] ); |
| 3127 |
VectorSet( ds->verts[ 5 ].xyz, fogMins[ 0 ], fogMaxs[ 1 ], fogMaxs[ 2 ] ); |
| 3128 |
VectorSet( ds->verts[ 6 ].xyz, fogMaxs[ 0 ], fogMaxs[ 1 ], fogMaxs[ 2 ] ); |
| 3129 |
VectorSet( ds->verts[ 7 ].xyz, fogMaxs[ 0 ], fogMins[ 1 ], fogMaxs[ 2 ] ); |
| 3130 |
|
| 3131 |
|
| 3132 |
memcpy( ds->indexes, indexes, ds->numIndexes * sizeof( *ds->indexes ) ); |
| 3133 |
} |
| 3134 |
|
| 3135 |
|
| 3136 |
|
| 3137 |
|
| 3138 |
|
| 3139 |
|
| 3140 |
|
| 3141 |
|
| 3142 |
void BiasSurfaceTextures( mapDrawSurface_t *ds ){ |
| 3143 |
int i; |
| 3144 |
|
| 3145 |
|
| 3146 |
|
| 3147 |
CalcSurfaceTextureRange( ds ); |
| 3148 |
|
| 3149 |
|
| 3150 |
if ( ds->shaderInfo->globalTexture ) { |
| 3151 |
return; |
| 3152 |
} |
| 3153 |
|
| 3154 |
|
| 3155 |
for ( i = 0; i < ds->numVerts; i++ ) |
| 3156 |
{ |
| 3157 |
ds->verts[ i ].st[ 0 ] += ds->bias[ 0 ]; |
| 3158 |
ds->verts[ i ].st[ 1 ] += ds->bias[ 1 ]; |
| 3159 |
} |
| 3160 |
} |
| 3161 |
|
| 3162 |
|
| 3163 |
|
| 3164 |
|
| 3165 |
|
| 3166 |
|
| 3167 |
|
| 3168 |
|
| 3169 |
int AddSurfaceModelsToTriangle_r( mapDrawSurface_t *ds, surfaceModel_t *model, bspDrawVert_t **tri ){ |
| 3170 |
bspDrawVert_t mid, *tri2[ 3 ]; |
| 3171 |
int max, n, localNumSurfaceModels; |
| 3172 |
|
| 3173 |
|
| 3174 |
|
| 3175 |
localNumSurfaceModels = 0; |
| 3176 |
|
| 3177 |
|
| 3178 |
{ |
| 3179 |
int i; |
| 3180 |
float *a, *b, dx, dy, dz, dist, maxDist; |
| 3181 |
|
| 3182 |
|
| 3183 |
|
| 3184 |
max = -1; |
| 3185 |
maxDist = 0.0f; |
| 3186 |
for ( i = 0; i < 3; i++ ) |
| 3187 |
{ |
| 3188 |
|
| 3189 |
a = tri[ i ]->xyz; |
| 3190 |
b = tri[ ( i + 1 ) % 3 ]->xyz; |
| 3191 |
|
| 3192 |
|
| 3193 |
dx = a[ 0 ] - b[ 0 ]; |
| 3194 |
dy = a[ 1 ] - b[ 1 ]; |
| 3195 |
dz = a[ 2 ] - b[ 2 ]; |
| 3196 |
dist = ( dx * dx ) + ( dy * dy ) + ( dz * dz ); |
| 3197 |
|
| 3198 |
|
| 3199 |
if ( dist > maxDist ) { |
| 3200 |
maxDist = dist; |
| 3201 |
max = i; |
| 3202 |
} |
| 3203 |
} |
| 3204 |
|
| 3205 |
|
| 3206 |
if ( max < 0 || maxDist <= ( model->density * model->density ) ) { |
| 3207 |
float odds, r, angle; |
| 3208 |
vec3_t origin, normal, scale, axis[ 3 ], angles; |
| 3209 |
m4x4_t transform, temp; |
| 3210 |
|
| 3211 |
|
| 3212 |
|
| 3213 |
odds = model->odds * ( tri[ 0 ]->color[ 0 ][ 3 ] + tri[ 0 ]->color[ 0 ][ 3 ] + tri[ 0 ]->color[ 0 ][ 3 ] ) / 765.0f; |
| 3214 |
r = Random(); |
| 3215 |
if ( r > odds ) { |
| 3216 |
return 0; |
| 3217 |
} |
| 3218 |
|
| 3219 |
|
| 3220 |
r = model->minScale + Random() * ( model->maxScale - model->minScale ); |
| 3221 |
VectorSet( scale, r, r, r ); |
| 3222 |
|
| 3223 |
|
| 3224 |
angle = model->minAngle + Random() * ( model->maxAngle - model->minAngle ); |
| 3225 |
|
| 3226 |
|
| 3227 |
VectorCopy( tri[ 0 ]->xyz, origin ); |
| 3228 |
VectorAdd( origin, tri[ 1 ]->xyz, origin ); |
| 3229 |
VectorAdd( origin, tri[ 2 ]->xyz, origin ); |
| 3230 |
VectorScale( origin, ( 1.0f / 3.0f ), origin ); |
| 3231 |
|
| 3232 |
|
| 3233 |
m4x4_identity( transform ); |
| 3234 |
|
| 3235 |
|
| 3236 |
if ( model->oriented ) { |
| 3237 |
|
| 3238 |
VectorSet( angles, 0.0f, 0.0f, angle ); |
| 3239 |
|
| 3240 |
|
| 3241 |
VectorCopy( tri[ 0 ]->normal, normal ); |
| 3242 |
VectorAdd( normal, tri[ 1 ]->normal, normal ); |
| 3243 |
VectorAdd( normal, tri[ 2 ]->normal, normal ); |
| 3244 |
if ( VectorNormalize( normal, axis[ 2 ] ) == 0.0f ) { |
| 3245 |
VectorCopy( tri[ 0 ]->normal, axis[ 2 ] ); |
| 3246 |
} |
| 3247 |
|
| 3248 |
|
| 3249 |
MakeNormalVectors( axis[ 2 ], axis[ 1 ], axis[ 0 ] ); |
| 3250 |
|
| 3251 |
|
| 3252 |
m4x4_identity( temp ); |
| 3253 |
temp[ 0 ] = axis[ 0 ][ 0 ]; temp[ 1 ] = axis[ 0 ][ 1 ]; temp[ 2 ] = axis[ 0 ][ 2 ]; |
| 3254 |
temp[ 4 ] = axis[ 1 ][ 0 ]; temp[ 5 ] = axis[ 1 ][ 1 ]; temp[ 6 ] = axis[ 1 ][ 2 ]; |
| 3255 |
temp[ 8 ] = axis[ 2 ][ 0 ]; temp[ 9 ] = axis[ 2 ][ 1 ]; temp[ 10 ] = axis[ 2 ][ 2 ]; |
| 3256 |
|
| 3257 |
|
| 3258 |
m4x4_scale_by_vec3( temp, scale ); |
| 3259 |
|
| 3260 |
|
| 3261 |
m4x4_rotate_by_vec3( temp, angles, eXYZ ); |
| 3262 |
|
| 3263 |
|
| 3264 |
m4x4_translate_by_vec3( transform, origin ); |
| 3265 |
|
| 3266 |
|
| 3267 |
m4x4_multiply_by_m4x4( transform, temp ); |
| 3268 |
} |
| 3269 |
|
| 3270 |
|
| 3271 |
else |
| 3272 |
{ |
| 3273 |
|
| 3274 |
VectorSet( angles, 0.0f, 0.0f, angle ); |
| 3275 |
|
| 3276 |
|
| 3277 |
m4x4_pivoted_transform_by_vec3( transform, origin, angles, eXYZ, scale, vec3_origin ); |
| 3278 |
} |
| 3279 |
|
| 3280 |
|
| 3281 |
InsertModel( (char *) model->model, 0, 0, transform, NULL, ds->celShader, ds->entityNum, ds->castShadows, ds->recvShadows, 0, ds->lightmapScale, 0, 0, clipDepthGlobal ); |
| 3282 |
|
| 3283 |
|
| 3284 |
return 1; |
| 3285 |
} |
| 3286 |
} |
| 3287 |
|
| 3288 |
|
| 3289 |
LerpDrawVert( tri[ max ], tri[ ( max + 1 ) % 3 ], &mid ); |
| 3290 |
|
| 3291 |
|
| 3292 |
VectorCopy( tri, tri2 ); |
| 3293 |
tri2[ max ] = ∣ |
| 3294 |
n = AddSurfaceModelsToTriangle_r( ds, model, tri2 ); |
| 3295 |
if ( n < 0 ) { |
| 3296 |
return n; |
| 3297 |
} |
| 3298 |
localNumSurfaceModels += n; |
| 3299 |
|
| 3300 |
|
| 3301 |
VectorCopy( tri, tri2 ); |
| 3302 |
tri2[ ( max + 1 ) % 3 ] = ∣ |
| 3303 |
n = AddSurfaceModelsToTriangle_r( ds, model, tri2 ); |
| 3304 |
if ( n < 0 ) { |
| 3305 |
return n; |
| 3306 |
} |
| 3307 |
localNumSurfaceModels += n; |
| 3308 |
|
| 3309 |
|
| 3310 |
return localNumSurfaceModels; |
| 3311 |
} |
| 3312 |
|
| 3313 |
|
| 3314 |
|
| 3315 |
|
| 3316 |
|
| 3317 |
|
| 3318 |
|
| 3319 |
|
| 3320 |
int AddSurfaceModels( mapDrawSurface_t *ds ){ |
| 3321 |
surfaceModel_t *model; |
| 3322 |
int i, x, y, n, pw[ 5 ], r, localNumSurfaceModels, iterations; |
| 3323 |
mesh_t src, *mesh, *subdivided; |
| 3324 |
bspDrawVert_t centroid, *tri[ 3 ]; |
| 3325 |
float alpha; |
| 3326 |
|
| 3327 |
|
| 3328 |
|
| 3329 |
if ( ds == NULL || ds->shaderInfo == NULL || ds->shaderInfo->surfaceModel == NULL ) { |
| 3330 |
return 0; |
| 3331 |
} |
| 3332 |
|
| 3333 |
|
| 3334 |
localNumSurfaceModels = 0; |
| 3335 |
|
| 3336 |
|
| 3337 |
for ( model = ds->shaderInfo->surfaceModel; model != NULL; model = model->next ) |
| 3338 |
{ |
| 3339 |
|
| 3340 |
switch ( ds->type ) |
| 3341 |
{ |
| 3342 |
|
| 3343 |
case SURFACE_FACE: |
| 3344 |
case SURFACE_DECAL: |
| 3345 |
|
| 3346 |
memset( ¢roid, 0, sizeof( centroid ) ); |
| 3347 |
alpha = 0.0f; |
| 3348 |
|
| 3349 |
|
| 3350 |
for ( i = 0; i < ds->numVerts; i++ ) |
| 3351 |
{ |
| 3352 |
VectorAdd( centroid.xyz, ds->verts[ i ].xyz, centroid.xyz ); |
| 3353 |
VectorAdd( centroid.normal, ds->verts[ i ].normal, centroid.normal ); |
| 3354 |
centroid.st[ 0 ] += ds->verts[ i ].st[ 0 ]; |
| 3355 |
centroid.st[ 1 ] += ds->verts[ i ].st[ 1 ]; |
| 3356 |
alpha += ds->verts[ i ].color[ 0 ][ 3 ]; |
| 3357 |
} |
| 3358 |
|
| 3359 |
|
| 3360 |
centroid.xyz[ 0 ] /= ds->numVerts; |
| 3361 |
centroid.xyz[ 1 ] /= ds->numVerts; |
| 3362 |
centroid.xyz[ 2 ] /= ds->numVerts; |
| 3363 |
if ( VectorNormalize( centroid.normal, centroid.normal ) == 0.0f ) { |
| 3364 |
VectorCopy( ds->verts[ 0 ].normal, centroid.normal ); |
| 3365 |
} |
| 3366 |
centroid.st[ 0 ] /= ds->numVerts; |
| 3367 |
centroid.st[ 1 ] /= ds->numVerts; |
| 3368 |
alpha /= ds->numVerts; |
| 3369 |
centroid.color[ 0 ][ 0 ] = 0xFF; |
| 3370 |
centroid.color[ 0 ][ 1 ] = 0xFF; |
| 3371 |
|
| 3372 |
centroid.color[ 0 ][ 2 ] = ( alpha > 255.0f ? 0xFF : alpha ); |
| 3373 |
|
| 3374 |
|
| 3375 |
tri[ 0 ] = ¢roid; |
| 3376 |
|
| 3377 |
|
| 3378 |
for ( i = 0; i < ds->numVerts; i++ ) |
| 3379 |
{ |
| 3380 |
|
| 3381 |
tri[ 1 ] = &ds->verts[ i ]; |
| 3382 |
tri[ 2 ] = &ds->verts[ ( i + 1 ) % ds->numVerts ]; |
| 3383 |
|
| 3384 |
|
| 3385 |
n = AddSurfaceModelsToTriangle_r( ds, model, tri ); |
| 3386 |
if ( n < 0 ) { |
| 3387 |
return n; |
| 3388 |
} |
| 3389 |
localNumSurfaceModels += n; |
| 3390 |
} |
| 3391 |
break; |
| 3392 |
|
| 3393 |
|
| 3394 |
case SURFACE_PATCH: |
| 3395 |
|
| 3396 |
src.width = ds->patchWidth; |
| 3397 |
src.height = ds->patchHeight; |
| 3398 |
src.verts = ds->verts; |
| 3399 |
|
| 3400 |
iterations = IterationsForCurve( ds->longestCurve, patchSubdivisions ); |
| 3401 |
subdivided = SubdivideMesh2( src, iterations ); |
| 3402 |
|
| 3403 |
|
| 3404 |
PutMeshOnCurve( *subdivided ); |
| 3405 |
mesh = RemoveLinearMeshColumnsRows( subdivided ); |
| 3406 |
FreeMesh( subdivided ); |
| 3407 |
|
| 3408 |
|
| 3409 |
for ( y = 0; y < ( mesh->height - 1 ); y++ ) |
| 3410 |
{ |
| 3411 |
for ( x = 0; x < ( mesh->width - 1 ); x++ ) |
| 3412 |
{ |
| 3413 |
|
| 3414 |
pw[ 0 ] = x + ( y * mesh->width ); |
| 3415 |
pw[ 1 ] = x + ( ( y + 1 ) * mesh->width ); |
| 3416 |
pw[ 2 ] = x + 1 + ( ( y + 1 ) * mesh->width ); |
| 3417 |
pw[ 3 ] = x + 1 + ( y * mesh->width ); |
| 3418 |
pw[ 4 ] = x + ( y * mesh->width ); |
| 3419 |
|
| 3420 |
|
| 3421 |
r = ( x + y ) & 1; |
| 3422 |
|
| 3423 |
|
| 3424 |
tri[ 0 ] = &mesh->verts[ pw[ r + 0 ] ]; |
| 3425 |
tri[ 1 ] = &mesh->verts[ pw[ r + 1 ] ]; |
| 3426 |
tri[ 2 ] = &mesh->verts[ pw[ r + 2 ] ]; |
| 3427 |
n = AddSurfaceModelsToTriangle_r( ds, model, tri ); |
| 3428 |
if ( n < 0 ) { |
| 3429 |
return n; |
| 3430 |
} |
| 3431 |
localNumSurfaceModels += n; |
| 3432 |
|
| 3433 |
|
| 3434 |
tri[ 0 ] = &mesh->verts[ pw[ r + 0 ] ]; |
| 3435 |
tri[ 1 ] = &mesh->verts[ pw[ r + 2 ] ]; |
| 3436 |
tri[ 2 ] = &mesh->verts[ pw[ r + 3 ] ]; |
| 3437 |
n = AddSurfaceModelsToTriangle_r( ds, model, tri ); |
| 3438 |
if ( n < 0 ) { |
| 3439 |
return n; |
| 3440 |
} |
| 3441 |
localNumSurfaceModels += n; |
| 3442 |
} |
| 3443 |
} |
| 3444 |
|
| 3445 |
|
| 3446 |
FreeMesh( mesh ); |
| 3447 |
break; |
| 3448 |
|
| 3449 |
|
| 3450 |
case SURFACE_TRIANGLES: |
| 3451 |
case SURFACE_FORCED_META: |
| 3452 |
case SURFACE_META: |
| 3453 |
|
| 3454 |
for ( i = 0; i < ds->numIndexes; i += 3 ) |
| 3455 |
{ |
| 3456 |
tri[ 0 ] = &ds->verts[ ds->indexes[ i ] ]; |
| 3457 |
tri[ 1 ] = &ds->verts[ ds->indexes[ i + 1 ] ]; |
| 3458 |
tri[ 2 ] = &ds->verts[ ds->indexes[ i + 2 ] ]; |
| 3459 |
n = AddSurfaceModelsToTriangle_r( ds, model, tri ); |
| 3460 |
if ( n < 0 ) { |
| 3461 |
return n; |
| 3462 |
} |
| 3463 |
localNumSurfaceModels += n; |
| 3464 |
} |
| 3465 |
break; |
| 3466 |
|
| 3467 |
|
| 3468 |
default: |
| 3469 |
break; |
| 3470 |
} |
| 3471 |
} |
| 3472 |
|
| 3473 |
|
| 3474 |
return localNumSurfaceModels; |
| 3475 |
} |
| 3476 |
|
| 3477 |
|
| 3478 |
|
| 3479 |
|
| 3480 |
|
| 3481 |
|
| 3482 |
|
| 3483 |
|
| 3484 |
void AddEntitySurfaceModels( entity_t *e ){ |
| 3485 |
int i; |
| 3486 |
|
| 3487 |
|
| 3488 |
|
| 3489 |
Sys_FPrintf( SYS_VRB, "--- AddEntitySurfaceModels ---\n" ); |
| 3490 |
|
| 3491 |
|
| 3492 |
for ( i = e->firstDrawSurf; i < numMapDrawSurfs; i++ ) |
| 3493 |
numSurfaceModels += AddSurfaceModels( &mapDrawSurfs[ i ] ); |
| 3494 |
} |
| 3495 |
|
| 3496 |
|
| 3497 |
|
| 3498 |
|
| 3499 |
|
| 3500 |
|
| 3501 |
|
| 3502 |
|
| 3503 |
static void VolumeColorMods( entity_t *e, mapDrawSurface_t *ds ){ |
| 3504 |
int i, j; |
| 3505 |
float d; |
| 3506 |
brush_t *b; |
| 3507 |
plane_t *plane; |
| 3508 |
|
| 3509 |
|
| 3510 |
|
| 3511 |
if ( e->colorModBrushes == NULL ) { |
| 3512 |
return; |
| 3513 |
} |
| 3514 |
|
| 3515 |
|
| 3516 |
for ( b = e->colorModBrushes; b != NULL; b = b->nextColorModBrush ) |
| 3517 |
{ |
| 3518 |
|
| 3519 |
if ( b->entityNum != 0 && b->entityNum != ds->entityNum ) { |
| 3520 |
continue; |
| 3521 |
} |
| 3522 |
|
| 3523 |
|
| 3524 |
if ( b->mins[ 0 ] > ds->maxs[ 0 ] || b->maxs[ 0 ] < ds->mins[ 0 ] || |
| 3525 |
b->mins[ 1 ] > ds->maxs[ 1 ] || b->maxs[ 1 ] < ds->mins[ 1 ] || |
| 3526 |
b->mins[ 2 ] > ds->maxs[ 2 ] || b->maxs[ 2 ] < ds->mins[ 2 ] ) { |
| 3527 |
continue; |
| 3528 |
} |
| 3529 |
|
| 3530 |
|
| 3531 |
for ( i = 0; i < ds->numVerts; i++ ) |
| 3532 |
{ |
| 3533 |
|
| 3534 |
for ( j = 0; j < b->numsides; j++ ) |
| 3535 |
{ |
| 3536 |
|
| 3537 |
plane = &mapplanes[ b->sides[ j ].planenum ]; |
| 3538 |
d = DotProduct( ds->verts[ i ].xyz, plane->normal ) - plane->dist; |
| 3539 |
if ( d > 1.0f ) { |
| 3540 |
break; |
| 3541 |
} |
| 3542 |
} |
| 3543 |
|
| 3544 |
|
| 3545 |
if ( j == b->numsides ) { |
| 3546 |
ColorMod( b->contentShader->colorMod, 1, &ds->verts[ i ] ); |
| 3547 |
} |
| 3548 |
} |
| 3549 |
} |
| 3550 |
} |
| 3551 |
|
| 3552 |
|
| 3553 |
|
| 3554 |
|
| 3555 |
|
| 3556 |
|
| 3557 |
|
| 3558 |
|
| 3559 |
|
| 3560 |
|
| 3561 |
void FilterDrawsurfsIntoTree( entity_t *e, tree_t *tree ){ |
| 3562 |
int i, j; |
| 3563 |
mapDrawSurface_t *ds; |
| 3564 |
shaderInfo_t *si; |
| 3565 |
vec3_t origin, mins, maxs; |
| 3566 |
int refs; |
| 3567 |
int numSurfs, numRefs, numSkyboxSurfaces; |
| 3568 |
qboolean sb; |
| 3569 |
|
| 3570 |
|
| 3571 |
|
| 3572 |
Sys_FPrintf( SYS_VRB, "--- FilterDrawsurfsIntoTree ---\n" ); |
| 3573 |
|
| 3574 |
|
| 3575 |
numSurfs = 0; |
| 3576 |
numRefs = 0; |
| 3577 |
numSkyboxSurfaces = 0; |
| 3578 |
for ( i = e->firstDrawSurf; i < numMapDrawSurfs; i++ ) |
| 3579 |
{ |
| 3580 |
|
| 3581 |
ds = &mapDrawSurfs[ i ]; |
| 3582 |
if ( ds->numVerts == 0 && ds->type != SURFACE_FLARE && ds->type != SURFACE_SHADER ) { |
| 3583 |
continue; |
| 3584 |
} |
| 3585 |
|
| 3586 |
|
| 3587 |
si = ds->shaderInfo; |
| 3588 |
|
| 3589 |
|
| 3590 |
if ( ds->skybox ) { |
| 3591 |
refs = AddReferenceToTree_r( ds, tree->headnode, qtrue ); |
| 3592 |
ds->skybox = qfalse; |
| 3593 |
sb = qtrue; |
| 3594 |
} |
| 3595 |
else |
| 3596 |
{ |
| 3597 |
sb = qfalse; |
| 3598 |
|
| 3599 |
|
| 3600 |
refs = 0; |
| 3601 |
|
| 3602 |
|
| 3603 |
for ( j = 0; j < ds->numVerts; j++ ) |
| 3604 |
TCMod( si->mod, ds->verts[ j ].st ); |
| 3605 |
|
| 3606 |
|
| 3607 |
ColorMod( ds->shaderInfo->colorMod, ds->numVerts, ds->verts ); |
| 3608 |
|
| 3609 |
|
| 3610 |
VolumeColorMods( e, ds ); |
| 3611 |
|
| 3612 |
|
| 3613 |
if ( si->furNumLayers > 0 ) { |
| 3614 |
Fur( ds ); |
| 3615 |
} |
| 3616 |
|
| 3617 |
|
| 3618 |
if ( si->foliage != NULL ) { |
| 3619 |
Foliage( ds ); |
| 3620 |
} |
| 3621 |
|
| 3622 |
|
| 3623 |
if ( si->flareShader != NULL && si->flareShader[ 0 ] ) { |
| 3624 |
AddSurfaceFlare( ds, e->origin ); |
| 3625 |
} |
| 3626 |
|
| 3627 |
|
| 3628 |
if ( ( si->compileFlags & C_NODRAW ) && ds->type != SURFACE_PATCH ) { |
| 3629 |
continue; |
| 3630 |
} |
| 3631 |
|
| 3632 |
|
| 3633 |
BiasSurfaceTextures( ds ); |
| 3634 |
|
| 3635 |
|
| 3636 |
if ( e != entities && si->noFog == qfalse ) { |
| 3637 |
|
| 3638 |
VectorAdd( ds->mins, ds->maxs, origin ); |
| 3639 |
VectorScale( origin, 0.5f, origin ); |
| 3640 |
VectorAdd( origin, e->origin, origin ); |
| 3641 |
|
| 3642 |
VectorAdd( ds->mins, e->origin, mins ); |
| 3643 |
VectorAdd( ds->maxs, e->origin, maxs ); |
| 3644 |
|
| 3645 |
|
| 3646 |
ds->fogNum = FogForBounds( mins, maxs, 1.0f ); |
| 3647 |
} |
| 3648 |
} |
| 3649 |
|
| 3650 |
|
| 3651 |
|
| 3652 |
|
| 3653 |
|
| 3654 |
|
| 3655 |
|
| 3656 |
switch ( ds->type ) |
| 3657 |
{ |
| 3658 |
|
| 3659 |
case SURFACE_FACE: |
| 3660 |
case SURFACE_DECAL: |
| 3661 |
if ( refs == 0 ) { |
| 3662 |
refs = FilterFaceIntoTree( ds, tree ); |
| 3663 |
} |
| 3664 |
if ( refs > 0 ) { |
| 3665 |
EmitFaceSurface( ds ); |
| 3666 |
} |
| 3667 |
break; |
| 3668 |
|
| 3669 |
|
| 3670 |
case SURFACE_PATCH: |
| 3671 |
if ( refs == 0 ) { |
| 3672 |
refs = FilterPatchIntoTree( ds, tree ); |
| 3673 |
} |
| 3674 |
if ( refs > 0 ) { |
| 3675 |
EmitPatchSurface( e, ds ); |
| 3676 |
} |
| 3677 |
break; |
| 3678 |
|
| 3679 |
|
| 3680 |
case SURFACE_TRIANGLES: |
| 3681 |
case SURFACE_FORCED_META: |
| 3682 |
case SURFACE_META: |
| 3683 |
|
| 3684 |
if ( refs == 0 ) { |
| 3685 |
refs = FilterTrianglesIntoTree( ds, tree ); |
| 3686 |
} |
| 3687 |
if ( refs > 0 ) { |
| 3688 |
EmitTriangleSurface( ds ); |
| 3689 |
} |
| 3690 |
break; |
| 3691 |
|
| 3692 |
|
| 3693 |
case SURFACE_FOLIAGE: |
| 3694 |
|
| 3695 |
if ( refs == 0 ) { |
| 3696 |
refs = FilterFoliageIntoTree( ds, tree ); |
| 3697 |
} |
| 3698 |
if ( refs > 0 ) { |
| 3699 |
EmitTriangleSurface( ds ); |
| 3700 |
} |
| 3701 |
break; |
| 3702 |
|
| 3703 |
|
| 3704 |
case SURFACE_FOGHULL: |
| 3705 |
if ( refs == 0 ) { |
| 3706 |
refs = AddReferenceToTree_r( ds, tree->headnode, qfalse ); |
| 3707 |
} |
| 3708 |
if ( refs > 0 ) { |
| 3709 |
EmitTriangleSurface( ds ); |
| 3710 |
} |
| 3711 |
break; |
| 3712 |
|
| 3713 |
|
| 3714 |
case SURFACE_FLARE: |
| 3715 |
if ( refs == 0 ) { |
| 3716 |
refs = FilterFlareSurfIntoTree( ds, tree ); |
| 3717 |
} |
| 3718 |
if ( refs > 0 ) { |
| 3719 |
EmitFlareSurface( ds ); |
| 3720 |
} |
| 3721 |
break; |
| 3722 |
|
| 3723 |
|
| 3724 |
case SURFACE_SHADER: |
| 3725 |
refs = 1; |
| 3726 |
EmitFlareSurface( ds ); |
| 3727 |
break; |
| 3728 |
|
| 3729 |
|
| 3730 |
default: |
| 3731 |
refs = 0; |
| 3732 |
break; |
| 3733 |
} |
| 3734 |
|
| 3735 |
|
| 3736 |
|
| 3737 |
if ( sb ) { |
| 3738 |
ds->skybox = qfalse; |
| 3739 |
} |
| 3740 |
|
| 3741 |
|
| 3742 |
if ( refs > 0 ) { |
| 3743 |
|
| 3744 |
numSurfs++; |
| 3745 |
numRefs += refs; |
| 3746 |
|
| 3747 |
|
| 3748 |
SetSurfaceExtra( ds, numBSPDrawSurfaces - 1 ); |
| 3749 |
|
| 3750 |
|
| 3751 |
|
| 3752 |
{ |
| 3753 |
bspDrawSurface_t *out; |
| 3754 |
out = &bspDrawSurfaces[ numBSPDrawSurfaces - 1 ]; |
| 3755 |
if ( out->numVerts == 3 && out->numIndexes > 3 ) { |
| 3756 |
Sys_FPrintf( SYS_WRN, "WARNING: Potentially bad %s surface (%d: %d, %d)\n %s\n", |
| 3757 |
surfaceTypes[ ds->type ], |
| 3758 |
numBSPDrawSurfaces - 1, out->numVerts, out->numIndexes, si->shader ); |
| 3759 |
} |
| 3760 |
} |
| 3761 |
|
| 3762 |
|
| 3763 |
if ( ds->skybox ) { |
| 3764 |
MakeSkyboxSurface( ds ); |
| 3765 |
numSkyboxSurfaces++; |
| 3766 |
} |
| 3767 |
} |
| 3768 |
} |
| 3769 |
|
| 3770 |
|
| 3771 |
Sys_FPrintf( SYS_VRB, "%9d references\n", numRefs ); |
| 3772 |
Sys_FPrintf( SYS_VRB, "%9d (%d) emitted drawsurfs\n", numSurfs, numBSPDrawSurfaces ); |
| 3773 |
Sys_FPrintf( SYS_VRB, "%9d stripped face surfaces\n", numStripSurfaces ); |
| 3774 |
Sys_FPrintf( SYS_VRB, "%9d fanned face surfaces\n", numFanSurfaces ); |
| 3775 |
Sys_FPrintf( SYS_VRB, "%9d maxarea'd face surfaces\n", numMaxAreaSurfaces ); |
| 3776 |
Sys_FPrintf( SYS_VRB, "%9d surface models generated\n", numSurfaceModels ); |
| 3777 |
Sys_FPrintf( SYS_VRB, "%9d skybox surfaces generated\n", numSkyboxSurfaces ); |
| 3778 |
for ( i = 0; i < NUM_SURFACE_TYPES; i++ ) |
| 3779 |
Sys_FPrintf( SYS_VRB, "%9d %s surfaces\n", numSurfacesByType[ i ], surfaceTypes[ i ] ); |
| 3780 |
|
| 3781 |
Sys_FPrintf( SYS_VRB, "%9d redundant indexes suppressed, saving %d Kbytes\n", numRedundantIndexes, ( numRedundantIndexes * 4 / 1024 ) ); |
| 3782 |
} |