| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#include "tr_local.h" |
| 25 |
|
| 26 |
|
| 27 |
#define MAX_VERTS_ON_POLY 64 |
| 28 |
|
| 29 |
#define MARKER_OFFSET 0 // 1 |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
#define SIDE_FRONT 0 |
| 39 |
#define SIDE_BACK 1 |
| 40 |
#define SIDE_ON 2 |
| 41 |
static void R_ChopPolyBehindPlane( int numInPoints, vec3_t inPoints[MAX_VERTS_ON_POLY], |
| 42 |
int *numOutPoints, vec3_t outPoints[MAX_VERTS_ON_POLY], |
| 43 |
vec3_t normal, vec_t dist, vec_t epsilon) { |
| 44 |
float dists[MAX_VERTS_ON_POLY+4] = { 0 }; |
| 45 |
int sides[MAX_VERTS_ON_POLY+4] = { 0 }; |
| 46 |
int counts[3]; |
| 47 |
float dot; |
| 48 |
int i, j; |
| 49 |
float *p1, *p2, *clip; |
| 50 |
float d; |
| 51 |
|
| 52 |
|
| 53 |
if ( numInPoints >= MAX_VERTS_ON_POLY - 2 ) { |
| 54 |
*numOutPoints = 0; |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
counts[0] = counts[1] = counts[2] = 0; |
| 59 |
|
| 60 |
|
| 61 |
for ( i = 0 ; i < numInPoints ; i++ ) { |
| 62 |
dot = DotProduct( inPoints[i], normal ); |
| 63 |
dot -= dist; |
| 64 |
dists[i] = dot; |
| 65 |
if ( dot > epsilon ) { |
| 66 |
sides[i] = SIDE_FRONT; |
| 67 |
} else if ( dot < -epsilon ) { |
| 68 |
sides[i] = SIDE_BACK; |
| 69 |
} else { |
| 70 |
sides[i] = SIDE_ON; |
| 71 |
} |
| 72 |
counts[sides[i]]++; |
| 73 |
} |
| 74 |
sides[i] = sides[0]; |
| 75 |
dists[i] = dists[0]; |
| 76 |
|
| 77 |
*numOutPoints = 0; |
| 78 |
|
| 79 |
if ( !counts[0] ) { |
| 80 |
return; |
| 81 |
} |
| 82 |
if ( !counts[1] ) { |
| 83 |
*numOutPoints = numInPoints; |
| 84 |
Com_Memcpy( outPoints, inPoints, numInPoints * sizeof(vec3_t) ); |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
for ( i = 0 ; i < numInPoints ; i++ ) { |
| 89 |
p1 = inPoints[i]; |
| 90 |
clip = outPoints[ *numOutPoints ]; |
| 91 |
|
| 92 |
if ( sides[i] == SIDE_ON ) { |
| 93 |
VectorCopy( p1, clip ); |
| 94 |
(*numOutPoints)++; |
| 95 |
continue; |
| 96 |
} |
| 97 |
|
| 98 |
if ( sides[i] == SIDE_FRONT ) { |
| 99 |
VectorCopy( p1, clip ); |
| 100 |
(*numOutPoints)++; |
| 101 |
clip = outPoints[ *numOutPoints ]; |
| 102 |
} |
| 103 |
|
| 104 |
if ( sides[i+1] == SIDE_ON || sides[i+1] == sides[i] ) { |
| 105 |
continue; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
p2 = inPoints[ (i+1) % numInPoints ]; |
| 110 |
|
| 111 |
d = dists[i] - dists[i+1]; |
| 112 |
if ( d == 0 ) { |
| 113 |
dot = 0; |
| 114 |
} else { |
| 115 |
dot = dists[i] / d; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
for (j=0 ; j<3 ; j++) { |
| 121 |
clip[j] = p1[j] + dot * ( p2[j] - p1[j] ); |
| 122 |
} |
| 123 |
|
| 124 |
(*numOutPoints)++; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
void R_BoxSurfaces_r(mnode_t *node, vec3_t mins, vec3_t maxs, surfaceType_t **list, int listsize, int *listlength, vec3_t dir) { |
| 135 |
|
| 136 |
int s, c; |
| 137 |
msurface_t *surf, **mark; |
| 138 |
|
| 139 |
|
| 140 |
while ( node->contents == -1 ) { |
| 141 |
s = BoxOnPlaneSide( mins, maxs, node->plane ); |
| 142 |
if (s == 1) { |
| 143 |
node = node->children[0]; |
| 144 |
} else if (s == 2) { |
| 145 |
node = node->children[1]; |
| 146 |
} else { |
| 147 |
R_BoxSurfaces_r(node->children[0], mins, maxs, list, listsize, listlength, dir); |
| 148 |
node = node->children[1]; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
mark = node->firstmarksurface; |
| 154 |
c = node->nummarksurfaces; |
| 155 |
while (c--) { |
| 156 |
|
| 157 |
if (*listlength >= listsize) break; |
| 158 |
|
| 159 |
surf = *mark; |
| 160 |
|
| 161 |
if ( ( surf->shader->surfaceFlags & ( SURF_NOIMPACT | SURF_NOMARKS ) ) |
| 162 |
|| ( surf->shader->contentFlags & CONTENTS_FOG ) ) { |
| 163 |
surf->viewCount = tr.viewCount; |
| 164 |
} |
| 165 |
|
| 166 |
else if (*(surf->data) == SF_FACE) { |
| 167 |
|
| 168 |
s = BoxOnPlaneSide( mins, maxs, &(( srfSurfaceFace_t * ) surf->data)->plane ); |
| 169 |
if (s == 1 || s == 2) { |
| 170 |
surf->viewCount = tr.viewCount; |
| 171 |
} else if (DotProduct((( srfSurfaceFace_t * ) surf->data)->plane.normal, dir) > -0.5) { |
| 172 |
|
| 173 |
surf->viewCount = tr.viewCount; |
| 174 |
} |
| 175 |
} |
| 176 |
else if (*(surfaceType_t *) (surf->data) != SF_GRID && |
| 177 |
*(surfaceType_t *) (surf->data) != SF_TRIANGLES) |
| 178 |
surf->viewCount = tr.viewCount; |
| 179 |
|
| 180 |
|
| 181 |
if (surf->viewCount != tr.viewCount) { |
| 182 |
surf->viewCount = tr.viewCount; |
| 183 |
list[*listlength] = (surfaceType_t *) surf->data; |
| 184 |
(*listlength)++; |
| 185 |
} |
| 186 |
mark++; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
void R_AddMarkFragments(int numClipPoints, vec3_t clipPoints[2][MAX_VERTS_ON_POLY], |
| 197 |
int numPlanes, vec3_t *normals, float *dists, |
| 198 |
int maxPoints, vec3_t pointBuffer, |
| 199 |
int maxFragments, markFragment_t *fragmentBuffer, |
| 200 |
int *returnedPoints, int *returnedFragments, |
| 201 |
vec3_t mins, vec3_t maxs) { |
| 202 |
int pingPong, i; |
| 203 |
markFragment_t *mf; |
| 204 |
|
| 205 |
|
| 206 |
pingPong = 0; |
| 207 |
|
| 208 |
for ( i = 0 ; i < numPlanes ; i++ ) { |
| 209 |
|
| 210 |
R_ChopPolyBehindPlane( numClipPoints, clipPoints[pingPong], |
| 211 |
&numClipPoints, clipPoints[!pingPong], |
| 212 |
normals[i], dists[i], 0.5 ); |
| 213 |
pingPong ^= 1; |
| 214 |
if ( numClipPoints == 0 ) { |
| 215 |
break; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
if ( numClipPoints == 0 ) { |
| 220 |
return; |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
if ( numClipPoints + (*returnedPoints) > maxPoints ) { |
| 225 |
return; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
mf = fragmentBuffer + (*returnedFragments); |
| 241 |
mf->firstPoint = (*returnedPoints); |
| 242 |
mf->numPoints = numClipPoints; |
| 243 |
Com_Memcpy( pointBuffer + (*returnedPoints) * 3, clipPoints[pingPong], numClipPoints * sizeof(vec3_t) ); |
| 244 |
|
| 245 |
(*returnedPoints) += numClipPoints; |
| 246 |
(*returnedFragments)++; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
int R_MarkFragments( int numPoints, const vec3_t *points, const vec3_t projection, |
| 256 |
int maxPoints, vec3_t pointBuffer, int maxFragments, markFragment_t *fragmentBuffer ) { |
| 257 |
int numsurfaces, numPlanes; |
| 258 |
int i, j, k, m, n; |
| 259 |
surfaceType_t *surfaces[64]; |
| 260 |
vec3_t mins, maxs; |
| 261 |
int returnedFragments; |
| 262 |
int returnedPoints; |
| 263 |
vec3_t normals[MAX_VERTS_ON_POLY+2]; |
| 264 |
float dists[MAX_VERTS_ON_POLY+2]; |
| 265 |
vec3_t clipPoints[2][MAX_VERTS_ON_POLY]; |
| 266 |
int numClipPoints; |
| 267 |
float *v; |
| 268 |
srfGridMesh_t *cv; |
| 269 |
drawVert_t *dv; |
| 270 |
vec3_t normal; |
| 271 |
vec3_t projectionDir; |
| 272 |
vec3_t v1, v2; |
| 273 |
int *indexes; |
| 274 |
|
| 275 |
if (numPoints <= 0) { |
| 276 |
return 0; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
tr.viewCount++; |
| 281 |
|
| 282 |
|
| 283 |
VectorNormalize2( projection, projectionDir ); |
| 284 |
|
| 285 |
ClearBounds( mins, maxs ); |
| 286 |
for ( i = 0 ; i < numPoints ; i++ ) { |
| 287 |
vec3_t temp; |
| 288 |
|
| 289 |
AddPointToBounds( points[i], mins, maxs ); |
| 290 |
VectorAdd( points[i], projection, temp ); |
| 291 |
AddPointToBounds( temp, mins, maxs ); |
| 292 |
|
| 293 |
VectorMA( points[i], -20, projectionDir, temp ); |
| 294 |
AddPointToBounds( temp, mins, maxs ); |
| 295 |
} |
| 296 |
|
| 297 |
if (numPoints > MAX_VERTS_ON_POLY) numPoints = MAX_VERTS_ON_POLY; |
| 298 |
|
| 299 |
for ( i = 0 ; i < numPoints ; i++ ) { |
| 300 |
VectorSubtract(points[(i+1)%numPoints], points[i], v1); |
| 301 |
VectorAdd(points[i], projection, v2); |
| 302 |
VectorSubtract(points[i], v2, v2); |
| 303 |
CrossProduct(v1, v2, normals[i]); |
| 304 |
VectorNormalizeFast(normals[i]); |
| 305 |
dists[i] = DotProduct(normals[i], points[i]); |
| 306 |
} |
| 307 |
|
| 308 |
VectorCopy(projectionDir, normals[numPoints]); |
| 309 |
dists[numPoints] = DotProduct(normals[numPoints], points[0]) - 32; |
| 310 |
VectorCopy(projectionDir, normals[numPoints+1]); |
| 311 |
VectorInverse(normals[numPoints+1]); |
| 312 |
dists[numPoints+1] = DotProduct(normals[numPoints+1], points[0]) - 20; |
| 313 |
numPlanes = numPoints + 2; |
| 314 |
|
| 315 |
numsurfaces = 0; |
| 316 |
R_BoxSurfaces_r(tr.world->nodes, mins, maxs, surfaces, 64, &numsurfaces, projectionDir); |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
returnedPoints = 0; |
| 321 |
returnedFragments = 0; |
| 322 |
|
| 323 |
for ( i = 0 ; i < numsurfaces ; i++ ) { |
| 324 |
|
| 325 |
if (*surfaces[i] == SF_GRID) { |
| 326 |
|
| 327 |
cv = (srfGridMesh_t *) surfaces[i]; |
| 328 |
for ( m = 0 ; m < cv->height - 1 ; m++ ) { |
| 329 |
for ( n = 0 ; n < cv->width - 1 ; n++ ) { |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
numClipPoints = 3; |
| 352 |
|
| 353 |
dv = cv->verts + m * cv->width + n; |
| 354 |
|
| 355 |
VectorCopy(dv[0].xyz, clipPoints[0][0]); |
| 356 |
VectorMA(clipPoints[0][0], MARKER_OFFSET, dv[0].normal, clipPoints[0][0]); |
| 357 |
VectorCopy(dv[cv->width].xyz, clipPoints[0][1]); |
| 358 |
VectorMA(clipPoints[0][1], MARKER_OFFSET, dv[cv->width].normal, clipPoints[0][1]); |
| 359 |
VectorCopy(dv[1].xyz, clipPoints[0][2]); |
| 360 |
VectorMA(clipPoints[0][2], MARKER_OFFSET, dv[1].normal, clipPoints[0][2]); |
| 361 |
|
| 362 |
VectorSubtract(clipPoints[0][0], clipPoints[0][1], v1); |
| 363 |
VectorSubtract(clipPoints[0][2], clipPoints[0][1], v2); |
| 364 |
CrossProduct(v1, v2, normal); |
| 365 |
VectorNormalizeFast(normal); |
| 366 |
if (DotProduct(normal, projectionDir) < -0.1) { |
| 367 |
|
| 368 |
R_AddMarkFragments(numClipPoints, clipPoints, |
| 369 |
numPlanes, normals, dists, |
| 370 |
maxPoints, pointBuffer, |
| 371 |
maxFragments, fragmentBuffer, |
| 372 |
&returnedPoints, &returnedFragments, mins, maxs); |
| 373 |
|
| 374 |
if ( returnedFragments == maxFragments ) { |
| 375 |
return returnedFragments; |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
VectorCopy(dv[1].xyz, clipPoints[0][0]); |
| 380 |
VectorMA(clipPoints[0][0], MARKER_OFFSET, dv[1].normal, clipPoints[0][0]); |
| 381 |
VectorCopy(dv[cv->width].xyz, clipPoints[0][1]); |
| 382 |
VectorMA(clipPoints[0][1], MARKER_OFFSET, dv[cv->width].normal, clipPoints[0][1]); |
| 383 |
VectorCopy(dv[cv->width+1].xyz, clipPoints[0][2]); |
| 384 |
VectorMA(clipPoints[0][2], MARKER_OFFSET, dv[cv->width+1].normal, clipPoints[0][2]); |
| 385 |
|
| 386 |
VectorSubtract(clipPoints[0][0], clipPoints[0][1], v1); |
| 387 |
VectorSubtract(clipPoints[0][2], clipPoints[0][1], v2); |
| 388 |
CrossProduct(v1, v2, normal); |
| 389 |
VectorNormalizeFast(normal); |
| 390 |
if (DotProduct(normal, projectionDir) < -0.05) { |
| 391 |
|
| 392 |
R_AddMarkFragments(numClipPoints, clipPoints, |
| 393 |
numPlanes, normals, dists, |
| 394 |
maxPoints, pointBuffer, |
| 395 |
maxFragments, fragmentBuffer, |
| 396 |
&returnedPoints, &returnedFragments, mins, maxs); |
| 397 |
|
| 398 |
if ( returnedFragments == maxFragments ) { |
| 399 |
return returnedFragments; |
| 400 |
} |
| 401 |
} |
| 402 |
} |
| 403 |
} |
| 404 |
} |
| 405 |
else if (*surfaces[i] == SF_FACE) { |
| 406 |
|
| 407 |
srfSurfaceFace_t *surf = ( srfSurfaceFace_t * ) surfaces[i]; |
| 408 |
|
| 409 |
|
| 410 |
if (DotProduct(surf->plane.normal, projectionDir) > -0.5) { |
| 411 |
continue; |
| 412 |
} |
| 413 |
|
| 414 |
indexes = (int *)( (byte *)surf + surf->ofsIndices ); |
| 415 |
for ( k = 0 ; k < surf->numIndices ; k += 3 ) { |
| 416 |
for ( j = 0 ; j < 3 ; j++ ) { |
| 417 |
v = &surf->points[0][0] + VERTEXSIZE * indexes[k+j]; |
| 418 |
VectorMA( v, MARKER_OFFSET, surf->plane.normal, clipPoints[0][j] ); |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
R_AddMarkFragments( 3 , clipPoints, |
| 423 |
numPlanes, normals, dists, |
| 424 |
maxPoints, pointBuffer, |
| 425 |
maxFragments, fragmentBuffer, |
| 426 |
&returnedPoints, &returnedFragments, mins, maxs); |
| 427 |
if ( returnedFragments == maxFragments ) { |
| 428 |
return returnedFragments; |
| 429 |
} |
| 430 |
} |
| 431 |
} |
| 432 |
else if(*surfaces[i] == SF_TRIANGLES && r_marksOnTriangleMeshes->integer) { |
| 433 |
|
| 434 |
srfTriangles_t *surf = (srfTriangles_t *) surfaces[i]; |
| 435 |
|
| 436 |
for (k = 0; k < surf->numIndexes; k += 3) |
| 437 |
{ |
| 438 |
for(j = 0; j < 3; j++) |
| 439 |
{ |
| 440 |
v = surf->verts[surf->indexes[k + j]].xyz; |
| 441 |
VectorMA(v, MARKER_OFFSET, surf->verts[surf->indexes[k + j]].normal, clipPoints[0][j]); |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
R_AddMarkFragments(3, clipPoints, |
| 446 |
numPlanes, normals, dists, |
| 447 |
maxPoints, pointBuffer, |
| 448 |
maxFragments, fragmentBuffer, &returnedPoints, &returnedFragments, mins, maxs); |
| 449 |
if(returnedFragments == maxFragments) |
| 450 |
{ |
| 451 |
return returnedFragments; |
| 452 |
} |
| 453 |
} |
| 454 |
} |
| 455 |
} |
| 456 |
return returnedFragments; |
| 457 |
} |
| 458 |
|