]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - r_shadow.c
fixed realtime lighting bugs with gl_mesh_batching 1 (... by not using batching while...
[xonotic/darkplaces.git] / r_shadow.c
index de429d0b49711a8318b307d6a81de1a990529ef7..19e36e7933b010318b65024f63a97965e0369ea6 100644 (file)
@@ -14,8 +14,6 @@ extern void R_Shadow_EditLights_Init(void);
 int r_shadowstage = SHADOWSTAGE_NONE;
 int r_shadow_reloadlights = false;
 
-int r_shadow_lightingmode = 0;
-
 mempool_t *r_shadow_mempool;
 
 int maxshadowelements;
@@ -24,8 +22,9 @@ int maxtrianglefacinglight;
 qbyte *trianglefacinglight;
 int *trianglefacinglightlist;
 
-int maxshadowvertices;
-float *shadowvertex3f;
+int maxvertexupdate;
+int *vertexupdate;
+int vertexupdatenum;
 
 rtexturepool_t *r_shadow_texturepool;
 rtexture_t *r_shadow_normalcubetexture;
@@ -38,13 +37,15 @@ rtexture_t *r_shadow_blankwhitetexture;
 cvar_t r_shadow_lightattenuationpower = {0, "r_shadow_lightattenuationpower", "0.5"};
 cvar_t r_shadow_lightattenuationscale = {0, "r_shadow_lightattenuationscale", "1"};
 cvar_t r_shadow_lightintensityscale = {0, "r_shadow_lightintensityscale", "1"};
-cvar_t r_shadow_realtime = {0, "r_shadow_realtime", "0"};
+cvar_t r_shadow_realtime_world = {0, "r_shadow_realtime_world", "0"};
+cvar_t r_shadow_realtime_dlight = {0, "r_shadow_realtime_dlight", "0"};
+cvar_t r_shadow_visiblevolumes = {0, "r_shadow_visiblevolumes", "0"};
 cvar_t r_shadow_gloss = {0, "r_shadow_gloss", "1"};
 cvar_t r_shadow_debuglight = {0, "r_shadow_debuglight", "-1"};
 cvar_t r_shadow_scissor = {0, "r_shadow_scissor", "1"};
 cvar_t r_shadow_bumpscale_bumpmap = {0, "r_shadow_bumpscale_bumpmap", "4"};
 cvar_t r_shadow_bumpscale_basetexture = {0, "r_shadow_bumpscale_basetexture", "0"};
-cvar_t r_shadow_shadownudge = {0, "r_shadow_shadownudge", "1"};
+cvar_t r_shadow_polygonoffset = {0, "r_shadow_polygonoffset", "-1"};
 cvar_t r_shadow_portallight = {0, "r_shadow_portallight", "1"};
 cvar_t r_shadow_projectdistance = {0, "r_shadow_projectdistance", "100000"};
 cvar_t r_shadow_texture3d = {0, "r_shadow_texture3d", "1"};
@@ -65,8 +66,9 @@ void r_shadow_start(void)
        r_shadow_mempool = Mem_AllocPool("R_Shadow");
        maxshadowelements = 0;
        shadowelements = NULL;
-       maxshadowvertices = 0;
-       shadowvertex3f = NULL;
+       maxvertexupdate = 0;
+       vertexupdate = NULL;
+       vertexupdatenum = 0;
        maxtrianglefacinglight = 0;
        trianglefacinglight = NULL;
        trianglefacinglightlist = NULL;
@@ -94,8 +96,9 @@ void r_shadow_shutdown(void)
        R_FreeTexturePool(&r_shadow_texturepool);
        maxshadowelements = 0;
        shadowelements = NULL;
-       maxshadowvertices = 0;
-       shadowvertex3f = NULL;
+       maxvertexupdate = 0;
+       vertexupdate = NULL;
+       vertexupdatenum = 0;
        maxtrianglefacinglight = 0;
        trianglefacinglight = NULL;
        trianglefacinglightlist = NULL;
@@ -113,13 +116,15 @@ void R_Shadow_Init(void)
        Cvar_RegisterVariable(&r_shadow_lightattenuationpower);
        Cvar_RegisterVariable(&r_shadow_lightattenuationscale);
        Cvar_RegisterVariable(&r_shadow_lightintensityscale);
-       Cvar_RegisterVariable(&r_shadow_realtime);
+       Cvar_RegisterVariable(&r_shadow_realtime_world);
+       Cvar_RegisterVariable(&r_shadow_realtime_dlight);
+       Cvar_RegisterVariable(&r_shadow_visiblevolumes);
        Cvar_RegisterVariable(&r_shadow_gloss);
        Cvar_RegisterVariable(&r_shadow_debuglight);
        Cvar_RegisterVariable(&r_shadow_scissor);
        Cvar_RegisterVariable(&r_shadow_bumpscale_bumpmap);
        Cvar_RegisterVariable(&r_shadow_bumpscale_basetexture);
-       Cvar_RegisterVariable(&r_shadow_shadownudge);
+       Cvar_RegisterVariable(&r_shadow_polygonoffset);
        Cvar_RegisterVariable(&r_shadow_portallight);
        Cvar_RegisterVariable(&r_shadow_projectdistance);
        Cvar_RegisterVariable(&r_shadow_texture3d);
@@ -127,21 +132,6 @@ void R_Shadow_Init(void)
        R_RegisterModule("R_Shadow", r_shadow_start, r_shadow_shutdown, r_shadow_newmap);
 }
 
-void R_Shadow_ProjectVertex3f(float *verts, int numverts, const float *relativelightorigin, float projectdistance)
-{
-       int i;
-       float *in, *out, diff[3];
-       in = verts;
-       out = verts + numverts * 3;
-       for (i = 0;i < numverts;i++, in += 3, out += 3)
-       {
-               VectorSubtract(in, relativelightorigin, diff);
-               VectorNormalizeFast(diff);
-               VectorMA(in, projectdistance, diff, out);
-               VectorMA(in, r_shadow_shadownudge.value, diff, in);
-       }
-}
-
 int R_Shadow_MakeTriangleShadowFlags_Vertex3f(const int *elements, const float *vertex, int numtris, qbyte *facing, int *list, const float *relativelightorigin)
 {
        int i, tris = 0;
@@ -163,10 +153,21 @@ int R_Shadow_MakeTriangleShadowFlags_Vertex3f(const int *elements, const float *
        return tris;
 }
 
-int R_Shadow_BuildShadowVolumeTriangles(const int *elements, const int *neighbors, int numverts, const qbyte *facing, const int *facinglist, int numfacing, int *out)
+int R_Shadow_BuildShadowVolume(const int *elements, const int *neighbors, int numverts, const qbyte *facing, const int *facinglist, int numfacing, int *out, float *vertices, const float *relativelightorigin, float projectdistance)
 {
-       int i, tris;
+       int i, j, tris, vertexpointeradjust = numverts * 3;
        const int *e, *n;
+       float *vin, *vout;
+
+       if (maxvertexupdate < numverts)
+       {
+               maxvertexupdate = numverts;
+               if (vertexupdate)
+                       Mem_Free(vertexupdate);
+               vertexupdate = Mem_Alloc(r_shadow_mempool, maxvertexupdate * sizeof(int));
+       }
+       vertexupdatenum++;
+
        // check each frontface for bordering backfaces,
        // and cast shadow polygons from those edges,
        // also create front and back caps for shadow volume
@@ -184,6 +185,19 @@ int R_Shadow_BuildShadowVolumeTriangles(const int *elements, const int *neighbor
        for (i = 0;i < numfacing;i++)
        {
                e = elements + facinglist[i] * 3;
+               // generate vertices if needed
+               for (j = 0;j < 3;j++)
+               {
+                       if (vertexupdate[e[j]] != vertexupdatenum)
+                       {
+                               vertexupdate[e[j]] = vertexupdatenum;
+                               vin = vertices + e[j] * 3;
+                               vout = vin + vertexpointeradjust;
+                               vout[0] = relativelightorigin[0] + projectdistance * (vin[0] - relativelightorigin[0]);
+                               vout[1] = relativelightorigin[1] + projectdistance * (vin[1] - relativelightorigin[1]);
+                               vout[2] = relativelightorigin[2] + projectdistance * (vin[2] - relativelightorigin[2]);
+                       }
+               }
                out[0] = e[2] + numverts;
                out[1] = e[1] + numverts;
                out[2] = e[0] + numverts;
@@ -264,18 +278,6 @@ int *R_Shadow_ResizeShadowElements(int numtris)
        return shadowelements;
 }
 
-float *R_Shadow_VertexBuffer(int numvertices)
-{
-       if (maxshadowvertices < numvertices)
-       {
-               maxshadowvertices = numvertices;
-               if (shadowvertex3f)
-                       Mem_Free(shadowvertex3f);
-               shadowvertex3f = Mem_Alloc(r_shadow_mempool, maxshadowvertices * sizeof(float[3]));
-       }
-       return shadowvertex3f;
-}
-
 void R_Shadow_Volume(int numverts, int numtris, int *elements, int *neighbors, vec3_t relativelightorigin, float lightradius, float projectdistance)
 {
        int tris;
@@ -319,28 +321,29 @@ void R_Shadow_Volume(int numverts, int numtris, int *elements, int *neighbors, v
        if (!tris)
                return;
 
-       // output triangle elements
-       tris = R_Shadow_BuildShadowVolumeTriangles(elements, neighbors, numverts, trianglefacinglight, trianglefacinglightlist, tris, shadowelements);
-       if (!tris)
-               return;
-
        // by clever use of elements we can construct the whole shadow from
        // the unprojected vertices and the projected vertices
-       R_Shadow_ProjectVertex3f(varray_vertex3f, numverts, relativelightorigin, projectdistance);
+
+       // output triangle elements and vertices
+       tris = R_Shadow_BuildShadowVolume(elements, neighbors, numverts, trianglefacinglight, trianglefacinglightlist, tris, shadowelements, varray_vertex3f, relativelightorigin, projectdistance);
+       if (!tris)
+               return;
 
        if (r_shadowstage == SHADOWSTAGE_STENCIL)
        {
                // increment stencil if backface is behind depthbuffer
+               //R_Mesh_EndBatch();
                qglCullFace(GL_BACK); // quake is backwards, this culls front faces
                qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
-               R_Mesh_Draw(numverts * 2, tris, shadowelements);
+               R_Mesh_Draw_NoBatching(numverts * 2, tris, shadowelements);
                c_rt_shadowmeshes++;
                c_rt_shadowtris += numtris;
                // decrement stencil if frontface is behind depthbuffer
+               //R_Mesh_EndBatch();
                qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
                qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
        }
-       R_Mesh_Draw(numverts * 2, tris, shadowelements);
+       R_Mesh_Draw_NoBatching(numverts * 2, tris, shadowelements);
        c_rt_shadowmeshes++;
        c_rt_shadowtris += numtris;
 }
@@ -351,17 +354,19 @@ void R_Shadow_RenderShadowMeshVolume(shadowmesh_t *firstmesh)
        if (r_shadowstage == SHADOWSTAGE_STENCIL)
        {
                // increment stencil if backface is behind depthbuffer
+               //R_Mesh_EndBatch();
                qglCullFace(GL_BACK); // quake is backwards, this culls front faces
                qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
                for (mesh = firstmesh;mesh;mesh = mesh->next)
                {
                        R_Mesh_GetSpace(mesh->numverts);
                        R_Mesh_CopyVertex3f(mesh->vertex3f, mesh->numverts);
-                       R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
+                       R_Mesh_Draw_NoBatching(mesh->numverts, mesh->numtriangles, mesh->element3i);
                        c_rtcached_shadowmeshes++;
                        c_rtcached_shadowtris += mesh->numtriangles;
                }
                // decrement stencil if frontface is behind depthbuffer
+               //R_Mesh_EndBatch();
                qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
                qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
        }
@@ -369,7 +374,7 @@ void R_Shadow_RenderShadowMeshVolume(shadowmesh_t *firstmesh)
        {
                R_Mesh_GetSpace(mesh->numverts);
                R_Mesh_CopyVertex3f(mesh->vertex3f, mesh->numverts);
-               R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
+               R_Mesh_Draw_NoBatching(mesh->numverts, mesh->numtriangles, mesh->element3i);
                c_rtcached_shadowmeshes++;
                c_rtcached_shadowtris += mesh->numtriangles;
        }
@@ -517,6 +522,23 @@ void R_Shadow_Stage_Begin(void)
         || r_shadow_lightattenuationpower.value != r_shadow_attenpower
         || r_shadow_lightattenuationscale.value != r_shadow_attenscale)
                R_Shadow_MakeTextures();
+
+       R_Mesh_EndBatch();
+       memset(&m, 0, sizeof(m));
+       m.blendfunc1 = GL_ONE;
+       m.blendfunc2 = GL_ZERO;
+       R_Mesh_State(&m);
+       GL_Color(0, 0, 0, 1);
+       qglDisable(GL_SCISSOR_TEST);
+       r_shadowstage = SHADOWSTAGE_NONE;
+
+       c_rt_lights = c_rt_clears = c_rt_scissored = 0;
+       c_rt_shadowmeshes = c_rt_shadowtris = c_rt_lightmeshes = c_rt_lighttris = 0;
+       c_rtcached_shadowmeshes = c_rtcached_shadowtris = 0;
+}
+
+void R_Shadow_LoadWorldLightsIfNeeded(void)
+{
        if (r_shadow_reloadlights && cl.worldmodel)
        {
                R_Shadow_ClearWorldLights();
@@ -529,22 +551,12 @@ void R_Shadow_Stage_Begin(void)
                                R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite();
                }
        }
-
-       memset(&m, 0, sizeof(m));
-       m.blendfunc1 = GL_ONE;
-       m.blendfunc2 = GL_ZERO;
-       R_Mesh_State(&m);
-       GL_Color(0, 0, 0, 1);
-       r_shadowstage = SHADOWSTAGE_NONE;
-
-       c_rt_lights = c_rt_clears = c_rt_scissored = 0;
-       c_rt_shadowmeshes = c_rt_shadowtris = c_rt_lightmeshes = c_rt_lighttris = 0;
-       c_rtcached_shadowmeshes = c_rtcached_shadowtris = 0;
 }
 
 void R_Shadow_Stage_ShadowVolumes(void)
 {
        rmeshstate_t m;
+       //R_Mesh_EndBatch();
        memset(&m, 0, sizeof(m));
        R_Mesh_TextureState(&m);
        GL_Color(1, 1, 1, 1);
@@ -572,6 +584,7 @@ void R_Shadow_Stage_ShadowVolumes(void)
 void R_Shadow_Stage_LightWithoutShadows(void)
 {
        rmeshstate_t m;
+       //R_Mesh_EndBatch();
        memset(&m, 0, sizeof(m));
        R_Mesh_TextureState(&m);
        qglActiveTexture(GL_TEXTURE0_ARB);
@@ -594,6 +607,7 @@ void R_Shadow_Stage_LightWithoutShadows(void)
 void R_Shadow_Stage_LightWithShadows(void)
 {
        rmeshstate_t m;
+       //R_Mesh_EndBatch();
        memset(&m, 0, sizeof(m));
        R_Mesh_TextureState(&m);
        qglActiveTexture(GL_TEXTURE0_ARB);
@@ -618,6 +632,7 @@ void R_Shadow_Stage_LightWithShadows(void)
 void R_Shadow_Stage_End(void)
 {
        rmeshstate_t m;
+       //R_Mesh_EndBatch();
        // attempt to restore state to what Mesh_State thinks it is
        qglDisable(GL_BLEND);
        qglBlendFunc(GL_ONE, GL_ZERO);
@@ -657,12 +672,14 @@ int R_Shadow_ScissorForBBoxAndSphere(const float *mins, const float *maxs, const
         && r_origin[1] >= mins[1] && r_origin[1] <= maxs[1]
         && r_origin[2] >= mins[2] && r_origin[2] <= maxs[2])
        {
+               //R_Mesh_EndBatch();
                qglDisable(GL_SCISSOR_TEST);
                return false;
        }
        VectorSubtract(r_origin, origin, v);
        if (DotProduct(v, v) < radius * radius)
        {
+               //R_Mesh_EndBatch();
                qglDisable(GL_SCISSOR_TEST);
                return false;
        }
@@ -777,6 +794,7 @@ int R_Shadow_ScissorForBBoxAndSphere(const float *mins, const float *maxs, const
        if (ix2 <= ix1 || iy2 <= iy1)
                return true;
        // set up the scissor rectangle
+       //R_Mesh_EndBatch();
        qglScissor(ix1, iy1, ix2 - ix1, iy2 - iy1);
        qglEnable(GL_SCISSOR_TEST);
        c_rt_scissored++;
@@ -793,8 +811,11 @@ int R_Shadow_ScissorForBBox(const float *mins, const float *maxs)
        if (!r_shadow_scissor.integer)
                return false;
        // if view is inside the box, just say yes it's visible
-       if (BoxesOverlap(r_origin, r_origin, mins, maxs))
+       // LordHavoc: for some odd reason scissor seems broken without stencil
+       // (?!?  seems like a driver bug) so abort if gl_stencil is false
+       if (!gl_stencil || BoxesOverlap(r_origin, r_origin, mins, maxs))
        {
+               //R_Mesh_EndBatch();
                qglDisable(GL_SCISSOR_TEST);
                return false;
        }
@@ -815,8 +836,7 @@ int R_Shadow_ScissorForBBox(const float *mins, const float *maxs)
        if (DotProduct(vpn, v2) <= f)
        {
                // entirely behind nearclip plane
-               qglDisable(GL_SCISSOR_TEST);
-               return false;
+               return true;
        }
        if (DotProduct(vpn, v) >= f)
        {
@@ -949,25 +969,35 @@ int R_Shadow_ScissorForBBox(const float *mins, const float *maxs)
        if (ix2 <= ix1 || iy2 <= iy1)
                return true;
        // set up the scissor rectangle
+       //R_Mesh_EndBatch();
        qglScissor(ix1, iy1, ix2 - ix1, iy2 - iy1);
        qglEnable(GL_SCISSOR_TEST);
        c_rt_scissored++;
        return false;
 }
 
-void R_Shadow_VertexLighting(int numverts, const float *vertex3f, const float *normal3f, const float *lightcolor, const float *relativelightorigin, float lightradius)
+void R_Shadow_VertexLighting(int numverts, const float *vertex3f, const float *normal3f, const float *lightcolor, const matrix4x4_t *m)
 {
        float *color4f = varray_color4f;
-       float dist, dot, intensity, iradius = 1.0f / lightradius, radius2 = lightradius * lightradius, v[3];
+       float dist, dot, intensity, v[3], n[3];
        for (;numverts > 0;numverts--, vertex3f += 3, normal3f += 3, color4f += 4)
        {
-               VectorSubtract(vertex3f, relativelightorigin, v);
-               if ((dot = DotProduct(normal3f, v)) > 0 && (dist = DotProduct(v, v)) < radius2)
+               Matrix4x4_Transform(m, vertex3f, v);
+               if ((dist = DotProduct(v, v)) < 1)
                {
-                       dist = sqrt(dist);
-                       intensity = pow(1 - (dist * iradius), r_shadow_attenpower) * r_shadow_attenscale * dot / dist;
-                       VectorScale(lightcolor, intensity, color4f);
-                       color4f[3] = 1;
+                       Matrix4x4_Transform3x3(m, normal3f, n);
+                       if ((dot = DotProduct(n, v)) > 0)
+                       {
+                               dist = sqrt(dist);
+                               intensity = pow(1 - dist, r_shadow_attenpower) * r_shadow_attenscale * dot / sqrt(DotProduct(n,n));
+                               VectorScale(lightcolor, intensity, color4f);
+                               color4f[3] = 1;
+                       }
+                       else
+                       {
+                               VectorClear(color4f);
+                               color4f[3] = 1;
+                       }
                }
                else
                {
@@ -977,18 +1007,27 @@ void R_Shadow_VertexLighting(int numverts, const float *vertex3f, const float *n
        }
 }
 
-void R_Shadow_VertexLightingWithXYAttenuationTexture(int numverts, const float *vertex3f, const float *normal3f, const float *lightcolor, const float *relativelightorigin, float lightradius, const float *zdir)
+void R_Shadow_VertexLightingWithXYAttenuationTexture(int numverts, const float *vertex3f, const float *normal3f, const float *lightcolor, const matrix4x4_t *m)
 {
        float *color4f = varray_color4f;
-       float dist, dot, intensity, iradius = 1.0f / lightradius, v[3];
+       float dist, dot, intensity, v[3], n[3];
        for (;numverts > 0;numverts--, vertex3f += 3, normal3f += 3, color4f += 4)
        {
-               VectorSubtract(vertex3f, relativelightorigin, v);
-               if ((dot = DotProduct(normal3f, v)) > 0 && (dist = fabs(DotProduct(zdir, v))) < lightradius)
+               Matrix4x4_Transform(m, vertex3f, v);
+               if ((dist = fabs(v[2])) < 1)
                {
-                       intensity = pow(1 - (dist * iradius), r_shadow_attenpower) * r_shadow_attenscale * dot / sqrt(DotProduct(v,v));
-                       VectorScale(lightcolor, intensity, color4f);
-                       color4f[3] = 1;
+                       Matrix4x4_Transform3x3(m, normal3f, n);
+                       if ((dot = DotProduct(n, v)) > 0)
+                       {
+                               intensity = pow(1 - dist, r_shadow_attenpower) * r_shadow_attenscale * dot / sqrt(DotProduct(n,n));
+                               VectorScale(lightcolor, intensity, color4f);
+                               color4f[3] = 1;
+                       }
+                       else
+                       {
+                               VectorClear(color4f);
+                               color4f[3] = 1;
+                       }
                }
                else
                {
@@ -1074,6 +1113,7 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element
                if (r_shadow_texture3d.integer && r_textureunits.integer >= 4)
                {
                        // 3/2 3D combine path (Geforce3, Radeon 8500)
+                       //R_Mesh_EndBatch();
                        m.tex[0] = R_GetTexture(bumptexture);
                        m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
                        m.tex3d[2] = R_GetTexture(r_shadow_attenuation3dtexture);
@@ -1088,10 +1128,11 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element
                        R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
                        R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin);
                        R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[2], numverts, vertex3f, matrix_modeltoattenuationxyz);
-                       R_Mesh_Draw(numverts, numtriangles, elements);
+                       R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
                        c_rt_lightmeshes++;
                        c_rt_lighttris += numtriangles;
 
+                       //R_Mesh_EndBatch();
                        m.tex[0] = R_GetTexture(basetexture);
                        m.tex[1] = 0;
                        m.texcubemap[1] = R_GetTexture(lightcubemap);
@@ -1102,7 +1143,11 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element
                        qglColorMask(1,1,1,0);
                        qglBlendFunc(GL_DST_ALPHA, GL_ONE);
                        qglEnable(GL_BLEND);
-
+                       R_Mesh_GetSpace(numverts);
+                       R_Mesh_CopyVertex3f(vertex3f, numverts);
+                       R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
+                       if (lightcubemap)
+                               R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter);
                        VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color2);
                        for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
                        {
@@ -1110,12 +1155,7 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element
                                color[1] = bound(0, color2[1], 1);
                                color[2] = bound(0, color2[2], 1);
                                GL_Color(color[0], color[1], color[2], 1);
-                               R_Mesh_GetSpace(numverts);
-                               R_Mesh_CopyVertex3f(vertex3f, numverts);
-                               R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
-                               if (lightcubemap)
-                                       R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter);
-                               R_Mesh_Draw(numverts, numtriangles, elements);
+                               R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
                                c_rt_lightmeshes++;
                                c_rt_lighttris += numtriangles;
                        }
@@ -1123,6 +1163,7 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element
                else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && lightcubemap)
                {
                        // 1/2/2 3D combine path (original Radeon)
+                       //R_Mesh_EndBatch();
                        m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
                        R_Mesh_TextureState(&m);
                        qglColorMask(0,0,0,1);
@@ -1131,10 +1172,11 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element
                        R_Mesh_GetSpace(numverts);
                        R_Mesh_CopyVertex3f(vertex3f, numverts);
                        R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[0], numverts, vertex3f, matrix_modeltoattenuationxyz);
-                       R_Mesh_Draw(numverts, numtriangles, elements);
+                       R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
                        c_rt_lightmeshes++;
                        c_rt_lighttris += numtriangles;
 
+                       //R_Mesh_EndBatch();
                        m.tex[0] = R_GetTexture(bumptexture);
                        m.tex3d[0] = 0;
                        m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
@@ -1147,10 +1189,11 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element
                        R_Mesh_CopyVertex3f(vertex3f, numverts);
                        R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
                        R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin);
-                       R_Mesh_Draw(numverts, numtriangles, elements);
+                       R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
                        c_rt_lightmeshes++;
                        c_rt_lighttris += numtriangles;
 
+                       //R_Mesh_EndBatch();
                        m.tex[0] = R_GetTexture(basetexture);
                        m.texcubemap[1] = R_GetTexture(lightcubemap);
                        m.texcombinergb[0] = GL_MODULATE;
@@ -1158,7 +1201,11 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element
                        R_Mesh_TextureState(&m);
                        qglColorMask(1,1,1,0);
                        qglBlendFunc(GL_DST_ALPHA, GL_ONE);
-
+                       R_Mesh_GetSpace(numverts);
+                       R_Mesh_CopyVertex3f(vertex3f, numverts);
+                       R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
+                       if (lightcubemap)
+                               R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter);
                        VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color2);
                        for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
                        {
@@ -1166,12 +1213,7 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element
                                color[1] = bound(0, color2[1], 1);
                                color[2] = bound(0, color2[2], 1);
                                GL_Color(color[0], color[1], color[2], 1);
-                               R_Mesh_GetSpace(numverts);
-                               R_Mesh_CopyVertex3f(vertex3f, numverts);
-                               R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
-                               if (lightcubemap)
-                                       R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter);
-                               R_Mesh_Draw(numverts, numtriangles, elements);
+                               R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
                                c_rt_lightmeshes++;
                                c_rt_lighttris += numtriangles;
                        }
@@ -1179,6 +1221,7 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element
                else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && !lightcubemap)
                {
                        // 2/2 3D combine path (original Radeon)
+                       //R_Mesh_EndBatch();
                        m.tex[0] = R_GetTexture(bumptexture);
                        m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
                        m.texcombinergb[0] = GL_REPLACE;
@@ -1191,20 +1234,25 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element
                        R_Mesh_CopyVertex3f(vertex3f, numverts);
                        R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
                        R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin);
-                       R_Mesh_Draw(numverts, numtriangles, elements);
+                       R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
                        c_rt_lightmeshes++;
                        c_rt_lighttris += numtriangles;
 
+                       //R_Mesh_EndBatch();
                        m.tex[0] = R_GetTexture(basetexture);
                        m.tex3d[1] = R_GetTexture(r_shadow_attenuation3dtexture);
                        m.texcubemap[1] = 0;
                        m.texcombinergb[0] = GL_MODULATE;
                        m.texcombinergb[1] = GL_MODULATE;
                        R_Mesh_TextureState(&m);
+                       R_Mesh_GetSpace(numverts);
                        qglColorMask(1,1,1,0);
                        qglBlendFunc(GL_DST_ALPHA, GL_ONE);
                        qglEnable(GL_BLEND);
-
+                       R_Mesh_GetSpace(numverts);
+                       R_Mesh_CopyVertex3f(vertex3f, numverts);
+                       R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
+                       R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltoattenuationxyz);
                        VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color2);
                        for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
                        {
@@ -1212,11 +1260,7 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element
                                color[1] = bound(0, color2[1], 1);
                                color[2] = bound(0, color2[2], 1);
                                GL_Color(color[0], color[1], color[2], 1);
-                               R_Mesh_GetSpace(numverts);
-                               R_Mesh_CopyVertex3f(vertex3f, numverts);
-                               R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
-                               R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltoattenuationxyz);
-                               R_Mesh_Draw(numverts, numtriangles, elements);
+                               R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
                                c_rt_lightmeshes++;
                                c_rt_lighttris += numtriangles;
                        }
@@ -1224,6 +1268,7 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element
                else if (r_textureunits.integer >= 4)
                {
                        // 4/2 2D combine path (Geforce3, Radeon 8500)
+                       //R_Mesh_EndBatch();
                        m.tex[0] = R_GetTexture(bumptexture);
                        m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
                        m.texcombinergb[0] = GL_REPLACE;
@@ -1240,10 +1285,11 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element
                        R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin);
                        R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[2], numverts, vertex3f, matrix_modeltoattenuationxyz);
                        R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[3], numverts, vertex3f, matrix_modeltoattenuationz);
-                       R_Mesh_Draw(numverts, numtriangles, elements);
+                       R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
                        c_rt_lightmeshes++;
                        c_rt_lighttris += numtriangles;
 
+                       //R_Mesh_EndBatch();
                        m.tex[0] = R_GetTexture(basetexture);
                        m.texcubemap[1] = R_GetTexture(lightcubemap);
                        m.texcombinergb[0] = GL_MODULATE;
@@ -1254,7 +1300,11 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element
                        qglColorMask(1,1,1,0);
                        qglBlendFunc(GL_DST_ALPHA, GL_ONE);
                        qglEnable(GL_BLEND);
-
+                       R_Mesh_GetSpace(numverts);
+                       R_Mesh_CopyVertex3f(vertex3f, numverts);
+                       R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
+                       if (lightcubemap)
+                               R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter);
                        VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color2);
                        for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
                        {
@@ -1262,12 +1312,7 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element
                                color[1] = bound(0, color2[1], 1);
                                color[2] = bound(0, color2[2], 1);
                                GL_Color(color[0], color[1], color[2], 1);
-                               R_Mesh_GetSpace(numverts);
-                               R_Mesh_CopyVertex3f(vertex3f, numverts);
-                               R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
-                               if (lightcubemap)
-                                       R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter);
-                               R_Mesh_Draw(numverts, numtriangles, elements);
+                               R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
                                c_rt_lightmeshes++;
                                c_rt_lighttris += numtriangles;
                        }
@@ -1275,6 +1320,7 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element
                else
                {
                        // 2/2/2 2D combine path (any dot3 card)
+                       //R_Mesh_EndBatch();
                        m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
                        m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
                        R_Mesh_TextureState(&m);
@@ -1285,10 +1331,11 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element
                        R_Mesh_CopyVertex3f(vertex3f, numverts);
                        R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[0], numverts, vertex3f, matrix_modeltoattenuationxyz);
                        R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[1], numverts, vertex3f, matrix_modeltoattenuationz);
-                       R_Mesh_Draw(numverts, numtriangles, elements);
+                       R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
                        c_rt_lightmeshes++;
                        c_rt_lighttris += numtriangles;
 
+                       //R_Mesh_EndBatch();
                        m.tex[0] = R_GetTexture(bumptexture);
                        m.tex[1] = 0;
                        m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
@@ -1301,10 +1348,11 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element
                        R_Mesh_CopyVertex3f(vertex3f, numverts);
                        R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
                        R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin);
-                       R_Mesh_Draw(numverts, numtriangles, elements);
+                       R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
                        c_rt_lightmeshes++;
                        c_rt_lighttris += numtriangles;
 
+                       //R_Mesh_EndBatch();
                        m.tex[0] = R_GetTexture(basetexture);
                        m.texcubemap[1] = R_GetTexture(lightcubemap);
                        m.texcombinergb[0] = GL_MODULATE;
@@ -1312,7 +1360,11 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element
                        R_Mesh_TextureState(&m);
                        qglColorMask(1,1,1,0);
                        qglBlendFunc(GL_DST_ALPHA, GL_ONE);
-
+                       R_Mesh_GetSpace(numverts);
+                       R_Mesh_CopyVertex3f(vertex3f, numverts);
+                       R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
+                       if (lightcubemap)
+                               R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter);
                        VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color2);
                        for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
                        {
@@ -1320,12 +1372,7 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element
                                color[1] = bound(0, color2[1], 1);
                                color[2] = bound(0, color2[2], 1);
                                GL_Color(color[0], color[1], color[2], 1);
-                               R_Mesh_GetSpace(numverts);
-                               R_Mesh_CopyVertex3f(vertex3f, numverts);
-                               R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
-                               if (lightcubemap)
-                                       R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter);
-                               R_Mesh_Draw(numverts, numtriangles, elements);
+                               R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
                                c_rt_lightmeshes++;
                                c_rt_lighttris += numtriangles;
                        }
@@ -1336,6 +1383,7 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element
                if (r_textureunits.integer >= 2)
                {
                        // voodoo2
+                       //R_Mesh_EndBatch();
 #if 1
                        m.tex[0] = R_GetTexture(basetexture);
                        m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
@@ -1360,13 +1408,16 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element
                                R_Mesh_CopyVertex3f(vertex3f, numverts);
                                R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
                                R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[1], numverts, vertex3f, matrix_modeltoattenuationxyz);
-                               R_Shadow_VertexLightingWithXYAttenuationTexture(numverts, vertex3f, normal3f, color2, relativelightorigin, lightradius, matrix_modeltofilter->m[2]);
-                               R_Mesh_Draw(numverts, numtriangles, elements);
+                               R_Shadow_VertexLightingWithXYAttenuationTexture(numverts, vertex3f, normal3f, color, matrix_modeltofilter);
+                               R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
+                               c_rt_lightmeshes++;
+                               c_rt_lighttris += numtriangles;
                        }
                }
                else
                {
                        // voodoo1
+                       //R_Mesh_EndBatch();
 #if 1
                        m.tex[0] = R_GetTexture(basetexture);
                        R_Mesh_TextureState(&m);
@@ -1388,9 +1439,10 @@ void R_Shadow_DiffuseLighting(int numverts, int numtriangles, const int *element
                                R_Mesh_GetSpace(numverts);
                                R_Mesh_CopyVertex3f(vertex3f, numverts);
                                R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
-                               VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value, color);
-                               R_Shadow_VertexLighting(numverts, vertex3f, normal3f, color, relativelightorigin, lightradius);
-                               R_Mesh_Draw(numverts, numtriangles, elements);
+                               R_Shadow_VertexLighting(numverts, vertex3f, normal3f, color, matrix_modeltofilter);
+                               R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
+                               c_rt_lightmeshes++;
+                               c_rt_lighttris += numtriangles;
                        }
                }
        }
@@ -1412,59 +1464,68 @@ void R_Shadow_SpecularLighting(int numverts, int numtriangles, const int *elemen
        {
                if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && lightcubemap /*&& gl_support_blendsquare*/) // FIXME: detect blendsquare!
                {
-                       // 2/0/0/0/1/2 3D combine blendsquare path
+                       // 2/0/0/1/2 3D combine blendsquare path
+                       //R_Mesh_EndBatch();
                        m.tex[0] = R_GetTexture(bumptexture);
                        m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
                        m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
                        R_Mesh_TextureState(&m);
                        qglColorMask(0,0,0,1);
-                       qglDisable(GL_BLEND);
+                       // this squares the result
+                       qglEnable(GL_BLEND);
+                       qglBlendFunc(GL_SRC_ALPHA, GL_ZERO);
                        GL_Color(1,1,1,1);
                        R_Mesh_GetSpace(numverts);
                        R_Mesh_CopyVertex3f(vertex3f, numverts);
                        R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
                        R_Shadow_GenTexCoords_Specular_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin, relativeeyeorigin);
-                       R_Mesh_Draw(numverts, numtriangles, elements);
+                       R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
                        c_rt_lightmeshes++;
                        c_rt_lighttris += numtriangles;
 
+                       //R_Mesh_EndBatch();
                        m.tex[0] = 0;
                        m.texcubemap[1] = 0;
                        m.texcombinergb[1] = GL_MODULATE;
                        R_Mesh_TextureState(&m);
                        // square alpha in framebuffer a few times to make it shiny
                        qglBlendFunc(GL_ZERO, GL_DST_ALPHA);
-                       qglEnable(GL_BLEND);
                        // these comments are a test run through this math for intensity 0.5
-                       // 0.5 * 0.5 = 0.25
-                       // 0.25 * 0.25 = 0.0625
-                       // 0.0625 * 0.0625 = 0.00390625
-                       for (renders = 0;renders < 3;renders++)
-                       {
-                               R_Mesh_GetSpace(numverts);
-                               R_Mesh_CopyVertex3f(vertex3f, numverts);
-                               R_Mesh_Draw(numverts, numtriangles, elements);
-                       }
-                       c_rt_lightmeshes += 3;
-                       c_rt_lighttris += numtriangles * 3;
+                       // 0.5 * 0.5 = 0.25 (done by the BlendFunc earlier)
+                       // 0.25 * 0.25 = 0.0625 (this is another pass)
+                       // 0.0625 * 0.0625 = 0.00390625 (this is another pass)
+                       R_Mesh_GetSpace(numverts);
+                       R_Mesh_CopyVertex3f(vertex3f, numverts);
+                       R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
+                       c_rt_lightmeshes++;
+                       c_rt_lighttris += numtriangles;
+                       R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
+                       c_rt_lightmeshes++;
+                       c_rt_lighttris += numtriangles;
 
+                       //R_Mesh_EndBatch();
                        m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
                        R_Mesh_TextureState(&m);
                        qglBlendFunc(GL_DST_ALPHA, GL_ZERO);
                        R_Mesh_GetSpace(numverts);
                        R_Mesh_CopyVertex3f(vertex3f, numverts);
                        R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[0], numverts, vertex3f, matrix_modeltoattenuationxyz);
-                       R_Mesh_Draw(numverts, numtriangles, elements);
+                       R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
                        c_rt_lightmeshes++;
                        c_rt_lighttris += numtriangles;
 
+                       //R_Mesh_EndBatch();
                        m.tex3d[0] = 0;
                        m.tex[0] = R_GetTexture(glosstexture);
                        m.texcubemap[1] = R_GetTexture(lightcubemap);
                        R_Mesh_TextureState(&m);
                        qglColorMask(1,1,1,0);
                        qglBlendFunc(GL_DST_ALPHA, GL_ONE);
-
+                       R_Mesh_GetSpace(numverts);
+                       R_Mesh_CopyVertex3f(vertex3f, numverts);
+                       R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
+                       if (lightcubemap)
+                               R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter);
                        VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value * 0.25f, color2);
                        for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
                        {
@@ -1472,54 +1533,54 @@ void R_Shadow_SpecularLighting(int numverts, int numtriangles, const int *elemen
                                color[1] = bound(0, color2[1], 1);
                                color[2] = bound(0, color2[2], 1);
                                GL_Color(color[0], color[1], color[2], 1);
-                               R_Mesh_GetSpace(numverts);
-                               R_Mesh_CopyVertex3f(vertex3f, numverts);
-                               R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
-                               if (lightcubemap)
-                                       R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter);
-                               R_Mesh_Draw(numverts, numtriangles, elements);
+                               R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
                                c_rt_lightmeshes++;
                                c_rt_lighttris += numtriangles;
                        }
                }
                else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && !lightcubemap /*&& gl_support_blendsquare*/) // FIXME: detect blendsquare!
                {
-                       // 2/0/0/0/2 3D combine blendsquare path
+                       // 2/0/0/2 3D combine blendsquare path
+                       //R_Mesh_EndBatch();
                        m.tex[0] = R_GetTexture(bumptexture);
                        m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
                        m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
                        R_Mesh_TextureState(&m);
                        qglColorMask(0,0,0,1);
-                       qglDisable(GL_BLEND);
+                       // this squares the result
+                       qglEnable(GL_BLEND);
+                       qglBlendFunc(GL_SRC_ALPHA, GL_ZERO);
                        GL_Color(1,1,1,1);
                        R_Mesh_GetSpace(numverts);
                        R_Mesh_CopyVertex3f(vertex3f, numverts);
                        R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
                        R_Shadow_GenTexCoords_Specular_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin, relativeeyeorigin);
-                       R_Mesh_Draw(numverts, numtriangles, elements);
+                       R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
                        c_rt_lightmeshes++;
                        c_rt_lighttris += numtriangles;
 
+                       //R_Mesh_EndBatch();
                        m.tex[0] = 0;
                        m.texcubemap[1] = 0;
                        m.texcombinergb[1] = GL_MODULATE;
                        R_Mesh_TextureState(&m);
                        // square alpha in framebuffer a few times to make it shiny
                        qglBlendFunc(GL_ZERO, GL_DST_ALPHA);
-                       qglEnable(GL_BLEND);
                        // these comments are a test run through this math for intensity 0.5
-                       // 0.5 * 0.5 = 0.25
-                       // 0.25 * 0.25 = 0.0625
-                       // 0.0625 * 0.0625 = 0.00390625
-                       for (renders = 0;renders < 3;renders++)
-                       {
-                               R_Mesh_GetSpace(numverts);
-                               R_Mesh_CopyVertex3f(vertex3f, numverts);
-                               R_Mesh_Draw(numverts, numtriangles, elements);
-                       }
-                       c_rt_lightmeshes += 3;
-                       c_rt_lighttris += numtriangles * 3;
+                       // 0.5 * 0.5 = 0.25 (done by the BlendFunc earlier)
+                       // 0.25 * 0.25 = 0.0625 (this is another pass)
+                       // 0.0625 * 0.0625 = 0.00390625 (this is another pass)
+                       R_Mesh_GetSpace(numverts);
+                       R_Mesh_CopyVertex3f(vertex3f, numverts);
+                       R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
+                       c_rt_lightmeshes++;
+                       c_rt_lighttris += numtriangles;
+                       R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
+                       c_rt_lightmeshes++;
+                       c_rt_lighttris += numtriangles;
 
+                       //R_Mesh_EndBatch();
+                       R_Mesh_GetSpace(numverts);
                        R_Mesh_CopyVertex3f(vertex3f, numverts);
                        R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
                        m.tex[0] = R_GetTexture(glosstexture);
@@ -1527,9 +1588,10 @@ void R_Shadow_SpecularLighting(int numverts, int numtriangles, const int *elemen
                        R_Mesh_TextureState(&m);
                        qglColorMask(1,1,1,0);
                        qglBlendFunc(GL_DST_ALPHA, GL_ONE);
-                       c_rt_lightmeshes++;
-                       c_rt_lighttris += numtriangles;
-
+                       R_Mesh_GetSpace(numverts);
+                       R_Mesh_CopyVertex3f(vertex3f, numverts);
+                       R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
+                       R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltoattenuationxyz);
                        VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value * 0.25f, color2);
                        for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
                        {
@@ -1537,53 +1599,53 @@ void R_Shadow_SpecularLighting(int numverts, int numtriangles, const int *elemen
                                color[1] = bound(0, color2[1], 1);
                                color[2] = bound(0, color2[2], 1);
                                GL_Color(color[0], color[1], color[2], 1);
-                               R_Mesh_GetSpace(numverts);
-                               R_Mesh_CopyVertex3f(vertex3f, numverts);
-                               R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
-                               R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltoattenuationxyz);
-                               R_Mesh_Draw(numverts, numtriangles, elements);
+                               R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
                                c_rt_lightmeshes++;
                                c_rt_lighttris += numtriangles;
                        }
                }
                else if (r_textureunits.integer >= 2 /*&& gl_support_blendsquare*/) // FIXME: detect blendsquare!
                {
-                       // 2/0/0/0/2/2 2D combine blendsquare path
+                       // 2/0/0/2/2 2D combine blendsquare path
+                       //R_Mesh_EndBatch();
                        m.tex[0] = R_GetTexture(bumptexture);
                        m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
                        m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
                        R_Mesh_TextureState(&m);
                        qglColorMask(0,0,0,1);
-                       qglDisable(GL_BLEND);
+                       // this squares the result
+                       qglEnable(GL_BLEND);
+                       qglBlendFunc(GL_SRC_ALPHA, GL_ZERO);
                        GL_Color(1,1,1,1);
                        R_Mesh_GetSpace(numverts);
                        R_Mesh_CopyVertex3f(vertex3f, numverts);
                        R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
                        R_Shadow_GenTexCoords_Specular_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin, relativeeyeorigin);
-                       R_Mesh_Draw(numverts, numtriangles, elements);
+                       R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
                        c_rt_lightmeshes++;
                        c_rt_lighttris += numtriangles;
 
+                       //R_Mesh_EndBatch();
                        m.tex[0] = 0;
                        m.texcubemap[1] = 0;
                        m.texcombinergb[1] = GL_MODULATE;
                        R_Mesh_TextureState(&m);
                        // square alpha in framebuffer a few times to make it shiny
                        qglBlendFunc(GL_ZERO, GL_DST_ALPHA);
-                       qglEnable(GL_BLEND);
                        // these comments are a test run through this math for intensity 0.5
-                       // 0.5 * 0.5 = 0.25
-                       // 0.25 * 0.25 = 0.0625
-                       // 0.0625 * 0.0625 = 0.00390625
-                       for (renders = 0;renders < 3;renders++)
-                       {
-                               R_Mesh_GetSpace(numverts);
-                               R_Mesh_CopyVertex3f(vertex3f, numverts);
-                               R_Mesh_Draw(numverts, numtriangles, elements);
-                       }
-                       c_rt_lightmeshes += 3;
-                       c_rt_lighttris += numtriangles * 3;
+                       // 0.5 * 0.5 = 0.25 (done by the BlendFunc earlier)
+                       // 0.25 * 0.25 = 0.0625 (this is another pass)
+                       // 0.0625 * 0.0625 = 0.00390625 (this is another pass)
+                       R_Mesh_GetSpace(numverts);
+                       R_Mesh_CopyVertex3f(vertex3f, numverts);
+                       R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
+                       c_rt_lightmeshes++;
+                       c_rt_lighttris += numtriangles;
+                       R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
+                       c_rt_lightmeshes++;
+                       c_rt_lighttris += numtriangles;
 
+                       //R_Mesh_EndBatch();
                        m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
                        m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
                        R_Mesh_TextureState(&m);
@@ -1592,16 +1654,21 @@ void R_Shadow_SpecularLighting(int numverts, int numtriangles, const int *elemen
                        R_Mesh_CopyVertex3f(vertex3f, numverts);
                        R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[0], numverts, vertex3f, matrix_modeltoattenuationxyz);
                        R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[1], numverts, vertex3f, matrix_modeltoattenuationz);
-                       R_Mesh_Draw(numverts, numtriangles, elements);
+                       R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
                        c_rt_lightmeshes++;
                        c_rt_lighttris += numtriangles;
 
+                       //R_Mesh_EndBatch();
                        m.tex[0] = R_GetTexture(glosstexture);
                        m.texcubemap[1] = R_GetTexture(lightcubemap);
                        R_Mesh_TextureState(&m);
                        qglColorMask(1,1,1,0);
                        qglBlendFunc(GL_DST_ALPHA, GL_ONE);
-
+                       R_Mesh_GetSpace(numverts);
+                       R_Mesh_CopyVertex3f(vertex3f, numverts);
+                       R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
+                       if (lightcubemap)
+                               R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter);
                        VectorScale(lightcolor, r_colorscale * r_shadow_lightintensityscale.value * 0.25f, color2);
                        for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
                        {
@@ -1609,12 +1676,7 @@ void R_Shadow_SpecularLighting(int numverts, int numtriangles, const int *elemen
                                color[1] = bound(0, color2[1], 1);
                                color[2] = bound(0, color2[2], 1);
                                GL_Color(color[0], color[1], color[2], 1);
-                               R_Mesh_GetSpace(numverts);
-                               R_Mesh_CopyVertex3f(vertex3f, numverts);
-                               R_Mesh_CopyTexCoord2f(0, texcoord2f, numverts);
-                               if (lightcubemap)
-                                       R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter);
-                               R_Mesh_Draw(numverts, numtriangles, elements);
+                               R_Mesh_Draw_NoBatching(numverts, numtriangles, elements);
                                c_rt_lightmeshes++;
                                c_rt_lighttris += numtriangles;
                        }
@@ -1811,9 +1873,8 @@ void R_Shadow_NewWorldLight(vec3_t origin, float radius, vec3_t color, int style
 
                                // now that we have the buffers big enough, construct shadow volume mesh
                                memcpy(vertex3f, castmesh->vertex3f, castmesh->numverts * sizeof(float[3]));
-                               R_Shadow_ProjectVertex3f(vertex3f, castmesh->numverts, e->origin, r_shadow_projectdistance.value);//, e->lightradius);
                                tris = R_Shadow_MakeTriangleShadowFlags_Vertex3f(castmesh->element3i, vertex3f, castmesh->numtriangles, trianglefacinglight, trianglefacinglightlist, e->origin);
-                               tris = R_Shadow_BuildShadowVolumeTriangles(castmesh->element3i, castmesh->neighbor3i, castmesh->numverts, trianglefacinglight, trianglefacinglightlist, tris, shadowelements);
+                               tris = R_Shadow_BuildShadowVolume(castmesh->element3i, castmesh->neighbor3i, castmesh->numverts, trianglefacinglight, trianglefacinglightlist, tris, shadowelements, vertex3f, e->origin, r_shadow_projectdistance.value);
                                // add the constructed shadow volume mesh
                                Mod_ShadowMesh_AddMesh(r_shadow_mempool, e->shadowvolume, castmesh->numverts, vertex3f, tris, shadowelements);
                        }
@@ -2254,18 +2315,6 @@ void R_Shadow_SetCursorLocationForView(void)
        r_editlights_cursorlocation[2] = floor(endpos[2] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
 }
 
-void R_Shadow_UpdateLightingMode(void)
-{
-       r_shadow_lightingmode = 0;
-       if (r_shadow_realtime.integer)
-       {
-               if (r_shadow_worldlightchain)
-                       r_shadow_lightingmode = 2;
-               else
-                       r_shadow_lightingmode = 1;
-       }
-}
-
 void R_Shadow_UpdateWorldLightSelection(void)
 {
        R_Shadow_SetCursorLocationForView();