| 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_META_C |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
#include "q3map2.h" |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
#define LIGHTMAP_EXCEEDED -1 |
| 42 |
#define S_EXCEEDED -2 |
| 43 |
#define T_EXCEEDED -3 |
| 44 |
#define ST_EXCEEDED -4 |
| 45 |
#define UNSUITABLE_TRIANGLE -10 |
| 46 |
#define VERTS_EXCEEDED -1000 |
| 47 |
#define INDEXES_EXCEEDED -2000 |
| 48 |
|
| 49 |
#define GROW_META_VERTS 1024 |
| 50 |
#define GROW_META_TRIANGLES 1024 |
| 51 |
|
| 52 |
static int numMetaSurfaces, numPatchMetaSurfaces; |
| 53 |
|
| 54 |
static int maxMetaVerts = 0; |
| 55 |
static int numMetaVerts = 0; |
| 56 |
static int firstSearchMetaVert = 0; |
| 57 |
static bspDrawVert_t *metaVerts = NULL; |
| 58 |
|
| 59 |
static int maxMetaTriangles = 0; |
| 60 |
static int numMetaTriangles = 0; |
| 61 |
static metaTriangle_t *metaTriangles = NULL; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
void ClearMetaTriangles( void ){ |
| 71 |
numMetaVerts = 0; |
| 72 |
numMetaTriangles = 0; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
static int FindMetaVertex( bspDrawVert_t *src ){ |
| 83 |
int i; |
| 84 |
bspDrawVert_t *v, *temp; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
for ( i = firstSearchMetaVert, v = &metaVerts[ i ]; i < numMetaVerts; i++, v++ ) |
| 89 |
{ |
| 90 |
if ( memcmp( src, v, sizeof( bspDrawVert_t ) ) == 0 ) { |
| 91 |
return i; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
if ( numMetaVerts >= maxMetaVerts ) { |
| 97 |
|
| 98 |
maxMetaVerts += GROW_META_VERTS; |
| 99 |
temp = safe_malloc( maxMetaVerts * sizeof( bspDrawVert_t ) ); |
| 100 |
if ( metaVerts != NULL ) { |
| 101 |
memcpy( temp, metaVerts, numMetaVerts * sizeof( bspDrawVert_t ) ); |
| 102 |
free( metaVerts ); |
| 103 |
} |
| 104 |
metaVerts = temp; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
memcpy( &metaVerts[ numMetaVerts ], src, sizeof( bspDrawVert_t ) ); |
| 109 |
numMetaVerts++; |
| 110 |
|
| 111 |
|
| 112 |
return ( numMetaVerts - 1 ); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
static int AddMetaTriangle( void ){ |
| 123 |
metaTriangle_t *temp; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
if ( numMetaTriangles >= maxMetaTriangles ) { |
| 128 |
|
| 129 |
maxMetaTriangles += GROW_META_TRIANGLES; |
| 130 |
temp = safe_malloc( maxMetaTriangles * sizeof( metaTriangle_t ) ); |
| 131 |
if ( metaTriangles != NULL ) { |
| 132 |
memcpy( temp, metaTriangles, numMetaTriangles * sizeof( metaTriangle_t ) ); |
| 133 |
free( metaTriangles ); |
| 134 |
} |
| 135 |
metaTriangles = temp; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
numMetaTriangles++; |
| 140 |
return numMetaTriangles - 1; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
int FindMetaTriangle( metaTriangle_t *src, bspDrawVert_t *a, bspDrawVert_t *b, bspDrawVert_t *c, int planeNum ){ |
| 152 |
int triIndex; |
| 153 |
vec3_t dir; |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
VectorSubtract( a->xyz, b->xyz, dir ); |
| 159 |
if ( VectorLength( dir ) < 0.125f ) { |
| 160 |
return -1; |
| 161 |
} |
| 162 |
VectorSubtract( b->xyz, c->xyz, dir ); |
| 163 |
if ( VectorLength( dir ) < 0.125f ) { |
| 164 |
return -1; |
| 165 |
} |
| 166 |
VectorSubtract( c->xyz, a->xyz, dir ); |
| 167 |
if ( VectorLength( dir ) < 0.125f ) { |
| 168 |
return -1; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
if ( planeNum >= 0 ) { |
| 173 |
|
| 174 |
src->planeNum = planeNum; |
| 175 |
VectorCopy( mapplanes[ planeNum ].normal, src->plane ); |
| 176 |
src->plane[ 3 ] = mapplanes[ planeNum ].dist; |
| 177 |
} |
| 178 |
else |
| 179 |
{ |
| 180 |
|
| 181 |
src->planeNum = -1; |
| 182 |
if ( PlaneFromPoints( src->plane, a->xyz, b->xyz, c->xyz ) == qfalse ) { |
| 183 |
return -1; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
if ( VectorLength( a->normal ) <= 0.0f ) { |
| 189 |
VectorCopy( src->plane, a->normal ); |
| 190 |
} |
| 191 |
if ( VectorLength( b->normal ) <= 0.0f ) { |
| 192 |
VectorCopy( src->plane, b->normal ); |
| 193 |
} |
| 194 |
if ( VectorLength( c->normal ) <= 0.0f ) { |
| 195 |
VectorCopy( src->plane, c->normal ); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
if ( !( src->si->compileFlags & C_VERTEXLIT ) && |
| 200 |
src->lightmapAxis[ 0 ] == 0.0f && src->lightmapAxis[ 1 ] == 0.0f && src->lightmapAxis[ 2 ] == 0.0f ) { |
| 201 |
|
| 202 |
if ( src->si->lightmapAxis[ 0 ] || src->si->lightmapAxis[ 1 ] || src->si->lightmapAxis[ 2 ] ) { |
| 203 |
VectorCopy( src->si->lightmapAxis, src->lightmapAxis ); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
else{ |
| 208 |
CalcLightmapAxis( src->plane, src->lightmapAxis ); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
src->indexes[ 0 ] = FindMetaVertex( a ); |
| 214 |
src->indexes[ 1 ] = FindMetaVertex( b ); |
| 215 |
src->indexes[ 2 ] = FindMetaVertex( c ); |
| 216 |
|
| 217 |
|
| 218 |
#ifdef USE_EXHAUSTIVE_SEARCH |
| 219 |
{ |
| 220 |
int i; |
| 221 |
metaTriangle_t *tri; |
| 222 |
|
| 223 |
|
| 224 |
for ( i = 0, tri = metaTriangles; i < numMetaTriangles; i++, tri++ ) |
| 225 |
{ |
| 226 |
if ( memcmp( src, tri, sizeof( metaTriangle_t ) ) == 0 ) { |
| 227 |
return i; |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
#endif |
| 232 |
|
| 233 |
|
| 234 |
triIndex = AddMetaTriangle(); |
| 235 |
|
| 236 |
|
| 237 |
memcpy( &metaTriangles[ triIndex ], src, sizeof( metaTriangle_t ) ); |
| 238 |
|
| 239 |
|
| 240 |
return triIndex; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
static void SurfaceToMetaTriangles( mapDrawSurface_t *ds ){ |
| 251 |
int i; |
| 252 |
metaTriangle_t src; |
| 253 |
bspDrawVert_t a, b, c; |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
if ( ds->type != SURFACE_FACE && |
| 258 |
ds->type != SURFACE_META && |
| 259 |
ds->type != SURFACE_FORCED_META && |
| 260 |
ds->type != SURFACE_DECAL ) { |
| 261 |
return; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
firstSearchMetaVert = numMetaVerts; |
| 266 |
|
| 267 |
|
| 268 |
if ( ds->type != SURFACE_BAD && ds->numVerts >= 3 && ds->numIndexes >= 3 ) { |
| 269 |
|
| 270 |
for ( i = 0; i < ds->numIndexes; i += 3 ) |
| 271 |
{ |
| 272 |
|
| 273 |
if ( ds->indexes[ i ] == ds->indexes[ i + 1 ] || |
| 274 |
ds->indexes[ i ] == ds->indexes[ i + 2 ] || |
| 275 |
ds->indexes[ i + 1 ] == ds->indexes[ i + 2 ] ) { |
| 276 |
|
| 277 |
continue; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
src.si = ds->shaderInfo; |
| 282 |
src.side = ( ds->sideRef != NULL ? ds->sideRef->side : NULL ); |
| 283 |
src.entityNum = ds->entityNum; |
| 284 |
src.surfaceNum = ds->surfaceNum; |
| 285 |
src.planeNum = ds->planeNum; |
| 286 |
src.castShadows = ds->castShadows; |
| 287 |
src.recvShadows = ds->recvShadows; |
| 288 |
src.fogNum = ds->fogNum; |
| 289 |
src.sampleSize = ds->sampleSize; |
| 290 |
src.shadeAngleDegrees = ds->shadeAngleDegrees; |
| 291 |
VectorCopy( ds->lightmapAxis, src.lightmapAxis ); |
| 292 |
|
| 293 |
|
| 294 |
memcpy( &a, &ds->verts[ ds->indexes[ i ] ], sizeof( a ) ); |
| 295 |
memcpy( &b, &ds->verts[ ds->indexes[ i + 1 ] ], sizeof( b ) ); |
| 296 |
memcpy( &c, &ds->verts[ ds->indexes[ i + 2 ] ], sizeof( c ) ); |
| 297 |
FindMetaTriangle( &src, &a, &b, &c, ds->planeNum ); |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
numMetaSurfaces++; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
ClearSurface( ds ); |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
void TriangulatePatchSurface( entity_t *e, mapDrawSurface_t *ds ){ |
| 316 |
int iterations, x, y, pw[ 5 ], r; |
| 317 |
mapDrawSurface_t *dsNew; |
| 318 |
mesh_t src, *subdivided, *mesh; |
| 319 |
int forcePatchMeta; |
| 320 |
int patchQuality; |
| 321 |
int patchSubdivision; |
| 322 |
|
| 323 |
|
| 324 |
forcePatchMeta = IntForKey( e, "_patchMeta" ); |
| 325 |
if ( !forcePatchMeta ) { |
| 326 |
forcePatchMeta = IntForKey( e, "patchMeta" ); |
| 327 |
} |
| 328 |
patchQuality = IntForKey( e, "_patchQuality" ); |
| 329 |
if ( !patchQuality ) { |
| 330 |
patchQuality = IntForKey( e, "patchQuality" ); |
| 331 |
} |
| 332 |
if ( !patchQuality ) { |
| 333 |
patchQuality = 1.0; |
| 334 |
} |
| 335 |
patchSubdivision = IntForKey( e, "_patchSubdivide" ); |
| 336 |
if ( !patchSubdivision ) { |
| 337 |
patchSubdivision = IntForKey( e, "patchSubdivide" ); |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
if ( ds->numVerts == 0 || ds->type != SURFACE_PATCH || ( patchMeta == qfalse && !forcePatchMeta ) ) { |
| 342 |
return; |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
src.width = ds->patchWidth; |
| 347 |
src.height = ds->patchHeight; |
| 348 |
src.verts = ds->verts; |
| 349 |
|
| 350 |
if ( patchSubdivision ) { |
| 351 |
iterations = IterationsForCurve( ds->longestCurve, patchSubdivision ); |
| 352 |
} |
| 353 |
else{ |
| 354 |
iterations = IterationsForCurve( ds->longestCurve, patchSubdivisions / patchQuality ); |
| 355 |
} |
| 356 |
|
| 357 |
subdivided = SubdivideMesh2( src, iterations ); |
| 358 |
|
| 359 |
|
| 360 |
PutMeshOnCurve( *subdivided ); |
| 361 |
mesh = RemoveLinearMeshColumnsRows( subdivided ); |
| 362 |
FreeMesh( subdivided ); |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
dsNew = AllocDrawSurface( SURFACE_META ); |
| 367 |
memcpy( dsNew, ds, sizeof( *ds ) ); |
| 368 |
|
| 369 |
|
| 370 |
if ( !( ds->shaderInfo->compileFlags & C_SOLID ) ) { |
| 371 |
ClearSurface( ds ); |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
ds = dsNew; |
| 376 |
|
| 377 |
|
| 378 |
ds->type = SURFACE_META; |
| 379 |
ds->numIndexes = 0; |
| 380 |
ds->indexes = safe_malloc( mesh->width * mesh->height * 6 * sizeof( int ) ); |
| 381 |
|
| 382 |
|
| 383 |
ds->numVerts = ( mesh->width * mesh->height ); |
| 384 |
ds->verts = mesh->verts; |
| 385 |
|
| 386 |
|
| 387 |
for ( y = 0; y < ( mesh->height - 1 ); y++ ) |
| 388 |
{ |
| 389 |
for ( x = 0; x < ( mesh->width - 1 ); x++ ) |
| 390 |
{ |
| 391 |
|
| 392 |
pw[ 0 ] = x + ( y * mesh->width ); |
| 393 |
pw[ 1 ] = x + ( ( y + 1 ) * mesh->width ); |
| 394 |
pw[ 2 ] = x + 1 + ( ( y + 1 ) * mesh->width ); |
| 395 |
pw[ 3 ] = x + 1 + ( y * mesh->width ); |
| 396 |
pw[ 4 ] = x + ( y * mesh->width ); |
| 397 |
|
| 398 |
|
| 399 |
r = ( x + y ) & 1; |
| 400 |
|
| 401 |
|
| 402 |
ds->indexes[ ds->numIndexes++ ] = pw[ r + 0 ]; |
| 403 |
ds->indexes[ ds->numIndexes++ ] = pw[ r + 1 ]; |
| 404 |
ds->indexes[ ds->numIndexes++ ] = pw[ r + 2 ]; |
| 405 |
|
| 406 |
|
| 407 |
ds->indexes[ ds->numIndexes++ ] = pw[ r + 0 ]; |
| 408 |
ds->indexes[ ds->numIndexes++ ] = pw[ r + 2 ]; |
| 409 |
ds->indexes[ ds->numIndexes++ ] = pw[ r + 3 ]; |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
|
| 414 |
free( mesh ); |
| 415 |
|
| 416 |
|
| 417 |
numPatchMetaSurfaces++; |
| 418 |
|
| 419 |
|
| 420 |
ClassifySurfaces( 1, ds ); |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
#define TINY_AREA 1.0f |
| 426 |
#define MAXAREA_MAXTRIES 8 |
| 427 |
int MaxAreaIndexes( bspDrawVert_t *vert, int cnt, int *indexes ){ |
| 428 |
int r, s, t, bestR = 0, bestS = 1, bestT = 2; |
| 429 |
int i, j, try; |
| 430 |
double A, bestA = -1, V, bestV = -1; |
| 431 |
vec3_t ab, ac, bc, cross; |
| 432 |
bspDrawVert_t *buf; |
| 433 |
double shiftWidth; |
| 434 |
|
| 435 |
if ( cnt < 3 ) { |
| 436 |
return 0; |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
A = 0; |
| 441 |
for ( i = 1; i + 1 < cnt; ++i ) |
| 442 |
{ |
| 443 |
VectorSubtract( vert[i].xyz, vert[0].xyz, ab ); |
| 444 |
VectorSubtract( vert[i + 1].xyz, vert[0].xyz, ac ); |
| 445 |
CrossProduct( ab, ac, cross ); |
| 446 |
A += VectorLength( cross ); |
| 447 |
} |
| 448 |
V = 0; |
| 449 |
for ( i = 0; i < cnt; ++i ) |
| 450 |
{ |
| 451 |
VectorSubtract( vert[( i + 1 ) % cnt].xyz, vert[i].xyz, ab ); |
| 452 |
V += VectorLength( ab ); |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
shiftWidth = sqrt( A ) * 0.0125; |
| 463 |
|
| 464 |
if ( A - ceil( log( cnt / 1.5 ) / log( 2 ) ) * V * shiftWidth * 2 < 0 ) { |
| 465 |
|
| 466 |
shiftWidth = A / ( ceil( log( cnt / 1.5 ) / log( 2 ) ) * V * 2 ); |
| 467 |
|
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
for ( r = 0; r + 2 < cnt; ++r ) |
| 472 |
for ( s = r + 1; s + 1 < cnt; ++s ) |
| 473 |
for ( t = s + 1; t < cnt; ++t ) |
| 474 |
{ |
| 475 |
VectorSubtract( vert[s].xyz, vert[r].xyz, ab ); |
| 476 |
VectorSubtract( vert[t].xyz, vert[r].xyz, ac ); |
| 477 |
VectorSubtract( vert[t].xyz, vert[s].xyz, bc ); |
| 478 |
CrossProduct( ab, ac, cross ); |
| 479 |
A = VectorLength( cross ); |
| 480 |
|
| 481 |
V = A - ( VectorLength( ab ) - VectorLength( ac ) - VectorLength( bc ) ) * shiftWidth; |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
if ( bestA < 0 || V > bestV ) { |
| 486 |
bestA = A; |
| 487 |
bestV = V; |
| 488 |
bestR = r; |
| 489 |
bestS = s; |
| 490 |
bestT = t; |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
for ( try = 0; try < MAXAREA_MAXTRIES; ++try ) |
| 500 |
{ |
| 501 |
if ( try ) { |
| 502 |
bestR = rand() % cnt; |
| 503 |
bestS = rand() % cnt; |
| 504 |
bestT = rand() % cnt; |
| 505 |
if ( bestR == bestS || bestR == bestT || bestS == bestT ) { |
| 506 |
continue; |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
if ( bestR > bestS ) { |
| 511 |
j = bestR; |
| 512 |
bestR = bestS; |
| 513 |
bestS = j; |
| 514 |
} |
| 515 |
|
| 516 |
if ( bestS > bestT ) { |
| 517 |
j = bestS; |
| 518 |
bestS = bestT; |
| 519 |
bestT = j; |
| 520 |
} |
| 521 |
|
| 522 |
if ( bestR > bestS ) { |
| 523 |
j = bestR; |
| 524 |
bestR = bestS; |
| 525 |
bestS = j; |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
VectorSubtract( vert[bestS].xyz, vert[bestR].xyz, ab ); |
| 530 |
VectorSubtract( vert[bestT].xyz, vert[bestR].xyz, ac ); |
| 531 |
CrossProduct( ab, ac, cross ); |
| 532 |
bestA = VectorLength( cross ); |
| 533 |
} |
| 534 |
|
| 535 |
if ( bestA < TINY_AREA ) { |
| 536 |
|
| 537 |
continue; |
| 538 |
} |
| 539 |
|
| 540 |
i = 0; |
| 541 |
indexes[i++] = bestR; |
| 542 |
indexes[i++] = bestS; |
| 543 |
indexes[i++] = bestT; |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
j = MaxAreaIndexes( vert + bestR, bestS - bestR + 1, indexes + i ); |
| 555 |
if ( j < 0 ) { |
| 556 |
continue; |
| 557 |
} |
| 558 |
j += i; |
| 559 |
for (; i < j; ++i ) |
| 560 |
indexes[i] += bestR; |
| 561 |
|
| 562 |
j = MaxAreaIndexes( vert + bestS, bestT - bestS + 1, indexes + i ); |
| 563 |
if ( j < 0 ) { |
| 564 |
continue; |
| 565 |
} |
| 566 |
j += i; |
| 567 |
for (; i < j; ++i ) |
| 568 |
indexes[i] += bestS; |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
if ( cnt + bestR - bestT + 1 >= 3 ) { |
| 573 |
buf = safe_malloc( sizeof( *vert ) * ( cnt + bestR - bestT + 1 ) ); |
| 574 |
memcpy( buf, vert + bestT, sizeof( *vert ) * ( cnt - bestT ) ); |
| 575 |
memcpy( buf + ( cnt - bestT ), vert, sizeof( *vert ) * ( bestR + 1 ) ); |
| 576 |
j = MaxAreaIndexes( buf, cnt + bestR - bestT + 1, indexes + i ); |
| 577 |
if ( j < 0 ) { |
| 578 |
free( buf ); |
| 579 |
continue; |
| 580 |
} |
| 581 |
j += i; |
| 582 |
for (; i < j; ++i ) |
| 583 |
indexes[i] = ( indexes[i] + bestT ) % cnt; |
| 584 |
|
| 585 |
free( buf ); |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
return i; |
| 590 |
} |
| 591 |
|
| 592 |
return -1; |
| 593 |
} |
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
void MaxAreaFaceSurface( mapDrawSurface_t *ds ){ |
| 603 |
int n; |
| 604 |
|
| 605 |
if ( !ds->numVerts || ( ds->type != SURFACE_FACE && ds->type != SURFACE_DECAL ) ) { |
| 606 |
return; |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
if ( ds->numVerts == 3 ) { |
| 611 |
ds->numIndexes = 3; |
| 612 |
ds->indexes = safe_malloc( ds->numIndexes * sizeof( int ) ); |
| 613 |
VectorSet( ds->indexes, 0, 1, 2 ); |
| 614 |
numMaxAreaSurfaces++; |
| 615 |
return; |
| 616 |
} |
| 617 |
|
| 618 |
|
| 619 |
ds->numIndexes = 3 * ds->numVerts - 6; |
| 620 |
ds->indexes = safe_malloc( ds->numIndexes * sizeof( int ) ); |
| 621 |
n = MaxAreaIndexes( ds->verts, ds->numVerts, ds->indexes ); |
| 622 |
if ( n < 0 ) { |
| 623 |
|
| 624 |
free( ds->indexes ); |
| 625 |
ds->numIndexes = 0; |
| 626 |
StripFaceSurface( ds ); |
| 627 |
return; |
| 628 |
} |
| 629 |
ds->numIndexes = n; |
| 630 |
|
| 631 |
|
| 632 |
numMaxAreaSurfaces++; |
| 633 |
|
| 634 |
|
| 635 |
ClassifySurfaces( 1, ds ); |
| 636 |
} |
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
void FanFaceSurface( mapDrawSurface_t *ds ){ |
| 647 |
int i, j, k, a, b, c, color[ MAX_LIGHTMAPS ][ 4 ]; |
| 648 |
bspDrawVert_t *verts, *centroid, *dv; |
| 649 |
double iv; |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
if ( !ds->numVerts || ( ds->type != SURFACE_FACE && ds->type != SURFACE_DECAL ) ) { |
| 654 |
return; |
| 655 |
} |
| 656 |
|
| 657 |
|
| 658 |
verts = safe_malloc( ( ds->numVerts + 1 ) * sizeof( bspDrawVert_t ) ); |
| 659 |
|
| 660 |
memset( verts, 0, sizeof( bspDrawVert_t ) ); |
| 661 |
memcpy( &verts[ 1 ], ds->verts, ds->numVerts * sizeof( bspDrawVert_t ) ); |
| 662 |
free( ds->verts ); |
| 663 |
ds->verts = verts; |
| 664 |
|
| 665 |
|
| 666 |
centroid = &verts[ 0 ]; |
| 667 |
memset( color, 0, 4 * MAX_LIGHTMAPS * sizeof( int ) ); |
| 668 |
for ( i = 1, dv = &verts[ 1 ]; i < ( ds->numVerts + 1 ); i++, dv++ ) |
| 669 |
{ |
| 670 |
VectorAdd( centroid->xyz, dv->xyz, centroid->xyz ); |
| 671 |
VectorAdd( centroid->normal, dv->normal, centroid->normal ); |
| 672 |
for ( j = 0; j < 4; j++ ) |
| 673 |
{ |
| 674 |
for ( k = 0; k < MAX_LIGHTMAPS; k++ ) |
| 675 |
color[ k ][ j ] += dv->color[ k ][ j ]; |
| 676 |
if ( j < 2 ) { |
| 677 |
centroid->st[ j ] += dv->st[ j ]; |
| 678 |
for ( k = 0; k < MAX_LIGHTMAPS; k++ ) |
| 679 |
centroid->lightmap[ k ][ j ] += dv->lightmap[ k ][ j ]; |
| 680 |
} |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
|
| 685 |
iv = 1.0f / ds->numVerts; |
| 686 |
VectorScale( centroid->xyz, iv, centroid->xyz ); |
| 687 |
if ( VectorNormalize( centroid->normal, centroid->normal ) <= 0 ) { |
| 688 |
VectorCopy( verts[ 1 ].normal, centroid->normal ); |
| 689 |
} |
| 690 |
for ( j = 0; j < 4; j++ ) |
| 691 |
{ |
| 692 |
for ( k = 0; k < MAX_LIGHTMAPS; k++ ) |
| 693 |
{ |
| 694 |
color[ k ][ j ] /= ds->numVerts; |
| 695 |
centroid->color[ k ][ j ] = ( color[ k ][ j ] < 255.0f ? color[ k ][ j ] : 255 ); |
| 696 |
} |
| 697 |
if ( j < 2 ) { |
| 698 |
centroid->st[ j ] *= iv; |
| 699 |
for ( k = 0; k < MAX_LIGHTMAPS; k++ ) |
| 700 |
centroid->lightmap[ k ][ j ] *= iv; |
| 701 |
} |
| 702 |
} |
| 703 |
|
| 704 |
|
| 705 |
ds->numVerts++; |
| 706 |
|
| 707 |
|
| 708 |
ds->numIndexes = 0; |
| 709 |
ds->indexes = safe_malloc( ds->numVerts * 3 * sizeof( int ) ); |
| 710 |
for ( i = 1; i < ds->numVerts; i++ ) |
| 711 |
{ |
| 712 |
a = 0; |
| 713 |
b = i; |
| 714 |
c = ( i + 1 ) % ds->numVerts; |
| 715 |
c = c ? c : 1; |
| 716 |
ds->indexes[ ds->numIndexes++ ] = a; |
| 717 |
ds->indexes[ ds->numIndexes++ ] = b; |
| 718 |
ds->indexes[ ds->numIndexes++ ] = c; |
| 719 |
} |
| 720 |
|
| 721 |
|
| 722 |
numFanSurfaces++; |
| 723 |
|
| 724 |
|
| 725 |
ClassifySurfaces( 1, ds ); |
| 726 |
} |
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
#define MAX_INDEXES 1024 |
| 737 |
|
| 738 |
void StripFaceSurface( mapDrawSurface_t *ds ){ |
| 739 |
int i, r, least, rotate, numIndexes, ni, a, b, c, indexes[ MAX_INDEXES ]; |
| 740 |
vec_t *v1, *v2; |
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
if ( !ds->numVerts || ( ds->type != SURFACE_FACE && ds->type != SURFACE_DECAL ) ) { |
| 745 |
return; |
| 746 |
} |
| 747 |
|
| 748 |
|
| 749 |
if ( ds->numVerts == 3 ) { |
| 750 |
numIndexes = 3; |
| 751 |
VectorSet( indexes, 0, 1, 2 ); |
| 752 |
} |
| 753 |
else |
| 754 |
{ |
| 755 |
|
| 756 |
least = 0; |
| 757 |
if ( ds->shaderInfo != NULL && ds->shaderInfo->autosprite == qfalse ) { |
| 758 |
for ( i = 0; i < ds->numVerts; i++ ) |
| 759 |
{ |
| 760 |
|
| 761 |
v1 = ds->verts[ i ].xyz; |
| 762 |
v2 = ds->verts[ least ].xyz; |
| 763 |
|
| 764 |
|
| 765 |
if ( v1[ 0 ] < v2[ 0 ] || |
| 766 |
( v1[ 0 ] == v2[ 0 ] && v1[ 1 ] < v2[ 1 ] ) || |
| 767 |
( v1[ 0 ] == v2[ 0 ] && v1[ 1 ] == v2[ 1 ] && v1[ 2 ] < v2[ 2 ] ) ) { |
| 768 |
least = i; |
| 769 |
} |
| 770 |
} |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
numIndexes = ( ds->numVerts - 2 ) * 3; |
| 775 |
if ( numIndexes > MAX_INDEXES ) { |
| 776 |
Error( "MAX_INDEXES exceeded for surface (%d > %d) (%d verts)", numIndexes, MAX_INDEXES, ds->numVerts ); |
| 777 |
} |
| 778 |
|
| 779 |
|
| 780 |
ni = 0; |
| 781 |
for ( r = 0; r < ds->numVerts; r++ ) |
| 782 |
{ |
| 783 |
|
| 784 |
rotate = ( r + least ) % ds->numVerts; |
| 785 |
|
| 786 |
|
| 787 |
for ( ni = 0, i = 0; i < ds->numVerts - 2 - i; i++ ) |
| 788 |
{ |
| 789 |
|
| 790 |
a = ( ds->numVerts - 1 - i + rotate ) % ds->numVerts; |
| 791 |
b = ( i + rotate ) % ds->numVerts; |
| 792 |
c = ( ds->numVerts - 2 - i + rotate ) % ds->numVerts; |
| 793 |
|
| 794 |
|
| 795 |
if ( ds->numVerts > 4 && IsTriangleDegenerate( ds->verts, a, b, c ) ) { |
| 796 |
break; |
| 797 |
} |
| 798 |
indexes[ ni++ ] = a; |
| 799 |
indexes[ ni++ ] = b; |
| 800 |
indexes[ ni++ ] = c; |
| 801 |
|
| 802 |
|
| 803 |
if ( i + 1 != ds->numVerts - 1 - i ) { |
| 804 |
|
| 805 |
a = ( ds->numVerts - 2 - i + rotate ) % ds->numVerts; |
| 806 |
b = ( i + rotate ) % ds->numVerts; |
| 807 |
c = ( i + 1 + rotate ) % ds->numVerts; |
| 808 |
|
| 809 |
|
| 810 |
if ( ds->numVerts > 4 && IsTriangleDegenerate( ds->verts, a, b, c ) ) { |
| 811 |
break; |
| 812 |
} |
| 813 |
indexes[ ni++ ] = a; |
| 814 |
indexes[ ni++ ] = b; |
| 815 |
indexes[ ni++ ] = c; |
| 816 |
} |
| 817 |
} |
| 818 |
|
| 819 |
|
| 820 |
if ( ni == numIndexes ) { |
| 821 |
break; |
| 822 |
} |
| 823 |
} |
| 824 |
|
| 825 |
|
| 826 |
if ( ni < numIndexes ) { |
| 827 |
FanFaceSurface( ds ); |
| 828 |
return; |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
|
| 833 |
ds->numIndexes = numIndexes; |
| 834 |
ds->indexes = safe_malloc( ds->numIndexes * sizeof( int ) ); |
| 835 |
memcpy( ds->indexes, indexes, ds->numIndexes * sizeof( int ) ); |
| 836 |
|
| 837 |
|
| 838 |
numStripSurfaces++; |
| 839 |
|
| 840 |
|
| 841 |
ClassifySurfaces( 1, ds ); |
| 842 |
} |
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
void EmitMetaStats(){ |
| 852 |
Sys_Printf( "--- EmitMetaStats ---\n" ); |
| 853 |
Sys_Printf( "%9d total meta surfaces\n", numMetaSurfaces ); |
| 854 |
Sys_Printf( "%9d stripped surfaces\n", numStripSurfaces ); |
| 855 |
Sys_Printf( "%9d fanned surfaces\n", numFanSurfaces ); |
| 856 |
Sys_Printf( "%9d maxarea'd surfaces\n", numMaxAreaSurfaces ); |
| 857 |
Sys_Printf( "%9d patch meta surfaces\n", numPatchMetaSurfaces ); |
| 858 |
Sys_Printf( "%9d meta verts\n", numMetaVerts ); |
| 859 |
Sys_Printf( "%9d meta triangles\n", numMetaTriangles ); |
| 860 |
} |
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
void MakeEntityMetaTriangles( entity_t *e ){ |
| 870 |
int i, f, fOld, start; |
| 871 |
mapDrawSurface_t *ds; |
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
Sys_FPrintf( SYS_VRB, "--- MakeEntityMetaTriangles ---\n" ); |
| 876 |
|
| 877 |
|
| 878 |
fOld = -1; |
| 879 |
start = I_FloatTime(); |
| 880 |
|
| 881 |
|
| 882 |
for ( i = e->firstDrawSurf; i < numMapDrawSurfs; i++ ) |
| 883 |
{ |
| 884 |
|
| 885 |
f = 10 * ( i - e->firstDrawSurf ) / ( numMapDrawSurfs - e->firstDrawSurf ); |
| 886 |
if ( f != fOld ) { |
| 887 |
fOld = f; |
| 888 |
Sys_FPrintf( SYS_VRB, "%d...", f ); |
| 889 |
} |
| 890 |
|
| 891 |
|
| 892 |
ds = &mapDrawSurfs[ i ]; |
| 893 |
if ( ds->numVerts <= 0 ) { |
| 894 |
continue; |
| 895 |
} |
| 896 |
|
| 897 |
|
| 898 |
if ( ds->shaderInfo->autosprite ) { |
| 899 |
continue; |
| 900 |
} |
| 901 |
|
| 902 |
|
| 903 |
if ( meta == qfalse && ds->shaderInfo->forceMeta == qfalse ) { |
| 904 |
continue; |
| 905 |
} |
| 906 |
|
| 907 |
|
| 908 |
switch ( ds->type ) |
| 909 |
{ |
| 910 |
case SURFACE_FACE: |
| 911 |
case SURFACE_DECAL: |
| 912 |
if ( maxAreaFaceSurface ) { |
| 913 |
MaxAreaFaceSurface( ds ); |
| 914 |
} |
| 915 |
else{ |
| 916 |
StripFaceSurface( ds ); |
| 917 |
} |
| 918 |
SurfaceToMetaTriangles( ds ); |
| 919 |
break; |
| 920 |
|
| 921 |
case SURFACE_PATCH: |
| 922 |
TriangulatePatchSurface( e, ds ); |
| 923 |
break; |
| 924 |
|
| 925 |
case SURFACE_TRIANGLES: |
| 926 |
break; |
| 927 |
|
| 928 |
case SURFACE_FORCED_META: |
| 929 |
case SURFACE_META: |
| 930 |
SurfaceToMetaTriangles( ds ); |
| 931 |
break; |
| 932 |
|
| 933 |
default: |
| 934 |
break; |
| 935 |
} |
| 936 |
} |
| 937 |
|
| 938 |
|
| 939 |
if ( ( numMapDrawSurfs - e->firstDrawSurf ) ) { |
| 940 |
Sys_FPrintf( SYS_VRB, " (%d)\n", (int) ( I_FloatTime() - start ) ); |
| 941 |
} |
| 942 |
|
| 943 |
|
| 944 |
Sys_FPrintf( SYS_VRB, "%9d total meta surfaces\n", numMetaSurfaces ); |
| 945 |
Sys_FPrintf( SYS_VRB, "%9d stripped surfaces\n", numStripSurfaces ); |
| 946 |
Sys_FPrintf( SYS_VRB, "%9d fanned surfaces\n", numFanSurfaces ); |
| 947 |
Sys_FPrintf( SYS_VRB, "%9d maxarea'd surfaces\n", numMaxAreaSurfaces ); |
| 948 |
Sys_FPrintf( SYS_VRB, "%9d patch meta surfaces\n", numPatchMetaSurfaces ); |
| 949 |
Sys_FPrintf( SYS_VRB, "%9d meta verts\n", numMetaVerts ); |
| 950 |
Sys_FPrintf( SYS_VRB, "%9d meta triangles\n", numMetaTriangles ); |
| 951 |
|
| 952 |
|
| 953 |
TidyEntitySurfaces( e ); |
| 954 |
} |
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
typedef struct edge_s |
| 964 |
{ |
| 965 |
vec3_t origin; |
| 966 |
vec4_t edge; |
| 967 |
vec_t length, kingpinLength; |
| 968 |
int kingpin; |
| 969 |
vec4_t plane; |
| 970 |
} |
| 971 |
edge_t; |
| 972 |
|
| 973 |
void CreateEdge( vec4_t plane, vec3_t a, vec3_t b, edge_t *edge ){ |
| 974 |
|
| 975 |
VectorCopy( a, edge->origin ); |
| 976 |
|
| 977 |
|
| 978 |
VectorSubtract( b, a, edge->edge ); |
| 979 |
|
| 980 |
if ( fabs( edge->edge[ 0 ] ) > fabs( edge->edge[ 1 ] ) && fabs( edge->edge[ 0 ] ) > fabs( edge->edge[ 2 ] ) ) { |
| 981 |
edge->kingpin = 0; |
| 982 |
} |
| 983 |
else if ( fabs( edge->edge[ 1 ] ) > fabs( edge->edge[ 0 ] ) && fabs( edge->edge[ 1 ] ) > fabs( edge->edge[ 2 ] ) ) { |
| 984 |
edge->kingpin = 1; |
| 985 |
} |
| 986 |
else{ |
| 987 |
edge->kingpin = 2; |
| 988 |
} |
| 989 |
edge->kingpinLength = edge->edge[ edge->kingpin ]; |
| 990 |
|
| 991 |
VectorNormalize( edge->edge, edge->edge ); |
| 992 |
edge->edge[ 3 ] = DotProduct( a, edge->edge ); |
| 993 |
edge->length = DotProduct( b, edge->edge ) - edge->edge[ 3 ]; |
| 994 |
|
| 995 |
|
| 996 |
CrossProduct( plane, edge->edge, edge->plane ); |
| 997 |
edge->plane[ 3 ] = DotProduct( a, edge->plane ); |
| 998 |
} |
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
#define TJ_PLANE_EPSILON ( 1.0f / 8.0f ) |
| 1008 |
#define TJ_EDGE_EPSILON ( 1.0f / 8.0f ) |
| 1009 |
#define TJ_POINT_EPSILON ( 1.0f / 8.0f ) |
| 1010 |
|
| 1011 |
void FixMetaTJunctions( void ){ |
| 1012 |
int i, j, k, f, fOld, start, vertIndex, triIndex, numTJuncs; |
| 1013 |
metaTriangle_t *tri, *newTri; |
| 1014 |
shaderInfo_t *si; |
| 1015 |
bspDrawVert_t *a, *b, *c, junc; |
| 1016 |
float dist, amount; |
| 1017 |
vec3_t pt; |
| 1018 |
vec4_t plane; |
| 1019 |
edge_t edges[ 3 ]; |
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
return; |
| 1024 |
|
| 1025 |
|
| 1026 |
Sys_FPrintf( SYS_VRB, "--- FixMetaTJunctions ---\n" ); |
| 1027 |
|
| 1028 |
|
| 1029 |
fOld = -1; |
| 1030 |
start = I_FloatTime(); |
| 1031 |
|
| 1032 |
|
| 1033 |
numTJuncs = 0; |
| 1034 |
for ( i = 0; i < numMetaTriangles; i++ ) |
| 1035 |
{ |
| 1036 |
|
| 1037 |
tri = &metaTriangles[ i ]; |
| 1038 |
|
| 1039 |
|
| 1040 |
f = 10 * i / numMetaTriangles; |
| 1041 |
if ( f != fOld ) { |
| 1042 |
fOld = f; |
| 1043 |
Sys_FPrintf( SYS_VRB, "%d...", f ); |
| 1044 |
} |
| 1045 |
|
| 1046 |
|
| 1047 |
si = tri->si; |
| 1048 |
if ( ( si->compileFlags & C_NODRAW ) || si->autosprite || si->notjunc ) { |
| 1049 |
continue; |
| 1050 |
} |
| 1051 |
|
| 1052 |
|
| 1053 |
VectorCopy( tri->plane, plane ); |
| 1054 |
plane[ 3 ] = tri->plane[ 3 ]; |
| 1055 |
CreateEdge( plane, metaVerts[ tri->indexes[ 0 ] ].xyz, metaVerts[ tri->indexes[ 1 ] ].xyz, &edges[ 0 ] ); |
| 1056 |
CreateEdge( plane, metaVerts[ tri->indexes[ 1 ] ].xyz, metaVerts[ tri->indexes[ 2 ] ].xyz, &edges[ 1 ] ); |
| 1057 |
CreateEdge( plane, metaVerts[ tri->indexes[ 2 ] ].xyz, metaVerts[ tri->indexes[ 0 ] ].xyz, &edges[ 2 ] ); |
| 1058 |
|
| 1059 |
|
| 1060 |
for ( j = 0; j < numMetaVerts; j++ ) |
| 1061 |
{ |
| 1062 |
|
| 1063 |
VectorCopy( metaVerts[ j ].xyz, pt ); |
| 1064 |
|
| 1065 |
|
| 1066 |
dist = DotProduct( pt, plane ) - plane[ 3 ]; |
| 1067 |
if ( fabs( dist ) > TJ_PLANE_EPSILON ) { |
| 1068 |
continue; |
| 1069 |
} |
| 1070 |
|
| 1071 |
|
| 1072 |
for ( k = 0; k < 3; k++ ) |
| 1073 |
{ |
| 1074 |
if ( fabs( pt[ 0 ] - metaVerts[ tri->indexes[ k ] ].xyz[ 0 ] ) <= TJ_POINT_EPSILON && |
| 1075 |
fabs( pt[ 1 ] - metaVerts[ tri->indexes[ k ] ].xyz[ 1 ] ) <= TJ_POINT_EPSILON && |
| 1076 |
fabs( pt[ 2 ] - metaVerts[ tri->indexes[ k ] ].xyz[ 2 ] ) <= TJ_POINT_EPSILON ) { |
| 1077 |
break; |
| 1078 |
} |
| 1079 |
} |
| 1080 |
if ( k < 3 ) { |
| 1081 |
continue; |
| 1082 |
} |
| 1083 |
|
| 1084 |
|
| 1085 |
for ( k = 0; k < 3; k++ ) |
| 1086 |
{ |
| 1087 |
|
| 1088 |
if ( fabs( edges[ k ].kingpinLength ) < TJ_EDGE_EPSILON ) { |
| 1089 |
continue; |
| 1090 |
} |
| 1091 |
|
| 1092 |
|
| 1093 |
dist = DotProduct( pt, edges[ k ].plane ) - edges[ k ].plane[ 3 ]; |
| 1094 |
if ( fabs( dist ) > TJ_EDGE_EPSILON ) { |
| 1095 |
continue; |
| 1096 |
} |
| 1097 |
|
| 1098 |
|
| 1099 |
amount = ( pt[ edges[ k ].kingpin ] - edges[ k ].origin[ edges[ k ].kingpin ] ) / edges[ k ].kingpinLength; |
| 1100 |
if ( amount <= 0.0f || amount >= 1.0f ) { |
| 1101 |
continue; |
| 1102 |
} |
| 1103 |
|
| 1104 |
#if 0 |
| 1105 |
dist = DotProduct( pt, edges[ k ].edge ) - edges[ k ].edge[ 3 ]; |
| 1106 |
if ( dist <= -0.0f || dist >= edges[ k ].length ) { |
| 1107 |
continue; |
| 1108 |
} |
| 1109 |
amount = dist / edges[ k ].length; |
| 1110 |
#endif |
| 1111 |
|
| 1112 |
|
| 1113 |
a = &metaVerts[ tri->indexes[ k % 3 ] ]; |
| 1114 |
b = &metaVerts[ tri->indexes[ ( k + 1 ) % 3 ] ]; |
| 1115 |
c = &metaVerts[ tri->indexes[ ( k + 2 ) % 3 ] ]; |
| 1116 |
|
| 1117 |
|
| 1118 |
LerpDrawVertAmount( a, b, amount, &junc ); |
| 1119 |
VectorCopy( pt, junc.xyz ); |
| 1120 |
|
| 1121 |
|
| 1122 |
if ( VectorCompare( junc.xyz, a->xyz ) || VectorCompare( junc.xyz, b->xyz ) || VectorCompare( junc.xyz, c->xyz ) ) { |
| 1123 |
continue; |
| 1124 |
} |
| 1125 |
|
| 1126 |
|
| 1127 |
if ( !memcmp( &metaVerts[ j ], &junc, sizeof( junc ) ) ) { |
| 1128 |
vertIndex = j; |
| 1129 |
} |
| 1130 |
else |
| 1131 |
{ |
| 1132 |
|
| 1133 |
firstSearchMetaVert = numMetaVerts; |
| 1134 |
vertIndex = FindMetaVertex( &junc ); |
| 1135 |
if ( vertIndex < 0 ) { |
| 1136 |
continue; |
| 1137 |
} |
| 1138 |
} |
| 1139 |
|
| 1140 |
|
| 1141 |
triIndex = AddMetaTriangle(); |
| 1142 |
if ( triIndex < 0 ) { |
| 1143 |
continue; |
| 1144 |
} |
| 1145 |
|
| 1146 |
|
| 1147 |
tri = &metaTriangles[ i ]; |
| 1148 |
newTri = &metaTriangles[ triIndex ]; |
| 1149 |
|
| 1150 |
|
| 1151 |
memcpy( newTri, tri, sizeof( *tri ) ); |
| 1152 |
|
| 1153 |
|
| 1154 |
tri->indexes[ ( k + 1 ) % 3 ] = vertIndex; |
| 1155 |
newTri->indexes[ k ] = vertIndex; |
| 1156 |
|
| 1157 |
|
| 1158 |
CreateEdge( plane, metaVerts[ tri->indexes[ 0 ] ].xyz, metaVerts[ tri->indexes[ 1 ] ].xyz, &edges[ 0 ] ); |
| 1159 |
CreateEdge( plane, metaVerts[ tri->indexes[ 1 ] ].xyz, metaVerts[ tri->indexes[ 2 ] ].xyz, &edges[ 1 ] ); |
| 1160 |
CreateEdge( plane, metaVerts[ tri->indexes[ 2 ] ].xyz, metaVerts[ tri->indexes[ 0 ] ].xyz, &edges[ 2 ] ); |
| 1161 |
|
| 1162 |
|
| 1163 |
metaVerts[ vertIndex ].color[ 0 ][ 0 ] = 255; |
| 1164 |
metaVerts[ vertIndex ].color[ 0 ][ 1 ] = 204; |
| 1165 |
metaVerts[ vertIndex ].color[ 0 ][ 2 ] = 0; |
| 1166 |
|
| 1167 |
|
| 1168 |
numTJuncs++; |
| 1169 |
break; |
| 1170 |
} |
| 1171 |
} |
| 1172 |
} |
| 1173 |
|
| 1174 |
|
| 1175 |
Sys_FPrintf( SYS_VRB, " (%d)\n", (int) ( I_FloatTime() - start ) ); |
| 1176 |
|
| 1177 |
|
| 1178 |
Sys_FPrintf( SYS_VRB, "%9d T-junctions added\n", numTJuncs ); |
| 1179 |
} |
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
#define MAX_SAMPLES 256 |
| 1189 |
#define THETA_EPSILON 0.000001 |
| 1190 |
#define EQUAL_NORMAL_EPSILON 0.01 |
| 1191 |
|
| 1192 |
void SmoothMetaTriangles( void ){ |
| 1193 |
int i, j, k, f, fOld, start, cs, numVerts, numVotes, numSmoothed; |
| 1194 |
float shadeAngle, defaultShadeAngle, maxShadeAngle, dot, testAngle; |
| 1195 |
metaTriangle_t *tri; |
| 1196 |
float *shadeAngles; |
| 1197 |
byte *smoothed; |
| 1198 |
vec3_t average, diff; |
| 1199 |
int indexes[ MAX_SAMPLES ]; |
| 1200 |
vec3_t votes[ MAX_SAMPLES ]; |
| 1201 |
|
| 1202 |
|
| 1203 |
Sys_FPrintf( SYS_VRB, "--- SmoothMetaTriangles ---\n" ); |
| 1204 |
|
| 1205 |
|
| 1206 |
shadeAngles = safe_malloc0( numMetaVerts * sizeof( float ) ); |
| 1207 |
|
| 1208 |
|
| 1209 |
cs = ( numMetaVerts / 8 ) + 1; |
| 1210 |
smoothed = safe_malloc0( cs ); |
| 1211 |
|
| 1212 |
|
| 1213 |
defaultShadeAngle = DEG2RAD( npDegrees ); |
| 1214 |
maxShadeAngle = 0.0f; |
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
for ( i = 0, tri = &metaTriangles[ i ]; i < numMetaTriangles; i++, tri++ ) |
| 1219 |
{ |
| 1220 |
shadeAngle = defaultShadeAngle; |
| 1221 |
|
| 1222 |
|
| 1223 |
if ( tri->si->shadeAngleDegrees > 0.0f ) { |
| 1224 |
shadeAngle = DEG2RAD( tri->si->shadeAngleDegrees ); |
| 1225 |
} |
| 1226 |
|
| 1227 |
else if ( tri->shadeAngleDegrees > 0.0f ) { |
| 1228 |
shadeAngle = DEG2RAD( tri->shadeAngleDegrees ); |
| 1229 |
} |
| 1230 |
|
| 1231 |
if ( shadeAngle <= 0.0f ) { |
| 1232 |
shadeAngle = defaultShadeAngle; |
| 1233 |
} |
| 1234 |
|
| 1235 |
if ( shadeAngle > maxShadeAngle ) { |
| 1236 |
maxShadeAngle = shadeAngle; |
| 1237 |
} |
| 1238 |
|
| 1239 |
|
| 1240 |
for ( j = 0; j < 3; j++ ) |
| 1241 |
{ |
| 1242 |
shadeAngles[ tri->indexes[ j ] ] = shadeAngle; |
| 1243 |
if ( shadeAngle <= 0 ) { |
| 1244 |
smoothed[ tri->indexes[ j ] >> 3 ] |= ( 1 << ( tri->indexes[ j ] & 7 ) ); |
| 1245 |
} |
| 1246 |
} |
| 1247 |
} |
| 1248 |
|
| 1249 |
|
| 1250 |
if ( maxShadeAngle <= 0 ) { |
| 1251 |
Sys_FPrintf( SYS_VRB, "No smoothing angles specified, aborting\n" ); |
| 1252 |
free( shadeAngles ); |
| 1253 |
free( smoothed ); |
| 1254 |
return; |
| 1255 |
} |
| 1256 |
|
| 1257 |
|
| 1258 |
fOld = -1; |
| 1259 |
start = I_FloatTime(); |
| 1260 |
|
| 1261 |
|
| 1262 |
numSmoothed = 0; |
| 1263 |
for ( i = 0; i < numMetaVerts; i++ ) |
| 1264 |
{ |
| 1265 |
|
| 1266 |
f = 10 * i / numMetaVerts; |
| 1267 |
if ( f != fOld ) { |
| 1268 |
fOld = f; |
| 1269 |
Sys_FPrintf( SYS_VRB, "%d...", f ); |
| 1270 |
} |
| 1271 |
|
| 1272 |
|
| 1273 |
if ( smoothed[ i >> 3 ] & ( 1 << ( i & 7 ) ) ) { |
| 1274 |
continue; |
| 1275 |
} |
| 1276 |
|
| 1277 |
|
| 1278 |
VectorClear( average ); |
| 1279 |
numVerts = 0; |
| 1280 |
numVotes = 0; |
| 1281 |
|
| 1282 |
|
| 1283 |
for ( j = i; j < numMetaVerts && numVerts < MAX_SAMPLES; j++ ) |
| 1284 |
{ |
| 1285 |
|
| 1286 |
if ( smoothed[ j >> 3 ] & ( 1 << ( j & 7 ) ) ) { |
| 1287 |
continue; |
| 1288 |
} |
| 1289 |
|
| 1290 |
|
| 1291 |
if ( VectorCompare( metaVerts[ i ].xyz, metaVerts[ j ].xyz ) == qfalse ) { |
| 1292 |
continue; |
| 1293 |
} |
| 1294 |
|
| 1295 |
|
| 1296 |
shadeAngle = ( shadeAngles[ i ] < shadeAngles[ j ] ? shadeAngles[ i ] : shadeAngles[ j ] ); |
| 1297 |
|
| 1298 |
|
| 1299 |
dot = DotProduct( metaVerts[ i ].normal, metaVerts[ j ].normal ); |
| 1300 |
if ( dot > 1.0 ) { |
| 1301 |
dot = 1.0; |
| 1302 |
} |
| 1303 |
else if ( dot < -1.0 ) { |
| 1304 |
dot = -1.0; |
| 1305 |
} |
| 1306 |
testAngle = acos( dot ) + THETA_EPSILON; |
| 1307 |
if ( testAngle >= shadeAngle ) { |
| 1308 |
continue; |
| 1309 |
} |
| 1310 |
|
| 1311 |
|
| 1312 |
indexes[ numVerts++ ] = j; |
| 1313 |
|
| 1314 |
|
| 1315 |
smoothed[ j >> 3 ] |= ( 1 << ( j & 7 ) ); |
| 1316 |
|
| 1317 |
|
| 1318 |
for ( k = 0; k < numVotes; k++ ) |
| 1319 |
{ |
| 1320 |
VectorSubtract( metaVerts[ j ].normal, votes[ k ], diff ); |
| 1321 |
if ( fabs( diff[ 0 ] ) < EQUAL_NORMAL_EPSILON && |
| 1322 |
fabs( diff[ 1 ] ) < EQUAL_NORMAL_EPSILON && |
| 1323 |
fabs( diff[ 2 ] ) < EQUAL_NORMAL_EPSILON ) { |
| 1324 |
break; |
| 1325 |
} |
| 1326 |
} |
| 1327 |
|
| 1328 |
|
| 1329 |
if ( k == numVotes && numVotes < MAX_SAMPLES ) { |
| 1330 |
VectorAdd( average, metaVerts[ j ].normal, average ); |
| 1331 |
VectorCopy( metaVerts[ j ].normal, votes[ numVotes ] ); |
| 1332 |
numVotes++; |
| 1333 |
} |
| 1334 |
} |
| 1335 |
|
| 1336 |
|
| 1337 |
if ( numVerts < 2 ) { |
| 1338 |
continue; |
| 1339 |
} |
| 1340 |
|
| 1341 |
|
| 1342 |
if ( VectorNormalize( average, average ) > 0 ) { |
| 1343 |
|
| 1344 |
for ( j = 0; j < numVerts; j++ ) |
| 1345 |
VectorCopy( average, metaVerts[ indexes[ j ] ].normal ); |
| 1346 |
numSmoothed++; |
| 1347 |
} |
| 1348 |
} |
| 1349 |
|
| 1350 |
|
| 1351 |
free( shadeAngles ); |
| 1352 |
free( smoothed ); |
| 1353 |
|
| 1354 |
|
| 1355 |
Sys_FPrintf( SYS_VRB, " (%d)\n", (int) ( I_FloatTime() - start ) ); |
| 1356 |
|
| 1357 |
|
| 1358 |
Sys_FPrintf( SYS_VRB, "%9d smoothed vertexes\n", numSmoothed ); |
| 1359 |
} |
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
int AddMetaVertToSurface( mapDrawSurface_t *ds, bspDrawVert_t *dv1, int *coincident ){ |
| 1370 |
int i; |
| 1371 |
bspDrawVert_t *dv2; |
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
for ( i = 0; i < ds->numVerts; i++ ) |
| 1376 |
{ |
| 1377 |
|
| 1378 |
dv2 = &ds->verts[ i ]; |
| 1379 |
|
| 1380 |
|
| 1381 |
if ( VectorCompare( dv1->xyz, dv2->xyz ) == qfalse ) { |
| 1382 |
continue; |
| 1383 |
} |
| 1384 |
if ( VectorCompare( dv1->normal, dv2->normal ) == qfalse ) { |
| 1385 |
continue; |
| 1386 |
} |
| 1387 |
|
| 1388 |
|
| 1389 |
( *coincident )++; |
| 1390 |
|
| 1391 |
|
| 1392 |
if ( dv1->st[ 0 ] != dv2->st[ 0 ] || dv1->st[ 1 ] != dv2->st[ 1 ] ) { |
| 1393 |
continue; |
| 1394 |
} |
| 1395 |
if ( dv1->color[ 0 ][ 3 ] != dv2->color[ 0 ][ 3 ] ) { |
| 1396 |
continue; |
| 1397 |
} |
| 1398 |
|
| 1399 |
|
| 1400 |
numMergedVerts++; |
| 1401 |
return i; |
| 1402 |
} |
| 1403 |
|
| 1404 |
|
| 1405 |
if ( ds->numVerts >= ( ( ds->shaderInfo->compileFlags & C_VERTEXLIT ) ? maxSurfaceVerts : maxLMSurfaceVerts ) ) { |
| 1406 |
return VERTS_EXCEEDED; |
| 1407 |
} |
| 1408 |
|
| 1409 |
|
| 1410 |
dv2 = &ds->verts[ ds->numVerts++ ]; |
| 1411 |
*dv2 = *dv1; |
| 1412 |
return ( ds->numVerts - 1 ); |
| 1413 |
} |
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
#define AXIS_SCORE 100000 |
| 1425 |
#define AXIS_MIN 100000 |
| 1426 |
#define VERT_SCORE 10000 |
| 1427 |
#define SURFACE_SCORE 1000 |
| 1428 |
#define ST_SCORE 50 |
| 1429 |
#define ST_SCORE2 ( 2 * ( ST_SCORE ) ) |
| 1430 |
|
| 1431 |
#define DEFAULT_ADEQUATE_SCORE ( (AXIS_MIN) +1 * ( VERT_SCORE ) ) |
| 1432 |
#define DEFAULT_GOOD_SCORE ( (AXIS_MIN) +2 * (VERT_SCORE) +4 * ( ST_SCORE ) ) |
| 1433 |
#define PERFECT_SCORE ( (AXIS_MIN) +3 * ( VERT_SCORE ) + (SURFACE_SCORE) +4 * ( ST_SCORE ) ) |
| 1434 |
|
| 1435 |
#define ADEQUATE_SCORE ( metaAdequateScore >= 0 ? metaAdequateScore : DEFAULT_ADEQUATE_SCORE ) |
| 1436 |
#define GOOD_SCORE ( metaGoodScore >= 0 ? metaGoodScore : DEFAULT_GOOD_SCORE ) |
| 1437 |
|
| 1438 |
static int AddMetaTriangleToSurface( mapDrawSurface_t *ds, metaTriangle_t *tri, qboolean testAdd ){ |
| 1439 |
vec3_t p; |
| 1440 |
int i, score, coincident, ai, bi, ci, oldTexRange[ 2 ]; |
| 1441 |
float lmMax; |
| 1442 |
vec3_t mins, maxs; |
| 1443 |
qboolean inTexRange; |
| 1444 |
mapDrawSurface_t old; |
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
if ( ds->numIndexes >= maxSurfaceIndexes ) { |
| 1449 |
return 0; |
| 1450 |
} |
| 1451 |
|
| 1452 |
|
| 1453 |
if ( ds->entityNum != tri->entityNum ) { |
| 1454 |
return 0; |
| 1455 |
} |
| 1456 |
if ( ds->castShadows != tri->castShadows || ds->recvShadows != tri->recvShadows ) { |
| 1457 |
return 0; |
| 1458 |
} |
| 1459 |
if ( ds->shaderInfo != tri->si || ds->fogNum != tri->fogNum || ds->sampleSize != tri->sampleSize ) { |
| 1460 |
return 0; |
| 1461 |
} |
| 1462 |
#if 0 |
| 1463 |
if ( !( ds->shaderInfo->compileFlags & C_VERTEXLIT ) && |
| 1464 |
|
| 1465 |
DotProduct( ds->lightmapAxis, tri->plane ) < 0.25f ) { |
| 1466 |
return 0; |
| 1467 |
} |
| 1468 |
#endif |
| 1469 |
|
| 1470 |
|
| 1471 |
if ( npDegrees == 0.0f && ds->shaderInfo->nonplanar == qfalse && ds->planeNum >= 0 ) { |
| 1472 |
if ( VectorCompare( mapplanes[ ds->planeNum ].normal, tri->plane ) == qfalse || mapplanes[ ds->planeNum ].dist != tri->plane[ 3 ] ) { |
| 1473 |
return 0; |
| 1474 |
} |
| 1475 |
if ( tri->planeNum >= 0 && tri->planeNum != ds->planeNum ) { |
| 1476 |
return 0; |
| 1477 |
} |
| 1478 |
} |
| 1479 |
|
| 1480 |
if ( metaMaxBBoxDistance >= 0 ) { |
| 1481 |
if ( ds->numIndexes > 0 ) { |
| 1482 |
VectorCopy( ds->mins, mins ); |
| 1483 |
VectorCopy( ds->maxs, maxs ); |
| 1484 |
mins[0] -= metaMaxBBoxDistance; |
| 1485 |
mins[1] -= metaMaxBBoxDistance; |
| 1486 |
mins[2] -= metaMaxBBoxDistance; |
| 1487 |
maxs[0] += metaMaxBBoxDistance; |
| 1488 |
maxs[1] += metaMaxBBoxDistance; |
| 1489 |
maxs[2] += metaMaxBBoxDistance; |
| 1490 |
#define CHECK_1D( mins, v, maxs ) ( ( mins ) <= ( v ) && ( v ) <= ( maxs ) ) |
| 1491 |
#define CHECK_3D( mins, v, maxs ) ( CHECK_1D( ( mins )[0], ( v )[0], ( maxs )[0] ) && CHECK_1D( ( mins )[1], ( v )[1], ( maxs )[1] ) && CHECK_1D( ( mins )[2], ( v )[2], ( maxs )[2] ) ) |
| 1492 |
VectorCopy( metaVerts[ tri->indexes[ 0 ] ].xyz, p ); |
| 1493 |
if ( !CHECK_3D( mins, p, maxs ) ) { |
| 1494 |
VectorCopy( metaVerts[ tri->indexes[ 1 ] ].xyz, p ); |
| 1495 |
if ( !CHECK_3D( mins, p, maxs ) ) { |
| 1496 |
VectorCopy( metaVerts[ tri->indexes[ 2 ] ].xyz, p ); |
| 1497 |
if ( !CHECK_3D( mins, p, maxs ) ) { |
| 1498 |
return 0; |
| 1499 |
} |
| 1500 |
} |
| 1501 |
} |
| 1502 |
#undef CHECK_3D |
| 1503 |
#undef CHECK_1D |
| 1504 |
} |
| 1505 |
} |
| 1506 |
|
| 1507 |
|
| 1508 |
score = tri->surfaceNum == ds->surfaceNum ? SURFACE_SCORE : 0; |
| 1509 |
|
| 1510 |
|
| 1511 |
if ( ( ds->shaderInfo->compileFlags & C_VERTEXLIT ) || VectorCompare( ds->lightmapAxis, tri->lightmapAxis ) ) { |
| 1512 |
score += AXIS_SCORE; |
| 1513 |
} |
| 1514 |
else{ |
| 1515 |
score += AXIS_SCORE * DotProduct( ds->lightmapAxis, tri->plane ); |
| 1516 |
} |
| 1517 |
|
| 1518 |
|
| 1519 |
memcpy( &old, ds, sizeof( *ds ) ); |
| 1520 |
|
| 1521 |
|
| 1522 |
coincident = 0; |
| 1523 |
ai = AddMetaVertToSurface( ds, &metaVerts[ tri->indexes[ 0 ] ], &coincident ); |
| 1524 |
bi = AddMetaVertToSurface( ds, &metaVerts[ tri->indexes[ 1 ] ], &coincident ); |
| 1525 |
ci = AddMetaVertToSurface( ds, &metaVerts[ tri->indexes[ 2 ] ], &coincident ); |
| 1526 |
|
| 1527 |
|
| 1528 |
if ( ai < 0 || bi < 0 || ci < 0 ) { |
| 1529 |
memcpy( ds, &old, sizeof( *ds ) ); |
| 1530 |
return 0; |
| 1531 |
} |
| 1532 |
|
| 1533 |
|
| 1534 |
score += ( coincident * VERT_SCORE ); |
| 1535 |
|
| 1536 |
|
| 1537 |
VectorCopy( ds->mins, mins ); |
| 1538 |
VectorCopy( ds->maxs, maxs ); |
| 1539 |
AddPointToBounds( metaVerts[ tri->indexes[ 0 ] ].xyz, mins, maxs ); |
| 1540 |
AddPointToBounds( metaVerts[ tri->indexes[ 1 ] ].xyz, mins, maxs ); |
| 1541 |
AddPointToBounds( metaVerts[ tri->indexes[ 2 ] ].xyz, mins, maxs ); |
| 1542 |
|
| 1543 |
|
| 1544 |
if ( !( ds->shaderInfo->compileFlags & C_VERTEXLIT ) && |
| 1545 |
ds->numIndexes > 0 && VectorLength( ds->lightmapAxis ) > 0.0f && |
| 1546 |
( VectorCompare( ds->mins, mins ) == qfalse || VectorCompare( ds->maxs, maxs ) == qfalse ) ) { |
| 1547 |
|
| 1548 |
|
| 1549 |
|
| 1550 |
lmMax = ( ds->sampleSize * ( ds->shaderInfo->lmCustomWidth - 1 ) ); |
| 1551 |
for ( i = 0; i < 3; i++ ) |
| 1552 |
{ |
| 1553 |
if ( ( maxs[ i ] - mins[ i ] ) > lmMax ) { |
| 1554 |
memcpy( ds, &old, sizeof( *ds ) ); |
| 1555 |
return 0; |
| 1556 |
} |
| 1557 |
} |
| 1558 |
} |
| 1559 |
|
| 1560 |
|
| 1561 |
oldTexRange[ 0 ] = ds->texRange[ 0 ]; |
| 1562 |
oldTexRange[ 1 ] = ds->texRange[ 1 ]; |
| 1563 |
inTexRange = CalcSurfaceTextureRange( ds ); |
| 1564 |
|
| 1565 |
if ( inTexRange == qfalse && ds->numIndexes > 0 ) { |
| 1566 |
memcpy( ds, &old, sizeof( *ds ) ); |
| 1567 |
return UNSUITABLE_TRIANGLE; |
| 1568 |
} |
| 1569 |
|
| 1570 |
|
| 1571 |
if ( ds->texRange[ 0 ] <= oldTexRange[ 0 ] ) { |
| 1572 |
score += ST_SCORE2; |
| 1573 |
} |
| 1574 |
else if ( ds->texRange[ 0 ] > oldTexRange[ 0 ] && oldTexRange[ 1 ] > oldTexRange[ 0 ] ) { |
| 1575 |
score += ST_SCORE; |
| 1576 |
} |
| 1577 |
|
| 1578 |
if ( ds->texRange[ 1 ] <= oldTexRange[ 1 ] ) { |
| 1579 |
score += ST_SCORE2; |
| 1580 |
} |
| 1581 |
else if ( ds->texRange[ 1 ] > oldTexRange[ 1 ] && oldTexRange[ 0 ] > oldTexRange[ 1 ] ) { |
| 1582 |
score += ST_SCORE; |
| 1583 |
} |
| 1584 |
|
| 1585 |
|
| 1586 |
|
| 1587 |
for ( i = 0; i < ds->numIndexes; i += 3 ) |
| 1588 |
{ |
| 1589 |
|
| 1590 |
if ( ( ai == ds->indexes[ i ] && bi == ds->indexes[ i + 1 ] && ci == ds->indexes[ i + 2 ] ) || |
| 1591 |
( bi == ds->indexes[ i ] && ci == ds->indexes[ i + 1 ] && ai == ds->indexes[ i + 2 ] ) || |
| 1592 |
( ci == ds->indexes[ i ] && ai == ds->indexes[ i + 1 ] && bi == ds->indexes[ i + 2 ] ) ) { |
| 1593 |
|
| 1594 |
memcpy( ds, &old, sizeof( *ds ) ); |
| 1595 |
tri->si = NULL; |
| 1596 |
return 0; |
| 1597 |
} |
| 1598 |
|
| 1599 |
|
| 1600 |
if ( ( ai == ds->indexes[ i ] && bi == ds->indexes[ i + 2 ] && ci == ds->indexes[ i + 1 ] ) || |
| 1601 |
( bi == ds->indexes[ i ] && ci == ds->indexes[ i + 2 ] && ai == ds->indexes[ i + 1 ] ) || |
| 1602 |
( ci == ds->indexes[ i ] && ai == ds->indexes[ i + 2 ] && bi == ds->indexes[ i + 1 ] ) ) { |
| 1603 |
|
| 1604 |
Sys_FPrintf( SYS_WRN, "WARNING: Flipped triangle: (%6.0f %6.0f %6.0f) (%6.0f %6.0f %6.0f) (%6.0f %6.0f %6.0f)\n", |
| 1605 |
ds->verts[ ai ].xyz[ 0 ], ds->verts[ ai ].xyz[ 1 ], ds->verts[ ai ].xyz[ 2 ], |
| 1606 |
ds->verts[ bi ].xyz[ 0 ], ds->verts[ bi ].xyz[ 1 ], ds->verts[ bi ].xyz[ 2 ], |
| 1607 |
ds->verts[ ci ].xyz[ 0 ], ds->verts[ ci ].xyz[ 1 ], ds->verts[ ci ].xyz[ 2 ] ); |
| 1608 |
|
| 1609 |
|
| 1610 |
memcpy( ds, &old, sizeof( *ds ) ); |
| 1611 |
tri->si = NULL; |
| 1612 |
return 0; |
| 1613 |
} |
| 1614 |
} |
| 1615 |
|
| 1616 |
|
| 1617 |
if ( ds->numIndexes < maxSurfaceIndexes ) { |
| 1618 |
ds->indexes[ ds->numIndexes++ ] = ai; |
| 1619 |
} |
| 1620 |
if ( ds->numIndexes < maxSurfaceIndexes ) { |
| 1621 |
ds->indexes[ ds->numIndexes++ ] = bi; |
| 1622 |
} |
| 1623 |
if ( ds->numIndexes < maxSurfaceIndexes ) { |
| 1624 |
ds->indexes[ ds->numIndexes++ ] = ci; |
| 1625 |
} |
| 1626 |
|
| 1627 |
|
| 1628 |
if ( ds->numIndexes >= maxSurfaceIndexes ) { |
| 1629 |
memcpy( ds, &old, sizeof( *ds ) ); |
| 1630 |
return 0; |
| 1631 |
} |
| 1632 |
|
| 1633 |
|
| 1634 |
if ( ds->numIndexes >= 3 && |
| 1635 |
( ds->indexes[ ds->numIndexes - 3 ] == ds->indexes[ ds->numIndexes - 2 ] || |
| 1636 |
ds->indexes[ ds->numIndexes - 3 ] == ds->indexes[ ds->numIndexes - 1 ] || |
| 1637 |
ds->indexes[ ds->numIndexes - 2 ] == ds->indexes[ ds->numIndexes - 1 ] ) ) { |
| 1638 |
Sys_Printf( "DEG:%d! ", ds->numVerts ); |
| 1639 |
} |
| 1640 |
|
| 1641 |
|
| 1642 |
if ( testAdd ) { |
| 1643 |
memcpy( ds, &old, sizeof( *ds ) ); |
| 1644 |
} |
| 1645 |
else |
| 1646 |
{ |
| 1647 |
|
| 1648 |
VectorCopy( mins, ds->mins ); |
| 1649 |
VectorCopy( maxs, ds->maxs ); |
| 1650 |
|
| 1651 |
|
| 1652 |
tri->si = NULL; |
| 1653 |
} |
| 1654 |
|
| 1655 |
|
| 1656 |
ds->sideRef = AllocSideRef( tri->side, ds->sideRef ); |
| 1657 |
|
| 1658 |
|
| 1659 |
return score; |
| 1660 |
} |
| 1661 |
|
| 1662 |
|
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
|
| 1669 |
static void MetaTrianglesToSurface( int numPossibles, metaTriangle_t *possibles, int *fOld, int *numAdded ){ |
| 1670 |
int i, j, f, best, score, bestScore; |
| 1671 |
metaTriangle_t *seed, *test; |
| 1672 |
mapDrawSurface_t *ds; |
| 1673 |
bspDrawVert_t *verts; |
| 1674 |
int *indexes; |
| 1675 |
qboolean added; |
| 1676 |
|
| 1677 |
|
| 1678 |
|
| 1679 |
verts = safe_malloc( sizeof( *verts ) * maxSurfaceVerts ); |
| 1680 |
indexes = safe_malloc( sizeof( *indexes ) * maxSurfaceIndexes ); |
| 1681 |
|
| 1682 |
|
| 1683 |
for ( i = 0, seed = possibles; i < numPossibles; i++, seed++ ) |
| 1684 |
{ |
| 1685 |
|
| 1686 |
if ( seed->si == NULL ) { |
| 1687 |
continue; |
| 1688 |
} |
| 1689 |
|
| 1690 |
|
| 1691 |
|
| 1692 |
|
| 1693 |
|
| 1694 |
|
| 1695 |
ds = AllocDrawSurface( SURFACE_META ); |
| 1696 |
ds->entityNum = seed->entityNum; |
| 1697 |
ds->surfaceNum = seed->surfaceNum; |
| 1698 |
ds->castShadows = seed->castShadows; |
| 1699 |
ds->recvShadows = seed->recvShadows; |
| 1700 |
|
| 1701 |
ds->shaderInfo = seed->si; |
| 1702 |
ds->planeNum = seed->planeNum; |
| 1703 |
ds->fogNum = seed->fogNum; |
| 1704 |
ds->sampleSize = seed->sampleSize; |
| 1705 |
ds->shadeAngleDegrees = seed->shadeAngleDegrees; |
| 1706 |
ds->verts = verts; |
| 1707 |
ds->indexes = indexes; |
| 1708 |
VectorCopy( seed->lightmapAxis, ds->lightmapAxis ); |
| 1709 |
ds->sideRef = AllocSideRef( seed->side, NULL ); |
| 1710 |
|
| 1711 |
ClearBounds( ds->mins, ds->maxs ); |
| 1712 |
|
| 1713 |
|
| 1714 |
|
| 1715 |
|
| 1716 |
|
| 1717 |
memset( verts, 0, sizeof( *verts ) * maxSurfaceVerts ); |
| 1718 |
memset( indexes, 0, sizeof( *indexes ) * maxSurfaceIndexes ); |
| 1719 |
|
| 1720 |
|
| 1721 |
if ( AddMetaTriangleToSurface( ds, seed, qfalse ) ) { |
| 1722 |
( *numAdded )++; |
| 1723 |
} |
| 1724 |
|
| 1725 |
|
| 1726 |
|
| 1727 |
|
| 1728 |
|
| 1729 |
|
| 1730 |
added = qtrue; |
| 1731 |
while ( added ) |
| 1732 |
{ |
| 1733 |
|
| 1734 |
f = 10 * *numAdded / numMetaTriangles; |
| 1735 |
if ( f > *fOld ) { |
| 1736 |
*fOld = f; |
| 1737 |
Sys_FPrintf( SYS_VRB, "%d...", f ); |
| 1738 |
} |
| 1739 |
|
| 1740 |
|
| 1741 |
best = -1; |
| 1742 |
bestScore = 0; |
| 1743 |
added = qfalse; |
| 1744 |
|
| 1745 |
|
| 1746 |
for ( j = i + 1, test = &possibles[ j ]; j < numPossibles; j++, test++ ) |
| 1747 |
{ |
| 1748 |
|
| 1749 |
if ( test->si == NULL ) { |
| 1750 |
continue; |
| 1751 |
} |
| 1752 |
|
| 1753 |
|
| 1754 |
score = AddMetaTriangleToSurface( ds, test, qtrue ); |
| 1755 |
if ( score > bestScore ) { |
| 1756 |
best = j; |
| 1757 |
bestScore = score; |
| 1758 |
|
| 1759 |
|
| 1760 |
if ( bestScore >= GOOD_SCORE ) { |
| 1761 |
if ( AddMetaTriangleToSurface( ds, &possibles[ best ], qfalse ) ) { |
| 1762 |
( *numAdded )++; |
| 1763 |
} |
| 1764 |
|
| 1765 |
|
| 1766 |
best = -1; |
| 1767 |
bestScore = 0; |
| 1768 |
added = qtrue; |
| 1769 |
} |
| 1770 |
} |
| 1771 |
} |
| 1772 |
|
| 1773 |
|
| 1774 |
if ( best >= 0 && bestScore > ADEQUATE_SCORE ) { |
| 1775 |
if ( AddMetaTriangleToSurface( ds, &possibles[ best ], qfalse ) ) { |
| 1776 |
( *numAdded )++; |
| 1777 |
} |
| 1778 |
|
| 1779 |
|
| 1780 |
added = qtrue; |
| 1781 |
} |
| 1782 |
} |
| 1783 |
|
| 1784 |
|
| 1785 |
ds->verts = safe_malloc( ds->numVerts * sizeof( bspDrawVert_t ) ); |
| 1786 |
memcpy( ds->verts, verts, ds->numVerts * sizeof( bspDrawVert_t ) ); |
| 1787 |
ds->indexes = safe_malloc( ds->numIndexes * sizeof( int ) ); |
| 1788 |
memcpy( ds->indexes, indexes, ds->numIndexes * sizeof( int ) ); |
| 1789 |
|
| 1790 |
|
| 1791 |
ClassifySurfaces( 1, ds ); |
| 1792 |
|
| 1793 |
|
| 1794 |
numMergedSurfaces++; |
| 1795 |
} |
| 1796 |
|
| 1797 |
|
| 1798 |
free( verts ); |
| 1799 |
free( indexes ); |
| 1800 |
} |
| 1801 |
|
| 1802 |
|
| 1803 |
|
| 1804 |
|
| 1805 |
|
| 1806 |
|
| 1807 |
|
| 1808 |
|
| 1809 |
static int CompareMetaTriangles( const void *a, const void *b ){ |
| 1810 |
int i, j, av, bv; |
| 1811 |
vec3_t aMins, bMins; |
| 1812 |
|
| 1813 |
|
| 1814 |
|
| 1815 |
if ( ( (const metaTriangle_t*) a )->si < ( (const metaTriangle_t*) b )->si ) { |
| 1816 |
return 1; |
| 1817 |
} |
| 1818 |
else if ( ( (const metaTriangle_t*) a )->si > ( (const metaTriangle_t*) b )->si ) { |
| 1819 |
return -1; |
| 1820 |
} |
| 1821 |
|
| 1822 |
|
| 1823 |
else if ( ( (const metaTriangle_t*) a )->fogNum < ( (const metaTriangle_t*) b )->fogNum ) { |
| 1824 |
return 1; |
| 1825 |
} |
| 1826 |
else if ( ( (const metaTriangle_t*) a )->fogNum > ( (const metaTriangle_t*) b )->fogNum ) { |
| 1827 |
return -1; |
| 1828 |
} |
| 1829 |
|
| 1830 |
|
| 1831 |
#if 0 |
| 1832 |
else if ( npDegrees == 0.0f && ( (const metaTriangle_t*) a )->si->nonplanar == qfalse && |
| 1833 |
( (const metaTriangle_t*) a )->planeNum >= 0 && ( (const metaTriangle_t*) a )->planeNum >= 0 ) { |
| 1834 |
if ( ( (const metaTriangle_t*) a )->plane[ 3 ] < ( (const metaTriangle_t*) b )->plane[ 3 ] ) { |
| 1835 |
return 1; |
| 1836 |
} |
| 1837 |
else if ( ( (const metaTriangle_t*) a )->plane[ 3 ] > ( (const metaTriangle_t*) b )->plane[ 3 ] ) { |
| 1838 |
return -1; |
| 1839 |
} |
| 1840 |
else if ( ( (const metaTriangle_t*) a )->plane[ 0 ] < ( (const metaTriangle_t*) b )->plane[ 0 ] ) { |
| 1841 |
return 1; |
| 1842 |
} |
| 1843 |
else if ( ( (const metaTriangle_t*) a )->plane[ 0 ] > ( (const metaTriangle_t*) b )->plane[ 0 ] ) { |
| 1844 |
return -1; |
| 1845 |
} |
| 1846 |
else if ( ( (const metaTriangle_t*) a )->plane[ 1 ] < ( (const metaTriangle_t*) b )->plane[ 1 ] ) { |
| 1847 |
return 1; |
| 1848 |
} |
| 1849 |
else if ( ( (const metaTriangle_t*) a )->plane[ 1 ] > ( (const metaTriangle_t*) b )->plane[ 1 ] ) { |
| 1850 |
return -1; |
| 1851 |
} |
| 1852 |
else if ( ( (const metaTriangle_t*) a )->plane[ 2 ] < ( (const metaTriangle_t*) b )->plane[ 2 ] ) { |
| 1853 |
return 1; |
| 1854 |
} |
| 1855 |
else if ( ( (const metaTriangle_t*) a )->plane[ 2 ] > ( (const metaTriangle_t*) b )->plane[ 2 ] ) { |
| 1856 |
return -1; |
| 1857 |
} |
| 1858 |
} |
| 1859 |
#endif |
| 1860 |
|
| 1861 |
|
| 1862 |
|
| 1863 |
|
| 1864 |
VectorSet( aMins, 999999, 999999, 999999 ); |
| 1865 |
VectorSet( bMins, 999999, 999999, 999999 ); |
| 1866 |
for ( i = 0; i < 3; i++ ) |
| 1867 |
{ |
| 1868 |
av = ( (const metaTriangle_t*) a )->indexes[ i ]; |
| 1869 |
bv = ( (const metaTriangle_t*) b )->indexes[ i ]; |
| 1870 |
for ( j = 0; j < 3; j++ ) |
| 1871 |
{ |
| 1872 |
if ( metaVerts[ av ].xyz[ j ] < aMins[ j ] ) { |
| 1873 |
aMins[ j ] = metaVerts[ av ].xyz[ j ]; |
| 1874 |
} |
| 1875 |
if ( metaVerts[ bv ].xyz[ j ] < bMins[ j ] ) { |
| 1876 |
bMins[ j ] = metaVerts[ bv ].xyz[ j ]; |
| 1877 |
} |
| 1878 |
} |
| 1879 |
} |
| 1880 |
|
| 1881 |
|
| 1882 |
for ( i = 0; i < 3; i++ ) |
| 1883 |
{ |
| 1884 |
if ( aMins[ i ] < bMins[ i ] ) { |
| 1885 |
return 1; |
| 1886 |
} |
| 1887 |
else if ( aMins[ i ] > bMins[ i ] ) { |
| 1888 |
return -1; |
| 1889 |
} |
| 1890 |
} |
| 1891 |
|
| 1892 |
|
| 1893 |
return 0; |
| 1894 |
} |
| 1895 |
|
| 1896 |
|
| 1897 |
|
| 1898 |
|
| 1899 |
|
| 1900 |
|
| 1901 |
|
| 1902 |
|
| 1903 |
void MergeMetaTriangles( void ){ |
| 1904 |
int i, j, fOld, start, numAdded; |
| 1905 |
metaTriangle_t *head, *end; |
| 1906 |
|
| 1907 |
|
| 1908 |
|
| 1909 |
if ( numMetaTriangles <= 0 ) { |
| 1910 |
return; |
| 1911 |
} |
| 1912 |
|
| 1913 |
|
| 1914 |
Sys_FPrintf( SYS_VRB, "--- MergeMetaTriangles ---\n" ); |
| 1915 |
|
| 1916 |
|
| 1917 |
qsort( metaTriangles, numMetaTriangles, sizeof( metaTriangle_t ), CompareMetaTriangles ); |
| 1918 |
|
| 1919 |
|
| 1920 |
fOld = -1; |
| 1921 |
start = I_FloatTime(); |
| 1922 |
numAdded = 0; |
| 1923 |
|
| 1924 |
|
| 1925 |
for ( i = 0, j = 0; i < numMetaTriangles; i = j ) |
| 1926 |
{ |
| 1927 |
|
| 1928 |
head = &metaTriangles[ i ]; |
| 1929 |
|
| 1930 |
|
| 1931 |
if ( head->si == NULL ) { |
| 1932 |
continue; |
| 1933 |
} |
| 1934 |
|
| 1935 |
|
| 1936 |
if ( j <= i ) { |
| 1937 |
for ( j = i + 1; j < numMetaTriangles; j++ ) |
| 1938 |
{ |
| 1939 |
|
| 1940 |
end = &metaTriangles[ j ]; |
| 1941 |
if ( head->si != end->si || head->fogNum != end->fogNum ) { |
| 1942 |
break; |
| 1943 |
} |
| 1944 |
} |
| 1945 |
} |
| 1946 |
|
| 1947 |
|
| 1948 |
MetaTrianglesToSurface( ( j - i ), head, &fOld, &numAdded ); |
| 1949 |
} |
| 1950 |
|
| 1951 |
|
| 1952 |
ClearMetaTriangles(); |
| 1953 |
|
| 1954 |
|
| 1955 |
if ( i ) { |
| 1956 |
Sys_FPrintf( SYS_VRB, " (%d)\n", (int) ( I_FloatTime() - start ) ); |
| 1957 |
} |
| 1958 |
|
| 1959 |
|
| 1960 |
Sys_FPrintf( SYS_VRB, "%9d surfaces merged\n", numMergedSurfaces ); |
| 1961 |
Sys_FPrintf( SYS_VRB, "%9d vertexes merged\n", numMergedVerts ); |
| 1962 |
} |