]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_rsurf.c
bmodel shadow volumes
[xonotic/darkplaces.git] / gl_rsurf.c
index 9cdd6cf64068f2eac009bc191dbba720ac585888..9448717d8f77e6d51a6954f6f6d71c89f51ce466 100644 (file)
@@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 // r_surf.c: surface-related refresh code
 
 #include "quakedef.h"
+#include "r_shadow.h"
 
 #define MAX_LIGHTMAP_SIZE 256
 
@@ -46,17 +47,10 @@ int r_pvsviewleafnovis = 0;
 
 static int R_IntAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
 {
-       int sdtable[256], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, red, green, blue, lit, dist2, impacts, impactt, subtract;
+       int sdtable[256], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, red, green, blue, lit, dist2, impacts, impactt, subtract, k;
        unsigned int *bl;
        float dist, impact[3], local[3];
 
-       // LordHavoc: use 64bit integer...  shame it's not very standardized...
-#if _MSC_VER || __BORLANDC__
-       __int64     k;
-#else
-       long long   k;
-#endif
-
        lit = false;
 
        smax = (surf->extents[0] >> 4) + 1;
@@ -108,9 +102,9 @@ static int R_IntAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
                maxdist3 = maxdist - dist2;
 
                // convert to 8.8 blocklights format
-               red = r_dlight[lnum].light[0];
-               green = r_dlight[lnum].light[1];
-               blue = r_dlight[lnum].light[2];
+               red = r_dlight[lnum].light[0] * (1.0f / 128.0f);
+               green = r_dlight[lnum].light[1] * (1.0f / 128.0f);
+               blue = r_dlight[lnum].light[2] * (1.0f / 128.0f);
                subtract = (int) (r_dlight[lnum].subtract * 4194304.0f);
                bl = intblocklights;
 
@@ -129,9 +123,9 @@ static int R_IntAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
                                                k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
                                                if (k > 0)
                                                {
-                                                       bl[0] += (red   * k) >> 7;
-                                                       bl[1] += (green * k) >> 7;
-                                                       bl[2] += (blue  * k) >> 7;
+                                                       bl[0] += (red   * k);
+                                                       bl[1] += (green * k);
+                                                       bl[2] += (blue  * k);
                                                        lit = true;
                                                }
                                        }
@@ -252,7 +246,7 @@ static void R_BuildLightMap (const entity_render_t *ent, msurface_t *surf, int d
 
                // update cached lighting info
                surf->cached_dlight = 0;
-               surf->cached_lightscalebit = lightscalebit;
+               surf->cached_lightmapscalebit = r_lightmapscalebit;
                surf->cached_ambient = r_ambient.value;
                surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
                surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
@@ -307,7 +301,7 @@ static void R_BuildLightMap (const entity_render_t *ent, msurface_t *surf, int d
                bl = intblocklights;
                out = templight;
                // deal with lightmap brightness scale
-               shift = 7 + lightscalebit + 8;
+               shift = 7 + r_lightmapscalebit + 8;
                if (ent->model->lightmaprgba)
                {
                        stride = (surf->lightmaptexturestride - smax) * 4;
@@ -346,7 +340,7 @@ static void R_BuildLightMap (const entity_render_t *ent, msurface_t *surf, int d
 
                // update cached lighting info
                surf->cached_dlight = 0;
-               surf->cached_lightscalebit = lightscalebit;
+               surf->cached_lightmapscalebit = r_lightmapscalebit;
                surf->cached_ambient = r_ambient.value;
                surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
                surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
@@ -397,7 +391,7 @@ static void R_BuildLightMap (const entity_render_t *ent, msurface_t *surf, int d
                bl = floatblocklights;
                out = templight;
                // deal with lightmap brightness scale
-               scale = 1.0f / (1 << (7 + lightscalebit + 8));
+               scale = 1.0f / (1 << (7 + r_lightmapscalebit + 8));
                if (ent->model->lightmaprgba)
                {
                        stride = (surf->lightmaptexturestride - smax) * 4;
@@ -430,30 +424,16 @@ static void R_BuildLightMap (const entity_render_t *ent, msurface_t *surf, int d
        }
 }
 
-void R_StainNode (mnode_t *node, model_t *model, vec3_t origin, float radius, int icolor[8])
+void R_StainNode (mnode_t *node, model_t *model, vec3_t origin, float radius, float fcolor[8])
 {
-       float ndist;
+       float ndist, a, ratio, maxdist, maxdist2, maxdist3, invradius, sdtable[256], td, dist2;
        msurface_t *surf, *endsurf;
-       int sdtable[256], td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, dist2, impacts, impactt, subtract, a, stained, cr, cg, cb, ca, ratio;
+       int i, s, t, smax, tmax, smax3, impacts, impactt, stained;
        qbyte *bl;
        vec3_t impact;
-       // LordHavoc: use 64bit integer...  shame it's not very standardized...
-#if _MSC_VER || __BORLANDC__
-       __int64     k;
-#else
-       long long   k;
-#endif
-
 
-       // for comparisons to minimum acceptable light
-       // compensate for 256 offset
-       maxdist = radius * radius + 256.0f;
-
-       // clamp radius to avoid exceeding 32768 entry division table
-       if (maxdist > 4194304)
-               maxdist = 4194304;
-
-       subtract = (int) ((1.0f / maxdist) * 4194304.0f);
+       maxdist = radius * radius;
+       invradius = 1.0f / radius;
 
 loc0:
        if (node->contents < 0)
@@ -470,89 +450,78 @@ loc0:
                goto loc0;
        }
 
-       dist2 = ndist * ndist + 256.0f;
-       if (dist2 < maxdist)
-       {
-               maxdist3 = maxdist - dist2;
+       dist2 = ndist * ndist;
+       maxdist3 = maxdist - dist2;
 
-               if (node->plane->type < 3)
-               {
-                       VectorCopy(origin, impact);
-                       impact[node->plane->type] -= ndist;
-               }
-               else
-               {
-                       impact[0] = origin[0] - node->plane->normal[0] * ndist;
-                       impact[1] = origin[1] - node->plane->normal[1] * ndist;
-                       impact[2] = origin[2] - node->plane->normal[2] * ndist;
-               }
+       if (node->plane->type < 3)
+       {
+               VectorCopy(origin, impact);
+               impact[node->plane->type] -= ndist;
+       }
+       else
+       {
+               impact[0] = origin[0] - node->plane->normal[0] * ndist;
+               impact[1] = origin[1] - node->plane->normal[1] * ndist;
+               impact[2] = origin[2] - node->plane->normal[2] * ndist;
+       }
 
-               for (surf = model->surfaces + node->firstsurface, endsurf = surf + node->numsurfaces;surf < endsurf;surf++)
+       for (surf = model->surfaces + node->firstsurface, endsurf = surf + node->numsurfaces;surf < endsurf;surf++)
+       {
+               if (surf->stainsamples)
                {
-                       if (surf->stainsamples)
-                       {
-                               smax = (surf->extents[0] >> 4) + 1;
-                               tmax = (surf->extents[1] >> 4) + 1;
+                       smax = (surf->extents[0] >> 4) + 1;
+                       tmax = (surf->extents[1] >> 4) + 1;
 
-                               impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
-                               impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
+                       impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
+                       impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
 
-                               s = bound(0, impacts, smax * 16) - impacts;
-                               t = bound(0, impactt, tmax * 16) - impactt;
-                               i = s * s + t * t + dist2;
-                               if (i > maxdist)
-                                       continue;
+                       s = bound(0, impacts, smax * 16) - impacts;
+                       t = bound(0, impactt, tmax * 16) - impactt;
+                       i = s * s + t * t + dist2;
+                       if (i > maxdist)
+                               continue;
 
-                               // reduce calculations
-                               for (s = 0, i = impacts; s < smax; s++, i -= 16)
-                                       sdtable[s] = i * i + dist2;
+                       // reduce calculations
+                       for (s = 0, i = impacts; s < smax; s++, i -= 16)
+                               sdtable[s] = i * i + dist2;
 
-                               // convert to 8.8 blocklights format
-                               bl = surf->stainsamples;
-                               smax3 = smax * 3;
-                               stained = false;
+                       bl = surf->stainsamples;
+                       smax3 = smax * 3;
+                       stained = false;
 
-                               i = impactt;
-                               for (t = 0;t < tmax;t++, i -= 16)
+                       i = impactt;
+                       for (t = 0;t < tmax;t++, i -= 16)
+                       {
+                               td = i * i;
+                               // make sure some part of it is visible on this line
+                               if (td < maxdist3)
                                {
-                                       td = i * i;
-                                       // make sure some part of it is visible on this line
-                                       if (td < maxdist3)
+                                       maxdist2 = maxdist - td;
+                                       for (s = 0;s < smax;s++)
                                        {
-                                               maxdist2 = maxdist - td;
-                                               for (s = 0;s < smax;s++)
+                                               if (sdtable[s] < maxdist2)
                                                {
-                                                       if (sdtable[s] < maxdist2)
+                                                       ratio = lhrandom(0.0f, 1.0f);
+                                                       a = (fcolor[3] + ratio * fcolor[7]) * (1.0f - sqrt(sdtable[s] + td) * invradius);
+                                                       if (a >= (1.0f / 64.0f))
                                                        {
-                                                               k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
-                                                               if (k > 0)
-                                                               {
-                                                                       ratio = rand() & 255;
-                                                                       ca = (((icolor[7] - icolor[3]) * ratio) >> 8) + icolor[3];
-                                                                       a = (ca * k) >> 8;
-                                                                       if (a > 0)
-                                                                       {
-                                                                               a = bound(0, a, 256);
-                                                                               cr = (((icolor[4] - icolor[0]) * ratio) >> 8) + icolor[0];
-                                                                               cg = (((icolor[5] - icolor[1]) * ratio) >> 8) + icolor[1];
-                                                                               cb = (((icolor[6] - icolor[2]) * ratio) >> 8) + icolor[2];
-                                                                               bl[0] = (qbyte) ((((cr - (int) bl[0]) * a) >> 8) + (int) bl[0]);
-                                                                               bl[1] = (qbyte) ((((cg - (int) bl[1]) * a) >> 8) + (int) bl[1]);
-                                                                               bl[2] = (qbyte) ((((cb - (int) bl[2]) * a) >> 8) + (int) bl[2]);
-                                                                               stained = true;
-                                                                       }
-                                                               }
+                                                               if (a > 1)
+                                                                       a = 1;
+                                                               bl[0] = (qbyte) ((float) bl[0] + a * ((fcolor[0] + ratio * fcolor[4]) - (float) bl[0]));
+                                                               bl[1] = (qbyte) ((float) bl[1] + a * ((fcolor[1] + ratio * fcolor[5]) - (float) bl[1]));
+                                                               bl[2] = (qbyte) ((float) bl[2] + a * ((fcolor[2] + ratio * fcolor[6]) - (float) bl[2]));
+                                                               stained = true;
                                                        }
-                                                       bl += 3;
                                                }
+                                               bl += 3;
                                        }
-                                       else // skip line
-                                               bl += smax3;
                                }
-                               // force lightmap upload
-                               if (stained)
-                                       surf->cached_dlight = true;
+                               else // skip line
+                                       bl += smax3;
                        }
+                       // force lightmap upload
+                       if (stained)
+                               surf->cached_dlight = true;
                }
        }
 
@@ -560,7 +529,7 @@ loc0:
        {
                if (node->children[1]->contents >= 0)
                {
-                       R_StainNode(node->children[0], model, origin, radius, icolor);
+                       R_StainNode(node->children[0], model, origin, radius, fcolor);
                        node = node->children[1];
                        goto loc0;
                }
@@ -579,22 +548,23 @@ loc0:
 
 void R_Stain (vec3_t origin, float radius, int cr1, int cg1, int cb1, int ca1, int cr2, int cg2, int cb2, int ca2)
 {
-       int n, icolor[8];
+       int n;
+       float fcolor[8];
        entity_render_t *ent;
        model_t *model;
        vec3_t org;
-       icolor[0] = cr1;
-       icolor[1] = cg1;
-       icolor[2] = cb1;
-       icolor[3] = ca1;
-       icolor[4] = cr2;
-       icolor[5] = cg2;
-       icolor[6] = cb2;
-       icolor[7] = ca2;
+       fcolor[0] = cr1;
+       fcolor[1] = cg1;
+       fcolor[2] = cb1;
+       fcolor[3] = ca1 * (1.0f / 64.0f);
+       fcolor[4] = cr2 - cr1;
+       fcolor[5] = cg2 - cg1;
+       fcolor[6] = cb2 - cb1;
+       fcolor[7] = (ca2 - ca1) * (1.0f / 64.0f);
 
        model = cl.worldmodel;
        if (model)
-               R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, origin, radius, icolor);
+               R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, origin, radius, fcolor);
 
        // look for embedded bmodels
        for (n = 0;n < cl_num_brushmodel_entities;n++)
@@ -607,7 +577,7 @@ void R_Stain (vec3_t origin, float radius, int cr1, int cg1, int cb1, int ca1, i
                        if (model->type == mod_brush)
                        {
                                Matrix4x4_Transform(&ent->inversematrix, origin, org);
-                               R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, org, radius, icolor);
+                               R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, org, radius, fcolor);
                        }
                }
        }
@@ -805,6 +775,9 @@ static void RSurfShader_Sky(const entity_render_t *ent, const texture_t *texture
        memset(&m, 0, sizeof(m));
        if (skyrendermasked)
        {
+               qglColorMask(0,0,0,0);
+               // just to make sure that braindead drivers don't draw anything
+               // despite that colormask...
                m.blendfunc1 = GL_ZERO;
                m.blendfunc2 = GL_ONE;
        }
@@ -814,7 +787,6 @@ static void RSurfShader_Sky(const entity_render_t *ent, const texture_t *texture
                m.blendfunc1 = GL_ONE;
                m.blendfunc2 = GL_ZERO;
        }
-       m.wantoverbright = false;
        m.depthwrite = true;
        R_Mesh_State(&m);
        for (surf = texture->surfacechain;surf;surf = surf->texturechain)
@@ -823,20 +795,18 @@ static void RSurfShader_Sky(const entity_render_t *ent, const texture_t *texture
                {
                        R_Mesh_ResizeCheck(mesh->numverts);
                        memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
-                       if (skyrendermasked)
-                               memset(varray_color, 0, mesh->numverts * sizeof(float[4]));
-                       else
-                               R_FillColors(varray_color, mesh->numverts, fogcolor[0] * mesh_colorscale, fogcolor[1] * mesh_colorscale, fogcolor[2] * mesh_colorscale, 1);
+                       GL_Color(fogcolor[0] * r_colorscale, fogcolor[1] * r_colorscale, fogcolor[2] * r_colorscale, 1);
                        R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
                }
        }
+       qglColorMask(1,1,1,1);
 }
 
 static void RSurfShader_Water_Callback(const void *calldata1, int calldata2)
 {
        const entity_render_t *ent = calldata1;
        const msurface_t *surf = ent->model->surfaces + calldata2;
-       float f;
+       float f, colorscale;
        const surfmesh_t *mesh;
        rmeshstate_t m;
        float alpha = ent->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
@@ -861,9 +831,15 @@ static void RSurfShader_Water_Callback(const void *calldata1, int calldata2)
                m.blendfunc1 = GL_ONE;
                m.blendfunc2 = GL_ZERO;
        }
-       m.wantoverbright = true;
        m.tex[0] = R_GetTexture(surf->currenttexture->texture);
+       colorscale = r_colorscale;
+       if (gl_combine.integer)
+       {
+               m.texrgbscale[0] = 4;
+               colorscale *= 0.25f;
+       }
        R_Mesh_State(&m);
+       GL_UseColorArray();
        for (mesh = surf->mesh;mesh;mesh = mesh->chain)
        {
                R_Mesh_ResizeCheck(mesh->numverts);
@@ -878,7 +854,7 @@ static void RSurfShader_Water_Callback(const void *calldata1, int calldata2)
                        if (surf->flags & SURF_LIGHTMAP)
                                RSurf_AddLightmapToVertexColors(mesh->lightmapoffsets, varray_color, mesh->numverts, surf->samples, ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3, surf->styles);
                }
-               RSurf_FogColors(varray_vertex, varray_color, mesh_colorscale, mesh->numverts, modelorg);
+               RSurf_FogColors(varray_vertex, varray_color, colorscale, mesh->numverts, modelorg);
                R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
        }
 
@@ -887,7 +863,6 @@ static void RSurfShader_Water_Callback(const void *calldata1, int calldata2)
                memset(&m, 0, sizeof(m));
                m.blendfunc1 = GL_SRC_ALPHA;
                m.blendfunc2 = GL_ONE;
-               m.wantoverbright = false;
                m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
                R_Mesh_State(&m);
                for (mesh = surf->mesh;mesh;mesh = mesh->chain)
@@ -896,7 +871,7 @@ static void RSurfShader_Water_Callback(const void *calldata1, int calldata2)
                        memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
                        if (m.tex[0])
                                memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
-                       RSurf_FogPassColors(varray_vertex, varray_color, fogcolor[0], fogcolor[1], fogcolor[2], alpha, mesh_colorscale, mesh->numverts, modelorg);
+                       RSurf_FogPassColors(varray_vertex, varray_color, fogcolor[0], fogcolor[1], fogcolor[2], alpha, r_colorscale, mesh->numverts, modelorg);
                        R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
                }
        }
@@ -921,7 +896,7 @@ static void RSurfShader_Water(const entity_render_t *ent, const texture_t *textu
 
 static void RSurfShader_Wall_Pass_BaseVertex(const entity_render_t *ent, const msurface_t *surf)
 {
-       float base;
+       float base, colorscale;
        const surfmesh_t *mesh;
        rmeshstate_t m;
        float modelorg[3];
@@ -942,10 +917,16 @@ static void RSurfShader_Wall_Pass_BaseVertex(const entity_render_t *ent, const m
                m.blendfunc1 = GL_ONE;
                m.blendfunc2 = GL_ZERO;
        }
-       m.wantoverbright = true;
        m.tex[0] = R_GetTexture(surf->currenttexture->texture);
+       colorscale = r_colorscale;
+       if (gl_combine.integer)
+       {
+               m.texrgbscale[0] = 4;
+               colorscale *= 0.25f;
+       }
        base = ent->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f);
        R_Mesh_State(&m);
+       GL_UseColorArray();
        for (mesh = surf->mesh;mesh;mesh = mesh->chain)
        {
                R_Mesh_ResizeCheck(mesh->numverts);
@@ -959,7 +940,7 @@ static void RSurfShader_Wall_Pass_BaseVertex(const entity_render_t *ent, const m
                        if (surf->flags & SURF_LIGHTMAP)
                                RSurf_AddLightmapToVertexColors(mesh->lightmapoffsets, varray_color, mesh->numverts, surf->samples, ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3, surf->styles);
                }
-               RSurf_FogColors(varray_vertex, varray_color, mesh_colorscale, mesh->numverts, modelorg);
+               RSurf_FogColors(varray_vertex, varray_color, colorscale, mesh->numverts, modelorg);
                R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
        }
 }
@@ -986,15 +967,15 @@ static void RSurfShader_Wall_Pass_BaseFullbright(const entity_render_t *ent, con
                m.blendfunc1 = GL_ONE;
                m.blendfunc2 = GL_ZERO;
        }
-       m.wantoverbright = false;
        m.tex[0] = R_GetTexture(surf->currenttexture->texture);
        R_Mesh_State(&m);
+       GL_UseColorArray();
        for (mesh = surf->mesh;mesh;mesh = mesh->chain)
        {
                R_Mesh_ResizeCheck(mesh->numverts);
                memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
                memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
-               RSurf_FoggedColors(varray_vertex, varray_color, 1, 1, 1, ent->alpha, mesh_colorscale, mesh->numverts, modelorg);
+               RSurf_FoggedColors(varray_vertex, varray_color, 1, 1, 1, ent->alpha, r_colorscale, mesh->numverts, modelorg);
                R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
        }
 }
@@ -1008,15 +989,15 @@ static void RSurfShader_Wall_Pass_Glow(const entity_render_t *ent, const msurfac
        memset(&m, 0, sizeof(m));
        m.blendfunc1 = GL_SRC_ALPHA;
        m.blendfunc2 = GL_ONE;
-       m.wantoverbright = false;
        m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
        R_Mesh_State(&m);
+       GL_UseColorArray();
        for (mesh = surf->mesh;mesh;mesh = mesh->chain)
        {
                R_Mesh_ResizeCheck(mesh->numverts);
                memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
                memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
-               RSurf_FoggedColors(varray_vertex, varray_color, 1, 1, 1, ent->alpha, mesh_colorscale, mesh->numverts, modelorg);
+               RSurf_FoggedColors(varray_vertex, varray_color, 1, 1, 1, ent->alpha, r_colorscale, mesh->numverts, modelorg);
                R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
        }
 }
@@ -1030,16 +1011,16 @@ static void RSurfShader_Wall_Pass_Fog(const entity_render_t *ent, const msurface
        memset(&m, 0, sizeof(m));
        m.blendfunc1 = GL_SRC_ALPHA;
        m.blendfunc2 = GL_ONE;
-       m.wantoverbright = false;
        m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
        R_Mesh_State(&m);
+       GL_UseColorArray();
        for (mesh = surf->mesh;mesh;mesh = mesh->chain)
        {
                R_Mesh_ResizeCheck(mesh->numverts);
                memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
                if (m.tex[0])
                        memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
-               RSurf_FogPassColors(varray_vertex, varray_color, fogcolor[0], fogcolor[1], fogcolor[2], ent->alpha, mesh_colorscale, mesh->numverts, modelorg);
+               RSurf_FogPassColors(varray_vertex, varray_color, fogcolor[0], fogcolor[1], fogcolor[2], ent->alpha, r_colorscale, mesh->numverts, modelorg);
                R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
        }
 }
@@ -1054,7 +1035,6 @@ static void RSurfShader_OpaqueWall_Pass_BaseTripleTexCombine(const entity_render
        memset(&m, 0, sizeof(m));
        m.blendfunc1 = GL_ONE;
        m.blendfunc2 = GL_ZERO;
-       //m.wantoverbright = false;
        m.tex[0] = R_GetTexture(texture->texture);
        m.tex[1] = R_GetTexture(texture->surfacechain->lightmaptexture);
        m.tex[2] = R_GetTexture(texture->detailtexture);
@@ -1062,6 +1042,8 @@ static void RSurfShader_OpaqueWall_Pass_BaseTripleTexCombine(const entity_render
        m.texrgbscale[1] = 4;
        m.texrgbscale[2] = 2;
        R_Mesh_State(&m);
+       cl = (float) (1 << r_lightmapscalebit) * r_colorscale;
+       GL_Color(cl, cl, cl, 1);
        for (surf = texture->surfacechain;surf;surf = surf->texturechain)
        {
                lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
@@ -1077,8 +1059,6 @@ static void RSurfShader_OpaqueWall_Pass_BaseTripleTexCombine(const entity_render
                        memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
                        memcpy(varray_texcoord[1], mesh->uv, mesh->numverts * sizeof(float[2]));
                        memcpy(varray_texcoord[2], mesh->ab, mesh->numverts * sizeof(float[2]));
-                       cl = (float) (1 << lightscalebit) * mesh_colorscale;
-                       R_FillColors(varray_color, mesh->numverts, cl, cl, cl, 1);
                        R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
                }
        }
@@ -1090,14 +1070,15 @@ static void RSurfShader_OpaqueWall_Pass_BaseDoubleTex(const entity_render_t *ent
        const surfmesh_t *mesh;
        rmeshstate_t m;
        int lightmaptexturenum;
-       float cl;
        memset(&m, 0, sizeof(m));
        m.blendfunc1 = GL_ONE;
        m.blendfunc2 = GL_ZERO;
-       m.wantoverbright = true;
        m.tex[0] = R_GetTexture(texture->texture);
        m.tex[1] = R_GetTexture(texture->surfacechain->lightmaptexture);
+       if (gl_combine.integer)
+               m.texrgbscale[1] = 4;
        R_Mesh_State(&m);
+       GL_Color(r_colorscale, r_colorscale, r_colorscale, 1);
        for (surf = texture->surfacechain;surf;surf = surf->texturechain)
        {
                lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
@@ -1112,8 +1093,6 @@ static void RSurfShader_OpaqueWall_Pass_BaseDoubleTex(const entity_render_t *ent
                        memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
                        memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
                        memcpy(varray_texcoord[1], mesh->uv, mesh->numverts * sizeof(float[2]));
-                       cl = (float) (1 << lightscalebit) * mesh_colorscale;
-                       R_FillColors(varray_color, mesh->numverts, cl, cl, cl, 1);
                        R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
                }
        }
@@ -1127,9 +1106,9 @@ static void RSurfShader_OpaqueWall_Pass_BaseTexture(const entity_render_t *ent,
        memset(&m, 0, sizeof(m));
        m.blendfunc1 = GL_ONE;
        m.blendfunc2 = GL_ZERO;
-       //m.wantoverbright = false;
        m.tex[0] = R_GetTexture(texture->texture);
        R_Mesh_State(&m);
+       GL_Color(1, 1, 1, 1);
        for (surf = texture->surfacechain;surf;surf = surf->texturechain)
        {
                for (mesh = surf->mesh;mesh;mesh = mesh->chain)
@@ -1137,7 +1116,6 @@ static void RSurfShader_OpaqueWall_Pass_BaseTexture(const entity_render_t *ent,
                        R_Mesh_ResizeCheck(mesh->numverts);
                        memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
                        memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
-                       R_FillColors(varray_color, mesh->numverts, 1, 1, 1, 1);
                        R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
                }
        }
@@ -1149,13 +1127,14 @@ static void RSurfShader_OpaqueWall_Pass_BaseLightmap(const entity_render_t *ent,
        const surfmesh_t *mesh;
        rmeshstate_t m;
        int lightmaptexturenum;
-       float cl;
        memset(&m, 0, sizeof(m));
        m.blendfunc1 = GL_ZERO;
        m.blendfunc2 = GL_SRC_COLOR;
-       m.wantoverbright = true;
        m.tex[0] = R_GetTexture(texture->surfacechain->lightmaptexture);
+       if (gl_combine.integer)
+               m.texrgbscale[0] = 4;
        R_Mesh_State(&m);
+       GL_Color(r_colorscale, r_colorscale, r_colorscale, 1);
        for (surf = texture->surfacechain;surf;surf = surf->texturechain)
        {
                lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
@@ -1169,8 +1148,6 @@ static void RSurfShader_OpaqueWall_Pass_BaseLightmap(const entity_render_t *ent,
                        R_Mesh_ResizeCheck(mesh->numverts);
                        memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
                        memcpy(varray_texcoord[0], mesh->uv, mesh->numverts * sizeof(float[2]));
-                       cl = (float) (1 << lightscalebit) * mesh_colorscale;
-                       R_FillColors(varray_color, mesh->numverts, cl, cl, cl, 1);
                        R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
                }
        }
@@ -1180,14 +1157,21 @@ static void RSurfShader_OpaqueWall_Pass_Light(const entity_render_t *ent, const
 {
        const msurface_t *surf;
        const surfmesh_t *mesh;
+       float colorscale;
        rmeshstate_t m;
 
        memset(&m, 0, sizeof(m));
        m.blendfunc1 = GL_SRC_ALPHA;
        m.blendfunc2 = GL_ONE;
-       m.wantoverbright = true;
        m.tex[0] = R_GetTexture(texture->texture);
+       colorscale = r_colorscale;
+       if (gl_combine.integer)
+       {
+               m.texrgbscale[0] = 4;
+               colorscale *= 0.25f;
+       }
        R_Mesh_State(&m);
+       GL_UseColorArray();
        for (surf = texture->surfacechain;surf;surf = surf->texturechain)
        {
                if (surf->dlightframe == r_framecount)
@@ -1201,7 +1185,7 @@ static void RSurfShader_OpaqueWall_Pass_Light(const entity_render_t *ent, const
                                        memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
                                        R_FillColors(varray_color, mesh->numverts, 0, 0, 0, 1);
                                        RSurf_LightSeparate(&ent->inversematrix, surf->dlightbits, mesh->numverts, varray_vertex, varray_color);
-                                       RSurf_ScaleColors(varray_color, mesh_colorscale, mesh->numverts);
+                                       RSurf_ScaleColors(varray_color, colorscale, mesh->numverts);
                                        R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
                                }
                        }
@@ -1219,9 +1203,8 @@ static void RSurfShader_OpaqueWall_Pass_Fog(const entity_render_t *ent, const te
        memset(&m, 0, sizeof(m));
        m.blendfunc1 = GL_SRC_ALPHA;
        m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
-       //m.wantoverbright = false;
-       //m.tex[0] = 0;
        R_Mesh_State(&m);
+       GL_UseColorArray();
        for (surf = texture->surfacechain;surf;surf = surf->texturechain)
        {
                for (mesh = surf->mesh;mesh;mesh = mesh->chain)
@@ -1230,7 +1213,7 @@ static void RSurfShader_OpaqueWall_Pass_Fog(const entity_render_t *ent, const te
                        memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
                        if (m.tex[0])
                                memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
-                       RSurf_FogPassColors(varray_vertex, varray_color, fogcolor[0], fogcolor[1], fogcolor[2], 1, mesh_colorscale, mesh->numverts, modelorg);
+                       RSurf_FogPassColors(varray_vertex, varray_color, fogcolor[0], fogcolor[1], fogcolor[2], 1, r_colorscale, mesh->numverts, modelorg);
                        R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
                }
        }
@@ -1244,9 +1227,9 @@ static void RSurfShader_OpaqueWall_Pass_BaseDetail(const entity_render_t *ent, c
        memset(&m, 0, sizeof(m));
        m.blendfunc1 = GL_DST_COLOR;
        m.blendfunc2 = GL_SRC_COLOR;
-       //m.wantoverbright = false;
        m.tex[0] = R_GetTexture(texture->detailtexture);
        R_Mesh_State(&m);
+       GL_Color(1, 1, 1, 1);
        for (surf = texture->surfacechain;surf;surf = surf->texturechain)
        {
                for (mesh = surf->mesh;mesh;mesh = mesh->chain)
@@ -1254,7 +1237,6 @@ static void RSurfShader_OpaqueWall_Pass_BaseDetail(const entity_render_t *ent, c
                        R_Mesh_ResizeCheck(mesh->numverts);
                        memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
                        memcpy(varray_texcoord[0], mesh->ab, mesh->numverts * sizeof(float[2]));
-                       R_FillColors(varray_color, mesh->numverts, 1, 1, 1, 1);
                        R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
                }
        }
@@ -1268,9 +1250,9 @@ static void RSurfShader_OpaqueWall_Pass_Glow(const entity_render_t *ent, const t
        memset(&m, 0, sizeof(m));
        m.blendfunc1 = GL_SRC_ALPHA;
        m.blendfunc2 = GL_ONE;
-       //m.wantoverbright = false;
        m.tex[0] = R_GetTexture(texture->glowtexture);
        R_Mesh_State(&m);
+       GL_Color(r_colorscale, r_colorscale, r_colorscale, 1);
        for (surf = texture->surfacechain;surf;surf = surf->texturechain)
        {
                for (mesh = surf->mesh;mesh;mesh = mesh->chain)
@@ -1278,7 +1260,6 @@ static void RSurfShader_OpaqueWall_Pass_Glow(const entity_render_t *ent, const t
                        R_Mesh_ResizeCheck(mesh->numverts);
                        memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
                        memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
-                       R_FillColors(varray_color, mesh->numverts, mesh_colorscale, mesh_colorscale, mesh_colorscale, 1);
                        R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
                }
        }
@@ -1470,7 +1451,7 @@ void R_DrawSurfaces(entity_render_t *ent, int sky, int normal)
                                {
                                        if (surf->cached_dlight
                                        || surf->cached_ambient != r_ambient.value
-                                       || surf->cached_lightscalebit != lightscalebit)
+                                       || surf->cached_lightmapscalebit != r_lightmapscalebit)
                                                R_BuildLightMap(ent, surf, false); // base lighting changed
                                        else if (r_dynamic.integer)
                                        {
@@ -1508,16 +1489,14 @@ static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
        memset(&m, 0, sizeof(m));
        m.blendfunc1 = GL_SRC_ALPHA;
        m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
-       //m.wantoverbright = false;
        R_Mesh_Matrix(&ent->matrix);
        R_Mesh_State(&m);
        R_Mesh_ResizeCheck(portal->numpoints);
        i = portal - ent->model->portals;
-       R_FillColors(varray_color, portal->numpoints,
-               ((i & 0x0007) >> 0) * (1.0f / 7.0f) * mesh_colorscale,
-               ((i & 0x0038) >> 3) * (1.0f / 7.0f) * mesh_colorscale,
-               ((i & 0x01C0) >> 6) * (1.0f / 7.0f) * mesh_colorscale,
-               0.125f);
+       GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f) * r_colorscale,
+                        ((i & 0x0038) >> 3) * (1.0f / 7.0f) * r_colorscale,
+                        ((i & 0x01C0) >> 6) * (1.0f / 7.0f) * r_colorscale,
+                        0.125f);
        if (PlaneDiff(r_origin, (&portal->plane)) > 0)
        {
                for (i = portal->numpoints - 1, v = varray_vertex;i >= 0;i--, v += 4)
@@ -1851,6 +1830,87 @@ void R_DrawBrushModelNormal (entity_render_t *ent)
        R_DrawBrushModel(ent, false, true);
 }
 
+void R_DrawBrushModelShadowVolumes (entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int visiblevolume)
+{
+       int i, numsurfaces;
+       msurface_t *surf;
+       float projectdistance, f, temp[3], lightradius2;
+       surfmesh_t *mesh;
+       numsurfaces = ent->model->nummodelsurfaces;
+       lightradius2 = lightradius * lightradius;
+       for (i = 0, surf = ent->model->surfaces + ent->model->firstmodelsurface;i < numsurfaces;i++, surf++)
+       {
+               VectorSubtract(relativelightorigin, surf->poly_center, temp);
+               if (DotProduct(temp, temp) < (surf->poly_radius2 + lightradius2))
+               {
+                       f = PlaneDiff(relativelightorigin, surf->plane);
+                       if (surf->flags & SURF_PLANEBACK)
+                               f = -f;
+                       // draw shadows only for backfaces
+                       if (f < 0)
+                       {
+                               projectdistance = lightradius + f;
+                               if (projectdistance > 0)
+                               {
+                                       for (mesh = surf->mesh;mesh;mesh = mesh->chain)
+                                       {
+                                               R_Mesh_ResizeCheck(mesh->numverts * 2);
+                                               memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
+                                               R_Shadow_Volume(mesh->numverts, mesh->numtriangles, mesh->index, mesh->triangleneighbors, relativelightorigin, projectdistance, visiblevolume);
+                                       }
+                               }
+                       }
+               }
+       }
+}
+
+extern cvar_t r_shadows;
+void R_DrawBrushModelFakeShadow (entity_render_t *ent)
+{
+       int i;
+       vec3_t relativelightorigin;
+       rmeshstate_t m;
+       mlight_t *sl;
+       rdlight_t *rd;
+
+       if (r_shadows.integer < 2)
+               return;
+
+       memset(&m, 0, sizeof(m));
+       m.blendfunc1 = GL_ONE;
+       m.blendfunc2 = GL_ONE;
+       R_Mesh_State(&m);
+       R_Mesh_Matrix(&ent->matrix);
+       GL_Color(0.0125 * r_colorscale, 0.025 * r_colorscale, 0.1 * r_colorscale, 1);
+       for (i = 0, sl = cl.worldmodel->lights;i < cl.worldmodel->numlights;i++, sl++)
+       {
+               if (d_lightstylevalue[sl->style] > 0
+                && ent->maxs[0] >= sl->origin[0] - sl->cullradius
+                && ent->mins[0] <= sl->origin[0] + sl->cullradius
+                && ent->maxs[1] >= sl->origin[1] - sl->cullradius
+                && ent->mins[1] <= sl->origin[1] + sl->cullradius
+                && ent->maxs[2] >= sl->origin[2] - sl->cullradius
+                && ent->mins[2] <= sl->origin[2] + sl->cullradius)
+               {
+                       Matrix4x4_Transform(&ent->inversematrix, sl->origin, relativelightorigin);
+                       R_DrawBrushModelShadowVolumes (ent, relativelightorigin, sl->cullradius, true);
+               }
+       }
+       for (i = 0, rd = r_dlight;i < r_numdlights;i++, rd++)
+       {
+               if (ent->maxs[0] >= rd->origin[0] - rd->cullradius
+                && ent->mins[0] <= rd->origin[0] + rd->cullradius
+                && ent->maxs[1] >= rd->origin[1] - rd->cullradius
+                && ent->mins[1] <= rd->origin[1] + rd->cullradius
+                && ent->maxs[2] >= rd->origin[2] - rd->cullradius
+                && ent->mins[2] <= rd->origin[2] + rd->cullradius)
+               {
+                       Matrix4x4_Transform(&ent->inversematrix, rd->origin, relativelightorigin);
+                       R_DrawBrushModelShadowVolumes (ent, relativelightorigin, rd->cullradius, true);
+               }
+       }
+}
+
 static void gl_surf_start(void)
 {
 }