The source release of the qutils.

This commit is contained in:
Travis Bradshaw
2012-01-31 15:48:05 -06:00
commit 6df9737f9c
111 changed files with 23929 additions and 0 deletions

278
qutils/LIGHT/ENTITIES.C Normal file
View File

@@ -0,0 +1,278 @@
// entities.c
#include "light.h"
entity_t entities[MAX_MAP_ENTITIES];
int num_entities;
/*
==============================================================================
ENTITY FILE PARSING
If a light has a targetname, generate a unique style in the 32-63 range
==============================================================================
*/
int numlighttargets;
char lighttargets[32][64];
int LightStyleForTargetname (char *targetname, qboolean alloc)
{
int i;
for (i=0 ; i<numlighttargets ; i++)
if (!strcmp (lighttargets[i], targetname))
return 32 + i;
if (!alloc)
return -1;
strcpy (lighttargets[i], targetname);
numlighttargets++;
return numlighttargets-1 + 32;
}
/*
==================
MatchTargets
==================
*/
void MatchTargets (void)
{
int i,j;
for (i=0 ; i<num_entities ; i++)
{
if (!entities[i].target[0])
continue;
for (j=0 ; j<num_entities ; j++)
if (!strcmp(entities[j].targetname, entities[i].target))
{
entities[i].targetent = &entities[j];
break;
}
if (j==num_entities)
{
printf ("WARNING: entity at (%i,%i,%i) (%s) has unmatched target\n", (int)entities[i].origin[0], (int)entities[i].origin[1], (int)entities[i].origin[2], entities[i].classname);
continue;
}
// set the style on the source ent for switchable lights
if (entities[j].style)
{
char s[16];
entities[i].style = entities[j].style;
sprintf (s,"%i", entities[i].style);
SetKeyValue (&entities[i], "style", s);
}
}
}
/*
==================
LoadEntities
==================
*/
void LoadEntities (void)
{
char *data;
entity_t *entity;
char key[64];
epair_t *epair;
double vec[3];
int i;
data = dentdata;
//
// start parsing
//
num_entities = 0;
// go through all the entities
while (1)
{
// parse the opening brace
data = COM_Parse (data);
if (!data)
break;
if (com_token[0] != '{')
Error ("LoadEntities: found %s when expecting {",com_token);
if (num_entities == MAX_MAP_ENTITIES)
Error ("LoadEntities: MAX_MAP_ENTITIES");
entity = &entities[num_entities];
num_entities++;
// go through all the keys in this entity
while (1)
{
int c;
// parse key
data = COM_Parse (data);
if (!data)
Error ("LoadEntities: EOF without closing brace");
if (!strcmp(com_token,"}"))
break;
strcpy (key, com_token);
// parse value
data = COM_Parse (data);
if (!data)
Error ("LoadEntities: EOF without closing brace");
c = com_token[0];
if (c == '}')
Error ("LoadEntities: closing brace without data");
epair = malloc (sizeof(epair_t));
memset (epair, 0, sizeof(epair));
strcpy (epair->key, key);
strcpy (epair->value, com_token);
epair->next = entity->epairs;
entity->epairs = epair;
if (!strcmp(key, "classname"))
strcpy (entity->classname, com_token);
else if (!strcmp(key, "target"))
strcpy (entity->target, com_token);
else if (!strcmp(key, "targetname"))
strcpy (entity->targetname, com_token);
else if (!strcmp(key, "origin"))
{
// scan into doubles, then assign
// which makes it vec_t size independent
if (sscanf(com_token, "%lf %lf %lf",
&vec[0], &vec[1], &vec[2]) != 3)
Error ("LoadEntities: not 3 values for origin");
for (i=0 ; i<3 ; i++)
entity->origin[i] = vec[i];
}
else if (!strncmp(key, "light", 5) || !strcmp (key, "_light") )
{
entity->light = atof(com_token);
}
else if (!strcmp(key, "style"))
{
entity->style = atof(com_token);
if ((unsigned)entity->style > 254)
Error ("Bad light style %i (must be 0-254)", entity->style);
}
else if (!strcmp(key, "angle"))
{
entity->angle = atof(com_token);
}
}
// all fields have been parsed
if (!strncmp (entity->classname, "light", 5) && !entity->light)
entity->light = DEFAULTLIGHTLEVEL;
if (!strcmp (entity->classname, "light"))
{
if (entity->targetname[0] && !entity->style)
{
char s[16];
entity->style = LightStyleForTargetname (entity->targetname, true);
sprintf (s,"%i", entity->style);
SetKeyValue (entity, "style", s);
}
}
}
printf ("%d entities read\n", num_entities);
MatchTargets ();
}
char *ValueForKey (entity_t *ent, char *key)
{
epair_t *ep;
for (ep=ent->epairs ; ep ; ep=ep->next)
if (!strcmp (ep->key, key) )
return ep->value;
return "";
}
void SetKeyValue (entity_t *ent, char *key, char *value)
{
epair_t *ep;
for (ep=ent->epairs ; ep ; ep=ep->next)
if (!strcmp (ep->key, key) )
{
strcpy (ep->value, value);
return;
}
ep = malloc (sizeof(*ep));
ep->next = ent->epairs;
ent->epairs = ep;
strcpy (ep->key, key);
strcpy (ep->value, value);
}
float FloatForKey (entity_t *ent, char *key)
{
char *k;
k = ValueForKey (ent, key);
return atof(k);
}
void GetVectorForKey (entity_t *ent, char *key, vec3_t vec)
{
char *k;
k = ValueForKey (ent, key);
sscanf (k, "%lf %lf %lf", &vec[0], &vec[1], &vec[2]);
}
/*
================
WriteEntitiesToString
================
*/
void WriteEntitiesToString (void)
{
char *buf, *end;
epair_t *ep;
char line[128];
int i;
buf = dentdata;
end = buf;
*end = 0;
printf ("%i switchable light styles\n", numlighttargets);
for (i=0 ; i<num_entities ; i++)
{
ep = entities[i].epairs;
if (!ep)
continue; // ent got removed
strcat (end,"{\n");
end += 2;
for (ep = entities[i].epairs ; ep ; ep=ep->next)
{
sprintf (line, "\"%s\" \"%s\"\n", ep->key, ep->value);
strcat (end, line);
end += strlen(line);
}
strcat (end,"}\n");
end += 2;
if (end > buf + MAX_MAP_ENTSTRING)
Error ("Entity text too long");
}
entdatasize = end - buf + 1;
}

33
qutils/LIGHT/ENTITIES.H Normal file
View File

@@ -0,0 +1,33 @@
#define DEFAULTLIGHTLEVEL 300
typedef struct epair_s
{
struct epair_s *next;
char key[MAX_KEY];
char value[MAX_VALUE];
} epair_t;
typedef struct entity_s
{
char classname[64];
vec3_t origin;
float angle;
int light;
int style;
char target[32];
char targetname[32];
struct epair_s *epairs;
struct entity_s *targetent;
} entity_t;
extern entity_t entities[MAX_MAP_ENTITIES];
extern int num_entities;
char *ValueForKey (entity_t *ent, char *key);
void SetKeyValue (entity_t *ent, char *key, char *value);
float FloatForKey (entity_t *ent, char *key);
void GetVectorForKey (entity_t *ent, char *key, vec3_t vec);
void LoadEntities (void);
void WriteEntitiesToString (void);

146
qutils/LIGHT/LIGHT.C Normal file
View File

@@ -0,0 +1,146 @@
// lighting.c
#include "light.h"
/*
NOTES
-----
*/
float scaledist = 1.0;
float scalecos = 0.5;
float rangescale = 0.5;
byte *filebase, *file_p, *file_end;
dmodel_t *bspmodel;
int bspfileface; // next surface to dispatch
vec3_t bsp_origin;
qboolean extrasamples;
float minlights[MAX_MAP_FACES];
byte *GetFileSpace (int size)
{
byte *buf;
LOCK;
file_p = (byte *)(((long)file_p + 3)&~3);
buf = file_p;
file_p += size;
UNLOCK;
if (file_p > file_end)
Error ("GetFileSpace: overrun");
return buf;
}
void LightThread (void *junk)
{
int i;
while (1)
{
LOCK;
i = bspfileface++;
UNLOCK;
if (i >= numfaces)
return;
LightFace (i);
}
}
/*
=============
LightWorld
=============
*/
void LightWorld (void)
{
filebase = file_p = dlightdata;
file_end = filebase + MAX_MAP_LIGHTING;
RunThreadsOn (LightThread);
lightdatasize = file_p - filebase;
printf ("lightdatasize: %i\n", lightdatasize);
}
/*
========
main
light modelfile
========
*/
int main (int argc, char **argv)
{
int i;
double start, end;
char source[1024];
printf ("----- LightFaces ----\n");
for (i=1 ; i<argc ; i++)
{
if (!strcmp(argv[i],"-threads"))
{
numthreads = atoi (argv[i+1]);
i++;
}
else if (!strcmp(argv[i],"-extra"))
{
extrasamples = true;
printf ("extra sampling enabled\n");
}
else if (!strcmp(argv[i],"-dist"))
{
scaledist = atof (argv[i+1]);
i++;
}
else if (!strcmp(argv[i],"-range"))
{
rangescale = atof (argv[i+1]);
i++;
}
else if (argv[i][0] == '-')
Error ("Unknown option \"%s\"", argv[i]);
else
break;
}
if (i != argc - 1)
Error ("usage: light [-threads num] [-extra] bspfile");
InitThreads ();
start = I_FloatTime ();
strcpy (source, argv[i]);
StripExtension (source);
DefaultExtension (source, ".bsp");
LoadBSPFile (source);
LoadEntities ();
MakeTnodes (&dmodels[0]);
LightWorld ();
WriteEntitiesToString ();
WriteBSPFile (source);
end = I_FloatTime ();
printf ("%5.1f seconds elapsed\n", end-start);
return 0;
}

38
qutils/LIGHT/LIGHT.H Normal file
View File

@@ -0,0 +1,38 @@
#include "cmdlib.h"
#include "mathlib.h"
#include "bspfile.h"
#include "entities.h"
#include "threads.h"
#define ON_EPSILON 0.1
#define MAXLIGHTS 1024
void LoadNodes (char *file);
qboolean TestLine (vec3_t start, vec3_t stop);
void LightFace (int surfnum);
void LightLeaf (dleaf_t *leaf);
void MakeTnodes (dmodel_t *bm);
extern float scaledist;
extern float scalecos;
extern float rangescale;
extern int c_culldistplane, c_proper;
byte *GetFileSpace (int size);
extern byte *filebase;
extern vec3_t bsp_origin;
extern vec3_t bsp_xvector;
extern vec3_t bsp_yvector;
void TransformSample (vec3_t in, vec3_t out);
void RotateSample (vec3_t in, vec3_t out);
extern qboolean extrasamples;
extern float minlights[MAX_MAP_FACES];

407
qutils/LIGHT/LIGHT.MAK Normal file
View File

@@ -0,0 +1,407 @@
# Microsoft Developer Studio Generated NMAKE File, Format Version 4.00
# ** DO NOT EDIT **
# TARGTYPE "Win32 (x86) Console Application" 0x0103
!IF "$(CFG)" == ""
CFG=light - Win32 Debug
!MESSAGE No configuration specified. Defaulting to light - Win32 Debug.
!ENDIF
!IF "$(CFG)" != "light - Win32 Release" && "$(CFG)" != "light - Win32 Debug"
!MESSAGE Invalid configuration "$(CFG)" specified.
!MESSAGE You can specify a configuration when running NMAKE on this makefile
!MESSAGE by defining the macro CFG on the command line. For example:
!MESSAGE
!MESSAGE NMAKE /f "light.mak" CFG="light - Win32 Debug"
!MESSAGE
!MESSAGE Possible choices for configuration are:
!MESSAGE
!MESSAGE "light - Win32 Release" (based on "Win32 (x86) Console Application")
!MESSAGE "light - Win32 Debug" (based on "Win32 (x86) Console Application")
!MESSAGE
!ERROR An invalid configuration is specified.
!ENDIF
!IF "$(OS)" == "Windows_NT"
NULL=
!ELSE
NULL=nul
!ENDIF
################################################################################
# Begin Project
# PROP Target_Last_Scanned "light - Win32 Debug"
CPP=cl.exe
RSC=rc.exe
!IF "$(CFG)" == "light - Win32 Release"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 0
# PROP BASE Output_Dir "Release"
# PROP BASE Intermediate_Dir "Release"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 0
# PROP Output_Dir "Release"
# PROP Intermediate_Dir "Release"
# PROP Target_Dir ""
OUTDIR=.\Release
INTDIR=.\Release
ALL : "$(OUTDIR)\light.exe"
CLEAN :
-@erase ".\Release\light.exe"
-@erase ".\Release\trilib.obj"
-@erase ".\Release\threads.obj"
-@erase ".\Release\mathlib.obj"
-@erase ".\Release\light.obj"
-@erase ".\Release\entities.obj"
-@erase ".\Release\bspfile.obj"
-@erase ".\Release\ltface.obj"
-@erase ".\Release\cmdlib.obj"
-@erase ".\Release\trace.obj"
"$(OUTDIR)" :
if not exist "$(OUTDIR)/$(NULL)" mkdir "$(OUTDIR)"
# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /YX /c
# ADD CPP /nologo /GX /O2 /I "..\common" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /YX /c
CPP_PROJ=/nologo /ML /GX /O2 /I "..\common" /D "WIN32" /D "NDEBUG" /D\
"_CONSOLE" /Fp"$(INTDIR)/light.pch" /YX /Fo"$(INTDIR)/" /c
CPP_OBJS=.\Release/
CPP_SBRS=
# ADD BASE RSC /l 0x409 /d "NDEBUG"
# ADD RSC /l 0x409 /d "NDEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
BSC32_FLAGS=/nologo /o"$(OUTDIR)/light.bsc"
BSC32_SBRS=
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386
LINK32_FLAGS=kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib\
advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib\
odbccp32.lib /nologo /subsystem:console /incremental:no\
/pdb:"$(OUTDIR)/light.pdb" /machine:I386 /out:"$(OUTDIR)/light.exe"
LINK32_OBJS= \
".\Release\trilib.obj" \
".\Release\threads.obj" \
".\Release\mathlib.obj" \
".\Release\light.obj" \
".\Release\entities.obj" \
".\Release\bspfile.obj" \
".\Release\ltface.obj" \
".\Release\cmdlib.obj" \
".\Release\trace.obj"
"$(OUTDIR)\light.exe" : "$(OUTDIR)" $(DEF_FILE) $(LINK32_OBJS)
$(LINK32) @<<
$(LINK32_FLAGS) $(LINK32_OBJS)
<<
!ELSEIF "$(CFG)" == "light - Win32 Debug"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 1
# PROP BASE Output_Dir "Debug"
# PROP BASE Intermediate_Dir "Debug"
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 1
# PROP Output_Dir "Debug"
# PROP Intermediate_Dir "Debug"
# PROP Target_Dir ""
OUTDIR=.\Debug
INTDIR=.\Debug
ALL : "$(OUTDIR)\light.exe"
CLEAN :
-@erase ".\Debug\vc40.pdb"
-@erase ".\Debug\vc40.idb"
-@erase ".\Debug\light.exe"
-@erase ".\Debug\threads.obj"
-@erase ".\Debug\trilib.obj"
-@erase ".\Debug\bspfile.obj"
-@erase ".\Debug\light.obj"
-@erase ".\Debug\trace.obj"
-@erase ".\Debug\entities.obj"
-@erase ".\Debug\mathlib.obj"
-@erase ".\Debug\ltface.obj"
-@erase ".\Debug\cmdlib.obj"
-@erase ".\Debug\light.ilk"
-@erase ".\Debug\light.pdb"
"$(OUTDIR)" :
if not exist "$(OUTDIR)/$(NULL)" mkdir "$(OUTDIR)"
# ADD BASE CPP /nologo /W3 /Gm /GX /Zi /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /YX /c
# ADD CPP /nologo /Gm /GX /Zi /Od /I "..\common" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /YX /c
CPP_PROJ=/nologo /MLd /Gm /GX /Zi /Od /I "..\common" /D "WIN32" /D "_DEBUG" /D\
"_CONSOLE" /Fp"$(INTDIR)/light.pch" /YX /Fo"$(INTDIR)/" /Fd"$(INTDIR)/" /c
CPP_OBJS=.\Debug/
CPP_SBRS=
# ADD BASE RSC /l 0x409 /d "_DEBUG"
# ADD RSC /l 0x409 /d "_DEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
BSC32_FLAGS=/nologo /o"$(OUTDIR)/light.bsc"
BSC32_SBRS=
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386
# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386
LINK32_FLAGS=kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib\
advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib\
odbccp32.lib /nologo /subsystem:console /incremental:yes\
/pdb:"$(OUTDIR)/light.pdb" /debug /machine:I386 /out:"$(OUTDIR)/light.exe"
LINK32_OBJS= \
".\Debug\threads.obj" \
".\Debug\trilib.obj" \
".\Debug\bspfile.obj" \
".\Debug\light.obj" \
".\Debug\trace.obj" \
".\Debug\entities.obj" \
".\Debug\mathlib.obj" \
".\Debug\ltface.obj" \
".\Debug\cmdlib.obj"
"$(OUTDIR)\light.exe" : "$(OUTDIR)" $(DEF_FILE) $(LINK32_OBJS)
$(LINK32) @<<
$(LINK32_FLAGS) $(LINK32_OBJS)
<<
!ENDIF
.c{$(CPP_OBJS)}.obj:
$(CPP) $(CPP_PROJ) $<
.cpp{$(CPP_OBJS)}.obj:
$(CPP) $(CPP_PROJ) $<
.cxx{$(CPP_OBJS)}.obj:
$(CPP) $(CPP_PROJ) $<
.c{$(CPP_SBRS)}.sbr:
$(CPP) $(CPP_PROJ) $<
.cpp{$(CPP_SBRS)}.sbr:
$(CPP) $(CPP_PROJ) $<
.cxx{$(CPP_SBRS)}.sbr:
$(CPP) $(CPP_PROJ) $<
################################################################################
# Begin Target
# Name "light - Win32 Release"
# Name "light - Win32 Debug"
!IF "$(CFG)" == "light - Win32 Release"
!ELSEIF "$(CFG)" == "light - Win32 Debug"
!ENDIF
################################################################################
# Begin Source File
SOURCE=.\trace.c
!IF "$(CFG)" == "light - Win32 Release"
DEP_CPP_TRACE=\
".\light.h"\
"$(INTDIR)\trace.obj" : $(SOURCE) $(DEP_CPP_TRACE) "$(INTDIR)"
!ELSEIF "$(CFG)" == "light - Win32 Debug"
DEP_CPP_TRACE=\
".\light.h"\
".\..\common\cmdlib.h"\
".\..\common\mathlib.h"\
".\..\common\bspfile.h"\
".\entities.h"\
".\threads.h"\
"$(INTDIR)\trace.obj" : $(SOURCE) $(DEP_CPP_TRACE) "$(INTDIR)"
!ENDIF
# End Source File
################################################################################
# Begin Source File
SOURCE=.\threads.c
DEP_CPP_THREA=\
".\..\common\cmdlib.h"\
".\threads.h"\
"$(INTDIR)\threads.obj" : $(SOURCE) $(DEP_CPP_THREA) "$(INTDIR)"
# End Source File
################################################################################
# Begin Source File
SOURCE=.\ltface.c
!IF "$(CFG)" == "light - Win32 Release"
DEP_CPP_LTFAC=\
".\light.h"\
"$(INTDIR)\ltface.obj" : $(SOURCE) $(DEP_CPP_LTFAC) "$(INTDIR)"
!ELSEIF "$(CFG)" == "light - Win32 Debug"
DEP_CPP_LTFAC=\
".\light.h"\
".\..\common\cmdlib.h"\
".\..\common\mathlib.h"\
".\..\common\bspfile.h"\
".\entities.h"\
".\threads.h"\
"$(INTDIR)\ltface.obj" : $(SOURCE) $(DEP_CPP_LTFAC) "$(INTDIR)"
!ENDIF
# End Source File
################################################################################
# Begin Source File
SOURCE=.\light.c
!IF "$(CFG)" == "light - Win32 Release"
DEP_CPP_LIGHT=\
".\light.h"\
"$(INTDIR)\light.obj" : $(SOURCE) $(DEP_CPP_LIGHT) "$(INTDIR)"
!ELSEIF "$(CFG)" == "light - Win32 Debug"
DEP_CPP_LIGHT=\
".\light.h"\
".\..\common\cmdlib.h"\
".\..\common\mathlib.h"\
".\..\common\bspfile.h"\
".\entities.h"\
".\threads.h"\
"$(INTDIR)\light.obj" : $(SOURCE) $(DEP_CPP_LIGHT) "$(INTDIR)"
!ENDIF
# End Source File
################################################################################
# Begin Source File
SOURCE=.\entities.c
!IF "$(CFG)" == "light - Win32 Release"
DEP_CPP_ENTIT=\
".\light.h"\
"$(INTDIR)\entities.obj" : $(SOURCE) $(DEP_CPP_ENTIT) "$(INTDIR)"
!ELSEIF "$(CFG)" == "light - Win32 Debug"
DEP_CPP_ENTIT=\
".\light.h"\
".\..\common\cmdlib.h"\
".\..\common\mathlib.h"\
".\..\common\bspfile.h"\
".\entities.h"\
".\threads.h"\
"$(INTDIR)\entities.obj" : $(SOURCE) $(DEP_CPP_ENTIT) "$(INTDIR)"
!ENDIF
# End Source File
################################################################################
# Begin Source File
SOURCE=\quake\utils2\common\trilib.c
DEP_CPP_TRILI=\
".\..\common\cmdlib.h"\
".\..\common\mathlib.h"\
".\..\common\trilib.h"\
"$(INTDIR)\trilib.obj" : $(SOURCE) $(DEP_CPP_TRILI) "$(INTDIR)"
$(CPP) $(CPP_PROJ) $(SOURCE)
# End Source File
################################################################################
# Begin Source File
SOURCE=\quake\utils2\common\mathlib.c
DEP_CPP_MATHL=\
".\..\common\cmdlib.h"\
".\..\common\mathlib.h"\
"$(INTDIR)\mathlib.obj" : $(SOURCE) $(DEP_CPP_MATHL) "$(INTDIR)"
$(CPP) $(CPP_PROJ) $(SOURCE)
# End Source File
################################################################################
# Begin Source File
SOURCE=\quake\utils2\common\cmdlib.c
DEP_CPP_CMDLI=\
".\..\common\cmdlib.h"\
{$(INCLUDE)}"\sys\TYPES.H"\
{$(INCLUDE)}"\sys\STAT.H"\
"$(INTDIR)\cmdlib.obj" : $(SOURCE) $(DEP_CPP_CMDLI) "$(INTDIR)"
$(CPP) $(CPP_PROJ) $(SOURCE)
# End Source File
################################################################################
# Begin Source File
SOURCE=\quake\utils2\common\bspfile.c
DEP_CPP_BSPFI=\
".\..\common\cmdlib.h"\
".\..\common\mathlib.h"\
".\..\common\bspfile.h"\
"$(INTDIR)\bspfile.obj" : $(SOURCE) $(DEP_CPP_BSPFI) "$(INTDIR)"
$(CPP) $(CPP_PROJ) $(SOURCE)
# End Source File
# End Target
# End Project
################################################################################

BIN
qutils/LIGHT/LIGHT.MDP Normal file

Binary file not shown.

BIN
qutils/LIGHT/LIGHT.NCB Normal file

Binary file not shown.

588
qutils/LIGHT/LTFACE.C Normal file
View File

@@ -0,0 +1,588 @@
#include "light.h"
/*
============
CastRay
Returns the distance between the points, or -1 if blocked
=============
*/
vec_t CastRay (vec3_t p1, vec3_t p2)
{
int i;
vec_t t;
qboolean trace;
trace = TestLine (p1, p2);
if (!trace)
return -1; // ray was blocked
t = 0;
for (i=0 ; i< 3 ; i++)
t += (p2[i]-p1[i]) * (p2[i]-p1[i]);
if (t == 0)
t = 1; // don't blow up...
return sqrt(t);
}
/*
===============================================================================
SAMPLE POINT DETERMINATION
void SetupBlock (dface_t *f) Returns with surfpt[] set
This is a little tricky because the lightmap covers more area than the face.
If done in the straightforward fashion, some of the
sample points will be inside walls or on the other side of walls, causing
false shadows and light bleeds.
To solve this, I only consider a sample point valid if a line can be drawn
between it and the exact midpoint of the face. If invalid, it is adjusted
towards the center until it is valid.
(this doesn't completely work)
===============================================================================
*/
#define SINGLEMAP (18*18*4)
typedef struct
{
vec_t lightmaps[MAXLIGHTMAPS][SINGLEMAP];
int numlightstyles;
vec_t *light;
vec_t facedist;
vec3_t facenormal;
int numsurfpt;
vec3_t surfpt[SINGLEMAP];
vec3_t texorg;
vec3_t worldtotex[2]; // s = (world - texorg) . worldtotex[0]
vec3_t textoworld[2]; // world = texorg + s * textoworld[0]
vec_t exactmins[2], exactmaxs[2];
int texmins[2], texsize[2];
int lightstyles[256];
int surfnum;
dface_t *face;
} lightinfo_t;
/*
================
CalcFaceVectors
Fills in texorg, worldtotex. and textoworld
================
*/
void CalcFaceVectors (lightinfo_t *l)
{
texinfo_t *tex;
int i, j;
vec3_t texnormal;
float distscale;
vec_t dist, len;
tex = &texinfo[l->face->texinfo];
// convert from float to vec_t
for (i=0 ; i<2 ; i++)
for (j=0 ; j<3 ; j++)
l->worldtotex[i][j] = tex->vecs[i][j];
// calculate a normal to the texture axis. points can be moved along this
// without changing their S/T
texnormal[0] = tex->vecs[1][1]*tex->vecs[0][2]
- tex->vecs[1][2]*tex->vecs[0][1];
texnormal[1] = tex->vecs[1][2]*tex->vecs[0][0]
- tex->vecs[1][0]*tex->vecs[0][2];
texnormal[2] = tex->vecs[1][0]*tex->vecs[0][1]
- tex->vecs[1][1]*tex->vecs[0][0];
VectorNormalize (texnormal);
// flip it towards plane normal
distscale = DotProduct (texnormal, l->facenormal);
if (!distscale)
Error ("Texture axis perpendicular to face");
if (distscale < 0)
{
distscale = -distscale;
VectorSubtract (vec3_origin, texnormal, texnormal);
}
// distscale is the ratio of the distance along the texture normal to
// the distance along the plane normal
distscale = 1/distscale;
for (i=0 ; i<2 ; i++)
{
len = VectorLength (l->worldtotex[i]);
dist = DotProduct (l->worldtotex[i], l->facenormal);
dist *= distscale;
VectorMA (l->worldtotex[i], -dist, texnormal, l->textoworld[i]);
VectorScale (l->textoworld[i], (1/len)*(1/len), l->textoworld[i]);
}
// calculate texorg on the texture plane
for (i=0 ; i<3 ; i++)
l->texorg[i] = -tex->vecs[0][3]* l->textoworld[0][i] - tex->vecs[1][3] * l->textoworld[1][i];
// project back to the face plane
dist = DotProduct (l->texorg, l->facenormal) - l->facedist - 1;
dist *= distscale;
VectorMA (l->texorg, -dist, texnormal, l->texorg);
}
/*
================
CalcFaceExtents
Fills in s->texmins[] and s->texsize[]
also sets exactmins[] and exactmaxs[]
================
*/
void CalcFaceExtents (lightinfo_t *l)
{
dface_t *s;
vec_t mins[2], maxs[2], val;
int i,j, e;
dvertex_t *v;
texinfo_t *tex;
s = l->face;
mins[0] = mins[1] = 999999;
maxs[0] = maxs[1] = -99999;
tex = &texinfo[s->texinfo];
for (i=0 ; i<s->numedges ; i++)
{
e = dsurfedges[s->firstedge+i];
if (e >= 0)
v = dvertexes + dedges[e].v[0];
else
v = dvertexes + dedges[-e].v[1];
for (j=0 ; j<2 ; j++)
{
val = v->point[0] * tex->vecs[j][0] +
v->point[1] * tex->vecs[j][1] +
v->point[2] * tex->vecs[j][2] +
tex->vecs[j][3];
if (val < mins[j])
mins[j] = val;
if (val > maxs[j])
maxs[j] = val;
}
}
for (i=0 ; i<2 ; i++)
{
l->exactmins[i] = mins[i];
l->exactmaxs[i] = maxs[i];
mins[i] = floor(mins[i]/16);
maxs[i] = ceil(maxs[i]/16);
l->texmins[i] = mins[i];
l->texsize[i] = maxs[i] - mins[i];
if (l->texsize[i] > 17)
Error ("Bad surface extents");
}
}
/*
=================
CalcPoints
For each texture aligned grid point, back project onto the plane
to get the world xyz value of the sample point
=================
*/
int c_bad;
void CalcPoints (lightinfo_t *l)
{
int i;
int s, t, j;
int w, h, step;
vec_t starts, startt, us, ut;
vec_t *surf;
vec_t mids, midt;
vec3_t facemid, move;
//
// fill in surforg
// the points are biased towards the center of the surface
// to help avoid edge cases just inside walls
//
surf = l->surfpt[0];
mids = (l->exactmaxs[0] + l->exactmins[0])/2;
midt = (l->exactmaxs[1] + l->exactmins[1])/2;
for (j=0 ; j<3 ; j++)
facemid[j] = l->texorg[j] + l->textoworld[0][j]*mids + l->textoworld[1][j]*midt;
if (extrasamples)
{ // extra filtering
h = (l->texsize[1]+1)*2;
w = (l->texsize[0]+1)*2;
starts = (l->texmins[0]-0.5)*16;
startt = (l->texmins[1]-0.5)*16;
step = 8;
}
else
{
h = l->texsize[1]+1;
w = l->texsize[0]+1;
starts = l->texmins[0]*16;
startt = l->texmins[1]*16;
step = 16;
}
l->numsurfpt = w * h;
for (t=0 ; t<h ; t++)
{
for (s=0 ; s<w ; s++, surf+=3)
{
us = starts + s*step;
ut = startt + t*step;
// if a line can be traced from surf to facemid, the point is good
for (i=0 ; i<6 ; i++)
{
// calculate texture point
for (j=0 ; j<3 ; j++)
surf[j] = l->texorg[j] + l->textoworld[0][j]*us
+ l->textoworld[1][j]*ut;
if (CastRay (facemid, surf) != -1)
break; // got it
if (i & 1)
{
if (us > mids)
{
us -= 8;
if (us < mids)
us = mids;
}
else
{
us += 8;
if (us > mids)
us = mids;
}
}
else
{
if (ut > midt)
{
ut -= 8;
if (ut < midt)
ut = midt;
}
else
{
ut += 8;
if (ut > midt)
ut = midt;
}
}
// move surf 8 pixels towards the center
VectorSubtract (facemid, surf, move);
VectorNormalize (move);
VectorMA (surf, 8, move, surf);
}
if (i == 2)
c_bad++;
}
}
}
/*
===============================================================================
FACE LIGHTING
===============================================================================
*/
int c_culldistplane, c_proper;
/*
================
SingleLightFace
================
*/
void SingleLightFace (entity_t *light, lightinfo_t *l)
{
vec_t dist;
vec3_t incoming;
vec_t angle;
vec_t add;
vec_t *surf;
qboolean hit;
int mapnum;
int size;
int c, i;
vec3_t rel;
vec3_t spotvec;
vec_t falloff;
vec_t *lightsamp;
VectorSubtract (light->origin, bsp_origin, rel);
dist = scaledist * (DotProduct (rel, l->facenormal) - l->facedist);
// don't bother with lights behind the surface
if (dist <= 0)
return;
// don't bother with light too far away
if (dist > light->light)
{
c_culldistplane++;
return;
}
if (light->targetent)
{
VectorSubtract (light->targetent->origin, light->origin, spotvec);
VectorNormalize (spotvec);
if (!light->angle)
falloff = -cos(20*Q_PI/180);
else
falloff = -cos(light->angle/2*Q_PI/180);
}
else
falloff = 0; // shut up compiler warnings
mapnum = 0;
for (mapnum=0 ; mapnum<l->numlightstyles ; mapnum++)
if (l->lightstyles[mapnum] == light->style)
break;
lightsamp = l->lightmaps[mapnum];
if (mapnum == l->numlightstyles)
{ // init a new light map
if (mapnum == MAXLIGHTMAPS)
{
printf ("WARNING: Too many light styles on a face\n");
return;
}
size = (l->texsize[1]+1)*(l->texsize[0]+1);
for (i=0 ; i<size ; i++)
lightsamp[i] = 0;
}
//
// check it for real
//
hit = false;
c_proper++;
surf = l->surfpt[0];
for (c=0 ; c<l->numsurfpt ; c++, surf+=3)
{
dist = CastRay(light->origin, surf)*scaledist;
if (dist < 0)
continue; // light doesn't reach
VectorSubtract (light->origin, surf, incoming);
VectorNormalize (incoming);
angle = DotProduct (incoming, l->facenormal);
if (light->targetent)
{ // spotlight cutoff
if (DotProduct (spotvec, incoming) > falloff)
continue;
}
angle = (1.0-scalecos) + scalecos*angle;
add = light->light - dist;
add *= angle;
if (add < 0)
continue;
lightsamp[c] += add;
if (lightsamp[c] > 1) // ignore real tiny lights
hit = true;
}
if (mapnum == l->numlightstyles && hit)
{
l->lightstyles[mapnum] = light->style;
l->numlightstyles++; // the style has some real data now
}
}
/*
============
FixMinlight
============
*/
void FixMinlight (lightinfo_t *l)
{
int i, j;
float minlight;
minlight = minlights[l->surfnum];
// if minlight is set, there must be a style 0 light map
if (!minlight)
return;
for (i=0 ; i< l->numlightstyles ; i++)
{
if (l->lightstyles[i] == 0)
break;
}
if (i == l->numlightstyles)
{
if (l->numlightstyles == MAXLIGHTMAPS)
return; // oh well..
for (j=0 ; j<l->numsurfpt ; j++)
l->lightmaps[i][j] = minlight;
l->lightstyles[i] = 0;
l->numlightstyles++;
}
else
{
for (j=0 ; j<l->numsurfpt ; j++)
if ( l->lightmaps[i][j] < minlight)
l->lightmaps[i][j] = minlight;
}
}
/*
============
LightFace
============
*/
void LightFace (int surfnum)
{
dface_t *f;
lightinfo_t l;
int s, t;
int i,j,c;
vec_t total;
int size;
int lightmapwidth, lightmapsize;
byte *out;
vec_t *light;
int w, h;
f = dfaces + surfnum;
//
// some surfaces don't need lightmaps
//
f->lightofs = -1;
for (j=0 ; j<MAXLIGHTMAPS ; j++)
f->styles[j] = 255;
if ( texinfo[f->texinfo].flags & TEX_SPECIAL)
{ // non-lit texture
return;
}
memset (&l, 0, sizeof(l));
l.surfnum = surfnum;
l.face = f;
//
// rotate plane
//
VectorCopy (dplanes[f->planenum].normal, l.facenormal);
l.facedist = dplanes[f->planenum].dist;
if (f->side)
{
VectorSubtract (vec3_origin, l.facenormal, l.facenormal);
l.facedist = -l.facedist;
}
CalcFaceVectors (&l);
CalcFaceExtents (&l);
CalcPoints (&l);
lightmapwidth = l.texsize[0]+1;
size = lightmapwidth*(l.texsize[1]+1);
if (size > SINGLEMAP)
Error ("Bad lightmap size");
for (i=0 ; i<MAXLIGHTMAPS ; i++)
l.lightstyles[i] = 255;
//
// cast all lights
//
l.numlightstyles = 0;
for (i=0 ; i<num_entities ; i++)
{
if (entities[i].light)
SingleLightFace (&entities[i], &l);
}
FixMinlight (&l);
if (!l.numlightstyles)
{ // no light hitting it
return;
}
//
// save out the values
//
for (i=0 ; i <MAXLIGHTMAPS ; i++)
f->styles[i] = l.lightstyles[i];
lightmapsize = size*l.numlightstyles;
out = GetFileSpace (lightmapsize);
f->lightofs = out - filebase;
// extra filtering
h = (l.texsize[1]+1)*2;
w = (l.texsize[0]+1)*2;
for (i=0 ; i< l.numlightstyles ; i++)
{
if (l.lightstyles[i] == 0xff)
Error ("Wrote empty lightmap");
light = l.lightmaps[i];
c = 0;
for (t=0 ; t<=l.texsize[1] ; t++)
for (s=0 ; s<=l.texsize[0] ; s++, c++)
{
if (extrasamples)
{ // filtered sample
total = light[t*2*w+s*2] + light[t*2*w+s*2+1]
+ light[(t*2+1)*w+s*2] + light[(t*2+1)*w+s*2+1];
total *= 0.25;
}
else
total = light[c];
total *= rangescale; // scale before clamping
if (total > 255)
total = 255;
if (total < 0)
Error ("light < 0");
*out++ = total;
}
}
}

66
qutils/LIGHT/THREADS.C Normal file
View File

@@ -0,0 +1,66 @@
#include "cmdlib.h"
#include "threads.h"
#ifdef __alpha
int numthreads = 4;
pthread_mutex_t *my_mutex;
#else
int numthreads = 1;
#endif
void InitThreads (void)
{
#ifdef __alpha
pthread_mutexattr_t mattrib;
my_mutex = malloc (sizeof(*my_mutex));
if (pthread_mutexattr_create (&mattrib) == -1)
Error ("pthread_mutex_attr_create failed");
if (pthread_mutexattr_setkind_np (&mattrib, MUTEX_FAST_NP) == -1)
Error ("pthread_mutexattr_setkind_np failed");
if (pthread_mutex_init (my_mutex, mattrib) == -1)
Error ("pthread_mutex_init failed");
#endif
}
/*
===============
RunThreadsOn
===============
*/
void RunThreadsOn ( threadfunc_t func )
{
#ifdef __alpha
pthread_t work_threads[256];
pthread_addr_t status;
pthread_attr_t attrib;
int i;
if (numthreads == 1)
{
func (NULL);
return;
}
if (pthread_attr_create (&attrib) == -1)
Error ("pthread_attr_create failed");
if (pthread_attr_setstacksize (&attrib, 0x100000) == -1)
Error ("pthread_attr_setstacksize failed");
for (i=0 ; i<numthreads ; i++)
{
if (pthread_create(&work_threads[i], attrib
, (pthread_startroutine_t)func, (pthread_addr_t)i) == -1)
Error ("pthread_create failed");
}
for (i=0 ; i<numthreads ; i++)
{
if (pthread_join (work_threads[i], &status) == -1)
Error ("pthread_join failed");
}
#else
func (NULL);
#endif
}

17
qutils/LIGHT/THREADS.H Normal file
View File

@@ -0,0 +1,17 @@
#ifdef __alpha
#include <pthread.h>
extern pthread_mutex_t *my_mutex;
#define LOCK pthread_mutex_lock (my_mutex)
#define UNLOCK pthread_mutex_unlock (my_mutex)
#else
#define LOCK
#define UNLOCK
#endif
extern int numthreads;
typedef void (threadfunc_t) (void *);
void InitThreads (void);
void RunThreadsOn ( threadfunc_t func );

197
qutils/LIGHT/TRACE.C Normal file
View File

@@ -0,0 +1,197 @@
// trace.c
#include "light.h"
typedef struct tnode_s
{
int type;
vec3_t normal;
float dist;
int children[2];
int pad;
} tnode_t;
tnode_t *tnodes, *tnode_p;
/*
==============
MakeTnode
Converts the disk node structure into the efficient tracing structure
==============
*/
void MakeTnode (int nodenum)
{
tnode_t *t;
dplane_t *plane;
int i;
dnode_t *node;
t = tnode_p++;
node = dnodes + nodenum;
plane = dplanes + node->planenum;
t->type = plane->type;
VectorCopy (plane->normal, t->normal);
t->dist = plane->dist;
for (i=0 ; i<2 ; i++)
{
if (node->children[i] < 0)
t->children[i] = dleafs[-node->children[i] - 1].contents;
else
{
t->children[i] = tnode_p - tnodes;
MakeTnode (node->children[i]);
}
}
}
/*
=============
MakeTnodes
Loads the node structure out of a .bsp file to be used for light occlusion
=============
*/
void MakeTnodes (dmodel_t *bm)
{
tnode_p = tnodes = malloc(numnodes * sizeof(tnode_t));
MakeTnode (0);
}
/*
==============================================================================
LINE TRACING
The major lighting operation is a point to point visibility test, performed
by recursive subdivision of the line by the BSP tree.
==============================================================================
*/
typedef struct
{
vec3_t backpt;
int side;
int node;
} tracestack_t;
/*
==============
TestLine
==============
*/
qboolean TestLine (vec3_t start, vec3_t stop)
{
int node;
float front, back;
tracestack_t *tstack_p;
int side;
float frontx,fronty, frontz, backx, backy, backz;
tracestack_t tracestack[64];
tnode_t *tnode;
frontx = start[0];
fronty = start[1];
frontz = start[2];
backx = stop[0];
backy = stop[1];
backz = stop[2];
tstack_p = tracestack;
node = 0;
while (1)
{
while (node < 0 && node != CONTENTS_SOLID)
{
// pop up the stack for a back side
tstack_p--;
if (tstack_p < tracestack)
return true;
node = tstack_p->node;
// set the hit point for this plane
frontx = backx;
fronty = backy;
frontz = backz;
// 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)
return false; // DONE!
tnode = &tnodes[node];
switch (tnode->type)
{
case PLANE_X:
front = frontx - tnode->dist;
back = backx - tnode->dist;
break;
case PLANE_Y:
front = fronty - tnode->dist;
back = backy - tnode->dist;
break;
case PLANE_Z:
front = frontz - tnode->dist;
back = backz - tnode->dist;
break;
default:
front = (frontx*tnode->normal[0] + fronty*tnode->normal[1] + frontz*tnode->normal[2]) - tnode->dist;
back = (backx*tnode->normal[0] + backy*tnode->normal[1] + backz*tnode->normal[2]) - tnode->dist;
break;
}
if (front > -ON_EPSILON && back > -ON_EPSILON)
// if (front > 0 && back > 0)
{
node = tnode->children[0];
continue;
}
if (front < ON_EPSILON && back < ON_EPSILON)
// if (front <= 0 && back <= 0)
{
node = tnode->children[1];
continue;
}
side = front < 0;
front = 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++;
backx = frontx + front*(backx-frontx);
backy = fronty + front*(backy-fronty);
backz = frontz + front*(backz-frontz);
node = tnode->children[side];
}
}