]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_rsurf.c
improved shadowmap side culling
[xonotic/darkplaces.git] / gl_rsurf.c
index 7d68daed542ab5f7db889625fd77790cc588bd02..f3f121ce376b76d545424c20280f4e4905ad534f 100644 (file)
@@ -335,6 +335,7 @@ static void R_DrawPortal_Callback(const entity_render_t *ent, const rtlight_t *r
        // called with a batch, so numsurfaces is always 1, and the surfacelist
        // contains only a leaf number for coloring purposes
        const mportal_t *portal = (mportal_t *)ent;
+       qboolean isvis;
        int i, numpoints;
        float *v;
        float vertex3f[POLYGONELEMENTS_MAXPOINTS*3];
@@ -354,11 +355,13 @@ static void R_DrawPortal_Callback(const entity_render_t *ent, const rtlight_t *r
        R_Mesh_ResetTextureState();
        R_SetupGenericShader(false);
 
-       i = surfacelist[0];
+       isvis = (portal->here->clusterindex >= 0 && portal->past->clusterindex >= 0 && portal->here->clusterindex != portal->past->clusterindex);
+
+       i = surfacelist[0] >> 1;
        GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f) * r_refdef.view.colorscale,
                         ((i & 0x0038) >> 3) * (1.0f / 7.0f) * r_refdef.view.colorscale,
                         ((i & 0x01C0) >> 6) * (1.0f / 7.0f) * r_refdef.view.colorscale,
-                        0.125f);
+                        isvis ? 0.125f : 0.03125f);
        for (i = 0, v = vertex3f;i < numpoints;i++, v += 3)
                VectorCopy(portal->points[i].position, v);
        R_Mesh_Draw(0, numpoints, 0, numpoints - 2, NULL, polygonelements, 0, 0);
@@ -975,7 +978,7 @@ void R_Q1BSP_GetLightInfo(entity_render_t *ent, vec3_t relativelightorigin, floa
                // optionally using svbsp for exact culling of compiled lights
                // (or if the user enables dlight svbsp culling, which is mostly for
                //  debugging not actual use)
-               R_Q1BSP_CallRecursiveGetLightInfo(&info, r_shadow_compilingrtlight ? r_shadow_realtime_world_compilesvbsp.integer : r_shadow_realtime_dlight_svbspculling.integer);
+               R_Q1BSP_CallRecursiveGetLightInfo(&info, (r_shadow_compilingrtlight ? r_shadow_realtime_world_compilesvbsp.integer : r_shadow_realtime_dlight_svbspculling.integer) != 0);
        }
 
        rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
@@ -1056,6 +1059,68 @@ void R_Q1BSP_DrawShadowVolume(entity_render_t *ent, const vec3_t relativelightor
                GL_PolygonOffset(r_refdef.shadowpolygonfactor, r_refdef.shadowpolygonoffset);
 }
 
+void R_Q1BSP_CompileShadowMap(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativelightdirection, float lightradius, int numsurfaces, const int *surfacelist)
+{
+       dp_model_t *model = ent->model;
+       msurface_t *surface;
+       int surfacelistindex;
+       int sidetotals[6] = { 0, 0, 0, 0, 0, 0 }, sidemasks = 0;
+       int i;
+       r_shadow_compilingrtlight->static_meshchain_shadow_shadowmap = Mod_ShadowMesh_Begin(r_main_mempool, 32768, 32768, NULL, NULL, NULL, false, false, true);
+       R_Shadow_PrepareShadowSides(model->brush.shadowmesh->numtriangles);
+       for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
+       {
+               surface = model->data_surfaces + surfacelist[surfacelistindex];
+               sidemasks |= R_Shadow_ChooseSidesFromBox(surface->num_firstshadowmeshtriangle, surface->num_triangles, model->brush.shadowmesh->vertex3f, model->brush.shadowmesh->element3i, &r_shadow_compilingrtlight->matrix_worldtolight, relativelightorigin, relativelightdirection, r_shadow_compilingrtlight->cullmins, r_shadow_compilingrtlight->cullmaxs, surface->mins, surface->maxs, surface->texture->basematerialflags & MATERIALFLAG_NOSHADOW ? NULL : sidetotals);
+       }
+       R_Shadow_ShadowMapFromList(model->brush.shadowmesh->numverts, model->brush.shadowmesh->numtriangles, model->brush.shadowmesh->vertex3f, model->brush.shadowmesh->element3i, numshadowsides, sidetotals, shadowsides, shadowsideslist);
+       r_shadow_compilingrtlight->static_meshchain_shadow_shadowmap = Mod_ShadowMesh_Finish(r_main_mempool, r_shadow_compilingrtlight->static_meshchain_shadow_shadowmap, false, false, true);
+       r_shadow_compilingrtlight->static_shadowmap_receivers &= sidemasks;
+       for(i = 0;i<6;i++)
+               if(!sidetotals[i])
+                       r_shadow_compilingrtlight->static_shadowmap_casters &= ~(1 << i);
+}
+
+void R_Q1BSP_DrawShadowMap(int side, entity_render_t *ent, const vec3_t relativelightorigin, const vec3_t relativelightdirection, float lightradius, int modelnumsurfaces, const int *modelsurfacelist, const vec3_t lightmins, const vec3_t lightmaxs)
+{
+       dp_model_t *model = ent->model;
+       msurface_t *surface, *batch[64];
+       int modelsurfacelistindex, batchsize;
+       // check the box in modelspace, it was already checked in worldspace
+       if (!BoxesOverlap(model->normalmins, model->normalmaxs, lightmins, lightmaxs))
+               return;
+       // identify lit faces within the bounding box
+       for (modelsurfacelistindex = 0;modelsurfacelistindex < modelnumsurfaces;modelsurfacelistindex++)
+       {
+               surface = model->data_surfaces + modelsurfacelist[modelsurfacelistindex];
+               rsurface.texture = R_GetCurrentTexture(surface->texture);
+               if (rsurface.texture->currentmaterialflags & MATERIALFLAG_NOSHADOW)
+                       continue;
+               if(!BoxesOverlap(lightmins, lightmaxs, surface->mins, surface->maxs))
+                       continue;
+               r_refdef.stats.lights_dynamicshadowtriangles += surface->num_triangles;
+               r_refdef.stats.lights_shadowtriangles += surface->num_triangles;
+               batch[0] = surface;
+        batchsize = 1;
+               while(++modelsurfacelistindex < modelnumsurfaces && batchsize < (int)(sizeof(batch)/sizeof(batch[0])))
+               {
+                       surface = model->data_surfaces + modelsurfacelist[modelsurfacelistindex];
+                       if(surface->texture != batch[0]->texture)
+                               break;
+                       if(!BoxesOverlap(lightmins, lightmaxs, surface->mins, surface->maxs))
+                               continue;
+                       r_refdef.stats.lights_dynamicshadowtriangles += surface->num_triangles;
+                       r_refdef.stats.lights_shadowtriangles += surface->num_triangles;
+                       batch[batchsize++] = surface;
+               }
+               --modelsurfacelistindex;
+               GL_CullFace(rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE ? GL_NONE : r_refdef.view.cullface_back);
+               RSurf_PrepareVerticesForBatch(false, false, batchsize, batch);
+               RSurf_DrawBatch_Simple(batchsize, batch);
+               GL_LockArrays(0, 0);
+       }
+}
+
 #define BATCHSIZE 1024
 
 static void R_Q1BSP_DrawLight_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
@@ -1066,7 +1131,7 @@ static void R_Q1BSP_DrawLight_TransparentCallback(const entity_render_t *ent, co
        // note: in practice this never actually receives batches), oh well
        R_Shadow_RenderMode_Begin();
        R_Shadow_RenderMode_ActiveLight(rtlight);
-       R_Shadow_RenderMode_Lighting(false, true);
+       R_Shadow_RenderMode_Lighting(false, true, false);
        R_Shadow_SetupEntityLight(ent);
        for (i = 0;i < numsurfaces;i = j)
        {