]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_rsurf.c
removed support for r_ser (also known as R_Clip or "Hidden Surface Removal") as it...
[xonotic/darkplaces.git] / gl_rsurf.c
index edfe4cdbcd4e6b7831eafe783cf2439f696512a6..a9a6cb55ae91d9049143fbe093a58cd1c0b41c2e 100644 (file)
@@ -23,7 +23,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 #define MAX_LIGHTMAP_SIZE 256
 
-static signed int blocklights[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*3]; // LordHavoc: *3 for colored lighting
+static unsigned int intblocklights[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*3]; // LordHavoc: *3 for colored lighting
+static float floatblocklights[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*3]; // LordHavoc: *3 for colored lighting
 
 static qbyte templight[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*4];
 
@@ -32,43 +33,15 @@ cvar_t r_vertexsurfaces = {0, "r_vertexsurfaces", "0"};
 cvar_t r_dlightmap = {CVAR_SAVE, "r_dlightmap", "1"};
 cvar_t r_drawportals = {0, "r_drawportals", "0"};
 cvar_t r_testvis = {0, "r_testvis", "0"};
-
-static void gl_surf_start(void)
-{
-}
-
-static void gl_surf_shutdown(void)
-{
-}
-
-static void gl_surf_newmap(void)
-{
-}
+cvar_t r_floatbuildlightmap = {0, "r_floatbuildlightmap", "0"};
 
 static int dlightdivtable[32768];
 
-void GL_Surf_Init(void)
-{
-       int i;
-       dlightdivtable[0] = 4194304;
-       for (i = 1;i < 32768;i++)
-               dlightdivtable[i] = 4194304 / (i << 7);
-
-       Cvar_RegisterVariable(&r_ambient);
-       Cvar_RegisterVariable(&r_vertexsurfaces);
-       Cvar_RegisterVariable(&r_dlightmap);
-       Cvar_RegisterVariable(&r_drawportals);
-       Cvar_RegisterVariable(&r_testvis);
-
-       R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);
-}
-
-static int R_AddDynamicLights (msurface_t *surf)
+static int R_IntAddDynamicLights (msurface_t *surf)
 {
-       int         sdtable[256], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, red, green, blue, lit, dist2, impacts, impactt, subtract;
+       int sdtable[256], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, red, green, blue, lit, dist2, impacts, impactt, subtract;
        unsigned int *bl;
-       float       dist;
-       vec3_t      impact, local;
+       float dist, impact[3], local[3];
 
        // LordHavoc: use 64bit integer...  shame it's not very standardized...
 #if _MSC_VER || __BORLANDC__
@@ -81,6 +54,7 @@ static int R_AddDynamicLights (msurface_t *surf)
 
        smax = (surf->extents[0] >> 4) + 1;
        tmax = (surf->extents[1] >> 4) + 1;
+       smax3 = smax * 3;
 
        for (lnum = 0; lnum < r_numdlights; lnum++)
        {
@@ -88,26 +62,28 @@ static int R_AddDynamicLights (msurface_t *surf)
                        continue;                                       // not lit by this light
 
                softwareuntransform(r_dlight[lnum].origin, local);
-//             VectorSubtract (r_dlight[lnum].origin, currentrenderentity->origin, local);
                dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
 
                // for comparisons to minimum acceptable light
                // compensate for LIGHTOFFSET
                maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
 
-               // already clamped, skip this
-               // clamp radius to avoid exceeding 32768 entry division table
-               //if (maxdist > 4194304)
-               //      maxdist = 4194304;
-
                dist2 = dist * dist;
                dist2 += LIGHTOFFSET;
                if (dist2 >= maxdist)
                        continue;
 
-               impact[0] = local[0] - surf->plane->normal[0] * dist;
-               impact[1] = local[1] - surf->plane->normal[1] * dist;
-               impact[2] = local[2] - surf->plane->normal[2] * dist;
+               if (surf->plane->type < 3)
+               {
+                       VectorCopy(local, impact);
+                       impact[surf->plane->type] -= dist;
+               }
+               else
+               {
+                       impact[0] = local[0] - surf->plane->normal[0] * dist;
+                       impact[1] = local[1] - surf->plane->normal[1] * dist;
+                       impact[2] = local[2] - surf->plane->normal[2] * dist;
+               }
 
                impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
                impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
@@ -129,8 +105,7 @@ static int R_AddDynamicLights (msurface_t *surf)
                green = r_dlight[lnum].light[1];
                blue = r_dlight[lnum].light[2];
                subtract = (int) (r_dlight[lnum].lightsubtract * 4194304.0f);
-               bl = blocklights;
-               smax3 = smax * 3;
+               bl = intblocklights;
 
                i = impactt;
                for (t = 0;t < tmax;t++, i -= 16)
@@ -163,6 +138,291 @@ static int R_AddDynamicLights (msurface_t *surf)
        return lit;
 }
 
+static int R_FloatAddDynamicLights (msurface_t *surf)
+{
+       int lnum, s, t, smax, tmax, smax3, lit, impacts, impactt;
+       float sdtable[256], *bl, k, dist, dist2, maxdist, maxdist2, maxdist3, td1, td, red, green, blue, impact[3], local[3], subtract;
+
+       lit = false;
+
+       smax = (surf->extents[0] >> 4) + 1;
+       tmax = (surf->extents[1] >> 4) + 1;
+       smax3 = smax * 3;
+
+       for (lnum = 0; lnum < r_numdlights; lnum++)
+       {
+               if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
+                       continue;                                       // not lit by this light
+
+               softwareuntransform(r_dlight[lnum].origin, local);
+               dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
+
+               // for comparisons to minimum acceptable light
+               // compensate for LIGHTOFFSET
+               maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
+
+               dist2 = dist * dist;
+               dist2 += LIGHTOFFSET;
+               if (dist2 >= maxdist)
+                       continue;
+
+               if (surf->plane->type < 3)
+               {
+                       VectorCopy(local, impact);
+                       impact[surf->plane->type] -= dist;
+               }
+               else
+               {
+                       impact[0] = local[0] - surf->plane->normal[0] * dist;
+                       impact[1] = local[1] - surf->plane->normal[1] * dist;
+                       impact[2] = local[2] - surf->plane->normal[2] * dist;
+               }
+
+               impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
+               impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
+
+               td = bound(0, impacts, smax * 16) - impacts;
+               td1 = bound(0, impactt, tmax * 16) - impactt;
+               td = td * td + td1 * td1 + dist2;
+               if (td > maxdist)
+                       continue;
+
+               // reduce calculations
+               for (s = 0, td1 = impacts; s < smax; s++, td1 -= 16.0f)
+                       sdtable[s] = td1 * td1 + dist2;
+
+               maxdist3 = maxdist - dist2;
+
+               // convert to 8.8 blocklights format
+               red = r_dlight[lnum].light[0];
+               green = r_dlight[lnum].light[1];
+               blue = r_dlight[lnum].light[2];
+               subtract = r_dlight[lnum].lightsubtract * 16384.0f;
+               bl = floatblocklights;
+
+               td1 = impactt;
+               for (t = 0;t < tmax;t++, td1 -= 16.0f)
+               {
+                       td = td1 * td1;
+                       // make sure some part of it is visible on this line
+                       if (td < maxdist3)
+                       {
+                               maxdist2 = maxdist - td;
+                               for (s = 0;s < smax;s++)
+                               {
+                                       if (sdtable[s] < maxdist2)
+                                       {
+                                               k = (16384.0f / (sdtable[s] + td)) - subtract;
+                                               bl[0] += red   * k;
+                                               bl[1] += green * k;
+                                               bl[2] += blue  * k;
+                                               lit = true;
+                                       }
+                                       bl += 3;
+                               }
+                       }
+                       else // skip line
+                               bl += smax3;
+               }
+       }
+       return lit;
+}
+
+/*
+===============
+R_BuildLightMap
+
+Combine and scale multiple lightmaps into the 8.8 format in blocklights
+===============
+*/
+static void R_BuildLightMap (msurface_t *surf, int dlightchanged)
+{
+       if (!r_floatbuildlightmap.integer)
+       {
+               int smax, tmax, i, j, size, size3, shift, maps, stride, l;
+               unsigned int *bl, scale;
+               qbyte *lightmap, *out, *stain;
+
+               // update cached lighting info
+               surf->cached_dlight = 0;
+               surf->cached_lightscalebit = lightscalebit;
+               surf->cached_ambient = r_ambient.value;
+               surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
+               surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
+               surf->cached_light[2] = d_lightstylevalue[surf->styles[2]];
+               surf->cached_light[3] = d_lightstylevalue[surf->styles[3]];
+
+               smax = (surf->extents[0]>>4)+1;
+               tmax = (surf->extents[1]>>4)+1;
+               size = smax*tmax;
+               size3 = size*3;
+               lightmap = surf->samples;
+
+       // set to full bright if no light data
+               bl = intblocklights;
+               if ((currentrenderentity->effects & EF_FULLBRIGHT) || !currentrenderentity->model->lightdata)
+               {
+                       for (i = 0;i < size3;i++)
+                               bl[i] = 255*256;
+               }
+               else
+               {
+       // clear to no light
+                       j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
+                       if (j)
+                       {
+                               for (i = 0;i < size3;i++)
+                                       *bl++ = j;
+                       }
+                       else
+                               memset(bl, 0, size*3*sizeof(unsigned int));
+
+                       if (surf->dlightframe == r_framecount && r_dlightmap.integer)
+                       {
+                               surf->cached_dlight = R_IntAddDynamicLights(surf);
+                               if (surf->cached_dlight)
+                                       c_light_polys++;
+                               else if (dlightchanged)
+                                       return; // don't upload if only updating dlights and none mattered
+                       }
+
+       // add all the lightmaps
+                       if (lightmap)
+                       {
+                               bl = intblocklights;
+                               for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
+                                       for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
+                                               bl[i] += lightmap[i] * scale;
+                       }
+               }
+
+               stain = surf->stainsamples;
+               bl = intblocklights;
+               out = templight;
+               // deal with lightmap brightness scale
+               shift = 7 + lightscalebit + 8;
+               if (currentrenderentity->model->lightmaprgba)
+               {
+                       stride = (surf->lightmaptexturestride - smax) * 4;
+                       for (i = 0;i < tmax;i++, out += stride)
+                       {
+                               for (j = 0;j < smax;j++)
+                               {
+                                       l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
+                                       l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
+                                       l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
+                                       *out++ = 255;
+                               }
+                       }
+               }
+               else
+               {
+                       stride = (surf->lightmaptexturestride - smax) * 3;
+                       for (i = 0;i < tmax;i++, out += stride)
+                       {
+                               for (j = 0;j < smax;j++)
+                               {
+                                       l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
+                                       l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
+                                       l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
+                               }
+                       }
+               }
+
+               R_UpdateTexture(surf->lightmaptexture, templight);
+       }
+       else
+       {
+               int smax, tmax, i, j, size, size3, maps, stride, l;
+               float *bl, scale;
+               qbyte *lightmap, *out, *stain;
+
+               // update cached lighting info
+               surf->cached_dlight = 0;
+               surf->cached_lightscalebit = lightscalebit;
+               surf->cached_ambient = r_ambient.value;
+               surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
+               surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
+               surf->cached_light[2] = d_lightstylevalue[surf->styles[2]];
+               surf->cached_light[3] = d_lightstylevalue[surf->styles[3]];
+
+               smax = (surf->extents[0]>>4)+1;
+               tmax = (surf->extents[1]>>4)+1;
+               size = smax*tmax;
+               size3 = size*3;
+               lightmap = surf->samples;
+
+       // set to full bright if no light data
+               bl = floatblocklights;
+               if ((currentrenderentity->effects & EF_FULLBRIGHT) || !currentrenderentity->model->lightdata)
+                       j = 255*256;
+               else
+                       j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
+
+               // clear to no light
+               if (j)
+               {
+                       for (i = 0;i < size3;i++)
+                               *bl++ = j;
+               }
+               else
+                       memset(bl, 0, size*3*sizeof(float));
+
+               if (surf->dlightframe == r_framecount && r_dlightmap.integer)
+               {
+                       surf->cached_dlight = R_FloatAddDynamicLights(surf);
+                       if (surf->cached_dlight)
+                               c_light_polys++;
+                       else if (dlightchanged)
+                               return; // don't upload if only updating dlights and none mattered
+               }
+
+               // add all the lightmaps
+               if (lightmap)
+               {
+                       bl = floatblocklights;
+                       for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
+                               for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
+                                       bl[i] += lightmap[i] * scale;
+               }
+
+               stain = surf->stainsamples;
+               bl = floatblocklights;
+               out = templight;
+               // deal with lightmap brightness scale
+               scale = 1.0f / (1 << (7 + lightscalebit + 8));
+               if (currentrenderentity->model->lightmaprgba)
+               {
+                       stride = (surf->lightmaptexturestride - smax) * 4;
+                       for (i = 0;i < tmax;i++, out += stride)
+                       {
+                               for (j = 0;j < smax;j++)
+                               {
+                                       l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
+                                       l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
+                                       l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
+                                       *out++ = 255;
+                               }
+                       }
+               }
+               else
+               {
+                       stride = (surf->lightmaptexturestride - smax) * 3;
+                       for (i = 0;i < tmax;i++, out += stride)
+                       {
+                               for (j = 0;j < smax;j++)
+                               {
+                                       l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
+                                       l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
+                                       l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
+                               }
+                       }
+               }
+
+               R_UpdateTexture(surf->lightmaptexture, templight);
+       }
+}
+
 void R_StainNode (mnode_t *node, model_t *model, vec3_t origin, float radius, int icolor[8])
 {
        float ndist;
@@ -209,9 +469,17 @@ loc0:
        {
                maxdist3 = maxdist - dist2;
 
-               impact[0] = origin[0] - node->plane->normal[0] * ndist;
-               impact[1] = origin[1] - node->plane->normal[1] * ndist;
-               impact[2] = origin[2] - node->plane->normal[2] * ndist;
+               if (node->plane->type < 3)
+               {
+                       VectorCopy(origin, impact);
+                       impact[node->plane->type] -= ndist;
+               }
+               else
+               {
+                       impact[0] = origin[0] - node->plane->normal[0] * ndist;
+                       impact[1] = origin[1] - node->plane->normal[1] * ndist;
+                       impact[2] = origin[2] - node->plane->normal[2] * ndist;
+               }
 
                for (surf = model->surfaces + node->firstsurface, endsurf = surf + node->numsurfaces;surf < endsurf;surf++)
                {
@@ -323,9 +591,9 @@ void R_Stain (vec3_t origin, float radius, int cr1, int cg1, int cb1, int ca1, i
        R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, origin, radius, icolor);
 
        // look for embedded bmodels
-       for (n = 1;n < MAX_EDICTS;n++)
+       for (n = 0;n < cl_num_brushmodel_entities;n++)
        {
-               ent = &cl_entities[n].render;
+               ent = cl_brushmodel_entities[n];
                model = ent->model;
                if (model && model->name[0] == '*')
                {
@@ -340,135 +608,6 @@ void R_Stain (vec3_t origin, float radius, int cr1, int cg1, int cb1, int ca1, i
        }
 }
 
-/*
-===============
-R_BuildLightMap
-
-Combine and scale multiple lightmaps into the 8.8 format in blocklights
-===============
-*/
-static void R_BuildLightMap (msurface_t *surf, int dlightchanged)
-{
-       int smax, tmax, i, j, size, size3, shift, scale, maps, *bl, stride, l;
-       qbyte *lightmap, *out, *stain;
-
-       // update cached lighting info
-       surf->cached_dlight = 0;
-       surf->cached_lightscalebit = lightscalebit;
-       surf->cached_ambient = r_ambient.value;
-       surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
-       surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
-       surf->cached_light[2] = d_lightstylevalue[surf->styles[2]];
-       surf->cached_light[3] = d_lightstylevalue[surf->styles[3]];
-
-       smax = (surf->extents[0]>>4)+1;
-       tmax = (surf->extents[1]>>4)+1;
-       size = smax*tmax;
-       size3 = size*3;
-       lightmap = surf->samples;
-
-// set to full bright if no light data
-       if ((currentrenderentity->effects & EF_FULLBRIGHT) || !cl.worldmodel->lightdata)
-       {
-               bl = blocklights;
-               for (i = 0;i < size;i++)
-               {
-                       *bl++ = 255*256;
-                       *bl++ = 255*256;
-                       *bl++ = 255*256;
-               }
-       }
-       else
-       {
-// clear to no light
-               j = r_ambient.value * 512.0f; // would be 256.0f logically, but using 512.0f to match winquake style
-               if (j)
-               {
-                       bl = blocklights;
-                       for (i = 0;i < size3;i++)
-                               *bl++ = j;
-               }
-               else
-                       memset(&blocklights[0], 0, size*3*sizeof(int));
-
-               if (surf->dlightframe == r_framecount && r_dlightmap.integer)
-               {
-                       surf->cached_dlight = R_AddDynamicLights(surf);
-                       if (surf->cached_dlight)
-                               c_light_polys++;
-                       else if (dlightchanged)
-                               return; // don't upload if only updating dlights and none mattered
-               }
-
-// add all the lightmaps
-               if (lightmap)
-                       for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
-                               for (scale = d_lightstylevalue[surf->styles[maps]], bl = blocklights, i = 0;i < size3;i++)
-                                       *bl++ += *lightmap++ * scale;
-       }
-
-       stain = surf->stainsamples;
-       if (stain)
-               for (bl = blocklights, i = 0;i < size3;i++)
-                       if (stain[i] < 255)
-                               bl[i] = (bl[i] * stain[i]) >> 8;
-
-       bl = blocklights;
-       out = templight;
-       // deal with lightmap brightness scale
-       shift = 7 + lightscalebit;
-       if (currentrenderentity->model->lightmaprgba)
-       {
-               stride = (surf->lightmaptexturestride - smax) * 4;
-               for (i = 0;i < tmax;i++, out += stride)
-               {
-                       for (j = 0;j < smax;j++)
-                       {
-                               l = *bl++ >> shift;*out++ = min(l, 255);
-                               l = *bl++ >> shift;*out++ = min(l, 255);
-                               l = *bl++ >> shift;*out++ = min(l, 255);
-                               *out++ = 255;
-                       }
-               }
-       }
-       else
-       {
-               stride = (surf->lightmaptexturestride - smax) * 3;
-               for (i = 0;i < tmax;i++, out += stride)
-               {
-                       for (j = 0;j < smax;j++)
-                       {
-                               l = *bl++ >> shift;*out++ = min(l, 255);
-                               l = *bl++ >> shift;*out++ = min(l, 255);
-                               l = *bl++ >> shift;*out++ = min(l, 255);
-                       }
-               }
-       }
-
-       R_UpdateTexture(surf->lightmaptexture, templight);
-}
-
-/*
-===============
-R_TextureAnimation
-
-Returns the proper texture for a given time and base texture
-===============
-*/
-/*
-// note: this was manually inlined in R_PrepareSurfaces
-static texture_t *R_TextureAnimation (texture_t *base)
-{
-       if (currentrenderentity->frame && base->alternate_anims != NULL)
-               base = base->alternate_anims;
-
-       if (base->anim_total < 2)
-               return base;
-
-       return base->anim_frames[(int)(cl.time * 5.0f) % base->anim_total];
-}
-*/
-
 
 /*
 =============================================================
@@ -485,7 +624,8 @@ static float turbsin[256] =
 };
 #define TURBSCALE (256.0 / (2 * M_PI))
 
-#define MAX_SURFVERTS 1024
+// only need to hold as many verts as the mesh splitter will allow in model_brush.c
+#define MAX_SURFVERTS 3072
 typedef struct
 {
        float v[4];
@@ -530,10 +670,6 @@ static void RSurfShader_Sky(msurface_t *firstsurf)
                                m.numtriangles = mesh->numtriangles;
                                m.numverts = mesh->numverts;
                                m.index = mesh->index;
-                               //m.cr = 0;
-                               //m.cg = 0;
-                               //m.cb = 0;
-                               //m.ca = 0;
                                if (softwaretransform_complexity)
                                {
                                        m.vertex = &svert[0].v[0];
@@ -903,8 +1039,7 @@ static void RSurfShader_Water(msurface_t *firstsurf)
                        RSurfShader_Water_Pass_Glow(surf);
        if (fogenabled)
                for (surf = firstsurf;surf;surf = surf->chain)
-                       //if (currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value) >= 1.0f)
-                               RSurfShader_Water_Pass_Fog(surf);
+                       RSurfShader_Water_Pass_Fog(surf);
 }
 
 static void RSurfShader_Wall_Pass_BaseMTex(msurface_t *surf)
@@ -1617,11 +1752,6 @@ static void RSurfShader_Wall_Lightmap(msurface_t *firstsurf)
 =============================================================
 */
 
-static void RSurf_Callback(void *data, void *junk)
-{
-       ((msurface_t *)data)->visframe = r_framecount;
-}
-
 static void R_SolidWorldNode (void)
 {
        if (r_viewleaf->contents != CONTENTS_SOLID)
@@ -1630,7 +1760,6 @@ static void R_SolidWorldNode (void)
                mportal_t *p, *pstack[8192];
                msurface_t *surf, **mark, **endmark;
                mleaf_t *leaf;
-               tinyplane_t plane;
                // LordHavoc: portal-passage worldnode; follows portals leading
                // outward from viewleaf, if a portal leads offscreen it is not
                // followed, in indoor maps this can often cull a great deal of
@@ -1648,54 +1777,25 @@ static void R_SolidWorldNode (void)
                {
                        mark = leaf->firstmarksurface;
                        endmark = mark + leaf->nummarksurfaces;
-                       if (r_ser.integer)
+                       do
                        {
-                               do
+                               surf = *mark++;
+                               // make sure surfaces are only processed once
+                               if (surf->worldnodeframe == r_framecount)
+                                       continue;
+                               surf->worldnodeframe = r_framecount;
+                               if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
                                {
-                                       surf = *mark++;
-                                       // make sure surfaces are only processed once
-                                       if (surf->worldnodeframe == r_framecount)
-                                               continue;
-                                       surf->worldnodeframe = r_framecount;
-                                       if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
-                                       {
-                                               if (surf->flags & SURF_PLANEBACK)
-                                               {
-                                                       VectorNegate(surf->plane->normal, plane.normal);
-                                                       plane.dist = -surf->plane->dist;
-                                                       R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
-                                               }
-                                       }
-                                       else
-                                       {
-                                               if (!(surf->flags & SURF_PLANEBACK))
-                                                       R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, (tinyplane_t *)surf->plane);
-                                       }
+                                       if (surf->flags & SURF_PLANEBACK)
+                                               surf->visframe = r_framecount;
                                }
-                               while (mark < endmark);
-                       }
-                       else
-                       {
-                               do
+                               else
                                {
-                                       surf = *mark++;
-                                       // make sure surfaces are only processed once
-                                       if (surf->worldnodeframe == r_framecount)
-                                               continue;
-                                       surf->worldnodeframe = r_framecount;
-                                       if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
-                                       {
-                                               if (surf->flags & SURF_PLANEBACK)
-                                                       surf->visframe = r_framecount;
-                                       }
-                                       else
-                                       {
-                                               if (!(surf->flags & SURF_PLANEBACK))
-                                                       surf->visframe = r_framecount;
-                                       }
+                                       if (!(surf->flags & SURF_PLANEBACK))
+                                               surf->visframe = r_framecount;
                                }
-                               while (mark < endmark);
                        }
+                       while (mark < endmark);
                }
 
                // follow portals into other leafs
@@ -1741,49 +1841,21 @@ loc2:
                {
                        if (node->numsurfaces)
                        {
-                               if (r_ser.integer)
+                               msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
+                               if (PlaneDiff (r_origin, node->plane) < 0)
                                {
-                                       msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
-                                       tinyplane_t plane;
-                                       if (PlaneDiff (r_origin, node->plane) < 0)
+                                       for (;surf < surfend;surf++)
                                        {
-                                               for (;surf < surfend;surf++)
-                                               {
-                                                       if (surf->flags & SURF_PLANEBACK)
-                                                       {
-                                                               VectorNegate(surf->plane->normal, plane.normal);
-                                                               plane.dist = -surf->plane->dist;
-                                                               R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), surf->flags & SURF_CLIPSOLID, RSurf_Callback, surf, NULL, &plane);
-                                                       }
-                                               }
-                                       }
-                                       else
-                                       {
-                                               for (;surf < surfend;surf++)
-                                               {
-                                                       if (!(surf->flags & SURF_PLANEBACK))
-                                                               R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), surf->flags & SURF_CLIPSOLID, RSurf_Callback, surf, NULL, (tinyplane_t *)surf->plane);
-                                               }
+                                               if (surf->flags & SURF_PLANEBACK)
+                                                       surf->visframe = r_framecount;
                                        }
                                }
                                else
                                {
-                                       msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
-                                       if (PlaneDiff (r_origin, node->plane) < 0)
+                                       for (;surf < surfend;surf++)
                                        {
-                                               for (;surf < surfend;surf++)
-                                               {
-                                                       if (surf->flags & SURF_PLANEBACK)
-                                                               surf->visframe = r_framecount;
-                                               }
-                                       }
-                                       else
-                                       {
-                                               for (;surf < surfend;surf++)
-                                               {
-                                                       if (!(surf->flags & SURF_PLANEBACK))
-                                                               surf->visframe = r_framecount;
-                                               }
+                                               if (!(surf->flags & SURF_PLANEBACK))
+                                                       surf->visframe = r_framecount;
                                        }
                                }
                        }
@@ -1835,7 +1907,6 @@ static void R_PVSWorldNode()
        mportal_t *p, *pstack[8192];
        msurface_t *surf, **mark, **endmark;
        mleaf_t *leaf;
-       tinyplane_t plane;
        qbyte *worldvis;
 
        worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
@@ -1852,54 +1923,25 @@ loc0:
        {
                mark = leaf->firstmarksurface;
                endmark = mark + leaf->nummarksurfaces;
-               if (r_ser.integer)
+               do
                {
-                       do
+                       surf = *mark++;
+                       // make sure surfaces are only processed once
+                       if (surf->worldnodeframe == r_framecount)
+                               continue;
+                       surf->worldnodeframe = r_framecount;
+                       if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
                        {
-                               surf = *mark++;
-                               // make sure surfaces are only processed once
-                               if (surf->worldnodeframe == r_framecount)
-                                       continue;
-                               surf->worldnodeframe = r_framecount;
-                               if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
-                               {
-                                       if (surf->flags & SURF_PLANEBACK)
-                                       {
-                                               VectorNegate(surf->plane->normal, plane.normal);
-                                               plane.dist = -surf->plane->dist;
-                                               R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
-                                       }
-                               }
-                               else
-                               {
-                                       if (!(surf->flags & SURF_PLANEBACK))
-                                               R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, (tinyplane_t *)surf->plane);
-                               }
+                               if (surf->flags & SURF_PLANEBACK)
+                                       surf->visframe = r_framecount;
                        }
-                       while (mark < endmark);
-               }
-               else
-               {
-                       do
+                       else
                        {
-                               surf = *mark++;
-                               // make sure surfaces are only processed once
-                               if (surf->worldnodeframe == r_framecount)
-                                       continue;
-                               surf->worldnodeframe = r_framecount;
-                               if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
-                               {
-                                       if (surf->flags & SURF_PLANEBACK)
-                                               surf->visframe = r_framecount;
-                               }
-                               else
-                               {
-                                       if (!(surf->flags & SURF_PLANEBACK))
-                                               surf->visframe = r_framecount;
-                               }
+                               if (!(surf->flags & SURF_PLANEBACK))
+                                       surf->visframe = r_framecount;
                        }
-                       while (mark < endmark);
                }
+               while (mark < endmark);
        }
 
        // follow portals into other leafs
@@ -1952,7 +1994,7 @@ Cshader_t *Cshaders[5] =
 
 void R_PrepareSurfaces(void)
 {
-       int i;
+       int i, alttextures, texframe, framecount;
        texture_t *t;
        model_t *model;
        msurface_t *surf;
@@ -1961,6 +2003,8 @@ void R_PrepareSurfaces(void)
                Cshaders[i]->chain = NULL;
 
        model = currentrenderentity->model;
+       alttextures = currentrenderentity->frame != 0;
+       texframe = (int)(cl.time * 5.0f);
 
        for (i = 0;i < model->nummodelsurfaces;i++)
        {
@@ -1971,14 +2015,17 @@ void R_PrepareSurfaces(void)
                        {
                                surf->insertframe = r_framecount;
                                c_faces++;
-                               // manually inlined R_TextureAnimation
-                               //t = R_TextureAnimation(surf->texinfo->texture);
                                t = surf->texinfo->texture;
-                               if (t->alternate_anims != NULL && currentrenderentity->frame)
-                                       t = t->alternate_anims;
-                               if (t->anim_total >= 2)
-                                       t = t->anim_frames[(int)(cl.time * 5.0f) % t->anim_total];
-                               surf->currenttexture = t;
+                               if (t->animated)
+                               {
+                                       framecount = t->anim_total[alttextures];
+                                       if (framecount >= 2)
+                                               surf->currenttexture = t->anim_frames[alttextures][texframe % framecount];
+                                       else
+                                               surf->currenttexture = t->anim_frames[alttextures][0];
+                               }
+                               else
+                                       surf->currenttexture = t;
                        }
 
                        surf->chain = surf->shader->chain;
@@ -2005,38 +2052,14 @@ static float portalpointbuffer[256][3];
 void R_DrawPortals(void)
 {
        int drawportals, i;
-//     mleaf_t *leaf, *endleaf;
        mportal_t *portal, *endportal;
-       mvertex_t *point/*, *endpoint*/;
+       mvertex_t *point;
        rmeshinfo_t m;
        drawportals = r_drawportals.integer;
+
        if (drawportals < 1)
                return;
-       /*
-       leaf = cl.worldmodel->leafs;
-       endleaf = leaf + cl.worldmodel->numleafs;
-       for (;leaf < endleaf;leaf++)
-       {
-               if (leaf->visframe == r_framecount && leaf->portals)
-               {
-                       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;
-                       }
-               }
-       }
-       */
+
        memset(&m, 0, sizeof(m));
        m.transparent = true;
        m.blendfunc1 = GL_SRC_ALPHA;
@@ -2134,10 +2157,6 @@ static void R_SurfMarkLights (void)
                                         || (surf->styles[1] != 255 && (d_lightstylevalue[surf->styles[1]] != surf->cached_light[1]
                                         || (surf->styles[2] != 255 && (d_lightstylevalue[surf->styles[2]] != surf->cached_light[2]
                                         || (surf->styles[3] != 255 && (d_lightstylevalue[surf->styles[3]] != surf->cached_light[3]))))))))
-                                       //if (surf->cached_light[0] != d_lightstylevalue[surf->styles[0]]
-                                       // || surf->cached_light[1] != d_lightstylevalue[surf->styles[1]]
-                                       // || surf->cached_light[2] != d_lightstylevalue[surf->styles[2]]
-                                       // || surf->cached_light[3] != d_lightstylevalue[surf->styles[3]])
                                                R_BuildLightMap(surf, false); // base lighting changed
                                        else if (surf->dlightframe == r_framecount && r_dlightmap.integer)
                                                R_BuildLightMap(surf, true); // only dlights
@@ -2198,3 +2217,33 @@ void R_DrawBrushModelNormal (void)
                R_DrawSurfaces(SHADERSTAGE_SKY);
        R_DrawSurfaces(SHADERSTAGE_NORMAL);
 }
+
+static void gl_surf_start(void)
+{
+}
+
+static void gl_surf_shutdown(void)
+{
+}
+
+static void gl_surf_newmap(void)
+{
+}
+
+void GL_Surf_Init(void)
+{
+       int i;
+       dlightdivtable[0] = 4194304;
+       for (i = 1;i < 32768;i++)
+               dlightdivtable[i] = 4194304 / (i << 7);
+
+       Cvar_RegisterVariable(&r_ambient);
+       Cvar_RegisterVariable(&r_vertexsurfaces);
+       Cvar_RegisterVariable(&r_dlightmap);
+       Cvar_RegisterVariable(&r_drawportals);
+       Cvar_RegisterVariable(&r_testvis);
+       Cvar_RegisterVariable(&r_floatbuildlightmap);
+
+       R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);
+}
+