]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_rsurf.c
rearranged sky rendering code
[xonotic/darkplaces.git] / gl_rsurf.c
index 9031969f8c27bc30ccd493922145779fb7d22c32..8029f021f66a3e7c7e10982e1f22f5473f645984 100644 (file)
@@ -46,27 +46,27 @@ cvar_t gl_nosubimagefragments = {"gl_nosubimagefragments", "0"};
 cvar_t gl_nosubimage = {"gl_nosubimage", "0"};
 cvar_t r_ambient = {"r_ambient", "0"};
 cvar_t gl_vertex = {"gl_vertex", "0"};
-cvar_t gl_texsort = {"gl_texsort", "1"};
-cvar_t r_newworldnode = {"r_newworldnode", "0"};
-cvar_t r_oldclip = {"r_oldclip", "1"};
 cvar_t r_dlightmap = {"r_dlightmap", "1"};
+cvar_t r_drawportals = {"r_drawportals", "0"};
 
-qboolean lightmaprgba, nosubimagefragments, nosubimage, skyisvisible;
+qboolean lightmaprgba, nosubimagefragments, nosubimage;
 int lightmapbytes;
 
-void gl_surf_start()
+int wateralpha;
+
+void gl_surf_start(void)
 {
 }
 
-void gl_surf_shutdown()
+void gl_surf_shutdown(void)
 {
 }
 
-void gl_surf_newmap()
+void gl_surf_newmap(void)
 {
 }
 
-void GL_Surf_Init()
+void GL_Surf_Init(void)
 {
        int i;
        for (i = 0;i < MAX_LIGHTMAPS;i++)
@@ -77,10 +77,8 @@ void GL_Surf_Init()
        Cvar_RegisterVariable(&gl_nosubimage);
        Cvar_RegisterVariable(&r_ambient);
        Cvar_RegisterVariable(&gl_vertex);
-       Cvar_RegisterVariable(&gl_texsort);
-       Cvar_RegisterVariable(&r_newworldnode);
-       Cvar_RegisterVariable(&r_oldclip);
        Cvar_RegisterVariable(&r_dlightmap);
+       Cvar_RegisterVariable(&r_drawportals);
 
        R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);
 }
@@ -121,7 +119,7 @@ int R_AddDynamicLights (msurface_t *surf)
                if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
                        continue;                                       // not lit by this light
 
-               VectorSubtract (cl_dlights[lnum].origin, currententity->origin, local);
+               VectorSubtract (cl_dlights[lnum].origin, currententity->render.origin, local);
                dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
 
                // for comparisons to minimum acceptable light
@@ -280,7 +278,7 @@ void R_BuildLightMap (msurface_t *surf, byte *dest, int stride)
        lightmap = surf->samples;
 
 // set to full bright if no light data
-       if ((currententity && (currententity->effects & EF_FULLBRIGHT)) || !cl.worldmodel->lightdata)
+       if ((currententity && (currententity->render.effects & EF_FULLBRIGHT)) || !cl.worldmodel->lightdata)
        {
                bl = blocklights;
                for (i=0 ; i<size ; i++)
@@ -315,7 +313,7 @@ void R_BuildLightMap (msurface_t *surf, byte *dest, int stride)
                                        *bl++ += *lightmap++ * scale;
                        }
                }
-               if (r_dlightmap.value && surf->dlightframe == r_dlightframecount)
+               if (r_dlightmap.value && surf->dlightframe == r_framecount)
                        if ((surf->cached_dlight = R_AddDynamicLights(surf)))
                                c_light_polys++;
        }
@@ -370,11 +368,11 @@ Returns the proper texture for a given time and base texture
 */
 texture_t *R_TextureAnimation (texture_t *base)
 {
-       texture_t *original;
-       int             relative;
-       int             count;
+//     texture_t *original;
+//     int             relative;
+//     int             count;
 
-       if (currententity->frame)
+       if (currententity->render.frame)
        {
                if (base->alternate_anims)
                        base = base->alternate_anims;
@@ -383,9 +381,12 @@ texture_t *R_TextureAnimation (texture_t *base)
        if (!base->anim_total)
                return base;
 
+       return base->anim_frames[(int)(cl.time*5) % base->anim_total];
+
+       /*
        original = base;
 
-       relative = (int)(cl.time*10) % base->anim_total;
+       relative = (int)(cl.time*5) % base->anim_total;
 
        count = 0;      
        while (base->anim_min > relative || base->anim_max <= relative)
@@ -404,6 +405,7 @@ texture_t *R_TextureAnimation (texture_t *base)
        }
 
        return base;
+       */
 }
 
 
@@ -416,12 +418,6 @@ texture_t *R_TextureAnimation (texture_t *base)
 */
 
 
-extern int             solidskytexture;
-extern int             alphaskytexture;
-extern float   speedscale;             // for top sky and bottom sky
-
-extern char skyname[];
-
 float  turbsin[256] =
 {
        #include "gl_warp_sin.h"
@@ -429,7 +425,7 @@ float       turbsin[256] =
 #define TURBSCALE (256.0 / (2 * M_PI))
 
 
-void UploadLightmaps()
+void UploadLightmaps(void)
 {
        int i;
        if (nosubimage || nosubimagefragments)
@@ -470,6 +466,11 @@ void RSurf_DrawSky(msurface_t *s, int transform)
        glpoly_t *p;
        int i;
        float *v;
+
+       // LordHavoc: HalfLife maps have freaky skypolys...
+       if (hlbsp)
+               return;
+
        for (p=s->polys ; p ; p=p->next)
        {
                if (currentskypoly < MAX_SKYPOLYS && currentskyvert + p->numverts <= MAX_SKYVERTS)
@@ -547,12 +548,12 @@ int RSurf_Light(int *dlightbits, glpoly_t *polys)
 void RSurf_DrawWater(msurface_t *s, texture_t *t, int transform, int alpha)
 {
        int             i;
-       float   os = turbsin[(int)(realtime * TURBSCALE) & 255], ot = turbsin[(int)(cl.time * TURBSCALE + 96.0) & 255];
+       float   os = turbsin[(int)(cl.time * TURBSCALE) & 255], ot = turbsin[(int)(cl.time * TURBSCALE + 96.0) & 255];
        glpoly_t *p;
        float   *v;
        // FIXME: make fog texture if water texture is transparent?
 
-       if (s->dlightframe != r_dlightframecount)
+       if (s->dlightframe != r_framecount)
        {
                vec3_t temp;
                // LordHavoc: fast path for no vertex lighting cases
@@ -566,7 +567,7 @@ void RSurf_DrawWater(msurface_t *s, texture_t *t, int transform, int alpha)
                                        for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
                                        {
                                                softwaretransform(v, temp);
-                                               transpolyvert(temp[0], temp[1], temp[2] + r_waterripple.value * turbsin[(int)((temp[0]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255] * turbsin[(int)((temp[1]*(1.0f/32.0f)+realtime) * TURBSCALE) & 255] * (1.0f / 64.0f), (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), 128, 128, 128, alpha);
+                                               transpolyvert(temp[0], temp[1], temp[2] + r_waterripple.value * turbsin[(int)((temp[0]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255] * turbsin[(int)((temp[1]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255] * (1.0f / 64.0f), (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), 128, 128, 128, alpha);
                                        }
                                        transpolyend();
                                }
@@ -593,7 +594,7 @@ void RSurf_DrawWater(msurface_t *s, texture_t *t, int transform, int alpha)
                                {
                                        transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
                                        for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
-                                               transpolyvert(v[0], v[1], v[2] + r_waterripple.value * turbsin[(int)((v[0]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255] * turbsin[(int)((v[1]*(1.0f/32.0f)+realtime) * TURBSCALE) & 255] * (1.0f / 64.0f), (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), 128, 128, 128, alpha);
+                                               transpolyvert(v[0], v[1], v[2] + r_waterripple.value * turbsin[(int)((v[0]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255] * turbsin[(int)((v[1]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255] * (1.0f / 64.0f), (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), 128, 128, 128, alpha);
                                        transpolyend();
                                }
                        }
@@ -622,12 +623,12 @@ void RSurf_DrawWater(msurface_t *s, texture_t *t, int transform, int alpha)
                                else
                                        VectorCopy(v, wv);
                                if (r_waterripple.value)
-                                       wv[2] += r_waterripple.value * turbsin[(int)((wv[0]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255] * turbsin[(int)((wv[1]*(1.0f/32.0f)+realtime) * TURBSCALE) & 255] * (1.0f / 64.0f);
+                                       wv[2] += r_waterripple.value * turbsin[(int)((wv[0]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255] * turbsin[(int)((wv[1]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255] * (1.0f / 64.0f);
                                wv[3] = wv[4] = wv[5] = 128.0f;
                                wv += 6;
                        }
                }
-               if (s->dlightframe == r_dlightframecount)
+               if (s->dlightframe == r_framecount)
                        RSurf_Light(s->dlightbits, s->polys);
                wv = wvert;
                for (p=s->polys ; p ; p=p->next)
@@ -650,7 +651,7 @@ void RSurf_DrawWall(msurface_t *s, texture_t *t, int transform)
        wallvertcolor_t *outcolor;
        // check for lightmap modification
        if (s->cached_dlight
-        || (r_dynamic.value && r_dlightmap.value && s->dlightframe == r_dlightframecount)
+        || (r_dynamic.value && r_dlightmap.value && s->dlightframe == r_framecount)
         || r_ambient.value != s->cached_ambient
         || lighthalf != s->cached_lighthalf
         || (r_dynamic.value
@@ -659,7 +660,7 @@ void RSurf_DrawWall(msurface_t *s, texture_t *t, int transform)
         || (s->styles[2] != 255 && d_lightstylevalue[s->styles[2]] != s->cached_light[2])
         || (s->styles[3] != 255 && d_lightstylevalue[s->styles[3]] != s->cached_light[3]))))
                R_UpdateLightmap(s, s->lightmaptexturenum);
-       if (r_dlightmap.value || s->dlightframe != r_dlightframecount)
+       if (r_dlightmap.value || s->dlightframe != r_framecount)
        {
                // LordHavoc: fast path version for no vertex lighting cases
                wp = &wallpoly[currentwallpoly];
@@ -726,7 +727,7 @@ void RSurf_DrawWall(msurface_t *s, texture_t *t, int transform)
                }
                if ((currentwallpoly + polys > MAX_WALLPOLYS) || (currentwallvert+verts > MAX_WALLVERTS))
                        return;
-               if ((!r_dlightmap.value) && s->dlightframe == r_dlightframecount)
+               if ((!r_dlightmap.value) && s->dlightframe == r_framecount)
                        lit = RSurf_Light(s->dlightbits, s->polys);
                wv = wvert;
                wp = &wallpoly[currentwallpoly];
@@ -776,8 +777,6 @@ void RSurf_DrawWall(msurface_t *s, texture_t *t, int transform)
 }
 
 // LordHavoc: transparent brush models
-extern float modelalpha;
-
 void RSurf_DrawWallVertex(msurface_t *s, texture_t *t, int transform, int isbmodel)
 {
        int i, alpha, size3;
@@ -816,17 +815,17 @@ void RSurf_DrawWallVertex(msurface_t *s, texture_t *t, int transform, int isbmod
                        wv += 6;
                }
        }
-       if (s->dlightframe == r_dlightframecount)
+       if (s->dlightframe == r_framecount)
                RSurf_Light(s->dlightbits, s->polys);
        wv = wvert;
-       if (isbmodel && (currententity->colormod[0] != 1 || currententity->colormod[1] != 1 || currententity->colormod[2] != 1))
+       if (isbmodel && (currententity->render.colormod[0] != 1 || currententity->render.colormod[1] != 1 || currententity->render.colormod[2] != 1))
        {
                for (p = s->polys;p;p = p->next)
                {
                        v = p->verts[0];
-                       transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
+                       transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, currententity->render.effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
                        for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
-                               transpolyvert(wv[0], wv[1], wv[2], v[3], v[4], wv[3] * currententity->colormod[0], wv[4] * currententity->colormod[1], wv[5] * currententity->colormod[2], alpha);
+                               transpolyvert(wv[0], wv[1], wv[2], v[3], v[4], wv[3] * currententity->render.colormod[0], wv[4] * currententity->render.colormod[1], wv[5] * currententity->render.colormod[2], alpha);
                        transpolyend();
                }
        }
@@ -835,7 +834,7 @@ void RSurf_DrawWallVertex(msurface_t *s, texture_t *t, int transform, int isbmod
                for (p = s->polys;p;p = p->next)
                {
                        v = p->verts[0];
-                       transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
+                       transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, currententity->render.effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
                        for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
                                transpolyvert(wv[0], wv[1], wv[2], v[3], v[4], wv[3], wv[4], wv[5], alpha);
                        transpolyend();
@@ -843,76 +842,6 @@ void RSurf_DrawWallVertex(msurface_t *s, texture_t *t, int transform, int isbmod
        }
 }
 
-/*
-================
-DrawTextureChains
-================
-*/
-extern qboolean hlbsp;
-extern char skyname[];
-void R_DrawSurf(msurface_t *s, int isbmodel, int vertexlit)
-{
-       texture_t *t;
-       if (s->flags & SURF_DRAWSKY)
-       {
-               skyisvisible = true;
-               if (!hlbsp) // LordHavoc: HalfLife maps have freaky skypolys...
-                       RSurf_DrawSky(s, false);
-               return;
-       }
-       t = R_TextureAnimation (s->texinfo->texture);
-       if (s->flags & SURF_DRAWTURB)
-       {
-               RSurf_DrawWater(s, t, false, s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f);
-               return;
-       }
-       if (vertexlit)
-               RSurf_DrawWallVertex(s, t, false, false);
-       else
-               RSurf_DrawWall(s, t, false);
-}
-
-void DrawTextureChains (void)
-{
-       int                     n;
-       msurface_t      *s;
-       texture_t       *t;
-
-       for (n = 0;n < cl.worldmodel->numtextures;n++)
-       {
-               if (!cl.worldmodel->textures[n] || !(s = cl.worldmodel->textures[n]->texturechain))
-                       continue;
-               cl.worldmodel->textures[n]->texturechain = NULL;
-//             for (;s;s = s->texturechain)
-//                     R_DrawSurf(s, false, gl_vertex.value);
-               // LordHavoc: decide the render type only once, because the surface properties were determined by texture anyway
-               // sky
-               if (s->flags & SURF_DRAWSKY)
-               {
-                       skyisvisible = true;
-                       if (!hlbsp) // LordHavoc: HalfLife maps have freaky skypolys...
-                               for (;s;s = s->texturechain)
-                                       RSurf_DrawSky(s, false);
-                       continue;
-               }
-               t = R_TextureAnimation (cl.worldmodel->textures[n]);
-               // subdivided water surface warp
-               if (s->flags & SURF_DRAWTURB)
-               {
-                       int alpha = s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f;
-                       for (;s;s = s->texturechain)
-                               RSurf_DrawWater(s, t, false, alpha);
-                       continue;
-               }
-               if (gl_vertex.value)
-                       for (;s;s = s->texturechain)
-                               RSurf_DrawWallVertex(s, t, false, false);
-               else
-                       for (;s;s = s->texturechain)
-                               RSurf_DrawWall(s, t, false);
-       }
-}
-
 void R_NoVisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, model_t *model);
 
 /*
@@ -927,48 +856,53 @@ void R_DrawBrushModel (entity_t *e)
        msurface_t      *s;
        model_t         *clmodel;
        int     rotated, vertexlit = false;
-       texture_t       *t;
        vec3_t          org;
 
        currententity = e;
 
-       clmodel = e->model;
+       clmodel = e->render.model;
 
-       if (e->angles[0] || e->angles[1] || e->angles[2])
+       if (e->render.angles[0] || e->render.angles[1] || e->render.angles[2])
        {
                rotated = true;
                for (i=0 ; i<3 ; i++)
                {
-                       mins[i] = e->origin[i] - clmodel->radius;
-                       maxs[i] = e->origin[i] + clmodel->radius;
+                       mins[i] = e->render.origin[i] - clmodel->radius;
+                       maxs[i] = e->render.origin[i] + clmodel->radius;
                }
        }
        else
        {
                rotated = false;
-               VectorAdd (e->origin, clmodel->mins, mins);
-               VectorAdd (e->origin, clmodel->maxs, maxs);
+               VectorAdd (e->render.origin, clmodel->mins, mins);
+               VectorAdd (e->render.origin, clmodel->maxs, maxs);
        }
 
-       if (R_CullBox (mins, maxs))
+       if (R_VisibleCullBox (mins, maxs))
                return;
 
        c_bmodels++;
 
-       VectorSubtract (r_refdef.vieworg, e->origin, modelorg);
+       VectorSubtract (r_origin, e->render.origin, modelorg);
        if (rotated)
        {
                vec3_t  temp;
                vec3_t  forward, right, up;
 
                VectorCopy (modelorg, temp);
-               AngleVectors (e->angles, forward, right, up);
+               AngleVectors (e->render.angles, forward, right, up);
                modelorg[0] = DotProduct (temp, forward);
                modelorg[1] = -DotProduct (temp, right);
                modelorg[2] = DotProduct (temp, up);
        }
 
-       s = &clmodel->surfaces[clmodel->firstmodelsurface];
+       for (i = 0, s = &clmodel->surfaces[clmodel->firstmodelsurface];i < clmodel->nummodelsurfaces;i++, s++)
+       {
+               if (((s->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, s->plane) >= 0))
+                       s->visframe = r_framecount;
+               else
+                       s->visframe = -1;
+       }
 
 // calculate dynamic lighting for bmodel if it's not an
 // instanced model
@@ -977,36 +911,37 @@ void R_DrawBrushModel (entity_t *e)
                if (!cl_dlights[i].radius)
                        continue;
 
-               VectorSubtract(cl_dlights[i].origin, currententity->origin, org);
+               VectorSubtract(cl_dlights[i].origin, currententity->render.origin, org);
                R_NoVisMarkLights (org, &cl_dlights[i], 1<<(i&31), i >> 5, clmodel);
        }
-       vertexlit = modelalpha != 1 || clmodel->firstmodelsurface == 0 || (currententity->effects & EF_FULLBRIGHT) || currententity->colormod[0] != 1 || currententity->colormod[2] != 1 || currententity->colormod[2] != 1;
+       vertexlit = modelalpha != 1 || clmodel->firstmodelsurface == 0 || (currententity->render.effects & EF_FULLBRIGHT) || currententity->render.colormod[0] != 1 || currententity->render.colormod[2] != 1 || currententity->render.colormod[2] != 1;
 
-e->angles[0] = -e->angles[0];  // stupid quake bug
+       e->render.angles[0] = -e->render.angles[0];     // stupid quake bug
        softwaretransformforentity (e);
-e->angles[0] = -e->angles[0];  // stupid quake bug
+       e->render.angles[0] = -e->render.angles[0];     // stupid quake bug
 
        // draw texture
-       for (i = 0;i < clmodel->nummodelsurfaces;i++, s++)
+       for (i = 0, s = &clmodel->surfaces[clmodel->firstmodelsurface];i < clmodel->nummodelsurfaces;i++, s++)
        {
-               if (((s->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, s->plane) >= 0))
+               if (s->visframe == r_framecount)
                {
 //                     R_DrawSurf(s, true, vertexlit || s->texinfo->texture->transparent);
-                       if (s->flags & SURF_DRAWSKY)
+                       if (s->flags & (SURF_DRAWSKY | SURF_DRAWTURB))
                        {
-                               RSurf_DrawSky(s, true);
-                               continue;
+                               // sky and liquid don't need sorting (skypoly/transpoly)
+                               if (s->flags & SURF_DRAWSKY)
+                                       RSurf_DrawSky(s, true);
+                               else
+                                       RSurf_DrawWater(s, R_TextureAnimation(s->texinfo->texture), true, s->flags & SURF_DRAWNOALPHA ? 255 : wateralpha);
                        }
-                       t = R_TextureAnimation (s->texinfo->texture);
-                       if (s->flags & SURF_DRAWTURB)
+                       else
                        {
-                               RSurf_DrawWater(s, t, true, s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f);
-                               continue;
+                               texture_t *t = R_TextureAnimation(s->texinfo->texture);
+                               if (vertexlit || s->texinfo->texture->transparent)
+                                       RSurf_DrawWallVertex(s, t, true, true);
+                               else
+                                       RSurf_DrawWall(s, t, true);
                        }
-                       if (vertexlit || s->texinfo->texture->transparent)
-                               RSurf_DrawWallVertex(s, t, true, true);
-                       else
-                               RSurf_DrawWall(s, t, true);
                }
        }
        UploadLightmaps();
@@ -1020,26 +955,36 @@ e->angles[0] = -e->angles[0];    // stupid quake bug
 =============================================================
 */
 
-void R_StoreEfrags (efrag_t **ppefrag);
+static byte *worldvis;
 
-void R_NewWorldNode ()
+void R_MarkLeaves (void)
 {
-       int l, texsort = gl_texsort.value, vertex = gl_vertex.value;
+       static float noviscache;
+       if (r_oldviewleaf == r_viewleaf && noviscache == r_novis.value)
+               return;
+
+       r_oldviewleaf = r_viewleaf;
+       noviscache = r_novis.value;
+
+       worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
+}
+
+void R_SolidWorldNode (void)
+{
+       int l;
        mleaf_t *leaf;
        msurface_t *surf, **mark, **endmark;
 
        for (l = 0, leaf = cl.worldmodel->leafs;l < cl.worldmodel->numleafs;l++, leaf++)
        {
-               if ((leaf->visframe == r_visframecount) && (leaf->efrags || leaf->nummarksurfaces))
+               if (leaf->nummarksurfaces)
                {
-                       if (R_CullBox(leaf->minmaxs, leaf->minmaxs+3))
+                       if (R_CullBox(leaf->mins, leaf->maxs))
                                continue;
 
                        c_leafs++;
 
-                       // deal with model fragments in this leaf
-                       if (leaf->efrags)
-                               R_StoreEfrags (&leaf->efrags);
+                       leaf->visframe = r_framecount;
 
                        if (leaf->nummarksurfaces)
                        {
@@ -1054,33 +999,13 @@ void R_NewWorldNode ()
                                        surf->worldnodeframe = r_framecount;
                                        if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
                                        {
-                                               if ( (surf->flags & SURF_PLANEBACK))
-                                               {
+                                               if (surf->flags & SURF_PLANEBACK)
                                                        surf->visframe = r_framecount;
-                                                       c_faces++;
-                                                       if (texsort)
-                                                       {
-                                                               surf->texturechain = surf->texinfo->texture->texturechain;
-                                                               surf->texinfo->texture->texturechain = surf;
-                                                       }
-                                                       else
-                                                               R_DrawSurf(surf, false, vertex);
-                                               }
                                        }
                                        else
                                        {
                                                if (!(surf->flags & SURF_PLANEBACK))
-                                               {
                                                        surf->visframe = r_framecount;
-                                                       c_faces++;
-                                                       if (texsort)
-                                                       {
-                                                               surf->texturechain = surf->texinfo->texture->texturechain;
-                                                               surf->texinfo->texture->texturechain = surf;
-                                                       }
-                                                       else
-                                                               R_DrawSurf(surf, false, vertex);
-                                               }
                                        }
                                }
                                while (mark < endmark);
@@ -1089,205 +1014,230 @@ void R_NewWorldNode ()
        }
 }
 
-struct nodestack_s
-{
-       int side;
-       mnode_t *node;
-       int noclipping;
-} nodestack[8192];
-
 /*
-================
-R_WorldNode
-================
-*/
-void R_WorldNode ()
+// experimental and inferior to the other in recursion depth allowances
+void R_PortalWorldNode (void)
 {
-       int side, texsort = gl_texsort.value, vertex = gl_vertex.value, ca, cb, cc, cd, noclipping = false, oldclip = r_oldclip.value;
-       struct nodestack_s *nstack;
-       mnode_t *node;
-       mleaf_t *pleaf;
-       msurface_t *surf, *endsurf, **mark, **endmark;
-       nstack = nodestack;
-
-       if (!(node = cl.worldmodel->nodes))
-               return;
+       int i, j;
+       mportal_t *p;
+       msurface_t *surf, **mark, **endmark;
+       mleaf_t *leaf, *llistbuffer[8192], **l, **llist;
 
-       while(1)
+       leaf = r_viewleaf;
+       leaf->worldnodeframe = r_framecount;
+       l = llist = &llistbuffer[0];
+       *llist++ = r_viewleaf;
+       while (l < llist)
        {
-               if (oldclip)
+               leaf = *l++;
+
+               c_leafs++;
+
+               leaf->visframe = r_framecount;
+
+               if (leaf->nummarksurfaces)
                {
-                       if (R_CullBox(node->minmaxs, node->minmaxs+3))
+                       mark = leaf->firstmarksurface;
+                       endmark = mark + leaf->nummarksurfaces;
+                       do
                        {
-backupstack:
-                               if (nstack <= nodestack)
-                                       break;
-                               nstack--;
-                               node = nstack->node;
-                               side = nstack->side;
-                               noclipping = nstack->noclipping;
-                               goto loc0;
+                               surf = *mark++;
+                               // make sure surfaces are only processed once
+                               if (surf->worldnodeframe == r_framecount)
+                                       continue;
+                               surf->worldnodeframe = r_framecount;
+                               if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
+                               {
+                                       if (surf->flags & SURF_PLANEBACK)
+                                               surf->visframe = r_framecount;
+                               }
+                               else
+                               {
+                                       if (!(surf->flags & SURF_PLANEBACK))
+                                               surf->visframe = r_framecount;
+                               }
                        }
+                       while (mark < endmark);
                }
-               else
-               if (!noclipping)
-               {
-                       ca = frustum[0].BoxOnPlaneSideFunc(node->minmaxs, node->minmaxs+3, &frustum[0]);if (ca == 2) goto backupstack; // completely clipped away
-                       cb = frustum[1].BoxOnPlaneSideFunc(node->minmaxs, node->minmaxs+3, &frustum[1]);if (cb == 2) goto backupstack; // completely clipped away
-                       cc = frustum[2].BoxOnPlaneSideFunc(node->minmaxs, node->minmaxs+3, &frustum[2]);if (cc == 2) goto backupstack; // completely clipped away
-                       cd = frustum[3].BoxOnPlaneSideFunc(node->minmaxs, node->minmaxs+3, &frustum[3]);if (cd == 2) goto backupstack; // completely clipped away
-                       if (ca == 0 && cb == 0 && cc == 0 && cd == 0)
-                               noclipping = true; // not clipped at all, no need to clip any children of this node
-                       // partially clipped node
-               }
-       // if a leaf node, draw stuff
-               if (node->contents < 0)
+
+               // follow portals into other leafs
+               p = leaf->portals;
+               for (;p;p = p->next)
                {
-                       if (node->contents != CONTENTS_SOLID)
+                       leaf = p->past;
+                       if (leaf->worldnodeframe != r_framecount)
                        {
-                               pleaf = (mleaf_t *)node;
-
-                               c_leafs++;
-                               if (pleaf->nummarksurfaces)
-                               {
-                                       mark = pleaf->firstmarksurface;
-                                       endmark = mark + pleaf->nummarksurfaces;
-                                       do
-                                       {
-                                               (*mark)->visframe = r_framecount;
-                                               mark++;
-                                       }
-                                       while (mark < endmark);
-                               }
-
-                               // deal with model fragments in this leaf
-                               if (pleaf->efrags)
-                                       R_StoreEfrags (&pleaf->efrags);
+                               leaf->worldnodeframe = r_framecount;
+                               i = (leaf - cl.worldmodel->leafs) - 1;
+                               if ((worldvis[i>>3] & (1<<(i&7))) && R_NotCulledBox(leaf->mins, leaf->maxs))
+                                       *llist++ = leaf;
                        }
-
-                       if (nstack <= nodestack)
-                               break;
-                       nstack--;
-                       node = nstack->node;
-                       side = nstack->side;
-                       noclipping = nstack->noclipping;
-                       goto loc0;
                }
+       }
+
+       i = 0;
+       j = 0;
+       p = r_viewleaf->portals;
+       for (;p;p = p->next)
+       {
+               j++;
+               if (p->past->worldnodeframe != r_framecount)
+                       i++;
+       }
+       if (i)
+               Con_Printf("%i portals of viewleaf (%i portals) were not checked\n", i, j);
+}
+*/
 
-               c_nodes++;
+void R_PortalWorldNode (void)
+{
+       int portalstack, i;
+       mportal_t *p, *pstack[8192];
+       msurface_t *surf, **mark, **endmark;
+       mleaf_t *leaf;
 
-               // node is just a decision point, so go down the apropriate sides
+       leaf = r_viewleaf;
+       leaf->worldnodeframe = r_framecount;
+       portalstack = 0;
+loc0:
+       c_leafs++;
 
-               // find which side of the node we are on
-               side = PlaneDist(modelorg, node->plane) < node->plane->dist;
+       leaf->visframe = r_framecount;
 
-               // recurse down the children, front side first
-               if (node->children[side]->visframe == r_visframecount)
+       if (leaf->nummarksurfaces)
+       {
+               mark = leaf->firstmarksurface;
+               endmark = mark + leaf->nummarksurfaces;
+               do
                {
-                       nstack->node = node;
-                       nstack->side = !side; // go down back side when we come back up
-                       nstack->noclipping = noclipping;
-                       nstack++;
-                       node = node->children[side];
-                       continue;
+                       surf = *mark++;
+                       // make sure surfaces are only processed once
+                       if (surf->worldnodeframe == r_framecount)
+                               continue;
+                       surf->worldnodeframe = r_framecount;
+                       if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
+                       {
+                               if (surf->flags & SURF_PLANEBACK)
+                                       surf->visframe = r_framecount;
+                       }
+                       else
+                       {
+                               if (!(surf->flags & SURF_PLANEBACK))
+                                       surf->visframe = r_framecount;
+                       }
                }
-               side = !side;
-loc0:
+               while (mark < endmark);
+       }
 
-       // draw stuff
-               if (node->numsurfaces)
+       // follow portals into other leafs
+       p = leaf->portals;
+       for (;p;p = p->next)
+       {
+               leaf = p->past;
+               if (leaf->worldnodeframe != r_framecount)
                {
-                       surf = cl.worldmodel->surfaces + node->firstsurface;
-                       endsurf = surf + node->numsurfaces;
-
-                       if (texsort)
+                       leaf->worldnodeframe = r_framecount;
+                       if (leaf->contents != CONTENTS_SOLID)
                        {
-                               if (side)
+                               i = (leaf - cl.worldmodel->leafs) - 1;
+                               if (worldvis[i>>3] & (1<<(i&7)))
                                {
-                                       do
+                                       if (R_NotCulledBox(leaf->mins, leaf->maxs))
                                        {
-                                               if (surf->visframe == r_framecount && !(surf->flags & SURF_PLANEBACK))
-                                               {
-                                                       c_faces++;
-                                                       surf->texturechain = surf->texinfo->texture->texturechain;
-                                                       surf->texinfo->texture->texturechain = surf;
-                                               }
-                                               else
-                                                       surf->visframe = -1; // LordHavoc: mark as not visible, so lighting will not touch it
-                                               surf++;
+                                               pstack[portalstack++] = p;
+                                               goto loc0;
+
+loc1:
+                                               p = pstack[--portalstack];
                                        }
-                                       while (surf < endsurf);
                                }
+                       }
+               }
+       }
+
+       if (portalstack)
+               goto loc1;
+
+       i = 0;
+       portalstack = 0;
+       p = r_viewleaf->portals;
+       for (;p;p = p->next)
+       {
+               portalstack++;
+               if (p->past->worldnodeframe != r_framecount)
+                       i++;
+       }
+       if (i)
+               Con_Printf("%i portals of viewleaf (%i portals) were not checked\n", i, portalstack);
+}
+
+void R_DrawSurfaces (void)
+{
+       msurface_t      *surf, *endsurf;
+       texture_t       *t;
+       int vertex = gl_vertex.value;
+
+       surf = &cl.worldmodel->surfaces[cl.worldmodel->firstmodelsurface];
+       endsurf = surf + cl.worldmodel->nummodelsurfaces;
+       for (;surf < endsurf;surf++)
+       {
+               if (surf->visframe == r_framecount)
+               {
+                       c_faces++;
+                       if (surf->flags & (SURF_DRAWSKY | SURF_DRAWTURB))
+                       {
+                               // sky and liquid don't need sorting (skypoly/transpoly)
+                               if (surf->flags & SURF_DRAWSKY)
+                                       RSurf_DrawSky(surf, false);
                                else
-                               {
-                                       do
-                                       {
-                                               if (surf->visframe == r_framecount && (surf->flags & SURF_PLANEBACK))
-                                               {
-                                                       c_faces++;
-                                                       surf->texturechain = surf->texinfo->texture->texturechain;
-                                                       surf->texinfo->texture->texturechain = surf;
-                                               }
-                                               else
-                                                       surf->visframe = -1; // LordHavoc: mark as not visible, so lighting will not touch it
-                                               surf++;
-                                       }
-                                       while (surf < endsurf);
-                               }
+                                       RSurf_DrawWater(surf, R_TextureAnimation(surf->texinfo->texture), false, surf->flags & SURF_DRAWNOALPHA ? 255 : wateralpha);
                        }
                        else
                        {
-                               if (side)
-                               {
-                                       do
-                                       {
-                                               if (surf->visframe == r_framecount && !(surf->flags & SURF_PLANEBACK))
-                                               {
-                                                       c_faces++;
-                                                       R_DrawSurf(surf, false, vertex);
-                                               }
-                                               else
-                                                       surf->visframe = -1; // LordHavoc: mark as not visible, so lighting will not touch it
-                                               surf++;
-                                       }
-                                       while (surf < endsurf);
-                               }
+                               t = R_TextureAnimation(surf->texinfo->texture);
+                               if (vertex)
+                                       RSurf_DrawWallVertex(surf, t, false, false);
                                else
-                               {
-                                       do
-                                       {
-                                               if (surf->visframe == r_framecount && (surf->flags & SURF_PLANEBACK))
-                                               {
-                                                       c_faces++;
-                                                       R_DrawSurf(surf, false, vertex);
-                                               }
-                                               else
-                                                       surf->visframe = -1; // LordHavoc: mark as not visible, so lighting will not touch it
-                                               surf++;
-                                       }
-                                       while (surf < endsurf);
-                               }
+                                       RSurf_DrawWall(surf, t, false);
                        }
                }
+       }
+}
 
-       // recurse down the back side
-               if (node->children[side]->visframe == r_visframecount)
+void R_DrawPortals(void)
+{
+       int drawportals, i, r, g, b;
+       mleaf_t *leaf, *endleaf;
+       mportal_t *portal;
+       mvertex_t *point, *endpoint;
+       drawportals = (int)r_drawportals.value;
+       if (drawportals < 1)
+               return;
+       leaf = cl.worldmodel->leafs;
+       endleaf = leaf + cl.worldmodel->numleafs;
+       for (;leaf < endleaf;leaf++)
+       {
+               if (leaf->visframe == r_framecount && leaf->portals)
                {
-                       node = node->children[side];
-                       continue;
+                       i = leaf - cl.worldmodel->leafs;
+                       r = (i & 0x0007) << 5;
+                       g = (i & 0x0038) << 2;
+                       b = (i & 0x01C0) >> 1;
+                       portal = leaf->portals;
+                       while (portal)
+                       {
+                               transpolybegin(0, 0, 0, TPOLYTYPE_ALPHA);
+                               point = portal->points + portal->numpoints - 1;
+                               endpoint = portal->points;
+                               for (;point >= endpoint;point--)
+                                       transpolyvertub(point->position[0], point->position[1], point->position[2], 0, 0, r, g, b, 32);
+                               transpolyend();
+                               portal = portal->next;
+                       }
                }
-
-               if (nstack <= nodestack)
-                       break;
-               nstack--;
-               node = nstack->node;
-               side = nstack->side;
-               noclipping = nstack->noclipping;
-               goto loc0;
        }
 }
 
-
 /*
 =============
 R_DrawWorld
@@ -1297,13 +1247,15 @@ void R_DrawWorld (void)
 {
        entity_t        ent;
 
+       wateralpha = bound(0, r_wateralpha.value*255.0f, 255);
+
        memset (&ent, 0, sizeof(ent));
-       ent.model = cl.worldmodel;
-       ent.colormod[0] = ent.colormod[1] = ent.colormod[2] = 1;
-       modelalpha = ent.alpha = 1;
-       ent.scale = 1;
+       ent.render.model = cl.worldmodel;
+       ent.render.colormod[0] = ent.render.colormod[1] = ent.render.colormod[2] = 1;
+       modelalpha = ent.render.alpha = 1;
+       ent.render.scale = 1;
 
-       VectorCopy (r_refdef.vieworg, modelorg);
+       VectorCopy (r_origin, modelorg);
 
        currententity = &ent;
 
@@ -1311,72 +1263,22 @@ void R_DrawWorld (void)
 
        if (cl.worldmodel)
        {
-               if (r_newworldnode.value)
-                       R_NewWorldNode ();
+               if (r_viewleaf->contents == CONTENTS_SOLID)
+                       R_SolidWorldNode ();
                else
-                       R_WorldNode ();
+               {
+                       R_MarkLeaves ();
+                       R_PortalWorldNode ();
+               }
        }
 
        R_PushDlights (); // now mark the lit surfaces
 
-       DrawTextureChains ();
-}
+       R_DrawSurfaces ();
 
-
-/*
-===============
-R_MarkLeaves
-===============
-*/
-void R_MarkLeaves (void)
-{
-       byte    *vis;
-       mnode_t *node;
-       int             i;
-
-       if (r_oldviewleaf == r_viewleaf && !r_novis.value)
-               return;
-       
-       r_visframecount++;
-       r_oldviewleaf = r_viewleaf;
-
-       if (r_novis.value)
-       {
-               for (i=0 ; i<cl.worldmodel->numleafs ; i++)
-               {
-                       node = (mnode_t *)&cl.worldmodel->leafs[i+1];
-                       do
-                       {
-                               if (node->visframe == r_visframecount)
-                                       break;
-                               node->visframe = r_visframecount;
-                               node = node->parent;
-                       } while (node);
-               }
-       }
-       else
-       {
-               vis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
-               
-               for (i=0 ; i<cl.worldmodel->numleafs ; i++)
-               {
-                       if (vis[i>>3] & (1<<(i&7)))
-                       {
-                               node = (mnode_t *)&cl.worldmodel->leafs[i+1];
-                               do
-                               {
-                                       if (node->visframe == r_visframecount)
-                                               break;
-                                       node->visframe = r_visframecount;
-                                       node = node->parent;
-                               } while (node);
-                       }
-               }
-       }
+       R_DrawPortals ();
 }
 
-
-
 /*
 =============================================================================
 
@@ -1392,11 +1294,11 @@ int AllocBlock (int w, int h, short *x, short *y)
        int             best, best2;
        int             texnum;
 
-       for (texnum=0 ; texnum<MAX_LIGHTMAPS ; texnum++)
+       for (texnum = 0;texnum < MAX_LIGHTMAPS;texnum++)
        {
                best = BLOCK_HEIGHT;
 
-               for (i=0 ; i<BLOCK_WIDTH-w ; i+=lightmapalign) // LordHavoc: NVIDIA has broken subimage, so align the lightmaps
+               for (i = 0;i < BLOCK_WIDTH - w;i += lightmapalign) // LordHavoc: NVIDIA has broken subimage, so align the lightmaps
                {
                        best2 = 0;
 
@@ -1442,7 +1344,7 @@ int AllocBlock (int w, int h, short *x, short *y)
                        }
                }
 
-               for (i=0 ; i<w ; i++)
+               for (i = 0;i < w;i++)
                        allocated[texnum][*x + i] = best + h;
 
                return texnum;