light: make shadow bmodels block sunlight too

Signed-off-by: Kevin Shanahan <kmshanah@disenchant.net>
This commit is contained in:
Kevin Shanahan 2013-03-08 13:47:20 +10:30
parent feda78b4a3
commit dc39137874
1 changed files with 59 additions and 65 deletions

View File

@ -22,7 +22,7 @@
typedef struct tnode_s { typedef struct tnode_s {
int type; int type;
vec3_t normal; vec3_t normal;
float dist; vec_t dist;
int children[2]; int children[2];
int pad; int pad;
} tnode_t; } tnode_t;
@ -95,104 +95,90 @@ typedef struct {
#define MAX_TSTACK 128 #define MAX_TSTACK 128
static qboolean static qboolean
TestLineOrSky(const dmodel_t *model, const vec3_t start, const vec3_t stop, TestLineOrSky(const dmodel_t *model, const vec3_t start, const vec3_t stop,
qboolean sky_test) qboolean skytest, vec3_t skypoint)
{ {
int node; int node, side;
float front, back; vec3_t front, back;
tracestack_t *tstack_p; vec_t frontdist, backdist;
int side;
float frontx, fronty, frontz, backx, backy, backz;
tracestack_t tracestack[MAX_TSTACK]; tracestack_t tracestack[MAX_TSTACK];
tracestack_t *tstack;
tnode_t *tnode; tnode_t *tnode;
const tracestack_t *const tstack_max = tracestack + MAX_TSTACK;
const tracestack_t *tstack_max = tracestack + MAX_TSTACK; VectorCopy(start, front);
VectorCopy(stop, back);
frontx = start[0]; tstack = tracestack;
fronty = start[1];
frontz = start[2];
backx = stop[0];
backy = stop[1];
backz = stop[2];
tstack_p = tracestack;
node = model->headnode[0]; node = model->headnode[0];
while (1) { while (1) {
while (node < 0 && node != CONTENTS_SOLID while (node < 0 && node != CONTENTS_SOLID) {
&& (!sky_test || node != CONTENTS_SKY)) { if (skytest && node == CONTENTS_SKY)
break;
/* pop up the stack for a back side */ /* If the stack is empty, not obstructions were hit */
tstack_p--; if (tstack == tracestack)
return !skytest;
if (tstack_p < tracestack) /*
return !sky_test; /* no obstructions */ * pop the stack, set the hit point for this plane and
* go down the back side
/* set the hit point for this plane */ */
frontx = backx; tstack--;
fronty = backy; VectorCopy(back, front);
frontz = backz; VectorCopy(tstack->backpt, back);
node = tnodes[tstack->node].children[!tstack->side];
/* go down the back side */
backx = tstack_p->backpt[0];
backy = tstack_p->backpt[1];
backz = tstack_p->backpt[2];
node = tnodes[tstack_p->node].children[!tstack_p->side];
} }
if (node == CONTENTS_SOLID) if (node == CONTENTS_SOLID)
return false; /* DONE! */ return false;
else if (node == CONTENTS_SKY && sky_test) if (node == CONTENTS_SKY && skytest) {
return true; /* DONE! */ VectorCopy(front, skypoint);
return true;
}
tnode = &tnodes[node]; tnode = &tnodes[node];
switch (tnode->type) { switch (tnode->type) {
case PLANE_X: case PLANE_X:
front = frontx - tnode->dist; frontdist = front[0] - tnode->dist;
back = backx - tnode->dist; backdist = back[0] - tnode->dist;
break; break;
case PLANE_Y: case PLANE_Y:
front = fronty - tnode->dist; frontdist = front[1] - tnode->dist;
back = backy - tnode->dist; backdist = back[1] - tnode->dist;
break; break;
case PLANE_Z: case PLANE_Z:
front = frontz - tnode->dist; frontdist = front[2] - tnode->dist;
back = backz - tnode->dist; backdist = back[2] - tnode->dist;
break; break;
default: default:
front = (frontx * tnode->normal[0] + fronty * tnode->normal[1] frontdist = DotProduct(front, tnode->normal) - tnode->dist;
+ frontz * tnode->normal[2]) - tnode->dist; backdist = DotProduct(back, tnode->normal) - tnode->dist;
back = (backx * tnode->normal[0] + backy * tnode->normal[1]
+ backz * tnode->normal[2]) - tnode->dist;
break; break;
} }
if (front > -ON_EPSILON && back > -ON_EPSILON) { if (frontdist > -ON_EPSILON && backdist > -ON_EPSILON) {
node = tnode->children[0]; node = tnode->children[0];
continue; continue;
} }
if (front < ON_EPSILON && back < ON_EPSILON) { if (frontdist < ON_EPSILON && backdist < ON_EPSILON) {
node = tnode->children[1]; node = tnode->children[1];
continue; continue;
} }
side = front < 0.0f ? 1 : 0; if (tstack == tstack_max)
front /= (front - back);
tstack_p->node = node;
tstack_p->side = side;
tstack_p->backpt[0] = backx;
tstack_p->backpt[1] = backy;
tstack_p->backpt[2] = backz;
tstack_p++;
if (tstack_p >= tstack_max)
Error("%s: tstack overflow\n", __func__); Error("%s: tstack overflow\n", __func__);
backx = frontx + front * (backx - frontx); side = frontdist < 0.0f ? 1 : 0;
backy = fronty + front * (backy - fronty); tstack->node = node;
backz = frontz + front * (backz - frontz); tstack->side = side;
VectorCopy(back, tstack->backpt);
tstack++;
/* The new back is the intersection point with the node plane */
VectorSubtract(back, front, back);
VectorMA(front, frontdist / (frontdist - backdist), back, back);
node = tnode->children[side]; node = tnode->children[side];
} }
@ -217,7 +203,7 @@ TestLine(const vec3_t start, const vec3_t stop)
qboolean qboolean
TestLineModel(const dmodel_t *model, const vec3_t start, const vec3_t stop) TestLineModel(const dmodel_t *model, const vec3_t start, const vec3_t stop)
{ {
return TestLineOrSky(model, start, stop, false); return TestLineOrSky(model, start, stop, false, NULL);
} }
/* /*
@ -232,7 +218,15 @@ qboolean
TestSky(const vec3_t start, const vec3_t dirn) TestSky(const vec3_t start, const vec3_t dirn)
{ {
vec3_t stop; vec3_t stop;
const dmodel_t *const *model;
VectorAdd(dirn, start, stop); VectorAdd(dirn, start, stop);
return TestLineOrSky(tracelist[0], start, stop, true); if (!TestLineOrSky(tracelist[0], start, stop, true, stop))
return false;
for (model = tracelist + 1; *model; model++)
if (!TestLineModel(*model, start, stop))
break;
return !*model;
} }