]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_backend.c
cleanup of float/byte colors, now colors are always floats until R_Mesh_Render conver...
[xonotic/darkplaces.git] / gl_backend.c
index e1bd699ff9c2e987b3fc53119dd3234f1b64b38d..1b75ccb089991fb86efced0268f06efc28ef7492 100644 (file)
@@ -1,20 +1,26 @@
 
 #include "quakedef.h"
 
+cvar_t         r_render = {0, "r_render", "1"};
+cvar_t         gl_dither = {CVAR_SAVE, "gl_dither", "1"}; // whether or not to use dithering
+
+int                    lightscalebit;
+float          lightscale;
+float          overbrightscale;
+
+void SCR_ScreenShot_f (void);
+static void R_Envmap_f (void);
+
 static int max_meshs;
 static int max_batch;
 static int max_verts; // always max_meshs * 3
 #define TRANSDEPTHRES 4096
 
-//#define FLOATCOLORS
-
 //static cvar_t gl_mesh_maxtriangles = {0, "gl_mesh_maxtriangles", "21760"};
 static cvar_t gl_mesh_maxtriangles = {0, "gl_mesh_maxtriangles", "8192"};
 static cvar_t gl_mesh_batchtriangles = {0, "gl_mesh_batchtriangles", "1024"};
 static cvar_t gl_mesh_merge = {0, "gl_mesh_merge", "1"};
-#if FLOATCOLORS
-static cvar_t gl_mesh_floatcolors = {0, "gl_mesh_floatcolors", "1"};
-#endif
+static cvar_t gl_mesh_floatcolors = {0, "gl_mesh_floatcolors", "0"};
 static cvar_t gl_mesh_dupetransverts = {0, "gl_mesh_dupetransverts", "0"};
 static cvar_t gl_mesh_sorttransbymesh = {0, "gl_mesh_sorttransbymesh", "1"};
 
@@ -57,13 +63,11 @@ typedef struct
 }
 buf_vertex_t;
 
-#if FLOATCOLORS
 typedef struct
 {
        float c[4];
 }
 buf_fcolor_t;
-#endif
 
 typedef struct
 {
@@ -79,15 +83,10 @@ buf_texcoord_t;
 
 static float meshfarclip;
 static int currentmesh, currenttriangle, currentvertex, backendunits, backendactive, meshmerge, transranout;
-#if FLOATCOLORS
-static int floatcolors;
-#endif
 static buf_mesh_t *buf_mesh;
 static buf_tri_t *buf_tri;
 static buf_vertex_t *buf_vertex;
-#if FLOATCOLORS
 static buf_fcolor_t *buf_fcolor;
-#endif
 static buf_bcolor_t *buf_bcolor;
 static buf_texcoord_t *buf_texcoord[MAX_TEXTUREUNITS];
 
@@ -96,10 +95,7 @@ static buf_mesh_t *buf_transmesh;
 static buf_transtri_t *buf_transtri;
 static buf_transtri_t **buf_transtri_list;
 static buf_vertex_t *buf_transvertex;
-#if FLOATCOLORS
 static buf_fcolor_t *buf_transfcolor;
-#endif
-static buf_bcolor_t *buf_transbcolor;
 static buf_texcoord_t *buf_transtexcoord[MAX_TEXTUREUNITS];
 
 static mempool_t *gl_backend_mempool;
@@ -125,19 +121,14 @@ static void gl_backend_start(void)
        BACKENDALLOC(buf_mesh, max_meshs, buf_mesh_t)
        BACKENDALLOC(buf_tri, max_meshs, buf_tri_t)
        BACKENDALLOC(buf_vertex, max_verts, buf_vertex_t)
-#if FLOATCOLORS
        BACKENDALLOC(buf_fcolor, max_verts, buf_fcolor_t)
-#endif
        BACKENDALLOC(buf_bcolor, max_verts, buf_bcolor_t)
 
        BACKENDALLOC(buf_transmesh, max_meshs, buf_mesh_t)
        BACKENDALLOC(buf_transtri, max_meshs, buf_transtri_t)
        BACKENDALLOC(buf_transtri_list, TRANSDEPTHRES, buf_transtri_t *)
        BACKENDALLOC(buf_transvertex, max_verts, buf_vertex_t)
-#if FLOATCOLORS
        BACKENDALLOC(buf_transfcolor, max_verts, buf_fcolor_t)
-#endif
-       BACKENDALLOC(buf_transbcolor, max_verts, buf_bcolor_t)
 
        for (i = 0;i < MAX_TEXTUREUNITS;i++)
        {
@@ -174,19 +165,14 @@ static void gl_backend_shutdown(void)
        BACKENDFREE(buf_mesh)
        BACKENDFREE(buf_tri)
        BACKENDFREE(buf_vertex)
-#if FLOATCOLORS
        BACKENDFREE(buf_fcolor)
-#endif
        BACKENDFREE(buf_bcolor)
 
        BACKENDFREE(buf_transmesh)
        BACKENDFREE(buf_transtri)
        BACKENDFREE(buf_transtri_list)
        BACKENDFREE(buf_transvertex)
-#if FLOATCOLORS
        BACKENDFREE(buf_transfcolor)
-#endif
-       BACKENDFREE(buf_transbcolor)
 
        for (i = 0;i < MAX_TEXTUREUNITS;i++)
        {
@@ -245,12 +231,20 @@ int polyindexarray[768];
 void gl_backend_init(void)
 {
        int i;
+
+       Cvar_RegisterVariable (&r_render);
+       Cvar_RegisterVariable (&gl_dither);
+#ifdef NORENDER
+       Cvar_SetValue("r_render", 0);
+#endif
+
+       Cmd_AddCommand ("screenshot",SCR_ScreenShot_f);
+       Cmd_AddCommand ("envmap", R_Envmap_f);
+
        Cvar_RegisterVariable(&gl_mesh_maxtriangles);
        Cvar_RegisterVariable(&gl_mesh_batchtriangles);
        Cvar_RegisterVariable(&gl_mesh_merge);
-#if FLOATCOLORS
        Cvar_RegisterVariable(&gl_mesh_floatcolors);
-#endif
        Cvar_RegisterVariable(&gl_mesh_dupetransverts);
        Cvar_RegisterVariable(&gl_mesh_sorttransbymesh);
        R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap);
@@ -263,6 +257,73 @@ void gl_backend_init(void)
        }
 }
 
+static void MYgluPerspective(GLdouble fovx, GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar )
+{
+       GLdouble xmax, ymax;
+
+       xmax = zNear * tan( fovx * M_PI / 360.0 ) * aspect;
+       ymax = zNear * tan( fovy * M_PI / 360.0 );
+
+       glFrustum(-xmax, xmax, -ymax, ymax, zNear, zFar );
+}
+
+
+/*
+=============
+GL_SetupFrame
+=============
+*/
+static void GL_SetupFrame (void)
+{
+       if (!r_render.integer)
+               return;
+
+//     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // LordHavoc: moved to SCR_UpdateScreen
+       gldepthmin = 0;
+       gldepthmax = 1;
+       glDepthFunc (GL_LEQUAL);
+
+       glDepthRange (gldepthmin, gldepthmax);
+
+       // update farclip based on previous frame
+       r_farclip = r_newfarclip;
+
+       // set up viewpoint
+       glMatrixMode(GL_PROJECTION);
+       glLoadIdentity ();
+
+       // y is weird beause OpenGL is bottom to top, we use top to bottom
+       glViewport(r_refdef.x, vid.realheight - (r_refdef.y + r_refdef.height), r_refdef.width, r_refdef.height);
+//     yfov = 2*atan((float)r_refdef.height/r_refdef.width)*180/M_PI;
+       MYgluPerspective (r_refdef.fov_x, r_refdef.fov_y, r_refdef.width/r_refdef.height, 4, r_farclip);
+
+       glCullFace(GL_FRONT);
+
+       glMatrixMode(GL_MODELVIEW);
+       glLoadIdentity ();
+
+       glRotatef (-90,  1, 0, 0);          // put Z going up
+       glRotatef (90,  0, 0, 1);           // put Z going up
+       glRotatef (-r_refdef.viewangles[2],  1, 0, 0);
+       glRotatef (-r_refdef.viewangles[0],  0, 1, 0);
+       glRotatef (-r_refdef.viewangles[1],  0, 0, 1);
+       glTranslatef (-r_refdef.vieworg[0],  -r_refdef.vieworg[1],  -r_refdef.vieworg[2]);
+
+//     glGetFloatv (GL_MODELVIEW_MATRIX, r_world_matrix);
+
+       //
+       // set drawing parms
+       //
+//     if (gl_cull.integer)
+               glEnable(GL_CULL_FACE);
+//     else
+//             glDisable(GL_CULL_FACE);
+
+       glEnable(GL_BLEND); // was Disable
+       glEnable(GL_DEPTH_TEST);
+       glDepthMask(1);
+}
+
 static float viewdist;
 
 int c_meshs, c_meshtris, c_transmeshs, c_transtris;
@@ -283,9 +344,6 @@ void R_Mesh_Clear(void)
        currenttransvertex = 0;
        meshfarclip = 0;
        meshmerge = gl_mesh_merge.integer;
-#if FLOATCOLORS
-       floatcolors = gl_mesh_floatcolors.integer;
-#endif
        transranout = false;
        viewdist = DotProduct(r_origin, vpn);
 
@@ -293,6 +351,8 @@ void R_Mesh_Clear(void)
        c_meshtris = 0;
        c_transmeshs = 0;
        c_transtris = 0;
+
+       GL_SetupFrame();
 }
 
 #ifdef DEBUGGL
@@ -338,6 +398,10 @@ void R_Mesh_Render(void)
        int i, k, blendfunc1, blendfunc2, blend, depthmask, depthtest, unit = 0, clientunit = 0, firsttriangle, triangles, firstvert, lastvert, texture[MAX_TEXTUREUNITS];
        float farclip, texturergbscale[MAX_TEXTUREUNITS];
        buf_mesh_t *mesh;
+       // float to byte color conversion
+       int *icolor;
+       float *fcolor;
+       byte *bcolor;
        if (!backendactive)
                Sys_Error("R_Mesh_Render: called when backend is not active\n");
        if (!currentmesh)
@@ -384,20 +448,32 @@ CHECKGLERROR
 CHECKGLERROR
        glEnableClientState(GL_VERTEX_ARRAY);
 CHECKGLERROR
-#if FLOATCOLORS
-       if (floatcolors)
+       if (gl_mesh_floatcolors.integer)
        {
                glColorPointer(4, GL_FLOAT, sizeof(buf_fcolor_t), buf_fcolor);
 CHECKGLERROR
        }
        else
        {
-#endif
+               // shift float to have 8bit fraction at base of number
+               for (i = 0, fcolor = &buf_fcolor->c[0];i < currentvertex;i++)
+               {
+                       *fcolor++ += 32768.0f;
+                       *fcolor++ += 32768.0f;
+                       *fcolor++ += 32768.0f;
+                       *fcolor++ += 32768.0f;
+               }
+               // then read as integer and kill float bits...
+               for (i = 0, icolor = (int *)&buf_fcolor->c[0], bcolor = &buf_bcolor->c[0];i < currentvertex;i++)
+               {
+                       k = (*icolor++) & 0x7FFFFF;*bcolor++ = k > 255 ? 255 : k;
+                       k = (*icolor++) & 0x7FFFFF;*bcolor++ = k > 255 ? 255 : k;
+                       k = (*icolor++) & 0x7FFFFF;*bcolor++ = k > 255 ? 255 : k;
+                       k = (*icolor++) & 0x7FFFFF;*bcolor++ = k > 255 ? 255 : k;
+               }
                glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(buf_bcolor_t), buf_bcolor);
 CHECKGLERROR
-#if FLOATCOLORS
        }
-#endif
        glEnableClientState(GL_COLOR_ARRAY);
 CHECKGLERROR
 
@@ -789,12 +865,7 @@ void R_Mesh_AddTransparent(void)
                // note: can't batch these because they can be rendered in any order
                // there can never be more transparent triangles than fit in main buffers
                memcpy(&buf_vertex[currentvertex], &buf_transvertex[0], currenttransvertex * sizeof(buf_vertex_t));
-#if FLOATCOLORS
-               if (floatcolors)
-                       memcpy(&buf_fcolor[currentvertex], &buf_transfcolor[0], currenttransvertex * sizeof(buf_fcolor_t));
-               else
-#endif
-                       memcpy(&buf_bcolor[currentvertex], &buf_transbcolor[0], currenttransvertex * sizeof(buf_bcolor_t));
+               memcpy(&buf_fcolor[currentvertex], &buf_transfcolor[0], currenttransvertex * sizeof(buf_fcolor_t));
                for (i = 0;i < backendunits;i++)
                        memcpy(&buf_texcoord[i][currentvertex], &buf_transtexcoord[i][0], currenttransvertex * sizeof(buf_texcoord_t));
                #endif
@@ -838,12 +909,7 @@ void R_Mesh_AddTransparent(void)
                                R_Mesh_Render();
 
                        memcpy(&buf_vertex[currentvertex], &buf_transvertex[transmesh->firstvert], numverts * sizeof(buf_vertex_t));
-#if FLOATCOLORS
-                       if (floatcolors)
-                               memcpy(&buf_fcolor[currentvertex], &buf_transfcolor[transmesh->firstvert], numverts * sizeof(buf_fcolor_t));
-                       else
-#endif
-                               memcpy(&buf_bcolor[currentvertex], &buf_transbcolor[transmesh->firstvert], numverts * sizeof(buf_bcolor_t));
+                       memcpy(&buf_fcolor[currentvertex], &buf_transfcolor[transmesh->firstvert], numverts * sizeof(buf_fcolor_t));
                        for (i = 0;i < backendunits && transmesh->textures[i];i++)
                                memcpy(&buf_texcoord[i][currentvertex], &buf_transtexcoord[i][transmesh->firstvert], numverts * sizeof(buf_texcoord_t));
 
@@ -993,12 +1059,7 @@ void R_Mesh_AddTransparent(void)
                                                index = tri->index[k];
                                                buf_tri[currenttriangle].index[k] = currentvertex;
                                                memcpy(buf_vertex[currentvertex].v, buf_transvertex[index].v, sizeof(buf_vertex_t));
-#if FLOATCOLORS
-                                               if (floatcolors)
-                                                       memcpy(buf_fcolor[currentvertex].c, buf_transfcolor[index].c, sizeof(buf_fcolor_t));
-                                               else
-#endif
-                                                       memcpy(buf_bcolor[currentvertex].c, buf_transbcolor[index].c, sizeof(buf_bcolor_t));
+                                               memcpy(buf_fcolor[currentvertex].c, buf_transfcolor[index].c, sizeof(buf_fcolor_t));
                                                for (i = 0;i < backendunits && tri->mesh->textures[i];i++)
                                                        memcpy(buf_texcoord[i][currentvertex].t, buf_transtexcoord[i][index].t, sizeof(buf_texcoord_t));
                                                currentvertex++;
@@ -1067,12 +1128,7 @@ void R_Mesh_AddTransparent(void)
                // note: can't batch these because they can be rendered in any order
                // there can never be more transparent triangles than fit in main buffers
                memcpy(&buf_vertex[currentvertex], &buf_transvertex[0], currenttransvertex * sizeof(buf_vertex_t));
-#if FLOATCOLORS
-               if (floatcolors)
-                       memcpy(&buf_fcolor[currentvertex], &buf_transfcolor[0], currenttransvertex * sizeof(buf_fcolor_t));
-               else
-#endif
-                       memcpy(&buf_bcolor[currentvertex], &buf_transbcolor[0], currenttransvertex * sizeof(buf_bcolor_t));
+               memcpy(&buf_fcolor[currentvertex], &buf_transfcolor[0], currenttransvertex * sizeof(buf_fcolor_t));
                for (i = 0;i < backendunits;i++)
                        memcpy(&buf_texcoord[i][currentvertex], &buf_transtexcoord[i][0], currenttransvertex * sizeof(buf_texcoord_t));
 
@@ -1106,19 +1162,13 @@ void R_Mesh_Draw(const rmeshinfo_t *m)
 {
        // these are static because gcc runs out of virtual registers otherwise
        static int i, j, *index, overbright;
-       static float c, *in, scaler;
-#if FLOATCOLORS
+       static float *in, scaler;
        static float cr, cg, cb, ca;
-#endif
        static buf_mesh_t *mesh;
        static buf_vertex_t *vert;
-#if FLOATCOLORS
        static buf_fcolor_t *fcolor;
-#endif
-       static buf_bcolor_t *bcolor;
        static buf_texcoord_t *texcoord[MAX_TEXTUREUNITS];
        static buf_transtri_t *tri;
-       static buf_bcolor_t flatbcolor;
 
        if (m->index == NULL
         || !m->numtriangles
@@ -1157,8 +1207,7 @@ void R_Mesh_Draw(const rmeshinfo_t *m)
                        if (overbright)
                                scaler *= 0.25f;
                }
-               if (lighthalf)
-                       scaler *= 0.5f;
+               scaler *= overbrightscale;
        }
 
        if (m->transparent)
@@ -1176,10 +1225,7 @@ void R_Mesh_Draw(const rmeshinfo_t *m)
                c_transmeshs++;
                c_transtris += m->numtriangles;
                vert = &buf_transvertex[currenttransvertex];
-#if FLOATCOLORS
                fcolor = &buf_transfcolor[currenttransvertex];
-#endif
-               bcolor = &buf_transbcolor[currenttransvertex];
                for (i = 0;i < backendunits;i++)
                        texcoord[i] = &buf_transtexcoord[i][currenttransvertex];
 
@@ -1217,10 +1263,7 @@ void R_Mesh_Draw(const rmeshinfo_t *m)
                c_meshs++;
                c_meshtris += m->numtriangles;
                vert = &buf_vertex[currentvertex];
-#if FLOATCOLORS
                fcolor = &buf_fcolor[currentvertex];
-#endif
-               bcolor = &buf_bcolor[currentvertex];
                for (i = 0;i < backendunits;i++)
                        texcoord[i] = &buf_texcoord[i][currentvertex];
 
@@ -1267,61 +1310,30 @@ void R_Mesh_Draw(const rmeshinfo_t *m)
        else
                memcpy(vert, m->vertex, m->numverts * sizeof(buf_vertex_t));
 
-#if FLOATCOLORS
-       if (floatcolors)
+       if (m->color)
        {
-               if (m->color)
+               for (i = 0, in = m->color;i < m->numverts;i++, (int)in += m->colorstep)
                {
-                       for (i = 0, in = m->color;i < m->numverts;i++, (int)in += m->colorstep)
-                       {
-                               fcolor[i].c[0] = in[0] * scaler;
-                               fcolor[i].c[1] = in[1] * scaler;
-                               fcolor[i].c[2] = in[2] * scaler;
-                               fcolor[i].c[3] = in[3];
-                       }
-               }
-               else
-               {
-                       cr = m->cr * scaler;
-                       cg = m->cg * scaler;
-                       cb = m->cb * scaler;
-                       ca = m->ca;
-                       for (i = 0;i < m->numverts;i++)
-                       {
-                               fcolor[i].c[0] = cr;
-                               fcolor[i].c[1] = cg;
-                               fcolor[i].c[2] = cb;
-                               fcolor[i].c[3] = ca;
-                       }
+                       fcolor[i].c[0] = in[0] * scaler;
+                       fcolor[i].c[1] = in[1] * scaler;
+                       fcolor[i].c[2] = in[2] * scaler;
+                       fcolor[i].c[3] = in[3];
                }
        }
        else
        {
-#endif
-               if (m->color)
-               {
-                       for (i = 0, in = m->color;i < m->numverts;i++, (int)in += m->colorstep)
-                       {
-                               // shift float to have 8bit fraction at base of number,
-                               // then read as integer and kill float bits...
-                               c = in[0] * scaler + 32768.0f;j = (*((long *)&c) & 0x7FFFFF);if (j > 255) j = 255;bcolor[i].c[0] = (byte) j;
-                               c = in[1] * scaler + 32768.0f;j = (*((long *)&c) & 0x7FFFFF);if (j > 255) j = 255;bcolor[i].c[1] = (byte) j;
-                               c = in[2] * scaler + 32768.0f;j = (*((long *)&c) & 0x7FFFFF);if (j > 255) j = 255;bcolor[i].c[2] = (byte) j;
-                               c = in[3]          + 32768.0f;j = (*((long *)&c) & 0x7FFFFF);if (j > 255) j = 255;bcolor[i].c[3] = (byte) j;
-                       }
-               }
-               else
+               cr = m->cr * scaler;
+               cg = m->cg * scaler;
+               cb = m->cb * scaler;
+               ca = m->ca;
+               for (i = 0;i < m->numverts;i++)
                {
-                       c = m->cr * scaler + 32768.0f;j = (*((long *)&c) & 0x7FFFFF);if (j > 255) j = 255;flatbcolor.c[0] = (byte) j;
-                       c = m->cg * scaler + 32768.0f;j = (*((long *)&c) & 0x7FFFFF);if (j > 255) j = 255;flatbcolor.c[1] = (byte) j;
-                       c = m->cb * scaler + 32768.0f;j = (*((long *)&c) & 0x7FFFFF);if (j > 255) j = 255;flatbcolor.c[2] = (byte) j;
-                       c = m->ca          + 32768.0f;j = (*((long *)&c) & 0x7FFFFF);if (j > 255) j = 255;flatbcolor.c[3] = (byte) j;
-                       for (i = 0;i < m->numverts;i++)
-                               bcolor[i] = flatbcolor;
+                       fcolor[i].c[0] = cr;
+                       fcolor[i].c[1] = cg;
+                       fcolor[i].c[2] = cb;
+                       fcolor[i].c[3] = ca;
                }
-#if FLOATCOLORS
        }
-#endif
 
        for (j = 0;j < MAX_TEXTUREUNITS && m->tex[j];j++)
        {
@@ -1366,20 +1378,14 @@ void R_Mesh_DrawPolygon(rmeshinfo_t *m, int numverts)
 void R_Mesh_DrawDecal(const rmeshinfo_t *m)
 {
        // these are static because gcc runs out of virtual registers otherwise
-       static int i, j, *index, overbright;
-       static float c, scaler;
-#if FLOATCOLORS
+       static int i, *index, overbright;
+       static float scaler;
        static float cr, cg, cb, ca;
-#endif
        static buf_mesh_t *mesh;
        static buf_vertex_t *vert;
-#if FLOATCOLORS
        static buf_fcolor_t *fcolor;
-#endif
-       static buf_bcolor_t *bcolor;
        static buf_texcoord_t *texcoord;
        static buf_transtri_t *tri;
-       static buf_bcolor_t flatbcolor;
 
        if (!backendactive)
                Sys_Error("R_Mesh_Draw: called when backend is not active\n");
@@ -1391,8 +1397,7 @@ void R_Mesh_DrawDecal(const rmeshinfo_t *m)
                if (overbright)
                        scaler *= 0.25f;
        }
-       if (lighthalf)
-               scaler *= 0.5f;
+       scaler *= overbrightscale;
 
        if (m->transparent)
        {
@@ -1409,10 +1414,7 @@ void R_Mesh_DrawDecal(const rmeshinfo_t *m)
                c_transmeshs++;
                c_transtris += 2;
                vert = &buf_transvertex[currenttransvertex];
-#if FLOATCOLORS
                fcolor = &buf_transfcolor[currenttransvertex];
-#endif
-               bcolor = &buf_transbcolor[currenttransvertex];
                texcoord = &buf_transtexcoord[0][currenttransvertex];
 
                // transmesh is only for storage of transparent meshs until they
@@ -1464,10 +1466,7 @@ void R_Mesh_DrawDecal(const rmeshinfo_t *m)
                c_meshs++;
                c_meshtris += 2;
                vert = &buf_vertex[currentvertex];
-#if FLOATCOLORS
                fcolor = &buf_fcolor[currentvertex];
-#endif
-               bcolor = &buf_bcolor[currentvertex];
                texcoord = &buf_texcoord[0][currentvertex];
 
                mesh = &buf_mesh[currentmesh++];
@@ -1502,45 +1501,223 @@ void R_Mesh_DrawDecal(const rmeshinfo_t *m)
        // buf_vertex_t must match the size of the decal vertex array (or vice versa)
        memcpy(vert, m->vertex, 4 * sizeof(buf_vertex_t));
 
-#if FLOATCOLORS
-       if (floatcolors)
+       cr = m->cr * scaler;
+       cg = m->cg * scaler;
+       cb = m->cb * scaler;
+       ca = m->ca;
+       fcolor[0].c[0] = cr;
+       fcolor[0].c[1] = cg;
+       fcolor[0].c[2] = cb;
+       fcolor[0].c[3] = ca;
+       fcolor[1].c[0] = cr;
+       fcolor[1].c[1] = cg;
+       fcolor[1].c[2] = cb;
+       fcolor[1].c[3] = ca;
+       fcolor[2].c[0] = cr;
+       fcolor[2].c[1] = cg;
+       fcolor[2].c[2] = cb;
+       fcolor[2].c[3] = ca;
+       fcolor[3].c[0] = cr;
+       fcolor[3].c[1] = cg;
+       fcolor[3].c[2] = cb;
+       fcolor[3].c[3] = ca;
+
+       // buf_texcoord_t must be the same size as the decal texcoord array (or vice versa)
+       memcpy(&texcoord[0].t[0], m->texcoords[0], 4 * sizeof(buf_texcoord_t));
+}
+
+/*
+==============================================================================
+
+                                               SCREEN SHOTS
+
+==============================================================================
+*/
+
+float CalcFov (float fov_x, float width, float height);
+void R_ClearScreen(void);
+
+void SCR_ScreenShot(char *filename, int x, int y, int width, int height)
+{
+       int i;
+       byte *buffer;
+
+       buffer = Mem_Alloc(tempmempool, width*height*3);
+       glReadPixels (x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer);
+       CHECKGLERROR
+
+       // LordHavoc: compensate for v_overbrightbits when using hardware gamma
+       if (v_hwgamma.integer)
+               for (i = 0;i < width * height * 3;i++)
+                       buffer[i] <<= v_overbrightbits.integer;
+
+       Image_WriteTGARGB_preflipped(filename, width, height, buffer);
+
+       Mem_Free(buffer);
+}
+
+/*
+==================
+SCR_ScreenShot_f
+==================
+*/
+void SCR_ScreenShot_f (void)
+{
+       int i;
+       char filename[16];
+       char checkname[MAX_OSPATH];
+//
+// find a file name to save it to
+//
+       strcpy(filename, "dp0000.tga");
+
+       for (i=0 ; i<=9999 ; i++)
        {
-               cr = m->cr * scaler;
-               cg = m->cg * scaler;
-               cb = m->cb * scaler;
-               ca = m->ca;
-               fcolor[0].c[0] = cr;
-               fcolor[0].c[1] = cg;
-               fcolor[0].c[2] = cb;
-               fcolor[0].c[3] = ca;
-               fcolor[1].c[0] = cr;
-               fcolor[1].c[1] = cg;
-               fcolor[1].c[2] = cb;
-               fcolor[1].c[3] = ca;
-               fcolor[2].c[0] = cr;
-               fcolor[2].c[1] = cg;
-               fcolor[2].c[2] = cb;
-               fcolor[2].c[3] = ca;
-               fcolor[3].c[0] = cr;
-               fcolor[3].c[1] = cg;
-               fcolor[3].c[2] = cb;
-               fcolor[3].c[3] = ca;
+               filename[2] = (i/1000)%10 + '0';
+               filename[3] = (i/ 100)%10 + '0';
+               filename[4] = (i/  10)%10 + '0';
+               filename[5] = (i/   1)%10 + '0';
+               sprintf (checkname, "%s/%s", com_gamedir, filename);
+               if (Sys_FileTime(checkname) == -1)
+                       break;  // file doesn't exist
        }
-       else
+       if (i==10000)
        {
-#endif
-               c = m->cr * scaler + 32768.0f;j = (*((long *)&c) & 0x7FFFFF);if (j > 255) j = 255;flatbcolor.c[0] = (byte) j;
-               c = m->cg * scaler + 32768.0f;j = (*((long *)&c) & 0x7FFFFF);if (j > 255) j = 255;flatbcolor.c[1] = (byte) j;
-               c = m->cb * scaler + 32768.0f;j = (*((long *)&c) & 0x7FFFFF);if (j > 255) j = 255;flatbcolor.c[2] = (byte) j;
-               c = m->ca          + 32768.0f;j = (*((long *)&c) & 0x7FFFFF);if (j > 255) j = 255;flatbcolor.c[3] = (byte) j;
-               bcolor[0] = flatbcolor;
-               bcolor[1] = flatbcolor;
-               bcolor[2] = flatbcolor;
-               bcolor[3] = flatbcolor;
-#if FLOATCOLORS
+               Con_Printf ("SCR_ScreenShot_f: Couldn't create a TGA file\n");
+               return;
+       }
+
+       SCR_ScreenShot(filename, vid.realx, vid.realy, vid.realwidth, vid.realheight);
+       Con_Printf ("Wrote %s\n", filename);
+}
+
+/*
+===============
+R_Envmap_f
+
+Grab six views for environment mapping tests
+===============
+*/
+struct
+{
+       float angles[3];
+       char *name;
+}
+envmapinfo[6] =
+{
+       {{  0,   0, 0}, "ft"},
+       {{  0,  90, 0}, "rt"},
+       {{  0, 180, 0}, "bk"},
+       {{  0, 270, 0}, "lf"},
+       {{-90,  90, 0}, "up"},
+       {{ 90,  90, 0}, "dn"}
+};
+static void R_Envmap_f (void)
+{
+       int j, size;
+       char filename[256], basename[256];
+
+       if (Cmd_Argc() != 3)
+       {
+               Con_Printf ("envmap <basename> <size>: save out 6 cubic environment map images, usable with loadsky, note that size must one of 128, 256, 512, or 1024 and can't be bigger than your current resolution\n");
+               return;
        }
-#endif
 
-       // buf_texcoord_t must be the same size as the decal texcoord array (or vice versa)
-       memcpy(&texcoord[0].t[0], m->texcoords[0], 4 * sizeof(buf_texcoord_t));
+       if (!r_render.integer)
+               return;
+
+       strcpy(basename, Cmd_Argv(1));
+       size = atoi(Cmd_Argv(2));
+       if (size != 128 && size != 256 && size != 512 && size != 1024)
+       {
+               Con_Printf("envmap: size must be one of 128, 256, 512, or 1024\n");
+               return;
+       }
+       if (size > vid.realwidth || size > vid.realheight)
+       {
+               Con_Printf("envmap: your resolution is not big enough to render that size\n");
+               return;
+       }
+
+       envmap = true;
+
+       r_refdef.x = 0;
+       r_refdef.y = 0;
+       r_refdef.width = size;
+       r_refdef.height = size;
+
+       r_refdef.fov_x = 90;
+       r_refdef.fov_y = 90;
+
+       for (j = 0;j < 6;j++)
+       {
+               sprintf(filename, "env/%s%s.tga", basename, envmapinfo[j].name);
+               VectorCopy(envmapinfo[j].angles, r_refdef.viewangles);
+               R_ClearScreen();
+               R_RenderView ();
+               SCR_ScreenShot(filename, vid.realx, vid.realy, size, size);
+       }
+
+       envmap = false;
+}
+
+//=============================================================================
+
+void R_ClearScreen(void)
+{
+       if (r_render.integer)
+       {
+               glClearColor(0,0,0,0);
+               CHECKGLERROR
+               glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // LordHavoc: clear the screen (around the view as well)
+               CHECKGLERROR
+               if (gl_dither.integer)
+                       glEnable(GL_DITHER);
+               else
+                       glDisable(GL_DITHER);
+               CHECKGLERROR
+       }
+}
+
+/*
+==================
+SCR_UpdateScreen
+
+This is called every frame, and can also be called explicitly to flush
+text to the screen.
+==================
+*/
+void SCR_UpdateScreen (void)
+{
+       //Mem_CheckSentinelsGlobal();
+       //R_TimeReport("memtest");
+
+       glFinish ();
+       CHECKGLERROR
+
+       VID_Finish ();
+
+       R_TimeReport("finish");
+
+       if (gl_combine.integer && !gl_combine_extension)
+               Cvar_SetValue("gl_combine", 0);
+
+       lightscalebit = v_overbrightbits.integer;
+       if (gl_combine.integer && r_multitexture.integer)
+               lightscalebit += 2;
+
+       lightscale = 1.0f / (float) (1 << lightscalebit);
+       overbrightscale = 1.0f / (float) (1 << v_overbrightbits.integer);
+
+       R_TimeReport("setup");
+
+       R_ClearScreen();
+
+       R_TimeReport("clear");
+
+       if (scr_conlines < vid.conheight)
+               R_RenderView();
+
+       // draw 2D stuff
+       R_DrawQueue();
 }