From d85e7fc2b1a2000dbd217808c0f949c758cdf27a Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Sun, 12 Apr 2026 23:17:05 +0500 Subject: [PATCH] ref: move remaining light functions to libref_common --- ref/common/ref_common.h | 7 +- ref/common/ref_context.c | 2 + ref/common/ref_light.c | 294 ++++++++++++++++++++++++++++++++++- ref/gl/gl_context.c | 2 +- ref/gl/gl_local.h | 10 -- ref/gl/gl_opengl.c | 2 - ref/gl/gl_rlight.c | 307 ------------------------------------ ref/gl/gl_rsurf.c | 25 +-- ref/soft/r_context.c | 2 +- ref/soft/r_light.c | 326 --------------------------------------- ref/soft/r_local.h | 10 -- ref/soft/r_surf.c | 9 +- 12 files changed, 305 insertions(+), 691 deletions(-) delete mode 100644 ref/gl/gl_rlight.c delete mode 100644 ref/soft/r_light.c diff --git a/ref/common/ref_common.h b/ref/common/ref_common.h index 2f12be6b..04b3c064 100644 --- a/ref/common/ref_common.h +++ b/ref/common/ref_common.h @@ -53,11 +53,13 @@ void *_Mem_Realloc( poolhandle_t poolptr, void *memptr, size_t size, qboolean cl #define Mem_EmptyPool( pool ) gEngfuncs._Mem_EmptyPool( pool, __FILE__, __LINE__ ) extern dlight_t *gp_dlights; +extern int g_lightstylevalue[MAX_LIGHTSTYLES]; // // ref_common cvars // extern convar_t r_dlight_virtual_radius; +extern convar_t r_lighting_extended; // // ref_math.c @@ -73,9 +75,12 @@ void Matrix4x4_ToArrayFloatGL( const matrix4x4 in, float out[16] ); // // ref_light.c // -void CL_RunLightStyles_( lightstyle_t *ls, int *lightstylevalue ); +void CL_RunLightStyles( lightstyle_t *ls ); void R_PushDlightsForBmodel( model_t *model, int framecount, const matrix4x4 object_matrix ); int R_PushDlights( model_t *model, int framecount ); +colorVec R_LightVec( const vec3_t start, const vec3_t end, vec3_t lspot, vec3_t lvec ); +colorVec R_LightPoint( const vec3_t p0 ); +void R_UpdateSurfaceCachedLight( msurface_t *surf ); #endif // REF_COMMON_H diff --git a/ref/common/ref_context.c b/ref/common/ref_context.c index 41640c04..8c4e78f9 100644 --- a/ref/common/ref_context.c +++ b/ref/common/ref_context.c @@ -24,6 +24,7 @@ ref_client_t *gp_cl; ref_host_t *gp_host; uint16_t rtable[MOD_FRAMES][MOD_FRAMES]; dlight_t *gp_dlights; +int g_lightstylevalue[MAX_LIGHTSTYLES]; void _Mem_Free( void *data, const char *filename, int fileline ) { @@ -73,6 +74,7 @@ int EXPORT GetRefAPI( int version, ref_interface_t *funcs, ref_api_t *engfuncs, RETRIEVE_ENGINE_SHARED_CVAR_LIST(); gEngfuncs.Cvar_RegisterVariable( &r_dlight_virtual_radius ); + gEngfuncs.Cvar_RegisterVariable( &r_lighting_extended ); return REF_API_VERSION; } diff --git a/ref/common/ref_light.c b/ref/common/ref_light.c index e2c56305..66209dd2 100644 --- a/ref/common/ref_light.c +++ b/ref/common/ref_light.c @@ -18,14 +18,15 @@ GNU General Public License for more details. #include "pm_local.h" CVAR_DEFINE_AUTO( r_dlight_virtual_radius, "3", FCVAR_GLCONFIG, "increase dlight radius virtually by this amount" ); +CVAR_DEFINE_AUTO( r_lighting_extended, "1", FCVAR_GLCONFIG, "allow to get lighting from world and bmodels" ); /* ================== -CL_RunLightStyles_ +CL_RunLightStyles ================== */ -void CL_RunLightStyles_( lightstyle_t *ls, int *lightstylevalue ) +void CL_RunLightStyles( lightstyle_t *ls ) { const model_t *world = gp_cl->models[1]; int i; @@ -37,7 +38,7 @@ void CL_RunLightStyles_( lightstyle_t *ls, int *lightstylevalue ) if( r_fullbright->value || !world->lightdata ) { for( i = 0; i < MAX_LIGHTSTYLES; i++ ) - lightstylevalue[i] = 256 * 256; + g_lightstylevalue[i] = 256 * 256; return; } @@ -53,13 +54,13 @@ void CL_RunLightStyles_( lightstyle_t *ls, int *lightstylevalue ) if( !ls[i].length ) { - lightstylevalue[i] = 256; + g_lightstylevalue[i] = 256; continue; } else if( ls[i].length == 1 ) { // single length style so don't bother interpolating - lightstylevalue[i] = ls[i].map[0] * 22; + g_lightstylevalue[i] = ls[i].map[0] * 22; continue; } @@ -67,7 +68,7 @@ void CL_RunLightStyles_( lightstyle_t *ls, int *lightstylevalue ) if( !ls[i].interp || !cl_lightstyle_lerping->value ) { - lightstylevalue[i] = ls[i].map[flight % ls[i].length] * 22; + g_lightstylevalue[i] = ls[i].map[flight % ls[i].length] * 22; continue; } @@ -84,7 +85,7 @@ void CL_RunLightStyles_( lightstyle_t *ls, int *lightstylevalue ) k = ls[i].map[clight % ls[i].length]; l += (float)( k * 22.0f ) * lerpfrac; - lightstylevalue[i] = (int)l; + g_lightstylevalue[i] = (int)l; } } @@ -205,3 +206,282 @@ void R_PushDlightsForBmodel( model_t *model, int framecount, const matrix4x4 obj VectorCopy( oldorigin, l->origin ); } } + +/* +======================================================================= + + AMBIENT LIGHTING + +======================================================================= +*/ +static vec3_t g_trace_lightspot; +static vec3_t g_trace_lightvec; +static float g_trace_fraction; + +/* +================= +R_RecursiveLightPoint +================= +*/ +static qboolean R_RecursiveLightPoint( model_t *model, mnode_t *node, float p1f, float p2f, colorVec *cv, const vec3_t start, const vec3_t end ) +{ +start: + // didn't hit anything + if( !node || node->contents < 0 ) + { + cv->r = cv->g = cv->b = cv->a = 0; + return false; + } + + // calculate mid point + float front = PlaneDiff( start, node->plane ); + float back = PlaneDiff( end, node->plane ); + + int side = front < 0; + if(( back < 0 ) == side ) + { + node = node_child( node, side, model ); + goto start; + } + + float frac = front / ( front - back ); + + vec3_t mid; + VectorLerp( start, frac, end, mid ); + + float midf = p1f + ( p2f - p1f ) * frac; + + // co down front side + if( R_RecursiveLightPoint( model, node_child( node, side, model ), p1f, midf, cv, start, mid )) + return true; // hit something + + if(( back < 0 ) == side ) + { + cv->r = cv->g = cv->b = cv->a = 0; + return false; // didn't hit anything + } + + // check for impact on this node + int firstsurface = node_firstsurface( node, model ); + int numsurfaces = node_numsurfaces( node, model ); + + VectorCopy( mid, g_trace_lightspot ); + + for( int i = 0; i < numsurfaces; i++ ) + { + const msurface_t *surf = &model->surfaces[firstsurface + i]; + const mextrasurf_t *info = surf->info; + + if( FBitSet( surf->flags, SURF_DRAWTILED )) + continue; // no lightmaps + + float s = DotProduct( mid, info->lmvecs[0] ) + info->lmvecs[0][3]; + float t = DotProduct( mid, info->lmvecs[1] ) + info->lmvecs[1][3]; + + if( s < info->lightmapmins[0] || t < info->lightmapmins[1] ) + continue; + + float ds = s - info->lightmapmins[0]; + float dt = t - info->lightmapmins[1]; + + if ( ds > info->lightextents[0] || dt > info->lightextents[1] ) + continue; + + cv->r = cv->g = cv->b = cv->a = 0; + + if( !surf->samples ) + return true; + + int sample_size = gEngfuncs.Mod_SampleSizeForFace( surf ); + int smax = (info->lightextents[0] / sample_size) + 1; + int tmax = (info->lightextents[1] / sample_size) + 1; + + ds /= sample_size; + dt /= sample_size; + + g_trace_fraction = midf; + + const color24 *lm = surf->samples + Q_rint( dt ) * smax + Q_rint( ds ); + const color24 *dm = NULL; + matrix3x4 tbn; + + if( surf->info->deluxemap ) + { + vec3_t faceNormal; + + dm = surf->info->deluxemap + Q_rint( dt ) * smax + Q_rint( ds ); + + 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] ); + } + + int size = smax * tmax; + + for( int map = 0; map < MAXLIGHTMAPS && surf->styles[map] != 255; map++ ) + { + uint scale = g_lightstylevalue[surf->styles[map]]; + + cv->r += lm->r * scale; + cv->g += lm->g * scale; + cv->b += lm->b * scale; + + lm += size; // skip to next lightmap + + if( dm != NULL ) + { + vec3_t srcNormal, lightNormal; + float f = (1.0f / 128.0f); + + VectorSet( srcNormal, ((float)dm->r - 128.0f) * f, ((float)dm->g - 128.0f) * f, ((float)dm->b - 128.0f) * f ); + 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 + } + } + + return true; + } + + // go down back side + return R_RecursiveLightPoint( model, node_child( node, !side, model ), midf, p2f, cv, mid, end ); +} + +/* +================= +R_LightVecInternal + +check bspmodels to get light from +================= +*/ +static colorVec R_LightVecInternal( const vec3_t start, const vec3_t end, vec3_t lspot, vec3_t lvec ) +{ + if( lspot ) + VectorClear( lspot ); + + if( lvec ) + VectorClear( lvec ); + + if( !gp_cl->models[1] || !gp_cl->models[1]->lightdata ) + return (colorVec){ 255, 255, 255, 0 }; + + float last_fraction = 1.0f; + int max_ents = r_lighting_extended.value ? MAX_PHYSENTS : 1; // get light from bmodels too + colorVec light = { 0 }; + + // check all the bsp-models + for( int i = 0; i < max_ents; i++ ) + { + const physent_t *pe = gEngfuncs.EV_GetPhysent( i ); + + if( !pe ) + break; + + if( !pe->model || pe->model->type != mod_brush ) + continue; // skip non-bsp models + + mnode_t *pnodes = &pe->model->nodes[pe->model->hulls[0].firstclipnode]; + vec3_t offset, start_l, end_l; + + 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 matrix; + Matrix4x4_CreateFromEntity( matrix, pe->angles, offset, 1.0f ); + Matrix4x4_VectorITransform( matrix, start, start_l ); + Matrix4x4_VectorITransform( matrix, end, end_l ); + } + + VectorClear( g_trace_lightspot ); + VectorClear( g_trace_lightvec ); + g_trace_fraction = 1.0f; + + colorVec cv; + if( !R_RecursiveLightPoint( pe->model, pnodes, 0.0f, 1.0f, &cv, start_l, end_l )) + continue; // didn't hit anything + + if( g_trace_fraction < last_fraction ) + { + if( lspot ) VectorCopy( g_trace_lightspot, lspot ); + if( lvec ) VectorNormalize2( g_trace_lightvec, lvec ); + + light.r = Q_min(( cv.r >> 8 ), 255 ); + light.g = Q_min(( cv.g >> 8 ), 255 ); + light.b = Q_min(( cv.b >> 8 ), 255 ); + last_fraction = g_trace_fraction; + + if(( light.r + light.g + light.b ) != 0 ) + break; // we get light now + } + } + + return light; +} + +/* +================= +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( r_lighting_extended.value && 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; +} + +/* +================= +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 ); + + return R_LightVec( p0, p1, NULL, NULL ); +} + +/* +================ +R_SetCacheState +================ +*/ +void R_UpdateSurfaceCachedLight( msurface_t *surf ) +{ + for( int i = 0; i < MAXLIGHTMAPS && surf->styles[i] != 255; i++ ) + surf->cached_light[i] = g_lightstylevalue[surf->styles[i]]; +} diff --git a/ref/gl/gl_context.c b/ref/gl/gl_context.c index f796b29b..d14a1cf8 100644 --- a/ref/gl/gl_context.c +++ b/ref/gl/gl_context.c @@ -214,7 +214,7 @@ static int GL_RefGetParm( int parm, int arg ) return glState.activeTMU; case PARM_LIGHTSTYLEVALUE: arg = bound( 0, arg, MAX_LIGHTSTYLES - 1 ); - return tr.lightstylevalue[arg]; + return g_lightstylevalue[arg]; case PARM_MAX_IMAGE_UNITS: return GL_MaxTextureUnits(); case PARM_REBUILD_GAMMA: diff --git a/ref/gl/gl_local.h b/ref/gl/gl_local.h index ea24d015..a04c8833 100644 --- a/ref/gl/gl_local.h +++ b/ref/gl/gl_local.h @@ -233,7 +233,6 @@ typedef struct qboolean fFlipViewModel; byte visbytes[(MAX_MAP_LEAFS+7)/8]; // member custom PVS - int lightstylevalue[MAX_LIGHTSTYLES]; // value 0 - 65536 int block_size; // lightmap blocksize double frametime; // special frametime for multipass rendering (will set to 0 on a nextview) @@ -370,14 +369,6 @@ qboolean R_SearchForTextureReplacement( char *out, size_t size, const char *mode void R_TextureReplacementReport( const char *modelname, int gl_texturenum, const char *foundpath ); void R_ShowTextures( void ); -// -// gl_rlight.c -// -void CL_RunLightStyles( lightstyle_t *ls ); -void R_GetLightSpot( vec3_t lightspot ); -colorVec R_LightVec( const vec3_t start, const vec3_t end, vec3_t lightspot, vec3_t lightvec ); -colorVec R_LightPoint( const vec3_t p0 ); - // // gl_rmain.c // @@ -759,7 +750,6 @@ extern convar_t gl_stencilbits; extern convar_t gl_overbright; extern convar_t gl_fog; -extern convar_t r_lighting_extended; extern convar_t r_lighting_ambient; extern convar_t r_studio_lambert; extern convar_t r_detailtextures; diff --git a/ref/gl/gl_opengl.c b/ref/gl/gl_opengl.c index a94ade3f..8254daf7 100644 --- a/ref/gl/gl_opengl.c +++ b/ref/gl/gl_opengl.c @@ -22,7 +22,6 @@ CVAR_DEFINE_AUTO( gl_msaa, "1", FCVAR_GLCONFIG, "enable or disable multisample a CVAR_DEFINE_AUTO( gl_stencilbits, "8", FCVAR_GLCONFIG|FCVAR_READ_ONLY, "pixelformat stencil bits (0 - auto)" ); CVAR_DEFINE_AUTO( gl_overbright, "1", FCVAR_GLCONFIG, "overbrights" ); CVAR_DEFINE_AUTO( gl_fog, "1", FCVAR_GLCONFIG, "allow for rendering fog using built-in OpenGL fog implementation" ); -CVAR_DEFINE_AUTO( r_lighting_extended, "1", FCVAR_GLCONFIG, "allow to get lighting from world and bmodels" ); CVAR_DEFINE_AUTO( r_lighting_ambient, "0.3", FCVAR_GLCONFIG, "map ambient lighting scale" ); CVAR_DEFINE_AUTO( r_detailtextures, "1", FCVAR_GLCONFIG, "enable detail textures support" ); CVAR_DEFINE_AUTO( r_novis, "0", 0, "ignore vis information (perfomance test)" ); @@ -1139,7 +1138,6 @@ GL_InitCommands */ static void GL_InitCommands( void ) { - gEngfuncs.Cvar_RegisterVariable( &r_lighting_extended ); gEngfuncs.Cvar_RegisterVariable( &r_lighting_ambient ); gEngfuncs.Cvar_RegisterVariable( &r_novis ); gEngfuncs.Cvar_RegisterVariable( &r_nocull ); diff --git a/ref/gl/gl_rlight.c b/ref/gl/gl_rlight.c deleted file mode 100644 index 5f75f4d6..00000000 --- a/ref/gl/gl_rlight.c +++ /dev/null @@ -1,307 +0,0 @@ -/* -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. -*/ - -#include "gl_local.h" -#include "pm_local.h" -#include "studio.h" -#include "xash3d_mathlib.h" -#include "ref_params.h" - -/* -============================================================================= - -DYNAMIC LIGHTS - -============================================================================= -*/ -/* -================== -CL_RunLightStyles - -================== -*/ -void CL_RunLightStyles( lightstyle_t *ls ) -{ - CL_RunLightStyles_( ls, tr.lightstylevalue ); -} - - -/* -======================================================================= - - AMBIENT LIGHTING - -======================================================================= -*/ -static vec3_t g_trace_lightspot; -static vec3_t g_trace_lightvec; -static float g_trace_fraction; - -/* -================= -R_RecursiveLightPoint -================= -*/ -static qboolean R_RecursiveLightPoint( model_t *model, mnode_t *node, float p1f, float p2f, colorVec *cv, const vec3_t start, const vec3_t end ) -{ -start: - // didn't hit anything - if( !node || node->contents < 0 ) - { - cv->r = cv->g = cv->b = cv->a = 0; - return false; - } - - // calculate mid point - float front = PlaneDiff( start, node->plane ); - float back = PlaneDiff( end, node->plane ); - - int side = front < 0; - if(( back < 0 ) == side ) - { - node = node_child( node, side, model ); - goto start; - } - - float frac = front / ( front - back ); - - vec3_t mid; - VectorLerp( start, frac, end, mid ); - - float midf = p1f + ( p2f - p1f ) * frac; - - // co down front side - if( R_RecursiveLightPoint( model, node_child( node, side, model ), p1f, midf, cv, start, mid )) - return true; // hit something - - if(( back < 0 ) == side ) - { - cv->r = cv->g = cv->b = cv->a = 0; - return false; // didn't hit anything - } - - // check for impact on this node - int firstsurface = node_firstsurface( node, model ); - int numsurfaces = node_numsurfaces( node, model ); - - VectorCopy( mid, g_trace_lightspot ); - - for( int i = 0; i < numsurfaces; i++ ) - { - const msurface_t *surf = &model->surfaces[firstsurface + i]; - const mextrasurf_t *info = surf->info; - - if( FBitSet( surf->flags, SURF_DRAWTILED )) - continue; // no lightmaps - - float s = DotProduct( mid, info->lmvecs[0] ) + info->lmvecs[0][3]; - float t = DotProduct( mid, info->lmvecs[1] ) + info->lmvecs[1][3]; - - if( s < info->lightmapmins[0] || t < info->lightmapmins[1] ) - continue; - - float ds = s - info->lightmapmins[0]; - float dt = t - info->lightmapmins[1]; - - if ( ds > info->lightextents[0] || dt > info->lightextents[1] ) - continue; - - cv->r = cv->g = cv->b = cv->a = 0; - - if( !surf->samples ) - return true; - - int sample_size = gEngfuncs.Mod_SampleSizeForFace( surf ); - int smax = (info->lightextents[0] / sample_size) + 1; - int tmax = (info->lightextents[1] / sample_size) + 1; - - ds /= sample_size; - dt /= sample_size; - - g_trace_fraction = midf; - - const color24 *lm = surf->samples + Q_rint( dt ) * smax + Q_rint( ds ); - const color24 *dm = NULL; - matrix3x4 tbn; - - if( surf->info->deluxemap ) - { - vec3_t faceNormal; - - dm = surf->info->deluxemap + Q_rint( dt ) * smax + Q_rint( ds ); - - 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] ); - } - - int size = smax * tmax; - - for( int map = 0; map < MAXLIGHTMAPS && surf->styles[map] != 255; map++ ) - { - uint scale = tr.lightstylevalue[surf->styles[map]]; - - cv->r += lm->r * scale; - cv->g += lm->g * scale; - cv->b += lm->b * scale; - - lm += size; // skip to next lightmap - - if( dm != NULL ) - { - vec3_t srcNormal, lightNormal; - float f = (1.0f / 128.0f); - - VectorSet( srcNormal, ((float)dm->r - 128.0f) * f, ((float)dm->g - 128.0f) * f, ((float)dm->b - 128.0f) * f ); - 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 - } - } - - return true; - } - - // go down back side - return R_RecursiveLightPoint( model, node_child( node, !side, model ), midf, p2f, cv, mid, end ); -} - -/* -================= -R_LightVec - -check bspmodels to get light from -================= -*/ -static colorVec R_LightVecInternal( const vec3_t start, const vec3_t end, vec3_t lspot, vec3_t lvec ) -{ - if( lspot ) - VectorClear( lspot ); - - if( lvec ) - VectorClear( lvec ); - - if( !tr.worldmodel || !tr.worldmodel->lightdata ) - return (colorVec){ 255, 255, 255, 0 }; - - float last_fraction = 1.0f; - int max_ents = r_lighting_extended.value ? MAX_PHYSENTS : 1; // get light from bmodels too - colorVec light = { 0 }; - - // check all the bsp-models - for( int i = 0; i < max_ents; i++ ) - { - const physent_t *pe = gEngfuncs.EV_GetPhysent( i ); - - if( !pe ) - break; - - if( !pe->model || pe->model->type != mod_brush ) - continue; // skip non-bsp models - - mnode_t *pnodes = &pe->model->nodes[pe->model->hulls[0].firstclipnode]; - vec3_t offset, start_l, end_l; - - 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 matrix; - Matrix4x4_CreateFromEntity( matrix, pe->angles, offset, 1.0f ); - Matrix4x4_VectorITransform( matrix, start, start_l ); - Matrix4x4_VectorITransform( matrix, end, end_l ); - } - - VectorClear( g_trace_lightspot ); - VectorClear( g_trace_lightvec ); - g_trace_fraction = 1.0f; - - colorVec cv; - if( !R_RecursiveLightPoint( pe->model, pnodes, 0.0f, 1.0f, &cv, start_l, end_l )) - continue; // didn't hit anything - - if( g_trace_fraction < last_fraction ) - { - if( lspot ) VectorCopy( g_trace_lightspot, lspot ); - if( lvec ) VectorNormalize2( g_trace_lightvec, lvec ); - - light.r = Q_min(( cv.r >> 8 ), 255 ); - light.g = Q_min(( cv.g >> 8 ), 255 ); - light.b = Q_min(( cv.b >> 8 ), 255 ); - last_fraction = g_trace_fraction; - - if(( light.r + light.g + light.b ) != 0 ) - break; // we get light now - } - } - - return light; -} - -/* -================= -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( r_lighting_extended.value && 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; -} - -/* -================= -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 ); - - return R_LightVec( p0, p1, NULL, NULL ); -} diff --git a/ref/gl/gl_rsurf.c b/ref/gl/gl_rsurf.c index 9c68b4e2..67d6d6ad 100644 --- a/ref/gl/gl_rsurf.c +++ b/ref/gl/gl_rsurf.c @@ -637,21 +637,6 @@ static void R_AddDynamicLights( const msurface_t *surf ) } } -/* -================ -R_SetCacheState -================ -*/ -static void R_SetCacheState( msurface_t *surf ) -{ - int maps; - - for( maps = 0; maps < MAXLIGHTMAPS && surf->styles[maps] != 255; maps++ ) - { - surf->cached_light[maps] = tr.lightstylevalue[surf->styles[maps]]; - } -} - /* ============================================================================= @@ -782,7 +767,7 @@ static void R_BuildLightMap( const msurface_t *surf, byte *dest, int stride, qbo if( surf->styles[map] >= 255 ) break; - scale = tr.lightstylevalue[surf->styles[map]]; + scale = g_lightstylevalue[surf->styles[map]]; for( i = 0; i < size; i++ ) { @@ -959,7 +944,7 @@ EmitWaterPolys Does a water warp on the pre-fragmented glpoly_t chain ============= */ -void EmitWaterPolys( msurface_t *warp, qboolean reverse, qboolean ripples ) +static void EmitWaterPolys( msurface_t *warp, qboolean reverse, qboolean ripples ) { float *v, nv, waveHeight; float s, t, os, ot; @@ -1397,7 +1382,7 @@ static qboolean R_CheckLightMap( msurface_t *fa ) // check for light styles for( maps = 0; maps < MAXLIGHTMAPS && fa->styles[maps] != 255; maps++ ) { - if( tr.lightstylevalue[fa->styles[maps]] == fa->cached_light[maps] ) + if( g_lightstylevalue[fa->styles[maps]] == fa->cached_light[maps] ) continue; const int style = fa->styles[maps]; @@ -1422,7 +1407,7 @@ static qboolean R_CheckLightMap( msurface_t *fa ) //Host_MapDesignError( "%s: bad surface extents: %d %d", __func__, fa->extents[0], fa->extents[1] ); } - R_SetCacheState( fa ); + R_UpdateSurfaceCachedLight( fa ); #if XASH_WES GL_Bind( XASH_TEXTURE1, tr.lightmapTextures[fa->lightmaptexturenum] ); @@ -3858,7 +3843,7 @@ static void GL_CreateSurfaceLightmap( msurface_t *surf, model_t *loadmodel ) base = gl_lms.lightmap_buffer; base += ( surf->light_t * BLOCK_SIZE + surf->light_s ) * 4; - R_SetCacheState( surf ); + R_UpdateSurfaceCachedLight( surf ); R_BuildLightMap( surf, base, BLOCK_SIZE * 4, false ); } diff --git a/ref/soft/r_context.c b/ref/soft/r_context.c index 0548fadf..b500c9c8 100644 --- a/ref/soft/r_context.c +++ b/ref/soft/r_context.c @@ -174,7 +174,7 @@ static int GL_RefGetParm( int parm, int arg ) return 0; // glState.activeTMU; case PARM_LIGHTSTYLEVALUE: arg = bound( 0, arg, MAX_LIGHTSTYLES - 1 ); - return tr.lightstylevalue[arg]; + return g_lightstylevalue[arg]; case PARM_MAX_IMAGE_UNITS: return 0; // GL_MaxTextureUnits(); case PARM_REBUILD_GAMMA: diff --git a/ref/soft/r_light.c b/ref/soft/r_light.c deleted file mode 100644 index abb66656..00000000 --- a/ref/soft/r_light.c +++ /dev/null @@ -1,326 +0,0 @@ -/* -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. -*/ - -#include "r_local.h" -#include "pm_local.h" -#include "studio.h" -#include "xash3d_mathlib.h" -#include "ref_params.h" - -// unused, need refactor -unsigned blocklights[10240]; - -/* -============================================================================= - -DYNAMIC LIGHTS - -============================================================================= -*/ -/* -================== -CL_RunLightStyles - -================== -*/ -void CL_RunLightStyles( lightstyle_t *ls ) -{ - CL_RunLightStyles_( ls, tr.lightstylevalue ); -} - - -/* -======================================================================= - - AMBIENT LIGHTING - -======================================================================= -*/ -static vec3_t g_trace_lightspot; -static vec3_t g_trace_lightvec; -static float g_trace_fraction; - -/* -================= -R_RecursiveLightPoint -================= -*/ -static qboolean R_RecursiveLightPoint( model_t *model, mnode_t *node, float p1f, float p2f, colorVec *cv, const vec3_t start, const vec3_t end ) -{ - float front, back, frac, midf; - int i, map, side, size; - float ds, dt, s, t; - int sample_size; - color24 *lm, *dm; - mextrasurf_t *info; - msurface_t *surf; - matrix3x4 tbn; - vec3_t mid; - int firstsurface, numsurfaces; - - // didn't hit anything - if( !node || node->contents < 0 ) - { - cv->r = cv->g = cv->b = cv->a = 0; - return false; - } - - firstsurface = node_firstsurface( node, model ); - numsurfaces = node_numsurfaces( node, model ); - - // calculate mid point - front = PlaneDiff( start, node->plane ); - back = PlaneDiff( end, node->plane ); - - side = front < 0; - if(( back < 0 ) == side ) - return R_RecursiveLightPoint( model, node_child( node, side, model ), p1f, p2f, cv, start, end ); - - frac = front / ( front - back ); - - VectorLerp( start, frac, end, mid ); - midf = p1f + ( p2f - p1f ) * frac; - - // co down front side - if( R_RecursiveLightPoint( model, node_child( node, side, model ), p1f, midf, cv, start, mid )) - return true; // hit something - - if(( back < 0 ) == side ) - { - cv->r = cv->g = cv->b = cv->a = 0; - return false; // didn't hit anything - } - - // check for impact on this node - surf = model->surfaces + firstsurface; - VectorCopy( mid, g_trace_lightspot ); - - for( i = 0; i < numsurfaces; i++, surf++ ) - { - int smax, tmax; - - info = surf->info; - - if( FBitSet( surf->flags, SURF_DRAWTILED )) - continue; // no lightmaps - - 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; - - ds = s - info->lightmapmins[0]; - dt = t - info->lightmapmins[1]; - - if( ds > info->lightextents[0] || dt > info->lightextents[1] ) - continue; - - cv->r = cv->g = cv->b = cv->a = 0; - - if( !surf->samples ) - return true; - - sample_size = gEngfuncs.Mod_SampleSizeForFace( surf ); - smax = ( info->lightextents[0] / sample_size ) + 1; - tmax = ( info->lightextents[1] / sample_size ) + 1; - ds /= sample_size; - dt /= sample_size; - - lm = surf->samples + Q_rint( dt ) * smax + Q_rint( ds ); - g_trace_fraction = midf; - size = smax * tmax; - 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 ); - } - - for( map = 0; map < MAXLIGHTMAPS && surf->styles[map] != 255; map++ ) - { - uint scale = tr.lightstylevalue[surf->styles[map]]; - - cv->r += lm->r * scale; - cv->g += lm->g * scale; - cv->b += lm->b * scale; - - lm += size; // skip to next lightmap - - if( dm != NULL ) - { - vec3_t srcNormal, lightNormal; - float f = ( 1.0f / 128.0f ); - - VectorSet( srcNormal, ((float)dm->r - 128.0f ) * f, ((float)dm->g - 128.0f ) * f, ((float)dm->b - 128.0f ) * f ); - 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 - } - } - - return true; - } - - // go down back side - return R_RecursiveLightPoint( model, node_child( node, !side, model ), midf, p2f, cv, mid, end ); -} - -/* -================= -R_LightVec - -check bspmodels to get light from -================= -*/ -static colorVec R_LightVecInternal( const vec3_t start, const vec3_t end, vec3_t lspot, vec3_t lvec ) -{ - float last_fraction; - int i, maxEnts = 1; - colorVec light, cv; - - if( lspot ) - VectorClear( lspot ); - if( lvec ) - VectorClear( lvec ); - - if( WORLDMODEL && WORLDMODEL->lightdata ) - { - light.r = light.g = light.b = light.a = 0; - last_fraction = 1.0f; - - // get light from bmodels too - // if( CVAR_TO_BOOL( r_lighting_extended )) - maxEnts = MAX_PHYSENTS; - - // check all the bsp-models - for( i = 0; i < maxEnts; i++ ) - { - physent_t *pe = gEngfuncs.EV_GetPhysent( i ); - vec3_t offset, start_l, end_l; - mnode_t *pnodes; - matrix4x4 matrix; - - if( !pe ) - break; - - 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 ); - VectorClear( g_trace_lightvec ); - g_trace_fraction = 1.0f; - - if( !R_RecursiveLightPoint( pe->model, pnodes, 0.0f, 1.0f, &cv, start_l, end_l )) - continue; // didn't hit anything - - if( g_trace_fraction < last_fraction ) - { - if( lspot ) - VectorCopy( g_trace_lightspot, lspot ); - if( lvec ) - VectorNormalize2( g_trace_lightvec, lvec ); - light.r = Q_min(( cv.r >> 8 ), 255 ); - light.g = Q_min(( cv.g >> 8 ), 255 ); - light.b = Q_min(( cv.b >> 8 ), 255 ); - last_fraction = g_trace_fraction; - - if(( light.r + light.g + light.b ) != 0 ) - break; // we get light now - } - } - } - else - { - light.r = light.g = light.b = 255; - light.a = 0; - } - - return light; -} - -/* -================= -R_LightVec - -check bspmodels to get light from -================= -*/ -colorVec GAME_EXPORT R_LightVec( const vec3_t start, const vec3_t end, vec3_t lspot, vec3_t lvec ) -{ - colorVec light = R_LightVecInternal( start, end, lspot, lvec ); - - // light.r = light.g = light.b = 255; - - if( lspot != NULL && lvec != NULL ) // CVAR_TO_BOOL( r_lighting_extended ) && - { - // 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; -} - -/* -================= -R_LightPoint - -light from floor -================= -*/ -colorVec GAME_EXPORT R_LightPoint( const vec3_t p0 ) -{ - vec3_t p1; - - VectorSet( p1, p0[0], p0[1], p0[2] - 2048.0f ); - - return R_LightVec( p0, p1, NULL, NULL ); -} diff --git a/ref/soft/r_local.h b/ref/soft/r_local.h index 3231d79e..ca60f6bf 100644 --- a/ref/soft/r_local.h +++ b/ref/soft/r_local.h @@ -253,7 +253,6 @@ typedef struct int max_recursion; byte visbytes[( MAX_MAP_LEAFS + 7 ) / 8]; // member custom PVS - int lightstylevalue[MAX_LIGHTSTYLES]; // value 0 - 65536 int block_size; // lightmap blocksize double frametime; // special frametime for multipass rendering (will set to 0 on a nextview) @@ -382,15 +381,6 @@ void R_InitImages( void ); void R_ShutdownImages( void ); int R_TexMemory( void ); -#if 1 -// -// gl_rlight.c -// -void CL_RunLightStyles( lightstyle_t *ls ); -void R_GetLightSpot( vec3_t lightspot ); -colorVec R_LightVec( const vec3_t start, const vec3_t end, vec3_t lightspot, vec3_t lightvec ); -colorVec R_LightPoint( const vec3_t p0 ); -#endif // // gl_rmain.c // diff --git a/ref/soft/r_surf.c b/ref/soft/r_surf.c index db0d7e92..ff4925e4 100644 --- a/ref/soft/r_surf.c +++ b/ref/soft/r_surf.c @@ -208,7 +208,7 @@ static void R_BuildLightMap( void ) if( surf->styles[map] >= 255 ) break; - scale = tr.lightstylevalue[surf->styles[map]]; + scale = g_lightstylevalue[surf->styles[map]]; for( i = 0; i < size; i++ ) blocklights[i] += ( lm[i].r + lm[i].g + lm[i].b ) * scale; @@ -1103,7 +1103,7 @@ surfcache_t *D_CacheSurface( msurface_t *surface, int miplevel ) // check for lightmap modification for( maps = 0; maps < MAXLIGHTMAPS && surface->styles[maps] != 255; maps++ ) { - if( tr.lightstylevalue[surface->styles[maps]] != surface->cached_light[maps] ) + if( g_lightstylevalue[surface->styles[maps]] != surface->cached_light[maps] ) { surface->dlightframe = tr.framecount; } @@ -1173,10 +1173,7 @@ surfcache_t *D_CacheSurface( msurface_t *surface, int miplevel ) cache->lightadj[1] = r_drawsurf.lightadj[1]; cache->lightadj[2] = r_drawsurf.lightadj[2]; cache->lightadj[3] = r_drawsurf.lightadj[3]; - for( maps = 0; maps < MAXLIGHTMAPS && surface->styles[maps] != 255; maps++ ) - { - surface->cached_light[maps] = tr.lightstylevalue[surface->styles[maps]]; - } + R_UpdateSurfaceCachedLight( surface ); // // draw and light the surface texture //