| 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 DECALS_C |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
#include "q3map2.h" |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
#define MAX_PROJECTORS 1024 |
| 42 |
|
| 43 |
typedef struct decalProjector_s |
| 44 |
{ |
| 45 |
shaderInfo_t *si; |
| 46 |
vec3_t mins, maxs; |
| 47 |
vec3_t center; |
| 48 |
float radius, radius2; |
| 49 |
int numPlanes; |
| 50 |
vec4_t planes[ 6 ]; |
| 51 |
vec4_t texMat[ 2 ]; |
| 52 |
} |
| 53 |
decalProjector_t; |
| 54 |
|
| 55 |
static int numProjectors = 0; |
| 56 |
static decalProjector_t projectors[ MAX_PROJECTORS ]; |
| 57 |
|
| 58 |
static int numDecalSurfaces = 0; |
| 59 |
|
| 60 |
static vec3_t entityOrigin; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
typedef double dvec_t; |
| 70 |
typedef dvec_t dvec3_t[ 3 ]; |
| 71 |
|
| 72 |
dvec_t DVectorNormalize( dvec3_t in, dvec3_t out ){ |
| 73 |
dvec_t len, ilen; |
| 74 |
|
| 75 |
|
| 76 |
len = (dvec_t) sqrt( in[ 0 ] * in[ 0 ] + in[ 1 ] * in[ 1 ] + in[ 2 ] * in[ 2 ] ); |
| 77 |
if ( len == 0.0 ) { |
| 78 |
VectorClear( out ); |
| 79 |
return 0.0; |
| 80 |
} |
| 81 |
|
| 82 |
ilen = 1.0 / len; |
| 83 |
out[ 0 ] = in[ 0 ] * ilen; |
| 84 |
out[ 1 ] = in[ 1 ] * ilen; |
| 85 |
out[ 2 ] = in[ 2 ] * ilen; |
| 86 |
|
| 87 |
return len; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
#define Vector2Subtract( a,b,c ) ( ( c )[ 0 ] = ( a )[ 0 ] - ( b )[ 0 ], ( c )[ 1 ] = ( a )[ 1 ] - ( b )[ 1 ] ) |
| 99 |
|
| 100 |
static qboolean MakeTextureMatrix( decalProjector_t *dp, vec4_t projection, bspDrawVert_t *a, bspDrawVert_t *b, bspDrawVert_t *c ){ |
| 101 |
int i, j; |
| 102 |
double bb, s, t, d; |
| 103 |
dvec3_t pa, pb, pc; |
| 104 |
dvec3_t bary, xyz; |
| 105 |
dvec3_t vecs[ 3 ], axis[ 3 ], lengths; |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
d = DotProduct( a->xyz, projection ) - projection[ 3 ]; |
| 110 |
VectorMA( a->xyz, -d, projection, pa ); |
| 111 |
d = DotProduct( b->xyz, projection ) - projection[ 3 ]; |
| 112 |
VectorMA( b->xyz, -d, projection, pb ); |
| 113 |
d = DotProduct( c->xyz, projection ) - projection[ 3 ]; |
| 114 |
VectorMA( c->xyz, -d, projection, pc ); |
| 115 |
|
| 116 |
|
| 117 |
#if 1 |
| 118 |
{ |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
bb = ( b->st[ 0 ] - a->st[ 0 ] ) * ( c->st[ 1 ] - a->st[ 1 ] ) - ( c->st[ 0 ] - a->st[ 0 ] ) * ( b->st[ 1 ] - a->st[ 1 ] ); |
| 123 |
if ( fabs( bb ) < 0.00000001 ) { |
| 124 |
return qfalse; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
#if 0 |
| 129 |
s = 0.0; |
| 130 |
t = 0.0; |
| 131 |
bary[ 0 ] = ( ( b->st[ 0 ] - s ) * ( c->st[ 1 ] - t ) - ( c->st[ 0 ] - s ) * ( b->st[ 1 ] - t ) ) / bb; |
| 132 |
bary[ 1 ] = ( ( c->st[ 0 ] - s ) * ( a->st[ 1 ] - t ) - ( a->st[ 0 ] - s ) * ( c->st[ 1 ] - t ) ) / bb; |
| 133 |
bary[ 2 ] = ( ( a->st[ 0 ] - s ) * ( b->st[ 1 ] - t ) - ( b->st[ 0 ] - s ) * ( a->st[ 1 ] - t ) ) / bb; |
| 134 |
|
| 135 |
origin[ 0 ] = bary[ 0 ] * pa[ 0 ] + bary[ 1 ] * pb[ 0 ] + bary[ 2 ] * pc[ 0 ]; |
| 136 |
origin[ 1 ] = bary[ 0 ] * pa[ 1 ] + bary[ 1 ] * pb[ 1 ] + bary[ 2 ] * pc[ 1 ]; |
| 137 |
origin[ 2 ] = bary[ 0 ] * pa[ 2 ] + bary[ 1 ] * pb[ 2 ] + bary[ 2 ] * pc[ 2 ]; |
| 138 |
#endif |
| 139 |
|
| 140 |
|
| 141 |
s = a->st[ 0 ] + 1.0; |
| 142 |
t = a->st[ 1 ] + 0.0; |
| 143 |
bary[ 0 ] = ( ( b->st[ 0 ] - s ) * ( c->st[ 1 ] - t ) - ( c->st[ 0 ] - s ) * ( b->st[ 1 ] - t ) ) / bb; |
| 144 |
bary[ 1 ] = ( ( c->st[ 0 ] - s ) * ( a->st[ 1 ] - t ) - ( a->st[ 0 ] - s ) * ( c->st[ 1 ] - t ) ) / bb; |
| 145 |
bary[ 2 ] = ( ( a->st[ 0 ] - s ) * ( b->st[ 1 ] - t ) - ( b->st[ 0 ] - s ) * ( a->st[ 1 ] - t ) ) / bb; |
| 146 |
|
| 147 |
xyz[ 0 ] = bary[ 0 ] * pa[ 0 ] + bary[ 1 ] * pb[ 0 ] + bary[ 2 ] * pc[ 0 ]; |
| 148 |
xyz[ 1 ] = bary[ 0 ] * pa[ 1 ] + bary[ 1 ] * pb[ 1 ] + bary[ 2 ] * pc[ 1 ]; |
| 149 |
xyz[ 2 ] = bary[ 0 ] * pa[ 2 ] + bary[ 1 ] * pb[ 2 ] + bary[ 2 ] * pc[ 2 ]; |
| 150 |
|
| 151 |
|
| 152 |
VectorSubtract( xyz, pa, vecs[ 0 ] ); |
| 153 |
|
| 154 |
|
| 155 |
s = a->st[ 0 ] + 0.0; |
| 156 |
t = a->st[ 1 ] + 1.0; |
| 157 |
bary[ 0 ] = ( ( b->st[ 0 ] - s ) * ( c->st[ 1 ] - t ) - ( c->st[ 0 ] - s ) * ( b->st[ 1 ] - t ) ) / bb; |
| 158 |
bary[ 1 ] = ( ( c->st[ 0 ] - s ) * ( a->st[ 1 ] - t ) - ( a->st[ 0 ] - s ) * ( c->st[ 1 ] - t ) ) / bb; |
| 159 |
bary[ 2 ] = ( ( a->st[ 0 ] - s ) * ( b->st[ 1 ] - t ) - ( b->st[ 0 ] - s ) * ( a->st[ 1 ] - t ) ) / bb; |
| 160 |
|
| 161 |
xyz[ 0 ] = bary[ 0 ] * pa[ 0 ] + bary[ 1 ] * pb[ 0 ] + bary[ 2 ] * pc[ 0 ]; |
| 162 |
xyz[ 1 ] = bary[ 0 ] * pa[ 1 ] + bary[ 1 ] * pb[ 1 ] + bary[ 2 ] * pc[ 1 ]; |
| 163 |
xyz[ 2 ] = bary[ 0 ] * pa[ 2 ] + bary[ 1 ] * pb[ 2 ] + bary[ 2 ] * pc[ 2 ]; |
| 164 |
|
| 165 |
|
| 166 |
VectorSubtract( xyz, pa, vecs[ 1 ] ); |
| 167 |
|
| 168 |
|
| 169 |
VectorScale( projection, -1.0, vecs[ 2 ] ); |
| 170 |
|
| 171 |
|
| 172 |
for ( i = 0; i < 3; i++ ) |
| 173 |
lengths[ i ] = DVectorNormalize( vecs[ i ], axis[ i ] ); |
| 174 |
for ( i = 0; i < 2; i++ ) |
| 175 |
for ( j = 0; j < 3; j++ ) |
| 176 |
dp->texMat[ i ][ j ] = lengths[ i ] > 0.0 ? ( axis[ i ][ j ] / lengths[ i ] ) : 0.0; |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
dp->texMat[ 0 ][ 3 ] = a->st[ 0 ] - DotProduct( a->xyz, dp->texMat[ 0 ] ); |
| 182 |
dp->texMat[ 1 ][ 3 ] = a->st[ 1 ] - DotProduct( a->xyz, dp->texMat[ 1 ] ); |
| 183 |
} |
| 184 |
#else |
| 185 |
{ |
| 186 |
int k; |
| 187 |
dvec3_t origin, deltas[ 3 ]; |
| 188 |
double texDeltas[ 3 ][ 2 ]; |
| 189 |
double delta, texDelta; |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
VectorSubtract( pa, pb, deltas[ 0 ] ); |
| 196 |
VectorSubtract( pa, pc, deltas[ 1 ] ); |
| 197 |
VectorSubtract( pb, pc, deltas[ 2 ] ); |
| 198 |
Vector2Subtract( a->st, b->st, texDeltas[ 0 ] ); |
| 199 |
Vector2Subtract( a->st, c->st, texDeltas[ 1 ] ); |
| 200 |
Vector2Subtract( b->st, c->st, texDeltas[ 2 ] ); |
| 201 |
|
| 202 |
|
| 203 |
for ( i = 0; i < 2; i++ ) |
| 204 |
{ |
| 205 |
|
| 206 |
for ( j = 0; j < 3; j++ ) |
| 207 |
{ |
| 208 |
|
| 209 |
delta = 0.0; |
| 210 |
texDelta = 0.0; |
| 211 |
|
| 212 |
|
| 213 |
for ( k = 0; k < 3; k++ ) |
| 214 |
{ |
| 215 |
if ( fabs( deltas[ k ][ j ] ) > delta && |
| 216 |
fabs( texDeltas[ k ][ i ] ) > texDelta ) { |
| 217 |
delta = deltas[ k ][ j ]; |
| 218 |
texDelta = texDeltas[ k ][ i ]; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
if ( fabs( delta ) > 0.0 ) { |
| 224 |
dp->texMat[ i ][ j ] = texDelta / delta; |
| 225 |
} |
| 226 |
else{ |
| 227 |
dp->texMat[ i ][ j ] = 0.0; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
dp->texMat[ i ][ 3 ] = a->st[ i ] - DotProduct( pa, dp->texMat[ i ] ); |
| 233 |
} |
| 234 |
} |
| 235 |
#endif |
| 236 |
|
| 237 |
|
| 238 |
#if 1 |
| 239 |
Sys_Printf( "Mat: [ %f %f %f %f ] [ %f %f %f %f ] Theta: %f (%f)\n", |
| 240 |
dp->texMat[ 0 ][ 0 ], dp->texMat[ 0 ][ 1 ], dp->texMat[ 0 ][ 2 ], dp->texMat[ 0 ][ 3 ], |
| 241 |
dp->texMat[ 1 ][ 0 ], dp->texMat[ 1 ][ 1 ], dp->texMat[ 1 ][ 2 ], dp->texMat[ 1 ][ 3 ], |
| 242 |
RAD2DEG( acos( DotProduct( dp->texMat[ 0 ], dp->texMat[ 1 ] ) ) ), |
| 243 |
RAD2DEG( acos( DotProduct( axis[ 0 ], axis[ 1 ] ) ) ) ); |
| 244 |
|
| 245 |
Sys_Printf( "XYZ: %f %f %f ST: %f %f ST(t): %f %f\n", |
| 246 |
a->xyz[ 0 ], a->xyz[ 1 ], a->xyz[ 2 ], |
| 247 |
a->st[ 0 ], a->st[ 1 ], |
| 248 |
DotProduct( a->xyz, dp->texMat[ 0 ] ) + dp->texMat[ 0 ][ 3 ], DotProduct( a->xyz, dp->texMat[ 1 ] ) + dp->texMat[ 1 ][ 3 ] ); |
| 249 |
#endif |
| 250 |
|
| 251 |
|
| 252 |
s = DotProduct( a->xyz, dp->texMat[ 0 ] ) + dp->texMat[ 0 ][ 3 ]; |
| 253 |
t = DotProduct( a->xyz, dp->texMat[ 1 ] ) + dp->texMat[ 1 ][ 3 ]; |
| 254 |
if ( fabs( s - a->st[ 0 ] ) > 0.01 || fabs( t - a->st[ 1 ] ) > 0.01 ) { |
| 255 |
Sys_Printf( "Bad texture matrix! (A) (%f, %f) != (%f, %f)\n", |
| 256 |
s, t, a->st[ 0 ], a->st[ 1 ] ); |
| 257 |
|
| 258 |
} |
| 259 |
s = DotProduct( b->xyz, dp->texMat[ 0 ] ) + dp->texMat[ 0 ][ 3 ]; |
| 260 |
t = DotProduct( b->xyz, dp->texMat[ 1 ] ) + dp->texMat[ 1 ][ 3 ]; |
| 261 |
if ( fabs( s - b->st[ 0 ] ) > 0.01 || fabs( t - b->st[ 1 ] ) > 0.01 ) { |
| 262 |
Sys_Printf( "Bad texture matrix! (B) (%f, %f) != (%f, %f)\n", |
| 263 |
s, t, b->st[ 0 ], b->st[ 1 ] ); |
| 264 |
|
| 265 |
} |
| 266 |
s = DotProduct( c->xyz, dp->texMat[ 0 ] ) + dp->texMat[ 0 ][ 3 ]; |
| 267 |
t = DotProduct( c->xyz, dp->texMat[ 1 ] ) + dp->texMat[ 1 ][ 3 ]; |
| 268 |
if ( fabs( s - c->st[ 0 ] ) > 0.01 || fabs( t - c->st[ 1 ] ) > 0.01 ) { |
| 269 |
Sys_Printf( "Bad texture matrix! (C) (%f, %f) != (%f, %f)\n", |
| 270 |
s, t, c->st[ 0 ], c->st[ 1 ] ); |
| 271 |
|
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
return qtrue; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
static void TransformDecalProjector( decalProjector_t *in, vec3_t axis[ 3 ], vec3_t origin, decalProjector_t *out ){ |
| 287 |
int i; |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
out->si = in->si; |
| 292 |
out->numPlanes = in->numPlanes; |
| 293 |
|
| 294 |
|
| 295 |
VectorSubtract( in->mins, origin, out->mins ); |
| 296 |
VectorSubtract( in->maxs, origin, out->maxs ); |
| 297 |
VectorSubtract( in->center, origin, out->center ); |
| 298 |
out->radius = in->radius; |
| 299 |
out->radius2 = in->radius2; |
| 300 |
|
| 301 |
|
| 302 |
for ( i = 0; i < in->numPlanes; i++ ) |
| 303 |
{ |
| 304 |
out->planes[ i ][ 0 ] = DotProduct( in->planes[ i ], axis[ 0 ] ); |
| 305 |
out->planes[ i ][ 1 ] = DotProduct( in->planes[ i ], axis[ 1 ] ); |
| 306 |
out->planes[ i ][ 2 ] = DotProduct( in->planes[ i ], axis[ 2 ] ); |
| 307 |
out->planes[ i ][ 3 ] = in->planes[ i ][ 3 ] - DotProduct( out->planes[ i ], origin ); |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
for ( i = 0; i < 2; i++ ) |
| 312 |
{ |
| 313 |
out->texMat[ i ][ 0 ] = DotProduct( in->texMat[ i ], axis[ 0 ] ); |
| 314 |
out->texMat[ i ][ 1 ] = DotProduct( in->texMat[ i ], axis[ 1 ] ); |
| 315 |
out->texMat[ i ][ 2 ] = DotProduct( in->texMat[ i ], axis[ 2 ] ); |
| 316 |
out->texMat[ i ][ 3 ] = in->texMat[ i ][ 3 ] + DotProduct( out->texMat[ i ], origin ); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
static int MakeDecalProjector( shaderInfo_t *si, vec4_t projection, float distance, int numVerts, bspDrawVert_t **dv ){ |
| 328 |
int i, j; |
| 329 |
decalProjector_t *dp; |
| 330 |
vec3_t xyz; |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
if ( numVerts != 3 && numVerts != 4 ) { |
| 335 |
return -1; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
if ( numProjectors >= MAX_PROJECTORS ) { |
| 340 |
Sys_FPrintf( SYS_WRN, "WARNING: MAX_PROJECTORS (%d) exceeded, no more decal projectors available.\n", MAX_PROJECTORS ); |
| 341 |
return -2; |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
dp = &projectors[ numProjectors ]; |
| 346 |
memset( dp, 0, sizeof( *dp ) ); |
| 347 |
|
| 348 |
|
| 349 |
dp->si = si; |
| 350 |
dp->numPlanes = numVerts + 2; |
| 351 |
|
| 352 |
|
| 353 |
if ( !MakeTextureMatrix( dp, projection, dv[ 0 ], dv[ 1 ], dv[ 2 ] ) ) { |
| 354 |
return -1; |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
ClearBounds( dp->mins, dp->maxs ); |
| 359 |
for ( i = 0; i < numVerts; i++ ) |
| 360 |
{ |
| 361 |
AddPointToBounds( dv[ i ]->xyz, dp->mins, dp->maxs ); |
| 362 |
VectorMA( dv[ i ]->xyz, distance, projection, xyz ); |
| 363 |
AddPointToBounds( xyz, dp->mins, dp->maxs ); |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
VectorAdd( dp->mins, dp->maxs, dp->center ); |
| 368 |
VectorScale( dp->center, 0.5f, dp->center ); |
| 369 |
VectorSubtract( dp->maxs, dp->center, xyz ); |
| 370 |
dp->radius = VectorLength( xyz ); |
| 371 |
dp->radius2 = dp->radius * dp->radius; |
| 372 |
|
| 373 |
|
| 374 |
if ( !PlaneFromPoints( dp->planes[ 0 ], dv[ 0 ]->xyz, dv[ 1 ]->xyz, dv[ 2 ]->xyz ) ) { |
| 375 |
return -1; |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
VectorSubtract( vec3_origin, dp->planes[ 0 ], dp->planes[ 1 ] ); |
| 380 |
VectorMA( dv[ 0 ]->xyz, distance, projection, xyz ); |
| 381 |
dp->planes[ 1 ][ 3 ] = DotProduct( xyz, dp->planes[ 1 ] ); |
| 382 |
|
| 383 |
|
| 384 |
for ( i = 0; i < numVerts; i++ ) |
| 385 |
{ |
| 386 |
j = ( i + 1 ) % numVerts; |
| 387 |
VectorMA( dv[ i ]->xyz, distance, projection, xyz ); |
| 388 |
if ( !PlaneFromPoints( dp->planes[ i + 2 ], dv[ j ]->xyz, dv[ i ]->xyz, xyz ) ) { |
| 389 |
return -1; |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
numProjectors++; |
| 395 |
return numProjectors - 1; |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
#define PLANAR_EPSILON 0.5f |
| 406 |
|
| 407 |
void ProcessDecals( void ){ |
| 408 |
int i, j, x, y, pw[ 5 ], r, iterations; |
| 409 |
float distance; |
| 410 |
vec4_t projection, plane; |
| 411 |
vec3_t origin, target, delta; |
| 412 |
entity_t *e, *e2; |
| 413 |
parseMesh_t *p; |
| 414 |
mesh_t *mesh, *subdivided; |
| 415 |
bspDrawVert_t *dv[ 4 ]; |
| 416 |
const char *value; |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
Sys_FPrintf( SYS_VRB, "--- ProcessDecals ---\n" ); |
| 421 |
|
| 422 |
|
| 423 |
for ( i = 0; i < numEntities; i++ ) |
| 424 |
{ |
| 425 |
|
| 426 |
e = &entities[ i ]; |
| 427 |
value = ValueForKey( e, "classname" ); |
| 428 |
if ( Q_stricmp( value, "_decal" ) ) { |
| 429 |
continue; |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
if ( e->patches == NULL ) { |
| 434 |
Sys_FPrintf( SYS_WRN, "WARNING: Decal entity without any patch meshes, ignoring.\n" ); |
| 435 |
e->epairs = NULL; |
| 436 |
continue; |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
value = ValueForKey( e, "target" ); |
| 441 |
e2 = FindTargetEntity( value ); |
| 442 |
|
| 443 |
|
| 444 |
if ( e2 == NULL ) { |
| 445 |
Sys_FPrintf( SYS_WRN, "WARNING: Decal entity without a valid target, ignoring.\n" ); |
| 446 |
continue; |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
for ( p = e->patches; p != NULL; p = e->patches ) |
| 451 |
{ |
| 452 |
|
| 453 |
if ( VectorCompare( e->origin, vec3_origin ) ) { |
| 454 |
VectorAdd( p->eMins, p->eMaxs, origin ); |
| 455 |
VectorScale( origin, 0.5f, origin ); |
| 456 |
} |
| 457 |
else{ |
| 458 |
VectorCopy( e->origin, origin ); |
| 459 |
} |
| 460 |
|
| 461 |
VectorCopy( e2->origin, target ); |
| 462 |
VectorSubtract( target, origin, delta ); |
| 463 |
|
| 464 |
|
| 465 |
distance = VectorNormalize( delta, projection ); |
| 466 |
projection[ 3 ] = DotProduct( origin, projection ); |
| 467 |
|
| 468 |
|
| 469 |
if ( distance > 0.125f ) { |
| 470 |
|
| 471 |
iterations = IterationsForCurve( p->longestCurve, patchSubdivisions ); |
| 472 |
subdivided = SubdivideMesh2( p->mesh, iterations ); |
| 473 |
|
| 474 |
|
| 475 |
PutMeshOnCurve( *subdivided ); |
| 476 |
mesh = RemoveLinearMeshColumnsRows( subdivided ); |
| 477 |
FreeMesh( subdivided ); |
| 478 |
|
| 479 |
|
| 480 |
for ( j = 0; j < ( mesh->width * mesh->height ); j++ ) |
| 481 |
VectorAdd( mesh->verts[ j ].xyz, e->origin, mesh->verts[ j ].xyz ); |
| 482 |
|
| 483 |
|
| 484 |
for ( y = 0; y < ( mesh->height - 1 ); y++ ) |
| 485 |
{ |
| 486 |
for ( x = 0; x < ( mesh->width - 1 ); x++ ) |
| 487 |
{ |
| 488 |
|
| 489 |
pw[ 0 ] = x + ( y * mesh->width ); |
| 490 |
pw[ 1 ] = x + ( ( y + 1 ) * mesh->width ); |
| 491 |
pw[ 2 ] = x + 1 + ( ( y + 1 ) * mesh->width ); |
| 492 |
pw[ 3 ] = x + 1 + ( y * mesh->width ); |
| 493 |
pw[ 4 ] = x + ( y * mesh->width ); |
| 494 |
|
| 495 |
|
| 496 |
r = ( x + y ) & 1; |
| 497 |
|
| 498 |
|
| 499 |
dv[ 0 ] = &mesh->verts[ pw[ r + 0 ] ]; |
| 500 |
dv[ 1 ] = &mesh->verts[ pw[ r + 1 ] ]; |
| 501 |
dv[ 2 ] = &mesh->verts[ pw[ r + 2 ] ]; |
| 502 |
dv[ 3 ] = &mesh->verts[ pw[ r + 3 ] ]; |
| 503 |
|
| 504 |
|
| 505 |
plane[ 0 ] = 0.0f; |
| 506 |
if ( 0 && PlaneFromPoints( plane, dv[ 0 ]->xyz, dv[ 1 ]->xyz, dv[ 2 ]->xyz ) && |
| 507 |
fabs( DotProduct( dv[ 1 ]->xyz, plane ) - plane[ 3 ] ) <= PLANAR_EPSILON ) { |
| 508 |
|
| 509 |
MakeDecalProjector( p->shaderInfo, projection, distance, 4, dv ); |
| 510 |
} |
| 511 |
else |
| 512 |
{ |
| 513 |
|
| 514 |
MakeDecalProjector( p->shaderInfo, projection, distance, 3, dv ); |
| 515 |
|
| 516 |
|
| 517 |
dv[ 1 ] = dv[ 2 ]; |
| 518 |
dv[ 2 ] = dv[ 3 ]; |
| 519 |
MakeDecalProjector( p->shaderInfo, projection, distance, 3, dv ); |
| 520 |
} |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
free( mesh ); |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
e->patches = p->next; |
| 530 |
|
| 531 |
|
| 532 |
#if 0 |
| 533 |
p->next = entities[ 0 ].patches; |
| 534 |
entities[ 0 ].patches = p; |
| 535 |
#endif |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
Sys_FPrintf( SYS_VRB, "%9d decal projectors\n", numProjectors ); |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
static void ProjectDecalOntoWinding( decalProjector_t *dp, mapDrawSurface_t *ds, winding_t *w ){ |
| 551 |
int i, j; |
| 552 |
float d, d2, alpha; |
| 553 |
winding_t *front, *back; |
| 554 |
mapDrawSurface_t *ds2; |
| 555 |
bspDrawVert_t *dv; |
| 556 |
vec4_t plane; |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
if ( w->numpoints < 3 ) { |
| 561 |
FreeWinding( w ); |
| 562 |
return; |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
for ( i = 0; i < w->numpoints; i++ ) |
| 567 |
VectorAdd( w->p[ i ], entityOrigin, w->p[ i ] ); |
| 568 |
|
| 569 |
|
| 570 |
if ( !PlaneFromPoints( plane, w->p[ 0 ], w->p[ 1 ], w->p[ 2 ] ) ) { |
| 571 |
FreeWinding( w ); |
| 572 |
return; |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
d = DotProduct( dp->planes[ 0 ], plane ); |
| 577 |
if ( d < -0.0001f ) { |
| 578 |
FreeWinding( w ); |
| 579 |
return; |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
for ( i = 0; i < dp->numPlanes; i++ ) |
| 584 |
{ |
| 585 |
|
| 586 |
ClipWindingEpsilonStrict( w, dp->planes[ i ], dp->planes[ i ][ 3 ], 0.0625f, &front, &back ); |
| 587 |
FreeWinding( w ); |
| 588 |
|
| 589 |
|
| 590 |
if ( front != NULL ) { |
| 591 |
FreeWinding( front ); |
| 592 |
} |
| 593 |
|
| 594 |
|
| 595 |
if ( back == NULL ) { |
| 596 |
return; |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
w = back; |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
if ( w == NULL || w->numpoints < 3 ) { |
| 605 |
return; |
| 606 |
} |
| 607 |
|
| 608 |
|
| 609 |
numDecalSurfaces++; |
| 610 |
|
| 611 |
|
| 612 |
ds2 = AllocDrawSurface( SURFACE_DECAL ); |
| 613 |
|
| 614 |
|
| 615 |
ds2->entityNum = ds->entityNum; |
| 616 |
ds2->castShadows = ds->castShadows; |
| 617 |
ds2->recvShadows = ds->recvShadows; |
| 618 |
ds2->shaderInfo = dp->si; |
| 619 |
ds2->fogNum = ds->fogNum; |
| 620 |
ds2->lightmapScale = ds->lightmapScale; |
| 621 |
ds2->shadeAngleDegrees = ds->shadeAngleDegrees; |
| 622 |
ds2->numVerts = w->numpoints; |
| 623 |
ds2->verts = safe_malloc0( ds2->numVerts * sizeof( *ds2->verts ) ); |
| 624 |
|
| 625 |
|
| 626 |
for ( i = 0; i < ds2->numVerts; i++ ) |
| 627 |
{ |
| 628 |
|
| 629 |
dv = &ds2->verts[ i ]; |
| 630 |
|
| 631 |
|
| 632 |
d = DotProduct( w->p[ i ], dp->planes[ 0 ] ) - dp->planes[ 0 ][ 3 ]; |
| 633 |
d2 = DotProduct( w->p[ i ], dp->planes[ 1 ] ) - dp->planes[ 1 ][ 3 ]; |
| 634 |
alpha = 255.0f * d2 / ( d + d2 ); |
| 635 |
if ( alpha > 255 ) { |
| 636 |
alpha = 255; |
| 637 |
} |
| 638 |
else if ( alpha < 0 ) { |
| 639 |
alpha = 0; |
| 640 |
} |
| 641 |
|
| 642 |
|
| 643 |
VectorSubtract( w->p[ i ], entityOrigin, dv->xyz ); |
| 644 |
VectorCopy( plane, dv->normal ); |
| 645 |
dv->st[ 0 ] = DotProduct( dv->xyz, dp->texMat[ 0 ] ) + dp->texMat[ 0 ][ 3 ]; |
| 646 |
dv->st[ 1 ] = DotProduct( dv->xyz, dp->texMat[ 1 ] ) + dp->texMat[ 1 ][ 3 ]; |
| 647 |
|
| 648 |
|
| 649 |
for ( j = 0; j < MAX_LIGHTMAPS; j++ ) |
| 650 |
{ |
| 651 |
dv->color[ j ][ 0 ] = 255; |
| 652 |
dv->color[ j ][ 1 ] = 255; |
| 653 |
dv->color[ j ][ 2 ] = 255; |
| 654 |
dv->color[ j ][ 3 ] = alpha; |
| 655 |
} |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
static void ProjectDecalOntoFace( decalProjector_t *dp, mapDrawSurface_t *ds ){ |
| 667 |
vec4_t plane; |
| 668 |
float d; |
| 669 |
winding_t *w; |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
if ( ds->sideRef == NULL || ds->sideRef->side == NULL ) { |
| 674 |
return; |
| 675 |
} |
| 676 |
|
| 677 |
|
| 678 |
if ( ds->planar ) { |
| 679 |
VectorCopy( mapplanes[ ds->planeNum ].normal, plane ); |
| 680 |
plane[ 3 ] = mapplanes[ ds->planeNum ].dist + DotProduct( plane, entityOrigin ); |
| 681 |
d = DotProduct( dp->planes[ 0 ], plane ); |
| 682 |
if ( d < -0.0001f ) { |
| 683 |
return; |
| 684 |
} |
| 685 |
} |
| 686 |
|
| 687 |
|
| 688 |
w = WindingFromDrawSurf( ds ); |
| 689 |
ProjectDecalOntoWinding( dp, ds, w ); |
| 690 |
} |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
static void ProjectDecalOntoPatch( decalProjector_t *dp, mapDrawSurface_t *ds ){ |
| 700 |
int x, y, pw[ 5 ], r, iterations; |
| 701 |
vec4_t plane; |
| 702 |
float d; |
| 703 |
mesh_t src, *mesh, *subdivided; |
| 704 |
winding_t *w; |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
if ( ds->planar ) { |
| 709 |
VectorCopy( mapplanes[ ds->planeNum ].normal, plane ); |
| 710 |
plane[ 3 ] = mapplanes[ ds->planeNum ].dist + DotProduct( plane, entityOrigin ); |
| 711 |
d = DotProduct( dp->planes[ 0 ], plane ); |
| 712 |
if ( d < -0.0001f ) { |
| 713 |
return; |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
|
| 718 |
src.width = ds->patchWidth; |
| 719 |
src.height = ds->patchHeight; |
| 720 |
src.verts = ds->verts; |
| 721 |
iterations = IterationsForCurve( ds->longestCurve, patchSubdivisions ); |
| 722 |
subdivided = SubdivideMesh2( src, iterations ); |
| 723 |
|
| 724 |
|
| 725 |
PutMeshOnCurve( *subdivided ); |
| 726 |
mesh = RemoveLinearMeshColumnsRows( subdivided ); |
| 727 |
FreeMesh( subdivided ); |
| 728 |
|
| 729 |
|
| 730 |
for ( y = 0; y < ( mesh->height - 1 ); y++ ) |
| 731 |
{ |
| 732 |
for ( x = 0; x < ( mesh->width - 1 ); x++ ) |
| 733 |
{ |
| 734 |
|
| 735 |
pw[ 0 ] = x + ( y * mesh->width ); |
| 736 |
pw[ 1 ] = x + ( ( y + 1 ) * mesh->width ); |
| 737 |
pw[ 2 ] = x + 1 + ( ( y + 1 ) * mesh->width ); |
| 738 |
pw[ 3 ] = x + 1 + ( y * mesh->width ); |
| 739 |
pw[ 4 ] = x + ( y * mesh->width ); |
| 740 |
|
| 741 |
|
| 742 |
r = ( x + y ) & 1; |
| 743 |
|
| 744 |
|
| 745 |
w = AllocWinding( 3 ); |
| 746 |
w->numpoints = 3; |
| 747 |
VectorCopy( mesh->verts[ pw[ r + 0 ] ].xyz, w->p[ 0 ] ); |
| 748 |
VectorCopy( mesh->verts[ pw[ r + 1 ] ].xyz, w->p[ 1 ] ); |
| 749 |
VectorCopy( mesh->verts[ pw[ r + 2 ] ].xyz, w->p[ 2 ] ); |
| 750 |
ProjectDecalOntoWinding( dp, ds, w ); |
| 751 |
|
| 752 |
|
| 753 |
w = AllocWinding( 3 ); |
| 754 |
w->numpoints = 3; |
| 755 |
VectorCopy( mesh->verts[ pw[ r + 0 ] ].xyz, w->p[ 0 ] ); |
| 756 |
VectorCopy( mesh->verts[ pw[ r + 2 ] ].xyz, w->p[ 1 ] ); |
| 757 |
VectorCopy( mesh->verts[ pw[ r + 3 ] ].xyz, w->p[ 2 ] ); |
| 758 |
ProjectDecalOntoWinding( dp, ds, w ); |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
|
| 763 |
free( mesh ); |
| 764 |
} |
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
static void ProjectDecalOntoTriangles( decalProjector_t *dp, mapDrawSurface_t *ds ){ |
| 774 |
int i; |
| 775 |
vec4_t plane; |
| 776 |
float d; |
| 777 |
winding_t *w; |
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
if ( ds->type == SURFACE_TRIANGLES && ds->shaderInfo->shaderText == NULL ) { |
| 782 |
return; |
| 783 |
} |
| 784 |
|
| 785 |
|
| 786 |
if ( ds->planar ) { |
| 787 |
VectorCopy( mapplanes[ ds->planeNum ].normal, plane ); |
| 788 |
plane[ 3 ] = mapplanes[ ds->planeNum ].dist + DotProduct( plane, entityOrigin ); |
| 789 |
d = DotProduct( dp->planes[ 0 ], plane ); |
| 790 |
if ( d < -0.0001f ) { |
| 791 |
return; |
| 792 |
} |
| 793 |
} |
| 794 |
|
| 795 |
|
| 796 |
for ( i = 0; i < ds->numIndexes; i += 3 ) |
| 797 |
{ |
| 798 |
|
| 799 |
w = AllocWinding( 3 ); |
| 800 |
w->numpoints = 3; |
| 801 |
VectorCopy( ds->verts[ ds->indexes[ i ] ].xyz, w->p[ 0 ] ); |
| 802 |
VectorCopy( ds->verts[ ds->indexes[ i + 1 ] ].xyz, w->p[ 1 ] ); |
| 803 |
VectorCopy( ds->verts[ ds->indexes[ i + 2 ] ].xyz, w->p[ 2 ] ); |
| 804 |
ProjectDecalOntoWinding( dp, ds, w ); |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
void MakeEntityDecals( entity_t *e ){ |
| 816 |
int i, j, k, f, fOld, start; |
| 817 |
decalProjector_t dp; |
| 818 |
mapDrawSurface_t *ds; |
| 819 |
vec3_t identityAxis[ 3 ] = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } }; |
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
Sys_FPrintf( SYS_VRB, "--- MakeEntityDecals ---\n" ); |
| 824 |
|
| 825 |
|
| 826 |
VectorCopy( e->origin, entityOrigin ); |
| 827 |
|
| 828 |
|
| 829 |
VectorClear( entityOrigin ); |
| 830 |
|
| 831 |
|
| 832 |
fOld = -1; |
| 833 |
start = I_FloatTime(); |
| 834 |
|
| 835 |
|
| 836 |
for ( i = 0; i < numProjectors; i++ ) |
| 837 |
{ |
| 838 |
|
| 839 |
f = 10 * i / numProjectors; |
| 840 |
if ( f != fOld ) { |
| 841 |
fOld = f; |
| 842 |
Sys_FPrintf( SYS_VRB, "%d...", f ); |
| 843 |
} |
| 844 |
|
| 845 |
|
| 846 |
TransformDecalProjector( &projectors[ i ], identityAxis, e->origin, &dp ); |
| 847 |
|
| 848 |
|
| 849 |
for ( j = e->firstDrawSurf; j < numMapDrawSurfs; j++ ) |
| 850 |
{ |
| 851 |
|
| 852 |
ds = &mapDrawSurfs[ j ]; |
| 853 |
if ( ds->numVerts <= 0 ) { |
| 854 |
continue; |
| 855 |
} |
| 856 |
|
| 857 |
|
| 858 |
if ( ds->shaderInfo->autosprite || ( ds->shaderInfo->compileFlags & C_NOMARKS ) ) { |
| 859 |
continue; |
| 860 |
} |
| 861 |
|
| 862 |
|
| 863 |
for ( k = 0; k < 3; k++ ) |
| 864 |
if ( ds->mins[ k ] >= ( dp.center[ k ] + dp.radius ) || |
| 865 |
ds->maxs[ k ] <= ( dp.center[ k ] - dp.radius ) ) { |
| 866 |
break; |
| 867 |
} |
| 868 |
if ( k < 3 ) { |
| 869 |
continue; |
| 870 |
} |
| 871 |
|
| 872 |
|
| 873 |
switch ( ds->type ) |
| 874 |
{ |
| 875 |
case SURFACE_FACE: |
| 876 |
ProjectDecalOntoFace( &dp, ds ); |
| 877 |
break; |
| 878 |
|
| 879 |
case SURFACE_PATCH: |
| 880 |
ProjectDecalOntoPatch( &dp, ds ); |
| 881 |
break; |
| 882 |
|
| 883 |
case SURFACE_TRIANGLES: |
| 884 |
case SURFACE_FORCED_META: |
| 885 |
case SURFACE_META: |
| 886 |
ProjectDecalOntoTriangles( &dp, ds ); |
| 887 |
break; |
| 888 |
|
| 889 |
default: |
| 890 |
break; |
| 891 |
} |
| 892 |
} |
| 893 |
} |
| 894 |
|
| 895 |
|
| 896 |
Sys_FPrintf( SYS_VRB, " (%d)\n", (int) ( I_FloatTime() - start ) ); |
| 897 |
|
| 898 |
|
| 899 |
Sys_FPrintf( SYS_VRB, "%9d decal surfaces\n", numDecalSurfaces ); |
| 900 |
} |