2002-08-25 23:06:23 +00:00
|
|
|
/*
|
2012-12-26 04:05:24 +00:00
|
|
|
qflight.c
|
2002-08-25 23:06:23 +00:00
|
|
|
|
|
|
|
(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 <getopt.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "QF/bspfile.h"
|
2002-08-27 07:16:28 +00:00
|
|
|
#include "QF/dstring.h"
|
2002-08-25 23:06:23 +00:00
|
|
|
#include "QF/mathlib.h"
|
2002-08-27 07:16:28 +00:00
|
|
|
#include "QF/qtypes.h"
|
|
|
|
#include "QF/quakefs.h"
|
|
|
|
#include "QF/sys.h"
|
2003-09-10 15:48:46 +00:00
|
|
|
#include "QF/va.h"
|
2002-08-27 07:16:28 +00:00
|
|
|
|
2020-06-21 14:15:17 +00:00
|
|
|
#include "tools/qflight/include/light.h"
|
|
|
|
#include "tools/qflight/include/threads.h"
|
|
|
|
#include "tools/qflight/include/entities.h"
|
|
|
|
#include "tools/qflight/include/options.h"
|
|
|
|
#include "tools/qflight/include/properties.h"
|
2002-08-25 23:06:23 +00:00
|
|
|
|
|
|
|
options_t options;
|
2002-09-19 05:35:17 +00:00
|
|
|
bsp_t *bsp;
|
2002-08-25 23:06:23 +00:00
|
|
|
|
2010-08-24 07:20:07 +00:00
|
|
|
dstring_t *bspfile;
|
2003-09-10 15:48:46 +00:00
|
|
|
dstring_t *litfile;
|
2002-08-25 23:06:23 +00:00
|
|
|
|
|
|
|
float scalecos = 0.5;
|
|
|
|
|
2002-09-20 22:31:44 +00:00
|
|
|
dstring_t *lightdata;
|
2003-09-10 15:48:46 +00:00
|
|
|
dstring_t *rgblightdata;
|
2002-08-25 23:06:23 +00:00
|
|
|
|
|
|
|
dmodel_t *bspmodel;
|
2021-08-01 12:54:05 +00:00
|
|
|
size_t bspfileface; // next surface to dispatch
|
2003-09-09 08:13:33 +00:00
|
|
|
int bspfileent; // next entity to dispatch
|
2002-08-25 23:06:23 +00:00
|
|
|
|
|
|
|
vec3_t bsp_origin;
|
|
|
|
|
2023-06-13 09:06:11 +00:00
|
|
|
bool extrasamples;
|
2002-08-25 23:06:23 +00:00
|
|
|
|
2002-09-20 22:31:44 +00:00
|
|
|
int
|
2002-08-25 23:06:23 +00:00
|
|
|
GetFileSpace (int size)
|
|
|
|
{
|
2012-12-30 11:51:13 +00:00
|
|
|
int oldsize;
|
2002-09-20 22:31:44 +00:00
|
|
|
int ofs;
|
2002-09-19 02:37:52 +00:00
|
|
|
|
|
|
|
LOCK;
|
2012-12-30 11:51:13 +00:00
|
|
|
oldsize = lightdata->size;
|
2002-09-20 22:44:29 +00:00
|
|
|
lightdata->size = (lightdata->size + 3) & ~3;
|
2002-09-20 22:31:44 +00:00
|
|
|
ofs = lightdata->size;
|
|
|
|
lightdata->size += size;
|
|
|
|
dstring_adjust (lightdata);
|
2012-12-30 11:51:13 +00:00
|
|
|
memset (lightdata->str + oldsize, 0, ofs - oldsize);
|
|
|
|
memset (lightdata->str + ofs, 0, size);
|
2003-09-10 15:48:46 +00:00
|
|
|
|
|
|
|
rgblightdata->size = (ofs + size) * 3;
|
|
|
|
dstring_adjust (rgblightdata);
|
2012-12-30 11:51:13 +00:00
|
|
|
memset (rgblightdata->str + oldsize * 3, 0, (ofs - oldsize) * 3);
|
|
|
|
memset (rgblightdata->str + ofs * 3, 0, size * 3);
|
2002-09-19 02:37:52 +00:00
|
|
|
UNLOCK;
|
2002-09-20 22:31:44 +00:00
|
|
|
return ofs;
|
2002-08-25 23:06:23 +00:00
|
|
|
}
|
|
|
|
|
2003-09-09 08:13:33 +00:00
|
|
|
static void *
|
|
|
|
VisThread (void *junk)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
LOCK;
|
|
|
|
i = bspfileent++;
|
|
|
|
UNLOCK;
|
|
|
|
if (i >= num_entities)
|
|
|
|
return 0;
|
|
|
|
VisEntity (i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void *
|
2003-09-10 15:48:46 +00:00
|
|
|
LightThread (void *l)
|
2002-08-25 23:06:23 +00:00
|
|
|
{
|
2021-08-01 12:54:05 +00:00
|
|
|
size_t i;
|
2002-08-25 23:06:23 +00:00
|
|
|
|
2002-09-19 02:37:52 +00:00
|
|
|
while (1) {
|
2002-08-25 23:06:23 +00:00
|
|
|
LOCK;
|
|
|
|
i = bspfileface++;
|
2003-09-12 20:40:14 +00:00
|
|
|
if (i < bsp->numfaces) {
|
2021-08-01 12:54:05 +00:00
|
|
|
printf ("%5zd / %zd\r", i, bsp->numfaces);
|
2003-09-12 20:40:14 +00:00
|
|
|
fflush (stdout);
|
|
|
|
}
|
2002-08-25 23:06:23 +00:00
|
|
|
UNLOCK;
|
2002-09-19 05:35:17 +00:00
|
|
|
if (i >= bsp->numfaces)
|
2002-09-20 21:58:40 +00:00
|
|
|
return 0;
|
2002-08-25 23:06:23 +00:00
|
|
|
|
2003-09-10 15:48:46 +00:00
|
|
|
LightFace (l, i);
|
2002-09-19 02:37:52 +00:00
|
|
|
}
|
2002-08-25 23:06:23 +00:00
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2012-12-26 08:14:07 +00:00
|
|
|
FindFaceOffsets (void)
|
2002-08-25 23:06:23 +00:00
|
|
|
{
|
2003-09-10 15:48:46 +00:00
|
|
|
entity_t *ent;
|
2012-12-26 08:14:07 +00:00
|
|
|
vec3_t org;
|
2003-09-10 15:48:46 +00:00
|
|
|
const char *name;
|
|
|
|
|
|
|
|
surfaceorgs = (vec3_t *) calloc (bsp->numfaces, sizeof (vec3_t));
|
|
|
|
|
2021-08-01 12:54:05 +00:00
|
|
|
for (size_t i = 1; i < bsp->nummodels; i++) {
|
|
|
|
ent = FindEntityWithKeyPair ("model", name = va (0, "*%zd", i));
|
2003-09-10 15:48:46 +00:00
|
|
|
VectorZero (org);
|
|
|
|
if (!ent)
|
|
|
|
Sys_Error ("FindFaceOffsets: Couldn't find entity for model %s.\n",
|
|
|
|
name);
|
|
|
|
if (!strncmp (ValueForKey (ent, "classname"), "rotate_", 7))
|
|
|
|
GetVectorForKey (ent, "origin", org);
|
2021-08-01 12:54:05 +00:00
|
|
|
for (uint32_t j = 0; j < bsp->models[i].numfaces; j++)
|
2012-12-26 08:14:07 +00:00
|
|
|
VectorCopy (org, surfaceorgs[bsp->models[i].firstface]);
|
2003-09-10 15:48:46 +00:00
|
|
|
}
|
2012-12-26 08:14:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
LightWorld (void)
|
|
|
|
{
|
|
|
|
lightdata = dstring_new ();
|
|
|
|
rgblightdata = dstring_new ();
|
|
|
|
surfacelightchain = (lightchain_t **) calloc (bsp->numfaces,
|
|
|
|
sizeof (lightchain_t *));
|
2002-08-25 23:06:23 +00:00
|
|
|
|
2003-09-09 08:13:33 +00:00
|
|
|
VisThread (0); // not worth threading :/
|
|
|
|
VisStats ();
|
2002-09-19 02:37:52 +00:00
|
|
|
RunThreadsOn (LightThread);
|
2002-08-25 23:06:23 +00:00
|
|
|
|
2005-06-08 06:35:48 +00:00
|
|
|
BSP_AddLighting (bsp, (byte *) lightdata->str, lightdata->size);
|
2002-08-25 23:06:23 +00:00
|
|
|
|
|
|
|
if (options.verbosity >= 0)
|
2010-12-23 02:40:16 +00:00
|
|
|
printf ("lightdatasize: %ld\n", (long) bsp->lightdatasize);
|
2002-08-25 23:06:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
2002-09-19 02:37:52 +00:00
|
|
|
double start, stop;
|
2002-09-19 05:35:17 +00:00
|
|
|
QFile *f;
|
2002-09-19 02:37:52 +00:00
|
|
|
|
2002-08-25 23:06:23 +00:00
|
|
|
start = Sys_DoubleTime ();
|
2002-09-19 02:37:52 +00:00
|
|
|
|
2002-08-25 23:06:23 +00:00
|
|
|
this_program = argv[0];
|
2002-09-19 02:37:52 +00:00
|
|
|
|
2012-05-21 23:23:22 +00:00
|
|
|
DecodeArgs (argc, argv);
|
2002-08-25 23:06:23 +00:00
|
|
|
|
|
|
|
if (!bspfile) {
|
|
|
|
fprintf (stderr, "%s: no bsp file specified.\n", this_program);
|
|
|
|
usage (1);
|
|
|
|
}
|
2002-09-19 02:37:52 +00:00
|
|
|
|
2002-08-25 23:06:23 +00:00
|
|
|
InitThreads ();
|
|
|
|
|
2010-08-24 07:20:07 +00:00
|
|
|
QFS_SetExtension (bspfile, ".bsp");
|
2002-08-25 23:06:23 +00:00
|
|
|
|
2010-08-24 07:20:07 +00:00
|
|
|
litfile = dstring_strdup (bspfile->str);
|
|
|
|
QFS_SetExtension (litfile, ".lit");
|
2003-09-10 15:48:46 +00:00
|
|
|
|
2004-01-27 08:31:34 +00:00
|
|
|
if (options.properties_filename)
|
|
|
|
LoadProperties (options.properties_filename);
|
|
|
|
|
2010-08-24 07:20:07 +00:00
|
|
|
f = Qopen (bspfile->str, "rbz");
|
2003-09-11 18:52:37 +00:00
|
|
|
if (!f)
|
2010-08-24 07:20:07 +00:00
|
|
|
Sys_Error ("could not open %s for reading", bspfile->str);
|
2002-09-19 05:35:17 +00:00
|
|
|
bsp = LoadBSPFile (f, Qfilesize (f));
|
|
|
|
Qclose (f);
|
2002-09-19 02:37:52 +00:00
|
|
|
LoadEntities ();
|
|
|
|
|
2002-09-19 05:35:17 +00:00
|
|
|
MakeTnodes (&bsp->models[0]);
|
2002-08-25 23:06:23 +00:00
|
|
|
|
2012-12-26 08:14:07 +00:00
|
|
|
FindFaceOffsets ();
|
2002-09-19 02:37:52 +00:00
|
|
|
LightWorld ();
|
2002-08-25 23:06:23 +00:00
|
|
|
|
2002-09-19 02:37:52 +00:00
|
|
|
WriteEntitiesToString ();
|
2002-09-19 05:35:17 +00:00
|
|
|
|
2010-08-24 07:20:07 +00:00
|
|
|
f = Qopen (bspfile->str, "wb");
|
2003-09-11 18:52:37 +00:00
|
|
|
if (!f)
|
2010-08-24 07:20:07 +00:00
|
|
|
Sys_Error ("could not open %s for writing", bspfile->str);
|
2002-09-19 05:35:17 +00:00
|
|
|
WriteBSPFile (bsp, f);
|
|
|
|
Qclose (f);
|
2002-08-25 23:06:23 +00:00
|
|
|
|
2003-09-10 15:48:46 +00:00
|
|
|
dstring_insert (rgblightdata, 0, "QLIT\x01\x00\x00\x00", 8);
|
|
|
|
|
|
|
|
f = Qopen (litfile->str, "wb");
|
2003-09-11 18:52:37 +00:00
|
|
|
if (!f)
|
|
|
|
Sys_Error ("could not open %s for writing", litfile->str);
|
2003-09-10 15:48:46 +00:00
|
|
|
Qwrite (f, rgblightdata->str, rgblightdata->size);
|
|
|
|
Qclose (f);
|
|
|
|
|
2002-08-25 23:06:23 +00:00
|
|
|
stop = Sys_DoubleTime ();
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2002-08-25 23:06:23 +00:00
|
|
|
if (options.verbosity >= 0)
|
|
|
|
printf ("%5.1f seconds elapsed\n", stop - start);
|
|
|
|
|
2002-09-19 02:37:52 +00:00
|
|
|
return 0;
|
2002-08-25 23:06:23 +00:00
|
|
|
}
|