rtexture_t *r_shadow_blankglosstexture;
rtexture_t *r_shadow_blankwhitetexture;
+cvar_t r_shadow_lightattenuationpower = {0, "r_shadow_lightattenuationpower", "2"};
cvar_t r_shadow_lightattenuationscale = {0, "r_shadow_lightattenuationscale", "2"};
cvar_t r_shadow_lightintensityscale = {0, "r_shadow_lightintensityscale", "1"};
cvar_t r_shadow_realtime = {0, "r_shadow_realtime", "0"};
cvar_t r_shadow_gloss = {0, "r_shadow_gloss", "1"};
cvar_t r_shadow_debuglight = {0, "r_shadow_debuglight", "-1"};
cvar_t r_shadow_scissor = {0, "r_shadow_scissor", "1"};
-cvar_t r_shadow_bumpscale = {0, "r_shadow_bumpscale", "4"};
+cvar_t r_shadow_bumpscale_bumpmap = {0, "r_shadow_bumpscale_bumpmap", "4"};
+cvar_t r_shadow_bumpscale_basetexture = {0, "r_shadow_bumpscale_basetexture", "4"};
cvar_t r_shadow_shadownudge = {0, "r_shadow_shadownudge", "1"};
void R_Shadow_ClearWorldLights(void);
void R_Shadow_Init(void)
{
+ Cvar_RegisterVariable(&r_shadow_lightattenuationpower);
Cvar_RegisterVariable(&r_shadow_lightattenuationscale);
Cvar_RegisterVariable(&r_shadow_lightintensityscale);
Cvar_RegisterVariable(&r_shadow_realtime);
Cvar_RegisterVariable(&r_shadow_gloss);
Cvar_RegisterVariable(&r_shadow_debuglight);
Cvar_RegisterVariable(&r_shadow_scissor);
- Cvar_RegisterVariable(&r_shadow_bumpscale);
+ Cvar_RegisterVariable(&r_shadow_bumpscale_bumpmap);
+ Cvar_RegisterVariable(&r_shadow_bumpscale_basetexture);
Cvar_RegisterVariable(&r_shadow_shadownudge);
R_Shadow_EditLights_Init();
R_RegisterModule("R_Shadow", r_shadow_start, r_shadow_shutdown, r_shadow_newmap);
// of the comparison use it, therefore they are both multiplied the
// same amount... furthermore the subtract can be done on the
// vectors, saving a little bit of math in the dotproducts
-#if 0
+#if 1
// fast version
// subtracts v1 from v0 and v2, combined into a crossproduct,
// combined with a dotproduct of the light location relative to the
#else
// readable version
{
- float dir0[3], dir1[3], temp[3], f;
+ float dir0[3], dir1[3], temp[3];
// calculate two mostly perpendicular edge directions
VectorSubtract(v0, v1, dir0);
// I.E. flat, so all points give the same answer)
// the normal is not normalized because it is used on both sides of
// the comparison, so it's magnitude does not matter
- //trianglefacinglight[i] = DotProduct(relativelightorigin, temp) >= DotProduct(v0, temp);
- f = DotProduct(relativelightorigin, temp) - DotProduct(v0, temp);
- trianglefacinglight[i] = f > 0 && f < lightradius * sqrt(DotProduct(temp, temp));
+ trianglefacinglight[i] = DotProduct(relativelightorigin, temp) >= DotProduct(v0, temp);
}
#endif
}
}
}
-float r_shadow_atten1;
+float r_shadow_attenpower, r_shadow_attenscale;
static void R_Shadow_MakeTextures(void)
{
int x, y, d, side;
qbyte *data;
R_FreeTexturePool(&r_shadow_texturepool);
r_shadow_texturepool = R_AllocTexturePool();
- r_shadow_atten1 = r_shadow_lightattenuationscale.value;
+ r_shadow_attenpower = r_shadow_lightattenuationpower.value;
+ r_shadow_attenscale = r_shadow_lightattenuationscale.value;
data = Mem_Alloc(tempmempool, 6*128*128*4);
data[0] = 128;
data[1] = 128;
data[2] = 255;
data[3] = 255;
r_shadow_blankbumptexture = R_LoadTexture2D(r_shadow_texturepool, "blankbump", 1, 1, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
- data[0] = 255;
- data[1] = 255;
- data[2] = 255;
+ data[0] = 64;
+ data[1] = 64;
+ data[2] = 64;
data[3] = 255;
r_shadow_blankglosstexture = R_LoadTexture2D(r_shadow_texturepool, "blankgloss", 1, 1, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
data[0] = 255;
v[2] = 0;
intensity = 1.0f - sqrt(DotProduct(v, v));
if (intensity > 0)
- intensity *= intensity;
- intensity = bound(0, intensity * r_shadow_atten1 * 256.0f, 255.0f);
+ intensity = pow(intensity, r_shadow_attenpower);
+ intensity = bound(0, intensity * r_shadow_attenscale * 256.0f, 255.0f);
d = bound(0, intensity, 255);
data[((0*128+y)*128+x)*4+0] = d;
data[((0*128+y)*128+x)*4+1] = d;
//cl.worldmodel->numlights = min(cl.worldmodel->numlights, 1);
if (!r_shadow_attenuation2dtexture
- || r_shadow_lightattenuationscale.value != r_shadow_atten1)
+ || r_shadow_lightattenuationpower.value != r_shadow_attenpower
+ || r_shadow_lightattenuationscale.value != r_shadow_attenscale)
R_Shadow_MakeTextures();
if (r_shadow_reloadlights && cl.worldmodel)
{
+ R_Shadow_ClearWorldLights();
r_shadow_reloadlights = false;
R_Shadow_LoadWorldLights();
if (r_shadow_worldlightchain == NULL)
return true;
// ok some of it is infront of the view, transform each corner back to
// worldspace and then to screenspace and make screen rect
+ // initialize these variables just to avoid compiler warnings
+ x1 = y1 = x2 = y2 = 0;
for (i = 0;i < 8;i++)
{
v2[0] = (i & 1) ? smins[0] : smaxs[0];
if (lightcubemap)
R_Shadow_GenTexCoords_LightCubeMap(varray_texcoord[1], numverts, varray_vertex, relativelightorigin);
- // the 0.25f makes specular lighting much dimmer than diffuse (intentionally)
- colorscale = r_colorscale * 0.25f * r_shadow_lightintensityscale.value;
+ 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);
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;
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);
}
}
+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, temp[3], dist;
+ float bestrating, rating, temp[3];
worldlight_t *best, *light;
best = NULL;
- bestrating = 1e30;
+ bestrating = 0;
for (light = r_shadow_worldlightchain;light;light = light->next)
{
VectorSubtract(light->origin, r_refdef.vieworg, temp);
- dist = sqrt(DotProduct(temp, temp));
- if (DotProduct(temp, vpn) >= 0.97 * dist && bestrating > dist && CL_TraceLine(light->origin, r_refdef.vieworg, NULL, NULL, 0, true, NULL) == 1.0f)
+ rating = (DotProduct(temp, vpn) / sqrt(DotProduct(temp, temp)));
+ if (rating >= 0.95)
{
- bestrating = dist;
- best = light;
+ 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);
}
if (light <= 0 && islight)
light = 300;
- radius = bound(0, light / scale, 1048576) + 15.0f;
- light = bound(0, light, 1048576) * (1.0f / 256.0f);
+ 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);
r_editlights_cursorlocation[2] = floor(endpos[2] / r_editlights_cursorgrid.value + 0.5f) * r_editlights_cursorgrid.value;
}
-extern void R_DrawCrosshairSprite(rtexture_t *texture, vec3_t origin, vec_t scale, float cr, float cg, float cb, float ca);
-void R_Shadow_DrawCursorCallback(const void *calldata1, int calldata2)
-{
- cachepic_t *pic;
- pic = Draw_CachePic("gfx/crosshair1.tga");
- if (pic)
- R_DrawCrosshairSprite(pic->tex, r_editlights_cursorlocation, r_editlights_cursorgrid.value * 0.5f, 1, 1, 1, 1);
-}
-
-void R_Shadow_DrawCursor(void)
-{
- R_MeshQueue_AddTransparent(r_editlights_cursorlocation, R_Shadow_DrawCursorCallback, NULL, 0);
-}
-
void R_Shadow_UpdateLightingMode(void)
{
r_shadow_lightingmode = 0;
{
if (r_editlights.integer)
{
- R_Shadow_SelectLightInView();
R_Shadow_SetCursorLocationForView();
- R_Shadow_DrawCursor();
+ R_Shadow_SelectLightInView();
+ R_Shadow_DrawLightSprites();
}
else
R_Shadow_SelectLight(NULL);
void R_Shadow_EditLights_Reload_f(void)
{
- if (cl.worldmodel)
- {
- R_Shadow_ClearWorldLights();
- R_Shadow_LoadWorldLights();
- }
+ r_shadow_reloadlights = true;
}
void R_Shadow_EditLights_Save_f(void)
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);