]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_rsurf.c
now always uses glPolygonOffset, and collision brush rendering has been fixed with...
[xonotic/darkplaces.git] / gl_rsurf.c
index 3e237555beb2ac7c1276c5f9ce367f205f96e94f..c05b8794cbcc2680e593f25f6f29abfb79877c93 100644 (file)
@@ -35,7 +35,8 @@ cvar_t r_testvis = {0, "r_testvis", "0"};
 cvar_t r_floatbuildlightmap = {0, "r_floatbuildlightmap", "0"};
 cvar_t r_detailtextures = {CVAR_SAVE, "r_detailtextures", "1"};
 cvar_t r_surfaceworldnode = {0, "r_surfaceworldnode", "1"};
-cvar_t r_drawcollisionbrushes_polygonoffset = {0, "r_drawcollisionbrushes_polygonoffset", "-4"};
+cvar_t r_drawcollisionbrushes_polygonfactor = {0, "r_drawcollisionbrushes_polygonfactor", "-1"};
+cvar_t r_drawcollisionbrushes_polygonoffset = {0, "r_drawcollisionbrushes_polygonoffset", "0"};
 
 static int dlightdivtable[32768];
 
@@ -234,7 +235,7 @@ static void R_BuildLightMap (const entity_render_t *ent, msurface_t *surf)
 {
        if (!r_floatbuildlightmap.integer)
        {
-               int smax, tmax, i, j, size, size3, shift, maps, stride, l;
+               int smax, tmax, i, j, size, size3, maps, stride, l;
                unsigned int *bl, scale;
                qbyte *lightmap, *out, *stain;
 
@@ -249,7 +250,7 @@ static void R_BuildLightMap (const entity_render_t *ent, msurface_t *surf)
 
        // set to full bright if no light data
                bl = intblocklights;
-               if ((ent->effects & EF_FULLBRIGHT) || !ent->model->brushq1.lightdata)
+               if (!ent->model->brushq1.lightdata)
                {
                        for (i = 0;i < size3;i++)
                                bl[i] = 255*256;
@@ -286,8 +287,10 @@ static void R_BuildLightMap (const entity_render_t *ent, msurface_t *surf)
                stain = surf->stainsamples;
                bl = intblocklights;
                out = templight;
-               // deal with lightmap brightness scale
-               shift = 7 + r_lightmapscalebit + 8;
+               // the >> 16 shift adjusts down 8 bits to account for the stainmap
+               // scaling, and remaps the 0-65536 (2x overbright) to 0-256, it will
+               // be doubled during rendering to achieve 2x overbright
+               // (0 = 0.0, 128 = 1.0, 256 = 2.0)
                if (ent->model->brushq1.lightmaprgba)
                {
                        stride = (surf->lightmaptexturestride - smax) * 4;
@@ -295,9 +298,9 @@ static void R_BuildLightMap (const entity_render_t *ent, msurface_t *surf)
                        {
                                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);
+                                       l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
+                                       l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
+                                       l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
                                        *out++ = 255;
                                }
                        }
@@ -309,9 +312,9 @@ static void R_BuildLightMap (const entity_render_t *ent, msurface_t *surf)
                        {
                                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);
+                                       l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
+                                       l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
+                                       l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
                                }
                        }
                }
@@ -335,7 +338,7 @@ static void R_BuildLightMap (const entity_render_t *ent, msurface_t *surf)
 
        // set to full bright if no light data
                bl = floatblocklights;
-               if ((ent->effects & EF_FULLBRIGHT) || !ent->model->brushq1.lightdata)
+               if (!ent->model->brushq1.lightdata)
                        j = 255*256;
                else
                        j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
@@ -368,8 +371,11 @@ static void R_BuildLightMap (const entity_render_t *ent, msurface_t *surf)
                stain = surf->stainsamples;
                bl = floatblocklights;
                out = templight;
-               // deal with lightmap brightness scale
-               scale = 1.0f / (1 << (7 + r_lightmapscalebit + 8));
+               // this scaling adjusts down 8 bits to account for the stainmap
+               // scaling, and remaps the 0.0-2.0 (2x overbright) to 0-256, it will
+               // be doubled during rendering to achieve 2x overbright
+               // (0 = 0.0, 128 = 1.0, 256 = 2.0)
+               scale = 1.0f / (1 << 16);
                if (ent->model->brushq1.lightmaprgba)
                {
                        stride = (surf->lightmaptexturestride - smax) * 4;
@@ -775,7 +781,7 @@ static void RSurfShader_Water_Callback(const void *calldata1, int calldata2)
        }
 
        R_Mesh_Matrix(&ent->matrix);
-       Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
+       Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
 
        memset(&m, 0, sizeof(m));
        texture = surf->texinfo->texture->currentframe;
@@ -887,7 +893,7 @@ static void RSurfShader_Wall_Pass_BaseVertex(const entity_render_t *ent, const m
        float base, colorscale;
        rmeshstate_t m;
        float modelorg[3];
-       Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
+       Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
        memset(&m, 0, sizeof(m));
        if (rendertype == SURFRENDER_ADD)
        {
@@ -934,7 +940,7 @@ static void RSurfShader_Wall_Pass_Glow(const entity_render_t *ent, const msurfac
 {
        rmeshstate_t m;
        float modelorg[3];
-       Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
+       Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
        memset(&m, 0, sizeof(m));
        GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
        GL_DepthMask(false);
@@ -954,7 +960,7 @@ static void RSurfShader_Wall_Pass_Fog(const entity_render_t *ent, const msurface
 {
        rmeshstate_t m;
        float modelorg[3];
-       Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
+       Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
        memset(&m, 0, sizeof(m));
        GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
        GL_DepthMask(false);
@@ -975,19 +981,16 @@ static void RSurfShader_OpaqueWall_Pass_BaseTripleTexCombine(const entity_render
        const msurface_t *surf;
        rmeshstate_t m;
        int lightmaptexturenum;
-       float cl;
        memset(&m, 0, sizeof(m));
        GL_BlendFunc(GL_ONE, GL_ZERO);
        GL_DepthMask(true);
        GL_DepthTest(true);
        m.tex[0] = R_GetTexture(texture->skin.base);
        m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
+       m.texrgbscale[1] = 2;
        m.tex[2] = R_GetTexture(texture->skin.detail);
-       m.texrgbscale[0] = 1;
-       m.texrgbscale[1] = 4;
        m.texrgbscale[2] = 2;
-       cl = (float) (1 << r_lightmapscalebit);
-       GL_Color(cl, cl, cl, 1);
+       GL_Color(1, 1, 1, 1);
 
        while((surf = *surfchain++) != NULL)
        {
@@ -1009,7 +1012,7 @@ static void RSurfShader_OpaqueWall_Pass_BaseTripleTexCombine(const entity_render
        }
 }
 
-static void RSurfShader_OpaqueWall_Pass_BaseDoubleTex(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
+static void RSurfShader_OpaqueWall_Pass_BaseDoubleTexCombine(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
 {
        const msurface_t *surf;
        rmeshstate_t m;
@@ -1020,8 +1023,7 @@ static void RSurfShader_OpaqueWall_Pass_BaseDoubleTex(const entity_render_t *ent
        GL_DepthTest(true);
        m.tex[0] = R_GetTexture(texture->skin.base);
        m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
-       if (gl_combine.integer)
-               m.texrgbscale[1] = 4;
+       m.texrgbscale[1] = 2;
        GL_Color(1, 1, 1, 1);
        while((surf = *surfchain++) != NULL)
        {
@@ -1070,12 +1072,10 @@ static void RSurfShader_OpaqueWall_Pass_BaseLightmap(const entity_render_t *ent,
        rmeshstate_t m;
        int lightmaptexturenum;
        memset(&m, 0, sizeof(m));
-       GL_BlendFunc(GL_ZERO, GL_SRC_COLOR);
+       GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
        GL_DepthMask(false);
        GL_DepthTest(true);
        m.tex[0] = R_GetTexture((**surfchain).lightmaptexture);
-       if (gl_combine.integer)
-               m.texrgbscale[0] = 4;
        GL_Color(1, 1, 1, 1);
        while((surf = *surfchain++) != NULL)
        {
@@ -1100,7 +1100,7 @@ static void RSurfShader_OpaqueWall_Pass_Fog(const entity_render_t *ent, const te
        const msurface_t *surf;
        rmeshstate_t m;
        float modelorg[3];
-       Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
+       Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
        memset(&m, 0, sizeof(m));
        GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        GL_DepthMask(false);
@@ -1233,6 +1233,16 @@ static void RSurfShader_Wall_Lightmap(const entity_render_t *ent, const texture_
                        }
                }
        }
+       else if (ent->effects & EF_FULLBRIGHT)
+       {
+               RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture, surfchain);
+               if (r_detailtextures.integer)
+                       RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
+               if (texture->skin.glow)
+                       RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
+               if (fogenabled)
+                       RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
+       }
        else if (r_shadow_realtime_world.integer)
        {
                // opaque base lighting
@@ -1244,12 +1254,22 @@ static void RSurfShader_Wall_Lightmap(const entity_render_t *ent, const texture_
        {
                // opaque lightmapped
                if (r_textureunits.integer >= 3 && gl_combine.integer && r_detailtextures.integer)
+               {
                        RSurfShader_OpaqueWall_Pass_BaseTripleTexCombine(ent, texture, surfchain);
-               else if (r_textureunits.integer >= 2)
+                       if (texture->skin.glow)
+                               RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
+                       if (fogenabled)
+                               RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
+               }
+               else if (r_textureunits.integer >= 2 && gl_combine.integer)
                {
-                       RSurfShader_OpaqueWall_Pass_BaseDoubleTex(ent, texture, surfchain);
+                       RSurfShader_OpaqueWall_Pass_BaseDoubleTexCombine(ent, texture, surfchain);
                        if (r_detailtextures.integer)
                                RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
+                       if (texture->skin.glow)
+                               RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
+                       if (fogenabled)
+                               RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
                }
                else
                {
@@ -1257,11 +1277,11 @@ static void RSurfShader_Wall_Lightmap(const entity_render_t *ent, const texture_
                        RSurfShader_OpaqueWall_Pass_BaseLightmap(ent, texture, surfchain);
                        if (r_detailtextures.integer)
                                RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
+                       if (texture->skin.glow)
+                               RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
+                       if (fogenabled)
+                               RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
                }
-               if (texture->skin.glow)
-                       RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
-               if (fogenabled)
-                       RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
        }
 }
 
@@ -1317,7 +1337,7 @@ void R_PrepareSurfaces(entity_render_t *ent)
                return;
 
        model = ent->model;
-       Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
+       Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
        numsurfaces = model->brushq1.nummodelsurfaces;
        surfaces = model->brushq1.surfaces + model->brushq1.firstmodelsurface;
        surfacevisframes = model->brushq1.surfacevisframes + model->brushq1.firstmodelsurface;
@@ -1327,10 +1347,9 @@ void R_PrepareSurfaces(entity_render_t *ent)
        if (r_dynamic.integer && !r_shadow_realtime_dlight.integer)
                R_MarkLights(ent);
 
-       if (model->brushq1.light_ambient != r_ambient.value || model->brushq1.light_scalebit != r_lightmapscalebit)
+       if (model->brushq1.light_ambient != r_ambient.value)
        {
                model->brushq1.light_ambient = r_ambient.value;
-               model->brushq1.light_scalebit = r_lightmapscalebit;
                for (i = 0;i < model->brushq1.nummodelsurfaces;i++)
                        model->brushq1.surfaces[i + model->brushq1.firstmodelsurface].cached_dlight = true;
        }
@@ -1408,7 +1427,7 @@ static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
                         ((i & 0x0038) >> 3) * (1.0f / 7.0f),
                         ((i & 0x01C0) >> 6) * (1.0f / 7.0f),
                         0.125f);
-       if (PlaneDiff(r_origin, (&portal->plane)) < 0)
+       if (PlaneDiff(r_vieworigin, (&portal->plane)) < 0)
        {
                for (i = portal->numpoints - 1, v = varray_vertex3f;i >= 0;i--, v += 3)
                        VectorCopy(portal->points[i].position, v);
@@ -1457,7 +1476,7 @@ void R_PrepareBrushModel(entity_render_t *ent)
        if (model == NULL)
                return;
 #if WORLDNODECULLBACKFACES
-       Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
+       Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
 #endif
        numsurfaces = model->brushq1.nummodelsurfaces;
        surf = model->brushq1.surfaces + model->brushq1.firstmodelsurface;
@@ -1496,7 +1515,7 @@ void R_SurfaceWorldNode (entity_render_t *ent)
                return;
        surfacevisframes = model->brushq1.surfacevisframes + model->brushq1.firstmodelsurface;
        surfacepvsframes = model->brushq1.surfacepvsframes + model->brushq1.firstmodelsurface;
-       Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
+       Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
 
        for (leaf = model->brushq1.pvsleafchain;leaf;leaf = leaf->pvschain)
        {
@@ -1548,7 +1567,7 @@ static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
        // RecursiveWorldNode
        surfaces = ent->model->brushq1.surfaces;
        surfacevisframes = ent->model->brushq1.surfacevisframes;
-       Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
+       Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
        viewleaf->worldnodeframe = r_framecount;
        leafstack[0] = viewleaf;
        leafstackpos = 1;
@@ -1644,7 +1663,7 @@ void R_WorldVisibility(entity_render_t *ent)
        vec3_t modelorg;
        mleaf_t *viewleaf;
 
-       Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
+       Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
        viewleaf = (ent->model && ent->model->brushq1.PointInLeaf) ? ent->model->brushq1.PointInLeaf(ent->model, modelorg) : NULL;
        R_PVSUpdate(ent, viewleaf);
 
@@ -1778,6 +1797,7 @@ void R_Q3BSP_DrawSkyFace(entity_render_t *ent, q3mface_t *face)
        rmeshstate_t m;
        if (!face->num_triangles)
                return;
+       c_faces++;
        if (skyrendernow)
        {
                skyrendernow = false;
@@ -1832,7 +1852,7 @@ void R_Q3BSP_DrawFace_OpaqueWall_Pass_OpaqueGlow(entity_render_t *ent, q3mface_t
        R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
 }
 
-void R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureLightmap(entity_render_t *ent, q3mface_t *face)
+void R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureLightmapCombine(entity_render_t *ent, q3mface_t *face)
 {
        rmeshstate_t m;
        memset(&m, 0, sizeof(m));
@@ -1869,7 +1889,7 @@ void R_Q3BSP_DrawFace_OpaqueWall_Pass_Lightmap(entity_render_t *ent, q3mface_t *
 {
        rmeshstate_t m;
        memset(&m, 0, sizeof(m));
-       GL_BlendFunc(GL_ONE, GL_SRC_COLOR);
+       GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
        GL_DepthMask(true);
        GL_DepthTest(true);
        m.tex[0] = R_GetTexture(face->lightmaptexture);
@@ -1909,34 +1929,139 @@ void R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureVertex(entity_render_t *ent, q3mfac
        GL_DepthTest(true);
        m.tex[0] = R_GetTexture(face->texture->skin.base);
        m.pointer_texcoord[0] = face->data_texcoordtexture2f;
-       m.texrgbscale[0] = 2;
-       GL_ColorPointer(face->data_color4f);
+       if (gl_combine.integer)
+       {
+               m.texrgbscale[0] = 2;
+               GL_ColorPointer(face->data_color4f);
+       }
+       else
+       {
+               int i;
+               for (i = 0;i < face->num_vertices;i++)
+               {
+                       varray_color4f[i*4+0] = face->data_color4f[i*4+0] * 2.0f;
+                       varray_color4f[i*4+1] = face->data_color4f[i*4+1] * 2.0f;
+                       varray_color4f[i*4+2] = face->data_color4f[i*4+2] * 2.0f;
+                       varray_color4f[i*4+3] = face->data_color4f[i*4+3];
+               }
+               GL_ColorPointer(varray_color4f);
+       }
+       R_Mesh_State_Texture(&m);
+       GL_VertexPointer(face->data_vertex3f);
+       R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
+}
+
+void R_Q3BSP_DrawFace_OpaqueWall_Pass_AddTextureAmbient(entity_render_t *ent, q3mface_t *face)
+{
+       rmeshstate_t m;
+       memset(&m, 0, sizeof(m));
+       GL_BlendFunc(GL_ONE, GL_ONE);
+       GL_DepthMask(true);
+       GL_DepthTest(true);
+       m.tex[0] = R_GetTexture(face->texture->skin.base);
+       m.pointer_texcoord[0] = face->data_texcoordtexture2f;
+       GL_Color(r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), 1);
+       R_Mesh_State_Texture(&m);
+       GL_VertexPointer(face->data_vertex3f);
+       R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
+}
+
+void R_Q3BSP_DrawFace_TransparentCallback(const void *voident, int facenumber)
+{
+       const entity_render_t *ent = voident;
+       q3mface_t *face = ent->model->brushq3.data_faces + facenumber;
+       rmeshstate_t m;
+       R_Mesh_Matrix(&ent->matrix);
+       memset(&m, 0, sizeof(m));
+       if (ent->effects & EF_ADDITIVE)
+               GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
+       else
+               GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+       GL_DepthMask(false);
+       GL_DepthTest(true);
+       m.tex[0] = R_GetTexture(face->texture->skin.base);
+       m.pointer_texcoord[0] = face->data_texcoordtexture2f;
+       // LordHavoc: quake3 was not able to do this; lit transparent surfaces
+       if (gl_combine.integer)
+       {
+               m.texrgbscale[0] = 2;
+               if (r_textureunits.integer >= 2)
+               {
+                       m.tex[1] = R_GetTexture(face->lightmaptexture);
+                       m.pointer_texcoord[1] = face->data_texcoordlightmap2f;
+                       GL_Color(1, 1, 1, ent->alpha);
+               }
+               else
+               {
+                       if (ent->alpha == 1)
+                               GL_ColorPointer(face->data_color4f);
+                       else
+                       {
+                               int i;
+                               for (i = 0;i < face->num_vertices;i++)
+                               {
+                                       varray_color4f[i*4+0] = face->data_color4f[i*4+0];
+                                       varray_color4f[i*4+1] = face->data_color4f[i*4+1];
+                                       varray_color4f[i*4+2] = face->data_color4f[i*4+2];
+                                       varray_color4f[i*4+3] = face->data_color4f[i*4+3] * ent->alpha;
+                               }
+                               GL_ColorPointer(varray_color4f);
+                       }
+               }
+       }
+       else
+       {
+               int i;
+               for (i = 0;i < face->num_vertices;i++)
+               {
+                       varray_color4f[i*4+0] = face->data_color4f[i*4+0] * 2.0f;
+                       varray_color4f[i*4+1] = face->data_color4f[i*4+1] * 2.0f;
+                       varray_color4f[i*4+2] = face->data_color4f[i*4+2] * 2.0f;
+                       varray_color4f[i*4+3] = face->data_color4f[i*4+3] * ent->alpha;
+               }
+               GL_ColorPointer(varray_color4f);
+       }
        R_Mesh_State_Texture(&m);
        GL_VertexPointer(face->data_vertex3f);
+       qglDisable(GL_CULL_FACE);
        R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
+       qglEnable(GL_CULL_FACE);
 }
 
 void R_Q3BSP_DrawFace(entity_render_t *ent, q3mface_t *face)
 {
        if (!face->num_triangles)
                return;
-       if (face->texture->renderflags)
+       if (face->texture->surfaceparms)
        {
-               if (face->texture->renderflags & Q3MTEXTURERENDERFLAGS_SKY)
-                       return;
-               if (face->texture->renderflags & Q3MTEXTURERENDERFLAGS_NODRAW)
+               if (face->texture->surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NODRAW))
                        return;
        }
-       if (face->texture->nativecontents & CONTENTSQ3_TRANSLUCENT)
-               qglDisable(GL_CULL_FACE);
-       R_Mesh_Matrix(&ent->matrix);
+       c_faces++;
        face->visframe = r_framecount;
+       if ((face->texture->surfaceparms & Q3SURFACEPARM_TRANS) || ent->alpha < 1 || (ent->effects & EF_ADDITIVE))
+       {
+               vec3_t facecenter, center;
+               facecenter[0] = (face->mins[0] + face->maxs[0]) * 0.5f;
+               facecenter[1] = (face->mins[1] + face->maxs[1]) * 0.5f;
+               facecenter[2] = (face->mins[2] + face->maxs[2]) * 0.5f;
+               Matrix4x4_Transform(&ent->matrix, facecenter, center);
+               R_MeshQueue_AddTransparent(center, R_Q3BSP_DrawFace_TransparentCallback, ent, face - ent->model->brushq3.data_faces);
+               return;
+       }
+       R_Mesh_Matrix(&ent->matrix);
        if (r_shadow_realtime_world.integer)
                R_Q3BSP_DrawFace_OpaqueWall_Pass_OpaqueGlow(ent, face);
+       else if ((ent->effects & EF_FULLBRIGHT) || r_fullbright.integer)
+       {
+               R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(ent, face);
+               if (face->texture->skin.glow)
+                       R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
+       }
        else if (face->lightmaptexture)
        {
-               if (r_textureunits.integer >= 2)
-                       R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureLightmap(ent, face);
+               if (r_textureunits.integer >= 2 && gl_combine.integer)
+                       R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureLightmapCombine(ent, face);
                else
                {
                        R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(ent, face);
@@ -1951,27 +2076,50 @@ void R_Q3BSP_DrawFace(entity_render_t *ent, q3mface_t *face)
                if (face->texture->skin.glow)
                        R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
        }
-       if (face->texture->nativecontents & CONTENTSQ3_TRANSLUCENT)
-               qglEnable(GL_CULL_FACE);
+       if (r_ambient.value)
+               R_Q3BSP_DrawFace_OpaqueWall_Pass_AddTextureAmbient(ent, face);
 }
 
 void R_Q3BSP_RecursiveWorldNode(entity_render_t *ent, q3mnode_t *node, const vec3_t modelorg, qbyte *pvs, int markframe)
 {
        int i;
        q3mleaf_t *leaf;
-       while (node->isnode)
+       for (;;)
        {
                if (R_CullBox(node->mins, node->maxs))
                        return;
+               if (!node->plane)
+                       break;
+               c_nodes++;
                R_Q3BSP_RecursiveWorldNode(ent, node->children[0], modelorg, pvs, markframe);
                node = node->children[1];
        }
-       if (R_CullBox(node->mins, node->maxs))
-               return;
        leaf = (q3mleaf_t *)node;
        if (pvs[leaf->clusterindex >> 3] & (1 << (leaf->clusterindex & 7)))
+       {
+               c_leafs++;
                for (i = 0;i < leaf->numleaffaces;i++)
                        leaf->firstleafface[i]->markframe = markframe;
+       }
+}
+
+// FIXME: num_leafs needs to be recalculated at load time to include only
+// node-referenced leafs, as some maps are incorrectly compiled with leafs for
+// the submodels (which would render the submodels occasionally, as part of
+// the world - not good)
+void R_Q3BSP_MarkLeafPVS(entity_render_t *ent, qbyte *pvs, int markframe)
+{
+       int i, j;
+       q3mleaf_t *leaf;
+       for (j = 0, leaf = ent->model->brushq3.data_leafs;j < ent->model->brushq3.num_leafs;j++, leaf++)
+       {
+               if (pvs[leaf->clusterindex >> 3] & (1 << (leaf->clusterindex & 7)))
+               {
+                       c_leafs++;
+                       for (i = 0;i < leaf->numleaffaces;i++)
+                               leaf->firstleafface[i]->markframe = markframe;
+               }
+       }
 }
 
 static int r_q3bsp_framecount = -1;
@@ -1987,21 +2135,22 @@ void R_Q3BSP_DrawSky(entity_render_t *ent)
        model = ent->model;
        if (r_drawcollisionbrushes.integer < 2)
        {
-               Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
+               Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
                if (ent == &cl_entities[0].render && model->brushq3.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
                {
                        if (r_q3bsp_framecount != r_framecount)
                        {
                                r_q3bsp_framecount = r_framecount;
                                R_Q3BSP_RecursiveWorldNode(ent, model->brushq3.data_nodes, modelorg, pvs, r_framecount);
+                               //R_Q3BSP_MarkLeafPVS(ent, pvs, r_framecount);
                        }
                        for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
-                               if (face->markframe == r_framecount && (face->texture->renderflags & Q3MTEXTURERENDERFLAGS_SKY) && !R_CullBox(face->mins, face->maxs))
+                               if (face->markframe == r_framecount && (face->texture->surfaceflags & Q3SURFACEFLAG_SKY) && !R_CullBox(face->mins, face->maxs))
                                        R_Q3BSP_DrawSkyFace(ent, face);
                }
                else
                        for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
-                               if ((face->texture->renderflags & Q3MTEXTURERENDERFLAGS_SKY))
+                               if ((face->texture->surfaceflags & Q3SURFACEFLAG_SKY))
                                        R_Q3BSP_DrawSkyFace(ent, face);
        }
 }
@@ -2017,13 +2166,14 @@ void R_Q3BSP_Draw(entity_render_t *ent)
        model = ent->model;
        if (r_drawcollisionbrushes.integer < 2)
        {
-               Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
+               Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
                if (ent == &cl_entities[0].render && model->brushq3.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
                {
                        if (r_q3bsp_framecount != r_framecount)
                        {
                                r_q3bsp_framecount = r_framecount;
                                R_Q3BSP_RecursiveWorldNode(ent, model->brushq3.data_nodes, modelorg, pvs, r_framecount);
+                               //R_Q3BSP_MarkLeafPVS(ent, pvs, r_framecount);
                        }
                        for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
                                if (face->markframe == r_framecount && !R_CullBox(face->mins, face->maxs))
@@ -2041,9 +2191,11 @@ void R_Q3BSP_Draw(entity_render_t *ent)
                GL_DepthMask(false);
                GL_DepthTest(true);
                R_Mesh_State_Texture(&m);
+               qglPolygonOffset(r_drawcollisionbrushes_polygonfactor.value, r_drawcollisionbrushes_polygonoffset.value);
                for (i = 0;i < model->brushq3.data_thismodel->numbrushes;i++)
                        if (model->brushq3.data_thismodel->firstbrush[i].colbrushf && model->brushq3.data_thismodel->firstbrush[i].colbrushf->numtriangles)
                                R_DrawCollisionBrush(model->brushq3.data_thismodel->firstbrush[i].colbrushf);
+               qglPolygonOffset(0, 0);
        }
 }
 
@@ -2059,7 +2211,7 @@ void R_Q3BSP_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin,
        {
                model = ent->model;
                R_Mesh_Matrix(&ent->matrix);
-               Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
+               Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
                lightmins[0] = relativelightorigin[0] - lightradius;
                lightmins[1] = relativelightorigin[1] - lightradius;
                lightmins[2] = relativelightorigin[2] - lightradius;
@@ -2077,7 +2229,7 @@ void R_Q3BSP_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin,
 
 void R_Q3BSP_DrawFaceLight(entity_render_t *ent, q3mface_t *face, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, const matrix4x4_t *matrix_modeltofilter, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz)
 {
-       if ((face->texture->renderflags & Q3MTEXTURERENDERFLAGS_NODRAW) || !face->num_triangles)
+       if ((face->texture->surfaceflags & Q3SURFACEFLAG_NODRAW) || !face->num_triangles)
                return;
        R_Shadow_DiffuseLighting(face->num_vertices, face->num_triangles, face->data_element3i, face->data_vertex3f, face->data_svector3f, face->data_tvector3f, face->data_normal3f, face->data_texcoordtexture2f, relativelightorigin, lightradius, lightcolor, matrix_modeltofilter, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, face->texture->skin.base, face->texture->skin.nmap, NULL);
        R_Shadow_SpecularLighting(face->num_vertices, face->num_triangles, face->data_element3i, face->data_vertex3f, face->data_svector3f, face->data_tvector3f, face->data_normal3f, face->data_texcoordtexture2f, relativelightorigin, relativeeyeorigin, lightradius, lightcolor, matrix_modeltofilter, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, face->texture->skin.gloss, face->texture->skin.nmap, NULL);
@@ -2095,7 +2247,7 @@ void R_Q3BSP_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t
        {
                model = ent->model;
                R_Mesh_Matrix(&ent->matrix);
-               Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
+               Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
                lightmins[0] = relativelightorigin[0] - lightradius;
                lightmins[1] = relativelightorigin[1] - lightradius;
                lightmins[2] = relativelightorigin[2] - lightradius;
@@ -2136,6 +2288,7 @@ void GL_Surf_Init(void)
        Cvar_RegisterVariable(&r_floatbuildlightmap);
        Cvar_RegisterVariable(&r_detailtextures);
        Cvar_RegisterVariable(&r_surfaceworldnode);
+       Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonfactor);
        Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonoffset);
 
        R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);