engine: slight refactoring of lightstyle handling functions

This commit is contained in:
Alibek Omarov 2024-12-17 17:04:26 +03:00
parent 48cc526c7e
commit ca0c5f929a
4 changed files with 64 additions and 74 deletions

View file

@ -2427,18 +2427,18 @@ static void CL_ClearLightStyles( void )
void CL_SetLightstyle( int style, const char *s, float f ) void CL_SetLightstyle( int style, const char *s, float f )
{ {
int i, k; int i;
lightstyle_t *ls; lightstyle_t *ls;
float val1, val2;
Assert( s != NULL ); if( unlikely( style < 0 || style >= MAX_LIGHTSTYLES ))
Assert( style >= 0 && style < MAX_LIGHTSTYLES ); {
Con_Printf( S_WARN "%s: ignored invalid lightstyle id %d\n", style );
return;
}
ls = &cl.lightstyles[style]; ls = &cl.lightstyles[style];
Q_strncpy( ls->pattern, s, sizeof( ls->pattern )); ls->length = Q_strncpy( ls->pattern, s, sizeof( ls->pattern ));
ls->length = Q_strlen( s );
ls->time = f; // set local time ls->time = f; // set local time
for( i = 0; i < ls->length; i++ ) for( i = 0; i < ls->length; i++ )
@ -2448,10 +2448,10 @@ void CL_SetLightstyle( int style, const char *s, float f )
// check for allow interpolate // check for allow interpolate
// NOTE: fast flickering styles looks ugly when interpolation is running // NOTE: fast flickering styles looks ugly when interpolation is running
for( k = 0; k < (ls->length - 1); k++ ) for( i = 0; i < ( ls->length - 1 ); i++ )
{ {
val1 = ls->map[(k+0) % ls->length]; float val1 = ls->map[( i + 0 ) % ls->length];
val2 = ls->map[(k+1) % ls->length]; float val2 = ls->map[( i + 1 ) % ls->length];
if( fabs( val1 - val2 ) > STYLE_LERPING_THRESHOLD ) if( fabs( val1 - val2 ) > STYLE_LERPING_THRESHOLD )
{ {
@ -2471,8 +2471,8 @@ DLIGHT MANAGEMENT
============================================================== ==============================================================
*/ */
dlight_t cl_dlights[MAX_DLIGHTS]; static dlight_t cl_dlights[MAX_DLIGHTS];
dlight_t cl_elights[MAX_ELIGHTS]; static dlight_t cl_elights[MAX_ELIGHTS];
/* /*
================ ================

View file

@ -1565,21 +1565,6 @@ edict_t *SV_FindGlobalEntity( string_t classname, string_t globalname )
return pent; return pent;
} }
/*
==============
pfnGetEntityIllum
returns averaged lightvalue for entity
==============
*/
static int GAME_EXPORT pfnGetEntityIllum( edict_t* pEnt )
{
if( !SV_IsValidEdict( pEnt ))
return -1;
return SV_LightForEntity( pEnt );
}
/* /*
================= =================
pfnFindEntityInSphere pfnFindEntityInSphere
@ -2450,10 +2435,17 @@ pfnLightStyle
*/ */
static void GAME_EXPORT pfnLightStyle( int style, const char* val ) static void GAME_EXPORT pfnLightStyle( int style, const char* val )
{ {
if( style < 0 ) style = 0; if( style < 0 )
style = 0;
if( style >= MAX_LIGHTSTYLES ) if( style >= MAX_LIGHTSTYLES )
{
Host_Error( "%s: style: %i >= %d", __func__, style, MAX_LIGHTSTYLES ); Host_Error( "%s: style: %i >= %d", __func__, style, MAX_LIGHTSTYLES );
if( sv.loadgame ) return; // don't let the world overwrite our restored styles return;
}
if( sv.loadgame )
return; // don't let the world overwrite our restored styles
SV_SetLightStyle( style, val, 0.0f ); // set correct style SV_SetLightStyle( style, val, 0.0f ); // set correct style
} }
@ -4677,7 +4669,7 @@ static enginefuncs_t gEngfuncs =
pfnChangeYaw, pfnChangeYaw,
pfnChangePitch, pfnChangePitch,
SV_FindEntityByString, SV_FindEntityByString,
pfnGetEntityIllum, SV_LightForEntity,
pfnFindEntityInSphere, pfnFindEntityInSphere,
pfnFindClientInPVS, pfnFindClientInPVS,
pfnEntitiesInPVS, pfnEntitiesInPVS,

View file

@ -1784,21 +1784,23 @@ static void SV_Physics_Entity( edict_t *ent )
static void SV_RunLightStyles( void ) static void SV_RunLightStyles( void )
{ {
int i, ofs; int i;
lightstyle_t *ls;
float scale;
scale = 1.0f;
// run lightstyles animation // run lightstyles animation
for( i = 0, ls = sv.lightstyles; i < MAX_LIGHTSTYLES; i++, ls++ ) for( i = 0; i < MAX_LIGHTSTYLES; i++ )
{ {
lightstyle_t *ls = &sv.lightstyles[i];
int ofs;
ls->time += sv.frametime; ls->time += sv.frametime;
ofs = (ls->time * 10); ofs = (ls->time * 10);
if( ls->length == 0 ) ls->value = scale; // disable this light if( ls->length == 0 )
else if( ls->length == 1 ) ls->value = ( ls->map[0] / 12.0f ) * scale; ls->value = 1.0f; // disable this light
else ls->value = ( ls->map[ofs % ls->length] / 12.0f ) * scale; else if( ls->length == 1 )
ls->value = ls->map[0] / 12.0f;
else
ls->value = ls->map[ofs % ls->length] / 12.0f;
} }
} }

View file

@ -1508,23 +1508,16 @@ trace_t SV_MoveToss( edict_t *tossent, edict_t *ignore )
=============================================================================== ===============================================================================
*/ */
static vec3_t sv_pointColor;
/* /*
================= =================
SV_RecursiveLightPoint SV_RecursiveLightPoint
================= =================
*/ */
static qboolean SV_RecursiveLightPoint( model_t *model, mnode_t *node, const vec3_t start, const vec3_t end ) static qboolean SV_RecursiveLightPoint( model_t *model, mnode_t *node, const vec3_t start, const vec3_t end, vec3_t point_color )
{ {
float front, back, scale, frac; float front, back, frac;
int i, map, side, size; int i, side;
float ds, dt, s, t; vec3_t mid;
int sample_size;
msurface_t *surf;
mextrasurf_t *info;
color24 *lm;
vec3_t mid;
// didn't hit anything // didn't hit anything
if( !node || node->contents < 0 ) if( !node || node->contents < 0 )
@ -1536,27 +1529,28 @@ static qboolean SV_RecursiveLightPoint( model_t *model, mnode_t *node, const vec
side = front < 0.0f; side = front < 0.0f;
if(( back < 0.0f ) == side ) if(( back < 0.0f ) == side )
return SV_RecursiveLightPoint( model, node->children[side], start, end ); return SV_RecursiveLightPoint( model, node->children[side], start, end, point_color );
frac = front / ( front - back ); frac = front / ( front - back );
VectorLerp( start, frac, end, mid ); VectorLerp( start, frac, end, mid );
// co down front side // co down front side
if( SV_RecursiveLightPoint( model, node->children[side], start, mid )) if( SV_RecursiveLightPoint( model, node->children[side], start, mid, point_color ))
return true; // hit something return true; // hit something
if(( back < 0.0f ) == side ) if(( back < 0.0f ) == side )
return false;// didn't hit anything return false; // didn't hit anything
// check for impact on this node // check for impact on this node
surf = model->surfaces + node->firstsurface; for( i = 0; i < node->numsurfaces; i++ )
for( i = 0; i < node->numsurfaces; i++, surf++ )
{ {
int smax, tmax; const msurface_t *surf = &model->surfaces[node->firstsurface + i];
const mextrasurf_t *info = surf->info;
info = surf->info; int smax, tmax, map, size;
int sample_size;
float ds, dt, s, t;
const color24 *lm;
if( FBitSet( surf->flags, SURF_DRAWTILED )) if( FBitSet( surf->flags, SURF_DRAWTILED ))
continue; // no lightmaps continue; // no lightmaps
@ -1582,26 +1576,27 @@ static qboolean SV_RecursiveLightPoint( model_t *model, mnode_t *node, const vec
ds /= sample_size; ds /= sample_size;
dt /= sample_size; dt /= sample_size;
VectorClear( sv_pointColor ); VectorClear( point_color );
lm = surf->samples + Q_rint( dt ) * smax + Q_rint( ds ); lm = surf->samples + Q_rint( dt ) * smax + Q_rint( ds );
size = smax * tmax; size = smax * tmax;
for( map = 0; map < MAXLIGHTMAPS && surf->styles[map] != 255; map++ ) for( map = 0; map < MAXLIGHTMAPS && surf->styles[map] != 255; map++ )
{ {
scale = sv.lightstyles[surf->styles[map]].value; float scale = sv.lightstyles[surf->styles[map]].value;
sv_pointColor[0] += lm->r * scale; point_color[0] += lm->r * scale;
sv_pointColor[1] += lm->g * scale; point_color[1] += lm->g * scale;
sv_pointColor[2] += lm->b * scale; point_color[2] += lm->b * scale;
lm += size; // skip to next lightmap lm += size; // skip to next lightmap
} }
return true; return true;
} }
// go down back side // go down back side
return SV_RecursiveLightPoint( model, node->children[!side], mid, end ); return SV_RecursiveLightPoint( model, node->children[!side], mid, end, point_color );
} }
/* /*
@ -1615,10 +1610,8 @@ void SV_SetLightStyle( int style, const char* s, float f )
{ {
int j, k; int j, k;
Q_strncpy( sv.lightstyles[style].pattern, s, sizeof( sv.lightstyles[0].pattern )); j = Q_strncpy( sv.lightstyles[style].pattern, s, sizeof( sv.lightstyles[0].pattern ));
sv.lightstyles[style].time = f; sv.lightstyles[style].time = f;
j = Q_strlen( s );
sv.lightstyles[style].length = j; sv.lightstyles[style].length = j;
for( k = 0; k < j; k++ ) for( k = 0; k < j; k++ )
@ -1642,12 +1635,16 @@ grab the ambient lighting color for current point
*/ */
int SV_LightForEntity( edict_t *pEdict ) int SV_LightForEntity( edict_t *pEdict )
{ {
vec3_t start, end; vec3_t point_color = { 1.0f, 1.0f, 1.0f };
vec3_t start, end;
if( !SV_IsValidEdict( pEdict ))
return -1;
if( FBitSet( pEdict->v.effects, EF_FULLBRIGHT ) || !sv.worldmodel->lightdata ) if( FBitSet( pEdict->v.effects, EF_FULLBRIGHT ) || !sv.worldmodel->lightdata )
return 255; return 255;
// player has more precision light level that come from client-side // player has more precise light level that come from client-side
if( FBitSet( pEdict->v.flags, FL_CLIENT )) if( FBitSet( pEdict->v.flags, FL_CLIENT ))
return pEdict->v.light_level; return pEdict->v.light_level;
@ -1657,9 +1654,8 @@ int SV_LightForEntity( edict_t *pEdict )
if( FBitSet( pEdict->v.effects, EF_INVLIGHT )) if( FBitSet( pEdict->v.effects, EF_INVLIGHT ))
end[2] = start[2] + world.size[2]; end[2] = start[2] + world.size[2];
else end[2] = start[2] - world.size[2]; else end[2] = start[2] - world.size[2];
VectorSet( sv_pointColor, 1.0f, 1.0f, 1.0f );
SV_RecursiveLightPoint( sv.worldmodel, sv.worldmodel->nodes, start, end ); SV_RecursiveLightPoint( sv.worldmodel, sv.worldmodel->nodes, start, end, point_color );
return VectorAvg( sv_pointColor ); return VectorAvg( point_color );
} }