📄 gl_light.c
字号:
int i;
int smax, tmax;
mtexinfo_t *tex;
dlight_t *dl;
float *pfBL;
float fsacc, ftacc;
smax = (surf->extents[0]>>4)+1;
tmax = (surf->extents[1]>>4)+1;
tex = surf->texinfo;
for (lnum=0 ; lnum<r_newrefdef.num_dlights ; lnum++)
{
if ( !(surf->dlightbits & (1<<lnum) ) )
continue; // not lit by this light
dl = &r_newrefdef.dlights[lnum];
frad = dl->intensity;
fdist = DotProduct (dl->origin, surf->plane->normal) -
surf->plane->dist;
frad -= fabs(fdist);
// rad is now the highest intensity on the plane
fminlight = DLIGHT_CUTOFF; // FIXME: make configurable?
if (frad < fminlight)
continue;
fminlight = frad - fminlight;
for (i=0 ; i<3 ; i++)
{
impact[i] = dl->origin[i] -
surf->plane->normal[i]*fdist;
}
local[0] = DotProduct (impact, tex->vecs[0]) + tex->vecs[0][3] - surf->texturemins[0];
local[1] = DotProduct (impact, tex->vecs[1]) + tex->vecs[1][3] - surf->texturemins[1];
pfBL = s_blocklights;
for (t = 0, ftacc = 0 ; t<tmax ; t++, ftacc += 16)
{
td = local[1] - ftacc;
if ( td < 0 )
td = -td;
for ( s=0, fsacc = 0 ; s<smax ; s++, fsacc += 16, pfBL += 3)
{
sd = Q_ftol( local[0] - fsacc );
if ( sd < 0 )
sd = -sd;
if (sd > td)
fdist = sd + (td>>1);
else
fdist = td + (sd>>1);
if ( fdist < fminlight )
{
pfBL[0] += ( frad - fdist ) * dl->color[0];
pfBL[1] += ( frad - fdist ) * dl->color[1];
pfBL[2] += ( frad - fdist ) * dl->color[2];
}
}
}
}
}
/*
** R_SetCacheState
*/
void R_SetCacheState( msurface_t *surf )
{
int maps;
for (maps = 0 ; maps < MAXLIGHTMAPS && surf->styles[maps] != 255 ;
maps++)
{
surf->cached_light[maps] = r_newrefdef.lightstyles[surf->styles[maps]].white;
}
}
/*
===============
R_BuildLightMap
Combine and scale multiple lightmaps into the floating format in blocklights
===============
*/
void R_BuildLightMap (msurface_t *surf, byte *dest, int stride)
{
int smax, tmax;
int r, g, b, a, max;
int i, j, size;
byte *lightmap;
float scale[4];
int nummaps;
float *bl;
lightstyle_t *style;
int monolightmap;
if ( surf->texinfo->flags & (SURF_SKY|SURF_TRANS33|SURF_TRANS66|SURF_WARP) )
ri.Sys_Error (ERR_DROP, "R_BuildLightMap called for non-lit surface");
smax = (surf->extents[0]>>4)+1;
tmax = (surf->extents[1]>>4)+1;
size = smax*tmax;
if (size > (sizeof(s_blocklights)>>4) )
ri.Sys_Error (ERR_DROP, "Bad s_blocklights size");
// set to full bright if no light data
if (!surf->samples)
{
int maps;
for (i=0 ; i<size*3 ; i++)
s_blocklights[i] = 255;
for (maps = 0 ; maps < MAXLIGHTMAPS && surf->styles[maps] != 255 ;
maps++)
{
style = &r_newrefdef.lightstyles[surf->styles[maps]];
}
goto store;
}
// count the # of maps
for ( nummaps = 0 ; nummaps < MAXLIGHTMAPS && surf->styles[nummaps] != 255 ;
nummaps++)
;
lightmap = surf->samples;
// add all the lightmaps
if ( nummaps == 1 )
{
int maps;
for (maps = 0 ; maps < MAXLIGHTMAPS && surf->styles[maps] != 255 ;
maps++)
{
bl = s_blocklights;
for (i=0 ; i<3 ; i++)
scale[i] = gl_modulate->value*r_newrefdef.lightstyles[surf->styles[maps]].rgb[i];
if ( scale[0] == 1.0F &&
scale[1] == 1.0F &&
scale[2] == 1.0F )
{
for (i=0 ; i<size ; i++, bl+=3)
{
bl[0] = lightmap[i*3+0];
bl[1] = lightmap[i*3+1];
bl[2] = lightmap[i*3+2];
}
}
else
{
for (i=0 ; i<size ; i++, bl+=3)
{
bl[0] = lightmap[i*3+0] * scale[0];
bl[1] = lightmap[i*3+1] * scale[1];
bl[2] = lightmap[i*3+2] * scale[2];
}
}
lightmap += size*3; // skip to next lightmap
}
}
else
{
int maps;
memset( s_blocklights, 0, sizeof( s_blocklights[0] ) * size * 3 );
for (maps = 0 ; maps < MAXLIGHTMAPS && surf->styles[maps] != 255 ;
maps++)
{
bl = s_blocklights;
for (i=0 ; i<3 ; i++)
scale[i] = gl_modulate->value*r_newrefdef.lightstyles[surf->styles[maps]].rgb[i];
if ( scale[0] == 1.0F &&
scale[1] == 1.0F &&
scale[2] == 1.0F )
{
for (i=0 ; i<size ; i++, bl+=3 )
{
bl[0] += lightmap[i*3+0];
bl[1] += lightmap[i*3+1];
bl[2] += lightmap[i*3+2];
}
}
else
{
for (i=0 ; i<size ; i++, bl+=3)
{
bl[0] += lightmap[i*3+0] * scale[0];
bl[1] += lightmap[i*3+1] * scale[1];
bl[2] += lightmap[i*3+2] * scale[2];
}
}
lightmap += size*3; // skip to next lightmap
}
}
// add all the dynamic lights
if (surf->dlightframe == r_framecount)
R_AddDynamicLights (surf);
// put into texture format
store:
stride -= (smax<<2);
bl = s_blocklights;
monolightmap = gl_monolightmap->string[0];
if ( monolightmap == '0' )
{
for (i=0 ; i<tmax ; i++, dest += stride)
{
for (j=0 ; j<smax ; j++)
{
r = Q_ftol( bl[0] );
g = Q_ftol( bl[1] );
b = Q_ftol( bl[2] );
// catch negative lights
if (r < 0)
r = 0;
if (g < 0)
g = 0;
if (b < 0)
b = 0;
/*
** determine the brightest of the three color components
*/
if (r > g)
max = r;
else
max = g;
if (b > max)
max = b;
/*
** alpha is ONLY used for the mono lightmap case. For this reason
** we set it to the brightest of the color components so that
** things don't get too dim.
*/
a = max;
/*
** rescale all the color components if the intensity of the greatest
** channel exceeds 1.0
*/
if (max > 255)
{
float t = 255.0F / max;
r = r*t;
g = g*t;
b = b*t;
a = a*t;
}
dest[0] = r;
dest[1] = g;
dest[2] = b;
dest[3] = a;
bl += 3;
dest += 4;
}
}
}
else
{
for (i=0 ; i<tmax ; i++, dest += stride)
{
for (j=0 ; j<smax ; j++)
{
r = Q_ftol( bl[0] );
g = Q_ftol( bl[1] );
b = Q_ftol( bl[2] );
// catch negative lights
if (r < 0)
r = 0;
if (g < 0)
g = 0;
if (b < 0)
b = 0;
/*
** determine the brightest of the three color components
*/
if (r > g)
max = r;
else
max = g;
if (b > max)
max = b;
/*
** alpha is ONLY used for the mono lightmap case. For this reason
** we set it to the brightest of the color components so that
** things don't get too dim.
*/
a = max;
/*
** rescale all the color components if the intensity of the greatest
** channel exceeds 1.0
*/
if (max > 255)
{
float t = 255.0F / max;
r = r*t;
g = g*t;
b = b*t;
a = a*t;
}
/*
** So if we are doing alpha lightmaps we need to set the R, G, and B
** components to 0 and we need to set alpha to 1-alpha.
*/
switch ( monolightmap )
{
case 'L':
case 'I':
r = a;
g = b = 0;
break;
case 'C':
// try faking colored lighting
a = 255 - ((r+g+b)/3);
r *= a/255.0;
g *= a/255.0;
b *= a/255.0;
break;
case 'A':
default:
r = g = b = 0;
a = 255 - a;
break;
}
dest[0] = r;
dest[1] = g;
dest[2] = b;
dest[3] = a;
bl += 3;
dest += 4;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -