quakeforge/tools/qfbsp/source/qfbsp.c

495 lines
10 KiB
C
Raw Normal View History

2002-09-19 18:51:19 +00:00
/*
Copyright (C) 1996-1997 Id Software, Inc.
2002-09-19 18:51:19 +00:00
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.
2002-09-19 18:51:19 +00:00
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.
2002-09-19 18:51:19 +00:00
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2002-09-19 18:51:19 +00:00
See file, 'COPYING', for details.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
static __attribute__ ((used)) const char rcsid[] =
"$Id$";
2002-11-08 17:36:47 +00:00
#include <sys/types.h>
#ifdef HAVE_SYS_WAIT_H
# include <sys/wait.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
2011-08-25 13:35:20 +00:00
#if defined(_WIN32) && defined(HAVE_MALLOC_H)
#include <malloc.h>
#endif
#include <stdlib.h>
2003-08-11 17:11:44 +00:00
#include <errno.h>
#include "QF/quakefs.h"
#include "QF/sys.h"
#include "csg4.h"
#include "brush.h"
#include "bsp5.h"
#include "merge.h"
2002-09-20 21:48:34 +00:00
#include "options.h"
#include "outside.h"
#include "portals.h"
#include "readbsp.h"
#include "solidbsp.h"
#include "surfaces.h"
#include "writebsp.h"
#include "tjunc.h"
/** \addtogroup qfbsp
*/
//@{
2002-09-20 21:48:34 +00:00
options_t options;
2002-09-20 21:48:34 +00:00
bsp_t *bsp;
2002-09-19 17:14:23 +00:00
brushset_t *brushset;
2002-09-19 17:14:23 +00:00
int valid;
2002-09-19 17:14:23 +00:00
char *argv0; // changed after fork();
2002-09-19 17:14:23 +00:00
qboolean worldmodel;
2002-09-19 17:14:23 +00:00
int hullnum;
2002-09-23 16:27:17 +00:00
node_t *
2002-09-19 17:14:23 +00:00
AllocNode (void)
{
2002-09-19 17:14:23 +00:00
node_t *n;
n = malloc (sizeof (node_t));
memset (n, 0, sizeof (node_t));
return n;
}
static void
2002-09-19 17:14:23 +00:00
ProcessEntity (int entnum)
{
2002-09-19 18:51:19 +00:00
brushset_t *bs;
2002-09-19 17:14:23 +00:00
char mod[80];
2002-09-19 18:51:19 +00:00
entity_t *ent;
2002-09-19 17:14:23 +00:00
surface_t *surfs;
node_t *nodes;
ent = &entities[entnum];
if (!ent->brushes)
2002-09-19 17:14:23 +00:00
return; // non-bmodel entity
2002-09-19 17:14:23 +00:00
if (entnum > 0) {
worldmodel = false;
if (entnum == 1)
qprintf ("--- Internal Entities ---\n");
sprintf (mod, "*%i", bsp->nummodels);
2002-09-20 21:48:34 +00:00
if (options.verbosity)
PrintEntity (ent);
if (hullnum == 0)
printf ("MODEL: %s\n", mod);
SetKeyValue (ent, "model", mod);
2002-09-19 17:14:23 +00:00
} else
worldmodel = true;
2002-09-19 17:14:23 +00:00
2002-09-23 16:27:17 +00:00
// take the brush_ts and clip off all overlapping and contained faces,
// leaving a perfect skin of the model with no hidden faces
bs = Brush_LoadEntity (ent, hullnum);
2002-09-19 17:14:23 +00:00
if (!bs->brushes) {
PrintEntity (ent);
Sys_Error ("Entity with no valid brushes");
}
2002-09-19 17:14:23 +00:00
brushset = bs;
surfs = CSGFaces (bs);
2002-09-19 17:14:23 +00:00
if (hullnum != 0) {
nodes = SolidBSP (surfs, true);
2002-09-20 21:48:34 +00:00
if (entnum == 0 && !options.nofill) {
// assume non-world bmodels are simple
PortalizeWorld (nodes);
2002-09-19 17:14:23 +00:00
if (FillOutside (nodes)) {
surfs = GatherNodeFaces (nodes);
2002-09-19 18:51:19 +00:00
nodes = SolidBSP (surfs, false); // make a really good tree
}
FreeAllPortals (nodes);
}
WriteNodePlanes (nodes);
WriteClipNodes (nodes);
BumpModel (hullnum);
2002-09-19 17:14:23 +00:00
} else {
// SolidBSP generates a node tree
2010-08-27 13:12:13 +00:00
//
2002-09-19 17:14:23 +00:00
// if not the world, make a good tree first
// the world is just going to make a bad tree
// because the outside filling will force a regeneration later
nodes = SolidBSP (surfs, entnum == 0);
// build all the portals in the bsp tree
// some portals are solid polygons, and some are paths to other leafs
2002-09-20 21:48:34 +00:00
if (entnum == 0 && !options.nofill) {
// assume non-world bmodels are simple
PortalizeWorld (nodes);
2002-09-19 17:14:23 +00:00
if (FillOutside (nodes)) {
FreeAllPortals (nodes);
2002-09-19 17:14:23 +00:00
// get the remaining faces together into surfaces again
surfs = GatherNodeFaces (nodes);
2002-09-19 17:14:23 +00:00
// merge polygons
MergeAll (surfs);
2002-09-19 17:14:23 +00:00
// make a really good tree
nodes = SolidBSP (surfs, false);
2002-09-19 17:14:23 +00:00
// make the real portals for vis tracing
PortalizeWorldDetail (nodes);
2002-09-19 17:14:23 +00:00
// save portal file for vis tracing
WritePortalfile (nodes);
2002-09-19 17:14:23 +00:00
// fix tjunctions
tjunc (nodes);
}
FreeAllPortals (nodes);
}
WriteNodePlanes (nodes);
MakeFaceEdges (nodes);
WriteDrawNodes (nodes);
}
}
static void
2002-09-19 17:14:23 +00:00
UpdateEntLump (void)
{
2002-09-19 17:14:23 +00:00
int m, entnum;
char mod[80];
QFile *f;
2002-09-19 17:14:23 +00:00
m = 1;
2002-09-19 17:14:23 +00:00
for (entnum = 1; entnum < num_entities; entnum++) {
if (!entities[entnum].brushes)
continue;
sprintf (mod, "*%i", m);
SetKeyValue (&entities[entnum], "model", mod);
m++;
}
printf ("Updating entities lump...\n");
2002-09-20 21:48:34 +00:00
f = Qopen (options.bspfile, "rb");
2003-08-11 17:11:44 +00:00
if (!f)
Sys_Error ("couldn't open %s. %s", options.bspfile, strerror(errno));
bsp = LoadBSPFile (f, Qfilesize (f));
Qclose (f);
2002-09-19 17:14:23 +00:00
WriteEntitiesToString ();
2002-09-20 21:48:34 +00:00
f = Qopen (options.bspfile, "wb");
2003-08-11 17:11:44 +00:00
if (!f)
Sys_Error ("couldn't open %s. %s", options.bspfile, strerror(errno));
2002-09-19 17:14:23 +00:00
WriteBSPFile (bsp, f);
BSP_Free (bsp);
Qclose (f);
}
/*
2002-09-23 16:27:17 +00:00
WriteClipHull
2002-09-23 16:27:17 +00:00
Write the clipping hull out to a text file so the parent process can get it
*/
static void
2002-09-19 17:14:23 +00:00
WriteClipHull (void)
{
2002-09-19 18:51:19 +00:00
FILE *f;
2002-09-19 17:14:23 +00:00
dclipnode_t *d;
2002-09-19 18:51:19 +00:00
dplane_t *p;
int i;
2002-09-20 21:48:34 +00:00
options.hullfile[strlen (options.hullfile) - 1] = '0' + hullnum;
qprintf ("---- WriteClipHull ----\n");
2002-09-20 21:48:34 +00:00
qprintf ("Writing %s\n", options.hullfile);
2002-09-20 21:48:34 +00:00
f = fopen (options.hullfile, "w");
if (!f)
2002-09-20 21:48:34 +00:00
Sys_Error ("Couldn't open %s", options.hullfile);
fprintf (f, "%i\n", bsp->nummodels);
2002-09-19 17:14:23 +00:00
for (i = 0; i < bsp->nummodels; i++)
fprintf (f, "%i\n", bsp->models[i].headnode[hullnum]);
2002-09-19 17:14:23 +00:00
fprintf (f, "\n%i\n", bsp->numclipnodes);
2002-09-19 17:14:23 +00:00
for (i = 0; i < bsp->numclipnodes; i++) {
d = &bsp->clipnodes[i];
p = &bsp->planes[d->planenum];
// the node number is written out only for human readability
2002-09-19 17:14:23 +00:00
fprintf (f, "%5i : %f %f %f %f : %5i %5i\n", i, p->normal[0],
p->normal[1], p->normal[2], p->dist, d->children[0],
d->children[1]);
}
2002-09-19 17:14:23 +00:00
fclose (f);
}
/*
2002-09-23 16:27:17 +00:00
ReadClipHull
2002-09-23 16:27:17 +00:00
Read the files written out by the child processes
*/
static void
2002-09-19 17:14:23 +00:00
ReadClipHull (int hullnum)
{
2002-09-19 18:51:19 +00:00
FILE *f;
dclipnode_t d;
plane_t p;
dplane_t dp;
2002-09-19 18:51:19 +00:00
float f1, f2, f3, f4;
int firstclipnode, junk, c1, c2, i, j, n;
int flip;
2002-09-19 17:14:23 +00:00
2002-09-20 21:48:34 +00:00
options.hullfile[strlen (options.hullfile) - 1] = '0' + hullnum;
2002-09-20 21:48:34 +00:00
f = fopen (options.hullfile, "r");
if (!f)
2002-09-20 21:48:34 +00:00
Sys_Error ("Couldn't open %s", options.hullfile);
if (fscanf (f, "%d\n", &n) != 1)
2002-09-20 21:48:34 +00:00
Sys_Error ("Error parsing %s", options.hullfile);
if (n != bsp->nummodels)
2002-09-19 17:14:23 +00:00
Sys_Error ("ReadClipHull: hull had %i models, base had %i", n,
bsp->nummodels);
2002-09-19 17:14:23 +00:00
for (i = 0; i < n; i++) {
2007-01-06 21:31:03 +00:00
if (fscanf (f, "%d\n", &j) != 1)
Sys_Error ("Error parsing %s", options.hullfile);
bsp->models[i].headnode[hullnum] = bsp->numclipnodes + j;
}
2002-09-19 17:14:23 +00:00
2007-01-06 21:31:03 +00:00
if (fscanf (f, "\n%d\n", &n) != 1)
Sys_Error ("Error parsing %s", options.hullfile);
firstclipnode = bsp->numclipnodes;
2002-09-19 17:14:23 +00:00
for (i = 0; i < n; i++) {
if (bsp->numclipnodes == MAX_MAP_CLIPNODES)
Sys_Error ("ReadClipHull: MAX_MAP_CLIPNODES");
if (fscanf (f, "%d : %f %f %f %f : %d %d\n", &junk, &f1, &f2, &f3, &f4,
2002-09-19 18:51:19 +00:00
&c1, &c2) != 7)
2002-09-20 21:48:34 +00:00
Sys_Error ("Error parsing %s", options.hullfile);
2002-09-19 17:14:23 +00:00
2010-09-02 04:01:17 +00:00
VectorSet (f1, f2, f3, p.normal);
p.dist = f4;
flip = NormalizePlane (&p);
VectorCopy (p.normal, dp.normal);
dp.dist = p.dist;
dp.type = p.type;
2002-09-19 17:14:23 +00:00
d.children[flip] = c1 >= 0 ? c1 + firstclipnode : c1;
d.children[!flip] = c2 >= 0 ? c2 + firstclipnode : c2;
d.planenum = FindFinalPlane (&dp);
BSP_AddClipnode (bsp, &d);
}
2002-09-19 17:14:23 +00:00
}
static void
2002-09-19 17:14:23 +00:00
CreateSingleHull (void)
{
2002-09-19 17:14:23 +00:00
int entnum;
2002-09-23 16:27:17 +00:00
// for each entity in the map file that has geometry
2002-09-19 17:14:23 +00:00
for (entnum = 0; entnum < num_entities; entnum++) {
ProcessEntity (entnum);
2002-09-20 21:48:34 +00:00
if (options.verbosity < 2)
options.verbosity = 0; // don't print rest of entities
}
if (hullnum)
WriteClipHull ();
}
static void
2002-09-19 17:14:23 +00:00
CreateHulls (void)
{
// commanded to create only a single hull
2002-09-19 17:14:23 +00:00
if (hullnum) {
CreateSingleHull ();
exit (0);
}
2002-09-23 16:27:17 +00:00
// commanded to use the allready existing hulls 1 and 2
2002-09-20 21:48:34 +00:00
if (options.usehulls) {
CreateSingleHull ();
return;
}
2002-09-23 16:27:17 +00:00
// commanded to ignore the hulls altogether
2002-09-20 21:48:34 +00:00
if (options.noclip) {
CreateSingleHull ();
return;
}
2002-09-23 16:27:17 +00:00
// create all the hulls
#ifdef __alpha
printf ("forking hull processes...\n");
2002-09-23 16:27:17 +00:00
// fork a process for each clipping hull
fflush (stdout);
2002-09-19 17:14:23 +00:00
if (!fork ()) {
hullnum = 1;
2002-09-20 21:48:34 +00:00
options.verbosity = 0;
2002-11-10 02:50:42 +00:00
options.drawflag = false;
sprintf (argv0, "HUL%i", hullnum);
2002-09-19 17:14:23 +00:00
} else if (!fork ()) {
hullnum = 2;
2002-09-20 21:48:34 +00:00
options.verbosity = 0;
2002-11-10 02:50:42 +00:00
options.drawflag = false;
sprintf (argv0, "HUL%i", hullnum);
}
CreateSingleHull ();
if (hullnum)
exit (0);
2002-09-19 17:14:23 +00:00
2002-09-19 18:51:19 +00:00
wait (NULL); // wait for clip hull process to finish
wait (NULL); // wait for clip hull process to finish
#else
2002-09-23 16:27:17 +00:00
// create the hulls sequentially
printf ("building hulls sequentially...\n");
hullnum = 1;
CreateSingleHull ();
2002-09-19 17:14:23 +00:00
bsp->nummodels = 0;
bsp->numplanes = 0;
bsp->numclipnodes = 0;
hullnum = 2;
CreateSingleHull ();
2002-09-19 17:14:23 +00:00
bsp->nummodels = 0;
bsp->numplanes = 0;
bsp->numclipnodes = 0;
hullnum = 0;
CreateSingleHull ();
2002-09-19 17:14:23 +00:00
#endif
}
static void
ProcessFile (void)
{
bsp = BSP_New ();
if (options.extract) {
LoadBSP ();
if (options.portal)
bsp2prt ();
if (options.extract_textures)
extract_textures ();
if (options.extract_entities)
extract_entities ();
if (options.extract_hull)
extract_hull ();
BSP_Free (bsp);
return;
}
// load brushes and entities
LoadMapFile (options.mapfile);
2002-09-20 21:48:34 +00:00
if (options.onlyents) {
UpdateEntLump ();
BSP_Free (bsp);
return;
}
remove (options.bspfile);
if (!options.usehulls) {
options.hullfile[strlen (options.hullfile) - 1] = '1';
remove (options.hullfile);
options.hullfile[strlen (options.hullfile) - 1] = '2';
remove (options.hullfile);
}
remove (options.portfile);
remove (options.pointfile);
2002-09-23 16:27:17 +00:00
// init the tables to be shared by all models
BeginBSPFile ();
2002-09-23 16:27:17 +00:00
// the clipping hulls will be written out to text files by forked processes
CreateHulls ();
ReadClipHull (1);
ReadClipHull (2);
2002-09-19 17:14:23 +00:00
WriteEntitiesToString ();
FinishBSPFile ();
BSP_Free (bsp);
}
2002-09-19 17:14:23 +00:00
int
main (int argc, char **argv)
{
2002-09-19 18:51:19 +00:00
double start, end;
2002-09-19 17:14:23 +00:00
2002-09-25 17:48:43 +00:00
// let forked processes change name for ps status
this_program = argv0 = argv[0];
2002-09-23 16:27:17 +00:00
// check command line flags
2002-09-20 21:48:34 +00:00
DecodeArgs (argc, argv);
2002-09-19 18:51:19 +00:00
// XXX SetQdirFromPath (argv[i]);
2002-09-23 16:27:17 +00:00
// do it!
start = Sys_DoubleTime ();
ProcessFile ();
end = Sys_DoubleTime ();
2002-09-19 17:14:23 +00:00
printf ("%5.1f seconds elapsed\n", end - start);
return 0;
}
2003-05-07 07:18:36 +00:00
void
qprintf (const char *fmt, ...)
{
va_list argptr;
if (!options.verbosity)
return;
va_start (argptr, fmt);
vprintf (fmt, argptr);
va_end (argptr);
}
//@}