]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_rmain.c
made darkplaces compile successfully with g++ to test for errors C doesn't care about...
[xonotic/darkplaces.git] / gl_rmain.c
index ec27666473e46b340e91993ef3c470abdd3f4ac7..8525e1d2bfdda26afe90c6d98a04aecc3384b7cf 100644 (file)
@@ -297,6 +297,7 @@ static void R_BuildNormalizationCube(void)
                                t = (y + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
                                switch(side)
                                {
+                               default:
                                case 0:
                                        v[0] = 1;
                                        v[1] = -t;
@@ -379,7 +380,7 @@ void gl_main_newmap(void)
                if (l >= 0 && !strcmp(entname + l, ".bsp"))
                {
                        strcpy(entname + l, ".ent");
-                       if ((entities = FS_LoadFile(entname, tempmempool, true)))
+                       if ((entities = (char *)FS_LoadFile(entname, tempmempool, true)))
                        {
                                CL_ParseEntityLump(entities);
                                Mem_Free(entities);
@@ -1155,7 +1156,7 @@ float nomodelcolor4f[6*4] =
 
 void R_DrawNoModelCallback(const void *calldata1, int calldata2)
 {
-       const entity_render_t *ent = calldata1;
+       const entity_render_t *ent = (entity_render_t *)calldata1;
        int i;
        float f1, f2, *c, diff[3];
        float color4f[6*4];
@@ -1354,7 +1355,7 @@ void R_Mesh_AddBrushMeshFromPlanes(rmesh_t *mesh, int numplanes, mplane_t *plane
        }
 }
 
-void R_Texture_AddLayer(texture_t *t, qboolean depthmask, int blendfunc1, int blendfunc2, texturelayertype_t type, rtexture_t *texture, matrix4x4_t *matrix, float r, float g, float b, float a, int texrgbscale)
+void R_Texture_AddLayer(texture_t *t, qboolean depthmask, int blendfunc1, int blendfunc2, texturelayertype_t type, rtexture_t *texture, matrix4x4_t *matrix, float r, float g, float b, float a)
 {
        texturelayer_t *layer;
        layer = t->currentlayers + t->currentnumlayers++;
@@ -1368,13 +1369,13 @@ void R_Texture_AddLayer(texture_t *t, qboolean depthmask, int blendfunc1, int bl
        layer->color[1] = g;
        layer->color[2] = b;
        layer->color[3] = a;
-       layer->texrgbscale = texrgbscale;
 }
 
 void R_UpdateTextureInfo(const entity_render_t *ent, texture_t *t)
 {
        // FIXME: identify models using a better check than ent->model->brush.shadowmesh
        //int lightmode = ((ent->effects & EF_FULLBRIGHT) || ent->model->brush.shadowmesh) ? 0 : 2;
+       float currentalpha;
 
        {
                texture_t *texture = t;
@@ -1399,17 +1400,14 @@ void R_UpdateTextureInfo(const entity_render_t *ent, texture_t *t)
        }
 
        t->currentmaterialflags = t->basematerialflags;
-       t->currentcolorbase[0] = ent->colormod[0];
-       t->currentcolorbase[1] = ent->colormod[1];
-       t->currentcolorbase[2] = ent->colormod[2];
-       t->currentcolorbase[3] = ent->alpha;
+       currentalpha = ent->alpha;
        if (t->basematerialflags & MATERIALFLAG_WATERALPHA)
-               t->currentcolorbase[3] *= r_wateralpha.value;
+               currentalpha *= r_wateralpha.value;
        if (!(ent->flags & RENDER_LIGHT))
                t->currentmaterialflags |= MATERIALFLAG_FULLBRIGHT;
        if (ent->effects & EF_ADDITIVE)
                t->currentmaterialflags |= MATERIALFLAG_ADD | MATERIALFLAG_TRANSPARENT;
-       else if (t->currentcolorbase[3] < 1)
+       else if (currentalpha < 1)
                t->currentmaterialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_TRANSPARENT;
        if (ent->effects & EF_NODEPTHTEST)
                t->currentmaterialflags |= MATERIALFLAG_NODEPTHTEST;
@@ -1417,224 +1415,103 @@ void R_UpdateTextureInfo(const entity_render_t *ent, texture_t *t)
                t->currenttexmatrix = r_waterscrollmatrix;
        else
                t->currenttexmatrix = r_identitymatrix;
-       if (ent->colormap >= 0)
-       {
-               int b;
-               qbyte *bcolor;
-               t->currentbasetexture = t->skin.base;
-               t->currentdopants = t->skin.pants != NULL;
-               t->currentdoshirt = t->skin.shirt != NULL;
-               // 128-224 are backwards ranges
-               b = (ent->colormap & 0xF) << 4;b += (b >= 128 && b < 224) ? 4 : 12;
-               t->currentdofullbrightpants = (b >= 224) || (t->currentmaterialflags & MATERIALFLAG_FULLBRIGHT);
-               bcolor = (qbyte *) (&palette_complete[b]);
-               t->currentcolorpants[0] = bcolor[0] * (1.0f / 255.0f) * t->currentcolorbase[0];
-               t->currentcolorpants[1] = bcolor[1] * (1.0f / 255.0f) * t->currentcolorbase[1];
-               t->currentcolorpants[2] = bcolor[2] * (1.0f / 255.0f) * t->currentcolorbase[2];
-               t->currentcolorpants[3] = t->currentcolorbase[3];
-               // 128-224 are backwards ranges
-               b = (ent->colormap & 0xF0);b += (b >= 128 && b < 224) ? 4 : 12;
-               t->currentdofullbrightshirt = (b >= 224) || (t->currentmaterialflags & MATERIALFLAG_FULLBRIGHT);
-               bcolor = (qbyte *) (&palette_complete[b]);
-               t->currentcolorshirt[0] = bcolor[0] * (1.0f / 255.0f) * t->currentcolorbase[0];
-               t->currentcolorshirt[1] = bcolor[1] * (1.0f / 255.0f) * t->currentcolorbase[1];
-               t->currentcolorshirt[2] = bcolor[2] * (1.0f / 255.0f) * t->currentcolorbase[2];
-               t->currentcolorshirt[3] = t->currentcolorbase[3];
-       }
-       else
-       {
-               t->currentbasetexture = t->skin.merged ? t->skin.merged : t->skin.base;
-               t->currentdopants = false;
-               t->currentdoshirt = false;
-               t->currentdofullbrightpants = false;
-               t->currentdofullbrightshirt = false;
-       }
-       t->currentfogallpasses = fogenabled && (t->currentmaterialflags & MATERIALFLAG_TRANSPARENT);
        t->currentnumlayers = 0;
        if (!(t->currentmaterialflags & MATERIALFLAG_NODRAW))
        {
-               int blendfunc1, blendfunc2, depthmask;
-               if (t->currentmaterialflags & MATERIALFLAG_ADD)
-               {
-                       blendfunc1 = GL_SRC_ALPHA;
-                       blendfunc2 = GL_ONE;
-                       depthmask = false;
-               }
-               else if (t->currentmaterialflags & MATERIALFLAG_ALPHA)
-               {
-                       blendfunc1 = GL_SRC_ALPHA;
-                       blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
-                       depthmask = false;
-               }
-               else
-               {
-                       blendfunc1 = GL_ONE;
-                       blendfunc2 = GL_ZERO;
-                       depthmask = true;
-               }
                if (gl_lightmaps.integer)
-                       R_Texture_AddLayer(t, depthmask, blendfunc1, blendfunc2, TEXTURELAYERTYPE_LIGHTMAP, r_texture_white, &r_identitymatrix, 1, 1, 1, 1, 1);
+                       R_Texture_AddLayer(t, true, GL_ONE, GL_ZERO, TEXTURELAYERTYPE_LITTEXTURE_MULTIPASS, r_texture_white, &r_identitymatrix, 1, 1, 1, 1);
                else if (t->currentmaterialflags & MATERIALFLAG_SKY)
                {
                        // transparent sky would be ridiculous
                        if (!(t->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
-                               R_Texture_AddLayer(t, depthmask, blendfunc1, blendfunc2, TEXTURELAYERTYPE_SKY, r_texture_white, &r_identitymatrix, fogcolor[0], fogcolor[1], fogcolor[2], 1, 1);
+                               R_Texture_AddLayer(t, true, GL_ONE, GL_ZERO, TEXTURELAYERTYPE_SKY, r_texture_white, &r_identitymatrix, fogcolor[0], fogcolor[1], fogcolor[2], 1);
                }
-               else if (t->currentmaterialflags & (MATERIALFLAG_WATER | MATERIALFLAG_WALL))
+               else
                {
-                       int type;
-                       float colorscale, r, g, b;
-                       rtexture_t *currentbasetexture;
-                       colorscale = 2;
-                       // q3bsp has no lightmap updates, so the lightstylevalue that
-                       // would normally be baked into the lightmaptexture must be
-                       // applied to the color
-                       if (!(t->currentmaterialflags & MATERIALFLAG_FULLBRIGHT) && ent->model->type == mod_brushq3)
-                               colorscale *= d_lightstylevalue[0] * (1.0f / 128.0f);
-                       r = t->currentcolorbase[0] * colorscale;
-                       g = t->currentcolorbase[1] * colorscale;
-                       b = t->currentcolorbase[2] * colorscale;
-                       // transparent and fullbright are not affected by r_lightmapintensity
-                       if (!(t->currentmaterialflags & (MATERIALFLAG_TRANSPARENT | MATERIALFLAG_FULLBRIGHT)))
+                       int blendfunc1, blendfunc2, depthmask;
+                       if (t->currentmaterialflags & MATERIALFLAG_ADD)
                        {
-                               r *= r_lightmapintensity;
-                               g *= r_lightmapintensity;
-                               b *= r_lightmapintensity;
+                               blendfunc1 = GL_SRC_ALPHA;
+                               blendfunc2 = GL_ONE;
+                               depthmask = false;
                        }
-                       currentbasetexture = (ent->colormap >= 0 && t->skin.merged) ? t->skin.merged : t->skin.base;
-                       if (!(t->currentmaterialflags & MATERIALFLAG_FULLBRIGHT) && r_textureunits.integer >= 2 && gl_combine.integer)
+                       else if (t->currentmaterialflags & MATERIALFLAG_ALPHA)
                        {
-                               if ((r > 2 || g > 2 || b > 2) && gl_combine.integer)
-                                       R_Texture_AddLayer(t, depthmask, blendfunc1, blendfunc2, TEXTURELAYERTYPE_LIGHTMAP_DOUBLEMODULATE_TEXTURE, currentbasetexture, &t->currenttexmatrix, r * 0.25f, g * 0.25f, b * 0.25f, t->currentcolorbase[3], 4);
-                               else if ((r > 1 || g > 1 || b > 1) && gl_combine.integer)
-                                       R_Texture_AddLayer(t, depthmask, blendfunc1, blendfunc2, TEXTURELAYERTYPE_LIGHTMAP_DOUBLEMODULATE_TEXTURE, currentbasetexture, &t->currenttexmatrix, r * 0.5f, g * 0.5f, b * 0.5f, t->currentcolorbase[3], 2);
-                               else
-                                       R_Texture_AddLayer(t, depthmask, blendfunc1, blendfunc2, TEXTURELAYERTYPE_LIGHTMAP_DOUBLEMODULATE_TEXTURE, currentbasetexture, &t->currenttexmatrix, r, g, b, t->currentcolorbase[3], 1);
-                       }
-                       else if ((t->currentmaterialflags & (MATERIALFLAG_FULLBRIGHT | MATERIALFLAG_TRANSPARENT)) == 0)
-                       {
-                               R_Texture_AddLayer(t, true, GL_ONE, GL_ZERO, TEXTURELAYERTYPE_LIGHTMAP, r_texture_white, &r_identitymatrix, 1, 1, 1, 1, 1);
-                               if (r > 1 || g > 1 || b > 1)
-                                       R_Texture_AddLayer(t, false, GL_DST_COLOR, GL_SRC_COLOR, TEXTURELAYERTYPE_TEXTURE, currentbasetexture, &t->currenttexmatrix, r * 0.5f, g * 0.5f, b * 0.5f, t->currentcolorbase[3], 1);
-                               else
-                                       R_Texture_AddLayer(t, false, GL_ZERO, GL_SRC_COLOR, TEXTURELAYERTYPE_TEXTURE, currentbasetexture, &t->currenttexmatrix, r, g, b, t->currentcolorbase[3], 1);
+                               blendfunc1 = GL_SRC_ALPHA;
+                               blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
+                               depthmask = false;
                        }
                        else
                        {
-                               if (t->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)
-                               {
-                                       type = TEXTURELAYERTYPE_TEXTURE;
-                                       r *= 0.5f;
-                                       g *= 0.5f;
-                                       b *= 0.5f;
-                               }
-                               else
-                                       type = TEXTURELAYERTYPE_VERTEXTEXTURE;
-                               if ((r > 2 || g > 2 || b > 2) && gl_combine.integer)
-                                       R_Texture_AddLayer(t, depthmask, blendfunc1, blendfunc2, type, currentbasetexture, &t->currenttexmatrix, r * 0.25f, g * 0.25f, b * 0.25f, t->currentcolorbase[3], 4);
-                               else if ((r > 1 || g > 1 || b > 1) && gl_combine.integer)
-                                       R_Texture_AddLayer(t, depthmask, blendfunc1, blendfunc2, type, currentbasetexture, &t->currenttexmatrix, r * 0.5f, g * 0.5f, b * 0.5f, t->currentcolorbase[3], 2);
-                               else
-                                       R_Texture_AddLayer(t, depthmask, blendfunc1, blendfunc2, type, currentbasetexture, &t->currenttexmatrix, r, g, b, t->currentcolorbase[3], 1);
+                               blendfunc1 = GL_ONE;
+                               blendfunc2 = GL_ZERO;
+                               depthmask = true;
                        }
-                       if (ent->colormap >= 0 && t->skin.pants)
+                       if (t->currentmaterialflags & (MATERIALFLAG_WATER | MATERIALFLAG_WALL))
                        {
-                               int cb;
-                               qbyte *cbcolor;
-                               // 128-224 are backwards ranges
-                               cb = (ent->colormap & 0xF) << 4;cb += (cb >= 128 && cb < 224) ? 4 : 12;
-                               cbcolor = (qbyte *) (&palette_complete[cb]);
-                               r = cbcolor[0] * (1.0f / 255.0f) * t->currentcolorbase[0] * colorscale;
-                               g = cbcolor[1] * (1.0f / 255.0f) * t->currentcolorbase[1] * colorscale;
-                               b = cbcolor[2] * (1.0f / 255.0f) * t->currentcolorbase[2] * colorscale;
-                               // transparent and fullbright are not affected by r_lightmapintensity
-                               if ((cb >= 224) || (t->currentmaterialflags & MATERIALFLAG_FULLBRIGHT))
+                               rtexture_t *currentbasetexture;
+                               int layerflags = 0;
+                               if (fogenabled && (t->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
+                                       layerflags |= TEXTURELAYERFLAG_FOGDARKEN;
+                               currentbasetexture = (ent->colormap < 0 && t->skin.merged) ? t->skin.merged : t->skin.base;
+                               if (t->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)
                                {
-                                       type = TEXTURELAYERTYPE_TEXTURE;
-                                       r *= 0.5f;
-                                       g *= 0.5f;
-                                       b *= 0.5f;
+                                       R_Texture_AddLayer(t, depthmask, blendfunc1, blendfunc2, TEXTURELAYERTYPE_TEXTURE, currentbasetexture, &t->currenttexmatrix, ent->colormod[0], ent->colormod[1], ent->colormod[2], currentalpha);
+                                       if (ent->colormap >= 0 && t->skin.pants)
+                                               R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->skin.pants, &t->currenttexmatrix, ent->colormap_pantscolor[0], ent->colormap_pantscolor[1], ent->colormap_pantscolor[2], currentalpha);
+                                       if (ent->colormap >= 0 && t->skin.shirt)
+                                               R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->skin.shirt, &t->currenttexmatrix, ent->colormap_shirtcolor[0], ent->colormap_shirtcolor[1], ent->colormap_shirtcolor[2], currentalpha);
                                }
                                else
                                {
-                                       type = TEXTURELAYERTYPE_VERTEXTEXTURE;
-                                       if (!(t->currentmaterialflags & (MATERIALFLAG_TRANSPARENT | MATERIALFLAG_FULLBRIGHT)))
+                                       float colorscale;
+                                       colorscale = 2;
+                                       // q3bsp has no lightmap updates, so the lightstylevalue that
+                                       // would normally be baked into the lightmaptexture must be
+                                       // applied to the color
+                                       if (ent->model->type == mod_brushq3)
+                                               colorscale *= d_lightstylevalue[0] * (1.0f / 256.0f);
+                                       // transparent and fullbright are not affected by r_lightmapintensity
+                                       if (!(t->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
+                                               colorscale *= r_lightmapintensity;
+                                       if (r_textureunits.integer >= 2 && gl_combine.integer)
+                                               R_Texture_AddLayer(t, depthmask, blendfunc1, blendfunc2, TEXTURELAYERTYPE_LITTEXTURE_COMBINE, currentbasetexture, &t->currenttexmatrix, ent->colormod[0] * colorscale, ent->colormod[1] * colorscale, ent->colormod[2] * colorscale, currentalpha);
+                                       else if ((t->currentmaterialflags & MATERIALFLAG_TRANSPARENT) == 0)
+                                               R_Texture_AddLayer(t, true, GL_ONE, GL_ZERO, TEXTURELAYERTYPE_LITTEXTURE_MULTIPASS, currentbasetexture, &t->currenttexmatrix, ent->colormod[0] * colorscale * 0.5f, ent->colormod[1] * colorscale * 0.5f, ent->colormod[2] * colorscale * 0.5f, currentalpha);
+                                       else
+                                               R_Texture_AddLayer(t, depthmask, blendfunc1, blendfunc2, TEXTURELAYERTYPE_LITTEXTURE_VERTEX, currentbasetexture, &t->currenttexmatrix, ent->colormod[0] * colorscale, ent->colormod[1] * colorscale, ent->colormod[2] * colorscale, currentalpha);
+                                       if (r_ambient.value >= (1.0f/64.0f))
+                                               R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, currentbasetexture, &t->currenttexmatrix, ent->colormod[0] * r_ambient.value * (1.0f / 64.0f), ent->colormod[1] * r_ambient.value * (1.0f / 64.0f), ent->colormod[2] * r_ambient.value * (1.0f / 64.0f), currentalpha);
+                                       if (ent->colormap >= 0 && t->skin.pants)
                                        {
-                                               r *= r_lightmapintensity;
-                                               g *= r_lightmapintensity;
-                                               b *= r_lightmapintensity;
+                                               R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_LITTEXTURE_VERTEX, t->skin.pants, &t->currenttexmatrix, ent->colormap_pantscolor[0] * colorscale, ent->colormap_pantscolor[1] * colorscale, ent->colormap_pantscolor[2] * colorscale, currentalpha);
+                                               if (r_ambient.value >= (1.0f/64.0f))
+                                                       R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->skin.pants, &t->currenttexmatrix, ent->colormap_pantscolor[0] * r_ambient.value * (1.0f / 64.0f), ent->colormap_pantscolor[1] * r_ambient.value * (1.0f / 64.0f), ent->colormap_pantscolor[2] * r_ambient.value * (1.0f / 64.0f), currentalpha);
                                        }
-                               }
-                               if ((r > 2 || g > 2 || b > 2) && gl_combine.integer)
-                                       R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, type, t->skin.pants, &t->currenttexmatrix, r * 0.25f, g * 0.25f, b * 0.25f, t->currentcolorbase[3], 4);
-                               else if ((r > 1 || g > 1 || b > 1) && gl_combine.integer)
-                                       R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, type, t->skin.pants, &t->currenttexmatrix, r * 0.5f, g * 0.5f, b * 0.5f, t->currentcolorbase[3], 2);
-                               else
-                                       R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, type, t->skin.pants, &t->currenttexmatrix, r, g, b, t->currentcolorbase[3], 1);
-                       }
-                       if (ent->colormap >= 0 && t->skin.shirt)
-                       {
-                               int cb;
-                               qbyte *cbcolor;
-                               // 128-224 are backwards ranges
-                               cb = (ent->colormap & 0xF0);cb += (cb >= 128 && cb < 224) ? 4 : 12;
-                               cbcolor = (qbyte *) (&palette_complete[cb]);
-                               r = cbcolor[0] * (1.0f / 255.0f) * t->currentcolorbase[0] * colorscale;
-                               g = cbcolor[1] * (1.0f / 255.0f) * t->currentcolorbase[1] * colorscale;
-                               b = cbcolor[2] * (1.0f / 255.0f) * t->currentcolorbase[2] * colorscale;
-                               // transparent and fullbright are not affected by r_lightmapintensity
-                               if ((cb >= 224) || (t->currentmaterialflags & MATERIALFLAG_FULLBRIGHT))
-                               {
-                                       type = TEXTURELAYERTYPE_TEXTURE;
-                                       r *= 0.5f;
-                                       g *= 0.5f;
-                                       b *= 0.5f;
-                               }
-                               else
-                               {
-                                       type = TEXTURELAYERTYPE_VERTEXTEXTURE;
-                                       if (!(t->currentmaterialflags & (MATERIALFLAG_TRANSPARENT | MATERIALFLAG_FULLBRIGHT)))
+                                       if (ent->colormap >= 0 && t->skin.shirt)
                                        {
-                                               r *= r_lightmapintensity;
-                                               g *= r_lightmapintensity;
-                                               b *= r_lightmapintensity;
+                                               R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_LITTEXTURE_VERTEX, t->skin.shirt, &t->currenttexmatrix, ent->colormap_shirtcolor[0] * colorscale, ent->colormap_shirtcolor[1] * colorscale, ent->colormap_shirtcolor[2] * colorscale, currentalpha);
+                                               if (r_ambient.value >= (1.0f/64.0f))
+                                                       R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->skin.shirt, &t->currenttexmatrix, ent->colormap_shirtcolor[0] * r_ambient.value * (1.0f / 64.0f), ent->colormap_shirtcolor[1] * r_ambient.value * (1.0f / 64.0f), ent->colormap_shirtcolor[2] * r_ambient.value * (1.0f / 64.0f), currentalpha);
                                        }
                                }
-                               if ((r > 2 || g > 2 || b > 2) && gl_combine.integer)
-                                       R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, type, t->skin.shirt, &t->currenttexmatrix, r * 0.25f, g * 0.25f, b * 0.25f, t->currentcolorbase[3], 4);
-                               else if ((r > 1 || g > 1 || b > 1) && gl_combine.integer)
-                                       R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, type, t->skin.shirt, &t->currenttexmatrix, r * 0.5f, g * 0.5f, b * 0.5f, t->currentcolorbase[3], 2);
-                               else
-                                       R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, type, t->skin.shirt, &t->currenttexmatrix, r, g, b, t->currentcolorbase[3], 1);
-                       }
-                       if (r_ambient.value >= (1.0f/64.0f))
-                       {
-                               r = t->currentcolorbase[0] * colorscale * r_ambient.value * (1.0f / 64.0f);
-                               g = t->currentcolorbase[1] * colorscale * r_ambient.value * (1.0f / 64.0f);
-                               b = t->currentcolorbase[2] * colorscale * r_ambient.value * (1.0f / 64.0f);
-                               if ((r > 2 || g > 2 || b > 2) && gl_combine.integer)
-                                       R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, currentbasetexture, &t->currenttexmatrix, r * 0.25f, g * 0.25f, b * 0.25f, t->currentcolorbase[3], 4);
-                               else if ((r > 1 || g > 1 || b > 1) && gl_combine.integer)
-                                       R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, currentbasetexture, &t->currenttexmatrix, r * 0.5f, g * 0.5f, b * 0.5f, t->currentcolorbase[3], 2);
-                               else
-                                       R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, currentbasetexture, &t->currenttexmatrix, r, g, b, t->currentcolorbase[3], 1);
-                       }
-                       if (t->skin.glow != NULL)
-                               R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->skin.glow, &t->currenttexmatrix, 1, 1, 1, t->currentcolorbase[3], 1);
-                       if (fogenabled && !(t->currentmaterialflags & MATERIALFLAG_ADD))
-                       {
-                               // if this is opaque use alpha blend which will darken the earlier
-                               // passes cheaply.
-                               //
-                               // if this is an alpha blended material, all the earlier passes
-                               // were darkened by fog already, so we only need to add the fog
-                               // color ontop through the fog mask texture
-                               //
-                               // if this is an additive blended material, all the earlier passes
-                               // were darkened by fog already, and we should not add fog color
-                               // (because the background was not darkened, there is no fog color
-                               // that was lost behind it).
-                               R_Texture_AddLayer(t, false, GL_SRC_ALPHA, t->currentfogallpasses ? GL_ONE : GL_ONE_MINUS_SRC_ALPHA, TEXTURELAYERTYPE_FOG, t->skin.fog, &r_identitymatrix, fogcolor[0], fogcolor[1], fogcolor[2], t->currentcolorbase[3], 1);
+                               if (t->skin.glow != NULL)
+                                       R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->skin.glow, &t->currenttexmatrix, 1, 1, 1, currentalpha);
+                               if (fogenabled && !(t->currentmaterialflags & MATERIALFLAG_ADD))
+                               {
+                                       // if this is opaque use alpha blend which will darken the earlier
+                                       // passes cheaply.
+                                       //
+                                       // if this is an alpha blended material, all the earlier passes
+                                       // were darkened by fog already, so we only need to add the fog
+                                       // color ontop through the fog mask texture
+                                       //
+                                       // if this is an additive blended material, all the earlier passes
+                                       // were darkened by fog already, and we should not add fog color
+                                       // (because the background was not darkened, there is no fog color
+                                       // that was lost behind it).
+                                       R_Texture_AddLayer(t, false, GL_SRC_ALPHA, (t->currentmaterialflags & MATERIALFLAG_TRANSPARENT) ? GL_ONE : GL_ONE_MINUS_SRC_ALPHA, TEXTURELAYERTYPE_FOG, t->skin.fog, &r_identitymatrix, fogcolor[0], fogcolor[1], fogcolor[2], currentalpha);
+                               }
                        }
                }
        }
@@ -1789,27 +1666,32 @@ void RSurf_SetColorPointer(const entity_render_t *ent, const msurface_t *surface
                        for (i = 0, c = varray_color4f + 4 * surface->num_firstvertex;i < surface->num_vertices;i++, c += 4)
                        {
                                const qbyte *lm = surface->lightmapinfo->samples + (surface->groupmesh->data_lightmapoffsets + surface->num_firstvertex)[i];
-                               float scale = d_lightstylevalue[surface->lightmapinfo->styles[0]] * (1.0f / 32768.0f);
-                               VectorScale(lm, scale, c);
-                               if (surface->lightmapinfo->styles[1] != 255)
+                               if (lm)
                                {
-                                       int size3 = ((surface->lightmapinfo->extents[0]>>4)+1)*((surface->lightmapinfo->extents[1]>>4)+1)*3;
-                                       lm += size3;
-                                       scale = d_lightstylevalue[surface->lightmapinfo->styles[1]] * (1.0f / 32768.0f);
-                                       VectorMA(c, scale, lm, c);
-                                       if (surface->lightmapinfo->styles[2] != 255)
+                                       float scale = d_lightstylevalue[surface->lightmapinfo->styles[0]] * (1.0f / 32768.0f);
+                                       VectorScale(lm, scale, c);
+                                       if (surface->lightmapinfo->styles[1] != 255)
                                        {
+                                               int size3 = ((surface->lightmapinfo->extents[0]>>4)+1)*((surface->lightmapinfo->extents[1]>>4)+1)*3;
                                                lm += size3;
-                                               scale = d_lightstylevalue[surface->lightmapinfo->styles[2]] * (1.0f / 32768.0f);
+                                               scale = d_lightstylevalue[surface->lightmapinfo->styles[1]] * (1.0f / 32768.0f);
                                                VectorMA(c, scale, lm, c);
-                                               if (surface->lightmapinfo->styles[3] != 255)
+                                               if (surface->lightmapinfo->styles[2] != 255)
                                                {
                                                        lm += size3;
-                                                       scale = d_lightstylevalue[surface->lightmapinfo->styles[3]] * (1.0f / 32768.0f);
+                                                       scale = d_lightstylevalue[surface->lightmapinfo->styles[2]] * (1.0f / 32768.0f);
                                                        VectorMA(c, scale, lm, c);
+                                                       if (surface->lightmapinfo->styles[3] != 255)
+                                                       {
+                                                               lm += size3;
+                                                               scale = d_lightstylevalue[surface->lightmapinfo->styles[3]] * (1.0f / 32768.0f);
+                                                               VectorMA(c, scale, lm, c);
+                                                       }
                                                }
                                        }
                                }
+                               else
+                                       VectorClear(c);
                        }
                        rsurface_lightmapcolor4f = varray_color4f;
                }
@@ -1864,13 +1746,8 @@ void RSurf_SetColorPointer(const entity_render_t *ent, const msurface_t *surface
 
 static void R_DrawTextureSurfaceList(const entity_render_t *ent, texture_t *texture, int texturenumsurfaces, const msurface_t **texturesurfacelist, const vec3_t modelorg)
 {
-       int i;
        int texturesurfaceindex;
        int lightmode;
-       const float *v;
-       float *c;
-       float diff[3];
-       float f, r, g, b, a, colorscale;
        const msurface_t *surface;
        qboolean applycolor;
        rmeshstate_t m;
@@ -1879,45 +1756,37 @@ static void R_DrawTextureSurfaceList(const entity_render_t *ent, texture_t *text
        c_faces += texturenumsurfaces;
        // FIXME: identify models using a better check than ent->model->brush.shadowmesh
        lightmode = ((ent->effects & EF_FULLBRIGHT) || ent->model->brush.shadowmesh) ? 0 : 2;
-       // gl_lightmaps debugging mode skips normal texturing
-       if (gl_lightmaps.integer)
-       {
-               GL_BlendFunc(GL_ONE, GL_ZERO);
-               GL_DepthMask(true);
-               GL_DepthTest(true);
-               qglDisable(GL_CULL_FACE);
-               GL_Color(1, 1, 1, 1);
-               memset(&m, 0, sizeof(m));
-               R_Mesh_State(&m);
-               for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
-               {
-                       surface = texturesurfacelist[texturesurfaceindex];
-                       R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
-                       R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordlightmap2f);
-                       RSurf_SetVertexPointer(ent, texture, surface, modelorg);
-                       RSurf_SetColorPointer(ent, surface, modelorg, 1, 1, 1, 1, lightmode ? lightmode : !surface->lightmaptexture, false, false);
-                       GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
-                       R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
-                       GL_LockArrays(0, 0);
-               }
-               qglEnable(GL_CULL_FACE);
-               return;
-       }
        GL_DepthTest(!(texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST));
        if (texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
                qglDisable(GL_CULL_FACE);
-if (r_test.integer)
-{
        if (texture->currentnumlayers)
        {
                int layerindex;
                texturelayer_t *layer;
                for (layerindex = 0, layer = texture->currentlayers;layerindex < texture->currentnumlayers;layerindex++, layer++)
                {
+                       vec4_t layercolor;
+                       int layertexrgbscale;
                        GL_DepthMask(layer->depthmask);
                        GL_BlendFunc(layer->blendfunc1, layer->blendfunc2);
-                       GL_Color(layer->color[0], layer->color[1], layer->color[2], layer->color[3]);
-                       applycolor = layer->color[0] != 1 || layer->color[1] != 1 || layer->color[2] != 1 || layer->color[3] != 1;
+                       if ((layer->color[0] > 2 || layer->color[1] > 2 || layer->color[2] > 2) && (gl_combine.integer || layer->depthmask))
+                       {
+                               layertexrgbscale = 4;
+                               VectorScale(layer->color, 0.25f, layercolor);
+                       }
+                       else if ((layer->color[0] > 1 || layer->color[1] > 1 || layer->color[2] > 1) && (gl_combine.integer || layer->depthmask))
+                       {
+                               layertexrgbscale = 2;
+                               VectorScale(layer->color, 0.5f, layercolor);
+                       }
+                       else
+                       {
+                               layertexrgbscale = 1;
+                               VectorScale(layer->color, 1.0f, layercolor);
+                       }
+                       layercolor[3] = layer->color[3];
+                       GL_Color(layercolor[0], layercolor[1], layercolor[2], layercolor[3]);
+                       applycolor = layercolor[0] != 1 || layercolor[1] != 1 || layercolor[2] != 1 || layercolor[3] != 1;
                        switch (layer->type)
                        {
                        case TEXTURELAYERTYPE_SKY:
@@ -1929,7 +1798,7 @@ if (r_test.integer)
                                                R_Sky();
                                                // restore entity matrix and GL_Color
                                                R_Mesh_Matrix(&ent->matrix);
-                                               GL_Color(layer->color[0], layer->color[1], layer->color[2], layer->color[3]);
+                                               GL_Color(layercolor[0], layercolor[1], layercolor[2], layercolor[3]);
                                        }
                                }
                                // LordHavoc: HalfLife maps have freaky skypolys...
@@ -1962,11 +1831,11 @@ if (r_test.integer)
                                                GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 1);
                                }
                                break;
-                       case TEXTURELAYERTYPE_LIGHTMAP_DOUBLEMODULATE_TEXTURE:
+                       case TEXTURELAYERTYPE_LITTEXTURE_COMBINE:
                                memset(&m, 0, sizeof(m));
                                m.tex[1] = R_GetTexture(layer->texture);
                                m.texmatrix[1] = layer->texmatrix;
-                               m.texrgbscale[1] = layer->texrgbscale;
+                               m.texrgbscale[1] = layertexrgbscale;
                                m.pointer_color = varray_color4f;
                                R_Mesh_State(&m);
                                for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
@@ -1978,25 +1847,29 @@ if (r_test.integer)
                                        if (lightmode == 2)
                                        {
                                                R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
-                                               RSurf_SetColorPointer(ent, surface, modelorg, layer->color[0], layer->color[1], layer->color[2], layer->color[3], 2, applycolor, texture->currentfogallpasses);
+                                               RSurf_SetColorPointer(ent, surface, modelorg, layercolor[0], layercolor[1], layercolor[2], layercolor[3], 2, applycolor, layer->flags & TEXTURELAYERFLAG_FOGDARKEN);
                                        }
                                        else if (surface->lightmaptexture)
                                        {
                                                R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
-                                               RSurf_SetColorPointer(ent, surface, modelorg, layer->color[0], layer->color[1], layer->color[2], layer->color[3], 0, applycolor, texture->currentfogallpasses);
+                                               RSurf_SetColorPointer(ent, surface, modelorg, layercolor[0], layercolor[1], layercolor[2], layercolor[3], 0, applycolor, layer->flags & TEXTURELAYERFLAG_FOGDARKEN);
                                        }
                                        else
                                        {
                                                R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
-                                               RSurf_SetColorPointer(ent, surface, modelorg, layer->color[0], layer->color[1], layer->color[2], layer->color[3], 1, applycolor, texture->currentfogallpasses);
+                                               RSurf_SetColorPointer(ent, surface, modelorg, layercolor[0], layercolor[1], layercolor[2], layercolor[3], 1, applycolor, layer->flags & TEXTURELAYERFLAG_FOGDARKEN);
                                        }
                                        GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
                                        R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
                                        GL_LockArrays(0, 0);
                                }
                                break;
-                       case TEXTURELAYERTYPE_LIGHTMAP:
+                       case TEXTURELAYERTYPE_LITTEXTURE_MULTIPASS:
                                memset(&m, 0, sizeof(m));
+                               m.tex[0] = R_GetTexture(layer->texture);
+                               m.texmatrix[0] = layer->texmatrix;
+                               m.pointer_color = varray_color4f;
+                               m.texrgbscale[0] = layertexrgbscale;
                                R_Mesh_State(&m);
                                for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
                                {
@@ -2006,7 +1879,7 @@ if (r_test.integer)
                                        if (lightmode == 2)
                                        {
                                                R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
-                                               RSurf_SetColorPointer(ent, surface, modelorg, layer->color[0], layer->color[1], layer->color[2], layer->color[3], 2, applycolor, false);
+                                               RSurf_SetColorPointer(ent, surface, modelorg, 1, 1, 1, 1, 2, false, false);
                                        }
                                        else if (surface->lightmaptexture)
                                        {
@@ -2016,27 +1889,43 @@ if (r_test.integer)
                                        else
                                        {
                                                R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
-                                               // TODO: use a specialized RSurf_SetColorPointer
-                                               RSurf_SetColorPointer(ent, surface, modelorg, layer->color[0], layer->color[1], layer->color[2], layer->color[3], 1, applycolor, false);
+                                               RSurf_SetColorPointer(ent, surface, modelorg, 1, 1, 1, 1, 1, false, false);
                                        }
                                        GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
                                        R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
                                        GL_LockArrays(0, 0);
                                }
+                               GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
+                               memset(&m, 0, sizeof(m));
+                               m.tex[0] = R_GetTexture(layer->texture);
+                               m.texmatrix[0] = layer->texmatrix;
+                               m.pointer_color = varray_color4f;
+                               m.texrgbscale[0] = layertexrgbscale;
+                               R_Mesh_State(&m);
+                               for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
+                               {
+                                       surface = texturesurfacelist[texturesurfaceindex];
+                                       RSurf_SetVertexPointer(ent, texture, surface, modelorg);
+                                       R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
+                                       RSurf_SetColorPointer(ent, surface, modelorg, layercolor[0], layercolor[1], layercolor[2], layercolor[3], 0, applycolor, layer->flags & TEXTURELAYERFLAG_FOGDARKEN);
+                                       GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
+                                       R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
+                                       GL_LockArrays(0, 0);
+                               }
                                break;
-                       case TEXTURELAYERTYPE_VERTEXTEXTURE:
+                       case TEXTURELAYERTYPE_LITTEXTURE_VERTEX:
                                memset(&m, 0, sizeof(m));
                                m.tex[0] = R_GetTexture(layer->texture);
                                m.texmatrix[0] = layer->texmatrix;
+                               m.texrgbscale[0] = layertexrgbscale;
                                m.pointer_color = varray_color4f;
-                               m.texrgbscale[0] = layer->texrgbscale;
                                R_Mesh_State(&m);
                                for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
                                {
                                        surface = texturesurfacelist[texturesurfaceindex];
                                        RSurf_SetVertexPointer(ent, texture, surface, modelorg);
                                        R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
-                                       RSurf_SetColorPointer(ent, surface, modelorg, layer->color[0], layer->color[1], layer->color[2], layer->color[3], lightmode ? lightmode : 1, applycolor, texture->currentfogallpasses);
+                                       RSurf_SetColorPointer(ent, surface, modelorg, layercolor[0], layercolor[1], layercolor[2], layercolor[3], lightmode ? lightmode : 1, applycolor, layer->flags & TEXTURELAYERFLAG_FOGDARKEN);
                                        GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
                                        R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
                                        GL_LockArrays(0, 0);
@@ -2047,14 +1936,14 @@ if (r_test.integer)
                                m.tex[0] = R_GetTexture(layer->texture);
                                m.texmatrix[0] = layer->texmatrix;
                                m.pointer_color = varray_color4f;
-                               m.texrgbscale[0] = layer->texrgbscale;
+                               m.texrgbscale[0] = layertexrgbscale;
                                R_Mesh_State(&m);
                                for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
                                {
                                        surface = texturesurfacelist[texturesurfaceindex];
                                        RSurf_SetVertexPointer(ent, texture, surface, modelorg);
                                        R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
-                                       RSurf_SetColorPointer(ent, surface, modelorg, layer->color[0], layer->color[1], layer->color[2], layer->color[3], 0, applycolor, texture->currentfogallpasses);
+                                       RSurf_SetColorPointer(ent, surface, modelorg, layercolor[0], layercolor[1], layercolor[2], layercolor[3], 0, applycolor, layer->flags & TEXTURELAYERFLAG_FOGDARKEN);
                                        GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
                                        R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
                                        GL_LockArrays(0, 0);
@@ -2071,6 +1960,7 @@ if (r_test.integer)
                                for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
                                {
                                        int i;
+                                       float diff[3];
                                        float f, *v, *c;
                                        surface = texturesurfacelist[texturesurfaceindex];
                                        RSurf_SetVertexPointer(ent, texture, surface, modelorg);
@@ -2081,10 +1971,10 @@ if (r_test.integer)
                                        {
                                                VectorSubtract(v, modelorg, diff);
                                                f = exp(fogdensity/DotProduct(diff, diff));
-                                               c[0] = layer->color[0];
-                                               c[1] = layer->color[1];
-                                               c[2] = layer->color[2];
-                                               c[3] = f * layer->color[3];
+                                               c[0] = layercolor[0];
+                                               c[1] = layercolor[1];
+                                               c[2] = layercolor[2];
+                                               c[3] = f * layercolor[3];
                                        }
                                        GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
                                        R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
@@ -2094,439 +1984,34 @@ if (r_test.integer)
                        default:
                                Con_Printf("R_DrawTextureSurfaceList: unknown layer type %i\n", layer->type);
                        }
-               }
-       }
-}
-else
-{
-       GL_DepthMask(!(texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT));
-       if (texture->currentmaterialflags & MATERIALFLAG_ADD)
-               GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
-       else if (texture->currentmaterialflags & MATERIALFLAG_ALPHA)
-               GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-       else
-               GL_BlendFunc(GL_ONE, GL_ZERO);
-       if (texture->currentmaterialflags & MATERIALFLAG_SKY)
-       {
-               if (skyrendernow)
-               {
-                       skyrendernow = false;
-                       if (skyrendermasked)
-                               R_Sky();
-               }
-               // LordHavoc: HalfLife maps have freaky skypolys...
-               //if (!ent->model->brush.ishlbsp)
-               {
-                       R_Mesh_Matrix(&ent->matrix);
-                       GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
-                       if (skyrendermasked)
-                       {
-                               // depth-only (masking)
-                               GL_ColorMask(0,0,0,0);
-                               // just to make sure that braindead drivers don't draw anything
-                               // despite that colormask...
-                               GL_BlendFunc(GL_ZERO, GL_ONE);
-                       }
-                       else
-                       {
-                               // fog sky
-                               GL_BlendFunc(GL_ONE, GL_ZERO);
-                       }
-                       GL_DepthMask(true);
-                       GL_DepthTest(true);
-                       memset(&m, 0, sizeof(m));
-                       R_Mesh_State(&m);
-                       for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
-                       {
-                               surface = texturesurfacelist[texturesurfaceindex];
-                               RSurf_SetVertexPointer(ent, texture, surface, modelorg);
-                               GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
-                               R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
-                               GL_LockArrays(0, 0);
-                       }
-                       GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 1);
-               }
-       }
-       else if (texture->currentmaterialflags & (MATERIALFLAG_WATER | MATERIALFLAG_WALL))
-       {
-               // normal surface (wall or water)
-               if (!(texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT) && r_textureunits.integer >= 2 && gl_combine.integer)
-               {
-                       // multitexture single pass
-                       memset(&m, 0, sizeof(m));
-                       m.tex[1] = R_GetTexture(texture->currentbasetexture);
-                       m.texmatrix[1] = texture->currenttexmatrix;
-                       m.texrgbscale[1] = 2;
-                       m.pointer_color = varray_color4f;
-                       R_Mesh_State(&m);
-                       // transparent is not affected by r_lightmapintensity
-                       if (!(texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
-                               colorscale = r_lightmapintensity;
-                       else
-                               colorscale = 1;
-                       // q3bsp has no lightmap updates, so the lightstylevalue that
-                       // would normally be baked into the lightmaptexture must be
-                       // applied to the color
-                       if (ent->model->type == mod_brushq3)
-                               colorscale *= d_lightstylevalue[0] * (1.0f / 128.0f);
-                       r = texture->currentcolorbase[0] * colorscale;
-                       g = texture->currentcolorbase[1] * colorscale;
-                       b = texture->currentcolorbase[2] * colorscale;
-                       a = texture->currentcolorbase[3];
-                       applycolor = r != 1 || g != 1 || b != 1 || a != 1;
-                       for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
-                       {
-                               surface = texturesurfacelist[texturesurfaceindex];
-                               RSurf_SetVertexPointer(ent, texture, surface, modelorg);
-                               R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordlightmap2f);
-                               R_Mesh_TexCoordPointer(1, 2, surface->groupmesh->data_texcoordtexture2f);
-                               if (surface->lightmaptexture)
-                                       R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
-                               else
-                                       R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
-                               RSurf_SetColorPointer(ent, surface, modelorg, r, g, b, a, lightmode ? lightmode : !surface->lightmaptexture, applycolor, texture->currentfogallpasses);
-                               GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
-                               R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
-                               GL_LockArrays(0, 0);
-                       }
-               }
-               // FIXME: identify models using a better check than ent->model->brush.shadowmesh
-               else if (!(texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT) && !(texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT) && ((ent->effects & EF_FULLBRIGHT) || ent->model->brush.shadowmesh))
-               {
-                       // single texture two pass
-                       GL_BlendFunc(GL_ONE, GL_ZERO);
-                       GL_DepthMask(true);
-                       GL_Color(1, 1, 1, 1);
-                       memset(&m, 0, sizeof(m));
-                       R_Mesh_State(&m);
-                       for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
-                       {
-                               surface = texturesurfacelist[texturesurfaceindex];
-                               RSurf_SetVertexPointer(ent, texture, surface, modelorg);
-                               R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordlightmap2f);
-                               if (surface->lightmaptexture)
-                               {
-                                       R_Mesh_TexBind(0, R_GetTexture(surface->lightmaptexture));
-                                       R_Mesh_ColorPointer(NULL);
-                               }
-                               else
-                               {
-                                       R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
-                                       R_Mesh_ColorPointer(surface->groupmesh->data_lightmapcolor4f);
-                               }
-                               GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
-                               R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
-                               GL_LockArrays(0, 0);
-                       }
-                       GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
-                       GL_DepthMask(false);
-                       GL_Color(r_lightmapintensity * texture->currentcolorbase[0], r_lightmapintensity * texture->currentcolorbase[1], r_lightmapintensity * texture->currentcolorbase[2], texture->currentcolorbase[3]);
-                       memset(&m, 0, sizeof(m));
-                       m.tex[0] = R_GetTexture(texture->currentbasetexture);
-                       m.texmatrix[0] = texture->currenttexmatrix;
-                       R_Mesh_State(&m);
-                       for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
-                       {
-                               surface = texturesurfacelist[texturesurfaceindex];
-                               RSurf_SetVertexPointer(ent, texture, surface, modelorg);
-                               R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
-                               GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
-                               R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
-                               GL_LockArrays(0, 0);
-                       }
-               }
-               else
-               {
-                       // single texture single pass (vertex lit only)
-                       memset(&m, 0, sizeof(m));
-                       m.tex[0] = R_GetTexture(texture->currentbasetexture);
-                       m.texmatrix[0] = texture->currenttexmatrix;
-                       m.pointer_color = varray_color4f;
-                       colorscale = 2;
-                       if (gl_combine.integer)
-                       {
-                               m.texrgbscale[0] = 2;
-                               colorscale = 1;
-                       }
-                       // transparent is not affected by r_lightmapintensity
-                       if (!(texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
-                               colorscale *= r_lightmapintensity;
-                       // q3bsp has no lightmap updates, so the lightstylevalue that
-                       // would normally be baked into the lightmaptexture must be
-                       // applied to the color
-                       if (!(texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT) && ent->model->type == mod_brushq3)
-                               colorscale *= d_lightstylevalue[0] * (1.0f / 128.0f);
-                       R_Mesh_State(&m);
-                       r = texture->currentcolorbase[0] * colorscale;
-                       g = texture->currentcolorbase[1] * colorscale;
-                       b = texture->currentcolorbase[2] * colorscale;
-                       a = texture->currentcolorbase[3];
-                       applycolor = r != 1 || g != 1 || b != 1 || a != 1;
-                       if (!(texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT))
-                       {
-                               for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
-                               {
-                                       surface = texturesurfacelist[texturesurfaceindex];
-                                       RSurf_SetVertexPointer(ent, texture, surface, modelorg);
-                                       R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
-                                       RSurf_SetColorPointer(ent, surface, modelorg, r, g, b, a, lightmode ? lightmode : 1, applycolor, texture->currentfogallpasses);
-                                       GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
-                                       R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
-                                       GL_LockArrays(0, 0);
-                               }
-                       }
-                       else
-                       {
-                               for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
-                               {
-                                       surface = texturesurfacelist[texturesurfaceindex];
-                                       RSurf_SetVertexPointer(ent, texture, surface, modelorg);
-                                       R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
-                                       RSurf_SetColorPointer(ent, surface, modelorg, r, g, b, a, 0, applycolor, texture->currentfogallpasses);
-                                       GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
-                                       R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
-                                       GL_LockArrays(0, 0);
-                               }
-                       }
-               }
-               if (texture->currentdopants)
-               {
-                       GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
-                       memset(&m, 0, sizeof(m));
-                       m.tex[0] = R_GetTexture(texture->skin.pants);
-                       m.texmatrix[0] = texture->currenttexmatrix;
-                       m.pointer_color = varray_color4f;
-                       colorscale = 2;
-                       if (gl_combine.integer)
-                       {
-                               m.texrgbscale[0] = 2;
-                               colorscale = 1;
-                       }
-                       // transparent is not affected by r_lightmapintensity
-                       if (!(texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
-                               colorscale *= r_lightmapintensity;
-                       // q3bsp has no lightmap updates, so the lightstylevalue that
-                       // would normally be baked into the lightmaptexture must be
-                       // applied to the color
-                       if (!(texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT) && !texture->currentdofullbrightpants && ent->model->type == mod_brushq3)
-                               colorscale *= d_lightstylevalue[0] * (1.0f / 128.0f);
-                       R_Mesh_State(&m);
-                       r = texture->currentcolorpants[0] * colorscale;
-                       g = texture->currentcolorpants[1] * colorscale;
-                       b = texture->currentcolorpants[2] * colorscale;
-                       a = texture->currentcolorpants[3];
-                       applycolor = r != 1 || g != 1 || b != 1 || a != 1;
-                       if (!(texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT) && !texture->currentdofullbrightpants)
-                       {
-                               for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
-                               {
-                                       surface = texturesurfacelist[texturesurfaceindex];
-                                       RSurf_SetVertexPointer(ent, texture, surface, modelorg);
-                                       R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
-                                       RSurf_SetColorPointer(ent, surface, modelorg, r, g, b, a, lightmode, applycolor, texture->currentfogallpasses);
-                                       GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
-                                       R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
-                                       GL_LockArrays(0, 0);
-                               }
-                       }
-                       else
-                       {
-                               for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
-                               {
-                                       surface = texturesurfacelist[texturesurfaceindex];
-                                       RSurf_SetVertexPointer(ent, texture, surface, modelorg);
-                                       R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
-                                       RSurf_SetColorPointer(ent, surface, modelorg, r, g, b, a, 0, applycolor, texture->currentfogallpasses);
-                                       GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
-                                       R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
-                                       GL_LockArrays(0, 0);
-                               }
-                       }
-               }
-               if (texture->currentdoshirt)
-               {
-                       GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
-                       memset(&m, 0, sizeof(m));
-                       m.tex[0] = R_GetTexture(texture->skin.shirt);
-                       m.texmatrix[0] = texture->currenttexmatrix;
-                       m.pointer_color = varray_color4f;
-                       colorscale = 2;
-                       if (gl_combine.integer)
-                       {
-                               m.texrgbscale[0] = 2;
-                               colorscale = 1;
-                       }
-                       // transparent is not affected by r_lightmapintensity
-                       if (!(texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
-                               colorscale *= r_lightmapintensity;
-                       // q3bsp has no lightmap updates, so the lightstylevalue that
-                       // would normally be baked into the lightmaptexture must be
-                       // applied to the color
-                       if (!(texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT) && !texture->currentdofullbrightshirt && ent->model->type == mod_brushq3)
-                               colorscale *= d_lightstylevalue[0] * (1.0f / 128.0f);
-                       R_Mesh_State(&m);
-                       r = texture->currentcolorshirt[0] * colorscale;
-                       g = texture->currentcolorshirt[1] * colorscale;
-                       b = texture->currentcolorshirt[2] * colorscale;
-                       a = texture->currentcolorshirt[3];
-                       applycolor = r != 1 || g != 1 || b != 1 || a != 1;
-                       if (!(texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT) && !texture->currentdofullbrightshirt)
-                       {
-                               for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
-                               {
-                                       surface = texturesurfacelist[texturesurfaceindex];
-                                       RSurf_SetVertexPointer(ent, texture, surface, modelorg);
-                                       R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
-                                       RSurf_SetColorPointer(ent, surface, modelorg, r, g, b, a, lightmode, applycolor, texture->currentfogallpasses);
-                                       GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
-                                       R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
-                                       GL_LockArrays(0, 0);
-                               }
-                       }
-                       else
+                       // if trying to do overbright on first pass of an opaque surface
+                       // when combine is not supported, brighten as a post process
+                       if (layertexrgbscale > 1 && !gl_combine.integer && layer->depthmask)
                        {
+                               int scale;
+                               GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
+                               GL_Color(1, 1, 1, 1);
+                               memset(&m, 0, sizeof(m));
+                               R_Mesh_State(&m);
                                for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
                                {
                                        surface = texturesurfacelist[texturesurfaceindex];
                                        RSurf_SetVertexPointer(ent, texture, surface, modelorg);
-                                       R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
-                                       RSurf_SetColorPointer(ent, surface, modelorg, r, g, b, a, 0, applycolor, texture->currentfogallpasses);
                                        GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
-                                       R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
+                                       for (scale = 1;scale < layertexrgbscale;scale <<= 1)
+                                               R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
                                        GL_LockArrays(0, 0);
                                }
                        }
                }
-               if (r_ambient.value >= (1/64.0f))
-               {
-                       GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
-                       GL_DepthMask(false);
-                       memset(&m, 0, sizeof(m));
-                       m.tex[0] = R_GetTexture(texture->skin.base);
-                       m.texmatrix[0] = texture->currenttexmatrix;
-                       m.pointer_color = varray_color4f;
-                       colorscale = 1;
-                       if (gl_combine.integer && (ent->colormod[0] > 1 || ent->colormod[1] > 1 || ent->colormod[2] > 1))
-                       {
-                               m.texrgbscale[0] = 4;
-                               colorscale = 0.25f;
-                       }
-                       R_Mesh_State(&m);
-                       colorscale *= r_ambient.value * (1.0f / 64.0f);
-                       r = texture->currentcolorbase[0] * colorscale;
-                       g = texture->currentcolorbase[1] * colorscale;
-                       b = texture->currentcolorbase[2] * colorscale;
-                       a = texture->currentcolorbase[3];
-                       applycolor = r != 1 || g != 1 || b != 1 || a != 1;
-                       for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
-                       {
-                               surface = texturesurfacelist[texturesurfaceindex];
-                               RSurf_SetVertexPointer(ent, texture, surface, modelorg);
-                               R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
-                               RSurf_SetColorPointer(ent, surface, modelorg, r, g, b, a, 0, applycolor, texture->currentfogallpasses);
-                               GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
-                               R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
-                               GL_LockArrays(0, 0);
-                       }
-               }
-               if (texture->skin.glow != NULL)
-               {
-                       // if glow was not already done using multitexture, do it now.
-                       GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
-                       GL_DepthMask(false);
-                       memset(&m, 0, sizeof(m));
-                       m.tex[0] = R_GetTexture(texture->skin.glow);
-                       m.texmatrix[0] = texture->currenttexmatrix;
-                       m.pointer_color = varray_color4f;
-                       R_Mesh_State(&m);
-                       r = 1;
-                       g = 1;
-                       b = 1;
-                       a = texture->currentcolorbase[3];
-                       applycolor = r != 1 || g != 1 || b != 1 || a != 1;
-                       for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
-                       {
-                               surface = texturesurfacelist[texturesurfaceindex];
-                               RSurf_SetVertexPointer(ent, texture, surface, modelorg);
-                               R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
-                               RSurf_SetColorPointer(ent, surface, modelorg, r, g, b, a, 0, applycolor, texture->currentfogallpasses);
-                               GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
-                               R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
-                               GL_LockArrays(0, 0);
-                       }
-               }
-               if (fogenabled && !(texture->currentmaterialflags & MATERIALFLAG_ADD))
-               {
-                       // if this is opaque use alpha blend which will darken the earlier
-                       // passes cheaply.
-                       //
-                       // if this is an alpha blended material, all the earlier passes
-                       // were darkened by fog already, so we only need to add the fog
-                       // color ontop through the fog mask texture
-                       //
-                       // if this is an additive blended material, all the earlier passes
-                       // were darkened by fog already, and we should not add fog color
-                       // (because the background was not darkened, there is no fog color
-                       // that was lost behind it).
-                       if (texture->currentfogallpasses)
-                               GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
-                       else
-                               GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-                       GL_DepthMask(false);
-                       memset(&m, 0, sizeof(m));
-                       m.tex[0] = R_GetTexture(texture->skin.fog);
-                       m.texmatrix[0] = texture->currenttexmatrix;
-                       R_Mesh_State(&m);
-                       r = fogcolor[0];
-                       g = fogcolor[1];
-                       b = fogcolor[2];
-                       a = texture->currentcolorbase[3];
-                       applycolor = r != 1 || g != 1 || b != 1 || a != 1;
-                       for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
-                       {
-                               surface = texturesurfacelist[texturesurfaceindex];
-                               RSurf_SetVertexPointer(ent, texture, surface, modelorg);
-                               R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
-                               R_Mesh_ColorPointer(varray_color4f);
-                               //RSurf_FogPassColors_Vertex3f_Color4f((surface->groupmesh->data_vertex3f + 3 * surface->num_firstvertex), varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], texture->currentcolorbase[3], 1, surface->num_vertices, modelorg);
-                               if (!surface->lightmaptexture && surface->groupmesh->data_lightmapcolor4f && (texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
-                               {
-                                       for (i = 0, v = (rsurface_vertex3f + 3 * surface->num_firstvertex), c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
-                                       {
-                                               VectorSubtract(v, modelorg, diff);
-                                               f = exp(fogdensity/DotProduct(diff, diff));
-                                               c[0] = r;
-                                               c[1] = g;
-                                               c[2] = b;
-                                               c[3] = (surface->groupmesh->data_lightmapcolor4f + 4 * surface->num_firstvertex)[i*4+3] * f * a;
-                                       }
-                               }
-                               else
-                               {
-                                       for (i = 0, v = (rsurface_vertex3f + 3 * surface->num_firstvertex), c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
-                                       {
-                                               VectorSubtract(v, modelorg, diff);
-                                               f = exp(fogdensity/DotProduct(diff, diff));
-                                               c[0] = r;
-                                               c[1] = g;
-                                               c[2] = b;
-                                               c[3] = f * a;
-                                       }
-                               }
-                               GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
-                               R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (surface->groupmesh->data_element3i + 3 * surface->num_firsttriangle));
-                               GL_LockArrays(0, 0);
-                       }
-               }
        }
-}
        if (texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
                qglEnable(GL_CULL_FACE);
 }
 
 static void RSurfShader_Transparent_Callback(const void *calldata1, int calldata2)
 {
-       const entity_render_t *ent = calldata1;
+       const entity_render_t *ent = (entity_render_t *)calldata1;
        const msurface_t *surface = ent->model->data_surfaces + calldata2;
        vec3_t modelorg;
        texture_t *texture;