implemented r_glsl_water cvar (refraction and reflection rendering)
authorhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Sat, 22 Sep 2007 07:46:30 +0000 (07:46 +0000)
committerhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Sat, 22 Sep 2007 07:46:30 +0000 (07:46 +0000)
git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@7570 d7cf8633-e32d-0410-b094-e92efae38249

20 files changed:
cl_particles.c
cl_screen.c
client.h
gl_backend.c
gl_backend.h
gl_rmain.c
gl_rsurf.c
glquake.h
matrixlib.c
matrixlib.h
model_alias.c
model_brush.c
model_brush.h
model_shared.h
model_sprite.c
r_explosion.c
r_shadow.c
render.h
todo
vid_shared.c

index 0671614..ad30b92 100644 (file)
@@ -2182,7 +2182,7 @@ void R_DrawParticle_TransparentCallback(const entity_render_t *ent, const rtligh
        GL_DepthRange(0, 1);
        GL_PolygonOffset(0, 0);
        GL_DepthTest(true);
-       GL_CullFace(GL_FRONT); // quake is backwards, this culls back faces
+       GL_CullFace(GL_NONE);
 
        // first generate all the vertices at once
        for (surfacelistindex = 0, v3f = particle_vertex3f, t2f = particle_texcoord2f, c4f = particle_color4f;surfacelistindex < numsurfaces;surfacelistindex++, v3f += 3*4, t2f += 2*4, c4f += 4*4)
@@ -2263,14 +2263,7 @@ void R_DrawParticle_TransparentCallback(const entity_render_t *ent, const rtligh
                }
                else if (p->type->orientation == PARTICLE_ORIENTED_DOUBLESIDED)
                {
-                       // double-sided
-                       if (DotProduct(p->vel, r_view.origin) > DotProduct(p->vel, org))
-                       {
-                               VectorNegate(p->vel, v);
-                               VectorVectors(v, right, up);
-                       }
-                       else
-                               VectorVectors(p->vel, right, up);
+                       VectorVectors(p->vel, right, up);
                        VectorScale(right, size, right);
                        VectorScale(up, size, up);
                        v3f[ 0] = org[0] - right[0] - up[0];
index 2a531c1..c2efe5d 100644 (file)
@@ -1780,6 +1780,10 @@ void SCR_DrawScreen (void)
 
        R_UpdateVariables();
 
+       // Quake uses clockwise winding, so these are swapped
+       r_view.cullface_front = GL_BACK;
+       r_view.cullface_back = GL_FRONT;
+
        if (cls.signon == SIGNONS)
        {
                float size;
index b7d97b7..cd92bc8 100644 (file)
--- a/client.h
+++ b/client.h
@@ -1403,7 +1403,10 @@ typedef struct r_view_s
        vec3_t left;
        vec3_t right;
        vec3_t up;
-       mplane_t frustum[5];
+       int numfrustumplanes;
+       mplane_t frustum[6];
+       qboolean useclipplane;
+       mplane_t clipplane;
        float frustum_x, frustum_y;
        vec3_t frustumcorner[4];
        // if turned off it renders an ortho view
@@ -1428,6 +1431,10 @@ typedef struct r_view_s
        // view render, all secondary renders (HDR, mirrors, portals, cameras,
        // distortion effects, etc) omit such debugging information
        qboolean showdebug;
+
+       // these define which values to use in GL_CullFace calls to request frontface or backface culling
+       int cullface_front;
+       int cullface_back;
 }
 r_view_t;
 
index 8d3a8c3..7daeff3 100644 (file)
@@ -282,7 +282,7 @@ void GL_SetupView_Orientation_Identity (void)
 void GL_SetupView_Orientation_FromEntity(const matrix4x4_t *matrix)
 {
        matrix4x4_t tempmatrix, basematrix;
-       Matrix4x4_Invert_Simple(&tempmatrix, matrix);
+       Matrix4x4_Invert_Full(&tempmatrix, matrix);
        Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
        Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
        Matrix4x4_Concat(&backend_viewmatrix, &basematrix, &tempmatrix);
@@ -368,6 +368,65 @@ void GL_SetupView_Mode_Ortho (double x1, double y1, double x2, double y2, double
        CHECKGLERROR
 }
 
+void GL_SetupView_ApplyCustomNearClipPlane(double normalx, double normaly, double normalz, double dist)
+{
+       double matrix[16];
+       double q[4];
+       double d;
+       float clipPlane[4], v3[3], v4[3];
+       float normal[3];
+
+       // This is Olique Depth Projection from http://www.terathon.com/code/oblique.php
+       // modified to fit in this codebase.
+
+       VectorSet(normal, normalx, normaly, normalz);
+       Matrix4x4_Transform3x3(&backend_viewmatrix, normal, clipPlane);
+       VectorScale(normal, dist, v3);
+       Matrix4x4_Transform(&backend_viewmatrix, v3, v4);
+       // FIXME: LordHavoc: I think this can be done more efficiently somehow but I can't remember the technique
+       clipPlane[3] = -DotProduct(v4, clipPlane);
+
+#if 0
+{
+       // testing code for comparing results
+       float clipPlane2[4];
+       VectorCopy4(clipPlane, clipPlane2);
+       R_Mesh_Matrix(&identitymatrix);
+       VectorSet(q, normal[0], normal[1], normal[2], -dist);
+       qglClipPlane(GL_CLIP_PLANE0, q);
+       qglGetClipPlane(GL_CLIP_PLANE0, q);
+       VectorCopy4(q, clipPlane);
+}
+#endif
+
+       // Calculate the clip-space corner point opposite the clipping plane
+       // as (sgn(clipPlane.x), sgn(clipPlane.y), 1, 1) and
+       // transform it into camera space by multiplying it
+       // by the inverse of the projection matrix
+       Matrix4x4_ToArrayDoubleGL(&backend_projectmatrix, matrix);
+
+       q[0] = ((clipPlane[0] < 0.0f ? -1.0f : clipPlane[0] > 0.0f ? 1.0f : 0.0f) + matrix[8]) / matrix[0];
+       q[1] = ((clipPlane[1] < 0.0f ? -1.0f : clipPlane[1] > 0.0f ? 1.0f : 0.0f) + matrix[9]) / matrix[5];
+       q[2] = -1.0f;
+       q[3] = (1.0f + matrix[10]) / matrix[14];
+
+       // Calculate the scaled plane vector
+       d = 2.0f / DotProduct4(clipPlane, q);
+
+       // Replace the third row of the projection matrix
+       matrix[2] = clipPlane[0] * d;
+       matrix[6] = clipPlane[1] * d;
+       matrix[10] = clipPlane[2] * d + 1.0f;
+       matrix[14] = clipPlane[3] * d;
+
+       // Load it back into OpenGL
+       qglMatrixMode(GL_PROJECTION);CHECKGLERROR
+       qglLoadMatrixd(matrix);CHECKGLERROR
+       qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
+       CHECKGLERROR
+       Matrix4x4_FromArrayDoubleGL(&backend_projectmatrix, matrix);
+}
+
 typedef struct gltextureunit_s
 {
        const void *pointer_texcoord;
index a8c2c3b..a2209c9 100644 (file)
@@ -15,6 +15,7 @@ void GL_SetupView_Orientation_FromEntity(const matrix4x4_t *matrix);
 void GL_SetupView_Mode_Perspective(double frustumx, double frustumy, double zNear, double zFar);
 void GL_SetupView_Mode_PerspectiveInfiniteFarClip(double frustumx, double frustumy, double zNear);
 void GL_SetupView_Mode_Ortho(double x1, double y1, double x2, double y2, double zNear, double zFar);
+void GL_SetupView_ApplyCustomNearClipPlane(double normalx, double normaly, double normalz, double dist);
 void GL_BlendFunc(int blendfunc1, int blendfunc2);
 void GL_DepthMask(int state);
 void GL_DepthTest(int state);
index 042501d..de6689a 100644 (file)
@@ -78,6 +78,17 @@ cvar_t r_glsl = {CVAR_SAVE, "r_glsl", "1", "enables use of OpenGL 2.0 pixel shad
 cvar_t r_glsl_offsetmapping = {CVAR_SAVE, "r_glsl_offsetmapping", "0", "offset mapping effect (also known as parallax mapping or virtual displacement mapping)"};
 cvar_t r_glsl_offsetmapping_reliefmapping = {CVAR_SAVE, "r_glsl_offsetmapping_reliefmapping", "0", "relief mapping effect (higher quality)"};
 cvar_t r_glsl_offsetmapping_scale = {CVAR_SAVE, "r_glsl_offsetmapping_scale", "0.04", "how deep the offset mapping effect is"};
+cvar_t r_glsl_water = {CVAR_SAVE, "r_glsl_water", "0", "whether to use reflections and refraction on water surfaces (note: r_wateralpha must be set below 1)"};
+cvar_t r_glsl_water_clippingplanebias = {CVAR_SAVE, "r_glsl_water_clippingplanebias", "1", "a rather technical setting which avoids black pixels around water edges"};
+cvar_t r_glsl_water_resolutionmultiplier = {CVAR_SAVE, "r_glsl_water_resolutionmultiplier", "0.5", "multiplier for screen resolution when rendering refracted/reflected scenes, 1 is full quality, lower values are faster"};
+cvar_t r_glsl_water_refractcolor_r = {CVAR_SAVE, "r_glsl_water_refractcolor_r", "1", "water color tint for refraction"};
+cvar_t r_glsl_water_refractcolor_g = {CVAR_SAVE, "r_glsl_water_refractcolor_g", "1", "water color tint for refraction"};
+cvar_t r_glsl_water_refractcolor_b = {CVAR_SAVE, "r_glsl_water_refractcolor_b", "1", "water color tint for refraction"};
+cvar_t r_glsl_water_reflectcolor_r = {CVAR_SAVE, "r_glsl_water_reflectcolor_r", "1", "water color tint for reflection"};
+cvar_t r_glsl_water_reflectcolor_g = {CVAR_SAVE, "r_glsl_water_reflectcolor_g", "1", "water color tint for reflection"};
+cvar_t r_glsl_water_reflectcolor_b = {CVAR_SAVE, "r_glsl_water_reflectcolor_b", "1", "water color tint for reflection"};
+cvar_t r_glsl_water_refractdistort = {CVAR_SAVE, "r_glsl_water_refractdistort", "0.01", "how much water refractions shimmer"};
+cvar_t r_glsl_water_reflectdistort = {CVAR_SAVE, "r_glsl_water_reflectdistort", "0.01", "how much water reflections shimmer"};
 cvar_t r_glsl_deluxemapping = {CVAR_SAVE, "r_glsl_deluxemapping", "1", "use per pixel lighting on deluxemap-compiled q3bsp maps (or a value of 2 forces deluxemap shading even without deluxemaps)"};
 cvar_t r_glsl_contrastboost = {CVAR_SAVE, "r_glsl_contrastboost", "1", "by how much to multiply the contrast in dark areas (1 is no change)"};
 
@@ -138,6 +149,34 @@ static struct r_bloomstate_s
 }
 r_bloomstate;
 
+typedef struct r_waterstate_waterplane_s
+{
+       rtexture_t *texture_refraction;
+       rtexture_t *texture_reflection;
+       mplane_t plane;
+}
+r_waterstate_waterplane_t;
+
+#define MAX_WATERPLANES 16
+
+static struct r_waterstate_s
+{
+       qboolean enabled;
+
+       qboolean renderingscene; // true while rendering a refraction or reflection texture, disables water surfaces
+
+       int waterwidth, waterheight;
+       int texturewidth, textureheight;
+
+       int maxwaterplanes; // same as MAX_WATERPLANES
+       int numwaterplanes;
+       r_waterstate_waterplane_t waterplanes[MAX_WATERPLANES];
+
+       float screenscale[2];
+       float screencenter[2];
+}
+r_waterstate;
+
 // shadow volume bsp struct with automatically growing nodes buffer
 svbsp_t r_svbsp;
 
@@ -402,6 +441,12 @@ 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"
+"varying vec4 ModelViewProjectionPosition;\n"
+"//varying vec4 ModelViewProjectionPosition_svector;\n"
+"//varying vec4 ModelViewProjectionPosition_tvector;\n"
+"#endif\n"
+"\n"
 "\n"
 "\n"
 "\n"
@@ -458,9 +503,18 @@ static const char *builtinshaderstring =
 "      VectorR = gl_MultiTexCoord3.xyz;\n"
 "#endif\n"
 "\n"
-"      // transform vertex to camera space, using ftransform to match non-VS\n"
+"//#ifdef USEWATER\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"
+"//#endif\n"
+"\n"
+"// transform vertex to camera space, using ftransform to match non-VS\n"
 "      // rendering\n"
 "      gl_Position = ftransform();\n"
+"#ifdef USEWATER\n"
+"      ModelViewProjectionPosition = gl_Position;\n"
+"#endif\n"
 "}\n"
 "\n"
 "#endif // VERTEX_SHADER\n"
@@ -483,6 +537,8 @@ static const char *builtinshaderstring =
 "uniform sampler2D Texture_Lightmap;\n"
 "uniform sampler2D Texture_Deluxemap;\n"
 "uniform sampler2D Texture_Glow;\n"
+"uniform sampler2D Texture_Reflection;\n"
+"uniform sampler2D Texture_Refraction;\n"
 "\n"
 "uniform myhvec3 LightColor;\n"
 "uniform myhvec3 AmbientColor;\n"
@@ -492,6 +548,14 @@ static const char *builtinshaderstring =
 "uniform myhvec3 Color_Shirt;\n"
 "uniform myhvec3 FogColor;\n"
 "\n"
+"#ifdef USEWATER\n"
+"uniform vec4 DistortScaleRefractReflect;\n"
+"uniform vec4 ScreenScaleRefractReflect;\n"
+"uniform vec4 ScreenCenterRefractReflect;\n"
+"uniform myhvec3 RefractColor;\n"
+"uniform myhvec3 ReflectColor;\n"
+"#endif\n"
+"\n"
 "uniform myhalf GlowScale;\n"
 "uniform myhalf SceneBrightness;\n"
 "#ifdef USECONTRASTBOOST\n"
@@ -661,6 +725,23 @@ 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 USEWATER\n"
+"#ifdef MODE_LIGHTSOURCE\n"
+"      color.rgb *= color.a;\n"
+"#else\n"
+"      myhalf Fresnel = myhalf(max(pow(min(1, 1.0 - normalize(EyeVector).z), 5), 0.1));\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 + normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5)).xyxy * DistortScaleRefractReflect;\n"
+"      color.rgb = mix(mix(myhvec3(texture2D(Texture_Refraction, ScreenTexCoord.xy)) * RefractColor, myhvec3(texture2D(Texture_Reflection, ScreenTexCoord.zw)) * ReflectColor, Fresnel), color.rgb, color.a);\n"
+"      //color.rgb = myhvec3(texture2D(Texture_Refraction, ScreenTexCoord.xy)); // testing only\n"
+"      //color.rgb = myhvec3(texture2D(Texture_Reflection, ScreenTexCoord.zw)); // testing only\n"
+"      //vec4 RefractionPosition = ModelViewProjectionPosition + ModelViewProjectionPosition_svector * distort.x + ModelViewProjectionPosition_tvector * distort.y;\n"
+"      //vec4 ReflectionPosition = ModelViewProjectionPosition + ModelViewProjectionPosition_svector * distort.w + ModelViewProjectionPosition_tvector * distort.z;\n"
+"      //color.rgb += mix(myhvec3(texture2DProj(Texture_Refraction, RefractionPosition)) * RefractColor, myhvec3(texture2DProj(Texture_Reflection, ReflectionPosition)) * ReflectColor, Fresnel);\n"
+"#endif\n"
+"#endif\n"
+"\n"
 "#ifdef USECONTRASTBOOST\n"
 "      color.rgb = color.rgb * SceneBrightness / (ContrastBoostCoeff * color.rgb + myhvec3(1, 1, 1));\n"
 "#else\n"
@@ -680,6 +761,7 @@ const char *permutationinfo[][2] =
        {"#define MODE_LIGHTDIRECTIONMAP_MODELSPACE\n", " lightdirectionmap_modelspace"},
        {"#define MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n", " lightdirectionmap_tangentspace"},
        {"#define MODE_LIGHTDIRECTION\n", " lightdirection"},
+       {"#define USEWATER\n", " water"},
        {"#define USEGLOW\n", " glow"},
        {"#define USEFOG\n", " fog"},
        {"#define USECOLORMAPPING\n", " colormapping"},
@@ -778,6 +860,8 @@ void R_GLSL_CompilePermutation(const char *filename, int permutation)
                p->loc_Texture_Lightmap    = qglGetUniformLocationARB(p->program, "Texture_Lightmap");
                p->loc_Texture_Deluxemap   = qglGetUniformLocationARB(p->program, "Texture_Deluxemap");
                p->loc_Texture_Glow        = qglGetUniformLocationARB(p->program, "Texture_Glow");
+               p->loc_Texture_Refraction  = qglGetUniformLocationARB(p->program, "Texture_Refraction");
+               p->loc_Texture_Reflection  = qglGetUniformLocationARB(p->program, "Texture_Reflection");
                p->loc_FogColor            = qglGetUniformLocationARB(p->program, "FogColor");
                p->loc_LightPosition       = qglGetUniformLocationARB(p->program, "LightPosition");
                p->loc_EyePosition         = qglGetUniformLocationARB(p->program, "EyePosition");
@@ -797,6 +881,11 @@ void R_GLSL_CompilePermutation(const char *filename, int permutation)
                p->loc_SpecularColor       = qglGetUniformLocationARB(p->program, "SpecularColor");
                p->loc_LightDir            = qglGetUniformLocationARB(p->program, "LightDir");
                p->loc_ContrastBoostCoeff  = qglGetUniformLocationARB(p->program, "ContrastBoostCoeff");
+               p->loc_DistortScaleRefractReflect = qglGetUniformLocationARB(p->program, "DistortScaleRefractReflect");
+               p->loc_ScreenScaleRefractReflect = qglGetUniformLocationARB(p->program, "ScreenScaleRefractReflect");
+               p->loc_ScreenCenterRefractReflect = qglGetUniformLocationARB(p->program, "ScreenCenterRefractReflect");
+               p->loc_RefractColor        = qglGetUniformLocationARB(p->program, "RefractColor");
+               p->loc_ReflectColor        = qglGetUniformLocationARB(p->program, "ReflectColor");
                // initialize the samplers to refer to the texture units we use
                if (p->loc_Texture_Normal >= 0)    qglUniform1iARB(p->loc_Texture_Normal, 0);
                if (p->loc_Texture_Color >= 0)     qglUniform1iARB(p->loc_Texture_Color, 1);
@@ -809,6 +898,8 @@ void R_GLSL_CompilePermutation(const char *filename, int permutation)
                if (p->loc_Texture_Deluxemap >= 0) qglUniform1iARB(p->loc_Texture_Deluxemap, 8);
                if (p->loc_Texture_Glow >= 0)      qglUniform1iARB(p->loc_Texture_Glow, 9);
                if (p->loc_Texture_Attenuation >= 0) qglUniform1iARB(p->loc_Texture_Attenuation, 10);
+               if (p->loc_Texture_Refraction >= 0) qglUniform1iARB(p->loc_Texture_Refraction, 11);
+               if (p->loc_Texture_Reflection >= 0) qglUniform1iARB(p->loc_Texture_Reflection, 12);
                CHECKGLERROR
                qglUseProgramObjectARB(0);CHECKGLERROR
        }
@@ -886,6 +977,8 @@ 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_USEWATER;
        }
        else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)
        {
@@ -906,6 +999,8 @@ 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_USEWATER;
        }
        else if (modellighting)
        {
@@ -929,6 +1024,8 @@ 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_USEWATER;
        }
        else
        {
@@ -971,6 +1068,8 @@ 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_USEWATER;
        }
        if (!r_glsl_permutations[permutation & SHADERPERMUTATION_MASK].program)
        {
@@ -1054,6 +1153,8 @@ int R_SetupSurfaceShader(const vec3_t lightcolorbase, qboolean modellighting, fl
        //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->currentskinframe->glow));
+       if (r_glsl_permutation->loc_Texture_Refraction >= 0) R_Mesh_TexBind(11, R_GetTexture(r_texture_white)); // changed per surface
+       if (r_glsl_permutation->loc_Texture_Reflection >= 0) R_Mesh_TexBind(12, R_GetTexture(r_texture_white)); // changed per surface
        if (r_glsl_permutation->loc_GlowScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_GlowScale, r_hdr_glowintensity.value);
        if (r_glsl_permutation->loc_ContrastBoostCoeff >= 0)
        {
@@ -1097,6 +1198,11 @@ int R_SetupSurfaceShader(const vec3_t lightcolorbase, qboolean modellighting, fl
        if (r_glsl_permutation->loc_FogRangeRecip >= 0) qglUniform1fARB(r_glsl_permutation->loc_FogRangeRecip, r_refdef.fograngerecip);
        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);
+       if (r_glsl_permutation->loc_DistortScaleRefractReflect >= 0) qglUniform4fARB(r_glsl_permutation->loc_DistortScaleRefractReflect, r_glsl_water_refractdistort.value, r_glsl_water_refractdistort.value, r_glsl_water_reflectdistort.value, r_glsl_water_reflectdistort.value);
+       if (r_glsl_permutation->loc_ScreenScaleRefractReflect >= 0) qglUniform4fARB(r_glsl_permutation->loc_ScreenScaleRefractReflect, r_waterstate.screenscale[0], r_waterstate.screenscale[1], r_waterstate.screenscale[0], r_waterstate.screenscale[1]);
+       if (r_glsl_permutation->loc_ScreenCenterRefractReflect >= 0) qglUniform4fARB(r_glsl_permutation->loc_ScreenCenterRefractReflect, r_waterstate.screencenter[0], r_waterstate.screencenter[1], r_waterstate.screencenter[0], r_waterstate.screencenter[1]);
+       if (r_glsl_permutation->loc_RefractColor >= 0) qglUniform3fARB(r_glsl_permutation->loc_RefractColor, r_glsl_water_refractcolor_r.value, r_glsl_water_refractcolor_g.value, r_glsl_water_refractcolor_b.value);
+       if (r_glsl_permutation->loc_ReflectColor >= 0) qglUniform3fARB(r_glsl_permutation->loc_ReflectColor, r_glsl_water_reflectcolor_r.value, r_glsl_water_reflectcolor_g.value, r_glsl_water_reflectcolor_b.value);
        CHECKGLERROR
        return permutation;
 }
@@ -1470,6 +1576,7 @@ void gl_main_start(void)
        }
        R_BuildFogTexture();
        memset(&r_bloomstate, 0, sizeof(r_bloomstate));
+       memset(&r_waterstate, 0, sizeof(r_waterstate));
        memset(r_glsl_permutations, 0, sizeof(r_glsl_permutations));
        memset(&r_svbsp, 0, sizeof (r_svbsp));
 }
@@ -1494,6 +1601,7 @@ void gl_main_shutdown(void)
        r_texture_whitecube = NULL;
        r_texture_normalizationcube = NULL;
        memset(&r_bloomstate, 0, sizeof(r_bloomstate));
+       memset(&r_waterstate, 0, sizeof(r_waterstate));
        R_GLSL_Restart_f();
 }
 
@@ -1573,6 +1681,17 @@ void GL_Main_Init(void)
        Cvar_RegisterVariable(&r_glsl_offsetmapping);
        Cvar_RegisterVariable(&r_glsl_offsetmapping_reliefmapping);
        Cvar_RegisterVariable(&r_glsl_offsetmapping_scale);
+       Cvar_RegisterVariable(&r_glsl_water);
+       Cvar_RegisterVariable(&r_glsl_water_resolutionmultiplier);
+       Cvar_RegisterVariable(&r_glsl_water_clippingplanebias);
+       Cvar_RegisterVariable(&r_glsl_water_refractcolor_r);
+       Cvar_RegisterVariable(&r_glsl_water_refractcolor_g);
+       Cvar_RegisterVariable(&r_glsl_water_refractcolor_b);
+       Cvar_RegisterVariable(&r_glsl_water_reflectcolor_r);
+       Cvar_RegisterVariable(&r_glsl_water_reflectcolor_g);
+       Cvar_RegisterVariable(&r_glsl_water_reflectcolor_b);
+       Cvar_RegisterVariable(&r_glsl_water_refractdistort);
+       Cvar_RegisterVariable(&r_glsl_water_reflectdistort);
        Cvar_RegisterVariable(&r_glsl_deluxemapping);
        Cvar_RegisterVariable(&r_lerpsprites);
        Cvar_RegisterVariable(&r_lerpmodels);
@@ -1653,7 +1772,7 @@ int R_CullBox(const vec3_t mins, const vec3_t maxs)
 {
        int i;
        mplane_t *p;
-       for (i = 0;i < 4;i++)
+       for (i = 0;i < r_view.numfrustumplanes;i++)
        {
                p = r_view.frustum + i;
                switch(p->signbits)
@@ -1790,7 +1909,7 @@ static void R_View_UpdateEntityVisible (void)
        if (!r_drawentities.integer)
                return;
 
-       renderimask = r_refdef.envmap ? (RENDER_EXTERIORMODEL | RENDER_VIEWMODEL) : (chase_active.integer ? 0 : RENDER_EXTERIORMODEL);
+       renderimask = r_refdef.envmap ? (RENDER_EXTERIORMODEL | RENDER_VIEWMODEL) : ((chase_active.integer || r_waterstate.renderingscene) ? RENDER_VIEWMODEL : RENDER_EXTERIORMODEL);
        if (r_refdef.worldmodel && r_refdef.worldmodel->brush.BoxTouchingVisibleLeafs)
        {
                // worldmodel can check visibility
@@ -1852,8 +1971,8 @@ int R_DrawBrushModelsSky (void)
        return sky;
 }
 
-void R_DrawNoModel(entity_render_t *ent);
-void R_DrawModels(void)
+static void R_DrawNoModel(entity_render_t *ent);
+static void R_DrawModels(void)
 {
        int i;
        entity_render_t *ent;
@@ -1874,7 +1993,7 @@ void R_DrawModels(void)
        }
 }
 
-void R_DrawModelsDepth(void)
+static void R_DrawModelsDepth(void)
 {
        int i;
        entity_render_t *ent;
@@ -1893,8 +2012,28 @@ void R_DrawModelsDepth(void)
        }
 }
 
+static void R_DrawModelsAddWaterPlanes(void)
+{
+       int i;
+       entity_render_t *ent;
+
+       if (!r_drawentities.integer)
+               return;
+
+       for (i = 0;i < r_refdef.numentities;i++)
+       {
+               if (!r_viewcache.entityvisible[i])
+                       continue;
+               ent = r_refdef.entities[i];
+               r_refdef.stats.entities++;
+               if (ent->model && ent->model->DrawAddWaterPlanes != NULL)
+                       ent->model->DrawAddWaterPlanes(ent);
+       }
+}
+
 static void R_View_SetFrustum(void)
 {
+       int i;
        double slopex, slopey;
 
        // break apart the view matrix into vectors for various purposes
@@ -2000,12 +2139,16 @@ static void R_View_SetFrustum(void)
                r_view.frustum[3].dist = DotProduct (r_view.origin, r_view.frustum[3].normal) + r_view.ortho_y;
                r_view.frustum[4].dist = DotProduct (r_view.origin, r_view.frustum[4].normal) + r_refdef.nearclip;
        }
+       r_view.numfrustumplanes = 5;
 
-       PlaneClassify(&r_view.frustum[0]);
-       PlaneClassify(&r_view.frustum[1]);
-       PlaneClassify(&r_view.frustum[2]);
-       PlaneClassify(&r_view.frustum[3]);
-       PlaneClassify(&r_view.frustum[4]);
+       if (r_view.useclipplane)
+       {
+               r_view.numfrustumplanes = 6;
+               r_view.frustum[5] = r_view.clipplane;
+       }
+
+       for (i = 0;i < r_view.numfrustumplanes;i++)
+               PlaneClassify(r_view.frustum + i);
 
        // LordHavoc: note to all quake engine coders, Quake had a special case
        // for 90 degrees which assumed a square view (wrong), so I removed it,
@@ -2040,11 +2183,11 @@ static void R_View_SetFrustum(void)
 void R_View_Update(void)
 {
        R_View_SetFrustum();
-       R_View_WorldVisibility();
+       R_View_WorldVisibility(r_view.useclipplane);
        R_View_UpdateEntityVisible();
 }
 
-void R_SetupView(const matrix4x4_t *matrix)
+void R_SetupView(void)
 {
        if (!r_view.useperspective)
                GL_SetupView_Mode_Ortho(-r_view.ortho_x, -r_view.ortho_y, r_view.ortho_x, r_view.ortho_y, -r_refdef.farclip, r_refdef.farclip);
@@ -2053,7 +2196,17 @@ void R_SetupView(const matrix4x4_t *matrix)
        else
                GL_SetupView_Mode_Perspective(r_view.frustum_x, r_view.frustum_y, r_refdef.nearclip, r_refdef.farclip);
 
-       GL_SetupView_Orientation_FromEntity(matrix);
+       GL_SetupView_Orientation_FromEntity(&r_view.matrix);
+
+       if (r_view.useclipplane)
+       {
+               // LordHavoc: couldn't figure out how to make this approach the
+               vec_t dist = r_view.clipplane.dist - r_glsl_water_clippingplanebias.value;
+               vec_t viewdist = DotProduct(r_view.origin, r_view.clipplane.normal);
+               if (viewdist < r_view.clipplane.dist + r_glsl_water_clippingplanebias.value)
+                       dist = r_view.clipplane.dist;
+               GL_SetupView_ApplyCustomNearClipPlane(r_view.clipplane.normal[0], r_view.clipplane.normal[1], r_view.clipplane.normal[2], dist);
+       }
 }
 
 void R_ResetViewRendering2D(void)
@@ -2100,7 +2253,7 @@ void R_ResetViewRendering3D(void)
 
        // GL is weird because it's bottom to top, r_view.y is top to bottom
        qglViewport(r_view.x, vid.height - (r_view.y + r_view.height), r_view.width, r_view.height);CHECKGLERROR
-       R_SetupView(&r_view.matrix);
+       R_SetupView();
        GL_Scissor(r_view.x, r_view.y, r_view.width, r_view.height);
        GL_Color(1, 1, 1, 1);
        GL_ColorMask(r_view.colormask[0], r_view.colormask[1], r_view.colormask[2], 1);
@@ -2119,7 +2272,7 @@ void R_ResetViewRendering3D(void)
        qglStencilMask(~0);CHECKGLERROR
        qglStencilFunc(GL_ALWAYS, 128, ~0);CHECKGLERROR
        qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);CHECKGLERROR
-       GL_CullFace(GL_FRONT); // quake is backwards, this culls back faces
+       GL_CullFace(r_view.cullface_back);
 }
 
 /*
@@ -2183,7 +2336,170 @@ void R_ResetViewRendering3D(void)
 "#endif // FRAGMENT_SHADER\n"
 */
 
-void R_RenderScene(void);
+void R_RenderScene(qboolean addwaterplanes);
+
+static void R_Water_StartFrame(void)
+{
+       int i;
+       int texturewidth, textureheight;
+       r_waterstate_waterplane_t *p;
+
+       r_waterstate.maxwaterplanes = 0;
+
+       // set waterwidth and waterheight to the water resolution that will be
+       // used (often less than the screen resolution for faster rendering)
+       r_waterstate.waterwidth = (int)bound(1, r_view.width * r_glsl_water_resolutionmultiplier.value, r_view.width);
+       r_waterstate.waterheight = (int)bound(1, r_view.height * r_glsl_water_resolutionmultiplier.value, r_view.height);
+
+       // calculate desired texture sizes
+       if (gl_support_arb_texture_non_power_of_two)
+       {
+               texturewidth = r_waterstate.waterwidth;
+               textureheight = r_waterstate.waterheight;
+       }
+       else
+       {
+               for (texturewidth   = 1;texturewidth   < r_waterstate.waterwidth ;texturewidth   *= 2);
+               for (textureheight  = 1;textureheight  < r_waterstate.waterheight;textureheight  *= 2);
+       }
+
+       if (!r_glsl_water.integer)
+               texturewidth = textureheight = 0;
+
+       // allocate textures as needed
+       if (r_waterstate.texturewidth != texturewidth || r_waterstate.textureheight != textureheight)
+       {
+               for (i = 0, p = r_waterstate.waterplanes;i < r_waterstate.maxwaterplanes;i++, p++)
+               {
+                       if (p->texture_refraction)
+                               R_FreeTexture(p->texture_refraction);
+                       p->texture_refraction = NULL;
+                       if (p->texture_reflection)
+                               R_FreeTexture(p->texture_reflection);
+                       p->texture_reflection = NULL;
+               }
+               r_waterstate.texturewidth = texturewidth;
+               r_waterstate.textureheight = textureheight;
+       }
+
+       if ((!texturewidth && !textureheight) || texturewidth > gl_max_texture_size || textureheight > gl_max_texture_size)
+       {
+               // can't use water if the parameters are too weird
+               // can't use water if the card does not support the texture size
+               memset(&r_waterstate, 0, sizeof(r_waterstate));
+               return;
+       }
+
+       r_waterstate.enabled = true;
+
+       r_waterstate.maxwaterplanes = MAX_WATERPLANES;
+
+       // set up variables that will be used in shader setup
+       r_waterstate.screenscale[0] = 0.5f * (float)r_waterstate.waterwidth / (float)r_waterstate.texturewidth;
+       r_waterstate.screenscale[1] = 0.5f * (float)r_waterstate.waterheight / (float)r_waterstate.textureheight;
+       r_waterstate.screencenter[0] = 0.5f * (float)r_waterstate.waterwidth / (float)r_waterstate.texturewidth;
+       r_waterstate.screencenter[1] = 0.5f * (float)r_waterstate.waterheight / (float)r_waterstate.textureheight;
+}
+
+static void R_Water_AddWaterPlane(msurface_t *surface)
+{
+       int triangleindex, planeindex;
+       const int *e;
+       vec3_t vert[3];
+       vec3_t normal;
+       r_waterstate_waterplane_t *p;
+       // just use the first triangle with a valid normal for any decisions
+       VectorClear(normal);
+       for (triangleindex = 0, e = rsurface.modelelement3i + surface->num_firsttriangle * 3;triangleindex < surface->num_triangles;triangleindex++, e += 3)
+       {
+               Matrix4x4_Transform(&rsurface.matrix, rsurface.modelvertex3f + e[0]*3, vert[0]);
+               Matrix4x4_Transform(&rsurface.matrix, rsurface.modelvertex3f + e[1]*3, vert[1]);
+               Matrix4x4_Transform(&rsurface.matrix, rsurface.modelvertex3f + e[2]*3, vert[2]);
+               TriangleNormal(vert[0], vert[1], vert[2], normal);
+               if (VectorLength2(normal) >= 0.001)
+                       break;
+       }
+       for (planeindex = 0, p = r_waterstate.waterplanes;planeindex < r_waterstate.numwaterplanes;planeindex++, p++)
+               if (fabs(PlaneDiff(vert[0], &p->plane)) < 1 && fabs(PlaneDiff(vert[1], &p->plane)) < 1 && fabs(PlaneDiff(vert[2], &p->plane)) < 1)
+                       break;
+       // if this triangle does not fit any known plane rendered this frame, render textures for it
+       if (planeindex >= r_waterstate.numwaterplanes && planeindex < r_waterstate.maxwaterplanes)
+       {
+               // store the new plane
+               r_waterstate.numwaterplanes++;
+               VectorCopy(normal, p->plane.normal);
+               VectorNormalize(p->plane.normal);
+               p->plane.dist = DotProduct(vert[0], p->plane.normal);
+               PlaneClassify(&p->plane);
+               // flip the plane if it does not face the viewer
+               if (PlaneDiff(r_view.origin, &p->plane) < 0)
+               {
+                       VectorNegate(p->plane.normal, p->plane.normal);
+                       p->plane.dist *= -1;
+                       PlaneClassify(&p->plane);
+               }
+       }
+}
+
+static void R_Water_ProcessPlanes(void)
+{
+       r_view_t originalview;
+       int planeindex;
+       r_waterstate_waterplane_t *p;
+
+       for (planeindex = 0, p = r_waterstate.waterplanes;planeindex < r_waterstate.numwaterplanes;planeindex++, p++)
+       {
+               if (!p->texture_refraction)
+               {
+                       p->texture_refraction = R_LoadTexture2D(r_main_texturepool, va("waterplane%i_refraction", planeindex), r_waterstate.texturewidth, r_waterstate.textureheight, NULL, TEXTYPE_RGBA, TEXF_FORCELINEAR | TEXF_CLAMP | TEXF_ALWAYSPRECACHE, NULL);
+                       p->texture_reflection = R_LoadTexture2D(r_main_texturepool, va("waterplane%i_reflection", planeindex), r_waterstate.texturewidth, r_waterstate.textureheight, NULL, TEXTYPE_RGBA, TEXF_FORCELINEAR | TEXF_CLAMP | TEXF_ALWAYSPRECACHE, NULL);
+               }
+
+               originalview = r_view;
+               r_view.showdebug = false;
+               r_view.width = r_waterstate.waterwidth;
+               r_view.height = r_waterstate.waterheight;
+               r_view.useclipplane = true;
+
+               r_view.clipplane = p->plane;
+               VectorNegate(r_view.clipplane.normal, r_view.clipplane.normal);
+               r_view.clipplane.dist = -r_view.clipplane.dist;
+               PlaneClassify(&r_view.clipplane);
+               r_waterstate.renderingscene = true;
+               // render the normal view scene and copy into texture
+               // (except that a clipping plane should be used to hide everything on one side of the water, and the viewer's weapon model should be omitted)
+               R_RenderScene(false);
+
+               // copy view into the screen texture
+               R_Mesh_TexBind(0, R_GetTexture(p->texture_refraction));
+               GL_ActiveTexture(0);
+               CHECKGLERROR
+               qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_view.x, vid.height - (r_view.y + r_view.height), r_view.width, r_view.height);CHECKGLERROR
+
+               // render reflected scene and copy into texture
+               Matrix4x4_Reflect(&r_view.matrix, p->plane.normal[0], p->plane.normal[1], p->plane.normal[2], p->plane.dist, -2);
+               r_view.clipplane = p->plane;
+               // reverse the cullface settings for this render
+               r_view.cullface_front = GL_FRONT;
+               r_view.cullface_back = GL_BACK;
+
+               R_ResetViewRendering3D();
+               R_ClearScreen();
+
+               R_RenderScene(false);
+
+               R_Mesh_TexBind(0, R_GetTexture(p->texture_reflection));
+               GL_ActiveTexture(0);
+               CHECKGLERROR
+               qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_view.x, vid.height - (r_view.y + r_view.height), r_view.width, r_view.height);CHECKGLERROR
+
+               R_ResetViewRendering3D();
+               R_ClearScreen();
+
+               r_view = originalview;
+               r_waterstate.renderingscene = false;
+       }
+}
 
 void R_Bloom_StartFrame(void)
 {
@@ -2452,7 +2768,8 @@ void R_HDR_RenderBloomTexture(void)
        r_view.colorscale = r_bloom_colorscale.value * r_hdr_scenebrightness.value;
        if (r_hdr.integer)
                r_view.colorscale /= r_hdr_range.value;
-       R_RenderScene();
+       r_waterstate.numwaterplanes = 0;
+       R_RenderScene(r_waterstate.enabled);
        r_view.showdebug = true;
 
        R_ResetViewRendering2D();
@@ -2536,7 +2853,7 @@ static void R_BlendView(void)
        }
 }
 
-void R_RenderScene(void);
+void R_RenderScene(qboolean addwaterplanes);
 
 matrix4x4_t r_waterscrollmatrix;
 
@@ -2622,22 +2939,19 @@ void R_RenderView(void)
 
        R_Shadow_UpdateWorldLightSelection();
 
+       R_Bloom_StartFrame();
+       R_Water_StartFrame();
+
        CHECKGLERROR
        if (r_timereport_active)
                R_TimeReport("setup");
 
-       R_View_Update();
-       if (r_timereport_active)
-               R_TimeReport("visibility");
-
        R_ResetViewRendering3D();
 
        R_ClearScreen();
        if (r_timereport_active)
                R_TimeReport("clear");
 
-       R_Bloom_StartFrame();
-
        r_view.showdebug = true;
 
        // this produces a bloom texture to be used in R_BlendView() later
@@ -2645,7 +2959,8 @@ void R_RenderView(void)
                R_HDR_RenderBloomTexture();
 
        r_view.colorscale = r_hdr_scenebrightness.value;
-       R_RenderScene();
+       r_waterstate.numwaterplanes = 0;
+       R_RenderScene(r_waterstate.enabled);
 
        R_BlendView();
        if (r_timereport_active)
@@ -2662,18 +2977,50 @@ extern void R_DrawPortals (void);
 extern cvar_t cl_locs_show;
 static void R_DrawLocs(void);
 static void R_DrawEntityBBoxes(void);
-void R_RenderScene(void)
+void R_RenderScene(qboolean addwaterplanes)
 {
+       if (addwaterplanes)
+       {
+               R_ResetViewRendering3D();
+
+               R_View_Update();
+               if (r_timereport_active)
+                       R_TimeReport("watervisibility");
+
+               if (cl.csqc_vidvars.drawworld && r_refdef.worldmodel && r_refdef.worldmodel->Draw)
+               {
+                       r_refdef.worldmodel->DrawAddWaterPlanes(r_refdef.worldentity);
+                       if (r_timereport_active)
+                               R_TimeReport("waterworld");
+               }
+
+               // don't let sound skip if going slow
+               if (r_refdef.extraupdate)
+                       S_ExtraUpdate ();
+
+               R_DrawModelsAddWaterPlanes();
+               if (r_timereport_active)
+                       R_TimeReport("watermodels");
+
+               R_Water_ProcessPlanes();
+               if (r_timereport_active)
+                       R_TimeReport("waterscenes");
+       }
+
+       R_ResetViewRendering3D();
+
        // don't let sound skip if going slow
        if (r_refdef.extraupdate)
                S_ExtraUpdate ();
 
-       R_ResetViewRendering3D();
-
        R_MeshQueue_BeginScene();
 
        R_SkyStartFrame();
 
+       R_View_Update();
+       if (r_timereport_active)
+               R_TimeReport("visibility");
+
        Matrix4x4_CreateTranslate(&r_waterscrollmatrix, sin(r_refdef.time) * 0.025 * r_waterscroll.value, sin(r_refdef.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
 
        if (cl.csqc_vidvars.drawworld)
@@ -2773,14 +3120,14 @@ void R_RenderScene(void)
                        if (r_timereport_active)
                                R_TimeReport("showlocs");
                }
-       
+
                if (r_drawportals.integer)
                {
                        R_DrawPortals();
                        if (r_timereport_active)
                                R_TimeReport("portals");
                }
-       
+
                if (r_showbboxes.value > 0)
                {
                        R_DrawEntityBBoxes();
@@ -2887,7 +3234,7 @@ static void R_DrawEntityBBoxes_Callback(const entity_render_t *ent, const rtligh
                color[3] *= r_showbboxes.value;
                color[3] = bound(0, color[3], 1);
                GL_DepthTest(!r_showdisabledepthtest.integer);
-               GL_CullFace(GL_BACK);
+               GL_CullFace(r_view.cullface_front);
                R_DrawBBoxMesh(edict->priv.server->areamins, edict->priv.server->areamaxs, color[0], color[1], color[2], color[3]);
        }
        SV_VM_End();
@@ -2972,7 +3319,7 @@ void R_DrawNoModel_TransparentCallback(const entity_render_t *ent, const rtlight
        GL_DepthRange(0, (ent->flags & RENDER_VIEWMODEL) ? 0.0625 : 1);
        GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
        GL_DepthTest(!(ent->effects & EF_NODEPTHTEST));
-       GL_CullFace((ent->effects & EF_DOUBLESIDED) ? GL_NONE : GL_FRONT); // quake is backwards, this culls back faces
+       GL_CullFace((ent->effects & EF_DOUBLESIDED) ? GL_NONE : r_view.cullface_back);
        R_Mesh_VertexPointer(nomodelvertex3f, 0, 0);
        if (r_refdef.fogenabled)
        {
@@ -3060,10 +3407,10 @@ void R_DrawSprite(int blendfunc1, int blendfunc2, rtexture_t *texture, rtexture_
        {
                scalex1 = -scalex1;
                scalex2 = -scalex2;
-               GL_CullFace(GL_BACK);
+               GL_CullFace(r_view.cullface_front);
        }
        else
-               GL_CullFace(GL_FRONT);
+               GL_CullFace(r_view.cullface_back);
 
        GL_DepthMask(false);
        GL_DepthRange(0, depthshort ? 0.0625 : 1);
@@ -3329,7 +3676,12 @@ void R_UpdateTextureInfo(const entity_render_t *ent, texture_t *t)
        t->currentmaterialflags = t->basematerialflags;
        t->currentalpha = ent->alpha;
        if (t->basematerialflags & MATERIALFLAG_WATERALPHA && (model->brush.supportwateralpha || r_novis.integer))
+       {
                t->currentalpha *= r_wateralpha.value;
+               // if rendering refraction/reflection, disable transparency
+               if (r_waterstate.enabled && (t->currentalpha < 1 || (t->currentmaterialflags & MATERIALFLAG_ALPHA)))
+                       t->currentmaterialflags |= MATERIALFLAG_WATERSHADER;
+       }
        if (!(ent->flags & RENDER_LIGHT))
                t->currentmaterialflags |= MATERIALFLAG_FULLBRIGHT;
        if (ent->effects & EF_ADDITIVE)
@@ -3344,6 +3696,8 @@ void R_UpdateTextureInfo(const entity_render_t *ent, texture_t *t)
                t->currentmaterialflags |= MATERIALFLAG_SHORTDEPTHRANGE;
        if (t->backgroundnumskinframes && !(t->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED))
                t->currentmaterialflags |= MATERIALFLAG_VERTEXTEXTUREBLEND;
+       if (t->currentmaterialflags & MATERIALFLAG_WATERSHADER)
+               t->currentmaterialflags &= ~(MATERIALFLAG_ADD | MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_CUSTOMBLEND);
 
        for (i = 0, tcmod = t->tcmods;i < Q3MAXTCMODS && (tcmod->tcmod || i < 1);i++, tcmod++)
        {
@@ -3413,8 +3767,8 @@ void R_UpdateTextureInfo(const entity_render_t *ent, texture_t *t)
                {
                        if (r_shadow_glossintensity.value > 0)
                        {
-                               t->glosstexture = t->currentskinframe->gloss ? t->currentskinframe->gloss : r_texture_black;
-                               t->backgroundglosstexture = (t->backgroundcurrentskinframe && t->backgroundcurrentskinframe->gloss) ? t->backgroundcurrentskinframe->gloss : r_texture_black;
+                               t->glosstexture = t->currentskinframe->gloss ? t->currentskinframe->gloss : r_texture_white;
+                               t->backgroundglosstexture = (t->backgroundcurrentskinframe && t->backgroundcurrentskinframe->gloss) ? t->backgroundcurrentskinframe->gloss : r_texture_white;
                                t->specularscale = r_shadow_glossintensity.value;
                        }
                }
@@ -4244,6 +4598,59 @@ void RSurf_DrawBatch_Simple(int texturenumsurfaces, msurface_t **texturesurfacel
        }
 }
 
+static void RSurf_DrawBatch_WithLightmapSwitching_WithWaterTextureSwitching(int texturenumsurfaces, msurface_t **texturesurfacelist, int lightmaptexunit, int deluxemaptexunit, int refractiontexunit, int reflectiontexunit)
+{
+       int i, planeindex, vertexindex;
+       float d, bestd;
+       vec3_t vert;
+       const float *v;
+       r_waterstate_waterplane_t *p, *bestp;
+       msurface_t *surface;
+       if (r_waterstate.renderingscene)
+               return;
+       for (i = 0;i < texturenumsurfaces;i++)
+       {
+               surface = texturesurfacelist[i];
+               if (lightmaptexunit >= 0)
+                       R_Mesh_TexBind(lightmaptexunit, R_GetTexture(surface->lightmaptexture));
+               if (deluxemaptexunit >= 0)
+                       R_Mesh_TexBind(deluxemaptexunit, R_GetTexture(surface->deluxemaptexture));
+               // pick the closest matching water plane
+               bestd = 0;
+               bestp = NULL;
+               for (planeindex = 0, p = r_waterstate.waterplanes;planeindex < r_waterstate.numwaterplanes;planeindex++, p++)
+               {
+                       d = 0;
+                       for (vertexindex = 0, v = rsurface.modelvertex3f + surface->num_firstvertex * 3;vertexindex < surface->num_vertices;vertexindex++, v += 3)
+                       {
+                               Matrix4x4_Transform(&rsurface.matrix, v, vert);
+                               d += fabs(PlaneDiff(vert, &p->plane));
+                       }
+                       if (bestd > d || !bestp)
+                       {
+                               bestd = d;
+                               bestp = p;
+                       }
+               }
+               if (bestp)
+               {
+                       if (refractiontexunit >= 0)
+                               R_Mesh_TexBind(refractiontexunit, R_GetTexture(bestp->texture_refraction));
+                       if (reflectiontexunit >= 0)
+                               R_Mesh_TexBind(reflectiontexunit, R_GetTexture(bestp->texture_reflection));
+               }
+               else
+               {
+                       if (refractiontexunit >= 0)
+                               R_Mesh_TexBind(refractiontexunit, R_GetTexture(r_texture_black));
+                       if (reflectiontexunit >= 0)
+                               R_Mesh_TexBind(reflectiontexunit, R_GetTexture(r_texture_black));
+               }
+               GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
+               R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (rsurface.modelelement3i + 3 * surface->num_firsttriangle), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * surface->num_firsttriangle));
+       }
+}
+
 static void RSurf_DrawBatch_WithLightmapSwitching(int texturenumsurfaces, msurface_t **texturesurfacelist, int lightmaptexunit, int deluxemaptexunit)
 {
        int i;
@@ -4601,7 +5008,7 @@ static void R_DrawTextureSurfaceList_ShowSurfaces(int texturenumsurfaces, msurfa
        GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
        GL_PolygonOffset(rsurface.texture->currentpolygonfactor, rsurface.texture->currentpolygonoffset);
        GL_DepthTest(!(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST));
-       GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : GL_FRONT); // quake is backwards, this culls back faces
+       GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_view.cullface_back);
        if (rsurface.mode != RSURFMODE_SHOWSURFACES)
        {
                rsurface.mode = RSURFMODE_SHOWSURFACES;
@@ -4637,7 +5044,7 @@ static void R_DrawTextureSurfaceList_Sky(int texturenumsurfaces, msurface_t **te
        GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
        GL_PolygonOffset(rsurface.texture->currentpolygonfactor, rsurface.texture->currentpolygonoffset);
        GL_DepthTest(!(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST));
-       GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : GL_FRONT); // quake is backwards, this culls back faces
+       GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_view.cullface_back);
        GL_DepthMask(true);
        // LordHavoc: HalfLife maps have freaky skypolys so don't use
        // skymasking on them, and Quake3 never did sky masking (unlike
@@ -4716,9 +5123,19 @@ static void R_DrawTextureSurfaceList_GL20(int texturenumsurfaces, msurface_t **t
        }
 
        if (rsurface.uselightmaptexture && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT))
-               RSurf_DrawBatch_WithLightmapSwitching(texturenumsurfaces, texturesurfacelist, 7, r_glsl_permutation->loc_Texture_Deluxemap >= 0 ? 8 : -1);
+       {
+               if (rsurface.texture->currentmaterialflags & MATERIALFLAG_WATERSHADER)
+                       RSurf_DrawBatch_WithLightmapSwitching_WithWaterTextureSwitching(texturenumsurfaces, texturesurfacelist, 7, r_glsl_permutation->loc_Texture_Deluxemap >= 0 ? 8 : -1, r_glsl_permutation->loc_Texture_Refraction >= 0 ? 11 : -1, r_glsl_permutation->loc_Texture_Reflection >= 0 ? 12 : -1);
+               else
+                       RSurf_DrawBatch_WithLightmapSwitching(texturenumsurfaces, texturesurfacelist, 7, r_glsl_permutation->loc_Texture_Deluxemap >= 0 ? 8 : -1);
+       }
        else
-               RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
+       {
+               if (rsurface.texture->currentmaterialflags & MATERIALFLAG_WATERSHADER)
+                       RSurf_DrawBatch_WithLightmapSwitching_WithWaterTextureSwitching(texturenumsurfaces, texturesurfacelist, -1, -1, r_glsl_permutation->loc_Texture_Refraction >= 0 ? 11 : -1, r_glsl_permutation->loc_Texture_Reflection >= 0 ? 12 : -1);
+               else
+                       RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
+       }
        if (rsurface.texture->backgroundnumskinframes && !(rsurface.texture->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED))
        {
        }
@@ -4987,6 +5404,8 @@ static void R_DrawTextureSurfaceList(int texturenumsurfaces, msurface_t **textur
        {
                if ((rsurface.texture->currentmaterialflags & (MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_BLENDED | MATERIALFLAG_ALPHATEST)))
                        return;
+               if (r_waterstate.renderingscene && (rsurface.texture->currentmaterialflags & MATERIALFLAG_WATERSHADER))
+                       return;
                if (rsurface.mode != RSURFMODE_MULTIPASS)
                        rsurface.mode = RSURFMODE_MULTIPASS;
                if (r_depthfirst.integer == 3)
@@ -5004,7 +5423,7 @@ static void R_DrawTextureSurfaceList(int texturenumsurfaces, msurface_t **textur
                }
                GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
                GL_PolygonOffset(rsurface.texture->currentpolygonfactor, rsurface.texture->currentpolygonoffset);
-               GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : GL_FRONT); // quake is backwards, this culls back faces
+               GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_view.cullface_back);
                GL_DepthTest(true);
                GL_BlendFunc(GL_ONE, GL_ZERO);
                GL_DepthMask(true);
@@ -5030,7 +5449,7 @@ static void R_DrawTextureSurfaceList(int texturenumsurfaces, msurface_t **textur
                GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
                GL_PolygonOffset(rsurface.texture->currentpolygonfactor, rsurface.texture->currentpolygonoffset);
                GL_DepthTest(true);
-               GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : GL_FRONT); // quake is backwards, this culls back faces
+               GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_view.cullface_back);
                GL_BlendFunc(GL_ONE, GL_ZERO);
                GL_DepthMask(writedepth);
                GL_Color(1,1,1,1);
@@ -5048,7 +5467,7 @@ static void R_DrawTextureSurfaceList(int texturenumsurfaces, msurface_t **textur
                        rsurface.mode = RSURFMODE_MULTIPASS;
                GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
                GL_DepthTest(true);
-               GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : GL_FRONT); // quake is backwards, this culls back faces
+               GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_view.cullface_back);
                GL_BlendFunc(GL_ONE, GL_ZERO);
                GL_DepthMask(writedepth);
                GL_Color(1,1,1,1);
@@ -5084,7 +5503,7 @@ static void R_DrawTextureSurfaceList(int texturenumsurfaces, msurface_t **textur
                GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
                GL_PolygonOffset(rsurface.texture->currentpolygonfactor, rsurface.texture->currentpolygonoffset);
                GL_DepthTest(!(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST));
-               GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : GL_FRONT); // quake is backwards, this culls back faces
+               GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_view.cullface_back);
                GL_BlendFunc(rsurface.texture->currentlayers[0].blendfunc1, rsurface.texture->currentlayers[0].blendfunc2);
                GL_DepthMask(writedepth && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_BLENDED));
                GL_AlphaTest((rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST) != 0);
@@ -5146,11 +5565,19 @@ static void R_DrawSurface_TransparentCallback(const entity_render_t *ent, const
        RSurf_CleanUp();
 }
 
-void R_QueueSurfaceList(entity_render_t *ent, int numsurfaces, msurface_t **surfacelist, int flagsmask, qboolean writedepth, qboolean depthonly)
+void R_QueueSurfaceList(entity_render_t *ent, int numsurfaces, msurface_t **surfacelist, int flagsmask, qboolean writedepth, qboolean depthonly, qboolean addwaterplanes)
 {
        int i, j;
        vec3_t tempcenter, center;
        texture_t *texture;
+       // if we're rendering water textures (extra scene renders), use a separate loop to avoid burdening the main one
+       if (addwaterplanes)
+       {
+               for (i = 0;i < numsurfaces;i++)
+                       if (surfacelist[i]->texture->currentframe->currentmaterialflags & MATERIALFLAG_WATERSHADER)
+                               R_Water_AddWaterPlane(surfacelist[i]);
+               return;
+       }
        // break the surface list down into batches by texture and use of lightmapping
        for (i = 0;i < numsurfaces;i = j)
        {
@@ -5379,7 +5806,7 @@ void R_DrawTrianglesAndNormals(entity_render_t *ent, qboolean drawtris, qboolean
 }
 
 extern void R_BuildLightMap(const entity_render_t *ent, msurface_t *surface);
-void R_DrawWorldSurfaces(qboolean skysurfaces, qboolean writedepth, qboolean depthonly)
+void R_DrawWorldSurfaces(qboolean skysurfaces, qboolean writedepth, qboolean depthonly, qboolean addwaterplanes)
 {
        int i, j, endj, f, flagsmask;
        int counttriangles = 0;
@@ -5395,7 +5822,7 @@ void R_DrawWorldSurfaces(qboolean skysurfaces, qboolean writedepth, qboolean dep
        RSurf_ActiveWorldEntity();
 
        // update light styles
-       if (!skysurfaces && !depthonly && model->brushq1.light_styleupdatechains)
+       if (!skysurfaces && !depthonly && !addwaterplanes && model->brushq1.light_styleupdatechains)
        {
                for (i = 0;i < model->brushq1.light_styles;i++)
                {
@@ -5410,7 +5837,7 @@ void R_DrawWorldSurfaces(qboolean skysurfaces, qboolean writedepth, qboolean dep
        }
 
        R_UpdateAllTextureInfo(r_refdef.worldentity);
-       flagsmask = skysurfaces ? MATERIALFLAG_SKY : (MATERIALFLAG_WATER | MATERIALFLAG_WALL);
+       flagsmask = addwaterplanes ? MATERIALFLAG_WATERSHADER : (skysurfaces ? MATERIALFLAG_SKY : (MATERIALFLAG_WATER | MATERIALFLAG_WALL));
        f = 0;
        t = NULL;
        rsurface.uselightmaptexture = false;
@@ -5439,25 +5866,28 @@ void R_DrawWorldSurfaces(qboolean skysurfaces, qboolean writedepth, qboolean dep
                                counttriangles += surface->num_triangles;
                                if (numsurfacelist >= maxsurfacelist)
                                {
-                                       R_QueueSurfaceList(r_refdef.worldentity, numsurfacelist, surfacelist, flagsmask, writedepth, depthonly);
+                                       R_QueueSurfaceList(r_refdef.worldentity, numsurfacelist, surfacelist, flagsmask, writedepth, depthonly, addwaterplanes);
                                        numsurfacelist = 0;
                                }
                        }
                }
        }
        if (numsurfacelist)
-               R_QueueSurfaceList(r_refdef.worldentity, numsurfacelist, surfacelist, flagsmask, writedepth, depthonly);
+               R_QueueSurfaceList(r_refdef.worldentity, numsurfacelist, surfacelist, flagsmask, writedepth, depthonly, addwaterplanes);
        r_refdef.stats.entities_triangles += counttriangles;
        RSurf_CleanUp();
 
-       if (r_showcollisionbrushes.integer && r_view.showdebug && !skysurfaces && !depthonly)
-               R_DrawCollisionBrushes(r_refdef.worldentity);
+       if (r_view.showdebug)
+       {
+               if (r_showcollisionbrushes.integer && !skysurfaces && !addwaterplanes && !depthonly)
+                       R_DrawCollisionBrushes(r_refdef.worldentity);
 
-       if ((r_showtris.integer || r_shownormals.integer) && r_view.showdebug && !depthonly)
-               R_DrawTrianglesAndNormals(r_refdef.worldentity, r_showtris.integer, r_shownormals.integer, flagsmask);
+               if ((r_showtris.integer || r_shownormals.integer) && !addwaterplanes && !depthonly)
+                       R_DrawTrianglesAndNormals(r_refdef.worldentity, r_showtris.integer, r_shownormals.integer, flagsmask);
+       }
 }
 
-void R_DrawModelSurfaces(entity_render_t *ent, qboolean skysurfaces, qboolean writedepth, qboolean depthonly)
+void R_DrawModelSurfaces(entity_render_t *ent, qboolean skysurfaces, qboolean writedepth, qboolean depthonly, qboolean addwaterplanes)
 {
        int i, f, flagsmask;
        int counttriangles = 0;
@@ -5481,7 +5911,7 @@ void R_DrawModelSurfaces(entity_render_t *ent, qboolean skysurfaces, qboolean wr
                RSurf_ActiveModelEntity(ent, true, r_glsl.integer && gl_support_fragment_shader && !depthonly);
 
        // update light styles
-       if (!skysurfaces && !depthonly && model->brushq1.light_styleupdatechains)
+       if (!skysurfaces && !depthonly && !addwaterplanes && model->brushq1.light_styleupdatechains)
        {
                for (i = 0;i < model->brushq1.light_styles;i++)
                {
@@ -5496,7 +5926,7 @@ void R_DrawModelSurfaces(entity_render_t *ent, qboolean skysurfaces, qboolean wr
        }
 
        R_UpdateAllTextureInfo(ent);
-       flagsmask = skysurfaces ? MATERIALFLAG_SKY : (MATERIALFLAG_WATER | MATERIALFLAG_WALL);
+       flagsmask = addwaterplanes ? MATERIALFLAG_WATERSHADER : (skysurfaces ? MATERIALFLAG_SKY : (MATERIALFLAG_WATER | MATERIALFLAG_WALL));
        f = 0;
        t = NULL;
        rsurface.uselightmaptexture = false;
@@ -5517,22 +5947,22 @@ void R_DrawModelSurfaces(entity_render_t *ent, qboolean skysurfaces, qboolean wr
                        counttriangles += surface->num_triangles;
                        if (numsurfacelist >= maxsurfacelist)
                        {
-                               R_QueueSurfaceList(ent, numsurfacelist, surfacelist, flagsmask, writedepth, depthonly);
+                               R_QueueSurfaceList(ent, numsurfacelist, surfacelist, flagsmask, writedepth, depthonly, addwaterplanes);
                                numsurfacelist = 0;
                        }
                }
        }
        if (numsurfacelist)
-               R_QueueSurfaceList(ent, numsurfacelist, surfacelist, flagsmask, writedepth, depthonly);
+               R_QueueSurfaceList(ent, numsurfacelist, surfacelist, flagsmask, writedepth, depthonly, addwaterplanes);
        r_refdef.stats.entities_triangles += counttriangles;
        RSurf_CleanUp();
 
        if (r_view.showdebug)
        {
-               if (r_showcollisionbrushes.integer && !skysurfaces && !depthonly)
+               if (r_showcollisionbrushes.integer && !skysurfaces && !addwaterplanes && !depthonly)
                        R_DrawCollisionBrushes(ent);
 
-               if ((r_showtris.integer || r_shownormals.integer) && !depthonly)
+               if ((r_showtris.integer || r_shownormals.integer) && !addwaterplanes && !depthonly)
                        R_DrawTrianglesAndNormals(ent, r_showtris.integer, r_shownormals.integer, flagsmask);
        }
 }
index 1dd4c27..cdd4392 100644 (file)
@@ -404,7 +404,7 @@ void R_DrawPortals(void)
        }
 }
 
-void R_View_WorldVisibility(void)
+void R_View_WorldVisibility(qboolean forcenovis)
 {
        int i, j, *mark;
        mleaf_t *leaf;
@@ -430,7 +430,7 @@ void R_View_WorldVisibility(void)
 
                // if floating around in the void (no pvs data available, and no
                // portals available), simply use all on-screen leafs.
-               if (!viewleaf || viewleaf->clusterindex < 0)
+               if (!viewleaf || viewleaf->clusterindex < 0 || forcenovis)
                {
                        // no visibility method: (used when floating around in the void)
                        // simply cull each leaf to the frustum (view pyramid)
@@ -524,9 +524,20 @@ void R_Q1BSP_DrawSky(entity_render_t *ent)
        if (ent->model == NULL)
                return;
        if (ent == r_refdef.worldentity)
-               R_DrawWorldSurfaces(true, true, false);
+               R_DrawWorldSurfaces(true, true, false, false);
        else
-               R_DrawModelSurfaces(ent, true, true, false);
+               R_DrawModelSurfaces(ent, true, true, false, false);
+}
+
+void R_Q1BSP_DrawAddWaterPlanes(entity_render_t *ent)
+{
+       model_t *model = ent->model;
+       if (model == NULL)
+               return;
+       if (ent == r_refdef.worldentity)
+               R_DrawWorldSurfaces(false, false, false, true);
+       else
+               R_DrawModelSurfaces(ent, false, false, false, true);
 }
 
 void R_Q1BSP_Draw(entity_render_t *ent)
@@ -535,9 +546,9 @@ void R_Q1BSP_Draw(entity_render_t *ent)
        if (model == NULL)
                return;
        if (ent == r_refdef.worldentity)
-               R_DrawWorldSurfaces(false, true, false);
+               R_DrawWorldSurfaces(false, true, false, false);
        else
-               R_DrawModelSurfaces(ent, false, true, false);
+               R_DrawModelSurfaces(ent, false, true, false, false);
 }
 
 void R_Q1BSP_DrawDepth(entity_render_t *ent)
@@ -546,9 +557,9 @@ void R_Q1BSP_DrawDepth(entity_render_t *ent)
        if (model == NULL)
                return;
        if (ent == r_refdef.worldentity)
-               R_DrawWorldSurfaces(false, false, true);
+               R_DrawWorldSurfaces(false, false, true, false);
        else
-               R_DrawModelSurfaces(ent, false, false, true);
+               R_DrawModelSurfaces(ent, false, false, true, false);
 }
 
 typedef struct r_q1bsp_getlightinfo_s
index 93f67e9..5cd68ab 100644 (file)
--- a/glquake.h
+++ b/glquake.h
@@ -250,6 +250,13 @@ extern int gl_max_anisotropy;
 
 #define GL_POLYGON_STIPPLE                0x0B42
 
+#define GL_CLIP_PLANE0                    0x3000
+#define GL_CLIP_PLANE1                    0x3001
+#define GL_CLIP_PLANE2                    0x3002
+#define GL_CLIP_PLANE3                    0x3003
+#define GL_CLIP_PLANE4                    0x3004
+#define GL_CLIP_PLANE5                    0x3005
+
 #endif
 
 extern int gl_max_texture_size;
@@ -589,6 +596,9 @@ extern void (GLAPIENTRY *qglCopyTexSubImage2D)(GLenum target, GLint level, GLint
 extern void (GLAPIENTRY *qglPolygonOffset)(GLfloat factor, GLfloat units);
 extern void (GLAPIENTRY *qglPolygonMode)(GLenum face, GLenum mode);
 
+extern void (GLAPIENTRY *qglClipPlane)(GLenum plane, const GLdouble *equation);
+extern void (GLAPIENTRY *qglGetClipPlane)(GLenum plane, GLdouble *equation);
+
 //[515]: added on 29.07.2005
 extern void (GLAPIENTRY *qglLineWidth)(GLfloat width);
 extern void (GLAPIENTRY *qglPointSize)(GLfloat size);
index 7e4783e..5f5ea10 100644 (file)
@@ -365,6 +365,35 @@ void Matrix4x4_Normalize (matrix4x4_t *out, matrix4x4_t *in1)
        Matrix4x4_Scale(out, scale, 1);
 }
 
+void Matrix4x4_Reflect (matrix4x4_t *out, double normalx, double normaly, double normalz, double dist, double axisscale)
+{
+       int i;
+       double d;
+       double p[4], p2[4];
+       p[0] = normalx;
+       p[1] = normaly;
+       p[2] = normalz;
+       p[3] = -dist;
+       p2[0] = p[0] * axisscale;
+       p2[1] = p[1] * axisscale;
+       p2[2] = p[2] * axisscale;
+       p2[3] = 0;
+       for (i = 0;i < 4;i++)
+       {
+#ifdef MATRIX4x4_OPENGLORIENTATION
+               d = out->m[i][0] * p[0] + out->m[i][1] * p[1] + out->m[i][2] * p[2] + out->m[i][3] * p[3];
+               out->m[i][0] += p2[0] * d;
+               out->m[i][1] += p2[1] * d;
+               out->m[i][2] += p2[2] * d;
+#else
+               d = out->m[0][i] * p[0] + out->m[1][i] * p[1] + out->m[2][i] * p[2] + out->m[3][i] * p[3];
+               out->m[0][i] += p2[0] * d;
+               out->m[1][i] += p2[1] * d;
+               out->m[2][i] += p2[2] * d;
+#endif
+       }
+}
+
 void Matrix4x4_CreateIdentity (matrix4x4_t *out)
 {
        out->m[0][0]=1.0f;
index f510f3d..480dec1 100644 (file)
@@ -42,6 +42,9 @@ void Matrix4x4_Invert_Simple (matrix4x4_t *out, const matrix4x4_t *in1);
 // creates a matrix that does the same rotation and translation as the matrix
 // provided, but no uniform scaling, does not support scale3 matrices
 void Matrix4x4_Normalize (matrix4x4_t *out, matrix4x4_t *in1);
+// modifies a matrix to have all vectors and origin reflected across the plane
+// to the opposite side (at least if axisscale is -2)
+void Matrix4x4_Reflect (matrix4x4_t *out, double normalx, double normaly, double normalz, double dist, double axisscale);
 
 // creates an identity matrix
 // (a matrix which does nothing)
index ec808b7..c53867e 100644 (file)
@@ -787,6 +787,7 @@ void Mod_IDP0_Load(model_t *mod, void *buffer, void *bufferend)
 
        loadmodel->type = mod_alias;
        loadmodel->DrawSky = NULL;
+       loadmodel->DrawAddWaterPlanes = NULL;
        loadmodel->Draw = R_Q1BSP_Draw;
        loadmodel->DrawDepth = R_Q1BSP_DrawDepth;
        loadmodel->CompileShadowVolume = R_Q1BSP_CompileShadowVolume;
@@ -1106,6 +1107,7 @@ void Mod_IDP2_Load(model_t *mod, void *buffer, void *bufferend)
        
        loadmodel->type = mod_alias;
        loadmodel->DrawSky = NULL;
+       loadmodel->DrawAddWaterPlanes = NULL;
        loadmodel->Draw = R_Q1BSP_Draw;
        loadmodel->DrawDepth = R_Q1BSP_DrawDepth;
        loadmodel->CompileShadowVolume = R_Q1BSP_CompileShadowVolume;
@@ -1341,6 +1343,7 @@ void Mod_IDP3_Load(model_t *mod, void *buffer, void *bufferend)
 
        loadmodel->type = mod_alias;
        loadmodel->DrawSky = NULL;
+       loadmodel->DrawAddWaterPlanes = NULL;
        loadmodel->Draw = R_Q1BSP_Draw;
        loadmodel->DrawDepth = R_Q1BSP_DrawDepth;
        loadmodel->CompileShadowVolume = R_Q1BSP_CompileShadowVolume;
@@ -1545,6 +1548,7 @@ void Mod_ZYMOTICMODEL_Load(model_t *mod, void *buffer, void *bufferend)
        }
 
        loadmodel->DrawSky = NULL;
+       loadmodel->DrawAddWaterPlanes = NULL;
        loadmodel->Draw = R_Q1BSP_Draw;
        loadmodel->DrawDepth = R_Q1BSP_DrawDepth;
        loadmodel->CompileShadowVolume = R_Q1BSP_CompileShadowVolume;
@@ -1840,6 +1844,7 @@ void Mod_DARKPLACESMODEL_Load(model_t *mod, void *buffer, void *bufferend)
        }
 
        loadmodel->DrawSky = NULL;
+       loadmodel->DrawAddWaterPlanes = NULL;
        loadmodel->Draw = R_Q1BSP_Draw;
        loadmodel->DrawDepth = R_Q1BSP_DrawDepth;
        loadmodel->CompileShadowVolume = R_Q1BSP_CompileShadowVolume;
@@ -2111,6 +2116,7 @@ void Mod_PSKMODEL_Load(model_t *mod, void *buffer, void *bufferend)
        
        loadmodel->type = mod_alias;
        loadmodel->DrawSky = NULL;
+       loadmodel->DrawAddWaterPlanes = NULL;
        loadmodel->Draw = R_Q1BSP_Draw;
        loadmodel->DrawDepth = R_Q1BSP_DrawDepth;
        loadmodel->CompileShadowVolume = R_Q1BSP_CompileShadowVolume;
index e1accd9..6c776b3 100644 (file)
@@ -3595,8 +3595,9 @@ void Mod_Q1BSP_Load(model_t *mod, void *buffer, void *bufferend)
                for (j = 0;j < mod->nummodelsurfaces;j++)
                        mod->surfacelist[j] = mod->firstmodelsurface + j;
 
-               // this gets altered below if sky is used
+               // this gets altered below if sky or water is used
                mod->DrawSky = NULL;
+               mod->DrawAddWaterPlanes = NULL;
                mod->Draw = R_Q1BSP_Draw;
                mod->DrawDepth = R_Q1BSP_DrawDepth;
                mod->GetLightInfo = R_Q1BSP_GetLightInfo;
@@ -3628,6 +3629,8 @@ void Mod_Q1BSP_Load(model_t *mod, void *buffer, void *bufferend)
                                // we only need to have a drawsky function if it is used(usually only on world model)
                                if (surface->texture->basematerialflags & MATERIALFLAG_SKY)
                                        mod->DrawSky = R_Q1BSP_DrawSky;
+                               if (surface->texture->basematerialflags & MATERIALFLAG_WATERALPHA)
+                                       mod->DrawAddWaterPlanes = R_Q1BSP_DrawAddWaterPlanes;
                                // calculate bounding shapes
                                for (k = 0, vec = (loadmodel->surfmesh.data_vertex3f + 3 * surface->num_firstvertex);k < surface->num_vertices;k++, vec += 3)
                                {
@@ -5589,6 +5592,7 @@ void Mod_Q3BSP_Load(model_t *mod, void *buffer, void *bufferend)
        mod->CompileShadowVolume = R_Q1BSP_CompileShadowVolume;
        mod->DrawShadowVolume = R_Q1BSP_DrawShadowVolume;
        mod->DrawLight = R_Q1BSP_DrawLight;
+       mod->DrawAddWaterPlanes = NULL;
 
        mod_base = (unsigned char *)header;
 
@@ -5720,12 +5724,20 @@ void Mod_Q3BSP_Load(model_t *mod, void *buffer, void *bufferend)
                mod->radius2 = modelradius * modelradius;
 
                for (j = 0;j < mod->nummodelsurfaces;j++)
-                       if (mod->data_surfaces[j + mod->firstmodelsurface].texture->surfaceflags & Q3SURFACEFLAG_SKY)
+                       if (mod->data_surfaces[j + mod->firstmodelsurface].texture->basematerialflags & MATERIALFLAG_SKY)
                                break;
                if (j < mod->nummodelsurfaces)
                        mod->DrawSky = R_Q1BSP_DrawSky;
                else
                        mod->DrawSky = NULL;
+
+               for (j = 0;j < mod->nummodelsurfaces;j++)
+                       if (mod->data_surfaces[j + mod->firstmodelsurface].texture->basematerialflags & MATERIALFLAG_WATERALPHA)
+                               break;
+               if (j < mod->nummodelsurfaces)
+                       mod->DrawAddWaterPlanes = R_Q1BSP_DrawAddWaterPlanes;
+               else
+                       mod->DrawAddWaterPlanes = NULL;
        }
 }
 
index a401906..980a6ba 100644 (file)
@@ -98,6 +98,8 @@ mplane_t;
 #define MATERIALFLAG_NOCULLFACE 65536
 // render with a very short depth range (like 10% of normal), this causes entities to appear infront of most of the scene
 #define MATERIALFLAG_SHORTDEPTHRANGE 131072
+// render refraction and reflection (note: this is always opaque, the shader does the alpha effect)
+#define MATERIALFLAG_WATERSHADER 262144
 // combined mask of all attributes that require depth sorted rendering
 #define MATERIALFLAGMASK_DEPTHSORTED (MATERIALFLAG_BLENDED | MATERIALFLAG_NODEPTHTEST)
 
index 36580fd..f179f72 100644 (file)
@@ -820,6 +820,8 @@ typedef struct model_s
        const char              *modeldatatypestring;
        // draw the model's sky polygons (only used by brush models)
        void(*DrawSky)(struct entity_render_s *ent);
+       // draw refraction/reflection textures for the model's water polygons (only used by brush models)
+       void(*DrawAddWaterPlanes)(struct entity_render_s *ent);
        // draw the model using lightmap/dlight shading
        void(*Draw)(struct entity_render_s *ent);
        // draw the model to the depth buffer (no color rendering at all)
@@ -928,6 +930,7 @@ int Mod_Q1BSP_SuperContentsFromNativeContents(struct model_s *model, int nativec
 
 // a lot of model formats use the Q1BSP code, so here are the prototypes...
 struct entity_render_s;
+void R_Q1BSP_DrawAddWaterPlanes(struct entity_render_s *ent);
 void R_Q1BSP_DrawSky(struct entity_render_s *ent);
 void R_Q1BSP_Draw(struct entity_render_s *ent);
 void R_Q1BSP_DrawDepth(struct entity_render_s *ent);
index 94b5b37..8fec8a6 100644 (file)
@@ -237,6 +237,7 @@ void Mod_IDSP_Load(model_t *mod, void *buffer, void *bufferend)
        loadmodel->CompileShadowVolume = NULL;
        loadmodel->DrawShadowVolume = NULL;
        loadmodel->DrawLight = NULL;
+       loadmodel->DrawAddWaterPlanes = NULL;
 
        version = LittleLong(((dsprite_t *)buffer)->version);
        if (version == SPRITE_VERSION || version == SPRITE32_VERSION)
@@ -355,6 +356,7 @@ void Mod_IDS2_Load(model_t *mod, void *buffer, void *bufferend)
        loadmodel->CompileShadowVolume = NULL;
        loadmodel->DrawShadowVolume = NULL;
        loadmodel->DrawLight = NULL;
+       loadmodel->DrawAddWaterPlanes = NULL;
 
        pinqsprite = (dsprite2_t *)buffer;
 
index 48ce2ef..9f5888d 100644 (file)
@@ -190,7 +190,7 @@ static void R_DrawExplosion_TransparentCallback(const entity_render_t *ent, cons
        GL_DepthRange(0, 1);
        GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
        GL_DepthTest(true);
-       GL_CullFace(GL_FRONT); // quake is backwards, this culls back faces
+       GL_CullFace(r_view.cullface_back);
        R_Mesh_Matrix(&identitymatrix);
 
        R_Mesh_ColorPointer(NULL, 0, 0);
index afdd9bb..1ea3a7c 100644 (file)
@@ -895,11 +895,11 @@ void R_Shadow_RenderVolume(int numvertices, int numtriangles, const float *verte
        if (r_shadow_rendermode == R_SHADOW_RENDERMODE_STENCIL)
        {
                // decrement stencil if backface is behind depthbuffer
-               GL_CullFace(GL_BACK); // quake is backwards, this culls front faces
+               GL_CullFace(r_view.cullface_front);
                qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR
                R_Mesh_Draw(0, numvertices, numtriangles, element3i, 0, 0);
                // increment stencil if frontface is behind depthbuffer
-               GL_CullFace(GL_FRONT); // quake is backwards, this culls back faces
+               GL_CullFace(r_view.cullface_back);
                qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);CHECKGLERROR
        }
        R_Mesh_Draw(0, numvertices, numtriangles, element3i, 0, 0);
@@ -1033,7 +1033,7 @@ void R_Shadow_RenderMode_Reset(void)
        qglStencilMask(~0);CHECKGLERROR
        qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);CHECKGLERROR
        qglStencilFunc(GL_ALWAYS, 128, ~0);CHECKGLERROR
-       GL_CullFace(GL_FRONT); // quake is backwards, this culls back faces
+       GL_CullFace(r_view.cullface_back);
        GL_Color(1, 1, 1, 1);
        GL_ColorMask(r_view.colormask[0], r_view.colormask[1], r_view.colormask[2], 1);
        GL_BlendFunc(GL_ONE, GL_ZERO);
@@ -1051,17 +1051,17 @@ void R_Shadow_RenderMode_StencilShadowVolumes(qboolean clearstencil)
        if (r_shadow_rendermode == R_SHADOW_RENDERMODE_SEPARATESTENCIL)
        {
                GL_CullFace(GL_NONE);
-               qglStencilOpSeparate(GL_BACK, GL_KEEP, GL_INCR, GL_KEEP);CHECKGLERROR // quake is backwards, this is front faces
-               qglStencilOpSeparate(GL_FRONT, GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR // quake is backwards, this is back faces
+               qglStencilOpSeparate(r_view.cullface_front, GL_KEEP, GL_INCR, GL_KEEP);CHECKGLERROR
+               qglStencilOpSeparate(r_view.cullface_back, GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR
        }
        else if (r_shadow_rendermode == R_SHADOW_RENDERMODE_STENCILTWOSIDE)
        {
                GL_CullFace(GL_NONE);
                qglEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
-               qglActiveStencilFaceEXT(GL_BACK);CHECKGLERROR // quake is backwards, this is front faces
+               qglActiveStencilFaceEXT(r_view.cullface_front);CHECKGLERROR
                qglStencilMask(~0);CHECKGLERROR
                qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);CHECKGLERROR
-               qglActiveStencilFaceEXT(GL_FRONT);CHECKGLERROR // quake is backwards, this is back faces
+               qglActiveStencilFaceEXT(r_view.cullface_back);CHECKGLERROR
                qglStencilMask(~0);CHECKGLERROR
                qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR
        }
@@ -2244,7 +2244,7 @@ void R_Shadow_RenderLighting(int firstvertex, int numvertices, int numtriangles,
        GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
        GL_PolygonOffset(rsurface.texture->currentpolygonfactor, rsurface.texture->currentpolygonoffset);
        GL_DepthTest(!(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST));
-       GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : GL_FRONT); // quake is backwards, this culls back faces
+       GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_view.cullface_back);
        nmap = rsurface.texture->currentskinframe->nmap;
        if (gl_lightmaps.integer)
                nmap = r_texture_blanknormalmap;
@@ -2660,11 +2660,11 @@ void R_Shadow_DrawWorldShadow(int numsurfaces, int *surfacelist, const unsigned
                        if (r_shadow_rendermode == R_SHADOW_RENDERMODE_STENCIL)
                        {
                                // decrement stencil if backface is behind depthbuffer
-                               GL_CullFace(GL_BACK); // quake is backwards, this culls front faces
+                               GL_CullFace(r_view.cullface_front);
                                qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR
                                R_Mesh_Draw(0, mesh->numverts, mesh->numtriangles, mesh->element3i, mesh->ebo, 0);
                                // increment stencil if frontface is behind depthbuffer
-                               GL_CullFace(GL_FRONT); // quake is backwards, this culls back faces
+                               GL_CullFace(r_view.cullface_back);
                                qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);CHECKGLERROR
                        }
                        R_Mesh_Draw(0, mesh->numverts, mesh->numtriangles, mesh->element3i, mesh->ebo, 0);
@@ -3061,7 +3061,7 @@ void R_ShadowVolumeLighting(qboolean visible)
        R_Shadow_RenderMode_End();
 }
 
-extern void R_SetupView(const matrix4x4_t *matrix);
+extern void R_SetupView(void);
 extern cvar_t r_shadows_throwdistance;
 void R_DrawModelShadows(void)
 {
@@ -3143,7 +3143,7 @@ void R_DrawModelShadows(void)
        R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
 
        // restoring the perspective view is done by R_RenderScene
-       //R_SetupView(&r_view.matrix);
+       //R_SetupView();
 
        // restore other state to normal
        R_Shadow_RenderMode_End();
index 889ed64..a275000 100644 (file)
--- a/render.h
+++ b/render.h
@@ -136,7 +136,7 @@ skinframe_t *R_SkinFrame_LoadExternal(const char *name, int textureflags, qboole
 skinframe_t *R_SkinFrame_LoadInternal(const char *name, int textureflags, int loadpantsandshirt, int loadglowtexture, const unsigned char *skindata, int width, int height, int bitsperpixel, const unsigned int *palette, const unsigned int *alphapalette);
 skinframe_t *R_SkinFrame_LoadMissing(void);
 
-void R_View_WorldVisibility();
+void R_View_WorldVisibility(qboolean forcenovis);
 void R_DrawParticles(void);
 void R_DrawExplosions(void);
 
@@ -353,8 +353,8 @@ struct msurface_s;
 void R_UpdateTextureInfo(const entity_render_t *ent, texture_t *t);
 void R_UpdateAllTextureInfo(entity_render_t *ent);
 void R_QueueTextureSurfaceList(int texturenumsurfaces, msurface_t **texturesurfacelist);
-void R_DrawWorldSurfaces(qboolean skysurfaces, qboolean writedepth, qboolean depthonly);
-void R_DrawModelSurfaces(entity_render_t *ent, qboolean skysurfaces, qboolean writedepth, qboolean depthonly);
+void R_DrawWorldSurfaces(qboolean skysurfaces, qboolean writedepth, qboolean depthonly, qboolean addwaterplanes);
+void R_DrawModelSurfaces(entity_render_t *ent, qboolean skysurfaces, qboolean writedepth, qboolean depthonly, qboolean addwaterplanes);
 
 void RSurf_PrepareVerticesForBatch(qboolean generatenormals, qboolean generatetangents, int texturenumsurfaces, msurface_t **texturesurfacelist);
 void RSurf_DrawBatch_Simple(int texturenumsurfaces, msurface_t **texturesurfacelist);
@@ -363,17 +363,18 @@ void RSurf_DrawBatch_Simple(int texturenumsurfaces, msurface_t **texturesurfacel
 #define SHADERPERMUTATION_MODE_LIGHTDIRECTIONMAP_MODELSPACE (1<<1) // (lightmap) use directional pixel shading from texture containing modelspace light directions (deluxemap)
 #define SHADERPERMUTATION_MODE_LIGHTDIRECTIONMAP_TANGENTSPACE (1<<2) // (lightmap) use directional pixel shading from texture containing tangentspace light directions (deluxemap)
 #define SHADERPERMUTATION_MODE_LIGHTDIRECTION (1<<3) // (lightmap) use directional pixel shading from fixed light direction (q3bsp)
-#define SHADERPERMUTATION_GLOW (1<<4) // (lightmap) blend in an additive glow texture
-#define SHADERPERMUTATION_FOG (1<<5) // tint the color by fog color or black if using additive blend mode
-#define SHADERPERMUTATION_COLORMAPPING (1<<6) // indicates this is a colormapped skin
-#define SHADERPERMUTATION_DIFFUSE (1<<7) // (lightsource) whether to use directional shading
-#define SHADERPERMUTATION_CONTRASTBOOST (1<<8) // r_glsl_contrastboost boosts the contrast at low color levels (similar to gamma)
-#define SHADERPERMUTATION_SPECULAR (1<<9) // (lightsource or deluxemapping) render specular effects
-#define SHADERPERMUTATION_CUBEFILTER (1<<10) // (lightsource) use cubemap light filter
-#define SHADERPERMUTATION_OFFSETMAPPING (1<<11) // adjust texcoords to roughly simulate a displacement mapped surface
-#define SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING (1<<12) // adjust texcoords to accurately simulate a displacement mapped surface (requires OFFSETMAPPING to also be set!)
-
-#define SHADERPERMUTATION_MAX (1<<13) // how many permutations are possible
+#define SHADERPERMUTATION_USEWATER (1<<4) // add a blend of screen-space reflection and refraction textures to the fragment color (with perturbed texture fetches)
+#define SHADERPERMUTATION_GLOW (1<<5) // (lightmap) blend in an additive glow texture
+#define SHADERPERMUTATION_FOG (1<<6) // tint the color by fog color or black if using additive blend mode
+#define SHADERPERMUTATION_COLORMAPPING (1<<7) // indicates this is a colormapped skin
+#define SHADERPERMUTATION_DIFFUSE (1<<8) // (lightsource) whether to use directional shading
+#define SHADERPERMUTATION_CONTRASTBOOST (1<<9) // r_glsl_contrastboost boosts the contrast at low color levels (similar to gamma)
+#define SHADERPERMUTATION_SPECULAR (1<<10) // (lightsource or deluxemapping) render specular effects
+#define SHADERPERMUTATION_CUBEFILTER (1<<11) // (lightsource) use cubemap light filter
+#define SHADERPERMUTATION_OFFSETMAPPING (1<<12) // adjust texcoords to roughly simulate a displacement mapped surface
+#define SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING (1<<13) // adjust texcoords to accurately simulate a displacement mapped surface (requires OFFSETMAPPING to also be set!)
+
+#define SHADERPERMUTATION_MAX (1<<14) // how many permutations are possible
 #define SHADERPERMUTATION_MASK (SHADERPERMUTATION_MAX - 1) // mask of valid indexing bits for r_glsl_permutations[] array
 
 // these are additional flags used only by R_GLSL_CompilePermutation
@@ -398,6 +399,8 @@ typedef struct r_glsl_permutation_s
        int loc_Texture_Lightmap;
        int loc_Texture_Deluxemap;
        int loc_Texture_Glow;
+       int loc_Texture_Refraction;
+       int loc_Texture_Reflection;
        int loc_FogColor;
        int loc_LightPosition;
        int loc_EyePosition;
@@ -417,6 +420,11 @@ typedef struct r_glsl_permutation_s
        int loc_SpecularColor;
        int loc_LightDir;
        int loc_ContrastBoostCoeff; // 1 - 1/ContrastBoost
+       int loc_DistortScaleRefractReflect;
+       int loc_ScreenScaleRefractReflect;
+       int loc_ScreenCenterRefractReflect;
+       int loc_RefractColor;
+       int loc_ReflectColor;
 }
 r_glsl_permutation_t;
 
diff --git a/todo b/todo
index 58607ab..7a016f3 100644 (file)
--- a/todo
+++ b/todo
@@ -1,14 +1,15 @@
 - todo: difficulty ratings are: 0 = trivial, 1 = easy, 2 = easy-moderate, 3 = moderate, 4 = moderate-hard, 5 = hard, 6 = hard++, 7 = nightmare, d = done, -d = done but have not notified the people who asked for it, f = failed, -f = failed but have not notified the people who asked for it
 -d (Amish, Fuh) feature darkplaces client: add qw protocol support (making darkplaces work as a qwcl client) (Amish, Fuh)
+-d (Stribbs, Jimmy Freestone) feature darkplaces renderer: add HalfLife2 style water rendering (Mitchell, Stribbs, Jimmy Freestone)
 0 bug darkplaces client qw: add .mvd demo support
 0 bug darkplaces client qw: add .qwd demo support
+0 bug darkplaces client qw: add spectator cvar (Plague Monkey Rat)
+0 bug darkplaces client qw: add spectator prediction
+0 bug darkplaces client qw: cl_netinputpacketspersecond 20 is too low for QW physics to behave properly, add a separate cl_netinputpacketspersecond_qw cvar which defaults to 72 (Plague Monkey Rat)
 0 bug darkplaces client qw: inactive player entities are showing up at 0 0 0 (Plague Monkey Rat)
 0 bug darkplaces client qw: qw skins should only be active on progs/player.mdl (Plague Monkey Rat)
 0 bug darkplaces client qw: qw/skins/*.pcx need to be cropped to 296x194 and loaded as internal textures so they are split into multiple layers (Plague Monkey Rat)
 0 bug darkplaces client qw: restrict wateralpha and such cvars according to what is permitted in qw serverinfo?
-0 bug darkplaces client qw: cl_netinputpacketspersecond 20 is too low for QW physics to behave properly, add a separate cl_netinputpacketspersecond_qw cvar which defaults to 72 (Plague Monkey Rat)
-0 bug darkplaces client qw: add spectator cvar (Plague Monkey Rat)
-0 bug darkplaces client qw: add spectator prediction
 0 bug darkplaces client: can't move mouse around in nexuiz menu if vid_mouse is 0
 0 bug darkplaces client: if you press 1 during the demo loop when quake starts, escape doesn't do anything until you hit some other key (daemon)
 0 bug darkplaces loader: crash when a mdl model has more replacement skins than the model contains (Lardarse)
@@ -18,6 +19,7 @@
 0 bug darkplaces memory: memstats doesn't account for memory used by VBO/EBO buffers in models
 0 bug darkplaces qc FRIK_FILE: when opening a file for writing that already has the data/ prefix in its path, it should not add another data/ prefix (daemon)
 0 bug darkplaces readme: it would be a very good idea to add documentation of sv_gameplayfix_* cvars in the readme as a means to run broken mods (xaGe)
+0 bug darkplaces readme: readme says that q3 shaders are not supported, this is not true, describe the working features in detail (qqshka)
 0 bug darkplaces renderer: GL13 path has broken handling of unlit surfaces in Nexuiz toxic.bsp - the small red light surfaces are black in GL13 path (m0rfar)
 0 bug darkplaces renderer: if an animated model has transparent surfaces, each one calls RSurf_ActiveModelEntity, recomputing all vertices
 0 bug darkplaces renderer: if an animated model is entirely transparent, the RSurf_ActiveModelEntity call updating vertices is completely wasted
 0 optimization darkplaces loader: remove the loop unrolling in Image_Resample32LerpLine and Image_Resample24LerpLine and related functions, as the loop is really too long to get much benefit, and it might even not fit in the L1 instruction cache on Pentium1 (fuh)
 0 optimization darkplaces particles: make pt_rain spawning do a single downward trace and set a timer for the impact, to reduce collision load (LordHavoc)
 0 optimization darkplaces renderer: add r_null.c so that dedicated server doesn't need renderer
-0 optimization darkplaces renderer: change water distortion textures from multiple 2D textures to one 3D texture for smoother animation (Tomaz)
 0 optimization darkplaces renderer: store p->past->clusterindex into p->clusterindex to speed up R_WorldVisibility, also store p->past - data_leafs index into p->leafindex, and move the CullBox check to last (Vic)
 0 optimization darkplaces renderer: support GL_ATI_separate_stencil since ATI does not support GL_EXT_stencil_two_side and GL_ATI_separate_stencil was integrated in OpenGL2.0 (romi)
 0 optimization darkplaces server: implement first unused/last used entity range optimization on entity spawn/remove similar to the client particles (LordHavoc)
 5 feature dpzoo.map: make some things that make the player bigger/smaller to demonstrate scaling and better viewheight handling and brush collisions (depends on brush collisions)
 5 feature hmap2: fix tjunctions on leaky maps
 6 feature darkplaces client: add a "edit" command that can edit text files (I.E. .qc and progs.src) using a dialog window (allow multiple), and then add a "frikqcc" command to run it on the current mod (if it's in the command path at least)
-6 feature darkplaces renderer: add HalfLife2 style water rendering (Mitchell, Stribbs, Jimmy Freestone)
-6 feature darkplaces renderer: add cubemap reflections like UT2003 somehow (perhaps entities would define the reflection maps for rooms, and a water entity would take care of the rest?) (TEU, Nexuiz)
 6 feature darkplaces renderer: figure out an LOD scheme for really large outdoor environments (Uffe)
 7 feature darkplaces protocol,renderer: add DP_EF_REFRACT which turns an entity into a refraction effect similar to doom3 shockwave/heathaze (Supajoe)
 7 feature darkplaces renderer: make it work on Savage4 again (Ender)
@@ -1368,8 +1367,10 @@ f darkplaces: should add quake3 shader support even though the language is utter
 f dpmod: figure out why the dbsg isn't selectable in deathmatch 7 mode
 f dpmod: make tarbabies have a self.resist_explosive = 3; like zombies (Sajt)
 f feature darkplaces client: add back cl_particles_lighting cvar and add back the particle lighting (romi)
+f feature darkplaces renderer: add cubemap reflections like UT2003 somehow (perhaps entities would define the reflection maps for rooms, and a water entity would take care of the rest?)
 f feature darkplaces server: add an extension to check if a file exists outside the data directory, FRIK_FILE can do this but only inside data directory (Error)
 f feature dpmod: include .lit and .dlit files for all id1 maps - this idea was rejected due to download size
 f feature dpmod: include .vis files for all id1 maps - this idea rejected due to lack of .vis support and download size
 f hqbsp: CreateBrushFaces should use RadiusFromBounds for its rotation box code, but hmap is obsolete (Vic)
+f optimization darkplaces renderer: change water distortion textures from multiple 2D textures to one 3D texture for smoother animation (Tomaz)
 f optimization darkplaces visibility: R_Q3BSP_RecursiveWorldNode should take clipflags parameter and do not cull a node against a plane if the parent node is totally on one side of the plane (Vic)
\ No newline at end of file
index fda1757..c96fd0c 100644 (file)
@@ -256,6 +256,9 @@ void (GLAPIENTRY *qglPolygonOffset)(GLfloat factor, GLfloat units);
 void (GLAPIENTRY *qglPolygonMode)(GLenum face, GLenum mode);
 void (GLAPIENTRY *qglPolygonStipple)(const GLubyte *mask);
 
+void (GLAPIENTRY *qglClipPlane)(GLenum plane, const GLdouble *equation);
+void (GLAPIENTRY *qglGetClipPlane)(GLenum plane, GLdouble *equation);
+
 //[515]: added on 29.07.2005
 void (GLAPIENTRY *qglLineWidth)(GLfloat width);
 void (GLAPIENTRY *qglPointSize)(GLfloat size);
@@ -507,6 +510,8 @@ static dllfunction_t opengl110funcs[] =
        {"glPolygonOffset", (void **) &qglPolygonOffset},
        {"glPolygonMode", (void **) &qglPolygonMode},
        {"glPolygonStipple", (void **) &qglPolygonStipple},
+       {"glClipPlane", (void **) &qglClipPlane},
+       {"glGetClipPlane", (void **) &qglGetClipPlane},
        {NULL, NULL}
 };