]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - r_shadow.c
fix D3D scissoring
[xonotic/darkplaces.git] / r_shadow.c
index 9b1fbf83735fdb266b39c9ebbf8957af08f7504b..eb571dd25ddbc5f82ffaa2d627e7ee9179eda6df 100644 (file)
@@ -349,6 +349,7 @@ static memexpandablearray_t r_shadow_worldlightsarray;
 dlight_t *r_shadow_selectedlight;
 dlight_t r_shadow_bufferlight;
 vec3_t r_editlights_cursorlocation;
+qboolean r_editlights_lockcursor;
 
 extern int con_vislines;
 
@@ -665,7 +666,7 @@ void r_shadow_newmap(void)
        if (r_editlights_sprcubemaplight)         R_SkinFrame_MarkUsed(r_editlights_sprcubemaplight);
        if (r_editlights_sprcubemapnoshadowlight) R_SkinFrame_MarkUsed(r_editlights_sprcubemapnoshadowlight);
        if (r_editlights_sprselection)            R_SkinFrame_MarkUsed(r_editlights_sprselection);
-       if (cl.worldmodel && strncmp(cl.worldmodel->name, r_shadow_mapname, sizeof(r_shadow_mapname)))
+       if (strncmp(cl.worldname, r_shadow_mapname, sizeof(r_shadow_mapname)))
                R_Shadow_EditLights_Reload_f();
 }
 
@@ -760,7 +761,7 @@ void R_Shadow_Init(void)
        r_shadow_buffer_surfacesides = NULL;
        r_shadow_buffer_shadowtrispvs = NULL;
        r_shadow_buffer_lighttrispvs = NULL;
-       R_RegisterModule("R_Shadow", r_shadow_start, r_shadow_shutdown, r_shadow_newmap);
+       R_RegisterModule("R_Shadow", r_shadow_start, r_shadow_shutdown, r_shadow_newmap, NULL, NULL);
 }
 
 matrix4x4_t matrix_attenuationxyz =
@@ -1753,12 +1754,12 @@ static void R_Shadow_MakeTextures(void)
        // 1D gradient texture
        for (x = 0;x < ATTEN1DSIZE;x++)
                data[x] = R_Shadow_MakeTextures_SamplePoint((x + 0.5f) * (1.0f / ATTEN1DSIZE) * (1.0f / 0.9375), 0, 0);
-       r_shadow_attenuationgradienttexture = R_LoadTexture2D(r_shadow_texturepool, "attenuation1d", ATTEN1DSIZE, 1, (unsigned char *)data, TEXTYPE_BGRA, TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCELINEAR, NULL);
+       r_shadow_attenuationgradienttexture = R_LoadTexture2D(r_shadow_texturepool, "attenuation1d", ATTEN1DSIZE, 1, (unsigned char *)data, TEXTYPE_BGRA, TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCELINEAR, -1, NULL);
        // 2D circle texture
        for (y = 0;y < ATTEN2DSIZE;y++)
                for (x = 0;x < ATTEN2DSIZE;x++)
                        data[y*ATTEN2DSIZE+x] = R_Shadow_MakeTextures_SamplePoint(((x + 0.5f) * (2.0f / ATTEN2DSIZE) - 1.0f) * (1.0f / 0.9375), ((y + 0.5f) * (2.0f / ATTEN2DSIZE) - 1.0f) * (1.0f / 0.9375), 0);
-       r_shadow_attenuation2dtexture = R_LoadTexture2D(r_shadow_texturepool, "attenuation2d", ATTEN2DSIZE, ATTEN2DSIZE, (unsigned char *)data, TEXTYPE_BGRA, TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCELINEAR, NULL);
+       r_shadow_attenuation2dtexture = R_LoadTexture2D(r_shadow_texturepool, "attenuation2d", ATTEN2DSIZE, ATTEN2DSIZE, (unsigned char *)data, TEXTYPE_BGRA, TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCELINEAR, -1, NULL);
        // 3D sphere texture
        if (r_shadow_texture3d.integer && vid.support.ext_texture_3d)
        {
@@ -1766,7 +1767,7 @@ static void R_Shadow_MakeTextures(void)
                        for (y = 0;y < ATTEN3DSIZE;y++)
                                for (x = 0;x < ATTEN3DSIZE;x++)
                                        data[(z*ATTEN3DSIZE+y)*ATTEN3DSIZE+x] = R_Shadow_MakeTextures_SamplePoint(((x + 0.5f) * (2.0f / ATTEN3DSIZE) - 1.0f) * (1.0f / 0.9375), ((y + 0.5f) * (2.0f / ATTEN3DSIZE) - 1.0f) * (1.0f / 0.9375), ((z + 0.5f) * (2.0f / ATTEN3DSIZE) - 1.0f) * (1.0f / 0.9375));
-               r_shadow_attenuation3dtexture = R_LoadTexture3D(r_shadow_texturepool, "attenuation3d", ATTEN3DSIZE, ATTEN3DSIZE, ATTEN3DSIZE, (unsigned char *)data, TEXTYPE_BGRA, TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCELINEAR, NULL);
+               r_shadow_attenuation3dtexture = R_LoadTexture3D(r_shadow_texturepool, "attenuation3d", ATTEN3DSIZE, ATTEN3DSIZE, ATTEN3DSIZE, (unsigned char *)data, TEXTYPE_BGRA, TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCELINEAR, -1, NULL);
        }
        else
                r_shadow_attenuation3dtexture = NULL;
@@ -2089,7 +2090,7 @@ static void R_Shadow_MakeVSDCT(void)
                0,   0, 0x33, 0xFF, // +Z: <0, 0>, <0.5, 2.5>
                0,   0, 0x99, 0xFF, // -Z: <0, 0>, <1.5, 2.5>
        };
-       r_shadow_shadowmapvsdcttexture = R_LoadTextureCubeMap(r_shadow_texturepool, "shadowmapvsdct", 1, data, TEXTYPE_RGBA, TEXF_FORCENEAREST | TEXF_CLAMP | TEXF_ALPHA, NULL);
+       r_shadow_shadowmapvsdcttexture = R_LoadTextureCubeMap(r_shadow_texturepool, "shadowmapvsdct", 1, data, TEXTYPE_RGBA, TEXF_FORCENEAREST | TEXF_CLAMP | TEXF_ALPHA, -1, NULL);
 }
 
 static void R_Shadow_MakeShadowMap(int side, int size)
@@ -2426,138 +2427,28 @@ void R_Shadow_RenderMode_End(void)
        r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE;
 }
 
-int bboxedges[12][2] =
-{
-       // top
-       {0, 1}, // +X
-       {0, 2}, // +Y
-       {1, 3}, // Y, +X
-       {2, 3}, // X, +Y
-       // bottom
-       {4, 5}, // +X
-       {4, 6}, // +Y
-       {5, 7}, // Y, +X
-       {6, 7}, // X, +Y
-       // verticals
-       {0, 4}, // +Z
-       {1, 5}, // X, +Z
-       {2, 6}, // Y, +Z
-       {3, 7}, // XY, +Z
-};
-
 qboolean R_Shadow_ScissorForBBox(const float *mins, const float *maxs)
 {
-       int i, ix1, iy1, ix2, iy2;
-       float x1, y1, x2, y2;
-       vec4_t v, v2;
-       float vertex[20][3];
-       int j, k;
-       vec4_t plane4f;
-       int numvertices;
-       float corner[8][4];
-       float dist[8];
-       int sign[8];
-       float f;
-
-       r_shadow_lightscissor[0] = r_refdef.view.viewport.x;
-       r_shadow_lightscissor[1] = r_refdef.view.viewport.y;
-       r_shadow_lightscissor[2] = r_refdef.view.viewport.width;
-       r_shadow_lightscissor[3] = r_refdef.view.viewport.height;
-
        if (!r_shadow_scissor.integer)
-               return false;
-
-       // if view is inside the light box, just say yes it's visible
-       if (BoxesOverlap(r_refdef.view.origin, r_refdef.view.origin, mins, maxs))
-               return false;
-
-       x1 = y1 = x2 = y2 = 0;
-
-       // transform all corners that are infront of the nearclip plane
-       VectorNegate(r_refdef.view.frustum[4].normal, plane4f);
-       plane4f[3] = r_refdef.view.frustum[4].dist;
-       numvertices = 0;
-       for (i = 0;i < 8;i++)
-       {
-               Vector4Set(corner[i], (i & 1) ? maxs[0] : mins[0], (i & 2) ? maxs[1] : mins[1], (i & 4) ? maxs[2] : mins[2], 1);
-               dist[i] = DotProduct4(corner[i], plane4f);
-               sign[i] = dist[i] > 0;
-               if (!sign[i])
-               {
-                       VectorCopy(corner[i], vertex[numvertices]);
-                       numvertices++;
-               }
-       }
-       // if some points are behind the nearclip, add clipped edge points to make
-       // sure that the scissor boundary is complete
-       if (numvertices > 0 && numvertices < 8)
        {
-               // add clipped edge points
-               for (i = 0;i < 12;i++)
-               {
-                       j = bboxedges[i][0];
-                       k = bboxedges[i][1];
-                       if (sign[j] != sign[k])
-                       {
-                               f = dist[j] / (dist[j] - dist[k]);
-                               VectorLerp(corner[j], f, corner[k], vertex[numvertices]);
-                               numvertices++;
-                       }
-               }
+               r_shadow_lightscissor[0] = r_refdef.view.viewport.x;
+               r_shadow_lightscissor[1] = r_refdef.view.viewport.y;
+               r_shadow_lightscissor[2] = r_refdef.view.viewport.width;
+               r_shadow_lightscissor[3] = r_refdef.view.viewport.height;
+               return false;
        }
 
-       // if we have no points to check, the light is behind the view plane
-       if (!numvertices)
-               return true;
-
-       // if we have some points to transform, check what screen area is covered
-       x1 = y1 = x2 = y2 = 0;
-       v[3] = 1.0f;
-       //Con_Printf("%i vertices to transform...\n", numvertices);
-       for (i = 0;i < numvertices;i++)
+       if(R_ScissorForBBox(mins, maxs, r_shadow_lightscissor))
        {
-               VectorCopy(vertex[i], v);
-               R_Viewport_TransformToScreen(&r_refdef.view.viewport, v, v2);
-               //Con_Printf("%.3f %.3f %.3f %.3f transformed to %.3f %.3f %.3f %.3f\n", v[0], v[1], v[2], v[3], v2[0], v2[1], v2[2], v2[3]);
-               if (i)
-               {
-                       if (x1 > v2[0]) x1 = v2[0];
-                       if (x2 < v2[0]) x2 = v2[0];
-                       if (y1 > v2[1]) y1 = v2[1];
-                       if (y2 < v2[1]) y2 = v2[1];
-               }
-               else
-               {
-                       x1 = x2 = v2[0];
-                       y1 = y2 = v2[1];
-               }
+               if(r_shadow_lightscissor[0] != r_refdef.view.viewport.x
+               || r_shadow_lightscissor[1] != r_refdef.view.viewport.y
+               || r_shadow_lightscissor[2] != r_refdef.view.viewport.width
+               || r_shadow_lightscissor[3] != r_refdef.view.viewport.height)
+                       r_refdef.stats.lights_scissored++;
+               return false;
        }
-
-       // now convert the scissor rectangle to integer screen coordinates
-       ix1 = (int)(x1 - 1.0f);
-       iy1 = vid.height - (int)(y2 - 1.0f);
-       ix2 = (int)(x2 + 1.0f);
-       iy2 = vid.height - (int)(y1 + 1.0f);
-       //Con_Printf("%f %f %f %f\n", x1, y1, x2, y2);
-
-       // clamp it to the screen
-       if (ix1 < r_refdef.view.viewport.x) ix1 = r_refdef.view.viewport.x;
-       if (iy1 < r_refdef.view.viewport.y) iy1 = r_refdef.view.viewport.y;
-       if (ix2 > r_refdef.view.viewport.x + r_refdef.view.viewport.width) ix2 = r_refdef.view.viewport.x + r_refdef.view.viewport.width;
-       if (iy2 > r_refdef.view.viewport.y + r_refdef.view.viewport.height) iy2 = r_refdef.view.viewport.y + r_refdef.view.viewport.height;
-
-       // if it is inside out, it's not visible
-       if (ix2 <= ix1 || iy2 <= iy1)
-               return true;
-
-       // the light area is visible, set up the scissor rectangle
-       r_shadow_lightscissor[0] = ix1;
-       r_shadow_lightscissor[1] = iy1;
-       r_shadow_lightscissor[2] = ix2 - ix1;
-       r_shadow_lightscissor[3] = iy2 - iy1;
-
-       r_refdef.stats.lights_scissored++;
-       return false;
+       else
+               return true; // invisible
 }
 
 static void R_Shadow_RenderLighting_Light_Vertex_Shading(int firstvertex, int numverts, int numtriangles, const int *element3i, const float *diffusecolor, const float *ambientcolor)
@@ -4105,9 +3996,9 @@ void R_Shadow_PrepareLights(void)
                        r_shadow_prepass_width = vid.width;
                        r_shadow_prepass_height = vid.height;
                        r_shadow_prepassgeometrydepthtexture = R_LoadTextureShadowMap2D(r_shadow_texturepool, "prepassgeometrydepthmap", vid.width, vid.height, 24, false);
-                       r_shadow_prepassgeometrynormalmaptexture = R_LoadTexture2D(r_shadow_texturepool, "prepassgeometrynormalmap", vid.width, vid.height, NULL, TEXTYPE_COLORBUFFER, TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCENEAREST, NULL);
-                       r_shadow_prepasslightingdiffusetexture = R_LoadTexture2D(r_shadow_texturepool, "prepasslightingdiffuse", vid.width, vid.height, NULL, TEXTYPE_COLORBUFFER, TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCENEAREST, NULL);
-                       r_shadow_prepasslightingspeculartexture = R_LoadTexture2D(r_shadow_texturepool, "prepasslightingspecular", vid.width, vid.height, NULL, TEXTYPE_COLORBUFFER, TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCENEAREST, NULL);
+                       r_shadow_prepassgeometrynormalmaptexture = R_LoadTexture2D(r_shadow_texturepool, "prepassgeometrynormalmap", vid.width, vid.height, NULL, TEXTYPE_COLORBUFFER, TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCENEAREST, -1, NULL);
+                       r_shadow_prepasslightingdiffusetexture = R_LoadTexture2D(r_shadow_texturepool, "prepasslightingdiffuse", vid.width, vid.height, NULL, TEXTYPE_COLORBUFFER, TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCENEAREST, -1, NULL);
+                       r_shadow_prepasslightingspeculartexture = R_LoadTexture2D(r_shadow_texturepool, "prepasslightingspecular", vid.width, vid.height, NULL, TEXTYPE_COLORBUFFER, TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCENEAREST, -1, NULL);
 
                        // set up the geometry pass fbo (depth + normalmap)
                        qglGenFramebuffersEXT(1, &r_shadow_prepassgeometryfbo);CHECKGLERROR
@@ -4258,7 +4149,9 @@ void R_Shadow_PrepareModelShadows(void)
        {
        case R_SHADOW_SHADOWMODE_SHADOWMAP2D:
        case R_SHADOW_SHADOWMODE_SHADOWMAPRECTANGLE:
-               break;
+               if (r_shadows.integer >= 2) 
+                       break;
+               // fall through
        case R_SHADOW_SHADOWMODE_STENCIL:
                for (i = 0;i < r_refdef.scene.numentities;i++)
                {
@@ -4486,7 +4379,7 @@ void R_DrawModelShadows(void)
        vec3_t relativeshadowmins, relativeshadowmaxs;
        vec3_t tmp, shadowdir;
 
-       if (!r_refdef.scene.numentities || !vid.stencil || r_shadow_shadowmode != R_SHADOW_SHADOWMODE_STENCIL)
+       if (!r_refdef.scene.numentities || !vid.stencil || (r_shadow_shadowmode != R_SHADOW_SHADOWMODE_STENCIL && r_shadows.integer != 1))
                return;
 
        CHECKGLERROR
@@ -4928,7 +4821,8 @@ void R_Shadow_DrawLightSprites(void)
                if (light)
                        R_MeshQueue_AddTransparent(light->origin, R_Shadow_DrawLightSprite_TransparentCallback, (entity_render_t *)light, 5, &light->rtlight);
        }
-       R_MeshQueue_AddTransparent(r_editlights_cursorlocation, R_Shadow_DrawCursor_TransparentCallback, NULL, 0, NULL);
+       if (!r_editlights_lockcursor)
+               R_MeshQueue_AddTransparent(r_editlights_cursorlocation, R_Shadow_DrawCursor_TransparentCallback, NULL, 0, NULL);
 }
 
 int R_Shadow_GetRTLightInfo(unsigned int lightindex, float *origin, float *radius, float *color)
@@ -4960,6 +4854,9 @@ void R_Shadow_SelectLightInView(void)
        size_t range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
        best = NULL;
        bestrating = 0;
+
+       if (r_editlights_lockcursor)
+               return;
        for (lightindex = 0;lightindex < range;lightindex++)
        {
                light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
@@ -4990,8 +4887,7 @@ void R_Shadow_LoadWorldLights(void)
                Con_Print("No map loaded.\n");
                return;
        }
-       FS_StripExtension (cl.worldmodel->name, name, sizeof (name));
-       strlcat (name, ".rtlights", sizeof (name));
+       dpsnprintf(name, sizeof(name), "%s.rtlights", cl.worldnamenoextension);
        lightsstring = (char *)FS_LoadFile(name, tempmempool, false, NULL);
        if (lightsstring)
        {
@@ -5101,8 +4997,7 @@ void R_Shadow_SaveWorldLights(void)
                Con_Print("No map loaded.\n");
                return;
        }
-       FS_StripExtension (cl.worldmodel->name, name, sizeof (name));
-       strlcat (name, ".rtlights", sizeof (name));
+       dpsnprintf(name, sizeof(name), "%s.rtlights", cl.worldnamenoextension);
        bufchars = bufmaxchars = 0;
        buf = NULL;
        for (lightindex = 0;lightindex < range;lightindex++)
@@ -5150,8 +5045,7 @@ void R_Shadow_LoadLightsFile(void)
                Con_Print("No map loaded.\n");
                return;
        }
-       FS_StripExtension (cl.worldmodel->name, name, sizeof (name));
-       strlcat (name, ".lights", sizeof (name));
+       dpsnprintf(name, sizeof(name), "%s.lights", cl.worldnamenoextension);
        lightsstring = (char *)FS_LoadFile(name, tempmempool, false, NULL);
        if (lightsstring)
        {
@@ -5213,8 +5107,7 @@ void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void)
                return;
        }
        // try to load a .ent file first
-       FS_StripExtension (cl.worldmodel->name, key, sizeof (key));
-       strlcat (key, ".ent", sizeof (key));
+       dpsnprintf(key, sizeof(key), "%s.ent", cl.worldnamenoextension);
        data = entfiledata = (char *)FS_LoadFile(key, tempmempool, true, NULL);
        // and if that is not found, fall back to the bsp file entity string
        if (!data)
@@ -5463,7 +5356,7 @@ void R_Shadow_EditLights_Reload_f(void)
 {
        if (!cl.worldmodel)
                return;
-       strlcpy(r_shadow_mapname, cl.worldmodel->name, sizeof(r_shadow_mapname));
+       strlcpy(r_shadow_mapname, cl.worldname, sizeof(r_shadow_mapname));
        R_Shadow_ClearWorldLights();
        R_Shadow_LoadWorldLights();
        if (!Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray))
@@ -5828,7 +5721,7 @@ void R_Shadow_EditLights_Edit_f(void)
 void R_Shadow_EditLights_EditAll_f(void)
 {
        size_t lightindex;
-       dlight_t *light;
+       dlight_t *light, *oldselected;
        size_t range;
 
        if (!r_editlights.integer)
@@ -5837,6 +5730,7 @@ void R_Shadow_EditLights_EditAll_f(void)
                return;
        }
 
+       oldselected = r_shadow_selectedlight;
        // EditLights doesn't seem to have a "remove" command or something so:
        range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
        for (lightindex = 0;lightindex < range;lightindex++)
@@ -5847,6 +5741,8 @@ void R_Shadow_EditLights_EditAll_f(void)
                R_Shadow_SelectLight(light);
                R_Shadow_EditLights_Edit_f();
        }
+       // return to old selected (to not mess editing once selection is locked)
+       R_Shadow_SelectLight(oldselected);
 }
 
 void R_Shadow_EditLights_DrawSelectedLightProperties(void)
@@ -5956,6 +5852,7 @@ void R_Shadow_EditLights_Help_f(void)
 "r_editlights_help : this help\n"
 "r_editlights_clear : remove all lights\n"
 "r_editlights_reload : reload .rtlights, .lights file, or entities\n"
+"r_editlights_lock : lock selection to current light, if already locked - unlock\n"
 "r_editlights_save : save to .rtlights file\n"
 "r_editlights_spawn : create a light with default settings\n"
 "r_editlights_edit command : edit selected light - more documentation below\n"
@@ -6040,6 +5937,26 @@ void R_Shadow_EditLights_PasteInfo_f(void)
        R_Shadow_UpdateWorldLight(r_shadow_selectedlight, r_shadow_selectedlight->origin, r_shadow_bufferlight.angles, r_shadow_bufferlight.color, r_shadow_bufferlight.radius, r_shadow_bufferlight.corona, r_shadow_bufferlight.style, r_shadow_bufferlight.shadow, r_shadow_bufferlight.cubemapname, r_shadow_bufferlight.coronasizescale, r_shadow_bufferlight.ambientscale, r_shadow_bufferlight.diffusescale, r_shadow_bufferlight.specularscale, r_shadow_bufferlight.flags);
 }
 
+void R_Shadow_EditLights_Lock_f(void)
+{
+       if (!r_editlights.integer)
+       {
+               Con_Print("Cannot lock on light when not in editing mode.  Set r_editlights to 1.\n");
+               return;
+       }
+       if (r_editlights_lockcursor)
+       {
+               r_editlights_lockcursor = false;
+               return;
+       }
+       if (!r_shadow_selectedlight)
+       {
+               Con_Print("No selected light to lock on.\n");
+               return;
+       }
+       r_editlights_lockcursor = true;
+}
+
 void R_Shadow_EditLights_Init(void)
 {
        Cvar_RegisterVariable(&r_editlights);
@@ -6062,6 +5979,7 @@ void R_Shadow_EditLights_Init(void)
        Cmd_AddCommand("r_editlights_importlightsfile", R_Shadow_EditLights_ImportLightsFile_f, "load lights from .lights file (ignoring .rtlights or .ent files and map entities)");
        Cmd_AddCommand("r_editlights_copyinfo", R_Shadow_EditLights_CopyInfo_f, "store a copy of all properties (except origin) of the selected light");
        Cmd_AddCommand("r_editlights_pasteinfo", R_Shadow_EditLights_PasteInfo_f, "apply the stored properties onto the selected light (making it exactly identical except for origin)");
+       Cmd_AddCommand("r_editlights_lock", R_Shadow_EditLights_Lock_f, "lock selection to current light, if already locked - unlock");
 }