]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - r_light.c
fix flipped sprites
[xonotic/darkplaces.git] / r_light.c
index 1c46419d1f7f66d7e0bf2bec9c2e24442742f195..395af9ec520b35648e3105d125effeb9a3849932 100644 (file)
--- a/r_light.c
+++ b/r_light.c
@@ -21,6 +21,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 #include "quakedef.h"
 #include "cl_collision.h"
+#include "r_shadow.h"
 
 rdlight_t r_dlight[MAX_DLIGHTS];
 int r_numdlights = 0;
@@ -153,10 +154,6 @@ void R_DrawCoronas(void)
        R_Mesh_Matrix(&r_identitymatrix);
        R_Mesh_State(&m);
        viewdist = DotProduct(r_origin, vpn);
-       varray_texcoord[0][ 0] = 0;varray_texcoord[0][ 1] = 0;
-       varray_texcoord[0][ 4] = 0;varray_texcoord[0][ 5] = 1;
-       varray_texcoord[0][ 8] = 1;varray_texcoord[0][ 9] = 1;
-       varray_texcoord[0][12] = 1;varray_texcoord[0][13] = 0;
        for (i = 0;i < r_numdlights;i++)
        {
                rd = r_dlight + i;
@@ -175,19 +172,7 @@ void R_DrawCoronas(void)
                        scale = rd->cullradius * 0.25f;
                        if (gl_flashblend.integer)
                                scale *= 2.0f;
-                       varray_vertex[0] = rd->origin[0] - vright[0] * scale - vup[0] * scale;
-                       varray_vertex[1] = rd->origin[1] - vright[1] * scale - vup[1] * scale;
-                       varray_vertex[2] = rd->origin[2] - vright[2] * scale - vup[2] * scale;
-                       varray_vertex[4] = rd->origin[0] - vright[0] * scale + vup[0] * scale;
-                       varray_vertex[5] = rd->origin[1] - vright[1] * scale + vup[1] * scale;
-                       varray_vertex[6] = rd->origin[2] - vright[2] * scale + vup[2] * scale;
-                       varray_vertex[8] = rd->origin[0] + vright[0] * scale + vup[0] * scale;
-                       varray_vertex[9] = rd->origin[1] + vright[1] * scale + vup[1] * scale;
-                       varray_vertex[10] = rd->origin[2] + vright[2] * scale + vup[2] * scale;
-                       varray_vertex[12] = rd->origin[0] + vright[0] * scale - vup[0] * scale;
-                       varray_vertex[13] = rd->origin[1] + vright[1] * scale - vup[1] * scale;
-                       varray_vertex[14] = rd->origin[2] + vright[2] * scale - vup[2] * scale;
-                       R_Mesh_Draw(4, 2, polygonelements);
+                       R_DrawSpriteMesh(rd->origin, vright, vup, scale, -scale, -scale, scale);
                }
        }
 }
@@ -284,6 +269,8 @@ loc0:
                {
                        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_framecount;
+                       if (r_dlightmap.integer)
+                               surf->cached_dlight = true;
                }
                surf->dlightbits[bitindex] |= bit;
        }
@@ -423,6 +410,8 @@ static void R_VisMarkLights (entity_render_t *ent, rdlight_t *rd, int bit, int b
                                                                {
                                                                        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_framecount;
+                                                                       if (r_dlightmap.integer)
+                                                                               surf->cached_dlight = true;
                                                                }
                                                                surf->dlightbits[bitindex] |= bit;
                                                        }
@@ -647,22 +636,6 @@ void R_CompleteLightPoint (vec3_t color, const vec3_t p, int dynamic, const mlea
        }
 }
 
-void R_ModelLightPoint (const entity_render_t *ent, vec3_t color, const vec3_t p)
-{
-       mleaf_t *leaf;
-       leaf = Mod_PointInLeaf(p, cl.worldmodel);
-       if (!leaf || leaf->contents == CONTENTS_SOLID || r_fullbright.integer || !cl.worldmodel->lightdata || ent->effects & EF_FULLBRIGHT)
-       {
-               color[0] = color[1] = color[2] = 1;
-               return;
-       }
-
-       color[0] = color[1] = color[2] = r_ambient.value * (2.0f / 128.0f);
-       if (!cl.worldmodel->numlights)
-               RecursiveLightPoint (color, cl.worldmodel->nodes, p[0], p[1], p[2], p[2] - 65536);
-}
-
-extern cvar_t r_shadows;
 void R_LightModel(const entity_render_t *ent, int numverts, float *vertices, float *normals, float *colors, float colorr, float colorg, float colorb, int worldcoords)
 {
        int i, j, nearlights = 0, maxnearlights = r_modellights.integer;
@@ -683,67 +656,81 @@ void R_LightModel(const entity_render_t *ent, int numverts, float *vertices, flo
        nearlight[MAX_DLIGHTS], *nl;
        mlight_t *sl;
        rdlight_t *rd;
+       mleaf_t *leaf;
        a = ent->alpha;
        // 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 = ent->scale * ent->scale;
-       if ((maxnearlights != 0) && !r_fullbright.integer && !(ent->effects & EF_FULLBRIGHT))
+       if (r_fullbright.integer || (ent->effects & EF_FULLBRIGHT))
+               basecolor[0] = basecolor[1] = basecolor[2] = 1.0f;
+       else if (r_shadow_realtime_world.integer)
+               basecolor[0] = basecolor[1] = basecolor[2] = r_ambient.value * (2.0f / 128.0f);
+       else if (maxnearlights == 0)
+               R_CompleteLightPoint (basecolor, ent->origin, true, NULL);
+       else
        {
-               R_ModelLightPoint(ent, basecolor, ent->origin);
-
-               if (r_shadows.integer != 3)
+               leaf = Mod_PointInLeaf(ent->origin, cl.worldmodel);
+               if (!leaf || leaf->contents == CONTENTS_SOLID || !cl.worldmodel->lightdata)
+                       basecolor[0] = basecolor[1] = basecolor[2] = 1;
+               else
+               {
+                       basecolor[0] = basecolor[1] = basecolor[2] = r_ambient.value * (2.0f / 128.0f);
+                       if (!cl.worldmodel->numlights)
+                               RecursiveLightPoint (basecolor, cl.worldmodel->nodes, ent->origin[0], ent->origin[1], ent->origin[2], ent->origin[2] - 65536);
+               }
+               nl = &nearlight[0];
+               for (i = 0;i < ent->numentlights;i++)
                {
-                       nl = &nearlight[0];
-                       for (i = 0;i < ent->numentlights;i++)
+                       sl = cl.worldmodel->lights + ent->entlights[i];
+                       stylescale = d_lightstylevalue[sl->style] * (1.0f / 65536.0f);
+                       VectorSubtract (ent->origin, sl->origin, v);
+                       f = ((1.0f / (DotProduct(v, v) * sl->falloff + sl->distbias)) - sl->subtract) * stylescale;
+                       VectorScale(sl->light, f, ambientcolor);
+                       intensity = DotProduct(ambientcolor, ambientcolor);
+                       if (f < 0)
+                               intensity *= -1.0f;
+                       if (nearlights < maxnearlights)
+                               j = nearlights++;
+                       else
                        {
-                               sl = cl.worldmodel->lights + ent->entlights[i];
-                               stylescale = d_lightstylevalue[sl->style] * (1.0f / 65536.0f);
-                               VectorSubtract (ent->origin, sl->origin, v);
-                               f = ((1.0f / (DotProduct(v, v) * sl->falloff + sl->distbias)) - sl->subtract) * stylescale;
-                               VectorScale(sl->light, f, ambientcolor);
-                               intensity = DotProduct(ambientcolor, ambientcolor);
-                               if (f < 0)
-                                       intensity *= -1.0f;
-                               if (nearlights < maxnearlights)
-                                       j = nearlights++;
-                               else
+                               for (j = 0;j < maxnearlights;j++)
                                {
-                                       for (j = 0;j < maxnearlights;j++)
+                                       if (nearlight[j].intensity < intensity)
                                        {
-                                               if (nearlight[j].intensity < intensity)
-                                               {
-                                                       if (nearlight[j].intensity > 0)
-                                                               VectorAdd(basecolor, nearlight[j].ambientlight, basecolor);
-                                                       break;
-                                               }
+                                               if (nearlight[j].intensity > 0)
+                                                       VectorAdd(basecolor, nearlight[j].ambientlight, basecolor);
+                                               break;
                                        }
                                }
-                               if (j >= maxnearlights)
-                               {
-                                       // this light is less significant than all others,
-                                       // add it to ambient
-                                       if (intensity > 0)
-                                               VectorAdd(basecolor, ambientcolor, basecolor);
-                               }
+                       }
+                       if (j >= maxnearlights)
+                       {
+                               // this light is less significant than all others,
+                               // add it to ambient
+                               if (intensity > 0)
+                                       VectorAdd(basecolor, ambientcolor, basecolor);
+                       }
+                       else
+                       {
+                               nl = nearlight + j;
+                               nl->intensity = intensity;
+                               // transform the light into the model's coordinate system
+                               if (worldcoords)
+                                       VectorCopy(sl->origin, nl->origin);
                                else
-                               {
-                                       nl = nearlight + j;
-                                       nl->intensity = intensity;
-                                       // transform the light into the model's coordinate system
-                                       if (worldcoords)
-                                               VectorCopy(sl->origin, nl->origin);
-                                       else
-                                               Matrix4x4_Transform(&ent->inversematrix, sl->origin, nl->origin);
-                                       // integrate mscale into falloff, for maximum speed
-                                       nl->falloff = sl->falloff * mscale;
-                                       VectorCopy(ambientcolor, nl->ambientlight);
-                                       nl->light[0] = sl->light[0] * stylescale * colorr * 4.0f;
-                                       nl->light[1] = sl->light[1] * stylescale * colorg * 4.0f;
-                                       nl->light[2] = sl->light[2] * stylescale * colorb * 4.0f;
-                                       nl->subtract = sl->subtract;
-                                       nl->offset = sl->distbias;
-                               }
+                                       Matrix4x4_Transform(&ent->inversematrix, sl->origin, nl->origin);
+                               // integrate mscale into falloff, for maximum speed
+                               nl->falloff = sl->falloff * mscale;
+                               VectorCopy(ambientcolor, nl->ambientlight);
+                               nl->light[0] = sl->light[0] * stylescale * colorr * 4.0f;
+                               nl->light[1] = sl->light[1] * stylescale * colorg * 4.0f;
+                               nl->light[2] = sl->light[2] * stylescale * colorb * 4.0f;
+                               nl->subtract = sl->subtract;
+                               nl->offset = sl->distbias;
                        }
+               }
+               if (!r_shadow_realtime_dlight.integer)
+               {
                        for (i = 0;i < r_numdlights;i++)
                        {
                                rd = r_dlight + i;
@@ -817,8 +804,6 @@ void R_LightModel(const entity_render_t *ent, int numverts, float *vertices, flo
                        }
                }
        }
-       else
-               R_CompleteLightPoint (basecolor, ent->origin, true, NULL);
        basecolor[0] *= colorr;
        basecolor[1] *= colorg;
        basecolor[2] *= colorb;
@@ -833,7 +818,7 @@ void R_LightModel(const entity_render_t *ent, int numverts, float *vertices, flo
                        VectorCopy(basecolor, color);
                        for (j = 0, nl = &nearlight[0];j < nearlights;j++, nl++)
                        {
-                               VectorSubtract(nl->origin, av, v);
+                               VectorSubtract(av, nl->origin, v);
                                // directional shading
                                dot = DotProduct(avn,v);
                                if (dot > 0)
@@ -868,8 +853,8 @@ void R_LightModel(const entity_render_t *ent, int numverts, float *vertices, flo
                        VectorCopy(color, avc);
                        avc[3] = a;
                        avc += 4;
-                       av += 4;
-                       avn += 4;
+                       av += 3;
+                       avn += 3;
                }
        }
        else
@@ -881,6 +866,8 @@ void R_UpdateEntLights(entity_render_t *ent)
        int i;
        const mlight_t *sl;
        vec3_t v;
+       if (r_shadow_realtime_dlight.integer)
+               return;
        VectorSubtract(ent->origin, ent->entlightsorigin, v);
        if (ent->entlightsframe != (r_framecount - 1) || (realtime > ent->entlightstime && DotProduct(v,v) >= 1.0f))
        {