2009-03-05 09:07:55 +00:00
|
|
|
/*
|
2010-10-20 09:15:50 +00:00
|
|
|
* Copyright (C) 1997-2001 Id Software, Inc.
|
|
|
|
*
|
2010-10-21 07:55:41 +00:00
|
|
|
* 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 2 of the License, or (at
|
|
|
|
* your option) any later version.
|
2010-10-20 09:15:50 +00:00
|
|
|
*
|
2010-10-21 07:55:41 +00:00
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
2010-10-20 09:15:50 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*
|
|
|
|
* See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2010-10-21 07:55:41 +00:00
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
|
|
* 02111-1307, USA.
|
2010-10-20 09:15:50 +00:00
|
|
|
*
|
2010-10-21 07:55:41 +00:00
|
|
|
* =======================================================================
|
|
|
|
*
|
|
|
|
* Lightmaps and dynamic lighting
|
|
|
|
*
|
|
|
|
* =======================================================================
|
2012-04-29 13:57:33 +00:00
|
|
|
*/
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2009-03-05 11:03:08 +00:00
|
|
|
#include "header/local.h"
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
#define DLIGHT_CUTOFF 64
|
2012-04-29 13:57:33 +00:00
|
|
|
|
2010-10-21 07:55:41 +00:00
|
|
|
int r_dlightframecount;
|
|
|
|
vec3_t pointcolor;
|
|
|
|
cplane_t *lightplane; /* used as shadow plane */
|
|
|
|
vec3_t lightspot;
|
2012-07-21 12:09:45 +00:00
|
|
|
static float s_blocklights[34 * 34 * 3];
|
2012-04-29 13:57:33 +00:00
|
|
|
|
2010-10-20 09:15:50 +00:00
|
|
|
void
|
2012-07-21 12:09:45 +00:00
|
|
|
R_RenderDlight(dlight_t *light)
|
2010-10-20 09:15:50 +00:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
float a;
|
|
|
|
vec3_t v;
|
|
|
|
float rad;
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2010-10-20 09:15:50 +00:00
|
|
|
rad = light->intensity * 0.35;
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
VectorSubtract(light->origin, r_origin, v);
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
qglBegin(GL_TRIANGLE_FAN);
|
|
|
|
qglColor3f(light->color[0] * 0.2, light->color[1] * 0.2,
|
|
|
|
light->color[2] * 0.2);
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
for (i = 0; i < 3; i++)
|
2010-10-20 09:15:50 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
v[i] = light->origin[i] - vpn[i] * rad;
|
2010-10-20 09:15:50 +00:00
|
|
|
}
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
qglVertex3fv(v);
|
|
|
|
qglColor3f(0, 0, 0);
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
for (i = 16; i >= 0; i--)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
2010-10-20 09:15:50 +00:00
|
|
|
a = i / 16.0 * M_PI * 2;
|
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
for (j = 0; j < 3; j++)
|
2010-10-20 09:15:50 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
v[j] = light->origin[j] + vright[j] * cos(a) * rad
|
|
|
|
+ vup[j] * sin(a) * rad;
|
2010-10-20 09:15:50 +00:00
|
|
|
}
|
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
qglVertex3fv(v);
|
2009-03-05 09:07:55 +00:00
|
|
|
}
|
2010-10-20 09:15:50 +00:00
|
|
|
|
|
|
|
qglEnd();
|
2009-03-05 09:07:55 +00:00
|
|
|
}
|
|
|
|
|
2010-10-20 11:52:50 +00:00
|
|
|
void
|
2012-07-21 12:09:45 +00:00
|
|
|
R_RenderDlights(void)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
2010-10-20 09:15:50 +00:00
|
|
|
int i;
|
2012-07-21 12:09:45 +00:00
|
|
|
dlight_t *l;
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
if (!gl_flashblend->value)
|
2010-10-20 09:15:50 +00:00
|
|
|
{
|
2009-03-05 09:07:55 +00:00
|
|
|
return;
|
2010-10-20 09:15:50 +00:00
|
|
|
}
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2010-10-20 09:15:50 +00:00
|
|
|
/* because the count hasn't advanced yet for this frame */
|
|
|
|
r_dlightframecount = r_framecount + 1;
|
2012-04-29 13:57:33 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
qglDepthMask(0);
|
|
|
|
qglDisable(GL_TEXTURE_2D);
|
|
|
|
qglShadeModel(GL_SMOOTH);
|
|
|
|
qglEnable(GL_BLEND);
|
|
|
|
qglBlendFunc(GL_ONE, GL_ONE);
|
2009-03-05 09:07:55 +00:00
|
|
|
|
|
|
|
l = r_newrefdef.dlights;
|
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
for (i = 0; i < r_newrefdef.num_dlights; i++, l++)
|
2010-10-20 09:15:50 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
R_RenderDlight(l);
|
2010-10-20 09:15:50 +00:00
|
|
|
}
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
qglColor3f(1, 1, 1);
|
|
|
|
qglDisable(GL_BLEND);
|
|
|
|
qglEnable(GL_TEXTURE_2D);
|
|
|
|
qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
qglDepthMask(1);
|
2010-10-20 09:15:50 +00:00
|
|
|
}
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2010-10-20 09:15:50 +00:00
|
|
|
void
|
2012-07-21 12:09:45 +00:00
|
|
|
R_MarkLights(dlight_t *light, int bit, mnode_t *node)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
cplane_t *splitplane;
|
2010-10-20 09:15:50 +00:00
|
|
|
float dist;
|
2012-07-21 12:09:45 +00:00
|
|
|
msurface_t *surf;
|
2010-10-20 09:15:50 +00:00
|
|
|
int i;
|
2010-10-20 11:52:50 +00:00
|
|
|
int sidebit;
|
2010-10-20 09:15:50 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
if (node->contents != -1)
|
2010-10-20 09:15:50 +00:00
|
|
|
{
|
2009-03-05 09:07:55 +00:00
|
|
|
return;
|
2010-10-20 09:15:50 +00:00
|
|
|
}
|
2009-03-05 09:07:55 +00:00
|
|
|
|
|
|
|
splitplane = node->plane;
|
2012-07-21 12:09:45 +00:00
|
|
|
dist = DotProduct(light->origin, splitplane->normal) - splitplane->dist;
|
2010-10-20 09:15:50 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
if (dist > light->intensity - DLIGHT_CUTOFF)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
R_MarkLights(light, bit, node->children[0]);
|
2009-03-05 09:07:55 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-10-20 09:15:50 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
if (dist < -light->intensity + DLIGHT_CUTOFF)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
R_MarkLights(light, bit, node->children[1]);
|
2009-03-05 09:07:55 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-10-20 09:15:50 +00:00
|
|
|
|
|
|
|
/* mark the polygons */
|
2009-03-05 09:07:55 +00:00
|
|
|
surf = r_worldmodel->surfaces + node->firstsurface;
|
2010-10-20 09:15:50 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
for (i = 0; i < node->numsurfaces; i++, surf++)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
dist = DotProduct(light->origin, surf->plane->normal) - surf->plane->dist;
|
2010-10-20 11:52:50 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
if (dist >= 0)
|
|
|
|
{
|
2010-10-20 11:52:50 +00:00
|
|
|
sidebit = 0;
|
2012-07-21 12:09:45 +00:00
|
|
|
}
|
2010-10-20 11:52:50 +00:00
|
|
|
else
|
2012-07-21 12:09:45 +00:00
|
|
|
{
|
2010-10-20 11:52:50 +00:00
|
|
|
sidebit = SURF_PLANEBACK;
|
2012-07-21 12:09:45 +00:00
|
|
|
}
|
2010-10-20 11:52:50 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
if ((surf->flags & SURF_PLANEBACK) != sidebit)
|
|
|
|
{
|
2010-10-20 11:52:50 +00:00
|
|
|
continue;
|
2012-07-21 12:09:45 +00:00
|
|
|
}
|
2010-10-20 11:52:50 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
if (surf->dlightframe != r_dlightframecount)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
|
|
|
surf->dlightbits = 0;
|
|
|
|
surf->dlightframe = r_dlightframecount;
|
|
|
|
}
|
2010-10-20 09:15:50 +00:00
|
|
|
|
2009-03-05 09:07:55 +00:00
|
|
|
surf->dlightbits |= bit;
|
|
|
|
}
|
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
R_MarkLights(light, bit, node->children[0]);
|
|
|
|
R_MarkLights(light, bit, node->children[1]);
|
2009-03-05 09:07:55 +00:00
|
|
|
}
|
|
|
|
|
2010-10-20 09:15:50 +00:00
|
|
|
void
|
2012-07-21 12:09:45 +00:00
|
|
|
R_PushDlights(void)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
2010-10-20 09:15:50 +00:00
|
|
|
int i;
|
2012-07-21 12:09:45 +00:00
|
|
|
dlight_t *l;
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
if (gl_flashblend->value)
|
2010-10-20 09:15:50 +00:00
|
|
|
{
|
2009-03-05 09:07:55 +00:00
|
|
|
return;
|
2010-10-20 09:15:50 +00:00
|
|
|
}
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2010-10-20 09:15:50 +00:00
|
|
|
/* because the count hasn't advanced yet for this frame */
|
2012-04-29 13:57:33 +00:00
|
|
|
r_dlightframecount = r_framecount + 1;
|
|
|
|
|
2009-03-05 09:07:55 +00:00
|
|
|
l = r_newrefdef.dlights;
|
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
for (i = 0; i < r_newrefdef.num_dlights; i++, l++)
|
2010-10-20 09:15:50 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
R_MarkLights(l, 1 << i, r_worldmodel->nodes);
|
2010-10-20 09:15:50 +00:00
|
|
|
}
|
|
|
|
}
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2010-10-20 09:15:50 +00:00
|
|
|
int
|
2012-07-21 12:09:45 +00:00
|
|
|
R_RecursiveLightPoint(mnode_t *node, vec3_t start, vec3_t end)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
2010-10-20 09:15:50 +00:00
|
|
|
float front, back, frac;
|
|
|
|
int side;
|
2012-07-21 12:09:45 +00:00
|
|
|
cplane_t *plane;
|
2010-10-20 09:15:50 +00:00
|
|
|
vec3_t mid;
|
2012-07-21 12:09:45 +00:00
|
|
|
msurface_t *surf;
|
2010-10-20 09:15:50 +00:00
|
|
|
int s, t, ds, dt;
|
|
|
|
int i;
|
2012-07-21 12:09:45 +00:00
|
|
|
mtexinfo_t *tex;
|
|
|
|
byte *lightmap;
|
2010-10-20 09:15:50 +00:00
|
|
|
int maps;
|
|
|
|
int r;
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
if (node->contents != -1)
|
2010-10-20 09:15:50 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
return -1; /* didn't hit anything */
|
2010-10-20 09:15:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* calculate mid point */
|
2009-03-05 09:07:55 +00:00
|
|
|
plane = node->plane;
|
2012-07-21 12:09:45 +00:00
|
|
|
front = DotProduct(start, plane->normal) - plane->dist;
|
|
|
|
back = DotProduct(end, plane->normal) - plane->dist;
|
2009-03-05 09:07:55 +00:00
|
|
|
side = front < 0;
|
2010-10-20 09:15:50 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
if ((back < 0) == side)
|
2010-10-20 09:15:50 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
return R_RecursiveLightPoint(node->children[side], start, end);
|
2010-10-20 09:15:50 +00:00
|
|
|
}
|
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
frac = front / (front - back);
|
|
|
|
mid[0] = start[0] + (end[0] - start[0]) * frac;
|
|
|
|
mid[1] = start[1] + (end[1] - start[1]) * frac;
|
|
|
|
mid[2] = start[2] + (end[2] - start[2]) * frac;
|
2010-10-20 09:15:50 +00:00
|
|
|
|
|
|
|
/* go down front side */
|
2012-07-21 12:09:45 +00:00
|
|
|
r = R_RecursiveLightPoint(node->children[side], start, mid);
|
2010-10-20 09:15:50 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
if (r >= 0)
|
2010-10-20 09:15:50 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
return r; /* hit something */
|
2010-10-20 09:15:50 +00:00
|
|
|
}
|
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
if ((back < 0) == side)
|
2010-10-20 09:15:50 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
return -1; /* didn't hit anuthing */
|
2010-10-20 09:15:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check for impact on this node */
|
2012-07-21 12:09:45 +00:00
|
|
|
VectorCopy(mid, lightspot);
|
2009-03-05 09:07:55 +00:00
|
|
|
lightplane = plane;
|
|
|
|
|
|
|
|
surf = r_worldmodel->surfaces + node->firstsurface;
|
2010-10-20 09:15:50 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
for (i = 0; i < node->numsurfaces; i++, surf++)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
if (surf->flags & (SURF_DRAWTURB | SURF_DRAWSKY))
|
2010-10-20 09:15:50 +00:00
|
|
|
{
|
|
|
|
continue; /* no lightmaps */
|
|
|
|
}
|
2009-03-05 09:07:55 +00:00
|
|
|
|
|
|
|
tex = surf->texinfo;
|
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
s = DotProduct(mid, tex->vecs[0]) + tex->vecs[0][3];
|
|
|
|
t = DotProduct(mid, tex->vecs[1]) + tex->vecs[1][3];
|
2010-10-20 09:15:50 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
if ((s < surf->texturemins[0]) ||
|
|
|
|
(t < surf->texturemins[1]))
|
2010-10-20 09:15:50 +00:00
|
|
|
{
|
2009-03-05 09:07:55 +00:00
|
|
|
continue;
|
2010-10-20 09:15:50 +00:00
|
|
|
}
|
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
ds = s - surf->texturemins[0];
|
|
|
|
dt = t - surf->texturemins[1];
|
2010-10-20 09:15:50 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
if ((ds > surf->extents[0]) || (dt > surf->extents[1]))
|
2010-10-20 09:15:50 +00:00
|
|
|
{
|
2009-03-05 09:07:55 +00:00
|
|
|
continue;
|
2010-10-20 09:15:50 +00:00
|
|
|
}
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
if (!surf->samples)
|
2010-10-20 09:15:50 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
return 0;
|
2010-10-20 09:15:50 +00:00
|
|
|
}
|
2009-03-05 09:07:55 +00:00
|
|
|
|
|
|
|
ds >>= 4;
|
|
|
|
dt >>= 4;
|
|
|
|
|
|
|
|
lightmap = surf->samples;
|
2012-07-21 12:09:45 +00:00
|
|
|
VectorCopy(vec3_origin, pointcolor);
|
2010-10-20 09:15:50 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
if (lightmap)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
|
|
|
vec3_t scale;
|
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
lightmap += 3 * (dt * ((surf->extents[0] >> 4) + 1) + ds);
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
for (maps = 0; maps < MAXLIGHTMAPS && surf->styles[maps] != 255;
|
|
|
|
maps++)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
for (i = 0; i < 3; i++)
|
2010-10-20 09:15:50 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
scale[i] = gl_modulate->value *
|
|
|
|
r_newrefdef.lightstyles[surf->styles[maps]].rgb[i];
|
2010-10-20 09:15:50 +00:00
|
|
|
}
|
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
pointcolor[0] += lightmap[0] * scale[0] * (1.0 / 255);
|
|
|
|
pointcolor[1] += lightmap[1] * scale[1] * (1.0 / 255);
|
|
|
|
pointcolor[2] += lightmap[2] * scale[2] * (1.0 / 255);
|
|
|
|
lightmap += 3 * ((surf->extents[0] >> 4) + 1) *
|
|
|
|
((surf->extents[1] >> 4) + 1);
|
2009-03-05 09:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
2010-10-20 09:15:50 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
return 1;
|
2009-03-05 09:07:55 +00:00
|
|
|
}
|
|
|
|
|
2010-10-20 09:15:50 +00:00
|
|
|
/* go down back side */
|
2012-07-21 12:09:45 +00:00
|
|
|
return R_RecursiveLightPoint(node->children[!side], mid, end);
|
2009-03-05 09:07:55 +00:00
|
|
|
}
|
|
|
|
|
2010-10-20 09:15:50 +00:00
|
|
|
void
|
2012-07-21 12:09:45 +00:00
|
|
|
R_LightPoint(vec3_t p, vec3_t color)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
2010-10-20 09:15:50 +00:00
|
|
|
vec3_t end;
|
|
|
|
float r;
|
|
|
|
int lnum;
|
2012-07-21 12:09:45 +00:00
|
|
|
dlight_t *dl;
|
2010-10-20 09:15:50 +00:00
|
|
|
vec3_t dist;
|
|
|
|
float add;
|
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
if (!r_worldmodel->lightdata)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
color[0] = color[1] = color[2] = 1.0;
|
2009-03-05 09:07:55 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-10-20 09:15:50 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
end[0] = p[0];
|
|
|
|
end[1] = p[1];
|
|
|
|
end[2] = p[2] - 2048;
|
2010-10-20 09:15:50 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
r = R_RecursiveLightPoint(r_worldmodel->nodes, p, end);
|
2010-10-20 09:15:50 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
if (r == -1)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
VectorCopy(vec3_origin, color);
|
2009-03-05 09:07:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
VectorCopy(pointcolor, color);
|
2009-03-05 09:07:55 +00:00
|
|
|
}
|
|
|
|
|
2010-10-20 09:15:50 +00:00
|
|
|
/* add dynamic lights */
|
2009-03-05 09:07:55 +00:00
|
|
|
dl = r_newrefdef.dlights;
|
2010-10-20 09:15:50 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
for (lnum = 0; lnum < r_newrefdef.num_dlights; lnum++, dl++)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
VectorSubtract(currententity->origin,
|
|
|
|
dl->origin, dist);
|
|
|
|
add = dl->intensity - VectorLength(dist);
|
|
|
|
add *= (1.0 / 256);
|
2010-10-20 09:15:50 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
if (add > 0)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
VectorMA(color, add, dl->color, color);
|
2009-03-05 09:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
VectorScale(color, gl_modulate->value, color);
|
2009-03-05 09:07:55 +00:00
|
|
|
}
|
|
|
|
|
2010-10-20 09:15:50 +00:00
|
|
|
void
|
2012-07-21 12:09:45 +00:00
|
|
|
R_AddDynamicLights(msurface_t *surf)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
2010-10-20 09:15:50 +00:00
|
|
|
int lnum;
|
|
|
|
int sd, td;
|
|
|
|
float fdist, frad, fminlight;
|
|
|
|
vec3_t impact, local;
|
|
|
|
int s, t;
|
|
|
|
int i;
|
|
|
|
int smax, tmax;
|
2012-07-21 12:09:45 +00:00
|
|
|
mtexinfo_t *tex;
|
|
|
|
dlight_t *dl;
|
|
|
|
float *pfBL;
|
2010-10-20 09:15:50 +00:00
|
|
|
float fsacc, ftacc;
|
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
smax = (surf->extents[0] >> 4) + 1;
|
|
|
|
tmax = (surf->extents[1] >> 4) + 1;
|
2009-03-05 09:07:55 +00:00
|
|
|
tex = surf->texinfo;
|
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
for (lnum = 0; lnum < r_newrefdef.num_dlights; lnum++)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
if (!(surf->dlightbits & (1 << lnum)))
|
2010-10-20 09:15:50 +00:00
|
|
|
{
|
|
|
|
continue; /* not lit by this light */
|
|
|
|
}
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
dl = &r_newrefdef.dlights[lnum];
|
2009-03-05 09:07:55 +00:00
|
|
|
frad = dl->intensity;
|
2012-07-21 12:09:45 +00:00
|
|
|
fdist = DotProduct(dl->origin, surf->plane->normal) -
|
2009-03-05 09:07:55 +00:00
|
|
|
surf->plane->dist;
|
2012-07-21 12:09:45 +00:00
|
|
|
frad -= fabs(fdist);
|
2010-10-20 09:15:50 +00:00
|
|
|
|
|
|
|
/* rad is now the highest intensity on the plane */
|
|
|
|
fminlight = DLIGHT_CUTOFF;
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
if (frad < fminlight)
|
2010-10-20 09:15:50 +00:00
|
|
|
{
|
2009-03-05 09:07:55 +00:00
|
|
|
continue;
|
2010-10-20 09:15:50 +00:00
|
|
|
}
|
|
|
|
|
2009-03-05 09:07:55 +00:00
|
|
|
fminlight = frad - fminlight;
|
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
for (i = 0; i < 3; i++)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
impact[i] = dl->origin[i] -
|
|
|
|
surf->plane->normal[i] * fdist;
|
2009-03-05 09:07:55 +00:00
|
|
|
}
|
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
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];
|
2009-03-05 09:07:55 +00:00
|
|
|
|
|
|
|
pfBL = s_blocklights;
|
2010-10-20 09:15:50 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
for (t = 0, ftacc = 0; t < tmax; t++, ftacc += 16)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
td = local[1] - ftacc;
|
2010-10-20 09:15:50 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
if (td < 0)
|
2010-10-20 09:15:50 +00:00
|
|
|
{
|
2009-03-05 09:07:55 +00:00
|
|
|
td = -td;
|
2010-10-20 09:15:50 +00:00
|
|
|
}
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
for (s = 0, fsacc = 0; s < smax; s++, fsacc += 16, pfBL += 3)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
sd = Q_ftol(local[0] - fsacc);
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
if (sd < 0)
|
2010-10-20 09:15:50 +00:00
|
|
|
{
|
2009-03-05 09:07:55 +00:00
|
|
|
sd = -sd;
|
2010-10-20 09:15:50 +00:00
|
|
|
}
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
if (sd > td)
|
2010-10-20 09:15:50 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
fdist = sd + (td >> 1);
|
2010-10-20 09:15:50 +00:00
|
|
|
}
|
2009-03-05 09:07:55 +00:00
|
|
|
else
|
2010-10-20 09:15:50 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
fdist = td + (sd >> 1);
|
2010-10-20 09:15:50 +00:00
|
|
|
}
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
if (fdist < fminlight)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
pfBL[0] += (frad - fdist) * dl->color[0];
|
|
|
|
pfBL[1] += (frad - fdist) * dl->color[1];
|
|
|
|
pfBL[2] += (frad - fdist) * dl->color[2];
|
2009-03-05 09:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-20 09:15:50 +00:00
|
|
|
void
|
2012-07-21 12:09:45 +00:00
|
|
|
R_SetCacheState(msurface_t *surf)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
|
|
|
int maps;
|
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
for (maps = 0; maps < MAXLIGHTMAPS && surf->styles[maps] != 255;
|
|
|
|
maps++)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
surf->cached_light[maps] =
|
|
|
|
r_newrefdef.lightstyles[surf->styles[maps]].white;
|
2009-03-05 09:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-10-20 09:15:50 +00:00
|
|
|
* Combine and scale multiple lightmaps into the floating format in blocklights
|
|
|
|
*/
|
|
|
|
void
|
2012-07-21 12:09:45 +00:00
|
|
|
R_BuildLightMap(msurface_t *surf, byte *dest, int stride)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
2010-10-20 09:15:50 +00:00
|
|
|
int smax, tmax;
|
|
|
|
int r, g, b, a, max;
|
|
|
|
int i, j, size;
|
2012-07-21 12:09:45 +00:00
|
|
|
byte *lightmap;
|
|
|
|
float scale[4];
|
2010-10-20 09:15:50 +00:00
|
|
|
int nummaps;
|
2012-07-21 12:09:45 +00:00
|
|
|
float *bl;
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
if (surf->texinfo->flags &
|
|
|
|
(SURF_SKY | SURF_TRANS33 | SURF_TRANS66 | SURF_WARP))
|
2010-10-20 09:15:50 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
ri.Sys_Error(ERR_DROP, "R_BuildLightMap called for non-lit surface");
|
2010-10-20 09:15:50 +00:00
|
|
|
}
|
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
smax = (surf->extents[0] >> 4) + 1;
|
|
|
|
tmax = (surf->extents[1] >> 4) + 1;
|
2010-10-20 09:15:50 +00:00
|
|
|
size = smax * tmax;
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
if (size > (sizeof(s_blocklights) >> 4))
|
2010-10-20 09:15:50 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
ri.Sys_Error(ERR_DROP, "Bad s_blocklights size");
|
2010-10-20 09:15:50 +00:00
|
|
|
}
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2010-10-20 09:15:50 +00:00
|
|
|
/* set to full bright if no light data */
|
2012-07-21 12:09:45 +00:00
|
|
|
if (!surf->samples)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
for (i = 0; i < size * 3; i++)
|
2010-10-20 09:15:50 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
s_blocklights[i] = 255;
|
2010-10-20 09:15:50 +00:00
|
|
|
}
|
|
|
|
|
2009-03-05 09:07:55 +00:00
|
|
|
goto store;
|
|
|
|
}
|
|
|
|
|
2010-10-20 09:15:50 +00:00
|
|
|
/* count the # of maps */
|
2012-07-21 12:09:45 +00:00
|
|
|
for (nummaps = 0; nummaps < MAXLIGHTMAPS && surf->styles[nummaps] != 255;
|
|
|
|
nummaps++)
|
2010-10-20 09:15:50 +00:00
|
|
|
{
|
|
|
|
}
|
2009-03-05 09:07:55 +00:00
|
|
|
|
|
|
|
lightmap = surf->samples;
|
|
|
|
|
2010-10-20 09:15:50 +00:00
|
|
|
/* add all the lightmaps */
|
2012-07-21 12:09:45 +00:00
|
|
|
if (nummaps == 1)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
|
|
|
int maps;
|
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
for (maps = 0; maps < MAXLIGHTMAPS && surf->styles[maps] != 255; maps++)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
|
|
|
bl = s_blocklights;
|
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
for (i = 0; i < 3; i++)
|
2010-10-20 09:15:50 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
scale[i] = gl_modulate->value *
|
|
|
|
r_newrefdef.lightstyles[surf->styles[maps]].rgb[i];
|
2010-10-20 09:15:50 +00:00
|
|
|
}
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
if ((scale[0] == 1.0F) &&
|
|
|
|
(scale[1] == 1.0F) &&
|
|
|
|
(scale[2] == 1.0F))
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
for (i = 0; i < size; i++, bl += 3)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
bl[0] = lightmap[i * 3 + 0];
|
|
|
|
bl[1] = lightmap[i * 3 + 1];
|
|
|
|
bl[2] = lightmap[i * 3 + 2];
|
2009-03-05 09:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
for (i = 0; i < size; i++, bl += 3)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
bl[0] = lightmap[i * 3 + 0] * scale[0];
|
|
|
|
bl[1] = lightmap[i * 3 + 1] * scale[1];
|
|
|
|
bl[2] = lightmap[i * 3 + 2] * scale[2];
|
2009-03-05 09:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
2010-10-20 09:15:50 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
lightmap += size * 3; /* skip to next lightmap */
|
2009-03-05 09:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int maps;
|
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
memset(s_blocklights, 0, sizeof(s_blocklights[0]) * size * 3);
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
for (maps = 0; maps < MAXLIGHTMAPS && surf->styles[maps] != 255; maps++)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
|
|
|
bl = s_blocklights;
|
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
for (i = 0; i < 3; i++)
|
2010-10-20 09:15:50 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
scale[i] = gl_modulate->value *
|
|
|
|
r_newrefdef.lightstyles[surf->styles[maps]].rgb[i];
|
2010-10-20 09:15:50 +00:00
|
|
|
}
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
if ((scale[0] == 1.0F) &&
|
|
|
|
(scale[1] == 1.0F) &&
|
|
|
|
(scale[2] == 1.0F))
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
for (i = 0; i < size; i++, bl += 3)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
bl[0] += lightmap[i * 3 + 0];
|
|
|
|
bl[1] += lightmap[i * 3 + 1];
|
|
|
|
bl[2] += lightmap[i * 3 + 2];
|
2009-03-05 09:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
for (i = 0; i < size; i++, bl += 3)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
bl[0] += lightmap[i * 3 + 0] * scale[0];
|
|
|
|
bl[1] += lightmap[i * 3 + 1] * scale[1];
|
|
|
|
bl[2] += lightmap[i * 3 + 2] * scale[2];
|
2009-03-05 09:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
2010-10-20 09:15:50 +00:00
|
|
|
|
|
|
|
lightmap += size * 3; /* skip to next lightmap */
|
2009-03-05 09:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-20 09:15:50 +00:00
|
|
|
/* add all the dynamic lights */
|
2012-07-21 12:09:45 +00:00
|
|
|
if (surf->dlightframe == r_framecount)
|
2010-10-20 09:15:50 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
R_AddDynamicLights(surf);
|
2010-10-20 09:15:50 +00:00
|
|
|
}
|
2009-03-05 09:07:55 +00:00
|
|
|
|
|
|
|
store:
|
2010-10-21 07:55:41 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
stride -= (smax << 2);
|
2009-03-05 09:07:55 +00:00
|
|
|
bl = s_blocklights;
|
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
for (i = 0; i < tmax; i++, dest += stride)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
for (j = 0; j < smax; j++)
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
2012-07-21 12:09:45 +00:00
|
|
|
r = Q_ftol(bl[0]);
|
|
|
|
g = Q_ftol(bl[1]);
|
|
|
|
b = Q_ftol(bl[2]);
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2010-10-22 08:20:49 +00:00
|
|
|
/* catch negative lights */
|
2012-07-21 12:09:45 +00:00
|
|
|
if (r < 0)
|
2010-10-22 08:20:49 +00:00
|
|
|
{
|
|
|
|
r = 0;
|
|
|
|
}
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
if (g < 0)
|
2010-10-22 08:20:49 +00:00
|
|
|
{
|
|
|
|
g = 0;
|
|
|
|
}
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
if (b < 0)
|
2010-10-22 08:20:49 +00:00
|
|
|
{
|
|
|
|
b = 0;
|
|
|
|
}
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2010-10-22 08:20:49 +00:00
|
|
|
/* determine the brightest of the three color components */
|
2012-07-21 12:09:45 +00:00
|
|
|
if (r > g)
|
2010-10-22 08:20:49 +00:00
|
|
|
{
|
|
|
|
max = r;
|
2009-03-05 09:07:55 +00:00
|
|
|
}
|
2010-10-22 08:20:49 +00:00
|
|
|
else
|
2009-03-05 09:07:55 +00:00
|
|
|
{
|
2010-10-22 08:20:49 +00:00
|
|
|
max = g;
|
|
|
|
}
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
if (b > max)
|
2010-10-22 08:20:49 +00:00
|
|
|
{
|
|
|
|
max = b;
|
|
|
|
}
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
/* 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. */
|
2010-10-22 08:20:49 +00:00
|
|
|
a = max;
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
/* rescale all the color components if the
|
|
|
|
intensity of the greatest channel exceeds
|
|
|
|
1.0 */
|
|
|
|
if (max > 255)
|
2010-10-22 08:20:49 +00:00
|
|
|
{
|
|
|
|
float t = 255.0F / max;
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2010-10-22 08:20:49 +00:00
|
|
|
r = r * t;
|
|
|
|
g = g * t;
|
|
|
|
b = b * t;
|
|
|
|
a = a * t;
|
|
|
|
}
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2012-07-21 12:09:45 +00:00
|
|
|
dest[0] = r;
|
|
|
|
dest[1] = g;
|
|
|
|
dest[2] = b;
|
|
|
|
dest[3] = a;
|
2009-03-05 09:07:55 +00:00
|
|
|
|
2010-10-22 08:20:49 +00:00
|
|
|
bl += 3;
|
|
|
|
dest += 4;
|
2009-03-05 09:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-07-21 12:09:45 +00:00
|
|
|
|