Index: darkplaces/gl_rmain.c
diff -u darkplaces/gl_rmain.c:1.278 darkplaces/gl_rmain.c:1.279
--- darkplaces/gl_rmain.c:1.278	Tue Apr 18 07:10:10 2006
+++ darkplaces/gl_rmain.c	Wed Apr 19 23:43:20 2006
@@ -845,14 +845,14 @@
 	memset(r_glsl_permutations, 0, sizeof(r_glsl_permutations));
 }
 
-void R_SetupSurfaceShader(const entity_render_t *ent, const texture_t *texture, rtexture_t *lightmaptexture, const vec3_t modelorg, const vec3_t lightcolorbase, qboolean modellighting)
+int R_SetupSurfaceShader(const vec3_t lightcolorbase, qboolean modellighting)
 {
 	// select a permutation of the lighting shader appropriate to this
 	// combination of texture, entity, light source, and fogging, only use the
 	// minimum features necessary to avoid wasting rendering time in the
 	// fragment shader on features that are not being used
 	int permutation = 0;
-	float specularscale = texture->specularscale;
+	float specularscale = rsurface_texture->specularscale;
 	r_glsl_permutation = NULL;
 	if (r_shadow_rtlight)
 	{
@@ -863,28 +863,31 @@
 	}
 	else
 	{
-		if (!(texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT))
+		if (!(rsurface_texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT))
 		{
 			if (modellighting)
 				permutation |= SHADERPERMUTATION_MODE_LIGHTDIRECTION;
-			else if (r_glsl_deluxemapping.integer >= 1 && r_refdef.worldmodel && r_refdef.worldmodel->brushq3.deluxemapping && lightmaptexture)
+			else
 			{
-				if (r_refdef.worldmodel->brushq3.deluxemapping_modelspace)
-					permutation |= SHADERPERMUTATION_MODE_LIGHTDIRECTIONMAP_MODELSPACE;
-				else
+				if (r_glsl_deluxemapping.integer >= 1 && r_refdef.worldmodel && r_refdef.worldmodel->brushq3.deluxemapping && rsurface_lightmaptexture)
+				{
+					if (r_refdef.worldmodel->brushq3.deluxemapping_modelspace)
+						permutation |= SHADERPERMUTATION_MODE_LIGHTDIRECTIONMAP_MODELSPACE;
+					else
+						permutation |= SHADERPERMUTATION_MODE_LIGHTDIRECTIONMAP_TANGENTSPACE;
+				}
+				else if (r_glsl_deluxemapping.integer >= 2) // fake mode
 					permutation |= SHADERPERMUTATION_MODE_LIGHTDIRECTIONMAP_TANGENTSPACE;
 			}
-			else if (r_glsl_deluxemapping.integer >= 2) // fake mode
-				permutation |= SHADERPERMUTATION_MODE_LIGHTDIRECTIONMAP_TANGENTSPACE;
 		}
-		if (texture->skin.glow)
+		if (rsurface_texture->skin.glow)
 			permutation |= SHADERPERMUTATION_GLOW;
 	}
 	if (specularscale > 0)
 		permutation |= SHADERPERMUTATION_SPECULAR;
 	if (fogenabled)
 		permutation |= SHADERPERMUTATION_FOG;
-	if (texture->colormapping)
+	if (rsurface_texture->colormapping)
 		permutation |= SHADERPERMUTATION_COLORMAPPING;
 	if (r_glsl_offsetmapping.integer)
 	{
@@ -911,18 +914,17 @@
 				if (r_glsl_permutations[permutation].program)
 					break;
 				if (!i)
-					return; // utterly failed
+					return 0; // utterly failed
 			}
 		}
 	}
 	r_glsl_permutation = r_glsl_permutations + permutation;
 	CHECKGLERROR
 	qglUseProgramObjectARB(r_glsl_permutation->program);CHECKGLERROR
-	R_Mesh_TexMatrix(0, &texture->currenttexmatrix);
+	R_Mesh_TexMatrix(0, &rsurface_texture->currenttexmatrix);
 	if (permutation & SHADERPERMUTATION_MODE_LIGHTSOURCE)
 	{
-		R_Mesh_TexMatrix(3, &r_shadow_entitytolight);
-		//if (r_glsl_permutation->loc_Texture_Cube >= 0) R_Mesh_TexBindCubeMap(3, R_GetTexture(r_shadow_rtlight->currentcubemap));
+		if (r_glsl_permutation->loc_Texture_Cube >= 0 && r_shadow_rtlight) R_Mesh_TexBindCubeMap(3, R_GetTexture(r_shadow_rtlight->currentcubemap));
 		if (r_glsl_permutation->loc_LightPosition >= 0) qglUniform3fARB(r_glsl_permutation->loc_LightPosition, r_shadow_entitylightorigin[0], r_shadow_entitylightorigin[1], r_shadow_entitylightorigin[2]);
 		if (r_glsl_permutation->loc_LightColor >= 0) qglUniform3fARB(r_glsl_permutation->loc_LightColor, lightcolorbase[0], lightcolorbase[1], lightcolorbase[2]);
 		if (r_glsl_permutation->loc_AmbientScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_AmbientScale, r_shadow_rtlight->ambientscale);
@@ -931,7 +933,7 @@
 	}
 	else if (permutation & SHADERPERMUTATION_MODE_LIGHTDIRECTION)
 	{
-		if (texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)
+		if (rsurface_texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)
 		{
 			if (r_glsl_permutation->loc_AmbientColor >= 0)
 				qglUniform3fARB(r_glsl_permutation->loc_AmbientColor, 1, 1, 1);
@@ -945,13 +947,13 @@
 		else
 		{
 			if (r_glsl_permutation->loc_AmbientColor >= 0)
-				qglUniform3fARB(r_glsl_permutation->loc_AmbientColor, ent->modellight_ambient[0], ent->modellight_ambient[1], ent->modellight_ambient[2]);
+				qglUniform3fARB(r_glsl_permutation->loc_AmbientColor, rsurface_entity->modellight_ambient[0], rsurface_entity->modellight_ambient[1], rsurface_entity->modellight_ambient[2]);
 			if (r_glsl_permutation->loc_DiffuseColor >= 0)
-				qglUniform3fARB(r_glsl_permutation->loc_DiffuseColor, ent->modellight_diffuse[0], ent->modellight_diffuse[1], ent->modellight_diffuse[2]);
+				qglUniform3fARB(r_glsl_permutation->loc_DiffuseColor, rsurface_entity->modellight_diffuse[0], rsurface_entity->modellight_diffuse[1], rsurface_entity->modellight_diffuse[2]);
 			if (r_glsl_permutation->loc_SpecularColor >= 0)
-				qglUniform3fARB(r_glsl_permutation->loc_SpecularColor, ent->modellight_diffuse[0] * texture->specularscale, ent->modellight_diffuse[1] * texture->specularscale, ent->modellight_diffuse[2] * texture->specularscale);
+				qglUniform3fARB(r_glsl_permutation->loc_SpecularColor, rsurface_entity->modellight_diffuse[0] * rsurface_texture->specularscale, rsurface_entity->modellight_diffuse[1] * rsurface_texture->specularscale, rsurface_entity->modellight_diffuse[2] * rsurface_texture->specularscale);
 			if (r_glsl_permutation->loc_LightDir >= 0)
-				qglUniform3fARB(r_glsl_permutation->loc_LightDir, ent->modellight_lightdir[0], ent->modellight_lightdir[1], ent->modellight_lightdir[2]);
+				qglUniform3fARB(r_glsl_permutation->loc_LightDir, rsurface_entity->modellight_lightdir[0], rsurface_entity->modellight_lightdir[1], rsurface_entity->modellight_lightdir[2]);
 		}
 	}
 	else
@@ -960,43 +962,55 @@
 		if (r_glsl_permutation->loc_DiffuseScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_DiffuseScale, r_lightmapintensity * 2.0f);
 		if (r_glsl_permutation->loc_SpecularScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularScale, r_lightmapintensity * specularscale * 2.0f);
 	}
-	if (r_glsl_permutation->loc_Texture_Normal >= 0) R_Mesh_TexBind(0, R_GetTexture(texture->skin.nmap));
-	if (r_glsl_permutation->loc_Texture_Color >= 0) R_Mesh_TexBind(1, R_GetTexture(texture->basetexture));
-	if (r_glsl_permutation->loc_Texture_Gloss >= 0) R_Mesh_TexBind(2, R_GetTexture(texture->glosstexture));
+	if (r_glsl_permutation->loc_Texture_Normal >= 0) R_Mesh_TexBind(0, R_GetTexture(rsurface_texture->skin.nmap));
+	if (r_glsl_permutation->loc_Texture_Color >= 0) R_Mesh_TexBind(1, R_GetTexture(rsurface_texture->basetexture));
+	if (r_glsl_permutation->loc_Texture_Gloss >= 0) R_Mesh_TexBind(2, R_GetTexture(rsurface_texture->glosstexture));
 	//if (r_glsl_permutation->loc_Texture_Cube >= 0 && permutation & SHADERPERMUTATION_MODE_LIGHTSOURCE) R_Mesh_TexBindCubeMap(3, R_GetTexture(r_shadow_rtlight->currentcubemap));
 	if (r_glsl_permutation->loc_Texture_FogMask >= 0) R_Mesh_TexBind(4, R_GetTexture(r_texture_fogattenuation));
-	if (r_glsl_permutation->loc_Texture_Pants >= 0) R_Mesh_TexBind(5, R_GetTexture(texture->skin.pants));
-	if (r_glsl_permutation->loc_Texture_Shirt >= 0) R_Mesh_TexBind(6, R_GetTexture(texture->skin.shirt));
-	if (r_glsl_permutation->loc_Texture_Lightmap >= 0) R_Mesh_TexBind(7, R_GetTexture(r_texture_white));
-	if (r_glsl_permutation->loc_Texture_Deluxemap >= 0) R_Mesh_TexBind(8, R_GetTexture(r_texture_blanknormalmap));
-	if (r_glsl_permutation->loc_Texture_Glow >= 0) R_Mesh_TexBind(9, R_GetTexture(texture->skin.glow));
+	if (r_glsl_permutation->loc_Texture_Pants >= 0) R_Mesh_TexBind(5, R_GetTexture(rsurface_texture->skin.pants));
+	if (r_glsl_permutation->loc_Texture_Shirt >= 0) R_Mesh_TexBind(6, R_GetTexture(rsurface_texture->skin.shirt));
+	//if (r_glsl_permutation->loc_Texture_Lightmap >= 0) R_Mesh_TexBind(7, R_GetTexture(r_texture_white));
+	//if (r_glsl_permutation->loc_Texture_Deluxemap >= 0) R_Mesh_TexBind(8, R_GetTexture(r_texture_blanknormalmap));
+	if (r_glsl_permutation->loc_Texture_Glow >= 0) R_Mesh_TexBind(9, R_GetTexture(rsurface_texture->skin.glow));
 	if (r_glsl_permutation->loc_FogColor >= 0)
 	{
 		// additive passes are only darkened by fog, not tinted
-		if (r_shadow_rtlight || (texture->currentmaterialflags & MATERIALFLAG_ADD))
+		if (r_shadow_rtlight || (rsurface_texture->currentmaterialflags & MATERIALFLAG_ADD))
 			qglUniform3fARB(r_glsl_permutation->loc_FogColor, 0, 0, 0);
 		else
 			qglUniform3fARB(r_glsl_permutation->loc_FogColor, fogcolor[0], fogcolor[1], fogcolor[2]);
 	}
-	if (r_glsl_permutation->loc_EyePosition >= 0) qglUniform3fARB(r_glsl_permutation->loc_EyePosition, modelorg[0], modelorg[1], modelorg[2]);
+	if (r_glsl_permutation->loc_EyePosition >= 0) qglUniform3fARB(r_glsl_permutation->loc_EyePosition, rsurface_modelorg[0], rsurface_modelorg[1], rsurface_modelorg[2]);
 	if (r_glsl_permutation->loc_Color_Pants >= 0)
 	{
-		if (texture->skin.pants)
-			qglUniform3fARB(r_glsl_permutation->loc_Color_Pants, ent->colormap_pantscolor[0], ent->colormap_pantscolor[1], ent->colormap_pantscolor[2]);
+		if (rsurface_texture->skin.pants)
+			qglUniform3fARB(r_glsl_permutation->loc_Color_Pants, rsurface_entity->colormap_pantscolor[0], rsurface_entity->colormap_pantscolor[1], rsurface_entity->colormap_pantscolor[2]);
 		else
 			qglUniform3fARB(r_glsl_permutation->loc_Color_Pants, 0, 0, 0);
 	}
 	if (r_glsl_permutation->loc_Color_Shirt >= 0)
 	{
-		if (texture->skin.shirt)
-			qglUniform3fARB(r_glsl_permutation->loc_Color_Shirt, ent->colormap_shirtcolor[0], ent->colormap_shirtcolor[1], ent->colormap_shirtcolor[2]);
+		if (rsurface_texture->skin.shirt)
+			qglUniform3fARB(r_glsl_permutation->loc_Color_Shirt, rsurface_entity->colormap_shirtcolor[0], rsurface_entity->colormap_shirtcolor[1], rsurface_entity->colormap_shirtcolor[2]);
 		else
 			qglUniform3fARB(r_glsl_permutation->loc_Color_Shirt, 0, 0, 0);
 	}
 	if (r_glsl_permutation->loc_FogRangeRecip >= 0) qglUniform1fARB(r_glsl_permutation->loc_FogRangeRecip, fograngerecip);
-	if (r_glsl_permutation->loc_SpecularPower >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularPower, texture->specularpower);
+	if (r_glsl_permutation->loc_SpecularPower >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularPower, rsurface_texture->specularpower);
 	if (r_glsl_permutation->loc_OffsetMapping_Scale >= 0) qglUniform1fARB(r_glsl_permutation->loc_OffsetMapping_Scale, r_glsl_offsetmapping_scale.value);
 	CHECKGLERROR
+	return permutation;
+}
+
+void R_SwitchSurfaceShader(int permutation)
+{
+	if (r_glsl_permutation != r_glsl_permutations + permutation)
+	{
+		r_glsl_permutation = r_glsl_permutations + permutation;
+		CHECKGLERROR
+		qglUseProgramObjectARB(r_glsl_permutation->program);
+		CHECKGLERROR
+	}
 }
 
 void gl_main_start(void)
@@ -2357,7 +2371,7 @@
 					float colorscale;
 					colorscale = 2;
 					// q3bsp has no lightmap updates, so the lightstylevalue that
-					// would normally be baked into the lightmaptexture must be
+					// would normally be baked into the lightmap must be
 					// applied to the color
 					if (ent->model->type == mod_brushq3)
 						colorscale *= r_refdef.lightstylevalue[0] * (1.0f / 256.0f);
@@ -2414,76 +2428,120 @@
 }
 
 int rsurface_array_size = 0;
-float *rsurface_array_vertex3f = NULL;
-float *rsurface_array_svector3f = NULL;
-float *rsurface_array_tvector3f = NULL;
-float *rsurface_array_normal3f = NULL;
+float *rsurface_array_modelvertex3f = NULL;
+float *rsurface_array_modelsvector3f = NULL;
+float *rsurface_array_modeltvector3f = NULL;
+float *rsurface_array_modelnormal3f = NULL;
+float *rsurface_array_deformedvertex3f = NULL;
+float *rsurface_array_deformedsvector3f = NULL;
+float *rsurface_array_deformedtvector3f = NULL;
+float *rsurface_array_deformednormal3f = NULL;
 float *rsurface_array_color4f = NULL;
 float *rsurface_array_texcoord3f = NULL;
 
 void R_Mesh_ResizeArrays(int newvertices)
 {
+	float *base;
 	if (rsurface_array_size >= newvertices)
 		return;
-	if (rsurface_array_vertex3f)
-		Mem_Free(rsurface_array_vertex3f);
+	if (rsurface_array_modelvertex3f)
+		Mem_Free(rsurface_array_modelvertex3f);
 	rsurface_array_size = (newvertices + 1023) & ~1023;
-	rsurface_array_vertex3f = (float *)Mem_Alloc(r_main_mempool, rsurface_array_size * sizeof(float[19]));
-	rsurface_array_svector3f = rsurface_array_vertex3f + rsurface_array_size * 3;
-	rsurface_array_tvector3f = rsurface_array_vertex3f + rsurface_array_size * 6;
-	rsurface_array_normal3f = rsurface_array_vertex3f + rsurface_array_size * 9;
-	rsurface_array_color4f = rsurface_array_vertex3f + rsurface_array_size * 12;
-	rsurface_array_texcoord3f = rsurface_array_vertex3f + rsurface_array_size * 16;
-}
-
+	base = (float *)Mem_Alloc(r_main_mempool, rsurface_array_size * sizeof(float[31]));
+	rsurface_array_modelvertex3f     = base + rsurface_array_size * 0;
+	rsurface_array_modelsvector3f    = base + rsurface_array_size * 3;
+	rsurface_array_modeltvector3f    = base + rsurface_array_size * 6;
+	rsurface_array_modelnormal3f     = base + rsurface_array_size * 9;
+	rsurface_array_deformedvertex3f  = base + rsurface_array_size * 12;
+	rsurface_array_deformedsvector3f = base + rsurface_array_size * 15;
+	rsurface_array_deformedtvector3f = base + rsurface_array_size * 18;
+	rsurface_array_deformednormal3f  = base + rsurface_array_size * 21;
+	rsurface_array_texcoord3f        = base + rsurface_array_size * 24;
+	rsurface_array_color4f           = base + rsurface_array_size * 27;
+}
+
+float *rsurface_modelvertex3f;
+float *rsurface_modelsvector3f;
+float *rsurface_modeltvector3f;
+float *rsurface_modelnormal3f;
 float *rsurface_vertex3f;
 float *rsurface_svector3f;
 float *rsurface_tvector3f;
 float *rsurface_normal3f;
 float *rsurface_lightmapcolor4f;
 vec3_t rsurface_modelorg;
+qboolean rsurface_generatedvertex;
 const entity_render_t *rsurface_entity;
 const model_t *rsurface_model;
-const texture_t *rsurface_texture;
+texture_t *rsurface_texture;
+rtexture_t *rsurface_lightmaptexture;
+rsurfmode_t rsurface_mode;
+texture_t *rsurface_glsl_texture;
 
-void RSurf_PrepareVerticesForBatch(const entity_render_t *ent, const texture_t *texture, const vec3_t modelorg, qboolean generatenormals, qboolean generatetangents, int texturenumsurfaces, msurface_t **texturesurfacelist)
+void RSurf_ActiveEntity(const entity_render_t *ent)
 {
-	VectorCopy(modelorg, rsurface_modelorg);
+	Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, rsurface_modelorg);
 	rsurface_entity = ent;
 	rsurface_model = ent->model;
-	rsurface_texture = texture;
 	if (rsurface_array_size < rsurface_model->surfmesh.num_vertices)
 		R_Mesh_ResizeArrays(rsurface_model->surfmesh.num_vertices);
+	R_Mesh_Matrix(&ent->matrix);
+	Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, rsurface_modelorg);
 	if ((rsurface_entity->frameblend[0].lerp != 1 || rsurface_entity->frameblend[0].frame != 0) && (rsurface_model->surfmesh.data_morphvertex3f || rsurface_model->surfmesh.data_vertexboneweights))
 	{
-		rsurface_vertex3f = rsurface_array_vertex3f;
-		rsurface_svector3f = NULL;
-		rsurface_tvector3f = NULL;
-		rsurface_normal3f = NULL;
-		Mod_Alias_GetMesh_Vertex3f(rsurface_model, rsurface_entity->frameblend, rsurface_array_vertex3f);
+		rsurface_modelvertex3f = rsurface_array_modelvertex3f;
+		rsurface_modelsvector3f = NULL;
+		rsurface_modeltvector3f = NULL;
+		rsurface_modelnormal3f = NULL;
+		Mod_Alias_GetMesh_Vertex3f(rsurface_model, rsurface_entity->frameblend, rsurface_array_modelvertex3f);
+		rsurface_generatedvertex = true;
+	}
+	else
+	{
+		rsurface_modelvertex3f  = rsurface_model->surfmesh.data_vertex3f;
+		rsurface_modelsvector3f = rsurface_model->surfmesh.data_svector3f;
+		rsurface_modeltvector3f = rsurface_model->surfmesh.data_tvector3f;
+		rsurface_modelnormal3f  = rsurface_model->surfmesh.data_normal3f;
+		rsurface_generatedvertex = false;
+	}
+	rsurface_vertex3f  = rsurface_modelvertex3f;
+	rsurface_svector3f = rsurface_modelsvector3f;
+	rsurface_tvector3f = rsurface_modeltvector3f;
+	rsurface_normal3f  = rsurface_modelnormal3f;
+	rsurface_mode = RSURFMODE_NONE;
+}
+
+void RSurf_CleanUp(void)
+{
+	if (rsurface_mode == RSURFMODE_GLSL)
+		qglUseProgramObjectARB(0);
+	GL_AlphaTest(false);
+	rsurface_mode = RSURFMODE_NONE;
+	rsurface_lightmaptexture = NULL;
+	rsurface_texture = NULL;
+	rsurface_glsl_texture = NULL;
+}
+
+void RSurf_PrepareVerticesForBatch(qboolean generatenormals, qboolean generatetangents, int texturenumsurfaces, msurface_t **texturesurfacelist)
+{
+	if (rsurface_generatedvertex)
+	{
 		if (rsurface_texture->textureflags & (Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2))
 			generatetangents = true;
 		if (generatetangents)
 			generatenormals = true;
-		if (generatenormals && !rsurface_normal3f)
+		if (generatenormals && !rsurface_modelnormal3f)
 		{
-			rsurface_normal3f = rsurface_array_normal3f;
-			Mod_BuildNormals(0, rsurface_model->surfmesh.num_vertices, rsurface_model->surfmesh.num_triangles, rsurface_array_vertex3f, rsurface_model->surfmesh.data_element3i, rsurface_array_normal3f, r_smoothnormals_areaweighting.integer);
+			rsurface_normal3f = rsurface_modelnormal3f = rsurface_array_modelnormal3f;
+			Mod_BuildNormals(0, rsurface_model->surfmesh.num_vertices, rsurface_model->surfmesh.num_triangles, rsurface_modelvertex3f, rsurface_model->surfmesh.data_element3i, rsurface_array_modelnormal3f, r_smoothnormals_areaweighting.integer);
 		}
-		if (generatetangents && !rsurface_svector3f)
+		if (generatetangents && !rsurface_modelsvector3f)
 		{
-			Mod_BuildTextureVectorsFromNormals(0, rsurface_model->surfmesh.num_vertices, rsurface_model->surfmesh.num_triangles, rsurface_array_vertex3f, rsurface_model->surfmesh.data_texcoordtexture2f, rsurface_normal3f, rsurface_model->surfmesh.data_element3i, rsurface_array_svector3f, rsurface_array_tvector3f, r_smoothnormals_areaweighting.integer);
-			rsurface_svector3f = rsurface_array_svector3f;
-			rsurface_tvector3f = rsurface_array_tvector3f;
+			rsurface_svector3f = rsurface_modelsvector3f = rsurface_array_modelsvector3f;
+			rsurface_tvector3f = rsurface_modeltvector3f = rsurface_array_modeltvector3f;
+			Mod_BuildTextureVectorsFromNormals(0, rsurface_model->surfmesh.num_vertices, rsurface_model->surfmesh.num_triangles, rsurface_modelvertex3f, rsurface_model->surfmesh.data_texcoordtexture2f, rsurface_modelnormal3f, rsurface_model->surfmesh.data_element3i, rsurface_array_modelsvector3f, rsurface_array_modeltvector3f, r_smoothnormals_areaweighting.integer);
 		}
 	}
-	else
-	{
-		rsurface_vertex3f = rsurface_model->surfmesh.data_vertex3f;
-		rsurface_svector3f = rsurface_model->surfmesh.data_svector3f;
-		rsurface_tvector3f = rsurface_model->surfmesh.data_tvector3f;
-		rsurface_normal3f = rsurface_model->surfmesh.data_normal3f;
-	}
 	if (rsurface_texture->textureflags & (Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2))
 	{
 		int texturesurfaceindex;
@@ -2521,15 +2579,15 @@
 				for (i = 0;i < 4;i++)
 					Matrix4x4_Transform(&imatrix1, (rsurface_vertex3f + 3 * surface->num_firstvertex) + (j+i)*3, v[i]);
 				for (i = 0;i < 4;i++)
-					VectorMAMAMAM(1, center, v[i][0], forward, v[i][1], right, v[i][2], up, rsurface_array_vertex3f + (surface->num_firstvertex+i+j) * 3);
+					VectorMAMAMAM(1, center, v[i][0], forward, v[i][1], right, v[i][2], up, rsurface_array_modelvertex3f + (surface->num_firstvertex+i+j) * 3);
 			}
-			Mod_BuildNormals(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, rsurface_array_vertex3f, rsurface_model->surfmesh.data_element3i + surface->num_firsttriangle * 3, rsurface_array_normal3f, r_smoothnormals_areaweighting.integer);
-			Mod_BuildTextureVectorsFromNormals(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, rsurface_array_vertex3f, rsurface_model->surfmesh.data_texcoordtexture2f, rsurface_array_normal3f, rsurface_model->surfmesh.data_element3i + surface->num_firsttriangle * 3, rsurface_array_svector3f, rsurface_array_tvector3f, r_smoothnormals_areaweighting.integer);
+			Mod_BuildNormals(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, rsurface_modelvertex3f, rsurface_model->surfmesh.data_element3i + surface->num_firsttriangle * 3, rsurface_array_deformednormal3f, r_smoothnormals_areaweighting.integer);
+			Mod_BuildTextureVectorsFromNormals(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, rsurface_modelvertex3f, rsurface_model->surfmesh.data_texcoordtexture2f, rsurface_array_deformednormal3f, rsurface_model->surfmesh.data_element3i + surface->num_firsttriangle * 3, rsurface_array_deformedsvector3f, rsurface_array_deformedtvector3f, r_smoothnormals_areaweighting.integer);
 		}
-		rsurface_vertex3f = rsurface_array_vertex3f;
-		rsurface_svector3f = rsurface_array_svector3f;
-		rsurface_tvector3f = rsurface_array_tvector3f;
-		rsurface_normal3f = rsurface_array_normal3f;
+		rsurface_vertex3f = rsurface_array_deformedvertex3f;
+		rsurface_svector3f = rsurface_array_deformedsvector3f;
+		rsurface_tvector3f = rsurface_array_deformedtvector3f;
+		rsurface_normal3f = rsurface_array_deformednormal3f;
 	}
 	R_Mesh_VertexPointer(rsurface_vertex3f);
 }
@@ -2671,30 +2729,33 @@
 	RSurf_Draw(surface);
 }
 
-static void R_DrawTextureSurfaceList(const entity_render_t *ent, texture_t *texture, rtexture_t *lightmaptexture, int texturenumsurfaces, msurface_t **texturesurfacelist, const vec3_t modelorg)
+static void R_DrawTextureSurfaceList(int texturenumsurfaces, msurface_t **texturesurfacelist)
 {
 	int texturesurfaceindex;
 	int lightmode;
-	model_t *model = ent->model;
 	qboolean applycolor;
 	qboolean applyfog;
 	rmeshstate_t m;
-	if (texture->currentmaterialflags & MATERIALFLAG_NODRAW)
+	if (rsurface_texture->currentmaterialflags & MATERIALFLAG_NODRAW)
 		return;
 	r_shadow_rtlight = NULL;
 	renderstats.entities_surfaces += texturenumsurfaces;
-	// FIXME: identify models using a better check than model->brush.shadowmesh
-	lightmode = ((ent->effects & EF_FULLBRIGHT) || model->brush.shadowmesh) ? 0 : 2;
-	GL_DepthTest(!(texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST));
-	if ((texture->textureflags & Q3TEXTUREFLAG_TWOSIDED) || (ent->flags & RENDER_NOCULLFACE))
+	// FIXME: identify models using a better check than rsurface_model->brush.shadowmesh
+	lightmode = ((rsurface_entity->effects & EF_FULLBRIGHT) || rsurface_model->brush.shadowmesh) ? 0 : 2;
+	GL_DepthTest(!(rsurface_texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST));
+	if ((rsurface_texture->textureflags & Q3TEXTUREFLAG_TWOSIDED) || (rsurface_entity->flags & RENDER_NOCULLFACE))
 		qglDisable(GL_CULL_FACE);
 	if (r_showsurfaces.integer)
 	{
-		GL_DepthMask(true);
-		GL_BlendFunc(GL_ONE, GL_ZERO);
-		R_Mesh_ColorPointer(NULL);
-		R_Mesh_ResetTextureState();
-		RSurf_PrepareVerticesForBatch(ent, texture, modelorg, false, false, texturenumsurfaces, texturesurfacelist);
+		if (rsurface_mode != RSURFMODE_SHOWSURFACES)
+		{
+			rsurface_mode = RSURFMODE_SHOWSURFACES;
+			GL_DepthMask(true);
+			GL_BlendFunc(GL_ONE, GL_ZERO);
+			R_Mesh_ColorPointer(NULL);
+			R_Mesh_ResetTextureState();
+		}
+		RSurf_PrepareVerticesForBatch(false, false, texturenumsurfaces, texturesurfacelist);
 		for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
 		{
 			const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
@@ -2703,44 +2764,59 @@
 			RSurf_Draw(surface);
 		}
 	}
-	else if (texture->currentmaterialflags & MATERIALFLAG_SKY)
+	else if (rsurface_texture->currentmaterialflags & MATERIALFLAG_SKY)
 	{
 		// transparent sky would be ridiculous
-		if (!(texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
+		if (!(rsurface_texture->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
 		{
-			if (skyrendernow)
+			if (rsurface_mode != RSURFMODE_SKY)
 			{
-				skyrendernow = false;
-				R_Sky();
-				// restore entity matrix
-				R_Mesh_Matrix(&ent->matrix);
+				if (rsurface_mode == RSURFMODE_GLSL)
+					qglUseProgramObjectARB(0);
+				rsurface_mode = RSURFMODE_SKY;
+				if (skyrendernow)
+				{
+					skyrendernow = false;
+					R_Sky();
+					// restore entity matrix
+					R_Mesh_Matrix(&rsurface_entity->matrix);
+				}
+				GL_DepthMask(true);
+				// LordHavoc: HalfLife maps have freaky skypolys so don't use
+				// skymasking on them, and Quake3 never did sky masking (unlike
+				// software Quake and software Quake2), so disable the sky masking
+				// in Quake3 maps as it causes problems with q3map2 sky tricks,
+				// and skymasking also looks very bad when noclipping outside the
+				// level, so don't use it then either.
+				if (rsurface_model->type == mod_brushq1 && r_q1bsp_skymasking.integer && !r_worldnovis)
+				{
+					GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
+					R_Mesh_ColorPointer(NULL);
+					R_Mesh_ResetTextureState();
+					if (skyrendermasked)
+					{
+						// depth-only (masking)
+						GL_ColorMask(0,0,0,0);
+						// just to make sure that braindead drivers don't draw
+						// anything despite that colormask...
+						GL_BlendFunc(GL_ZERO, GL_ONE);
+					}
+					else
+					{
+						// fog sky
+						GL_BlendFunc(GL_ONE, GL_ZERO);
+					}
+				}
 			}
-			GL_DepthMask(true);
 			// LordHavoc: HalfLife maps have freaky skypolys so don't use
 			// skymasking on them, and Quake3 never did sky masking (unlike
 			// software Quake and software Quake2), so disable the sky masking
 			// in Quake3 maps as it causes problems with q3map2 sky tricks,
 			// and skymasking also looks very bad when noclipping outside the
 			// level, so don't use it then either.
-			if (model->type == mod_brushq1 && r_q1bsp_skymasking.integer && !r_worldnovis)
+			if (rsurface_model->type == mod_brushq1 && r_q1bsp_skymasking.integer && !r_worldnovis)
 			{
-				GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
-				R_Mesh_ColorPointer(NULL);
-				R_Mesh_ResetTextureState();
-				if (skyrendermasked)
-				{
-					// depth-only (masking)
-					GL_ColorMask(0,0,0,0);
-					// just to make sure that braindead drivers don't draw
-					// anything despite that colormask...
-					GL_BlendFunc(GL_ZERO, GL_ONE);
-				}
-				else
-				{
-					// fog sky
-					GL_BlendFunc(GL_ONE, GL_ZERO);
-				}
-				RSurf_PrepareVerticesForBatch(ent, texture, modelorg, false, false, texturenumsurfaces, texturesurfacelist);
+				RSurf_PrepareVerticesForBatch(false, false, texturenumsurfaces, texturesurfacelist);
 				for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
 				{
 					const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
@@ -2751,28 +2827,40 @@
 			}
 		}
 	}
-	else if (texture->currentnumlayers && r_glsl.integer && gl_support_fragment_shader)
+	else if (rsurface_texture->currentnumlayers && r_glsl.integer && gl_support_fragment_shader)
 	{
-		GL_BlendFunc(texture->currentlayers[0].blendfunc1, texture->currentlayers[0].blendfunc2);
-		GL_DepthMask(!(texture->currentmaterialflags & MATERIALFLAG_BLENDED));
-
-		R_Mesh_ColorPointer(NULL);
-		R_Mesh_ResetTextureState();
-		GL_Color(ent->colormod[0], ent->colormod[1], ent->colormod[2], texture->currentalpha);
-		R_SetupSurfaceShader(ent, texture, lightmaptexture, modelorg, vec3_origin, lightmode == 2);
+		if (rsurface_mode != RSURFMODE_GLSL)
+		{
+			rsurface_mode = RSURFMODE_GLSL;
+			rsurface_glsl_texture = NULL;
+			R_Mesh_ResetTextureState();
+		}
+		if (rsurface_glsl_texture != rsurface_texture)
+		{
+			rsurface_glsl_texture = rsurface_texture;
+			GL_BlendFunc(rsurface_texture->currentlayers[0].blendfunc1, rsurface_texture->currentlayers[0].blendfunc2);
+			GL_DepthMask(!(rsurface_texture->currentmaterialflags & MATERIALFLAG_BLENDED));
+			GL_Color(rsurface_entity->colormod[0], rsurface_entity->colormod[1], rsurface_entity->colormod[2], rsurface_texture->currentalpha);
+			R_SetupSurfaceShader(vec3_origin, lightmode == 2);
+			//permutation_deluxemapping = permutation_lightmapping = R_SetupSurfaceShader(vec3_origin, lightmode == 2, false);
+			//if (r_glsl_deluxemapping.integer)
+			//	permutation_deluxemapping = R_SetupSurfaceShader(vec3_origin, lightmode == 2, true);
+			R_Mesh_TexCoordPointer(0, 2, rsurface_model->surfmesh.data_texcoordtexture2f);
+			if (lightmode != 2)
+				R_Mesh_TexCoordPointer(4, 2, rsurface_model->surfmesh.data_texcoordlightmap2f);
+			GL_AlphaTest((rsurface_texture->currentmaterialflags & MATERIALFLAG_ALPHATEST) != 0);
+		}
 		if (!r_glsl_permutation)
 			return;
-		RSurf_PrepareVerticesForBatch(ent, texture, modelorg, true, true, texturenumsurfaces, texturesurfacelist);
-		R_Mesh_TexCoordPointer(0, 2, model->surfmesh.data_texcoordtexture2f);
+		RSurf_PrepareVerticesForBatch(true, true, texturenumsurfaces, texturesurfacelist);
 		R_Mesh_TexCoordPointer(1, 3, rsurface_svector3f);
 		R_Mesh_TexCoordPointer(2, 3, rsurface_tvector3f);
 		R_Mesh_TexCoordPointer(3, 3, rsurface_normal3f);
 		if (lightmode != 2)
 		{
-			R_Mesh_TexCoordPointer(4, 2, model->surfmesh.data_texcoordlightmap2f);
-			if (lightmaptexture)
+			if (rsurface_lightmaptexture)
 			{
-				R_Mesh_TexBind(7, R_GetTexture(lightmaptexture));
+				R_Mesh_TexBind(7, R_GetTexture(rsurface_lightmaptexture));
 				if (r_glsl_permutation->loc_Texture_Deluxemap >= 0)
 					R_Mesh_TexBind(8, R_GetTexture(texturesurfacelist[0]->deluxemaptexture));
 				R_Mesh_ColorPointer(NULL);
@@ -2782,36 +2870,37 @@
 				R_Mesh_TexBind(7, R_GetTexture(r_texture_white));
 				if (r_glsl_permutation->loc_Texture_Deluxemap >= 0)
 					R_Mesh_TexBind(8, R_GetTexture(r_texture_blanknormalmap));
-				R_Mesh_ColorPointer(model->surfmesh.data_lightmapcolor4f);
+				R_Mesh_ColorPointer(rsurface_model->surfmesh.data_lightmapcolor4f);
 			}
 		}
-		if (texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
-			qglEnable(GL_ALPHA_TEST);
 		for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
 		{
 			const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
 			RSurf_Draw(surface);
 		}
-		if (texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
-			qglDisable(GL_ALPHA_TEST);
-		qglUseProgramObjectARB(0);
 	}
-	else if (texture->currentnumlayers)
+	else if (rsurface_texture->currentnumlayers)
 	{
 		int layerindex;
-		texturelayer_t *layer;
-		RSurf_PrepareVerticesForBatch(ent, texture, modelorg, true, false, texturenumsurfaces, texturesurfacelist);
-		for (layerindex = 0, layer = texture->currentlayers;layerindex < texture->currentnumlayers;layerindex++, layer++)
+		const texturelayer_t *layer;
+		if (rsurface_mode != RSURFMODE_MULTIPASS)
+		{
+			if (rsurface_mode == RSURFMODE_GLSL)
+				qglUseProgramObjectARB(0);
+			rsurface_mode = RSURFMODE_MULTIPASS;
+		}
+		RSurf_PrepareVerticesForBatch(true, false, texturenumsurfaces, texturesurfacelist);
+		for (layerindex = 0, layer = rsurface_texture->currentlayers;layerindex < rsurface_texture->currentnumlayers;layerindex++, layer++)
 		{
 			vec4_t layercolor;
 			int layertexrgbscale;
-			if (texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
+			if (rsurface_texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
 			{
 				if (layerindex == 0)
-					qglEnable(GL_ALPHA_TEST);
+					GL_AlphaTest(true);
 				else
 				{
-					qglDisable(GL_ALPHA_TEST);
+					GL_AlphaTest(false);
 					qglDepthFunc(GL_EQUAL);
 				}
 			}
@@ -2841,11 +2930,11 @@
 			{
 			case TEXTURELAYERTYPE_LITTEXTURE_COMBINE:
 				memset(&m, 0, sizeof(m));
-				m.pointer_texcoord[0] = model->surfmesh.data_texcoordlightmap2f;
+				m.pointer_texcoord[0] = rsurface_model->surfmesh.data_texcoordlightmap2f;
 				m.tex[1] = R_GetTexture(layer->texture);
 				m.texmatrix[1] = layer->texmatrix;
 				m.texrgbscale[1] = layertexrgbscale;
-				m.pointer_texcoord[1] = model->surfmesh.data_texcoordtexture2f;
+				m.pointer_texcoord[1] = rsurface_model->surfmesh.data_texcoordtexture2f;
 				R_Mesh_TextureState(&m);
 				if (lightmode == 2)
 				{
@@ -2856,9 +2945,9 @@
 						RSurf_DrawLightmap(surface, layercolor[0], layercolor[1], layercolor[2], layercolor[3], 2, applycolor, applyfog);
 					}
 				}
-				else if (lightmaptexture)
+				else if (rsurface_lightmaptexture)
 				{
-					R_Mesh_TexBind(0, R_GetTexture(lightmaptexture));
+					R_Mesh_TexBind(0, R_GetTexture(rsurface_lightmaptexture));
 					for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
 					{
 						const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
@@ -2880,7 +2969,7 @@
 				m.tex[0] = R_GetTexture(layer->texture);
 				m.texmatrix[0] = layer->texmatrix;
 				m.texrgbscale[0] = layertexrgbscale;
-				m.pointer_texcoord[0] = model->surfmesh.data_texcoordlightmap2f;
+				m.pointer_texcoord[0] = rsurface_model->surfmesh.data_texcoordlightmap2f;
 				R_Mesh_TextureState(&m);
 				if (lightmode == 2)
 				{
@@ -2891,9 +2980,9 @@
 						RSurf_DrawLightmap(surface, 1, 1, 1, 1, 2, false, false);
 					}
 				}
-				else if (lightmaptexture)
+				else if (rsurface_lightmaptexture)
 				{
-					R_Mesh_TexBind(0, R_GetTexture(lightmaptexture));
+					R_Mesh_TexBind(0, R_GetTexture(rsurface_lightmaptexture));
 					for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
 					{
 						const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
@@ -2915,7 +3004,7 @@
 				m.tex[0] = R_GetTexture(layer->texture);
 				m.texmatrix[0] = layer->texmatrix;
 				m.texrgbscale[0] = layertexrgbscale;
-				m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
+				m.pointer_texcoord[0] = rsurface_model->surfmesh.data_texcoordtexture2f;
 				R_Mesh_TextureState(&m);
 				for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
 				{
@@ -2928,7 +3017,7 @@
 				m.tex[0] = R_GetTexture(layer->texture);
 				m.texmatrix[0] = layer->texmatrix;
 				m.texrgbscale[0] = layertexrgbscale;
-				m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
+				m.pointer_texcoord[0] = rsurface_model->surfmesh.data_texcoordtexture2f;
 				R_Mesh_TextureState(&m);
 				if (lightmode == 2)
 				{
@@ -2952,7 +3041,7 @@
 				m.tex[0] = R_GetTexture(layer->texture);
 				m.texmatrix[0] = layer->texmatrix;
 				m.texrgbscale[0] = layertexrgbscale;
-				m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
+				m.pointer_texcoord[0] = rsurface_model->surfmesh.data_texcoordtexture2f;
 				R_Mesh_TextureState(&m);
 				for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
 				{
@@ -2967,7 +3056,7 @@
 					memset(&m, 0, sizeof(m));
 					m.tex[0] = R_GetTexture(layer->texture);
 					m.texmatrix[0] = layer->texmatrix;
-					m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
+					m.pointer_texcoord[0] = rsurface_model->surfmesh.data_texcoordtexture2f;
 					R_Mesh_TextureState(&m);
 				}
 				else
@@ -2979,7 +3068,7 @@
 					const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
 					for (i = 0, v = (rsurface_vertex3f + 3 * surface->num_firstvertex), c = (rsurface_array_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
 					{
-						f = VERTEXFOGTABLE(VectorDistance(v, modelorg));
+						f = VERTEXFOGTABLE(VectorDistance(v, rsurface_modelorg));
 						c[0] = layercolor[0];
 						c[1] = layercolor[1];
 						c[2] = layercolor[2];
@@ -3010,41 +3099,40 @@
 				GL_LockArrays(0, 0);
 			}
 		}
-		if (texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
+		if (rsurface_texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
 		{
 			qglDepthFunc(GL_LEQUAL);
-			qglDisable(GL_ALPHA_TEST);
+			GL_AlphaTest(false);
 		}
 	}
 	GL_LockArrays(0, 0);
-	if ((texture->textureflags & Q3TEXTUREFLAG_TWOSIDED) || (ent->flags & RENDER_NOCULLFACE))
+	if ((rsurface_texture->textureflags & Q3TEXTUREFLAG_TWOSIDED) || (rsurface_entity->flags & RENDER_NOCULLFACE))
 		qglEnable(GL_CULL_FACE);
 }
 
 static void R_DrawSurface_TransparentCallback(const entity_render_t *ent, int surfacenumber, const rtlight_t *rtlight)
 {
 	msurface_t *surface = ent->model->data_surfaces + surfacenumber;
-	vec3_t modelorg;
-	texture_t *texture;
 
-	texture = surface->texture;
-	if (texture->basematerialflags & MATERIALFLAG_SKY)
+	rsurface_texture = surface->texture;
+	if (rsurface_texture->basematerialflags & MATERIALFLAG_SKY)
 		return; // transparent sky is too difficult
-	R_UpdateTextureInfo(ent, texture);
 
-	R_Mesh_Matrix(&ent->matrix);
-	Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
-	R_DrawTextureSurfaceList(ent, texture->currentframe, surface->lightmaptexture, 1, &surface, modelorg);
+	RSurf_ActiveEntity(ent);
+	R_UpdateTextureInfo(ent, rsurface_texture);
+	rsurface_texture = rsurface_texture->currentframe;
+	R_DrawTextureSurfaceList(1, &surface);
+	RSurf_CleanUp();
 }
 
-void R_QueueTextureSurfaceList(entity_render_t *ent, texture_t *texture, rtexture_t *lightmaptexture, int texturenumsurfaces, msurface_t **texturesurfacelist, const vec3_t modelorg)
+void R_QueueTextureSurfaceList(int texturenumsurfaces, msurface_t **texturesurfacelist)
 {
 	int texturesurfaceindex;
 	vec3_t tempcenter, center;
-	if (texture->currentmaterialflags & MATERIALFLAG_BLENDED)
+	if (rsurface_texture->currentmaterialflags & MATERIALFLAG_BLENDED)
 	{
 		// drawing sky transparently would be too difficult
-		if (!(texture->currentmaterialflags & MATERIALFLAG_SKY))
+		if (!(rsurface_texture->currentmaterialflags & MATERIALFLAG_SKY))
 		{
 			for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
 			{
@@ -3052,13 +3140,13 @@
 				tempcenter[0] = (surface->mins[0] + surface->maxs[0]) * 0.5f;
 				tempcenter[1] = (surface->mins[1] + surface->maxs[1]) * 0.5f;
 				tempcenter[2] = (surface->mins[2] + surface->maxs[2]) * 0.5f;
-				Matrix4x4_Transform(&ent->matrix, tempcenter, center);
-				R_MeshQueue_AddTransparent(texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST ? r_vieworigin : center, R_DrawSurface_TransparentCallback, ent, surface - ent->model->data_surfaces, r_shadow_rtlight);
+				Matrix4x4_Transform(&rsurface_entity->matrix, tempcenter, center);
+				R_MeshQueue_AddTransparent(rsurface_texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST ? r_vieworigin : center, R_DrawSurface_TransparentCallback, rsurface_entity, surface - rsurface_model->data_surfaces, r_shadow_rtlight);
 			}
 		}
 	}
 	else
-		R_DrawTextureSurfaceList(ent, texture, lightmaptexture, texturenumsurfaces, texturesurfacelist, modelorg);
+		R_DrawTextureSurfaceList(texturenumsurfaces, texturesurfacelist);
 }
 
 extern void R_BuildLightMap(const entity_render_t *ent, msurface_t *surface);
@@ -3066,17 +3154,15 @@
 {
 	int i, j, f, flagsmask;
 	int counttriangles = 0;
-	texture_t *t, *texture;
-	rtexture_t *lmap;
+	texture_t *t;
 	model_t *model = ent->model;
-	vec3_t modelorg;
 	const int maxsurfacelist = 1024;
 	int numsurfacelist = 0;
 	msurface_t *surfacelist[1024];
 	if (model == NULL)
 		return;
-	R_Mesh_Matrix(&ent->matrix);
-	Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
+
+	RSurf_ActiveEntity(ent);
 
 	// update light styles
 	if (!skysurfaces && model->brushq1.light_styleupdatechains)
@@ -3098,8 +3184,8 @@
 	flagsmask = skysurfaces ? MATERIALFLAG_SKY : (MATERIALFLAG_WATER | MATERIALFLAG_WALL);
 	f = 0;
 	t = NULL;
-	lmap = NULL;
-	texture = NULL;
+	rsurface_lightmaptexture = NULL;
+	rsurface_texture = NULL;
 	numsurfacelist = 0;
 	if (ent == r_refdef.worldentity)
 	{
@@ -3108,17 +3194,17 @@
 		{
 			if (!r_worldsurfacevisible[j])
 				continue;
-			if (t != surface->texture || lmap != surface->lightmaptexture)
+			if (t != surface->texture || rsurface_lightmaptexture != surface->lightmaptexture)
 			{
 				if (numsurfacelist)
 				{
-					R_QueueTextureSurfaceList(ent, texture, lmap, numsurfacelist, surfacelist, modelorg);
+					R_QueueTextureSurfaceList(numsurfacelist, surfacelist);
 					numsurfacelist = 0;
 				}
 				t = surface->texture;
-				lmap = surface->lightmaptexture;
-				texture = t->currentframe;
-				f = texture->currentmaterialflags & flagsmask;
+				rsurface_lightmaptexture = surface->lightmaptexture;
+				rsurface_texture = t->currentframe;
+				f = rsurface_texture->currentmaterialflags & flagsmask;
 			}
 			if (f && surface->num_triangles)
 			{
@@ -3130,7 +3216,7 @@
 				counttriangles += surface->num_triangles;
 				if (numsurfacelist >= maxsurfacelist)
 				{
-					R_QueueTextureSurfaceList(ent, texture, lmap, numsurfacelist, surfacelist, modelorg);
+					R_QueueTextureSurfaceList(numsurfacelist, surfacelist);
 					numsurfacelist = 0;
 				}
 			}
@@ -3141,17 +3227,17 @@
 		msurface_t *surface;
 		for (i = 0, j = model->firstmodelsurface, surface = model->data_surfaces + j;i < model->nummodelsurfaces;i++, j++, surface++)
 		{
-			if (t != surface->texture || lmap != surface->lightmaptexture)
+			if (t != surface->texture || rsurface_lightmaptexture != surface->lightmaptexture)
 			{
 				if (numsurfacelist)
 				{
-					R_QueueTextureSurfaceList(ent, texture, lmap, numsurfacelist, surfacelist, modelorg);
+					R_QueueTextureSurfaceList(numsurfacelist, surfacelist);
 					numsurfacelist = 0;
 				}
 				t = surface->texture;
-				lmap = surface->lightmaptexture;
-				texture = t->currentframe;
-				f = texture->currentmaterialflags & flagsmask;
+				rsurface_lightmaptexture = surface->lightmaptexture;
+				rsurface_texture = t->currentframe;
+				f = rsurface_texture->currentmaterialflags & flagsmask;
 			}
 			if (f && surface->num_triangles)
 			{
@@ -3163,17 +3249,16 @@
 				counttriangles += surface->num_triangles;
 				if (numsurfacelist >= maxsurfacelist)
 				{
-					R_QueueTextureSurfaceList(ent, texture, lmap, numsurfacelist, surfacelist, modelorg);
+					R_QueueTextureSurfaceList(numsurfacelist, surfacelist);
 					numsurfacelist = 0;
 				}
 			}
 		}
 	}
 	if (numsurfacelist)
-		R_QueueTextureSurfaceList(ent, texture, lmap, numsurfacelist, surfacelist, modelorg);
+		R_QueueTextureSurfaceList(numsurfacelist, surfacelist);
 	renderstats.entities_triangles += counttriangles;
-	if (gl_support_fragment_shader)
-		qglUseProgramObjectARB(0);
+	RSurf_CleanUp();
 
 	if (r_showcollisionbrushes.integer && model->brush.num_brushes && !skysurfaces)
 	{
@@ -3213,13 +3298,13 @@
 		{
 			if (ent == r_refdef.worldentity && !r_worldsurfacevisible[j])
 				continue;
-			texture = surface->texture->currentframe;
-			if ((texture->currentmaterialflags & flagsmask) && surface->num_triangles)
+			rsurface_texture = surface->texture->currentframe;
+			if ((rsurface_texture->currentmaterialflags & flagsmask) && surface->num_triangles)
 			{
-				RSurf_PrepareVerticesForBatch(ent, texture, modelorg, false, r_shownormals.integer != 0, 1, &surface);
+				RSurf_PrepareVerticesForBatch(true, true, 1, &surface);
 				if (r_showtris.integer)
 				{
-					if (!texture->currentlayers->depthmask)
+					if (!rsurface_texture->currentlayers->depthmask)
 						GL_Color(r_showtris.value, 0, 0, 1);
 					else if (ent == r_refdef.worldentity)
 						GL_Color(r_showtris.value, r_showtris.value, r_showtris.value, 1);
@@ -3270,6 +3355,7 @@
 				}
 			}
 		}
+		rsurface_texture = NULL;
 		if (r_showdisabledepthtest.integer)
 			qglDepthFunc(GL_LEQUAL);
 	}
Index: darkplaces/gl_rsurf.c
diff -u darkplaces/gl_rsurf.c:1.326 darkplaces/gl_rsurf.c:1.327
--- darkplaces/gl_rsurf.c:1.326	Tue Apr 18 06:44:11 2006
+++ darkplaces/gl_rsurf.c	Wed Apr 19 23:43:20 2006
@@ -705,10 +705,10 @@
 	r_shadow_compilingrtlight->static_meshchain_shadow = Mod_ShadowMesh_Finish(r_main_mempool, r_shadow_compilingrtlight->static_meshchain_shadow, false, false);
 }
 
-void R_Q1BSP_DrawShadowVolume_Batch(entity_render_t *ent, texture_t *texture, const vec3_t modelorg, const vec3_t relativelightorigin, const vec3_t lightmins, const vec3_t lightmaxs, int texturenumsurfaces, msurface_t **texturesurfacelist)
+void R_Q1BSP_DrawShadowVolume_Batch(const vec3_t relativelightorigin, const vec3_t lightmins, const vec3_t lightmaxs, int texturenumsurfaces, msurface_t **texturesurfacelist)
 {
 	int texturesurfaceindex;
-	RSurf_PrepareVerticesForBatch(ent, texture, modelorg, false, false, texturenumsurfaces, texturesurfacelist);
+	RSurf_PrepareVerticesForBatch(false, false, texturenumsurfaces, texturesurfacelist);
 	for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
 	{
 		msurface_t *surface = texturesurfacelist[texturesurfaceindex];
@@ -723,8 +723,7 @@
 	int modelsurfacelistindex;
 	int f = 0;
 	float projectdistance = lightradius + model->radius*2 + r_shadow_projectdistance.value;
-	vec3_t modelorg;
-	texture_t *t = NULL, *texture = NULL;
+	texture_t *t = NULL;
 	const int maxsurfacelist = 1024;
 	int numsurfacelist = 0;
 	msurface_t *surfacelist[1024];
@@ -738,10 +737,10 @@
 		for (modelsurfacelistindex = 0;modelsurfacelistindex < modelnumsurfaces;modelsurfacelistindex++)
 		{
 			surface = model->data_surfaces + modelsurfacelist[modelsurfacelistindex];
-			texture = surface->texture->currentframe;
-			if ((texture->currentmaterialflags & (MATERIALFLAG_NODRAW | MATERIALFLAG_TRANSPARENT | MATERIALFLAG_WALL)) != MATERIALFLAG_WALL)
+			t = surface->texture->currentframe;
+			if ((t->currentmaterialflags & (MATERIALFLAG_NODRAW | MATERIALFLAG_TRANSPARENT | MATERIALFLAG_WALL)) != MATERIALFLAG_WALL)
 				continue;
-			if ((texture->textureflags & (Q3TEXTUREFLAG_TWOSIDED | Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2)) || (ent->flags & RENDER_NOCULLFACE))
+			if ((t->textureflags & (Q3TEXTUREFLAG_TWOSIDED | Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2)) || (ent->flags & RENDER_NOCULLFACE))
 				continue;
 			R_Shadow_MarkVolumeFromBox(surface->num_firstshadowmeshtriangle, surface->num_triangles, model->brush.shadowmesh->vertex3f, model->brush.shadowmesh->element3i, relativelightorigin, lightmins, lightmaxs, surface->mins, surface->maxs);
 		}
@@ -750,7 +749,7 @@
 	else
 	{
 		projectdistance = lightradius + model->radius*2;
-		Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
+		RSurf_ActiveEntity(ent);
 		R_Shadow_PrepareShadowMark(model->surfmesh.num_triangles);
 		// identify lit faces within the bounding box
 		for (modelsurfacelistindex = 0;modelsurfacelistindex < modelnumsurfaces;modelsurfacelistindex++)
@@ -760,18 +759,18 @@
 			{
 				if (numsurfacelist)
 				{
-					R_Q1BSP_DrawShadowVolume_Batch(ent, texture, modelorg, relativelightorigin, lightmins, lightmaxs, numsurfacelist, surfacelist);
+					R_Q1BSP_DrawShadowVolume_Batch(relativelightorigin, lightmins, lightmaxs, numsurfacelist, surfacelist);
 					numsurfacelist = 0;
 				}
 				t = surface->texture;
-				texture = t->currentframe;
-				f = (texture->currentmaterialflags & (MATERIALFLAG_NODRAW | MATERIALFLAG_TRANSPARENT | MATERIALFLAG_WALL)) == MATERIALFLAG_WALL;
+				rsurface_texture = t->currentframe;
+				f = (rsurface_texture->currentmaterialflags & (MATERIALFLAG_NODRAW | MATERIALFLAG_TRANSPARENT | MATERIALFLAG_WALL)) == MATERIALFLAG_WALL;
 			}
 			if (f && surface->num_triangles)
 				surfacelist[numsurfacelist++] = surface;
 		}
 		if (numsurfacelist)
-			R_Q1BSP_DrawShadowVolume_Batch(ent, texture, modelorg, relativelightorigin, lightmins, lightmaxs, numsurfacelist, surfacelist);
+			R_Q1BSP_DrawShadowVolume_Batch(relativelightorigin, lightmins, lightmaxs, numsurfacelist, surfacelist);
 		R_Shadow_VolumeFromList(model->surfmesh.num_vertices, model->surfmesh.num_triangles, rsurface_vertex3f, model->surfmesh.data_element3i, model->surfmesh.data_neighbor3i, relativelightorigin, projectdistance, numshadowmark, shadowmarklist);
 	}
 }
@@ -779,21 +778,19 @@
 static void R_Q1BSP_DrawLight_TransparentCallback(const entity_render_t *ent, int surfacenumber, const rtlight_t *rtlight)
 {
 	msurface_t *surface = ent->model->data_surfaces + surfacenumber;
-	texture_t *texture = surface->texture;
-	R_UpdateTextureInfo(ent, texture);
-	texture = texture->currentframe;
+	R_UpdateTextureInfo(ent, surface->texture);
 	R_Shadow_RenderMode_Begin();
 	R_Shadow_RenderMode_ActiveLight((rtlight_t *)rtlight);
 	R_Shadow_RenderMode_Lighting(false, true);
 	R_Shadow_SetupEntityLight(ent);
-	R_Shadow_RenderSurfacesLighting(ent, texture, 1, &surface);
+	rsurface_texture = surface->texture->currentframe;
+	R_Shadow_RenderSurfacesLighting(1, &surface);
 	R_Shadow_RenderMode_End();
 }
 
-static void R_Q1BSP_DrawLight_TransparentBatch(const entity_render_t *ent, texture_t *texture, int batchnumsurfaces, msurface_t **batchsurfacelist)
+static void R_Q1BSP_DrawLight_TransparentBatch(int batchnumsurfaces, msurface_t **batchsurfacelist)
 {
 	int batchsurfaceindex;
-	model_t *model = ent->model;
 	msurface_t *batchsurface;
 	vec3_t tempcenter, center;
 	for (batchsurfaceindex = 0;batchsurfaceindex < batchnumsurfaces;batchsurfaceindex++)
@@ -802,8 +799,8 @@
 		tempcenter[0] = (batchsurface->mins[0] + batchsurface->maxs[0]) * 0.5f;
 		tempcenter[1] = (batchsurface->mins[1] + batchsurface->maxs[1]) * 0.5f;
 		tempcenter[2] = (batchsurface->mins[2] + batchsurface->maxs[2]) * 0.5f;
-		Matrix4x4_Transform(&ent->matrix, tempcenter, center);
-		R_MeshQueue_AddTransparent(texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST ? r_vieworigin : center, R_Q1BSP_DrawLight_TransparentCallback, ent, batchsurface - model->data_surfaces, r_shadow_rtlight);
+		Matrix4x4_Transform(&rsurface_entity->matrix, tempcenter, center);
+		R_MeshQueue_AddTransparent(rsurface_texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST ? r_vieworigin : center, R_Q1BSP_DrawLight_TransparentCallback, rsurface_entity, batchsurface - rsurface_model->data_surfaces, r_shadow_rtlight);
 	}
 }
 
@@ -813,16 +810,14 @@
 {
 	model_t *model = ent->model;
 	msurface_t *surface;
-	texture_t *texture;
 	int surfacelistindex, batchnumsurfaces;
 	msurface_t *batchsurfacelist[RSURF_MAX_BATCHSURFACES];
-	vec3_t modelorg;
 	texture_t *tex;
 	qboolean skip;
+	RSurf_ActiveEntity(ent);
 	R_UpdateAllTextureInfo(ent);
-	Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
 	tex = NULL;
-	texture = NULL;
+	rsurface_texture = NULL;
 	skip = false;
 	batchnumsurfaces = 0;
 	for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
@@ -835,15 +830,15 @@
 		{
 			if (batchnumsurfaces > 0)
 			{
-				if (texture->currentmaterialflags & MATERIALFLAG_BLENDED)
-					R_Q1BSP_DrawLight_TransparentBatch(ent, texture, batchnumsurfaces, batchsurfacelist);
+				if (rsurface_texture->currentmaterialflags & MATERIALFLAG_BLENDED)
+					R_Q1BSP_DrawLight_TransparentBatch(batchnumsurfaces, batchsurfacelist);
 				else
-					R_Shadow_RenderSurfacesLighting(ent, texture, batchnumsurfaces, batchsurfacelist);
+					R_Shadow_RenderSurfacesLighting(batchnumsurfaces, batchsurfacelist);
 				batchnumsurfaces = 0;
 			}
 			tex = surface->texture;
-			texture = surface->texture->currentframe;
-			skip = (texture->currentmaterialflags & MATERIALFLAG_SKY) != 0;
+			rsurface_texture = surface->texture->currentframe;
+			skip = (rsurface_texture->currentmaterialflags & MATERIALFLAG_SKY) != 0;
 			if (skip)
 				continue;
 		}
@@ -851,10 +846,10 @@
 		{
 			if (batchnumsurfaces == RSURF_MAX_BATCHSURFACES)
 			{
-				if (texture->currentmaterialflags & MATERIALFLAG_BLENDED)
-					R_Q1BSP_DrawLight_TransparentBatch(ent, texture, batchnumsurfaces, batchsurfacelist);
+				if (rsurface_texture->currentmaterialflags & MATERIALFLAG_BLENDED)
+					R_Q1BSP_DrawLight_TransparentBatch(batchnumsurfaces, batchsurfacelist);
 				else
-					R_Shadow_RenderSurfacesLighting(ent, texture, batchnumsurfaces, batchsurfacelist);
+					R_Shadow_RenderSurfacesLighting(batchnumsurfaces, batchsurfacelist);
 				batchnumsurfaces = 0;
 			}
 			batchsurfacelist[batchnumsurfaces++] = surface;
@@ -862,10 +857,10 @@
 	}
 	if (batchnumsurfaces > 0)
 	{
-		if (texture->currentmaterialflags & MATERIALFLAG_BLENDED)
-			R_Q1BSP_DrawLight_TransparentBatch(ent, texture, batchnumsurfaces, batchsurfacelist);
+		if (rsurface_texture->currentmaterialflags & MATERIALFLAG_BLENDED)
+			R_Q1BSP_DrawLight_TransparentBatch(batchnumsurfaces, batchsurfacelist);
 		else
-			R_Shadow_RenderSurfacesLighting(ent, texture, batchnumsurfaces, batchsurfacelist);
+			R_Shadow_RenderSurfacesLighting(batchnumsurfaces, batchsurfacelist);
 		batchnumsurfaces = 0;
 	}
 	qglEnable(GL_CULL_FACE);
Index: darkplaces/r_shadow.c
diff -u darkplaces/r_shadow.c:1.282 darkplaces/r_shadow.c:1.283
--- darkplaces/r_shadow.c:1.282	Tue Apr 18 06:44:11 2006
+++ darkplaces/r_shadow.c	Wed Apr 19 23:43:20 2006
@@ -1229,55 +1229,52 @@
 	}
 }
 
-static void R_Shadow_RenderSurfacesLighting_VisibleLighting(const entity_render_t *ent, const texture_t *texture, int numsurfaces, msurface_t **surfacelist, const vec3_t lightcolorbase, const vec3_t lightcolorpants, const vec3_t lightcolorshirt, rtexture_t *basetexture, rtexture_t *pantstexture, rtexture_t *shirttexture, rtexture_t *normalmaptexture, rtexture_t *glosstexture, float specularscale, qboolean dopants, qboolean doshirt)
+static void R_Shadow_RenderSurfacesLighting_VisibleLighting(int numsurfaces, msurface_t **surfacelist, const vec3_t lightcolorbase, const vec3_t lightcolorpants, const vec3_t lightcolorshirt, rtexture_t *basetexture, rtexture_t *pantstexture, rtexture_t *shirttexture, rtexture_t *normalmaptexture, rtexture_t *glosstexture, float specularscale, qboolean dopants, qboolean doshirt)
 {
 	// used to display how many times a surface is lit for level design purposes
 	int surfacelistindex;
-	model_t *model = ent->model;
 	GL_Color(0.1, 0.025, 0, 1);
 	R_Mesh_ColorPointer(NULL);
 	R_Mesh_ResetTextureState();
-	RSurf_PrepareVerticesForBatch(ent, texture, r_shadow_entityeyeorigin, false, false, numsurfaces, surfacelist);
+	RSurf_PrepareVerticesForBatch(false, false, numsurfaces, surfacelist);
 	for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
 	{
 		const msurface_t *surface = surfacelist[surfacelistindex];
 		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);
+		R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, rsurface_model->surfmesh.data_element3i + 3 * surface->num_firsttriangle);
 	}
+	GL_LockArrays(0, 0);
 }
 
-static void R_Shadow_RenderSurfacesLighting_Light_GLSL(const entity_render_t *ent, const texture_t *texture, int numsurfaces, msurface_t **surfacelist, const vec3_t lightcolorbase, const vec3_t lightcolorpants, const vec3_t lightcolorshirt, rtexture_t *basetexture, rtexture_t *pantstexture, rtexture_t *shirttexture, rtexture_t *normalmaptexture, rtexture_t *glosstexture, float specularscale, qboolean dopants, qboolean doshirt)
+static void R_Shadow_RenderSurfacesLighting_Light_GLSL(int numsurfaces, msurface_t **surfacelist, const vec3_t lightcolorbase, const vec3_t lightcolorpants, const vec3_t lightcolorshirt, rtexture_t *basetexture, rtexture_t *pantstexture, rtexture_t *shirttexture, rtexture_t *normalmaptexture, rtexture_t *glosstexture, float specularscale, qboolean dopants, qboolean doshirt)
 {
 	// ARB2 GLSL shader path (GFFX5200, Radeon 9500)
 	int surfacelistindex;
-	model_t *model = ent->model;
-	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);
+	RSurf_PrepareVerticesForBatch(true, true, numsurfaces, surfacelist);
+	R_SetupSurfaceShader(lightcolorbase, false);
+	R_Mesh_TexCoordPointer(0, 2, rsurface_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)
+	if (rsurface_texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
 		qglDepthFunc(GL_EQUAL);
 	for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
 	{
 		const msurface_t *surface = surfacelist[surfacelistindex];
 		GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
-		R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, model->surfmesh.data_element3i + surface->num_firsttriangle * 3);
+		R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, rsurface_model->surfmesh.data_element3i + surface->num_firsttriangle * 3);
 	}
-	if (texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
-		qglDepthFunc(GL_LEQUAL);
 	GL_LockArrays(0, 0);
+	if (rsurface_texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
+		qglDepthFunc(GL_LEQUAL);
 }
 
-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)
+static void R_Shadow_RenderSurfacesLighting_Light_Dot3_AmbientPass(const msurface_t *surface, const vec3_t lightcolorbase, rtexture_t *basetexture, float colorscale)
 {
 	int renders;
-	model_t *model = ent->model;
 	float color2[3];
 	rmeshstate_t m;
-	const int *elements = model->surfmesh.data_element3i + surface->num_firsttriangle * 3;
+	const int *elements = rsurface_model->surfmesh.data_element3i + surface->num_firsttriangle * 3;
 	GL_Color(1,1,1,1);
 	// colorscale accounts for how much we multiply the brightness
 	// during combine.
@@ -1294,8 +1291,8 @@
 		m.pointer_texcoord3f[0] = rsurface_vertex3f;
 		m.texmatrix[0] = r_shadow_entitytoattenuationxyz;
 		m.tex[1] = R_GetTexture(basetexture);
-		m.pointer_texcoord[1] = model->surfmesh.data_texcoordtexture2f;
-		m.texmatrix[1] = texture->currenttexmatrix;
+		m.pointer_texcoord[1] = rsurface_model->surfmesh.data_texcoordtexture2f;
+		m.texmatrix[1] = rsurface_texture->currenttexmatrix;
 		m.texcubemap[2] = R_GetTexture(r_shadow_rtlight->currentcubemap);
 		m.pointer_texcoord3f[2] = rsurface_vertex3f;
 		m.texmatrix[2] = r_shadow_entitytolight;
@@ -1309,8 +1306,8 @@
 		m.pointer_texcoord3f[0] = rsurface_vertex3f;
 		m.texmatrix[0] = r_shadow_entitytoattenuationxyz;
 		m.tex[1] = R_GetTexture(basetexture);
-		m.pointer_texcoord[1] = model->surfmesh.data_texcoordtexture2f;
-		m.texmatrix[1] = texture->currenttexmatrix;
+		m.pointer_texcoord[1] = rsurface_model->surfmesh.data_texcoordtexture2f;
+		m.texmatrix[1] = rsurface_texture->currenttexmatrix;
 		GL_BlendFunc(GL_ONE, GL_ONE);
 	}
 	else if (r_textureunits.integer >= 4 && r_shadow_rtlight->currentcubemap != r_texture_whitecube)
@@ -1324,8 +1321,8 @@
 		m.pointer_texcoord3f[1] = rsurface_vertex3f;
 		m.texmatrix[1] = r_shadow_entitytoattenuationz;
 		m.tex[2] = R_GetTexture(basetexture);
-		m.pointer_texcoord[2] = model->surfmesh.data_texcoordtexture2f;
-		m.texmatrix[2] = texture->currenttexmatrix;
+		m.pointer_texcoord[2] = rsurface_model->surfmesh.data_texcoordtexture2f;
+		m.texmatrix[2] = rsurface_texture->currenttexmatrix;
 		if (r_shadow_rtlight->currentcubemap != r_texture_whitecube)
 		{
 			m.texcubemap[3] = R_GetTexture(r_shadow_rtlight->currentcubemap);
@@ -1345,8 +1342,8 @@
 		m.pointer_texcoord3f[1] = rsurface_vertex3f;
 		m.texmatrix[1] = r_shadow_entitytoattenuationz;
 		m.tex[2] = R_GetTexture(basetexture);
-		m.pointer_texcoord[2] = model->surfmesh.data_texcoordtexture2f;
-		m.texmatrix[2] = texture->currenttexmatrix;
+		m.pointer_texcoord[2] = rsurface_model->surfmesh.data_texcoordtexture2f;
+		m.texmatrix[2] = rsurface_texture->currenttexmatrix;
 		GL_BlendFunc(GL_ONE, GL_ONE);
 	}
 	else
@@ -1368,8 +1365,8 @@
 
 		memset(&m, 0, sizeof(m));
 		m.tex[0] = R_GetTexture(basetexture);
-		m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
-		m.texmatrix[0] = texture->currenttexmatrix;
+		m.pointer_texcoord[0] = rsurface_model->surfmesh.data_texcoordtexture2f;
+		m.texmatrix[0] = rsurface_texture->currenttexmatrix;
 		if (r_shadow_rtlight->currentcubemap != r_texture_whitecube)
 		{
 			m.texcubemap[1] = R_GetTexture(r_shadow_rtlight->currentcubemap);
@@ -1391,13 +1388,12 @@
 	GL_LockArrays(0, 0);
 }
 
-static void R_Shadow_RenderSurfacesLighting_Light_Dot3_DiffusePass(const entity_render_t *ent, const texture_t *texture, const msurface_t *surface, const vec3_t lightcolorbase, rtexture_t *basetexture, rtexture_t *normalmaptexture, float colorscale)
+static void R_Shadow_RenderSurfacesLighting_Light_Dot3_DiffusePass(const msurface_t *surface, const vec3_t lightcolorbase, rtexture_t *basetexture, rtexture_t *normalmaptexture, float colorscale)
 {
 	int renders;
-	model_t *model = ent->model;
 	float color2[3];
 	rmeshstate_t m;
-	const int *elements = model->surfmesh.data_element3i + surface->num_firsttriangle * 3;
+	const int *elements = rsurface_model->surfmesh.data_element3i + surface->num_firsttriangle * 3;
 	GL_Color(1,1,1,1);
 	// colorscale accounts for how much we multiply the brightness
 	// during combine.
@@ -1412,8 +1408,8 @@
 		memset(&m, 0, sizeof(m));
 		m.tex[0] = R_GetTexture(normalmaptexture);
 		m.texcombinergb[0] = GL_REPLACE;
-		m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
-		m.texmatrix[0] = texture->currenttexmatrix;
+		m.pointer_texcoord[0] = rsurface_model->surfmesh.data_texcoordtexture2f;
+		m.texmatrix[0] = rsurface_texture->currenttexmatrix;
 		m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
 		m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
 		m.pointer_texcoord3f[1] = rsurface_array_texcoord3f;
@@ -1430,8 +1426,8 @@
 
 		memset(&m, 0, sizeof(m));
 		m.tex[0] = R_GetTexture(basetexture);
-		m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
-		m.texmatrix[0] = texture->currenttexmatrix;
+		m.pointer_texcoord[0] = rsurface_model->surfmesh.data_texcoordtexture2f;
+		m.texmatrix[0] = rsurface_texture->currenttexmatrix;
 		if (r_shadow_rtlight->currentcubemap != r_texture_whitecube)
 		{
 			m.texcubemap[1] = R_GetTexture(r_shadow_rtlight->currentcubemap);
@@ -1457,8 +1453,8 @@
 		memset(&m, 0, sizeof(m));
 		m.tex[0] = R_GetTexture(normalmaptexture);
 		m.texcombinergb[0] = GL_REPLACE;
-		m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
-		m.texmatrix[0] = texture->currenttexmatrix;
+		m.pointer_texcoord[0] = rsurface_model->surfmesh.data_texcoordtexture2f;
+		m.texmatrix[0] = rsurface_texture->currenttexmatrix;
 		m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
 		m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
 		m.pointer_texcoord3f[1] = rsurface_array_texcoord3f;
@@ -1471,8 +1467,8 @@
 
 		memset(&m, 0, sizeof(m));
 		m.tex[0] = R_GetTexture(basetexture);
-		m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
-		m.texmatrix[0] = texture->currenttexmatrix;
+		m.pointer_texcoord[0] = rsurface_model->surfmesh.data_texcoordtexture2f;
+		m.texmatrix[0] = rsurface_texture->currenttexmatrix;
 		if (r_shadow_rtlight->currentcubemap != r_texture_whitecube)
 		{
 			m.texcubemap[1] = R_GetTexture(r_shadow_rtlight->currentcubemap);
@@ -1487,8 +1483,8 @@
 		memset(&m, 0, sizeof(m));
 		m.tex[0] = R_GetTexture(normalmaptexture);
 		m.texcombinergb[0] = GL_REPLACE;
-		m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
-		m.texmatrix[0] = texture->currenttexmatrix;
+		m.pointer_texcoord[0] = rsurface_model->surfmesh.data_texcoordtexture2f;
+		m.texmatrix[0] = rsurface_texture->currenttexmatrix;
 		m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
 		m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
 		m.pointer_texcoord3f[1] = rsurface_array_texcoord3f;
@@ -1502,8 +1498,8 @@
 
 		memset(&m, 0, sizeof(m));
 		m.tex[0] = R_GetTexture(basetexture);
-		m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
-		m.texmatrix[0] = texture->currenttexmatrix;
+		m.pointer_texcoord[0] = rsurface_model->surfmesh.data_texcoordtexture2f;
+		m.texmatrix[0] = rsurface_texture->currenttexmatrix;
 		m.tex3d[1] = R_GetTexture(r_shadow_attenuation3dtexture);
 		m.pointer_texcoord3f[1] = rsurface_vertex3f;
 		m.texmatrix[1] = r_shadow_entitytoattenuationxyz;
@@ -1515,8 +1511,8 @@
 		memset(&m, 0, sizeof(m));
 		m.tex[0] = R_GetTexture(normalmaptexture);
 		m.texcombinergb[0] = GL_REPLACE;
-		m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
-		m.texmatrix[0] = texture->currenttexmatrix;
+		m.pointer_texcoord[0] = rsurface_model->surfmesh.data_texcoordtexture2f;
+		m.texmatrix[0] = rsurface_texture->currenttexmatrix;
 		m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
 		m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
 		m.pointer_texcoord3f[1] = rsurface_array_texcoord3f;
@@ -1536,8 +1532,8 @@
 
 		memset(&m, 0, sizeof(m));
 		m.tex[0] = R_GetTexture(basetexture);
-		m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
-		m.texmatrix[0] = texture->currenttexmatrix;
+		m.pointer_texcoord[0] = rsurface_model->surfmesh.data_texcoordtexture2f;
+		m.texmatrix[0] = rsurface_texture->currenttexmatrix;
 		if (r_shadow_rtlight->currentcubemap != r_texture_whitecube)
 		{
 			m.texcubemap[1] = R_GetTexture(r_shadow_rtlight->currentcubemap);
@@ -1566,8 +1562,8 @@
 		memset(&m, 0, sizeof(m));
 		m.tex[0] = R_GetTexture(normalmaptexture);
 		m.texcombinergb[0] = GL_REPLACE;
-		m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
-		m.texmatrix[0] = texture->currenttexmatrix;
+		m.pointer_texcoord[0] = rsurface_model->surfmesh.data_texcoordtexture2f;
+		m.texmatrix[0] = rsurface_texture->currenttexmatrix;
 		m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
 		m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
 		m.pointer_texcoord3f[1] = rsurface_array_texcoord3f;
@@ -1580,8 +1576,8 @@
 
 		memset(&m, 0, sizeof(m));
 		m.tex[0] = R_GetTexture(basetexture);
-		m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
-		m.texmatrix[0] = texture->currenttexmatrix;
+		m.pointer_texcoord[0] = rsurface_model->surfmesh.data_texcoordtexture2f;
+		m.texmatrix[0] = rsurface_texture->currenttexmatrix;
 		if (r_shadow_rtlight->currentcubemap != r_texture_whitecube)
 		{
 			m.texcubemap[1] = R_GetTexture(r_shadow_rtlight->currentcubemap);
@@ -1603,13 +1599,12 @@
 	GL_LockArrays(0, 0);
 }
 
-static void R_Shadow_RenderSurfacesLighting_Light_Dot3_SpecularPass(const entity_render_t *ent, const texture_t *texture, const msurface_t *surface, const vec3_t lightcolorbase, rtexture_t *glosstexture, rtexture_t *normalmaptexture, float colorscale)
+static void R_Shadow_RenderSurfacesLighting_Light_Dot3_SpecularPass(const msurface_t *surface, const vec3_t lightcolorbase, rtexture_t *glosstexture, rtexture_t *normalmaptexture, float colorscale)
 {
 	int renders;
-	model_t *model = ent->model;
 	float color2[3];
 	rmeshstate_t m;
-	const int *elements = model->surfmesh.data_element3i + surface->num_firsttriangle * 3;
+	const int *elements = rsurface_model->surfmesh.data_element3i + surface->num_firsttriangle * 3;
 	// FIXME: detect blendsquare!
 	//if (!gl_support_blendsquare)
 	//	return;
@@ -1619,8 +1614,8 @@
 		// 2/0/0/1/2 3D combine blendsquare path
 		memset(&m, 0, sizeof(m));
 		m.tex[0] = R_GetTexture(normalmaptexture);
-		m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
-		m.texmatrix[0] = texture->currenttexmatrix;
+		m.pointer_texcoord[0] = rsurface_model->surfmesh.data_texcoordtexture2f;
+		m.texmatrix[0] = rsurface_texture->currenttexmatrix;
 		m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
 		m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
 		m.pointer_texcoord3f[1] = rsurface_array_texcoord3f;
@@ -1657,8 +1652,8 @@
 
 		memset(&m, 0, sizeof(m));
 		m.tex[0] = R_GetTexture(glosstexture);
-		m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
-		m.texmatrix[0] = texture->currenttexmatrix;
+		m.pointer_texcoord[0] = rsurface_model->surfmesh.data_texcoordtexture2f;
+		m.texmatrix[0] = rsurface_texture->currenttexmatrix;
 		if (r_shadow_rtlight->currentcubemap != r_texture_whitecube)
 		{
 			m.texcubemap[1] = R_GetTexture(r_shadow_rtlight->currentcubemap);
@@ -1672,8 +1667,8 @@
 		// 2/0/0/2 3D combine blendsquare path
 		memset(&m, 0, sizeof(m));
 		m.tex[0] = R_GetTexture(normalmaptexture);
-		m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
-		m.texmatrix[0] = texture->currenttexmatrix;
+		m.pointer_texcoord[0] = rsurface_model->surfmesh.data_texcoordtexture2f;
+		m.texmatrix[0] = rsurface_texture->currenttexmatrix;
 		m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
 		m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
 		m.pointer_texcoord3f[1] = rsurface_array_texcoord3f;
@@ -1700,8 +1695,8 @@
 
 		memset(&m, 0, sizeof(m));
 		m.tex[0] = R_GetTexture(glosstexture);
-		m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
-		m.texmatrix[0] = texture->currenttexmatrix;
+		m.pointer_texcoord[0] = rsurface_model->surfmesh.data_texcoordtexture2f;
+		m.texmatrix[0] = rsurface_texture->currenttexmatrix;
 		m.tex3d[1] = R_GetTexture(r_shadow_attenuation3dtexture);
 		m.pointer_texcoord3f[1] = rsurface_vertex3f;
 		m.texmatrix[1] = r_shadow_entitytoattenuationxyz;
@@ -1712,8 +1707,8 @@
 		// 2/0/0/2/2 2D combine blendsquare path
 		memset(&m, 0, sizeof(m));
 		m.tex[0] = R_GetTexture(normalmaptexture);
-		m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
-		m.texmatrix[0] = texture->currenttexmatrix;
+		m.pointer_texcoord[0] = rsurface_model->surfmesh.data_texcoordtexture2f;
+		m.texmatrix[0] = rsurface_texture->currenttexmatrix;
 		m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
 		m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
 		m.pointer_texcoord3f[1] = rsurface_array_texcoord3f;
@@ -1753,8 +1748,8 @@
 
 		memset(&m, 0, sizeof(m));
 		m.tex[0] = R_GetTexture(glosstexture);
-		m.pointer_texcoord[0] = model->surfmesh.data_texcoordtexture2f;
-		m.texmatrix[0] = texture->currenttexmatrix;
+		m.pointer_texcoord[0] = rsurface_model->surfmesh.data_texcoordtexture2f;
+		m.texmatrix[0] = rsurface_texture->currenttexmatrix;
 		if (r_shadow_rtlight->currentcubemap != r_texture_whitecube)
 		{
 			m.texcubemap[1] = R_GetTexture(r_shadow_rtlight->currentcubemap);
@@ -1775,7 +1770,7 @@
 	GL_LockArrays(0, 0);
 }
 
-static void R_Shadow_RenderSurfacesLighting_Light_Dot3(const entity_render_t *ent, const texture_t *texture, int numsurfaces, msurface_t **surfacelist, const vec3_t lightcolorbase, const vec3_t lightcolorpants, const vec3_t lightcolorshirt, rtexture_t *basetexture, rtexture_t *pantstexture, rtexture_t *shirttexture, rtexture_t *normalmaptexture, rtexture_t *glosstexture, float specularscale, qboolean dopants, qboolean doshirt)
+static void R_Shadow_RenderSurfacesLighting_Light_Dot3(int numsurfaces, msurface_t **surfacelist, const vec3_t lightcolorbase, const vec3_t lightcolorpants, const vec3_t lightcolorshirt, rtexture_t *basetexture, rtexture_t *pantstexture, rtexture_t *shirttexture, rtexture_t *normalmaptexture, rtexture_t *glosstexture, float specularscale, qboolean dopants, qboolean doshirt)
 {
 	// ARB path (any Geforce, any Radeon)
 	int surfacelistindex;
@@ -1784,38 +1779,38 @@
 	qboolean dospecular = specularscale > 0;
 	if (!doambient && !dodiffuse && !dospecular)
 		return;
-	RSurf_PrepareVerticesForBatch(ent, texture, r_shadow_entityeyeorigin, true, true, numsurfaces, surfacelist);
+	RSurf_PrepareVerticesForBatch(true, true, numsurfaces, surfacelist);
 	R_Mesh_ColorPointer(NULL);
 	for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
 	{
 		const msurface_t *surface = surfacelist[surfacelistindex];
 		if (doambient)
-			R_Shadow_RenderSurfacesLighting_Light_Dot3_AmbientPass(ent, texture, surface, lightcolorbase, basetexture, r_shadow_rtlight->ambientscale);
+			R_Shadow_RenderSurfacesLighting_Light_Dot3_AmbientPass(surface, lightcolorbase, basetexture, r_shadow_rtlight->ambientscale);
 		if (dodiffuse)
-			R_Shadow_RenderSurfacesLighting_Light_Dot3_DiffusePass(ent, texture, surface, lightcolorbase, basetexture, normalmaptexture, r_shadow_rtlight->diffusescale);
+			R_Shadow_RenderSurfacesLighting_Light_Dot3_DiffusePass(surface, lightcolorbase, basetexture, normalmaptexture, r_shadow_rtlight->diffusescale);
 		if (dopants)
 		{
 			if (doambient)
-				R_Shadow_RenderSurfacesLighting_Light_Dot3_AmbientPass(ent, texture, surface, lightcolorpants, pantstexture, r_shadow_rtlight->ambientscale);
+				R_Shadow_RenderSurfacesLighting_Light_Dot3_AmbientPass(surface, lightcolorpants, pantstexture, r_shadow_rtlight->ambientscale);
 			if (dodiffuse)
-				R_Shadow_RenderSurfacesLighting_Light_Dot3_DiffusePass(ent, texture, surface, lightcolorpants, pantstexture, normalmaptexture, r_shadow_rtlight->diffusescale);
+				R_Shadow_RenderSurfacesLighting_Light_Dot3_DiffusePass(surface, lightcolorpants, pantstexture, normalmaptexture, r_shadow_rtlight->diffusescale);
 		}
 		if (doshirt)
 		{
 			if (doambient)
-				R_Shadow_RenderSurfacesLighting_Light_Dot3_AmbientPass(ent, texture, surface, lightcolorshirt, shirttexture, r_shadow_rtlight->ambientscale);
+				R_Shadow_RenderSurfacesLighting_Light_Dot3_AmbientPass(surface, lightcolorshirt, shirttexture, r_shadow_rtlight->ambientscale);
 			if (dodiffuse)
-				R_Shadow_RenderSurfacesLighting_Light_Dot3_DiffusePass(ent, texture, surface, lightcolorshirt, shirttexture, normalmaptexture, r_shadow_rtlight->diffusescale);
+				R_Shadow_RenderSurfacesLighting_Light_Dot3_DiffusePass(surface, lightcolorshirt, shirttexture, normalmaptexture, r_shadow_rtlight->diffusescale);
 		}
 		if (dospecular)
-			R_Shadow_RenderSurfacesLighting_Light_Dot3_SpecularPass(ent, texture, surface, lightcolorbase, glosstexture, normalmaptexture, specularscale);
+			R_Shadow_RenderSurfacesLighting_Light_Dot3_SpecularPass(surface, lightcolorbase, glosstexture, normalmaptexture, specularscale);
 	}
 }
 
 void R_Shadow_RenderSurfacesLighting_Light_Vertex_Pass(const model_t *model, const msurface_t *surface, vec3_t diffusecolor2, vec3_t ambientcolor2)
 {
 	int renders;
-	const int *elements = model->surfmesh.data_element3i + surface->num_firsttriangle * 3;
+	const int *elements = rsurface_model->surfmesh.data_element3i + surface->num_firsttriangle * 3;
 	R_Shadow_RenderSurfacesLighting_Light_Vertex_Shading(surface, diffusecolor2, ambientcolor2);
 	for (renders = 0;renders < 64 && (ambientcolor2[0] > renders || ambientcolor2[1] > renders || ambientcolor2[2] > renders || diffusecolor2[0] > renders || diffusecolor2[1] > renders || diffusecolor2[2] > renders);renders++)
 	{
@@ -1882,10 +1877,10 @@
 	}
 }
 
-static void R_Shadow_RenderSurfacesLighting_Light_Vertex(const entity_render_t *ent, const texture_t *texture, int numsurfaces, msurface_t **surfacelist, const vec3_t lightcolorbase, const vec3_t lightcolorpants, const vec3_t lightcolorshirt, rtexture_t *basetexture, rtexture_t *pantstexture, rtexture_t *shirttexture, rtexture_t *normalmaptexture, rtexture_t *glosstexture, float specularscale, qboolean dopants, qboolean doshirt)
+static void R_Shadow_RenderSurfacesLighting_Light_Vertex(int numsurfaces, msurface_t **surfacelist, const vec3_t lightcolorbase, const vec3_t lightcolorpants, const vec3_t lightcolorshirt, rtexture_t *basetexture, rtexture_t *pantstexture, rtexture_t *shirttexture, rtexture_t *normalmaptexture, rtexture_t *glosstexture, float specularscale, qboolean dopants, qboolean doshirt)
 {
 	int surfacelistindex;
-	model_t *model = ent->model;
+	model_t *model = rsurface_entity->model;
 	float ambientcolorbase[3], diffusecolorbase[3];
 	float ambientcolorpants[3], diffusecolorpants[3];
 	float ambientcolorshirt[3], diffusecolorshirt[3];
@@ -1900,8 +1895,8 @@
 	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;
+	m.texmatrix[0] = rsurface_texture->currenttexmatrix;
+	m.pointer_texcoord[0] = rsurface_model->surfmesh.data_texcoordtexture2f;
 	if (r_textureunits.integer >= 2)
 	{
 		// voodoo2 or TNT
@@ -1917,7 +1912,7 @@
 		}
 	}
 	R_Mesh_TextureState(&m);
-	RSurf_PrepareVerticesForBatch(ent, texture, r_shadow_entityeyeorigin, true, false, numsurfaces, surfacelist);
+	RSurf_PrepareVerticesForBatch(true, false, numsurfaces, surfacelist);
 	for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
 	{
 		const msurface_t *surface = surfacelist[surfacelistindex];
@@ -1937,53 +1932,53 @@
 	}
 }
 
-void R_Shadow_RenderSurfacesLighting(const entity_render_t *ent, const texture_t *texture, int numsurfaces, msurface_t **surfacelist)
+void R_Shadow_RenderSurfacesLighting(int numsurfaces, msurface_t **surfacelist)
 {
 	// FIXME: support MATERIALFLAG_NODEPTHTEST
 	vec3_t lightcolorbase, lightcolorpants, lightcolorshirt;
 	// calculate colors to render this texture with
-	lightcolorbase[0] = r_shadow_rtlight->currentcolor[0] * ent->colormod[0] * texture->currentalpha;
-	lightcolorbase[1] = r_shadow_rtlight->currentcolor[1] * ent->colormod[1] * texture->currentalpha;
-	lightcolorbase[2] = r_shadow_rtlight->currentcolor[2] * ent->colormod[2] * texture->currentalpha;
-	if ((r_shadow_rtlight->ambientscale + r_shadow_rtlight->diffusescale) * VectorLength2(lightcolorbase) + (r_shadow_rtlight->specularscale * texture->specularscale) * VectorLength2(lightcolorbase) < (1.0f / 1048576.0f))
+	lightcolorbase[0] = r_shadow_rtlight->currentcolor[0] * rsurface_entity->colormod[0] * rsurface_texture->currentalpha;
+	lightcolorbase[1] = r_shadow_rtlight->currentcolor[1] * rsurface_entity->colormod[1] * rsurface_texture->currentalpha;
+	lightcolorbase[2] = r_shadow_rtlight->currentcolor[2] * rsurface_entity->colormod[2] * rsurface_texture->currentalpha;
+	if ((r_shadow_rtlight->ambientscale + r_shadow_rtlight->diffusescale) * VectorLength2(lightcolorbase) + (r_shadow_rtlight->specularscale * rsurface_texture->specularscale) * VectorLength2(lightcolorbase) < (1.0f / 1048576.0f))
 		return;
-	if ((texture->textureflags & Q3TEXTUREFLAG_TWOSIDED) || (ent->flags & RENDER_NOCULLFACE))
+	if ((rsurface_texture->textureflags & Q3TEXTUREFLAG_TWOSIDED) || (rsurface_entity->flags & RENDER_NOCULLFACE))
 		qglDisable(GL_CULL_FACE);
 	else
 		qglEnable(GL_CULL_FACE);
-	if (texture->colormapping)
+	if (rsurface_texture->colormapping)
 	{
-		qboolean dopants = texture->skin.pants != NULL && VectorLength2(ent->colormap_pantscolor) >= (1.0f / 1048576.0f);
-		qboolean doshirt = texture->skin.shirt != NULL && VectorLength2(ent->colormap_shirtcolor) >= (1.0f / 1048576.0f);
+		qboolean dopants = rsurface_texture->skin.pants != NULL && VectorLength2(rsurface_entity->colormap_pantscolor) >= (1.0f / 1048576.0f);
+		qboolean doshirt = rsurface_texture->skin.shirt != NULL && VectorLength2(rsurface_entity->colormap_shirtcolor) >= (1.0f / 1048576.0f);
 		if (dopants)
 		{
-			lightcolorpants[0] = lightcolorbase[0] * ent->colormap_pantscolor[0];
-			lightcolorpants[1] = lightcolorbase[1] * ent->colormap_pantscolor[1];
-			lightcolorpants[2] = lightcolorbase[2] * ent->colormap_pantscolor[2];
+			lightcolorpants[0] = lightcolorbase[0] * rsurface_entity->colormap_pantscolor[0];
+			lightcolorpants[1] = lightcolorbase[1] * rsurface_entity->colormap_pantscolor[1];
+			lightcolorpants[2] = lightcolorbase[2] * rsurface_entity->colormap_pantscolor[2];
 		}
 		else
 			VectorClear(lightcolorpants);
 		if (doshirt)
 		{
-			lightcolorshirt[0] = lightcolorbase[0] * ent->colormap_shirtcolor[0];
-			lightcolorshirt[1] = lightcolorbase[1] * ent->colormap_shirtcolor[1];
-			lightcolorshirt[2] = lightcolorbase[2] * ent->colormap_shirtcolor[2];
+			lightcolorshirt[0] = lightcolorbase[0] * rsurface_entity->colormap_shirtcolor[0];
+			lightcolorshirt[1] = lightcolorbase[1] * rsurface_entity->colormap_shirtcolor[1];
+			lightcolorshirt[2] = lightcolorbase[2] * rsurface_entity->colormap_shirtcolor[2];
 		}
 		else
 			VectorClear(lightcolorshirt);
 		switch (r_shadow_rendermode)
 		{
 		case R_SHADOW_RENDERMODE_VISIBLELIGHTING:
-			R_Shadow_RenderSurfacesLighting_VisibleLighting(ent, texture, numsurfaces, surfacelist, lightcolorbase, lightcolorpants, lightcolorshirt, texture->basetexture, texture->skin.pants, texture->skin.shirt, texture->skin.nmap, texture->glosstexture, r_shadow_rtlight->specularscale * texture->specularscale, dopants, doshirt);
+			R_Shadow_RenderSurfacesLighting_VisibleLighting(numsurfaces, surfacelist, lightcolorbase, lightcolorpants, lightcolorshirt, rsurface_texture->basetexture, rsurface_texture->skin.pants, rsurface_texture->skin.shirt, rsurface_texture->skin.nmap, rsurface_texture->glosstexture, r_shadow_rtlight->specularscale * rsurface_texture->specularscale, dopants, doshirt);
 			break;
 		case R_SHADOW_RENDERMODE_LIGHT_GLSL:
-			R_Shadow_RenderSurfacesLighting_Light_GLSL(ent, texture, numsurfaces, surfacelist, lightcolorbase, lightcolorpants, lightcolorshirt, texture->basetexture, texture->skin.pants, texture->skin.shirt, texture->skin.nmap, texture->glosstexture, r_shadow_rtlight->specularscale * texture->specularscale, dopants, doshirt);
+			R_Shadow_RenderSurfacesLighting_Light_GLSL(numsurfaces, surfacelist, lightcolorbase, lightcolorpants, lightcolorshirt, rsurface_texture->basetexture, rsurface_texture->skin.pants, rsurface_texture->skin.shirt, rsurface_texture->skin.nmap, rsurface_texture->glosstexture, r_shadow_rtlight->specularscale * rsurface_texture->specularscale, dopants, doshirt);
 			break;
 		case R_SHADOW_RENDERMODE_LIGHT_DOT3:
-			R_Shadow_RenderSurfacesLighting_Light_Dot3(ent, texture, numsurfaces, surfacelist, lightcolorbase, lightcolorpants, lightcolorshirt, texture->basetexture, texture->skin.pants, texture->skin.shirt, texture->skin.nmap, texture->glosstexture, r_shadow_rtlight->specularscale * texture->specularscale, dopants, doshirt);
+			R_Shadow_RenderSurfacesLighting_Light_Dot3(numsurfaces, surfacelist, lightcolorbase, lightcolorpants, lightcolorshirt, rsurface_texture->basetexture, rsurface_texture->skin.pants, rsurface_texture->skin.shirt, rsurface_texture->skin.nmap, rsurface_texture->glosstexture, r_shadow_rtlight->specularscale * rsurface_texture->specularscale, dopants, doshirt);
 			break;
 		case R_SHADOW_RENDERMODE_LIGHT_VERTEX:
-			R_Shadow_RenderSurfacesLighting_Light_Vertex(ent, texture, numsurfaces, surfacelist, lightcolorbase, lightcolorpants, lightcolorshirt, texture->basetexture, texture->skin.pants, texture->skin.shirt, texture->skin.nmap, texture->glosstexture, r_shadow_rtlight->specularscale * texture->specularscale, dopants, doshirt);
+			R_Shadow_RenderSurfacesLighting_Light_Vertex(numsurfaces, surfacelist, lightcolorbase, lightcolorpants, lightcolorshirt, rsurface_texture->basetexture, rsurface_texture->skin.pants, rsurface_texture->skin.shirt, rsurface_texture->skin.nmap, rsurface_texture->glosstexture, r_shadow_rtlight->specularscale * rsurface_texture->specularscale, dopants, doshirt);
 			break;
 		default:
 			Con_Printf("R_Shadow_RenderSurfacesLighting: unknown r_shadow_rendermode %i\n", r_shadow_rendermode);
@@ -1995,16 +1990,16 @@
 		switch (r_shadow_rendermode)
 		{
 		case R_SHADOW_RENDERMODE_VISIBLELIGHTING:
-			R_Shadow_RenderSurfacesLighting_VisibleLighting(ent, texture, numsurfaces, surfacelist, lightcolorbase, vec3_origin, vec3_origin, texture->basetexture, r_texture_black, r_texture_black, texture->skin.nmap, texture->glosstexture, r_shadow_rtlight->specularscale * texture->specularscale, false, false);
+			R_Shadow_RenderSurfacesLighting_VisibleLighting(numsurfaces, surfacelist, lightcolorbase, vec3_origin, vec3_origin, rsurface_texture->basetexture, r_texture_black, r_texture_black, rsurface_texture->skin.nmap, rsurface_texture->glosstexture, r_shadow_rtlight->specularscale * rsurface_texture->specularscale, false, false);
 			break;
 		case R_SHADOW_RENDERMODE_LIGHT_GLSL:
-			R_Shadow_RenderSurfacesLighting_Light_GLSL(ent, texture, numsurfaces, surfacelist, lightcolorbase, vec3_origin, vec3_origin, texture->basetexture, r_texture_black, r_texture_black, texture->skin.nmap, texture->glosstexture, r_shadow_rtlight->specularscale * texture->specularscale, false, false);
+			R_Shadow_RenderSurfacesLighting_Light_GLSL(numsurfaces, surfacelist, lightcolorbase, vec3_origin, vec3_origin, rsurface_texture->basetexture, r_texture_black, r_texture_black, rsurface_texture->skin.nmap, rsurface_texture->glosstexture, r_shadow_rtlight->specularscale * rsurface_texture->specularscale, false, false);
 			break;
 		case R_SHADOW_RENDERMODE_LIGHT_DOT3:
-			R_Shadow_RenderSurfacesLighting_Light_Dot3(ent, texture, numsurfaces, surfacelist, lightcolorbase, vec3_origin, vec3_origin, texture->basetexture, r_texture_black, r_texture_black, texture->skin.nmap, texture->glosstexture, r_shadow_rtlight->specularscale * texture->specularscale, false, false);
+			R_Shadow_RenderSurfacesLighting_Light_Dot3(numsurfaces, surfacelist, lightcolorbase, vec3_origin, vec3_origin, rsurface_texture->basetexture, r_texture_black, r_texture_black, rsurface_texture->skin.nmap, rsurface_texture->glosstexture, r_shadow_rtlight->specularscale * rsurface_texture->specularscale, false, false);
 			break;
 		case R_SHADOW_RENDERMODE_LIGHT_VERTEX:
-			R_Shadow_RenderSurfacesLighting_Light_Vertex(ent, texture, numsurfaces, surfacelist, lightcolorbase, vec3_origin, vec3_origin, texture->basetexture, r_texture_black, r_texture_black, texture->skin.nmap, texture->glosstexture, r_shadow_rtlight->specularscale * texture->specularscale, false, false);
+			R_Shadow_RenderSurfacesLighting_Light_Vertex(numsurfaces, surfacelist, lightcolorbase, vec3_origin, vec3_origin, rsurface_texture->basetexture, r_texture_black, r_texture_black, rsurface_texture->skin.nmap, rsurface_texture->glosstexture, r_shadow_rtlight->specularscale * rsurface_texture->specularscale, false, false);
 			break;
 		default:
 			Con_Printf("R_Shadow_RenderSurfacesLighting: unknown r_shadow_rendermode %i\n", r_shadow_rendermode);
@@ -2207,12 +2202,14 @@
 void R_Shadow_SetupEntityLight(const entity_render_t *ent)
 {
 	// set up properties for rendering light onto this entity
+	RSurf_ActiveEntity(ent);
 	Matrix4x4_Concat(&r_shadow_entitytolight, &r_shadow_rtlight->matrix_worldtolight, &ent->matrix);
 	Matrix4x4_Concat(&r_shadow_entitytoattenuationxyz, &matrix_attenuationxyz, &r_shadow_entitytolight);
 	Matrix4x4_Concat(&r_shadow_entitytoattenuationz, &matrix_attenuationz, &r_shadow_entitytolight);
 	Matrix4x4_Transform(&ent->inversematrix, r_shadow_rtlight->shadoworigin, r_shadow_entitylightorigin);
-	Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, r_shadow_entityeyeorigin);
-	R_Mesh_Matrix(&ent->matrix);
+	VectorCopy(rsurface_modelorg, r_shadow_entityeyeorigin);
+	if (r_shadow_lightingrendermode == R_SHADOW_RENDERMODE_LIGHT_GLSL)
+		R_Mesh_TexMatrix(3, &r_shadow_entitytolight);
 }
 
 void R_Shadow_DrawEntityLight(entity_render_t *ent, int numsurfaces, int *surfacelist)
Index: darkplaces/r_shadow.h
diff -u darkplaces/r_shadow.h:1.52 darkplaces/r_shadow.h:1.53
--- darkplaces/r_shadow.h:1.52	Tue Mar 21 00:49:29 2006
+++ darkplaces/r_shadow.h	Wed Apr 19 23:43:20 2006
@@ -32,7 +32,7 @@
 void R_Shadow_Init(void);
 void R_Shadow_VolumeFromList(int numverts, int numtris, const float *invertex3f, const int *elements, const int *neighbors, const vec3_t projectorigin, float projectdistance, int nummarktris, const int *marktris);
 void R_Shadow_MarkVolumeFromBox(int firsttriangle, int numtris, const float *invertex3f, const int *elements, const vec3_t projectorigin, const vec3_t lightmins, const vec3_t lightmaxs, const vec3_t surfacemins, const vec3_t surfacemaxs);
-void R_Shadow_RenderSurfacesLighting(const entity_render_t *ent, const texture_t *texture, int numsurfaces, msurface_t **surfacelist);
+void R_Shadow_RenderSurfacesLighting(int numsurfaces, msurface_t **surfacelist);
 void R_Shadow_RenderMode_Begin(void);
 void R_Shadow_RenderMode_ActiveLight(rtlight_t *rtlight);
 void R_Shadow_RenderMode_Reset(void);
Index: darkplaces/render.h
diff -u darkplaces/render.h:1.125 darkplaces/render.h:1.126
--- darkplaces/render.h:1.125	Sun Apr 16 21:58:32 2006
+++ darkplaces/render.h	Wed Apr 19 23:43:20 2006
@@ -263,22 +263,42 @@
 extern mempool_t *r_main_mempool;
 
 extern int rsurface_array_size;
-extern float *rsurface_array_vertex3f;
-extern float *rsurface_array_svector3f;
-extern float *rsurface_array_tvector3f;
-extern float *rsurface_array_normal3f;
+extern float *rsurface_array_modelvertex3f;
+extern float *rsurface_array_modelsvector3f;
+extern float *rsurface_array_modeltvector3f;
+extern float *rsurface_array_modelnormal3f;
+extern float *rsurface_array_deformedvertex3f;
+extern float *rsurface_array_deformedsvector3f;
+extern float *rsurface_array_deformedtvector3f;
+extern float *rsurface_array_deformednormal3f;
 extern float *rsurface_array_color4f;
 extern float *rsurface_array_texcoord3f;
 
+typedef enum rsurfmode_e
+{
+	RSURFMODE_NONE,
+	RSURFMODE_SHOWSURFACES,
+	RSURFMODE_SKY,
+	RSURFMODE_MULTIPASS,
+	RSURFMODE_GLSL
+}
+rsurfmode_t;
+
 extern float *rsurface_vertex3f;
 extern float *rsurface_svector3f;
 extern float *rsurface_tvector3f;
 extern float *rsurface_normal3f;
 extern float *rsurface_lightmapcolor4f;
 extern vec3_t rsurface_modelorg;
+extern qboolean rsurface_generatedvertex;
 extern const entity_render_t *rsurface_entity;
 extern const model_t *rsurface_model;
-extern const texture_t *rsurface_texture;
+extern texture_t *rsurface_texture;
+extern rtexture_t *rsurface_lightmaptexture;
+extern rsurfmode_t rsurface_mode;
+
+void RSurf_ActiveEntity(const entity_render_t *ent);
+void RSurf_CleanUp(void);
 
 void R_Mesh_ResizeArrays(int newvertices);
 
@@ -287,10 +307,10 @@
 struct msurface_s;
 void R_UpdateTextureInfo(const entity_render_t *ent, texture_t *t);
 void R_UpdateAllTextureInfo(entity_render_t *ent);
-void R_QueueTextureSurfaceList(entity_render_t *ent, texture_t *texture, rtexture_t *lightmaptexture, int texturenumsurfaces, msurface_t **texturesurfacelist, const vec3_t modelorg);
+void R_QueueTextureSurfaceList(int texturenumsurfaces, msurface_t **texturesurfacelist);
 void R_DrawSurfaces(entity_render_t *ent, qboolean skysurfaces);
 
-void RSurf_PrepareVerticesForBatch(const entity_render_t *ent, const texture_t *texture, const vec3_t modelorg, qboolean generatenormals, qboolean generatetangents, int texturenumsurfaces, msurface_t **texturesurfacelist);
+void RSurf_PrepareVerticesForBatch(qboolean generatenormals, qboolean generatetangents, int texturenumsurfaces, msurface_t **texturesurfacelist);
 
 #define SHADERPERMUTATION_MODE_LIGHTSOURCE (1<<0) // (lightsource) use directional pixel shading from light source (rtlight)
 #define SHADERPERMUTATION_MODE_LIGHTDIRECTIONMAP_MODELSPACE (1<<1) // (lightmap) use directional pixel shading from texture containing modelspace light directions (deluxemap)
@@ -346,7 +366,8 @@
 extern r_glsl_permutation_t *r_glsl_permutation;
 
 void R_GLSL_CompilePermutation(int permutation);
-void R_SetupSurfaceShader(const entity_render_t *ent, const texture_t *texture, rtexture_t *lightmaptexture, const vec3_t modelorg, const vec3_t lightcolorbase, qboolean modellighting);
+int R_SetupSurfaceShader(const vec3_t lightcolorbase, qboolean modellighting);
+void R_SwitchSurfaceShader(int permutation);
 
 #endif
 