implemented fog height setting, along with fade depth setting - global
authorhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Fri, 6 Nov 2009 13:08:39 +0000 (13:08 +0000)
committerhavoc <havoc@d7cf8633-e32d-0410-b094-e92efae38249>
Fri, 6 Nov 2009 13:08:39 +0000 (13:08 +0000)
fog now has a ceiling and a gradient fade to prevent harsh artifacts at
the transition line
reworked sky rendering to accept fog by using the surface renderer
reworked sprite rendering to use the surface renderer
reworked corona rendering to use the surface renderer
reworked fogging a bit on nomodel entities
eliminated R_DrawSprite

git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@9437 d7cf8633-e32d-0410-b094-e92efae38249

15 files changed:
cl_main.c
cl_parse.c
cl_particles.c
client.h
gl_draw.c
gl_rmain.c
model_brush.c
model_brush.h
model_shared.c
model_shared.h
r_lightning.c
r_shadow.c
r_sky.c
r_sprites.c
render.h

index 997bdb7..8f11bad 100644 (file)
--- a/cl_main.c
+++ b/cl_main.c
@@ -1878,7 +1878,7 @@ static void CL_Fog_f (void)
 {
        if (Cmd_Argc () == 1)
        {
-               Con_Printf("\"fog\" is \"%f %f %f %f %f %f %f\"\n", r_refdef.fog_density, r_refdef.fog_red, r_refdef.fog_green, r_refdef.fog_blue, r_refdef.fog_alpha, r_refdef.fog_start, r_refdef.fog_end);
+               Con_Printf("\"fog\" is \"%f %f %f %f %f %f %f %f %f\"\n", r_refdef.fog_density, r_refdef.fog_red, r_refdef.fog_green, r_refdef.fog_blue, r_refdef.fog_alpha, r_refdef.fog_start, r_refdef.fog_end, r_refdef.fog_height, r_refdef.fog_fadedepth);
                return;
        }
        r_refdef.fog_start = 0;
@@ -1898,6 +1898,10 @@ static void CL_Fog_f (void)
                r_refdef.fog_start = atof(Cmd_Argv(6));
        if(Cmd_Argc() > 7)
                r_refdef.fog_end = atof(Cmd_Argv(7));
+       if(Cmd_Argc() > 8)
+               r_refdef.fog_height = atof(Cmd_Argv(8));
+       if(Cmd_Argc() > 9)
+               r_refdef.fog_fadedepth = atof(Cmd_Argv(9));
 }
 
 /*
index 7f6e752..e41b20f 100644 (file)
@@ -391,10 +391,12 @@ void CL_ParseEntityLump(char *entdata)
                        r_refdef.fog_start = 0;
                        r_refdef.fog_alpha = 1;
                        r_refdef.fog_end = 16384;
+                       r_refdef.fog_height = 1<<30;
+                       r_refdef.fog_fadedepth = 128;
 #if _MSC_VER >= 1400
 #define sscanf sscanf_s
 #endif
-                       sscanf(value, "%f %f %f %f %f %f %f", &r_refdef.fog_density, &r_refdef.fog_red, &r_refdef.fog_green, &r_refdef.fog_blue, &r_refdef.fog_alpha, &r_refdef.fog_start, &r_refdef.fog_end);
+                       sscanf(value, "%f %f %f %f %f %f %f %f %f", &r_refdef.fog_density, &r_refdef.fog_red, &r_refdef.fog_green, &r_refdef.fog_blue, &r_refdef.fog_alpha, &r_refdef.fog_start, &r_refdef.fog_end, &r_refdef.fog_height, &r_refdef.fog_fadedepth);
                }
                else if (!strcmp("fog_density", key))
                        r_refdef.fog_density = atof(value);
@@ -410,6 +412,10 @@ void CL_ParseEntityLump(char *entdata)
                        r_refdef.fog_start = atof(value);
                else if (!strcmp("fog_end", key))
                        r_refdef.fog_end = atof(value);
+               else if (!strcmp("fog_height", key))
+                       r_refdef.fog_height = atof(value);
+               else if (!strcmp("fog_fadedepth", key))
+                       r_refdef.fog_fadedepth = atof(value);
        }
 }
 
index af7b725..89ac780 100644 (file)
@@ -2133,12 +2133,10 @@ void R_DrawDecal_TransparentCallback(const entity_render_t *ent, const rtlight_t
        float alphascale = (1.0f / 65536.0f) * cl_particles_alpha.value * r_refdef.view.colorscale;
        float particle_vertex3f[BATCHSIZE*12], particle_texcoord2f[BATCHSIZE*8], particle_color4f[BATCHSIZE*16];
 
-       // set up global fogging in worldspace (RSurf_FogVertex depends on this)
-       VectorCopy(r_refdef.view.origin, rsurface.localvieworigin);
+       RSurf_ActiveWorldEntity();
        rsurface.fograngerecip = r_refdef.fograngerecip;
 
        r_refdef.stats.decals += numsurfaces;
-       R_Mesh_Matrix(&identitymatrix);
        R_Mesh_ResetTextureState();
        R_Mesh_VertexPointer(particle_vertex3f, 0, 0);
        R_Mesh_TexCoordPointer(0, 2, particle_texcoord2f, 0, 0);
@@ -2288,14 +2286,12 @@ void R_DrawParticle_TransparentCallback(const entity_render_t *ent, const rtligh
        vec4_t colormultiplier;
        float particle_vertex3f[BATCHSIZE*12], particle_texcoord2f[BATCHSIZE*8], particle_color4f[BATCHSIZE*16];
 
-       // set up global fogging in worldspace (RSurf_FogVertex depends on this)
-       VectorCopy(r_refdef.view.origin, rsurface.localvieworigin);
+       RSurf_ActiveWorldEntity();
        rsurface.fograngerecip = r_refdef.fograngerecip;
 
        Vector4Set(colormultiplier, r_refdef.view.colorscale * (1.0 / 256.0f), r_refdef.view.colorscale * (1.0 / 256.0f), r_refdef.view.colorscale * (1.0 / 256.0f), cl_particles_alpha.value * (1.0 / 256.0f));
 
        r_refdef.stats.particles += numsurfaces;
-       R_Mesh_Matrix(&identitymatrix);
        R_Mesh_ResetTextureState();
        R_Mesh_VertexPointer(particle_vertex3f, 0, 0);
        R_Mesh_TexCoordPointer(0, 2, particle_texcoord2f, 0, 0);
index c91c314..43c33ef 100644 (file)
--- a/client.h
+++ b/client.h
@@ -1619,6 +1619,10 @@ typedef struct r_refdef_s
 
        r_refdef_scene_t scene;
 
+       float fogplane[4];
+       float fogplaneviewdist;
+       qboolean fogplaneviewabove;
+       float fogheightfade;
        float fogcolor[3];
        float fogrange;
        float fograngerecip;
@@ -1633,6 +1637,8 @@ typedef struct r_refdef_s
        float fog_alpha;
        float fog_start;
        float fog_end;
+       float fog_height;
+       float fog_fadedepth;
        qboolean fogenabled;
        qboolean oldgl_fogenable;
 
index e75a97a..f0a2c07 100644 (file)
--- a/gl_draw.c
+++ b/gl_draw.c
@@ -272,120 +272,6 @@ static const embeddedpic_t embeddedpics[] =
        ".3............3."
        "................"
        },
-       {
-       "gfx/editlights/cursor", 16, 16,
-       "................"
-       ".3............3."
-       "..5...2332...5.."
-       "...7.3....3.7..."
-       "....7......7...."
-       "...3.7....7.3..."
-       "..2...7..7...2.."
-       "..3..........3.."
-       "..3..........3.."
-       "..2...7..7...2.."
-       "...3.7....7.3..."
-       "....7......7...."
-       "...7.3....3.7..."
-       "..5...2332...5.."
-       ".3............3."
-       "................"
-       },
-       {
-       "gfx/editlights/light", 16, 16,
-       "................"
-       "................"
-       "......1111......"
-       "....11233211...."
-       "...1234554321..."
-       "...1356776531..."
-       "..124677776421.."
-       "..135777777531.."
-       "..135777777531.."
-       "..124677776421.."
-       "...1356776531..."
-       "...1234554321..."
-       "....11233211...."
-       "......1111......"
-       "................"
-       "................"
-       },
-       {
-       "gfx/editlights/noshadow", 16, 16,
-       "................"
-       "................"
-       "......1111......"
-       "....11233211...."
-       "...1234554321..."
-       "...1356226531..."
-       "..12462..26421.."
-       "..1352....2531.."
-       "..1352....2531.."
-       "..12462..26421.."
-       "...1356226531..."
-       "...1234554321..."
-       "....11233211...."
-       "......1111......"
-       "................"
-       "................"
-       },
-       {
-       "gfx/editlights/selection", 16, 16,
-       "................"
-       ".777752..257777."
-       ".742........247."
-       ".72..........27."
-       ".7............7."
-       ".5............5."
-       ".2............2."
-       "................"
-       "................"
-       ".2............2."
-       ".5............5."
-       ".7............7."
-       ".72..........27."
-       ".742........247."
-       ".777752..257777."
-       "................"
-       },
-       {
-       "gfx/editlights/cubemaplight", 16, 16,
-       "................"
-       "................"
-       "......2772......"
-       "....27755772...."
-       "..277533335772.."
-       "..753333333357.."
-       "..777533335777.."
-       "..735775577537.."
-       "..733357753337.."
-       "..733337733337.."
-       "..753337733357.."
-       "..277537735772.."
-       "....27777772...."
-       "......2772......"
-       "................"
-       "................"
-       },
-       {
-       "gfx/editlights/cubemapnoshadowlight", 16, 16,
-       "................"
-       "................"
-       "......2772......"
-       "....27722772...."
-       "..2772....2772.."
-       "..72........27.."
-       "..7772....2777.."
-       "..7.27722772.7.."
-       "..7...2772...7.."
-       "..7....77....7.."
-       "..72...77...27.."
-       "..2772.77.2772.."
-       "....27777772...."
-       "......2772......"
-       "................"
-       "................"
-       },
        {NULL, 0, 0, NULL}
 };
 
index 2a876cd..28242cc 100644 (file)
@@ -263,6 +263,8 @@ void FOG_clear(void)
        r_refdef.fog_alpha = 1;
        r_refdef.fog_start = 0;
        r_refdef.fog_end = 0;
+       r_refdef.fog_height = 1<<30;
+       r_refdef.fog_fadedepth = 128;
 }
 
 static void R_BuildBlankTextures(void)
@@ -446,7 +448,7 @@ static const char *builtinshaderstring =
 "// written by Forest 'LordHavoc' Hale\n"
 "\n"
 "// enable various extensions depending on permutation:\n"
-"\n" 
+"\n"
 "#ifdef USESHADOWMAPRECT\n"
 "# extension GL_ARB_texture_rectangle : enable\n"
 "#endif\n"
@@ -486,6 +488,14 @@ static const char *builtinshaderstring =
 "# define myhalf4 vec4\n"
 "//#endif\n"
 "\n"
+"#ifdef USEFOGINSIDE\n"
+"# define USEFOG\n"
+"#else\n"
+"# ifdef USEFOGOUTSIDE\n"
+"#  define USEFOG\n"
+"# endif\n"
+"#endif\n"
+"\n"
 "#ifdef MODE_DEPTH_OR_SHADOW\n"
 "\n"
 "# ifdef VERTEX_SHADER\n"
@@ -631,6 +641,36 @@ static const char *builtinshaderstring =
 "# endif\n"
 "\n"
 "#else // !MODE_GENERIC\n"
+"#ifdef MODE_BLOOMBLUR\n"
+"# ifdef VERTEX_SHADER\n"
+"void main(void)\n"
+"{\n"
+"      gl_FrontColor = gl_Color;\n"
+"      gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;\n"
+"      gl_Position = ftransform();\n"
+"}\n"
+"# endif\n"
+"# ifdef FRAGMENT_SHADER\n"
+"\n"
+"uniform sampler2D Texture_First;\n"
+"uniform vec4 BloomBlur_Parameters;\n"
+"\n"
+"void main(void)\n"
+"{\n"
+"      int i;\n"
+"      vec2 tc = gl_TexCoord[0].xy;\n"
+"      vec3 color = texture2D(Texture_First, tc).rgb;\n"
+"      tc += BloomBlur_Parameters.xy;\n"
+"      for (i = 1;i < SAMPLES;i++)\n"
+"      {\n"
+"              color += texture2D(Texture_First, tc).rgb;\n"
+"              tc += BloomBlur_Parameters.xy;\n"
+"      }\n"
+"      gl_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1);\n"
+"}\n"
+"# endif\n"
+"\n"
+"#else // !MODE_BLOOMBLUR\n"
 "\n"
 "varying vec2 TexCoord;\n"
 "#ifdef USEVERTEXTEXTUREBLEND\n"
@@ -652,6 +692,7 @@ static const char *builtinshaderstring =
 "varying vec3 EyeVector;\n"
 "#ifdef USEFOG\n"
 "varying vec3 EyeVectorModelSpace;\n"
+"varying float FogPlaneVertexDist;\n"
 "#endif\n"
 "\n"
 "varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent)\n"
@@ -678,6 +719,7 @@ static const char *builtinshaderstring =
 "uniform vec3 LightPosition;\n"
 "uniform vec3 EyePosition;\n"
 "uniform vec3 LightDir;\n"
+"uniform vec4 FogPlane;\n"
 "\n"
 "// TODO: get rid of tangentt (texcoord2) and use a crossproduct to regenerate it from tangents (texcoord1) and normal (texcoord3), this would require sending a 4 component texcoord1 with W as 1 or -1 according to which side the texcoord2 should be on\n"
 "\n"
@@ -724,6 +766,10 @@ static const char *builtinshaderstring =
 "      EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz);\n"
 "      EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz);\n"
 "\n"
+"#ifdef USEFOG\n"
+"      FogPlaneVertexDist = dot(FogPlane, gl_Vertex);\n"
+"#endif\n"
+"\n"
 "#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
 "      VectorS = gl_MultiTexCoord1.xyz;\n"
 "      VectorT = gl_MultiTexCoord2.xyz;\n"
@@ -852,6 +898,8 @@ static const char *builtinshaderstring =
 "uniform float OffsetMapping_Scale;\n"
 "uniform float OffsetMapping_Bias;\n"
 "uniform float FogRangeRecip;\n"
+"uniform float FogPlaneViewDist;\n"
+"uniform float FogHeightFade;\n"
 "\n"
 "uniform myhalf AmbientScale;\n"
 "uniform myhalf DiffuseScale;\n"
@@ -1058,7 +1106,7 @@ static const char *builtinshaderstring =
 "#      else\n"
 "#        define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r  \n"
 "#      endif\n"
-"#      if USESHADOWMAPPCF > 1\n"       
+"#      if USESHADOWMAPPCF > 1\n"
 "    vec2 center = shadowmaptc.xy - 0.5, offset = fract(center);\n"
 "    center *= ShadowMap_TextureScale;\n"
 "    vec4 row1 = step(shadowmaptc.z, vec4(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0), texval( 2.0, -1.0)));\n"
@@ -1074,7 +1122,7 @@ static const char *builtinshaderstring =
 "    vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0)));\n"
 "    vec3 cols = row2 + mix(row1, row3, offset.y);\n"
 "    f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25));\n"
-"#      endif\n"      
+"#      endif\n"
 "#     endif\n"
 "#    else\n"
 "    f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r);\n"
@@ -1378,7 +1426,39 @@ static const char *builtinshaderstring =
 "\n"
 "      // apply fog after Contrastboost/SceneBrightness because its color is already modified appropriately\n"
 "#ifdef USEFOG\n"
-"      color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0))));\n"
+"      float fogfrac;\n"
+"#ifdef USEFOGOUTSIDE\n"
+"      fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade);\n"
+"#else\n"
+"      fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade);\n"
+"#endif\n"
+"//    float FogHeightFade1 = -0.5/1024.0;\n"
+"//    if (FogPlaneViewDist >= 0.0)\n"
+"//            fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade1);\n"
+"//    else\n"
+"//            fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade1);\n"
+"//# ifdef USEFOGABOVE\n"
+"//    if (FogPlaneViewDist >= 0.0)\n"
+"//            fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist);\n"
+"//    else\n"
+"//            fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist));\n"
+"//    fogfrac *= min(1.0, (min(0.0, FogPlaneVertexDist) + min(0.0, FogPlaneViewDist))*FogHeightFade1);\n"
+"//    fogfrac *= min(1.0, (max(0.0, fade*FogPlaneVertexDist) + max(0.0, fade*FogPlaneViewDist)));\n"
+"//    fogfrac *= min(1.0, (max(0.0, FogHeightFade1*FogPlaneVertexDist) + max(0.0, FogHeightFade1*FogPlaneViewDist)));\n"
+"//    fogfrac *= min(1.0, (min(0.0, FogPlaneVertexDist) + min(0.0, FogPlaneViewDist))*FogHeightFade1);\n"
+"\n"
+"      //fogfrac *= min(1.0, max(0.0, (max(-2048, min(0, FogPlaneVertexDist)) + max(-2048, min(0, FogPlaneViewDist)))/-2048.0));\n"
+"      //float fade = -0.5/128.0;\n"
+"      //fogfrac *= max(0.0, min(1.0, fade*FogPlaneVertexDist)) + max(0.0, min(1.0, fade*FogPlaneViewDist));\n"
+"      //fogfrac *= max(0.0, min(1.0, FogHeightFade1*FogPlaneVertexDist)) + max(0.0, min(1.0, FogHeightFade1*FogPlaneViewDist));\n"
+"      //fogfrac *= min(1.0, max(0.0, FogHeightFade1*FogPlaneVertexDist)) + min(1.0, max(0.0, FogHeightFade1*FogPlaneViewDist));\n"
+"      //fogfrac *= min(1.0, max(0.0, FogHeightFade1*FogPlaneVertexDist) + max(0.0, FogHeightFade1*FogPlaneViewDist));\n"
+"      //fogfrac *= min(1.0, min(1.0, max(0.0, FogHeightFade1*FogPlaneVertexDist)) + min(1.0, max(0.0, FogHeightFade1*FogPlaneViewDist)));\n"
+"      //fogfrac *= min(1.0, max(0.0, FogHeightFade1*FogPlaneVertexDist) + max(0.0, FogHeightFade1*FogPlaneViewDist));\n"
+"      //fogfrac *= min(1.0, (min(0.0, FogPlaneVertexDist) + min(0.0, FogPlaneViewDist)) * FogHeightFade1);\n"
+"      //fogfrac *= min(1.0, (min(0.0, FogPlaneVertexDist) + min(0.0, FogPlaneViewDist)) * FogHeightFade1);\n"
+"//# endif\n"
+"      color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0))));\n"
 "#endif\n"
 "\n"
 "      // reflection must come last because it already contains exactly the correct fog (the reflection render preserves camera distance from the plane, it only flips the side) and ContrastBoost/SceneBrightness\n"
@@ -1432,6 +1512,7 @@ static const char *builtinshaderstring =
 "\n"
 "#endif // FRAGMENT_SHADER\n"
 "\n"
+"#endif // !MODE_BLOOMBLUR\n"
 "#endif // !MODE_GENERIC\n"
 "#endif // !MODE_POSTPROCESS\n"
 "#endif // !MODE_SHOWDEPTH\n"
@@ -1459,29 +1540,30 @@ typedef enum shaderpermutation_e
 {
        SHADERPERMUTATION_DIFFUSE = 1<<0, ///< (lightsource) whether to use directional shading
        SHADERPERMUTATION_VERTEXTEXTUREBLEND = 1<<1, ///< indicates this is a two-layer material blend based on vertex alpha (q3bsp)
-       SHADERPERMUTATION_VIEWTINT = 1<<1, ///< view tint (postprocessing only)
-       SHADERPERMUTATION_COLORMAPPING = 1<<2, ///< indicates this is a colormapped skin
-       SHADERPERMUTATION_SATURATION = 1<<2, ///< saturation (postprocessing only)
-       SHADERPERMUTATION_FOG = 1<<3, ///< tint the color by fog color or black if using additive blend mode
-       SHADERPERMUTATION_GAMMARAMPS = 1<<3, ///< gamma (postprocessing only)
-       SHADERPERMUTATION_CUBEFILTER = 1<<4, ///< (lightsource) use cubemap light filter
-       SHADERPERMUTATION_GLOW = 1<<5, ///< (lightmap) blend in an additive glow texture
-       SHADERPERMUTATION_BLOOM = 1<<5, ///< bloom (postprocessing only)
-       SHADERPERMUTATION_SPECULAR = 1<<6, ///< (lightsource or deluxemapping) render specular effects
-       SHADERPERMUTATION_POSTPROCESSING = 1<<6, ///< user defined postprocessing (postprocessing only)
-       SHADERPERMUTATION_EXACTSPECULARMATH = 1<<7, ///< (lightsource or deluxemapping) use exact reflection map for specular effects, as opposed to the usual OpenGL approximation
-       SHADERPERMUTATION_REFLECTION = 1<<8, ///< normalmap-perturbed reflection of the scene infront of the surface, preformed as an overlay on the surface
-       SHADERPERMUTATION_OFFSETMAPPING = 1<<9, ///< adjust texcoords to roughly simulate a displacement mapped surface
-       SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING = 1<<10, ///< adjust texcoords to accurately simulate a displacement mapped surface (requires OFFSETMAPPING to also be set!)
-       SHADERPERMUTATION_SHADOWMAPRECT = 1<<11, ///< (lightsource) use shadowmap rectangle texture as light filter
-       SHADERPERMUTATION_SHADOWMAPCUBE = 1<<12, ///< (lightsource) use shadowmap cubemap texture as light filter
-       SHADERPERMUTATION_SHADOWMAP2D = 1<<13, ///< (lightsource) use shadowmap rectangle texture as light filter
-       SHADERPERMUTATION_SHADOWMAPPCF = 1<<14, ///< (lightsource) use percentage closer filtering on shadowmap test results
-       SHADERPERMUTATION_SHADOWMAPPCF2 = 1<<15, ///< (lightsource) use higher quality percentage closer filtering on shadowmap test results
-       SHADERPERMUTATION_SHADOWSAMPLER = 1<<16, ///< (lightsource) use hardware shadowmap test
-       SHADERPERMUTATION_SHADOWMAPVSDCT = 1<<17, ///< (lightsource) use virtual shadow depth cube texture for shadowmap indexing
-       SHADERPERMUTATION_LIMIT = 1<<18, ///< size of permutations array
-       SHADERPERMUTATION_COUNT = 18 ///< size of shaderpermutationinfo array
+       SHADERPERMUTATION_VIEWTINT = 1<<2, ///< view tint (postprocessing only)
+       SHADERPERMUTATION_COLORMAPPING = 1<<3, ///< indicates this is a colormapped skin
+       SHADERPERMUTATION_SATURATION = 1<<4, ///< saturation (postprocessing only)
+       SHADERPERMUTATION_FOGINSIDE = 1<<5, ///< tint the color by fog color or black if using additive blend mode
+       SHADERPERMUTATION_FOGOUTSIDE = 1<<6, ///< tint the color by fog color or black if using additive blend mode
+       SHADERPERMUTATION_GAMMARAMPS = 1<<7, ///< gamma (postprocessing only)
+       SHADERPERMUTATION_CUBEFILTER = 1<<8, ///< (lightsource) use cubemap light filter
+       SHADERPERMUTATION_GLOW = 1<<9, ///< (lightmap) blend in an additive glow texture
+       SHADERPERMUTATION_BLOOM = 1<<10, ///< bloom (postprocessing only)
+       SHADERPERMUTATION_SPECULAR = 1<<11, ///< (lightsource or deluxemapping) render specular effects
+       SHADERPERMUTATION_POSTPROCESSING = 1<<12, ///< user defined postprocessing (postprocessing only)
+       SHADERPERMUTATION_EXACTSPECULARMATH = 1<<13, ///< (lightsource or deluxemapping) use exact reflection map for specular effects, as opposed to the usual OpenGL approximation
+       SHADERPERMUTATION_REFLECTION = 1<<14, ///< normalmap-perturbed reflection of the scene infront of the surface, preformed as an overlay on the surface
+       SHADERPERMUTATION_OFFSETMAPPING = 1<<15, ///< adjust texcoords to roughly simulate a displacement mapped surface
+       SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING = 1<<16, ///< adjust texcoords to accurately simulate a displacement mapped surface (requires OFFSETMAPPING to also be set!)
+       SHADERPERMUTATION_SHADOWMAPRECT = 1<<17, ///< (lightsource) use shadowmap rectangle texture as light filter
+       SHADERPERMUTATION_SHADOWMAPCUBE = 1<<18, ///< (lightsource) use shadowmap cubemap texture as light filter
+       SHADERPERMUTATION_SHADOWMAP2D = 1<<19, ///< (lightsource) use shadowmap rectangle texture as light filter
+       SHADERPERMUTATION_SHADOWMAPPCF = 1<<20, ///< (lightsource) use percentage closer filtering on shadowmap test results
+       SHADERPERMUTATION_SHADOWMAPPCF2 = 1<<21, ///< (lightsource) use higher quality percentage closer filtering on shadowmap test results
+       SHADERPERMUTATION_SHADOWSAMPLER = 1<<22, ///< (lightsource) use hardware shadowmap test
+       SHADERPERMUTATION_SHADOWMAPVSDCT = 1<<23, ///< (lightsource) use virtual shadow depth cube texture for shadowmap indexing
+       SHADERPERMUTATION_LIMIT = 1<<24, ///< size of permutations array
+       SHADERPERMUTATION_COUNT = 24 ///< size of shaderpermutationinfo array
 }
 shaderpermutation_t;
 
@@ -1489,12 +1571,18 @@ shaderpermutation_t;
 shaderpermutationinfo_t shaderpermutationinfo[SHADERPERMUTATION_COUNT] =
 {
        {"#define USEDIFFUSE\n", " diffuse"},
-       {"#define USEVERTEXTEXTUREBLEND\n#define USEVIEWTINT\n", " vertextextureblend/tint"},
-       {"#define USECOLORMAPPING\n#define USESATURATION\n", " colormapping/saturation"},
-       {"#define USEFOG\n#define USEGAMMARAMPS\n", " fog/gammaramps"},
+       {"#define USEVERTEXTEXTUREBLEND\n", " vertextextureblend"},
+       {"#define USEVIEWTINT\n", " viewtint"},
+       {"#define USECOLORMAPPING\n", " colormapping"},
+       {"#define USESATURATION\n", " saturation"},
+       {"#define USEFOGINSIDE\n", " foginside"},
+       {"#define USEFOGOUTSIDE\n", " fogoutside"},
+       {"#define USEGAMMARAMPS\n", " gammaramps"},
        {"#define USECUBEFILTER\n", " cubefilter"},
-       {"#define USEGLOW\n#define USEBLOOM\n", " glow/bloom"},
-       {"#define USESPECULAR\n#define USEPOSTPROCESSING", " specular/postprocessing"},
+       {"#define USEGLOW\n", " glow"},
+       {"#define USEBLOOM\n", " bloom"},
+       {"#define USESPECULAR\n", " specular"},
+       {"#define USEPOSTPROCESSING\n", " postprocessing"},
        {"#define USEEXACTSPECULARMATH\n", " exactspecularmath"},
        {"#define USEREFLECTION\n", " reflection"},
        {"#define USEOFFSETMAPPING\n", " offsetmapping"},
@@ -1588,7 +1676,10 @@ typedef struct r_glsl_permutation_s
        int loc_EyePosition;
        int loc_Color_Pants;
        int loc_Color_Shirt;
+       int loc_FogPlane;
+       int loc_FogPlaneViewDist;
        int loc_FogRangeRecip;
+       int loc_FogHeightFade;
        int loc_AmbientScale;
        int loc_DiffuseScale;
        int loc_SpecularScale;
@@ -1783,7 +1874,10 @@ static void R_GLSL_CompilePermutation(r_glsl_permutation_t *p, unsigned int mode
                p->loc_EyePosition                = qglGetUniformLocationARB(p->program, "EyePosition");
                p->loc_Color_Pants                = qglGetUniformLocationARB(p->program, "Color_Pants");
                p->loc_Color_Shirt                = qglGetUniformLocationARB(p->program, "Color_Shirt");
+               p->loc_FogPlane                   = qglGetUniformLocationARB(p->program, "FogPlane");
+               p->loc_FogPlaneViewDist           = qglGetUniformLocationARB(p->program, "FogPlaneViewDist");
                p->loc_FogRangeRecip              = qglGetUniformLocationARB(p->program, "FogRangeRecip");
+               p->loc_FogHeightFade              = qglGetUniformLocationARB(p->program, "FogHeightFade");
                p->loc_AmbientScale               = qglGetUniformLocationARB(p->program, "AmbientScale");
                p->loc_DiffuseScale               = qglGetUniformLocationARB(p->program, "DiffuseScale");
                p->loc_SpecularPower              = qglGetUniformLocationARB(p->program, "SpecularPower");
@@ -2045,7 +2139,7 @@ void R_SetupSurfaceShader(const vec3_t lightcolorbase, qboolean modellighting, f
                if (specularscale > 0)
                        permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
                if (r_refdef.fogenabled)
-                       permutation |= SHADERPERMUTATION_FOG;
+                       permutation |= r_refdef.fogplaneviewabove ? SHADERPERMUTATION_FOGOUTSIDE : SHADERPERMUTATION_FOGINSIDE;
                if (rsurface.texture->colormapping)
                        permutation |= SHADERPERMUTATION_COLORMAPPING;
                if (r_shadow_usingshadowmaprect || r_shadow_usingshadowmap2d || r_shadow_usingshadowmapcube)
@@ -2076,7 +2170,7 @@ void R_SetupSurfaceShader(const vec3_t lightcolorbase, qboolean modellighting, f
                if (rsurface.texture->currentskinframe->glow && r_hdr_glowintensity.value > 0 && !gl_lightmaps.integer)
                        permutation |= SHADERPERMUTATION_GLOW;
                if (r_refdef.fogenabled)
-                       permutation |= SHADERPERMUTATION_FOG;
+                       permutation |= r_refdef.fogplaneviewabove ? SHADERPERMUTATION_FOGOUTSIDE : SHADERPERMUTATION_FOGINSIDE;
                if (rsurface.texture->colormapping)
                        permutation |= SHADERPERMUTATION_COLORMAPPING;
                if (r_glsl_offsetmapping.integer)
@@ -2100,7 +2194,7 @@ void R_SetupSurfaceShader(const vec3_t lightcolorbase, qboolean modellighting, f
                if (specularscale > 0)
                        permutation |= SHADERPERMUTATION_SPECULAR;
                if (r_refdef.fogenabled)
-                       permutation |= SHADERPERMUTATION_FOG;
+                       permutation |= r_refdef.fogplaneviewabove ? SHADERPERMUTATION_FOGOUTSIDE : SHADERPERMUTATION_FOGINSIDE;
                if (rsurface.texture->colormapping)
                        permutation |= SHADERPERMUTATION_COLORMAPPING;
                if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
@@ -2115,7 +2209,7 @@ void R_SetupSurfaceShader(const vec3_t lightcolorbase, qboolean modellighting, f
                if (rsurface.texture->currentskinframe->glow && r_hdr_glowintensity.value > 0 && !gl_lightmaps.integer)
                        permutation |= SHADERPERMUTATION_GLOW;
                if (r_refdef.fogenabled)
-                       permutation |= SHADERPERMUTATION_FOG;
+                       permutation |= r_refdef.fogplaneviewabove ? SHADERPERMUTATION_FOGOUTSIDE : SHADERPERMUTATION_FOGINSIDE;
                if (rsurface.texture->colormapping)
                        permutation |= SHADERPERMUTATION_COLORMAPPING;
                if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
@@ -2158,7 +2252,7 @@ void R_SetupSurfaceShader(const vec3_t lightcolorbase, qboolean modellighting, f
                if (rsurface.texture->currentskinframe->glow && r_hdr_glowintensity.value > 0 && !gl_lightmaps.integer)
                        permutation |= SHADERPERMUTATION_GLOW;
                if (r_refdef.fogenabled)
-                       permutation |= SHADERPERMUTATION_FOG;
+                       permutation |= r_refdef.fogplaneviewabove ? SHADERPERMUTATION_FOGOUTSIDE : SHADERPERMUTATION_FOGINSIDE;
                if (rsurface.texture->colormapping)
                        permutation |= SHADERPERMUTATION_COLORMAPPING;
                if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
@@ -2241,7 +2335,10 @@ void R_SetupSurfaceShader(const vec3_t lightcolorbase, qboolean modellighting, f
                else
                        qglUniform3fARB(r_glsl_permutation->loc_Color_Shirt, 0, 0, 0);
        }
+       if (r_glsl_permutation->loc_FogPlane >= 0) qglUniform4fARB(r_glsl_permutation->loc_FogPlane, rsurface.fogplane[0], rsurface.fogplane[1], rsurface.fogplane[2], rsurface.fogplane[3]);
+       if (r_glsl_permutation->loc_FogPlaneViewDist >= 0) qglUniform1fARB(r_glsl_permutation->loc_FogPlaneViewDist, rsurface.fogplaneviewdist);
        if (r_glsl_permutation->loc_FogRangeRecip >= 0) qglUniform1fARB(r_glsl_permutation->loc_FogRangeRecip, rsurface.fograngerecip);
+       if (r_glsl_permutation->loc_FogHeightFade >= 0) qglUniform1fARB(r_glsl_permutation->loc_FogHeightFade, rsurface.fogheightfade);
        if(permutation & SHADERPERMUTATION_EXACTSPECULARMATH)
        {
                if (r_glsl_permutation->loc_SpecularPower >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularPower, rsurface.texture->specularpower * 0.25);
@@ -4543,6 +4640,11 @@ void R_UpdateFogColor(void) // needs to be called before HDR subrender too, as t
                r_refdef.fogcolor[1] = r_refdef.fog_green;
                r_refdef.fogcolor[2] = r_refdef.fog_blue;
 
+               Vector4Set(r_refdef.fogplane, 0, 0, 1, -r_refdef.fog_height);
+               r_refdef.fogplaneviewdist = DotProduct(r_refdef.fogplane, r_refdef.view.origin) + r_refdef.fogplane[3];
+               r_refdef.fogplaneviewabove = r_refdef.fogplaneviewdist >= 0;
+               r_refdef.fogheightfade = -0.5f/max(0.125f, r_refdef.fog_fadedepth);
+
                {
                        vec3_t fogvec;
                        VectorCopy(r_refdef.fogcolor, fogvec);
@@ -4599,6 +4701,8 @@ void R_UpdateVariables(void)
                        r_refdef.fog_alpha = 1;
                        r_refdef.fog_start = 0;
                        r_refdef.fog_end = gl_skyclip.value;
+                       r_refdef.fog_height = 1<<30;
+                       r_refdef.fog_fadedepth = 128;
                }
                else if (r_refdef.oldgl_fogenable)
                {
@@ -4610,6 +4714,8 @@ void R_UpdateVariables(void)
                        r_refdef.fog_alpha = 0;
                        r_refdef.fog_start = 0;
                        r_refdef.fog_end = 0;
+                       r_refdef.fog_height = 1<<30;
+                       r_refdef.fog_fadedepth = 128;
                }
        }
 
@@ -5022,17 +5128,15 @@ void R_DrawBBoxMesh(vec3_t mins, vec3_t maxs, float cr, float cg, float cb, floa
 {
        int i;
        float *v, *c, f1, f2, vertex3f[8*3], color4f[8*4];
+
+       RSurf_ActiveWorldEntity();
+
        GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        GL_DepthMask(false);
        GL_DepthRange(0, 1);
        GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
-       R_Mesh_Matrix(&identitymatrix);
        R_Mesh_ResetTextureState();
 
-       // set up global fogging in worldspace (RSurf_FogVertex depends on this)
-       VectorCopy(r_refdef.view.origin, rsurface.localvieworigin);
-       rsurface.fograngerecip = r_refdef.fograngerecip;
-
        vertex3f[ 0] = mins[0];vertex3f[ 1] = mins[1];vertex3f[ 2] = mins[2]; //
        vertex3f[ 3] = maxs[0];vertex3f[ 4] = mins[1];vertex3f[ 5] = mins[2];
        vertex3f[ 6] = mins[0];vertex3f[ 7] = maxs[1];vertex3f[ 8] = mins[2];
@@ -5178,20 +5282,17 @@ void R_DrawNoModel_TransparentCallback(const entity_render_t *ent, const rtlight
        float f1, f2, *c;
        float color4f[6*4];
 
-       // set up global fogging in worldspace (RSurf_FogVertex depends on this)
-       VectorCopy(r_refdef.view.origin, rsurface.localvieworigin);
-       rsurface.fograngerecip = r_refdef.fograngerecip;
+       RSurf_ActiveCustomEntity(&ent->matrix, &ent->inversematrix, ent->flags, ent->shadertime, ent->colormod[0], ent->colormod[1], ent->colormod[2], ent->alpha, 6, nomodelvertex3f, NULL, NULL, NULL, NULL, 8, nomodelelement3i, nomodelelement3s, false, false);
 
        // this is only called once per entity so numsurfaces is always 1, and
        // surfacelist is always {0}, so this code does not handle batches
-       R_Mesh_Matrix(&ent->matrix);
 
        if (rsurface.ent_flags & RENDER_ADDITIVE)
        {
                GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
                GL_DepthMask(false);
        }
-       else if (ent->alpha < 1)
+       else if (rsurface.ent_color[3] < 1)
        {
                GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
                GL_DepthMask(false);
@@ -5201,37 +5302,32 @@ void R_DrawNoModel_TransparentCallback(const entity_render_t *ent, const rtlight
                GL_BlendFunc(GL_ONE, GL_ZERO);
                GL_DepthMask(true);
        }
-       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 : r_refdef.view.cullface_back);
+       GL_DepthRange(0, (rsurface.ent_flags & RENDER_VIEWMODEL) ? 0.0625 : 1);
+       GL_PolygonOffset(rsurface.basepolygonfactor, rsurface.basepolygonoffset);
+       GL_DepthTest(!(rsurface.ent_flags & RENDER_NODEPTHTEST));
+       GL_CullFace((rsurface.ent_flags & RENDER_DOUBLESIDED) ? GL_NONE : r_refdef.view.cullface_back);
        R_SetupGenericShader(false);
-       R_Mesh_VertexPointer(nomodelvertex3f, 0, 0);
+       R_Mesh_VertexPointer(rsurface.vertex3f, rsurface.vertex3f_bufferobject, rsurface.vertex3f_bufferoffset);
+       memcpy(color4f, nomodelcolor4f, sizeof(float[6*4]));
+       R_Mesh_ColorPointer(color4f, 0, 0);
+       for (i = 0, c = color4f;i < 6;i++, c += 4)
+       {
+               c[0] *= rsurface.ent_color[0];
+               c[1] *= rsurface.ent_color[1];
+               c[2] *= rsurface.ent_color[2];
+               c[3] *= rsurface.ent_color[3];
+       }
        if (r_refdef.fogenabled)
        {
-               vec3_t org;
-               memcpy(color4f, nomodelcolor4f, sizeof(float[6*4]));
-               R_Mesh_ColorPointer(color4f, 0, 0);
-               Matrix4x4_OriginFromMatrix(&ent->matrix, org);
-               f1 = RSurf_FogVertex(org);
-               f2 = 1 - f1;
                for (i = 0, c = color4f;i < 6;i++, c += 4)
                {
+                       f1 = RSurf_FogVertex(rsurface.vertex3f + 3*i);
+                       f2 = 1 - f1;
                        c[0] = (c[0] * f1 + r_refdef.fogcolor[0] * f2);
                        c[1] = (c[1] * f1 + r_refdef.fogcolor[1] * f2);
                        c[2] = (c[2] * f1 + r_refdef.fogcolor[2] * f2);
-                       c[3] *= ent->alpha;
                }
        }
-       else if (ent->alpha != 1)
-       {
-               memcpy(color4f, nomodelcolor4f, sizeof(float[6*4]));
-               R_Mesh_ColorPointer(color4f, 0, 0);
-               for (i = 0, c = color4f;i < 6;i++, c += 4)
-                       c[3] *= ent->alpha;
-       }
-       else
-               R_Mesh_ColorPointer(nomodelcolor4f, 0, 0);
        R_Mesh_ResetTextureState();
        R_Mesh_Draw(0, 6, 0, 8, nomodelelement3i, nomodelelement3s, 0, 0);
 }
@@ -5276,31 +5372,8 @@ void R_CalcBeam_Vertex3f (float *vert, const vec3_t org1, const vec3_t org2, flo
        vert[11] = org2[2] + width * right2[2];
 }
 
-float spritetexcoord2f[4*2] = {0, 1, 0, 0, 1, 0, 1, 1};
-
-void R_DrawSprite(int blendfunc1, int blendfunc2, rtexture_t *texture, rtexture_t *fogtexture, qboolean depthdisable, qboolean depthshort, const vec3_t origin, const vec3_t left, const vec3_t up, float scalex1, float scalex2, float scaley1, float scaley2, float cr, float cg, float cb, float ca)
+void R_CalcSprite_Vertex3f(float *vertex3f, const vec3_t origin, const vec3_t left, const vec3_t up, float scalex1, float scalex2, float scaley1, float scaley2)
 {
-       // NOTE: this must not call qglDepthFunc (see r_shadow.c, R_BeginCoronaQuery) thanks to ATI
-       float fog = 1.0f;
-       float vertex3f[12];
-
-       // set up global fogging in worldspace (RSurf_FogVertex depends on this)
-       VectorCopy(r_refdef.view.origin, rsurface.localvieworigin);
-       rsurface.fograngerecip = r_refdef.fograngerecip;
-
-       if (r_refdef.fogenabled && !depthdisable) // TODO maybe make the unfog effect a separate flag?
-               fog = RSurf_FogVertex(origin);
-
-       R_Mesh_Matrix(&identitymatrix);
-       GL_BlendFunc(blendfunc1, blendfunc2);
-
-       GL_CullFace(GL_NONE);
-
-       GL_DepthMask(false);
-       GL_DepthRange(0, depthshort ? 0.0625 : 1);
-       GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
-       GL_DepthTest(!depthdisable);
-
        vertex3f[ 0] = origin[0] + left[0] * scalex2 + up[0] * scaley1;
        vertex3f[ 1] = origin[1] + left[1] * scalex2 + up[1] * scaley1;
        vertex3f[ 2] = origin[2] + left[2] * scalex2 + up[2] * scaley1;
@@ -5313,25 +5386,6 @@ void R_DrawSprite(int blendfunc1, int blendfunc2, rtexture_t *texture, rtexture_
        vertex3f[ 9] = origin[0] + left[0] * scalex1 + up[0] * scaley1;
        vertex3f[10] = origin[1] + left[1] * scalex1 + up[1] * scaley1;
        vertex3f[11] = origin[2] + left[2] * scalex1 + up[2] * scaley1;
-
-       R_Mesh_VertexPointer(vertex3f, 0, 0);
-       R_Mesh_ColorPointer(NULL, 0, 0);
-       R_Mesh_ResetTextureState();
-       R_SetupGenericShader(true);
-       R_Mesh_TexBind(0, R_GetTexture(texture));
-       R_Mesh_TexCoordPointer(0, 2, spritetexcoord2f, 0, 0);
-       // FIXME: fixed function path can't properly handle r_refdef.view.colorscale > 1
-       GL_Color(cr * fog * r_refdef.view.colorscale, cg * fog * r_refdef.view.colorscale, cb * fog * r_refdef.view.colorscale, ca);
-       R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, polygonelement3s, 0, 0);
-
-       if (blendfunc2 == GL_ONE_MINUS_SRC_ALPHA)
-       {
-               R_Mesh_TexBind(0, R_GetTexture(fogtexture));
-               GL_BlendFunc(blendfunc1, GL_ONE);
-               fog = 1 - fog;
-               GL_Color(r_refdef.fogcolor[0] * fog, r_refdef.fogcolor[1] * fog, r_refdef.fogcolor[2] * fog, ca);
-               R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, polygonelement3s, 0, 0);
-       }
 }
 
 int R_Mesh_AddVertex(rmesh_t *mesh, float x, float y, float z)
@@ -5670,7 +5724,7 @@ texture_t *R_GetCurrentTexture(texture_t *t)
                t->currenttexmatrix = r_waterscrollmatrix;
                t->currentbackgroundtexmatrix = r_waterscrollmatrix;
        }
-       else
+       else if (!(t->currentmaterialflags & MATERIALFLAG_CUSTOMSURFACE))
        {
                Matrix4x4_CreateIdentity(&t->currenttexmatrix);
                Matrix4x4_CreateIdentity(&t->currentbackgroundtexmatrix);
@@ -5860,7 +5914,10 @@ void RSurf_ActiveWorldEntity(void)
        rsurface.inversematrixscale = 1;
        R_Mesh_Matrix(&identitymatrix);
        VectorCopy(r_refdef.view.origin, rsurface.localvieworigin);
+       Vector4Copy(r_refdef.fogplane, rsurface.fogplane);
        rsurface.fograngerecip = r_refdef.fograngerecip;
+       rsurface.fogheightfade = r_refdef.fogheightfade;
+       rsurface.fogplaneviewdist = r_refdef.fogplaneviewdist;
        rsurface.fogmasktabledistmultiplier = FOGMASKTABLEWIDTH * rsurface.fograngerecip;
        VectorSet(rsurface.modellight_ambient, 0, 0, 0);
        VectorSet(rsurface.modellight_diffuse, 0, 0, 0);
@@ -5936,7 +5993,10 @@ void RSurf_ActiveModelEntity(const entity_render_t *ent, qboolean wantnormals, q
        rsurface.inversematrixscale = 1.0f / rsurface.matrixscale;
        R_Mesh_Matrix(&rsurface.matrix);
        Matrix4x4_Transform(&rsurface.inversematrix, r_refdef.view.origin, rsurface.localvieworigin);
+       Matrix4x4_TransformStandardPlane(&rsurface.inversematrix, r_refdef.fogplane[0], r_refdef.fogplane[1], r_refdef.fogplane[2], r_refdef.fogplane[3], rsurface.fogplane);
+       rsurface.fogplaneviewdist *= rsurface.inversematrixscale;
        rsurface.fograngerecip = r_refdef.fograngerecip * rsurface.matrixscale;
+       rsurface.fogheightfade = r_refdef.fogheightfade * rsurface.matrixscale;
        rsurface.fogmasktabledistmultiplier = FOGMASKTABLEWIDTH * rsurface.fograngerecip;
        VectorCopy(ent->modellight_ambient, rsurface.modellight_ambient);
        VectorCopy(ent->modellight_diffuse, rsurface.modellight_diffuse);
@@ -6043,19 +6103,211 @@ void RSurf_ActiveModelEntity(const entity_render_t *ent, qboolean wantnormals, q
        rsurface.texcoordtexture2f = rsurface.modeltexcoordtexture2f;
 }
 
+void RSurf_ActiveCustomEntity(const matrix4x4_t *matrix, const matrix4x4_t *inversematrix, int entflags, double shadertime, float r, float g, float b, float a, int numvertices, const float *vertex3f, const float *texcoord2f, const float *normal3f, const float *svector3f, const float *tvector3f, int numtriangles, const int *element3i, const unsigned short *element3s, qboolean wantnormals, qboolean wanttangents)
+{
+#if 0
+       static float scale[8];
+       vec3_t worldfogpoint, localfogpoint;
+#endif
+       rsurface.entity = r_refdef.scene.worldentity;
+       rsurface.ent_skinnum = 0;
+       rsurface.ent_qwskin = -1;
+       rsurface.ent_shadertime = shadertime;
+       Vector4Set(rsurface.ent_color, r, g, b, a);
+       rsurface.ent_flags = entflags;
+       rsurface.modelnum_vertices = numvertices;
+       rsurface.modelnum_triangles = numtriangles;
+       if (rsurface.array_size < rsurface.modelnum_vertices)
+               R_Mesh_ResizeArrays(rsurface.modelnum_vertices);
+       rsurface.matrix = *matrix;
+       rsurface.inversematrix = *inversematrix;
+       rsurface.matrixscale = Matrix4x4_ScaleFromMatrix(&rsurface.matrix);
+       rsurface.inversematrixscale = 1.0f / rsurface.matrixscale;
+       R_Mesh_Matrix(&rsurface.matrix);
+#if 0
+       scale[0] = 1;
+       scale[1] = rsurface.matrixscale;
+       scale[2] = rsurface.inversematrixscale;
+       scale[3] = rsurface.matrixscale * rsurface.matrixscale;
+       scale[4] = rsurface.inversematrixscale * rsurface.inversematrixscale;
+       scale[5] = rsurface.matrixscale * rsurface.matrixscale * rsurface.matrixscale;
+       scale[6] = rsurface.inversematrixscale * rsurface.inversematrixscale * rsurface.inversematrixscale;
+       scale[7] = 0;
+#endif
+#if 0
+       Matrix4x4_Transform(&rsurface.inversematrix, r_refdef.view.origin, rsurface.localvieworigin);
+       //Matrix4x4_TransformStandardPlane(&rsurface.inversematrix, r_refdef.fogplane[0], r_refdef.fogplane[1], r_refdef.fogplane[2], r_refdef.fogplane[3], rsurface.fogplane);
+       Matrix4x4_Transform3x3(&rsurface.inversematrix, r_refdef.fogplane, rsurface.fogplane);
+       VectorNormalize(rsurface.fogplane);
+       VectorScale(r_refdef.fogplane, -r_refdef.fogplane[3], worldfogpoint);
+       Matrix4x4_Transform(&rsurface.inversematrix, worldfogpoint, localfogpoint);
+       rsurface.fogplane[3] = -DotProduct(rsurface.fogplane, localfogpoint);
+       rsurface.fogplaneviewdist = DotProduct(rsurface.localvieworigin, rsurface.fogplane) + rsurface.fogplane[3];
+       rsurface.fograngerecip = r_refdef.fograngerecip * rsurface.matrixscale;
+       rsurface.fogheightfade = r_refdef.fogheightfade * rsurface.matrixscale;
+       rsurface.fogmasktabledistmultiplier = FOGMASKTABLEWIDTH * rsurface.fograngerecip;
+#endif
+#if 1
+       Matrix4x4_Transform(&rsurface.inversematrix, r_refdef.view.origin, rsurface.localvieworigin);
+       Matrix4x4_TransformStandardPlane(&rsurface.inversematrix, r_refdef.fogplane[0], r_refdef.fogplane[1], r_refdef.fogplane[2], r_refdef.fogplane[3], rsurface.fogplane);
+       rsurface.fogplaneviewdist *= rsurface.inversematrixscale;
+       rsurface.fograngerecip = r_refdef.fograngerecip * rsurface.matrixscale;
+       rsurface.fogheightfade = r_refdef.fogheightfade * rsurface.matrixscale;
+       rsurface.fogmasktabledistmultiplier = FOGMASKTABLEWIDTH * rsurface.fograngerecip;
+#endif
+
+#if 0
+if (r_refdef.fogenabled && memcmp(&rsurface.matrix, &identitymatrix, sizeof(rsurface.matrix)))
+{
+       qboolean fail = false;
+       vec3_t temp, temp2, temp3, temp4, temp5, temp6, temp7, temp8;
+       VectorScale(r_refdef.fogplane, r_refdef.fogplane[3], temp);
+       Matrix4x4_Transform(&rsurface.inversematrix, temp, temp2);
+       //rsurface.fogplane[3] = -DotProduct(temp2, rsurface.fogplane);
+       //rsurface.fogplaneviewdist = DotProduct(rsurface.localvieworigin, rsurface.fogplane) + rsurface.fogplane[3];
+       VectorScale(rsurface.fogplane, rsurface.fogplane[3], temp3);
+       Matrix4x4_Transform(&rsurface.matrix, temp3, temp4);
+       VectorSet(temp5, r_refdef.view.origin[0], r_refdef.view.origin[1], r_refdef.view.origin[2] + 1000);
+       Matrix4x4_Transform(&rsurface.inversematrix, temp5, temp6);
+       VectorSet(temp7, r_refdef.view.origin[0], r_refdef.view.origin[1], r_refdef.view.origin[2] - 1000);
+       Matrix4x4_Transform(&rsurface.inversematrix, temp7, temp8);
+       Con_Printf("#%i : %f %f %f %f : %f %f %f : %f %f %f : %f %f\n", r_test.integer,
+               rsurface.fogplane[0], rsurface.fogplane[1], rsurface.fogplane[2], rsurface.fogplane[3],
+               rsurface.localvieworigin[0], rsurface.localvieworigin[1], rsurface.localvieworigin[2],
+               rsurface.fogplaneviewdist,
+               DotProduct(rsurface.localvieworigin, rsurface.fogplane) - DotProduct(temp2, rsurface.fogplane),
+               DotProduct(r_refdef.view.origin, r_refdef.fogplane) + r_refdef.fogplane[3],
+               rsurface.fogplane[3] - -DotProduct(temp2, rsurface.fogplane),
+               r_refdef.fogplane[3] - -DotProduct(temp4, r_refdef.fogplane)
+               );
+       //if (fabs(RSurf_FogVertex(temp6) - RSurf_FogPoint(temp5)) > 0.1)
+       //      fail = true;
+       //if (fabs(RSurf_FogVertex(temp8) - RSurf_FogPoint(temp7)) > 0.1)
+       //      fail = true;
+       //if (fabs(rsurface.fogplaneviewdist * rsurface.matrixscale - r_refdef.fogplaneviewdist) > 1)
+       //      fail = true;
+       //if (fabs((DotProduct(rsurface.localvieworigin, rsurface.fogplane) + rsurface.fogplane[3]) * rsurface.matrixscale - r_refdef.fogplaneviewdist) > 1)
+       //      fail = true;
+       //if (fabs(-DotProduct(temp2, rsurface.fogplane) - rsurface.fogplane[3]) > 1)
+       //      fail = true;
+       //if (fabs(-DotProduct(temp4, r_refdef.fogplane) - r_refdef.fogplane[3]) > 1)
+       //      fail = true;
+       //if (fabs(rsurface.fogplaneviewdist - DotProduct(rsurface.localvieworigin, rsurface.fogplane) - rsurface.fogplane[3]) > 1)
+       //      fail = true;
+       //if (fabs(DotProduct(rsurface.localvieworigin, rsurface.fogplane) - DotProduct(temp2, rsurface.fogplane) - rsurface.fogplaneviewdist) > 1)
+       //      fail = true;
+       if (fail)
+               Cvar_SetValueQuick(&r_test, r_test.integer + 1);
+}
+#endif
+
+       VectorSet(rsurface.modellight_ambient, 0, 0, 0);
+       VectorSet(rsurface.modellight_diffuse, 0, 0, 0);
+       VectorSet(rsurface.modellight_lightdir, 0, 0, 1);
+       VectorSet(rsurface.colormap_pantscolor, 0, 0, 0);
+       VectorSet(rsurface.colormap_shirtcolor, 0, 0, 0);
+       VectorSet(rsurface.glowmod, 1, 1, 1);
+       memset(rsurface.frameblend, 0, sizeof(rsurface.frameblend));
+       rsurface.frameblend[0].lerp = 1;
+       rsurface.basepolygonfactor = r_refdef.polygonfactor;
+       rsurface.basepolygonoffset = r_refdef.polygonoffset;
+       if (wanttangents)
+       {
+               rsurface.modelvertex3f = vertex3f;
+               rsurface.modelsvector3f = svector3f ? svector3f : rsurface.array_modelsvector3f;
+               rsurface.modeltvector3f = tvector3f ? tvector3f : rsurface.array_modeltvector3f;
+               rsurface.modelnormal3f = normal3f ? normal3f : rsurface.array_modelnormal3f;
+       }
+       else if (wantnormals)
+       {
+               rsurface.modelvertex3f = vertex3f;
+               rsurface.modelsvector3f = NULL;
+               rsurface.modeltvector3f = NULL;
+               rsurface.modelnormal3f = normal3f ? normal3f : rsurface.array_modelnormal3f;
+       }
+       else
+       {
+               rsurface.modelvertex3f = vertex3f;
+               rsurface.modelsvector3f = NULL;
+               rsurface.modeltvector3f = NULL;
+               rsurface.modelnormal3f = NULL;
+       }
+       rsurface.modelvertex3f_bufferobject = 0;
+       rsurface.modelvertex3f_bufferoffset = 0;
+       rsurface.modelsvector3f_bufferobject = 0;
+       rsurface.modelsvector3f_bufferoffset = 0;
+       rsurface.modeltvector3f_bufferobject = 0;
+       rsurface.modeltvector3f_bufferoffset = 0;
+       rsurface.modelnormal3f_bufferobject = 0;
+       rsurface.modelnormal3f_bufferoffset = 0;
+       rsurface.generatedvertex = true;
+       rsurface.modellightmapcolor4f  = NULL;
+       rsurface.modellightmapcolor4f_bufferobject = 0;
+       rsurface.modellightmapcolor4f_bufferoffset = 0;
+       rsurface.modeltexcoordtexture2f  = texcoord2f;
+       rsurface.modeltexcoordtexture2f_bufferobject = 0;
+       rsurface.modeltexcoordtexture2f_bufferoffset = 0;
+       rsurface.modeltexcoordlightmap2f  = NULL;
+       rsurface.modeltexcoordlightmap2f_bufferobject = 0;
+       rsurface.modeltexcoordlightmap2f_bufferoffset = 0;
+       rsurface.modelelement3i = element3i;
+       rsurface.modelelement3s = element3s;
+       rsurface.modelelement3i_bufferobject = 0;
+       rsurface.modelelement3s_bufferobject = 0;
+       rsurface.modellightmapoffsets = NULL;
+       rsurface.modelsurfaces = NULL;
+       rsurface.vertex3f  = rsurface.modelvertex3f;
+       rsurface.vertex3f_bufferobject = rsurface.modelvertex3f_bufferobject;
+       rsurface.vertex3f_bufferoffset = rsurface.modelvertex3f_bufferoffset;
+       rsurface.svector3f = rsurface.modelsvector3f;
+       rsurface.svector3f_bufferobject = rsurface.modelsvector3f_bufferobject;
+       rsurface.svector3f_bufferoffset = rsurface.modelsvector3f_bufferoffset;
+       rsurface.tvector3f = rsurface.modeltvector3f;
+       rsurface.tvector3f_bufferobject = rsurface.modeltvector3f_bufferobject;
+       rsurface.tvector3f_bufferoffset = rsurface.modeltvector3f_bufferoffset;
+       rsurface.normal3f  = rsurface.modelnormal3f;
+       rsurface.normal3f_bufferobject = rsurface.modelnormal3f_bufferobject;
+       rsurface.normal3f_bufferoffset = rsurface.modelnormal3f_bufferoffset;
+       rsurface.texcoordtexture2f = rsurface.modeltexcoordtexture2f;
+
+       if (rsurface.modelnum_vertices && rsurface.modelelement3i)
+       {
+               if ((wantnormals || wanttangents) && !normal3f)
+                       Mod_BuildNormals(0, rsurface.modelnum_vertices, rsurface.modelnum_triangles, rsurface.modelvertex3f, rsurface.modelelement3i, rsurface.array_modelnormal3f, r_smoothnormals_areaweighting.integer != 0);
+               if (wanttangents && !svector3f)
+                       Mod_BuildTextureVectorsFromNormals(0, rsurface.modelnum_vertices, rsurface.modelnum_triangles, rsurface.modelvertex3f, rsurface.modeltexcoordtexture2f, rsurface.modelnormal3f, rsurface.modelelement3i, rsurface.array_modelsvector3f, rsurface.array_modeltvector3f, r_smoothnormals_areaweighting.integer != 0);
+       }
+}
+
 float RSurf_FogPoint(const float *v)
 {
-       float len = VectorDistance(r_refdef.view.origin, v);
+       // this code is identical to the USEFOGINSIDE/USEFOGOUTSIDE code in the shader
+       float FogPlaneViewDist = r_refdef.fogplaneviewdist;
+       float FogPlaneVertexDist = DotProduct(r_refdef.fogplane, v) + r_refdef.fogplane[3];
+       float FogHeightFade = r_refdef.fogheightfade;
+       float fogfrac;
        unsigned int fogmasktableindex;
-       fogmasktableindex = (unsigned int)(len * r_refdef.fogmasktabledistmultiplier);
+       if (r_refdef.fogplaneviewabove)
+               fogfrac = min(0.0f, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0f, min(0.0f, FogPlaneVertexDist) * FogHeightFade);
+       else
+               fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0f, FogPlaneVertexDist)) * min(1.0f, (min(0.0f, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade);
+       fogmasktableindex = (unsigned int)(VectorDistance(r_refdef.view.origin, v) * fogfrac * r_refdef.fogmasktabledistmultiplier);
        return r_refdef.fogmasktable[min(fogmasktableindex, FOGMASKTABLEWIDTH - 1)];
 }
 
 float RSurf_FogVertex(const float *v)
 {
-       float len = VectorDistance(rsurface.localvieworigin, v);
+       // this code is identical to the USEFOGINSIDE/USEFOGOUTSIDE code in the shader
+       float FogPlaneViewDist = rsurface.fogplaneviewdist;
+       float FogPlaneVertexDist = DotProduct(rsurface.fogplane, v) + rsurface.fogplane[3];
+       float FogHeightFade = rsurface.fogheightfade;
+       float fogfrac;
        unsigned int fogmasktableindex;
-       fogmasktableindex = (unsigned int)(len * rsurface.fogmasktabledistmultiplier);
+       if (r_refdef.fogplaneviewabove)
+               fogfrac = min(0.0f, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0f, min(0.0f, FogPlaneVertexDist) * FogHeightFade);
+       else
+               fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0f, FogPlaneVertexDist)) * min(1.0f, (min(0.0f, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade);
+       fogmasktableindex = (unsigned int)(VectorDistance(rsurface.localvieworigin, v) * fogfrac * rsurface.fogmasktabledistmultiplier);
        return r_refdef.fogmasktable[min(fogmasktableindex, FOGMASKTABLEWIDTH - 1)];
 }
 
@@ -8257,3 +8509,30 @@ void R_DrawModelSurfaces(entity_render_t *ent, qboolean skysurfaces, qboolean wr
        }
        rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
 }
+
+void R_DrawCustomSurface(skinframe_t *skinframe, const matrix4x4_t *texmatrix, int materialflags, int firstvertex, int numvertices, int firsttriangle, int numtriangles, qboolean writedepth)
+{
+       static texture_t texture;
+       static msurface_t surface;
+       const msurface_t *surfacelist = &surface;
+
+       // fake enough texture and surface state to render this geometry
+
+       texture.update_lastrenderframe = -1; // regenerate this texture
+       texture.basematerialflags = materialflags | MATERIALFLAG_CUSTOMSURFACE | MATERIALFLAG_WALL;
+       texture.currentskinframe = skinframe;
+       texture.currenttexmatrix = *texmatrix; // requires MATERIALFLAG_CUSTOMSURFACE
+       texture.specularscalemod = 1;
+       texture.specularpowermod = 1;
+
+       surface.texture = &texture;
+       surface.num_triangles = numtriangles;
+       surface.num_firsttriangle = firsttriangle;
+       surface.num_vertices = numvertices;
+       surface.num_firstvertex = firstvertex;
+
+       // now render it
+       rsurface.texture = R_GetCurrentTexture(surface.texture);
+       rsurface.uselightmaptexture = false;
+       R_DrawModelTextureSurfaceList(1, &surfacelist, writedepth);
+}
index dd6c0db..66e6b85 100644 (file)
@@ -1381,8 +1381,8 @@ void R_Q1BSP_LoadSplitSky (unsigned char *src, int width, int height, int bytesp
                }
        }
 
-       loadmodel->brush.solidskytexture = R_LoadTexture2D(loadmodel->texturepool, "sky_solidtexture", w, h, (unsigned char *) solidpixels, TEXTYPE_BGRA, TEXF_PRECACHE, NULL);
-       loadmodel->brush.alphaskytexture = R_LoadTexture2D(loadmodel->texturepool, "sky_alphatexture", w, h, (unsigned char *) alphapixels, TEXTYPE_BGRA, TEXF_ALPHA | TEXF_PRECACHE, NULL);
+       loadmodel->brush.solidskyskinframe = R_SkinFrame_LoadInternalBGRA("sky_solidtexture",              TEXF_PRECACHE, (unsigned char *) solidpixels, w, h);
+       loadmodel->brush.alphaskyskinframe = R_SkinFrame_LoadInternalBGRA("sky_alphatexture", TEXF_ALPHA | TEXF_PRECACHE, (unsigned char *) alphapixels, w, h);
        Mem_Free(solidpixels);
        Mem_Free(alphapixels);
 }
index 3e90e25..f2e9e94 100644 (file)
@@ -108,6 +108,8 @@ mplane_t;
 #define MATERIALFLAG_MODELLIGHT 4194304
 // add directional model lighting to this material (q3bsp lightgrid only)
 #define MATERIALFLAG_MODELLIGHT_DIRECTIONAL 8388608
+// causes RSurf_GetCurrentTexture to leave alone certain fields
+#define MATERIALFLAG_CUSTOMSURFACE 16777216
 // combined mask of all attributes that require depth sorted rendering
 #define MATERIALFLAGMASK_DEPTHSORTED (MATERIALFLAG_BLENDED | MATERIALFLAG_NODEPTHTEST)
 // combined mask of all attributes that cause some sort of transparency
index 62ad436..6ea5a85 100644 (file)
@@ -102,6 +102,10 @@ static void mod_newmap(void)
                                for (k = 0;k < mod->data_textures[j].backgroundnumskinframes;k++)
                                        R_SkinFrame_MarkUsed(mod->data_textures[j].backgroundskinframes[k]);
                        }
+                       if (mod->brush.solidskyskinframe)
+                               R_SkinFrame_MarkUsed(mod->brush.solidskyskinframe);
+                       if (mod->brush.alphaskyskinframe)
+                               R_SkinFrame_MarkUsed(mod->brush.alphaskyskinframe);
                }
        }
 
index d756c34..986bad0 100644 (file)
@@ -699,8 +699,8 @@ typedef struct model_brush_s
 
        char skybox[MAX_QPATH];
 
-       rtexture_t *solidskytexture;
-       rtexture_t *alphaskytexture;
+       skinframe_t *solidskyskinframe;
+       skinframe_t *alphaskyskinframe;
 
        qboolean supportwateralpha;
 
index 2de7600..7da3ee7 100644 (file)
@@ -10,22 +10,21 @@ cvar_t r_lightningbeam_color_green = {CVAR_SAVE, "r_lightningbeam_color_green",
 cvar_t r_lightningbeam_color_blue = {CVAR_SAVE, "r_lightningbeam_color_blue", "1", "color of the lightning beam effect"};
 cvar_t r_lightningbeam_qmbtexture = {CVAR_SAVE, "r_lightningbeam_qmbtexture", "0", "load the qmb textures/particles/lightning.pcx texture instead of generating one, can look better"};
 
-rtexture_t *r_lightningbeamtexture;
-rtexture_t *r_lightningbeamqmbtexture;
-rtexturepool_t *r_lightningbeamtexturepool;
+skinframe_t *r_lightningbeamtexture;
+skinframe_t *r_lightningbeamqmbtexture;
 
-unsigned short r_lightningbeamelements[18] = {0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11};
+int r_lightningbeamelement3i[18] = {0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11};
+unsigned short r_lightningbeamelement3s[18] = {0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11};
 
 void r_lightningbeams_start(void)
 {
-       r_lightningbeamtexturepool = R_AllocTexturePool();
        r_lightningbeamtexture = NULL;
        r_lightningbeamqmbtexture = NULL;
 }
 
 void r_lightningbeams_setupqmbtexture(void)
 {
-       r_lightningbeamqmbtexture = loadtextureimage(r_lightningbeamtexturepool, "textures/particles/lightning.pcx", false, TEXF_ALPHA | TEXF_PRECACHE | TEXF_FORCELINEAR, false);
+       r_lightningbeamqmbtexture = R_SkinFrame_LoadExternal("textures/particles/lightning.pcx", TEXF_ALPHA | TEXF_PRECACHE | TEXF_FORCELINEAR, false);
        if (r_lightningbeamqmbtexture == NULL)
                Cvar_SetValueQuick(&r_lightningbeam_qmbtexture, false);
 }
@@ -145,7 +144,7 @@ void r_lightningbeams_setuptexture(void)
                }
        }
 
-       r_lightningbeamtexture = R_LoadTexture2D(r_lightningbeamtexturepool, "lightningbeam", BEAMWIDTH, BEAMHEIGHT, data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_FORCELINEAR, NULL);
+       r_lightningbeamtexture = R_SkinFrame_LoadInternalBGRA("lightningbeam", TEXF_PRECACHE | TEXF_FORCELINEAR, data, BEAMWIDTH, BEAMHEIGHT);
        Mem_Free(noise1);
        Mem_Free(noise2);
        Mem_Free(data);
@@ -156,11 +155,14 @@ void r_lightningbeams_shutdown(void)
 {
        r_lightningbeamtexture = NULL;
        r_lightningbeamqmbtexture = NULL;
-       R_FreeTexturePool(&r_lightningbeamtexturepool);
 }
 
 void r_lightningbeams_newmap(void)
 {
+       if (r_lightningbeamtexture)
+               R_SkinFrame_MarkUsed(r_lightningbeamtexture);
+       if (r_lightningbeamqmbtexture)
+               R_SkinFrame_MarkUsed(r_lightningbeamqmbtexture);
 }
 
 void R_LightningBeams_Init(void)
@@ -232,49 +234,17 @@ float beamrepeatscale;
 void R_DrawLightningBeam_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
 {
        int surfacelistindex;
-       rmeshstate_t m;
        float vertex3f[12*3];
        float texcoord2f[12*2];
-       float color4f[12*4];
 
-       // set up global fogging in worldspace (RSurf_FogVertex depends on this)
-       VectorCopy(r_refdef.view.origin, rsurface.localvieworigin);
+       RSurf_ActiveCustomEntity(&identitymatrix, &identitymatrix, 0, 0, r_lightningbeam_color_red.value * r_refdef.view.colorscale, r_lightningbeam_color_green.value * r_refdef.view.colorscale, r_lightningbeam_color_blue.value * r_refdef.view.colorscale, 1, 12, vertex3f, texcoord2f, NULL, NULL, NULL, 6, r_lightningbeamelement3i, r_lightningbeamelement3s, false, false);
        rsurface.fograngerecip = r_refdef.fograngerecip;
 
-       R_Mesh_Matrix(&identitymatrix);
-       GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
-       GL_DepthMask(false);
-       GL_DepthRange(0, 1);
-       GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
-       GL_DepthTest(true);
-       GL_CullFace(GL_NONE);
        if (r_lightningbeam_qmbtexture.integer && r_lightningbeamqmbtexture == NULL)
                r_lightningbeams_setupqmbtexture();
        if (!r_lightningbeam_qmbtexture.integer && r_lightningbeamtexture == NULL)
                r_lightningbeams_setuptexture();
 
-       R_Mesh_VertexPointer(vertex3f, 0, 0);
-       R_SetupGenericShader(true);
-       // FIXME: fixed function path can't properly handle r_refdef.view.colorscale > 1
-       if (r_refdef.fogenabled)
-       {
-               // per vertex colors if fog is used
-               R_Mesh_ColorPointer(color4f, 0, 0);
-       }
-       else
-       {
-               // solid color if fog is not used
-               R_Mesh_ColorPointer(NULL, 0, 0);
-               GL_Color(r_lightningbeam_color_red.value * r_refdef.view.colorscale, r_lightningbeam_color_green.value * r_refdef.view.colorscale, r_lightningbeam_color_blue.value * r_refdef.view.colorscale, 1);
-       }
-       memset(&m, 0, sizeof(m));
-       if (r_lightningbeam_qmbtexture.integer)
-               m.tex[0] = R_GetTexture(r_lightningbeamqmbtexture);
-       else
-               m.tex[0] = R_GetTexture(r_lightningbeamtexture);
-       m.pointer_texcoord[0] = texcoord2f;
-       R_Mesh_TextureState(&m);
-
        for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
        {
                const beam_t *b = cl.beams + surfacelist[surfacelistindex];
@@ -336,16 +306,9 @@ void R_DrawLightningBeam_TransparentCallback(const entity_render_t *ent, const r
                R_CalcLightningBeamPolygonTexCoord2f(texcoord2f + 0, t1, t2);
                R_CalcLightningBeamPolygonTexCoord2f(texcoord2f + 8, t1 + 0.33, t2 + 0.33);
                R_CalcLightningBeamPolygonTexCoord2f(texcoord2f + 16, t1 + 0.66, t2 + 0.66);
-               if (r_refdef.fogenabled)
-               {
-                       // per vertex colors if fog is used
-                       R_FogLightningBeam_Vertex3f_Color4f(vertex3f, color4f, 12, r_lightningbeam_color_red.value, r_lightningbeam_color_green.value, r_lightningbeam_color_blue.value, 1);
-               }
 
                // draw the 3 polygons as one batch of 6 triangles using the 12 vertices
-               GL_LockArrays(0, 12);
-               R_Mesh_Draw(0, 12, 0, 6, NULL, r_lightningbeamelements, 0, 0);
-               GL_LockArrays(0, 0);
+               R_DrawCustomSurface(r_lightningbeam_qmbtexture.integer ? r_lightningbeamqmbtexture : r_lightningbeamtexture, &identitymatrix, MATERIALFLAG_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_FULLBRIGHT | MATERIALFLAG_NOCULLFACE, 0, 12, 0, 6, false);
        }
 }
 
index de8840a..56bf1b8 100644 (file)
@@ -243,7 +243,7 @@ rtexturepool_t *r_shadow_texturepool;
 rtexture_t *r_shadow_attenuationgradienttexture;
 rtexture_t *r_shadow_attenuation2dtexture;
 rtexture_t *r_shadow_attenuation3dtexture;
-rtexture_t *r_shadow_lightcorona;
+skinframe_t *r_shadow_lightcorona;
 rtexture_t *r_shadow_shadowmaprectangletexture;
 rtexture_t *r_shadow_shadowmap2dtexture;
 rtexture_t *r_shadow_shadowmapcubetexture[R_SHADOW_SHADOWMAP_NUMCUBEMAPS];
@@ -359,14 +359,13 @@ void R_Shadow_EditLights_Reload_f(void);
 void R_Shadow_ValidateCvars(void);
 static void R_Shadow_MakeTextures(void);
 
-// VorteX: custom editor light sprites
 #define EDLIGHTSPRSIZE                 8
-cachepic_t *r_editlights_sprcursor;
-cachepic_t *r_editlights_sprlight;
-cachepic_t *r_editlights_sprnoshadowlight;
-cachepic_t *r_editlights_sprcubemaplight;
-cachepic_t *r_editlights_sprcubemapnoshadowlight;
-cachepic_t *r_editlights_sprselection;
+skinframe_t *r_editlights_sprcursor;
+skinframe_t *r_editlights_sprlight;
+skinframe_t *r_editlights_sprnoshadowlight;
+skinframe_t *r_editlights_sprcubemaplight;
+skinframe_t *r_editlights_sprcubemapnoshadowlight;
+skinframe_t *r_editlights_sprselection;
 extern cvar_t gl_max_size;
 
 void R_Shadow_SetShadowMode(void)
@@ -619,6 +618,13 @@ void r_shadow_shutdown(void)
 
 void r_shadow_newmap(void)
 {
+       if (r_shadow_lightcorona)                 R_SkinFrame_MarkUsed(r_shadow_lightcorona);
+       if (r_editlights_sprcursor)               R_SkinFrame_MarkUsed(r_editlights_sprcursor);
+       if (r_editlights_sprlight)                R_SkinFrame_MarkUsed(r_editlights_sprlight);
+       if (r_editlights_sprnoshadowlight)        R_SkinFrame_MarkUsed(r_editlights_sprnoshadowlight);
+       if (r_editlights_sprcubemaplight)         R_SkinFrame_MarkUsed(r_editlights_sprcubemaplight);
+       if (r_editlights_sprcubemapnoshadowlight) R_SkinFrame_MarkUsed(r_editlights_sprcubemapnoshadowlight);
+       if (r_editlights_sprselection)            R_SkinFrame_MarkUsed(r_editlights_sprselection);
        if (cl.worldmodel && strncmp(cl.worldmodel->name, r_shadow_mapname, sizeof(r_shadow_mapname)))
                R_Shadow_EditLights_Reload_f();
 }
@@ -1678,7 +1684,7 @@ static void R_Shadow_MakeTextures_MakeCorona(void)
                        pixels[y][x][3] = 255;
                }
        }
-       r_shadow_lightcorona = R_LoadTexture2D(r_shadow_texturepool, "lightcorona", 32, 32, &pixels[0][0][0], TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_FORCELINEAR, NULL);
+       r_shadow_lightcorona = R_SkinFrame_LoadInternalBGRA("lightcorona", TEXF_PRECACHE | TEXF_FORCELINEAR, &pixels[0][0][0], 32, 32);
 }
 
 static unsigned int R_Shadow_MakeTextures_SamplePoint(float x, float y, float z)
@@ -1732,12 +1738,114 @@ static void R_Shadow_MakeTextures(void)
        R_Shadow_MakeTextures_MakeCorona();
 
        // Editor light sprites
-       r_editlights_sprcursor = Draw_CachePic ("gfx/editlights/cursor");
-       r_editlights_sprlight = Draw_CachePic ("gfx/editlights/light");
-       r_editlights_sprnoshadowlight = Draw_CachePic ("gfx/editlights/noshadow");
-       r_editlights_sprcubemaplight = Draw_CachePic ("gfx/editlights/cubemaplight");
-       r_editlights_sprcubemapnoshadowlight = Draw_CachePic ("gfx/editlights/cubemapnoshadowlight");
-       r_editlights_sprselection = Draw_CachePic ("gfx/editlights/selection");
+       r_editlights_sprcursor = R_SkinFrame_LoadInternal8bit("gfx/editlights/cursor", TEXF_ALPHA | TEXF_CLAMP, (const unsigned char *)
+       "................"
+       ".3............3."
+       "..5...2332...5.."
+       "...7.3....3.7..."
+       "....7......7...."
+       "...3.7....7.3..."
+       "..2...7..7...2.."
+       "..3..........3.."
+       "..3..........3.."
+       "..2...7..7...2.."
+       "...3.7....7.3..."
+       "....7......7...."
+       "...7.3....3.7..."
+       "..5...2332...5.."
+       ".3............3."
+       "................"
+       , 16, 16, palette_bgra_embeddedpic, palette_bgra_embeddedpic);
+       r_editlights_sprlight = R_SkinFrame_LoadInternal8bit("gfx/editlights/light", TEXF_ALPHA | TEXF_CLAMP, (const unsigned char *)
+       "................"
+       "................"
+       "......1111......"
+       "....11233211...."
+       "...1234554321..."
+       "...1356776531..."
+       "..124677776421.."
+       "..135777777531.."
+       "..135777777531.."
+       "..124677776421.."
+       "...1356776531..."
+       "...1234554321..."
+       "....11233211...."
+       "......1111......"
+       "................"
+       "................"
+       , 16, 16, palette_bgra_embeddedpic, palette_bgra_embeddedpic);
+       r_editlights_sprnoshadowlight = R_SkinFrame_LoadInternal8bit("gfx/editlights/noshadow", TEXF_ALPHA | TEXF_CLAMP, (const unsigned char *)
+       "................"
+       "................"
+       "......1111......"
+       "....11233211...."
+       "...1234554321..."
+       "...1356226531..."
+       "..12462..26421.."
+       "..1352....2531.."
+       "..1352....2531.."
+       "..12462..26421.."
+       "...1356226531..."
+       "...1234554321..."
+       "....11233211...."
+       "......1111......"
+       "................"
+       "................"
+       , 16, 16, palette_bgra_embeddedpic, palette_bgra_embeddedpic);
+       r_editlights_sprcubemaplight = R_SkinFrame_LoadInternal8bit("gfx/editlights/cubemaplight", TEXF_ALPHA | TEXF_CLAMP, (const unsigned char *)
+       "................"
+       "................"
+       "......2772......"
+       "....27755772...."
+       "..277533335772.."
+       "..753333333357.."
+       "..777533335777.."
+       "..735775577537.."
+       "..733357753337.."
+       "..733337733337.."
+       "..753337733357.."
+       "..277537735772.."
+       "....27777772...."
+       "......2772......"
+       "................"
+       "................"
+       , 16, 16, palette_bgra_embeddedpic, palette_bgra_embeddedpic);
+       r_editlights_sprcubemapnoshadowlight = R_SkinFrame_LoadInternal8bit("gfx/editlights/cubemapnoshadowlight", TEXF_ALPHA | TEXF_CLAMP, (const unsigned char *)
+       "................"
+       "................"
+       "......2772......"
+       "....27722772...."
+       "..2772....2772.."
+       "..72........27.."
+       "..7772....2777.."
+       "..7.27722772.7.."
+       "..7...2772...7.."
+       "..7....77....7.."
+       "..72...77...27.."
+       "..2772.77.2772.."
+       "....27777772...."
+       "......2772......"
+       "................"
+       "................"
+       , 16, 16, palette_bgra_embeddedpic, palette_bgra_embeddedpic);
+       r_editlights_sprselection = R_SkinFrame_LoadInternal8bit("gfx/editlights/selection", TEXF_ALPHA | TEXF_CLAMP, (unsigned char *)
+       "................"
+       ".777752..257777."
+       ".742........247."
+       ".72..........27."
+       ".7............7."
+       ".5............5."
+       ".2............2."
+       "................"
+       "................"
+       ".2............2."
+       ".5............5."
+       ".7............7."
+       ".72..........27."
+       ".742........247."
+       ".777752..257777."
+       "................"
+       , 16, 16, palette_bgra_embeddedpic, palette_bgra_embeddedpic);
 }
 
 void R_Shadow_ValidateCvars(void)
@@ -4533,6 +4641,7 @@ void R_BeginCoronaQuery(rtlight_t *rtlight, float scale, qboolean usequery)
 {
        float zdist;
        vec3_t centerorigin;
+       float vertex3f[12];
        // if it's too close, skip it
        if (VectorLength(rtlight->currentcolor) < (1.0f / 256.0f))
                return;
@@ -4543,23 +4652,30 @@ void R_BeginCoronaQuery(rtlight_t *rtlight, float scale, qboolean usequery)
        {
                rtlight->corona_queryindex_allpixels = r_queries[r_numqueries++];
                rtlight->corona_queryindex_visiblepixels = r_queries[r_numqueries++];
+               // we count potential samples in the middle of the screen, we count actual samples at the light location, this allows counting potential samples of off-screen lights
                VectorMA(r_refdef.view.origin, zdist, r_refdef.view.forward, centerorigin);
 
                CHECKGLERROR
-               // NOTE: we can't disable depth testing using R_DrawSprite's depthdisable argument, which calls GL_DepthTest, as that's broken in the ATI drivers
+               // NOTE: GL_DEPTH_TEST must be enabled or ATI won't count samples, so use qglDepthFunc instead
                qglBeginQueryARB(GL_SAMPLES_PASSED_ARB, rtlight->corona_queryindex_allpixels);
                qglDepthFunc(GL_ALWAYS);
-               R_DrawSprite(GL_ONE, GL_ZERO, r_shadow_lightcorona, NULL, false, false, centerorigin, r_refdef.view.right, r_refdef.view.up, scale, -scale, -scale, scale, 1, 1, 1, 1);
+               R_CalcSprite_Vertex3f(vertex3f, centerorigin, r_refdef.view.right, r_refdef.view.up, scale, -scale, -scale, scale);
+               R_Mesh_VertexPointer(vertex3f, 0, 0);
+               R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, polygonelement3s, 0, 0);
                qglEndQueryARB(GL_SAMPLES_PASSED_ARB);
                qglDepthFunc(GL_LEQUAL);
                qglBeginQueryARB(GL_SAMPLES_PASSED_ARB, rtlight->corona_queryindex_visiblepixels);
-               R_DrawSprite(GL_ONE, GL_ZERO, r_shadow_lightcorona, NULL, false, false, rtlight->shadoworigin, r_refdef.view.right, r_refdef.view.up, scale, -scale, -scale, scale, 1, 1, 1, 1);
+               R_CalcSprite_Vertex3f(vertex3f, rtlight->shadoworigin, r_refdef.view.right, r_refdef.view.up, scale, -scale, -scale, scale);
+               R_Mesh_VertexPointer(vertex3f, 0, 0);
+               R_Mesh_Draw(0, 4, 0, 2, polygonelement3i, polygonelement3s, 0, 0);
                qglEndQueryARB(GL_SAMPLES_PASSED_ARB);
                CHECKGLERROR
        }
        rtlight->corona_visibility = bound(0, (zdist - 32) / 32, 1);
 }
 
+static float spritetexcoord2f[4*2] = {0, 1, 0, 0, 1, 0, 1, 1};
+
 void R_DrawCorona(rtlight_t *rtlight, float cscale, float scale)
 {
        vec3_t color;
@@ -4586,13 +4702,16 @@ void R_DrawCorona(rtlight_t *rtlight, float cscale, float scale)
        VectorScale(rtlight->currentcolor, cscale, color);
        if (VectorLength(color) > (1.0f / 256.0f))
        {
+               float vertex3f[12];
                qboolean negated = (color[0] + color[1] + color[2] < 0) && gl_support_ext_blend_subtract;
                if(negated)
                {
                        VectorNegate(color, color);
                        qglBlendEquationEXT(GL_FUNC_REVERSE_SUBTRACT_EXT);
                }
-               R_DrawSprite(GL_ONE, GL_ONE, r_shadow_lightcorona, NULL, true, false, rtlight->shadoworigin, r_refdef.view.right, r_refdef.view.up, scale, -scale, -scale, scale, color[0], color[1], color[2], 1);
+               R_CalcSprite_Vertex3f(vertex3f, rtlight->shadoworigin, r_refdef.view.right, r_refdef.view.up, scale, -scale, -scale, scale);
+               RSurf_ActiveCustomEntity(&identitymatrix, &identitymatrix, RENDER_NODEPTHTEST, 0, color[0], color[1], color[2], 1, 4, vertex3f, spritetexcoord2f, NULL, NULL, NULL, 2, polygonelement3i, polygonelement3s, false, false);
+               R_DrawCustomSurface(r_shadow_lightcorona, &identitymatrix, MATERIALFLAG_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_FULLBRIGHT | MATERIALFLAG_NOCULLFACE, 0, 4, 0, 2, false);
                if(negated)
                        qglBlendEquationEXT(GL_FUNC_ADD_EXT);
        }
@@ -4633,6 +4752,16 @@ void R_DrawCoronas(void)
                        qglGenQueriesARB(r_maxqueries - i, r_queries + i);
                        CHECKGLERROR
                }
+               RSurf_ActiveWorldEntity();
+               GL_BlendFunc(GL_ONE, GL_ZERO);
+               GL_CullFace(GL_NONE);
+               GL_DepthMask(false);
+               GL_DepthRange(0, 1);
+               GL_PolygonOffset(0, 0);
+               GL_DepthTest(true);
+               R_Mesh_ColorPointer(NULL, 0, 0);
+               R_Mesh_ResetTextureState();
+               R_SetupGenericShader(false);
        }
        for (lightindex = 0;lightindex < range;lightindex++)
        {
@@ -4906,7 +5035,10 @@ void R_Shadow_SelectLight(dlight_t *light)
 void R_Shadow_DrawCursor_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
 {
        // this is never batched (there can be only one)
-       R_DrawSprite(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, r_editlights_sprcursor->tex, r_editlights_sprcursor->tex, false, false, r_editlights_cursorlocation, r_refdef.view.right, r_refdef.view.up, EDLIGHTSPRSIZE, -EDLIGHTSPRSIZE, -EDLIGHTSPRSIZE, EDLIGHTSPRSIZE, 1, 1, 1, 1);
+       float vertex3f[12];
+       R_CalcSprite_Vertex3f(vertex3f, r_editlights_cursorlocation, r_refdef.view.right, r_refdef.view.up, EDLIGHTSPRSIZE, -EDLIGHTSPRSIZE, -EDLIGHTSPRSIZE, EDLIGHTSPRSIZE);
+       RSurf_ActiveCustomEntity(&identitymatrix, &identitymatrix, 0, 0, 1, 1, 1, 1, 4, vertex3f, spritetexcoord2f, NULL, NULL, NULL, 2, polygonelement3i, polygonelement3s, false, false);
+       R_DrawCustomSurface(r_editlights_sprcursor, &identitymatrix, MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_FULLBRIGHT | MATERIALFLAG_NOCULLFACE, 0, 4, 0, 2, false);
 }
 
 void R_Shadow_DrawLightSprite_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
@@ -4914,12 +5046,16 @@ void R_Shadow_DrawLightSprite_TransparentCallback(const entity_render_t *ent, co
        float intensity;
        float s;
        vec3_t spritecolor;
-       cachepic_t *pic;
+       skinframe_t *skinframe;
+       float vertex3f[12];
 
        // this is never batched (due to the ent parameter changing every time)
        // so numsurfaces == 1 and surfacelist[0] == lightnumber
        const dlight_t *light = (dlight_t *)ent;
        s = EDLIGHTSPRSIZE;
+
+       R_CalcSprite_Vertex3f(vertex3f, light->origin, r_refdef.view.right, r_refdef.view.up, s, -s, -s, s);
+
        intensity = 0.5f;
        VectorScale(light->color, intensity, spritecolor);
        if (VectorLength(spritecolor) < 0.1732f)
@@ -4929,18 +5065,24 @@ void R_Shadow_DrawLightSprite_TransparentCallback(const entity_render_t *ent, co
 
        // draw light sprite
        if (light->cubemapname[0] && !light->shadow)
-               pic = r_editlights_sprcubemapnoshadowlight;
+               skinframe = r_editlights_sprcubemapnoshadowlight;
        else if (light->cubemapname[0])
-               pic = r_editlights_sprcubemaplight;
+               skinframe = r_editlights_sprcubemaplight;
        else if (!light->shadow)
-               pic = r_editlights_sprnoshadowlight;
+               skinframe = r_editlights_sprnoshadowlight;
        else
-               pic = r_editlights_sprlight;
-       R_DrawSprite(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, pic->tex, pic->tex, false, false, light->origin, r_refdef.view.right, r_refdef.view.up, s, -s, -s, s, spritecolor[0], spritecolor[1], spritecolor[2], 1);
+               skinframe = r_editlights_sprlight;
+
+       RSurf_ActiveCustomEntity(&identitymatrix, &identitymatrix, 0, 0, spritecolor[0], spritecolor[1], spritecolor[2], 1, 4, vertex3f, spritetexcoord2f, NULL, NULL, NULL, 2, polygonelement3i, polygonelement3s, false, false);
+       R_DrawCustomSurface(skinframe, &identitymatrix, MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_FULLBRIGHT | MATERIALFLAG_NOCULLFACE, 0, 4, 0, 2, false);
+
        // draw selection sprite if light is selected
        if (light->selected)
-               R_DrawSprite(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, r_editlights_sprselection->tex, r_editlights_sprselection->tex, false, false, light->origin, r_refdef.view.right, r_refdef.view.up, s, -s, -s, s, 1, 1, 1, 1);
-       // VorteX todo: add normalmode/realtime mode light overlay sprites?
+       {
+               RSurf_ActiveCustomEntity(&identitymatrix, &identitymatrix, 0, 0, 1, 1, 1, 1, 4, vertex3f, spritetexcoord2f, NULL, NULL, NULL, 2, polygonelement3i, polygonelement3s, false, false);
+               R_DrawCustomSurface(r_editlights_sprselection, &identitymatrix, MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_FULLBRIGHT | MATERIALFLAG_NOCULLFACE, 0, 4, 0, 2, false);
+               // VorteX todo: add normalmode/realtime mode light overlay sprites?
+       }
 }
 
 void R_Shadow_DrawLightSprites(void)
diff --git a/r_sky.c b/r_sky.c
index c3160f2..c106962 100644 (file)
--- a/r_sky.c
+++ b/r_sky.c
@@ -50,7 +50,7 @@ static const suffixinfo_t suffix[3][6] =
        }
 };
 
-static rtexture_t *skyboxside[6];
+static skinframe_t *skyboxskinframe[6];
 
 void R_SkyStartFrame(void)
 {
@@ -59,11 +59,11 @@ void R_SkyStartFrame(void)
        skyrendermasked = false;
        // for depth-masked sky, we need to know whether any sky was rendered
        skyrenderlater = false;
-       if (r_sky.integer && !(r_refdef.fogenabled && r_refdef.fogmasktable[FOGMASKTABLEWIDTH-1] < (1.0f / 256.0f)))
+       if (r_sky.integer)
        {
-               if (skyboxside[0] || skyboxside[1] || skyboxside[2] || skyboxside[3] || skyboxside[4] || skyboxside[5])
+               if (skyboxskinframe[0] || skyboxskinframe[1] || skyboxskinframe[2] || skyboxskinframe[3] || skyboxskinframe[4] || skyboxskinframe[5])
                        skyrenderbox = true;
-               else if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.solidskytexture)
+               else if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.solidskyskinframe)
                        skyrendersphere = true;
                skyrendermasked = true;
        }
@@ -80,12 +80,12 @@ void R_UnloadSkyBox(void)
        int c = 0;
        for (i = 0;i < 6;i++)
        {
-               if (skyboxside[i])
+               if (skyboxskinframe[i])
                {
-                       R_FreeTexture(skyboxside[i]);
+                       // TODO: make a R_SkinFrame_Purge for single skins...
                        c++;
                }
-               skyboxside[i] = NULL;
+               skyboxskinframe[i] = NULL;
        }
        if (c && developer_loading.integer)
                Con_Printf("unloading skybox\n");
@@ -122,7 +122,7 @@ int R_LoadSkyBox(void)
                        }
                        temp = (unsigned char *)Mem_Alloc(tempmempool, image_width*image_height*4);
                        Image_CopyMux (temp, image_buffer, image_width, image_height, suffix[j][i].flipx, suffix[j][i].flipy, suffix[j][i].flipdiagonal, 4, 4, indices);
-                       skyboxside[i] = R_LoadTexture2D(skytexturepool, va("skyboxside%d", i), image_width, image_height, temp, TEXTYPE_BGRA, TEXF_CLAMP | TEXF_PRECACHE | (gl_texturecompression_sky.integer ? TEXF_COMPRESS : 0), NULL);
+                       skyboxskinframe[i] = R_SkinFrame_LoadInternalBGRA(va("skyboxside%d", i), TEXF_CLAMP | TEXF_PRECACHE | (gl_texturecompression_sky.integer ? TEXF_COMPRESS : 0), temp, image_width, image_height);
                        Mem_Free(image_buffer);
                        Mem_Free(temp);
                        success++;
@@ -300,39 +300,9 @@ static const unsigned short skyboxelement3s[6*2*3] =
 static void R_SkyBox(void)
 {
        int i;
-       // FIXME: fixed function path can't properly handle r_refdef.view.colorscale > 1
-       GL_Color(1 * r_refdef.view.colorscale, 1 * r_refdef.view.colorscale, 1 * r_refdef.view.colorscale, 1);
-       GL_BlendFunc(GL_ONE, GL_ZERO);
-       GL_CullFace(GL_NONE);
-       GL_DepthMask(false);
-       GL_DepthRange(0, 1);
-       GL_PolygonOffset(0, 0);
-       GL_DepthTest(false); // don't modify or read zbuffer
-       R_Mesh_VertexPointer(skyboxvertex3f, 0, 0);
-       R_Mesh_ColorPointer(NULL, 0, 0);
-       R_Mesh_ResetTextureState();
-       R_Mesh_TexCoordPointer(0, 2, skyboxtexcoord2f, 0, 0);
-       R_SetupGenericShader(true);
-       GL_LockArrays(0, 6*4);
+       RSurf_ActiveCustomEntity(&skymatrix, &skyinversematrix, 0, 0, 1, 1, 1, 1, 6*4, skyboxvertex3f, skyboxtexcoord2f, NULL, NULL, NULL, 6*2, skyboxelement3i, skyboxelement3s, false, false);
        for (i = 0;i < 6;i++)
-       {
-               R_Mesh_TexBind(0, R_GetTexture(skyboxside[i]));
-               R_Mesh_Draw(0, 6*4, i*2, 2, skyboxelement3i, skyboxelement3s, 0, 0);
-       }
-
-       if(r_refdef.fogenabled)
-       {
-               R_SetupGenericShader(false);
-               GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-               GL_Color(r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2], 1 - r_refdef.fogmasktable[FOGMASKTABLEWIDTH-1]);
-               for (i = 0;i < 6;i++)
-               {
-                       R_Mesh_TexBind(0, 0);
-                       R_Mesh_Draw(0, 6*4, i*2, 2, skyboxelement3i, skyboxelement3s, 0, 0);
-               }
-       }
-
-       GL_LockArrays(0, 0);
+               R_DrawCustomSurface(skyboxskinframe[i], &identitymatrix, MATERIALFLAG_FULLBRIGHT | MATERIALFLAG_NOCULLFACE | MATERIALFLAG_NODEPTHTEST, i*4, 4, i*2, 2, false);
 }
 
 #define skygridx 32
@@ -419,57 +389,9 @@ static void R_SkySphere(void)
        speedscale -= floor(speedscale);
        Matrix4x4_CreateTranslate(&scroll2matrix, speedscale, speedscale, 0);
 
-       // FIXME: fixed function path can't properly handle r_refdef.view.colorscale > 1
-       GL_Color(1 * r_refdef.view.colorscale, 1 * r_refdef.view.colorscale, 1 * r_refdef.view.colorscale, 1);
-       GL_BlendFunc(GL_ONE, GL_ZERO);
-       GL_CullFace(GL_NONE);
-       GL_DepthMask(true);
-       GL_DepthRange(0, 1);
-       GL_PolygonOffset(0, 0);
-       GL_DepthTest(false); // don't modify or read zbuffer
-       R_Mesh_VertexPointer(skysphere_vertex3f, 0, 0);
-       R_Mesh_ColorPointer(NULL, 0, 0);
-       R_Mesh_ResetTextureState();
-       R_Mesh_TexBind(0, R_GetTexture(r_refdef.scene.worldmodel->brush.solidskytexture));
-       R_Mesh_TexCoordPointer(0, 2, skysphere_texcoord2f, 0, 0);
-       R_Mesh_TexMatrix(0, &scroll1matrix);
-       if (r_textureunits.integer >= 2 && r_refdef.view.colorscale == 1)
-       {
-               // one pass using GL_DECAL or GL_INTERPOLATE_ARB for alpha layer
-               R_SetupGenericTwoTextureShader(GL_DECAL);
-               R_Mesh_TexBind(1, R_GetTexture(r_refdef.scene.worldmodel->brush.alphaskytexture));
-               R_Mesh_TexCoordPointer(1, 2, skysphere_texcoord2f, 0, 0);
-               R_Mesh_TexMatrix(1, &scroll2matrix);
-               GL_LockArrays(0, skysphere_numverts);
-               R_Mesh_Draw(0, skysphere_numverts, 0, skysphere_numtriangles, skysphere_element3i, skysphere_element3s, 0, 0);
-               GL_LockArrays(0, 0);
-               R_Mesh_TexBind(1, 0);
-       }
-       else
-       {
-               // two pass
-               R_SetupGenericShader(true);
-               GL_LockArrays(0, skysphere_numverts);
-               R_Mesh_Draw(0, skysphere_numverts, 0, skysphere_numtriangles, skysphere_element3i, skysphere_element3s, 0, 0);
-
-               GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-               R_Mesh_TexBind(0, R_GetTexture(r_refdef.scene.worldmodel->brush.alphaskytexture));
-               R_Mesh_TexMatrix(0, &scroll2matrix);
-               GL_LockArrays(0, skysphere_numverts);
-               R_Mesh_Draw(0, skysphere_numverts, 0, skysphere_numtriangles, skysphere_element3i, skysphere_element3s, 0, 0);
-               GL_LockArrays(0, 0);
-       }
-
-       if(r_refdef.fogenabled)
-       {
-               R_SetupGenericShader(false);
-               R_Mesh_TexBind(0, 0);
-               GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-               GL_Color(r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2], 1 - r_refdef.fogmasktable[FOGMASKTABLEWIDTH-1]);
-               GL_LockArrays(0, skysphere_numverts);
-               R_Mesh_Draw(0, skysphere_numverts, 0, skysphere_numtriangles, skysphere_element3i, skysphere_element3s, 0, 0);
-               GL_LockArrays(0, 0);
-       }
+       RSurf_ActiveCustomEntity(&skymatrix, &skyinversematrix, 0, 0, 1, 1, 1, 1, skysphere_numverts, skysphere_vertex3f, skysphere_texcoord2f, NULL, NULL, NULL, skysphere_numtriangles, skysphere_element3i, skysphere_element3s, false, false);
+       R_DrawCustomSurface(r_refdef.scene.worldmodel->brush.solidskyskinframe, &scroll1matrix, MATERIALFLAG_FULLBRIGHT | MATERIALFLAG_NOCULLFACE | MATERIALFLAG_NODEPTHTEST                                            , 0, skysphere_numverts, 0, skysphere_numtriangles, false);
+       R_DrawCustomSurface(r_refdef.scene.worldmodel->brush.alphaskyskinframe, &scroll2matrix, MATERIALFLAG_FULLBRIGHT | MATERIALFLAG_NOCULLFACE | MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED, 0, skysphere_numverts, 0, skysphere_numtriangles, false);
 }
 
 void R_Sky(void)
@@ -477,7 +399,6 @@ void R_Sky(void)
        Matrix4x4_CreateFromQuakeEntity(&skymatrix, r_refdef.view.origin[0], r_refdef.view.origin[1], r_refdef.view.origin[2], 0, 0, 0, r_refdef.farclip * (0.5f / 16.0f));
        Matrix4x4_Invert_Simple(&skyinversematrix, &skymatrix);
 
-       R_Mesh_Matrix(&skymatrix);
        if (skyrendersphere)
        {
                // this does not modify depth buffer
@@ -497,9 +418,6 @@ void R_Sky(void)
                //GL_Clear(GL_DEPTH_BUFFER_BIT);
        }
        */
-       GL_DepthRange(0, 1);
-       GL_DepthTest(true);
-       GL_DepthMask(true);
 }
 
 //===============================================================
@@ -534,7 +452,7 @@ void R_Sky_Init(void)
        Cvar_RegisterVariable (&r_sky);
        Cvar_RegisterVariable (&r_skyscroll1);
        Cvar_RegisterVariable (&r_skyscroll2);
-       memset(&skyboxside, 0, sizeof(skyboxside));
+       memset(&skyboxskinframe, 0, sizeof(skyboxskinframe));
        skyname[0] = 0;
        R_RegisterModule("R_Sky", r_sky_start, r_sky_shutdown, r_sky_newmap);
 }
index 1c632f7..ffbdf21 100644 (file)
@@ -199,6 +199,8 @@ void R_RotateSprite(const mspriteframe_t *frame, vec3_t origin, vec3_t left, vec
        }
 }
 
+static float spritetexcoord2f[4*2] = {0, 1, 0, 0, 1, 0, 1, 1};
+
 void R_Model_Sprite_Draw_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
 {
        int i;
@@ -207,9 +209,7 @@ void R_Model_Sprite_Draw_TransparentCallback(const entity_render_t *ent, const r
        float scale, dx, dy, hud_vs_screen;
        int edge = 0;
        float dir_angle = 0.0f;
-
-       // R_GetCurrentTexture needs this
-       rsurface.entity = (entity_render_t *)ent;
+       float vertex3f[12];
 
        // nudge it toward the view to make sure it isn't in a wall
        Matrix4x4_ToVectors(&ent->matrix, mforward, mleft, mup, org);
@@ -355,18 +355,16 @@ void R_Model_Sprite_Draw_TransparentCallback(const entity_render_t *ent, const r
                break;
        }
 
-       R_Mesh_Matrix(&identitymatrix);
-
        // LordHavoc: interpolated sprite rendering
        for (i = 0;i < MAX_FRAMEBLENDS;i++)
        {
                if (ent->frameblend[i].lerp >= 0.01f)
                {
-                       mspriteframe_t *frame = model->sprite.sprdata_frames + ent->frameblend[i].subframe;
-                       texture_t *texture = R_GetCurrentTexture(model->data_textures + ent->frameblend[i].subframe);
-#if 0
-                       vec3_t o, l, u;
-#endif
+                       mspriteframe_t *frame;
+                       texture_t *texture;
+                       RSurf_ActiveCustomEntity(&identitymatrix, &identitymatrix, ent->flags, 0, ent->colormod[0], ent->colormod[1], ent->colormod[2], ent->alpha * ent->frameblend[i].lerp, 4, vertex3f, spritetexcoord2f, NULL, NULL, NULL, 2, polygonelement3i, polygonelement3s, false, false);
+                       frame = model->sprite.sprdata_frames + ent->frameblend[i].subframe;
+                       texture = R_GetCurrentTexture(model->data_textures + ent->frameblend[i].subframe);
 
                        // SPR_LABEL should not use depth test AT ALL
                        if(model->sprite.sprnum_type == SPR_LABEL || model->sprite.sprnum_type == SPR_LABEL_SCALE)
@@ -376,26 +374,13 @@ void R_Model_Sprite_Draw_TransparentCallback(const entity_render_t *ent, const r
                        if(edge)
                        {
                                // FIXME:: save vectors/origin and re-rotate? necessary if the hotspot can change per frame
-#if 0
-                               VectorCopy(org, o);
-                               VectorCopy(left, l);
-                               VectorCopy(up, u);
-#endif
                                R_RotateSprite(frame, org, left, up, edge, dir_angle);
-#if 1
                                edge = 0;
-#endif
-                       }
-                       // FIXME: negate left and right in loader
-                       R_DrawSprite(texture->currentlayers[0].blendfunc1, texture->currentlayers[0].blendfunc2, texture->basetexture, texture->currentskinframe->fog, (texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST) != 0, (texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) != 0, org, left, up, frame->left, frame->right, frame->down, frame->up, texture->currentlayers[0].color[0], texture->currentlayers[0].color[1], texture->currentlayers[0].color[2], ent->alpha * ent->frameblend[i].lerp);
-                       if(edge)
-                       {
-#if 0
-                               VectorCopy(o, org);
-                               VectorCopy(l, left);
-                               VectorCopy(u, up);
-#endif
                        }
+
+                       R_CalcSprite_Vertex3f(vertex3f, org, left, up, frame->left, frame->right, frame->down, frame->up);
+
+                       R_DrawCustomSurface(texture->currentskinframe, &identitymatrix, texture->currentmaterialflags, 0, 4, 0, 2, false);
                }
        }
 
@@ -409,6 +394,6 @@ void R_Model_Sprite_Draw(entity_render_t *ent)
                return;
 
        Matrix4x4_OriginFromMatrix(&ent->matrix, org);
-       R_MeshQueue_AddTransparent(ent->effects & EF_NODEPTHTEST ? r_refdef.view.origin : org, R_Model_Sprite_Draw_TransparentCallback, ent, 0, rsurface.rtlight);
+       R_MeshQueue_AddTransparent(ent->flags & RENDER_NODEPTHTEST ? r_refdef.view.origin : org, R_Model_Sprite_Draw_TransparentCallback, ent, 0, rsurface.rtlight);
 }
 
index d5696a4..2d25b95 100644 (file)
--- a/render.h
+++ b/render.h
@@ -206,7 +206,7 @@ void R_TimeReport(char *name);
 void R_Stain(const vec3_t origin, float radius, int cr1, int cg1, int cb1, int ca1, int cr2, int cg2, int cb2, int ca2);
 
 void R_CalcBeam_Vertex3f(float *vert, const vec3_t org1, const vec3_t org2, float width);
-void R_DrawSprite(int blendfunc1, int blendfunc2, rtexture_t *texture, rtexture_t *fogtexture, qboolean depthdisable, qboolean depthshort, const vec3_t origin, const vec3_t left, const vec3_t up, float scalex1, float scalex2, float scaley1, float scaley2, float cr, float cg, float cb, float ca);
+void R_CalcSprite_Vertex3f(float *vertex3f, const vec3_t origin, const vec3_t left, const vec3_t up, float scalex1, float scalex2, float scaley1, float scaley2);
 
 extern mempool_t *r_main_mempool;
 
@@ -337,6 +337,9 @@ typedef struct rsurfacestate_s
        // fog plane in model space for direct application to vertices
        float fograngerecip;
        float fogmasktabledistmultiplier;
+       float fogplane[4];
+       float fogheightfade;
+       float fogplaneviewdist;
 
        // rtlight rendering
        // light currently being rendered
@@ -370,6 +373,7 @@ extern rsurfacestate_t rsurface;
 
 void RSurf_ActiveWorldEntity(void);
 void RSurf_ActiveModelEntity(const entity_render_t *ent, qboolean wantnormals, qboolean wanttangents);
+void RSurf_ActiveCustomEntity(const matrix4x4_t *matrix, const matrix4x4_t *inversematrix, int entflags, double shadertime, float r, float g, float b, float a, int numvertices, const float *vertex3f, const float *texcoord2f, const float *normal3f, const float *svector3f, const float *tvector3f, int numtriangles, const int *element3i, const unsigned short *element3s, qboolean wantnormals, qboolean wanttangents);
 void RSurf_SetupDepthAndCulling(void);
 
 void R_Mesh_ResizeArrays(int newvertices);
@@ -378,6 +382,7 @@ texture_t *R_GetCurrentTexture(texture_t *t);
 void R_DrawWorldSurfaces(qboolean skysurfaces, qboolean writedepth, qboolean depthonly, qboolean debug);
 void R_DrawModelSurfaces(entity_render_t *ent, qboolean skysurfaces, qboolean writedepth, qboolean depthonly, qboolean debug);
 void R_AddWaterPlanes(entity_render_t *ent);
+void R_DrawCustomSurface(skinframe_t *skinframe, const matrix4x4_t *texmatrix, int materialflags, int firstvertex, int numvertices, int firsttriangle, int numtriangles, qboolean writedepth);
 
 void RSurf_PrepareVerticesForBatch(qboolean generatenormals, qboolean generatetangents, int texturenumsurfaces, const msurface_t **texturesurfacelist);
 void RSurf_DrawBatch_Simple(int texturenumsurfaces, const msurface_t **texturesurfacelist);