]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - r_light.c
don't change gamma ramps when running dedicated
[xonotic/darkplaces.git] / r_light.c
index 2c4d2d0d240b4e99292587f3e2e5b644b46bc640..f8f797c4b4c71ef95e9639ce92869f6c498d6e35 100644 (file)
--- a/r_light.c
+++ b/r_light.c
@@ -21,30 +21,26 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 #include "quakedef.h"
 
-cvar_t r_lightmodels = {"r_lightmodels", "1"};
+cvar_t r_lightmodels = {0, "r_lightmodels", "1"};
 
-extern cvar_t gl_transform;
-
-void r_light_start()
+void r_light_start(void)
 {
 }
 
-void r_light_shutdown()
+void r_light_shutdown(void)
 {
 }
 
-void r_light_newmap()
+void r_light_newmap(void)
 {
 }
 
-void R_Light_Init()
+void R_Light_Init(void)
 {
        Cvar_RegisterVariable(&r_lightmodels);
        R_RegisterModule("R_Light", r_light_start, r_light_shutdown, r_light_newmap);
 }
 
-int    r_dlightframecount;
-
 /*
 ==================
 R_AnimateLight
@@ -89,6 +85,7 @@ void R_OldMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex
 {
        float           ndist, maxdist;
        msurface_t      *surf;
+       mleaf_t         *leaf;
        int                     i;
 
        if (!r_dynamic.value)
@@ -103,35 +100,32 @@ void R_OldMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex
 
 loc0:
        if (node->contents < 0)
+       {
+               if (node->contents != CONTENTS_SOLID)
+               {
+                       leaf = (mleaf_t *)node;
+                       if (leaf->dlightframe != r_framecount) // not dynamic until now
+                       {
+                               leaf->dlightbits[0] = leaf->dlightbits[1] = leaf->dlightbits[2] = leaf->dlightbits[3] = leaf->dlightbits[4] = leaf->dlightbits[5] = leaf->dlightbits[6] = leaf->dlightbits[7] = 0;
+                               leaf->dlightframe = r_framecount;
+                       }
+                       leaf->dlightbits[bitindex] |= bit;
+               }
                return;
+       }
 
        ndist = PlaneDiff(lightorigin, node->plane);
        
        if (ndist > light->radius)
        {
-               if (node->children[0]->contents >= 0) // LordHavoc: save some time by not pushing another stack frame
-               {
-                       node = node->children[0];
-                       goto loc0;
-               }
-               return;
+               node = node->children[0];
+               goto loc0;
        }
        if (ndist < -light->radius)
        {
-               if (node->children[1]->contents >= 0) // LordHavoc: save some time by not pushing another stack frame
-               {
-                       node = node->children[1];
-                       goto loc0;
-               }
-               return;
-       }
-
-       if (node->dlightframe != r_dlightframecount) // not dynamic until now
-       {
-               node->dlightbits[0] = node->dlightbits[1] = node->dlightbits[2] = node->dlightbits[3] = node->dlightbits[4] = node->dlightbits[5] = node->dlightbits[6] = node->dlightbits[7] = 0;
-               node->dlightframe = r_dlightframecount;
+               node = node->children[1];
+               goto loc0;
        }
-       node->dlightbits[bitindex] |= bit;
 
 // mark the polygons
        surf = cl.worldmodel->surfaces + node->firstsurface;
@@ -139,6 +133,8 @@ loc0:
        {
                int d;
                float dist, dist2, impact[3];
+               if (surf->visframe != r_framecount)
+                       continue;
                dist = ndist;
                if (surf->flags & SURF_PLANEBACK)
                        dist = -dist;
@@ -192,20 +188,20 @@ loc0:
                        }
                }
 
-               if (surf->dlightframe != r_dlightframecount) // not dynamic until now
+               if (surf->dlightframe != r_framecount) // not dynamic until now
                {
                        surf->dlightbits[0] = surf->dlightbits[1] = surf->dlightbits[2] = surf->dlightbits[3] = surf->dlightbits[4] = surf->dlightbits[5] = surf->dlightbits[6] = surf->dlightbits[7] = 0;
-                       surf->dlightframe = r_dlightframecount;
+                       surf->dlightframe = r_framecount;
                }
                surf->dlightbits[bitindex] |= bit;
 
                /*
                if (((surf->flags & SURF_PLANEBACK) == 0) == ((PlaneDist(lightorigin, surf->plane)) >= surf->plane->dist))
                {
-                       if (surf->dlightframe != r_dlightframecount) // not dynamic until now
+                       if (surf->dlightframe != r_framecount) // not dynamic until now
                        {
                                surf->dlightbits[0] = surf->dlightbits[1] = surf->dlightbits[2] = surf->dlightbits[3] = surf->dlightbits[4] = surf->dlightbits[5] = surf->dlightbits[6] = surf->dlightbits[7] = 0;
-                               surf->dlightframe = r_dlightframecount;
+                               surf->dlightframe = r_framecount;
                        }
                        surf->dlightbits[bitindex] |= bit;
                }
@@ -238,9 +234,9 @@ void R_NoVisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitind
        R_OldMarkLights(lightorigin, light, bit, bitindex, model->nodes + model->hulls[0].firstclipnode);
 }
 
-int lightframe = 0;
 void R_VisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, model_t *model)
 {
+       static int lightframe = 0;
        mleaf_t *pvsleaf = Mod_PointInLeaf (lightorigin, model);
 
        if (!r_dynamic.value)
@@ -253,7 +249,7 @@ void R_VisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex
        }
        else
        {
-               int             i, k, l, m, c;
+               int             i, k, m, c, leafnum;
                msurface_t *surf, **mark;
                mleaf_t *leaf;
                byte    *in = pvsleaf->compressed_vis;
@@ -262,7 +258,7 @@ void R_VisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex
 
                lightframe++;
 
-               radius = light->radius * 2;
+               radius = light->radius * 3;
 
                // clamp radius to avoid exceeding 32768 entry division table
                if (radius > 2048)
@@ -280,28 +276,27 @@ void R_VisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex
                        c = *in++;
                        if (c)
                        {
-                               l = model->numleafs - (k << 3);
-                               if (l > 8)
-                                       l = 8;
-                               for (i=0 ; i<l ; i++)
+                               for (i = 0;i < 8;i++)
                                {
                                        if (c & (1<<i))
                                        {
-                                               leaf = &model->leafs[(k << 3)+i+1];
-                                               leaf->lightframe = lightframe;
-                                               if (leaf->visframe != r_visframecount)
-                                                       continue;
-                                               if (leaf->contents == CONTENTS_SOLID)
-                                                       continue;
+                                               leafnum = (k << 3)+i+1;
+                                               if (leafnum > model->numleafs)
+                                                       return;
+                                               leaf = &model->leafs[leafnum];
+//                                             if (leaf->visframe != r_framecount)
+//                                                     continue;
+//                                             if (leaf->contents == CONTENTS_SOLID)
+//                                                     continue;
                                                // if out of the light radius, skip
-                                               if (leaf->minmaxs[0] > high[0] || leaf->minmaxs[3] < low[0]
-                                                || leaf->minmaxs[1] > high[1] || leaf->minmaxs[4] < low[1]
-                                                || leaf->minmaxs[2] > high[2] || leaf->minmaxs[5] < low[2])
+                                               if (leaf->mins[0] > high[0] || leaf->maxs[0] < low[0]
+                                                || leaf->mins[1] > high[1] || leaf->maxs[1] < low[1]
+                                                || leaf->mins[2] > high[2] || leaf->maxs[2] < low[2])
                                                        continue;
-                                               if (leaf->dlightframe != r_dlightframecount) // not dynamic until now
+                                               if (leaf->dlightframe != r_framecount) // not dynamic until now
                                                {
                                                        leaf->dlightbits[0] = leaf->dlightbits[1] = leaf->dlightbits[2] = leaf->dlightbits[3] = leaf->dlightbits[4] = leaf->dlightbits[5] = leaf->dlightbits[6] = leaf->dlightbits[7] = 0;
-                                                       leaf->dlightframe = r_dlightframecount;
+                                                       leaf->dlightframe = r_framecount;
                                                }
                                                leaf->dlightbits[bitindex] |= bit;
                                                if ((m = leaf->nummarksurfaces))
@@ -310,14 +305,17 @@ void R_VisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex
                                                        do
                                                        {
                                                                surf = *mark++;
-                                                               if (surf->visframe != r_framecount || surf->lightframe == lightframe)
+                                                               // if not visible in current frame, or already marked because it was in another leaf we passed, skip
+                                                               if (surf->lightframe == lightframe)
                                                                        continue;
                                                                surf->lightframe = lightframe;
+                                                               if (surf->visframe != r_framecount)
+                                                                       continue;
                                                                dist = PlaneDiff(lightorigin, surf->plane);
                                                                if (surf->flags & SURF_PLANEBACK)
                                                                        dist = -dist;
                                                                // LordHavoc: make sure it is infront of the surface and not too far away
-                                                               if ((dist >= -0.25f || (surf->flags & SURF_LIGHTBOTHSIDES)) && dist < radius)
+                                                               if (dist < radius && (dist > -0.25f || ((surf->flags & SURF_LIGHTBOTHSIDES) && dist > -radius)))
                                                                {
                                                                        int d;
                                                                        float dist2, impact[3];
@@ -366,10 +364,10 @@ void R_VisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex
                                                                                }
                                                                        }
 
-                                                                       if (surf->dlightframe != r_dlightframecount) // not dynamic until now
+                                                                       if (surf->dlightframe != r_framecount) // not dynamic until now
                                                                        {
                                                                                surf->dlightbits[0] = surf->dlightbits[1] = surf->dlightbits[2] = surf->dlightbits[3] = surf->dlightbits[4] = surf->dlightbits[5] = surf->dlightbits[6] = surf->dlightbits[7] = 0;
-                                                                               surf->dlightframe = r_dlightframecount;
+                                                                               surf->dlightframe = r_framecount;
                                                                        }
                                                                        surf->dlightbits[bitindex] |= bit;
                                                                }
@@ -398,8 +396,6 @@ void R_PushDlights (void)
        int             i;
        dlight_t        *l;
 
-       r_dlightframecount = r_framecount + 1;  // because the count hasn't advanced yet for this frame
-
        if (!r_dynamic.value)
                return;
 
@@ -412,6 +408,8 @@ void R_PushDlights (void)
 //             R_MarkLights (l->origin, l, 1<<(i&31), i >> 5, cl.worldmodel->nodes );
                R_VisMarkLights (l->origin, l, 1<<(i&31), i >> 5, cl.worldmodel);
        }
+
+
 }
 
 
@@ -426,8 +424,6 @@ LIGHT SAMPLING
 mplane_t               *lightplane;
 vec3_t                 lightspot;
 
-extern cvar_t r_ambient;
-
 /*
 int RecursiveLightPoint (vec3_t color, mnode_t *node, vec3_t start, vec3_t end)
 {
@@ -654,6 +650,34 @@ loc0:
                                                lightmap += size3;
                                        }
 
+                                       /*
+                                       // LordHavoc: here's the readable version of the interpolation
+                                       // code, not quite as easy for the compiler to optimize...
+
+                                       // dsfrac is the X position in the lightmap pixel, * 16
+                                       // dtfrac is the Y position in the lightmap pixel, * 16
+                                       // r00 is top left corner, r01 is top right corner
+                                       // r10 is bottom left corner, r11 is bottom right corner
+                                       // g and b are the same layout.
+                                       // r0 and r1 are the top and bottom intermediate results
+
+                                       // first we interpolate the top two points, to get the top
+                                       // edge sample
+                                       r0 = (((r01-r00) * dsfrac) >> 4) + r00;
+                                       g0 = (((g01-g00) * dsfrac) >> 4) + g00;
+                                       b0 = (((b01-b00) * dsfrac) >> 4) + b00;
+                                       // then we interpolate the bottom two points, to get the
+                                       // bottom edge sample
+                                       r1 = (((r11-r10) * dsfrac) >> 4) + r10;
+                                       g1 = (((g11-g10) * dsfrac) >> 4) + g10;
+                                       b1 = (((b11-b10) * dsfrac) >> 4) + b10;
+                                       // then we interpolate the top and bottom samples to get the
+                                       // middle sample (the one which was requested)
+                                       r = (((r1-r0) * dtfrac) >> 4) + r0;
+                                       g = (((g1-g0) * dtfrac) >> 4) + g0;
+                                       b = (((b1-b0) * dtfrac) >> 4) + b0;
+                                       */
+
                                        color[0] += (float) ((((((((r11-r10) * dsfrac) >> 4) + r10)-((((r01-r00) * dsfrac) >> 4) + r00)) * dtfrac) >> 4) + ((((r01-r00) * dsfrac) >> 4) + r00)) * (1.0f / 256.0f);
                                        color[1] += (float) ((((((((g11-g10) * dsfrac) >> 4) + g10)-((((g01-g00) * dsfrac) >> 4) + g00)) * dtfrac) >> 4) + ((((g01-g00) * dsfrac) >> 4) + g00)) * (1.0f / 256.0f);
                                        color[2] += (float) ((((((((b11-b10) * dsfrac) >> 4) + b10)-((((b01-b00) * dsfrac) >> 4) + b00)) * dtfrac) >> 4) + ((((b01-b00) * dsfrac) >> 4) + b00)) * (1.0f / 256.0f);
@@ -750,7 +774,7 @@ void R_ModelLightPoint (vec3_t color, vec3_t p, int *dlightbits)
        color[0] = color[1] = color[2] = r_ambient.value * 2.0f;
        RecursiveLightPoint (color, cl.worldmodel->nodes, p[0], p[1], p[2], p[2] - 65536);
 
-       if (leaf->dlightframe == r_dlightframecount)
+       if (leaf->dlightframe == r_framecount)
        {
                dlightbits[0] = leaf->dlightbits[0];
                dlightbits[1] = leaf->dlightbits[1];
@@ -795,10 +819,6 @@ void R_DynamicLightPointNoMask(vec3_t color, vec3_t org)
 }
 */
 
-extern float *aliasvert;
-extern float *aliasvertnorm;
-extern byte *aliasvertcolor;
-extern float modelalpha;
 void R_LightModel(entity_t *ent, int numverts, vec3_t center, vec3_t basecolor)
 {
        // LordHavoc: warning: reliance on int being 4 bytes here (of course the d_8to24table relies on that too...)