This repository has been archived on 2022-06-27. You can view files and clone it, but cannot push or open issues or pull requests.
Xash3DArchive/engine/client/gl_rlight.c

490 lines
12 KiB
C
Raw Permalink Normal View History

2011-05-09 22:00:00 +02:00
/*
gl_rlight.c - dynamic and static lights
Copyright (C) 2010 Uncle Mike
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 3 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.
*/
2010-12-02 22:00:00 +01:00
#include "common.h"
#include "client.h"
2010-12-22 22:00:00 +01:00
#include "mathlib.h"
2010-12-02 22:00:00 +01:00
#include "gl_local.h"
2010-12-25 22:00:00 +01:00
#include "pm_local.h"
2010-12-16 22:00:00 +01:00
#include "studio.h"
2010-12-02 22:00:00 +01:00
2010-12-06 22:00:00 +01:00
/*
=============================================================================
DYNAMIC LIGHTS
=============================================================================
*/
2010-12-11 22:00:00 +01:00
/*
==================
2016-11-22 22:00:00 +01:00
CL_RunLightStyles
2010-12-11 22:00:00 +01:00
==================
*/
2016-11-22 22:00:00 +01:00
void CL_RunLightStyles( void )
2010-12-11 22:00:00 +01:00
{
int i, k, flight, clight;
2017-03-26 23:00:00 +02:00
float l, lerpfrac, backlerp;
2017-02-15 22:00:00 +01:00
float frametime = (cl.time - cl.oldtime);
2011-04-12 22:00:00 +02:00
float scale;
2010-12-11 22:00:00 +01:00
lightstyle_t *ls;
2017-03-16 22:00:00 +01:00
if( !cl.worldmodel ) return;
2010-12-22 22:00:00 +01:00
2011-04-12 22:00:00 +02:00
scale = r_lighting_modulate->value;
2010-12-11 22:00:00 +01:00
// light animations
// 'm' is normal light, 'a' is no light, 'z' is double bright
for( i = 0, ls = cl.lightstyles; i < MAX_LIGHTSTYLES; i++, ls++ )
{
2018-05-08 23:00:00 +02:00
if( !cl.worldmodel->lightdata )
2010-12-11 22:00:00 +01:00
{
2016-11-22 22:00:00 +01:00
tr.lightstylevalue[i] = 256 * 256;
2010-12-11 22:00:00 +01:00
continue;
}
2017-02-15 22:00:00 +01:00
if( !cl.paused && frametime <= 0.1f )
ls->time += frametime; // evaluate local time
2013-09-14 22:00:00 +02:00
2016-11-22 22:00:00 +01:00
flight = (int)Q_floor( ls->time * 10 );
clight = (int)Q_ceil( ls->time * 10 );
2013-09-14 22:00:00 +02:00
lerpfrac = ( ls->time * 10 ) - flight;
backlerp = 1.0f - lerpfrac;
2010-12-11 22:00:00 +01:00
if( !ls->length )
{
2016-11-22 22:00:00 +01:00
tr.lightstylevalue[i] = 256 * scale;
2010-12-11 22:00:00 +01:00
continue;
}
else if( ls->length == 1 )
{
// single length style so don't bother interpolating
2016-11-22 22:00:00 +01:00
tr.lightstylevalue[i] = ls->map[0] * 22 * scale;
2010-12-11 22:00:00 +01:00
continue;
}
2018-09-10 23:00:00 +02:00
else if( !ls->interp || !CVAR_TO_BOOL( cl_lightstyle_lerping ))
2010-12-11 22:00:00 +01:00
{
2016-11-22 22:00:00 +01:00
tr.lightstylevalue[i] = ls->map[flight%ls->length] * 22 * scale;
2010-12-11 22:00:00 +01:00
continue;
}
// interpolate animating light
// frame just gone
k = ls->map[flight % ls->length];
2011-04-12 22:00:00 +02:00
l = (float)( k * 22.0f ) * backlerp;
2010-12-11 22:00:00 +01:00
// upcoming frame
k = ls->map[clight % ls->length];
2011-04-12 22:00:00 +02:00
l += (float)( k * 22.0f ) * lerpfrac;
2010-12-11 22:00:00 +01:00
2016-11-22 22:00:00 +01:00
tr.lightstylevalue[i] = (int)l * scale;
2010-12-11 22:00:00 +01:00
}
}
2010-12-06 22:00:00 +01:00
/*
=============
R_MarkLights
=============
*/
void R_MarkLights( dlight_t *light, int bit, mnode_t *node )
2010-12-02 22:00:00 +01:00
{
2010-12-06 22:00:00 +01:00
float dist;
msurface_t *surf;
int i;
if( node->contents < 0 )
return;
2010-12-11 22:00:00 +01:00
dist = PlaneDiff( light->origin, node->plane );
2010-12-06 22:00:00 +01:00
if( dist > light->radius )
{
R_MarkLights( light, bit, node->children[0] );
return;
}
if( dist < -light->radius )
{
R_MarkLights( light, bit, node->children[1] );
return;
}
// mark the polygons
2011-07-07 22:00:00 +02:00
surf = RI.currentmodel->surfaces + node->firstsurface;
2010-12-06 22:00:00 +01:00
for( i = 0; i < node->numsurfaces; i++, surf++ )
{
2017-03-14 22:00:00 +01:00
if( !BoundsAndSphereIntersect( surf->info->mins, surf->info->maxs, light->origin, light->radius ))
2011-04-12 22:00:00 +02:00
continue; // no intersection
2010-12-06 22:00:00 +01:00
if( surf->dlightframe != tr.dlightframecount )
{
surf->dlightbits = 0;
surf->dlightframe = tr.dlightframecount;
}
surf->dlightbits |= bit;
}
R_MarkLights( light, bit, node->children[0] );
R_MarkLights( light, bit, node->children[1] );
2010-12-02 22:00:00 +01:00
}
2010-12-06 22:00:00 +01:00
/*
=============
R_PushDlights
=============
*/
void R_PushDlights( void )
{
dlight_t *l;
int i;
2011-08-14 22:00:00 +02:00
tr.dlightframecount = tr.framecount;
2010-12-06 22:00:00 +01:00
l = cl_dlights;
2011-07-07 22:00:00 +02:00
RI.currententity = clgame.entities;
RI.currentmodel = RI.currententity->model;
2010-12-06 22:00:00 +01:00
for( i = 0; i < MAX_DLIGHTS; i++, l++ )
{
if( l->die < cl.time || !l->radius )
continue;
2011-04-12 22:00:00 +02:00
2017-03-13 22:00:00 +01:00
if( GL_FrustumCullSphere( &RI.frustum, l->origin, l->radius, 15 ))
2011-04-12 22:00:00 +02:00
continue;
2011-07-07 22:00:00 +02:00
R_MarkLights( l, 1<<i, RI.currentmodel->nodes );
2010-12-06 22:00:00 +01:00
}
}
2010-12-16 22:00:00 +01:00
2011-11-02 21:00:00 +01:00
/*
=============
R_CountDlights
=============
*/
int R_CountDlights( void )
{
dlight_t *l;
int i, numDlights = 0;
for( i = 0, l = cl_dlights; i < MAX_DLIGHTS; i++, l++ )
{
if( l->die < cl.time || !l->radius )
continue;
numDlights++;
}
return numDlights;
}
/*
=============
R_CountSurfaceDlights
=============
*/
int R_CountSurfaceDlights( msurface_t *surf )
{
int i, numDlights = 0;
2011-12-04 21:00:00 +01:00
for( i = 0; i < MAX_DLIGHTS; i++ )
2011-11-02 21:00:00 +01:00
{
if(!( surf->dlightbits & BIT( i )))
continue; // not lit by this light
numDlights++;
}
return numDlights;
}
2010-12-16 22:00:00 +01:00
/*
=======================================================================
2011-03-03 22:00:00 +01:00
AMBIENT LIGHTING
2010-12-16 22:00:00 +01:00
=======================================================================
*/
2017-03-06 22:00:00 +01:00
static vec3_t g_trace_lightspot;
2018-05-20 23:00:00 +02:00
static vec3_t g_trace_lightvec;
2018-04-16 23:00:00 +02:00
static float g_trace_fraction;
2010-12-16 22:00:00 +01:00
/*
=================
R_RecursiveLightPoint
=================
*/
2018-04-16 23:00:00 +02:00
static qboolean R_RecursiveLightPoint( model_t *model, mnode_t *node, float p1f, float p2f, colorVec *cv, const vec3_t start, const vec3_t end )
2010-12-16 22:00:00 +01:00
{
2017-03-06 22:00:00 +01:00
float front, back, frac, midf;
2017-12-01 22:00:00 +01:00
int i, map, side, size;
float ds, dt, s, t;
int sample_size;
2018-05-20 23:00:00 +02:00
color24 *lm, *dm;
2017-11-04 22:00:00 +01:00
mextrasurf_t *info;
2010-12-16 22:00:00 +01:00
msurface_t *surf;
mtexinfo_t *tex;
2018-05-20 23:00:00 +02:00
matrix3x4 tbn;
2010-12-16 22:00:00 +01:00
vec3_t mid;
// didn't hit anything
2011-03-31 22:00:00 +02:00
if( !node || node->contents < 0 )
2017-03-06 22:00:00 +01:00
{
cv->r = cv->g = cv->b = cv->a = 0;
2010-12-16 22:00:00 +01:00
return false;
2017-03-06 22:00:00 +01:00
}
2010-12-16 22:00:00 +01:00
// calculate mid point
front = PlaneDiff( start, node->plane );
back = PlaneDiff( end, node->plane );
side = front < 0;
if(( back < 0 ) == side )
2018-04-16 23:00:00 +02:00
return R_RecursiveLightPoint( model, node->children[side], p1f, p2f, cv, start, end );
2010-12-16 22:00:00 +01:00
frac = front / ( front - back );
VectorLerp( start, frac, end, mid );
2017-03-06 22:00:00 +01:00
midf = p1f + ( p2f - p1f ) * frac;
2010-12-16 22:00:00 +01:00
// co down front side
2018-04-16 23:00:00 +02:00
if( R_RecursiveLightPoint( model, node->children[side], p1f, midf, cv, start, mid ))
2010-12-16 22:00:00 +01:00
return true; // hit something
if(( back < 0 ) == side )
2017-03-06 22:00:00 +01:00
{
cv->r = cv->g = cv->b = cv->a = 0;
return false; // didn't hit anything
}
2011-08-14 22:00:00 +02:00
2010-12-16 22:00:00 +01:00
// check for impact on this node
surf = model->surfaces + node->firstsurface;
2017-03-06 22:00:00 +01:00
VectorCopy( mid, g_trace_lightspot );
2010-12-16 22:00:00 +01:00
for( i = 0; i < node->numsurfaces; i++, surf++ )
{
2017-12-01 22:00:00 +01:00
int smax, tmax;
2010-12-16 22:00:00 +01:00
tex = surf->texinfo;
2017-11-04 22:00:00 +01:00
info = surf->info;
2010-12-16 22:00:00 +01:00
2017-03-11 22:00:00 +01:00
if( FBitSet( surf->flags, SURF_DRAWTILED ))
2010-12-16 22:00:00 +01:00
continue; // no lightmaps
2017-12-01 22:00:00 +01:00
s = DotProduct( mid, info->lmvecs[0] ) + info->lmvecs[0][3];
t = DotProduct( mid, info->lmvecs[1] ) + info->lmvecs[1][3];
if( s < info->lightmapmins[0] || t < info->lightmapmins[1] )
continue;
2010-12-16 22:00:00 +01:00
2017-12-01 22:00:00 +01:00
ds = s - info->lightmapmins[0];
dt = t - info->lightmapmins[1];
if ( ds > info->lightextents[0] || dt > info->lightextents[1] )
2010-12-16 22:00:00 +01:00
continue;
2017-03-06 22:00:00 +01:00
cv->r = cv->g = cv->b = cv->a = 0;
2010-12-16 22:00:00 +01:00
if( !surf->samples )
return true;
2017-03-11 22:00:00 +01:00
sample_size = Mod_SampleSizeForFace( surf );
2017-12-01 22:00:00 +01:00
smax = (info->lightextents[0] / sample_size) + 1;
tmax = (info->lightextents[1] / sample_size) + 1;
ds /= sample_size;
dt /= sample_size;
2017-03-11 22:00:00 +01:00
2017-12-01 22:00:00 +01:00
lm = surf->samples + Q_rint( dt ) * smax + Q_rint( ds );
2017-03-06 22:00:00 +01:00
g_trace_fraction = midf;
2017-12-01 22:00:00 +01:00
size = smax * tmax;
2018-05-20 23:00:00 +02:00
dm = NULL;
if( surf->info->deluxemap )
{
vec3_t faceNormal;
if( FBitSet( surf->flags, SURF_PLANEBACK ))
VectorNegate( surf->plane->normal, faceNormal );
else VectorCopy( surf->plane->normal, faceNormal );
// compute face TBN
#if 1
Vector4Set( tbn[0], surf->info->lmvecs[0][0], surf->info->lmvecs[0][1], surf->info->lmvecs[0][2], 0.0f );
Vector4Set( tbn[1], -surf->info->lmvecs[1][0], -surf->info->lmvecs[1][1], -surf->info->lmvecs[1][2], 0.0f );
Vector4Set( tbn[2], faceNormal[0], faceNormal[1], faceNormal[2], 0.0f );
#else
Vector4Set( tbn[0], surf->info->lmvecs[0][0], -surf->info->lmvecs[1][0], faceNormal[0], 0.0f );
Vector4Set( tbn[1], surf->info->lmvecs[0][1], -surf->info->lmvecs[1][1], faceNormal[1], 0.0f );
Vector4Set( tbn[2], surf->info->lmvecs[0][2], -surf->info->lmvecs[1][2], faceNormal[2], 0.0f );
#endif
VectorNormalize( tbn[0] );
VectorNormalize( tbn[1] );
VectorNormalize( tbn[2] );
dm = surf->info->deluxemap + Q_rint( dt ) * smax + Q_rint( ds );
}
2010-12-16 22:00:00 +01:00
for( map = 0; map < MAXLIGHTMAPS && surf->styles[map] != 255; map++ )
{
2016-11-22 22:00:00 +01:00
uint scale = tr.lightstylevalue[surf->styles[map]];
2010-12-16 22:00:00 +01:00
2017-04-01 23:00:00 +02:00
if( tr.ignore_lightgamma )
{
cv->r += lm->r * scale;
cv->g += lm->g * scale;
cv->b += lm->b * scale;
}
else
{
cv->r += LightToTexGamma( lm->r ) * scale;
cv->g += LightToTexGamma( lm->g ) * scale;
cv->b += LightToTexGamma( lm->b ) * scale;
}
2010-12-16 22:00:00 +01:00
lm += size; // skip to next lightmap
2018-05-20 23:00:00 +02:00
if( dm != NULL )
{
vec3_t srcNormal, lightNormal;
2018-06-10 23:00:00 +02:00
float f = (1.0f / 128.0f);
2018-05-20 23:00:00 +02:00
2018-06-10 23:00:00 +02:00
VectorSet( srcNormal, ((float)dm->r - 128.0f) * f, ((float)dm->g - 128.0f) * f, ((float)dm->b - 128.0f) * f );
2018-05-20 23:00:00 +02:00
Matrix3x4_VectorIRotate( tbn, srcNormal, lightNormal ); // turn to world space
VectorScale( lightNormal, (float)scale * -1.0f, lightNormal ); // turn direction from light
VectorAdd( g_trace_lightvec, lightNormal, g_trace_lightvec );
dm += size; // skip to next deluxmap
}
2010-12-16 22:00:00 +01:00
}
2017-03-06 22:00:00 +01:00
2010-12-16 22:00:00 +01:00
return true;
}
// go down back side
2018-04-16 23:00:00 +02:00
return R_RecursiveLightPoint( model, node->children[!side], midf, p2f, cv, mid, end );
2012-02-11 21:00:00 +01:00
}
2011-08-14 22:00:00 +02:00
/*
=================
2017-03-06 22:00:00 +01:00
R_LightVec
2011-08-14 22:00:00 +02:00
2017-03-06 22:00:00 +01:00
check bspmodels to get light from
2011-08-14 22:00:00 +02:00
=================
*/
2018-09-20 23:00:00 +02:00
colorVec R_LightVecInternal( const vec3_t start, const vec3_t end, vec3_t lspot, vec3_t lvec )
2011-08-14 22:00:00 +02:00
{
2017-03-06 22:00:00 +01:00
float last_fraction;
int i, maxEnts = 1;
colorVec light, cv;
2012-02-11 21:00:00 +01:00
2018-04-16 23:00:00 +02:00
if( lspot ) VectorClear( lspot );
2018-05-20 23:00:00 +02:00
if( lvec ) VectorClear( lvec );
2018-04-16 23:00:00 +02:00
2017-07-05 23:00:00 +02:00
if( cl.worldmodel && cl.worldmodel->lightdata )
2010-12-16 22:00:00 +01:00
{
2017-03-16 22:00:00 +01:00
light.r = light.g = light.b = light.a = 0;
2017-03-06 22:00:00 +01:00
last_fraction = 1.0f;
2010-12-16 22:00:00 +01:00
2017-03-06 22:00:00 +01:00
// get light from bmodels too
2018-05-20 23:00:00 +02:00
if( CVAR_TO_BOOL( r_lighting_extended ))
2017-03-06 22:00:00 +01:00
maxEnts = clgame.pmove->numphysent;
2010-12-16 22:00:00 +01:00
2018-01-21 22:00:00 +01:00
// check all the bsp-models
2017-03-06 22:00:00 +01:00
for( i = 0; i < maxEnts; i++ )
2011-03-20 22:00:00 +01:00
{
2017-03-06 22:00:00 +01:00
physent_t *pe = &clgame.pmove->physents[i];
vec3_t offset, start_l, end_l;
mnode_t *pnodes;
matrix4x4 matrix;
if( !pe->model || pe->model->type != mod_brush )
continue; // skip non-bsp models
pnodes = &pe->model->nodes[pe->model->hulls[0].firstclipnode];
VectorSubtract( pe->model->hulls[0].clip_mins, vec3_origin, offset );
VectorAdd( offset, pe->origin, offset );
VectorSubtract( start, offset, start_l );
VectorSubtract( end, offset, end_l );
// rotate start and end into the models frame of reference
if( !VectorIsNull( pe->angles ))
{
Matrix4x4_CreateFromEntity( matrix, pe->angles, offset, 1.0f );
Matrix4x4_VectorITransform( matrix, start, start_l );
Matrix4x4_VectorITransform( matrix, end, end_l );
}
VectorClear( g_trace_lightspot );
2018-05-20 23:00:00 +02:00
VectorClear( g_trace_lightvec );
2017-03-06 22:00:00 +01:00
g_trace_fraction = 1.0f;
2018-04-16 23:00:00 +02:00
if( !R_RecursiveLightPoint( pe->model, pnodes, 0.0f, 1.0f, &cv, start_l, end_l ))
2017-03-06 22:00:00 +01:00
continue; // didn't hit anything
if( g_trace_fraction < last_fraction )
{
if( lspot ) VectorCopy( g_trace_lightspot, lspot );
2018-05-20 23:00:00 +02:00
if( lvec ) VectorNormalize2( g_trace_lightvec, lvec );
2017-03-06 22:00:00 +01:00
light.r = Q_min(( cv.r >> 7 ), 255 );
light.g = Q_min(( cv.g >> 7 ), 255 );
light.b = Q_min(( cv.b >> 7 ), 255 );
last_fraction = g_trace_fraction;
2018-09-08 23:00:00 +02:00
if(( light.r + light.g + light.b ) != 0 )
break; // we get light now
2017-03-06 22:00:00 +01:00
}
2011-03-20 22:00:00 +01:00
}
2010-12-16 22:00:00 +01:00
}
2017-03-06 22:00:00 +01:00
else
{
light.r = light.g = light.b = 255;
light.a = 0;
}
return light;
2017-03-07 22:00:00 +01:00
}
2018-09-20 23:00:00 +02:00
/*
=================
R_LightVec
check bspmodels to get light from
=================
*/
colorVec R_LightVec( const vec3_t start, const vec3_t end, vec3_t lspot, vec3_t lvec )
{
colorVec light = R_LightVecInternal( start, end, lspot, lvec );
if( CVAR_TO_BOOL( r_lighting_extended ) && lspot != NULL && lvec != NULL )
{
// trying to get light from ceiling (but ignore gradient analyze)
if(( light.r + light.g + light.b ) == 0 )
return R_LightVecInternal( end, start, lspot, lvec );
}
return light;
}
2017-03-07 22:00:00 +01:00
/*
=================
R_LightPoint
light from floor
=================
*/
colorVec R_LightPoint( const vec3_t p0 )
{
vec3_t p1;
VectorSet( p1, p0[0], p0[1], p0[2] - 2048.0f );
2018-05-20 23:00:00 +02:00
return R_LightVec( p0, p1, NULL, NULL );
2010-12-02 22:00:00 +01:00
}