UFO: Alien Invasion
Loading...
Searching...
No Matches
lightmap.cpp File Reference
#include "lighting.h"
#include "bsp.h"
Include dependency graph for lightmap.cpp:

Go to the source code of this file.

Data Structures

struct  lightinfo_t
 lightinfo is a temporary bucket for lighting calculations More...
struct  extents_t
 face extents More...
struct  facelight_t
 buckets for sample accumulation - clipped by the surface More...
struct  light_t
 a light source More...

Macros

#define sun_angles   config.sun_angles[config.compile_for_day]
#define sun_normal   config.sun_normal[config.compile_for_day]
#define sun_color   config.sun_color[config.compile_for_day]
#define sun_intensity   config.sun_intensity[config.compile_for_day]
#define sun_ambient_color   config.sun_ambient_color[config.compile_for_day]
#define SAMPLE_NUDGE   0.25
#define MAX_VERT_FACES   256
#define MAX_SAMPLES   5
#define SOFT_SAMPLES   4
#define TGA_HEADER_SIZE   18

Enumerations

enum  emittype_t { emit_surface , emit_point , emit_spotlight }
 Different types of sources emitting light. More...

Functions

static void BuildFaceExtents (void)
 Populates face_extents for all dBspSurface_t, prior to light creation. This is done so that sample positions may be nudged outward along the face normal and towards the face center to help with traces.
static void CalcLightinfoExtents (lightinfo_t *l)
static void CalcLightinfoVectors (lightinfo_t *l)
 Fills in texorg, worldtotex. and textoworld.
static void CalcPoints (lightinfo_t *l, float sofs, float tofs)
 For each texture aligned grid point, back project onto the plane to get the world xyz value of the sample point.
void BuildLights (void)
 Create lights out of patches and entity lights.
static bool TR_TestLineSingleTile (const vec3_t start, const vec3_t stop, int *headhint)
 Checks traces against a single-tile map, optimized for ufo2map. This trace is only for visible levels.
static void GatherSampleSunlight (const vec3_t pos, const vec3_t normal, float *sample, float *direction, float scale, int *headhint)
 A follow-up to GatherSampleLight, simply trace along the sun normal, adding sunlight.
static void GatherSampleLight (vec3_t pos, const vec3_t normal, float *sample, float *direction, float scale, int *headhints)
static void NudgeSamplePosition (const vec3_t in, const vec3_t normal, const vec3_t center, vec3_t out)
 Move the incoming sample position towards the surface center and along the surface normal to reduce false-positive traces.
static void FacesWithVert (int vert, int *faces, int *nfaces)
 Populate faces with indexes of all dBspFace_t's referencing the specified edge.
void BuildVertexNormals (void)
 Calculate per-vertex (instead of per-plane) normal vectors. This is done by finding all of the faces which share a given vertex, and calculating a weighted average of their normals.
static void SampleNormal (const lightinfo_t *l, const vec3_t pos, vec3_t normal)
 For Phong-shaded samples, interpolate the vertex normals for the surface in question, weighting them according to their proximity to the sample position.
void BuildFacelights (unsigned int facenum)
static void WriteTGA24 (const char *filename, const byte *data, int width, int height, int offset)
static void CalcTextureSize (const dBspSurface_t *s, vec2_t texsize, int scale)
 Calculates the texture width for the lightmap texture. This depends on the surface mins and maxs and the texture scale.
static void ExportLightmap (const char *path, const char *name, bool day)
 Export all the faces for one particular lightmap (day or night).
void ExportLightmaps (const char *bspFileName)
 Export the day and night lightmap and direction data for the given map.
void FinalLightFace (unsigned int facenum)
 Add the indirect lighting on top of the direct lighting and save into final map format.

Variables

vec3_t face_offset [MAX_MAP_FACES]
static extents_t face_extents [MAX_MAP_FACES]
static facelight_t facelight [LIGHTMAP_MAX][MAX_MAP_FACES]
static light_tlights [LIGHTMAP_MAX]
static int numlights [LIGHTMAP_MAX]
static const float sampleofs [2][MAX_SAMPLES][2]
static const vec3_t luminosity = {0.2125, 0.7154, 0.0721}

Macro Definition Documentation

◆ MAX_SAMPLES

#define MAX_SAMPLES   5

Definition at line 742 of file lightmap.cpp.

Referenced by BuildFacelights().

◆ MAX_VERT_FACES

#define MAX_VERT_FACES   256

Definition at line 633 of file lightmap.cpp.

Referenced by BuildVertexNormals(), FacesWithVert(), and SampleNormal().

◆ SAMPLE_NUDGE

#define SAMPLE_NUDGE   0.25

Definition at line 613 of file lightmap.cpp.

Referenced by NudgeSamplePosition().

◆ SOFT_SAMPLES

#define SOFT_SAMPLES   4

Definition at line 743 of file lightmap.cpp.

Referenced by BuildFacelights().

◆ sun_ambient_color

#define sun_ambient_color   config.sun_ambient_color[config.compile_for_day]

Definition at line 32 of file lightmap.cpp.

Referenced by BuildLights(), and FinalLightFace().

◆ sun_angles

#define sun_angles   config.sun_angles[config.compile_for_day]

Definition at line 28 of file lightmap.cpp.

Referenced by BuildLights().

◆ sun_color

#define sun_color   config.sun_color[config.compile_for_day]

Definition at line 30 of file lightmap.cpp.

Referenced by BuildLights(), and GatherSampleSunlight().

◆ sun_intensity

#define sun_intensity   config.sun_intensity[config.compile_for_day]

Definition at line 31 of file lightmap.cpp.

Referenced by BuildLights(), and GatherSampleSunlight().

◆ sun_normal

#define sun_normal   config.sun_normal[config.compile_for_day]

Definition at line 29 of file lightmap.cpp.

Referenced by BuildLights(), and GatherSampleSunlight().

◆ TGA_HEADER_SIZE

#define TGA_HEADER_SIZE   18

Definition at line 871 of file lightmap.cpp.

Referenced by WriteTGA24().

Enumeration Type Documentation

◆ emittype_t

enum emittype_t

Different types of sources emitting light.

Enumerator
emit_surface 

surface light via SURF_LIGHT

emit_point 

point light given via light entity

emit_spotlight 

spotlight given via light entity (+target) or via light_spot entity

Definition at line 267 of file lightmap.cpp.

Function Documentation

◆ BuildFaceExtents()

void BuildFaceExtents ( void )
static

Populates face_extents for all dBspSurface_t, prior to light creation. This is done so that sample positions may be nudged outward along the face normal and towards the face center to help with traces.

Definition at line 72 of file lightmap.cpp.

References curTile, DotProduct, face_extents, dBspSurface_t::firstedge, i, dBspSurface_t::numedges, dBspSurface_t::texinfo, v, dBspTexinfo_t::vecs, and VectorSet.

Referenced by BuildVertexNormals(), and ExportLightmaps().

◆ BuildFacelights()

◆ BuildLights()

◆ BuildVertexNormals()

void BuildVertexNormals ( void )

Calculate per-vertex (instead of per-plane) normal vectors. This is done by finding all of the faces which share a given vertex, and calculating a weighted average of their normals.

Definition at line 673 of file lightmap.cpp.

References BuildFaceExtents(), curTile, face_extents, FacesWithVert(), i, MAX_VERT_FACES, extents_t::maxs, extents_t::mins, dBspPlane_t::normal, dBspSurface_t::planenum, scale, dBspSurface_t::side, VectorAdd, VectorClear, VectorLength(), VectorNormalize(), VectorScale, and VectorSubtract.

Referenced by LightWorld(), and main().

◆ CalcLightinfoExtents()

void CalcLightinfoExtents ( lightinfo_t * l)
static

◆ CalcLightinfoVectors()

◆ CalcPoints()

void CalcPoints ( lightinfo_t * l,
float sofs,
float tofs )
static

For each texture aligned grid point, back project onto the plane to get the world xyz value of the sample point.

Parameters
[in,out]lThe resulting lightinfo data
[in]sofsThe sample offset for the s coordinates
[in]tofsThe sample offset for the t coordinates

Definition at line 230 of file lightmap.cpp.

References lightinfo_t::step, lightinfo_t::surfpt, lightinfo_t::texmins, lightinfo_t::texorg, lightinfo_t::texsize, and lightinfo_t::textoworld.

Referenced by BuildFacelights().

◆ CalcTextureSize()

void CalcTextureSize ( const dBspSurface_t * s,
vec2_t texsize,
int scale )
static

Calculates the texture width for the lightmap texture. This depends on the surface mins and maxs and the texture scale.

Parameters
[in]sThe surface to calculate the lightmap size for
[out]texsizeThe resulting texture size vector. First value is width, second value is height
[in]scaleThe scale (1/scale) of the lightmap texture in relation to the surface texture

Definition at line 912 of file lightmap.cpp.

References curTile, face_extents, i, and scale.

Referenced by ExportLightmap().

◆ ExportLightmap()

void ExportLightmap ( const char * path,
const char * name,
bool day )
static

Export all the faces for one particular lightmap (day or night).

Parameters
pathThe path to write the files into
nameThe name of the map to export the lightmap for
daytrue to export the day lightmap data, false to export the night lightmap data

Definition at line 931 of file lightmap.cpp.

References CalcTextureSize(), Com_Printf(), Com_sprintf(), curTile, i, dBspSurface_t::lightofs, MAX_QPATH, name, scale, Vector2NotEmpty, and WriteTGA24().

Referenced by ExportLightmaps().

◆ ExportLightmaps()

void ExportLightmaps ( const char * bspFileName)

Export the day and night lightmap and direction data for the given map.

Note
The bsp file must already be loaded.
Parameters
bspFileNameThe path of the loaded bsp file.

Definition at line 963 of file lightmap.cpp.

References BuildFaceExtents(), Com_FilePath(), Com_Printf(), Com_SkipPath(), Com_StripExtension(), ExportLightmap(), and MAX_QPATH.

Referenced by main().

◆ FacesWithVert()

void FacesWithVert ( int vert,
int * faces,
int * nfaces )
static

Populate faces with indexes of all dBspFace_t's referencing the specified edge.

Parameters
[in]vertThe index of the vertex to check
[out]facesThe index table of the faces found
[out]nfacesThe number of dBspFace_t's referencing edge

Definition at line 641 of file lightmap.cpp.

References curTile, dBspSurface_t::firstedge, i, MAX_VERT_FACES, dBspSurface_t::numedges, SURF_PHONG, dBspTexinfo_t::surfaceFlags, Sys_Error(), dBspSurface_t::texinfo, and v.

Referenced by BuildVertexNormals().

◆ FinalLightFace()

void FinalLightFace ( unsigned int facenum)

Add the indirect lighting on top of the direct lighting and save into final map format.

See also
BuildFacelights

Definition at line 987 of file lightmap.cpp.

References config, curTile, dest, facelight_t::directions, DotProduct, f, facelight, luminosity, MAX_MAP_LIGHTING, facelight_t::numsamples, facelight_t::samples, sun_ambient_color, SURF_WARP, Sys_Error(), ThreadLock(), ThreadUnlock(), VectorAdd, VectorCopy, VectorMix(), VectorScale, and VectorSet.

Referenced by LightWorld().

◆ GatherSampleLight()

void GatherSampleLight ( vec3_t pos,
const vec3_t normal,
float * sample,
float * direction,
float scale,
int * headhints )
static
Parameters
[in]posThe point in the world that receives the light
[in]normalThe light direction (normal vector)
[out]sampleThe sample color
[out]direction
[in]scaleis the normalizer for multisampling
[in,out]headhintsAn array of theads for each light to optimize the tracing

Definition at line 548 of file lightmap.cpp.

References light_t::color, config, DotProduct, emit_point, emit_spotlight, emit_surface, GatherSampleSunlight(), light_t::intensity, lights, light_t::next, light_t::normal, light_t::origin, scale, light_t::stopdot, Sys_Error(), TR_TestLineSingleTile(), light_t::type, VectorMA(), VectorMix(), VectorNormalize(), and VectorSubtract.

Referenced by BuildFacelights().

◆ GatherSampleSunlight()

void GatherSampleSunlight ( const vec3_t pos,
const vec3_t normal,
float * sample,
float * direction,
float scale,
int * headhint )
static

A follow-up to GatherSampleLight, simply trace along the sun normal, adding sunlight.

Definition at line 507 of file lightmap.cpp.

References DotProduct, scale, sun_color, sun_intensity, sun_normal, TR_TestLineSingleTile(), VectorMA(), and VectorMix().

Referenced by GatherSampleLight().

◆ NudgeSamplePosition()

void NudgeSamplePosition ( const vec3_t in,
const vec3_t normal,
const vec3_t center,
vec3_t out )
inlinestatic

Move the incoming sample position towards the surface center and along the surface normal to reduce false-positive traces.

Definition at line 619 of file lightmap.cpp.

References SAMPLE_NUDGE, VectorCopy, VectorMA(), VectorNormalize(), and VectorSubtract.

Referenced by BuildFacelights().

◆ SampleNormal()

void SampleNormal ( const lightinfo_t * l,
const vec3_t pos,
vec3_t normal )
static

For Phong-shaded samples, interpolate the vertex normals for the surface in question, weighting them according to their proximity to the sample position.

Todo
Implement it (just clones the normal of nearest vertex for now)

Definition at line 714 of file lightmap.cpp.

References curTile, lightinfo_t::face, dBspSurface_t::firstedge, i, MAX_VERT_FACES, dBspSurface_t::numedges, v, VectorCopy, VectorLength(), and VectorSubtract.

Referenced by BuildFacelights().

◆ TR_TestLineSingleTile()

bool TR_TestLineSingleTile ( const vec3_t start,
const vec3_t stop,
int * headhint )
static

Checks traces against a single-tile map, optimized for ufo2map. This trace is only for visible levels.

Parameters
[in]startThe position to start the trace.
[in]stopThe position where the trace ends.
[in,out]headhintThe t-head where the previous trace found an obstacle.
See also
TR_TestLine
GatherSampleLight
Returns
false if not blocked

Definition at line 471 of file lightmap.cpp.

References curTile, i, level, LEVEL_LASTLIGHTBLOCKING, mapTiles, and TR_TestLine_r().

Referenced by GatherSampleLight(), and GatherSampleSunlight().

◆ WriteTGA24()

void WriteTGA24 ( const char * filename,
const byte * data,
int width,
int height,
int offset )
static

Definition at line 872 of file lightmap.cpp.

References data, FILE_WRITE, FS_OpenFile(), FS_Write(), i, Mem_AllocTypeN, Mem_Free, Sys_Error(), and TGA_HEADER_SIZE.

Referenced by ExportLightmap().

Variable Documentation

◆ face_extents

◆ face_offset

vec3_t face_offset[MAX_MAP_FACES]

for rotating bmodels

Definition at line 34 of file lightmap.cpp.

Referenced by BuildFacelights(), and BuildPatches().

◆ facelight

Definition at line 264 of file lightmap.cpp.

Referenced by BuildFacelights(), and FinalLightFace().

◆ lights

light_t* lights[LIGHTMAP_MAX]
static

Definition at line 285 of file lightmap.cpp.

Referenced by BuildLights(), GatherSampleLight(), and R_EnableModelLights().

◆ luminosity

const vec3_t luminosity = {0.2125, 0.7154, 0.0721}
static

Definition at line 980 of file lightmap.cpp.

Referenced by FinalLightFace().

◆ numlights

int numlights[LIGHTMAP_MAX]
static

Definition at line 286 of file lightmap.cpp.

Referenced by BuildFacelights(), and BuildLights().

◆ sampleofs

const float sampleofs[2][MAX_SAMPLES][2]
static
Initial value:
= {
{{0.0, 0.0}, {-0.125, -0.125}, {0.125, -0.125}, {0.125, 0.125}, {-0.125, 0.125}},
{{-0.66, 0.33}, {-0.33, -0.66}, {0.33, 0.66}, {0.66, -0.33},{0.0,0.0}}
}

Definition at line 744 of file lightmap.cpp.

Referenced by BuildFacelights().