From 94f4b44284f6276d58946eda4101b26faa4073a4 Mon Sep 17 00:00:00 2001
From: havoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Date: Sun, 18 Sep 2011 02:45:26 +0000
Subject: [PATCH] fix watershader on entities

git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@11361 d7cf8633-e32d-0410-b094-e92efae38249
::stable-branch::merge=5123996e8dbc654bb2401492d43205b6b2112ac5
---
 gl_rmain.c | 29 ++++++++++++++---------------
 gl_rsurf.c |  2 +-
 2 files changed, 15 insertions(+), 16 deletions(-)

diff --git a/gl_rmain.c b/gl_rmain.c
index e92620c0..b4ef313a 100644
--- a/gl_rmain.c
+++ b/gl_rmain.c
@@ -5325,6 +5325,7 @@ void R_View_UpdateWithScissor(const int *myscissor)
 	R_View_WorldVisibility(r_refdef.view.useclipplane);
 	R_View_UpdateEntityVisible();
 	R_View_UpdateEntityLighting();
+	R_AnimCache_CacheVisibleEntities();
 }
 
 void R_View_Update(void)
@@ -5334,6 +5335,7 @@ void R_View_Update(void)
 	R_View_WorldVisibility(r_refdef.view.useclipplane);
 	R_View_UpdateEntityVisible();
 	R_View_UpdateEntityLighting();
+	R_AnimCache_CacheVisibleEntities();
 }
 
 float viewscalefpsadjusted = 1.0f;
@@ -5633,16 +5635,18 @@ void R_Water_AddWaterPlane(msurface_t *surface, int entno)
 	r_waterstate_waterplane_t *p;
 	texture_t *t = R_GetCurrentTexture(surface->texture);
 
+	rsurface.texture = t;
+	RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_NOGAPS, 1, ((const msurface_t **)&surface));
 	// if the model has no normals, it's probably off-screen and they were not generated, so don't add it anyway
-	if (!rsurface.modelnormal3f)
+	if (!rsurface.batchnormal3f || rsurface.batchnumvertices < 1)
 		return;
-
-	RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | BATCHNEED_ARRAY_NORMAL | BATCHNEED_NOGAPS, 1, ((const msurface_t **)&surface));
 	// average the vertex normals, find the surface bounds (after deformvertexes)
-	VectorClear(normal);
-	VectorCopy(rsurface.batchvertex3f, mins);
-	VectorCopy(rsurface.batchvertex3f, maxs);
-	for (vertexindex = 0;vertexindex < rsurface.batchnumvertices;vertexindex++)
+	Matrix4x4_Transform(&rsurface.matrix, rsurface.batchvertex3f, v);
+	Matrix4x4_Transform3x3(&rsurface.matrix, rsurface.batchnormal3f, n);
+	VectorCopy(n, normal);
+	VectorCopy(v, mins);
+	VectorCopy(v, maxs);
+	for (vertexindex = 1;vertexindex < rsurface.batchnumvertices;vertexindex++)
 	{
 		Matrix4x4_Transform(&rsurface.matrix, rsurface.batchvertex3f + vertexindex*3, v);
 		Matrix4x4_Transform3x3(&rsurface.matrix, rsurface.batchnormal3f + vertexindex*3, n);
@@ -5664,8 +5668,8 @@ void R_Water_AddWaterPlane(msurface_t *surface, int entno)
 	if (PlaneDiff(r_refdef.view.origin, &plane) < 0)
 	{
 		// skip backfaces (except if nocullface is set)
-		if (!(t->currentmaterialflags & MATERIALFLAG_NOCULLFACE))
-			return;
+//		if (!(t->currentmaterialflags & MATERIALFLAG_NOCULLFACE))
+//			return;
 		VectorNegate(plane.normal, plane.normal);
 		plane.dist *= -1;
 		PlaneClassify(&plane);
@@ -5694,9 +5698,8 @@ void R_Water_AddWaterPlane(msurface_t *surface, int entno)
 	if ((planeindex < 0 || bestplanescore > 0.001f) && r_waterstate.numwaterplanes < r_waterstate.maxwaterplanes)
 	{
 		// store the new plane
-		planeindex = r_waterstate.numwaterplanes;
+		planeindex = r_waterstate.numwaterplanes++;
 		p = r_waterstate.waterplanes + planeindex;
-		r_waterstate.numwaterplanes++;
 		p->plane = plane;
 		// clear materialflags and pvs
 		p->materialflags = 0;
@@ -7046,10 +7049,6 @@ void R_RenderScene(void)
 		}
 	}
 
-	R_AnimCache_CacheVisibleEntities();
-	if (r_timereport_active)
-		R_TimeReport("animation");
-
 	R_Shadow_PrepareLights();
 	if (r_shadows.integer > 0 && r_refdef.lightmapintensity > 0)
 		R_Shadow_PrepareModelShadows();
diff --git a/gl_rsurf.c b/gl_rsurf.c
index 06805897..4f7d7f3d 100644
--- a/gl_rsurf.c
+++ b/gl_rsurf.c
@@ -581,7 +581,7 @@ void R_Q1BSP_DrawAddWaterPlanes(entity_render_t *ent)
 	if (ent == r_refdef.scene.worldentity)
 		RSurf_ActiveWorldEntity();
 	else
-		RSurf_ActiveModelEntity(ent, false, false, false);
+		RSurf_ActiveModelEntity(ent, true, false, false);
 
 	surfaces = model->data_surfaces;
 	flagsmask = MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION | MATERIALFLAG_CAMERA;
-- 
2.39.5