+ if (!bumptexture)
+ bumptexture = r_shadow_blankbumptexture;
+ if (!glosstexture)
+ glosstexture = r_shadow_blankglosstexture;
+ if (r_shadow_gloss.integer >= 2 || (r_shadow_gloss.integer >= 1 && glosstexture != r_shadow_blankglosstexture))
+ {
+ // 2 texture no3D combine path, five pass
+ memset(&m, 0, sizeof(m));
+
+ m.tex[0] = R_GetTexture(bumptexture);
+ m.texcubemap[1] = R_GetTexture(r_shadow_normalscubetexture);
+ m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
+ R_Mesh_TextureState(&m);
+ qglColorMask(0,0,0,1);
+ qglDisable(GL_BLEND);
+ GL_Color(1,1,1,1);
+ memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
+ R_Shadow_GenTexCoords_Specular_NormalCubeMap(varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin, relativeeyeorigin);
+ R_Mesh_Draw(numverts, numtriangles, elements);
+
+ m.tex[0] = 0;
+ m.texcubemap[1] = 0;
+ m.texcombinergb[1] = GL_MODULATE;
+ R_Mesh_TextureState(&m);
+ // square alpha in framebuffer a few times to make it shiny
+ qglBlendFunc(GL_ZERO, GL_DST_ALPHA);
+ qglEnable(GL_BLEND);
+ // these comments are a test run through this math for intensity 0.5
+ // 0.5 * 0.5 = 0.25
+ R_Mesh_Draw(numverts, numtriangles, elements);
+ // 0.25 * 0.25 = 0.0625
+ R_Mesh_Draw(numverts, numtriangles, elements);
+ // 0.0625 * 0.0625 = 0.00390625
+ R_Mesh_Draw(numverts, numtriangles, elements);
+
+ m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
+ m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
+ R_Mesh_TextureState(&m);
+ qglBlendFunc(GL_DST_ALPHA, GL_ZERO);
+ R_Shadow_GenTexCoords_Attenuation2D1D(varray_texcoord[0], varray_texcoord[1], numverts, varray_vertex, svectors, tvectors, normals, relativelightorigin, lightradius);
+ R_Mesh_Draw(numverts, numtriangles, elements);
+
+ m.tex[0] = R_GetTexture(glosstexture);
+ m.texcubemap[1] = R_GetTexture(lightcubemap);
+ R_Mesh_TextureState(&m);
+ qglColorMask(1,1,1,1);
+ qglBlendFunc(GL_DST_ALPHA, GL_ONE);
+ memcpy(varray_texcoord[0], texcoords, numverts * sizeof(float[4]));
+ if (lightcubemap)
+ R_Shadow_GenTexCoords_LightCubeMap(varray_texcoord[1], numverts, varray_vertex, relativelightorigin);
+
+ colorscale = r_colorscale * r_shadow_lightintensityscale.value;
+ for (mult = 1, scale = ixtable[mult];mult < 64 && (lightcolor[0] * scale * colorscale > 1 || lightcolor[1] * scale * colorscale > 1 || lightcolor[2] * scale * colorscale > 1);mult++, scale = ixtable[mult]);
+ colorscale *= scale;
+ GL_Color(lightcolor[0] * colorscale, lightcolor[1] * colorscale, lightcolor[2] * colorscale, 1);
+ for (renders = 0;renders < mult;renders++)
+ R_Mesh_Draw(numverts, numtriangles, elements);
+ }
+}
+
+void R_Shadow_DrawWorldLightShadowVolume(matrix4x4_t *matrix, worldlight_t *light)
+{
+ R_Mesh_Matrix(matrix);
+ R_Shadow_RenderShadowMeshVolume(light->shadowvolume);
+}
+
+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"};
+worldlight_t *r_shadow_worldlightchain;
+worldlight_t *r_shadow_selectedlight;
+vec3_t r_editlights_cursorlocation;
+
+static int castshadowcount = 1;
+void R_Shadow_NewWorldLight(vec3_t origin, float radius, vec3_t color, int style, const char *cubemapname)
+{
+ int i, j, k, l, maxverts, *mark, tris;
+ float *verts, *v, f, temp[3], radius2;
+ //float projectdistance, *v0, *v1, temp2[3], temp3[3];
+ worldlight_t *e;
+ shadowmesh_t *mesh, *castmesh;
+ mleaf_t *leaf;
+ msurface_t *surf;
+ qbyte *pvs;
+ surfmesh_t *surfmesh;
+
+ 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(color, e->light);
+ e->lightradius = radius;
+ VectorCopy(origin, e->mins);
+ VectorCopy(origin, e->maxs);
+ e->cullradius = 0;
+ e->style = style;
+ e->next = r_shadow_worldlightchain;
+ r_shadow_worldlightchain = e;
+ if (cubemapname)
+ {
+ e->cubemapname = Mem_Alloc(r_shadow_mempool, strlen(cubemapname) + 1);
+ strcpy(e->cubemapname, cubemapname);
+ // FIXME: add cubemap loading (and don't load a cubemap twice)
+ }
+ if (cl.worldmodel)
+ {
+ castshadowcount++;
+ leaf = Mod_PointInLeaf(origin, cl.worldmodel);
+ pvs = Mod_LeafPVS(leaf, cl.worldmodel);
+ for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
+ {
+ if (pvs[i >> 3] & (1 << (i & 7)))
+ {
+ VectorCopy(origin, temp);
+ if (temp[0] < leaf->mins[0]) temp[0] = leaf->mins[0];
+ if (temp[0] > leaf->maxs[0]) temp[0] = leaf->maxs[0];
+ if (temp[1] < leaf->mins[1]) temp[1] = leaf->mins[1];
+ if (temp[1] > leaf->maxs[1]) temp[1] = leaf->maxs[1];
+ if (temp[2] < leaf->mins[2]) temp[2] = leaf->mins[2];
+ if (temp[2] > leaf->maxs[2]) temp[2] = leaf->maxs[2];
+ VectorSubtract(temp, origin, temp);
+ if (DotProduct(temp, temp) < e->lightradius * e->lightradius)
+ {
+ leaf->worldnodeframe = castshadowcount;
+ for (j = 0, mark = leaf->firstmarksurface;j < leaf->nummarksurfaces;j++, mark++)
+ {
+ surf = cl.worldmodel->surfaces + *mark;
+ if (surf->castshadow != castshadowcount)
+ {
+ f = DotProduct(e->origin, surf->plane->normal) - surf->plane->dist;
+ if (surf->flags & SURF_PLANEBACK)
+ f = -f;
+ if (f > 0 && f < e->lightradius)
+ {
+ temp[0] = bound(surf->poly_mins[0], e->origin[0], surf->poly_maxs[0]) - e->origin[0];
+ temp[1] = bound(surf->poly_mins[1], e->origin[1], surf->poly_maxs[1]) - e->origin[1];
+ temp[2] = bound(surf->poly_mins[2], e->origin[2], surf->poly_maxs[2]) - e->origin[2];
+ if (DotProduct(temp, temp) < e->lightradius * e->lightradius)
+ surf->castshadow = castshadowcount;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ e->numleafs = 0;
+ for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
+ if (leaf->worldnodeframe == castshadowcount)
+ e->numleafs++;
+ e->numsurfaces = 0;
+ for (i = 0, surf = cl.worldmodel->surfaces + cl.worldmodel->firstmodelsurface;i < cl.worldmodel->nummodelsurfaces;i++, surf++)
+ if (surf->castshadow == castshadowcount)
+ e->numsurfaces++;
+
+ if (e->numleafs)
+ e->leafs = Mem_Alloc(r_shadow_mempool, e->numleafs * sizeof(mleaf_t *));
+ if (e->numsurfaces)
+ e->surfaces = Mem_Alloc(r_shadow_mempool, e->numsurfaces * sizeof(msurface_t *));
+ e->numleafs = 0;
+ for (i = 0, leaf = cl.worldmodel->leafs + 1;i < cl.worldmodel->numleafs;i++, leaf++)
+ if (leaf->worldnodeframe == castshadowcount)
+ e->leafs[e->numleafs++] = leaf;
+ e->numsurfaces = 0;
+ for (i = 0, surf = cl.worldmodel->surfaces + cl.worldmodel->firstmodelsurface;i < cl.worldmodel->nummodelsurfaces;i++, surf++)
+ if (surf->castshadow == castshadowcount)
+ e->surfaces[e->numsurfaces++] = surf;
+ // find bounding box and sphere of lit surfaces
+ // (these will be used for creating a shape to clip the light)
+ radius2 = 0;
+ VectorCopy(e->origin, e->mins);
+ VectorCopy(e->origin, e->maxs);
+ for (j = 0;j < e->numsurfaces;j++)
+ {
+ surf = e->surfaces[j];
+ for (k = 0, v = surf->poly_verts;k < surf->poly_numverts;k++, v += 3)
+ {
+ if (e->mins[0] > v[0]) e->mins[0] = v[0];if (e->maxs[0] < v[0]) e->maxs[0] = v[0];
+ if (e->mins[1] > v[1]) e->mins[1] = v[1];if (e->maxs[1] < v[1]) e->maxs[1] = v[1];
+ if (e->mins[2] > v[2]) e->mins[2] = v[2];if (e->maxs[2] < v[2]) e->maxs[2] = v[2];
+ VectorSubtract(v, e->origin, temp);
+ f = DotProduct(temp, temp);
+ if (radius2 < f)
+ radius2 = f;
+ }
+ }
+ e->cullradius = sqrt(radius2);
+ if (e->cullradius > e->lightradius)
+ e->cullradius = e->lightradius;
+ if (e->mins[0] < e->origin[0] - e->lightradius) e->mins[0] = e->origin[0] - e->lightradius;
+ if (e->maxs[0] > e->origin[0] + e->lightradius) e->maxs[0] = e->origin[0] + e->lightradius;
+ if (e->mins[1] < e->origin[1] - e->lightradius) e->mins[1] = e->origin[1] - e->lightradius;
+ if (e->maxs[1] > e->origin[1] + e->lightradius) e->maxs[1] = e->origin[1] + e->lightradius;
+ if (e->mins[2] < e->origin[2] - e->lightradius) e->mins[2] = e->origin[2] - e->lightradius;
+ if (e->maxs[2] > e->origin[2] + e->lightradius) e->maxs[2] = e->origin[2] + e->lightradius;
+ Con_Printf("%f %f %f, %f %f %f, %f, %f, %d, %d\n", e->mins[0], e->mins[1], e->mins[2], e->maxs[0], e->maxs[1], e->maxs[2], e->cullradius, e->lightradius, e->numleafs, e->numsurfaces);
+ // clip shadow volumes against eachother to remove unnecessary
+ // polygons (and sections of polygons)
+ maxverts = 256;
+ verts = NULL;
+ castshadowcount++;
+ for (j = 0;j < e->numsurfaces;j++)
+ {
+ surf = e->surfaces[j];
+ if (surf->flags & SURF_SHADOWCAST)
+ {
+ surf->castshadow = castshadowcount;
+ if (maxverts < surf->poly_numverts)
+ maxverts = surf->poly_numverts;
+ }
+ }
+ e->shadowvolume = Mod_ShadowMesh_Begin(r_shadow_mempool, 32768);
+ // make a mesh to cast a shadow volume from
+ castmesh = Mod_ShadowMesh_Begin(r_shadow_mempool, 32768);
+ for (j = 0;j < e->numsurfaces;j++)
+ if (e->surfaces[j]->castshadow == castshadowcount)
+ for (surfmesh = e->surfaces[j]->mesh;surfmesh;surfmesh = surfmesh->chain)
+ Mod_ShadowMesh_AddMesh(r_shadow_mempool, castmesh, surfmesh->numverts, surfmesh->verts, surfmesh->numtriangles, surfmesh->index);
+ castmesh = Mod_ShadowMesh_Finish(r_shadow_mempool, castmesh);
+
+ // cast shadow volume from castmesh
+ for (mesh = castmesh;mesh;mesh = mesh->next)
+ {
+ R_Shadow_ResizeTriangleFacingLight(castmesh->numtriangles);
+ R_Shadow_ResizeShadowElements(castmesh->numtriangles);
+
+ if (maxverts < castmesh->numverts * 2)
+ {
+ maxverts = castmesh->numverts * 2;
+ if (verts)
+ Mem_Free(verts);
+ verts = NULL;
+ }
+ if (verts == NULL && maxverts > 0)
+ verts = Mem_Alloc(r_shadow_mempool, maxverts * sizeof(float[4]));
+
+ // now that we have the buffers big enough, construct shadow volume mesh
+ memcpy(verts, castmesh->verts, castmesh->numverts * sizeof(float[4]));
+ R_Shadow_ProjectVertices(verts, castmesh->numverts, e->origin, 1000000.0f);//, e->lightradius);
+ R_Shadow_MakeTriangleShadowFlags(castmesh->elements, verts, castmesh->numtriangles, trianglefacinglight, e->origin, e->lightradius);
+ tris = R_Shadow_BuildShadowVolumeTriangles(castmesh->elements, castmesh->neighbors, castmesh->numtriangles, castmesh->numverts, trianglefacinglight, shadowelements);
+ // add the constructed shadow volume mesh
+ Mod_ShadowMesh_AddMesh(r_shadow_mempool, e->shadowvolume, castmesh->numverts, verts, tris, shadowelements);
+ }
+ // we're done with castmesh now
+ Mod_ShadowMesh_Free(castmesh);
+ e->shadowvolume = Mod_ShadowMesh_Finish(r_shadow_mempool, e->shadowvolume);
+ for (l = 0, mesh = e->shadowvolume;mesh;mesh = mesh->next)
+ l += mesh->numtriangles;
+ Con_Printf("static shadow volume built containing %i triangles\n", 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->shadowvolume)
+ Mod_ShadowMesh_Free(light->shadowvolume);
+ if (light->surfaces)
+ Mem_Free(light->surfaces);
+ if (light->leafs)
+ Mem_Free(light->leafs);
+ Mem_Free(light);
+}
+
+void R_Shadow_ClearWorldLights(void)
+{
+ while (r_shadow_worldlightchain)
+ R_Shadow_FreeWorldLight(r_shadow_worldlightchain);
+ r_shadow_selectedlight = NULL;
+}
+
+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;
+}
+
+void R_Shadow_FreeSelectedWorldLight(void)
+{
+ if (r_shadow_selectedlight)
+ {
+ R_Shadow_FreeWorldLight(r_shadow_selectedlight);
+ r_shadow_selectedlight = NULL;
+ }
+}
+
+void R_DrawLightSprite(int texnum, const vec3_t origin, vec_t scale, float cr, float cg, float cb, float ca)
+{
+ rmeshstate_t m;
+ float diff[3];
+
+ if (fogenabled)
+ {
+ VectorSubtract(origin, r_origin, diff);
+ ca *= 1 - exp(fogdensity/DotProduct(diff,diff));
+ }
+
+ memset(&m, 0, sizeof(m));
+ m.blendfunc1 = GL_SRC_ALPHA;
+ m.blendfunc2 = GL_ONE;
+ m.tex[0] = texnum;
+ R_Mesh_Matrix(&r_identitymatrix);