]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_rmain.c
found out why the water plane issue happend: namely, when a water plane is backface...
[xonotic/darkplaces.git] / gl_rmain.c
index c443a69a7f3ee2ec9cd490d1f5c5380c55c13fa1..626762f51ddc91735eeaaecdf1e3ce03200cb73e 100644 (file)
@@ -809,7 +809,11 @@ static const char *builtinshaderstring =
 "\n"
 "void main(void)\n"
 "{\n"
+"#ifdef USEVIEWTINT\n"
 "      gl_FragColor = gl_Color;\n"
+"#else\n"
+"      gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);\n"
+"#endif\n"
 "#ifdef USEDIFFUSE\n"
 "      gl_FragColor *= texture2D(Texture_First, TexCoord1);\n"
 "#endif\n"
@@ -1673,6 +1677,7 @@ static const char *builtinshaderstring =
 "      lightnormal.x = dot(lightnormal_modelspace, myhalf3(VectorS));\n"
 "      lightnormal.y = dot(lightnormal_modelspace, myhalf3(VectorT));\n"
 "      lightnormal.z = dot(lightnormal_modelspace, myhalf3(VectorR));\n"
+"      lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this\n"
 "      // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division)\n"
 "      // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar\n"
 "      // is used (the lightmap and deluxemap coords correspond to virtually random coordinates\n"
@@ -2089,7 +2094,11 @@ const char *builtincgshaderstring =
 "out float4 gl_FragColor : COLOR\n"
 ")\n"
 "{\n"
+"#ifdef USEVIEWTINT\n"
 "      gl_FragColor = gl_FrontColor;\n"
+"#else\n"
+"      gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);\n"
+"#endif\n"
 "#ifdef USEDIFFUSE\n"
 "      gl_FragColor *= tex2D(Texture_First, TexCoord1);\n"
 "#endif\n"
@@ -3336,7 +3345,7 @@ typedef enum shaderpermutation_e
 {
        SHADERPERMUTATION_DIFFUSE = 1<<0, ///< (lightsource) whether to use directional shading
        SHADERPERMUTATION_VERTEXTEXTUREBLEND = 1<<1, ///< indicates this is a two-layer material blend based on vertex alpha (q3bsp)
-       SHADERPERMUTATION_VIEWTINT = 1<<2, ///< view tint (postprocessing only)
+       SHADERPERMUTATION_VIEWTINT = 1<<2, ///< view tint (postprocessing only), use vertex colors (generic only)
        SHADERPERMUTATION_COLORMAPPING = 1<<3, ///< indicates this is a colormapped skin
        SHADERPERMUTATION_SATURATION = 1<<4, ///< saturation (postprocessing only)
        SHADERPERMUTATION_FOGINSIDE = 1<<5, ///< tint the color by fog color or black if using additive blend mode
@@ -5141,7 +5150,7 @@ void R_SetupShader_Generic(rtexture_t *first, rtexture_t *second, int texturemod
        {
        case RENDERPATH_D3D9:
 #ifdef SUPPORTD3D
-               R_SetupShader_SetPermutationHLSL(SHADERMODE_GENERIC, (first ? SHADERPERMUTATION_DIFFUSE : 0) | (second ? SHADERPERMUTATION_SPECULAR : 0) | (texturemode == GL_MODULATE ? SHADERPERMUTATION_COLORMAPPING : (texturemode == GL_ADD ? SHADERPERMUTATION_GLOW : (texturemode == GL_DECAL ? SHADERPERMUTATION_VERTEXTEXTUREBLEND : 0))));
+               R_SetupShader_SetPermutationHLSL(SHADERMODE_GENERIC, SHADERPERMUTATION_VIEWTINT | (first ? SHADERPERMUTATION_DIFFUSE : 0) | (second ? SHADERPERMUTATION_SPECULAR : 0) | (texturemode == GL_MODULATE ? SHADERPERMUTATION_COLORMAPPING : (texturemode == GL_ADD ? SHADERPERMUTATION_GLOW : (texturemode == GL_DECAL ? SHADERPERMUTATION_VERTEXTEXTUREBLEND : 0))));
                R_Mesh_TexBind(GL20TU_FIRST , first );
                R_Mesh_TexBind(GL20TU_SECOND, second);
 #endif
@@ -5153,14 +5162,14 @@ void R_SetupShader_Generic(rtexture_t *first, rtexture_t *second, int texturemod
                Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
                break;
        case RENDERPATH_GL20:
-               R_SetupShader_SetPermutationGLSL(SHADERMODE_GENERIC, (first ? SHADERPERMUTATION_DIFFUSE : 0) | (second ? SHADERPERMUTATION_SPECULAR : 0) | (texturemode == GL_MODULATE ? SHADERPERMUTATION_COLORMAPPING : (texturemode == GL_ADD ? SHADERPERMUTATION_GLOW : (texturemode == GL_DECAL ? SHADERPERMUTATION_VERTEXTEXTUREBLEND : 0))));
+               R_SetupShader_SetPermutationGLSL(SHADERMODE_GENERIC, SHADERPERMUTATION_VIEWTINT | (first ? SHADERPERMUTATION_DIFFUSE : 0) | (second ? SHADERPERMUTATION_SPECULAR : 0) | (texturemode == GL_MODULATE ? SHADERPERMUTATION_COLORMAPPING : (texturemode == GL_ADD ? SHADERPERMUTATION_GLOW : (texturemode == GL_DECAL ? SHADERPERMUTATION_VERTEXTEXTUREBLEND : 0))));
                R_Mesh_TexBind(GL20TU_FIRST , first );
                R_Mesh_TexBind(GL20TU_SECOND, second);
                break;
        case RENDERPATH_CGGL:
 #ifdef SUPPORTCG
                CHECKCGERROR
-               R_SetupShader_SetPermutationCG(SHADERMODE_GENERIC, (first ? SHADERPERMUTATION_DIFFUSE : 0) | (second ? SHADERPERMUTATION_SPECULAR : 0) | (texturemode == GL_MODULATE ? SHADERPERMUTATION_COLORMAPPING : (texturemode == GL_ADD ? SHADERPERMUTATION_GLOW : (texturemode == GL_DECAL ? SHADERPERMUTATION_VERTEXTEXTUREBLEND : 0))));
+               R_SetupShader_SetPermutationCG(SHADERMODE_GENERIC, SHADERPERMUTATION_VIEWTINT | (first ? SHADERPERMUTATION_DIFFUSE : 0) | (second ? SHADERPERMUTATION_SPECULAR : 0) | (texturemode == GL_MODULATE ? SHADERPERMUTATION_COLORMAPPING : (texturemode == GL_ADD ? SHADERPERMUTATION_GLOW : (texturemode == GL_DECAL ? SHADERPERMUTATION_VERTEXTEXTUREBLEND : 0))));
                if (r_cg_permutation->fp_Texture_First ) CG_BindTexture(r_cg_permutation->fp_Texture_First , first );CHECKCGERROR
                if (r_cg_permutation->fp_Texture_Second) CG_BindTexture(r_cg_permutation->fp_Texture_Second, second);CHECKCGERROR
 #endif
@@ -7901,7 +7910,7 @@ static void R_View_UpdateEntityLighting (void)
                        {
                                if (ent->model->sprite.sprnum_type == SPR_OVERHEAD) // apply offset for overhead sprites
                                        org[2] = org[2] + r_overheadsprites_pushback.value;
-                               R_CompleteLightPoint(ent->modellight_ambient, ent->modellight_diffuse, ent->modellight_lightdir, org, true, true);
+                               R_CompleteLightPoint(ent->modellight_ambient, ent->modellight_diffuse, ent->modellight_lightdir, org, LP_LIGHTMAP | LP_RTWORLD | LP_DYNLIGHT);
                        }
                        else
                                r_refdef.scene.worldmodel->brush.LightPoint(r_refdef.scene.worldmodel, org, ent->modellight_ambient, ent->modellight_diffuse, tempdiffusenormal);
@@ -9159,13 +9168,18 @@ void R_HDR_RenderBloomTexture(void)
 {
        int oldwidth, oldheight;
        float oldcolorscale;
+       int oldwaterstate;
 
+       oldwaterstate = r_waterstate.enabled;
        oldcolorscale = r_refdef.view.colorscale;
        oldwidth = r_refdef.view.width;
        oldheight = r_refdef.view.height;
        r_refdef.view.width = r_bloomstate.bloomwidth;
        r_refdef.view.height = r_bloomstate.bloomheight;
 
+       if(r_hdr.integer < 2)
+               r_waterstate.enabled = false;
+
        // TODO: support GL_EXT_framebuffer_object rather than reusing the framebuffer?  it might improve SLI performance.
        // TODO: add exposure compensation features
        // TODO: add fp16 framebuffer support (using GL_EXT_framebuffer_object)
@@ -9185,7 +9199,7 @@ void R_HDR_RenderBloomTexture(void)
 
        // only do secondary renders with HDR if r_hdr is 2 or higher
        r_waterstate.numwaterplanes = 0;
-       if (r_waterstate.enabled && r_hdr.integer >= 2)
+       if (r_waterstate.enabled)
                R_RenderWaterPlanes();
 
        r_refdef.view.showdebug = true;
@@ -9198,6 +9212,7 @@ void R_HDR_RenderBloomTexture(void)
        R_Bloom_MakeTexture();
 
        // restore the view settings
+       r_waterstate.enabled = oldwaterstate;
        r_refdef.view.width = oldwidth;
        r_refdef.view.height = oldheight;
        r_refdef.view.colorscale = oldcolorscale;
@@ -9464,7 +9479,7 @@ void R_UpdateVariables(void)
 
        r_refdef.scene.rtworld = r_shadow_realtime_world.integer != 0;
        r_refdef.scene.rtworldshadows = r_shadow_realtime_world_shadows.integer && vid.stencil;
-       r_refdef.scene.rtdlight = (r_shadow_realtime_world.integer || r_shadow_realtime_dlight.integer) && !gl_flashblend.integer && r_dynamic.integer;
+       r_refdef.scene.rtdlight = r_shadow_realtime_dlight.integer != 0 && !gl_flashblend.integer && r_dynamic.integer;
        r_refdef.scene.rtdlightshadows = r_refdef.scene.rtdlight && r_shadow_realtime_dlight_shadows.integer && vid.stencil;
        r_refdef.lightmapintensity = r_refdef.scene.rtworld ? r_shadow_realtime_world_lightmaps.value : 1;
        if (FAKELIGHT_ENABLED)
@@ -9628,6 +9643,7 @@ R_RenderView
 */
 void R_RenderView(void)
 {
+       matrix4x4_t originalmatrix = r_refdef.view.matrix, offsetmatrix;
        if (r_timereport_active)
                R_TimeReport("start");
        r_textureframe++; // used only by R_GetCurrentTexture
@@ -9642,6 +9658,13 @@ void R_RenderView(void)
        R_AnimCache_ClearCache();
        R_FrameData_NewFrame();
 
+       /* adjust for stereo display */
+       if(R_Stereo_Active())
+       {
+               Matrix4x4_CreateFromQuakeEntity(&offsetmatrix, 0, r_stereo_separation.value * (0.5f - r_stereo_side), 0, 0, r_stereo_angle.value * (0.5f - r_stereo_side), 0, 1);
+               Matrix4x4_Concat(&r_refdef.view.matrix, &originalmatrix, &offsetmatrix);
+       }
+
        if (r_refdef.view.isoverlay)
        {
                // TODO: FIXME: move this into its own backend function maybe? [2/5/2008 Andreas]
@@ -9655,12 +9678,17 @@ void R_RenderView(void)
 
                R_RenderScene();
 
+               r_refdef.view.matrix = originalmatrix;
+
                CHECKGLERROR
                return;
        }
 
        if (!r_refdef.scene.entities || r_refdef.view.width * r_refdef.view.height == 0 || !r_renderview.integer || cl_videoplaying/* || !r_refdef.scene.worldmodel*/)
+       {
+               r_refdef.view.matrix = originalmatrix;
                return; //Host_Error ("R_RenderView: NULL worldmodel");
+       }
 
        r_refdef.view.colorscale = r_hdr_scenebrightness.value;
 
@@ -9713,6 +9741,8 @@ void R_RenderView(void)
        GL_Scissor(0, 0, vid.width, vid.height);
        GL_ScissorTest(false);
 
+       r_refdef.view.matrix = originalmatrix;
+
        CHECKGLERROR
 }
 
@@ -10860,6 +10890,7 @@ void R_Mesh_ResizeArrays(int newvertices)
 
 void RSurf_ActiveWorldEntity(void)
 {
+       int newvertices;
        dp_model_t *model = r_refdef.scene.worldmodel;
        //if (rsurface.entity == r_refdef.scene.worldentity)
        //      return;
@@ -10870,7 +10901,9 @@ void RSurf_ActiveWorldEntity(void)
        rsurface.ent_qwskin = -1;
        rsurface.ent_shadertime = 0;
        rsurface.ent_flags = r_refdef.scene.worldentity->flags;
-       R_Mesh_ResizeArrays(max(model->surfmesh.num_vertices, model->surfmesh.num_triangles));
+       newvertices = max(model->surfmesh.num_vertices, model->surfmesh.num_triangles);
+       if (rsurface.array_size < newvertices)
+               R_Mesh_ResizeArrays(newvertices);
        rsurface.matrix = identitymatrix;
        rsurface.inversematrix = identitymatrix;
        rsurface.matrixscale = 1;
@@ -10974,6 +11007,7 @@ void RSurf_ActiveWorldEntity(void)
 
 void RSurf_ActiveModelEntity(const entity_render_t *ent, qboolean wantnormals, qboolean wanttangents, qboolean prepass)
 {
+       int newvertices;
        dp_model_t *model = ent->model;
        //if (rsurface.entity == ent && (!model->surfmesh.isanimated || (!wantnormals && !wanttangents)))
        //      return;
@@ -10984,7 +11018,9 @@ void RSurf_ActiveModelEntity(const entity_render_t *ent, qboolean wantnormals, q
        rsurface.ent_qwskin = (ent->entitynumber <= cl.maxclients && ent->entitynumber >= 1 && cls.protocol == PROTOCOL_QUAKEWORLD && cl.scores[ent->entitynumber - 1].qw_skin[0] && !strcmp(ent->model->name, "progs/player.mdl")) ? (ent->entitynumber - 1) : -1;
        rsurface.ent_shadertime = ent->shadertime;
        rsurface.ent_flags = ent->flags;
-       R_Mesh_ResizeArrays(max(model->surfmesh.num_vertices, model->surfmesh.num_triangles));
+       newvertices = max(model->surfmesh.num_vertices, model->surfmesh.num_triangles);
+       if (rsurface.array_size < newvertices)
+               R_Mesh_ResizeArrays(newvertices);
        rsurface.matrix = ent->matrix;
        rsurface.inversematrix = ent->inversematrix;
        rsurface.matrixscale = Matrix4x4_ScaleFromMatrix(&rsurface.matrix);
@@ -11154,7 +11190,7 @@ void RSurf_ActiveModelEntity(const entity_render_t *ent, qboolean wantnormals, q
 
 void RSurf_ActiveCustomEntity(const matrix4x4_t *matrix, const matrix4x4_t *inversematrix, int entflags, double shadertime, float r, float g, float b, float a, int numvertices, const float *vertex3f, const float *texcoord2f, const float *normal3f, const float *svector3f, const float *tvector3f, const float *color4f, int numtriangles, const int *element3i, const unsigned short *element3s, qboolean wantnormals, qboolean wanttangents)
 {
-       int i;
+       int newvertices;
 
        rsurface.entity = r_refdef.scene.worldentity;
        rsurface.skeleton = NULL;
@@ -11164,7 +11200,9 @@ void RSurf_ActiveCustomEntity(const matrix4x4_t *matrix, const matrix4x4_t *inve
        rsurface.ent_flags = entflags;
        rsurface.modelnumvertices = numvertices;
        rsurface.modelnumtriangles = numtriangles;
-       R_Mesh_ResizeArrays(max(rsurface.modelnumvertices, rsurface.modelnumtriangles));
+       newvertices = max(rsurface.modelnumvertices, rsurface.modelnumtriangles);
+       if (rsurface.array_size < newvertices)
+               R_Mesh_ResizeArrays(newvertices);
        rsurface.matrix = *matrix;
        rsurface.inversematrix = *inversematrix;
        rsurface.matrixscale = Matrix4x4_ScaleFromMatrix(&rsurface.matrix);
@@ -11293,25 +11331,6 @@ void RSurf_ActiveCustomEntity(const matrix4x4_t *matrix, const matrix4x4_t *inve
                        rsurface.modeltvector3f = rsurface.array_modeltvector3f;
                }
        }
-
-       // now convert arrays into vertexmesh structs
-       for (i = 0;i < numvertices;i++)
-       {
-               VectorCopy(rsurface.modelvertex3f + 3*i, rsurface.array_modelvertexposition[i].vertex3f);
-               VectorCopy(rsurface.modelvertex3f + 3*i, rsurface.array_modelvertexmesh[i].vertex3f);
-               if (rsurface.modelsvector3f)
-                       VectorCopy(rsurface.modelsvector3f + 3*i, rsurface.array_modelvertexmesh[i].svector3f);
-               if (rsurface.modeltvector3f)
-                       VectorCopy(rsurface.modeltvector3f + 3*i, rsurface.array_modelvertexmesh[i].tvector3f);
-               if (rsurface.modelnormal3f)
-                       VectorCopy(rsurface.modelnormal3f + 3*i, rsurface.array_modelvertexmesh[i].normal3f);
-               if (rsurface.modellightmapcolor4f)
-                       Vector4Scale(rsurface.modellightmapcolor4f + 4*i, 255.0f, rsurface.array_modelvertexmesh[i].color4ub);
-               if (rsurface.modeltexcoordtexture2f)
-                       Vector2Copy(rsurface.modeltexcoordtexture2f + 2*i, rsurface.array_modelvertexmesh[i].texcoordtexture2f);
-               if (rsurface.modeltexcoordlightmap2f)
-                       Vector2Copy(rsurface.modeltexcoordlightmap2f + 2*i, rsurface.array_modelvertexmesh[i].texcoordlightmap2f);
-       }
 }
 
 float RSurf_FogPoint(const float *v)
@@ -11846,8 +11865,8 @@ void RSurf_PrepareVerticesForBatch(int batchneed, int texturenumsurfaces, const
                                }
                        }
                        // if we get here, BATCHNEED_ARRAY_NORMAL and BATCHNEED_ARRAY_VECTOR are in batchneed, so no need to check
-                       Mod_BuildNormals(rsurface.batchfirstvertex, rsurface.batchnumvertices, rsurface.batchnumtriangles, rsurface.array_batchvertex3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.array_batchnormal3f, true);
-                       Mod_BuildTextureVectorsFromNormals(rsurface.batchfirstvertex, rsurface.batchnumvertices, rsurface.batchnumtriangles, rsurface.array_batchvertex3f, rsurface.batchtexcoordtexture2f, rsurface.array_batchnormal3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.array_batchsvector3f, rsurface.array_batchtvector3f, true);
+                       Mod_BuildNormals(rsurface.batchfirstvertex, rsurface.batchnumvertices, rsurface.batchnumtriangles, rsurface.array_batchvertex3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.array_batchnormal3f, r_smoothnormals_areaweighting.integer != 0);
+                       Mod_BuildTextureVectorsFromNormals(rsurface.batchfirstvertex, rsurface.batchnumvertices, rsurface.batchnumtriangles, rsurface.array_batchvertex3f, rsurface.batchtexcoordtexture2f, rsurface.array_batchnormal3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.array_batchsvector3f, rsurface.array_batchtvector3f, r_smoothnormals_areaweighting.integer != 0);
                        rsurface.batchvertex3f = rsurface.array_batchvertex3f;
                        rsurface.batchvertex3f_vertexbuffer = NULL;
                        rsurface.batchvertex3f_bufferoffset = 0;
@@ -11951,14 +11970,14 @@ void RSurf_PrepareVerticesForBatch(int batchneed, int texturenumsurfaces, const
                        rsurface.batchvertex3f_bufferoffset = 0;
                        if(batchneed & (BATCHNEED_ARRAY_NORMAL | BATCHNEED_ARRAY_VECTOR)) // otherwise these can stay NULL
                        {
-                               Mod_BuildNormals(rsurface.batchfirstvertex, rsurface.batchnumvertices, rsurface.batchnumtriangles, rsurface.array_batchvertex3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.array_batchnormal3f, true);
+                               Mod_BuildNormals(rsurface.batchfirstvertex, rsurface.batchnumvertices, rsurface.batchnumtriangles, rsurface.array_batchvertex3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.array_batchnormal3f, r_smoothnormals_areaweighting.integer != 0);
                                rsurface.batchnormal3f = rsurface.array_batchnormal3f;
                                rsurface.batchnormal3f_vertexbuffer = NULL;
                                rsurface.batchnormal3f_bufferoffset = 0;
                        }
                        if(batchneed & BATCHNEED_ARRAY_VECTOR) // otherwise these can stay NULL
                        {
-                               Mod_BuildTextureVectorsFromNormals(rsurface.batchfirstvertex, rsurface.batchnumvertices, rsurface.batchnumtriangles, rsurface.array_batchvertex3f, rsurface.batchtexcoordtexture2f, rsurface.array_batchnormal3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.array_batchsvector3f, rsurface.array_batchtvector3f, true);
+                               Mod_BuildTextureVectorsFromNormals(rsurface.batchfirstvertex, rsurface.batchnumvertices, rsurface.batchnumtriangles, rsurface.array_batchvertex3f, rsurface.batchtexcoordtexture2f, rsurface.array_batchnormal3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.array_batchsvector3f, rsurface.array_batchtvector3f, r_smoothnormals_areaweighting.integer != 0);
                                rsurface.batchsvector3f = rsurface.array_batchsvector3f;
                                rsurface.batchsvector3f_vertexbuffer = NULL;
                                rsurface.batchsvector3f_bufferoffset = 0;
@@ -11984,7 +12003,7 @@ void RSurf_PrepareVerticesForBatch(int batchneed, int texturenumsurfaces, const
                        rsurface.batchnormal3f_bufferoffset = 0;
                        if(batchneed & BATCHNEED_ARRAY_VECTOR) // otherwise these can stay NULL
                        {
-                               Mod_BuildTextureVectorsFromNormals(rsurface.batchfirstvertex, rsurface.batchnumvertices, rsurface.batchnumtriangles, rsurface.array_batchvertex3f, rsurface.batchtexcoordtexture2f, rsurface.array_batchnormal3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.array_batchsvector3f, rsurface.array_batchtvector3f, true);
+                               Mod_BuildTextureVectorsFromNormals(rsurface.batchfirstvertex, rsurface.batchnumvertices, rsurface.batchnumtriangles, rsurface.array_batchvertex3f, rsurface.batchtexcoordtexture2f, rsurface.array_batchnormal3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.array_batchsvector3f, rsurface.array_batchtvector3f, r_smoothnormals_areaweighting.integer != 0);
                                rsurface.batchsvector3f = rsurface.array_batchsvector3f;
                                rsurface.batchsvector3f_vertexbuffer = NULL;
                                rsurface.batchsvector3f_bufferoffset = 0;
@@ -12015,7 +12034,7 @@ void RSurf_PrepareVerticesForBatch(int batchneed, int texturenumsurfaces, const
                                VectorMA(rsurface.batchvertex3f + 3*j, scale, rsurface.batchnormal3f + 3*j, rsurface.array_batchvertex3f + 3*j);
                        }
                        // if we get here, BATCHNEED_ARRAY_NORMAL is in batchneed, so no need to check
-                       Mod_BuildNormals(rsurface.batchfirstvertex, rsurface.batchnumvertices, rsurface.batchnumtriangles, rsurface.array_batchvertex3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.array_batchnormal3f, true);
+                       Mod_BuildNormals(rsurface.batchfirstvertex, rsurface.batchnumvertices, rsurface.batchnumtriangles, rsurface.array_batchvertex3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.array_batchnormal3f, r_smoothnormals_areaweighting.integer != 0);
                        rsurface.batchvertex3f = rsurface.array_batchvertex3f;
                        rsurface.batchvertex3f_vertexbuffer = NULL;
                        rsurface.batchvertex3f_bufferoffset = 0;
@@ -12024,7 +12043,7 @@ void RSurf_PrepareVerticesForBatch(int batchneed, int texturenumsurfaces, const
                        rsurface.batchnormal3f_bufferoffset = 0;
                        if(batchneed & BATCHNEED_ARRAY_VECTOR) // otherwise these can stay NULL
                        {
-                               Mod_BuildTextureVectorsFromNormals(rsurface.batchfirstvertex, rsurface.batchnumvertices, rsurface.batchnumtriangles, rsurface.array_batchvertex3f, rsurface.batchtexcoordtexture2f, rsurface.array_batchnormal3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.array_batchsvector3f, rsurface.array_batchtvector3f, true);
+                               Mod_BuildTextureVectorsFromNormals(rsurface.batchfirstvertex, rsurface.batchnumvertices, rsurface.batchnumtriangles, rsurface.array_batchvertex3f, rsurface.batchtexcoordtexture2f, rsurface.array_batchnormal3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.array_batchsvector3f, rsurface.array_batchtvector3f, r_smoothnormals_areaweighting.integer != 0);
                                rsurface.batchsvector3f = rsurface.array_batchsvector3f;
                                rsurface.batchsvector3f_vertexbuffer = NULL;
                                rsurface.batchsvector3f_bufferoffset = 0;
@@ -12041,7 +12060,7 @@ void RSurf_PrepareVerticesForBatch(int batchneed, int texturenumsurfaces, const
                                VectorMA(rsurface.batchvertex3f + 3*j, scale, rsurface.batchnormal3f + 3*j, rsurface.array_batchvertex3f + 3*j);
                        }
                        // if we get here, BATCHNEED_ARRAY_NORMAL is in batchneed, so no need to check
-                       Mod_BuildNormals(rsurface.batchfirstvertex, rsurface.batchnumvertices, rsurface.batchnumtriangles, rsurface.array_batchvertex3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.array_batchnormal3f, true);
+                       Mod_BuildNormals(rsurface.batchfirstvertex, rsurface.batchnumvertices, rsurface.batchnumtriangles, rsurface.array_batchvertex3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.array_batchnormal3f, r_smoothnormals_areaweighting.integer != 0);
                        rsurface.batchvertex3f = rsurface.array_batchvertex3f;
                        rsurface.batchvertex3f_vertexbuffer = NULL;
                        rsurface.batchvertex3f_bufferoffset = 0;
@@ -12050,7 +12069,7 @@ void RSurf_PrepareVerticesForBatch(int batchneed, int texturenumsurfaces, const
                        rsurface.batchnormal3f_bufferoffset = 0;
                        if(batchneed & BATCHNEED_ARRAY_VECTOR) // otherwise these can stay NULL
                        {
-                               Mod_BuildTextureVectorsFromNormals(rsurface.batchfirstvertex, rsurface.batchnumvertices, rsurface.batchnumtriangles, rsurface.array_batchvertex3f, rsurface.batchtexcoordtexture2f, rsurface.array_batchnormal3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.array_batchsvector3f, rsurface.array_batchtvector3f, true);
+                               Mod_BuildTextureVectorsFromNormals(rsurface.batchfirstvertex, rsurface.batchnumvertices, rsurface.batchnumtriangles, rsurface.array_batchvertex3f, rsurface.batchtexcoordtexture2f, rsurface.array_batchnormal3f, rsurface.batchelement3i + 3 * rsurface.batchfirsttriangle, rsurface.array_batchsvector3f, rsurface.array_batchtvector3f, r_smoothnormals_areaweighting.integer != 0);
                                rsurface.batchsvector3f = rsurface.array_batchsvector3f;
                                rsurface.batchsvector3f_vertexbuffer = NULL;
                                rsurface.batchsvector3f_bufferoffset = 0;
@@ -12234,13 +12253,20 @@ static int RSurf_FindWaterPlaneForSurface(const msurface_t *surface)
        vec3_t vert;
        const float *v;
        r_waterstate_waterplane_t *p;
+       qboolean prepared = false;
        bestd = 0;
        for (planeindex = 0, p = r_waterstate.waterplanes;planeindex < r_waterstate.numwaterplanes;planeindex++, p++)
        {
                if(p->camera_entity != rsurface.texture->camera_entity)
                        continue;
                d = 0;
-               RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX, 1, &surface);
+               if(!prepared)
+               {
+                       RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX, 1, &surface);
+                       prepared = true;
+                       if(rsurface.batchnumvertices == 0)
+                               break;
+               }
                for (vertexindex = 0, v = rsurface.batchvertex3f + rsurface.batchfirstvertex * 3;vertexindex < rsurface.batchnumvertices;vertexindex++, v += 3)
                {
                        Matrix4x4_Transform(&rsurface.matrix, v, vert);
@@ -12253,6 +12279,10 @@ static int RSurf_FindWaterPlaneForSurface(const msurface_t *surface)
                }
        }
        return bestplaneindex;
+       // NOTE: this MAY return a totally unrelated water plane; we can ignore
+       // this situation though, as it might be better to render single larger
+       // batches with useless stuff (backface culled for example) than to
+       // render multiple smaller batches
 }
 
 static void RSurf_DrawBatch_GL11_MakeFullbrightLightmapColorArray(void)
@@ -12605,16 +12635,23 @@ static void R_DrawTextureSurfaceList_GL20(int texturenumsurfaces, const msurface
        // bind lightmap texture
 
        // water/refraction/reflection/camera surfaces have to be handled specially
-       if ((rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_CAMERA | MATERIALFLAG_REFLECTION)) && !r_waterstate.renderingscene)
+       if ((rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_CAMERA | MATERIALFLAG_REFLECTION)))
        {
                int start, end, startplaneindex;
                for (start = 0;start < texturenumsurfaces;start = end)
                {
                        startplaneindex = RSurf_FindWaterPlaneForSurface(texturesurfacelist[start]);
+                       if(startplaneindex < 0)
+                       {
+                               // this happens if the plane e.g. got backface culled and thus didn't get a water plane. We can just ignore this.
+                               // Con_Printf("No matching water plane for surface with material flags 0x%08x - PLEASE DEBUG THIS\n", rsurface.texture->currentmaterialflags);
+                               end = start + 1;
+                               continue;
+                       }
                        for (end = start + 1;end < texturenumsurfaces && startplaneindex == RSurf_FindWaterPlaneForSurface(texturesurfacelist[end]);end++)
                                ;
                        // now that we have a batch using the same planeindex, render it
-                       if ((rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_CAMERA)) && !r_waterstate.renderingscene)
+                       if ((rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_CAMERA)))
                        {
                                // render water or distortion background
                                GL_DepthMask(true);
@@ -12625,7 +12662,7 @@ static void R_DrawTextureSurfaceList_GL20(int texturenumsurfaces, const msurface
                                R_SetupShader_Surface(vec3_origin, (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT) != 0, 1, 1, rsurface.texture->specularscale, RSURFPASS_BASE, end-start, texturesurfacelist + start, NULL);
                                RSurf_DrawBatch();
                        }
-                       else if ((rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION) && !r_waterstate.renderingscene)
+                       else if ((rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION))
                        {
                                // render surface with reflection texture as input
                                GL_DepthMask(writedepth && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_BLENDED));
@@ -14037,9 +14074,9 @@ static void R_DrawModelDecals_Entity(entity_render_t *ent)
                if (decal->triangleindex >= 0 && decal->triangleindex < rsurface.modelnumtriangles)
                {
                        e = rsurface.modelelement3i + 3*decal->triangleindex;
-                       VectorCopy(rsurface.modelvertexposition[e[0]].vertex3f, v3f);
-                       VectorCopy(rsurface.modelvertexposition[e[1]].vertex3f, v3f + 3);
-                       VectorCopy(rsurface.modelvertexposition[e[2]].vertex3f, v3f + 6);
+                       VectorCopy(rsurface.modelvertex3f + 3*e[0], v3f);
+                       VectorCopy(rsurface.modelvertex3f + 3*e[1], v3f + 3);
+                       VectorCopy(rsurface.modelvertex3f + 3*e[2], v3f + 6);
                }
                else
                {