2002-08-25 23:06:23 +00:00
|
|
|
/*
|
|
|
|
trace.c
|
|
|
|
|
|
|
|
(description)
|
|
|
|
|
2002-09-25 01:51:58 +00:00
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
2002-08-25 23:06:23 +00:00
|
|
|
Copyright (C) 2002 Colin Thompson
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2003-01-15 15:31:36 +00:00
|
|
|
|
2002-08-25 23:06:23 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_IO_H
|
|
|
|
# include <io.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "QF/bspfile.h"
|
|
|
|
#include "QF/mathlib.h"
|
2002-08-27 07:16:28 +00:00
|
|
|
#include "QF/qtypes.h"
|
|
|
|
#include "QF/dstring.h"
|
|
|
|
#include "QF/quakefs.h"
|
|
|
|
#include "QF/sys.h"
|
2013-01-02 07:46:40 +00:00
|
|
|
|
2020-06-21 14:15:17 +00:00
|
|
|
#include "tools/qflight/include/light.h"
|
|
|
|
#include "tools/qflight/include/options.h"
|
2002-08-25 23:06:23 +00:00
|
|
|
|
|
|
|
typedef struct {
|
2004-01-26 07:39:29 +00:00
|
|
|
int type;
|
|
|
|
vec3_t normal;
|
|
|
|
float dist;
|
|
|
|
int children[2];
|
|
|
|
int pad;
|
2002-08-25 23:06:23 +00:00
|
|
|
} tnode_t;
|
|
|
|
|
|
|
|
typedef struct {
|
2004-01-26 07:39:29 +00:00
|
|
|
vec3_t backpt;
|
|
|
|
int side;
|
|
|
|
int node;
|
2002-08-25 23:06:23 +00:00
|
|
|
} tracestack_t;
|
|
|
|
|
|
|
|
tnode_t *tnodes, *tnode_p;
|
|
|
|
|
|
|
|
/*
|
|
|
|
LINE TRACING
|
|
|
|
|
|
|
|
The major lighting operation is a point to point visibility test, performed
|
|
|
|
by recursive subdivision of the line by the BSP tree.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
MakeTnode
|
|
|
|
|
|
|
|
Converts the disk node structure into the efficient tracing structure
|
|
|
|
*/
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-08-25 23:06:23 +00:00
|
|
|
MakeTnode (int nodenum)
|
|
|
|
{
|
2002-09-19 02:37:52 +00:00
|
|
|
dnode_t *node;
|
|
|
|
dplane_t *plane;
|
|
|
|
int i;
|
|
|
|
tnode_t *t;
|
2002-08-25 23:06:23 +00:00
|
|
|
|
2002-09-19 02:37:52 +00:00
|
|
|
t = tnode_p++;
|
2002-08-25 23:06:23 +00:00
|
|
|
|
2002-09-19 05:35:17 +00:00
|
|
|
node = bsp->nodes + nodenum;
|
|
|
|
plane = bsp->planes + node->planenum;
|
2002-08-25 23:06:23 +00:00
|
|
|
|
2002-09-19 02:37:52 +00:00
|
|
|
t->type = plane->type;
|
|
|
|
VectorCopy (plane->normal, t->normal);
|
|
|
|
t->dist = plane->dist;
|
2002-08-25 23:06:23 +00:00
|
|
|
|
2002-09-19 02:37:52 +00:00
|
|
|
for (i = 0; i < 2; i++) {
|
2013-01-02 07:46:40 +00:00
|
|
|
if (node->children[i] < 0) {
|
2002-09-19 05:35:17 +00:00
|
|
|
t->children[i] = bsp->leafs[-node->children[i] - 1].contents;
|
2013-01-02 07:46:40 +00:00
|
|
|
if (options.solid_sky && t->children[i] == CONTENTS_SOLID) {
|
|
|
|
dface_t *face = &bsp->faces[node->firstface];
|
|
|
|
if (!strncmp (get_tex_name (face->texinfo), "sky", 3))
|
|
|
|
t->children[i] = CONTENTS_SKY; // Simulate real sky
|
|
|
|
}
|
|
|
|
} else {
|
2002-08-25 23:06:23 +00:00
|
|
|
t->children[i] = tnode_p - tnodes;
|
|
|
|
MakeTnode (node->children[i]);
|
|
|
|
}
|
2002-09-19 02:37:52 +00:00
|
|
|
}
|
2002-08-25 23:06:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
MakeTnodes
|
|
|
|
|
|
|
|
Loads the node structure out of a .bsp file to be used for light occlusion
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
MakeTnodes (dmodel_t *bm)
|
|
|
|
{
|
2002-09-19 05:35:17 +00:00
|
|
|
tnode_p = tnodes = malloc (bsp->numnodes * sizeof (tnode_t));
|
2002-08-25 23:06:23 +00:00
|
|
|
|
2002-09-19 02:37:52 +00:00
|
|
|
MakeTnode (0);
|
2002-08-25 23:06:23 +00:00
|
|
|
}
|
|
|
|
|
2003-09-10 15:48:46 +00:00
|
|
|
/* LordHavoc: this function operates by doing depth-first front-to-back
|
|
|
|
recursion through the BSP tree, checking at every split for a empty to
|
|
|
|
solid transition (impact) in the children, and returns false if one is
|
|
|
|
found.
|
|
|
|
note: 'no impact' does not mean it is empty, it occurs when there is no
|
|
|
|
transition from empty to solid; all solid or a transition from solid to
|
|
|
|
empty are not considered impacts. (this does mean that tracing is not
|
|
|
|
symmetrical; point A to point B may have different results than point B to
|
|
|
|
point A, if either start in solid)
|
|
|
|
*/
|
2002-08-25 23:06:23 +00:00
|
|
|
|
2003-09-10 15:48:46 +00:00
|
|
|
#define TESTLINESTATE_BLOCKED 0
|
|
|
|
#define TESTLINESTATE_EMPTY 1
|
|
|
|
#define TESTLINESTATE_SOLID 2
|
2002-08-25 23:06:23 +00:00
|
|
|
|
2023-06-13 09:06:11 +00:00
|
|
|
static bool
|
2013-01-02 12:06:26 +00:00
|
|
|
TestLineOrSky (lightinfo_t *l, const vec3_t start, const vec3_t end,
|
2023-06-13 09:06:11 +00:00
|
|
|
bool sky_test)
|
2003-09-10 15:48:46 +00:00
|
|
|
{
|
2003-09-10 21:09:23 +00:00
|
|
|
vec_t front, back;
|
|
|
|
vec3_t frontpt, backpt;
|
|
|
|
int node, side, empty;
|
|
|
|
tracestack_t *tstack;
|
|
|
|
tracestack_t tracestack[64];
|
2003-09-10 15:48:46 +00:00
|
|
|
tnode_t *tnode;
|
|
|
|
|
2003-09-10 21:09:23 +00:00
|
|
|
VectorCopy (start, frontpt);
|
|
|
|
VectorCopy (end, backpt);
|
2002-08-25 23:06:23 +00:00
|
|
|
|
2003-09-10 21:09:23 +00:00
|
|
|
tstack = tracestack;
|
|
|
|
node = 0;
|
|
|
|
empty = 0;
|
2002-08-25 23:06:23 +00:00
|
|
|
|
2003-09-10 21:09:23 +00:00
|
|
|
while (1) {
|
|
|
|
while (node < 0) {
|
2013-01-02 07:48:13 +00:00
|
|
|
if (sky_test && node == CONTENTS_SKY)
|
|
|
|
return true;
|
2003-09-10 21:09:23 +00:00
|
|
|
if (node != CONTENTS_SOLID)
|
|
|
|
empty = 1;
|
|
|
|
else if (empty) {
|
|
|
|
// DONE!
|
|
|
|
VectorCopy (backpt, l->testlineimpact);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// pop up the stack for a back side
|
|
|
|
if (tstack-- == tracestack)
|
2013-01-02 07:48:13 +00:00
|
|
|
return !sky_test;
|
2003-09-10 21:09:23 +00:00
|
|
|
|
|
|
|
// set the hit point for this plane
|
|
|
|
VectorCopy (backpt, frontpt);
|
|
|
|
|
|
|
|
// go down the back side
|
|
|
|
VectorCopy (tstack->backpt, backpt);
|
2002-08-25 23:06:23 +00:00
|
|
|
|
2004-11-02 05:17:00 +00:00
|
|
|
node = tnodes[tstack->node].children[tstack->side ^ 1];
|
2003-09-10 15:48:46 +00:00
|
|
|
}
|
2002-08-25 23:06:23 +00:00
|
|
|
|
2003-09-10 21:09:23 +00:00
|
|
|
tnode = &tnodes[node];
|
2002-08-25 23:06:23 +00:00
|
|
|
|
2003-09-10 21:09:23 +00:00
|
|
|
if (tnode->type < 3) {
|
|
|
|
front = frontpt[tnode->type] - tnode->dist;
|
|
|
|
back = backpt[tnode->type] - tnode->dist;
|
|
|
|
} else {
|
|
|
|
front = DotProduct (tnode->normal, frontpt) - tnode->dist;
|
|
|
|
back = DotProduct (tnode->normal, backpt) - tnode->dist;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (front >= 0 && back >= 0) {
|
|
|
|
node = tnode->children[0];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (front < 0 && back < 0) {
|
|
|
|
node = tnode->children[1];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
side = front < 0;
|
|
|
|
|
|
|
|
front = front / (front - back);
|
|
|
|
|
|
|
|
tstack->node = node;
|
|
|
|
tstack->side = side;
|
|
|
|
VectorCopy (backpt, tstack->backpt);
|
|
|
|
|
|
|
|
tstack++;
|
|
|
|
|
|
|
|
backpt[0] = frontpt[0] + front * (backpt[0] - frontpt[0]);
|
|
|
|
backpt[1] = frontpt[1] + front * (backpt[1] - frontpt[1]);
|
|
|
|
backpt[2] = frontpt[2] + front * (backpt[2] - frontpt[2]);
|
|
|
|
|
|
|
|
node = tnode->children[side];
|
|
|
|
}
|
2002-08-25 23:06:23 +00:00
|
|
|
}
|
2013-01-02 07:48:13 +00:00
|
|
|
|
2023-06-13 09:06:11 +00:00
|
|
|
bool
|
2013-01-02 12:06:26 +00:00
|
|
|
TestLine (lightinfo_t *l, const vec3_t start, const vec3_t stop)
|
2013-01-02 07:48:13 +00:00
|
|
|
{
|
|
|
|
return TestLineOrSky (l, start, stop, false);
|
|
|
|
}
|
|
|
|
|
2023-06-13 09:06:11 +00:00
|
|
|
bool
|
2013-01-02 12:06:26 +00:00
|
|
|
TestSky (lightinfo_t *l, const vec3_t start, const vec3_t dir)
|
2013-01-02 07:48:13 +00:00
|
|
|
{
|
|
|
|
vec3_t stop;
|
|
|
|
VectorAdd (dir, start, stop);
|
|
|
|
return TestLineOrSky (l, start, stop, true);
|
|
|
|
}
|