]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - r_light.c
now entirely dynamic OpenGL binding
[xonotic/darkplaces.git] / r_light.c
index 7dd907640481631a0126d5506e5bb9e356b4c99b..fd18b086f2d48425f3ac35d32cbf13ebffd55d7e 100644 (file)
--- a/r_light.c
+++ b/r_light.c
@@ -43,7 +43,7 @@ void r_light_start(void)
                for (x = 0;x < 32;x++)
                {
                        dx = (x - 15.5f) * (1.0f / 16.0f);
-                       a = ((1.0f / (dx * dx + dy * dy + 0.2f)) - (1.0f / (1.0f + 0.2))) * 64.0f / (1.0f / (1.0f + 0.2));
+                       a = ((1.0f / (dx * dx + dy * dy + 0.2f)) - (1.0f / (1.0f + 0.2))) * 32.0f / (1.0f / (1.0f + 0.2));
                        a = bound(0, a, 255);
                        pixels[y][x][0] = 255;
                        pixels[y][x][1] = 255;
@@ -125,7 +125,7 @@ void R_BuildLightList(void)
                        rd->cullradius = 2048.0f;
                rd->cullradius2 = rd->cullradius * rd->cullradius;
                rd->lightsubtract = 1.0f / rd->cullradius2;
-               rd->ent = cd->ent;
+               //rd->ent = cd->ent;
                r_numdlights++;
                c_dlights++; // count every dlight in use
        }
@@ -171,7 +171,7 @@ void R_DrawCoronas(void)
                        VectorSubtract(rd->origin, vpn, diff);
                        if (CL_TraceLine(r_origin, diff, NULL, NULL, 0, true) == 1)
                        {
-                               scale = 1.0f / 262144.0f;
+                               scale = 1.0f / 131072.0f;
                                m.cr = rd->light[0] * scale;
                                m.cg = rd->light[1] * scale;
                                m.cb = rd->light[2] * scale;
@@ -274,9 +274,17 @@ loc0:
                if (dist2 >= maxdist)
                        continue;
 
-               impact[0] = rd->origin[0] - surf->plane->normal[0] * dist;
-               impact[1] = rd->origin[1] - surf->plane->normal[1] * dist;
-               impact[2] = rd->origin[2] - surf->plane->normal[2] * dist;
+               if (node->plane->type < 3)
+               {
+                       VectorCopy(rd->origin, impact);
+                       impact[node->plane->type] -= dist;
+               }
+               else
+               {
+                       impact[0] = rd->origin[0] - surf->plane->normal[0] * dist;
+                       impact[1] = rd->origin[1] - surf->plane->normal[1] * dist;
+                       impact[2] = rd->origin[2] - surf->plane->normal[2] * dist;
+               }
 
                impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
 
@@ -425,9 +433,17 @@ static void R_VisMarkLights (rdlight_t *rd, int bit, int bitindex)
 
                                                                dist2 = dist * dist;
 
-                                                               impact[0] = rd->origin[0] - surf->plane->normal[0] * dist;
-                                                               impact[1] = rd->origin[1] - surf->plane->normal[1] * dist;
-                                                               impact[2] = rd->origin[2] - surf->plane->normal[2] * dist;
+                                                               if (surf->plane->type < 3)
+                                                               {
+                                                                       VectorCopy(rd->origin, impact);
+                                                                       impact[surf->plane->type] -= dist;
+                                                               }
+                                                               else
+                                                               {
+                                                                       impact[0] = rd->origin[0] - surf->plane->normal[0] * dist;
+                                                                       impact[1] = rd->origin[1] - surf->plane->normal[1] * dist;
+                                                                       impact[2] = rd->origin[2] - surf->plane->normal[2] * dist;
+                                                               }
 
                                                                impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
                                                                d = bound(0, impacts, surf->extents[0] + 16) - impacts;
@@ -653,7 +669,7 @@ void R_CompleteLightPoint (vec3_t color, vec3_t p, int dynamic, mleaf_t *leaf)
                                {
                                        if (CL_TraceLine(p, sl->origin, NULL, NULL, 0, false) == 1)
                                        {
-                                               f *= d_lightstylevalue[sl->style] * (1.0f / 32768.0f);
+                                               f *= d_lightstylevalue[sl->style] * (1.0f / 16384.0f);
                                                VectorMA(color, f, sl->light, color);
                                        }
                                }
@@ -724,11 +740,11 @@ void R_ModelLightPoint (vec3_t color, vec3_t p, int *dlightbits)
 void R_LightModel(int numverts, float colorr, float colorg, float colorb, int worldcoords)
 {
        int i, j, nearlights = 0;
-       float color[3], basecolor[3], v[3], t, *av, *avn, *avc, a, number, f, dist2;
+       float color[3], basecolor[3], v[3], t, *av, *avn, *avc, a, f, dist2, mscale, dot;
        struct
        {
                vec3_t origin;
-               vec_t cullradius2;
+               //vec_t cullradius2;
                vec3_t light;
                vec_t lightsubtract;
                vec_t falloff;
@@ -738,11 +754,14 @@ void R_LightModel(int numverts, float colorr, float colorg, float colorb, int wo
        int modeldlightbits[8];
        mlight_t *sl;
        a = currentrenderentity->alpha;
-       if (currentrenderentity->effects & EF_FULLBRIGHT)
+       // scale of the model's coordinate space, to alter light attenuation to match
+       // make the mscale squared so it can scale the squared distance results
+       mscale = currentrenderentity->scale * currentrenderentity->scale;
+       if (r_fullbright.integer || r_ambient.value > 128.0f || currentrenderentity->effects & EF_FULLBRIGHT)
        {
-               basecolor[0] = colorr;
-               basecolor[1] = colorg;
-               basecolor[2] = colorb;
+               basecolor[0] = colorr * 2.0f;
+               basecolor[1] = colorg * 2.0f;
+               basecolor[2] = colorb * 2.0f;
        }
        else
        {
@@ -751,19 +770,56 @@ void R_LightModel(int numverts, float colorr, float colorg, float colorb, int wo
                        R_ModelLightPoint(basecolor, currentrenderentity->origin, modeldlightbits);
 
                        nl = &nearlight[0];
-                       for (i = 0, sl = cl.worldmodel->lights;i < cl.worldmodel->numlights && nearlights < MAX_DLIGHTS;i++, sl++)
+                       VectorSubtract(currentrenderentity->origin, currentrenderentity->entlightsorigin, v);
+                       if ((realtime > currentrenderentity->entlightstime && DotProduct(v,v) >= 1.0f) || currentrenderentity->numentlights >= MAXENTLIGHTS)
+                       {
+                               currentrenderentity->numentlights = 0;
+                               currentrenderentity->entlightstime = realtime + 0.2;
+                               VectorCopy(currentrenderentity->origin, currentrenderentity->entlightsorigin);
+                               for (i = 0, sl = cl.worldmodel->lights;i < cl.worldmodel->numlights && nearlights < MAX_DLIGHTS;i++, sl++)
+                               {
+                                       if (CL_TraceLine(currentrenderentity->origin, sl->origin, NULL, NULL, 0, false) == 1)
+                                       {
+                                               if (currentrenderentity->numentlights < MAXENTLIGHTS)
+                                                       currentrenderentity->entlights[currentrenderentity->numentlights++] = i;
+
+                                               // integrate mscale into falloff, for maximum speed
+                                               nl->falloff = mscale * sl->falloff;
+                                               // transform the light into the model's coordinate system
+                                               if (worldcoords)
+                                                       VectorCopy(sl->origin, nl->origin);
+                                               else
+                                                       softwareuntransform(sl->origin, nl->origin);
+                                               f = d_lightstylevalue[sl->style] * (1.0f / 65536.0f);
+                                               nl->light[0] = sl->light[0] * f * colorr;
+                                               nl->light[1] = sl->light[1] * f * colorg;
+                                               nl->light[2] = sl->light[2] * f * colorb;
+                                               //nl->cullradius2 = 99999999;
+                                               nl->lightsubtract = sl->subtract;
+                                               nl->offset = sl->distbias;
+                                               nl++;
+                                               nearlights++;
+                                       }
+                               }
+                       }
+                       else
                        {
-                               if (CL_TraceLine(currentrenderentity->origin, sl->origin, NULL, NULL, 0, false) == 1)
+                               for (i = 0;i < currentrenderentity->numentlights && nearlights < MAX_DLIGHTS;i++)
                                {
-                                       nl->falloff = sl->falloff;
+                                       sl = cl.worldmodel->lights + currentrenderentity->entlights[i];
+
+                                       // integrate mscale into falloff, for maximum speed
+                                       nl->falloff = mscale * sl->falloff;
                                        // transform the light into the model's coordinate system
                                        if (worldcoords)
                                                VectorCopy(sl->origin, nl->origin);
                                        else
                                                softwareuntransform(sl->origin, nl->origin);
-                                       f = d_lightstylevalue[sl->style] * (1.0f / 32768.0f);
-                                       VectorScale(sl->light, f, nl->light);
-                                       nl->cullradius2 = 99999999;
+                                       f = d_lightstylevalue[sl->style] * (1.0f / 65536.0f);
+                                       nl->light[0] = sl->light[0] * f * colorr;
+                                       nl->light[1] = sl->light[1] * f * colorg;
+                                       nl->light[2] = sl->light[2] * f * colorb;
+                                       //nl->cullradius2 = 99999999;
                                        nl->lightsubtract = sl->subtract;
                                        nl->offset = sl->distbias;
                                        nl++;
@@ -774,6 +830,7 @@ void R_LightModel(int numverts, float colorr, float colorg, float colorb, int wo
                        {
                                if (!(modeldlightbits[i >> 5] & (1 << (i & 31))))
                                        continue;
+                               /*
                                if (currentrenderentity == r_dlight[i].ent)
                                {
                                        f = (1.0f / LIGHTOFFSET) - nl->lightsubtract;
@@ -782,12 +839,16 @@ void R_LightModel(int numverts, float colorr, float colorg, float colorb, int wo
                                }
                                else
                                {
+                               */
                                        // transform the light into the model's coordinate system
                                        if (worldcoords)
                                                VectorCopy(r_dlight[i].origin, nl->origin);
                                        else
                                                softwareuntransform(r_dlight[i].origin, nl->origin);
-                                       nl->cullradius2 = r_dlight[i].cullradius2;
+                                       // integrate mscale into falloff, for maximum speed
+                                       nl->falloff = mscale;
+                                       // scale the cullradius so culling by distance is done before mscale is applied
+                                       //nl->cullradius2 = r_dlight[i].cullradius2 * currentrenderentity->scale * currentrenderentity->scale;
                                        nl->light[0] = r_dlight[i].light[0] * colorr;
                                        nl->light[1] = r_dlight[i].light[1] * colorg;
                                        nl->light[2] = r_dlight[i].light[2] * colorb;
@@ -795,7 +856,7 @@ void R_LightModel(int numverts, float colorr, float colorg, float colorb, int wo
                                        nl->offset = LIGHTOFFSET;
                                        nl++;
                                        nearlights++;
-                               }
+                               //}
                        }
                }
                else
@@ -814,27 +875,34 @@ void R_LightModel(int numverts, float colorr, float colorg, float colorb, int wo
                        VectorCopy(basecolor, color);
                        for (j = 0, nl = &nearlight[0];j < nearlights;j++, nl++)
                        {
-                               // distance attenuation
                                VectorSubtract(nl->origin, av, v);
-                               dist2 = DotProduct(v,v);
-                               if (dist2 < nl->cullradius2)
+                               // directional shading
+                               dot = DotProduct(avn,v);
+                               if (dot > 0)
                                {
-                                       f = (1.0f / (dist2 + nl->offset)) - nl->lightsubtract;
+                                       // the vertex normal faces the light
+
+                                       // do the distance attenuation
+                                       dist2 = DotProduct(v,v);
+                                       f = (1.0f / (dist2 * nl->falloff + nl->offset)) - nl->lightsubtract;
                                        if (f > 0)
                                        {
-                                               // directional shading
-#if SLOWMATH
+                                               #if SLOWMATH
                                                t = 1.0f / sqrt(dist2);
-#else
-                                               number = DotProduct(v, v);
-                                               *((int *)&t) = 0x5f3759df - ((* (int *) &number) >> 1);
-                                               t = t * (1.5f - (number * 0.5f * t * t));
-#endif
-                                               // DotProduct(avn,v) * t is dotproduct with a normalized v,
+                                               #else
+                                               *((int *)&t) = 0x5f3759df - ((* (int *) &dist2) >> 1);
+                                               t = t * (1.5f - (dist2 * 0.5f * t * t));
+                                               #endif
+
+                                               // dot * t is dotproduct with a normalized v.
+                                               // (the result would be -1 to +1, but we already
+                                               // eliminated the <= 0 case, so it is 0 to 1)
+
                                                // the hardness variables are for backlighting/shinyness
-                                               f *= DotProduct(avn,v) * t;// * hardness + hardnessoffset;
-                                               if (f > 0)
-                                                       VectorMA(color, f, nl->light, color);
+                                               // these have been hardwired at * 0.5 + 0.5 to match
+                                               // the quake map lighting utility's equations
+                                               f *= dot * t * 0.5f + 0.5f;// * hardness + hardnessoffset;
+                                               VectorMA(color, f, nl->light, color);
                                        }
                                }
                        }