]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - r_shadow.c
added GL_AlphaTest function to enable/disable GL_ALPHA_TEST
[xonotic/darkplaces.git] / r_shadow.c
index dcfdef54285da3fd9d994f99d546c073de4858e0..3b12bbd578f62d84ffda4b3af56111b391e4f4c1 100644 (file)
@@ -702,7 +702,6 @@ void R_Shadow_MarkVolumeFromBox(int firsttriangle, int numtris, const float *inv
 
 void R_Shadow_RenderVolume(int numvertices, int numtriangles, const float *vertex3f, const int *element3i)
 {
-       rmeshstate_t m;
        if (r_shadow_compilingrtlight)
        {
                // if we're compiling an rtlight, capture the mesh
@@ -710,9 +709,7 @@ void R_Shadow_RenderVolume(int numvertices, int numtriangles, const float *verte
                return;
        }
        renderstats.lights_shadowtriangles += numtriangles;
-       memset(&m, 0, sizeof(m));
-       m.pointer_vertex = vertex3f;
-       R_Mesh_State(&m);
+       R_Mesh_VertexPointer(vertex3f);
        GL_LockArrays(0, numvertices);
        if (r_shadow_rendermode == R_SHADOW_RENDERMODE_STENCIL)
        {
@@ -811,8 +808,6 @@ matrix4x4_t r_shadow_entitytoattenuationz;
 
 void R_Shadow_RenderMode_Begin(void)
 {
-       rmeshstate_t m;
-
        R_Shadow_ValidateCvars();
 
        if (!r_shadow_attenuation2dtexture
@@ -821,8 +816,8 @@ void R_Shadow_RenderMode_Begin(void)
         || r_shadow_lightattenuationscale.value != r_shadow_attenscale)
                R_Shadow_MakeTextures();
 
-       memset(&m, 0, sizeof(m));
-       R_Mesh_State(&m);
+       R_Mesh_ColorPointer(NULL);
+       R_Mesh_ResetTextureState();
        GL_BlendFunc(GL_ONE, GL_ZERO);
        GL_DepthMask(false);
        GL_DepthTest(true);
@@ -853,7 +848,6 @@ void R_Shadow_RenderMode_ActiveLight(rtlight_t *rtlight)
 
 void R_Shadow_RenderMode_Reset(void)
 {
-       rmeshstate_t m;
        if (r_shadow_rendermode == R_SHADOW_RENDERMODE_LIGHT_GLSL)
        {
                qglUseProgramObjectARB(0);
@@ -864,8 +858,8 @@ void R_Shadow_RenderMode_Reset(void)
        }
        else if (r_shadow_rendermode == R_SHADOW_RENDERMODE_STENCILTWOSIDE)
                qglDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
-       memset(&m, 0, sizeof(m));
-       R_Mesh_State(&m);
+       R_Mesh_ColorPointer(NULL);
+       R_Mesh_ResetTextureState();
 }
 
 void R_Shadow_RenderMode_StencilShadowVolumes(void)
@@ -940,6 +934,9 @@ void R_Shadow_RenderMode_Lighting(qboolean stenciltest, qboolean transparent)
                R_Mesh_TexBind(4, R_GetTexture(r_texture_fogattenuation)); // fog
                R_Mesh_TexBind(5, R_GetTexture(r_texture_white)); // pants
                R_Mesh_TexBind(6, R_GetTexture(r_texture_white)); // shirt
+               R_Mesh_TexBind(7, R_GetTexture(r_texture_white)); // lightmap
+               R_Mesh_TexBind(8, R_GetTexture(r_texture_blanknormalmap)); // deluxemap
+               R_Mesh_TexBind(9, R_GetTexture(r_texture_black)); // glow
                //R_Mesh_TexMatrix(3, r_shadow_entitytolight); // light filter matrix
                GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
                GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 0);
@@ -1237,16 +1234,13 @@ static void R_Shadow_RenderSurfacesLighting_VisibleLighting(const entity_render_
        // used to display how many times a surface is lit for level design purposes
        int surfacelistindex;
        model_t *model = ent->model;
-       rmeshstate_t m;
        GL_Color(0.1, 0.025, 0, 1);
-       memset(&m, 0, sizeof(m));
-       R_Mesh_State(&m);
-       RSurf_SetPointersForPass(false, false);
+       R_Mesh_ColorPointer(NULL);
+       R_Mesh_ResetTextureState();
+       RSurf_PrepareVerticesForBatch(ent, texture, r_shadow_entityeyeorigin, false, false, numsurfaces, surfacelist);
        for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
        {
                const msurface_t *surface = surfacelist[surfacelistindex];
-               if (rsurface_dynamicvertex)
-                       RSurf_PrepareDynamicSurfaceVertices(surface);
                GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
                R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, model->surfmesh.data_element3i + 3 * surface->num_firsttriangle);
                GL_LockArrays(0, 0);
@@ -1258,22 +1252,23 @@ static void R_Shadow_RenderSurfacesLighting_Light_GLSL(const entity_render_t *en
        // ARB2 GLSL shader path (GFFX5200, Radeon 9500)
        int surfacelistindex;
        model_t *model = ent->model;
-       R_SetupSurfaceShader(ent, texture, r_shadow_entityeyeorigin, lightcolorbase, false);
-       RSurf_SetPointersForPass(false, true);
+       RSurf_PrepareVerticesForBatch(ent, texture, r_shadow_entityeyeorigin, true, true, numsurfaces, surfacelist);
+       R_SetupSurfaceShader(ent, texture, NULL, r_shadow_entityeyeorigin, lightcolorbase, false);
+       R_Mesh_TexCoordPointer(0, 2, model->surfmesh.data_texcoordtexture2f);
+       R_Mesh_TexCoordPointer(1, 3, rsurface_svector3f);
+       R_Mesh_TexCoordPointer(2, 3, rsurface_tvector3f);
+       R_Mesh_TexCoordPointer(3, 3, rsurface_normal3f);
+       if (texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
+               qglDepthFunc(GL_EQUAL);
        for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
        {
                const msurface_t *surface = surfacelist[surfacelistindex];
-               const int *elements = model->surfmesh.data_element3i + surface->num_firsttriangle * 3;
-               if (rsurface_dynamicvertex)
-                       RSurf_PrepareDynamicSurfaceVertices(surface);
-               R_Mesh_TexCoordPointer(0, 2, model->surfmesh.data_texcoordtexture2f);
-               R_Mesh_TexCoordPointer(1, 3, rsurface_svector3f);
-               R_Mesh_TexCoordPointer(2, 3, rsurface_tvector3f);
-               R_Mesh_TexCoordPointer(3, 3, rsurface_normal3f);
                GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
-               R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, elements);
-               GL_LockArrays(0, 0);
+               R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, model->surfmesh.data_element3i + surface->num_firsttriangle * 3);
        }
+       if (texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
+               qglDepthFunc(GL_LEQUAL);
+       GL_LockArrays(0, 0);
 }
 
 static void R_Shadow_RenderSurfacesLighting_Light_Dot3_AmbientPass(const entity_render_t *ent, const texture_t *texture, const msurface_t *surface, const vec3_t lightcolorbase, rtexture_t *basetexture, float colorscale)
@@ -1295,7 +1290,6 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_AmbientPass(const entity_
        {
                // 3 3D combine path (Geforce3, Radeon 8500)
                memset(&m, 0, sizeof(m));
-               m.pointer_vertex = rsurface_vertex3f;
                m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
                m.pointer_texcoord3f[0] = rsurface_vertex3f;
                m.texmatrix[0] = r_shadow_entitytoattenuationxyz;
@@ -1311,7 +1305,6 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_AmbientPass(const entity_
        {
                // 2 3D combine path (Geforce3, original Radeon)
                memset(&m, 0, sizeof(m));
-               m.pointer_vertex = rsurface_vertex3f;
                m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
                m.pointer_texcoord3f[0] = rsurface_vertex3f;
                m.texmatrix[0] = r_shadow_entitytoattenuationxyz;
@@ -1324,7 +1317,6 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_AmbientPass(const entity_
        {
                // 4 2D combine path (Geforce3, Radeon 8500)
                memset(&m, 0, sizeof(m));
-               m.pointer_vertex = rsurface_vertex3f;
                m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
                m.pointer_texcoord3f[0] = rsurface_vertex3f;
                m.texmatrix[0] = r_shadow_entitytoattenuationxyz;
@@ -1346,7 +1338,6 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_AmbientPass(const entity_
        {
                // 3 2D combine path (Geforce3, original Radeon)
                memset(&m, 0, sizeof(m));
-               m.pointer_vertex = rsurface_vertex3f;
                m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
                m.pointer_texcoord3f[0] = rsurface_vertex3f;
                m.texmatrix[0] = r_shadow_entitytoattenuationxyz;
@@ -1362,14 +1353,13 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_AmbientPass(const entity_
        {
                // 2/2/2 2D combine path (any dot3 card)
                memset(&m, 0, sizeof(m));
-               m.pointer_vertex = rsurface_vertex3f;
                m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
                m.pointer_texcoord3f[0] = rsurface_vertex3f;
                m.texmatrix[0] = r_shadow_entitytoattenuationxyz;
                m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
                m.pointer_texcoord3f[1] = rsurface_vertex3f;
                m.texmatrix[1] = r_shadow_entitytoattenuationz;
-               R_Mesh_State(&m);
+               R_Mesh_TextureState(&m);
                GL_ColorMask(0,0,0,1);
                GL_BlendFunc(GL_ONE, GL_ZERO);
                GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
@@ -1377,7 +1367,6 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_AmbientPass(const entity_
                GL_LockArrays(0, 0);
 
                memset(&m, 0, sizeof(m));
-               m.pointer_vertex = rsurface_vertex3f;
                m.tex[0] = R_GetTexture(basetexture);
                m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
                m.texmatrix[0] = texture->currenttexmatrix;
@@ -1390,7 +1379,7 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_AmbientPass(const entity_
                GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
        }
        // this final code is shared
-       R_Mesh_State(&m);
+       R_Mesh_TextureState(&m);
        GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 0);
        VectorScale(lightcolorbase, colorscale, color2);
        GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
@@ -1421,7 +1410,6 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_DiffusePass(const entity_
        {
                // 3/2 3D combine path (Geforce3, Radeon 8500)
                memset(&m, 0, sizeof(m));
-               m.pointer_vertex = rsurface_vertex3f;
                m.tex[0] = R_GetTexture(normalmaptexture);
                m.texcombinergb[0] = GL_REPLACE;
                m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
@@ -1433,7 +1421,7 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_DiffusePass(const entity_
                m.tex3d[2] = R_GetTexture(r_shadow_attenuation3dtexture);
                m.pointer_texcoord3f[2] = rsurface_vertex3f;
                m.texmatrix[2] = r_shadow_entitytoattenuationxyz;
-               R_Mesh_State(&m);
+               R_Mesh_TextureState(&m);
                GL_ColorMask(0,0,0,1);
                GL_BlendFunc(GL_ONE, GL_ZERO);
                GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
@@ -1441,7 +1429,6 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_DiffusePass(const entity_
                GL_LockArrays(0, 0);
 
                memset(&m, 0, sizeof(m));
-               m.pointer_vertex = rsurface_vertex3f;
                m.tex[0] = R_GetTexture(basetexture);
                m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
                m.texmatrix[0] = texture->currenttexmatrix;
@@ -1457,11 +1444,10 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_DiffusePass(const entity_
        {
                // 1/2/2 3D combine path (original Radeon)
                memset(&m, 0, sizeof(m));
-               m.pointer_vertex = rsurface_vertex3f;
                m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
                m.pointer_texcoord3f[0] = rsurface_vertex3f;
                m.texmatrix[0] = r_shadow_entitytoattenuationxyz;
-               R_Mesh_State(&m);
+               R_Mesh_TextureState(&m);
                GL_ColorMask(0,0,0,1);
                GL_BlendFunc(GL_ONE, GL_ZERO);
                GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
@@ -1469,7 +1455,6 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_DiffusePass(const entity_
                GL_LockArrays(0, 0);
 
                memset(&m, 0, sizeof(m));
-               m.pointer_vertex = rsurface_vertex3f;
                m.tex[0] = R_GetTexture(normalmaptexture);
                m.texcombinergb[0] = GL_REPLACE;
                m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
@@ -1478,14 +1463,13 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_DiffusePass(const entity_
                m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
                m.pointer_texcoord3f[1] = rsurface_array_texcoord3f;
                R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(rsurface_array_texcoord3f + 3 * surface->num_firstvertex, surface->num_vertices, rsurface_vertex3f + 3 * surface->num_firstvertex, rsurface_svector3f + 3 * surface->num_firstvertex, rsurface_tvector3f + 3 * surface->num_firstvertex, rsurface_normal3f + 3 * surface->num_firstvertex, r_shadow_entitylightorigin);
-               R_Mesh_State(&m);
+               R_Mesh_TextureState(&m);
                GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
                GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
                R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, elements);
                GL_LockArrays(0, 0);
 
                memset(&m, 0, sizeof(m));
-               m.pointer_vertex = rsurface_vertex3f;
                m.tex[0] = R_GetTexture(basetexture);
                m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
                m.texmatrix[0] = texture->currenttexmatrix;
@@ -1501,7 +1485,6 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_DiffusePass(const entity_
        {
                // 2/2 3D combine path (original Radeon)
                memset(&m, 0, sizeof(m));
-               m.pointer_vertex = rsurface_vertex3f;
                m.tex[0] = R_GetTexture(normalmaptexture);
                m.texcombinergb[0] = GL_REPLACE;
                m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
@@ -1510,7 +1493,7 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_DiffusePass(const entity_
                m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
                m.pointer_texcoord3f[1] = rsurface_array_texcoord3f;
                R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(rsurface_array_texcoord3f + 3 * surface->num_firstvertex, surface->num_vertices, rsurface_vertex3f + 3 * surface->num_firstvertex, rsurface_svector3f + 3 * surface->num_firstvertex, rsurface_tvector3f + 3 * surface->num_firstvertex, rsurface_normal3f + 3 * surface->num_firstvertex, r_shadow_entitylightorigin);
-               R_Mesh_State(&m);
+               R_Mesh_TextureState(&m);
                GL_ColorMask(0,0,0,1);
                GL_BlendFunc(GL_ONE, GL_ZERO);
                GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
@@ -1518,7 +1501,6 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_DiffusePass(const entity_
                GL_LockArrays(0, 0);
 
                memset(&m, 0, sizeof(m));
-               m.pointer_vertex = rsurface_vertex3f;
                m.tex[0] = R_GetTexture(basetexture);
                m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
                m.texmatrix[0] = texture->currenttexmatrix;
@@ -1531,7 +1513,6 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_DiffusePass(const entity_
        {
                // 4/2 2D combine path (Geforce3, Radeon 8500)
                memset(&m, 0, sizeof(m));
-               m.pointer_vertex = rsurface_vertex3f;
                m.tex[0] = R_GetTexture(normalmaptexture);
                m.texcombinergb[0] = GL_REPLACE;
                m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
@@ -1546,7 +1527,7 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_DiffusePass(const entity_
                m.tex[3] = R_GetTexture(r_shadow_attenuation2dtexture);
                m.pointer_texcoord3f[3] = rsurface_vertex3f;
                m.texmatrix[3] = r_shadow_entitytoattenuationz;
-               R_Mesh_State(&m);
+               R_Mesh_TextureState(&m);
                GL_ColorMask(0,0,0,1);
                GL_BlendFunc(GL_ONE, GL_ZERO);
                GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
@@ -1554,7 +1535,6 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_DiffusePass(const entity_
                GL_LockArrays(0, 0);
 
                memset(&m, 0, sizeof(m));
-               m.pointer_vertex = rsurface_vertex3f;
                m.tex[0] = R_GetTexture(basetexture);
                m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
                m.texmatrix[0] = texture->currenttexmatrix;
@@ -1570,14 +1550,13 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_DiffusePass(const entity_
        {
                // 2/2/2 2D combine path (any dot3 card)
                memset(&m, 0, sizeof(m));
-               m.pointer_vertex = rsurface_vertex3f;
                m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
                m.pointer_texcoord3f[0] = rsurface_vertex3f;
                m.texmatrix[0] = r_shadow_entitytoattenuationxyz;
                m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
                m.pointer_texcoord3f[1] = rsurface_vertex3f;
                m.texmatrix[1] = r_shadow_entitytoattenuationz;
-               R_Mesh_State(&m);
+               R_Mesh_TextureState(&m);
                GL_ColorMask(0,0,0,1);
                GL_BlendFunc(GL_ONE, GL_ZERO);
                GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
@@ -1585,7 +1564,6 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_DiffusePass(const entity_
                GL_LockArrays(0, 0);
 
                memset(&m, 0, sizeof(m));
-               m.pointer_vertex = rsurface_vertex3f;
                m.tex[0] = R_GetTexture(normalmaptexture);
                m.texcombinergb[0] = GL_REPLACE;
                m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
@@ -1594,14 +1572,13 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_DiffusePass(const entity_
                m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
                m.pointer_texcoord3f[1] = rsurface_array_texcoord3f;
                R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(rsurface_array_texcoord3f + 3 * surface->num_firstvertex, surface->num_vertices, rsurface_vertex3f + 3 * surface->num_firstvertex, rsurface_svector3f + 3 * surface->num_firstvertex, rsurface_tvector3f + 3 * surface->num_firstvertex, rsurface_normal3f + 3 * surface->num_firstvertex, r_shadow_entitylightorigin);
-               R_Mesh_State(&m);
+               R_Mesh_TextureState(&m);
                GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
                GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
                R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, elements);
                GL_LockArrays(0, 0);
 
                memset(&m, 0, sizeof(m));
-               m.pointer_vertex = rsurface_vertex3f;
                m.tex[0] = R_GetTexture(basetexture);
                m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
                m.texmatrix[0] = texture->currenttexmatrix;
@@ -1614,7 +1591,7 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_DiffusePass(const entity_
                GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
        }
        // this final code is shared
-       R_Mesh_State(&m);
+       R_Mesh_TextureState(&m);
        GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 0);
        VectorScale(lightcolorbase, colorscale, color2);
        GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
@@ -1641,7 +1618,6 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_SpecularPass(const entity
        {
                // 2/0/0/1/2 3D combine blendsquare path
                memset(&m, 0, sizeof(m));
-               m.pointer_vertex = rsurface_vertex3f;
                m.tex[0] = R_GetTexture(normalmaptexture);
                m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
                m.texmatrix[0] = texture->currenttexmatrix;
@@ -1649,7 +1625,7 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_SpecularPass(const entity
                m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
                m.pointer_texcoord3f[1] = rsurface_array_texcoord3f;
                R_Shadow_GenTexCoords_Specular_NormalCubeMap(rsurface_array_texcoord3f + 3 * surface->num_firstvertex, surface->num_vertices, rsurface_vertex3f + 3 * surface->num_firstvertex, rsurface_svector3f + 3 * surface->num_firstvertex, rsurface_tvector3f + 3 * surface->num_firstvertex, rsurface_normal3f + 3 * surface->num_firstvertex, r_shadow_entitylightorigin, r_shadow_entityeyeorigin);
-               R_Mesh_State(&m);
+               R_Mesh_TextureState(&m);
                GL_ColorMask(0,0,0,1);
                // this squares the result
                GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
@@ -1657,9 +1633,7 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_SpecularPass(const entity
                R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, elements);
                GL_LockArrays(0, 0);
 
-               memset(&m, 0, sizeof(m));
-               m.pointer_vertex = rsurface_vertex3f;
-               R_Mesh_State(&m);
+               R_Mesh_ResetTextureState();
                GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
                // square alpha in framebuffer a few times to make it shiny
                GL_BlendFunc(GL_ZERO, GL_DST_ALPHA);
@@ -1672,18 +1646,16 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_SpecularPass(const entity
                GL_LockArrays(0, 0);
 
                memset(&m, 0, sizeof(m));
-               m.pointer_vertex = rsurface_vertex3f;
                m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
                m.pointer_texcoord3f[0] = rsurface_vertex3f;
                m.texmatrix[0] = r_shadow_entitytoattenuationxyz;
-               R_Mesh_State(&m);
+               R_Mesh_TextureState(&m);
                GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
                GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
                R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, elements);
                GL_LockArrays(0, 0);
 
                memset(&m, 0, sizeof(m));
-               m.pointer_vertex = rsurface_vertex3f;
                m.tex[0] = R_GetTexture(glosstexture);
                m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
                m.texmatrix[0] = texture->currenttexmatrix;
@@ -1699,7 +1671,6 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_SpecularPass(const entity
        {
                // 2/0/0/2 3D combine blendsquare path
                memset(&m, 0, sizeof(m));
-               m.pointer_vertex = rsurface_vertex3f;
                m.tex[0] = R_GetTexture(normalmaptexture);
                m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
                m.texmatrix[0] = texture->currenttexmatrix;
@@ -1707,7 +1678,7 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_SpecularPass(const entity
                m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
                m.pointer_texcoord3f[1] = rsurface_array_texcoord3f;
                R_Shadow_GenTexCoords_Specular_NormalCubeMap(rsurface_array_texcoord3f + 3 * surface->num_firstvertex, surface->num_vertices, rsurface_vertex3f + 3 * surface->num_firstvertex, rsurface_svector3f + 3 * surface->num_firstvertex, rsurface_tvector3f + 3 * surface->num_firstvertex, rsurface_normal3f + 3 * surface->num_firstvertex, r_shadow_entitylightorigin, r_shadow_entityeyeorigin);
-               R_Mesh_State(&m);
+               R_Mesh_TextureState(&m);
                GL_ColorMask(0,0,0,1);
                // this squares the result
                GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
@@ -1715,9 +1686,7 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_SpecularPass(const entity
                R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, elements);
                GL_LockArrays(0, 0);
 
-               memset(&m, 0, sizeof(m));
-               m.pointer_vertex = rsurface_vertex3f;
-               R_Mesh_State(&m);
+               R_Mesh_ResetTextureState();
                GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
                // square alpha in framebuffer a few times to make it shiny
                GL_BlendFunc(GL_ZERO, GL_DST_ALPHA);
@@ -1730,7 +1699,6 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_SpecularPass(const entity
                GL_LockArrays(0, 0);
 
                memset(&m, 0, sizeof(m));
-               m.pointer_vertex = rsurface_vertex3f;
                m.tex[0] = R_GetTexture(glosstexture);
                m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
                m.texmatrix[0] = texture->currenttexmatrix;
@@ -1743,7 +1711,6 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_SpecularPass(const entity
        {
                // 2/0/0/2/2 2D combine blendsquare path
                memset(&m, 0, sizeof(m));
-               m.pointer_vertex = rsurface_vertex3f;
                m.tex[0] = R_GetTexture(normalmaptexture);
                m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
                m.texmatrix[0] = texture->currenttexmatrix;
@@ -1751,7 +1718,7 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_SpecularPass(const entity
                m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
                m.pointer_texcoord3f[1] = rsurface_array_texcoord3f;
                R_Shadow_GenTexCoords_Specular_NormalCubeMap(rsurface_array_texcoord3f + 3 * surface->num_firstvertex, surface->num_vertices, rsurface_vertex3f + 3 * surface->num_firstvertex, rsurface_svector3f + 3 * surface->num_firstvertex, rsurface_tvector3f + 3 * surface->num_firstvertex, rsurface_normal3f + 3 * surface->num_firstvertex, r_shadow_entitylightorigin, r_shadow_entityeyeorigin);
-               R_Mesh_State(&m);
+               R_Mesh_TextureState(&m);
                GL_ColorMask(0,0,0,1);
                // this squares the result
                GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
@@ -1759,9 +1726,7 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_SpecularPass(const entity
                R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, elements);
                GL_LockArrays(0, 0);
 
-               memset(&m, 0, sizeof(m));
-               m.pointer_vertex = rsurface_vertex3f;
-               R_Mesh_State(&m);
+               R_Mesh_ResetTextureState();
                GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
                // square alpha in framebuffer a few times to make it shiny
                GL_BlendFunc(GL_ZERO, GL_DST_ALPHA);
@@ -1774,21 +1739,19 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_SpecularPass(const entity
                GL_LockArrays(0, 0);
 
                memset(&m, 0, sizeof(m));
-               m.pointer_vertex = rsurface_vertex3f;
                m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
                m.pointer_texcoord3f[0] = rsurface_vertex3f;
                m.texmatrix[0] = r_shadow_entitytoattenuationxyz;
                m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
                m.pointer_texcoord3f[1] = rsurface_vertex3f;
                m.texmatrix[1] = r_shadow_entitytoattenuationz;
-               R_Mesh_State(&m);
+               R_Mesh_TextureState(&m);
                GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
                GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
                R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, elements);
                GL_LockArrays(0, 0);
 
                memset(&m, 0, sizeof(m));
-               m.pointer_vertex = rsurface_vertex3f;
                m.tex[0] = R_GetTexture(glosstexture);
                m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
                m.texmatrix[0] = texture->currenttexmatrix;
@@ -1800,7 +1763,7 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3_SpecularPass(const entity
                }
                GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
        }
-       R_Mesh_State(&m);
+       R_Mesh_TextureState(&m);
        GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 0);
        VectorScale(lightcolorbase, colorscale, color2);
        GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
@@ -1821,12 +1784,11 @@ static void R_Shadow_RenderSurfacesLighting_Light_Dot3(const entity_render_t *en
        qboolean dospecular = specularscale > 0;
        if (!doambient && !dodiffuse && !dospecular)
                return;
-       RSurf_SetPointersForPass(false, true);
+       RSurf_PrepareVerticesForBatch(ent, texture, r_shadow_entityeyeorigin, true, true, numsurfaces, surfacelist);
+       R_Mesh_ColorPointer(NULL);
        for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
        {
                const msurface_t *surface = surfacelist[surfacelistindex];
-               if (rsurface_dynamicvertex)
-                       RSurf_PrepareDynamicSurfaceVertices(surface);
                if (doambient)
                        R_Shadow_RenderSurfacesLighting_Light_Dot3_AmbientPass(ent, texture, surface, lightcolorbase, basetexture, r_shadow_rtlight->ambientscale);
                if (dodiffuse)
@@ -1935,41 +1897,31 @@ static void R_Shadow_RenderSurfacesLighting_Light_Vertex(const entity_render_t *
        VectorScale(lightcolorshirt, r_shadow_rtlight->ambientscale * 2, ambientcolorshirt);
        VectorScale(lightcolorshirt, r_shadow_rtlight->diffusescale * 2, diffusecolorshirt);
        GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
+       R_Mesh_ColorPointer(rsurface_array_color4f);
        memset(&m, 0, sizeof(m));
        m.tex[0] = R_GetTexture(basetexture);
+       m.texmatrix[0] = texture->currenttexmatrix;
+       m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
        if (r_textureunits.integer >= 2)
        {
-               // voodoo2
+               // voodoo2 or TNT
                m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
                m.texmatrix[1] = r_shadow_entitytoattenuationxyz;
+               m.pointer_texcoord3f[1] = rsurface_vertex3f;
                if (r_textureunits.integer >= 3)
                {
-                       // Geforce3/Radeon class but not using dot3
+                       // Voodoo4 or Kyro (or Geforce3/Radeon with gl_combine off)
                        m.tex[2] = R_GetTexture(r_shadow_attenuation2dtexture);
                        m.texmatrix[2] = r_shadow_entitytoattenuationz;
+                       m.pointer_texcoord3f[2] = rsurface_vertex3f;
                }
        }
-       m.pointer_color = rsurface_array_color4f;
-       R_Mesh_State(&m);
-       RSurf_SetPointersForPass(true, false);
+       R_Mesh_TextureState(&m);
+       RSurf_PrepareVerticesForBatch(ent, texture, r_shadow_entityeyeorigin, true, false, numsurfaces, surfacelist);
        for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
        {
                const msurface_t *surface = surfacelist[surfacelistindex];
-               if (rsurface_dynamicvertex)
-                       RSurf_PrepareDynamicSurfaceVertices(surface);
                // OpenGL 1.1 path (anything)
-               R_Mesh_TexCoordPointer(0, 2, model->surfmesh.data_texcoordtexture2f);
-               R_Mesh_TexMatrix(0, &texture->currenttexmatrix);
-               if (r_textureunits.integer >= 2)
-               {
-                       // voodoo2 or TNT
-                       R_Mesh_TexCoordPointer(1, 3, rsurface_vertex3f);
-                       if (r_textureunits.integer >= 3)
-                       {
-                               // Voodoo4 or Kyro (or Geforce3/Radeon with gl_combine off)
-                               R_Mesh_TexCoordPointer(2, 3, rsurface_vertex3f);
-                       }
-               }
                R_Mesh_TexBind(0, R_GetTexture(basetexture));
                R_Shadow_RenderSurfacesLighting_Light_Vertex_Pass(model, surface, diffusecolorbase, ambientcolorbase);
                if (dopants)
@@ -1999,7 +1951,6 @@ void R_Shadow_RenderSurfacesLighting(const entity_render_t *ent, const texture_t
                qglDisable(GL_CULL_FACE);
        else
                qglEnable(GL_CULL_FACE);
-       RSurf_PrepareForBatch(ent, texture, r_shadow_entityeyeorigin);
        if (texture->colormapping)
        {
                qboolean dopants = texture->skin.pants != NULL && VectorLength2(ent->colormap_pantscolor) >= (1.0f / 1048576.0f);