+ // FIXME: detect blendsquare!
+ //if (gl_support_blendsquare)
+ {
+ colorscale = r_shadow_lightintensityscale.value * r_shadow_glossintensity.value;
+ if (glosstexture == r_shadow_blankglosstexture)
+ colorscale *= r_shadow_gloss2intensity.value;
+ GL_Color(1,1,1,1);
+ if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && lightcubemap /*&& gl_support_blendsquare*/) // FIXME: detect blendsquare!
+ {
+ // 2/0/0/1/2 3D combine blendsquare path
+ memset(&m, 0, sizeof(m));
+ m.pointer_vertex = vertex3f;
+ m.tex[0] = R_GetTexture(bumptexture);
+ m.pointer_texcoord[0] = texcoord2f;
+ m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
+ m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
+ m.pointer_texcoord3f[1] = varray_texcoord3f[1];
+ R_Shadow_GenTexCoords_Specular_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin, relativeeyeorigin);
+ R_Mesh_State(&m);
+ GL_ColorMask(0,0,0,1);
+ // this squares the result
+ GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
+ GL_LockArrays(0, numverts);
+ R_Mesh_Draw(numverts, numtriangles, elements);
+ GL_LockArrays(0, 0);
+ c_rt_lightmeshes++;
+ c_rt_lighttris += numtriangles;
+
+ memset(&m, 0, sizeof(m));
+ m.pointer_vertex = vertex3f;
+ R_Mesh_State(&m);
+ GL_LockArrays(0, numverts);
+ // square alpha in framebuffer a few times to make it shiny
+ GL_BlendFunc(GL_ZERO, GL_DST_ALPHA);
+ // these comments are a test run through this math for intensity 0.5
+ // 0.5 * 0.5 = 0.25 (done by the BlendFunc earlier)
+ // 0.25 * 0.25 = 0.0625 (this is another pass)
+ // 0.0625 * 0.0625 = 0.00390625 (this is another pass)
+ R_Mesh_Draw(numverts, numtriangles, elements);
+ c_rt_lightmeshes++;
+ c_rt_lighttris += numtriangles;
+ R_Mesh_Draw(numverts, numtriangles, elements);
+ c_rt_lightmeshes++;
+ c_rt_lighttris += numtriangles;
+ GL_LockArrays(0, 0);
+
+ memset(&m, 0, sizeof(m));
+ m.pointer_vertex = vertex3f;
+ m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
+#if USETEXMATRIX
+ m.pointer_texcoord3f[0] = vertex3f;
+ m.texmatrix[0] = *matrix_modeltoattenuationxyz;
+#else
+ m.pointer_texcoord3f[0] = varray_texcoord3f[0];
+ R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[0], numverts, vertex3f, matrix_modeltoattenuationxyz);
+#endif
+ R_Mesh_State(&m);
+ GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
+ GL_LockArrays(0, numverts);
+ R_Mesh_Draw(numverts, numtriangles, elements);
+ GL_LockArrays(0, 0);
+ c_rt_lightmeshes++;
+ c_rt_lighttris += numtriangles;
+
+ memset(&m, 0, sizeof(m));
+ m.pointer_vertex = vertex3f;
+ m.tex[0] = R_GetTexture(glosstexture);
+ m.pointer_texcoord[0] = texcoord2f;
+ if (lightcubemap)
+ {
+ m.texcubemap[1] = R_GetTexture(lightcubemap);
+#if USETEXMATRIX
+ m.pointer_texcoord3f[1] = vertex3f;
+ m.texmatrix[1] = *matrix_modeltolight;
+#else
+ m.pointer_texcoord3f[1] = varray_texcoord3f[1];
+ R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltolight);
+#endif
+ }
+ }
+ else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && !lightcubemap /*&& gl_support_blendsquare*/) // FIXME: detect blendsquare!
+ {
+ // 2/0/0/2 3D combine blendsquare path
+ memset(&m, 0, sizeof(m));
+ m.pointer_vertex = vertex3f;
+ m.tex[0] = R_GetTexture(bumptexture);
+ m.pointer_texcoord[0] = texcoord2f;
+ m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
+ m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
+ m.pointer_texcoord3f[1] = varray_texcoord3f[1];
+ R_Shadow_GenTexCoords_Specular_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin, relativeeyeorigin);
+ R_Mesh_State(&m);
+ GL_ColorMask(0,0,0,1);
+ // this squares the result
+ GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
+ GL_LockArrays(0, numverts);
+ R_Mesh_Draw(numverts, numtriangles, elements);
+ GL_LockArrays(0, 0);
+ c_rt_lightmeshes++;
+ c_rt_lighttris += numtriangles;
+
+ memset(&m, 0, sizeof(m));
+ m.pointer_vertex = vertex3f;
+ R_Mesh_State(&m);
+ GL_LockArrays(0, numverts);
+ // square alpha in framebuffer a few times to make it shiny
+ GL_BlendFunc(GL_ZERO, GL_DST_ALPHA);
+ // these comments are a test run through this math for intensity 0.5
+ // 0.5 * 0.5 = 0.25 (done by the BlendFunc earlier)
+ // 0.25 * 0.25 = 0.0625 (this is another pass)
+ // 0.0625 * 0.0625 = 0.00390625 (this is another pass)
+ R_Mesh_Draw(numverts, numtriangles, elements);
+ c_rt_lightmeshes++;
+ c_rt_lighttris += numtriangles;
+ R_Mesh_Draw(numverts, numtriangles, elements);
+ c_rt_lightmeshes++;
+ c_rt_lighttris += numtriangles;
+ GL_LockArrays(0, 0);
+
+ memset(&m, 0, sizeof(m));
+ m.pointer_vertex = vertex3f;
+ m.tex[0] = R_GetTexture(glosstexture);
+ m.pointer_texcoord[0] = texcoord2f;
+ m.tex3d[1] = R_GetTexture(r_shadow_attenuation3dtexture);
+#if USETEXMATRIX
+ m.pointer_texcoord3f[1] = vertex3f;
+ m.texmatrix[1] = *matrix_modeltoattenuationxyz;
+#else
+ m.pointer_texcoord3f[1] = varray_texcoord3f[1];
+ R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltoattenuationxyz);
+#endif
+ }
+ else
+ {
+ // 2/0/0/2/2 2D combine blendsquare path
+ memset(&m, 0, sizeof(m));
+ m.pointer_vertex = vertex3f;
+ m.tex[0] = R_GetTexture(bumptexture);
+ m.pointer_texcoord[0] = texcoord2f;
+ m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
+ m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
+ m.pointer_texcoord3f[1] = varray_texcoord3f[1];
+ R_Shadow_GenTexCoords_Specular_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin, relativeeyeorigin);
+ R_Mesh_State(&m);
+ GL_ColorMask(0,0,0,1);
+ // this squares the result
+ GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
+ GL_LockArrays(0, numverts);
+ R_Mesh_Draw(numverts, numtriangles, elements);
+ GL_LockArrays(0, 0);
+ c_rt_lightmeshes++;
+ c_rt_lighttris += numtriangles;
+
+ memset(&m, 0, sizeof(m));
+ m.pointer_vertex = vertex3f;
+ R_Mesh_State(&m);
+ GL_LockArrays(0, numverts);
+ // square alpha in framebuffer a few times to make it shiny
+ GL_BlendFunc(GL_ZERO, GL_DST_ALPHA);
+ // these comments are a test run through this math for intensity 0.5
+ // 0.5 * 0.5 = 0.25 (done by the BlendFunc earlier)
+ // 0.25 * 0.25 = 0.0625 (this is another pass)
+ // 0.0625 * 0.0625 = 0.00390625 (this is another pass)
+ R_Mesh_Draw(numverts, numtriangles, elements);
+ c_rt_lightmeshes++;
+ c_rt_lighttris += numtriangles;
+ R_Mesh_Draw(numverts, numtriangles, elements);
+ c_rt_lightmeshes++;
+ c_rt_lighttris += numtriangles;
+ GL_LockArrays(0, 0);
+
+ memset(&m, 0, sizeof(m));
+ m.pointer_vertex = vertex3f;
+ m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
+#if USETEXMATRIX
+ m.pointer_texcoord3f[0] = vertex3f;
+ m.texmatrix[0] = *matrix_modeltoattenuationxyz;
+#else
+ m.pointer_texcoord[0] = varray_texcoord2f[0];
+ R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[0], numverts, vertex3f, matrix_modeltoattenuationxyz);
+#endif
+ m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
+#if USETEXMATRIX
+ m.pointer_texcoord3f[1] = vertex3f;
+ m.texmatrix[1] = *matrix_modeltoattenuationz;
+#else
+ m.pointer_texcoord[1] = varray_texcoord2f[1];
+ R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[1], numverts, vertex3f, matrix_modeltoattenuationz);
+#endif
+ R_Mesh_State(&m);
+ GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
+ GL_LockArrays(0, numverts);
+ R_Mesh_Draw(numverts, numtriangles, elements);
+ GL_LockArrays(0, 0);
+ c_rt_lightmeshes++;
+ c_rt_lighttris += numtriangles;
+
+ memset(&m, 0, sizeof(m));
+ m.pointer_vertex = vertex3f;
+ m.tex[0] = R_GetTexture(glosstexture);
+ m.pointer_texcoord[0] = texcoord2f;
+ if (lightcubemap)
+ {
+ m.texcubemap[1] = R_GetTexture(lightcubemap);
+#if USETEXMATRIX
+ m.pointer_texcoord3f[1] = vertex3f;
+ m.texmatrix[1] = *matrix_modeltolight;
+#else
+ m.pointer_texcoord3f[1] = varray_texcoord3f[1];
+ R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltolight);
+#endif
+ }
+ }
+ }
+ R_Mesh_State(&m);
+ GL_ColorMask(1,1,1,0);
+ GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
+ VectorScale(lightcolor, colorscale, color2);
+ GL_LockArrays(0, numverts);
+ for (renders = 0;renders < 64 && (color2[0] > 0 || color2[1] > 0 || color2[2] > 0);renders++, color2[0]--, color2[1]--, color2[2]--)
+ {
+ GL_Color(bound(0, color2[0], 1), bound(0, color2[1], 1), bound(0, color2[2], 1), 1);
+ R_Mesh_Draw(numverts, numtriangles, elements);
+ c_rt_lightmeshes++;
+ c_rt_lighttris += numtriangles;
+ }
+ GL_LockArrays(0, 0);
+ }
+ }
+ else
+ {
+ if (lighting & LIGHTING_DIFFUSE)
+ {
+ GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
+ VectorScale(lightcolor, r_shadow_lightintensityscale.value, color2);
+ memset(&m, 0, sizeof(m));
+ m.pointer_vertex = vertex3f;
+ m.pointer_color = varray_color4f;
+ m.tex[0] = R_GetTexture(basetexture);
+ m.pointer_texcoord[0] = texcoord2f;
+ if (r_textureunits.integer >= 2)
+ {
+ // voodoo2
+ m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
+#if USETEXMATRIX
+ m.pointer_texcoord3f[1] = vertex3f;
+ m.texmatrix[1] = *matrix_modeltoattenuationxyz;
+#else
+ m.pointer_texcoord[1] = varray_texcoord2f[1];
+ R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[1], numverts, vertex3f, matrix_modeltoattenuationxyz);
+#endif
+ if (r_textureunits.integer >= 3)
+ {
+ // Geforce3/Radeon class but not using dot3
+ m.tex[2] = R_GetTexture(r_shadow_attenuation2dtexture);
+#if USETEXMATRIX
+ m.pointer_texcoord3f[2] = vertex3f;
+ m.texmatrix[2] = *matrix_modeltoattenuationz;
+#else
+ m.pointer_texcoord[2] = varray_texcoord2f[2];
+ R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[2], numverts, vertex3f, matrix_modeltoattenuationz);
+#endif
+ }
+ }
+ R_Mesh_State(&m);
+ 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);
+ if (r_textureunits.integer >= 3)
+ R_Shadow_VertexShading(numverts, vertex3f, normal3f, color, matrix_modeltolight);
+ else if (r_textureunits.integer >= 2)
+ R_Shadow_VertexShadingWithZAttenuation(numverts, vertex3f, normal3f, color, matrix_modeltolight);
+ else
+ R_Shadow_VertexShadingWithXYZAttenuation(numverts, vertex3f, normal3f, color, matrix_modeltolight);
+ GL_LockArrays(0, numverts);
+ R_Mesh_Draw(numverts, numtriangles, elements);
+ GL_LockArrays(0, 0);
+ 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->radius * rtlight->radius;
+ rtlight->cullmins[0] = rtlight->shadoworigin[0] - rtlight->radius;
+ rtlight->cullmins[1] = rtlight->shadoworigin[1] - rtlight->radius;
+ rtlight->cullmins[2] = rtlight->shadoworigin[2] - rtlight->radius;
+ rtlight->cullmaxs[0] = rtlight->shadoworigin[0] + rtlight->radius;
+ rtlight->cullmaxs[1] = rtlight->shadoworigin[1] + rtlight->radius;
+ rtlight->cullmaxs[2] = rtlight->shadoworigin[2] + rtlight->radius;
+ 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.125f, rtlight->lightmap_light);
+ rtlight->lightmap_subtract = 1.0f / rtlight->lightmap_cullradius2;
+}
+
+rtlight_t *r_shadow_compilingrtlight;
+
+// compiles rtlight geometry
+// (undone by R_FreeCompiledRTLight, which R_UpdateLight calls)
+void R_RTLight_Compile(rtlight_t *rtlight)
+{
+ int shadowmeshes, shadowtris, lightmeshes, lighttris, numclusters, numsurfaces;
+ entity_render_t *ent = &cl_entities[0].render;
+ model_t *model = ent->model;
+
+ // compile the light
+ rtlight->compiled = true;
+ rtlight->static_numclusters = 0;
+ rtlight->static_numclusterpvsbytes = 0;
+ rtlight->static_clusterlist = NULL;
+ rtlight->static_clusterpvs = NULL;
+ rtlight->cullmins[0] = rtlight->shadoworigin[0] - rtlight->radius;
+ rtlight->cullmins[1] = rtlight->shadoworigin[1] - rtlight->radius;
+ rtlight->cullmins[2] = rtlight->shadoworigin[2] - rtlight->radius;
+ rtlight->cullmaxs[0] = rtlight->shadoworigin[0] + rtlight->radius;
+ rtlight->cullmaxs[1] = rtlight->shadoworigin[1] + rtlight->radius;
+ rtlight->cullmaxs[2] = rtlight->shadoworigin[2] + rtlight->radius;
+
+ if (model && model->GetLightInfo)
+ {
+ // this variable directs the DrawShadowVolume and DrawLight code to capture into the mesh chain instead of rendering
+ r_shadow_compilingrtlight = rtlight;
+ R_Shadow_EnlargeClusterBuffer(model->brush.num_pvsclusters);
+ R_Shadow_EnlargeSurfaceBuffer(model->numsurfaces);
+ model->GetLightInfo(ent, rtlight->shadoworigin, rtlight->radius, rtlight->cullmins, rtlight->cullmaxs, r_shadow_buffer_clusterlist, r_shadow_buffer_clusterpvs, &numclusters, r_shadow_buffer_surfacelist, r_shadow_buffer_surfacepvs, &numsurfaces);
+ if (numclusters)
+ {
+ rtlight->static_numclusters = numclusters;
+ rtlight->static_numclusterpvsbytes = (model->brush.num_pvsclusters + 7) >> 3;
+ rtlight->static_clusterlist = Mem_Alloc(r_shadow_mempool, rtlight->static_numclusters * sizeof(*rtlight->static_clusterlist));
+ rtlight->static_clusterpvs = Mem_Alloc(r_shadow_mempool, rtlight->static_numclusterpvsbytes);
+ memcpy(rtlight->static_clusterlist, r_shadow_buffer_clusterlist, rtlight->static_numclusters * sizeof(*rtlight->static_clusterlist));
+ memcpy(rtlight->static_clusterpvs, r_shadow_buffer_clusterpvs, rtlight->static_numclusterpvsbytes);
+ }
+ if (model->DrawShadowVolume && rtlight->shadow)
+ {
+ rtlight->static_meshchain_shadow = Mod_ShadowMesh_Begin(r_shadow_mempool, 32768, 32768, NULL, NULL, NULL, false, false, true);
+ model->DrawShadowVolume(ent, rtlight->shadoworigin, rtlight->radius, numsurfaces, r_shadow_buffer_surfacelist);
+ rtlight->static_meshchain_shadow = Mod_ShadowMesh_Finish(r_shadow_mempool, rtlight->static_meshchain_shadow, false, false);
+ }
+ if (model->DrawLight)
+ {
+ rtlight->static_meshchain_light = Mod_ShadowMesh_Begin(r_shadow_mempool, 32768, 32768, NULL, NULL, NULL, true, false, true);
+ model->DrawLight(ent, rtlight->shadoworigin, vec3_origin, rtlight->radius, vec3_origin, &r_identitymatrix, &r_identitymatrix, &r_identitymatrix, NULL, numsurfaces, r_shadow_buffer_surfacelist);
+ rtlight->static_meshchain_light = Mod_ShadowMesh_Finish(r_shadow_mempool, rtlight->static_meshchain_light, true, false);
+ }
+ // switch back to rendering when DrawShadowVolume or DrawLight is called
+ r_shadow_compilingrtlight = NULL;
+ }
+
+
+ // use smallest available cullradius - box radius or light radius
+ //rtlight->cullradius = RadiusFromBoundsAndOrigin(rtlight->cullmins, rtlight->cullmaxs, rtlight->shadoworigin);
+ //rtlight->cullradius = min(rtlight->cullradius, rtlight->radius);
+
+ shadowmeshes = 0;
+ shadowtris = 0;
+ if (rtlight->static_meshchain_shadow)
+ {
+ shadowmesh_t *mesh;
+ for (mesh = rtlight->static_meshchain_shadow;mesh;mesh = mesh->next)
+ {
+ shadowmeshes++;
+ shadowtris += mesh->numtriangles;
+ }
+ }
+
+ lightmeshes = 0;
+ lighttris = 0;
+ if (rtlight->static_meshchain_light)
+ {
+ shadowmesh_t *mesh;
+ for (mesh = rtlight->static_meshchain_light;mesh;mesh = mesh->next)
+ {
+ lightmeshes++;
+ lighttris += mesh->numtriangles;
+ }
+ }
+
+ Con_DPrintf("static light built: %f %f %f : %f %f %f box, %i shadow volume triangles (in %i meshes), %i light triangles (in %i meshes)\n", rtlight->cullmins[0], rtlight->cullmins[1], rtlight->cullmins[2], rtlight->cullmaxs[0], rtlight->cullmaxs[1], rtlight->cullmaxs[2], shadowtris, shadowmeshes, lighttris, lightmeshes);
+}
+
+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_clusterlist)
+ Mem_Free(rtlight->static_clusterlist);
+ rtlight->static_clusterlist = NULL;
+ if (rtlight->static_clusterpvs)
+ Mem_Free(rtlight->static_clusterpvs);
+ rtlight->static_clusterpvs = NULL;
+ rtlight->static_numclusters = 0;
+ rtlight->static_numclusterpvsbytes = 0;
+ rtlight->compiled = false;
+ }
+}
+
+int shadowframecount = 0;
+
+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;
+ int numclusters, numsurfaces;
+ int *clusterlist, *surfacelist;
+ qbyte *clusterpvs;
+ vec3_t cullmins, cullmaxs;
+ shadowmesh_t *mesh;
+ rmeshstate_t m;
+
+ if (d_lightstylevalue[rtlight->style] <= 0)
+ return;
+ cullmins[0] = rtlight->shadoworigin[0] - rtlight->radius;
+ cullmins[1] = rtlight->shadoworigin[1] - rtlight->radius;
+ cullmins[2] = rtlight->shadoworigin[2] - rtlight->radius;
+ cullmaxs[0] = rtlight->shadoworigin[0] + rtlight->radius;
+ cullmaxs[1] = rtlight->shadoworigin[1] + rtlight->radius;
+ cullmaxs[2] = rtlight->shadoworigin[2] + rtlight->radius;
+ if (R_CullBox(cullmins, cullmaxs))
+ return;
+ if (rtlight->isstatic && !rtlight->compiled && r_shadow_staticworldlights.integer)
+ R_RTLight_Compile(rtlight);
+ numclusters = 0;
+ clusterlist = NULL;
+ clusterpvs = NULL;
+ numsurfaces = 0;
+ surfacelist = NULL;
+ if (rtlight->compiled && r_shadow_staticworldlights.integer)
+ {
+ numclusters = rtlight->static_numclusters;
+ clusterlist = rtlight->static_clusterlist;
+ clusterpvs = rtlight->static_clusterpvs;
+ VectorCopy(rtlight->cullmins, cullmins);
+ VectorCopy(rtlight->cullmaxs, cullmaxs);
+ }
+ else if (cl.worldmodel && cl.worldmodel->GetLightInfo)
+ {
+ R_Shadow_EnlargeClusterBuffer(cl.worldmodel->brush.num_pvsclusters);
+ R_Shadow_EnlargeSurfaceBuffer(cl.worldmodel->numsurfaces);
+ cl.worldmodel->GetLightInfo(&cl_entities[0].render, rtlight->shadoworigin, rtlight->radius, cullmins, cullmaxs, r_shadow_buffer_clusterlist, r_shadow_buffer_clusterpvs, &numclusters, r_shadow_buffer_surfacelist, r_shadow_buffer_surfacepvs, &numsurfaces);
+ clusterlist = r_shadow_buffer_clusterlist;
+ clusterpvs = r_shadow_buffer_clusterpvs;
+ surfacelist = r_shadow_buffer_surfacelist;
+ }
+ if (numclusters)
+ {
+ for (i = 0;i < numclusters;i++)
+ if (CHECKPVSBIT(r_pvsbits, clusterlist[i]))
+ break;
+ if (i == numclusters)
+ return;
+ }
+ if (R_CullBox(cullmins, cullmaxs))
+ return;
+ if (R_Shadow_ScissorForBBox(cullmins, cullmaxs))
+ return;
+
+ 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)
+ {
+ memset(&m, 0, sizeof(m));
+ R_Mesh_Matrix(&ent->matrix);
+ for (mesh = rtlight->static_meshchain_shadow;mesh;mesh = mesh->next)
+ {
+ m.pointer_vertex = mesh->vertex3f;
+ R_Mesh_State(&m);
+ GL_LockArrays(0, mesh->numverts);
+ if (r_shadowstage == SHADOWSTAGE_STENCIL)
+ {
+ // decrement stencil if frontface is behind depthbuffer
+ qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
+ qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
+ R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
+ c_rtcached_shadowmeshes++;
+ c_rtcached_shadowtris += mesh->numtriangles;
+ // increment stencil if backface is behind depthbuffer
+ qglCullFace(GL_BACK); // quake is backwards, this culls front faces
+ qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
+ }
+ R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
+ c_rtcached_shadowmeshes++;
+ c_rtcached_shadowtris += mesh->numtriangles;
+ GL_LockArrays(0, 0);
+ }
+ }
+ else
+ {
+ Matrix4x4_Transform(&ent->inversematrix, rtlight->shadoworigin, relativelightorigin);
+ ent->model->DrawShadowVolume(ent, relativelightorigin, rtlight->radius, numsurfaces, surfacelist);
+ }
+ if (r_drawentities.integer)
+ {
+ for (i = 0;i < r_refdef.numentities;i++)
+ {
+ ent = r_refdef.entities[i];
+ // rough checks
+ if (r_shadow_cull.integer)
+ {
+ if (!BoxesOverlap(ent->mins, ent->maxs, cullmins, cullmaxs))
+ continue;
+ if (cl.worldmodel != NULL && cl.worldmodel->brush.BoxTouchingPVS != NULL && !cl.worldmodel->brush.BoxTouchingPVS(cl.worldmodel, clusterpvs, ent->mins, ent->maxs))
+ continue;
+ }
+ if (!(ent->flags & RENDER_SHADOW) || !ent->model || !ent->model->DrawShadowVolume)
+ continue;
+ Matrix4x4_Transform(&ent->inversematrix, rtlight->shadoworigin, relativelightorigin);
+ // light emitting entities should not cast their own shadow
+ if (VectorLength2(relativelightorigin) < 0.1)
+ continue;
+ ent->model->DrawShadowVolume(ent, relativelightorigin, rtlight->radius, ent->model->numsurfaces, ent->model->surfacelist);
+ }
+ }
+ }
+
+ 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_Mesh_Matrix(&ent->matrix);
+ for (mesh = rtlight->static_meshchain_light;mesh;mesh = mesh->next)
+ R_Shadow_RenderLighting(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_diffuse, mesh->map_normal, mesh->map_specular, cubemaptexture, LIGHTING_DIFFUSE | LIGHTING_SPECULAR);
+ }
+ else
+ ent->model->DrawLight(ent, relativelightorigin, relativeeyeorigin, rtlight->radius, lightcolor, &matrix_modeltolight, &matrix_modeltoattenuationxyz, &matrix_modeltoattenuationz, cubemaptexture, numsurfaces, surfacelist);