]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_rmain.c
added cvar r_fixtrans_auto to automatically call fixtrans on all textures that are...
[xonotic/darkplaces.git] / gl_rmain.c
index 3bc4326a33af3e14500b30836ef179335108146c..86d9833a8eff4cbddf335ad748541684e2c11e25 100644 (file)
@@ -112,6 +112,10 @@ cvar_t gl_lightmaps = {0, "gl_lightmaps", "0", "draws only lightmaps, no texture
 
 cvar_t r_test = {0, "r_test", "0", "internal development use only, leave it alone (usually does nothing anyway)"};
 cvar_t r_batchmode = {0, "r_batchmode", "1", "selects method of rendering multiple surfaces with one driver call (values are 0, 1, 2, etc...)"};
+cvar_t r_track_sprites = {CVAR_SAVE, "r_track_sprites", "1", "track SPR_LABEL* sprites by putting them as indicator at the screen border to rotate to"};
+cvar_t r_track_sprites_flags = {CVAR_SAVE, "r_track_sprites_flags", "1", "1: Rotate sprites accodringly, 2: Make it a continuous rotation"};
+cvar_t r_track_sprites_scalew = {CVAR_SAVE, "r_track_sprites_scalew", "1", "width scaling of tracked sprites"};
+cvar_t r_track_sprites_scaleh = {CVAR_SAVE, "r_track_sprites_scaleh", "1", "height scaling of tracked sprites"};
 
 extern qboolean v_flipped_state;
 
@@ -444,10 +448,10 @@ static const char *builtinshaderstring =
 "varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal)\n"
 "varying vec3 VectorR; // direction of R texcoord (surface normal)\n"
 "\n"
-"//#ifdef USEWATER\n"
+"//#ifdef MODE_WATER\n"
 "varying vec4 ModelViewProjectionPosition;\n"
 "//#else\n"
-"//# ifdef USEREFRACTION\n"
+"//# ifdef MODE_REFRACTION\n"
 "//varying vec4 ModelViewProjectionPosition;\n"
 "//# else\n"
 "//#  ifdef USEREFLECTION\n"
@@ -515,7 +519,7 @@ static const char *builtinshaderstring =
 "      VectorR = gl_MultiTexCoord3.xyz;\n"
 "#endif\n"
 "\n"
-"//#if defined(USEWATER) || defined(USEREFRACTION) || defined(USEREFLECTION)\n"
+"//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION)\n"
 "//    ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix;\n"
 "//    //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition;\n"
 "//    //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition;\n"
@@ -531,12 +535,6 @@ static const char *builtinshaderstring =
 "#ifdef MODE_REFRACTION\n"
 "      ModelViewProjectionPosition = gl_Position;\n"
 "#endif\n"
-"#ifdef USEWATER\n"
-"      ModelViewProjectionPosition = gl_Position;\n"
-"#endif\n"
-"#ifdef USEREFRACTION\n"
-"      ModelViewProjectionPosition = gl_Position;\n"
-"#endif\n"
 "#ifdef USEREFLECTION\n"
 "      ModelViewProjectionPosition = gl_Position;\n"
 "#endif\n"
@@ -573,7 +571,7 @@ static const char *builtinshaderstring =
 "uniform myhvec3 Color_Shirt;\n"
 "uniform myhvec3 FogColor;\n"
 "\n"
-"//#ifdef USEWATER\n"
+"//#ifdef MODE_WATER\n"
 "uniform vec4 DistortScaleRefractReflect;\n"
 "uniform vec4 ScreenScaleRefractReflect;\n"
 "uniform vec4 ScreenCenterRefractReflect;\n"
@@ -582,11 +580,14 @@ static const char *builtinshaderstring =
 "uniform myhalf ReflectFactor;\n"
 "uniform myhalf ReflectOffset;\n"
 "//#else\n"
-"//# ifdef USEREFRACTION\n"
+"//# ifdef MODE_REFRACTION\n"
 "//uniform vec4 DistortScaleRefractReflect;\n"
 "//uniform vec4 ScreenScaleRefractReflect;\n"
 "//uniform vec4 ScreenCenterRefractReflect;\n"
 "//uniform myhvec4 RefractColor;\n"
+"//#  ifdef USEREFLECTION\n"
+"//uniform myhvec4 ReflectColor;\n"
+"//#  endif\n"
 "//# else\n"
 "//#  ifdef USEREFLECTION\n"
 "//uniform vec4 DistortScaleRefractReflect;\n"
@@ -827,27 +828,13 @@ static const char *builtinshaderstring =
 "      color.rgb += myhvec3(texture2D(Texture_Glow, TexCoord)) * GlowScale;\n"
 "#endif\n"
 "\n"
-"#ifdef MODE_LIGHTSOURCE\n"
-"# ifdef USEWATER\n"
-"      color.rgb *= color.a;\n"
-"# endif\n"
-"# ifdef USEREFRACTION\n"
-"      color.rgb *= color.a;\n"
-"# endif\n"
-"#else\n"
-"# ifdef USEWATER\n"
-"      vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);\n"
-"      //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
-"      vec4 ScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect + vec3(normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5))).xyxy * DistortScaleRefractReflect;\n"
-"      myhalf Fresnel = myhalf(pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 5.0)) * ReflectFactor + ReflectOffset;\n"
-"      color.rgb = mix(mix(myhvec3(texture2D(Texture_Refraction, ScreenTexCoord.xy)) * RefractColor.rgb, myhvec3(texture2D(Texture_Reflection, ScreenTexCoord.zw)) * ReflectColor.rgb, Fresnel), color.rgb, color.a);\n"
-"# endif\n"
-"# ifdef USEREFRACTION\n"
-"      vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);\n"
-"      //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
-"      vec4 ScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect + vec3(normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5))).xyxy * DistortScaleRefractReflect;\n"
-"      color.rgb = mix(myhvec3(texture2D(Texture_Refraction, ScreenTexCoord.xy)) * RefractColor.rgb, color.rgb, color.a);\n"
-"# endif\n"
+"#ifdef USECONTRASTBOOST\n"
+"      color.rgb = color.rgb / (ContrastBoostCoeff * color.rgb + myhvec3(1, 1, 1));\n"
+"#endif\n"
+"\n"
+"      color.rgb *= SceneBrightness;\n"
+"\n"
+"#ifndef MODE_LIGHTSOURCE\n"
 "# ifdef USEREFLECTION\n"
 "      vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);\n"
 "      //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
@@ -861,12 +848,6 @@ static const char *builtinshaderstring =
 "      color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhvec2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0))));\n"
 "#endif\n"
 "\n"
-"#ifdef USECONTRASTBOOST\n"
-"      color.rgb = color.rgb / (ContrastBoostCoeff * color.rgb + myhvec3(1, 1, 1));\n"
-"#endif\n"
-"\n"
-"      color.rgb *= SceneBrightness;\n"
-"\n"
 "      gl_FragColor = vec4(color);\n"
 "}\n"
 "#endif // MODE_REFRACTION\n"
@@ -885,9 +866,7 @@ static const char *builtinshaderstring =
 #define SHADERPERMUTATION_REFLECTION (1<<7) // normalmap-perturbed reflection of the scene infront of the surface, preformed as an overlay on the surface
 #define SHADERPERMUTATION_OFFSETMAPPING (1<<8) // adjust texcoords to roughly simulate a displacement mapped surface
 #define SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING (1<<9) // adjust texcoords to accurately simulate a displacement mapped surface (requires OFFSETMAPPING to also be set!)
-#define SHADERPERMUTATION_WATER (1<<10) // normalmap-perturbed refraction of the background, performed behind the surface (the texture or material must be transparent to see it)
-#define SHADERPERMUTATION_REFRACTION (1<<11) // normalmap-perturbed reflection of the scene infront of the surface, preformed as an overlay on the surface
-#define SHADERPERMUTATION_MODEBASE (1<<12) // multiplier for the SHADERMODE_ values to get a valid index
+#define SHADERPERMUTATION_MODEBASE (1<<10) // multiplier for the SHADERMODE_ values to get a valid index
 
 // NOTE: MUST MATCH ORDER OF SHADERPERMUTATION_* DEFINES!
 const char *shaderpermutationinfo[][2] =
@@ -902,8 +881,6 @@ const char *shaderpermutationinfo[][2] =
        {"#define USEREFLECTION\n", " reflection"},
        {"#define USEOFFSETMAPPING\n", " offsetmapping"},
        {"#define USEOFFSETMAPPING_RELIEFMAPPING\n", " reliefmapping"},
-       {"#define USEWATER\n", " water"},
-       {"#define USEREFRACTION\n", " refraction"},
        {NULL, NULL}
 };
 
@@ -1223,10 +1200,6 @@ int R_SetupSurfaceShader(const vec3_t lightcolorbase, qboolean modellighting, fl
                        permutation |= SHADERPERMUTATION_COLORMAPPING;
                if(r_glsl_contrastboost.value > 1 || r_glsl_contrastboost.value < 0)
                        permutation |= SHADERPERMUTATION_CONTRASTBOOST;
-               //if (rsurface.texture->currentmaterialflags & MATERIALFLAG_WATERSHADER)
-               //      permutation |= SHADERPERMUTATION_WATER;
-               //if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFRACTION)
-               //      permutation |= SHADERPERMUTATION_REFRACTION;
                if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
                        permutation |= SHADERPERMUTATION_REFLECTION;
        }
@@ -1248,10 +1221,6 @@ int R_SetupSurfaceShader(const vec3_t lightcolorbase, qboolean modellighting, fl
                }
                if(r_glsl_contrastboost.value > 1 || r_glsl_contrastboost.value < 0)
                        permutation |= SHADERPERMUTATION_CONTRASTBOOST;
-               //if (rsurface.texture->currentmaterialflags & MATERIALFLAG_WATERSHADER)
-               //      permutation |= SHADERPERMUTATION_WATER;
-               //if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFRACTION)
-               //      permutation |= SHADERPERMUTATION_REFRACTION;
                if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
                        permutation |= SHADERPERMUTATION_REFLECTION;
        }
@@ -1269,10 +1238,6 @@ int R_SetupSurfaceShader(const vec3_t lightcolorbase, qboolean modellighting, fl
                        permutation |= SHADERPERMUTATION_COLORMAPPING;
                if(r_glsl_contrastboost.value > 1 || r_glsl_contrastboost.value < 0)
                        permutation |= SHADERPERMUTATION_CONTRASTBOOST;
-               //if (rsurface.texture->currentmaterialflags & MATERIALFLAG_WATERSHADER)
-               //      permutation |= SHADERPERMUTATION_WATER;
-               //if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFRACTION)
-               //      permutation |= SHADERPERMUTATION_REFRACTION;
                if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
                        permutation |= SHADERPERMUTATION_REFLECTION;
        }
@@ -1309,10 +1274,6 @@ int R_SetupSurfaceShader(const vec3_t lightcolorbase, qboolean modellighting, fl
                        permutation |= SHADERPERMUTATION_COLORMAPPING;
                if(r_glsl_contrastboost.value > 1 || r_glsl_contrastboost.value < 0)
                        permutation |= SHADERPERMUTATION_CONTRASTBOOST;
-               //if (rsurface.texture->currentmaterialflags & MATERIALFLAG_WATERSHADER)
-               //      permutation |= SHADERPERMUTATION_WATER;
-               //if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFRACTION)
-               //      permutation |= SHADERPERMUTATION_REFRACTION;
                if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
                        permutation |= SHADERPERMUTATION_REFLECTION;
        }
@@ -1393,7 +1354,7 @@ int R_SetupSurfaceShader(const vec3_t lightcolorbase, qboolean modellighting, fl
                //   color.rgb *= [[SceneBrightness * ContrastBoost]];
                //   color.rgb /= [[(ContrastBoost - 1) / ContrastBoost]] * color.rgb + 1;
                // and Black:
-               //   color.rgb = [[SceneBrightness * ContrastBoost]] / ([[(ContrastBoost - 1) * SceneBrightness]] + 1 / color.rgb);
+               //   color.rgb = [[SceneBrightness * ContrastBoost]] * color.rgb / ([[(ContrastBoost - 1) * SceneBrightness]] * color.rgb + 1);
                // and do [[calculations]] here in the engine
                qglUniform1fARB(r_glsl_permutation->loc_ContrastBoostCoeff, (r_glsl_contrastboost.value - 1) * r_view.colorscale);
                if (r_glsl_permutation->loc_SceneBrightness >= 0) qglUniform1fARB(r_glsl_permutation->loc_SceneBrightness, r_view.colorscale * r_glsl_contrastboost.value);
@@ -1406,6 +1367,21 @@ int R_SetupSurfaceShader(const vec3_t lightcolorbase, qboolean modellighting, fl
                if (rsurface.rtlight || (rsurface.texture->currentmaterialflags & MATERIALFLAG_ADD))
                        qglUniform3fARB(r_glsl_permutation->loc_FogColor, 0, 0, 0);
                else
+               /*
+               {
+                       vec3_t fogvec;
+                       //   color.rgb *= SceneBrightness;
+                       VectorScale(r_refdef.fogcolor, r_view.colorscale, fogvec);
+                       if(r_glsl_permutation->loc_ContrastBoostCoeff >= 0) // need to support contrast boost
+                       {
+                               //   color.rgb *= ContrastBoost / ((ContrastBoost - 1) * color.rgb + 1);
+                               fogvec[0] *= r_glsl_contrastboost.value / ((r_glsl_contrastboost.value - 1) * fogvec[0] + 1);
+                               fogvec[1] *= r_glsl_contrastboost.value / ((r_glsl_contrastboost.value - 1) * fogvec[1] + 1);
+                               fogvec[2] *= r_glsl_contrastboost.value / ((r_glsl_contrastboost.value - 1) * fogvec[2] + 1);
+                       }
+                       qglUniform3fARB(r_glsl_permutation->loc_FogColor, fogvec[0], fogvec[1], fogvec[2]);
+               }
+               */
                        qglUniform3fARB(r_glsl_permutation->loc_FogColor, r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2]);
        }
        if (r_glsl_permutation->loc_EyePosition >= 0) qglUniform3fARB(r_glsl_permutation->loc_EyePosition, rsurface.modelorg[0], rsurface.modelorg[1], rsurface.modelorg[2]);
@@ -1548,7 +1524,7 @@ skinframe_t *R_SkinFrame_LoadExternal(const char *name, int textureflags, qboole
        if (skinframe && skinframe->base)
                return skinframe;
 
-       basepixels = loadimagepixels(name, complain, 0, 0);
+       basepixels = loadimagepixels(name, complain, 0, 0, true);
        if (basepixels == NULL)
                return NULL;
 
@@ -1593,13 +1569,13 @@ skinframe_t *R_SkinFrame_LoadExternal(const char *name, int textureflags, qboole
        // _norm is the name used by tenebrae and has been adopted as standard
        if (loadnormalmap)
        {
-               if ((pixels = loadimagepixels(va("%s_norm", skinframe->basename), false, 0, 0)) != NULL)
+               if ((pixels = loadimagepixels(va("%s_norm", skinframe->basename), false, 0, 0, false)) != NULL)
                {
                        skinframe->nmap = R_LoadTexture2D (r_main_texturepool, va("%s_nmap", skinframe->basename), image_width, image_height, pixels, TEXTYPE_RGBA, skinframe->textureflags & (gl_texturecompression_normal.integer ? ~0 : ~TEXF_COMPRESS), NULL);
                        Mem_Free(pixels);
                        pixels = NULL;
                }
-               else if (r_shadow_bumpscale_bumpmap.value > 0 && (bumppixels = loadimagepixels(va("%s_bump", skinframe->basename), false, 0, 0)) != NULL)
+               else if (r_shadow_bumpscale_bumpmap.value > 0 && (bumppixels = loadimagepixels(va("%s_bump", skinframe->basename), false, 0, 0, false)) != NULL)
                {
                        pixels = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
                        Image_HeightmapToNormalmap(bumppixels, pixels, image_width, image_height, false, r_shadow_bumpscale_bumpmap.value);
@@ -1618,10 +1594,10 @@ skinframe_t *R_SkinFrame_LoadExternal(const char *name, int textureflags, qboole
        // _luma is supported for tenebrae compatibility
        // (I think it's a very stupid name, but oh well)
        // _glow is the preferred name
-       if (loadglow          && ((pixels = loadimagepixels(va("%s_glow", skinframe->basename), false, 0, 0)) != NULL || (pixels = loadimagepixels(va("%s_luma", skinframe->basename), false, 0, 0)) != NULL)) {skinframe->glow = R_LoadTexture2D (r_main_texturepool, va("%s_glow", skinframe->basename), image_width, image_height, pixels, TEXTYPE_RGBA, skinframe->textureflags & (gl_texturecompression_glow.integer ? ~0 : ~TEXF_COMPRESS), NULL);Mem_Free(pixels);pixels = NULL;}
-       if (loadgloss         && (pixels = loadimagepixels(va("%s_gloss", skinframe->basename), false, 0, 0)) != NULL) {skinframe->gloss = R_LoadTexture2D (r_main_texturepool, va("%s_gloss", skinframe->basename), image_width, image_height, pixels, TEXTYPE_RGBA, skinframe->textureflags & (gl_texturecompression_gloss.integer ? ~0 : ~TEXF_COMPRESS), NULL);Mem_Free(pixels);pixels = NULL;}
-       if (loadpantsandshirt && (pixels = loadimagepixels(va("%s_pants", skinframe->basename), false, 0, 0)) != NULL) {skinframe->pants = R_LoadTexture2D (r_main_texturepool, va("%s_pants", skinframe->basename), image_width, image_height, pixels, TEXTYPE_RGBA, skinframe->textureflags & (gl_texturecompression_color.integer ? ~0 : ~TEXF_COMPRESS), NULL);Mem_Free(pixels);pixels = NULL;}
-       if (loadpantsandshirt && (pixels = loadimagepixels(va("%s_shirt", skinframe->basename), false, 0, 0)) != NULL) {skinframe->shirt = R_LoadTexture2D (r_main_texturepool, va("%s_shirt", skinframe->basename), image_width, image_height, pixels, TEXTYPE_RGBA, skinframe->textureflags & (gl_texturecompression_color.integer ? ~0 : ~TEXF_COMPRESS), NULL);Mem_Free(pixels);pixels = NULL;}
+       if (loadglow          && ((pixels = loadimagepixels(va("%s_glow", skinframe->basename), false, 0, 0, false)) != NULL || (pixels = loadimagepixels(va("%s_luma", skinframe->basename), false, 0, 0, false)) != NULL)) {skinframe->glow = R_LoadTexture2D (r_main_texturepool, va("%s_glow", skinframe->basename), image_width, image_height, pixels, TEXTYPE_RGBA, skinframe->textureflags & (gl_texturecompression_glow.integer ? ~0 : ~TEXF_COMPRESS), NULL);Mem_Free(pixels);pixels = NULL;}
+       if (loadgloss         && (pixels = loadimagepixels(va("%s_gloss", skinframe->basename), false, 0, 0, false)) != NULL) {skinframe->gloss = R_LoadTexture2D (r_main_texturepool, va("%s_gloss", skinframe->basename), image_width, image_height, pixels, TEXTYPE_RGBA, skinframe->textureflags & (gl_texturecompression_gloss.integer ? ~0 : ~TEXF_COMPRESS), NULL);Mem_Free(pixels);pixels = NULL;}
+       if (loadpantsandshirt && (pixels = loadimagepixels(va("%s_pants", skinframe->basename), false, 0, 0, false)) != NULL) {skinframe->pants = R_LoadTexture2D (r_main_texturepool, va("%s_pants", skinframe->basename), image_width, image_height, pixels, TEXTYPE_RGBA, skinframe->textureflags & (gl_texturecompression_color.integer ? ~0 : ~TEXF_COMPRESS), NULL);Mem_Free(pixels);pixels = NULL;}
+       if (loadpantsandshirt && (pixels = loadimagepixels(va("%s_shirt", skinframe->basename), false, 0, 0, false)) != NULL) {skinframe->shirt = R_LoadTexture2D (r_main_texturepool, va("%s_shirt", skinframe->basename), image_width, image_height, pixels, TEXTYPE_RGBA, skinframe->textureflags & (gl_texturecompression_color.integer ? ~0 : ~TEXF_COMPRESS), NULL);Mem_Free(pixels);pixels = NULL;}
 
        if (basepixels)
                Mem_Free(basepixels);
@@ -1929,6 +1905,11 @@ void GL_Main_Init(void)
        if (gamemode == GAME_NEHAHRA || gamemode == GAME_TENEBRAE)
                Cvar_SetValue("r_fullbrights", 0);
        R_RegisterModule("GL_Main", gl_main_start, gl_main_shutdown, gl_main_newmap);
+
+       Cvar_RegisterVariable(&r_track_sprites);
+       Cvar_RegisterVariable(&r_track_sprites_flags);
+       Cvar_RegisterVariable(&r_track_sprites_scalew);
+       Cvar_RegisterVariable(&r_track_sprites_scaleh);
 }
 
 extern void R_Textures_Init(void);
@@ -2132,7 +2113,8 @@ static void R_View_UpdateEntityVisible (void)
                for (i = 0;i < r_refdef.numentities;i++)
                {
                        ent = r_refdef.entities[i];
-                       r_viewcache.entityvisible[i] = !(ent->flags & renderimask) && !R_CullBox(ent->mins, ent->maxs) && ((ent->effects & EF_NODEPTHTEST) || (ent->flags & RENDER_VIEWMODEL) || r_refdef.worldmodel->brush.BoxTouchingVisibleLeafs(r_refdef.worldmodel, r_viewcache.world_leafvisible, ent->mins, ent->maxs));
+                       r_viewcache.entityvisible[i] = !(ent->flags & renderimask) && ((ent->model->type == mod_sprite && (ent->model->sprite.sprnum_type == SPR_LABEL || ent->model->sprite.sprnum_type == SPR_LABEL_SCALE)) || !R_CullBox(ent->mins, ent->maxs)) && ((ent->effects & EF_NODEPTHTEST) || (ent->flags & RENDER_VIEWMODEL) || r_refdef.worldmodel->brush.BoxTouchingVisibleLeafs(r_refdef.worldmodel, r_viewcache.world_leafvisible, ent->mins, ent->maxs));
+
                }
                if(r_cullentities_trace.integer)
                {
@@ -2155,7 +2137,7 @@ static void R_View_UpdateEntityVisible (void)
                for (i = 0;i < r_refdef.numentities;i++)
                {
                        ent = r_refdef.entities[i];
-                       r_viewcache.entityvisible[i] = !(ent->flags & renderimask) && !R_CullBox(ent->mins, ent->maxs);
+                       r_viewcache.entityvisible[i] = !(ent->flags & renderimask) && ((ent->model->type == mod_sprite && (ent->model->sprite.sprnum_type == SPR_LABEL || ent->model->sprite.sprnum_type == SPR_LABEL_SCALE)) || !R_CullBox(ent->mins, ent->maxs));
                }
        }
 
@@ -2337,8 +2319,6 @@ static void R_View_SetFrustum(void)
        r_view.frustum[5].dist = m[15] + m[14];
 #endif
 
-
-
        if (r_view.useperspective)
        {
                slopex = 1.0 / r_view.frustum_x;
@@ -2348,6 +2328,14 @@ static void R_View_SetFrustum(void)
                VectorMA(r_view.forward, -slopey, r_view.up  , r_view.frustum[2].normal);
                VectorMA(r_view.forward,  slopey, r_view.up  , r_view.frustum[3].normal);
                VectorCopy(r_view.forward, r_view.frustum[4].normal);
+               
+               // Leaving those out was a mistake, those were in the old code, and they
+               // fix a reproducable bug in this one: frustum culling got fucked up when viewmatrix was an identity matrix
+               // I couldn't reproduce it after adding those normalizations. --blub
+               VectorNormalize(r_view.frustum[0].normal);
+               VectorNormalize(r_view.frustum[1].normal);
+               VectorNormalize(r_view.frustum[2].normal);
+               VectorNormalize(r_view.frustum[3].normal);
 
                // calculate frustum corners, which are used to calculate deformed frustum planes for shadow caster culling
                VectorMAMAMAM(1, r_view.origin, 1024, r_view.forward, -1024 * slopex, r_view.left, -1024 * slopey, r_view.up, r_view.frustumcorner[0]);
@@ -2586,7 +2574,7 @@ static void R_Water_StartFrame(void)
 
        // calculate desired texture sizes
        // can't use water if the card does not support the texture size
-       if (!r_water.integer || waterwidth > gl_max_texture_size || waterheight > gl_max_texture_size)
+       if (!r_water.integer || !r_glsl.integer || !gl_support_fragment_shader || waterwidth > gl_max_texture_size || waterheight > gl_max_texture_size)
                texturewidth = textureheight = waterwidth = waterheight = 0;
        else if (gl_support_arb_texture_non_power_of_two)
        {
@@ -2775,6 +2763,8 @@ static void R_Water_ProcessPlanes(void)
 
                        R_ResetViewRendering3D();
                        R_ClearScreen();
+                       if (r_timereport_active)
+                               R_TimeReport("viewclear");
 
                        R_RenderScene(false);
 
@@ -2785,9 +2775,12 @@ static void R_Water_ProcessPlanes(void)
 
                        R_ResetViewRendering3D();
                        R_ClearScreen();
+                       if (r_timereport_active)
+                               R_TimeReport("viewclear");
                }
 
                r_view = originalview;
+               r_view.clear = true;
                r_waterstate.renderingscene = false;
        }
        return;
@@ -3079,8 +3072,7 @@ void R_HDR_RenderBloomTexture(void)
 
        R_ClearScreen();
        if (r_timereport_active)
-               R_TimeReport("clear");
-
+               R_TimeReport("viewclear");
 
        // restore the view settings
        r_view.width = oldwidth;
@@ -3242,13 +3234,17 @@ void R_RenderView(void)
 
        CHECKGLERROR
        if (r_timereport_active)
-               R_TimeReport("setup");
+               R_TimeReport("viewsetup");
 
        R_ResetViewRendering3D();
 
-       R_ClearScreen();
-       if (r_timereport_active)
-               R_TimeReport("clear");
+       if (r_view.clear)
+       {
+               R_ClearScreen();
+               if (r_timereport_active)
+                       R_TimeReport("viewclear");
+       }
+       r_view.clear = true;
 
        r_view.showdebug = true;
 
@@ -3283,7 +3279,7 @@ void R_RenderScene(qboolean addwaterplanes)
 
                R_View_Update();
                if (r_timereport_active)
-                       R_TimeReport("watervisibility");
+                       R_TimeReport("watervis");
 
                if (cl.csqc_vidvars.drawworld && r_refdef.worldmodel && r_refdef.worldmodel->DrawAddWaterPlanes)
                {
@@ -5299,105 +5295,6 @@ static void RSurf_DrawBatch_GL11_VertexShade(int texturenumsurfaces, msurface_t
        RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
 }
 
-#if 0
-static void RSurf_GL11_WaterTexCoords(int texturenumsurfaces, msurface_t **texturesurfacelist)
-{
-       int texturesurfaceindex;
-       const msurface_t *surface;
-       // compute texcoords for vertices (yes, these swim around the screen a bit unintentionally)
-       // compute vertex alpha to fade between reflection and refraction if this is water
-       for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
-       {
-               surface = texturesurfacelist[texturesurfaceindex];
-               GL_TransformToScreenTexCoords(surface->num_vertices, rsurface.vertex3f + 3 * surface->num_firstvertex, rsurface.array_generatedtexcoordtexture2f + 2 * surface->num_firstvertex, r_waterstate.screenscale[0], r_waterstate.screenscale[1], r_waterstate.screencenter[0], r_waterstate.screencenter[1]);
-       }
-}
-
-static void RSurf_GL11_WaterFresnelAlpha(int texturenumsurfaces, msurface_t **texturesurfacelist)
-{
-       int texturesurfaceindex;
-       int i;
-       const msurface_t *surface;
-       int numverts;
-       const float *v;
-       const float *n;
-       float *c2;
-       vec3_t dir;
-       float Fresnel;
-       // compute texcoords for vertices (yes, these swim around the screen a bit unintentionally)
-       // compute vertex alpha to fade between reflection and refraction if this is water
-       for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
-       {
-               surface = texturesurfacelist[texturesurfaceindex];
-               numverts = surface->num_vertices;
-               v = rsurface.vertex3f + 3 * surface->num_firstvertex;
-               n = rsurface.normal3f + 3 * surface->num_firstvertex;
-               c2 = rsurface.array_color4f + 4 * surface->num_firstvertex;
-               for (i = 0;i < numverts;i++, v += 3, n += 3, c2 += 4)
-               {
-                       VectorSubtract(rsurface.modelorg, v, dir);
-                       VectorNormalize(dir);
-                       Fresnel = pow(min(1.0f, 1.0f - DotProduct(dir, n)), 2.0f) * (rsurface.texture->reflectmax - rsurface.texture->reflectmin) + rsurface.texture->reflectmin;
-                       Vector4Set(c2, 1, 1, 1, Fresnel);
-               }
-       }
-}
-
-static void R_DrawTextureSurfaceList_GL11_Water(int texturenumsurfaces, msurface_t **texturesurfacelist)
-{
-       // OpenGL 1.1 path - crusty old voodoo path
-       rmeshstate_t m;
-       qboolean water;
-       if (rsurface.mode != RSURFMODE_MULTIPASS)
-               rsurface.mode = RSURFMODE_MULTIPASS;
-       RSurf_PrepareVerticesForBatch(true, false, texturenumsurfaces, texturesurfacelist);
-       R_Mesh_ColorPointer(NULL, 0, 0);
-       water = (rsurface.texture->currentmaterialflags & MATERIALFLAG_WATERSHADER) != 0;
-       RSurf_GL11_WaterTexCoords(texturenumsurfaces, texturesurfacelist);
-       if (water)
-               RSurf_GL11_WaterFresnelAlpha(texturenumsurfaces, texturesurfacelist);
-       // now draw the surfaces as refraction texture
-       memset(&m, 0, sizeof(m));
-       m.tex[0] = R_GetTexture(r_texture_white);
-       m.pointer_texcoord[0] = rsurface.array_generatedtexcoordtexture2f;
-       R_Mesh_TextureState(&m);
-       RSurf_DrawBatch_WithLightmapSwitching_WithWaterTextureSwitching(texturenumsurfaces, texturesurfacelist, -1, -1, 0, -1);
-       if (water)
-       {
-               // water also reflects before the normal render occurs, and uses a
-               // vertex-controlled alpha blend between refraction and reflection
-               GL_LockArrays(0, 0);
-               R_Mesh_ColorPointer(rsurface.array_color4f, 0, 0);
-               GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-               GL_DepthMask(false);
-               RSurf_DrawBatch_WithLightmapSwitching_WithWaterTextureSwitching(texturenumsurfaces, texturesurfacelist, -1, -1, -1, 0);
-       }
-       GL_LockArrays(0, 0);
-       CHECKGLERROR
-}
-
-static void R_DrawTextureSurfaceList_GL11_Reflection(int texturenumsurfaces, msurface_t **texturesurfacelist)
-{
-       // OpenGL 1.1 path - crusty old voodoo path
-       rmeshstate_t m;
-       qboolean water;
-       if (rsurface.mode != RSURFMODE_MULTIPASS)
-               rsurface.mode = RSURFMODE_MULTIPASS;
-       RSurf_PrepareVerticesForBatch(true, false, texturenumsurfaces, texturesurfacelist);
-       R_Mesh_ColorPointer(NULL, 0, 0);
-       water = (rsurface.texture->currentmaterialflags & MATERIALFLAG_WATERSHADER) != 0;
-       RSurf_GL11_WaterTexCoords(texturenumsurfaces, texturesurfacelist);
-       // now draw the surfaces as refraction texture
-       memset(&m, 0, sizeof(m));
-       m.tex[0] = R_GetTexture(r_texture_white);
-       m.pointer_texcoord[0] = rsurface.array_generatedtexcoordtexture2f;
-       R_Mesh_TextureState(&m);
-       RSurf_DrawBatch_WithLightmapSwitching_WithWaterTextureSwitching(texturenumsurfaces, texturesurfacelist, -1, -1, -1, 0);
-       GL_LockArrays(0, 0);
-       CHECKGLERROR
-}
-#endif
-
 static void R_DrawTextureSurfaceList_ShowSurfaces(int texturenumsurfaces, msurface_t **texturesurfacelist)
 {
        GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
@@ -5613,17 +5510,6 @@ static void R_DrawTextureSurfaceList_GL13(int texturenumsurfaces, msurface_t **t
                rsurface.mode = RSURFMODE_MULTIPASS;
        RSurf_PrepareVerticesForBatch(true, false, texturenumsurfaces, texturesurfacelist);
 
-#if 0
-       if (rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION))
-       {
-               // render background
-               GL_BlendFunc(GL_ONE, GL_ZERO);
-               GL_DepthMask(true);
-               GL_AlphaTest(false);
-               R_DrawTextureSurfaceList_GL11_Water(texturenumsurfaces, texturesurfacelist);
-       }
-#endif
-
        for (layerindex = 0, layer = rsurface.texture->currentlayers;layerindex < rsurface.texture->currentnumlayers;layerindex++, layer++)
        {
                vec4_t layercolor;
@@ -5733,15 +5619,6 @@ static void R_DrawTextureSurfaceList_GL13(int texturenumsurfaces, msurface_t **t
                qglDepthFunc(GL_LEQUAL);CHECKGLERROR
                GL_AlphaTest(false);
        }
-
-#if 0
-       if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
-       {
-               GL_BlendFunc(GL_ONE, GL_ONE);
-               GL_DepthMask(false);
-               R_DrawTextureSurfaceList_GL11_Reflection(texturenumsurfaces, texturesurfacelist);
-       }
-#endif
 }
 
 static void R_DrawTextureSurfaceList_GL11(int texturenumsurfaces, msurface_t **texturesurfacelist)
@@ -5756,17 +5633,6 @@ static void R_DrawTextureSurfaceList_GL11(int texturenumsurfaces, msurface_t **t
                rsurface.mode = RSURFMODE_MULTIPASS;
        RSurf_PrepareVerticesForBatch(true, false, texturenumsurfaces, texturesurfacelist);
 
-#if 0
-       if (rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION))
-       {
-               // render background
-               GL_BlendFunc(GL_ONE, GL_ZERO);
-               GL_DepthMask(true);
-               GL_AlphaTest(false);
-               R_DrawTextureSurfaceList_GL11_Water(texturenumsurfaces, texturesurfacelist);
-       }
-#endif
-
        for (layerindex = 0, layer = rsurface.texture->currentlayers;layerindex < rsurface.texture->currentnumlayers;layerindex++, layer++)
        {
                if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
@@ -5884,15 +5750,6 @@ static void R_DrawTextureSurfaceList_GL11(int texturenumsurfaces, msurface_t **t
                qglDepthFunc(GL_LEQUAL);CHECKGLERROR
                GL_AlphaTest(false);
        }
-
-#if 0
-       if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
-       {
-               GL_BlendFunc(GL_ONE, GL_ONE);
-               GL_DepthMask(false);
-               R_DrawTextureSurfaceList_GL11_Reflection(texturenumsurfaces, texturesurfacelist);
-       }
-#endif
 }
 
 static void R_DrawTextureSurfaceList(int texturenumsurfaces, msurface_t **texturesurfacelist, qboolean writedepth, qboolean depthonly)