]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_rsurf.c
fixed GL_Scissor call in rtlight code (apparently I need to feed it a top to bottom...
[xonotic/darkplaces.git] / gl_rsurf.c
index 1f68253dbe243f3a8a824b1bd298225019a0c5b2..75daf7108ceca04c8bec0af4b5b65bf6d720df96 100644 (file)
@@ -35,7 +35,9 @@ 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"};
+cvar_t gl_lightmaps = {0, "gl_lightmaps", "0"};
 
 static int dlightdivtable[32768];
 
@@ -975,7 +977,7 @@ static void RSurfShader_Wall_Pass_Fog(const entity_render_t *ent, const msurface
        R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
 }
 
-static void RSurfShader_OpaqueWall_Pass_BaseTripleTexCombine(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
+static void RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetailGlow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
 {
        const msurface_t *surf;
        rmeshstate_t m;
@@ -989,7 +991,15 @@ static void RSurfShader_OpaqueWall_Pass_BaseTripleTexCombine(const entity_render
        m.texrgbscale[1] = 2;
        m.tex[2] = R_GetTexture(texture->skin.detail);
        m.texrgbscale[2] = 2;
-       GL_Color(1, 1, 1, 1);
+       if (texture->skin.glow)
+       {
+               m.tex[3] = R_GetTexture(texture->skin.glow);
+               m.texcombinergb[3] = GL_ADD;
+       }
+       if (r_shadow_realtime_world.integer)
+               GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
+       else
+               GL_Color(1, 1, 1, 1);
 
        while((surf = *surfchain++) != NULL)
        {
@@ -1005,13 +1015,14 @@ static void RSurfShader_OpaqueWall_Pass_BaseTripleTexCombine(const entity_render
                        m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
                        m.pointer_texcoord[1] = surf->mesh.data_texcoordlightmap2f;
                        m.pointer_texcoord[2] = surf->mesh.data_texcoorddetail2f;
+                       m.pointer_texcoord[3] = surf->mesh.data_texcoordtexture2f;
                        R_Mesh_State_Texture(&m);
                        R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
                }
        }
 }
 
-static void RSurfShader_OpaqueWall_Pass_BaseDoubleTexCombine(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
+static void RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetail(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
 {
        const msurface_t *surf;
        rmeshstate_t m;
@@ -1023,7 +1034,49 @@ static void RSurfShader_OpaqueWall_Pass_BaseDoubleTexCombine(const entity_render
        m.tex[0] = R_GetTexture(texture->skin.base);
        m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
        m.texrgbscale[1] = 2;
-       GL_Color(1, 1, 1, 1);
+       m.tex[2] = R_GetTexture(texture->skin.detail);
+       m.texrgbscale[2] = 2;
+       if (r_shadow_realtime_world.integer)
+               GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
+       else
+               GL_Color(1, 1, 1, 1);
+
+       while((surf = *surfchain++) != NULL)
+       {
+               if (surf->visframe == r_framecount)
+               {
+                       lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
+                       //if (m.tex[1] != lightmaptexturenum)
+                       //{
+                               m.tex[1] = lightmaptexturenum;
+                       //      R_Mesh_State_Texture(&m);
+                       //}
+                       GL_VertexPointer(surf->mesh.data_vertex3f);
+                       m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
+                       m.pointer_texcoord[1] = surf->mesh.data_texcoordlightmap2f;
+                       m.pointer_texcoord[2] = surf->mesh.data_texcoorddetail2f;
+                       R_Mesh_State_Texture(&m);
+                       R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
+               }
+       }
+}
+
+static void RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmap(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
+{
+       const msurface_t *surf;
+       rmeshstate_t m;
+       int lightmaptexturenum;
+       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;
+       if (r_shadow_realtime_world.integer)
+               GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
+       else
+               GL_Color(1, 1, 1, 1);
        while((surf = *surfchain++) != NULL)
        {
                if (surf->visframe == r_framecount)
@@ -1052,7 +1105,10 @@ static void RSurfShader_OpaqueWall_Pass_BaseTexture(const entity_render_t *ent,
        GL_DepthTest(true);
        GL_BlendFunc(GL_ONE, GL_ZERO);
        m.tex[0] = R_GetTexture(texture->skin.base);
-       GL_Color(1, 1, 1, 1);
+       if (r_shadow_realtime_world.integer)
+               GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
+       else
+               GL_Color(1, 1, 1, 1);
        while((surf = *surfchain++) != NULL)
        {
                if (surf->visframe == r_framecount)
@@ -1163,6 +1219,7 @@ static void RSurfShader_OpaqueWall_Pass_Glow(const entity_render_t *ent, const t
        }
 }
 
+/*
 static void RSurfShader_OpaqueWall_Pass_OpaqueGlow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
 {
        const msurface_t *surf;
@@ -1186,6 +1243,39 @@ static void RSurfShader_OpaqueWall_Pass_OpaqueGlow(const entity_render_t *ent, c
                }
        }
 }
+*/
+
+static void RSurfShader_OpaqueWall_Pass_BaseLightmapOnly(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
+{
+       const msurface_t *surf;
+       rmeshstate_t m;
+       int lightmaptexturenum;
+       memset(&m, 0, sizeof(m));
+       GL_BlendFunc(GL_ONE, GL_ZERO);
+       GL_DepthMask(true);
+       GL_DepthTest(true);
+       m.tex[0] = R_GetTexture((**surfchain).lightmaptexture);
+       if (r_shadow_realtime_world.integer)
+               GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
+       else
+               GL_Color(1, 1, 1, 1);
+       while((surf = *surfchain++) != NULL)
+       {
+               if (surf->visframe == r_framecount)
+               {
+                       lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
+                       //if (m.tex[0] != lightmaptexturenum)
+                       //{
+                               m.tex[0] = lightmaptexturenum;
+                       //      R_Mesh_State_Texture(&m);
+                       //}
+                       GL_VertexPointer(surf->mesh.data_vertex3f);
+                       m.pointer_texcoord[0] = surf->mesh.data_texcoordlightmap2f;
+                       R_Mesh_State_Texture(&m);
+                       R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
+               }
+       }
+}
 
 static void RSurfShader_Wall_Vertex_Callback(const void *calldata1, int calldata2)
 {
@@ -1242,45 +1332,60 @@ static void RSurfShader_Wall_Lightmap(const entity_render_t *ent, const texture_
                if (fogenabled)
                        RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
        }
-       else if (r_shadow_realtime_world.integer)
+       /*
+       // opaque base lighting
+       else if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value <= 0)
+       {
+               if (r_ambient.value > 0)
+               {
+               }
+               else
+                       RSurfShader_OpaqueWall_Pass_OpaqueGlow(ent, texture, surfchain);
+               if (fogenabled)
+                       RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
+       }
+       */
+       // opaque lightmapped
+       else if (r_textureunits.integer >= 4 && gl_combine.integer && r_detailtextures.integer && !gl_lightmaps.integer)
+       {
+               RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetailGlow(ent, texture, surfchain);
+               if (fogenabled)
+                       RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
+       }
+       else if (r_textureunits.integer >= 3 && gl_combine.integer && r_detailtextures.integer && !gl_lightmaps.integer)
        {
-               // opaque base lighting
-               RSurfShader_OpaqueWall_Pass_OpaqueGlow(ent, texture, surfchain);
+               RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetail(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_textureunits.integer >= 2 && gl_combine.integer && !gl_lightmaps.integer)
+       {
+               RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmap(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 (!gl_lightmaps.integer)
+       {
+               RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture, surfchain);
+               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);
        }
        else
        {
-               // opaque lightmapped
-               if (r_textureunits.integer >= 3 && gl_combine.integer && r_detailtextures.integer)
-               {
-                       RSurfShader_OpaqueWall_Pass_BaseTripleTexCombine(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_textureunits.integer >= 2 && gl_combine.integer)
-               {
-                       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
-               {
-                       RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture, surfchain);
-                       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);
-               }
+               RSurfShader_OpaqueWall_Pass_BaseLightmapOnly(ent, texture, surfchain);
+               if (fogenabled)
+                       RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
        }
 }
 
@@ -1549,7 +1654,7 @@ void R_SurfaceWorldNode (entity_render_t *ent)
 
 static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
 {
-       int c, leafstackpos, *mark, *surfacevisframes, bitnum;
+       int c, leafstackpos, *mark, *surfacevisframes;
 #if WORLDNODECULLBACKFACES
        int n;
        msurface_t *surf;
@@ -1613,8 +1718,7 @@ static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
                                {
                                        leaf->worldnodeframe = r_framecount;
                                        // FIXME: R_CullBox is absolute, should be done relative
-                                       bitnum = (leaf - ent->model->brushq1.leafs) - 1;
-                                       if ((r_pvsbits[bitnum >> 3] & (1 << (bitnum & 7))) && !R_CullBox(leaf->mins, leaf->maxs))
+                                       if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex) && !R_CullBox(leaf->mins, leaf->maxs))
                                                leafstack[leafstackpos++] = leaf;
                                }
                        }
@@ -1639,11 +1743,10 @@ void R_PVSUpdate (entity_render_t *ent, mleaf_t *viewleaf)
                if (viewleaf)
                {
                        surfacepvsframes = model->brushq1.surfacepvsframes;
-                       for (j = 0;j < model->brushq1.visleafs;j++)
+                       for (j = 0, leaf = model->brushq1.data_leafs;j < model->brushq1.num_leafs;j++, leaf++)
                        {
-                               if (r_pvsbits[j >> 3] & (1 << (j & 7)))
+                               if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex))
                                {
-                                       leaf = model->brushq1.leafs + j + 1;
                                        leaf->pvsframe = model->brushq1.pvsframecount;
                                        leaf->pvschain = model->brushq1.pvsleafchain;
                                        model->brushq1.pvsleafchain = leaf;
@@ -1679,7 +1782,7 @@ void R_DrawWorld(entity_render_t *ent)
 {
        if (ent->model == NULL)
                return;
-       if (!ent->model->brushq1.numleafs)
+       if (!ent->model->brushq1.num_leafs)
        {
                if (ent->model->DrawSky)
                        ent->model->DrawSky(ent);
@@ -1746,7 +1849,7 @@ void R_Model_Brush_DrawShadowVolume (entity_render_t *ent, vec3_t relativelighto
        }
 }
 
-void R_Model_Brush_DrawLight(entity_render_t *ent, 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)
+void R_Model_Brush_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, const matrix4x4_t *matrix_modeltolight, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz, rtexture_t *lightcubemap)
 {
        int surfnum;
        msurface_t *surf;
@@ -1774,8 +1877,8 @@ void R_Model_Brush_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, v
                                t = surf->texinfo->texture->currentframe;
                                if (t->rendertype == SURFRENDER_OPAQUE && t->flags & SURF_SHADOWLIGHT)
                                {
-                                       R_Shadow_DiffuseLighting(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i, surf->mesh.data_vertex3f, surf->mesh.data_svector3f, surf->mesh.data_tvector3f, surf->mesh.data_normal3f, surf->mesh.data_texcoordtexture2f, relativelightorigin, lightradius, lightcolor, matrix_modeltofilter, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, t->skin.base, t->skin.nmap, NULL);
-                                       R_Shadow_SpecularLighting(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i, surf->mesh.data_vertex3f, surf->mesh.data_svector3f, surf->mesh.data_tvector3f, surf->mesh.data_normal3f, surf->mesh.data_texcoordtexture2f, relativelightorigin, relativeeyeorigin, lightradius, lightcolor, matrix_modeltofilter, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, t->skin.gloss, t->skin.nmap, NULL);
+                                       R_Shadow_DiffuseLighting(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i, surf->mesh.data_vertex3f, surf->mesh.data_svector3f, surf->mesh.data_tvector3f, surf->mesh.data_normal3f, surf->mesh.data_texcoordtexture2f, relativelightorigin, lightradius, lightcolor, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, t->skin.base, t->skin.nmap, lightcubemap);
+                                       R_Shadow_SpecularLighting(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i, surf->mesh.data_vertex3f, surf->mesh.data_svector3f, surf->mesh.data_tvector3f, surf->mesh.data_normal3f, surf->mesh.data_texcoordtexture2f, relativelightorigin, relativeeyeorigin, lightradius, lightcolor, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, t->skin.gloss, t->skin.nmap, lightcubemap);
                                }
                        }
                }
@@ -1796,6 +1899,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;
@@ -1887,8 +1991,8 @@ 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_DepthMask(true);
+       GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
+       GL_DepthMask(false);
        GL_DepthTest(true);
        m.tex[0] = R_GetTexture(face->lightmaptexture);
        m.pointer_texcoord[0] = face->data_texcoordlightmap2f;
@@ -1898,12 +2002,30 @@ void R_Q3BSP_DrawFace_OpaqueWall_Pass_Lightmap(entity_render_t *ent, q3mface_t *
        R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
 }
 
+void R_Q3BSP_DrawFace_OpaqueWall_Pass_LightmapOnly(entity_render_t *ent, q3mface_t *face)
+{
+       rmeshstate_t m;
+       memset(&m, 0, sizeof(m));
+       GL_BlendFunc(GL_ONE, GL_ZERO);
+       GL_DepthMask(true);
+       GL_DepthTest(true);
+       m.tex[0] = R_GetTexture(face->lightmaptexture);
+       m.pointer_texcoord[0] = face->data_texcoordlightmap2f;
+       if (r_shadow_realtime_world.integer)
+               GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
+       else
+               GL_Color(1, 1, 1, 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_OpaqueWall_Pass_Glow(entity_render_t *ent, q3mface_t *face)
 {
        rmeshstate_t m;
        memset(&m, 0, sizeof(m));
        GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
-       GL_DepthMask(true);
+       GL_DepthMask(false);
        GL_DepthTest(true);
        if (face->texture->skin.glow)
        {
@@ -1920,6 +2042,8 @@ void R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(entity_render_t *ent, q3mface_t *face
 
 void R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureVertex(entity_render_t *ent, q3mface_t *face)
 {
+       int i;
+       float mul;
        rmeshstate_t m;
        memset(&m, 0, sizeof(m));
        GL_BlendFunc(GL_ONE, GL_ZERO);
@@ -1927,14 +2051,49 @@ 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;
-       if (gl_combine.integer)
+       mul = 2.0f;
+       if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value != 1)
+               mul *= r_shadow_realtime_world_lightmaps.value;
+       if (mul == 2 && gl_combine.integer)
        {
                m.texrgbscale[0] = 2;
                GL_ColorPointer(face->data_color4f);
        }
+       else if (mul == 1)
+               GL_ColorPointer(face->data_color4f);
+       else
+       {
+               for (i = 0;i < face->num_vertices;i++)
+               {
+                       varray_color4f[i*4+0] = face->data_color4f[i*4+0] * mul;
+                       varray_color4f[i*4+1] = face->data_color4f[i*4+1] * mul;
+                       varray_color4f[i*4+2] = face->data_color4f[i*4+2] * mul;
+                       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_VertexOnly(entity_render_t *ent, q3mface_t *face)
+{
+       int i;
+       float mul;
+       rmeshstate_t m;
+       memset(&m, 0, sizeof(m));
+       GL_BlendFunc(GL_ONE, GL_ZERO);
+       GL_DepthMask(true);
+       GL_DepthTest(true);
+       R_Mesh_State_Texture(&m);
+       mul = 2.0f;
+       if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value != 1)
+               mul *= r_shadow_realtime_world_lightmaps.value;
+       if (mul == 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] * 2.0f;
@@ -1944,7 +2103,6 @@ void R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureVertex(entity_render_t *ent, q3mfac
                }
                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);
 }
@@ -2035,6 +2193,7 @@ void R_Q3BSP_DrawFace(entity_render_t *ent, q3mface_t *face)
                if (face->texture->surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NODRAW))
                        return;
        }
+       c_faces++;
        face->visframe = r_framecount;
        if ((face->texture->surfaceparms & Q3SURFACEPARM_TRANS) || ent->alpha < 1 || (ent->effects & EF_ADDITIVE))
        {
@@ -2047,7 +2206,7 @@ void R_Q3BSP_DrawFace(entity_render_t *ent, q3mface_t *face)
                return;
        }
        R_Mesh_Matrix(&ent->matrix);
-       if (r_shadow_realtime_world.integer)
+       if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value <= 0)
                R_Q3BSP_DrawFace_OpaqueWall_Pass_OpaqueGlow(ent, face);
        else if ((ent->effects & EF_FULLBRIGHT) || r_fullbright.integer)
        {
@@ -2057,21 +2216,31 @@ void R_Q3BSP_DrawFace(entity_render_t *ent, q3mface_t *face)
        }
        else if (face->lightmaptexture)
        {
-               if (r_textureunits.integer >= 2 && gl_combine.integer)
-                       R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureLightmapCombine(ent, face);
+               if (gl_lightmaps.integer)
+                       R_Q3BSP_DrawFace_OpaqueWall_Pass_LightmapOnly(ent, face);
                else
                {
-                       R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(ent, face);
-                       R_Q3BSP_DrawFace_OpaqueWall_Pass_Lightmap(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);
+                               R_Q3BSP_DrawFace_OpaqueWall_Pass_Lightmap(ent, face);
+                       }
+                       if (face->texture->skin.glow)
+                               R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
                }
-               if (face->texture->skin.glow)
-                       R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
        }
        else
        {
-               R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureVertex(ent, face);
-               if (face->texture->skin.glow)
-                       R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
+               if (gl_lightmaps.integer)
+                       R_Q3BSP_DrawFace_OpaqueWall_Pass_VertexOnly(ent, face);
+               else
+               {
+                       R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureVertex(ent, face);
+                       if (face->texture->skin.glow)
+                               R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
+               }
        }
        if (r_ambient.value)
                R_Q3BSP_DrawFace_OpaqueWall_Pass_AddTextureAmbient(ent, face);
@@ -2081,19 +2250,42 @@ void R_Q3BSP_RecursiveWorldNode(entity_render_t *ent, q3mnode_t *node, const vec
 {
        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)))
+       if (CHECKPVSBIT(pvs, leaf->clusterindex))
+       {
+               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 (CHECKPVSBIT(pvs, leaf->clusterindex))
+               {
+                       c_leafs++;
+                       for (i = 0;i < leaf->numleaffaces;i++)
+                               leaf->firstleafface[i]->markframe = markframe;
+               }
+       }
 }
 
 static int r_q3bsp_framecount = -1;
@@ -2116,6 +2308,7 @@ void R_Q3BSP_DrawSky(entity_render_t *ent)
                        {
                                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->surfaceflags & Q3SURFACEFLAG_SKY) && !R_CullBox(face->mins, face->maxs))
@@ -2146,6 +2339,7 @@ void R_Q3BSP_Draw(entity_render_t *ent)
                        {
                                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))
@@ -2163,9 +2357,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);
        }
 }
 
@@ -2197,15 +2393,15 @@ 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)
+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_modeltolight, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz, rtexture_t *lightcubemap)
 {
        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);
+       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_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, face->texture->skin.base, face->texture->skin.nmap, lightcubemap);
+       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_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, face->texture->skin.gloss, face->texture->skin.nmap, lightcubemap);
 }
 
-void R_Q3BSP_DrawLight(entity_render_t *ent, 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)
+void R_Q3BSP_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, const matrix4x4_t *matrix_modeltolight, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz, rtexture_t *lightcubemap)
 {
        int i;
        q3mface_t *face;
@@ -2229,7 +2425,7 @@ void R_Q3BSP_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t
                //else
                        for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
                                if ((ent != &cl_entities[0].render || face->visframe == r_framecount) && BoxesOverlap(lightmins, lightmaxs, face->mins, face->maxs))
-                                       R_Q3BSP_DrawFaceLight(ent, face, relativelightorigin, relativeeyeorigin, lightradius, lightcolor, matrix_modeltofilter, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz);
+                                       R_Q3BSP_DrawFaceLight(ent, face, relativelightorigin, relativeeyeorigin, lightradius, lightcolor, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, lightcubemap);
        }
 }
 
@@ -2258,7 +2454,9 @@ 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);
+       Cvar_RegisterVariable(&gl_lightmaps);
 
        R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);
 }