]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_rmain.c
Rename dhullinfo_t to hullinfo_t, fix byte->qbyte. More fixes later! Don't complain...
[xonotic/darkplaces.git] / gl_rmain.c
index 49004019f351ed464262c848cb315b08ba950085..ec27666473e46b340e91993ef3c470abdd3f4ac7 100644 (file)
@@ -97,7 +97,6 @@ cvar_t r_textureunits = {0, "r_textureunits", "32"};
 cvar_t r_lerpsprites = {CVAR_SAVE, "r_lerpsprites", "1"};
 cvar_t r_lerpmodels = {CVAR_SAVE, "r_lerpmodels", "1"};
 cvar_t r_waterscroll = {CVAR_SAVE, "r_waterscroll", "1"};
-cvar_t r_watershader = {CVAR_SAVE, "r_watershader", "1"};
 
 cvar_t r_bloom = {CVAR_SAVE, "r_bloom", "0"};
 cvar_t r_bloom_intensity = {CVAR_SAVE, "r_bloom_intensity", "2"};
@@ -111,6 +110,8 @@ cvar_t developer_texturelogging = {0, "developer_texturelogging", "0"};
 
 cvar_t gl_lightmaps = {0, "gl_lightmaps", "0"};
 
+cvar_t r_test = {0, "r_test", "0"}; // used for testing renderer code changes, otherwise does nothing
+
 rtexturepool_t *r_main_texturepool;
 rtexture_t *r_bloom_texture_screen;
 rtexture_t *r_bloom_texture_bloom;
@@ -120,8 +121,6 @@ rtexture_t *r_texture_black;
 rtexture_t *r_texture_notexture;
 rtexture_t *r_texture_whitecube;
 rtexture_t *r_texture_normalizationcube;
-rtexture_t *r_texture_detailtextures[NUM_DETAILTEXTURES];
-rtexture_t *r_texture_distorttexture[64];
 
 void R_ModulateColors(float *in, float *out, int verts, float r, float g, float b)
 {
@@ -221,103 +220,6 @@ void FOG_registercvars(void)
        }
 }
 
-static void R_BuildDetailTextures (void)
-{
-       int i, x, y, light;
-       float vc[3], vx[3], vy[3], vn[3], lightdir[3];
-#define DETAILRESOLUTION 256
-       qbyte (*data)[DETAILRESOLUTION][4];
-       qbyte (*noise)[DETAILRESOLUTION];
-
-       // Allocate the buffers dynamically to avoid having such big guys on the stack
-       data = Mem_Alloc(tempmempool, DETAILRESOLUTION * sizeof(*data));
-       noise = Mem_Alloc(tempmempool, DETAILRESOLUTION * sizeof(*noise));
-
-       lightdir[0] = 0.5;
-       lightdir[1] = 1;
-       lightdir[2] = -0.25;
-       VectorNormalize(lightdir);
-       for (i = 0;i < NUM_DETAILTEXTURES;i++)
-       {
-               fractalnoise(&noise[0][0], DETAILRESOLUTION, DETAILRESOLUTION >> 4);
-               for (y = 0;y < DETAILRESOLUTION;y++)
-               {
-                       for (x = 0;x < DETAILRESOLUTION;x++)
-                       {
-                               vc[0] = x;
-                               vc[1] = y;
-                               vc[2] = noise[y][x] * (1.0f / 32.0f);
-                               vx[0] = x + 1;
-                               vx[1] = y;
-                               vx[2] = noise[y][(x + 1) % DETAILRESOLUTION] * (1.0f / 32.0f);
-                               vy[0] = x;
-                               vy[1] = y + 1;
-                               vy[2] = noise[(y + 1) % DETAILRESOLUTION][x] * (1.0f / 32.0f);
-                               VectorSubtract(vx, vc, vx);
-                               VectorSubtract(vy, vc, vy);
-                               CrossProduct(vx, vy, vn);
-                               VectorNormalize(vn);
-                               light = 128 - DotProduct(vn, lightdir) * 128;
-                               light = bound(0, light, 255);
-                               data[y][x][0] = data[y][x][1] = data[y][x][2] = light;
-                               data[y][x][3] = 255;
-                       }
-               }
-               r_texture_detailtextures[i] = R_LoadTexture2D(r_main_texturepool, va("detailtexture%i", i), DETAILRESOLUTION, DETAILRESOLUTION, &data[0][0][0], TEXTYPE_RGBA, TEXF_MIPMAP | TEXF_PRECACHE, NULL);
-       }
-
-       Mem_Free(noise);
-       Mem_Free(data);
-}
-
-static qbyte R_MorphDistortTexture (double y0, double y1, double y2, double y3, double morph)
-{
-       int m = (int)(((y1 + y3 - (y0 + y2)) * morph * morph * morph) +
-                       ((2 * (y0 - y1) + y2 - y3) * morph * morph) +
-                       ((y2 - y0) * morph) +
-                       (y1));
-       return (qbyte)bound(0, m, 255);
-}
-
-static void R_BuildDistortTexture (void)
-{
-       int x, y, i, j;
-#define DISTORTRESOLUTION 32
-       qbyte data[5][DISTORTRESOLUTION][DISTORTRESOLUTION][2];
-
-       for (i=0; i<4; i++)
-       {
-               for (y=0; y<DISTORTRESOLUTION; y++)
-               {
-                       for (x=0; x<DISTORTRESOLUTION; x++)
-                       {
-                               data[i][y][x][0] = rand () & 255;
-                               data[i][y][x][1] = rand () & 255;
-                       }
-               }
-       }
-
-       for (i=0; i<4; i++)
-       {
-               for (j=0; j<16; j++)
-               {
-                       r_texture_distorttexture[i*16+j] = NULL;
-                       if (gl_textureshader)
-                       {
-                               for (y=0; y<DISTORTRESOLUTION; y++)
-                               {
-                                       for (x=0; x<DISTORTRESOLUTION; x++)
-                                       {
-                                               data[4][y][x][0] = R_MorphDistortTexture (data[(i-1)&3][y][x][0], data[i][y][x][0], data[(i+1)&3][y][x][0], data[(i+2)&3][y][x][0], 0.0625*j);
-                                               data[4][y][x][1] = R_MorphDistortTexture (data[(i-1)&3][y][x][1], data[i][y][x][1], data[(i+1)&3][y][x][1], data[(i+2)&3][y][x][1], 0.0625*j);
-                                       }
-                               }
-                               r_texture_distorttexture[i*16+j] = R_LoadTexture2D(r_main_texturepool, va("distorttexture%i", i*16+j), DISTORTRESOLUTION, DISTORTRESOLUTION, &data[4][0][0][0], TEXTYPE_DSDT, TEXF_PRECACHE, NULL);
-                       }
-               }
-       }
-}
-
 static void R_BuildBlankTextures(void)
 {
        qbyte data[4];
@@ -444,8 +346,6 @@ void gl_main_start(void)
        r_bloom_texture_bloom = NULL;
        R_BuildBlankTextures();
        R_BuildNoTexture();
-       R_BuildDetailTextures();
-       R_BuildDistortTexture();
        if (gl_texturecubemap)
        {
                R_BuildWhiteCube();
@@ -508,7 +408,6 @@ void GL_Main_Init(void)
        Cvar_RegisterVariable(&r_lerpsprites);
        Cvar_RegisterVariable(&r_lerpmodels);
        Cvar_RegisterVariable(&r_waterscroll);
-       Cvar_RegisterVariable(&r_watershader);
        Cvar_RegisterVariable(&r_drawcollisionbrushes);
        Cvar_RegisterVariable(&r_bloom);
        Cvar_RegisterVariable(&r_bloom_intensity);
@@ -518,6 +417,7 @@ void GL_Main_Init(void)
        Cvar_RegisterVariable(&r_smoothnormals_areaweighting);
        Cvar_RegisterVariable(&developer_texturelogging);
        Cvar_RegisterVariable(&gl_lightmaps);
+       Cvar_RegisterVariable(&r_test);
        if (gamemode == GAME_NEHAHRA || gamemode == GAME_NEXUIZ || gamemode == GAME_TENEBRAE)
                Cvar_SetValue("r_fullbrights", 0);
        R_RegisterModule("GL_Main", gl_main_start, gl_main_shutdown, gl_main_newmap);
@@ -1454,36 +1354,62 @@ 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)
+{
+       texturelayer_t *layer;
+       layer = t->currentlayers + t->currentnumlayers++;
+       layer->type = type;
+       layer->depthmask = depthmask;
+       layer->blendfunc1 = blendfunc1;
+       layer->blendfunc2 = blendfunc2;
+       layer->texture = texture;
+       layer->texmatrix = *matrix;
+       layer->color[0] = r;
+       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)
 {
-       texture_t *texture = t;
-       model_t *model = ent->model;
-       int s = ent->skinnum;
-       if ((unsigned int)s >= (unsigned int)model->numskins)
-               s = 0;
-       if (s >= 1)
-               c_models++;
-       if (model->skinscenes)
+       // FIXME: identify models using a better check than ent->model->brush.shadowmesh
+       //int lightmode = ((ent->effects & EF_FULLBRIGHT) || ent->model->brush.shadowmesh) ? 0 : 2;
+
        {
-               if (model->skinscenes[s].framecount > 1)
-                       s = model->skinscenes[s].firstframe + (unsigned int) (r_refdef.time * model->skinscenes[s].framerate) % model->skinscenes[s].framecount;
-               else
-                       s = model->skinscenes[s].firstframe;
+               texture_t *texture = t;
+               model_t *model = ent->model;
+               int s = ent->skinnum;
+               if ((unsigned int)s >= (unsigned int)model->numskins)
+                       s = 0;
+               if (s >= 1)
+                       c_models++;
+               if (model->skinscenes)
+               {
+                       if (model->skinscenes[s].framecount > 1)
+                               s = model->skinscenes[s].firstframe + (unsigned int) (r_refdef.time * model->skinscenes[s].framerate) % model->skinscenes[s].framecount;
+                       else
+                               s = model->skinscenes[s].firstframe;
+               }
+               if (s > 0)
+                       t = t + s * model->num_surfaces;
+               if (t->animated)
+                       t = t->anim_frames[ent->frame != 0][(t->anim_total[ent->frame != 0] >= 2) ? ((int)(r_refdef.time * 5.0f) % t->anim_total[ent->frame != 0]) : 0];
+               texture->currentframe = t;
        }
-       if (s > 0)
-               t = t + s * model->num_surfaces;
-       if (t->animated)
-               t = t->anim_frames[ent->frame != 0][(t->anim_total[ent->frame != 0] >= 2) ? ((int)(r_refdef.time * 5.0f) % t->anim_total[ent->frame != 0]) : 0];
-       texture->currentframe = t;
+
        t->currentmaterialflags = t->basematerialflags;
-       t->currentalpha = ent->alpha;
+       t->currentcolorbase[0] = ent->colormod[0];
+       t->currentcolorbase[1] = ent->colormod[1];
+       t->currentcolorbase[2] = ent->colormod[2];
+       t->currentcolorbase[3] = ent->alpha;
        if (t->basematerialflags & MATERIALFLAG_WATERALPHA)
-               t->currentalpha *= r_wateralpha.value;
+               t->currentcolorbase[3] *= 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->currentalpha < 1)
+       else if (t->currentcolorbase[3] < 1)
                t->currentmaterialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_TRANSPARENT;
        if (ent->effects & EF_NODEPTHTEST)
                t->currentmaterialflags |= MATERIALFLAG_NODEPTHTEST;
@@ -1491,6 +1417,227 @@ 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);
+               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);
+               }
+               else if (t->currentmaterialflags & (MATERIALFLAG_WATER | MATERIALFLAG_WALL))
+               {
+                       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)))
+                       {
+                               r *= r_lightmapintensity;
+                               g *= r_lightmapintensity;
+                               b *= r_lightmapintensity;
+                       }
+                       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)
+                       {
+                               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);
+                       }
+                       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);
+                       }
+                       if (ent->colormap >= 0 && t->skin.pants)
+                       {
+                               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))
+                               {
+                                       type = TEXTURELAYERTYPE_TEXTURE;
+                                       r *= 0.5f;
+                                       g *= 0.5f;
+                                       b *= 0.5f;
+                               }
+                               else
+                               {
+                                       type = TEXTURELAYERTYPE_VERTEXTEXTURE;
+                                       if (!(t->currentmaterialflags & (MATERIALFLAG_TRANSPARENT | MATERIALFLAG_FULLBRIGHT)))
+                                       {
+                                               r *= r_lightmapintensity;
+                                               g *= r_lightmapintensity;
+                                               b *= r_lightmapintensity;
+                                       }
+                               }
+                               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)))
+                                       {
+                                               r *= r_lightmapintensity;
+                                               g *= r_lightmapintensity;
+                                               b *= r_lightmapintensity;
+                                       }
+                               }
+                               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);
+                       }
+               }
+       }
 }
 
 void R_UpdateAllTextureInfo(entity_render_t *ent)
@@ -1607,6 +1754,7 @@ void RSurf_SetColorPointer(const entity_render_t *ent, const msurface_t *surface
        vec3_t diff;
        if (lightmode >= 2)
        {
+               // model lighting
                vec4_t ambientcolor4f;
                vec3_t diffusecolor;
                vec3_t diffusenormal;
@@ -1638,8 +1786,7 @@ void RSurf_SetColorPointer(const entity_render_t *ent, const msurface_t *surface
        {
                if (surface->lightmapinfo)
                {
-                       rsurface_lightmapcolor4f = varray_color4f;
-                       for (i = 0, c = rsurface_lightmapcolor4f + 4 * surface->num_firstvertex;i < surface->num_vertices;i++, c += 4)
+                       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);
@@ -1664,6 +1811,7 @@ void RSurf_SetColorPointer(const entity_render_t *ent, const msurface_t *surface
                                        }
                                }
                        }
+                       rsurface_lightmapcolor4f = varray_color4f;
                }
                else
                        rsurface_lightmapcolor4f = surface->groupmesh->data_lightmapcolor4f;
@@ -1707,6 +1855,7 @@ void RSurf_SetColorPointer(const entity_render_t *ent, const msurface_t *surface
                        c2[2] = c[2] * b;
                        c2[3] = c[3] * a;
                }
+               rsurface_lightmapcolor4f = varray_color4f;
        }
        R_Mesh_ColorPointer(rsurface_lightmapcolor4f);
        GL_Color(r, g, b, a);
@@ -1717,32 +1866,19 @@ static void R_DrawTextureSurfaceList(const entity_render_t *ent, texture_t *text
 {
        int i;
        int texturesurfaceindex;
+       int lightmode;
        const float *v;
        float *c;
        float diff[3];
-       float colorpants[3], colorshirt[3];
        float f, r, g, b, a, colorscale;
        const msurface_t *surface;
-       qboolean dolightmap;
-       qboolean doambient;
-       qboolean dodetail;
-       qboolean doglow;
-       qboolean dofogpass;
-       qboolean fogallpasses;
-       qboolean dopants;
-       qboolean doshirt;
-       qboolean dofullbrightpants;
-       qboolean dofullbrightshirt;
        qboolean applycolor;
-       qboolean lightmode = 0;
-       rtexture_t *basetexture;
        rmeshstate_t m;
        if (texture->currentmaterialflags & MATERIALFLAG_NODRAW)
                return;
        c_faces += texturenumsurfaces;
-       // FIXME: identify models using a better check than ent->model->shadowmesh
-       if (!(ent->effects & EF_FULLBRIGHT) && !ent->model->brush.shadowmesh)
-               lightmode = 2;
+       // 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)
        {
@@ -1768,6 +1904,201 @@ static void R_DrawTextureSurfaceList(const entity_render_t *ent, texture_t *text
                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++)
+               {
+                       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;
+                       switch (layer->type)
+                       {
+                       case TEXTURELAYERTYPE_SKY:
+                               if (skyrendernow)
+                               {
+                                       skyrendernow = false;
+                                       if (skyrendermasked)
+                                       {
+                                               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]);
+                                       }
+                               }
+                               // LordHavoc: HalfLife maps have freaky skypolys...
+                               //if (!ent->model->brush.ishlbsp)
+                               {
+                                       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);
+                                       }
+                                       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);
+                                       }
+                                       if (skyrendermasked)
+                                               GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 1);
+                               }
+                               break;
+                       case TEXTURELAYERTYPE_LIGHTMAP_DOUBLEMODULATE_TEXTURE:
+                               memset(&m, 0, sizeof(m));
+                               m.tex[1] = R_GetTexture(layer->texture);
+                               m.texmatrix[1] = layer->texmatrix;
+                               m.texrgbscale[1] = layer->texrgbscale;
+                               m.pointer_color = varray_color4f;
+                               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);
+                                       R_Mesh_TexCoordPointer(1, 2, surface->groupmesh->data_texcoordtexture2f);
+                                       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);
+                                       }
+                                       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);
+                                       }
+                                       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);
+                                       }
+                                       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:
+                               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 (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);
+                                       }
+                                       else 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));
+                                               // 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);
+                                       }
+                                       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:
+                               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] = 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);
+                                       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_TEXTURE:
+                               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] = 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], 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);
+                               }
+                               break;
+                       case TEXTURELAYERTYPE_FOG:
+                               memset(&m, 0, sizeof(m));
+                               if (layer->texture)
+                               {
+                                       m.tex[0] = R_GetTexture(layer->texture);
+                                       m.texmatrix[0] = layer->texmatrix;
+                               }
+                               R_Mesh_State(&m);
+                               for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
+                               {
+                                       int i;
+                                       float f, *v, *c;
+                                       surface = texturesurfacelist[texturesurfaceindex];
+                                       RSurf_SetVertexPointer(ent, texture, surface, modelorg);
+                                       if (layer->texture)
+                                               R_Mesh_TexCoordPointer(0, 2, surface->groupmesh->data_texcoordtexture2f);
+                                       R_Mesh_ColorPointer(varray_color4f);
+                                       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] = layer->color[0];
+                                               c[1] = layer->color[1];
+                                               c[2] = layer->color[2];
+                                               c[3] = f * layer->color[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));
+                                       GL_LockArrays(0, 0);
+                               }
+                               break;
+                       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);
@@ -1775,8 +2106,6 @@ static void R_DrawTextureSurfaceList(const entity_render_t *ent, texture_t *text
                GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        else
                GL_BlendFunc(GL_ONE, GL_ZERO);
-       if (texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
-               qglDisable(GL_CULL_FACE);
        if (texture->currentmaterialflags & MATERIALFLAG_SKY)
        {
                if (skyrendernow)
@@ -1818,82 +2147,14 @@ static void R_DrawTextureSurfaceList(const entity_render_t *ent, texture_t *text
                        GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 1);
                }
        }
-       else if ((texture->currentmaterialflags & MATERIALFLAG_WATER) && r_watershader.value && gl_textureshader && !texture->skin.glow && !fogenabled && ent->colormod[0] == 1 && ent->colormod[1] == 1 && ent->colormod[2] == 1)
-       {
-               // NVIDIA Geforce3 distortion texture shader on water
-               float args[4] = {0.05f,0,0,0.04f};
-               memset(&m, 0, sizeof(m));
-               m.tex[0] = R_GetTexture(r_texture_distorttexture[(int)(r_refdef.time * 16)&63]);
-               m.tex[1] = R_GetTexture(texture->skin.base);
-               m.texcombinergb[0] = GL_REPLACE;
-               m.texcombinergb[1] = GL_REPLACE;
-               Matrix4x4_CreateFromQuakeEntity(&m.texmatrix[0], 0, 0, 0, 0, 0, 0, r_watershader.value);
-               m.texmatrix[1] = texture->currenttexmatrix;
-               R_Mesh_State(&m);
-
-               GL_Color(1, 1, 1, texture->currentalpha);
-               GL_ActiveTexture(0);
-               qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
-               GL_ActiveTexture(1);
-               qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_OFFSET_TEXTURE_2D_NV);
-               qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB);
-               qglTexEnvfv(GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_MATRIX_NV, &args[0]);
-               qglEnable(GL_TEXTURE_SHADER_NV);
-
-               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_TexCoordPointer(1, 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);
-               }
-
-               qglDisable(GL_TEXTURE_SHADER_NV);
-               qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
-               GL_ActiveTexture(0);
-       }
        else if (texture->currentmaterialflags & (MATERIALFLAG_WATER | MATERIALFLAG_WALL))
        {
                // normal surface (wall or water)
-               dolightmap = !(texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT);
-               doambient = r_ambient.value >= (1/64.0f);
-               dodetail = r_detailtextures.integer && texture->skin.detail != NULL && !(texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT);
-               doglow = texture->skin.glow != NULL;
-               dofogpass = fogenabled && !(texture->currentmaterialflags & MATERIALFLAG_ADD);
-               fogallpasses = fogenabled && !(texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT);
-               if (ent->colormap >= 0)
-               {
-                       int b;
-                       qbyte *bcolor;
-                       basetexture = texture->skin.base;
-                       dopants = texture->skin.pants != NULL;
-                       doshirt = texture->skin.shirt != NULL;
-                       // 128-224 are backwards ranges
-                       b = (ent->colormap & 0xF) << 4;b += (b >= 128 && b < 224) ? 4 : 12;
-                       dofullbrightpants = b >= 224;
-                       bcolor = (qbyte *) (&palette_complete[b]);
-                       VectorScale(bcolor, (1.0f / 255.0f), colorpants);
-                       // 128-224 are backwards ranges
-                       b = (ent->colormap & 0xF0);b += (b >= 128 && b < 224) ? 4 : 12;
-                       dofullbrightshirt = b >= 224;
-                       bcolor = (qbyte *) (&palette_complete[b]);
-                       VectorScale(bcolor, (1.0f / 255.0f), colorshirt);
-               }
-               else
-               {
-                       basetexture = texture->skin.merged ? texture->skin.merged : texture->skin.base;
-                       dopants = false;
-                       doshirt = false;
-                       dofullbrightshirt = false;
-                       dofullbrightpants = false;
-               }
-               if (dolightmap && r_textureunits.integer >= 2 && gl_combine.integer)
+               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(basetexture);
+                       m.tex[1] = R_GetTexture(texture->currentbasetexture);
                        m.texmatrix[1] = texture->currenttexmatrix;
                        m.texrgbscale[1] = 2;
                        m.pointer_color = varray_color4f;
@@ -1908,10 +2169,10 @@ static void R_DrawTextureSurfaceList(const entity_render_t *ent, texture_t *text
                        // applied to the color
                        if (ent->model->type == mod_brushq3)
                                colorscale *= d_lightstylevalue[0] * (1.0f / 128.0f);
-                       r = ent->colormod[0] * colorscale;
-                       g = ent->colormod[1] * colorscale;
-                       b = ent->colormod[2] * colorscale;
-                       a = texture->currentalpha;
+                       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++)
                        {
@@ -1923,15 +2184,16 @@ static void R_DrawTextureSurfaceList(const entity_render_t *ent, texture_t *text
                                        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, fogallpasses);
+                               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);
                        }
                }
-               else if (dolightmap && !(texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT) && !lightmode)
+               // 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
+                       // single texture two pass
                        GL_BlendFunc(GL_ONE, GL_ZERO);
                        GL_DepthMask(true);
                        GL_Color(1, 1, 1, 1);
@@ -1958,9 +2220,9 @@ static void R_DrawTextureSurfaceList(const entity_render_t *ent, texture_t *text
                        }
                        GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
                        GL_DepthMask(false);
-                       GL_Color(r_lightmapintensity * ent->colormod[0], r_lightmapintensity * ent->colormod[1], r_lightmapintensity * ent->colormod[2], 1);
+                       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(basetexture);
+                       m.tex[0] = R_GetTexture(texture->currentbasetexture);
                        m.texmatrix[0] = texture->currenttexmatrix;
                        R_Mesh_State(&m);
                        for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
@@ -1975,8 +2237,9 @@ static void R_DrawTextureSurfaceList(const entity_render_t *ent, texture_t *text
                }
                else
                {
+                       // single texture single pass (vertex lit only)
                        memset(&m, 0, sizeof(m));
-                       m.tex[0] = R_GetTexture(basetexture);
+                       m.tex[0] = R_GetTexture(texture->currentbasetexture);
                        m.texmatrix[0] = texture->currenttexmatrix;
                        m.pointer_color = varray_color4f;
                        colorscale = 2;
@@ -1991,22 +2254,22 @@ static void R_DrawTextureSurfaceList(const entity_render_t *ent, texture_t *text
                        // q3bsp has no lightmap updates, so the lightstylevalue that
                        // would normally be baked into the lightmaptexture must be
                        // applied to the color
-                       if (dolightmap && ent->model->type == mod_brushq3)
+                       if (!(texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT) && ent->model->type == mod_brushq3)
                                colorscale *= d_lightstylevalue[0] * (1.0f / 128.0f);
                        R_Mesh_State(&m);
-                       r = ent->colormod[0] * colorscale;
-                       g = ent->colormod[1] * colorscale;
-                       b = ent->colormod[2] * colorscale;
-                       a = texture->currentalpha;
+                       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 (dolightmap)
+                       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, applycolor, fogallpasses);
+                                       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);
@@ -2019,14 +2282,14 @@ static void R_DrawTextureSurfaceList(const entity_render_t *ent, texture_t *text
                                        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, fogallpasses);
+                                       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 (dopants)
+               if (texture->currentdopants)
                {
                        GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
                        memset(&m, 0, sizeof(m));
@@ -2045,22 +2308,22 @@ static void R_DrawTextureSurfaceList(const entity_render_t *ent, texture_t *text
                        // q3bsp has no lightmap updates, so the lightstylevalue that
                        // would normally be baked into the lightmaptexture must be
                        // applied to the color
-                       if (dolightmap && !dofullbrightpants && ent->model->type == mod_brushq3)
+                       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 = ent->colormod[0] * colorpants[0] * colorscale;
-                       g = ent->colormod[1] * colorpants[1] * colorscale;
-                       b = ent->colormod[2] * colorpants[2] * colorscale;
-                       a = texture->currentalpha;
+                       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 (dolightmap && !dofullbrightpants)
+                       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, fogallpasses);
+                                       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);
@@ -2073,14 +2336,14 @@ static void R_DrawTextureSurfaceList(const entity_render_t *ent, texture_t *text
                                        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, fogallpasses);
+                                       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 (doshirt)
+               if (texture->currentdoshirt)
                {
                        GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
                        memset(&m, 0, sizeof(m));
@@ -2091,7 +2354,7 @@ static void R_DrawTextureSurfaceList(const entity_render_t *ent, texture_t *text
                        if (gl_combine.integer)
                        {
                                m.texrgbscale[0] = 2;
-                               colorscale *= 1;
+                               colorscale = 1;
                        }
                        // transparent is not affected by r_lightmapintensity
                        if (!(texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
@@ -2099,22 +2362,22 @@ static void R_DrawTextureSurfaceList(const entity_render_t *ent, texture_t *text
                        // q3bsp has no lightmap updates, so the lightstylevalue that
                        // would normally be baked into the lightmaptexture must be
                        // applied to the color
-                       if (dolightmap && !dofullbrightshirt && ent->model->type == mod_brushq3)
+                       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 = ent->colormod[0] * colorshirt[0] * colorscale;
-                       g = ent->colormod[1] * colorshirt[1] * colorscale;
-                       b = ent->colormod[2] * colorshirt[2] * colorscale;
-                       a = texture->currentalpha;
+                       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 (dolightmap && !dofullbrightshirt)
+                       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, fogallpasses);
+                                       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);
@@ -2127,16 +2390,15 @@ static void R_DrawTextureSurfaceList(const entity_render_t *ent, texture_t *text
                                        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, fogallpasses);
+                                       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 (doambient)
+               if (r_ambient.value >= (1/64.0f))
                {
-                       doambient = false;
                        GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
                        GL_DepthMask(false);
                        memset(&m, 0, sizeof(m));
@@ -2151,41 +2413,23 @@ static void R_DrawTextureSurfaceList(const entity_render_t *ent, texture_t *text
                        }
                        R_Mesh_State(&m);
                        colorscale *= r_ambient.value * (1.0f / 64.0f);
-                       r = ent->colormod[0] * colorscale;
-                       g = ent->colormod[1] * colorscale;
-                       b = ent->colormod[2] * colorscale;
-                       a = texture->currentalpha;
+                       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, fogallpasses);
+                               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 (dodetail)
-               {
-                       GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
-                       GL_DepthMask(false);
-                       GL_Color(1, 1, 1, 1);
-                       memset(&m, 0, sizeof(m));
-                       m.tex[0] = R_GetTexture(texture->skin.detail);
-                       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_texcoorddetail2f);
-                               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 (doglow)
+               if (texture->skin.glow != NULL)
                {
                        // if glow was not already done using multitexture, do it now.
                        GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
@@ -2198,20 +2442,20 @@ static void R_DrawTextureSurfaceList(const entity_render_t *ent, texture_t *text
                        r = 1;
                        g = 1;
                        b = 1;
-                       a = texture->currentalpha;
+                       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, fogallpasses);
+                               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 (dofogpass)
+               if (fogenabled && !(texture->currentmaterialflags & MATERIALFLAG_ADD))
                {
                        // if this is opaque use alpha blend which will darken the earlier
                        // passes cheaply.
@@ -2224,7 +2468,7 @@ static void R_DrawTextureSurfaceList(const entity_render_t *ent, texture_t *text
                        // 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 (fogallpasses)
+                       if (texture->currentfogallpasses)
                                GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
                        else
                                GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@@ -2236,7 +2480,7 @@ static void R_DrawTextureSurfaceList(const entity_render_t *ent, texture_t *text
                        r = fogcolor[0];
                        g = fogcolor[1];
                        b = fogcolor[2];
-                       a = texture->currentalpha;
+                       a = texture->currentcolorbase[3];
                        applycolor = r != 1 || g != 1 || b != 1 || a != 1;
                        for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
                        {
@@ -2244,7 +2488,7 @@ static void R_DrawTextureSurfaceList(const entity_render_t *ent, texture_t *text
                                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->currentalpha, 1, surface->num_vertices, modelorg);
+                               //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)
@@ -2275,6 +2519,7 @@ static void R_DrawTextureSurfaceList(const entity_render_t *ent, texture_t *text
                        }
                }
        }
+}
        if (texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
                qglEnable(GL_CULL_FACE);
 }