]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_rsurf.c
surfvertex_t is gone, in it's place are non-interleaved arrays, which keeps things...
[xonotic/darkplaces.git] / gl_rsurf.c
index 8851704d58d02d38899aaf83a2bbdc1df33aacc5..43318ed9c148d7e6c396505ef93270e82012af11 100644 (file)
@@ -31,13 +31,22 @@ static qbyte templight[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*4];
 cvar_t r_ambient = {0, "r_ambient", "0"};
 cvar_t r_vertexsurfaces = {0, "r_vertexsurfaces", "0"};
 cvar_t r_dlightmap = {CVAR_SAVE, "r_dlightmap", "1"};
-cvar_t r_drawportals = {0, "r_drawportals", "0"};
+//cvar_t r_drawportals = {0, "r_drawportals", "0"};
 cvar_t r_testvis = {0, "r_testvis", "0"};
 cvar_t r_floatbuildlightmap = {0, "r_floatbuildlightmap", "0"};
+cvar_t r_detailtextures = {CVAR_SAVE, "r_detailtextures", "1"};
+cvar_t r_surfaceworldnode = {0, "r_surfaceworldnode", "0"};
+cvar_t r_cullsurface = {0, "r_cullsurface", "0"};
 
 static int dlightdivtable[32768];
 
-static int R_IntAddDynamicLights (msurface_t *surf)
+// variables used by R_PVSUpdate
+int r_pvsframecount = 0;
+mleaf_t *r_pvsviewleaf = NULL;
+int r_pvsviewleafnovis = 0;
+msurface_t *r_pvsfirstsurface = NULL;
+
+static int R_IntAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
 {
        int sdtable[256], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, red, green, blue, lit, dist2, impacts, impactt, subtract;
        unsigned int *bl;
@@ -61,7 +70,7 @@ static int R_IntAddDynamicLights (msurface_t *surf)
                if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
                        continue;                                       // not lit by this light
 
-               softwareuntransform(r_dlight[lnum].origin, local);
+               Matrix4x4_Transform(matrix, r_dlight[lnum].origin, local);
                dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
 
                // for comparisons to minimum acceptable light
@@ -138,7 +147,7 @@ static int R_IntAddDynamicLights (msurface_t *surf)
        return lit;
 }
 
-static int R_FloatAddDynamicLights (msurface_t *surf)
+static int R_FloatAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
 {
        int lnum, s, t, smax, tmax, smax3, lit, impacts, impactt;
        float sdtable[256], *bl, k, dist, dist2, maxdist, maxdist2, maxdist3, td1, td, red, green, blue, impact[3], local[3], subtract;
@@ -154,7 +163,7 @@ static int R_FloatAddDynamicLights (msurface_t *surf)
                if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
                        continue;                                       // not lit by this light
 
-               softwareuntransform(r_dlight[lnum].origin, local);
+               Matrix4x4_Transform(matrix, r_dlight[lnum].origin, local);
                dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
 
                // for comparisons to minimum acceptable light
@@ -235,7 +244,7 @@ R_BuildLightMap
 Combine and scale multiple lightmaps into the 8.8 format in blocklights
 ===============
 */
-static void R_BuildLightMap (msurface_t *surf, int dlightchanged)
+static void R_BuildLightMap (const entity_render_t *ent, msurface_t *surf, int dlightchanged)
 {
        if (!r_floatbuildlightmap.integer)
        {
@@ -260,7 +269,7 @@ static void R_BuildLightMap (msurface_t *surf, int dlightchanged)
 
        // set to full bright if no light data
                bl = intblocklights;
-               if ((currentrenderentity->effects & EF_FULLBRIGHT) || !currentrenderentity->model->lightdata)
+               if ((ent->effects & EF_FULLBRIGHT) || !ent->model->lightdata)
                {
                        for (i = 0;i < size3;i++)
                                bl[i] = 255*256;
@@ -279,7 +288,7 @@ static void R_BuildLightMap (msurface_t *surf, int dlightchanged)
 
                        if (surf->dlightframe == r_framecount && r_dlightmap.integer)
                        {
-                               surf->cached_dlight = R_IntAddDynamicLights(surf);
+                               surf->cached_dlight = R_IntAddDynamicLights(&ent->inversematrix, surf);
                                if (surf->cached_dlight)
                                        c_light_polys++;
                                else if (dlightchanged)
@@ -301,7 +310,7 @@ static void R_BuildLightMap (msurface_t *surf, int dlightchanged)
                out = templight;
                // deal with lightmap brightness scale
                shift = 7 + lightscalebit + 8;
-               if (currentrenderentity->model->lightmaprgba)
+               if (ent->model->lightmaprgba)
                {
                        stride = (surf->lightmaptexturestride - smax) * 4;
                        for (i = 0;i < tmax;i++, out += stride)
@@ -354,7 +363,7 @@ static void R_BuildLightMap (msurface_t *surf, int dlightchanged)
 
        // set to full bright if no light data
                bl = floatblocklights;
-               if ((currentrenderentity->effects & EF_FULLBRIGHT) || !currentrenderentity->model->lightdata)
+               if ((ent->effects & EF_FULLBRIGHT) || !ent->model->lightdata)
                        j = 255*256;
                else
                        j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
@@ -370,7 +379,7 @@ static void R_BuildLightMap (msurface_t *surf, int dlightchanged)
 
                if (surf->dlightframe == r_framecount && r_dlightmap.integer)
                {
-                       surf->cached_dlight = R_FloatAddDynamicLights(surf);
+                       surf->cached_dlight = R_FloatAddDynamicLights(&ent->inversematrix, surf);
                        if (surf->cached_dlight)
                                c_light_polys++;
                        else if (dlightchanged)
@@ -391,7 +400,7 @@ static void R_BuildLightMap (msurface_t *surf, int dlightchanged)
                out = templight;
                // deal with lightmap brightness scale
                scale = 1.0f / (1 << (7 + lightscalebit + 8));
-               if (currentrenderentity->model->lightmaprgba)
+               if (ent->model->lightmaprgba)
                {
                        stride = (surf->lightmaptexturestride - smax) * 4;
                        for (i = 0;i < tmax;i++, out += stride)
@@ -439,8 +448,8 @@ void R_StainNode (mnode_t *node, model_t *model, vec3_t origin, float radius, in
 
 
        // for comparisons to minimum acceptable light
-       // compensate for 4096 offset
-       maxdist = radius * radius + 4096;
+       // compensate for 256 offset
+       maxdist = radius * radius + 256.0f;
 
        // clamp radius to avoid exceeding 32768 entry division table
        if (maxdist > 4194304)
@@ -463,8 +472,7 @@ loc0:
                goto loc0;
        }
 
-       dist2 = ndist * ndist;
-       dist2 += 4096.0f;
+       dist2 = ndist * ndist + 256.0f;
        if (dist2 < maxdist)
        {
                maxdist3 = maxdist - dist2;
@@ -587,8 +595,8 @@ void R_Stain (vec3_t origin, float radius, int cr1, int cg1, int cb1, int ca1, i
        icolor[7] = ca2;
 
        model = cl.worldmodel;
-       softwaretransformidentity();
-       R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, origin, radius, icolor);
+       if (model)
+               R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, origin, radius, icolor);
 
        // look for embedded bmodels
        for (n = 0;n < cl_num_brushmodel_entities;n++)
@@ -600,8 +608,7 @@ void R_Stain (vec3_t origin, float radius, int cr1, int cg1, int cb1, int ca1, i
                        Mod_CheckLoaded(model);
                        if (model->type == mod_brush)
                        {
-                               softwaretransformforentity(ent);
-                               softwareuntransform(origin, org);
+                               Matrix4x4_Transform(&ent->inversematrix, origin, org);
                                R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, org, radius, icolor);
                        }
                }
@@ -617,247 +624,129 @@ void R_Stain (vec3_t origin, float radius, int cr1, int cg1, int cb1, int ca1, i
 =============================================================
 */
 
-
-static float turbsin[256] =
-{
-       #include "gl_warp_sin.h"
-};
-#define TURBSCALE (256.0 / (2 * M_PI))
-
-// only need to hold as many verts as the mesh splitter will allow in model_brush.c
-#define MAX_SURFVERTS 3072
-typedef struct
-{
-       float v[4];
-       float st[2];
-       float uv[2];
-       float c[4];
-}
-surfvert_t;
-static surfvert_t svert[MAX_SURFVERTS]; // used by the following functions
-
-static void RSurfShader_Sky(msurface_t *firstsurf)
+static void RSurf_AddLightmapToVertexColors(const int *lightmapoffsets, float *c, int numverts, const qbyte *samples, int size3, const qbyte *styles)
 {
-       msurface_t *surf;
        int i;
-       float number, length, dir[3], speedscale;
-       surfvertex_t *v;
-       surfvert_t *sv;
-       surfmesh_t *mesh;
-       rmeshinfo_t m;
-
-       // LordHavoc: HalfLife maps have freaky skypolys...
-       if (currentrenderentity->model->ishlbsp)
-               return;
-
-       if (skyrendermasked)
+       float scale;
+       const qbyte *lm;
+       if (styles[0] != 255)
        {
-               if (skyrendernow)
-               {
-                       skyrendernow = false;
-                       R_Sky();
-               }
-               for (surf = firstsurf;surf;surf = surf->chain)
+               for (i = 0;i < numverts;i++, c += 4)
                {
-                       // draw depth-only polys
-                       memset(&m, 0, sizeof(m));
-                       m.transparent = false;
-                       m.blendfunc1 = GL_ZERO;
-                       m.blendfunc2 = GL_ONE;
-                       m.depthwrite = true;
-                       for (mesh = surf->mesh;mesh;mesh = mesh->chain)
+                       lm = samples + lightmapoffsets[i];
+                       scale = d_lightstylevalue[styles[0]] * (1.0f / 32768.0f);
+                       VectorMA(c, scale, lm, c);
+                       if (styles[1] != 255)
                        {
-                               m.numtriangles = mesh->numtriangles;
-                               m.numverts = mesh->numverts;
-                               m.index = mesh->index;
-                               if (softwaretransform_complexity)
-                               {
-                                       m.vertex = &svert[0].v[0];
-                                       m.vertexstep = sizeof(surfvert_t);
-                                       for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
-                                               softwaretransform(v->v, sv->v);
-                               }
-                               else
+                               lm += size3;
+                               scale = d_lightstylevalue[styles[1]] * (1.0f / 32768.0f);
+                               VectorMA(c, scale, lm, c);
+                               if (styles[2] != 255)
                                {
-                                       m.vertex = &mesh->vertex[0].v[0];
-                                       m.vertexstep = sizeof(surfvertex_t);
+                                       lm += size3;
+                                       scale = d_lightstylevalue[styles[2]] * (1.0f / 32768.0f);
+                                       VectorMA(c, scale, lm, c);
+                                       if (styles[3] != 255)
+                                       {
+                                               lm += size3;
+                                               scale = d_lightstylevalue[styles[3]] * (1.0f / 32768.0f);
+                                               VectorMA(c, scale, lm, c);
+                                       }
                                }
-                               R_Mesh_Draw(&m);
                        }
                }
        }
-       else if (skyrenderglquake)
+}
+
+static void RSurf_FogColors(const float *v, float *c, float colorscale, int numverts, const float *modelorg)
+{
+       int i;
+       float diff[3], f;
+       if (fogenabled)
        {
-               for (surf = firstsurf;surf;surf = surf->chain)
+               for (i = 0;i < numverts;i++, v += 4, c += 4)
                {
-                       memset(&m, 0, sizeof(m));
-                       m.transparent = false;
-                       m.blendfunc1 = GL_ONE;
-                       m.blendfunc2 = GL_ZERO;
-                       m.vertex = &svert[0].v[0];
-                       m.vertexstep = sizeof(surfvert_t);
-                       m.cr = 1;
-                       m.cg = 1;
-                       m.cb = 1;
-                       m.ca = 1;
-                       m.tex[0] = R_GetTexture(solidskytexture);
-                       m.texcoords[0] = &svert[0].st[0];
-                       m.texcoordstep[0] = sizeof(surfvert_t);
-                       speedscale = cl.time * (8.0/128.0);
-                       speedscale -= (int)speedscale;
-                       for (mesh = surf->mesh;mesh;mesh = mesh->chain)
-                       {
-                               m.numtriangles = mesh->numtriangles;
-                               m.numverts = mesh->numverts;
-                               m.index = mesh->index;
-                               for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
-                               {
-                                       softwaretransform(v->v, sv->v);
-                                       VectorSubtract (sv->v, r_origin, dir);
-                                       // flatten the sphere
-                                       dir[2] *= 3;
-
-                                       number = DotProduct(dir, dir);
-                                       #if SLOWMATH
-                                       length = 3.0f / sqrt(number);
-                                       #else
-                                       *((int *)&length) = 0x5f3759df - ((* (int *) &number) >> 1);
-                                       length = 3.0f * (length * (1.5f - (number * 0.5f * length * length)));
-                                       #endif
-
-                                       sv->st[0] = speedscale + dir[0] * length;
-                                       sv->st[1] = speedscale + dir[1] * length;
-                               }
-                               R_Mesh_Draw(&m);
-                       }
+                       VectorSubtract(v, modelorg, diff);
+                       f = colorscale * (1 - exp(fogdensity/DotProduct(diff, diff)));
+                       VectorScale(c, f, c);
                }
        }
-       else
+       else if (colorscale != 1)
+               for (i = 0;i < numverts;i++, c += 4)
+                       VectorScale(c, colorscale, c);
+}
+
+static void RSurf_FoggedColors(const float *v, float *c, float r, float g, float b, float a, float colorscale, int numverts, const float *modelorg)
+{
+       int i;
+       float diff[3], f;
+       r *= colorscale;
+       g *= colorscale;
+       b *= colorscale;
+       if (fogenabled)
        {
-               for (surf = firstsurf;surf;surf = surf->chain)
+               for (i = 0;i < numverts;i++, v += 4, c += 4)
                {
-                       // flat color
-                       memset(&m, 0, sizeof(m));
-                       m.transparent = false;
-                       m.blendfunc1 = GL_ONE;
-                       m.blendfunc2 = GL_ZERO;
-                       m.cr = fogcolor[0];
-                       m.cg = fogcolor[1];
-                       m.cb = fogcolor[2];
-                       m.ca = 1;
-                       for (mesh = surf->mesh;mesh;mesh = mesh->chain)
-                       {
-                               m.numtriangles = mesh->numtriangles;
-                               m.numverts = mesh->numverts;
-                               m.index = mesh->index;
-                               if (softwaretransform_complexity)
-                               {
-                                       m.vertex = &svert[0].v[0];
-                                       m.vertexstep = sizeof(surfvert_t);
-                                       for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
-                                               softwaretransform(v->v, sv->v);
-                               }
-                               else
-                               {
-                                       m.vertex = &mesh->vertex[0].v[0];
-                                       m.vertexstep = sizeof(surfvertex_t);
-                               }
-                               R_Mesh_Draw(&m);
-                       }
+                       VectorSubtract(v, modelorg, diff);
+                       f = 1 - exp(fogdensity/DotProduct(diff, diff));
+                       c[0] = r * f;
+                       c[1] = g * f;
+                       c[2] = b * f;
+                       c[3] = a;
                }
        }
-       if (skyrenderglquake)
+       else
        {
-               for (surf = firstsurf;surf;surf = surf->chain)
+               for (i = 0;i < numverts;i++, c += 4)
                {
-                       memset(&m, 0, sizeof(m));
-                       m.transparent = false;
-                       m.blendfunc1 = GL_SRC_ALPHA;
-                       m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
-                       m.vertex = &svert[0].v[0];
-                       m.vertexstep = sizeof(surfvert_t);
-                       m.cr = 1;
-                       m.cg = 1;
-                       m.cb = 1;
-                       m.ca = 1;
-                       m.tex[0] = R_GetTexture(alphaskytexture);
-                       m.texcoords[0] = &svert[0].st[0];
-                       m.texcoordstep[0] = sizeof(surfvert_t);
-                       speedscale = cl.time * (16.0/128.0);
-                       speedscale -= (int)speedscale;
-                       for (mesh = surf->mesh;mesh;mesh = mesh->chain)
-                       {
-                               m.numtriangles = mesh->numtriangles;
-                               m.numverts = mesh->numverts;
-                               m.index = mesh->index;
-                               for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
-                               {
-                                       softwaretransform(v->v, sv->v);
-                                       VectorSubtract (sv->v, r_origin, dir);
-                                       // flatten the sphere
-                                       dir[2] *= 3;
-
-                                       number = DotProduct(dir, dir);
-                                       #if SLOWMATH
-                                       length = 3.0f / sqrt(number);
-                                       #else
-                                       *((int *)&length) = 0x5f3759df - ((* (int *) &number) >> 1);
-                                       length = 3.0f * (length * (1.5f - (number * 0.5f * length * length)));
-                                       #endif
-
-                                       sv->st[0] = speedscale + dir[0] * length;
-                                       sv->st[1] = speedscale + dir[1] * length;
-                               }
-                               R_Mesh_Draw(&m);
-                       }
+                       c[0] = r;
+                       c[1] = g;
+                       c[2] = b;
+                       c[3] = a;
                }
        }
 }
 
-static int RSurf_Light(int *dlightbits, int numverts)
+static void RSurf_FogPassColors(const float *v, float *c, float r, float g, float b, float a, float colorscale, int numverts, const float *modelorg)
 {
-       float f;
-       int i, l, lit = false;
-       rdlight_t *rd;
-       vec3_t lightorigin;
-       surfvert_t *sv;
-       for (l = 0;l < r_numdlights;l++)
-       {
-               if (dlightbits[l >> 5] & (1 << (l & 31)))
-               {
-                       rd = &r_dlight[l];
-                       // FIXME: support softwareuntransform here and make bmodels use hardware transform?
-                       VectorCopy(rd->origin, lightorigin);
-                       for (i = 0, sv = svert;i < numverts;i++, sv++)
-                       {
-                               f = VectorDistance2(sv->v, lightorigin);
-                               if (f < rd->cullradius2)
-                               {
-                                       f = (1.0f / f) - rd->subtract;
-                                       sv->c[0] += rd->light[0] * f;
-                                       sv->c[1] += rd->light[1] * f;
-                                       sv->c[2] += rd->light[2] * f;
-                                       lit = true;
-                               }
-                       }
-               }
+       int i;
+       float diff[3], f;
+       r *= colorscale;
+       g *= colorscale;
+       b *= colorscale;
+       for (i = 0;i < numverts;i++, v += 4, c += 4)
+       {
+               VectorSubtract(v, modelorg, diff);
+               f = exp(fogdensity/DotProduct(diff, diff));
+               c[0] = r;
+               c[1] = g;
+               c[2] = b;
+               c[3] = a * f;
        }
-       return lit;
 }
 
-static int RSurf_LightSeparate(int *dlightbits, int numverts, float *vert, float *color)
+static void RSurf_ScaleColors(float *c, float scale, int numverts)
+{
+       int i;
+       if (scale != 1)
+               for (i = 0;i < numverts;i++, c += 4)
+                       VectorScale(c, scale, c);
+}
+
+static int RSurf_LightSeparate(const matrix4x4_t *matrix, const int *dlightbits, int numverts, const float *vert, float *color)
 {
-       float f, *v, *c;
+       float f;
+       const float *v;
+       float *c;
        int i, l, lit = false;
-       rdlight_t *rd;
+       const rdlight_t *rd;
        vec3_t lightorigin;
        for (l = 0;l < r_numdlights;l++)
        {
                if (dlightbits[l >> 5] & (1 << (l & 31)))
                {
                        rd = &r_dlight[l];
-                       // FIXME: support softwareuntransform here and make bmodels use hardware transform?
-                       VectorCopy(rd->origin, lightorigin);
+                       Matrix4x4_Transform(matrix, rd->origin, lightorigin);
                        for (i = 0, v = vert, c = color;i < numverts;i++, v += 4, c += 4)
                        {
                                f = VectorDistance2(v, lightorigin) + LIGHTOFFSET;
@@ -874,1902 +763,729 @@ static int RSurf_LightSeparate(int *dlightbits, int numverts, float *vert, float
 }
 
 // note: this untransforms lights to do the checking,
-// and takes surf->mesh->vertex data
-static int RSurf_LightCheck(int *dlightbits, surfmesh_t *mesh)
+// and takes surf->mesh->verts data
+static int RSurf_LightCheck(const matrix4x4_t *matrix, const int *dlightbits, const surfmesh_t *mesh)
 {
        int i, l;
-       rdlight_t *rd;
+       const rdlight_t *rd;
        vec3_t lightorigin;
-       surfvertex_t *sv;
+       const float *v;
        for (l = 0;l < r_numdlights;l++)
        {
                if (dlightbits[l >> 5] & (1 << (l & 31)))
                {
                        rd = &r_dlight[l];
-                       softwareuntransform(rd->origin, lightorigin);
-                       for (i = 0, sv = mesh->vertex;i < mesh->numverts;i++, sv++)
-                               if (VectorDistance2(sv->v, lightorigin) < rd->cullradius2)
+                       Matrix4x4_Transform(matrix, rd->origin, lightorigin);
+                       for (i = 0, v = mesh->verts;i < mesh->numverts;i++, v += 4)
+                               if (VectorDistance2(v, lightorigin) < rd->cullradius2)
                                        return true;
                }
        }
        return false;
 }
 
-static void RSurfShader_Water_Pass_Base(msurface_t *surf)
+static void RSurfShader_Sky(const entity_render_t *ent, const msurface_t *firstsurf)
 {
-#if 0
-       int i, size3;
-       surfvertex_t *v;
-       float *outv, *outc, *outst, *outuv, cl, ca, diff[3];
-       float base[3], scale, f;
-       qbyte *lm;
-       surfmesh_t *mesh;
+       const msurface_t *surf;
+       const surfmesh_t *mesh;
        rmeshbufferinfo_t m;
-       memset(&m, 0, sizeof(m));
-       if (currentrenderentity->effects & EF_ADDITIVE)
+
+       // LordHavoc: HalfLife maps have freaky skypolys...
+       if (ent->model->ishlbsp)
+               return;
+
+       if (skyrendernow)
        {
-               m.transparent = true;
-               m.blendfunc1 = GL_SRC_ALPHA;
-               m.blendfunc2 = GL_ONE;
+               skyrendernow = false;
+               if (skyrendermasked)
+                       R_Sky();
        }
-       else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
+
+       // draw depth-only polys
+       memset(&m, 0, sizeof(m));
+       if (skyrendermasked)
        {
-               m.transparent = true;
-               m.blendfunc1 = GL_SRC_ALPHA;
-               m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
+               m.blendfunc1 = GL_ZERO;
+               m.blendfunc2 = GL_ONE;
        }
        else
        {
-               m.transparent = false;
+               // fog sky
                m.blendfunc1 = GL_ONE;
                m.blendfunc2 = GL_ZERO;
        }
-       m.depthwrite = false;
-       m.depthdisable = false;
-       m.tex[0] = R_GetTexture(surf->currenttexture->texture);
-
-       size3 = ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3;
-
-       base[0] = base[1] = base[2] = currentrenderentity->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f) + surf->flags & SURF_LIGHTMAP ? 0 : ;
-
-       ca = currentrenderentity->alpha;
-       for (mesh = surf->mesh;mesh;mesh = mesh->chain)
+       m.depthwrite = true;
+       m.matrix = ent->matrix;
+       for (surf = firstsurf;surf;surf = surf->chain)
        {
-               m.numtriangles = mesh->numtriangles;
-               m.numverts = mesh->numverts;
-
-               if (R_Mesh_Draw_GetBuffer(&m))
+               for (mesh = surf->mesh;mesh;mesh = mesh->chain)
                {
-                       cl = m.colorscale;
-                       memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
-
-                       for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0], outuv = m.texcoords[1];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2, outuv += 2)
-                       {
-                               softwaretransform(v->v, outv);
-                               outv[3] = 1;
-                               VectorCopy(base, outc);
-                               outc[3] = ca;
-                               outst[0] = v->st[0];
-                               outst[1] = v->st[1];
-                       }
-
-                       if (surf->dlightframe == r_framecount)
-                               RSurf_LightSeparate(surf->dlightbits, m.numverts, m.vertex, m.color);
-
-                       for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color;i < m.numverts;i++, v++, outv += 4, outc += 4)
+                       m.numtriangles = mesh->numtriangles;
+                       m.numverts = mesh->numverts;
+                       if (R_Mesh_Draw_GetBuffer(&m, false))
                        {
-                               if (surf->flags & SURF_LIGHTMAP)
-                               if (surf->styles[0] != 255)
-                               {
-                                       lm = surf->samples + v->lightmapoffset;
-                                       scale = d_lightstylevalue[surf->styles[0]] * (1.0f / 32768.0f);
-                                       VectorMA(outc, scale, lm, outc);
-                                       if (surf->styles[1] != 255)
-                                       {
-                                               lm += size3;
-                                               scale = d_lightstylevalue[surf->styles[1]] * (1.0f / 32768.0f);
-                                               VectorMA(outc, scale, lm, outc);
-                                               if (surf->styles[2] != 255)
-                                               {
-                                                       lm += size3;
-                                                       scale = d_lightstylevalue[surf->styles[2]] * (1.0f / 32768.0f);
-                                                       VectorMA(outc, scale, lm, outc);
-                                                       if (surf->styles[3] != 255)
-                                                       {
-                                                               lm += size3;
-                                                               scale = d_lightstylevalue[surf->styles[3]] * (1.0f / 32768.0f);
-                                                               VectorMA(outc, scale, lm, outc);
-                                                       }
-                                               }
-                                       }
-                               }
-                               if (fogenabled)
-                               {
-                                       VectorSubtract(outv, r_origin, diff);
-                                       f = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
-                                       VectorScale(outc, f, outc);
-                               }
+                               memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
+                               memcpy(m.vertex, mesh->verts, m.numverts * sizeof(float[4]));
+                               if (skyrendermasked)
+                                       memset(m.color, 0, m.numverts * sizeof(float[4]));
                                else
-                                       VectorScale(outc, cl, outc);
+                                       R_FillColors(m.color, m.numverts, fogcolor[0] * m.colorscale, fogcolor[1] * m.colorscale, fogcolor[2] * m.colorscale, 1);
+                               R_Mesh_Render();
                        }
                }
        }
-       int i;
-       surfvertex_t *v;
-       float *outv, *outc, *outst, *outuv, cl, ca, diff[3];
-       surfmesh_t *mesh;
+}
+
+static void RSurfShader_Water_Callback(const void *calldata1, int calldata2)
+{
+       const entity_render_t *ent = calldata1;
+       const msurface_t *surf = ent->model->surfaces + calldata2;
+       float f;
+       const surfmesh_t *mesh;
        rmeshbufferinfo_t m;
+       float alpha = ent->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
+       float modelorg[3];
+       Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
+
        memset(&m, 0, sizeof(m));
-       if (currentrenderentity->effects & EF_ADDITIVE)
+       if (ent->effects & EF_ADDITIVE)
        {
-               m.transparent = true;
                m.blendfunc1 = GL_SRC_ALPHA;
                m.blendfunc2 = GL_ONE;
        }
-       else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
+       else if (surf->currenttexture->fogtexture != NULL || alpha < 1)
        {
-               m.transparent = true;
                m.blendfunc1 = GL_SRC_ALPHA;
                m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
        }
        else
        {
-               m.transparent = false;
                m.blendfunc1 = GL_ONE;
                m.blendfunc2 = GL_ZERO;
        }
-       m.depthwrite = false;
-       m.depthdisable = false;
        m.tex[0] = R_GetTexture(surf->currenttexture->texture);
-       m.tex[1] = R_GetTexture(surf->lightmaptexture);
-       ca = currentrenderentity->alpha;
+       m.matrix = ent->matrix;
        for (mesh = surf->mesh;mesh;mesh = mesh->chain)
        {
                m.numtriangles = mesh->numtriangles;
                m.numverts = mesh->numverts;
-
-               if (R_Mesh_Draw_GetBuffer(&m))
+               if (R_Mesh_Draw_GetBuffer(&m, true))
                {
-                       cl = (float) (1 << lightscalebit) * m.colorscale;
                        memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
-                       for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0], outuv = m.texcoords[1];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2, outuv += 2)
+                       memcpy(m.vertex, mesh->verts, m.numverts * sizeof(float[4]));
+                       memcpy(m.texcoords[0], mesh->st, m.numverts * sizeof(float[2]));
+                       f = surf->flags & SURF_DRAWFULLBRIGHT ? 1.0f : ((surf->flags & SURF_LIGHTMAP) ? 0 : 0.5f);
+                       R_FillColors(m.color, m.numverts, f, f, f, alpha);
+                       if (!(surf->flags & SURF_DRAWFULLBRIGHT || ent->effects & EF_FULLBRIGHT))
                        {
-                               softwaretransform(v->v, outv);
-                               if (r_waterripple.value)
-                                       outv[2] += r_waterripple.value * (1.0f / 64.0f) * turbsin[(int)((v->v[0]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255] * turbsin[(int)((v->v[1]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255];
-                               outv[3] = 1;
-                               if (fogenabled)
-                               {
-                                       VectorSubtract(outv, r_origin, diff);
-                                       outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
-                               }
-                               else
-                                       outc[0] = outc[1] = outc[2] = cl;
-                               outc[3] = ca;
-                               outst[0] = v->st[0];
-                               outst[1] = v->st[1];
-                               outuv[0] = v->uv[0];
-                               outuv[1] = v->uv[1];
+                               if (surf->dlightframe == r_framecount)
+                                       RSurf_LightSeparate(&ent->inversematrix, surf->dlightbits, m.numverts, m.vertex, m.color);
+                               if (surf->flags & SURF_LIGHTMAP)
+                                       RSurf_AddLightmapToVertexColors(mesh->lightmapoffsets, m.color, m.numverts, surf->samples, ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3, surf->styles);
                        }
+                       RSurf_FogColors(m.vertex, m.color, m.colorscale, m.numverts, modelorg);
+                       R_Mesh_Render();
                }
        }
-#else
-       int i;
-       float diff[3], alpha, ifog;
-       surfvertex_t *v;
-       surfvert_t *sv;
-       surfmesh_t *mesh;
-       rmeshinfo_t m;
-       alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
 
-       memset(&m, 0, sizeof(m));
-       if (alpha != 1 || surf->currenttexture->fogtexture != NULL)
+       if (fogenabled)
        {
-               m.transparent = true;
+               memset(&m, 0, sizeof(m));
                m.blendfunc1 = GL_SRC_ALPHA;
-               m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
-       }
-       else
-       {
-               m.transparent = false;
-               m.blendfunc1 = GL_ONE;
-               m.blendfunc2 = GL_ZERO;
-       }
-       m.vertex = &svert[0].v[0];
-       m.vertexstep = sizeof(surfvert_t);
-       m.color = &svert[0].c[0];
-       m.colorstep = sizeof(surfvert_t);
-       m.tex[0] = R_GetTexture(surf->currenttexture->texture);
-       m.texcoords[0] = &svert[0].st[0];
-       m.texcoordstep[0] = sizeof(surfvert_t);
-       for (mesh = surf->mesh;mesh;mesh = mesh->chain)
-       {
-               m.numtriangles = mesh->numtriangles;
-               m.numverts = mesh->numverts;
-               m.index = mesh->index;
-               for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
-               {
-                       softwaretransform(v->v, sv->v);
-                       if (r_waterripple.value)
-                               sv->v[2] += r_waterripple.value * (1.0f / 64.0f) * turbsin[(int)((v->v[0]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255] * turbsin[(int)((v->v[1]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255];
-                       if (surf->flags & SURF_DRAWFULLBRIGHT)
-                       {
-                               sv->c[0] = 1;
-                               sv->c[1] = 1;
-                               sv->c[2] = 1;
-                               sv->c[3] = alpha;
-                       }
-                       else
-                       {
-                               sv->c[0] = 0.5f;
-                               sv->c[1] = 0.5f;
-                               sv->c[2] = 0.5f;
-                               sv->c[3] = alpha;
-                       }
-                       sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
-                       sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
-               }
-               if (surf->dlightframe == r_framecount && !(surf->flags & SURF_DRAWFULLBRIGHT))
-                       RSurf_Light(surf->dlightbits, m.numverts);
-               if (fogenabled && (surf->flags & SURF_DRAWNOALPHA))
+               m.blendfunc2 = GL_ONE;
+               m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
+               m.matrix = ent->matrix;
+               for (mesh = surf->mesh;mesh;mesh = mesh->chain)
                {
-                       for (i = 0, sv = svert;i < m.numverts;i++, sv++)
+                       m.numtriangles = mesh->numtriangles;
+                       m.numverts = mesh->numverts;
+                       if (R_Mesh_Draw_GetBuffer(&m, false))
                        {
-                               VectorSubtract(sv->v, r_origin, diff);
-                               ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
-                               sv->c[0] *= ifog;
-                               sv->c[1] *= ifog;
-                               sv->c[2] *= ifog;
+                               memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
+                               memcpy(m.vertex, mesh->verts, m.numverts * sizeof(float[4]));
+                               if (m.tex[0])
+                                       memcpy(m.texcoords[0], mesh->st, m.numverts * sizeof(float[2]));
+                               RSurf_FogPassColors(m.vertex, m.color, fogcolor[0], fogcolor[1], fogcolor[2], alpha, m.colorscale, m.numverts, modelorg);
+                               R_Mesh_Render();
                        }
                }
-               R_Mesh_Draw(&m);
        }
-#endif
 }
 
-static void RSurfShader_Water_Pass_Fog(msurface_t *surf)
+static void RSurfShader_Water(const entity_render_t *ent, const msurface_t *firstsurf)
 {
-#if 0
-#else
-       int i;
-       float alpha;
-       surfvertex_t *v;
-       surfvert_t *sv;
-       surfmesh_t *mesh;
-       rmeshinfo_t m;
-       vec3_t diff;
-       alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
-
-       memset(&m, 0, sizeof(m));
-       m.transparent = alpha != 1 || surf->currenttexture->fogtexture != NULL;
-       m.blendfunc1 = GL_SRC_ALPHA;
-       m.blendfunc2 = GL_ONE;
-       m.vertex = &svert[0].v[0];
-       m.vertexstep = sizeof(surfvert_t);
-       m.color = &svert[0].c[0];
-       m.colorstep = sizeof(surfvert_t);
-       m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
-       m.texcoords[0] = &svert[0].st[0];
-       m.texcoordstep[0] = sizeof(surfvert_t);
-
-       for (mesh = surf->mesh;mesh;mesh = mesh->chain)
+       const msurface_t *surf;
+       vec3_t center;
+       for (surf = firstsurf;surf;surf = surf->chain)
        {
-               m.numtriangles = mesh->numtriangles;
-               m.numverts = mesh->numverts;
-               m.index = mesh->index;
-               for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
+               if ((r_wateralpha.value < 1 && !(surf->flags & SURF_DRAWNOALPHA)) || ent->effects & EF_ADDITIVE || surf->currenttexture->fogtexture)
                {
-                       softwaretransform(v->v, sv->v);
-                       if (r_waterripple.value)
-                               sv->v[2] += r_waterripple.value * (1.0f / 64.0f) * turbsin[(int)((v->v[0]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255] * turbsin[(int)((v->v[1]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255];
-                       if (m.tex[0])
-                       {
-                               sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
-                               sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
-                       }
-                       VectorSubtract(sv->v, r_origin, diff);
-                       sv->c[0] = fogcolor[0];
-                       sv->c[1] = fogcolor[1];
-                       sv->c[2] = fogcolor[2];
-                       sv->c[3] = alpha * exp(fogdensity/DotProduct(diff, diff));
+                       Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
+                       R_MeshQueue_AddTransparent(center, RSurfShader_Water_Callback, ent, surf - ent->model->surfaces);
                }
-               R_Mesh_Draw(&m);
+               else
+                       R_MeshQueue_Add(RSurfShader_Water_Callback, ent, surf - ent->model->surfaces);
        }
-#endif
-}
-
-static void RSurfShader_Water(msurface_t *firstsurf)
-{
-       msurface_t *surf;
-       for (surf = firstsurf;surf;surf = surf->chain)
-               RSurfShader_Water_Pass_Base(surf);
-       if (fogenabled)
-               for (surf = firstsurf;surf;surf = surf->chain)
-                       RSurfShader_Water_Pass_Fog(surf);
 }
 
-static void RSurfShader_Wall_Pass_BaseMTex(msurface_t *surf)
+static void RSurfShader_Wall_Pass_BaseVertex(const entity_render_t *ent, const msurface_t *surf)
 {
-#if 1
-       int i;
-       surfvertex_t *v;
-       float *outv, *outc, *outst, *outuv, cl, ca, diff[3];
-       surfmesh_t *mesh;
+       float base;
+       const surfmesh_t *mesh;
        rmeshbufferinfo_t m;
+       float modelorg[3];
+       Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
        memset(&m, 0, sizeof(m));
-       if (currentrenderentity->effects & EF_ADDITIVE)
+       if (ent->effects & EF_ADDITIVE)
        {
-               m.transparent = true;
                m.blendfunc1 = GL_SRC_ALPHA;
                m.blendfunc2 = GL_ONE;
        }
-       else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
+       else if (surf->currenttexture->fogtexture != NULL || ent->alpha < 1)
        {
-               m.transparent = true;
                m.blendfunc1 = GL_SRC_ALPHA;
                m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
        }
        else
        {
-               m.transparent = false;
                m.blendfunc1 = GL_ONE;
                m.blendfunc2 = GL_ZERO;
        }
-       m.depthwrite = false;
-       m.depthdisable = false;
        m.tex[0] = R_GetTexture(surf->currenttexture->texture);
-       m.tex[1] = R_GetTexture(surf->lightmaptexture);
-       ca = currentrenderentity->alpha;
+       base = ent->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f);
+       m.matrix = ent->matrix;
        for (mesh = surf->mesh;mesh;mesh = mesh->chain)
        {
                m.numtriangles = mesh->numtriangles;
                m.numverts = mesh->numverts;
-
-               if (R_Mesh_Draw_GetBuffer(&m))
+               if (R_Mesh_Draw_GetBuffer(&m, true))
                {
-                       cl = (float) (1 << lightscalebit) * m.colorscale;
                        memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
-                       if (fogenabled)
-                       {
-                               if (softwaretransform_complexity)
-                               {
-                                       for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0], outuv = m.texcoords[1];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2, outuv += 2)
-                                       {
-                                               softwaretransform(v->v, outv);
-                                               outv[3] = 1;
-                                               VectorSubtract(outv, r_origin, diff);
-                                               outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
-                                               outc[3] = ca;
-                                               outst[0] = v->st[0];
-                                               outst[1] = v->st[1];
-                                               outuv[0] = v->uv[0];
-                                               outuv[1] = v->uv[1];
-                                       }
-                               }
-                               else
-                               {
-                                       for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0], outuv = m.texcoords[1];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2, outuv += 2)
-                                       {
-                                               VectorCopy(v->v, outv);
-                                               outv[3] = 1;
-                                               VectorSubtract(outv, r_origin, diff);
-                                               outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
-                                               outc[3] = ca;
-                                               outst[0] = v->st[0];
-                                               outst[1] = v->st[1];
-                                               outuv[0] = v->uv[0];
-                                               outuv[1] = v->uv[1];
-                                       }
-                               }
-                       }
-                       else
+                       memcpy(m.vertex, mesh->verts, m.numverts * sizeof(float[4]));
+                       memcpy(m.texcoords[0], mesh->st, m.numverts * sizeof(float[2]));
+                       R_FillColors(m.color, m.numverts, base, base, base, ent->alpha);
+                       if (!(ent->effects & EF_FULLBRIGHT))
                        {
-                               if (softwaretransform_complexity)
-                               {
-                                       for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0], outuv = m.texcoords[1];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2, outuv += 2)
-                                       {
-                                               softwaretransform(v->v, outv);
-                                               outv[3] = 1;
-                                               outc[0] = outc[1] = outc[2] = cl;
-                                               outc[3] = ca;
-                                               outst[0] = v->st[0];
-                                               outst[1] = v->st[1];
-                                               outuv[0] = v->uv[0];
-                                               outuv[1] = v->uv[1];
-                                       }
-                               }
-                               else
-                               {
-                                       for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0], outuv = m.texcoords[1];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2, outuv += 2)
-                                       {
-                                               VectorCopy(v->v, outv);
-                                               outv[3] = 1;
-                                               outc[0] = outc[1] = outc[2] = cl;
-                                               outc[3] = ca;
-                                               outst[0] = v->st[0];
-                                               outst[1] = v->st[1];
-                                               outuv[0] = v->uv[0];
-                                               outuv[1] = v->uv[1];
-                                       }
-                               }
+                               if (surf->dlightframe == r_framecount)
+                                       RSurf_LightSeparate(&ent->inversematrix, surf->dlightbits, m.numverts, m.vertex, m.color);
+                               if (surf->flags & SURF_LIGHTMAP)
+                                       RSurf_AddLightmapToVertexColors(mesh->lightmapoffsets, m.color, m.numverts, surf->samples, ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3, surf->styles);
                        }
+                       RSurf_FogColors(m.vertex, m.color, m.colorscale, m.numverts, modelorg);
+                       R_Mesh_Render();
                }
        }
-#else
-       int i;
-       float diff[3], ifog;
-       surfvertex_t *v;
-       surfvert_t *sv;
-       surfmesh_t *mesh;
-       rmeshinfo_t m;
+}
 
+static void RSurfShader_Wall_Pass_BaseFullbright(const entity_render_t *ent, const msurface_t *surf)
+{
+       const surfmesh_t *mesh;
+       rmeshbufferinfo_t m;
+       float modelorg[3];
+       Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
        memset(&m, 0, sizeof(m));
-       if (currentrenderentity->effects & EF_ADDITIVE)
+       if (ent->effects & EF_ADDITIVE)
        {
-               m.transparent = true;
                m.blendfunc1 = GL_SRC_ALPHA;
                m.blendfunc2 = GL_ONE;
        }
-       else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
+       else if (surf->currenttexture->fogtexture != NULL || ent->alpha < 1)
        {
-               m.transparent = true;
                m.blendfunc1 = GL_SRC_ALPHA;
                m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
        }
        else
        {
-               m.transparent = false;
                m.blendfunc1 = GL_ONE;
                m.blendfunc2 = GL_ZERO;
        }
-       m.cr = m.cg = m.cb = (float) (1 << lightscalebit);
-       m.ca = currentrenderentity->alpha;
        m.tex[0] = R_GetTexture(surf->currenttexture->texture);
-       m.tex[1] = R_GetTexture(surf->lightmaptexture);
-       m.texcoordstep[0] = sizeof(surfvertex_t);
-       m.texcoordstep[1] = sizeof(surfvertex_t);
+       m.matrix = ent->matrix;
        for (mesh = surf->mesh;mesh;mesh = mesh->chain)
        {
                m.numtriangles = mesh->numtriangles;
                m.numverts = mesh->numverts;
-               m.index = mesh->index;
-               m.texcoords[0] = &mesh->vertex->st[0];
-               m.texcoords[1] = &mesh->vertex->uv[0];
-               if (fogenabled)
-               {
-                       m.color = &svert[0].c[0];
-                       m.colorstep = sizeof(surfvert_t);
-                       if (softwaretransform_complexity)
-                       {
-                               m.vertex = &svert[0].v[0];
-                               m.vertexstep = sizeof(surfvert_t);
-                               for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
-                               {
-                                       softwaretransform(v->v, sv->v);
-                                       VectorSubtract(sv->v, r_origin, diff);
-                                       ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
-                                       sv->c[0] = m.cr * ifog;
-                                       sv->c[1] = m.cg * ifog;
-                                       sv->c[2] = m.cb * ifog;
-                                       sv->c[3] = m.ca;
-                               }
-                       }
-                       else
-                       {
-                               m.vertex = &mesh->vertex->v[0];
-                               m.vertexstep = sizeof(surfvertex_t);
-                               for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
-                               {
-                                       VectorSubtract(v->v, r_origin, diff);
-                                       ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
-                                       sv->c[0] = m.cr * ifog;
-                                       sv->c[1] = m.cg * ifog;
-                                       sv->c[2] = m.cb * ifog;
-                                       sv->c[3] = m.ca;
-                               }
-                       }
-               }
-               else
+               if (R_Mesh_Draw_GetBuffer(&m, false))
                {
-                       if (softwaretransform_complexity)
-                       {
-                               m.vertex = &svert[0].v[0];
-                               m.vertexstep = sizeof(surfvert_t);
-                               for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
-                                       softwaretransform(v->v, sv->v);
-                       }
-                       else
-                       {
-                               m.vertex = &mesh->vertex->v[0];
-                               m.vertexstep = sizeof(surfvertex_t);
-                       }
+                       memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
+                       memcpy(m.vertex, mesh->verts, m.numverts * sizeof(float[4]));
+                       memcpy(m.texcoords[0], mesh->st, m.numverts * sizeof(float[2]));
+                       RSurf_FoggedColors(m.vertex, m.color, 1, 1, 1, ent->alpha, m.colorscale, m.numverts, modelorg);
+                       R_Mesh_Render();
                }
-               R_Mesh_Draw(&m);
        }
-#endif
 }
 
-static void RSurfShader_Wall_Pass_BaseTexture(msurface_t *surf)
+static void RSurfShader_Wall_Pass_Glow(const entity_render_t *ent, const msurface_t *surf)
 {
-#if 1
-       int i;
-       surfvertex_t *v;
-       float *outv, *outc, *outst, cl, ca;
-       surfmesh_t *mesh;
+       const surfmesh_t *mesh;
        rmeshbufferinfo_t m;
+       float modelorg[3];
+       Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
        memset(&m, 0, sizeof(m));
-       m.transparent = false;
-       m.blendfunc1 = GL_ONE;
-       m.blendfunc2 = GL_ZERO;
-       m.depthwrite = false;
-       m.depthdisable = false;
-       m.tex[0] = R_GetTexture(surf->currenttexture->texture);
-       ca = currentrenderentity->alpha;
+       m.blendfunc1 = GL_SRC_ALPHA;
+       m.blendfunc2 = GL_ONE;
+       m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
+       m.matrix = ent->matrix;
        for (mesh = surf->mesh;mesh;mesh = mesh->chain)
        {
                m.numtriangles = mesh->numtriangles;
                m.numverts = mesh->numverts;
-
-               if (R_Mesh_Draw_GetBuffer(&m))
+               if (R_Mesh_Draw_GetBuffer(&m, false))
                {
-                       cl = (float) (1 << lightscalebit) * m.colorscale;
                        memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
-                       if (softwaretransform_complexity)
-                       {
-                               for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
-                               {
-                                       softwaretransform(v->v, outv);
-                                       outv[3] = 1;
-                                       outc[0] = outc[1] = outc[2] = cl;
-                                       outc[3] = ca;
-                                       outst[0] = v->st[0];
-                                       outst[1] = v->st[1];
-                               }
-                       }
-                       else
-                       {
-                               for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
-                               {
-                                       VectorCopy(v->v, outv);
-                                       outv[3] = 1;
-                                       outc[0] = outc[1] = outc[2] = cl;
-                                       outc[3] = ca;
-                                       outst[0] = v->st[0];
-                                       outst[1] = v->st[1];
-                               }
-                       }
-               }
-       }
-#else
-       int i;
-       surfvertex_t *v;
-       surfvert_t *sv;
-       surfmesh_t *mesh;
-       rmeshinfo_t m;
-
-       memset(&m, 0, sizeof(m));
-       m.transparent = false;
-       m.blendfunc1 = GL_ONE;
-       m.blendfunc2 = GL_ZERO;
-       m.cr = m.cg = m.cb = (float) (1 << v_overbrightbits.integer);
-       m.ca = 1;
-       m.tex[0] = R_GetTexture(surf->currenttexture->texture);
-       m.texcoordstep[0] = sizeof(surfvertex_t);
-       for (mesh = surf->mesh;mesh;mesh = mesh->chain)
-       {
-               m.numtriangles = mesh->numtriangles;
-               m.numverts = mesh->numverts;
-               m.index = mesh->index;
-               m.texcoords[0] = &mesh->vertex->st[0];
-               if (softwaretransform_complexity)
-               {
-                       m.vertex = &svert[0].v[0];
-                       m.vertexstep = sizeof(surfvert_t);
-                       for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
-                               softwaretransform(v->v, sv->v);
-               }
-               else
-               {
-                       m.vertex = &mesh->vertex->v[0];
-                       m.vertexstep = sizeof(surfvertex_t);
+                       memcpy(m.vertex, mesh->verts, m.numverts * sizeof(float[4]));
+                       memcpy(m.texcoords[0], mesh->st, m.numverts * sizeof(float[2]));
+                       RSurf_FoggedColors(m.vertex, m.color, 1, 1, 1, ent->alpha, m.colorscale, m.numverts, modelorg);
+                       R_Mesh_Render();
                }
-               R_Mesh_Draw(&m);
        }
-#endif
 }
 
-static void RSurfShader_Wall_Pass_BaseLightmap(msurface_t *surf)
+static void RSurfShader_Wall_Pass_Fog(const entity_render_t *ent, const msurface_t *surf)
 {
-#if 1
-       int i;
-       surfvertex_t *v;
-       float *outv, *outc, *outuv, cl, ca, diff[3];
-       surfmesh_t *mesh;
+       const surfmesh_t *mesh;
        rmeshbufferinfo_t m;
+       float modelorg[3];
+       Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
        memset(&m, 0, sizeof(m));
-       m.transparent = false;
-       m.blendfunc1 = GL_ZERO;
-       m.blendfunc2 = GL_SRC_COLOR;
-       m.depthwrite = false;
-       m.depthdisable = false;
-       m.tex[0] = R_GetTexture(surf->lightmaptexture);
-       ca = currentrenderentity->alpha;
+       m.blendfunc1 = GL_SRC_ALPHA;
+       m.blendfunc2 = GL_ONE;
+       m.matrix = ent->matrix;
+       m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
        for (mesh = surf->mesh;mesh;mesh = mesh->chain)
        {
                m.numtriangles = mesh->numtriangles;
                m.numverts = mesh->numverts;
-
-               if (R_Mesh_Draw_GetBuffer(&m))
+               if (R_Mesh_Draw_GetBuffer(&m, false))
                {
-                       cl = (float) (1 << lightscalebit) * m.colorscale;
                        memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
-                       if (fogenabled)
-                       {
-                               if (softwaretransform_complexity)
-                               {
-                                       for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outuv = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outuv += 2)
-                                       {
-                                               softwaretransform(v->v, outv);
-                                               outv[3] = 1;
-                                               VectorSubtract(outv, r_origin, diff);
-                                               outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
-                                               outc[3] = ca;
-                                               outuv[0] = v->uv[0];
-                                               outuv[1] = v->uv[1];
-                                       }
-                               }
-                               else
-                               {
-                                       for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outuv = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outuv += 2)
-                                       {
-                                               VectorCopy(v->v, outv);
-                                               outv[3] = 1;
-                                               VectorSubtract(outv, r_origin, diff);
-                                               outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
-                                               outc[3] = ca;
-                                               outuv[0] = v->uv[0];
-                                               outuv[1] = v->uv[1];
-                                       }
-                               }
-                       }
-                       else
-                       {
-                               if (softwaretransform_complexity)
-                               {
-                                       for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outuv = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outuv += 2)
-                                       {
-                                               softwaretransform(v->v, outv);
-                                               outv[3] = 1;
-                                               outc[0] = outc[1] = outc[2] = cl;
-                                               outc[3] = ca;
-                                               outuv[0] = v->uv[0];
-                                               outuv[1] = v->uv[1];
-                                       }
-                               }
-                               else
-                               {
-                                       for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outuv = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outuv += 2)
-                                       {
-                                               VectorCopy(v->v, outv);
-                                               outv[3] = 1;
-                                               outc[0] = outc[1] = outc[2] = cl;
-                                               outc[3] = ca;
-                                               outuv[0] = v->uv[0];
-                                               outuv[1] = v->uv[1];
-                                       }
-                               }
-                       }
-               }
-       }
-#else
-       int i;
-       float diff[3], ifog;
-       surfvertex_t *v;
-       surfvert_t *sv;
-       surfmesh_t *mesh;
-       rmeshinfo_t m;
-
-       memset(&m, 0, sizeof(m));
-       m.transparent = false;
-       m.blendfunc1 = GL_ZERO;
-       m.blendfunc2 = GL_SRC_COLOR;
-       m.cr = m.cg = m.cb = (float) (1 << v_overbrightbits.integer);
-       m.ca = 1;
-       m.tex[0] = R_GetTexture(surf->lightmaptexture);
-       m.texcoordstep[0] = sizeof(surfvertex_t);
-       for (mesh = surf->mesh;mesh;mesh = mesh->chain)
-       {
-               m.numtriangles = mesh->numtriangles;
-               m.numverts = mesh->numverts;
-               m.index = mesh->index;
-               m.texcoords[0] = &mesh->vertex->uv[0];
-               if (fogenabled)
-               {
-                       m.color = &svert[0].c[0];
-                       m.colorstep = sizeof(surfvert_t);
-                       if (softwaretransform_complexity)
-                       {
-                               m.vertex = &svert[0].v[0];
-                               m.vertexstep = sizeof(surfvert_t);
-                               for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
-                               {
-                                       softwaretransform(v->v, sv->v);
-                                       VectorSubtract(sv->v, r_origin, diff);
-                                       ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
-                                       sv->c[0] = m.cr * ifog;
-                                       sv->c[1] = m.cg * ifog;
-                                       sv->c[2] = m.cb * ifog;
-                                       sv->c[3] = m.ca;
-                               }
-                       }
-                       else
-                       {
-                               m.vertex = &mesh->vertex->v[0];
-                               m.vertexstep = sizeof(surfvertex_t);
-                               for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
-                               {
-                                       VectorSubtract(v->v, r_origin, diff);
-                                       ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
-                                       sv->c[0] = m.cr * ifog;
-                                       sv->c[1] = m.cg * ifog;
-                                       sv->c[2] = m.cb * ifog;
-                                       sv->c[3] = m.ca;
-                               }
-                       }
-               }
-               else
-               {
-                       if (softwaretransform_complexity)
-                       {
-                               m.vertex = &svert[0].v[0];
-                               m.vertexstep = sizeof(surfvert_t);
-                               for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
-                                       softwaretransform(v->v, sv->v);
-                       }
-                       else
-                       {
-                               m.vertex = &mesh->vertex->v[0];
-                               m.vertexstep = sizeof(surfvertex_t);
-                       }
+                       memcpy(m.vertex, mesh->verts, m.numverts * sizeof(float[4]));
+                       if (m.tex[0])
+                               memcpy(m.texcoords[0], mesh->st, m.numverts * sizeof(float[2]));
+                       RSurf_FogPassColors(m.vertex, m.color, fogcolor[0], fogcolor[1], fogcolor[2], ent->alpha, m.colorscale, m.numverts, modelorg);
+                       R_Mesh_Render();
                }
-               R_Mesh_Draw(&m);
        }
-#endif
 }
 
-static void RSurfShader_Wall_Pass_BaseVertex(msurface_t *surf)
+static void RSurfShader_OpaqueWall_Pass_TripleTexCombine(const entity_render_t *ent, const msurface_t *surf)
 {
-#if 1
-       int i, size3;
-       surfvertex_t *v;
-       float *outv, *outc, *outst, *outuv, cl, ca, diff[3];
-       float base[3], scale, f;
-       qbyte *lm;
-       surfmesh_t *mesh;
-       rmeshbufferinfo_t m;
-       memset(&m, 0, sizeof(m));
-       if (currentrenderentity->effects & EF_ADDITIVE)
-       {
-               m.transparent = true;
-               m.blendfunc1 = GL_SRC_ALPHA;
-               m.blendfunc2 = GL_ONE;
-       }
-       else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
-       {
-               m.transparent = true;
-               m.blendfunc1 = GL_SRC_ALPHA;
-               m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
-       }
-       else
-       {
-               m.transparent = false;
-               m.blendfunc1 = GL_ONE;
-               m.blendfunc2 = GL_ZERO;
-       }
-       m.depthwrite = false;
-       m.depthdisable = false;
+       const surfmesh_t *mesh;
+       static rmeshbufferinfo_t m;
+       float cl;
+       //memset(&m, 0, sizeof(m));
+       m.blendfunc1 = GL_ONE;
+       m.blendfunc2 = GL_ZERO;
        m.tex[0] = R_GetTexture(surf->currenttexture->texture);
-
-       size3 = ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3;
-
-       base[0] = base[1] = base[2] = currentrenderentity->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f);
-
-       ca = currentrenderentity->alpha;
+       m.texrgbscale[0] = 1.0f;
+       m.tex[1] = R_GetTexture(surf->lightmaptexture);
+       m.texrgbscale[1] = 4.0f;
+       m.tex[2] = R_GetTexture(surf->currenttexture->detailtexture);
+       m.texrgbscale[2] = 2.0f;
+       m.matrix = ent->matrix;
        for (mesh = surf->mesh;mesh;mesh = mesh->chain)
        {
                m.numtriangles = mesh->numtriangles;
                m.numverts = mesh->numverts;
-
-               if (R_Mesh_Draw_GetBuffer(&m))
+               if (R_Mesh_Draw_GetBuffer(&m, false))
                {
-                       cl = m.colorscale;
                        memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
-
-                       if (currentrenderentity->effects & EF_FULLBRIGHT)
-                       {
-                               if (softwaretransform_complexity)
-                               {
-                                       for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0], outuv = m.texcoords[1];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2, outuv += 2)
-                                       {
-                                               softwaretransform(v->v, outv);
-                                               outv[3] = 1;
-                                               VectorSubtract(outv, r_origin, diff);
-                                               outc[0] = outc[1] = outc[2] = 2.0f * cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
-                                               outc[3] = ca;
-                                               outst[0] = v->st[0];
-                                               outst[1] = v->st[1];
-                                       }
-                               }
-                               else
-                               {
-                                       for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0], outuv = m.texcoords[1];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2, outuv += 2)
-                                       {
-                                               VectorCopy(v->v, outv);
-                                               outv[3] = 1;
-                                               VectorSubtract(outv, r_origin, diff);
-                                               outc[0] = outc[1] = outc[2] = 2.0f * cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
-                                               outc[3] = ca;
-                                               outst[0] = v->st[0];
-                                               outst[1] = v->st[1];
-                                       }
-                               }
-                       }
-                       else
-                       {
-                               if (softwaretransform_complexity)
-                               {
-                                       for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0], outuv = m.texcoords[1];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2, outuv += 2)
-                                       {
-                                               softwaretransform(v->v, outv);
-                                               outv[3] = 1;
-                                               VectorCopy(base, outc);
-                                               outc[3] = ca;
-                                               outst[0] = v->st[0];
-                                               outst[1] = v->st[1];
-                                       }
-                               }
-                               else
-                               {
-                                       for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0], outuv = m.texcoords[1];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2, outuv += 2)
-                                       {
-                                               VectorCopy(v->v, outv);
-                                               outv[3] = 1;
-                                               VectorCopy(base, outc);
-                                               outc[3] = ca;
-                                               outst[0] = v->st[0];
-                                               outst[1] = v->st[1];
-                                       }
-                               }
-
-                               if (surf->dlightframe == r_framecount)
-                                       RSurf_LightSeparate(surf->dlightbits, m.numverts, m.vertex, m.color);
-
-                               for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color;i < m.numverts;i++, v++, outv += 4, outc += 4)
-                               {
-                                       if (surf->styles[0] != 255)
-                                       {
-                                               lm = surf->samples + v->lightmapoffset;
-                                               scale = d_lightstylevalue[surf->styles[0]] * (1.0f / 32768.0f);
-                                               VectorMA(outc, scale, lm, outc);
-                                               if (surf->styles[1] != 255)
-                                               {
-                                                       lm += size3;
-                                                       scale = d_lightstylevalue[surf->styles[1]] * (1.0f / 32768.0f);
-                                                       VectorMA(outc, scale, lm, outc);
-                                                       if (surf->styles[2] != 255)
-                                                       {
-                                                               lm += size3;
-                                                               scale = d_lightstylevalue[surf->styles[2]] * (1.0f / 32768.0f);
-                                                               VectorMA(outc, scale, lm, outc);
-                                                               if (surf->styles[3] != 255)
-                                                               {
-                                                                       lm += size3;
-                                                                       scale = d_lightstylevalue[surf->styles[3]] * (1.0f / 32768.0f);
-                                                                       VectorMA(outc, scale, lm, outc);
-                                                               }
-                                                       }
-                                               }
-                                       }
-                                       if (fogenabled)
-                                       {
-                                               VectorSubtract(outv, r_origin, diff);
-                                               f = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
-                                               VectorScale(outc, f, outc);
-                                       }
-                                       else
-                                               VectorScale(outc, cl, outc);
-                               }
-                       }
+                       memcpy(m.vertex, mesh->verts, m.numverts * sizeof(float[4]));
+                       memcpy(m.texcoords[0], mesh->st, m.numverts * sizeof(float[2]));
+                       memcpy(m.texcoords[1], mesh->uv, m.numverts * sizeof(float[2]));
+                       memcpy(m.texcoords[2], mesh->ab, m.numverts * sizeof(float[2]));
+                       cl = (float) (1 << lightscalebit) * m.colorscale;
+                       R_FillColors(m.color, m.numverts, cl, cl, cl, 1);
+                       R_Mesh_Render();
                }
        }
-#else
-       int i, size3;
-       float c[3], base[3], scale, diff[3], ifog;
-       surfvertex_t *v;
-       surfvert_t *sv;
-       surfmesh_t *mesh;
-       rmeshinfo_t m;
-       qbyte *lm;
-
-       size3 = ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3;
-
-       base[0] = base[1] = base[2] = currentrenderentity->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 128.0f);
+}
 
+static void RSurfShader_OpaqueWall_Pass_BaseMTex(const entity_render_t *ent, const msurface_t *surf)
+{
+       const surfmesh_t *mesh;
+       rmeshbufferinfo_t m;
+       float cl;
        memset(&m, 0, sizeof(m));
-       if (currentrenderentity->effects & EF_ADDITIVE)
-       {
-               m.transparent = true;
-               m.blendfunc1 = GL_SRC_ALPHA;
-               m.blendfunc2 = GL_ONE;
-       }
-       else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
-       {
-               m.transparent = true;
-               m.blendfunc1 = GL_SRC_ALPHA;
-               m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
-       }
-       else
-       {
-               m.transparent = false;
-               m.blendfunc1 = GL_ONE;
-               m.blendfunc2 = GL_ZERO;
-       }
-       m.vertex = &svert[0].v[0];
-       m.vertexstep = sizeof(surfvert_t);
-       m.color = &svert[0].c[0];
-       m.colorstep = sizeof(surfvert_t);
+       m.blendfunc1 = GL_ONE;
+       m.blendfunc2 = GL_ZERO;
        m.tex[0] = R_GetTexture(surf->currenttexture->texture);
-       m.texcoordstep[0] = sizeof(surfvertex_t);
+       m.tex[1] = R_GetTexture(surf->lightmaptexture);
+       m.matrix = ent->matrix;
        for (mesh = surf->mesh;mesh;mesh = mesh->chain)
        {
                m.numtriangles = mesh->numtriangles;
                m.numverts = mesh->numverts;
-               m.index = mesh->index;
-               m.texcoords[0] = &mesh->vertex->st[0];
-               for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
-               {
-                       softwaretransform(v->v, sv->v);
-                       VectorCopy(base, c);
-                       if (surf->styles[0] != 255)
-                       {
-                               lm = surf->samples + v->lightmapoffset;
-                               scale = d_lightstylevalue[surf->styles[0]] * (1.0f / 32768.0f);
-                               VectorMA(c, scale, lm, c);
-                               if (surf->styles[1] != 255)
-                               {
-                                       lm += size3;
-                                       scale = d_lightstylevalue[surf->styles[1]] * (1.0f / 32768.0f);
-                                       VectorMA(c, scale, lm, c);
-                                       if (surf->styles[2] != 255)
-                                       {
-                                               lm += size3;
-                                               scale = d_lightstylevalue[surf->styles[2]] * (1.0f / 32768.0f);
-                                               VectorMA(c, scale, lm, c);
-                                               if (surf->styles[3] != 255)
-                                               {
-                                                       lm += size3;
-                                                       scale = d_lightstylevalue[surf->styles[3]] * (1.0f / 32768.0f);
-                                                       VectorMA(c, scale, lm, c);
-                                               }
-                                       }
-                               }
-                       }
-                       sv->c[0] = c[0];
-                       sv->c[1] = c[1];
-                       sv->c[2] = c[2];
-                       sv->c[3] = currentrenderentity->alpha;
-               }
-               if (surf->dlightframe == r_framecount)
-                       RSurf_Light(surf->dlightbits, m.numverts);
-               if (fogenabled)
+               if (R_Mesh_Draw_GetBuffer(&m, true))
                {
-                       for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
-                       {
-                               VectorSubtract(sv->v, r_origin, diff);
-                               ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
-                               sv->c[0] *= ifog;
-                               sv->c[1] *= ifog;
-                               sv->c[2] *= ifog;
-                       }
+                       memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
+                       memcpy(m.vertex, mesh->verts, m.numverts * sizeof(float[4]));
+                       memcpy(m.texcoords[0], mesh->st, m.numverts * sizeof(float[2]));
+                       memcpy(m.texcoords[1], mesh->uv, m.numverts * sizeof(float[2]));
+                       cl = (float) (1 << lightscalebit) * m.colorscale;
+                       R_FillColors(m.color, m.numverts, cl, cl, cl, 1);
+                       R_Mesh_Render();
                }
-               R_Mesh_Draw(&m);
        }
-#endif
 }
 
-static void RSurfShader_Wall_Pass_BaseFullbright(msurface_t *surf)
+static void RSurfShader_OpaqueWall_Pass_BaseTexture(const entity_render_t *ent, const msurface_t *surf)
 {
-#if 1
-       int i;
-       surfvertex_t *v;
-       float *outv, *outc, *outst, cl, ca, diff[3];
-       surfmesh_t *mesh;
+       const surfmesh_t *mesh;
        rmeshbufferinfo_t m;
+       float cl;
        memset(&m, 0, sizeof(m));
-       if (currentrenderentity->effects & EF_ADDITIVE)
-       {
-               m.transparent = true;
-               m.blendfunc1 = GL_SRC_ALPHA;
-               m.blendfunc2 = GL_ONE;
-       }
-       else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
-       {
-               m.transparent = true;
-               m.blendfunc1 = GL_SRC_ALPHA;
-               m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
-       }
-       else
-       {
-               m.transparent = false;
-               m.blendfunc1 = GL_ONE;
-               m.blendfunc2 = GL_ZERO;
-       }
-       m.depthwrite = false;
-       m.depthdisable = false;
+       m.blendfunc1 = GL_ONE;
+       m.blendfunc2 = GL_ZERO;
        m.tex[0] = R_GetTexture(surf->currenttexture->texture);
-       ca = currentrenderentity->alpha;
+       m.matrix = ent->matrix;
        for (mesh = surf->mesh;mesh;mesh = mesh->chain)
        {
                m.numtriangles = mesh->numtriangles;
                m.numverts = mesh->numverts;
-
-               if (R_Mesh_Draw_GetBuffer(&m))
+               if (R_Mesh_Draw_GetBuffer(&m, false))
                {
-                       cl = m.colorscale;
                        memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
-                       if (fogenabled)
-                       {
-                               if (softwaretransform_complexity)
-                               {
-                                       for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
-                                       {
-                                               softwaretransform(v->v, outv);
-                                               outv[3] = 1;
-                                               VectorSubtract(outv, r_origin, diff);
-                                               outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
-                                               outc[3] = ca;
-                                               outst[0] = v->st[0];
-                                               outst[1] = v->st[1];
-                                       }
-                               }
-                               else
-                               {
-                                       for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
-                                       {
-                                               VectorCopy(v->v, outv);
-                                               outv[3] = 1;
-                                               VectorSubtract(outv, r_origin, diff);
-                                               outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
-                                               outc[3] = ca;
-                                               outst[0] = v->st[0];
-                                               outst[1] = v->st[1];
-                                       }
-                               }
-                       }
-                       else
-                       {
-                               if (softwaretransform_complexity)
-                               {
-                                       for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
-                                       {
-                                               softwaretransform(v->v, outv);
-                                               outv[3] = 1;
-                                               outc[0] = outc[1] = outc[2] = cl;
-                                               outc[3] = ca;
-                                               outst[0] = v->st[0];
-                                               outst[1] = v->st[1];
-                                       }
-                               }
-                               else
-                               {
-                                       for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
-                                       {
-                                               VectorCopy(v->v, outv);
-                                               outv[3] = 1;
-                                               outc[0] = outc[1] = outc[2] = cl;
-                                               outc[3] = ca;
-                                               outst[0] = v->st[0];
-                                               outst[1] = v->st[1];
-                                       }
-                               }
-                       }
+                       memcpy(m.vertex, mesh->verts, m.numverts * sizeof(float[4]));
+                       memcpy(m.texcoords[0], mesh->st, m.numverts * sizeof(float[2]));
+                       cl = m.colorscale;
+                       R_FillColors(m.color, m.numverts, cl, cl, cl, 1);
+                       R_Mesh_Render();
                }
        }
-#else
-       int i;
-       float diff[3], ifog;
-       surfvertex_t *v;
-       surfvert_t *sv;
-       surfmesh_t *mesh;
-       rmeshinfo_t m;
+}
 
+static void RSurfShader_OpaqueWall_Pass_BaseLightmap(const entity_render_t *ent, const msurface_t *surf)
+{
+       const surfmesh_t *mesh;
+       rmeshbufferinfo_t m;
+       float cl;
        memset(&m, 0, sizeof(m));
-       if (currentrenderentity->effects & EF_ADDITIVE)
-       {
-               m.transparent = true;
-               m.blendfunc1 = GL_SRC_ALPHA;
-               m.blendfunc2 = GL_ONE;
-       }
-       else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
-       {
-               m.transparent = true;
-               m.blendfunc1 = GL_SRC_ALPHA;
-               m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
-       }
-       else
-       {
-               m.transparent = false;
-               m.blendfunc1 = GL_ONE;
-               m.blendfunc2 = GL_ZERO;
-       }
-       m.vertex = &svert[0].v[0];
-       m.vertexstep = sizeof(surfvert_t);
-       m.tex[0] = R_GetTexture(surf->currenttexture->texture);
-       m.texcoordstep[0] = sizeof(surfvertex_t);
+       m.blendfunc1 = GL_ZERO;
+       m.blendfunc2 = GL_SRC_COLOR;
+       m.tex[0] = R_GetTexture(surf->lightmaptexture);
+       m.matrix = ent->matrix;
        for (mesh = surf->mesh;mesh;mesh = mesh->chain)
        {
                m.numtriangles = mesh->numtriangles;
                m.numverts = mesh->numverts;
-               m.index = mesh->index;
-               m.texcoords[0] = &mesh->vertex->st[0];
-               if (fogenabled)
+               if (R_Mesh_Draw_GetBuffer(&m, true))
                {
-                       m.color = &svert[0].c[0];
-                       m.colorstep = sizeof(surfvert_t);
-                       for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
-                       {
-                               softwaretransform(v->v, sv->v);
-                               VectorSubtract(sv->v, r_origin, diff);
-                               ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
-                               sv->c[0] = ifog;
-                               sv->c[1] = ifog;
-                               sv->c[2] = ifog;
-                               sv->c[3] = currentrenderentity->alpha;
-                       }
-               }
-               else
-               {
-                       m.cr = m.cg = m.cb = 1;
-                       m.ca = currentrenderentity->alpha;
-                       for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
-                               softwaretransform(v->v, sv->v);
+                       memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
+                       memcpy(m.vertex, mesh->verts, m.numverts * sizeof(float[4]));
+                       memcpy(m.texcoords[0], mesh->uv, m.numverts * sizeof(float[2]));
+                       cl = (float) (1 << lightscalebit) * m.colorscale;
+                       R_FillColors(m.color, m.numverts, cl, cl, cl, 1);
+                       R_Mesh_Render();
                }
-               R_Mesh_Draw(&m);
        }
-#endif
 }
 
-static void RSurfShader_Wall_Pass_Light(msurface_t *surf)
+static void RSurfShader_OpaqueWall_Pass_Light(const entity_render_t *ent, const msurface_t *surf)
 {
-#if 1
-       int i;
-       surfvertex_t *v;
-       float *outv, *outc, *outst, *outuv, cl, ca, diff[3], f;
-       surfmesh_t *mesh;
+       const surfmesh_t *mesh;
        rmeshbufferinfo_t m;
 
        if (surf->dlightframe != r_framecount)
                return;
-       if (currentrenderentity->effects & EF_FULLBRIGHT)
+       if (ent->effects & EF_FULLBRIGHT)
                return;
 
        memset(&m, 0, sizeof(m));
-       if (currentrenderentity->effects & EF_ADDITIVE)
-       {
-               m.transparent = true;
-               m.blendfunc1 = GL_SRC_ALPHA;
-               m.blendfunc2 = GL_ONE;
-       }
-       else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
-       {
-               m.transparent = true;
-               m.blendfunc1 = GL_SRC_ALPHA;
-               m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
-       }
-       else
-       {
-               m.transparent = false;
-               m.blendfunc1 = GL_ONE;
-               m.blendfunc2 = GL_ZERO;
-       }
-       m.depthwrite = false;
-       m.depthdisable = false;
+       m.blendfunc1 = GL_SRC_ALPHA;
+       m.blendfunc2 = GL_ONE;
        m.tex[0] = R_GetTexture(surf->currenttexture->texture);
-       ca = currentrenderentity->alpha;
+       m.matrix = ent->matrix;
        for (mesh = surf->mesh;mesh;mesh = mesh->chain)
        {
-               if (RSurf_LightCheck(surf->dlightbits, mesh))
+               if (RSurf_LightCheck(&ent->inversematrix, surf->dlightbits, mesh))
                {
                        m.numtriangles = mesh->numtriangles;
                        m.numverts = mesh->numverts;
-
-                       if (R_Mesh_Draw_GetBuffer(&m))
+                       if (R_Mesh_Draw_GetBuffer(&m, true))
                        {
-                               cl = m.colorscale;
                                memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
-                               for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0], outuv = m.texcoords[1];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2, outuv += 2)
-                               {
-                                       softwaretransform(v->v, outv);
-                                       outv[3] = 1;
-                                       VectorClear(outc);
-                                       outc[3] = ca;
-                                       outst[0] = v->st[0];
-                                       outst[1] = v->st[1];
-                               }
-                               RSurf_LightSeparate(surf->dlightbits, m.numverts, m.vertex, m.color);
-                               if (fogenabled)
-                               {
-                                       for (i = 0, outv = m.vertex, outc = m.color;i < m.numverts;i++, outv += 4, outc += 4)
-                                       {
-                                               VectorSubtract(outv, r_origin, diff);
-                                               f = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
-                                               VectorScale(outc, f, outc);
-                                       }
-                               }
-                               else if (cl != 1)
-                                       for (i = 0, outc = m.color;i < m.numverts;i++, outc += 4)
-                                               VectorScale(outc, cl, outc);
-                       }
-               }
-       }
-#else
-       int i;
-       float diff[3], ifog;
-       surfvertex_t *v;
-       surfvert_t *sv;
-       surfmesh_t *mesh;
-       rmeshinfo_t m;
-
-       memset(&m, 0, sizeof(m));
-       if (currentrenderentity->effects & EF_ADDITIVE)
-               m.transparent = true;
-       else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
-               m.transparent = true;
-       else
-               m.transparent = false;
-       m.blendfunc1 = GL_SRC_ALPHA;
-       m.blendfunc2 = GL_ONE;
-       m.vertex = &svert[0].v[0];
-       m.vertexstep = sizeof(surfvert_t);
-       m.color = &svert[0].c[0];
-       m.colorstep = sizeof(surfvert_t);
-       m.tex[0] = R_GetTexture(surf->currenttexture->texture);
-       m.texcoordstep[0] = sizeof(surfvertex_t);
-       for (mesh = surf->mesh;mesh;mesh = mesh->chain)
-       {
-               m.numtriangles = mesh->numtriangles;
-               m.numverts = mesh->numverts;
-               m.index = mesh->index;
-               m.texcoords[0] = &mesh->vertex->st[0];
-               for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
-               {
-                       softwaretransform(v->v, sv->v);
-                       sv->c[0] = 0;
-                       sv->c[1] = 0;
-                       sv->c[2] = 0;
-                       sv->c[3] = currentrenderentity->alpha;
-               }
-               if (RSurf_Light(surf->dlightbits, m.numverts))
-               {
-                       if (fogenabled)
-                       {
-                               for (i = 0, sv = svert;i < m.numverts;i++, sv++)
-                               {
-                                       VectorSubtract(sv->v, r_origin, diff);
-                                       ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
-                                       sv->c[0] *= ifog;
-                                       sv->c[1] *= ifog;
-                                       sv->c[2] *= ifog;
-                               }
+                               memcpy(m.vertex, mesh->verts, m.numverts * sizeof(float[4]));
+                               memcpy(m.texcoords[0], mesh->st, m.numverts * sizeof(float[2]));
+                               R_FillColors(m.color, m.numverts, 0, 0, 0, 1);
+                               RSurf_LightSeparate(&ent->inversematrix, surf->dlightbits, m.numverts, m.vertex, m.color);
+                               RSurf_ScaleColors(m.color, m.colorscale, m.numverts);
+                               R_Mesh_Render();
                        }
-                       R_Mesh_Draw(&m);
                }
        }
-#endif
 }
 
-static void RSurfShader_Wall_Pass_Glow(msurface_t *surf)
+static void RSurfShader_OpaqueWall_Pass_Fog(const entity_render_t *ent, const msurface_t *surf)
 {
-#if 1
-       int i;
-       surfvertex_t *v;
-       float *outv, *outc, *outst, cl, ca, diff[3];
-       surfmesh_t *mesh;
+       const surfmesh_t *mesh;
        rmeshbufferinfo_t m;
+       float modelorg[3];
+       Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
        memset(&m, 0, sizeof(m));
-       m.transparent = currentrenderentity->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1;
        m.blendfunc1 = GL_SRC_ALPHA;
-       m.blendfunc2 = GL_ONE;
-       m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
-       ca = currentrenderentity->alpha;
+       m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
+       m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
+       m.matrix = ent->matrix;
        for (mesh = surf->mesh;mesh;mesh = mesh->chain)
        {
                m.numtriangles = mesh->numtriangles;
                m.numverts = mesh->numverts;
-
-               if (R_Mesh_Draw_GetBuffer(&m))
+               if (R_Mesh_Draw_GetBuffer(&m, false))
                {
-                       cl = m.colorscale;
                        memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
-                       if (fogenabled)
-                       {
-                               for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
-                               {
-                                       softwaretransform(v->v, outv);
-                                       outv[3] = 1;
-                                       VectorSubtract(outv, r_origin, diff);
-                                       outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
-                                       outc[3] = ca;
-                                       outst[0] = v->st[0];
-                                       outst[1] = v->st[1];
-                               }
-                       }
-                       else
-                       {
-                               for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
-                               {
-                                       softwaretransform(v->v, outv);
-                                       outv[3] = 1;
-                                       outc[0] = outc[1] = outc[2] = cl;
-                                       outc[3] = ca;
-                                       outst[0] = v->st[0];
-                                       outst[1] = v->st[1];
-                               }
-                       }
-               }
-       }
-#else
-       int i;
-       float diff[3], ifog;
-       surfvertex_t *v;
-       surfvert_t *sv;
-       surfmesh_t *mesh;
-       rmeshinfo_t m;
-
-       memset(&m, 0, sizeof(m));
-       m.transparent = currentrenderentity->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1;
-       m.blendfunc1 = GL_SRC_ALPHA;
-       m.blendfunc2 = GL_ONE;
-       m.cr = 1;
-       m.cg = 1;
-       m.cb = 1;
-       m.ca = currentrenderentity->alpha;
-       m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
-       m.texcoordstep[0] = sizeof(surfvertex_t);
-       for (mesh = surf->mesh;mesh;mesh = mesh->chain)
-       {
-               m.numtriangles = mesh->numtriangles;
-               m.numverts = mesh->numverts;
-               m.index = mesh->index;
-               m.texcoords[0] = &mesh->vertex->st[0];
-               if (fogenabled)
-               {
-                       m.color = &svert[0].c[0];
-                       m.colorstep = sizeof(surfvert_t);
-                       if (softwaretransform_complexity)
-                       {
-                               m.vertex = &svert[0].v[0];
-                               m.vertexstep = sizeof(surfvert_t);
-                               for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
-                               {
-                                       softwaretransform(v->v, sv->v);
-                                       VectorSubtract(sv->v, r_origin, diff);
-                                       ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
-                                       sv->c[0] = m.cr * ifog;
-                                       sv->c[1] = m.cg * ifog;
-                                       sv->c[2] = m.cb * ifog;
-                                       sv->c[3] = m.ca;
-                               }
-                       }
-                       else
-                       {
-                               m.vertex = &mesh->vertex->v[0];
-                               m.vertexstep = sizeof(surfvertex_t);
-                               for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
-                               {
-                                       VectorSubtract(v->v, r_origin, diff);
-                                       ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
-                                       sv->c[0] = m.cr * ifog;
-                                       sv->c[1] = m.cg * ifog;
-                                       sv->c[2] = m.cb * ifog;
-                                       sv->c[3] = m.ca;
-                               }
-                       }
-               }
-               else
-               {
-                       if (softwaretransform_complexity)
-                       {
-                               m.vertex = &svert[0].v[0];
-                               m.vertexstep = sizeof(surfvert_t);
-                               for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
-                                       softwaretransform(v->v, sv->v);
-                       }
-                       else
-                       {
-                               m.vertex = &mesh->vertex->v[0];
-                               m.vertexstep = sizeof(surfvertex_t);
-                       }
+                       memcpy(m.vertex, mesh->verts, m.numverts * sizeof(float[4]));
+                       if (m.tex[0])
+                               memcpy(m.texcoords[0], mesh->st, m.numverts * sizeof(float[2]));
+                       RSurf_FogPassColors(m.vertex, m.color, fogcolor[0], fogcolor[1], fogcolor[2], 1, m.colorscale, m.numverts, modelorg);
+                       R_Mesh_Render();
                }
-               R_Mesh_Draw(&m);
        }
-#endif
 }
 
-static void RSurfShader_Wall_Pass_Fog(msurface_t *surf)
+static void RSurfShader_OpaqueWall_Pass_BaseDetail(const entity_render_t *ent, const msurface_t *surf)
 {
-#if 1
-       int i;
-       surfvertex_t *v;
-       float *outv, *outc, *outst, cl, ca, diff[3], f;
-       surfmesh_t *mesh;
+       const surfmesh_t *mesh;
        rmeshbufferinfo_t m;
        memset(&m, 0, sizeof(m));
-       m.transparent = currentrenderentity->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1;
-       m.blendfunc1 = GL_SRC_ALPHA;
-       m.blendfunc2 = GL_ONE;
-       ca = currentrenderentity->alpha;
+       m.blendfunc1 = GL_DST_COLOR;
+       m.blendfunc2 = GL_SRC_COLOR;
+       m.tex[0] = R_GetTexture(surf->currenttexture->detailtexture);
+       m.matrix = ent->matrix;
        for (mesh = surf->mesh;mesh;mesh = mesh->chain)
        {
                m.numtriangles = mesh->numtriangles;
                m.numverts = mesh->numverts;
-
-               if (R_Mesh_Draw_GetBuffer(&m))
+               if (R_Mesh_Draw_GetBuffer(&m, false))
                {
-                       cl = m.colorscale;
                        memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
-                       if (softwaretransform_complexity)
-                       {
-                               for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
-                               {
-                                       softwaretransform(v->v, outv);
-                                       outv[3] = 1;
-                                       VectorSubtract(outv, r_origin, diff);
-                                       f = cl * exp(fogdensity/DotProduct(diff, diff));
-                                       VectorScale(fogcolor, f, outc);
-                                       outc[3] = ca;
-                                       outst[0] = v->st[0];
-                                       outst[1] = v->st[1];
-                               }
-                       }
-                       else
-                       {
-                               for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
-                               {
-                                       VectorCopy(v->v, outv);
-                                       outv[3] = 1;
-                                       VectorSubtract(outv, r_origin, diff);
-                                       VectorSubtract(outv, r_origin, diff);
-                                       f = cl * exp(fogdensity/DotProduct(diff, diff));
-                                       VectorScale(fogcolor, f, outc);
-                                       outc[3] = ca;
-                                       outst[0] = v->st[0];
-                                       outst[1] = v->st[1];
-                               }
-                       }
+                       memcpy(m.vertex, mesh->verts, m.numverts * sizeof(float[4]));
+                       memcpy(m.texcoords[0], mesh->ab, m.numverts * sizeof(float[2]));
+                       R_FillColors(m.color, m.numverts, 1, 1, 1, 1);
+                       R_Mesh_Render();
                }
        }
-#else
-       int i;
-       surfvertex_t *v;
-       surfvert_t *sv;
-       rmeshinfo_t m;
-       surfmesh_t *mesh;
-       vec3_t diff;
+}
 
+static void RSurfShader_OpaqueWall_Pass_Glow(const entity_render_t *ent, const msurface_t *surf)
+{
+       const surfmesh_t *mesh;
+       rmeshbufferinfo_t m;
        memset(&m, 0, sizeof(m));
-       m.transparent = currentrenderentity->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1;
        m.blendfunc1 = GL_SRC_ALPHA;
        m.blendfunc2 = GL_ONE;
-       m.color = &svert[0].c[0];
-       m.colorstep = sizeof(surfvert_t);
-       m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
-       m.texcoordstep[0] = sizeof(surfvertex_t);
+       m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
+       m.matrix = ent->matrix;
        for (mesh = surf->mesh;mesh;mesh = mesh->chain)
        {
                m.numtriangles = mesh->numtriangles;
                m.numverts = mesh->numverts;
-               m.index = mesh->index;
-               m.texcoords[0] = &mesh->vertex->st[0];
-               if (softwaretransform_complexity)
+               if (R_Mesh_Draw_GetBuffer(&m, false))
                {
-                       m.vertex = &svert[0].v[0];
-                       m.vertexstep = sizeof(surfvert_t);
-                       for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
-                       {
-                               softwaretransform(v->v, sv->v);
-                               VectorSubtract(sv->v, r_origin, diff);
-                               sv->c[0] = fogcolor[0];
-                               sv->c[1] = fogcolor[1];
-                               sv->c[2] = fogcolor[2];
-                               sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
-                       }
-               }
-               else
-               {
-                       m.vertex = &mesh->vertex->v[0];
-                       m.vertexstep = sizeof(surfvertex_t);
-                       for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
-                       {
-                               VectorSubtract(v->v, r_origin, diff);
-                               sv->c[0] = fogcolor[0];
-                               sv->c[1] = fogcolor[1];
-                               sv->c[2] = fogcolor[2];
-                               sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
-                       }
+                       memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
+                       memcpy(m.vertex, mesh->verts, m.numverts * sizeof(float[4]));
+                       memcpy(m.texcoords[0], mesh->st, m.numverts * sizeof(float[2]));
+                       R_FillColors(m.color, m.numverts, m.colorscale, m.colorscale, m.colorscale, 1);
+                       R_Mesh_Render();
                }
-               R_Mesh_Draw(&m);
-       }
-#endif
-}
-
-static void RSurfShader_Wall_Fullbright(msurface_t *firstsurf)
-{
-       msurface_t *surf;
-       for (surf = firstsurf;surf;surf = surf->chain)
-       {
-               c_brush_polys++;
-               RSurfShader_Wall_Pass_BaseFullbright(surf);
        }
-       for (surf = firstsurf;surf;surf = surf->chain)
-               if (surf->currenttexture->glowtexture)
-                       RSurfShader_Wall_Pass_Glow(surf);
-       if (fogenabled)
-               for (surf = firstsurf;surf;surf = surf->chain)
-                       RSurfShader_Wall_Pass_Fog(surf);
 }
 
-static void RSurfShader_Wall_Vertex(msurface_t *firstsurf)
+static void RSurfShader_Wall_Fullbright_Callback(const void *calldata1, int calldata2)
 {
-       msurface_t *surf;
-       for (surf = firstsurf;surf;surf = surf->chain)
-       {
-               c_brush_polys++;
-               RSurfShader_Wall_Pass_BaseVertex(surf);
-       }
-       for (surf = firstsurf;surf;surf = surf->chain)
-               if (surf->currenttexture->glowtexture)
-                       RSurfShader_Wall_Pass_Glow(surf);
+       const entity_render_t *ent = calldata1;
+       const msurface_t *surf = ent->model->surfaces + calldata2;
+       RSurfShader_Wall_Pass_BaseFullbright(ent, surf);
+       if (surf->currenttexture->glowtexture)
+               RSurfShader_Wall_Pass_Glow(ent, surf);
        if (fogenabled)
-               for (surf = firstsurf;surf;surf = surf->chain)
-                       RSurfShader_Wall_Pass_Fog(surf);
+               RSurfShader_Wall_Pass_Fog(ent, surf);
 }
 
-static void RSurfShader_Wall_Lightmap(msurface_t *firstsurf)
+static void RSurfShader_Wall_Fullbright(const entity_render_t *ent, const msurface_t *firstsurf)
 {
-       msurface_t *surf;
-       if (r_vertexsurfaces.integer)
+       const msurface_t *surf;
+       vec3_t center;
+       if (ent->effects & EF_ADDITIVE || ent->alpha < 1)
        {
                for (surf = firstsurf;surf;surf = surf->chain)
                {
-                       c_brush_polys++;
-                       RSurfShader_Wall_Pass_BaseVertex(surf);
+                       Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
+                       R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Fullbright_Callback, ent, surf - ent->model->surfaces);
                }
-               for (surf = firstsurf;surf;surf = surf->chain)
-                       if (surf->currenttexture->glowtexture)
-                               RSurfShader_Wall_Pass_Glow(surf);
-               if (fogenabled)
-                       for (surf = firstsurf;surf;surf = surf->chain)
-                               RSurfShader_Wall_Pass_Fog(surf);
        }
-       else if (r_multitexture.integer)
+       else
        {
-               if (r_dlightmap.integer)
-               {
-                       for (surf = firstsurf;surf;surf = surf->chain)
-                       {
-                               c_brush_polys++;
-                               RSurfShader_Wall_Pass_BaseMTex(surf);
-                       }
-                       for (surf = firstsurf;surf;surf = surf->chain)
-                               if (surf->currenttexture->glowtexture)
-                                       RSurfShader_Wall_Pass_Glow(surf);
-                       if (fogenabled)
-                               for (surf = firstsurf;surf;surf = surf->chain)
-                                       RSurfShader_Wall_Pass_Fog(surf);
-               }
-               else
+               for (surf = firstsurf;surf;surf = surf->chain)
                {
-                       for (surf = firstsurf;surf;surf = surf->chain)
+                       if (surf->currenttexture->fogtexture != NULL)
                        {
-                               c_brush_polys++;
-                               RSurfShader_Wall_Pass_BaseMTex(surf);
+                               Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
+                               R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Fullbright_Callback, ent, surf - ent->model->surfaces);
                        }
-                       for (surf = firstsurf;surf;surf = surf->chain)
-                               if (surf->dlightframe == r_framecount)
-                                       RSurfShader_Wall_Pass_Light(surf);
-                       for (surf = firstsurf;surf;surf = surf->chain)
-                               if (surf->currenttexture->glowtexture)
-                                       RSurfShader_Wall_Pass_Glow(surf);
-                       if (fogenabled)
-                               for (surf = firstsurf;surf;surf = surf->chain)
-                                       RSurfShader_Wall_Pass_Fog(surf);
-               }
-       }
-       else if (firstsurf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1 || currentrenderentity->effects & EF_ADDITIVE)
-       {
-               for (surf = firstsurf;surf;surf = surf->chain)
-               {
-                       c_brush_polys++;
-                       RSurfShader_Wall_Pass_BaseVertex(surf);
+                       else
+                               RSurfShader_Wall_Pass_BaseFullbright(ent, surf);
                }
                for (surf = firstsurf;surf;surf = surf->chain)
                        if (surf->currenttexture->glowtexture)
-                               RSurfShader_Wall_Pass_Glow(surf);
+                               if (surf->currenttexture->fogtexture == NULL)
+                                       RSurfShader_Wall_Pass_Glow(ent, surf);
                if (fogenabled)
                        for (surf = firstsurf;surf;surf = surf->chain)
-                               RSurfShader_Wall_Pass_Fog(surf);
-       }
-       else
-       {
-               if (r_dlightmap.integer)
-               {
-                       for (surf = firstsurf;surf;surf = surf->chain)
-                       {
-                               c_brush_polys++;
-                               RSurfShader_Wall_Pass_BaseTexture(surf);
-                       }
-                       for (surf = firstsurf;surf;surf = surf->chain)
-                               RSurfShader_Wall_Pass_BaseLightmap(surf);
-                       for (surf = firstsurf;surf;surf = surf->chain)
-                               if (surf->currenttexture->glowtexture)
-                                       RSurfShader_Wall_Pass_Glow(surf);
-                       if (fogenabled)
-                               for (surf = firstsurf;surf;surf = surf->chain)
-                                       RSurfShader_Wall_Pass_Fog(surf);
-               }
-               else
-               {
-                       for (surf = firstsurf;surf;surf = surf->chain)
-                       {
-                               c_brush_polys++;
-                               RSurfShader_Wall_Pass_BaseTexture(surf);
-                       }
-                       for (surf = firstsurf;surf;surf = surf->chain)
-                               RSurfShader_Wall_Pass_BaseLightmap(surf);
-                       for (surf = firstsurf;surf;surf = surf->chain)
-                               if (surf->dlightframe == r_framecount)
-                                       RSurfShader_Wall_Pass_Light(surf);
-                       for (surf = firstsurf;surf;surf = surf->chain)
-                               if (surf->currenttexture->glowtexture)
-                                       RSurfShader_Wall_Pass_Glow(surf);
-                       if (fogenabled)
-                               for (surf = firstsurf;surf;surf = surf->chain)
-                                       RSurfShader_Wall_Pass_Fog(surf);
-               }
+                               if (surf->currenttexture->fogtexture == NULL)
+                                       RSurfShader_Wall_Pass_Fog(ent, surf);
        }
 }
 
-/*
-=============================================================
-
-       WORLD MODEL
-
-=============================================================
-*/
-
-static void R_SolidWorldNode (void)
+static void RSurfShader_Wall_Vertex_Callback(const void *calldata1, int calldata2)
 {
-       if (r_viewleaf->contents != CONTENTS_SOLID)
-       {
-               int portalstack;
-               mportal_t *p, *pstack[8192];
-               msurface_t *surf, **mark, **endmark;
-               mleaf_t *leaf;
-               // LordHavoc: portal-passage worldnode; follows portals leading
-               // outward from viewleaf, if a portal leads offscreen it is not
-               // followed, in indoor maps this can often cull a great deal of
-               // geometry away when pvs data is not present (useful with pvs as well)
-
-               leaf = r_viewleaf;
-               leaf->worldnodeframe = r_framecount;
-               portalstack = 0;
-       loc0:
-               c_leafs++;
-
-               leaf->visframe = r_framecount;
-
-               if (leaf->nummarksurfaces)
-               {
-                       mark = leaf->firstmarksurface;
-                       endmark = mark + leaf->nummarksurfaces;
-                       do
-                       {
-                               surf = *mark++;
-                               // make sure surfaces are only processed once
-                               if (surf->worldnodeframe == r_framecount)
-                                       continue;
-                               surf->worldnodeframe = r_framecount;
-                               if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
-                               {
-                                       if (surf->flags & SURF_PLANEBACK)
-                                               surf->visframe = r_framecount;
-                               }
-                               else
-                               {
-                                       if (!(surf->flags & SURF_PLANEBACK))
-                                               surf->visframe = r_framecount;
-                               }
-                       }
-                       while (mark < endmark);
-               }
-
-               // follow portals into other leafs
-               p = leaf->portals;
-               for (;p;p = p->next)
-               {
-                       if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
-                       {
-                               leaf = p->past;
-                               if (leaf->worldnodeframe != r_framecount)
-                               {
-                                       leaf->worldnodeframe = r_framecount;
-                                       if (leaf->contents != CONTENTS_SOLID)
-                                       {
-                                               if (R_NotCulledBox(leaf->mins, leaf->maxs))
-                                               {
-                                                       p->visframe = r_framecount;
-                                                       pstack[portalstack++] = p;
-                                                       goto loc0;
+       const entity_render_t *ent = calldata1;
+       const msurface_t *surf = ent->model->surfaces + calldata2;
+       RSurfShader_Wall_Pass_BaseVertex(ent, surf);
+       if (surf->currenttexture->glowtexture)
+               RSurfShader_Wall_Pass_Glow(ent, surf);
+       if (fogenabled)
+               RSurfShader_Wall_Pass_Fog(ent, surf);
+}
 
-       loc1:
-                                                       p = pstack[--portalstack];
-                                               }
-                                       }
-                               }
-                       }
+static void RSurfShader_Wall_Vertex(const entity_render_t *ent, const msurface_t *firstsurf)
+{
+       const msurface_t *surf;
+       vec3_t center;
+       if (ent->effects & EF_ADDITIVE || ent->alpha < 1)
+       {
+               for (surf = firstsurf;surf;surf = surf->chain)
+               {
+                       Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
+                       R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Vertex_Callback, ent, surf - ent->model->surfaces);
                }
-
-               if (portalstack)
-                       goto loc1;
        }
        else
        {
-               mnode_t *nodestack[8192], *node = cl.worldmodel->nodes;
-               int nodestackpos = 0;
-               // LordHavoc: recursive descending worldnode; if portals are not
-               // available, this is a good last resort, can cull large amounts of
-               // geometry, but is more time consuming than portal-passage and renders
-               // things behind walls
-
-loc2:
-               if (R_NotCulledBox(node->mins, node->maxs))
+               for (surf = firstsurf;surf;surf = surf->chain)
                {
-                       if (node->numsurfaces)
-                       {
-                               msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
-                               if (PlaneDiff (r_origin, node->plane) < 0)
-                               {
-                                       for (;surf < surfend;surf++)
-                                       {
-                                               if (surf->flags & SURF_PLANEBACK)
-                                                       surf->visframe = r_framecount;
-                                       }
-                               }
-                               else
-                               {
-                                       for (;surf < surfend;surf++)
-                                       {
-                                               if (!(surf->flags & SURF_PLANEBACK))
-                                                       surf->visframe = r_framecount;
-                                       }
-                               }
-                       }
-
-                       // recurse down the children
-                       if (node->children[0]->contents >= 0)
+                       if (surf->currenttexture->fogtexture != NULL)
                        {
-                               if (node->children[1]->contents >= 0)
-                               {
-                                       if (nodestackpos < 8192)
-                                               nodestack[nodestackpos++] = node->children[1];
-                                       node = node->children[0];
-                                       goto loc2;
-                               }
-                               else
-                                       ((mleaf_t *)node->children[1])->visframe = r_framecount;
-                               node = node->children[0];
-                               goto loc2;
+                               Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
+                               R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Vertex_Callback, ent, surf - ent->model->surfaces);
                        }
                        else
-                       {
-                               ((mleaf_t *)node->children[0])->visframe = r_framecount;
-                               if (node->children[1]->contents >= 0)
-                               {
-                                       node = node->children[1];
-                                       goto loc2;
-                               }
-                               else if (nodestackpos > 0)
-                               {
-                                       ((mleaf_t *)node->children[1])->visframe = r_framecount;
-                                       node = nodestack[--nodestackpos];
-                                       goto loc2;
-                               }
-                       }
-               }
-               else if (nodestackpos > 0)
-               {
-                       node = nodestack[--nodestackpos];
-                       goto loc2;
+                               RSurfShader_Wall_Pass_BaseVertex(ent, surf);
                }
+               for (surf = firstsurf;surf;surf = surf->chain)
+                       if (surf->currenttexture->glowtexture)
+                               if (surf->currenttexture->fogtexture == NULL)
+                                       RSurfShader_Wall_Pass_Glow(ent, surf);
+               if (fogenabled)
+                       for (surf = firstsurf;surf;surf = surf->chain)
+                               if (surf->currenttexture->fogtexture == NULL)
+                                       RSurfShader_Wall_Pass_Fog(ent, surf);
        }
 }
 
-static int r_portalframecount = 0;
-
-static void R_PVSWorldNode()
+static void RSurfShader_Wall_Lightmap(const entity_render_t *ent, const msurface_t *firstsurf)
 {
-       int portalstack, i;
-       mportal_t *p, *pstack[8192];
-       msurface_t *surf, **mark, **endmark;
-       mleaf_t *leaf;
-       qbyte *worldvis;
-
-       worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
-
-       leaf = r_viewleaf;
-       leaf->worldnodeframe = r_framecount;
-       portalstack = 0;
-loc0:
-       c_leafs++;
-
-       leaf->visframe = r_framecount;
-
-       if (leaf->nummarksurfaces)
+       const msurface_t *surf;
+       vec3_t center;
+       if (ent->alpha < 1 || ent->effects & EF_ADDITIVE)
        {
-               mark = leaf->firstmarksurface;
-               endmark = mark + leaf->nummarksurfaces;
-               do
+               for (surf = firstsurf;surf;surf = surf->chain)
                {
-                       surf = *mark++;
-                       // make sure surfaces are only processed once
-                       if (surf->worldnodeframe == r_framecount)
-                               continue;
-                       surf->worldnodeframe = r_framecount;
-                       if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
+                       Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
+                       R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Vertex_Callback, ent, surf - ent->model->surfaces);
+               }
+       }
+       else if (r_vertexsurfaces.integer || ent->alpha < 1 || ent->effects & EF_ADDITIVE)
+       {
+               for (surf = firstsurf;surf;surf = surf->chain)
+               {
+                       if (surf->currenttexture->fogtexture != NULL)
                        {
-                               if (surf->flags & SURF_PLANEBACK)
-                                       surf->visframe = r_framecount;
+                               Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
+                               R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Vertex_Callback, ent, surf - ent->model->surfaces);
                        }
                        else
-                       {
-                               if (!(surf->flags & SURF_PLANEBACK))
-                                       surf->visframe = r_framecount;
-                       }
+                               RSurfShader_Wall_Pass_BaseVertex(ent, surf);
                }
-               while (mark < endmark);
+               for (surf = firstsurf;surf;surf = surf->chain)
+                       if (surf->currenttexture->glowtexture)
+                               if (surf->currenttexture->fogtexture == NULL)
+                                       RSurfShader_Wall_Pass_Glow(ent, surf);
+               if (fogenabled)
+                       for (surf = firstsurf;surf;surf = surf->chain)
+                               if (surf->currenttexture->fogtexture == NULL)
+                                       RSurfShader_Wall_Pass_Fog(ent, surf);
        }
-
-       // follow portals into other leafs
-       for (p = leaf->portals;p;p = p->next)
+       else
        {
-               if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
+               if (r_textureunits.integer >= 2)
                {
-                       leaf = p->past;
-                       if (leaf->worldnodeframe != r_framecount)
+                       if (r_textureunits.integer >= 3 && gl_combine.integer && r_detailtextures.integer)
                        {
-                               leaf->worldnodeframe = r_framecount;
-                               if (leaf->contents != CONTENTS_SOLID)
+                               for (surf = firstsurf;surf;surf = surf->chain)
                                {
-                                       i = (leaf - cl.worldmodel->leafs) - 1;
-                                       if (worldvis[i>>3] & (1<<(i&7)))
+                                       if (surf->currenttexture->fogtexture != NULL)
                                        {
-                                               if (R_NotCulledBox(leaf->mins, leaf->maxs))
-                                               {
-                                                       pstack[portalstack++] = p;
-                                                       goto loc0;
-
-loc1:
-                                                       p = pstack[--portalstack];
-                                               }
+                                               Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
+                                               R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Vertex_Callback, ent, surf - ent->model->surfaces);
                                        }
+                                       else
+                                               RSurfShader_OpaqueWall_Pass_TripleTexCombine(ent, surf);
+                               }
+                       }
+                       else
+                       {
+                               for (surf = firstsurf;surf;surf = surf->chain)
+                               {
+                                       if (surf->currenttexture->fogtexture != NULL)
+                                       {
+                                               Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
+                                               R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Vertex_Callback, ent, surf - ent->model->surfaces);
+                                       }
+                                       else
+                                               RSurfShader_OpaqueWall_Pass_BaseMTex(ent, surf);
+                               }
+                               if (r_detailtextures.integer)
+                                       for (surf = firstsurf;surf;surf = surf->chain)
+                                               if (surf->currenttexture->fogtexture == NULL)
+                                                       RSurfShader_OpaqueWall_Pass_BaseDetail(ent, surf);
+                       }
+               }
+               else
+               {
+                       for (surf = firstsurf;surf;surf = surf->chain)
+                       {
+                               if (surf->currenttexture->fogtexture != NULL)
+                               {
+                                       Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
+                                       R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Vertex_Callback, ent, surf - ent->model->surfaces);
                                }
+                               else
+                                       RSurfShader_OpaqueWall_Pass_BaseTexture(ent, surf);
                        }
+                       for (surf = firstsurf;surf;surf = surf->chain)
+                               if (surf->currenttexture->fogtexture == NULL)
+                                       RSurfShader_OpaqueWall_Pass_BaseLightmap(ent, surf);
+                       if (r_detailtextures.integer)
+                               for (surf = firstsurf;surf;surf = surf->chain)
+                                       if (surf->currenttexture->fogtexture == NULL)
+                                               RSurfShader_OpaqueWall_Pass_BaseDetail(ent, surf);
                }
+               if (!r_dlightmap.integer)
+                       for (surf = firstsurf;surf;surf = surf->chain)
+                               if (surf->dlightframe == r_framecount)
+                                       if (surf->currenttexture->fogtexture == NULL)
+                                               RSurfShader_OpaqueWall_Pass_Light(ent, surf);
+               for (surf = firstsurf;surf;surf = surf->chain)
+                       if (surf->currenttexture->glowtexture)
+                               if (surf->currenttexture->fogtexture == NULL)
+                                       RSurfShader_OpaqueWall_Pass_Glow(ent, surf);
+               if (fogenabled)
+                       for (surf = firstsurf;surf;surf = surf->chain)
+                               if (surf->currenttexture->fogtexture == NULL)
+                                       RSurfShader_OpaqueWall_Pass_Fog(ent, surf);
        }
-
-       if (portalstack)
-               goto loc1;
 }
 
 Cshader_t Cshader_wall_vertex = {{NULL, RSurfShader_Wall_Vertex}, NULL};
@@ -2788,184 +1504,365 @@ Cshader_t *Cshaders[5] =
        &Cshader_sky
 };
 
-void R_PrepareSurfaces(void)
+void R_DrawSurfaces(entity_render_t *ent, int sky, int normal)
 {
        int i, alttextures, texframe, framecount;
        texture_t *t;
        model_t *model;
        msurface_t *surf;
+       vec3_t modelorg;
+       Cshader_t *shader;
+
+       if (!ent->model)
+               return;
 
        for (i = 0;i < Cshader_count;i++)
                Cshaders[i]->chain = NULL;
 
-       model = currentrenderentity->model;
-       alttextures = currentrenderentity->frame != 0;
+       model = ent->model;
+       alttextures = ent->frame != 0;
        texframe = (int)(cl.time * 5.0f);
 
+       Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
        for (i = 0;i < model->nummodelsurfaces;i++)
        {
                surf = model->modelsortedsurfaces[i];
                if (surf->visframe == r_framecount)
                {
-                       if (surf->insertframe != r_framecount)
+                       // mark any backface surfaces as not visible
+                       if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
                        {
-                               surf->insertframe = r_framecount;
-                               c_faces++;
-                               t = surf->texinfo->texture;
-                               if (t->animated)
+                               if (!(surf->flags & SURF_PLANEBACK))
+                                       surf->visframe = -1;
+                       }
+                       else
+                       {
+                               if (surf->flags & SURF_PLANEBACK)
+                                       surf->visframe = -1;
+                       }
+                       if (surf->visframe == r_framecount)
+                       {
+                               if (r_cullsurface.integer && R_CullBox (surf->poly_mins, surf->poly_maxs))
+                                       surf->visframe = -1;
+                               else
                                {
-                                       framecount = t->anim_total[alttextures];
-                                       if (framecount >= 2)
-                                               surf->currenttexture = t->anim_frames[alttextures][texframe % framecount];
+                                       c_faces++;
+                                       t = surf->texinfo->texture;
+                                       if (t->animated)
+                                       {
+                                               framecount = t->anim_total[alttextures];
+                                               if (framecount >= 2)
+                                                       surf->currenttexture = t->anim_frames[alttextures][texframe % framecount];
+                                               else
+                                                       surf->currenttexture = t->anim_frames[alttextures][0];
+                                       }
                                        else
-                                               surf->currenttexture = t->anim_frames[alttextures][0];
+                                               surf->currenttexture = t;
+                                       surf->chain = surf->shader->chain;
+                                       surf->shader->chain = surf;
+                               }
+                       }
+               }
+       }
+
+       if (sky)
+       {
+               for (i = 0;i < Cshader_count;i++)
+               {
+                       shader = Cshaders[i];
+                       if (shader->chain && shader->shaderfunc[SHADERSTAGE_SKY])
+                               shader->shaderfunc[SHADERSTAGE_SKY](ent, shader->chain);
+               }
+       }
+
+       if (normal)
+       {
+               if (r_dynamic.integer)
+                       R_MarkLights(ent);
+
+               if (!r_vertexsurfaces.integer)
+               {
+                       for (i = 0, surf = ent->model->surfaces + ent->model->firstmodelsurface;i < ent->model->nummodelsurfaces;i++, surf++)
+                       {
+                               if (surf->visframe == r_framecount && surf->lightmaptexture != NULL)
+                               {
+                                       if (surf->cached_dlight
+                                       || surf->cached_ambient != r_ambient.value
+                                       || surf->cached_lightscalebit != lightscalebit)
+                                               R_BuildLightMap(ent, surf, false); // base lighting changed
+                                       else if (r_dynamic.integer)
+                                       {
+                                               if  (surf->styles[0] != 255 && (d_lightstylevalue[surf->styles[0]] != surf->cached_light[0]
+                                               || (surf->styles[1] != 255 && (d_lightstylevalue[surf->styles[1]] != surf->cached_light[1]
+                                               || (surf->styles[2] != 255 && (d_lightstylevalue[surf->styles[2]] != surf->cached_light[2]
+                                               || (surf->styles[3] != 255 && (d_lightstylevalue[surf->styles[3]] != surf->cached_light[3]))))))))
+                                                       R_BuildLightMap(ent, surf, false); // base lighting changed
+                                               else if (surf->dlightframe == r_framecount && r_dlightmap.integer)
+                                                       R_BuildLightMap(ent, surf, true); // only dlights
+                                       }
                                }
-                               else
-                                       surf->currenttexture = t;
                        }
+               }
 
-                       surf->chain = surf->shader->chain;
-                       surf->shader->chain = surf;
+               for (i = 0;i < Cshader_count;i++)
+               {
+                       shader = Cshaders[i];
+                       if (shader->chain && shader->shaderfunc[SHADERSTAGE_NORMAL])
+                               shader->shaderfunc[SHADERSTAGE_NORMAL](ent, shader->chain);
                }
        }
 }
 
-void R_DrawSurfaces (int type)
+/*
+static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
 {
-       int                     i;
-       Cshader_t       *shader;
-
-       for (i = 0;i < Cshader_count;i++)
+       int i;
+       float *v;
+       rmeshbufferinfo_t m;
+       const entity_render_t *ent = calldata1;
+       const mportal_t *portal = ent->model->portals + calldata2;
+       memset(&m, 0, sizeof(m));
+       m.blendfunc1 = GL_SRC_ALPHA;
+       m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
+       m.numverts = portal->numpoints;
+       m.numtriangles = portal->numpoints - 2;
+       m.matrix = ent->matrix;
+       if (R_Mesh_Draw_GetBuffer(&m, false))
        {
-               shader = Cshaders[i];
-               if (shader->chain && shader->shaderfunc[type])
-                       shader->shaderfunc[type](shader->chain);
+               for (i = 0;i < m.numtriangles;i++)
+               {
+                       m.index[i * 3 + 0] = 0;
+                       m.index[i * 3 + 1] = i + 1;
+                       m.index[i * 3 + 2] = i + 2;
+               }
+               i = portal - ent->model->portals;
+               R_FillColors(m.color, m.numverts,
+                       ((i & 0x0007) >> 0) * (1.0f / 7.0f) * m.colorscale,
+                       ((i & 0x0038) >> 3) * (1.0f / 7.0f) * m.colorscale,
+                       ((i & 0x01C0) >> 6) * (1.0f / 7.0f) * m.colorscale,
+                       0.125f);
+               if (PlaneDiff(r_origin, (&portal->plane)) > 0)
+               {
+                       for (i = portal->numpoints - 1, v = m.vertex;i >= 0;i--, v += 4)
+                               VectorCopy(portal->points[i].position, v);
+               }
+               else
+                       for (i = 0, v = m.vertex;i < portal->numpoints;i++, v += 4)
+                               VectorCopy(portal->points[i].position, v);
+               R_Mesh_Render();
        }
 }
 
-static float portalpointbuffer[256][3];
-
-void R_DrawPortals(void)
+static void R_DrawPortals(entity_render_t *ent)
 {
-       int drawportals, i;
+       int i;
        mportal_t *portal, *endportal;
-       mvertex_t *point;
-       rmeshinfo_t m;
-       drawportals = r_drawportals.integer;
+       float temp[3], center[3], f;
 
-       if (drawportals < 1)
+       if (r_drawportals.integer < 1)
                return;
 
-       memset(&m, 0, sizeof(m));
-       m.transparent = true;
-       m.blendfunc1 = GL_SRC_ALPHA;
-       m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
-       m.vertex = &portalpointbuffer[0][0];
-       m.vertexstep = sizeof(float[3]);
-       m.ca = 0.125;
-       for (portal = cl.worldmodel->portals, endportal = portal + cl.worldmodel->numportals;portal < endportal;portal++)
+       for (portal = ent->model->portals, endportal = portal + ent->model->numportals;portal < endportal;portal++)
        {
-               if (portal->visframe == r_portalframecount)
+               if (portal->here->visframe == r_framecount || portal->past->visframe == r_framecount)
                {
-                       if (portal->numpoints <= 256)
-                       {
-                               i = portal - cl.worldmodel->portals;
-                               m.cr = ((i & 0x0007) >> 0) * (1.0f / 7.0f);
-                               m.cg = ((i & 0x0038) >> 3) * (1.0f / 7.0f);
-                               m.cb = ((i & 0x01C0) >> 6) * (1.0f / 7.0f);
-                               point = portal->points;
-                               if (PlaneDiff(r_origin, (&portal->plane)) > 0)
-                               {
-                                       for (i = portal->numpoints - 1;i >= 0;i--)
-                                               VectorCopy(point[i].position, portalpointbuffer[i]);
-                               }
-                               else
-                               {
-                                       for (i = 0;i < portal->numpoints;i++)
-                                               VectorCopy(point[i].position, portalpointbuffer[i]);
-                               }
-                               R_Mesh_DrawPolygon(&m, portal->numpoints);
-                       }
+                       VectorClear(temp);
+                       for (i = 0;i < portal->numpoints;i++)
+                               VectorAdd(temp, portal->points[i].position, temp);
+                       f = ixtable[portal->numpoints];
+                       VectorScale(temp, f, temp);
+                       Matrix4x4_Transform(&ent->matrix, temp, center);
+                       R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, ent, portal - ent->model->portals);
                }
        }
 }
+*/
 
-void R_SetupForBModelRendering(void)
+void R_DrawBrushModel(entity_render_t *ent, int sky, int normal)
 {
-       int                     i;
-       msurface_t      *surf;
-       model_t         *model;
-       vec3_t          modelorg;
+       int i;
+       msurface_t *surf;
+       model_t *model;
+       vec3_t modelorg;
 
        // because bmodels can be reused, we have to decide which things to render
        // from scratch every time
-
-       model = currentrenderentity->model;
-
-       softwaretransformforentity (currentrenderentity);
-       softwareuntransform(r_origin, modelorg);
-
+       model = ent->model;
+       Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
        for (i = 0;i < model->nummodelsurfaces;i++)
        {
-               surf = model->modelsortedsurfaces[i];
-               if (((surf->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, surf->plane) >= 0))
-                       surf->visframe = r_framecount;
-               else
-                       surf->visframe = -1;
+               surf = model->surfaces + model->firstmodelsurface + i;
+               surf->visframe = r_framecount;
+               surf->pvsframe = -1;
                surf->worldnodeframe = -1;
                surf->lightframe = -1;
                surf->dlightframe = -1;
-               surf->insertframe = -1;
        }
+       R_DrawSurfaces(ent, sky, normal);
 }
 
-void R_SetupForWorldRendering(void)
+void R_SurfaceWorldNode (void)
 {
-       // there is only one instance of the world, but it can be rendered in
-       // multiple stages
-
-       currentrenderentity = &cl_entities[0].render;
-       softwaretransformidentity();
+       msurface_t *surf;
+       for (surf = r_pvsfirstsurface;surf;surf = surf->pvschain)
+               surf->visframe = r_framecount;
 }
 
-static void R_SurfMarkLights (void)
+/*
+static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
 {
-       int                     i;
-       msurface_t      *surf;
-
-       if (r_dynamic.integer)
-               R_MarkLights();
-
-       if (!r_vertexsurfaces.integer)
+       int portalstack, i;
+       mportal_t *p, *pstack[8192];
+       msurface_t *surf, **mark, **endmark;
+       mleaf_t *leaf;
+       // LordHavoc: portal-passage worldnode with PVS;
+       // follows portals leading outward from viewleaf, does not venture
+       // offscreen or into leafs that are not visible, faster than Quake's
+       // RecursiveWorldNode
+       leaf = viewleaf;
+       leaf->worldnodeframe = r_framecount;
+       portalstack = 0;
+loc0:
+       c_leafs++;
+       if (leaf->nummarksurfaces)
        {
-               for (i = 0;i < currentrenderentity->model->nummodelsurfaces;i++)
+               for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--)
                {
-                       surf = currentrenderentity->model->modelsortedsurfaces[i];
-                       if (surf->visframe == r_framecount && surf->lightmaptexture != NULL)
+                       surf = *mark++;
+                       // make sure surfaces are only processed once
+                       if (surf->worldnodeframe != r_framecount)
                        {
-                               if (surf->cached_dlight
-                                || surf->cached_ambient != r_ambient.value
-                                || surf->cached_lightscalebit != lightscalebit)
-                                       R_BuildLightMap(surf, false); // base lighting changed
-                               else if (r_dynamic.integer)
+                               surf->worldnodeframe = r_framecount;
+                               if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
+                               {
+                                       if (surf->flags & SURF_PLANEBACK)
+                                               surf->visframe = r_framecount;
+                               }
+                               else
                                {
-                                       if  (surf->styles[0] != 255 && (d_lightstylevalue[surf->styles[0]] != surf->cached_light[0]
-                                        || (surf->styles[1] != 255 && (d_lightstylevalue[surf->styles[1]] != surf->cached_light[1]
-                                        || (surf->styles[2] != 255 && (d_lightstylevalue[surf->styles[2]] != surf->cached_light[2]
-                                        || (surf->styles[3] != 255 && (d_lightstylevalue[surf->styles[3]] != surf->cached_light[3]))))))))
-                                               R_BuildLightMap(surf, false); // base lighting changed
-                                       else if (surf->dlightframe == r_framecount && r_dlightmap.integer)
-                                               R_BuildLightMap(surf, true); // only dlights
+                                       if (!(surf->flags & SURF_PLANEBACK))
+                                               surf->visframe = r_framecount;
                                }
                        }
                }
        }
+       // follow portals into other leafs
+       for (p = leaf->portals;p;p = p->next)
+       {
+               leaf = p->past;
+               if (leaf->worldnodeframe != r_framecount)
+               {
+                       leaf->worldnodeframe = r_framecount;
+                       // FIXME: R_NotCulledBox is absolute, should be done relative
+                       if (leaf->pvsframe == r_pvsframecount && R_NotCulledBox(leaf->mins, leaf->maxs))
+                       {
+                               p->visframe = r_framecount;
+                               pstack[portalstack++] = p;
+                               goto loc0;
+loc1:
+                               p = pstack[--portalstack];
+                       }
+               }
+       }
+       if (portalstack)
+               goto loc1;
+}
+*/
+
+static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
+{
+       int c, leafstackpos;
+       mleaf_t *leaf, *leafstack[8192];
+       mportal_t *p;
+       msurface_t **mark;
+       vec3_t modelorg;
+       // LordHavoc: portal-passage worldnode with PVS;
+       // follows portals leading outward from viewleaf, does not venture
+       // offscreen or into leafs that are not visible, faster than Quake's
+       // RecursiveWorldNode
+       Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
+       viewleaf->worldnodeframe = r_framecount;
+       leafstack[0] = viewleaf;
+       leafstackpos = 1;
+       while (leafstackpos)
+       {
+               c_leafs++;
+               leaf = leafstack[--leafstackpos];
+               // only useful for drawing portals
+               //leaf->visframe = r_framecount;
+               // draw any surfaces bounding this leaf
+               if (leaf->nummarksurfaces)
+                       for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--)
+                               (*mark++)->visframe = r_framecount;
+               // follow portals into other leafs
+               for (p = leaf->portals;p;p = p->next)
+               {
+                       leaf = p->past;
+                       if (leaf->worldnodeframe != r_framecount)
+                       {
+                               leaf->worldnodeframe = r_framecount;
+                               // FIXME: R_NotCulledBox is absolute, should be done relative
+                               if (leaf->pvsframe == r_pvsframecount && R_NotCulledBox(leaf->mins, leaf->maxs))
+                                       leafstack[leafstackpos++] = leaf;
+                       }
+               }
+       }
+       //if (r_drawportals.integer)
+       //      R_DrawPortals(ent);
 }
 
-void R_MarkWorldLights(void)
+
+void R_PVSUpdate (mleaf_t *viewleaf)
 {
-       R_SetupForWorldRendering();
-       R_SurfMarkLights();
+       int i, j, l, c, bits;
+       mleaf_t *leaf;
+       qbyte *vis;
+       msurface_t **mark, *surf;
+
+       if (r_pvsviewleaf == viewleaf && r_pvsviewleafnovis == r_novis.integer)
+               return;
+
+       r_pvsframecount++;
+       r_pvsviewleaf = viewleaf;
+       r_pvsviewleafnovis = r_novis.integer;
+
+       if (viewleaf)
+       {
+               vis = Mod_LeafPVS (viewleaf, cl.worldmodel);
+               for (j = 0;j < cl.worldmodel->numleafs;j += 8)
+               {
+                       bits = *vis++;
+                       if (bits)
+                       {
+                               l = cl.worldmodel->numleafs - j;
+                               if (l > 8)
+                                       l = 8;
+                               for (i = 0;i < l;i++)
+                               {
+                                       if (bits & (1 << i))
+                                       {
+                                               leaf = &cl.worldmodel->leafs[j + i + 1];
+                                               leaf->pvsframe = r_pvsframecount;
+                                               // mark surfaces bounding this leaf as visible
+                                               for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--)
+                                                       (*mark++)->pvsframe = r_pvsframecount;
+                                       }
+                               }
+                       }
+               }
+               // build pvs surfacechain
+               r_pvsfirstsurface = NULL;
+               mark = &r_pvsfirstsurface;
+               for (c = cl.worldmodel->nummodelsurfaces, surf = cl.worldmodel->surfaces + cl.worldmodel->firstmodelsurface;c;c--, surf++)
+               {
+                       if (surf->pvsframe == r_pvsframecount)
+                       {
+                               *mark = surf;
+                               mark = &surf->pvschain;
+                       }
+               }
+               *mark = NULL;
+       }
 }
 
 /*
@@ -2973,14 +1870,18 @@ void R_MarkWorldLights(void)
 R_DrawWorld
 =============
 */
-void R_DrawWorld (void)
+void R_DrawWorld (entity_render_t *ent)
 {
-       R_SetupForWorldRendering();
-
-       if (r_viewleaf->contents == CONTENTS_SOLID || r_novis.integer || r_viewleaf->compressed_vis == NULL)
-               R_SolidWorldNode ();
+       mleaf_t *viewleaf;
+       viewleaf = Mod_PointInLeaf (r_origin, cl.worldmodel);
+       R_PVSUpdate(viewleaf);
+       if (!viewleaf)
+               return;
+       if (r_surfaceworldnode.integer || viewleaf->contents == CONTENTS_SOLID)
+               R_SurfaceWorldNode ();
        else
-               R_PVSWorldNode ();
+               R_PortalWorldNode (ent, viewleaf);
+       R_DrawSurfaces(ent, true, true);
 }
 
 /*
@@ -2988,30 +1889,15 @@ void R_DrawWorld (void)
 R_DrawBrushModel
 =================
 */
-void R_DrawBrushModelSky (void)
+void R_DrawBrushModelSky (entity_render_t *ent)
 {
-       R_SetupForBModelRendering();
-
-       R_PrepareSurfaces();
-       R_DrawSurfaces(SHADERSTAGE_SKY);
+       R_DrawBrushModel(ent, true, false);
 }
 
-void R_DrawBrushModelNormal (void)
+void R_DrawBrushModelNormal (entity_render_t *ent)
 {
        c_bmodels++;
-
-       // have to flush queue because of possible lightmap reuse
-       R_Mesh_Render();
-
-       R_SetupForBModelRendering();
-
-       R_SurfMarkLights();
-
-       R_PrepareSurfaces();
-
-       if (!skyrendermasked)
-               R_DrawSurfaces(SHADERSTAGE_SKY);
-       R_DrawSurfaces(SHADERSTAGE_NORMAL);
+       R_DrawBrushModel(ent, false, true);
 }
 
 static void gl_surf_start(void)
@@ -3024,6 +1910,11 @@ static void gl_surf_shutdown(void)
 
 static void gl_surf_newmap(void)
 {
+       // reset pvs visibility variables so it will update on first frame
+       r_pvsframecount = 1;
+       r_pvsviewleaf = NULL;
+       r_pvsviewleafnovis = false;
+       r_pvsfirstsurface = NULL;
 }
 
 void GL_Surf_Init(void)
@@ -3036,9 +1927,12 @@ void GL_Surf_Init(void)
        Cvar_RegisterVariable(&r_ambient);
        Cvar_RegisterVariable(&r_vertexsurfaces);
        Cvar_RegisterVariable(&r_dlightmap);
-       Cvar_RegisterVariable(&r_drawportals);
+       //Cvar_RegisterVariable(&r_drawportals);
        Cvar_RegisterVariable(&r_testvis);
        Cvar_RegisterVariable(&r_floatbuildlightmap);
+       Cvar_RegisterVariable(&r_detailtextures);
+       Cvar_RegisterVariable(&r_surfaceworldnode);
+       Cvar_RegisterVariable(&r_cullsurface);
 
        R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);
 }