]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - r_shadow.c
ignore visofs errors on leaf 0 (solid), thanks to Vic for pointing out the problem...
[xonotic/darkplaces.git] / r_shadow.c
index a5d5c7461bb77f3a39eda27b815d4588d9f50f6d..9a9071a853434108c9eb4a4b62446d692ca98fd5 100644 (file)
@@ -14,7 +14,16 @@ did not intersect the visible geometry, suitable as a stencil mask for
 rendering lighting everywhere but shadow.
 
 In our case we use a biased stencil clear of 128 to avoid requiring the
-stencil wrap extension (but probably should support it).
+stencil wrap extension (but probably should support it), and to address
+Creative's patent on this sort of technology we also draw the frontfaces
+first, and backfaces second (decrement, increment).
+
+Patent warning:
+This algorithm may be covered by Creative's patent (US Patent #6384822)
+on Carmack's Reverse paper (which I have not read), however that patent
+seems to be about drawing a stencil shadow from a model in an otherwise
+unshadowed scene, where as realtime lighting technology draws light where
+shadows do not lie.
 
 
 
@@ -626,15 +635,15 @@ void R_Shadow_Volume(int numverts, int numtris, const float *invertex3f, int *el
                GL_VertexPointer(varray_vertex3f2);
                if (r_shadowstage == SHADOWSTAGE_STENCIL)
                {
-                       // increment stencil if backface is behind depthbuffer
-                       qglCullFace(GL_BACK); // quake is backwards, this culls front faces
-                       qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
-                       R_Mesh_Draw(outverts, tris, shadowelements);
-                       c_rt_shadowmeshes++;
-                       c_rt_shadowtris += numtris;
                        // decrement stencil if frontface is behind depthbuffer
                        qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
                        qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
+                       R_Mesh_Draw(outverts, tris, shadowelements);
+                       c_rt_shadowmeshes++;
+                       c_rt_shadowtris += numtris;
+                       // increment stencil if backface is behind depthbuffer
+                       qglCullFace(GL_BACK); // quake is backwards, this culls front faces
+                       qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
                }
                R_Mesh_Draw(outverts, tris, shadowelements);
                c_rt_shadowmeshes++;
@@ -647,9 +656,9 @@ void R_Shadow_RenderShadowMeshVolume(shadowmesh_t *firstmesh)
        shadowmesh_t *mesh;
        if (r_shadowstage == SHADOWSTAGE_STENCIL)
        {
-               // increment stencil if backface is behind depthbuffer
-               qglCullFace(GL_BACK); // quake is backwards, this culls front faces
-               qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
+               // decrement stencil if frontface is behind depthbuffer
+               qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
+               qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
                for (mesh = firstmesh;mesh;mesh = mesh->next)
                {
                        GL_VertexPointer(mesh->vertex3f);
@@ -657,9 +666,9 @@ void R_Shadow_RenderShadowMeshVolume(shadowmesh_t *firstmesh)
                        c_rtcached_shadowmeshes++;
                        c_rtcached_shadowtris += mesh->numtriangles;
                }
-               // decrement stencil if frontface is behind depthbuffer
-               qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
-               qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
+               // increment stencil if backface is behind depthbuffer
+               qglCullFace(GL_BACK); // quake is backwards, this culls front faces
+               qglStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
        }
        for (mesh = firstmesh;mesh;mesh = mesh->next)
        {
@@ -818,6 +827,7 @@ void R_Shadow_Stage_Begin(void)
        GL_DepthTest(true);
        R_Mesh_State_Texture(&m);
        GL_Color(0, 0, 0, 1);
+       qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
        qglDisable(GL_SCISSOR_TEST);
        r_shadowstage = SHADOWSTAGE_NONE;
 
@@ -860,6 +870,7 @@ void R_Shadow_Stage_ShadowVolumes(void)
        else
                qglDisable(GL_POLYGON_OFFSET_FILL);
        qglDepthFunc(GL_LESS);
+       qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
        qglEnable(GL_STENCIL_TEST);
        qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
        qglStencilFunc(GL_ALWAYS, 128, 0xFF);
@@ -887,6 +898,7 @@ void R_Shadow_Stage_LightWithoutShadows(void)
        GL_Color(1, 1, 1, 1);
        qglColorMask(1, 1, 1, 1);
        qglDepthFunc(GL_EQUAL);
+       qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
        qglDisable(GL_STENCIL_TEST);
        qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
        qglStencilFunc(GL_EQUAL, 128, 0xFF);
@@ -906,6 +918,7 @@ void R_Shadow_Stage_LightWithShadows(void)
        GL_Color(1, 1, 1, 1);
        qglColorMask(1, 1, 1, 1);
        qglDepthFunc(GL_EQUAL);
+       qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
        qglEnable(GL_STENCIL_TEST);
        qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
        // only draw light where this geometry was already rendered AND the
@@ -928,6 +941,7 @@ void R_Shadow_Stage_End(void)
        qglColorMask(1, 1, 1, 1);
        qglDisable(GL_SCISSOR_TEST);
        qglDepthFunc(GL_LEQUAL);
+       qglCullFace(GL_FRONT); // quake is backwards, this culls back faces
        qglDisable(GL_STENCIL_TEST);
        qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
        qglStencilFunc(GL_ALWAYS, 128, 0xFF);
@@ -945,14 +959,14 @@ int R_Shadow_ScissorForBBox(const float *mins, const float *maxs)
        // if view is inside the box, just say yes it's visible
        // LordHavoc: for some odd reason scissor seems broken without stencil
        // (?!?  seems like a driver bug) so abort if gl_stencil is false
-       if (!gl_stencil || BoxesOverlap(r_origin, r_origin, mins, maxs))
+       if (!gl_stencil || BoxesOverlap(r_vieworigin, r_vieworigin, mins, maxs))
        {
                qglDisable(GL_SCISSOR_TEST);
                return false;
        }
        for (i = 0;i < 3;i++)
        {
-               if (vpn[i] >= 0)
+               if (r_viewforward[i] >= 0)
                {
                        v[i] = mins[i];
                        v2[i] = maxs[i];
@@ -963,13 +977,13 @@ int R_Shadow_ScissorForBBox(const float *mins, const float *maxs)
                        v2[i] = mins[i];
                }
        }
-       f = DotProduct(vpn, r_origin) + 1;
-       if (DotProduct(vpn, v2) <= f)
+       f = DotProduct(r_viewforward, r_vieworigin) + 1;
+       if (DotProduct(r_viewforward, v2) <= f)
        {
                // entirely behind nearclip plane
                return true;
        }
-       if (DotProduct(vpn, v) >= f)
+       if (DotProduct(r_viewforward, v) >= f)
        {
                // entirely infront of nearclip plane
                x1 = y1 = x2 = y2 = 0;
@@ -1004,12 +1018,12 @@ int R_Shadow_ScissorForBBox(const float *mins, const float *maxs)
                // create viewspace bbox
                for (i = 0;i < 8;i++)
                {
-                       v[0] = ((i & 1) ? mins[0] : maxs[0]) - r_origin[0];
-                       v[1] = ((i & 2) ? mins[1] : maxs[1]) - r_origin[1];
-                       v[2] = ((i & 4) ? mins[2] : maxs[2]) - r_origin[2];
-                       v2[0] = DotProduct(v, vright);
-                       v2[1] = DotProduct(v, vup);
-                       v2[2] = DotProduct(v, vpn);
+                       v[0] = ((i & 1) ? mins[0] : maxs[0]) - r_vieworigin[0];
+                       v[1] = ((i & 2) ? mins[1] : maxs[1]) - r_vieworigin[1];
+                       v[2] = ((i & 4) ? mins[2] : maxs[2]) - r_vieworigin[2];
+                       v2[0] = -DotProduct(v, r_viewleft);
+                       v2[1] = DotProduct(v, r_viewup);
+                       v2[2] = DotProduct(v, r_viewforward);
                        if (i)
                        {
                                if (smins[0] > v2[0]) smins[0] = v2[0];
@@ -1042,9 +1056,9 @@ int R_Shadow_ScissorForBBox(const float *mins, const float *maxs)
                        v2[0] = (i & 1) ? smins[0] : smaxs[0];
                        v2[1] = (i & 2) ? smins[1] : smaxs[1];
                        v2[2] = (i & 4) ? smins[2] : smaxs[2];
-                       v[0] = v2[0] * vright[0] + v2[1] * vup[0] + v2[2] * vpn[0] + r_origin[0];
-                       v[1] = v2[0] * vright[1] + v2[1] * vup[1] + v2[2] * vpn[1] + r_origin[1];
-                       v[2] = v2[0] * vright[2] + v2[1] * vup[2] + v2[2] * vpn[2] + r_origin[2];
+                       v[0] = v2[0] * -r_viewleft[0] + v2[1] * r_viewup[0] + v2[2] * r_viewforward[0] + r_vieworigin[0];
+                       v[1] = v2[0] * -r_viewleft[1] + v2[1] * r_viewup[1] + v2[2] * r_viewforward[1] + r_vieworigin[1];
+                       v[2] = v2[0] * -r_viewleft[2] + v2[1] * r_viewup[2] + v2[2] * r_viewforward[2] + r_vieworigin[2];
                        v[3] = 1.0f;
                        GL_TransformToScreen(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]);
@@ -1877,12 +1891,12 @@ void R_Shadow_NewWorldLight(vec3_t origin, float radius, vec3_t color, int style
                                if (face->lighttemp_castshadow)
                                {
                                        face->lighttemp_castshadow = false;
-                                       if (!(face->texture->renderflags & (Q3MTEXTURERENDERFLAGS_NODRAW | Q3MTEXTURERENDERFLAGS_SKY)))
+                                       if (!(face->texture->surfaceflags & (Q3SURFACEFLAG_NODRAW | Q3SURFACEFLAG_SKY)))
                                        {
                                                if (e->castshadows)
                                                        if (!(face->texture->nativecontents & CONTENTSQ3_TRANSLUCENT))
                                                                Mod_ShadowMesh_AddMesh(r_shadow_mempool, castmesh, NULL, NULL, NULL, face->data_vertex3f, NULL, NULL, NULL, NULL, face->num_triangles, face->data_element3i);
-                                               if (!(face->texture->renderflags & (Q3MTEXTURERENDERFLAGS_SKY)))
+                                               if (!(face->texture->surfaceflags & Q3SURFACEFLAG_SKY))
                                                        Mod_ShadowMesh_AddMesh(r_shadow_mempool, e->meshchain_light, face->texture->skin.base, face->texture->skin.gloss, face->texture->skin.nmap, face->data_vertex3f, face->data_svector3f, face->data_tvector3f, face->data_normal3f, face->data_texcoordtexture2f, face->num_triangles, face->data_element3i);
                                        }
                                }
@@ -2055,7 +2069,7 @@ rtexture_t *lighttextures[5];
 void R_Shadow_DrawCursorCallback(const void *calldata1, int calldata2)
 {
        float scale = r_editlights_cursorgrid.value * 0.5f;
-       R_DrawSprite(GL_SRC_ALPHA, GL_ONE, lighttextures[0], false, r_editlights_cursorlocation, vright, vup, scale, -scale, -scale, scale, 1, 1, 1, 0.5f);
+       R_DrawSprite(GL_SRC_ALPHA, GL_ONE, lighttextures[0], false, r_editlights_cursorlocation, r_viewright, r_viewup, scale, -scale, -scale, scale, 1, 1, 1, 0.5f);
 }
 
 void R_Shadow_DrawLightSpriteCallback(const void *calldata1, int calldata2)
@@ -2068,7 +2082,7 @@ void R_Shadow_DrawLightSpriteCallback(const void *calldata1, int calldata2)
                intensity = 0.75 + 0.25 * sin(realtime * M_PI * 4.0);
        if (!light->meshchain_shadow)
                intensity *= 0.5f;
-       R_DrawSprite(GL_SRC_ALPHA, GL_ONE, lighttextures[calldata2], false, light->origin, vright, vup, 8, -8, -8, 8, intensity, intensity, intensity, 0.5);
+       R_DrawSprite(GL_SRC_ALPHA, GL_ONE, lighttextures[calldata2], false, light->origin, r_viewright, r_viewup, 8, -8, -8, 8, intensity, intensity, intensity, 0.5);
 }
 
 void R_Shadow_DrawLightSprites(void)
@@ -2097,12 +2111,12 @@ void R_Shadow_SelectLightInView(void)
        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)));
+               VectorSubtract(light->origin, r_vieworigin, temp);
+               rating = (DotProduct(temp, r_viewforward) / 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, true, NULL, SUPERCONTENTS_SOLID) == 1.0f)
+                       if (bestrating < rating && CL_TraceLine(light->origin, r_vieworigin, NULL, NULL, true, NULL, SUPERCONTENTS_SOLID) == 1.0f)
                        {
                                bestrating = rating;
                                best = light;
@@ -2259,7 +2273,7 @@ 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];
+       float origin[3], radius, color[3], light, fadescale, lightscale, originhack[3], overridecolor[3];
        const char *data;
 
        if (cl.worldmodel == NULL)
@@ -2277,7 +2291,8 @@ void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void)
                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)
@@ -2304,7 +2319,7 @@ void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void)
                        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))
@@ -2377,10 +2392,21 @@ void R_Shadow_LoadWorldLightsFromMap_LightArghliteTyrlite(void)
                        }
                        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);
@@ -2396,8 +2422,8 @@ 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, true, NULL, SUPERCONTENTS_SOLID);
+       VectorMA(r_vieworigin, r_editlights_cursordistance.value, r_viewforward, dest);
+       frac = CL_TraceLine(r_vieworigin, dest, endpos, normal, true, NULL, SUPERCONTENTS_SOLID);
        if (frac < 1)
        {
                dist = frac * r_editlights_cursordistance.value;
@@ -2405,7 +2431,7 @@ void R_Shadow_SetCursorLocationForView(void)
                if (push > dist)
                        push = dist;
                push = -push;
-               VectorMA(endpos, push, vpn, endpos);
+               VectorMA(endpos, push, r_viewforward, 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;