+ c_rt_lightmeshes++;
+ c_rt_lighttris += numtriangles;
+ R_Mesh_Draw(numverts, numtriangles, elements);
+ c_rt_lightmeshes++;
+ c_rt_lighttris += numtriangles;
+
+ memset(&m, 0, sizeof(m));
+ m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
+ m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
+ m.pointer_texcoord[0] = varray_texcoord2f[0];
+ m.pointer_texcoord[1] = varray_texcoord2f[1];
+ R_Mesh_State_Texture(&m);
+ GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
+ R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[0], numverts, vertex3f, matrix_modeltoattenuationxyz);
+ R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[1], numverts, vertex3f, matrix_modeltoattenuationz);
+ R_Mesh_Draw(numverts, numtriangles, elements);
+ c_rt_lightmeshes++;
+ c_rt_lighttris += numtriangles;
+
+ memset(&m, 0, sizeof(m));
+ m.tex[0] = R_GetTexture(glosstexture);
+ if (lightcubemap)
+ {
+ m.texcubemap[1] = R_GetTexture(lightcubemap);
+ m.pointer_texcoord[1] = varray_texcoord3f[1];
+ R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltolight);
+ }
+ m.pointer_texcoord[0] = texcoord2f;
+ R_Mesh_State_Texture(&m);
+ qglColorMask(1,1,1,0);
+ GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
+ VectorScale(lightcolor, colorscale, color2);
+ for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
+ {
+ color[0] = bound(0, color2[0], 1);
+ color[1] = bound(0, color2[1], 1);
+ color[2] = bound(0, color2[2], 1);
+ GL_Color(color[0], color[1], color[2], 1);
+ R_Mesh_Draw(numverts, numtriangles, elements);
+ c_rt_lightmeshes++;
+ c_rt_lighttris += numtriangles;
+ }
+ }
+ }
+}
+
+void R_RTLight_UpdateFromDLight(rtlight_t *rtlight, const dlight_t *light, int isstatic)
+{
+ int j, k;
+ float scale;
+ R_RTLight_Uncompile(rtlight);
+ memset(rtlight, 0, sizeof(*rtlight));
+
+ VectorCopy(light->origin, rtlight->shadoworigin);
+ VectorCopy(light->color, rtlight->color);
+ rtlight->radius = light->radius;
+ rtlight->cullradius = rtlight->radius;
+ rtlight->cullradius2 = rtlight->cullradius * rtlight->cullradius;
+ rtlight->cullmins[0] = rtlight->shadoworigin[0] - rtlight->cullradius;
+ rtlight->cullmins[1] = rtlight->shadoworigin[1] - rtlight->cullradius;
+ rtlight->cullmins[2] = rtlight->shadoworigin[2] - rtlight->cullradius;
+ rtlight->cullmaxs[0] = rtlight->shadoworigin[0] + rtlight->cullradius;
+ rtlight->cullmaxs[1] = rtlight->shadoworigin[1] + rtlight->cullradius;
+ rtlight->cullmaxs[2] = rtlight->shadoworigin[2] + rtlight->cullradius;
+ rtlight->cubemapname[0] = 0;
+ if (light->cubemapname[0])
+ strcpy(rtlight->cubemapname, light->cubemapname);
+ else if (light->cubemapnum > 0)
+ sprintf(rtlight->cubemapname, "cubemaps/%i", light->cubemapnum);
+ rtlight->shadow = light->shadow;
+ rtlight->corona = light->corona;
+ rtlight->style = light->style;
+ rtlight->isstatic = isstatic;
+ Matrix4x4_Invert_Simple(&rtlight->matrix_worldtolight, &light->matrix);
+ // ConcatScale won't work here because this needs to scale rotate and
+ // translate, not just rotate
+ scale = 1.0f / rtlight->radius;
+ for (k = 0;k < 3;k++)
+ for (j = 0;j < 4;j++)
+ rtlight->matrix_worldtolight.m[k][j] *= scale;
+ Matrix4x4_Concat(&rtlight->matrix_worldtoattenuationxyz, &matrix_attenuationxyz, &rtlight->matrix_worldtolight);
+ Matrix4x4_Concat(&rtlight->matrix_worldtoattenuationz, &matrix_attenuationz, &rtlight->matrix_worldtolight);
+
+ rtlight->lightmap_cullradius = bound(0, rtlight->radius, 2048.0f);
+ rtlight->lightmap_cullradius2 = rtlight->lightmap_cullradius * rtlight->lightmap_cullradius;
+ VectorScale(rtlight->color, rtlight->radius * d_lightstylevalue[rtlight->style] * 0.25f, rtlight->lightmap_light);
+ rtlight->lightmap_subtract = 1.0f / rtlight->lightmap_cullradius2;
+}
+
+// compiles rtlight geometry
+// (undone by R_FreeCompiledRTLight, which R_UpdateLight calls)
+void R_RTLight_Compile(rtlight_t *rtlight)
+{
+ int i, j, k, l, maxverts = 256, tris;
+ float *vertex3f = NULL, mins[3], maxs[3];
+ shadowmesh_t *mesh, *castmesh = NULL;
+ int lightpvsbytes;
+ qbyte lightpvs[(MAX_MAP_LEAFS + 7)/ 8];
+ qbyte lightfullpvs[(MAX_MAP_LEAFS + 7)/ 8];
+
+ // compile the light
+ rtlight->compiled = true;
+ VectorCopy(rtlight->cullmins, mins);
+ VectorCopy(rtlight->cullmaxs, maxs);
+ if (rtlight->shadow)
+ castmesh = Mod_ShadowMesh_Begin(r_shadow_mempool, 32768, 32768, NULL, NULL, NULL, false, false, true);
+ rtlight->static_meshchain_light = Mod_ShadowMesh_Begin(r_shadow_mempool, 32768, 32768, NULL, NULL, NULL, true, false, true);
+ if (cl.worldmodel)
+ {
+ lightpvsbytes = cl.worldmodel->brush.FatPVS(cl.worldmodel, rtlight->shadoworigin, 0, lightfullpvs, sizeof(lightfullpvs));
+ memset(lightpvs, 0, lightpvsbytes);
+ if (cl.worldmodel->brushq3.num_leafs)
+ {
+ q3mleaf_t *leaf;
+ q3mface_t *face;
+
+ // make a pvs that only includes things within the box
+ for (i = 0, leaf = cl.worldmodel->brushq3.data_leafs;i < cl.worldmodel->brushq3.num_leafs;i++, leaf++)
+ if (CHECKPVSBIT(lightfullpvs, leaf->clusterindex) && BoxesOverlap(leaf->mins, leaf->maxs, mins, maxs))
+ SETPVSBIT(lightpvs, leaf->clusterindex);
+
+ // make a cluster list for fast visibility checking during rendering
+ for (i = 0, rtlight->static_numclusters = 0;i < cl.worldmodel->brush.num_pvsclusters;i++)
+ if (CHECKPVSBIT(lightpvs, i))
+ rtlight->static_numclusters++;
+ rtlight->static_clusterindices = Mem_Alloc(r_shadow_mempool, rtlight->static_numclusters * sizeof(int));
+ for (i = 0, rtlight->static_numclusters = 0;i < cl.worldmodel->brush.num_pvsclusters;i++)
+ if (CHECKPVSBIT(lightpvs, i))
+ rtlight->static_clusterindices[rtlight->static_numclusters++] = i;
+
+ VectorCopy(rtlight->shadoworigin, rtlight->cullmins);
+ VectorCopy(rtlight->shadoworigin, rtlight->cullmaxs);
+ for (i = 0, face = cl.worldmodel->brushq3.data_thismodel->firstface;i < cl.worldmodel->brushq3.data_thismodel->numfaces;i++, face++)
+ face->lighttemp_castshadow = false;
+ for (i = 0, leaf = cl.worldmodel->brushq3.data_leafs;i < cl.worldmodel->brushq3.num_leafs;i++, leaf++)
+ {
+ if (CHECKPVSBIT(lightpvs, leaf->clusterindex) && BoxesOverlap(leaf->mins, leaf->maxs, mins, maxs))
+ {
+ for (k = 0;k < 3;k++)
+ {
+ if (rtlight->cullmins[k] > leaf->mins[k]) rtlight->cullmins[k] = leaf->mins[k];
+ if (rtlight->cullmaxs[k] < leaf->maxs[k]) rtlight->cullmaxs[k] = leaf->maxs[k];
+ }
+ for (j = 0;j < leaf->numleaffaces;j++)
+ {
+ face = leaf->firstleafface[j];
+ if (BoxesOverlap(face->mins, face->maxs, mins, maxs))
+ face->lighttemp_castshadow = true;
+ }
+ }
+ }
+
+ // add surfaces to shadow casting mesh and light mesh
+ for (i = 0, face = cl.worldmodel->brushq3.data_thismodel->firstface;i < cl.worldmodel->brushq3.data_thismodel->numfaces;i++, face++)
+ {
+ if (face->lighttemp_castshadow)
+ {
+ face->lighttemp_castshadow = false;
+ if (!(face->texture->surfaceflags & (Q3SURFACEFLAG_NODRAW | Q3SURFACEFLAG_SKY)))
+ {
+ if (rtlight->shadow)
+ if (!(face->texture->nativecontents & CONTENTSQ3_TRANSLUCENT))
+ Mod_ShadowMesh_AddMesh(r_shadow_mempool, castmesh, NULL, NULL, NULL, face->data_vertex3f, NULL, NULL, NULL, NULL, face->num_triangles, face->data_element3i);
+ if (!(face->texture->surfaceflags & Q3SURFACEFLAG_SKY))
+ Mod_ShadowMesh_AddMesh(r_shadow_mempool, rtlight->static_meshchain_light, face->texture->skin.base, face->texture->skin.gloss, face->texture->skin.nmap, face->data_vertex3f, face->data_svector3f, face->data_tvector3f, face->data_normal3f, face->data_texcoordtexture2f, face->num_triangles, face->data_element3i);
+ }
+ }
+ }
+ }
+ else if (cl.worldmodel->brushq1.num_leafs)
+ {
+ mleaf_t *leaf;
+ msurface_t *surf;
+ VectorCopy(rtlight->shadoworigin, rtlight->cullmins);
+ VectorCopy(rtlight->shadoworigin, rtlight->cullmaxs);
+ i = CL_PointQ1Contents(rtlight->shadoworigin);
+
+ for (i = 0, surf = cl.worldmodel->brushq1.surfaces + cl.worldmodel->brushq1.firstmodelsurface;i < cl.worldmodel->brushq1.nummodelsurfaces;i++, surf++)
+ surf->lighttemp_castshadow = false;
+
+ if (r_shadow_portallight.integer && i != CONTENTS_SOLID && i != CONTENTS_SKY)
+ {
+ qbyte *byteleafpvs;
+ qbyte *bytesurfacepvs;
+
+ byteleafpvs = Mem_Alloc(tempmempool, cl.worldmodel->brushq1.num_leafs);
+ bytesurfacepvs = Mem_Alloc(tempmempool, cl.worldmodel->brushq1.numsurfaces);
+
+ Portal_Visibility(cl.worldmodel, rtlight->shadoworigin, byteleafpvs, bytesurfacepvs, NULL, 0, true, mins, maxs, rtlight->cullmins, rtlight->cullmaxs);
+
+ // make a pvs that only includes things within the box
+ for (i = 0, leaf = cl.worldmodel->brushq1.data_leafs;i < cl.worldmodel->brushq1.num_leafs;i++, leaf++)
+ {
+ if (byteleafpvs[i] && BoxesOverlap(leaf->mins, leaf->maxs, mins, maxs))
+ {
+ SETPVSBIT(lightpvs, leaf->clusterindex);
+ for (k = 0;k < 3;k++)
+ {
+ if (rtlight->cullmins[k] > leaf->mins[k]) rtlight->cullmins[k] = leaf->mins[k];
+ if (rtlight->cullmaxs[k] < leaf->maxs[k]) rtlight->cullmaxs[k] = leaf->maxs[k];
+ }
+ }
+ }
+
+ for (i = 0, surf = cl.worldmodel->brushq1.surfaces;i < cl.worldmodel->brushq1.numsurfaces;i++, surf++)
+ if (bytesurfacepvs[i] && BoxesOverlap(surf->poly_mins, surf->poly_maxs, mins, maxs))
+ surf->lighttemp_castshadow = true;
+
+ Mem_Free(byteleafpvs);
+ Mem_Free(bytesurfacepvs);
+
+ // make a cluster list for fast visibility checking during rendering
+ for (i = 0, rtlight->static_numclusters = 0;i < cl.worldmodel->brush.num_pvsclusters;i++)
+ if (CHECKPVSBIT(lightpvs, i))
+ rtlight->static_numclusters++;
+ rtlight->static_clusterindices = Mem_Alloc(r_shadow_mempool, rtlight->static_numclusters * sizeof(int));
+ for (i = 0, rtlight->static_numclusters = 0;i < cl.worldmodel->brush.num_pvsclusters;i++)
+ if (CHECKPVSBIT(lightpvs, i))
+ rtlight->static_clusterindices[rtlight->static_numclusters++] = i;
+ }
+ else
+ {
+ for (i = 0, leaf = cl.worldmodel->brushq1.data_leafs;i < cl.worldmodel->brushq1.num_leafs;i++, leaf++)
+ {
+ if (CHECKPVSBIT(lightfullpvs, leaf->clusterindex) && BoxesOverlap(leaf->mins, leaf->maxs, mins, maxs))
+ {
+ // make a pvs that only includes things within the box
+ SETPVSBIT(lightpvs, leaf->clusterindex);
+ for (k = 0;k < 3;k++)
+ {
+ if (rtlight->cullmins[k] > leaf->mins[k]) rtlight->cullmins[k] = leaf->mins[k];
+ if (rtlight->cullmaxs[k] < leaf->maxs[k]) rtlight->cullmaxs[k] = leaf->maxs[k];
+ }
+ for (j = 0;j < leaf->nummarksurfaces;j++)
+ {
+ surf = cl.worldmodel->brushq1.surfaces + leaf->firstmarksurface[j];
+ if (!surf->lighttemp_castshadow && BoxesOverlap(surf->poly_mins, surf->poly_maxs, mins, maxs))
+ surf->lighttemp_castshadow = true;
+ }
+ }
+ }
+
+ // make a pvs that only includes things within the box
+ for (i = 0, leaf = cl.worldmodel->brushq1.data_leafs;i < cl.worldmodel->brushq1.num_leafs;i++, leaf++)
+ if (CHECKPVSBIT(lightfullpvs, leaf->clusterindex) && BoxesOverlap(leaf->mins, leaf->maxs, mins, maxs))
+ SETPVSBIT(lightpvs, leaf->clusterindex);
+
+ // make a cluster list for fast visibility checking during rendering
+ for (i = 0, rtlight->static_numclusters = 0;i < cl.worldmodel->brush.num_pvsclusters;i++)
+ if (CHECKPVSBIT(lightpvs, i))
+ rtlight->static_numclusters++;
+ rtlight->static_clusterindices = Mem_Alloc(r_shadow_mempool, rtlight->static_numclusters * sizeof(int));
+ for (i = 0, rtlight->static_numclusters = 0;i < cl.worldmodel->brush.num_pvsclusters;i++)
+ if (CHECKPVSBIT(lightpvs, i))
+ rtlight->static_clusterindices[rtlight->static_numclusters++] = i;
+ }
+
+ // add surfaces to shadow casting mesh and light mesh
+ for (i = 0, surf = cl.worldmodel->brushq1.surfaces + cl.worldmodel->brushq1.firstmodelsurface;i < cl.worldmodel->brushq1.nummodelsurfaces;i++, surf++)
+ {
+ if (surf->lighttemp_castshadow)
+ {
+ surf->lighttemp_castshadow = false;
+ if (rtlight->shadow && (surf->flags & SURF_SHADOWCAST))
+ Mod_ShadowMesh_AddMesh(r_shadow_mempool, castmesh, NULL, NULL, NULL, surf->mesh.data_vertex3f, NULL, NULL, NULL, NULL, surf->mesh.num_triangles, surf->mesh.data_element3i);
+ if (!(surf->flags & SURF_DRAWSKY))
+ Mod_ShadowMesh_AddMesh(r_shadow_mempool, rtlight->static_meshchain_light, surf->texinfo->texture->skin.base, surf->texinfo->texture->skin.gloss, surf->texinfo->texture->skin.nmap, surf->mesh.data_vertex3f, surf->mesh.data_svector3f, surf->mesh.data_tvector3f, surf->mesh.data_normal3f, surf->mesh.data_texcoordtexture2f, surf->mesh.num_triangles, surf->mesh.data_element3i);
+ }
+ }
+ }
+ }
+
+ // limit box to light bounds (in case it grew larger)
+ for (k = 0;k < 3;k++)
+ {
+ if (rtlight->cullmins[k] < rtlight->shadoworigin[k] - rtlight->radius) rtlight->cullmins[k] = rtlight->shadoworigin[k] - rtlight->radius;
+ if (rtlight->cullmaxs[k] > rtlight->shadoworigin[k] + rtlight->radius) rtlight->cullmaxs[k] = rtlight->shadoworigin[k] + rtlight->radius;
+ }
+ rtlight->cullradius = RadiusFromBoundsAndOrigin(rtlight->cullmins, rtlight->cullmaxs, rtlight->shadoworigin);
+ rtlight->cullradius = min(rtlight->cullradius, rtlight->radius);
+
+ // cast shadow volume from castmesh
+ castmesh = Mod_ShadowMesh_Finish(r_shadow_mempool, castmesh, false, true);
+ if (castmesh)
+ {
+ maxverts = 0;
+ for (mesh = castmesh;mesh;mesh = mesh->next)
+ {
+ R_Shadow_ResizeShadowElements(mesh->numtriangles);
+ maxverts = max(maxverts, mesh->numverts * 2);
+ }
+
+ if (maxverts > 0)
+ {
+ vertex3f = Mem_Alloc(r_shadow_mempool, maxverts * sizeof(float[3]));
+ // now that we have the buffers big enough, construct and add
+ // the shadow volume mesh
+ if (rtlight->shadow)
+ rtlight->static_meshchain_shadow = Mod_ShadowMesh_Begin(r_shadow_mempool, 32768, 32768, NULL, NULL, NULL, false, false, true);
+ for (mesh = castmesh;mesh;mesh = mesh->next)
+ {
+ Mod_BuildTriangleNeighbors(mesh->neighbor3i, mesh->element3i, mesh->numtriangles);
+ if ((tris = R_Shadow_ConstructShadowVolume(castmesh->numverts, 0, castmesh->numtriangles, castmesh->element3i, castmesh->neighbor3i, castmesh->vertex3f, NULL, shadowelements, vertex3f, rtlight->shadoworigin, r_shadow_projectdistance.value)))
+ Mod_ShadowMesh_AddMesh(r_shadow_mempool, rtlight->static_meshchain_shadow, NULL, NULL, NULL, vertex3f, NULL, NULL, NULL, NULL, tris, shadowelements);
+ }
+ Mem_Free(vertex3f);
+ vertex3f = NULL;
+ }
+ // we're done with castmesh now
+ Mod_ShadowMesh_Free(castmesh);
+ }
+
+ rtlight->static_meshchain_shadow = Mod_ShadowMesh_Finish(r_shadow_mempool, rtlight->static_meshchain_shadow, false, false);
+ rtlight->static_meshchain_light = Mod_ShadowMesh_Finish(r_shadow_mempool, rtlight->static_meshchain_light, true, false);
+
+ k = 0;
+ if (rtlight->static_meshchain_shadow)
+ for (mesh = rtlight->static_meshchain_shadow;mesh;mesh = mesh->next)
+ k += mesh->numtriangles;
+ l = 0;
+ if (rtlight->static_meshchain_light)
+ for (mesh = rtlight->static_meshchain_light;mesh;mesh = mesh->next)
+ l += mesh->numtriangles;
+ Con_DPrintf("static light built: %f %f %f : %f %f %f box, %i shadow volume triangles, %i light triangles\n", rtlight->cullmins[0], rtlight->cullmins[1], rtlight->cullmins[2], rtlight->cullmaxs[0], rtlight->cullmaxs[1], rtlight->cullmaxs[2], k, l);
+}
+
+void R_RTLight_Uncompile(rtlight_t *rtlight)
+{
+ if (rtlight->compiled)
+ {
+ if (rtlight->static_meshchain_shadow)
+ Mod_ShadowMesh_Free(rtlight->static_meshchain_shadow);
+ rtlight->static_meshchain_shadow = NULL;
+ if (rtlight->static_meshchain_light)
+ Mod_ShadowMesh_Free(rtlight->static_meshchain_light);
+ rtlight->static_meshchain_light = NULL;
+ if (rtlight->static_clusterindices)
+ Mem_Free(rtlight->static_clusterindices);
+ rtlight->static_clusterindices = NULL;
+ rtlight->static_numclusters = 0;
+ rtlight->compiled = false;
+ }
+}
+
+int shadowframecount = 0;
+
+void R_TestAndDrawShadowVolume(entity_render_t *ent, vec3_t shadoworigin, vec_t shadowradius, vec3_t cullmins, vec3_t cullmaxs)
+{
+ // rough checks
+ if ((BoxesOverlap(ent->mins, ent->maxs, cullmins, cullmaxs) || !r_shadow_cull.integer) && (ent->flags & RENDER_SHADOW) && ent->model && ent->model->DrawShadowVolume)
+ {
+ vec3_t relativeshadoworigin;
+ Matrix4x4_Transform(&ent->inversematrix, shadoworigin, relativeshadoworigin);
+ ent->model->DrawShadowVolume (ent, relativeshadoworigin, shadowradius);
+ }
+}
+
+void R_Shadow_DrawWorldLightShadowVolume(matrix4x4_t *matrix, dlight_t *light);
+
+void R_DrawRTLight(rtlight_t *rtlight, int visiblevolumes)
+{
+ int i, shadow;
+ entity_render_t *ent;
+ float f;
+ vec3_t relativelightorigin, relativeeyeorigin, lightcolor;
+ rtexture_t *cubemaptexture;
+ matrix4x4_t matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz;
+
+ if (d_lightstylevalue[rtlight->style] <= 0)
+ return;
+ if (rtlight->compiled)
+ {
+ if (R_CullBox(rtlight->cullmins, rtlight->cullmaxs))
+ return;
+ for (i = 0;i < rtlight->static_numclusters;i++)
+ if (CHECKPVSBIT(r_pvsbits, rtlight->static_clusterindices[i]))
+ break;
+ if (i == rtlight->static_numclusters)
+ return;
+ }
+ else if (VIS_CullBox(rtlight->cullmins, rtlight->cullmaxs))
+ return;
+ if (R_Shadow_ScissorForBBox(rtlight->cullmins, rtlight->cullmaxs))
+ return;
+
+ if (rtlight->isstatic && !rtlight->compiled && r_shadow_staticworldlights.integer)
+ R_RTLight_Compile(rtlight);
+
+ f = d_lightstylevalue[rtlight->style] * (1.0f / 256.0f);
+ VectorScale(rtlight->color, f, lightcolor);
+ /*
+ if (rtlight->selected)
+ {
+ f = 2 + sin(realtime * M_PI * 4.0);
+ VectorScale(lightcolor, f, lightcolor);
+ }
+ */
+
+ if (rtlight->cubemapname[0])
+ cubemaptexture = R_Shadow_Cubemap(rtlight->cubemapname);
+ else
+ cubemaptexture = NULL;
+
+ shadow = rtlight->shadow && (rtlight->isstatic ? r_shadow_worldshadows.integer : r_shadow_dlightshadows.integer);
+ if (shadow && (gl_stencil || visiblevolumes))
+ {
+ if (!visiblevolumes)
+ R_Shadow_Stage_ShadowVolumes();
+ ent = &cl_entities[0].render;
+ if (r_shadow_staticworldlights.integer && rtlight->compiled)
+ {
+ R_Mesh_Matrix(&ent->matrix);
+ if (r_shadow_showtris.integer)
+ {
+ shadowmesh_t *mesh;
+ rmeshstate_t m;
+ int depthenabled = qglIsEnabled(GL_DEPTH_TEST);
+ int stencilenabled = qglIsEnabled(GL_STENCIL_TEST);
+ qglDisable(GL_DEPTH_TEST);
+ qglDisable(GL_STENCIL_TEST);
+ //qglDisable(GL_CULL_FACE);
+ qglColorMask(1,1,1,1);
+ memset(&m, 0, sizeof(m));
+ R_Mesh_State_Texture(&m);
+ GL_Color(0,0.1,0,1);
+ GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
+ for (mesh = rtlight->static_meshchain_shadow;mesh;mesh = mesh->next)
+ {
+ GL_VertexPointer(mesh->vertex3f);
+ R_Mesh_Draw_ShowTris(mesh->numverts, mesh->numtriangles, mesh->element3i);
+ }
+ //qglEnable(GL_CULL_FACE);
+ if (depthenabled)
+ qglEnable(GL_DEPTH_TEST);
+ if (stencilenabled)
+ {
+ qglEnable(GL_STENCIL_TEST);
+ qglColorMask(0,0,0,0);
+ }
+ }
+ R_Shadow_RenderShadowMeshVolume(rtlight->static_meshchain_shadow);
+ }
+ else
+ R_TestAndDrawShadowVolume(ent, rtlight->shadoworigin, rtlight->radius, rtlight->cullmins, rtlight->cullmaxs);
+ if (r_drawentities.integer)
+ for (i = 0;i < r_refdef.numentities;i++)
+ if (r_refdef.entities[i]->flags & RENDER_SHADOW)
+ R_TestAndDrawShadowVolume(r_refdef.entities[i], rtlight->shadoworigin, rtlight->radius, rtlight->cullmins, rtlight->cullmaxs);
+ }
+
+ if (!visiblevolumes)
+ {
+ if (shadow && gl_stencil)
+ R_Shadow_Stage_LightWithShadows();
+ else
+ R_Shadow_Stage_LightWithoutShadows();
+
+ ent = &cl_entities[0].render;
+ if (ent->model && ent->model->DrawLight)
+ {
+ Matrix4x4_Transform(&ent->inversematrix, rtlight->shadoworigin, relativelightorigin);
+ Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, relativeeyeorigin);
+ Matrix4x4_Concat(&matrix_modeltolight, &rtlight->matrix_worldtolight, &ent->matrix);
+ Matrix4x4_Concat(&matrix_modeltoattenuationxyz, &rtlight->matrix_worldtoattenuationxyz, &ent->matrix);
+ Matrix4x4_Concat(&matrix_modeltoattenuationz, &rtlight->matrix_worldtoattenuationz, &ent->matrix);
+ if (r_shadow_staticworldlights.integer && rtlight->compiled)
+ {
+ //R_Shadow_DrawStaticWorldLight_Light(rtlight, &ent->matrix, relativelightorigin, relativeeyeorigin, rtlight->radius, lightcolor, &matrix_modeltolight, &matrix_modeltoattenuationxyz, &matrix_modeltoattenuationz, cubemaptexture);
+ shadowmesh_t *mesh;
+ R_Mesh_Matrix(&ent->matrix);
+ if (r_shadow_showtris.integer)
+ {
+ rmeshstate_t m;
+ int depthenabled = qglIsEnabled(GL_DEPTH_TEST);
+ int stencilenabled = qglIsEnabled(GL_STENCIL_TEST);
+ qglDisable(GL_DEPTH_TEST);
+ qglDisable(GL_STENCIL_TEST);
+ //qglDisable(GL_CULL_FACE);
+ memset(&m, 0, sizeof(m));
+ R_Mesh_State_Texture(&m);
+ GL_Color(0.2,0,0,1);
+ GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
+ for (mesh = rtlight->static_meshchain_light;mesh;mesh = mesh->next)
+ {
+ GL_VertexPointer(mesh->vertex3f);
+ R_Mesh_Draw_ShowTris(mesh->numverts, mesh->numtriangles, mesh->element3i);
+ }
+ //qglEnable(GL_CULL_FACE);
+ if (depthenabled)
+ qglEnable(GL_DEPTH_TEST);
+ if (stencilenabled)
+ qglEnable(GL_STENCIL_TEST);
+ }
+ for (mesh = rtlight->static_meshchain_light;mesh;mesh = mesh->next)
+ {
+ R_Shadow_DiffuseLighting(mesh->numverts, mesh->numtriangles, mesh->element3i, mesh->vertex3f, mesh->svector3f, mesh->tvector3f, mesh->normal3f, mesh->texcoord2f, relativelightorigin, lightcolor, &matrix_modeltolight, &matrix_modeltoattenuationxyz, &matrix_modeltoattenuationz, mesh->map_diffuse, mesh->map_normal, cubemaptexture);
+ R_Shadow_SpecularLighting(mesh->numverts, mesh->numtriangles, mesh->element3i, mesh->vertex3f, mesh->svector3f, mesh->tvector3f, mesh->normal3f, mesh->texcoord2f, relativelightorigin, relativeeyeorigin, lightcolor, &matrix_modeltolight, &matrix_modeltoattenuationxyz, &matrix_modeltoattenuationz, mesh->map_specular, mesh->map_normal, cubemaptexture);
+ }
+ }
+ else
+ ent->model->DrawLight(ent, relativelightorigin, relativeeyeorigin, rtlight->radius, lightcolor, &matrix_modeltolight, &matrix_modeltoattenuationxyz, &matrix_modeltoattenuationz, cubemaptexture);
+ }
+ 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, rtlight->cullmins, rtlight->cullmaxs)
+ && (ent->flags & RENDER_LIGHT))
+ {
+ Matrix4x4_Transform(&ent->inversematrix, rtlight->shadoworigin, relativelightorigin);
+ Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, relativeeyeorigin);
+ Matrix4x4_Concat(&matrix_modeltolight, &rtlight->matrix_worldtolight, &ent->matrix);
+ Matrix4x4_Concat(&matrix_modeltoattenuationxyz, &rtlight->matrix_worldtoattenuationxyz, &ent->matrix);
+ Matrix4x4_Concat(&matrix_modeltoattenuationz, &rtlight->matrix_worldtoattenuationz, &ent->matrix);
+ ent->model->DrawLight(ent, relativelightorigin, relativeeyeorigin, rtlight->radius, lightcolor, &matrix_modeltolight, &matrix_modeltoattenuationxyz, &matrix_modeltoattenuationz, cubemaptexture);
+ }
+ }
+ }