+#define SHADOWSPHERE_SEGMENTS 16
+
+shadowmesh_t *shadowsphere;
+void R_CreateShadowSphere(void)
+{
+ int i, j;
+ vec3_t angles, angles2, angles3, angles4;
+ float verts[12];
+ shadowsphere = Mod_ShadowMesh_Begin(zonemempool);
+ for (i = 0;i < SHADOWSPHERE_SEGMENTS / 2;i++)
+ {
+ for (j = 0;j < SHADOWSPHERE_SEGMENTS;j++)
+ {
+ angles[0] = (i * 360.0f / SHADOWSPHERE_SEGMENTS) + 90.0f;
+ angles[1] = j * 360.0f / SHADOWSPHERE_SEGMENTS;
+ angles[2] = 0;
+ VectorCopy(angles, angles2);
+ VectorCopy(angles, angles3);
+ VectorCopy(angles, angles4);
+ angles2[1] += 360.0f / SHADOWSPHERE_SEGMENTS;
+ angles3[0] += 360.0f / SHADOWSPHERE_SEGMENTS;
+ angles3[1] += 360.0f / SHADOWSPHERE_SEGMENTS;
+ angles4[0] += 360.0f / SHADOWSPHERE_SEGMENTS;
+ AngleVectorsFLU(angles, verts, NULL, NULL);
+ AngleVectorsFLU(angles2, verts + 9, NULL, NULL);
+ AngleVectorsFLU(angles3, verts + 6, NULL, NULL);
+ AngleVectorsFLU(angles4, verts + 3, NULL, NULL);
+ VectorScale(&verts[0], 1.0f, &verts[0]);
+ VectorScale(&verts[3], 1.0f, &verts[3]);
+ VectorScale(&verts[6], 1.0f, &verts[6]);
+ VectorScale(&verts[9], 1.0f, &verts[9]);
+ Mod_ShadowMesh_AddPolygon(zonemempool, shadowsphere, 4, verts);
+ }
+ }
+ shadowsphere = Mod_ShadowMesh_Finish(zonemempool, shadowsphere);
+}
+
+
+void R_DrawShadowSphere(vec3_t origin, float cullradius, float lightradius)
+{
+ shadowmesh_t *mesh;
+ matrix4x4_t matrix;
+ if (!shadowsphere)
+ R_CreateShadowSphere();
+ Matrix4x4_CreateScale(&matrix, lightradius);
+ Matrix4x4_ConcatTranslate(&matrix, origin[0], origin[1], origin[2]);
+ R_Mesh_Matrix(&matrix);
+ for (mesh = shadowsphere;mesh;mesh = mesh->next)
+ {
+ memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
+ R_Shadow_RenderVolume(mesh->numverts, mesh->numtriangles, mesh->elements);
+ }
+ Matrix4x4_CreateScale(&matrix, -cullradius);
+ Matrix4x4_ConcatTranslate(&matrix, origin[0], origin[1], origin[2]);
+ R_Mesh_Matrix(&matrix);
+ for (mesh = shadowsphere;mesh;mesh = mesh->next)
+ {
+ memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
+ R_Shadow_RenderVolume(mesh->numverts, mesh->numtriangles, mesh->elements);
+ }
+}
+
+void R_ShadowVolumeLighting (int visiblevolumes)
+{
+ int i;
+ entity_render_t *ent;
+ int lnum;
+ float f, lightradius, cullradius;
+ vec3_t relativelightorigin, relativeeyeorigin, lightcolor;
+ mlight_t *sl;
+ rdlight_t *rd;
+ rmeshstate_t m;
+
+ if (visiblevolumes)
+ {
+ memset(&m, 0, sizeof(m));
+ m.blendfunc1 = GL_ONE;
+ m.blendfunc2 = GL_ONE;
+ R_Mesh_State(&m);
+ GL_Color(0.0 * r_colorscale, 0.0125 * r_colorscale, 0.1 * r_colorscale, 1);
+ }
+ else
+ R_Shadow_Stage_Begin();
+ for (lnum = 0, sl = cl.worldmodel->lights;lnum < cl.worldmodel->numlights;lnum++, sl++)
+ {
+ if (d_lightstylevalue[sl->style] <= 0)
+ continue;
+ if (r_light_debuglight.integer >= 0 && lnum != r_light_debuglight.integer)
+ continue;
+ cullradius = sl->cullradius;
+ lightradius = sl->lightradius;
+ if (R_CullBox(sl->mins, sl->maxs) || PVS_CullBox(sl->mins, sl->maxs) || R_CullSphere(sl->origin, cullradius) || PVS_CullSphere(sl->origin, cullradius))
+ continue;
+
+ f = d_lightstylevalue[sl->style] * (1.0f / 32768.0f);
+ VectorScale(sl->light, f, lightcolor);
+
+ if (!visiblevolumes)
+ R_Shadow_Stage_ShadowVolumes();
+ R_DrawShadowSphere(sl->origin, cullradius, lightradius * 2);
+ if (sl->shadowvolume && r_staticworldlights.integer)
+ R_DrawWorldLightShadowVolume(sl);
+ else
+ R_TestAndDrawShadowVolume(&cl_entities[0].render, sl->origin, cullradius);
+ 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])
+ R_TestAndDrawShadowVolume(r_refdef.entities[i], sl->origin, cullradius);
+ }
+ }
+
+ 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])
+ {
+ 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);
+ }
+ }
+ }
+
+ R_Shadow_Stage_EraseShadowVolumes();
+ R_DrawShadowSphere(sl->origin, cullradius, lightradius * 2);
+ if (sl->shadowvolume && r_staticworldlights.integer)
+ R_DrawWorldLightShadowVolume(sl);
+ else
+ R_TestAndDrawShadowVolume(&cl_entities[0].render, sl->origin, cullradius);
+ 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])
+ R_TestAndDrawShadowVolume(r_refdef.entities[i], sl->origin, cullradius);
+ }
+ }
+ }
+ }
+ for (lnum = 0, rd = r_dlight;lnum < r_numdlights;lnum++, rd++)
+ {
+ cullradius = rd->cullradius;
+ lightradius = rd->cullradius;
+ if (R_CullSphere(rd->origin, cullradius) || PVS_CullSphere(rd->origin, cullradius))
+ continue;
+
+ VectorScale(rd->light, 4.0f, lightcolor);
+
+ if (!visiblevolumes)
+ R_Shadow_Stage_ShadowVolumes();
+ R_TestAndDrawShadowVolume(&cl_entities[0].render, rd->origin, cullradius);
+ 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);
+ }
+ }
+
+ 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, cullradius, LIGHTOFFSET, rd->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)
+ {
+ Matrix4x4_Transform(&ent->inversematrix, rd->origin, relativelightorigin);
+ Matrix4x4_Transform(&ent->inversematrix, r_origin, relativeeyeorigin);
+ ent->model->DrawLight(ent, relativelightorigin, relativeeyeorigin, cullradius, LIGHTOFFSET, rd->subtract, lightcolor);
+ }
+ }
+ }
+
+ R_Shadow_Stage_EraseShadowVolumes();
+ R_TestAndDrawShadowVolume(&cl_entities[0].render, rd->origin, cullradius);
+ 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);
+ }
+ }
+ }
+ }
+
+ if (!visiblevolumes)
+ R_Shadow_Stage_End();