]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_rsurf.c
some renderer/client separation cleanup, migrated some things to r_refdef to eliminat...
[xonotic/darkplaces.git] / gl_rsurf.c
index 87ad132a8e80f8f47d4834a602b89f2260414f0a..32a34c0bdf6a65ef02e8e15a1950127a38ed068b 100644 (file)
@@ -563,7 +563,7 @@ void R_Stain (const vec3_t origin, float radius, int cr1, int cg1, int cb1, int
        entity_render_t *ent;
        model_t *model;
        vec3_t org;
-       if (cl.worldmodel == NULL || !cl.worldmodel->brushq1.nodes)
+       if (r_refdef.worldmodel == NULL || !r_refdef.worldmodel->brushq1.nodes)
                return;
        fcolor[0] = cr1;
        fcolor[1] = cg1;
@@ -574,7 +574,7 @@ void R_Stain (const vec3_t origin, float radius, int cr1, int cg1, int cb1, int
        fcolor[6] = cb2 - cb1;
        fcolor[7] = (ca2 - ca1) * (1.0f / 64.0f);
 
-       R_StainNode(cl.worldmodel->brushq1.nodes + cl.worldmodel->brushq1.hulls[0].firstclipnode, cl.worldmodel, origin, radius, fcolor);
+       R_StainNode(r_refdef.worldmodel->brushq1.nodes + r_refdef.worldmodel->brushq1.hulls[0].firstclipnode, r_refdef.worldmodel, origin, radius, fcolor);
 
        // look for embedded bmodels
        for (n = 0;n < cl_num_brushmodel_entities;n++)
@@ -802,7 +802,7 @@ static void RSurfShader_Transparent_Callback(const void *calldata1, int calldata
 
        texture = surf->texinfo->texture;
        if (texture->animated)
-               texture = texture->anim_frames[ent->frame != 0][(texture->anim_total[ent->frame != 0] >= 2) ? ((int) (cl.time * 5.0f) % texture->anim_total[ent->frame != 0]) : 0];
+               texture = texture->anim_frames[ent->frame != 0][(texture->anim_total[ent->frame != 0] >= 2) ? ((int) (r_refdef.time * 5.0f) % texture->anim_total[ent->frame != 0]) : 0];
        currentalpha = ent->alpha;
        if (surf->flags & SURF_WATERALPHA)
                currentalpha *= r_wateralpha.value;
@@ -832,20 +832,20 @@ static void RSurfShader_Transparent_Callback(const void *calldata1, int calldata
        base = fullbright ? 2.0f : r_ambient.value * (1.0f / 64.0f);
        if (surf->flags & SURF_DRAWTURB)
                base *= 0.5f;
-       if ((surf->flags & SURF_DRAWTURB) && gl_textureshader && r_watershader.value && !fogenabled && fullbright)
+       if ((surf->flags & SURF_DRAWTURB) && gl_textureshader && r_watershader.value && !fogenabled && fullbright && ent->colormod[0] == 1 && ent->colormod[1] == 1 && ent->colormod[2] == 1)
        {
                // NVIDIA Geforce3 distortion texture shader on water
                GL_Color(1, 1, 1, currentalpha);
                memset(&m, 0, sizeof(m));
                m.pointer_vertex = surf->mesh.data_vertex3f;
-               m.tex[0] = R_GetTexture(mod_shared_distorttexture[(int)(cl.time * 16)&63]);
+               m.tex[0] = R_GetTexture(mod_shared_distorttexture[(int)(r_refdef.time * 16)&63]);
                m.tex[1] = R_GetTexture(texture->skin.base);
                m.texcombinergb[0] = GL_REPLACE;
                m.texcombinergb[1] = GL_REPLACE;
                m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
                m.pointer_texcoord[1] = surf->mesh.data_texcoordtexture2f;
                Matrix4x4_CreateFromQuakeEntity(&m.texmatrix[0], 0, 0, 0, 0, 0, 0, r_watershader.value);
-               Matrix4x4_CreateTranslate(&m.texmatrix[1], sin(cl.time) * 0.025 * r_waterscroll.value, sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
+               Matrix4x4_CreateTranslate(&m.texmatrix[1], sin(r_refdef.time) * 0.025 * r_waterscroll.value, sin(r_refdef.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
                R_Mesh_State(&m);
 
                GL_ActiveTexture(0);
@@ -874,7 +874,7 @@ static void RSurfShader_Transparent_Callback(const void *calldata1, int calldata
                if (turb)
                {
                        // scrolling in texture matrix
-                       Matrix4x4_CreateTranslate(&m.texmatrix[0], sin(cl.time) * 0.025 * r_waterscroll.value, sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
+                       Matrix4x4_CreateTranslate(&m.texmatrix[0], sin(r_refdef.time) * 0.025 * r_waterscroll.value, sin(r_refdef.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
                }
                colorscale = 1;
                if (gl_combine.integer)
@@ -882,7 +882,7 @@ static void RSurfShader_Transparent_Callback(const void *calldata1, int calldata
                        m.texrgbscale[0] = 4;
                        colorscale *= 0.25f;
                }
-               R_FillColors(varray_color4f, surf->mesh.num_vertices, base, base, base, currentalpha);
+               R_FillColors(varray_color4f, surf->mesh.num_vertices, base * ent->colormod[0], base * ent->colormod[1], base * ent->colormod[2], currentalpha);
                if (!fullbright)
                {
                        if (surf->dlightframe == r_framecount)
@@ -909,7 +909,7 @@ static void RSurfShader_Transparent_Callback(const void *calldata1, int calldata
                                if (turb)
                                {
                                        // scrolling in texture matrix
-                                       Matrix4x4_CreateTranslate(&m.texmatrix[0], sin(cl.time) * 0.025 * r_waterscroll.value, sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
+                                       Matrix4x4_CreateTranslate(&m.texmatrix[0], sin(r_refdef.time) * 0.025 * r_waterscroll.value, sin(r_refdef.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
                                }
                        }
                        R_Mesh_State(&m);
@@ -932,7 +932,7 @@ static void RSurfShader_Transparent_Callback(const void *calldata1, int calldata
                                if (turb)
                                {
                                        // scrolling in texture matrix
-                                       Matrix4x4_CreateTranslate(&m.texmatrix[0], sin(cl.time) * 0.025 * r_waterscroll.value, sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
+                                       Matrix4x4_CreateTranslate(&m.texmatrix[0], sin(r_refdef.time) * 0.025 * r_waterscroll.value, sin(r_refdef.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
                                }
                        }
                        R_Mesh_State(&m);
@@ -963,7 +963,7 @@ static void RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetailGlow(co
                m.tex[3] = R_GetTexture(texture->skin.glow);
                m.texcombinergb[3] = GL_ADD;
        }
-       GL_Color(r_lightmapintensity, r_lightmapintensity, r_lightmapintensity, 1);
+       GL_Color(r_lightmapintensity * ent->colormod[0], r_lightmapintensity * ent->colormod[1], r_lightmapintensity * ent->colormod[2], 1);
 
        while((surf = *surfchain++) != NULL)
        {
@@ -998,7 +998,7 @@ static void RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetail(const
        m.texrgbscale[1] = 2;
        m.tex[2] = R_GetTexture(texture->skin.detail);
        m.texrgbscale[2] = 2;
-       GL_Color(r_lightmapintensity, r_lightmapintensity, r_lightmapintensity, 1);
+       GL_Color(r_lightmapintensity * ent->colormod[0], r_lightmapintensity * ent->colormod[1], r_lightmapintensity * ent->colormod[2], 1);
 
        while((surf = *surfchain++) != NULL)
        {
@@ -1030,7 +1030,7 @@ static void RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmap(const entity
        m.tex[0] = R_GetTexture(texture->skin.base);
        m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
        m.texrgbscale[1] = 2;
-       GL_Color(r_lightmapintensity, r_lightmapintensity, r_lightmapintensity, 1);
+       GL_Color(r_lightmapintensity * ent->colormod[0], r_lightmapintensity * ent->colormod[1], r_lightmapintensity * ent->colormod[2], 1);
        while((surf = *surfchain++) != NULL)
        {
                if (surf->visframe == r_framecount)
@@ -1058,9 +1058,9 @@ static void RSurfShader_OpaqueWall_Pass_BaseTexture(const entity_render_t *ent,
        GL_BlendFunc(GL_ONE, GL_ZERO);
        m.tex[0] = R_GetTexture(texture->skin.base);
        if (ent->flags & RENDER_LIGHT)
-               GL_Color(r_lightmapintensity, r_lightmapintensity, r_lightmapintensity, 1);
+               GL_Color(r_lightmapintensity * ent->colormod[0], r_lightmapintensity * ent->colormod[1], r_lightmapintensity * ent->colormod[2], 1);
        else
-               GL_Color(1, 1, 1, 1);
+               GL_Color(ent->colormod[0], ent->colormod[1], ent->colormod[2], 1);
        while((surf = *surfchain++) != NULL)
        {
                if (surf->visframe == r_framecount)
@@ -1111,7 +1111,7 @@ static void RSurfShader_OpaqueWall_Pass_BaseAmbient(const entity_render_t *ent,
        GL_DepthMask(false);
        GL_DepthTest(true);
        m.tex[0] = R_GetTexture(texture->skin.base);
-       GL_Color(r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), 1);
+       GL_Color(r_ambient.value * (1.0f / 128.0f) * ent->colormod[0], r_ambient.value * (1.0f / 128.0f) * ent->colormod[1], r_ambient.value * (1.0f / 128.0f) * ent->colormod[2], 1);
        while((surf = *surfchain++) != NULL)
        {
                if (surf->visframe == r_framecount)
@@ -1265,7 +1265,7 @@ void R_UpdateTextureInfo(entity_render_t *ent)
                return;
 
        alttextures = ent->frame != 0;
-       texframe = (int)(cl.time * 5.0f);
+       texframe = (int)(r_refdef.time * 5.0f);
        for (i = 0;i < ent->model->brushq1.numtextures;i++)
        {
                t = ent->model->brushq1.textures + i;
@@ -1299,8 +1299,9 @@ void R_UpdateLightmapInfo(entity_render_t *ent)
                if (model->brushq1.light_stylevalue[i] != d_lightstylevalue[model->brushq1.light_style[i]])
                {
                        model->brushq1.light_stylevalue[i] = d_lightstylevalue[model->brushq1.light_style[i]];
-                       for (surfacechain = model->brushq1.light_styleupdatechains[i];(surface = *surfacechain);surfacechain++)
-                               surface->cached_dlight = true;
+                       if ((surfacechain = model->brushq1.light_styleupdatechains[i]))
+                               for (;(surface = *surfacechain);surfacechain++)
+                                       surface->cached_dlight = true;
                }
        }
 }
@@ -1446,18 +1447,17 @@ static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
        int i;
        float *v;
        rmeshstate_t m;
-       const entity_render_t *ent = calldata1;
-       const mportal_t *portal = ent->model->brushq1.portals + calldata2;
+       const mportal_t *portal = calldata1;
        GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        GL_DepthMask(false);
        GL_DepthTest(true);
-       R_Mesh_Matrix(&ent->matrix);
+       R_Mesh_Matrix(&r_identitymatrix);
 
        memset(&m, 0, sizeof(m));
        m.pointer_vertex = varray_vertex3f;
        R_Mesh_State(&m);
 
-       i = portal - ent->model->brushq1.portals;
+       i = calldata2;
        GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f),
                         ((i & 0x0038) >> 3) * (1.0f / 7.0f),
                         ((i & 0x01C0) >> 6) * (1.0f / 7.0f),
@@ -1476,24 +1476,24 @@ static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
 }
 
 // LordHavoc: this is just a nice debugging tool, very slow
-static void R_DrawPortals(entity_render_t *ent)
+static void R_DrawPortals(void)
 {
-       int i;
-       mportal_t *portal, *endportal;
-       float temp[3], center[3], f;
-       if (ent->model == NULL)
+       int i, portalnum;
+       mportal_t *portal;
+       float center[3], f;
+       model_t *model = r_refdef.worldmodel;
+       if (model == NULL)
                return;
-       for (portal = ent->model->brushq1.portals, endportal = portal + ent->model->brushq1.numportals;portal < endportal;portal++)
+       for (portalnum = 0, portal = model->brushq1.portals;portalnum < model->brushq1.numportals;portalnum++, portal++)
        {
                if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
                {
-                       VectorClear(temp);
+                       VectorClear(center);
                        for (i = 0;i < portal->numpoints;i++)
-                               VectorAdd(temp, portal->points[i].position, temp);
+                               VectorAdd(center, portal->points[i].position, center);
                        f = ixtable[portal->numpoints];
-                       VectorScale(temp, f, temp);
-                       Matrix4x4_Transform(&ent->matrix, temp, center);
-                       R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, ent, portal - ent->model->brushq1.portals);
+                       VectorScale(center, f, center);
+                       R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, portal, portalnum);
                }
        }
 }
@@ -1539,21 +1539,19 @@ void R_PrepareBrushModel(entity_render_t *ent)
        R_UpdateLightmapInfo(ent);
 }
 
-void R_SurfaceWorldNode (entity_render_t *ent)
+void R_SurfaceWorldNode (void)
 {
        int i, *surfacevisframes, *surfacepvsframes, surfnum;
        msurface_t *surf;
        mleaf_t *leaf;
        model_t *model;
-       vec3_t modelorg;
 
        // equivilant to quake's RecursiveWorldNode but faster and more effective
-       model = ent->model;
+       model = r_refdef.worldmodel;
        if (model == NULL)
                return;
        surfacevisframes = model->brushq1.surfacevisframes + model->firstmodelsurface;
        surfacepvsframes = model->brushq1.surfacepvsframes + model->firstmodelsurface;
-       Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
 
        for (leaf = model->brushq1.pvsleafchain;leaf;leaf = leaf->pvschain)
        {
@@ -1569,7 +1567,7 @@ void R_SurfaceWorldNode (entity_render_t *ent)
                surfnum = model->brushq1.pvssurflist[i];
                surf = model->brushq1.surfaces + surfnum;
 #if WORLDNODECULLBACKFACES
-               if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
+               if (PlaneDist(r_vieworigin, surf->plane) < surf->plane->dist)
                {
                        if ((surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
                                surfacevisframes[surfnum] = r_framecount;
@@ -1586,7 +1584,7 @@ void R_SurfaceWorldNode (entity_render_t *ent)
        }
 }
 
-static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
+static void R_PortalWorldNode(mleaf_t *viewleaf)
 {
        int c, leafstackpos, *mark, *surfacevisframes;
 #if WORLDNODECULLBACKFACES
@@ -1595,17 +1593,16 @@ static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
 #endif
        mleaf_t *leaf, *leafstack[8192];
        mportal_t *p;
-       vec3_t modelorg;
        msurface_t *surfaces;
-       if (ent->model == NULL)
+       model_t *model = r_refdef.worldmodel;
+       if (model == NULL)
                return;
        // LordHavoc: portal-passage worldnode with PVS;
        // follows portals leading outward from viewleaf, does not venture
        // offscreen or into leafs that are not visible, faster than Quake's
        // RecursiveWorldNode
-       surfaces = ent->model->brushq1.surfaces;
-       surfacevisframes = ent->model->brushq1.surfacevisframes;
-       Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
+       surfaces = model->brushq1.surfaces;
+       surfacevisframes = model->brushq1.surfacevisframes;
        viewleaf->worldnodeframe = r_framecount;
        leafstack[0] = viewleaf;
        leafstackpos = 1;
@@ -1624,7 +1621,7 @@ static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
                                if (surfacevisframes[n] != r_framecount)
                                {
                                        surf = surfaces + n;
-                                       if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
+                                       if (PlaneDist(r_vieworigin, surf->plane) < surf->plane->dist)
                                        {
                                                if ((surf->flags & SURF_PLANEBACK))
                                                        surfacevisframes[n] = r_framecount;
@@ -1645,7 +1642,7 @@ static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
                {
                        // LordHavoc: this DotProduct hurts less than a cache miss
                        // (which is more likely to happen if backflowing through leafs)
-                       if (DotProduct(modelorg, p->plane.normal) < (p->plane.dist + 1))
+                       if (DotProduct(r_vieworigin, p->plane.normal) < (p->plane.dist + 1))
                        {
                                leaf = p->past;
                                if (leaf->worldnodeframe != r_framecount)
@@ -1660,13 +1657,13 @@ static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
        }
 }
 
-void R_PVSUpdate (entity_render_t *ent, mleaf_t *viewleaf)
+void R_PVSUpdate (mleaf_t *viewleaf)
 {
        int j, c, *surfacepvsframes, *mark;
        mleaf_t *leaf;
        model_t *model;
 
-       model = ent->model;
+       model = r_refdef.worldmodel;
        if (model && (model->brushq1.pvsviewleaf != viewleaf || model->brushq1.pvsviewleafnovis != r_novis.integer))
        {
                model->brushq1.pvsframecount++;
@@ -1694,32 +1691,30 @@ void R_PVSUpdate (entity_render_t *ent, mleaf_t *viewleaf)
        }
 }
 
-void R_WorldVisibility(entity_render_t *ent)
+void R_WorldVisibility(void)
 {
-       vec3_t modelorg;
        mleaf_t *viewleaf;
 
-       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);
+       viewleaf = (r_refdef.worldmodel && r_refdef.worldmodel->brushq1.PointInLeaf) ? r_refdef.worldmodel->brushq1.PointInLeaf(r_refdef.worldmodel, r_vieworigin) : NULL;
+       R_PVSUpdate(viewleaf);
 
        if (!viewleaf)
                return;
 
        if (r_surfaceworldnode.integer || viewleaf->contents == CONTENTS_SOLID)
-               R_SurfaceWorldNode (ent);
+               R_SurfaceWorldNode();
        else
-               R_PortalWorldNode (ent, viewleaf);
+               R_PortalWorldNode(viewleaf);
 
        if (r_drawportals.integer)
-               R_DrawPortals(ent);
+               R_DrawPortals();
 }
 
 void R_Q1BSP_DrawSky(entity_render_t *ent)
 {
        if (ent->model == NULL)
                return;
-       if (ent != &cl_entities[0].render)
+       if (ent != r_refdef.worldentity)
                R_PrepareBrushModel(ent);
        R_PrepareSurfaces(ent);
        R_DrawSurfaces(ent, SURF_DRAWSKY);
@@ -1730,7 +1725,7 @@ void R_Q1BSP_Draw(entity_render_t *ent)
        if (ent->model == NULL)
                return;
        c_bmodels++;
-       if (ent != &cl_entities[0].render)
+       if (ent != r_refdef.worldentity)
                R_PrepareBrushModel(ent);
        R_PrepareSurfaces(ent);
        R_UpdateTextureInfo(ent);
@@ -1855,7 +1850,7 @@ void R_Q1BSP_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin,
        }
 }
 
-void R_Q1BSP_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 numsurfaces, const int *surfacelist)
+void R_Q1BSP_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, vec_t ambientscale, vec_t diffusescale, vec_t specularscale, int numsurfaces, const int *surfacelist)
 {
        model_t *model = ent->model;
        vec3_t lightmins, lightmaxs, modelorg;
@@ -1883,12 +1878,12 @@ void R_Q1BSP_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t
                                if (t->flags & SURF_LIGHTMAP && t->skin.fog == NULL)
                                        Mod_ShadowMesh_AddMesh(r_shadow_mempool, r_shadow_compilingrtlight->static_meshchain_light, surface->texinfo->texture->skin.base, surface->texinfo->texture->skin.gloss, surface->texinfo->texture->skin.nmap, surface->mesh.data_vertex3f, surface->mesh.data_svector3f, surface->mesh.data_tvector3f, surface->mesh.data_normal3f, surface->mesh.data_texcoordtexture2f, surface->mesh.num_triangles, surface->mesh.data_element3i);
                        }
-                       else if (ent != &cl_entities[0].render || surface->visframe == r_framecount)
+                       else if (ent != r_refdef.worldentity || surface->visframe == r_framecount)
                        {
                                t = surface->texinfo->texture->currentframe;
                                // FIXME: transparent surfaces need to be lit later
                                if (t->flags & SURF_LIGHTMAP && t->rendertype == SURFRENDER_OPAQUE)
-                                       R_Shadow_RenderLighting(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i, surface->mesh.data_vertex3f, surface->mesh.data_svector3f, surface->mesh.data_tvector3f, surface->mesh.data_normal3f, surface->mesh.data_texcoordtexture2f, relativelightorigin, relativeeyeorigin, lightcolor, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, t->skin.base, t->skin.nmap, t->skin.gloss, lightcubemap, LIGHTING_DIFFUSE | LIGHTING_SPECULAR);
+                                       R_Shadow_RenderLighting(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i, surface->mesh.data_vertex3f, surface->mesh.data_svector3f, surface->mesh.data_tvector3f, surface->mesh.data_normal3f, surface->mesh.data_texcoordtexture2f, relativelightorigin, relativeeyeorigin, lightcolor, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, t->skin.base, t->skin.nmap, t->skin.gloss, lightcubemap, ambientscale, diffusescale, specularscale);
                        }
                }
        }
@@ -1931,7 +1926,7 @@ void R_Q3BSP_DrawFace_TransparentCallback(const void *voident, int facenumber)
        rmeshstate_t m;
        R_Mesh_Matrix(&ent->matrix);
        memset(&m, 0, sizeof(m));
-       if (ent->effects & EF_ADDITIVE)
+       if ((ent->effects & EF_ADDITIVE) || (face->texture->textureflags & Q3TEXTUREFLAG_ADDITIVE))
                GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
        else
                GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@@ -1947,20 +1942,20 @@ void R_Q3BSP_DrawFace_TransparentCallback(const void *voident, int facenumber)
                {
                        m.tex[1] = R_GetTexture(face->lightmaptexture);
                        m.pointer_texcoord[1] = face->data_texcoordlightmap2f;
-                       GL_Color(1, 1, 1, ent->alpha);
+                       GL_Color(ent->colormod[0], ent->colormod[1], ent->colormod[2], ent->alpha);
                }
                else
                {
-                       if (ent->alpha == 1)
+                       if (ent->colormod[0] == 1 && ent->colormod[1] == 1 && ent->colormod[2] == 1 && ent->alpha == 1)
                                m.pointer_color = 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+0] = face->data_color4f[i*4+0] * ent->colormod[0];
+                                       varray_color4f[i*4+1] = face->data_color4f[i*4+1] * ent->colormod[1];
+                                       varray_color4f[i*4+2] = face->data_color4f[i*4+2] * ent->colormod[2];
                                        varray_color4f[i*4+3] = face->data_color4f[i*4+3] * ent->alpha;
                                }
                                m.pointer_color = varray_color4f;
@@ -1972,20 +1967,66 @@ void R_Q3BSP_DrawFace_TransparentCallback(const void *voident, int facenumber)
                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+0] = face->data_color4f[i*4+0] * ent->colormod[0] * 2.0f;
+                       varray_color4f[i*4+1] = face->data_color4f[i*4+1] * ent->colormod[1] * 2.0f;
+                       varray_color4f[i*4+2] = face->data_color4f[i*4+2] * ent->colormod[2] * 2.0f;
                        varray_color4f[i*4+3] = face->data_color4f[i*4+3] * ent->alpha;
                }
                m.pointer_color = varray_color4f;
        }
-       m.pointer_vertex = face->data_vertex3f;
+       if (face->texture->textureflags & (Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2))
+       {
+               int i, j;
+               float center[3], center2[3], forward[3], right[3], up[3], v[4][3];
+               matrix4x4_t matrix1, imatrix1;
+               R_Mesh_Matrix(&r_identitymatrix);
+               // a single autosprite surface can contain multiple sprites...
+               for (j = 0;j < face->num_vertices - 3;j += 4)
+               {
+                       VectorClear(center);
+                       for (i = 0;i < 4;i++)
+                               VectorAdd(center, face->data_vertex3f + (j+i) * 3, center);
+                       VectorScale(center, 0.25f, center);
+                       Matrix4x4_Transform(&ent->matrix, center, center2);
+                       // FIXME: calculate vectors from triangle edges instead of using texture vectors as an easy way out?
+                       Matrix4x4_FromVectors(&matrix1, face->data_normal3f + j*3, face->data_svector3f + j*3, face->data_tvector3f + j*3, center);
+                       Matrix4x4_Invert_Simple(&imatrix1, &matrix1);
+                       for (i = 0;i < 4;i++)
+                               Matrix4x4_Transform(&imatrix1, face->data_vertex3f + (j+i)*3, v[i]);
+                       if (face->texture->textureflags & Q3TEXTUREFLAG_AUTOSPRITE2)
+                       {
+                               forward[0] = r_vieworigin[0] - center2[0];
+                               forward[1] = r_vieworigin[1] - center2[1];
+                               forward[2] = 0;
+                               VectorNormalize(forward);
+                               right[0] = forward[1];
+                               right[1] = -forward[0];
+                               right[2] = 0;
+                               up[0] = 0;
+                               up[1] = 0;
+                               up[2] = 1;
+                       }
+                       else
+                       {
+                               VectorCopy(r_viewforward, forward);
+                               VectorCopy(r_viewright, right);
+                               VectorCopy(r_viewup, up);
+                       }
+                       for (i = 0;i < 4;i++)
+                               VectorMAMAMAM(1, center2, v[i][0], forward, v[i][1], right, v[i][2], up, varray_vertex3f + (i+j) * 3);
+               }
+               m.pointer_vertex = varray_vertex3f;
+       }
+       else
+               m.pointer_vertex = face->data_vertex3f;
        R_Mesh_State(&m);
-       qglDisable(GL_CULL_FACE);
+       if (face->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
+               qglDisable(GL_CULL_FACE);
        GL_LockArrays(0, face->num_vertices);
        R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
        GL_LockArrays(0, 0);
-       qglEnable(GL_CULL_FACE);
+       if (face->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
+               qglEnable(GL_CULL_FACE);
 }
 
 void R_Q3BSP_DrawFaceList(entity_render_t *ent, q3mtexture_t *t, int texturenumfaces, q3msurface_t **texturefacelist)
@@ -2063,6 +2104,7 @@ void R_Q3BSP_DrawFaceList(entity_render_t *ent, q3mtexture_t *t, int texturenumf
                GL_DepthMask(true);
                GL_DepthTest(true);
                GL_BlendFunc(GL_ONE, GL_ZERO);
+               qglDisable(GL_CULL_FACE);
                memset(&m, 0, sizeof(m));
                for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
                {
@@ -2082,6 +2124,7 @@ void R_Q3BSP_DrawFaceList(entity_render_t *ent, q3mtexture_t *t, int texturenumf
                        R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
                        GL_LockArrays(0, 0);
                }
+               qglEnable(GL_CULL_FACE);
                return;
        }
        // anything else is a typical wall, lightmap * texture + glow
@@ -2090,7 +2133,9 @@ void R_Q3BSP_DrawFaceList(entity_render_t *ent, q3mtexture_t *t, int texturenumf
                GL_DepthMask(true);
                GL_DepthTest(true);
                GL_BlendFunc(GL_ONE, GL_ZERO);
-               GL_Color(1, 1, 1, 1);
+               GL_Color(ent->colormod[0], ent->colormod[1], ent->colormod[2], 1);
+               if (t->textureflags & Q3TEXTUREFLAG_TWOSIDED)
+                       qglDisable(GL_CULL_FACE);
                memset(&m, 0, sizeof(m));
                m.tex[0] = R_GetTexture(t->skin.base);
                for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
@@ -2105,6 +2150,7 @@ void R_Q3BSP_DrawFaceList(entity_render_t *ent, q3mtexture_t *t, int texturenumf
                }
                if (t->skin.glow)
                {
+                       GL_Color(1, 1, 1, 1);
                        GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
                        GL_DepthMask(false);
                        m.tex[0] = R_GetTexture(t->skin.glow);
@@ -2119,8 +2165,12 @@ void R_Q3BSP_DrawFaceList(entity_render_t *ent, q3mtexture_t *t, int texturenumf
                                GL_LockArrays(0, 0);
                        }
                }
+               if (t->textureflags & Q3TEXTUREFLAG_TWOSIDED)
+                       qglEnable(GL_CULL_FACE);
                return;
        }
+       if (t->textureflags & Q3TEXTUREFLAG_TWOSIDED)
+               qglDisable(GL_CULL_FACE);
        if (r_lightmapintensity <= 0)
        {
                GL_DepthMask(true);
@@ -2155,19 +2205,19 @@ void R_Q3BSP_DrawFaceList(entity_render_t *ent, q3mtexture_t *t, int texturenumf
                        m.texrgbscale[1] = 2;
                        if (face->lightmaptexture)
                        {
-                               GL_Color(r_lightmapintensity, r_lightmapintensity, r_lightmapintensity, 1);
+                               GL_Color(r_lightmapintensity * ent->colormod[0], r_lightmapintensity * ent->colormod[1], r_lightmapintensity * ent->colormod[2], 1);
                                m.pointer_color = NULL;
                        }
-                       else if (r_lightmapintensity == 1)
+                       else if (r_lightmapintensity == 1 && ent->colormod[0] == 1 && ent->colormod[1] == 1 && ent->colormod[2] == 1)
                                m.pointer_color = face->data_color4f;
                        else
                        {
                                m.pointer_color = varray_color4f;
                                for (i = 0;i < face->num_vertices;i++)
                                {
-                                       varray_color4f[i*4+0] = face->data_color4f[i*4+0] * r_lightmapintensity;
-                                       varray_color4f[i*4+1] = face->data_color4f[i*4+1] * r_lightmapintensity;
-                                       varray_color4f[i*4+2] = face->data_color4f[i*4+2] * r_lightmapintensity;
+                                       varray_color4f[i*4+0] = face->data_color4f[i*4+0] * ent->colormod[0] * r_lightmapintensity;
+                                       varray_color4f[i*4+1] = face->data_color4f[i*4+1] * ent->colormod[1] * r_lightmapintensity;
+                                       varray_color4f[i*4+2] = face->data_color4f[i*4+2] * ent->colormod[2] * r_lightmapintensity;
                                        varray_color4f[i*4+3] = face->data_color4f[i*4+3];
                                }
                        }
@@ -2202,7 +2252,7 @@ void R_Q3BSP_DrawFaceList(entity_render_t *ent, q3mtexture_t *t, int texturenumf
                }
                GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
                GL_DepthMask(false);
-               GL_Color(r_lightmapintensity, r_lightmapintensity, r_lightmapintensity, 1);
+               GL_Color(r_lightmapintensity * ent->colormod[0], r_lightmapintensity * ent->colormod[1], r_lightmapintensity * ent->colormod[2], 1);
                memset(&m, 0, sizeof(m));
                m.tex[0] = R_GetTexture(t->skin.base);
                for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
@@ -2220,7 +2270,7 @@ void R_Q3BSP_DrawFaceList(entity_render_t *ent, q3mtexture_t *t, int texturenumf
        {
                GL_BlendFunc(GL_ONE, GL_ONE);
                GL_DepthMask(false);
-               GL_Color(r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), 1);
+               GL_Color(r_ambient.value * (1.0f / 128.0f) * ent->colormod[0], r_ambient.value * (1.0f / 128.0f) * ent->colormod[1], r_ambient.value * (1.0f / 128.0f) * ent->colormod[2], 1);
                memset(&m, 0, sizeof(m));
                m.tex[0] = R_GetTexture(t->skin.base);
                for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
@@ -2252,6 +2302,8 @@ void R_Q3BSP_DrawFaceList(entity_render_t *ent, q3mtexture_t *t, int texturenumf
                        GL_LockArrays(0, 0);
                }
        }
+       if (t->textureflags & Q3TEXTUREFLAG_TWOSIDED)
+               qglEnable(GL_CULL_FACE);
 }
 
 void R_Q3BSP_DrawFaces(entity_render_t *ent, int skyfaces)
@@ -2270,12 +2322,12 @@ void R_Q3BSP_DrawFaces(entity_render_t *ent, int skyfaces)
                flags = Q3SURFACEFLAG_SKY;
        else
                flags = 0;
-       if (ent == &cl_entities[0].render)
+       if (ent == r_refdef.worldentity)
        {
                int j;
                q3mleaf_t *leaf;
-               R_Surf_ClearSurfaceVisible(cl.worldmodel->brushq3.num_faces);
-               for (j = 0, leaf = cl.worldmodel->brushq3.data_leafs;j < cl.worldmodel->brushq3.num_leafs;j++, leaf++)
+               R_Surf_ClearSurfaceVisible(r_refdef.worldmodel->brushq3.num_faces);
+               for (j = 0, leaf = r_refdef.worldmodel->brushq3.data_leafs;j < r_refdef.worldmodel->brushq3.num_leafs;j++, leaf++)
                {
                        if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex) && !R_CullBox(leaf->mins, leaf->maxs))
                        {
@@ -2294,7 +2346,7 @@ void R_Q3BSP_DrawFaces(entity_render_t *ent, int skyfaces)
                                        if (r_surf_surfacevisible[t->facenumlist[i]])
                                        {
                                                face = t->facelist[i];
-                                               if (!R_CullBox(face->mins, face->maxs) && face->numtriangles)
+                                               if (!R_CullBox(face->mins, face->maxs) && face->num_triangles)
                                                {
                                                        if (numfaces >= maxfaces)
                                                        {
@@ -2317,7 +2369,7 @@ void R_Q3BSP_DrawFaces(entity_render_t *ent, int skyfaces)
                numfaces = 0;
                for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
                {
-                       if ((face->texture->surfaceflags & flagsmask) == flags && face->numtriangles)
+                       if ((face->texture->surfaceflags & flagsmask) == flags && face->num_triangles)
                        {                                                     
                                if (t != face->texture || numfaces >= maxfaces)
                                {
@@ -2422,18 +2474,36 @@ void R_Q3BSP_GetLightInfo(entity_render_t *ent, vec3_t relativelightorigin, floa
                                        surfaceindex = surface - model->brushq3.data_faces;
                                        if (!CHECKPVSBIT(outsurfacepvs, surfaceindex))
                                        {
-                                               if (BoxesOverlap(lightmins, lightmaxs, surface->mins, surface->maxs) && !(surface->texture->surfaceparms & Q3SURFACEPARM_TRANS) && !(surface->texture->surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NODRAW)) && surface->numtriangles)
+                                               if (BoxesOverlap(lightmins, lightmaxs, surface->mins, surface->maxs) && !(surface->texture->surfaceparms & Q3SURFACEPARM_TRANS) && !(surface->texture->surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NODRAW)) && surface->num_triangles)
                                                {
-                                                       for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->num_triangles;triangleindex++, t++, e += 3)
+                                                       if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
                                                        {
-                                                               v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
-                                                               v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
-                                                               v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
-                                                               if (PointInfrontOfTriangle(relativelightorigin, v[0], v[1], v[2]) && lightmaxs[0] > min(v[0][0], min(v[1][0], v[2][0])) && lightmins[0] < max(v[0][0], max(v[1][0], v[2][0])) && lightmaxs[1] > min(v[0][1], min(v[1][1], v[2][1])) && lightmins[1] < max(v[0][1], max(v[1][1], v[2][1])) && lightmaxs[2] > min(v[0][2], min(v[1][2], v[2][2])) && lightmins[2] < max(v[0][2], max(v[1][2], v[2][2])))
+                                                               for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->num_triangles;triangleindex++, t++, e += 3)
                                                                {
-                                                                       SETPVSBIT(outsurfacepvs, surfaceindex);
-                                                                       outsurfacelist[outnumsurfaces++] = surfaceindex;
-                                                                       break;
+                                                                       v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
+                                                                       v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
+                                                                       v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
+                                                                       if (lightmaxs[0] > min(v[0][0], min(v[1][0], v[2][0])) && lightmins[0] < max(v[0][0], max(v[1][0], v[2][0])) && lightmaxs[1] > min(v[0][1], min(v[1][1], v[2][1])) && lightmins[1] < max(v[0][1], max(v[1][1], v[2][1])) && lightmaxs[2] > min(v[0][2], min(v[1][2], v[2][2])) && lightmins[2] < max(v[0][2], max(v[1][2], v[2][2])))
+                                                                       {
+                                                                               SETPVSBIT(outsurfacepvs, surfaceindex);
+                                                                               outsurfacelist[outnumsurfaces++] = surfaceindex;
+                                                                               break;
+                                                                       }
+                                                               }
+                                                       }
+                                                       else
+                                                       {
+                                                               for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->num_triangles;triangleindex++, t++, e += 3)
+                                                               {
+                                                                       v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
+                                                                       v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
+                                                                       v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
+                                                                       if (PointInfrontOfTriangle(relativelightorigin, v[0], v[1], v[2]) && lightmaxs[0] > min(v[0][0], min(v[1][0], v[2][0])) && lightmins[0] < max(v[0][0], max(v[1][0], v[2][0])) && lightmaxs[1] > min(v[0][1], min(v[1][1], v[2][1])) && lightmins[1] < max(v[0][1], max(v[1][1], v[2][1])) && lightmaxs[2] > min(v[0][2], min(v[1][2], v[2][2])) && lightmins[2] < max(v[0][2], max(v[1][2], v[2][2])))
+                                                                       {
+                                                                               SETPVSBIT(outsurfacepvs, surfaceindex);
+                                                                               outsurfacelist[outnumsurfaces++] = surfaceindex;
+                                                                               break;
+                                                                       }
                                                                }
                                                        }
                                                }
@@ -2475,14 +2545,14 @@ void R_Q3BSP_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin,
                {
                        surface = model->brushq3.data_faces + surfacelist[surfacelistindex];
                        // FIXME: check some manner of face->rendermode here?
-                       if (!(surface->texture->surfaceflags & Q3SURFACEFLAG_NODRAW) && !(surface->texture->surfaceparms & Q3SURFACEPARM_TRANS))
+                       if (!(surface->texture->surfaceflags & Q3SURFACEFLAG_NODRAW) && !(surface->texture->surfaceparms & (Q3SURFACEPARM_SKY | Q3SURFACEPARM_TRANS)) && !(surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED))
                                R_Shadow_MarkVolumeFromBox(surface->num_firstshadowmeshtriangle, surface->num_triangles, model->brush.shadowmesh->vertex3f, model->brush.shadowmesh->element3i, relativelightorigin, lightmins, lightmaxs, surface->mins, surface->maxs);
                }
                R_Shadow_VolumeFromList(model->brush.shadowmesh->numverts, model->brush.shadowmesh->numtriangles, model->brush.shadowmesh->vertex3f, model->brush.shadowmesh->element3i, model->brush.shadowmesh->neighbor3i, relativelightorigin, lightradius + model->radius + r_shadow_projectdistance.value, numshadowmark, shadowmarklist);
        }
 }
 
-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 numsurfaces, const int *surfacelist)
+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, vec_t ambientscale, vec_t diffusescale, vec_t specularscale, int numsurfaces, const int *surfacelist)
 {
        model_t *model = ent->model;
        vec3_t lightmins, lightmaxs, modelorg;
@@ -2507,7 +2577,13 @@ void R_Q3BSP_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t
                                Mod_ShadowMesh_AddMesh(r_shadow_mempool, r_shadow_compilingrtlight->static_meshchain_light, surface->texture->skin.base, surface->texture->skin.gloss, surface->texture->skin.nmap, surface->data_vertex3f, surface->data_svector3f, surface->data_tvector3f, surface->data_normal3f, surface->data_texcoordtexture2f, surface->num_triangles, surface->data_element3i);
                        }
                        else if (!(surface->texture->surfaceflags & Q3SURFACEFLAG_NODRAW) && surface->num_triangles)
-                               R_Shadow_RenderLighting(surface->num_vertices, surface->num_triangles, surface->data_element3i, surface->data_vertex3f, surface->data_svector3f, surface->data_tvector3f, surface->data_normal3f, surface->data_texcoordtexture2f, relativelightorigin, relativeeyeorigin, lightcolor, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, surface->texture->skin.base, surface->texture->skin.nmap, surface->texture->skin.gloss, lightcubemap, LIGHTING_DIFFUSE | LIGHTING_SPECULAR);
+                       {
+                               if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
+                                       qglDisable(GL_CULL_FACE);
+                               R_Shadow_RenderLighting(surface->num_vertices, surface->num_triangles, surface->data_element3i, surface->data_vertex3f, surface->data_svector3f, surface->data_tvector3f, surface->data_normal3f, surface->data_texcoordtexture2f, relativelightorigin, relativeeyeorigin, lightcolor, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, surface->texture->skin.base, surface->texture->skin.nmap, surface->texture->skin.gloss, lightcubemap, ambientscale, diffusescale, specularscale);
+                               if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
+                                       qglEnable(GL_CULL_FACE);
+                       }
                }
        }
 }