+
+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);
+ R_Mesh_State(&m);
+
+ GL_Color(cr * r_colorscale, cg * r_colorscale, cb * r_colorscale, ca);
+ varray_texcoord[0][ 0] = 0;varray_texcoord[0][ 1] = 0;
+ varray_texcoord[0][ 4] = 0;varray_texcoord[0][ 5] = 1;
+ varray_texcoord[0][ 8] = 1;varray_texcoord[0][ 9] = 1;
+ varray_texcoord[0][12] = 1;varray_texcoord[0][13] = 0;
+ varray_vertex[0] = origin[0] - vright[0] * scale - vup[0] * scale;
+ varray_vertex[1] = origin[1] - vright[1] * scale - vup[1] * scale;
+ varray_vertex[2] = origin[2] - vright[2] * scale - vup[2] * scale;
+ varray_vertex[4] = origin[0] - vright[0] * scale + vup[0] * scale;
+ varray_vertex[5] = origin[1] - vright[1] * scale + vup[1] * scale;
+ varray_vertex[6] = origin[2] - vright[2] * scale + vup[2] * scale;
+ varray_vertex[8] = origin[0] + vright[0] * scale + vup[0] * scale;
+ varray_vertex[9] = origin[1] + vright[1] * scale + vup[1] * scale;
+ varray_vertex[10] = origin[2] + vright[2] * scale + vup[2] * scale;
+ varray_vertex[12] = origin[0] + vright[0] * scale - vup[0] * scale;
+ varray_vertex[13] = origin[1] + vright[1] * scale - vup[1] * scale;
+ varray_vertex[14] = origin[2] + vright[2] * scale - vup[2] * scale;
+ R_Mesh_Draw(4, 2, polygonelements);
+}
+
+void R_Shadow_DrawCursorCallback(const void *calldata1, int calldata2)
+{
+ cachepic_t *pic;
+ pic = Draw_CachePic("gfx/crosshair1.tga");
+ if (pic)
+ R_DrawLightSprite(R_GetTexture(pic->tex), r_editlights_cursorlocation, r_editlights_cursorgrid.value * 0.5f, 1, 1, 1, 0.5);
+}
+
+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->shadowvolume)
+ R_DrawLightSprite(calldata2, light->origin, 8, intensity, intensity, intensity, 0.5);
+ else
+ R_DrawLightSprite(calldata2, light->origin, 8, intensity * 0.5, intensity * 0.5, intensity * 0.5, 0.5);
+}
+
+void R_Shadow_DrawLightSprites(void)
+{
+ int i, texnums[5];
+ cachepic_t *pic;
+ worldlight_t *light;
+
+ for (i = 0;i < 5;i++)
+ {
+ pic = Draw_CachePic(va("gfx/crosshair%i.tga", i + 1));
+ if (pic)
+ texnums[i] = R_GetTexture(pic->tex);
+ else
+ texnums[i] = 0;
+ }
+
+ for (light = r_shadow_worldlightchain;light;light = light->next)
+ R_MeshQueue_AddTransparent(light->origin, R_Shadow_DrawLightSpriteCallback, light, texnums[((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_refdef.vieworg, temp);
+ rating = (DotProduct(temp, vpn) / sqrt(DotProduct(temp, temp)));
+ if (rating >= 0.95)
+ {
+ rating /= (1 + 0.0625f * sqrt(DotProduct(temp, temp)));
+ if (bestrating < rating && CL_TraceLine(light->origin, r_refdef.vieworg, NULL, NULL, 0, true, NULL) == 1.0f)
+ {
+ bestrating = rating;
+ best = light;
+ }
+ }
+ }
+ R_Shadow_SelectLight(best);
+}
+
+void R_Shadow_LoadWorldLights(void)
+{
+ int n, a, style;
+ char name[MAX_QPATH], cubemapname[MAX_QPATH], *lightsstring, *s, *t;
+ float origin[3], radius, color[3];
+ COM_StripExtension(cl.worldmodel->name, name);
+ strcat(name, ".rtlights");
+ lightsstring = COM_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 %d %s", &origin[0], &origin[1], &origin[2], &radius, &color[0], &color[1], &color[2], &style, &cubemapname);
+ 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;
+ }
+ R_Shadow_NewWorldLight(origin, radius, color, style, 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;
+ COM_StripExtension(cl.worldmodel->name, name);
+ strcat(name, ".rtlights");
+ bufchars = bufmaxchars = 0;
+ buf = NULL;
+ for (light = r_shadow_worldlightchain;light;light = light->next)
+ {
+ sprintf(line, "%g %g %g %g %g %g %g %d %s\n", light->origin[0], light->origin[1], light->origin[2], light->lightradius, light->light[0], light->light[1], light->light[2], light->style, light->cubemapname ? light->cubemapname : "");
+ if (bufchars + 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)
+ COM_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;
+ COM_StripExtension(cl.worldmodel->name, name);
+ strcat(name, ".lights");
+ lightsstring = COM_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, (1.0f / (8388608.0f)), color);
+ R_Shadow_NewWorldLight(origin, radius, color, style, 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, scale, originhack[3], overridecolor[3];
+ const char *data;
+
+ data = cl.worldmodel->entities;
+ if (!data)
+ return;
+ for (entnum = 0;COM_ParseToken(&data) && 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;
+ scale = 1;
+ style = 0;
+ islight = false;
+ while (1)
+ {
+ if (!COM_ParseToken(&data))
+ 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))
+ 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))
+ scale = 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.7;
+ overridecolor[2] = 0.2;
+ }
+ if (!strcmp(value, "light_flame_small_yellow"))
+ {
+ originhack[0] = 0;
+ originhack[1] = 0;
+ originhack[2] = 40;
+ overridecolor[0] = 1;
+ overridecolor[1] = 0.7;
+ overridecolor[2] = 0.2;
+ }
+ if (!strcmp(value, "light_torch_small_white"))
+ {
+ originhack[0] = 0;
+ originhack[1] = 0;
+ originhack[2] = 40;
+ overridecolor[0] = 1;
+ overridecolor[1] = 0.9;
+ overridecolor[2] = 0.7;
+ }
+ if (!strcmp(value, "light_torch_small_walltorch"))
+ {
+ originhack[0] = 0;
+ originhack[1] = 0;
+ originhack[2] = 40;
+ overridecolor[0] = 1;
+ overridecolor[1] = 0.7;
+ overridecolor[2] = 0.2;
+ }
+ }
+ }
+ else if (!strcmp("style", key))
+ style = atoi(value);
+ }
+ if (light <= 0 && islight)
+ light = 300;
+ radius = bound(15, light * r_editlights_quakelightsizescale.value / scale, 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, radius, color, style, NULL);
+ }
+}
+
+
+void R_Shadow_SetCursorLocationForView(void)
+{
+ vec_t dist, push, frac;
+ vec3_t dest, endpos, normal;
+ VectorMA(r_refdef.vieworg, r_editlights_cursordistance.value, vpn, dest);
+ frac = CL_TraceLine(r_refdef.vieworg, dest, endpos, normal, 0, true, NULL);
+ if (frac < 1)
+ {
+ dist = frac * r_editlights_cursordistance.value;
+ push = r_editlights_cursorpushback.value;
+ if (push > dist)
+ push = dist;
+ push = -push;
+ VectorMA(endpos, push, vpn, 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_UpdateLightingMode(void)
+{
+ r_shadow_lightingmode = 0;
+ if (r_shadow_realtime.integer)
+ {
+ if (r_shadow_worldlightchain)
+ r_shadow_lightingmode = 2;
+ else
+ r_shadow_lightingmode = 1;
+ }
+}
+
+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 origin, color;
+ vec_t radius;
+ int style;
+ const char *cubemapname;
+ if (!r_editlights.integer)
+ {
+ Con_Printf("Cannot spawn light when not in editing mode. Set r_editlights to 1.\n");
+ return;
+ }
+ if (Cmd_Argc() <= 7)
+ {
+ radius = 200;
+ color[0] = color[1] = color[2] = 1;
+ style = 0;
+ cubemapname = NULL;
+ if (Cmd_Argc() >= 2)
+ {
+ radius = atof(Cmd_Argv(1));
+ if (Cmd_Argc() >= 3)
+ {
+ color[0] = atof(Cmd_Argv(2));
+ color[1] = color[0];
+ color[2] = color[0];
+ if (Cmd_Argc() >= 5)
+ {
+ color[1] = atof(Cmd_Argv(3));
+ color[2] = atof(Cmd_Argv(4));
+ if (Cmd_Argc() >= 6)
+ {
+ style = atoi(Cmd_Argv(5));
+ if (Cmd_Argc() >= 7)
+ cubemapname = Cmd_Argv(6);
+ }
+ }
+ }
+ }
+ if (cubemapname && !cubemapname[0])
+ cubemapname = NULL;
+ if (radius >= 16 && color[0] >= 0 && color[1] >= 0 && color[2] >= 0 && style >= 0 && style < 256 && (color[0] >= 0.1 || color[1] >= 0.1 || color[2] >= 0.1))
+ {
+ VectorCopy(r_editlights_cursorlocation, origin);
+ R_Shadow_NewWorldLight(origin, radius, color, style, cubemapname);
+ return;
+ }
+ }
+ Con_Printf("usage: r_editlights_spawn radius red green blue [style [cubemap]]\n");
+}
+
+void R_Shadow_EditLights_Edit_f(void)
+{
+ vec3_t origin, color;
+ vec_t radius;
+ int style;
+ const char *cubemapname;
+ 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;
+ }
+ if (Cmd_Argc() <= 7)
+ {
+ radius = 200;
+ color[0] = color[1] = color[2] = 1;
+ style = 0;
+ cubemapname = NULL;
+ if (Cmd_Argc() >= 2)
+ {
+ radius = atof(Cmd_Argv(1));
+ if (Cmd_Argc() >= 3)
+ {
+ color[0] = atof(Cmd_Argv(2));
+ color[1] = color[0];
+ color[2] = color[0];
+ if (Cmd_Argc() >= 5)
+ {
+ color[1] = atof(Cmd_Argv(3));
+ color[2] = atof(Cmd_Argv(4));
+ if (Cmd_Argc() >= 6)
+ {
+ style = atoi(Cmd_Argv(5));
+ if (Cmd_Argc() >= 7)
+ cubemapname = Cmd_Argv(6);
+ }
+ }
+ }
+ }
+ if (cubemapname && !cubemapname[0])
+ cubemapname = NULL;
+ if (radius >= 16 && color[0] >= 0 && color[1] >= 0 && color[2] >= 0 && style >= 0 && style < 256 && (color[0] >= 0.1 || color[1] >= 0.1 || color[2] >= 0.1))
+ {
+ VectorCopy(r_shadow_selectedlight->origin, origin);
+ R_Shadow_FreeWorldLight(r_shadow_selectedlight);
+ r_shadow_selectedlight = NULL;
+ R_Shadow_NewWorldLight(origin, radius, color, style, cubemapname);
+ return;
+ }
+ }
+ Con_Printf("usage: r_editlights_edit radius red green blue [style [cubemap]]\n");
+}
+
+void R_Shadow_EditLights_Remove_f(void)
+{
+ if (!r_editlights.integer)
+ {
+ Con_Printf("Cannot remove light when not in editing mode. Set r_editlights to 1.\n");
+ return;
+ }
+ if (!r_shadow_selectedlight)
+ {
+ Con_Printf("No selected light.\n");
+ return;
+ }
+ R_Shadow_FreeSelectedWorldLight();
+}
+
+void R_Shadow_EditLights_Init(void)
+{
+ Cvar_RegisterVariable(&r_editlights);
+ Cvar_RegisterVariable(&r_editlights_cursordistance);
+ Cvar_RegisterVariable(&r_editlights_cursorpushback);
+ Cvar_RegisterVariable(&r_editlights_cursorpushoff);
+ Cvar_RegisterVariable(&r_editlights_cursorgrid);
+ Cvar_RegisterVariable(&r_editlights_quakelightsizescale);
+ Cmd_AddCommand("r_editlights_clear", R_Shadow_EditLights_Clear_f);
+ Cmd_AddCommand("r_editlights_reload", R_Shadow_EditLights_Reload_f);
+ Cmd_AddCommand("r_editlights_save", R_Shadow_EditLights_Save_f);
+ Cmd_AddCommand("r_editlights_spawn", R_Shadow_EditLights_Spawn_f);
+ Cmd_AddCommand("r_editlights_edit", R_Shadow_EditLights_Edit_f);
+ Cmd_AddCommand("r_editlights_remove", R_Shadow_EditLights_Remove_f);
+ Cmd_AddCommand("r_editlights_importlightentitiesfrommap", R_Shadow_EditLights_ImportLightEntitiesFromMap_f);
+ Cmd_AddCommand("r_editlights_importlightsfile", R_Shadow_EditLights_ImportLightsFile_f);
+}