#include "r_shadow.h"
#include "cl_collision.h"
#include "portals.h"
+#include "image.h"
extern void R_Shadow_EditLights_Init(void);
rtexture_t *r_shadow_blankglosstexture;
rtexture_t *r_shadow_blankwhitetexture;
+// used only for light filters (cubemaps)
+rtexturepool_t *r_shadow_filters_texturepool;
+
+cvar_t r_shadow_realtime_world_lightmaps = {0, "r_shadow_realtime_world_lightmaps", "0"};
cvar_t r_shadow_lightattenuationpower = {0, "r_shadow_lightattenuationpower", "0.5"};
cvar_t r_shadow_lightattenuationscale = {0, "r_shadow_lightattenuationscale", "1"};
cvar_t r_shadow_lightintensityscale = {0, "r_shadow_lightintensityscale", "1"};
cvar_t r_shadow_scissor = {0, "r_shadow_scissor", "1"};
cvar_t r_shadow_bumpscale_bumpmap = {0, "r_shadow_bumpscale_bumpmap", "4"};
cvar_t r_shadow_bumpscale_basetexture = {0, "r_shadow_bumpscale_basetexture", "0"};
-cvar_t r_shadow_polygonoffset = {0, "r_shadow_polygonoffset", "0"};
+cvar_t r_shadow_polygonfactor = {0, "r_shadow_polygonfactor", "0"};
+cvar_t r_shadow_polygonoffset = {0, "r_shadow_polygonoffset", "1"};
cvar_t r_shadow_portallight = {0, "r_shadow_portallight", "1"};
cvar_t r_shadow_projectdistance = {0, "r_shadow_projectdistance", "10000"};
cvar_t r_shadow_texture3d = {0, "r_shadow_texture3d", "1"};
cvar_t r_shadow_singlepassvolumegeneration = {0, "r_shadow_singlepassvolumegeneration", "1"};
-cvar_t r_shadow_shadows = {CVAR_SAVE, "r_shadow_shadows", "1"};
+cvar_t r_shadow_worldshadows = {0, "r_shadow_worldshadows", "1"};
+cvar_t r_shadow_dlightshadows = {CVAR_SAVE, "r_shadow_dlightshadows", "1"};
cvar_t r_shadow_showtris = {0, "r_shadow_showtris", "0"};
int c_rt_lights, c_rt_clears, c_rt_scissored;
r_shadow_blankglosstexture = NULL;
r_shadow_blankwhitetexture = NULL;
r_shadow_texturepool = NULL;
+ r_shadow_filters_texturepool = NULL;
R_Shadow_ClearWorldLights();
r_shadow_reloadlights = true;
}
r_shadow_blankglosstexture = NULL;
r_shadow_blankwhitetexture = NULL;
R_FreeTexturePool(&r_shadow_texturepool);
+ R_FreeTexturePool(&r_shadow_filters_texturepool);
maxshadowelements = 0;
shadowelements = NULL;
maxvertexupdate = 0;
"r_shadow_lightintensityscale : scale rendering brightness of all lights\n"
"r_shadow_realtime_world : use realtime world light rendering\n"
"r_shadow_realtime_dlight : use high quality dlight rendering\n"
+"r_shadow_realtime_world_lightmaps : use lightmaps in addition to rtlights\n"
"r_shadow_visiblevolumes : useful for performance testing; bright = slow!\n"
"r_shadow_gloss 0/1/2 : no gloss, gloss textures only, force gloss\n"
"r_shadow_glossintensity : brightness of textured gloss\n"
"r_shadow_scissor : use scissor optimization\n"
"r_shadow_bumpscale_bumpmap : depth scale for bumpmap conversion\n"
"r_shadow_bumpscale_basetexture : base texture as bumpmap with this scale\n"
+"r_shadow_polygonfactor : nudge shadow volumes closer/further\n"
"r_shadow_polygonoffset : nudge shadow volumes closer/further\n"
"r_shadow_portallight : use portal visibility for static light precomputation\n"
"r_shadow_projectdistance : shadow volume projection distance\n"
"r_shadow_texture3d : use 3d attenuation texture (if hardware supports)\n"
"r_shadow_singlepassvolumegeneration : selects shadow volume algorithm\n"
-"r_shadow_shadows : dlight shadows (world always has shadows)\n"
+"r_shadow_worldshadows : enable world shadows\n"
+"r_shadow_dlightshadows : enable dlight shadows\n"
"Commands:\n"
"r_shadow_help : this help\n"
);
Cvar_RegisterVariable(&r_shadow_lightattenuationscale);
Cvar_RegisterVariable(&r_shadow_lightintensityscale);
Cvar_RegisterVariable(&r_shadow_realtime_world);
+ Cvar_RegisterVariable(&r_shadow_realtime_world_lightmaps);
Cvar_RegisterVariable(&r_shadow_realtime_dlight);
Cvar_RegisterVariable(&r_shadow_visiblevolumes);
Cvar_RegisterVariable(&r_shadow_gloss);
Cvar_RegisterVariable(&r_shadow_scissor);
Cvar_RegisterVariable(&r_shadow_bumpscale_bumpmap);
Cvar_RegisterVariable(&r_shadow_bumpscale_basetexture);
+ Cvar_RegisterVariable(&r_shadow_polygonfactor);
Cvar_RegisterVariable(&r_shadow_polygonoffset);
Cvar_RegisterVariable(&r_shadow_portallight);
Cvar_RegisterVariable(&r_shadow_projectdistance);
Cvar_RegisterVariable(&r_shadow_texture3d);
Cvar_RegisterVariable(&r_shadow_singlepassvolumegeneration);
- Cvar_RegisterVariable(&r_shadow_shadows);
+ Cvar_RegisterVariable(&r_shadow_worldshadows);
+ Cvar_RegisterVariable(&r_shadow_dlightshadows);
Cvar_RegisterVariable(&r_shadow_showtris);
Cmd_AddCommand("r_shadow_help", R_Shadow_Help_f);
R_Shadow_EditLights_Init();
GL_BlendFunc(GL_ONE, GL_ZERO);
GL_DepthMask(false);
GL_DepthTest(true);
- if (r_shadow_polygonoffset.value != 0)
- {
- qglPolygonOffset(1.0f, r_shadow_polygonoffset.value);
- qglEnable(GL_POLYGON_OFFSET_FILL);
- }
- else
- qglDisable(GL_POLYGON_OFFSET_FILL);
+ qglPolygonOffset(r_shadow_polygonfactor.value, r_shadow_polygonoffset.value);
+ //if (r_shadow_polygonoffset.value != 0)
+ //{
+ // qglPolygonOffset(r_shadow_polygonfactor.value, r_shadow_polygonoffset.value);
+ // qglEnable(GL_POLYGON_OFFSET_FILL);
+ //}
+ //else
+ // qglDisable(GL_POLYGON_OFFSET_FILL);
qglDepthFunc(GL_LESS);
qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
qglEnable(GL_STENCIL_TEST);
GL_BlendFunc(GL_ONE, GL_ONE);
GL_DepthMask(false);
GL_DepthTest(true);
- qglDisable(GL_POLYGON_OFFSET_FILL);
+ qglPolygonOffset(0, 0);
+ //qglDisable(GL_POLYGON_OFFSET_FILL);
GL_Color(1, 1, 1, 1);
qglColorMask(1, 1, 1, 1);
qglDepthFunc(GL_EQUAL);
GL_BlendFunc(GL_ONE, GL_ONE);
GL_DepthMask(false);
GL_DepthTest(true);
- qglDisable(GL_POLYGON_OFFSET_FILL);
+ qglPolygonOffset(0, 0);
+ //qglDisable(GL_POLYGON_OFFSET_FILL);
GL_Color(1, 1, 1, 1);
qglColorMask(1, 1, 1, 1);
qglDepthFunc(GL_EQUAL);
GL_BlendFunc(GL_ONE, GL_ZERO);
GL_DepthMask(true);
GL_DepthTest(true);
- qglDisable(GL_POLYGON_OFFSET_FILL);
+ qglPolygonOffset(0, 0);
+ //qglDisable(GL_POLYGON_OFFSET_FILL);
GL_Color(1, 1, 1, 1);
qglColorMask(1, 1, 1, 1);
qglDisable(GL_SCISSOR_TEST);
memset(&m, 0, sizeof(m));
m.tex[0] = R_GetTexture(basetexture);
- m.texcubemap[1] = R_GetTexture(lightcubemap);
m.pointer_texcoord[0] = texcoord2f;
- m.pointer_texcoord[1] = lightcubemap ? varray_texcoord3f[1] : NULL;
+ 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_modeltofilter);
+ }
R_Mesh_State_Texture(&m);
qglColorMask(1,1,1,0);
GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
- if (lightcubemap)
- R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter);
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]--)
{
memset(&m, 0, sizeof(m));
m.tex[0] = R_GetTexture(basetexture);
- m.texcubemap[1] = R_GetTexture(lightcubemap);
m.pointer_texcoord[0] = texcoord2f;
- m.pointer_texcoord[1] = lightcubemap ? varray_texcoord3f[1] : NULL;
+ 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_modeltofilter);
+ }
R_Mesh_State_Texture(&m);
qglColorMask(1,1,1,0);
GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
- if (lightcubemap)
- R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter);
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]--)
{
memset(&m, 0, sizeof(m));
m.tex[0] = R_GetTexture(basetexture);
- m.texcubemap[1] = R_GetTexture(lightcubemap);
m.pointer_texcoord[0] = texcoord2f;
- m.pointer_texcoord[1] = lightcubemap ? varray_texcoord3f[1] : NULL;
+ 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_modeltofilter);
+ }
R_Mesh_State_Texture(&m);
qglColorMask(1,1,1,0);
GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
- if (lightcubemap)
- R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter);
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]--)
{
memset(&m, 0, sizeof(m));
m.tex[0] = R_GetTexture(basetexture);
- m.texcubemap[1] = R_GetTexture(lightcubemap);
m.pointer_texcoord[0] = texcoord2f;
- m.pointer_texcoord[1] = lightcubemap ? varray_texcoord3f[1] : NULL;
+ 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_modeltofilter);
+ }
R_Mesh_State_Texture(&m);
qglColorMask(1,1,1,0);
GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
- if (lightcubemap)
- R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter);
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]--)
{
memset(&m, 0, sizeof(m));
m.tex[0] = R_GetTexture(glosstexture);
- m.texcubemap[1] = R_GetTexture(lightcubemap);
+ 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_modeltofilter);
+ }
m.pointer_texcoord[0] = texcoord2f;
- m.pointer_texcoord[1] = lightcubemap ? varray_texcoord3f[1] : NULL;
R_Mesh_State_Texture(&m);
qglColorMask(1,1,1,0);
GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
- if (lightcubemap)
- R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter);
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]--)
{
memset(&m, 0, sizeof(m));
m.tex[0] = R_GetTexture(glosstexture);
- m.texcubemap[1] = R_GetTexture(lightcubemap);
+ 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_modeltofilter);
+ }
m.pointer_texcoord[0] = texcoord2f;
- m.pointer_texcoord[1] = lightcubemap ? varray_texcoord3f[1] : NULL;
R_Mesh_State_Texture(&m);
qglColorMask(1,1,1,0);
GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
- if (lightcubemap)
- R_Shadow_Transform_Vertex3f_TexCoord3f(varray_texcoord3f[1], numverts, vertex3f, matrix_modeltofilter);
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]--)
{
}
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_modeltofilter, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, mesh->map_diffuse, mesh->map_normal, NULL);
- R_Shadow_SpecularLighting(mesh->numverts, mesh->numtriangles, mesh->element3i, mesh->vertex3f, mesh->svector3f, mesh->tvector3f, mesh->normal3f, mesh->texcoord2f, relativelightorigin, relativeeyeorigin, lightradius, lightcolor, matrix_modeltofilter, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, mesh->map_specular, mesh->map_normal, NULL);
+ R_Shadow_DiffuseLighting(mesh->numverts, mesh->numtriangles, mesh->element3i, mesh->vertex3f, mesh->svector3f, mesh->tvector3f, mesh->normal3f, mesh->texcoord2f, relativelightorigin, lightradius, lightcolor, matrix_modeltofilter, 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_modeltofilter, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, mesh->map_specular, mesh->map_normal, light->cubemap);
}
}
static int lightpvsbytes;
static qbyte lightpvs[(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, true, 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\"\n", basename);
+ 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, float radius, vec3_t color, int style, const char *cubemapname, int castshadow)
{
int i, j, k, l, maxverts = 256, tris;
{
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)
+ e->cubemap = R_Shadow_Cubemap(e->cubemapname);
}
// FIXME: rewrite this to store ALL geometry into a cache in the light
if (e->castshadows)
while (r_shadow_worldlightchain)
R_Shadow_FreeWorldLight(r_shadow_worldlightchain);
r_shadow_selectedlight = NULL;
+ R_Shadow_FreeCubemaps();
}
void R_Shadow_SelectLight(worldlight_t *light)
{
int entnum, style, islight;
char key[256], value[1024];
- float origin[3], radius, color[3], light, scale, originhack[3], overridecolor[3];
+ float origin[3], radius, color[3], light, fadescale, lightscale, originhack[3], overridecolor[3];
const char *data;
if (cl.worldmodel == NULL)
originhack[0] = originhack[1] = originhack[2] = 0;
color[0] = color[1] = color[2] = 1;
overridecolor[0] = overridecolor[1] = overridecolor[2] = 1;
- scale = 1;
+ fadescale = 1;
+ lightscale = 1;
style = 0;
islight = false;
while (1)
else if (!strcmp("color", key))
sscanf(value, "%f %f %f", &color[0], &color[1], &color[2]);
else if (!strcmp("wait", key))
- scale = atof(value);
+ fadescale = atof(value);
else if (!strcmp("classname", key))
{
if (!strncmp(value, "light", 5))
}
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;
- radius = min(light * r_editlights_quakelightsizescale.value / scale, 1048576);
+ 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);