]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - r_shadow.c
implemented occlusion query support on corona rendering, this enables
[xonotic/darkplaces.git] / r_shadow.c
index 8c98c5981fcaeebe0f9a9372c1368e994ffc88a8..cfb1d58a19956224e4c2e5bcd62b02d221b9bd0a 100644 (file)
@@ -178,6 +178,7 @@ int *vertexremap;
 int vertexupdatenum;
 
 int r_shadow_buffer_numleafpvsbytes;
+unsigned char *r_shadow_buffer_visitingleafpvs;
 unsigned char *r_shadow_buffer_leafpvs;
 int *r_shadow_buffer_leaflist;
 
@@ -210,6 +211,7 @@ cvar_t r_shadow_gloss = {CVAR_SAVE, "r_shadow_gloss", "1", "0 disables gloss (sp
 cvar_t r_shadow_gloss2intensity = {0, "r_shadow_gloss2intensity", "0.125", "how bright the forced flat gloss should look if r_shadow_gloss is 2"};
 cvar_t r_shadow_glossintensity = {0, "r_shadow_glossintensity", "1", "how bright textured glossmaps should look if r_shadow_gloss is 1 or 2"};
 cvar_t r_shadow_glossexponent = {0, "r_shadow_glossexponent", "32", "how 'sharp' the gloss should appear (specular power)"};
+cvar_t r_shadow_glossexact = {0, "r_shadow_glossexact", "1", "use exact reflection math for gloss (slightly slower, but should look a tad better)"};
 cvar_t r_shadow_lightattenuationdividebias = {0, "r_shadow_lightattenuationdividebias", "1", "changes attenuation texture generation"};
 cvar_t r_shadow_lightattenuationlinearscale = {0, "r_shadow_lightattenuationlinearscale", "2", "changes attenuation texture generation"};
 cvar_t r_shadow_lightintensityscale = {0, "r_shadow_lightintensityscale", "1", "renders all world lights brighter or darker"};
@@ -234,6 +236,8 @@ cvar_t r_shadow_polygonfactor = {0, "r_shadow_polygonfactor", "0", "how much to
 cvar_t r_shadow_polygonoffset = {0, "r_shadow_polygonoffset", "1", "how much to push shadow volumes into the distance when rendering, to reduce chances of zfighting artifacts (should not be less than 0)"};
 cvar_t r_shadow_texture3d = {0, "r_shadow_texture3d", "1", "use 3D voxel textures for spherical attenuation rather than cylindrical (does not affect r_glsl lighting)"};
 cvar_t r_coronas = {CVAR_SAVE, "r_coronas", "1", "brightness of corona flare effects around certain lights, 0 disables corona effects"};
+cvar_t r_coronas_occlusionsizescale = {CVAR_SAVE, "r_coronas_occlusionsizescale", "0.1", "size of light source for corona occlusion checksm the proportion of hidden pixels controls corona intensity"};
+cvar_t r_coronas_occlusionquery = {CVAR_SAVE, "r_coronas_occlusionquery", "1", "use GL_ARB_occlusion_query extension if supported (fades coronas according to visibility)"};
 cvar_t gl_flashblend = {CVAR_SAVE, "gl_flashblend", "0", "render bright coronas for dynamic lights instead of actual lighting, fast but ugly"};
 cvar_t gl_ext_separatestencil = {0, "gl_ext_separatestencil", "1", "make use of OpenGL 2.0 glStencilOpSeparate or GL_ATI_separate_stencil extension"};
 cvar_t gl_ext_stenciltwoside = {0, "gl_ext_stenciltwoside", "1", "make use of GL_EXT_stenciltwoside extension (NVIDIA only)"};
@@ -318,6 +322,7 @@ void r_shadow_start(void)
        shadowmarklist = NULL;
        shadowmarkcount = 0;
        r_shadow_buffer_numleafpvsbytes = 0;
+       r_shadow_buffer_visitingleafpvs = NULL;
        r_shadow_buffer_leafpvs = NULL;
        r_shadow_buffer_leaflist = NULL;
        r_shadow_buffer_numsurfacepvsbytes = 0;
@@ -363,6 +368,9 @@ void r_shadow_shutdown(void)
        shadowmarklist = NULL;
        shadowmarkcount = 0;
        r_shadow_buffer_numleafpvsbytes = 0;
+       if (r_shadow_buffer_visitingleafpvs)
+               Mem_Free(r_shadow_buffer_visitingleafpvs);
+       r_shadow_buffer_visitingleafpvs = NULL;
        if (r_shadow_buffer_leafpvs)
                Mem_Free(r_shadow_buffer_leafpvs);
        r_shadow_buffer_leafpvs = NULL;
@@ -435,6 +443,7 @@ void R_Shadow_Init(void)
        Cvar_RegisterVariable(&r_shadow_gloss2intensity);
        Cvar_RegisterVariable(&r_shadow_glossintensity);
        Cvar_RegisterVariable(&r_shadow_glossexponent);
+       Cvar_RegisterVariable(&r_shadow_glossexact);
        Cvar_RegisterVariable(&r_shadow_lightattenuationdividebias);
        Cvar_RegisterVariable(&r_shadow_lightattenuationlinearscale);
        Cvar_RegisterVariable(&r_shadow_lightintensityscale);
@@ -459,6 +468,8 @@ void R_Shadow_Init(void)
        Cvar_RegisterVariable(&r_shadow_polygonoffset);
        Cvar_RegisterVariable(&r_shadow_texture3d);
        Cvar_RegisterVariable(&r_coronas);
+       Cvar_RegisterVariable(&r_coronas_occlusionsizescale);
+       Cvar_RegisterVariable(&r_coronas_occlusionquery);
        Cvar_RegisterVariable(&gl_flashblend);
        Cvar_RegisterVariable(&gl_ext_separatestencil);
        Cvar_RegisterVariable(&gl_ext_stenciltwoside);
@@ -484,6 +495,7 @@ void R_Shadow_Init(void)
        shadowmarklist = NULL;
        shadowmarkcount = 0;
        r_shadow_buffer_numleafpvsbytes = 0;
+       r_shadow_buffer_visitingleafpvs = NULL;
        r_shadow_buffer_leafpvs = NULL;
        r_shadow_buffer_leaflist = NULL;
        r_shadow_buffer_numsurfacepvsbytes = 0;
@@ -542,11 +554,14 @@ static void R_Shadow_EnlargeLeafSurfaceTrisBuffer(int numleafs, int numsurfaces,
        int numlighttrispvsbytes = (((numlighttriangles + 7) >> 3) + 255) & ~255;
        if (r_shadow_buffer_numleafpvsbytes < numleafpvsbytes)
        {
+               if (r_shadow_buffer_visitingleafpvs)
+                       Mem_Free(r_shadow_buffer_visitingleafpvs);
                if (r_shadow_buffer_leafpvs)
                        Mem_Free(r_shadow_buffer_leafpvs);
                if (r_shadow_buffer_leaflist)
                        Mem_Free(r_shadow_buffer_leaflist);
                r_shadow_buffer_numleafpvsbytes = numleafpvsbytes;
+               r_shadow_buffer_visitingleafpvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numleafpvsbytes);
                r_shadow_buffer_leafpvs = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numleafpvsbytes);
                r_shadow_buffer_leaflist = (int *)Mem_Alloc(r_main_mempool, r_shadow_buffer_numleafpvsbytes * 8 * sizeof(*r_shadow_buffer_leaflist));
        }
@@ -613,28 +628,6 @@ int R_Shadow_ConstructShadowVolume(int innumvertices, int innumtris, const int *
        else
                VectorClear(projectvector);
 
-       if (maxvertexupdate < innumvertices)
-       {
-               maxvertexupdate = innumvertices;
-               if (vertexupdate)
-                       Mem_Free(vertexupdate);
-               if (vertexremap)
-                       Mem_Free(vertexremap);
-               vertexupdate = (int *)Mem_Alloc(r_main_mempool, maxvertexupdate * sizeof(int));
-               vertexremap = (int *)Mem_Alloc(r_main_mempool, maxvertexupdate * sizeof(int));
-               vertexupdatenum = 0;
-       }
-       vertexupdatenum++;
-       if (vertexupdatenum == 0)
-       {
-               vertexupdatenum = 1;
-               memset(vertexupdate, 0, maxvertexupdate * sizeof(int));
-               memset(vertexremap, 0, maxvertexupdate * sizeof(int));
-       }
-
-       for (i = 0;i < numshadowmarktris;i++)
-               shadowmark[shadowmarktris[i]] = shadowmarkcount;
-
        // create the vertices
        if (projectdirection)
        {
@@ -878,36 +871,9 @@ void R_Shadow_MarkVolumeFromBox(int firsttriangle, int numtris, const float *inv
        }
 }
 
-static void R_Shadow_RenderVolume(int numvertices, int numtriangles, const float *vertex3f, const int *element3i)
+void R_Shadow_VolumeFromList(int numverts, int numtris, const float *invertex3f, const int *elements, const int *neighbors, const vec3_t projectorigin, const vec3_t projectdirection, float projectdistance, int nummarktris, const int *marktris, vec3_t trismins, vec3_t trismaxs)
 {
-       if (r_shadow_compilingrtlight)
-       {
-               // if we're compiling an rtlight, capture the mesh
-               Mod_ShadowMesh_AddMesh(r_main_mempool, r_shadow_compilingrtlight->static_meshchain_shadow, NULL, NULL, NULL, vertex3f, NULL, NULL, NULL, NULL, numtriangles, element3i);
-               return;
-       }
-       r_refdef.stats.lights_shadowtriangles += numtriangles;
-       CHECKGLERROR
-       R_Mesh_VertexPointer(vertex3f, 0, 0);
-       GL_LockArrays(0, numvertices);
-       if (r_shadow_rendermode == R_SHADOW_RENDERMODE_STENCIL)
-       {
-               // decrement stencil if backface is behind depthbuffer
-               GL_CullFace(r_refdef.view.cullface_front);
-               qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR
-               R_Mesh_Draw(0, numvertices, 0, numtriangles, element3i, NULL, 0, 0);
-               // increment stencil if frontface is behind depthbuffer
-               GL_CullFace(r_refdef.view.cullface_back);
-               qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);CHECKGLERROR
-       }
-       R_Mesh_Draw(0, numvertices, 0, numtriangles, element3i, NULL, 0, 0);
-       GL_LockArrays(0, 0);
-       CHECKGLERROR
-}
-
-void R_Shadow_VolumeFromList(int numverts, int numtris, const float *invertex3f, const int *elements, const int *neighbors, const vec3_t projectorigin, const vec3_t projectdirection, float projectdistance, int nummarktris, const int *marktris)
-{
-       int tris, outverts;
+       int i, tris, outverts;
        if (projectdistance < 0.1)
        {
                Con_Printf("R_Shadow_Volume: projectdistance %f\n", projectdistance);
@@ -918,9 +884,57 @@ void R_Shadow_VolumeFromList(int numverts, int numtris, const float *invertex3f,
        // make sure shadowelements is big enough for this volume
        if (maxshadowtriangles < nummarktris || maxshadowvertices < numverts)
                R_Shadow_ResizeShadowArrays((numverts + 255) & ~255, (nummarktris + 255) & ~255);
-       tris = R_Shadow_ConstructShadowVolume(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdirection, projectdistance, nummarktris, marktris);
-       r_refdef.stats.lights_dynamicshadowtriangles += tris;
-       R_Shadow_RenderVolume(outverts, tris, shadowvertex3f, shadowelements);
+
+       if (maxvertexupdate < numverts)
+       {
+               maxvertexupdate = numverts;
+               if (vertexupdate)
+                       Mem_Free(vertexupdate);
+               if (vertexremap)
+                       Mem_Free(vertexremap);
+               vertexupdate = (int *)Mem_Alloc(r_main_mempool, maxvertexupdate * sizeof(int));
+               vertexremap = (int *)Mem_Alloc(r_main_mempool, maxvertexupdate * sizeof(int));
+               vertexupdatenum = 0;
+       }
+       vertexupdatenum++;
+       if (vertexupdatenum == 0)
+       {
+               vertexupdatenum = 1;
+               memset(vertexupdate, 0, maxvertexupdate * sizeof(int));
+               memset(vertexremap, 0, maxvertexupdate * sizeof(int));
+       }
+
+       for (i = 0;i < nummarktris;i++)
+               shadowmark[marktris[i]] = shadowmarkcount;
+
+       if (r_shadow_compilingrtlight)
+       {
+               // if we're compiling an rtlight, capture the mesh
+               tris = R_Shadow_ConstructShadowVolume(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdirection, projectdistance, nummarktris, marktris);
+               Mod_ShadowMesh_AddMesh(r_main_mempool, r_shadow_compilingrtlight->static_meshchain_shadow, NULL, NULL, NULL, shadowvertex3f, NULL, NULL, NULL, NULL, tris, shadowelements);
+       }
+       else
+       {
+               tris = R_Shadow_ConstructShadowVolume(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdirection, projectdistance, nummarktris, marktris);
+               r_refdef.stats.lights_dynamicshadowtriangles += tris;
+               r_refdef.stats.lights_shadowtriangles += tris;
+               CHECKGLERROR
+               R_Mesh_VertexPointer(shadowvertex3f, 0, 0);
+               GL_LockArrays(0, outverts);
+               if (r_shadow_rendermode == R_SHADOW_RENDERMODE_STENCIL)
+               {
+                       // decrement stencil if backface is behind depthbuffer
+                       GL_CullFace(r_refdef.view.cullface_front);
+                       qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR
+                       R_Mesh_Draw(0, outverts, 0, tris, shadowelements, NULL, 0, 0);
+                       // increment stencil if frontface is behind depthbuffer
+                       GL_CullFace(r_refdef.view.cullface_back);
+                       qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);CHECKGLERROR
+               }
+               R_Mesh_Draw(0, outverts, 0, tris, shadowelements, NULL, 0, 0);
+               GL_LockArrays(0, 0);
+               CHECKGLERROR
+       }
 }
 
 static void R_Shadow_MakeTextures_MakeCorona(void)
@@ -1081,7 +1095,14 @@ void R_Shadow_RenderMode_Reset(void)
        R_SetupGenericShader(false);
 }
 
-void R_Shadow_RenderMode_StencilShadowVolumes(qboolean clearstencil)
+void R_Shadow_ClearStencil(void)
+{
+       CHECKGLERROR
+       GL_Clear(GL_STENCIL_BUFFER_BIT);
+       r_refdef.stats.lights_clears++;
+}
+
+void R_Shadow_RenderMode_StencilShadowVolumes(void)
 {
        CHECKGLERROR
        R_Shadow_RenderMode_Reset();
@@ -1108,9 +1129,6 @@ void R_Shadow_RenderMode_StencilShadowVolumes(qboolean clearstencil)
                qglStencilMask(~0);CHECKGLERROR
                qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR
        }
-       if (clearstencil)
-               GL_Clear(GL_STENCIL_BUFFER_BIT);
-       r_refdef.stats.lights_clears++;
 }
 
 void R_Shadow_RenderMode_Lighting(qboolean stenciltest, qboolean transparent)
@@ -2468,7 +2486,7 @@ void R_RTLight_Compile(rtlight_t *rtlight)
                // this variable must be set for the CompileShadowVolume code
                r_shadow_compilingrtlight = rtlight;
                R_Shadow_EnlargeLeafSurfaceTrisBuffer(model->brush.num_leafs, model->num_surfaces, model->brush.shadowmesh ? model->brush.shadowmesh->numtriangles : model->surfmesh.num_triangles, model->surfmesh.num_triangles);
-               model->GetLightInfo(ent, rtlight->shadoworigin, rtlight->radius, rtlight->cullmins, rtlight->cullmaxs, r_shadow_buffer_leaflist, r_shadow_buffer_leafpvs, &numleafs, r_shadow_buffer_surfacelist, r_shadow_buffer_surfacepvs, &numsurfaces, r_shadow_buffer_shadowtrispvs, r_shadow_buffer_lighttrispvs);
+               model->GetLightInfo(ent, rtlight->shadoworigin, rtlight->radius, rtlight->cullmins, rtlight->cullmaxs, r_shadow_buffer_leaflist, r_shadow_buffer_leafpvs, &numleafs, r_shadow_buffer_surfacelist, r_shadow_buffer_surfacepvs, &numsurfaces, r_shadow_buffer_shadowtrispvs, r_shadow_buffer_lighttrispvs, r_shadow_buffer_visitingleafpvs);
                numleafpvsbytes = (model->brush.num_leafs + 7) >> 3;
                numshadowtrispvsbytes = ((model->brush.shadowmesh ? model->brush.shadowmesh->numtriangles : model->surfmesh.num_triangles) + 7) >> 3;
                numlighttrispvsbytes = (model->surfmesh.num_triangles + 7) >> 3;
@@ -2560,9 +2578,10 @@ void R_Shadow_UncompileWorldLights(void)
 {
        size_t lightindex;
        dlight_t *light;
-       for (lightindex = 0;lightindex < Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray);lightindex++)
+       size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
+       for (lightindex = 0;lightindex < range;lightindex++)
        {
-               light = Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
+               light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
                if (!light)
                        continue;
                R_RTLight_Uncompile(&light->rtlight);
@@ -2775,7 +2794,7 @@ void R_Shadow_DrawWorldShadow(int numsurfaces, int *surfacelist, const unsigned
                                if (CHECKPVSBIT(trispvs, t))
                                        shadowmarklist[numshadowmark++] = t;
                }
-               R_Shadow_VolumeFromList(r_refdef.scene.worldmodel->brush.shadowmesh->numverts, r_refdef.scene.worldmodel->brush.shadowmesh->numtriangles, r_refdef.scene.worldmodel->brush.shadowmesh->vertex3f, r_refdef.scene.worldmodel->brush.shadowmesh->element3i, r_refdef.scene.worldmodel->brush.shadowmesh->neighbor3i, rsurface.rtlight->shadoworigin, NULL, rsurface.rtlight->radius + r_refdef.scene.worldmodel->radius*2 + r_shadow_projectdistance.value, numshadowmark, shadowmarklist);
+               R_Shadow_VolumeFromList(r_refdef.scene.worldmodel->brush.shadowmesh->numverts, r_refdef.scene.worldmodel->brush.shadowmesh->numtriangles, r_refdef.scene.worldmodel->brush.shadowmesh->vertex3f, r_refdef.scene.worldmodel->brush.shadowmesh->element3i, r_refdef.scene.worldmodel->brush.shadowmesh->neighbor3i, rsurface.rtlight->shadoworigin, NULL, rsurface.rtlight->radius + r_refdef.scene.worldmodel->radius*2 + r_shadow_projectdistance.value, numshadowmark, shadowmarklist, r_refdef.scene.worldmodel->normalmins, r_refdef.scene.worldmodel->normalmaxs);
        }
        else if (numsurfaces)
                r_refdef.scene.worldmodel->DrawShadowVolume(r_refdef.scene.worldentity, rsurface.rtlight->shadoworigin, NULL, rsurface.rtlight->radius, numsurfaces, surfacelist, rsurface.rtlight_cullmins, rsurface.rtlight_cullmaxs);
@@ -2850,10 +2869,10 @@ void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
        int numlightentities_noselfshadow;
        int numshadowentities;
        int numshadowentities_noselfshadow;
-       entity_render_t *lightentities[MAX_EDICTS];
-       entity_render_t *lightentities_noselfshadow[MAX_EDICTS];
-       entity_render_t *shadowentities[MAX_EDICTS];
-       entity_render_t *shadowentities_noselfshadow[MAX_EDICTS];
+       static entity_render_t *lightentities[MAX_EDICTS];
+       static entity_render_t *lightentities_noselfshadow[MAX_EDICTS];
+       static entity_render_t *shadowentities[MAX_EDICTS];
+       static entity_render_t *shadowentities_noselfshadow[MAX_EDICTS];
 
        // skip lights that don't light because of ambientscale+diffusescale+specularscale being 0 (corona only lights)
        // skip lights that are basically invisible (color 0 0 0)
@@ -2908,7 +2927,7 @@ void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
                // dynamic light, world available and can receive realtime lighting
                // calculate lit surfaces and leafs
                R_Shadow_EnlargeLeafSurfaceTrisBuffer(r_refdef.scene.worldmodel->brush.num_leafs, r_refdef.scene.worldmodel->num_surfaces, r_refdef.scene.worldmodel->brush.shadowmesh ? r_refdef.scene.worldmodel->brush.shadowmesh->numtriangles : r_refdef.scene.worldmodel->surfmesh.num_triangles, r_refdef.scene.worldmodel->surfmesh.num_triangles);
-               r_refdef.scene.worldmodel->GetLightInfo(r_refdef.scene.worldentity, rtlight->shadoworigin, rtlight->radius, rsurface.rtlight_cullmins, rsurface.rtlight_cullmaxs, r_shadow_buffer_leaflist, r_shadow_buffer_leafpvs, &numleafs, r_shadow_buffer_surfacelist, r_shadow_buffer_surfacepvs, &numsurfaces, r_shadow_buffer_shadowtrispvs, r_shadow_buffer_lighttrispvs);
+               r_refdef.scene.worldmodel->GetLightInfo(r_refdef.scene.worldentity, rtlight->shadoworigin, rtlight->radius, rsurface.rtlight_cullmins, rsurface.rtlight_cullmaxs, r_shadow_buffer_leaflist, r_shadow_buffer_leafpvs, &numleafs, r_shadow_buffer_surfacelist, r_shadow_buffer_surfacepvs, &numsurfaces, r_shadow_buffer_shadowtrispvs, r_shadow_buffer_lighttrispvs, r_shadow_buffer_visitingleafpvs);
                leaflist = r_shadow_buffer_leaflist;
                leafpvs = r_shadow_buffer_leafpvs;
                surfacelist = r_shadow_buffer_surfacelist;
@@ -3045,7 +3064,8 @@ void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
        {
                // draw stencil shadow volumes to mask off pixels that are in shadow
                // so that they won't receive lighting
-               R_Shadow_RenderMode_StencilShadowVolumes(true);
+               R_Shadow_ClearStencil();
+               R_Shadow_RenderMode_StencilShadowVolumes();
                if (numsurfaces)
                        R_Shadow_DrawWorldShadow(numsurfaces, surfacelist, shadowtrispvs);
                for (i = 0;i < numshadowentities;i++)
@@ -3066,7 +3086,7 @@ void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
                                        R_Shadow_DrawEntityLight(lightentities_noselfshadow[i]);
                        }
 
-                       R_Shadow_RenderMode_StencilShadowVolumes(false);
+                       R_Shadow_RenderMode_StencilShadowVolumes();
                }
                for (i = 0;i < numshadowentities_noselfshadow;i++)
                        R_Shadow_DrawEntityShadow(shadowentities_noselfshadow[i]);
@@ -3128,6 +3148,7 @@ void R_ShadowVolumeLighting(qboolean visible)
        int lnum;
        size_t lightindex;
        dlight_t *light;
+       size_t range;
 
        if (r_editlights.integer)
                R_Shadow_DrawLightSprites();
@@ -3138,15 +3159,16 @@ void R_ShadowVolumeLighting(qboolean visible)
        if (r_shadow_debuglight.integer >= 0)
        {
                lightindex = r_shadow_debuglight.integer;
-               light = Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
+               light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
                if (light && (light->flags & flag))
                        R_DrawRTLight(&light->rtlight, visible);
        }
        else
        {
-               for (lightindex = 0;lightindex < Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray);lightindex++)
+               range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
+               for (lightindex = 0;lightindex < range;lightindex++)
                {
-                       light = Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
+                       light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
                        if (light && (light->flags & flag))
                                R_DrawRTLight(&light->rtlight, visible);
                }
@@ -3159,6 +3181,7 @@ void R_ShadowVolumeLighting(qboolean visible)
 }
 
 extern void R_SetupView(qboolean allowwaterclippingplane);
+extern cvar_t r_shadows;
 extern cvar_t r_shadows_throwdistance;
 void R_DrawModelShadows(void)
 {
@@ -3168,6 +3191,7 @@ void R_DrawModelShadows(void)
        vec3_t relativelightorigin;
        vec3_t relativelightdirection;
        vec3_t relativeshadowmins, relativeshadowmaxs;
+       vec3_t tmp;
        float vertex3f[12];
 
        if (!r_drawentities.integer || !gl_stencil)
@@ -3185,7 +3209,8 @@ void R_DrawModelShadows(void)
        else
                r_shadow_shadowingrendermode = R_SHADOW_RENDERMODE_STENCIL;
 
-       R_Shadow_RenderMode_StencilShadowVolumes(true);
+       R_Shadow_ClearStencil();
+       R_Shadow_RenderMode_StencilShadowVolumes();
 
        for (i = 0;i < r_refdef.scene.numentities;i++)
        {
@@ -3196,7 +3221,42 @@ void R_DrawModelShadows(void)
                        relativethrowdistance = r_shadows_throwdistance.value * Matrix4x4_ScaleFromMatrix(&ent->inversematrix);
                        VectorSet(relativeshadowmins, -relativethrowdistance, -relativethrowdistance, -relativethrowdistance);
                        VectorSet(relativeshadowmaxs, relativethrowdistance, relativethrowdistance, relativethrowdistance);
-                       VectorNegate(ent->modellight_lightdir, relativelightdirection);
+
+                       if(r_shadows.integer == 2)
+                       {
+                               // 2: simpler mode, throw shadows always DOWN
+                               VectorSet(tmp, 0, 0, -1);
+                               Matrix4x4_Transform3x3(&ent->inversematrix, tmp, relativelightdirection);
+                       }
+                       else
+                       {
+                               if(ent->entitynumber != 0)
+                               {
+                                       // networked entity - might be attached in some way (then we should use the parent's light direction, to not tear apart attached entities)
+                                       int entnum, entnum2, recursion;
+                                       entnum = entnum2 = ent->entitynumber;
+                                       for(recursion = 32; recursion > 0; --recursion)
+                                       {
+                                               entnum2 = cl.entities[entnum].state_current.tagentity;
+                                               if(entnum2 >= 1 && entnum2 < cl.num_entities && cl.entities_active[entnum2])
+                                                       entnum = entnum2;
+                                               else
+                                                       break;
+                                       }
+                                       if(recursion && recursion != 32) // if we followed a valid non-empty attachment chain
+                                       {
+                                               VectorNegate(cl.entities[entnum].render.modellight_lightdir, relativelightdirection);
+                                               // transform into modelspace of OUR entity
+                                               Matrix4x4_Transform3x3(&cl.entities[entnum].render.matrix, relativelightdirection, tmp);
+                                               Matrix4x4_Transform3x3(&ent->inversematrix, tmp, relativelightdirection);
+                                       }
+                                       else
+                                               VectorNegate(ent->modellight_lightdir, relativelightdirection);
+                               }
+                               else
+                                       VectorNegate(ent->modellight_lightdir, relativelightdirection);
+                       }
+
                        VectorScale(relativelightdirection, -relativethrowdistance, relativelightorigin);
                        RSurf_ActiveModelEntity(ent, false, false);
                        ent->model->DrawShadowVolume(ent, relativelightorigin, relativelightdirection, relativethrowdistance, ent->model->nummodelsurfaces, ent->model->surfacelist, relativeshadowmins, relativeshadowmaxs);
@@ -3246,62 +3306,145 @@ void R_DrawModelShadows(void)
        R_Shadow_RenderMode_End();
 }
 
+void R_BeginCoronaQuery(rtlight_t *rtlight, float scale, qboolean usequery)
+{
+       // if it's too close, skip it
+       if (VectorLength(rtlight->color) < (1.0f / 256.0f))
+               return;
+       if (VectorDistance2(rtlight->shadoworigin, r_refdef.view.origin) < 32.0f * 32.0f)
+               return;
+       if (usequery && r_numqueries + 2 <= r_maxqueries)
+       {
+               rtlight->corona_queryindex_allpixels = r_queries[r_numqueries++];
+               rtlight->corona_queryindex_visiblepixels = r_queries[r_numqueries++];
+               CHECKGLERROR
+               qglBeginQueryARB(GL_SAMPLES_PASSED_ARB, rtlight->corona_queryindex_allpixels);
+               R_DrawSprite(GL_ONE, GL_ZERO, r_shadow_lightcorona, NULL, true, false, rtlight->shadoworigin, r_refdef.view.right, r_refdef.view.up, scale, -scale, -scale, scale, 1, 1, 1, 1);
+               qglEndQueryARB(GL_SAMPLES_PASSED_ARB);
+               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);
+               qglEndQueryARB(GL_SAMPLES_PASSED_ARB);
+               CHECKGLERROR
+       }
+       rtlight->corona_visibility = 1;
+}
+
+void R_DrawCorona(rtlight_t *rtlight, float cscale, float scale)
+{
+       vec3_t color;
+       GLint allpixels = 0, visiblepixels = 0;
+       // now we have to check the query result
+       if (rtlight->corona_queryindex_visiblepixels)
+       {
+               CHECKGLERROR
+               qglGetQueryObjectivARB(rtlight->corona_queryindex_visiblepixels, GL_QUERY_RESULT_ARB, &visiblepixels);
+               qglGetQueryObjectivARB(rtlight->corona_queryindex_allpixels, GL_QUERY_RESULT_ARB, &allpixels);
+               CHECKGLERROR
+               //Con_Printf("%i of %i pixels\n", (int)visiblepixels, (int)allpixels);
+               if (visiblepixels < 1 || allpixels < 1)
+                       return;
+               rtlight->corona_visibility *= (float)visiblepixels / (float)allpixels;
+               cscale *= rtlight->corona_visibility;
+       }
+       else
+       {
+               // FIXME: these traces should scan all render entities instead of cl.world
+               if (CL_Move(r_refdef.view.origin, vec3_origin, vec3_origin, rtlight->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, true, false, NULL, false).fraction < 1)
+                       return;
+       }
+       VectorScale(rtlight->color, cscale, color);
+       if (VectorLength(color) > (1.0f / 256.0f))
+               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);
+}
+
 void R_DrawCoronas(void)
 {
        int i, flag;
-       float cscale, scale;
+       qboolean usequery;
        size_t lightindex;
        dlight_t *light;
        rtlight_t *rtlight;
+       size_t range;
        if (r_coronas.value < (1.0f / 256.0f) && !gl_flashblend.integer)
                return;
-       R_Mesh_Matrix(&identitymatrix);
+       if (r_waterstate.renderingscene)
+               return;
        flag = r_refdef.scene.rtworld ? LIGHTFLAG_REALTIMEMODE : LIGHTFLAG_NORMALMODE;
-       // FIXME: these traces should scan all render entities instead of cl.world
-       for (lightindex = 0;lightindex < Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray);lightindex++)
+       R_Mesh_Matrix(&identitymatrix);
+
+       range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
+
+       // check occlusion of coronas
+       // use GL_ARB_occlusion_query if available
+       // otherwise use raytraces
+       r_numqueries = 0;
+       usequery = gl_support_arb_occlusion_query && r_coronas_occlusionquery.integer;
+       if (usequery)
        {
-               light = Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
+               GL_ColorMask(0,0,0,0);
+               if (r_maxqueries < range + r_refdef.scene.numlights)
+               if (r_maxqueries < R_MAX_OCCLUSION_QUERIES)
+               {
+                       i = r_maxqueries;
+                       r_maxqueries = (range + r_refdef.scene.numlights) * 2;
+                       r_maxqueries = min(r_maxqueries, R_MAX_OCCLUSION_QUERIES);
+                       CHECKGLERROR
+                       qglGenQueriesARB(r_maxqueries - i, r_queries + i);
+                       CHECKGLERROR
+               }
+       }
+       for (lightindex = 0;lightindex < range;lightindex++)
+       {
+               light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
                if (!light)
                        continue;
                rtlight = &light->rtlight;
+               rtlight->corona_visibility = 0;
+               rtlight->corona_queryindex_visiblepixels = 0;
+               rtlight->corona_queryindex_allpixels = 0;
                if (!(rtlight->flags & flag))
                        continue;
-               if (rtlight->corona * r_coronas.value <= 0)
+               if (rtlight->corona <= 0)
                        continue;
                if (r_shadow_debuglight.integer >= 0 && r_shadow_debuglight.integer != (int)lightindex)
                        continue;
-               cscale = rtlight->corona * r_coronas.value* 0.25f;
-               scale = rtlight->radius * rtlight->coronasizescale;
-               if (VectorDistance2(rtlight->shadoworigin, r_refdef.view.origin) < 16.0f * 16.0f)
-                       continue;
-               if (CL_Move(r_refdef.view.origin, vec3_origin, vec3_origin, rtlight->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, true, false, NULL, false).fraction < 1)
-                       continue;
-               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, rtlight->color[0] * cscale, rtlight->color[1] * cscale, rtlight->color[2] * cscale, 1);
+               R_BeginCoronaQuery(rtlight, rtlight->radius * rtlight->coronasizescale * r_coronas_occlusionsizescale.value, usequery);
        }
        for (i = 0;i < r_refdef.scene.numlights;i++)
        {
                rtlight = &r_refdef.scene.lights[i];
+               rtlight->corona_visibility = 0;
+               rtlight->corona_queryindex_visiblepixels = 0;
+               rtlight->corona_queryindex_allpixels = 0;
                if (!(rtlight->flags & flag))
                        continue;
                if (rtlight->corona <= 0)
                        continue;
-               if (VectorDistance2(rtlight->shadoworigin, r_refdef.view.origin) < 32.0f * 32.0f)
+               R_BeginCoronaQuery(rtlight, rtlight->radius * rtlight->coronasizescale * r_coronas_occlusionsizescale.value, usequery);
+       }
+       if (usequery)
+               GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 1);
+
+       // now draw the coronas using the query data for intensity info
+       for (lightindex = 0;lightindex < range;lightindex++)
+       {
+               light = Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
+               if (!light)
                        continue;
-               if (gl_flashblend.integer)
-               {
-                       cscale = rtlight->corona * 1.0f;
-                       scale = rtlight->radius * rtlight->coronasizescale * 2.0f;
-               }
-               else
-               {
-                       cscale = rtlight->corona * r_coronas.value* 0.25f;
-                       scale = rtlight->radius * rtlight->coronasizescale;
-               }
-               if (VectorLength(rtlight->color) * cscale < (1.0f / 256.0f))
+               rtlight = &light->rtlight;
+               if (rtlight->corona_visibility <= 0)
                        continue;
-               if (CL_Move(r_refdef.view.origin, vec3_origin, vec3_origin, rtlight->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, true, false, NULL, false).fraction < 1)
+               R_DrawCorona(rtlight, rtlight->corona * r_coronas.value * 0.25f, rtlight->radius * rtlight->coronasizescale);
+       }
+       for (i = 0;i < r_refdef.scene.numlights;i++)
+       {
+               rtlight = &r_refdef.scene.lights[i];
+               if (rtlight->corona_visibility <= 0)
                        continue;
-               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, rtlight->color[0] * cscale, rtlight->color[1] * cscale, rtlight->color[2] * cscale, 1);
+               if (gl_flashblend.integer)
+                       R_DrawCorona(rtlight, rtlight->corona, rtlight->radius * rtlight->coronasizescale * 2.0f);
+               else
+                       R_DrawCorona(rtlight, rtlight->corona * r_coronas.value * 0.25f, rtlight->radius * rtlight->coronasizescale);
        }
 }
 
@@ -3494,9 +3637,10 @@ void R_Shadow_ClearWorldLights(void)
 {
        size_t lightindex;
        dlight_t *light;
-       for (lightindex = 0;lightindex < Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray);lightindex++)
+       size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
+       for (lightindex = 0;lightindex < range;lightindex++)
        {
-               light = Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
+               light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
                if (light)
                        R_Shadow_FreeWorldLight(light);
        }
@@ -3557,9 +3701,10 @@ void R_Shadow_DrawLightSprites(void)
 {
        size_t lightindex;
        dlight_t *light;
-       for (lightindex = 0;lightindex < Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray);lightindex++)
+       size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
+       for (lightindex = 0;lightindex < range;lightindex++)
        {
-               light = Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
+               light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
                if (light)
                        R_MeshQueue_AddTransparent(light->origin, R_Shadow_DrawLightSprite_TransparentCallback, (entity_render_t *)light, 5, &light->rtlight);
        }
@@ -3572,11 +3717,12 @@ void R_Shadow_SelectLightInView(void)
        dlight_t *best;
        size_t lightindex;
        dlight_t *light;
+       size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
        best = NULL;
        bestrating = 0;
-       for (lightindex = 0;lightindex < Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray);lightindex++)
+       for (lightindex = 0;lightindex < range;lightindex++)
        {
-               light = Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
+               light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
                if (!light)
                        continue;
                VectorSubtract(light->origin, r_refdef.view.origin, temp);
@@ -3646,7 +3792,15 @@ void R_Shadow_LoadWorldLights(void)
 #if _MSC_VER >= 1400
 #define sscanf sscanf_s
 #endif
-                       a = sscanf(t, "%f %f %f %f %f %f %f %d %s %f %f %f %f %f %f %f %f %i", &origin[0], &origin[1], &origin[2], &radius, &color[0], &color[1], &color[2], &style, cubemapname, &corona, &angles[0], &angles[1], &angles[2], &coronasizescale, &ambientscale, &diffusescale, &specularscale, &flags);
+                       cubemapname[sizeof(cubemapname)-1] = 0;
+#if MAX_QPATH != 128
+#error update this code if MAX_QPATH changes
+#endif
+                       a = sscanf(t, "%f %f %f %f %f %f %f %d %127s %f %f %f %f %f %f %f %f %i", &origin[0], &origin[1], &origin[2], &radius, &color[0], &color[1], &color[2], &style, cubemapname
+#if _MSC_VER >= 1400
+, sizeof(cubemapname)
+#endif
+, &corona, &angles[0], &angles[1], &angles[2], &coronasizescale, &ambientscale, &diffusescale, &specularscale, &flags);
                        *s = tempchar;
                        if (a < 18)
                                flags = LIGHTFLAG_REALTIMEMODE;
@@ -3698,7 +3852,9 @@ void R_Shadow_SaveWorldLights(void)
        char *buf, *oldbuf;
        char name[MAX_QPATH];
        char line[MAX_INPUTLINE];
-       if (!Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray))
+       size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked, assuming the dpsnprintf mess doesn't screw it up...
+       // I hate lines which are 3 times my screen size :( --blub
+       if (!range)
                return;
        if (cl.worldmodel == NULL)
        {
@@ -3709,9 +3865,9 @@ void R_Shadow_SaveWorldLights(void)
        strlcat (name, ".rtlights", sizeof (name));
        bufchars = bufmaxchars = 0;
        buf = NULL;
-       for (lightindex = 0;lightindex < Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray);lightindex++)
+       for (lightindex = 0;lightindex < range;lightindex++)
        {
-               light = Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
+               light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
                if (!light)
                        continue;
                if (light->coronasizescale != 0.25f || light->ambientscale != 0 || light->diffusescale != 1 || light->specularscale != 1 || light->flags != LIGHTFLAG_REALTIMEMODE)
@@ -4426,6 +4582,7 @@ void R_Shadow_EditLights_EditAll_f(void)
 {
        size_t lightindex;
        dlight_t *light;
+       size_t range;
 
        if (!r_editlights.integer)
        {
@@ -4433,9 +4590,11 @@ void R_Shadow_EditLights_EditAll_f(void)
                return;
        }
 
-       for (lightindex = 0;lightindex < Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray);lightindex++)
+       // EditLights doesn't seem to have a "remove" command or something so:
+       range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
+       for (lightindex = 0;lightindex < range;lightindex++)
        {
-               light = Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
+               light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
                if (!light)
                        continue;
                R_Shadow_SelectLight(light);
@@ -4446,7 +4605,7 @@ void R_Shadow_EditLights_EditAll_f(void)
 void R_Shadow_EditLights_DrawSelectedLightProperties(void)
 {
        int lightnumber, lightcount;
-       size_t lightindex;
+       size_t lightindex, range;
        dlight_t *light;
        float x, y;
        char temp[256];
@@ -4457,9 +4616,10 @@ void R_Shadow_EditLights_DrawSelectedLightProperties(void)
        DrawQ_Pic(x-5, y-5, NULL, 250, 155, 0, 0, 0, 0.75, 0);
        lightnumber = -1;
        lightcount = 0;
-       for (lightindex = 0;lightindex < Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray);lightindex++)
+       range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
+       for (lightindex = 0;lightindex < range;lightindex++)
        {
-               light = Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
+               light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
                if (!light)
                        continue;
                if (light == r_shadow_selectedlight)