+ c_rt_lightmeshes++;
+ c_rt_lighttris += numtriangles;
+
+ memset(&m, 0, sizeof(m));
+ m.tex[0] = R_GetTexture(basetexture);
+ m.pointer_texcoord[0] = texcoord2f;
+ 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);
+ }
+ R_Mesh_State_Texture(&m);
+ qglColorMask(1,1,1,0);
+ GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
+ VectorScale(lightcolor, r_shadow_lightintensityscale.value, 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;
+ }
+ }
+ else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && !lightcubemap)
+ {
+ // 2/2 3D combine path (original Radeon)
+ memset(&m, 0, sizeof(m));
+ m.tex[0] = R_GetTexture(bumptexture);
+ m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
+ m.texcombinergb[0] = GL_REPLACE;
+ m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
+ m.pointer_texcoord[0] = texcoord2f;
+ m.pointer_texcoord[1] = varray_texcoord3f[1];
+ R_Mesh_State_Texture(&m);
+ qglColorMask(0,0,0,1);
+ GL_BlendFunc(GL_ONE, GL_ZERO);
+ R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin);
+ R_Mesh_Draw(numverts, numtriangles, elements);
+ c_rt_lightmeshes++;
+ c_rt_lighttris += numtriangles;
+
+ memset(&m, 0, sizeof(m));
+ m.tex[0] = R_GetTexture(basetexture);
+ m.tex3d[1] = R_GetTexture(r_shadow_attenuation3dtexture);
+ m.pointer_texcoord[0] = texcoord2f;
+ m.pointer_texcoord[1] = varray_texcoord3f[1];
+ R_Mesh_State_Texture(&m);
+ qglColorMask(1,1,1,0);
+ GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
+ R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltoattenuationxyz);
+ VectorScale(lightcolor, r_shadow_lightintensityscale.value, 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;
+ }
+ }
+ else if (r_textureunits.integer >= 4)
+ {
+ // 4/2 2D combine path (Geforce3, Radeon 8500)
+ memset(&m, 0, sizeof(m));
+ m.tex[0] = R_GetTexture(bumptexture);
+ m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
+ m.texcombinergb[0] = GL_REPLACE;
+ m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
+ m.tex[2] = R_GetTexture(r_shadow_attenuation2dtexture);
+ m.tex[3] = R_GetTexture(r_shadow_attenuation2dtexture);
+ m.pointer_texcoord[0] = texcoord2f;
+ m.pointer_texcoord[1] = varray_texcoord3f[1];
+ m.pointer_texcoord[2] = varray_texcoord2f[2];
+ m.pointer_texcoord[3] = varray_texcoord2f[3];
+ R_Mesh_State_Texture(&m);
+ qglColorMask(0,0,0,1);
+ GL_BlendFunc(GL_ONE, GL_ZERO);
+ R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin);
+ R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[2], numverts, vertex3f, matrix_modeltoattenuationxyz);
+ R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[3], 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(basetexture);
+ m.pointer_texcoord[0] = texcoord2f;
+ 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);
+ }
+ R_Mesh_State_Texture(&m);
+ qglColorMask(1,1,1,0);
+ GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
+ VectorScale(lightcolor, r_shadow_lightintensityscale.value, 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;
+ }
+ }
+ else
+ {
+ // 2/2/2 2D combine path (any dot3 card)
+ 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);
+ qglColorMask(0,0,0,1);
+ GL_BlendFunc(GL_ONE, 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(bumptexture);
+ m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
+ m.texcombinergb[0] = GL_REPLACE;
+ m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
+ m.pointer_texcoord[0] = texcoord2f;
+ m.pointer_texcoord[1] = varray_texcoord3f[1];
+ R_Mesh_State_Texture(&m);
+ GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
+ R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin);
+ R_Mesh_Draw(numverts, numtriangles, elements);
+ c_rt_lightmeshes++;
+ c_rt_lighttris += numtriangles;
+
+ memset(&m, 0, sizeof(m));
+ m.tex[0] = R_GetTexture(basetexture);
+ m.pointer_texcoord[0] = texcoord2f;
+ 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);
+ }
+ R_Mesh_State_Texture(&m);
+ qglColorMask(1,1,1,0);
+ GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
+ VectorScale(lightcolor, r_shadow_lightintensityscale.value, 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;
+ }
+ }
+ }
+ else
+ {
+ GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
+ GL_DepthMask(false);
+ GL_DepthTest(true);
+ GL_ColorPointer(varray_color4f);
+ VectorScale(lightcolor, r_shadow_lightintensityscale.value, color2);
+ memset(&m, 0, sizeof(m));
+ 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);
+ m.pointer_texcoord[1] = varray_texcoord2f[1];
+ R_Shadow_Transform_Vertex3f_TexCoord2f(varray_texcoord2f[1], numverts, vertex3f, matrix_modeltoattenuationxyz);
+ }
+ R_Mesh_State_Texture(&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 >= 2)
+ R_Shadow_VertexLightingWithXYAttenuationTexture(numverts, vertex3f, normal3f, color, matrix_modeltolight);
+ else
+ R_Shadow_VertexLighting(numverts, vertex3f, normal3f, color, matrix_modeltolight);
+ R_Mesh_Draw(numverts, numtriangles, elements);
+ c_rt_lightmeshes++;
+ c_rt_lighttris += numtriangles;
+ }
+ }
+}
+
+void R_Shadow_SpecularLighting(int numverts, int numtriangles, const int *elements, const float *vertex3f, const float *svector3f, const float *tvector3f, const float *normal3f, const float *texcoord2f, const float *relativelightorigin, const float *relativeeyeorigin, float lightradius, const float *lightcolor, const matrix4x4_t *matrix_modeltolight, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz, rtexture_t *glosstexture, rtexture_t *bumptexture, rtexture_t *lightcubemap)
+{
+ int renders;
+ float color[3], color2[3], colorscale;
+ rmeshstate_t m;
+ if (!gl_dot3arb || !gl_texturecubemap || !gl_combine.integer || !gl_stencil)
+ return;
+ if (!glosstexture)
+ glosstexture = r_shadow_blankglosstexture;
+ if (r_shadow_gloss.integer >= 2 || (r_shadow_gloss.integer >= 1 && glosstexture != r_shadow_blankglosstexture))
+ {
+ colorscale = r_shadow_glossintensity.value;
+ if (!bumptexture)
+ bumptexture = r_shadow_blankbumptexture;
+ if (glosstexture == r_shadow_blankglosstexture)
+ colorscale *= r_shadow_gloss2intensity.value;
+ GL_VertexPointer(vertex3f);
+ 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.tex[0] = R_GetTexture(bumptexture);
+ m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
+ m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
+ m.pointer_texcoord[0] = texcoord2f;
+ m.pointer_texcoord[1] = varray_texcoord3f[1];
+ R_Mesh_State_Texture(&m);
+ qglColorMask(0,0,0,1);
+ // this squares the result
+ GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
+ R_Shadow_GenTexCoords_Specular_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin, relativeeyeorigin);
+ R_Mesh_Draw(numverts, numtriangles, elements);
+ c_rt_lightmeshes++;
+ c_rt_lighttris += numtriangles;
+
+ memset(&m, 0, sizeof(m));
+ R_Mesh_State_Texture(&m);
+ // 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;
+
+ memset(&m, 0, sizeof(m));
+ m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
+ m.pointer_texcoord[0] = varray_texcoord3f[0];
+ R_Mesh_State_Texture(&m);
+ GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
+ R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[0], numverts, vertex3f, matrix_modeltoattenuationxyz);
+ 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;
+ }
+ }
+ 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.tex[0] = R_GetTexture(bumptexture);
+ m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
+ m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
+ m.pointer_texcoord[0] = texcoord2f;
+ m.pointer_texcoord[1] = varray_texcoord3f[1];
+ R_Mesh_State_Texture(&m);
+ qglColorMask(0,0,0,1);
+ // this squares the result
+ GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
+ R_Shadow_GenTexCoords_Specular_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin, relativeeyeorigin);
+ R_Mesh_Draw(numverts, numtriangles, elements);
+ c_rt_lightmeshes++;
+ c_rt_lighttris += numtriangles;
+
+ memset(&m, 0, sizeof(m));
+ R_Mesh_State_Texture(&m);
+ // 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;
+
+ memset(&m, 0, sizeof(m));
+ m.tex[0] = R_GetTexture(glosstexture);
+ m.tex3d[1] = R_GetTexture(r_shadow_attenuation3dtexture);
+ m.pointer_texcoord[0] = texcoord2f;
+ m.pointer_texcoord[1] = varray_texcoord3f[1];
+ R_Mesh_State_Texture(&m);
+ qglColorMask(1,1,1,0);
+ GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
+ R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltoattenuationxyz);
+ 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;
+ }
+ }
+ else if (r_textureunits.integer >= 2 /*&& gl_support_blendsquare*/) // FIXME: detect blendsquare!
+ {
+ // 2/0/0/2/2 2D combine blendsquare path
+ memset(&m, 0, sizeof(m));
+ m.tex[0] = R_GetTexture(bumptexture);
+ m.texcubemap[1] = R_GetTexture(r_shadow_normalcubetexture);
+ m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
+ m.pointer_texcoord[0] = texcoord2f;
+ m.pointer_texcoord[1] = varray_texcoord3f[1];
+ R_Mesh_State_Texture(&m);
+ qglColorMask(0,0,0,1);
+ // this squares the result
+ GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
+ R_Shadow_GenTexCoords_Specular_NormalCubeMap(varray_texcoord3f[1], numverts, vertex3f, svector3f, tvector3f, normal3f, relativelightorigin, relativeeyeorigin);
+ R_Mesh_Draw(numverts, numtriangles, elements);
+ c_rt_lightmeshes++;
+ c_rt_lighttris += numtriangles;
+
+ memset(&m, 0, sizeof(m));
+ R_Mesh_State_Texture(&m);
+ // 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;
+
+ 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_Shadow_DrawStaticWorldLight_Shadow(worldlight_t *light, matrix4x4_t *matrix)
+{
+ R_Mesh_Matrix(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 = light->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(light->meshchain_shadow);
+}
+
+void R_Shadow_DrawStaticWorldLight_Light(worldlight_t *light, matrix4x4_t *matrix, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, const matrix4x4_t *matrix_modeltolight, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz)
+{
+ shadowmesh_t *mesh;
+ R_Mesh_Matrix(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 = light->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 = light->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, lightradius, lightcolor, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, mesh->map_diffuse, mesh->map_normal, light->cubemap);
+ R_Shadow_SpecularLighting(mesh->numverts, mesh->numtriangles, mesh->element3i, mesh->vertex3f, mesh->svector3f, mesh->tvector3f, mesh->normal3f, mesh->texcoord2f, relativelightorigin, relativeeyeorigin, lightradius, lightcolor, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, mesh->map_specular, mesh->map_normal, light->cubemap);
+ }
+}
+
+cvar_t r_editlights = {0, "r_editlights", "0"};
+cvar_t r_editlights_cursordistance = {0, "r_editlights_distance", "1024"};
+cvar_t r_editlights_cursorpushback = {0, "r_editlights_pushback", "0"};
+cvar_t r_editlights_cursorpushoff = {0, "r_editlights_pushoff", "4"};
+cvar_t r_editlights_cursorgrid = {0, "r_editlights_grid", "4"};
+cvar_t r_editlights_quakelightsizescale = {CVAR_SAVE, "r_editlights_quakelightsizescale", "0.8"};
+cvar_t r_editlights_rtlightssizescale = {CVAR_SAVE, "r_editlights_rtlightssizescale", "0.7"};
+cvar_t r_editlights_rtlightscolorscale = {CVAR_SAVE, "r_editlights_rtlightscolorscale", "2"};
+worldlight_t *r_shadow_worldlightchain;
+worldlight_t *r_shadow_selectedlight;
+vec3_t r_editlights_cursorlocation;
+
+static int lightpvsbytes;
+static qbyte lightpvs[(MAX_MAP_LEAFS + 7)/ 8];
+static qbyte lightfullpvs[(MAX_MAP_LEAFS + 7)/ 8];
+
+typedef struct cubemapinfo_s
+{
+ char basename[64];
+ rtexture_t *texture;
+}
+cubemapinfo_t;
+
+#define MAX_CUBEMAPS 128
+static int numcubemaps;
+static cubemapinfo_t cubemaps[MAX_CUBEMAPS];
+
+//static char *suffix[6] = {"ft", "bk", "rt", "lf", "up", "dn"};
+typedef struct suffixinfo_s
+{
+ char *suffix;
+ int flipx, flipy, flipdiagonal;
+}
+suffixinfo_t;
+static suffixinfo_t suffix[3][6] =
+{
+ {
+ {"posx", false, false, false},
+ {"negx", false, false, false},
+ {"posy", false, false, false},
+ {"negy", false, false, false},
+ {"posz", false, false, false},
+ {"negz", false, false, false}
+ },
+ {
+ {"px", false, false, false},
+ {"nx", false, false, false},
+ {"py", false, false, false},
+ {"ny", false, false, false},
+ {"pz", false, false, false},
+ {"nz", false, false, false}
+ },
+ {
+ {"ft", true, false, true},
+ {"bk", false, true, true},
+ {"lf", true, true, false},
+ {"rt", false, false, false},
+ {"up", false, false, false},
+ {"dn", false, false, false}
+ }
+};
+
+static int componentorder[4] = {0, 1, 2, 3};
+
+rtexture_t *R_Shadow_LoadCubemap(const char *basename)
+{
+ int i, j, cubemapsize;
+ qbyte *cubemappixels, *image_rgba;
+ rtexture_t *cubemaptexture;
+ char name[256];
+ // must start 0 so the first loadimagepixels has no requested width/height
+ cubemapsize = 0;
+ cubemappixels = NULL;
+ cubemaptexture = NULL;
+ for (j = 0;j < 3 && !cubemappixels;j++)
+ {
+ for (i = 0;i < 6;i++)
+ {
+ snprintf(name, sizeof(name), "%s%s", basename, suffix[j][i].suffix);
+ if ((image_rgba = loadimagepixels(name, false, cubemapsize, cubemapsize)))
+ {
+ if (image_width == image_height)
+ {
+ if (!cubemappixels && image_width >= 1)
+ {
+ cubemapsize = image_width;
+ // note this clears to black, so unavailable sizes are black
+ cubemappixels = Mem_Alloc(tempmempool, 6*cubemapsize*cubemapsize*4);
+ }
+ if (cubemappixels)
+ Image_CopyMux(cubemappixels+i*cubemapsize*cubemapsize*4, image_rgba, cubemapsize, cubemapsize, suffix[j][i].flipx, suffix[j][i].flipy, suffix[j][i].flipdiagonal, 4, 4, componentorder);
+ }
+ else
+ Con_Printf("Cubemap image \"%s\" (%ix%i) is not square, OpenGL requires square cubemaps.\n", name, image_width, image_height);
+ Mem_Free(image_rgba);
+ }
+ }
+ }
+ if (cubemappixels)
+ {
+ if (!r_shadow_filters_texturepool)
+ r_shadow_filters_texturepool = R_AllocTexturePool();
+ cubemaptexture = R_LoadTextureCubeMap(r_shadow_filters_texturepool, basename, cubemapsize, cubemappixels, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
+ Mem_Free(cubemappixels);
+ }
+ else
+ {
+ Con_Printf("Failed to load Cubemap \"%s\", tried ", basename);
+ for (j = 0;j < 3;j++)
+ for (i = 0;i < 6;i++)
+ Con_Printf("%s\"%s%s.tga\"", j + i > 0 ? ", " : "", basename, suffix[j][i].suffix);
+ Con_Printf(" and was unable to find any of them.\n");
+ }
+ return cubemaptexture;
+}
+
+rtexture_t *R_Shadow_Cubemap(const char *basename)
+{
+ int i;
+ for (i = 0;i < numcubemaps;i++)
+ if (!strcasecmp(cubemaps[i].basename, basename))
+ return cubemaps[i].texture;
+ if (i >= MAX_CUBEMAPS)
+ return NULL;
+ numcubemaps++;
+ strcpy(cubemaps[i].basename, basename);
+ cubemaps[i].texture = R_Shadow_LoadCubemap(cubemaps[i].basename);
+ return cubemaps[i].texture;
+}
+
+void R_Shadow_FreeCubemaps(void)
+{
+ numcubemaps = 0;
+ R_FreeTexturePool(&r_shadow_filters_texturepool);
+}
+
+void R_Shadow_NewWorldLight(vec3_t origin, vec3_t angles, vec3_t color, vec_t radius, vec_t corona, int style, int shadowenable, const char *cubemapname)
+{
+ int i, j, k, l, maxverts = 256, tris;
+ float *vertex3f = NULL, mins[3], maxs[3];
+ worldlight_t *e;
+ shadowmesh_t *mesh, *castmesh = NULL;
+
+ if (radius < 15 || DotProduct(color, color) < 0.03)
+ {
+ Con_Printf("R_Shadow_NewWorldLight: refusing to create a light too small/dim\n");
+ return;
+ }
+
+ e = Mem_Alloc(r_shadow_mempool, sizeof(worldlight_t));
+ VectorCopy(origin, e->origin);
+ VectorCopy(angles, e->angles);
+ VectorCopy(color, e->color);
+ e->radius = radius;
+ e->style = style;
+ if (e->style < 0 || e->style >= MAX_LIGHTSTYLES)
+ {
+ Con_Printf("R_Shadow_NewWorldLight: invalid light style number %i, must be >= 0 and < %i\n", e->style, MAX_LIGHTSTYLES);
+ e->style = 0;
+ }
+ e->drawshadows = shadowenable;
+ e->corona = corona;
+
+ Matrix4x4_CreateFromQuakeEntity(&e->matrix_lighttoworld, e->origin[0], e->origin[1], e->origin[2], e->angles[0], e->angles[1], e->angles[2], e->radius);
+ Matrix4x4_Invert_Simple(&e->matrix_worldtolight, &e->matrix_lighttoworld);
+ Matrix4x4_Concat(&e->matrix_worldtoattenuationxyz, &matrix_attenuationxyz, &e->matrix_worldtolight);
+ Matrix4x4_Concat(&e->matrix_worldtoattenuationz, &matrix_attenuationz, &e->matrix_worldtolight);
+
+ e->cullradius = e->radius;
+ for (k = 0;k < 3;k++)
+ {
+ mins[k] = e->origin[k] - e->radius;
+ maxs[k] = e->origin[k] + e->radius;
+ }
+
+ e->next = r_shadow_worldlightchain;
+ r_shadow_worldlightchain = e;
+ if (cubemapname && cubemapname[0])
+ {
+ e->cubemapname = Mem_Alloc(r_shadow_mempool, strlen(cubemapname) + 1);
+ strcpy(e->cubemapname, cubemapname);
+ e->cubemap = R_Shadow_Cubemap(e->cubemapname);
+ }
+ // FIXME: rewrite this to store ALL geometry into a cache in the light
+ if (e->drawshadows)
+ castmesh = Mod_ShadowMesh_Begin(r_shadow_mempool, 32768, 32768, NULL, NULL, NULL, false, false, true);
+ e->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, origin, 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, e->numclusters = 0;i < cl.worldmodel->brushq3.num_pvsclusters;i++)
+ if (CHECKPVSBIT(lightpvs, i))
+ e->numclusters++;
+ e->clusterindices = Mem_Alloc(r_shadow_mempool, e->numclusters * sizeof(int));
+ for (i = 0, e->numclusters = 0;i < cl.worldmodel->brushq3.num_pvsclusters;i++)
+ if (CHECKPVSBIT(lightpvs, i))
+ e->clusterindices[e->numclusters++] = i;
+
+ VectorCopy(e->origin, e->mins);
+ VectorCopy(e->origin, e->maxs);
+ 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 (e->mins[k] > leaf->mins[k]) e->mins[k] = leaf->mins[k];
+ if (e->maxs[k] < leaf->maxs[k]) e->maxs[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 (e->drawshadows)
+ 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, e->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(e->origin, e->mins);
+ VectorCopy(e->origin, e->maxs);
+ i = CL_PointQ1Contents(e->origin);
+
+ 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, e->origin, byteleafpvs, bytesurfacepvs, NULL, 0, true, mins, maxs, e->mins, e->maxs);
+
+ // 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 (e->mins[k] > leaf->mins[k]) e->mins[k] = leaf->mins[k];
+ if (e->maxs[k] < leaf->maxs[k]) e->maxs[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, e->numclusters = 0;i < cl.worldmodel->brushq1.num_pvsclusters;i++)
+ if (CHECKPVSBIT(lightpvs, i))
+ e->numclusters++;
+ e->clusterindices = Mem_Alloc(r_shadow_mempool, e->numclusters * sizeof(int));
+ for (i = 0, e->numclusters = 0;i < cl.worldmodel->brushq1.num_pvsclusters;i++)
+ if (CHECKPVSBIT(lightpvs, i))
+ e->clusterindices[e->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 (e->mins[k] > leaf->mins[k]) e->mins[k] = leaf->mins[k];
+ if (e->maxs[k] < leaf->maxs[k]) e->maxs[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, e->numclusters = 0;i < cl.worldmodel->brushq1.num_pvsclusters;i++)
+ if (CHECKPVSBIT(lightpvs, i))
+ e->numclusters++;
+ e->clusterindices = Mem_Alloc(r_shadow_mempool, e->numclusters * sizeof(int));
+ for (i = 0, e->numclusters = 0;i < cl.worldmodel->brushq1.num_pvsclusters;i++)
+ if (CHECKPVSBIT(lightpvs, i))
+ e->clusterindices[e->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 (e->drawshadows && (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, e->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 (e->mins[k] < e->origin[k] - e->radius) e->mins[k] = e->origin[k] - e->radius;
+ if (e->maxs[k] > e->origin[k] + e->radius) e->maxs[k] = e->origin[k] + e->radius;
+ }
+ e->cullradius = RadiusFromBoundsAndOrigin(e->mins, e->maxs, e->origin);
+
+ // 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 (e->drawshadows)
+ e->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, e->origin, r_shadow_projectdistance.value)))
+ Mod_ShadowMesh_AddMesh(r_shadow_mempool, e->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);
+ }
+
+ e->meshchain_shadow = Mod_ShadowMesh_Finish(r_shadow_mempool, e->meshchain_shadow, false, false);
+ e->meshchain_light = Mod_ShadowMesh_Finish(r_shadow_mempool, e->meshchain_light, true, false);
+
+ k = 0;
+ if (e->meshchain_shadow)
+ for (mesh = e->meshchain_shadow;mesh;mesh = mesh->next)
+ k += mesh->numtriangles;
+ l = 0;
+ if (e->meshchain_light)
+ for (mesh = e->meshchain_light;mesh;mesh = mesh->next)
+ l += mesh->numtriangles;
+ Con_Printf("static light built: %f %f %f : %f %f %f box, %i shadow volume triangles, %i light triangles\n", e->mins[0], e->mins[1], e->mins[2], e->maxs[0], e->maxs[1], e->maxs[2], k, l);
+}
+
+void R_Shadow_FreeWorldLight(worldlight_t *light)
+{
+ worldlight_t **lightpointer;
+ for (lightpointer = &r_shadow_worldlightchain;*lightpointer && *lightpointer != light;lightpointer = &(*lightpointer)->next);
+ if (*lightpointer != light)
+ Sys_Error("R_Shadow_FreeWorldLight: light not linked into chain\n");
+ *lightpointer = light->next;
+ if (light->cubemapname)
+ Mem_Free(light->cubemapname);
+ if (light->meshchain_shadow)
+ Mod_ShadowMesh_Free(light->meshchain_shadow);
+ if (light->meshchain_light)
+ Mod_ShadowMesh_Free(light->meshchain_light);
+ Mem_Free(light);
+}
+
+void R_Shadow_ClearWorldLights(void)
+{
+ while (r_shadow_worldlightchain)
+ R_Shadow_FreeWorldLight(r_shadow_worldlightchain);
+ r_shadow_selectedlight = NULL;
+ R_Shadow_FreeCubemaps();
+}
+
+void R_Shadow_SelectLight(worldlight_t *light)
+{
+ if (r_shadow_selectedlight)
+ r_shadow_selectedlight->selected = false;
+ r_shadow_selectedlight = light;
+ if (r_shadow_selectedlight)
+ r_shadow_selectedlight->selected = true;
+}
+
+rtexture_t *lighttextures[5];
+
+void R_Shadow_DrawCursorCallback(const void *calldata1, int calldata2)
+{
+ float scale = r_editlights_cursorgrid.value * 0.5f;
+ R_DrawSprite(GL_SRC_ALPHA, GL_ONE, lighttextures[0], false, r_editlights_cursorlocation, r_viewright, r_viewup, scale, -scale, -scale, scale, 1, 1, 1, 0.5f);
+}
+
+void R_Shadow_DrawLightSpriteCallback(const void *calldata1, int calldata2)
+{
+ float intensity;
+ const worldlight_t *light;
+ light = calldata1;
+ intensity = 0.5;
+ if (light->selected)
+ intensity = 0.75 + 0.25 * sin(realtime * M_PI * 4.0);
+ if (!light->meshchain_shadow)
+ intensity *= 0.5f;
+ R_DrawSprite(GL_SRC_ALPHA, GL_ONE, lighttextures[calldata2], false, light->origin, r_viewright, r_viewup, 8, -8, -8, 8, intensity, intensity, intensity, 0.5);
+}
+
+void R_Shadow_DrawLightSprites(void)
+{
+ int i;
+ cachepic_t *pic;
+ worldlight_t *light;
+
+ for (i = 0;i < 5;i++)
+ {
+ lighttextures[i] = NULL;
+ if ((pic = Draw_CachePic(va("gfx/crosshair%i.tga", i + 1))))
+ lighttextures[i] = pic->tex;
+ }
+
+ for (light = r_shadow_worldlightchain;light;light = light->next)
+ R_MeshQueue_AddTransparent(light->origin, R_Shadow_DrawLightSpriteCallback, light, ((int) light) % 5);
+ R_MeshQueue_AddTransparent(r_editlights_cursorlocation, R_Shadow_DrawCursorCallback, NULL, 0);
+}
+
+void R_Shadow_SelectLightInView(void)
+{
+ float bestrating, rating, temp[3];
+ worldlight_t *best, *light;
+ best = NULL;
+ bestrating = 0;
+ for (light = r_shadow_worldlightchain;light;light = light->next)
+ {
+ VectorSubtract(light->origin, r_vieworigin, temp);
+ rating = (DotProduct(temp, r_viewforward) / sqrt(DotProduct(temp, temp)));
+ if (rating >= 0.95)
+ {
+ rating /= (1 + 0.0625f * sqrt(DotProduct(temp, temp)));
+ if (bestrating < rating && CL_TraceLine(light->origin, r_vieworigin, NULL, NULL, true, NULL, SUPERCONTENTS_SOLID) == 1.0f)
+ {
+ bestrating = rating;
+ best = light;
+ }
+ }
+ }
+ R_Shadow_SelectLight(best);
+}
+
+void R_Shadow_LoadWorldLights(void)
+{
+ int n, a, style, shadow;
+ char name[MAX_QPATH], cubemapname[MAX_QPATH], *lightsstring, *s, *t;
+ float origin[3], radius, color[3], angles[3], corona;
+ if (cl.worldmodel == NULL)
+ {
+ Con_Printf("No map loaded.\n");
+ return;
+ }
+ FS_StripExtension (cl.worldmodel->name, name, sizeof (name));
+ strlcat (name, ".rtlights", sizeof (name));
+ lightsstring = FS_LoadFile(name, false);
+ if (lightsstring)
+ {
+ s = lightsstring;
+ n = 0;
+ while (*s)
+ {
+ t = s;
+ while (*s && *s != '\n')
+ s++;
+ if (!*s)
+ break;
+ *s = 0;
+ shadow = true;
+ // check for modifier flags
+ if (*t == '!')
+ {
+ shadow = false;
+ t++;
+ }
+ a = sscanf(t, "%f %f %f %f %f %f %f %d %s %f %f %f %f", &origin[0], &origin[1], &origin[2], &radius, &color[0], &color[1], &color[2], &style, cubemapname, &corona, &angles[0], &angles[1], &angles[2]);
+ if (a < 13)
+ {
+ corona = 0;
+ VectorClear(angles);
+ }
+ if (a < 9)
+ cubemapname[0] = 0;
+ *s = '\n';
+ if (a < 8)
+ {
+ Con_Printf("found %d parameters on line %i, should be 8 or 9 parameters (origin[0] origin[1] origin[2] radius color[0] color[1] color[2] style cubemapname)\n", a, n + 1);
+ break;
+ }
+ VectorScale(color, r_editlights_rtlightscolorscale.value, color);
+ radius *= r_editlights_rtlightssizescale.value;
+ R_Shadow_NewWorldLight(origin, angles, color, radius, corona, style, shadow, cubemapname);
+ s++;
+ n++;
+ }
+ if (*s)
+ Con_Printf("invalid rtlights file \"%s\"\n", name);
+ Mem_Free(lightsstring);
+ }
+}
+
+void R_Shadow_SaveWorldLights(void)
+{
+ worldlight_t *light;
+ int bufchars, bufmaxchars;
+ char *buf, *oldbuf;
+ char name[MAX_QPATH];
+ char line[1024];
+ if (!r_shadow_worldlightchain)
+ return;
+ if (cl.worldmodel == NULL)
+ {
+ Con_Printf("No map loaded.\n");
+ return;
+ }
+ FS_StripExtension (cl.worldmodel->name, name, sizeof (name));
+ strlcat (name, ".rtlights", sizeof (name));
+ bufchars = bufmaxchars = 0;
+ buf = NULL;
+ for (light = r_shadow_worldlightchain;light;light = light->next)
+ {
+ sprintf(line, "%s%f %f %f %f %f %f %f %d %s\n", light->drawshadows ? "" : "!", light->origin[0], light->origin[1], light->origin[2], light->radius / r_editlights_rtlightssizescale.value, light->color[0] / r_editlights_rtlightscolorscale.value, light->color[1] / r_editlights_rtlightscolorscale.value, light->color[2] / r_editlights_rtlightscolorscale.value, light->style, light->cubemapname ? light->cubemapname : "");
+ if (bufchars + (int) strlen(line) > bufmaxchars)
+ {
+ bufmaxchars = bufchars + strlen(line) + 2048;
+ oldbuf = buf;
+ buf = Mem_Alloc(r_shadow_mempool, bufmaxchars);
+ if (oldbuf)
+ {
+ if (bufchars)
+ memcpy(buf, oldbuf, bufchars);
+ Mem_Free(oldbuf);
+ }
+ }
+ if (strlen(line))
+ {
+ memcpy(buf + bufchars, line, strlen(line));
+ bufchars += strlen(line);
+ }
+ }
+ if (bufchars)
+ FS_WriteFile(name, buf, bufchars);
+ if (buf)
+ Mem_Free(buf);
+}
+
+void R_Shadow_LoadLightsFile(void)
+{
+ int n, a, style;
+ char name[MAX_QPATH], *lightsstring, *s, *t;
+ float origin[3], radius, color[3], subtract, spotdir[3], spotcone, falloff, distbias;
+ if (cl.worldmodel == NULL)
+ {
+ Con_Printf("No map loaded.\n");
+ return;
+ }
+ FS_StripExtension (cl.worldmodel->name, name, sizeof (name));
+ strlcat (name, ".lights", sizeof (name));
+ lightsstring = FS_LoadFile(name, false);
+ if (lightsstring)
+ {
+ s = lightsstring;
+ n = 0;
+ while (*s)
+ {
+ t = s;
+ while (*s && *s != '\n')
+ s++;
+ if (!*s)
+ break;
+ *s = 0;
+ a = sscanf(t, "%f %f %f %f %f %f %f %f %f %f %f %f %f %d", &origin[0], &origin[1], &origin[2], &falloff, &color[0], &color[1], &color[2], &subtract, &spotdir[0], &spotdir[1], &spotdir[2], &spotcone, &distbias, &style);
+ *s = '\n';
+ if (a < 14)
+ {
+ Con_Printf("invalid lights file, found %d parameters on line %i, should be 14 parameters (origin[0] origin[1] origin[2] falloff light[0] light[1] light[2] subtract spotdir[0] spotdir[1] spotdir[2] spotcone distancebias style)\n", a, n + 1);
+ break;
+ }
+ radius = sqrt(DotProduct(color, color) / (falloff * falloff * 8192.0f * 8192.0f));
+ radius = bound(15, radius, 4096);
+ VectorScale(color, (2.0f / (8388608.0f)), color);
+ R_Shadow_NewWorldLight(origin, vec3_origin, color, radius, 0, style, true, NULL);
+ s++;
+ n++;
+ }
+ if (*s)
+ Con_Printf("invalid lights file \"%s\"\n", name);
+ Mem_Free(lightsstring);
+ }
+}
+
+void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void)
+{
+ int entnum, style, islight;
+ char key[256], value[1024];
+ float origin[3], radius, color[3], light, fadescale, lightscale, originhack[3], overridecolor[3];
+ const char *data;
+
+ if (cl.worldmodel == NULL)
+ {
+ Con_Printf("No map loaded.\n");
+ return;
+ }
+ data = cl.worldmodel->brush.entities;
+ if (!data)
+ return;
+ for (entnum = 0;COM_ParseToken(&data, false) && com_token[0] == '{';entnum++)
+ {
+ light = 0;
+ origin[0] = origin[1] = origin[2] = 0;
+ originhack[0] = originhack[1] = originhack[2] = 0;
+ color[0] = color[1] = color[2] = 1;
+ overridecolor[0] = overridecolor[1] = overridecolor[2] = 1;
+ fadescale = 1;
+ lightscale = 1;
+ style = 0;
+ islight = false;
+ while (1)
+ {
+ if (!COM_ParseToken(&data, false))
+ break; // error
+ if (com_token[0] == '}')
+ break; // end of entity
+ if (com_token[0] == '_')
+ strcpy(key, com_token + 1);
+ else
+ strcpy(key, com_token);
+ while (key[strlen(key)-1] == ' ') // remove trailing spaces
+ key[strlen(key)-1] = 0;
+ if (!COM_ParseToken(&data, false))
+ break; // error
+ strcpy(value, com_token);
+
+ // now that we have the key pair worked out...
+ if (!strcmp("light", key))
+ light = atof(value);
+ else if (!strcmp("origin", key))
+ sscanf(value, "%f %f %f", &origin[0], &origin[1], &origin[2]);
+ else if (!strcmp("color", key))
+ sscanf(value, "%f %f %f", &color[0], &color[1], &color[2]);
+ else if (!strcmp("wait", key))
+ fadescale = atof(value);
+ else if (!strcmp("classname", key))
+ {
+ if (!strncmp(value, "light", 5))
+ {
+ islight = true;
+ if (!strcmp(value, "light_fluoro"))
+ {
+ originhack[0] = 0;
+ originhack[1] = 0;
+ originhack[2] = 0;
+ overridecolor[0] = 1;
+ overridecolor[1] = 1;
+ overridecolor[2] = 1;
+ }
+ if (!strcmp(value, "light_fluorospark"))
+ {
+ originhack[0] = 0;
+ originhack[1] = 0;
+ originhack[2] = 0;
+ overridecolor[0] = 1;
+ overridecolor[1] = 1;
+ overridecolor[2] = 1;
+ }
+ if (!strcmp(value, "light_globe"))
+ {
+ originhack[0] = 0;
+ originhack[1] = 0;
+ originhack[2] = 0;
+ overridecolor[0] = 1;
+ overridecolor[1] = 0.8;
+ overridecolor[2] = 0.4;
+ }
+ if (!strcmp(value, "light_flame_large_yellow"))
+ {
+ originhack[0] = 0;
+ originhack[1] = 0;
+ originhack[2] = 48;
+ overridecolor[0] = 1;
+ overridecolor[1] = 0.5;
+ overridecolor[2] = 0.1;
+ }
+ if (!strcmp(value, "light_flame_small_yellow"))
+ {
+ originhack[0] = 0;
+ originhack[1] = 0;
+ originhack[2] = 40;
+ overridecolor[0] = 1;
+ overridecolor[1] = 0.5;
+ overridecolor[2] = 0.1;
+ }
+ if (!strcmp(value, "light_torch_small_white"))
+ {
+ originhack[0] = 0;
+ originhack[1] = 0;
+ originhack[2] = 40;
+ overridecolor[0] = 1;
+ overridecolor[1] = 0.5;
+ overridecolor[2] = 0.1;
+ }
+ if (!strcmp(value, "light_torch_small_walltorch"))
+ {
+ originhack[0] = 0;
+ originhack[1] = 0;
+ originhack[2] = 40;
+ overridecolor[0] = 1;
+ overridecolor[1] = 0.5;
+ overridecolor[2] = 0.1;
+ }
+ }
+ }
+ else if (!strcmp("style", key))
+ style = atoi(value);
+ else if (cl.worldmodel->type == mod_brushq3)
+ {
+ if (!strcmp("scale", key))
+ lightscale = atof(value);
+ if (!strcmp("fade", key))
+ fadescale = atof(value);
+ }
+ }
+ if (light <= 0 && islight)
+ light = 300;
+ if (lightscale <= 0)
+ lightscale = 1;
+ if (fadescale <= 0)
+ fadescale = 1;
+ radius = min(light * r_editlights_quakelightsizescale.value * lightscale / fadescale, 1048576);
+ light = sqrt(bound(0, light, 1048576)) * (1.0f / 16.0f);
+ if (color[0] == 1 && color[1] == 1 && color[2] == 1)
+ VectorCopy(overridecolor, color);
+ VectorScale(color, light, color);
+ VectorAdd(origin, originhack, origin);
+ if (radius >= 15)
+ R_Shadow_NewWorldLight(origin, vec3_origin, color, radius, 0, style, true, NULL);
+ }
+}
+
+
+void R_Shadow_SetCursorLocationForView(void)
+{
+ vec_t dist, push, frac;
+ vec3_t dest, endpos, normal;
+ VectorMA(r_vieworigin, r_editlights_cursordistance.value, r_viewforward, dest);
+ frac = CL_TraceLine(r_vieworigin, dest, endpos, normal, true, NULL, SUPERCONTENTS_SOLID);
+ if (frac < 1)
+ {
+ dist = frac * r_editlights_cursordistance.value;
+ push = r_editlights_cursorpushback.value;
+ if (push > dist)
+ push = dist;
+ push = -push;
+ VectorMA(endpos, push, r_viewforward, endpos);
+ VectorMA(endpos, r_editlights_cursorpushoff.value, normal, endpos);
+ }
+ r_editlights_cursorlocation[0] = floor(endpos[0] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
+ r_editlights_cursorlocation[1] = floor(endpos[1] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
+ r_editlights_cursorlocation[2] = floor(endpos[2] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
+}
+
+void R_Shadow_UpdateWorldLightSelection(void)
+{
+ if (r_editlights.integer)
+ {
+ R_Shadow_SetCursorLocationForView();
+ R_Shadow_SelectLightInView();
+ R_Shadow_DrawLightSprites();
+ }
+ else
+ R_Shadow_SelectLight(NULL);
+}
+
+void R_Shadow_EditLights_Clear_f(void)
+{
+ R_Shadow_ClearWorldLights();
+}
+
+void R_Shadow_EditLights_Reload_f(void)
+{
+ r_shadow_reloadlights = true;
+}
+
+void R_Shadow_EditLights_Save_f(void)
+{
+ if (cl.worldmodel)
+ R_Shadow_SaveWorldLights();
+}
+
+void R_Shadow_EditLights_ImportLightEntitiesFromMap_f(void)
+{
+ R_Shadow_ClearWorldLights();
+ R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite();
+}
+
+void R_Shadow_EditLights_ImportLightsFile_f(void)
+{
+ R_Shadow_ClearWorldLights();
+ R_Shadow_LoadLightsFile();
+}
+
+void R_Shadow_EditLights_Spawn_f(void)
+{
+ vec3_t color;
+ if (!r_editlights.integer)
+ {
+ Con_Printf("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
+ return;
+ }
+ if (Cmd_Argc() != 1)
+ {
+ Con_Printf("r_editlights_spawn does not take parameters\n");
+ return;
+ }
+ color[0] = color[1] = color[2] = 1;
+ R_Shadow_NewWorldLight(r_editlights_cursorlocation, vec3_origin, color, 200, 0, 0, true, NULL);
+}
+
+void R_Shadow_EditLights_Edit_f(void)
+{
+ vec3_t origin, angles, color;
+ vec_t radius, corona;
+ int style, shadows;
+ char cubemapname[1024];
+ if (!r_editlights.integer)
+ {
+ Con_Printf("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
+ return;
+ }
+ if (!r_shadow_selectedlight)
+ {
+ Con_Printf("No selected light.\n");
+ return;
+ }
+ VectorCopy(r_shadow_selectedlight->origin, origin);
+ VectorCopy(r_shadow_selectedlight->angles, angles);
+ VectorCopy(r_shadow_selectedlight->color, color);
+ radius = r_shadow_selectedlight->radius;
+ style = r_shadow_selectedlight->style;
+ if (r_shadow_selectedlight->cubemapname)
+ strcpy(cubemapname, r_shadow_selectedlight->cubemapname);
+ else
+ cubemapname[0] = 0;
+ shadows = r_shadow_selectedlight->drawshadows;
+ corona = r_shadow_selectedlight->corona;
+ if (!strcmp(Cmd_Argv(1), "origin"))
+ {
+ if (Cmd_Argc() != 5)
+ {
+ Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(1));
+ return;
+ }
+ origin[0] = atof(Cmd_Argv(2));
+ origin[1] = atof(Cmd_Argv(3));
+ origin[2] = atof(Cmd_Argv(4));
+ }
+ else if (!strcmp(Cmd_Argv(1), "originx"))
+ {
+ if (Cmd_Argc() != 3)
+ {
+ Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
+ return;
+ }
+ origin[0] = atof(Cmd_Argv(2));
+ }
+ else if (!strcmp(Cmd_Argv(1), "originy"))
+ {
+ if (Cmd_Argc() != 3)
+ {
+ Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
+ return;
+ }
+ origin[1] = atof(Cmd_Argv(2));
+ }
+ else if (!strcmp(Cmd_Argv(1), "originz"))
+ {
+ if (Cmd_Argc() != 3)
+ {
+ Con_Printf("usage: r_editlights_edit %s value\n", Cmd_Argv(1));
+ return;
+ }
+ origin[2] = atof(Cmd_Argv(2));
+ }
+ else if (!strcmp(Cmd_Argv(1), "move"))
+ {
+ if (Cmd_Argc() != 5)
+ {
+ Con_Printf("usage: r_editlights_edit %s x y z\n", Cmd_Argv(1));
+ return;