+extern void R_Model_Brush_DrawLightForSurfaceList(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, msurface_t **surflist, int numsurfaces);
+void R_ShadowVolumeLighting (int visiblevolumes)
+{
+ int i;
+ entity_render_t *ent;
+ int lnum;
+ float f, lightradius, cullradius;
+ vec3_t relativelightorigin, relativeeyeorigin, lightcolor, clipmins, clipmaxs;
+ worldlight_t *wl;
+ //mlight_t *sl;
+ rdlight_t *rd;
+ rmeshstate_t m;
+ mleaf_t *leaf;
+
+ if (visiblevolumes)
+ {
+ memset(&m, 0, sizeof(m));
+ m.blendfunc1 = GL_ONE;
+ m.blendfunc2 = GL_ONE;
+ if (r_shadow_realtime.integer >= 3)
+ m.depthdisable = true;
+ R_Mesh_State(&m);
+ qglDisable(GL_CULL_FACE);
+ GL_Color(0.0 * r_colorscale, 0.0125 * r_colorscale, 0.1 * r_colorscale, 1);
+ }
+ else
+ R_Shadow_Stage_Begin();
+ shadowframecount++;
+ for (lnum = 0, wl = r_shadow_worldlightchain;wl;wl = wl->next, lnum++)
+ {
+ if (d_lightstylevalue[wl->style] <= 0)
+ continue;
+ cullradius = wl->cullradius;
+ lightradius = wl->lightradius;
+ if (R_CullSphere(wl->origin, lightradius))
+ continue;
+ //if (R_CullBox(wl->mins, wl->maxs) || R_CullSphere(wl->origin, lightradius))
+ // continue;
+ //if (VIS_CullBox(wl->mins, wl->maxs) || VIS_CullSphere(wl->origin, lightradius))
+ // continue;
+ if (r_shadow_debuglight.integer >= 0 && lnum != r_shadow_debuglight.integer)
+ continue;
+
+ for (i = 0;i < wl->numleafs;i++)
+ if (wl->leafs[i]->visframe == r_framecount)
+ break;
+ if (i == wl->numleafs)
+ continue;
+ leaf = wl->leafs[i++];
+ VectorCopy(leaf->mins, clipmins);
+ VectorCopy(leaf->maxs, clipmaxs);
+ for (i++;i < wl->numleafs;i++)
+ {
+ leaf = wl->leafs[i];
+ if (leaf->visframe == r_framecount)
+ {
+ if (clipmins[0] > leaf->mins[0]) clipmins[0] = leaf->mins[0];
+ if (clipmaxs[0] < leaf->maxs[0]) clipmaxs[0] = leaf->maxs[0];
+ if (clipmins[1] > leaf->mins[1]) clipmins[1] = leaf->mins[1];
+ if (clipmaxs[1] < leaf->maxs[1]) clipmaxs[1] = leaf->maxs[1];
+ if (clipmins[2] > leaf->mins[2]) clipmins[2] = leaf->mins[2];
+ if (clipmaxs[2] < leaf->maxs[2]) clipmaxs[2] = leaf->maxs[2];
+ }
+ }
+ if (clipmins[0] < wl->mins[0]) clipmins[0] = wl->mins[0];
+ if (clipmins[1] < wl->mins[1]) clipmins[1] = wl->mins[1];
+ if (clipmins[2] < wl->mins[2]) clipmins[2] = wl->mins[2];
+ if (clipmaxs[0] > wl->maxs[0]) clipmaxs[0] = wl->maxs[0];
+ if (clipmaxs[1] > wl->maxs[1]) clipmaxs[1] = wl->maxs[1];
+ if (clipmaxs[2] > wl->maxs[2]) clipmaxs[2] = wl->maxs[2];
+
+ if (R_Shadow_ScissorForBBoxAndSphere(clipmins, clipmaxs, wl->origin, wl->cullradius))
+ continue;
+
+ // mark the leafs we care about so only things in those leafs will matter
+ for (i = 0;i < wl->numleafs;i++)
+ wl->leafs[i]->worldnodeframe = shadowframecount;
+
+
+ f = d_lightstylevalue[wl->style] * (1.0f / 256.0f);
+ VectorScale(wl->light, f, lightcolor);
+ if (wl->selected)
+ {
+ f = 2 + sin(realtime * M_PI * 4.0);
+ VectorScale(lightcolor, f, lightcolor);
+ }
+
+ if (!visiblevolumes)
+ R_Shadow_Stage_ShadowVolumes();
+ ent = &cl_entities[0].render;
+ if (wl->shadowvolume && r_shadow_staticworldlights.integer)
+ R_Shadow_DrawWorldLightShadowVolume(&ent->matrix, wl);
+ else
+ R_TestAndDrawShadowVolume(ent, wl->origin, cullradius, lightradius, wl->mins, wl->maxs, clipmins, clipmaxs);
+ if (r_drawentities.integer)
+ {
+ for (i = 0;i < r_refdef.numentities;i++)
+ {
+ ent = r_refdef.entities[i];
+ if (ent->model && !strcmp(ent->model->name, "progs/missile.mdl"))
+ R_TestAndDrawShadowVolume(ent, wl->origin, cullradius, lightradius, wl->mins, wl->maxs, clipmins, clipmaxs);
+ }
+ }
+
+ if (!visiblevolumes)
+ {
+ R_Shadow_Stage_Light();
+ ent = &cl_entities[0].render;
+ if (ent->model && ent->model->DrawLight)
+ {
+ Matrix4x4_Transform(&ent->inversematrix, wl->origin, relativelightorigin);
+ Matrix4x4_Transform(&ent->inversematrix, r_origin, relativeeyeorigin);
+ if (wl->numsurfaces)
+ R_Model_Brush_DrawLightForSurfaceList(ent, relativelightorigin, relativeeyeorigin, lightradius, lightcolor, wl->surfaces, wl->numsurfaces);
+ else
+ ent->model->DrawLight(ent, relativelightorigin, relativeeyeorigin, lightradius / ent->scale, lightcolor);
+ }
+ if (r_drawentities.integer)
+ {
+ for (i = 0;i < r_refdef.numentities;i++)
+ {
+ ent = r_refdef.entities[i];
+ if (ent->visframe == r_framecount && ent->model && ent->model->DrawLight
+ && ent->maxs[0] >= wl->mins[0] && ent->mins[0] <= wl->maxs[0]
+ && ent->maxs[1] >= wl->mins[1] && ent->mins[1] <= wl->maxs[1]
+ && ent->maxs[2] >= wl->mins[2] && ent->mins[2] <= wl->maxs[2]
+ && !(ent->effects & EF_ADDITIVE) && ent->alpha == 1)
+ {
+ Matrix4x4_Transform(&ent->inversematrix, wl->origin, relativelightorigin);
+ Matrix4x4_Transform(&ent->inversematrix, r_origin, relativeeyeorigin);
+ ent->model->DrawLight(ent, relativelightorigin, relativeeyeorigin, lightradius / ent->scale, lightcolor);
+ }
+ }
+ }
+ }
+ }
+ /*
+ for (lnum = 0, sl = cl.worldmodel->lights;lnum < cl.worldmodel->numlights;lnum++, sl++)
+ {
+ if (d_lightstylevalue[sl->style] <= 0)
+ continue;
+ if (r_shadow_debuglight.integer >= 0 && lnum != r_shadow_debuglight.integer)
+ continue;
+ cullradius = sl->cullradius;
+ lightradius = sl->lightradius;
+ if (VIS_CullBox(sl->mins, sl->maxs) || VIS_CullSphere(sl->origin, lightradius))
+ continue;
+
+ f = d_lightstylevalue[sl->style] * (1.0f / 32768.0f);
+ VectorScale(sl->light, f, lightcolor);
+
+ if (!visiblevolumes)
+ R_Shadow_Stage_ShadowVolumes();
+ if (sl->shadowvolume && r_shadow_staticworldlights.integer)
+ R_DrawWorldLightShadowVolume(&cl_entities[0].render.matrix, sl->shadowvolume);
+ else
+ R_TestAndDrawShadowVolume(&cl_entities[0].render, sl->origin, cullradius, lightradius);
+ if (r_drawentities.integer)
+ {
+ for (i = 0;i < r_refdef.numentities;i++)
+ {
+ ent = r_refdef.entities[i];
+ if (ent->maxs[0] >= sl->mins[0] && ent->mins[0] <= sl->maxs[0]
+ && ent->maxs[1] >= sl->mins[1] && ent->mins[1] <= sl->maxs[1]
+ && ent->maxs[2] >= sl->mins[2] && ent->mins[2] <= sl->maxs[2]
+ && !(ent->effects & EF_ADDITIVE) && ent->alpha == 1)
+ R_TestAndDrawShadowVolume(r_refdef.entities[i], sl->origin, cullradius, lightradius);
+ }
+ }
+
+ if (!visiblevolumes)
+ {
+ R_Shadow_Stage_Light();
+ ent = &cl_entities[0].render;
+ if (ent->model && ent->model->DrawLight)
+ {
+ Matrix4x4_Transform(&ent->inversematrix, sl->origin, relativelightorigin);
+ Matrix4x4_Transform(&ent->inversematrix, r_origin, relativeeyeorigin);
+ ent->model->DrawLight(ent, relativelightorigin, relativeeyeorigin, lightradius, sl->distbias, sl->subtract, lightcolor);
+ }
+ if (r_drawentities.integer)
+ {
+ for (i = 0;i < r_refdef.numentities;i++)
+ {
+ ent = r_refdef.entities[i];
+ if (ent->visframe == r_framecount && ent->model && ent->model->DrawLight
+ && ent->maxs[0] >= sl->mins[0] && ent->mins[0] <= sl->maxs[0]
+ && ent->maxs[1] >= sl->mins[1] && ent->mins[1] <= sl->maxs[1]
+ && ent->maxs[2] >= sl->mins[2] && ent->mins[2] <= sl->maxs[2]
+ && !(ent->effects & EF_ADDITIVE) && ent->alpha == 1)
+ {
+ Matrix4x4_Transform(&ent->inversematrix, sl->origin, relativelightorigin);
+ Matrix4x4_Transform(&ent->inversematrix, r_origin, relativeeyeorigin);
+ ent->model->DrawLight(ent, relativelightorigin, relativeeyeorigin, lightradius, sl->distbias, sl->subtract, lightcolor);
+ }
+ }
+ }
+ }
+ }
+ */
+ for (lnum = 0, rd = r_dlight;lnum < r_numdlights;lnum++, rd++)
+ {
+ cullradius = rd->cullradius;
+ lightradius = rd->cullradius;
+ if (VIS_CullSphere(rd->origin, lightradius))
+ continue;
+
+ VectorScale(rd->light, (1.0f / 8192.0f), lightcolor);
+ clipmins[0] = rd->origin[0] - cullradius;
+ clipmins[1] = rd->origin[1] - cullradius;
+ clipmins[2] = rd->origin[2] - cullradius;
+ clipmaxs[0] = rd->origin[0] + cullradius;
+ clipmaxs[1] = rd->origin[1] + cullradius;
+ clipmaxs[2] = rd->origin[2] + cullradius;
+
+ if (R_Shadow_ScissorForBBoxAndSphere(clipmins, clipmaxs, rd->origin, rd->cullradius))
+ continue;
+
+ if (!visiblevolumes)
+ R_Shadow_Stage_ShadowVolumes();
+ ent = &cl_entities[0].render;
+ R_TestAndDrawShadowVolume(ent, rd->origin, cullradius, lightradius, clipmins, clipmaxs, clipmins, clipmaxs);
+ if (r_drawentities.integer)
+ {
+ for (i = 0;i < r_refdef.numentities;i++)
+ {
+ ent = r_refdef.entities[i];
+ if (ent != rd->ent)
+ R_TestAndDrawShadowVolume(ent, rd->origin, cullradius, lightradius, clipmins, clipmaxs, clipmins, clipmaxs);
+ }
+ }
+
+ if (!visiblevolumes)
+ {
+ R_Shadow_Stage_Light();
+ ent = &cl_entities[0].render;
+ if (ent->model && ent->model->DrawLight)
+ {
+ Matrix4x4_Transform(&ent->inversematrix, rd->origin, relativelightorigin);
+ Matrix4x4_Transform(&ent->inversematrix, r_origin, relativeeyeorigin);
+ ent->model->DrawLight(ent, relativelightorigin, relativeeyeorigin, lightradius / ent->scale, lightcolor);
+ }
+ if (r_drawentities.integer)
+ {
+ for (i = 0;i < r_refdef.numentities;i++)
+ {
+ ent = r_refdef.entities[i];
+ if (ent->visframe == r_framecount && ent->model && ent->model->DrawLight
+ && !(ent->effects & EF_ADDITIVE) && ent->alpha == 1)
+ {
+ Matrix4x4_Transform(&ent->inversematrix, rd->origin, relativelightorigin);
+ Matrix4x4_Transform(&ent->inversematrix, r_origin, relativeeyeorigin);
+ ent->model->DrawLight(ent, relativelightorigin, relativeeyeorigin, lightradius / ent->scale, lightcolor);
+ }
+ }
+ }
+ }
+ }
+
+ if (!visiblevolumes)
+ R_Shadow_Stage_End();
+ qglEnable(GL_CULL_FACE);
+ qglDisable(GL_SCISSOR_TEST);
+}
+