+ if (!r_drawentities.integer)
+ return;
+
+ for (i = 0;i < r_refdef.numentities;i++)
+ {
+ ent = r_refdef.entities[i];
+ if (ent->visframe == r_framecount)
+ {
+ if (ent->model && ent->model->Draw != NULL)
+ ent->model->Draw(ent);
+ else
+ R_DrawNoModel(ent);
+ }
+ }
+}
+
+#include "r_shadow.h"
+
+int shadowframecount = 0;
+
+void R_TestAndDrawShadowVolume(entity_render_t *ent, vec3_t lightorigin, float cullradius, float lightradius, vec3_t lightmins, vec3_t lightmaxs, vec3_t clipmins, vec3_t clipmaxs, int lightmarked)
+{
+ vec3_t relativelightorigin;
+ // rough checks
+ if ((ent->flags & RENDER_SHADOW) && ent->model && ent->model->DrawShadowVolume && !(r_shadow_cull.integer && (ent->maxs[0] < lightmins[0] || ent->mins[0] > lightmaxs[0] || ent->maxs[1] < lightmins[1] || ent->mins[1] > lightmaxs[1] || ent->maxs[2] < lightmins[2] || ent->mins[2] > lightmaxs[2])))
+ {
+ Matrix4x4_Transform(&ent->inversematrix, lightorigin, relativelightorigin);
+ ent->model->DrawShadowVolume (ent, relativelightorigin, lightradius);
+ }
+}
+
+void R_Shadow_DrawWorldLightShadowVolume(matrix4x4_t *matrix, worldlight_t *light);
+
+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;
+ rdlight_t *rd;
+ rmeshstate_t m;
+ matrix4x4_t matrix;
+ matrix4x4_t matrix_worldtofilter, matrix_worldtoattenuationxyz, matrix_worldtoattenuationz;
+ matrix4x4_t matrix_modeltofilter, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz;
+
+ if (visiblevolumes)
+ {
+ memset(&m, 0, sizeof(m));
+ R_Mesh_State_Texture(&m);
+
+ GL_BlendFunc(GL_ONE, GL_ONE);
+ GL_DepthMask(false);
+ GL_DepthTest(r_shadow_visiblevolumes.integer < 2);
+ qglDisable(GL_CULL_FACE);
+ GL_Color(0.0, 0.0125, 0.1, 1);
+ }
+ else
+ R_Shadow_Stage_Begin();
+ shadowframecount++;
+ if (r_shadow_realtime_world.integer)
+ {
+ R_Shadow_LoadWorldLightsIfNeeded();
+ for (lnum = 0, wl = r_shadow_worldlightchain;wl;wl = wl->next, lnum++)
+ {
+ if (d_lightstylevalue[wl->style] <= 0)
+ continue;
+ if (VIS_CullBox(wl->mins, wl->maxs))
+ continue;
+ if (r_shadow_debuglight.integer >= 0 && lnum != r_shadow_debuglight.integer)
+ continue;
+ if (R_Shadow_ScissorForBBox(wl->mins, wl->maxs))
+ continue;
+
+ cullradius = wl->cullradius;
+ lightradius = wl->lightradius;
+ VectorCopy(wl->mins, clipmins);
+ VectorCopy(wl->maxs, clipmaxs);
+
+ 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 (wl->castshadows && (gl_stencil || visiblevolumes))
+ {
+ if (!visiblevolumes)
+ R_Shadow_Stage_ShadowVolumes();
+ ent = &cl_entities[0].render;
+ if (r_shadow_staticworldlights.integer)
+ R_Shadow_DrawStaticWorldLight_Shadow(wl, &ent->matrix);
+ else
+ R_TestAndDrawShadowVolume(ent, wl->origin, cullradius, lightradius, wl->mins, wl->maxs, clipmins, clipmaxs, true);
+ if (r_drawentities.integer)
+ for (i = 0;i < r_refdef.numentities;i++)
+ R_TestAndDrawShadowVolume(r_refdef.entities[i], wl->origin, cullradius, lightradius, wl->mins, wl->maxs, clipmins, clipmaxs, true);
+ }
+
+ if (!visiblevolumes)
+ {
+ if (wl->castshadows && gl_stencil)
+ R_Shadow_Stage_LightWithShadows();
+ else
+ R_Shadow_Stage_LightWithoutShadows();
+
+ // calculate world to filter matrix
+ Matrix4x4_CreateFromQuakeEntity(&matrix, wl->origin[0], wl->origin[1], wl->origin[2], wl->angles[0], wl->angles[1], wl->angles[2], lightradius);
+ Matrix4x4_Invert_Simple(&matrix_worldtofilter, &matrix);
+ // calculate world to attenuationxyz/xy matrix
+ Matrix4x4_CreateFromQuakeEntity(&matrix, 0.5, 0.5, 0.5, 0, 0, 0, 0.5);
+ Matrix4x4_Concat(&matrix_worldtoattenuationxyz, &matrix, &matrix_worldtofilter);
+ // calculate world to attenuationz matrix
+ matrix.m[0][0] = 0;matrix.m[0][1] = 0;matrix.m[0][2] = 0.5;matrix.m[0][3] = 0.5;
+ matrix.m[1][0] = 0;matrix.m[1][1] = 0;matrix.m[1][2] = 0 ;matrix.m[1][3] = 0.5;
+ matrix.m[2][0] = 0;matrix.m[2][1] = 0;matrix.m[2][2] = 0 ;matrix.m[2][3] = 0.5;
+ matrix.m[3][0] = 0;matrix.m[3][1] = 0;matrix.m[3][2] = 0 ;matrix.m[3][3] = 1;
+ Matrix4x4_Concat(&matrix_worldtoattenuationz, &matrix, &matrix_worldtofilter);
+
+ ent = &cl_entities[0].render;
+ if (ent->model && ent->model->DrawLight)
+ {
+ Matrix4x4_Transform(&ent->inversematrix, wl->origin, relativelightorigin);
+ Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, relativeeyeorigin);
+ Matrix4x4_Concat(&matrix_modeltofilter, &matrix_worldtofilter, &ent->matrix);
+ Matrix4x4_Concat(&matrix_modeltoattenuationxyz, &matrix_worldtoattenuationxyz, &ent->matrix);
+ Matrix4x4_Concat(&matrix_modeltoattenuationz, &matrix_worldtoattenuationz, &ent->matrix);
+ if (r_shadow_staticworldlights.integer)
+ R_Shadow_DrawStaticWorldLight_Light(wl, &ent->matrix, relativelightorigin, relativeeyeorigin, lightradius, lightcolor, &matrix_modeltofilter, &matrix_modeltoattenuationxyz, &matrix_modeltoattenuationz);
+ else
+ ent->model->DrawLight(ent, relativelightorigin, relativeeyeorigin, lightradius / ent->scale, lightcolor, &matrix_modeltofilter, &matrix_modeltoattenuationxyz, &matrix_modeltoattenuationz);
+ }
+ 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
+ && BoxesOverlap(ent->mins, ent->maxs, clipmins, clipmaxs)
+ && !(ent->effects & EF_ADDITIVE) && ent->alpha == 1)
+ {
+ Matrix4x4_Transform(&ent->inversematrix, wl->origin, relativelightorigin);
+ Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, relativeeyeorigin);
+ Matrix4x4_Concat(&matrix_modeltofilter, &matrix_worldtofilter, &ent->matrix);
+ Matrix4x4_Concat(&matrix_modeltoattenuationxyz, &matrix_worldtoattenuationxyz, &ent->matrix);
+ Matrix4x4_Concat(&matrix_modeltoattenuationz, &matrix_worldtoattenuationz, &ent->matrix);
+ ent->model->DrawLight(ent, relativelightorigin, relativeeyeorigin, lightradius / ent->scale, lightcolor, &matrix_modeltofilter, &matrix_modeltoattenuationxyz, &matrix_modeltoattenuationz);
+ }
+ }
+ }
+ }
+ }
+ }
+ if (r_shadow_realtime_world.integer || r_shadow_realtime_dlight.integer)
+ {
+ for (lnum = 0, rd = r_dlight;lnum < r_numdlights;lnum++, rd++)
+ {
+ lightradius = rd->cullradius;
+ clipmins[0] = rd->origin[0] - lightradius;
+ clipmins[1] = rd->origin[1] - lightradius;
+ clipmins[2] = rd->origin[2] - lightradius;
+ clipmaxs[0] = rd->origin[0] + lightradius;
+ clipmaxs[1] = rd->origin[1] + lightradius;
+ clipmaxs[2] = rd->origin[2] + lightradius;
+ if (VIS_CullBox(clipmins, clipmaxs) || R_Shadow_ScissorForBBox(clipmins, clipmaxs))
+ continue;
+
+ cullradius = RadiusFromBoundsAndOrigin(clipmins, clipmaxs, rd->origin);
+ VectorScale(rd->light, (1.0f / 4096.0f), lightcolor);
+
+ if (r_shadow_shadows.integer && (gl_stencil || visiblevolumes))
+ {
+ if (!visiblevolumes)
+ R_Shadow_Stage_ShadowVolumes();
+ ent = &cl_entities[0].render;
+ R_TestAndDrawShadowVolume(ent, rd->origin, cullradius, lightradius, clipmins, clipmaxs, clipmins, clipmaxs, false);
+ 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, false);
+ }
+ }
+ }
+
+ if (!visiblevolumes)
+ {
+ if (r_shadow_shadows.integer && gl_stencil)
+ R_Shadow_Stage_LightWithShadows();
+ else
+ R_Shadow_Stage_LightWithoutShadows();
+
+ // calculate world to filter matrix
+ Matrix4x4_CreateFromQuakeEntity(&matrix, rd->origin[0], rd->origin[1], rd->origin[2], 0, 0, 0, lightradius);
+ Matrix4x4_Invert_Simple(&matrix_worldtofilter, &matrix);
+ // calculate world to attenuationxyz/xy matrix
+ Matrix4x4_CreateFromQuakeEntity(&matrix, 0.5, 0.5, 0.5, 0, 0, 0, 0.5);
+ Matrix4x4_Concat(&matrix_worldtoattenuationxyz, &matrix, &matrix_worldtofilter);
+ // calculate world to attenuationz matrix
+ matrix.m[0][0] = 0;matrix.m[0][1] = 0;matrix.m[0][2] = 0.5;matrix.m[0][3] = 0.5;
+ matrix.m[1][0] = 0;matrix.m[1][1] = 0;matrix.m[1][2] = 0 ;matrix.m[1][3] = 0.5;
+ matrix.m[2][0] = 0;matrix.m[2][1] = 0;matrix.m[2][2] = 0 ;matrix.m[2][3] = 0.5;
+ matrix.m[3][0] = 0;matrix.m[3][1] = 0;matrix.m[3][2] = 0 ;matrix.m[3][3] = 1;
+ Matrix4x4_Concat(&matrix_worldtoattenuationz, &matrix, &matrix_worldtofilter);
+
+ ent = &cl_entities[0].render;
+ if (ent->model && ent->model->DrawLight)
+ {
+ Matrix4x4_Transform(&ent->inversematrix, rd->origin, relativelightorigin);
+ Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, relativeeyeorigin);
+ Matrix4x4_Concat(&matrix_modeltofilter, &matrix_worldtofilter, &ent->matrix);
+ Matrix4x4_Concat(&matrix_modeltoattenuationxyz, &matrix_worldtoattenuationxyz, &ent->matrix);
+ Matrix4x4_Concat(&matrix_modeltoattenuationz, &matrix_worldtoattenuationz, &ent->matrix);
+ ent->model->DrawLight(ent, relativelightorigin, relativeeyeorigin, lightradius / ent->scale, lightcolor, &matrix_modeltofilter, &matrix_modeltoattenuationxyz, &matrix_modeltoattenuationz);
+ }
+ 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
+ && BoxesOverlap(ent->mins, ent->maxs, clipmins, clipmaxs)
+ && !(ent->effects & EF_ADDITIVE) && ent->alpha == 1)
+ {
+ Matrix4x4_Transform(&ent->inversematrix, rd->origin, relativelightorigin);
+ Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, relativeeyeorigin);
+ Matrix4x4_Concat(&matrix_modeltofilter, &matrix_worldtofilter, &ent->matrix);
+ Matrix4x4_Concat(&matrix_modeltoattenuationxyz, &matrix_worldtoattenuationxyz, &ent->matrix);
+ Matrix4x4_Concat(&matrix_modeltoattenuationz, &matrix_worldtoattenuationz, &ent->matrix);
+ ent->model->DrawLight(ent, relativelightorigin, relativeeyeorigin, lightradius / ent->scale, lightcolor, &matrix_modeltofilter, &matrix_modeltoattenuationxyz, &matrix_modeltoattenuationz);
+ }
+ }
+ }
+ }
+ }
+ }
+
+ if (visiblevolumes)
+ qglEnable(GL_CULL_FACE);
+ else
+ R_Shadow_Stage_End();
+ qglDisable(GL_SCISSOR_TEST);
+}
+
+static void R_SetFrustum (void)
+{