]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_backend.c
added cl_avidemo cvar (saves a series of tga images named gamedir/dpavi000000.tga...
[xonotic/darkplaces.git] / gl_backend.c
index 0c43daf19b87767cf27c3e0ade87c51fa8aa5202..0859df0b7eeac4c8b42098217dd16cd00a48513c 100644 (file)
@@ -1,36 +1,86 @@
 
 #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
+//cvar_t gl_mesh_maxtriangles = {0, "gl_mesh_maxtriangles", "21760"};
+cvar_t gl_mesh_maxtriangles = {0, "gl_mesh_maxtriangles", "8192"};
+//cvar_t gl_mesh_batchtriangles = {0, "gl_mesh_batchtriangles", "1024"};
+cvar_t gl_mesh_batchtriangles = {0, "gl_mesh_batchtriangles", "0"};
+cvar_t gl_mesh_floatcolors = {0, "gl_mesh_floatcolors", "0"};
+
+cvar_t r_render = {0, "r_render", "1"};
+cvar_t gl_dither = {CVAR_SAVE, "gl_dither", "1"}; // whether or not to use dithering
+cvar_t gl_lockarrays = {0, "gl_lockarrays", "1"};
+
+#ifdef DEBUGGL
+int errornumber = 0;
+
+void GL_PrintError(int errornumber, char *filename, int linenumber)
+{
+       switch(errornumber)
+       {
+#ifdef GL_INVALID_ENUM
+       case GL_INVALID_ENUM:
+               Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
+               break;
+#endif
+#ifdef GL_INVALID_VALUE
+       case GL_INVALID_VALUE:
+               Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
+               break;
+#endif
+#ifdef GL_INVALID_OPERATION
+       case GL_INVALID_OPERATION:
+               Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
+               break;
+#endif
+#ifdef GL_STACK_OVERFLOW
+       case GL_STACK_OVERFLOW:
+               Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
+               break;
+#endif
+#ifdef GL_STACK_UNDERFLOW
+       case GL_STACK_UNDERFLOW:
+               Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
+               break;
+#endif
+#ifdef GL_OUT_OF_MEMORY
+       case GL_OUT_OF_MEMORY:
+               Con_Printf("GL_OUT_OF_MEMORY at %s:%i\n", filename, linenumber);
+               break;
+#endif
+#ifdef GL_TABLE_TOO_LARGE
+    case GL_TABLE_TOO_LARGE:
+               Con_Printf("GL_TABLE_TOO_LARGE at %s:%i\n", filename, linenumber);
+               break;
+#endif
+       default:
+               Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
+               break;
+       }
+}
+#endif
+
+float r_farclip, r_newfarclip;
+
+int polyindexarray[768];
+
+static float viewdist;
+
+int c_meshs, c_meshtris, c_transmeshs, c_transtris;
 
 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_dupetransverts = {0, "gl_mesh_dupetransverts", "0"};
-static cvar_t gl_mesh_sorttransbymesh = {0, "gl_mesh_sorttransbymesh", "1"};
-
 typedef struct buf_mesh_s
 {
-       //struct buf_mesh_s *next;
        int depthmask;
        int depthtest;
        int blendfunc1, blendfunc2;
@@ -39,10 +89,9 @@ typedef struct buf_mesh_s
        int firsttriangle;
        int triangles;
        int firstvert;
-       int lastvert;
+       int verts;
        struct buf_mesh_s *chain;
        struct buf_transtri_s *transchain;
-       //struct buf_transtri_s **transchainpointer;
 }
 buf_mesh_t;
 
@@ -67,17 +116,15 @@ typedef struct
 }
 buf_vertex_t;
 
-#if FLOATCOLORS
 typedef struct
 {
        float c[4];
 }
 buf_fcolor_t;
-#endif
 
 typedef struct
 {
-       byte c[4];
+       qbyte c[4];
 }
 buf_bcolor_t;
 
@@ -88,28 +135,21 @@ typedef struct
 buf_texcoord_t;
 
 static float meshfarclip;
-static int currentmesh, currenttriangle, currentvertex, backendunits, backendactive, meshmerge, transranout;
-#if FLOATCOLORS
-static int floatcolors;
-#endif
+static int currentmesh, currenttriangle, currentvertex, backendunits, backendactive, transranout;
 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];
 
 static int currenttransmesh, currenttransvertex, currenttranstriangle;
 static buf_mesh_t *buf_transmesh;
-static buf_transtri_t *buf_transtri;
-static buf_transtri_t **buf_transtri_list;
+static buf_transtri_t *buf_sorttranstri;
+static buf_transtri_t **buf_sorttranstri_list;
+static buf_tri_t *buf_transtri;
 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;
@@ -135,19 +175,15 @@ 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_sorttranstri, max_meshs, buf_transtri_t)
+       BACKENDALLOC(buf_sorttranstri_list, TRANSDEPTHRES, buf_transtri_t *)
+       BACKENDALLOC(buf_transtri, max_meshs, buf_tri_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++)
        {
@@ -169,42 +205,6 @@ static void gl_backend_start(void)
 
 static void gl_backend_shutdown(void)
 {
-       //int i;
-       /*
-#define BACKENDFREE(var)\
-       if (var)\
-       {\
-               Mem_Free(var);\
-               var = NULL;\
-       }
-       */
-       /*
-#define BACKENDFREE(var) var = NULL;
-
-       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++)
-       {
-               BACKENDFREE(buf_texcoord[i])
-               BACKENDFREE(buf_transtexcoord[i])
-       }
-       */
-
        if (resizingbuffers)
                Mem_EmptyPool(gl_backend_mempool);
        else
@@ -243,36 +243,25 @@ static void gl_backend_bufferchanges(int init)
        }
 }
 
-float r_farclip, r_newfarclip;
-
 static void gl_backend_newmap(void)
 {
        r_farclip = r_newfarclip = 2048.0f;
 }
 
-int polyindexarray[768];
-
 void gl_backend_init(void)
 {
        int i;
 
-       Cvar_RegisterVariable (&r_render);
-       Cvar_RegisterVariable (&gl_dither);
+       Cvar_RegisterVariable(&r_render);
+       Cvar_RegisterVariable(&gl_dither);
+       Cvar_RegisterVariable(&gl_lockarrays);
 #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);
        gl_backend_bufferchanges(true);
        for (i = 0;i < 256;i++)
@@ -283,16 +272,29 @@ void gl_backend_init(void)
        }
 }
 
-static void MYgluPerspective(GLdouble fovx, GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar )
-{
-       GLdouble xmax, ymax;
+int arraylocked = false;
 
-       xmax = zNear * tan( fovx * M_PI / 360.0 ) * aspect;
-       ymax = zNear * tan( fovy * M_PI / 360.0 );
+void GL_LockArray(int first, int count)
+{
+       if (!arraylocked && gl_supportslockarrays && gl_lockarrays.integer)
+       {
+               qglLockArraysEXT(first, count);
+               CHECKGLERROR
+               arraylocked = true;
+       }
+}
 
-       glFrustum(-xmax, xmax, -ymax, ymax, zNear, zFar );
+void GL_UnlockArray(void)
+{
+       if (arraylocked)
+       {
+               qglUnlockArraysEXT();
+               CHECKGLERROR
+               arraylocked = false;
+       }
 }
 
+//static float gldepthmin, gldepthmax;
 
 /*
 =============
@@ -301,39 +303,59 @@ GL_SetupFrame
 */
 static void GL_SetupFrame (void)
 {
+       double xmax, ymax;
+       double fovx, fovy, zNear, zFar, aspect;
+
+       // update farclip based on previous frame
+       r_farclip = r_newfarclip;
+
        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);
+//     gldepthmin = 0;
+//     gldepthmax = 1;
+       glDepthFunc (GL_LEQUAL);CHECKGLERROR
 
-       glDepthRange (gldepthmin, gldepthmax);
-
-       // update farclip based on previous frame
-       r_farclip = r_newfarclip;
+//     glDepthRange (gldepthmin, gldepthmax);CHECKGLERROR
 
        // set up viewpoint
-       glMatrixMode(GL_PROJECTION);
-       glLoadIdentity ();
+       glMatrixMode(GL_PROJECTION);CHECKGLERROR
+       glLoadIdentity ();CHECKGLERROR
 
        // 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);
+       glViewport(r_refdef.x, vid.realheight - (r_refdef.y + r_refdef.height), r_refdef.width, r_refdef.height);CHECKGLERROR
+
+       // depth range
+       zNear = 1.0;
+       zFar = r_farclip;
+
+       // fov angles
+       fovx = r_refdef.fov_x;
+       fovy = r_refdef.fov_y;
+       aspect = r_refdef.width / r_refdef.height;
+
+       // pyramid slopes
+       xmax = zNear * tan(fovx * M_PI / 360.0) * aspect;
+       ymax = zNear * tan(fovy * M_PI / 360.0);
 
-       glCullFace(GL_FRONT);
+       // set view pyramid
+       glFrustum(-xmax, xmax, -ymax, ymax, zNear, zFar);CHECKGLERROR
 
-       glMatrixMode(GL_MODELVIEW);
-       glLoadIdentity ();
+//     glCullFace(GL_FRONT);CHECKGLERROR
 
-       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]);
+       glMatrixMode(GL_MODELVIEW);CHECKGLERROR
+       glLoadIdentity ();CHECKGLERROR
+
+       // put Z going up
+       glRotatef (-90,  1, 0, 0);CHECKGLERROR
+       glRotatef (90,  0, 0, 1);CHECKGLERROR
+       // camera rotation
+       glRotatef (-r_refdef.viewangles[2],  1, 0, 0);CHECKGLERROR
+       glRotatef (-r_refdef.viewangles[0],  0, 1, 0);CHECKGLERROR
+       glRotatef (-r_refdef.viewangles[1],  0, 0, 1);CHECKGLERROR
+       // camera location
+       glTranslatef (-r_refdef.vieworg[0],  -r_refdef.vieworg[1],  -r_refdef.vieworg[2]);CHECKGLERROR
 
 //     glGetFloatv (GL_MODELVIEW_MATRIX, r_world_matrix);
 
@@ -341,25 +363,103 @@ static void GL_SetupFrame (void)
        // set drawing parms
        //
 //     if (gl_cull.integer)
-               glEnable(GL_CULL_FACE);
+//     {
+//             glEnable(GL_CULL_FACE);CHECKGLERROR
+//     }
 //     else
-//             glDisable(GL_CULL_FACE);
+//     {
+//             glDisable(GL_CULL_FACE);CHECKGLERROR
+//     }
 
-       glEnable(GL_BLEND); // was Disable
-       glEnable(GL_DEPTH_TEST);
-       glDepthMask(1);
+//     glEnable(GL_BLEND);CHECKGLERROR
+//     glEnable(GL_DEPTH_TEST);CHECKGLERROR
+//     glDepthMask(1);CHECKGLERROR
 }
 
-static float viewdist;
-
-int c_meshs, c_meshtris, c_transmeshs, c_transtris;
+static int mesh_blendfunc1;
+static int mesh_blendfunc2;
+static int mesh_blend;
+static GLboolean mesh_depthmask;
+static int mesh_depthtest;
+static int mesh_unit;
+static int mesh_clientunit;
+static int mesh_texture[MAX_TEXTUREUNITS];
+static float mesh_texturergbscale[MAX_TEXTUREUNITS];
+
+void GL_SetupTextureState(void)
+{
+       int i;
+       if (backendunits > 1)
+       {
+               for (i = 0;i < backendunits;i++)
+               {
+                       qglActiveTexture(GL_TEXTURE0_ARB + (mesh_unit = i));CHECKGLERROR
+                       glBindTexture(GL_TEXTURE_2D, mesh_texture[i]);CHECKGLERROR
+                       if (gl_combine.integer)
+                       {
+                               glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);CHECKGLERROR
+                               glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);CHECKGLERROR
+                               glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);CHECKGLERROR
+                               glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
+                               glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_CONSTANT_ARB);CHECKGLERROR
+                               glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
+                               glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
+                               glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);CHECKGLERROR
+                               glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);CHECKGLERROR
+                               glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);CHECKGLERROR
+                               glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
+                               glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, GL_CONSTANT_ARB);CHECKGLERROR
+                               glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
+                               glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
+                               glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
+                               glTexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, mesh_texturergbscale[i]);CHECKGLERROR
+                               glTexEnvf(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1.0f);CHECKGLERROR
+                       }
+                       else
+                       {
+                               glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
+                       }
+                       qglClientActiveTexture(GL_TEXTURE0_ARB + (mesh_clientunit = i));CHECKGLERROR
+                       glTexCoordPointer(2, GL_FLOAT, sizeof(buf_texcoord_t), buf_texcoord[i]);CHECKGLERROR
+                       if (mesh_texture[i])
+                       {
+                               glEnable(GL_TEXTURE_2D);CHECKGLERROR
+                               glEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
+                       }
+                       else
+                       {
+                               glDisable(GL_TEXTURE_2D);CHECKGLERROR
+                               glDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
+                       }
+               }
+       }
+       else
+       {
+               glBindTexture(GL_TEXTURE_2D, mesh_texture[0]);CHECKGLERROR
+               glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
+               glTexCoordPointer(2, GL_FLOAT, sizeof(buf_texcoord_t), buf_texcoord[0]);CHECKGLERROR
+               if (mesh_texture[0])
+               {
+                       glEnable(GL_TEXTURE_2D);CHECKGLERROR
+                       glEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
+               }
+               else
+               {
+                       glDisable(GL_TEXTURE_2D);CHECKGLERROR
+                       glDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
+               }
+       }
+}
 
 // called at beginning of frame
-void R_Mesh_Clear(void)
+void R_Mesh_Start(void)
 {
+       int i;
        if (!backendactive)
                Sys_Error("R_Mesh_Clear: called when backend is not active\n");
 
+       CHECKGLERROR
+
        gl_backend_bufferchanges(false);
 
        currentmesh = 0;
@@ -369,10 +469,6 @@ void R_Mesh_Clear(void)
        currenttranstriangle = 0;
        currenttransvertex = 0;
        meshfarclip = 0;
-       meshmerge = gl_mesh_merge.integer;
-#if FLOATCOLORS
-       floatcolors = gl_mesh_floatcolors.integer;
-#endif
        transranout = false;
        viewdist = DotProduct(r_origin, vpn);
 
@@ -382,59 +478,56 @@ void R_Mesh_Clear(void)
        c_transtris = 0;
 
        GL_SetupFrame();
-}
 
-#ifdef DEBUGGL
-void GL_PrintError(int errornumber, char *filename, int linenumber)
-{
-       switch(errornumber)
+       mesh_unit = 0;
+       mesh_clientunit = 0;
+
+       for (i = 0;i < backendunits;i++)
        {
-       case GL_INVALID_ENUM:
-               Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
-               break;
-       case GL_INVALID_VALUE:
-               Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
-               break;
-       case GL_INVALID_OPERATION:
-               Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
-               break;
-       case GL_STACK_OVERFLOW:
-               Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
-               break;
-       case GL_STACK_UNDERFLOW:
-               Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
-               break;
-       case GL_OUT_OF_MEMORY:
-               Con_Printf("GL_OUT_OF_MEMORY at %s:%i\n", filename, linenumber);
-               break;
-#ifdef GL_TABLE_TOO_LARGE
-    case GL_TABLE_TOO_LARGE:
-               Con_Printf("GL_TABLE_TOO_LARGE at %s:%i\n", filename, linenumber);
-               break;
-#endif
-       default:
-               Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
-               break;
+               mesh_texture[i] = 0;
+               mesh_texturergbscale[i] = 1;
+       }
+
+       glEnable(GL_CULL_FACE);CHECKGLERROR
+       glCullFace(GL_FRONT);CHECKGLERROR
+
+       mesh_depthtest = true;
+       glEnable(GL_DEPTH_TEST);CHECKGLERROR
+
+       mesh_blendfunc1 = GL_ONE;
+       mesh_blendfunc2 = GL_ZERO;
+       glBlendFunc(mesh_blendfunc1, mesh_blendfunc2);CHECKGLERROR
+
+       mesh_blend = 0;
+       glDisable(GL_BLEND);CHECKGLERROR
+
+       mesh_depthmask = GL_TRUE;
+       glDepthMask(mesh_depthmask);CHECKGLERROR
+
+       glVertexPointer(3, GL_FLOAT, sizeof(buf_vertex_t), &buf_vertex[0].v[0]);CHECKGLERROR
+       glEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
+       if (gl_mesh_floatcolors.integer)
+       {
+               glColorPointer(4, GL_FLOAT, sizeof(buf_fcolor_t), &buf_fcolor[0].c[0]);CHECKGLERROR
+       }
+       else
+       {
+               glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(buf_bcolor_t), &buf_bcolor[0].c[0]);CHECKGLERROR
        }
+       glEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
+
+       GL_SetupTextureState();
 }
 
-int errornumber = 0;
-#endif
+int gl_backend_rebindtextures;
 
-// renders mesh buffers, called to flush buffers when full
-void R_Mesh_Render(void)
+void GL_UpdateFarclip(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;
-       if (!backendactive)
-               Sys_Error("R_Mesh_Render: called when backend is not active\n");
-       if (!currentmesh)
-               return;
-
-CHECKGLERROR
+       int i;
+       float farclip;
 
        // push out farclip based on vertices
+       // FIXME: wouldn't this be slow when using matrix transforms?
        for (i = 0;i < currentvertex;i++)
        {
                farclip = DotProduct(buf_vertex[i].v, vpn);
@@ -447,773 +540,406 @@ CHECKGLERROR
        // push out farclip for next frame
        if (farclip > r_newfarclip)
                r_newfarclip = ceil((farclip + 255) / 256) * 256 + 256;
+}
 
-       for (i = 0;i < backendunits;i++)
-               texturergbscale[i] = 1;
-
-       glEnable(GL_CULL_FACE);
-CHECKGLERROR
-       glCullFace(GL_FRONT);
-CHECKGLERROR
-       depthtest = true;
-       glEnable(GL_DEPTH_TEST);
-CHECKGLERROR
-       blendfunc1 = GL_ONE;
-       blendfunc2 = GL_ZERO;
-       glBlendFunc(blendfunc1, blendfunc2);
-CHECKGLERROR
-       blend = 0;
-       glDisable(GL_BLEND);
-CHECKGLERROR
-       depthmask = true;
-       glDepthMask((GLboolean) depthmask);
-CHECKGLERROR
-
-       glVertexPointer(3, GL_FLOAT, sizeof(buf_vertex_t), buf_vertex);
-CHECKGLERROR
-       glEnableClientState(GL_VERTEX_ARRAY);
-CHECKGLERROR
-#if FLOATCOLORS
-       if (floatcolors)
+void GL_ConvertColorsFloatToByte(void)
+{
+       int i, k, *icolor;
+       float *fcolor;
+       qbyte *bcolor;
+
+       // shift float to have 8bit fraction at base of number
+       for (i = 0, fcolor = &buf_fcolor->c[0];i < currentvertex;i++)
        {
-               glColorPointer(4, GL_FLOAT, sizeof(buf_fcolor_t), buf_fcolor);
-CHECKGLERROR
+               *fcolor++ += 32768.0f;
+               *fcolor++ += 32768.0f;
+               *fcolor++ += 32768.0f;
+               *fcolor++ += 32768.0f;
        }
-       else
+       // 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++)
        {
-#endif
-               glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(buf_bcolor_t), buf_bcolor);
-CHECKGLERROR
-#if FLOATCOLORS
+               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;
        }
-#endif
-       glEnableClientState(GL_COLOR_ARRAY);
-CHECKGLERROR
+}
 
+void GL_MeshState(buf_mesh_t *mesh)
+{
+       int i;
        if (backendunits > 1)
        {
                for (i = 0;i < backendunits;i++)
                {
-                       qglActiveTexture(GL_TEXTURE0_ARB + (unit = i));
-CHECKGLERROR
-                       glBindTexture(GL_TEXTURE_2D, (texture[i] = 0));
-CHECKGLERROR
-                       glDisable(GL_TEXTURE_2D);
-CHECKGLERROR
-                       if (gl_combine.integer)
-                       {
-                               glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
-CHECKGLERROR
-                               glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);
-CHECKGLERROR
-                               glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);
-CHECKGLERROR
-                               glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);
-CHECKGLERROR
-                               glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_CONSTANT_ARB);
-CHECKGLERROR
-                               glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
-CHECKGLERROR
-                               glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);
-CHECKGLERROR
-                               glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);
-CHECKGLERROR
-                               glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);
-CHECKGLERROR
-                               glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);
-CHECKGLERROR
-                               glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS_ARB);
-CHECKGLERROR
-                               glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, GL_CONSTANT_ARB);
-CHECKGLERROR
-                               glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);
-CHECKGLERROR
-                               glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA);
-CHECKGLERROR
-                               glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, GL_SRC_ALPHA);
-CHECKGLERROR
-                               glTexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1.0f);
-CHECKGLERROR
-                               glTexEnvf(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1.0f);
-CHECKGLERROR
-                       }
-                       else
-                       {
-                               glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
-CHECKGLERROR
-                       }
-
-                       qglClientActiveTexture(GL_TEXTURE0_ARB + (clientunit = i));
-CHECKGLERROR
-                       glTexCoordPointer(2, GL_FLOAT, sizeof(buf_texcoord_t), buf_texcoord[i]);
-CHECKGLERROR
-                       glEnableClientState(GL_TEXTURE_COORD_ARRAY);
-CHECKGLERROR
-               }
-       }
-       else
-       {
-               glBindTexture(GL_TEXTURE_2D, (texture[0] = 0));
-CHECKGLERROR
-               glDisable(GL_TEXTURE_2D);
-CHECKGLERROR
-               glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
-CHECKGLERROR
-
-               glTexCoordPointer(2, GL_FLOAT, sizeof(buf_texcoord_t), buf_texcoord[0]);
-CHECKGLERROR
-               glEnableClientState(GL_TEXTURE_COORD_ARRAY);
-CHECKGLERROR
-       }
-
-       // lock as early as possible
-       GL_LockArray(0, currentvertex);
-CHECKGLERROR
-
-       for (k = 0;k < currentmesh;)
-       {
-               mesh = &buf_mesh[k];
-
-               if (backendunits > 1)
-               {
-//                     int topunit = 0;
-                       for (i = 0;i < backendunits;i++)
+                       if (mesh_texture[i] != mesh->textures[i])
                        {
-                               if (texture[i] != mesh->textures[i])
+                               if (mesh_unit != i)
                                {
-                                       if (unit != i)
-                                       {
-                                               qglActiveTexture(GL_TEXTURE0_ARB + (unit = i));
-CHECKGLERROR
-                                       }
-                                       if (texture[i] == 0)
-                                       {
-                                               glEnable(GL_TEXTURE_2D);
-CHECKGLERROR
-                                               // have to disable texcoord array on disabled texture
-                                               // units due to NVIDIA driver bug with
-                                               // compiled_vertex_array
-                                               if (clientunit != i)
-                                               {
-                                                       qglClientActiveTexture(GL_TEXTURE0_ARB + (clientunit = i));
-CHECKGLERROR
-                                               }
-                                               glEnableClientState(GL_TEXTURE_COORD_ARRAY);
-CHECKGLERROR
-                                       }
-                                       glBindTexture(GL_TEXTURE_2D, (texture[i] = mesh->textures[i]));
-CHECKGLERROR
-                                       if (texture[i] == 0)
+                                       qglActiveTexture(GL_TEXTURE0_ARB + (mesh_unit = i));CHECKGLERROR
+                               }
+                               if (mesh_texture[i] == 0)
+                               {
+                                       glEnable(GL_TEXTURE_2D);CHECKGLERROR
+                                       // have to disable texcoord array on disabled texture
+                                       // units due to NVIDIA driver bug with
+                                       // compiled_vertex_array
+                                       if (mesh_clientunit != i)
                                        {
-                                               glDisable(GL_TEXTURE_2D);
-CHECKGLERROR
-                                               // have to disable texcoord array on disabled texture
-                                               // units due to NVIDIA driver bug with
-                                               // compiled_vertex_array
-                                               if (clientunit != i)
-                                               {
-                                                       qglClientActiveTexture(GL_TEXTURE0_ARB + (clientunit = i));
-CHECKGLERROR
-                                               }
-                                               glDisableClientState(GL_TEXTURE_COORD_ARRAY);
-CHECKGLERROR
+                                               qglClientActiveTexture(GL_TEXTURE0_ARB + (mesh_clientunit = i));CHECKGLERROR
                                        }
+                                       glEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
                                }
-                               if (texturergbscale[i] != mesh->texturergbscale[i])
+                               glBindTexture(GL_TEXTURE_2D, (mesh_texture[i] = mesh->textures[i]));CHECKGLERROR
+                               if (mesh_texture[i] == 0)
                                {
-                                       if (unit != i)
+                                       glDisable(GL_TEXTURE_2D);CHECKGLERROR
+                                       // have to disable texcoord array on disabled texture
+                                       // units due to NVIDIA driver bug with
+                                       // compiled_vertex_array
+                                       if (mesh_clientunit != i)
                                        {
-                                               qglActiveTexture(GL_TEXTURE0_ARB + (unit = i));
-CHECKGLERROR
+                                               qglClientActiveTexture(GL_TEXTURE0_ARB + (mesh_clientunit = i));CHECKGLERROR
                                        }
-                                       glTexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (texturergbscale[i] = mesh->texturergbscale[i]));
-CHECKGLERROR
+                                       glDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
                                }
-//                             if (texture[i])
-//                                     topunit = i;
                        }
-//                     if (unit != topunit)
-//                     {
-//                             qglActiveTexture(GL_TEXTURE0_ARB + (unit = topunit));
-//CHECKGLERROR
-//                     }
-               }
-               else
-               {
-                       if (texture[0] != mesh->textures[0])
+                       if (mesh_texturergbscale[i] != mesh->texturergbscale[i])
                        {
-                               if (texture[0] == 0)
-                               {
-                                       glEnable(GL_TEXTURE_2D);
-CHECKGLERROR
-                                       glEnableClientState(GL_TEXTURE_COORD_ARRAY);
-CHECKGLERROR
-                               }
-                               glBindTexture(GL_TEXTURE_2D, (texture[0] = mesh->textures[0]));
-CHECKGLERROR
-                               if (texture[0] == 0)
+                               if (mesh_unit != i)
                                {
-                                       glDisable(GL_TEXTURE_2D);
-CHECKGLERROR
-                                       glDisableClientState(GL_TEXTURE_COORD_ARRAY);
-CHECKGLERROR
+                                       qglActiveTexture(GL_TEXTURE0_ARB + (mesh_unit = i));CHECKGLERROR
                                }
+                               glTexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (mesh_texturergbscale[i] = mesh->texturergbscale[i]));CHECKGLERROR
                        }
                }
-               if (blendfunc1 != mesh->blendfunc1 || blendfunc2 != mesh->blendfunc2)
+       }
+       else
+       {
+               if (mesh_texture[0] != mesh->textures[0])
                {
-                       blendfunc1 = mesh->blendfunc1;
-                       blendfunc2 = mesh->blendfunc2;
-                       glBlendFunc(blendfunc1, blendfunc2);
-CHECKGLERROR
-                       if (blendfunc2 == GL_ZERO)
+                       if (mesh_texture[0] == 0)
                        {
-                               if (blendfunc1 == GL_ONE)
-                               {
-                                       if (blend)
-                                       {
-                                               blend = 0;
-                                               glDisable(GL_BLEND);
-CHECKGLERROR
-                                       }
-                               }
-                               else
+                               glEnable(GL_TEXTURE_2D);CHECKGLERROR
+                               glEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
+                       }
+                       glBindTexture(GL_TEXTURE_2D, (mesh_texture[0] = mesh->textures[0]));CHECKGLERROR
+                       if (mesh_texture[0] == 0)
+                       {
+                               glDisable(GL_TEXTURE_2D);CHECKGLERROR
+                               glDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
+                       }
+               }
+       }
+       if (mesh_blendfunc1 != mesh->blendfunc1 || mesh_blendfunc2 != mesh->blendfunc2)
+       {
+               glBlendFunc(mesh_blendfunc1 = mesh->blendfunc1, mesh_blendfunc2 = mesh->blendfunc2);CHECKGLERROR
+               if (mesh_blendfunc2 == GL_ZERO)
+               {
+                       if (mesh_blendfunc1 == GL_ONE)
+                       {
+                               if (mesh_blend)
                                {
-                                       if (!blend)
-                                       {
-                                               blend = 1;
-                                               glEnable(GL_BLEND);
-CHECKGLERROR
-                                       }
+                                       mesh_blend = 0;
+                                       glDisable(GL_BLEND);CHECKGLERROR
                                }
                        }
                        else
                        {
-                               if (!blend)
+                               if (!mesh_blend)
                                {
-                                       blend = 1;
-                                       glEnable(GL_BLEND);
-CHECKGLERROR
+                                       mesh_blend = 1;
+                                       glEnable(GL_BLEND);CHECKGLERROR
                                }
                        }
                }
-               if (depthtest != mesh->depthtest)
-               {
-                       depthtest = mesh->depthtest;
-                       if (depthtest)
-                               glEnable(GL_DEPTH_TEST);
-                       else
-                               glDisable(GL_DEPTH_TEST);
-               }
-               if (depthmask != mesh->depthmask)
-               {
-                       depthmask = mesh->depthmask;
-                       glDepthMask((GLboolean) depthmask);
-CHECKGLERROR
-               }
-
-               firsttriangle = mesh->firsttriangle;
-               triangles = mesh->triangles;
-               firstvert = mesh->firstvert;
-               lastvert = mesh->lastvert;
-               mesh = &buf_mesh[++k];
-
-               if (meshmerge)
+               else
                {
-                       #if MAX_TEXTUREUNITS != 4
-                       #error update this code
-                       #endif
-                       while (k < currentmesh
-                               && mesh->blendfunc1 == blendfunc1
-                               && mesh->blendfunc2 == blendfunc2
-                               && mesh->depthtest == depthtest
-                               && mesh->depthmask == depthmask
-                               && mesh->textures[0] == texture[0]
-                               && mesh->textures[1] == texture[1]
-                               && mesh->textures[2] == texture[2]
-                               && mesh->textures[3] == texture[3]
-                               && mesh->texturergbscale[0] == texturergbscale[0]
-                               && mesh->texturergbscale[1] == texturergbscale[1]
-                               && mesh->texturergbscale[2] == texturergbscale[2]
-                               && mesh->texturergbscale[3] == texturergbscale[3])
+                       if (!mesh_blend)
                        {
-                               triangles += mesh->triangles;
-                               if (firstvert > mesh->firstvert)
-                                       firstvert = mesh->firstvert;
-                               if (lastvert < mesh->lastvert)
-                                       lastvert = mesh->lastvert;
-                               mesh = &buf_mesh[++k];
+                               mesh_blend = 1;
+                               glEnable(GL_BLEND);CHECKGLERROR
                        }
                }
-
-#ifdef WIN32
-               // FIXME: dynamic link to GL so we can get DrawRangeElements on WIN32
-               glDrawElements(GL_TRIANGLES, triangles * 3, GL_UNSIGNED_INT, (unsigned int *)&buf_tri[firsttriangle]);
-#else
-               glDrawRangeElements(GL_TRIANGLES, firstvert, lastvert + 1, triangles * 3, GL_UNSIGNED_INT, (unsigned int *)&buf_tri[firsttriangle]);
-#endif
-CHECKGLERROR
+       }
+       if (mesh_depthtest != mesh->depthtest)
+       {
+               mesh_depthtest = mesh->depthtest;
+               if (mesh_depthtest)
+                       glEnable(GL_DEPTH_TEST);
+               else
+                       glDisable(GL_DEPTH_TEST);
+       }
+       if (mesh_depthmask != mesh->depthmask)
+       {
+               glDepthMask(mesh_depthmask = mesh->depthmask);CHECKGLERROR
+       }
+}
+
+// renders mesh buffers, called to flush buffers when full
+void R_Mesh_Render(void)
+{
+       int i;
+       int k;
+       int indexcount;
+       int firstvert;
+       buf_mesh_t *mesh;
+       unsigned int *index;
+
+       if (!backendactive)
+               Sys_Error("R_Mesh_Render: called when backend is not active\n");
+
+       if (!currentmesh)
+               return;
+
+       CHECKGLERROR
+
+       GL_UpdateFarclip();
+
+       if (!gl_mesh_floatcolors.integer)
+               GL_ConvertColorsFloatToByte();
+
+       // lock the arrays now that they will have no further modifications
+       //GL_LockArray(0, currentvertex);CHECKGLERROR
+       if (gl_backend_rebindtextures)
+       {
+               gl_backend_rebindtextures = false;
+               GL_SetupTextureState();
+       }
+
+       GL_MeshState(buf_mesh);
+       GL_LockArray(0, currentvertex);
+       #ifdef WIN32
+       // FIXME: dynamic link to GL so we can get DrawRangeElements on WIN32
+       glDrawElements(GL_TRIANGLES, buf_mesh->triangles * 3, GL_UNSIGNED_INT, (unsigned int *)&buf_tri[buf_mesh->firsttriangle].index[0]);CHECKGLERROR
+       #else
+       glDrawRangeElements(GL_TRIANGLES, buf_mesh->firstvert, buf_mesh->firstvert + buf_mesh->verts, buf_mesh->triangles * 3, GL_UNSIGNED_INT, (unsigned int *)&buf_tri[buf_mesh->firsttriangle].index[0]);CHECKGLERROR
+       #endif
+
+       if (currentmesh >= 2)
+       {
+               for (k = 1, mesh = buf_mesh + k;k < currentmesh;k++, mesh++)
+               {
+                       GL_MeshState(mesh);
+
+                       firstvert = mesh->firstvert;
+                       indexcount = mesh->triangles * 3;
+                       index = (unsigned int *)&buf_tri[mesh->firsttriangle].index[0];
+
+                       // if not using batching, skip the index adjustment
+                       if (firstvert != 0)
+                               for (i = 0;i < indexcount;i++)
+                                       index[i] += firstvert;
+
+                       #ifdef WIN32
+                       // FIXME: dynamic link to GL so we can get DrawRangeElements on WIN32
+                       glDrawElements(GL_TRIANGLES, indexcount, GL_UNSIGNED_INT, index);CHECKGLERROR
+                       #else
+                       glDrawRangeElements(GL_TRIANGLES, firstvert, firstvert + mesh->verts, indexcount, GL_UNSIGNED_INT, index);CHECKGLERROR
+                       #endif
+               }
        }
 
        currentmesh = 0;
        currenttriangle = 0;
        currentvertex = 0;
 
-       GL_UnlockArray();
-CHECKGLERROR
+       GL_UnlockArray();CHECKGLERROR
+}
+
+// restores backend state, used when done with 3D rendering
+void R_Mesh_Finish(void)
+{
+       int i;
+       // flush any queued meshs
+       R_Mesh_Render();
 
        if (backendunits > 1)
        {
                for (i = backendunits - 1;i >= 0;i--)
                {
-                       qglActiveTexture(GL_TEXTURE0_ARB + (unit = i));
-CHECKGLERROR
-                       glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
-CHECKGLERROR
+                       qglActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
+                       glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
                        if (gl_combine.integer)
                        {
-                               glTexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1.0f);
-CHECKGLERROR
+                               glTexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1.0f);CHECKGLERROR
                        }
                        if (i > 0)
                        {
-                               glDisable(GL_TEXTURE_2D);
-CHECKGLERROR
+                               glDisable(GL_TEXTURE_2D);CHECKGLERROR
                        }
                        else
                        {
-                               glEnable(GL_TEXTURE_2D);
-CHECKGLERROR
+                               glEnable(GL_TEXTURE_2D);CHECKGLERROR
                        }
-                       glBindTexture(GL_TEXTURE_2D, 0);
-CHECKGLERROR
+                       glBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
 
-                       qglClientActiveTexture(GL_TEXTURE0_ARB + (clientunit = i));
-CHECKGLERROR
-                       glDisableClientState(GL_TEXTURE_COORD_ARRAY);
-CHECKGLERROR
+                       qglClientActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
+                       glDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
                }
        }
        else
        {
-               glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
-CHECKGLERROR
-               glEnable(GL_TEXTURE_2D);
-CHECKGLERROR
-               glDisableClientState(GL_TEXTURE_COORD_ARRAY);
-CHECKGLERROR
+               glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
+               glEnable(GL_TEXTURE_2D);CHECKGLERROR
+               glDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
        }
-       glDisableClientState(GL_COLOR_ARRAY);
-CHECKGLERROR
-       glDisableClientState(GL_VERTEX_ARRAY);
-CHECKGLERROR
-
-       glDisable(GL_BLEND);
-CHECKGLERROR
-       glEnable(GL_DEPTH_TEST);
-CHECKGLERROR
-       glDepthMask(true);
-CHECKGLERROR
-       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-CHECKGLERROR
+       glDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
+       glDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
+
+       glDisable(GL_BLEND);CHECKGLERROR
+       glEnable(GL_DEPTH_TEST);CHECKGLERROR
+       glDepthMask(true);CHECKGLERROR
+       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);CHECKGLERROR
 }
 
-void R_Mesh_AddTransparent(void)
+void R_Mesh_ClearDepth(void)
 {
-       if (gl_mesh_sorttransbymesh.integer)
-       {
-               int i, j, k;
-               float viewdistcompare, centerscaler, dist1, dist2, dist3, center, maxdist;
-               buf_vertex_t *vert1, *vert2, *vert3;
-               buf_transtri_t *tri;
-               buf_mesh_t *mesh, *transmesh;
-
-               // process and add transparent mesh triangles
-               if (!currenttranstriangle)
-                       return;
-
-               // map farclip to 0-4095 list range
-               centerscaler = (TRANSDEPTHRES / r_farclip) * (1.0f / 3.0f);
-               viewdistcompare = viewdist + 4.0f;
-
-               memset(buf_transtri_list, 0, TRANSDEPTHRES * sizeof(buf_transtri_t *));
-
-               k = 0;
-               for (j = 0;j < currenttranstriangle;j++)
-               {
-                       tri = &buf_transtri[j];
-
-                       vert1 = &buf_transvertex[tri->index[0]];
-                       vert2 = &buf_transvertex[tri->index[1]];
-                       vert3 = &buf_transvertex[tri->index[2]];
-
-                       dist1 = DotProduct(vert1->v, vpn);
-                       dist2 = DotProduct(vert2->v, vpn);
-                       dist3 = DotProduct(vert3->v, vpn);
-
-                       maxdist = max(dist1, max(dist2, dist3));
-                       if (maxdist < viewdistcompare)
-                               continue;
-
-                       center = (dist1 + dist2 + dist3) * centerscaler - viewdist;
-       #if SLOWMATH
-                       i = (int) center;
-                       i = bound(0, i, (TRANSDEPTHRES - 1));
-       #else
-                       if (center < 0.0f)
-                               center = 0.0f;
-                       center += 8388608.0f;
-                       i = *((long *)&center) & 0x7FFFFF;
-                       i = min(i, (TRANSDEPTHRES - 1));
-       #endif
-                       tri->next = buf_transtri_list[i];
-                       buf_transtri_list[i] = tri;
-                       k++;
-               }
-
-               #ifndef TRANSBATCH
-               if (currentmesh + k > max_meshs || currenttriangle + k > max_batch || currentvertex + currenttransvertex > max_verts)
-                       R_Mesh_Render();
+       R_Mesh_AddTransparent();
+       R_Mesh_Finish();
+       glClear(GL_DEPTH_BUFFER_BIT);
+       R_Mesh_Start();
+}
 
-               // 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));
-               for (i = 0;i < backendunits;i++)
-                       memcpy(&buf_texcoord[i][currentvertex], &buf_transtexcoord[i][0], currenttransvertex * sizeof(buf_texcoord_t));
-               #endif
+void R_Mesh_AddTransparent(void)
+{
+       int i, j, k, *index;
+       float viewdistcompare, centerscaler, dist1, dist2, dist3, center, maxdist;
+       buf_vertex_t *vert1, *vert2, *vert3;
+       buf_transtri_t *tri;
+       buf_mesh_t *mesh, *transmesh;
 
-               for (i = 0;i < currenttransmesh;i++)
-               {
-                       buf_transmesh[i].transchain = NULL;
-                       //buf_transmesh[i].transchainpointer = &buf_transmesh[i].transchain;
-                       buf_transmesh[i].triangles = 0;
-               }
-               transmesh = NULL;
-               for (j = 0;j < TRANSDEPTHRES;j++)
-               {
-                       if ((tri = buf_transtri_list[j]))
-                       {
-                               for (;tri;tri = tri->next)
-                               {
-                                       if (!tri->mesh->transchain)
-                                       {
-                                               tri->mesh->chain = transmesh;
-                                               transmesh = tri->mesh;
-                                       }
-                                       tri->meshsortchain = tri->mesh->transchain;
-                                       tri->mesh->transchain = tri;
-                                       /*
-                                       *tri->mesh->transchainpointer = tri;
-                                       tri->meshsortchain = NULL;
-                                       tri->mesh->transchainpointer = &tri->meshsortchain;
-                                       */
-                                       tri->mesh->triangles++;
-                               }
-                       }
-               }
+       if (!currenttransmesh)
+               return;
 
-               #if TRANSBATCH
-               for (;transmesh;transmesh = transmesh->chain)
-               {
-                       int meshvertexadjust;
-                       int numverts = transmesh->lastvert - transmesh->firstvert + 1;
-                       if (currentmesh >= max_meshs || currenttriangle + transmesh->triangles > max_batch || currentvertex + numverts > max_verts)
-                               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));
-                       for (i = 0;i < backendunits && transmesh->textures[i];i++)
-                               memcpy(&buf_texcoord[i][currentvertex], &buf_transtexcoord[i][transmesh->firstvert], numverts * sizeof(buf_texcoord_t));
-
-                       mesh = &buf_mesh[currentmesh++];
-                       *mesh = *transmesh; // copy mesh properties
-                       mesh->firstvert = currentvertex;
-                       mesh->lastvert = currentvertex + numverts - 1;
-                       currentvertex += numverts;
-                       meshvertexadjust = mesh->firstvert - transmesh->firstvert;
-                       mesh->firsttriangle = currenttriangle;
-                       for (tri = transmesh->transchain;tri;tri = tri->meshsortchain)
-                       {
-                               buf_tri[currenttriangle].index[0] = tri->index[0] + meshvertexadjust;
-                               buf_tri[currenttriangle].index[1] = tri->index[1] + meshvertexadjust;
-                               buf_tri[currenttriangle].index[2] = tri->index[2] + meshvertexadjust;
-                               /*
-                               if (tri->mesh != transmesh)
-                                       Sys_Error("!?!");
-                               if ((unsigned int) buf_tri[currenttriangle].index[0] < (unsigned int) mesh->firstvert
-                                || (unsigned int) buf_tri[currenttriangle].index[0] > (unsigned int) mesh->lastvert
-                                || (unsigned int) buf_tri[currenttriangle].index[1] < (unsigned int) mesh->firstvert
-                                || (unsigned int) buf_tri[currenttriangle].index[1] > (unsigned int) mesh->lastvert
-                                || (unsigned int) buf_tri[currenttriangle].index[2] < (unsigned int) mesh->firstvert
-                                || (unsigned int) buf_tri[currenttriangle].index[2] > (unsigned int) mesh->lastvert)
-                                       Sys_Error("!?");
-                               */
-                               currenttriangle++;
-                       }
-                       /*
-                       if (mesh->triangles != currenttriangle - mesh->firsttriangle)
-                               Sys_Error("!");
-                       */
-               }
-               #else
-               for (;transmesh;transmesh = transmesh->chain)
+       // convert index data to transtris for sorting
+       for (j = 0;j < currenttransmesh;j++)
+       {
+               mesh = buf_transmesh + j;
+               k = mesh->firsttriangle;
+               index = &buf_transtri[k].index[0];
+               for (i = 0;i < mesh->triangles;i++)
                {
-                       mesh = &buf_mesh[currentmesh++];
-                       *mesh = *transmesh; // copy mesh properties
-                       mesh->firstvert += currentvertex;
-                       mesh->lastvert += currentvertex;
-                       mesh->firsttriangle = currenttriangle;
-                       for (tri = transmesh->transchain;tri;tri = tri->meshsortchain)
-                       {
-                               buf_tri[currenttriangle].index[0] = tri->index[0] + currentvertex;
-                               buf_tri[currenttriangle].index[1] = tri->index[1] + currentvertex;
-                               buf_tri[currenttriangle].index[2] = tri->index[2] + currentvertex;
-                               /*
-                               if (tri->mesh != transmesh)
-                                       Sys_Error("!?!");
-                               if ((unsigned int) buf_tri[currenttriangle].index[0] < (unsigned int) mesh->firstvert
-                                || (unsigned int) buf_tri[currenttriangle].index[0] > (unsigned int) mesh->lastvert
-                                || (unsigned int) buf_tri[currenttriangle].index[1] < (unsigned int) mesh->firstvert
-                                || (unsigned int) buf_tri[currenttriangle].index[1] > (unsigned int) mesh->lastvert
-                                || (unsigned int) buf_tri[currenttriangle].index[2] < (unsigned int) mesh->firstvert
-                                || (unsigned int) buf_tri[currenttriangle].index[2] > (unsigned int) mesh->lastvert)
-                                       Sys_Error("!?");
-                               */
-                               currenttriangle++;
-                       }
-                       /*
-                       if (mesh->triangles != currenttriangle - mesh->firsttriangle)
-                               Sys_Error("!");
-                       */
+                       tri = &buf_sorttranstri[k++];
+                       tri->mesh = mesh;
+                       tri->index[0] = *index++;
+                       tri->index[1] = *index++;
+                       tri->index[2] = *index++;
                }
-               currentvertex += currenttransvertex;
-               #endif
-
-               currenttransmesh = 0;
-               currenttranstriangle = 0;
-               currenttransvertex = 0;
        }
-       else if (gl_mesh_dupetransverts.integer)
-       {
-               int i, j, k, index;
-               float viewdistcompare, centerscaler, dist1, dist2, dist3, center, maxdist;
-               buf_vertex_t *vert1, *vert2, *vert3;
-               buf_transtri_t *tri;
-               buf_mesh_t *mesh;
-
-               // process and add transparent mesh triangles
-               if (!currenttranstriangle)
-                       return;
 
-               // map farclip to 0-4095 list range
-               centerscaler = (TRANSDEPTHRES / r_farclip) * (1.0f / 3.0f);
-               viewdistcompare = viewdist + 4.0f;
-
-               memset(buf_transtri_list, 0, TRANSDEPTHRES * sizeof(buf_transtri_t *));
-
-               // process in reverse because transtri_list adding code is in reverse as well
-               k = 0;
-               for (j = currenttranstriangle - 1;j >= 0;j--)
-               {
-                       tri = &buf_transtri[j];
+       // map farclip to 0-4095 list range
+       centerscaler = (TRANSDEPTHRES / r_farclip) * (1.0f / 3.0f);
+       viewdistcompare = viewdist + 4.0f;
 
-                       vert1 = &buf_transvertex[tri->index[0]];
-                       vert2 = &buf_transvertex[tri->index[1]];
-                       vert3 = &buf_transvertex[tri->index[2]];
+       memset(buf_sorttranstri_list, 0, TRANSDEPTHRES * sizeof(buf_transtri_t *));
 
-                       dist1 = DotProduct(vert1->v, vpn);
-                       dist2 = DotProduct(vert2->v, vpn);
-                       dist3 = DotProduct(vert3->v, vpn);
+       k = 0;
+       for (j = 0;j < currenttranstriangle;j++)
+       {
+               tri = &buf_sorttranstri[j];
+               i = tri->mesh->firstvert;
 
-                       maxdist = max(dist1, max(dist2, dist3));
-                       if (maxdist < viewdistcompare)
-                               continue;
+               vert1 = &buf_transvertex[tri->index[0] + i];
+               vert2 = &buf_transvertex[tri->index[1] + i];
+               vert3 = &buf_transvertex[tri->index[2] + i];
 
-                       center = (dist1 + dist2 + dist3) * centerscaler - viewdist;
-       #if SLOWMATH
-                       i = (int) center;
-                       i = bound(0, i, (TRANSDEPTHRES - 1));
-       #else
-                       if (center < 0.0f)
-                               center = 0.0f;
-                       center += 8388608.0f;
-                       i = *((long *)&center) & 0x7FFFFF;
-                       i = min(i, (TRANSDEPTHRES - 1));
-       #endif
-                       tri->next = buf_transtri_list[i];
-                       buf_transtri_list[i] = tri;
-                       k++;
-               }
+               dist1 = DotProduct(vert1->v, vpn);
+               dist2 = DotProduct(vert2->v, vpn);
+               dist3 = DotProduct(vert3->v, vpn);
 
-               if (currentmesh + k > max_meshs || currenttriangle + k > max_batch || currentvertex + k * 3 > max_verts)
-                       R_Mesh_Render();
+               maxdist = max(dist1, max(dist2, dist3));
+               if (maxdist < viewdistcompare)
+                       continue;
 
-               currenttransmesh = 0;
-               currenttranstriangle = 0;
-               currenttransvertex = 0;
+               center = (dist1 + dist2 + dist3) * centerscaler - viewdist;
+#if SLOWMATH
+               i = (int) center;
+               i = bound(0, i, (TRANSDEPTHRES - 1));
+#else
+               if (center < 0.0f)
+                       center = 0.0f;
+               center += 8388608.0f;
+               i = *((long *)&center) & 0x7FFFFF;
+               i = min(i, (TRANSDEPTHRES - 1));
+#endif
+               tri->next = buf_sorttranstri_list[i];
+               buf_sorttranstri_list[i] = tri;
+               k++;
+       }
 
-               // 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
-               for (j = TRANSDEPTHRES - 1;j >= 0;j--)
+       for (i = 0;i < currenttransmesh;i++)
+               buf_transmesh[i].transchain = NULL;
+       transmesh = NULL;
+       for (j = 0;j < TRANSDEPTHRES;j++)
+       {
+               if ((tri = buf_sorttranstri_list[j]))
                {
-                       if ((tri = buf_transtri_list[j]))
+                       for (;tri;tri = tri->next)
                        {
-                               while(tri)
+                               if (!tri->mesh->transchain)
                                {
-                                       mesh = &buf_mesh[currentmesh++];
-                                       *mesh = *tri->mesh; // copy mesh properties
-                                       mesh->firstvert = currentvertex;
-                                       mesh->lastvert = currentvertex + 2;
-                                       mesh->firsttriangle = currenttriangle;
-                                       mesh->triangles = 1;
-                                       for (k = 0;k < 3;k++)
-                                       {
-                                               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));
-                                               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++;
-                                       }
-                                       currenttriangle++;
-                                       tri = tri->next;
+                                       tri->mesh->chain = transmesh;
+                                       transmesh = tri->mesh;
                                }
+                               tri->meshsortchain = tri->mesh->transchain;
+                               tri->mesh->transchain = tri;
                        }
                }
        }
-       else
-       {
-               int i, j, k;
-               float viewdistcompare, centerscaler, dist1, dist2, dist3, center, maxdist;
-               buf_vertex_t *vert1, *vert2, *vert3;
-               buf_transtri_t *tri;
-               buf_mesh_t *mesh;
-
-               // process and add transparent mesh triangles
-               if (!currenttranstriangle)
-                       return;
 
-               // map farclip to 0-4095 list range
-               centerscaler = (TRANSDEPTHRES / r_farclip) * (1.0f / 3.0f);
-               viewdistcompare = viewdist + 4.0f;
-
-               memset(buf_transtri_list, 0, TRANSDEPTHRES * sizeof(buf_transtri_t *));
-
-               // process in reverse because transtri_list adding code is in reverse as well
-               k = 0;
-               for (j = currenttranstriangle - 1;j >= 0;j--)
-               {
-                       tri = &buf_transtri[j];
-
-                       vert1 = &buf_transvertex[tri->index[0]];
-                       vert2 = &buf_transvertex[tri->index[1]];
-                       vert3 = &buf_transvertex[tri->index[2]];
-
-                       dist1 = DotProduct(vert1->v, vpn);
-                       dist2 = DotProduct(vert2->v, vpn);
-                       dist3 = DotProduct(vert3->v, vpn);
-
-                       maxdist = max(dist1, max(dist2, dist3));
-                       if (maxdist < viewdistcompare)
-                               continue;
-
-                       center = (dist1 + dist2 + dist3) * centerscaler - viewdist;
-       #if SLOWMATH
-                       i = (int) center;
-                       i = bound(0, i, (TRANSDEPTHRES - 1));
-       #else
-                       if (center < 0.0f)
-                               center = 0.0f;
-                       center += 8388608.0f;
-                       i = *((long *)&center) & 0x7FFFFF;
-                       i = min(i, (TRANSDEPTHRES - 1));
-       #endif
-                       tri->next = buf_transtri_list[i];
-                       buf_transtri_list[i] = tri;
-                       k++;
-               }
-
-               if (currentmesh + k > max_meshs || currenttriangle + k > max_batch || currentvertex + currenttransvertex > max_verts)
+       for (;transmesh;transmesh = transmesh->chain)
+       {
+               if (currentmesh >= max_meshs || currenttriangle + transmesh->triangles > max_batch || currentvertex + transmesh->verts > max_verts)
                        R_Mesh_Render();
 
-               // 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));
-               for (i = 0;i < backendunits;i++)
-                       memcpy(&buf_texcoord[i][currentvertex], &buf_transtexcoord[i][0], currenttransvertex * sizeof(buf_texcoord_t));
+               mesh = &buf_mesh[currentmesh++];
+               *mesh = *transmesh; // copy mesh properties
+
+               mesh->firstvert = currentvertex;
+               memcpy(&buf_vertex[currentvertex], &buf_transvertex[transmesh->firstvert], transmesh->verts * sizeof(buf_vertex_t));
+               memcpy(&buf_fcolor[currentvertex], &buf_transfcolor[transmesh->firstvert], transmesh->verts * sizeof(buf_fcolor_t));
+               for (i = 0;i < backendunits && transmesh->textures[i];i++)
+                       memcpy(&buf_texcoord[i][currentvertex], &buf_transtexcoord[i][transmesh->firstvert], transmesh->verts * sizeof(buf_texcoord_t));
+               currentvertex += mesh->verts;
 
-               for (j = TRANSDEPTHRES - 1;j >= 0;j--)
+               mesh->firsttriangle = currenttriangle;
+               for (tri = transmesh->transchain;tri;tri = tri->meshsortchain)
                {
-                       if ((tri = buf_transtri_list[j]))
-                       {
-                               while(tri)
-                               {
-                                       mesh = &buf_mesh[currentmesh++];
-                                       *mesh = *tri->mesh; // copy mesh properties
-                                       buf_tri[currenttriangle].index[0] = tri->index[0] + currentvertex;
-                                       buf_tri[currenttriangle].index[1] = tri->index[1] + currentvertex;
-                                       buf_tri[currenttriangle].index[2] = tri->index[2] + currentvertex;
-                                       mesh->firstvert = min(buf_tri[currenttriangle].index[0], min(buf_tri[currenttriangle].index[1], buf_tri[currenttriangle].index[2]));
-                                       mesh->lastvert = max(buf_tri[currenttriangle].index[0], max(buf_tri[currenttriangle].index[1], buf_tri[currenttriangle].index[2]));
-                                       mesh->firsttriangle = currenttriangle++;
-                                       mesh->triangles = 1;
-                                       tri = tri->next;
-                               }
-                       }
+                       buf_tri[currenttriangle].index[0] = tri->index[0];
+                       buf_tri[currenttriangle].index[1] = tri->index[1];
+                       buf_tri[currenttriangle].index[2] = tri->index[2];
+                       currenttriangle++;
                }
-               currentvertex += currenttransvertex;
-               currenttransmesh = 0;
-               currenttranstriangle = 0;
-               currenttransvertex = 0;
+               mesh->triangles = currenttriangle - mesh->firsttriangle;
        }
+
+       currenttransmesh = 0;
+       currenttranstriangle = 0;
+       currenttransvertex = 0;
 }
 
 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 int i, j, overbright, *index;
+       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 (!backendactive)
+               Sys_Error("R_Mesh_Draw: called when backend is not active\n");
 
        if (m->index == NULL
         || !m->numtriangles
         || m->vertex == NULL
         || !m->numverts)
-               return;
+               Host_Error("R_Mesh_Draw: no triangles or verts\n");
+
        // ignore meaningless alpha meshs
        if (!m->depthwrite && m->blendfunc1 == GL_SRC_ALPHA && (m->blendfunc2 == GL_ONE || m->blendfunc2 == GL_ONE_MINUS_SRC_ALPHA))
        {
@@ -1232,22 +958,11 @@ void R_Mesh_Draw(const rmeshinfo_t *m)
        if (!backendactive)
                Sys_Error("R_Mesh_Draw: called when backend is not active\n");
 
-       scaler = 1;
-       if (m->blendfunc2 == GL_SRC_COLOR)
-       {
-               if (m->blendfunc1 == GL_DST_COLOR) // 2x modulate with framebuffer
-                       scaler *= 0.5f;
-       }
-       else
-       {
-               if (m->tex[0])
-               {
-                       overbright = gl_combine.integer;
-                       if (overbright)
-                               scaler *= 0.25f;
-               }
-               scaler *= overbrightscale;
-       }
+#ifdef DEBUGGL
+       for (i = 0;i < m->numtriangles * 3;i++)
+               if ((unsigned int) m->index[i] >= (unsigned int) m->numverts)
+                       Host_Error("R_Mesh_Draw: invalid index (%i of %i verts)\n", m->index, m->numverts);
+#endif
 
        if (m->transparent)
        {
@@ -1264,31 +979,18 @@ 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];
 
                // transmesh is only for storage of transparent meshs until they
                // are inserted into the main mesh array
                mesh = &buf_transmesh[currenttransmesh++];
-
-               // transparent meshs are broken up into individual triangles which can
-               // be sorted by depth
-               index = m->index;
-               for (i = 0;i < m->numtriangles;i++)
-               {
-                       tri = &buf_transtri[currenttranstriangle++];
-                       tri->mesh = mesh;
-                       tri->index[0] = *index++ + currenttransvertex;
-                       tri->index[1] = *index++ + currenttransvertex;
-                       tri->index[2] = *index++ + currenttransvertex;
-               }
-
+               mesh->firsttriangle = currenttranstriangle;
                mesh->firstvert = currenttransvertex;
-               mesh->lastvert = currenttransvertex + m->numverts - 1;
+               index = &buf_transtri[currenttranstriangle].index[0];
+
+               currenttranstriangle += m->numtriangles;
                currenttransvertex += m->numverts;
        }
        else
@@ -1305,32 +1007,48 @@ 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];
 
-               mesh = &buf_mesh[currentmesh++];
                // opaque meshs are rendered directly
-               index = (int *)&buf_tri[currenttriangle];
+               mesh = &buf_mesh[currentmesh++];
                mesh->firsttriangle = currenttriangle;
-               currenttriangle += m->numtriangles;
-               for (i = 0;i < m->numtriangles * 3;i++)
-                       index[i] = m->index[i] + currentvertex;
-
                mesh->firstvert = currentvertex;
-               mesh->lastvert = currentvertex + m->numverts - 1;
+               index = &buf_tri[currenttriangle].index[0];
+
+               currenttriangle += m->numtriangles;
                currentvertex += m->numverts;
        }
 
        // code shared for transparent and opaque meshs
+       memcpy(index, m->index, sizeof(int[3]) * m->numtriangles);
        mesh->blendfunc1 = m->blendfunc1;
        mesh->blendfunc2 = m->blendfunc2;
        mesh->depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite);
        mesh->depthtest = !m->depthdisable;
        mesh->triangles = m->numtriangles;
+       mesh->verts = m->numverts;
+
+       overbright = false;
+       scaler = 1;
+       if (m->blendfunc2 == GL_SRC_COLOR)
+       {
+               if (m->blendfunc1 == GL_DST_COLOR) // 2x modulate with framebuffer
+                       scaler *= 0.5f;
+       }
+       else
+       {
+               if (m->tex[0])
+               {
+                       overbright = gl_combine.integer;
+                       if (overbright)
+                               scaler *= 0.25f;
+               }
+               scaler *= overbrightscale;
+       }
+
+
        j = -1;
        for (i = 0;i < backendunits;i++)
        {
@@ -1355,61 +1073,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)
-                       {
-                               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
+               for (i = 0, in = m->color;i < m->numverts;i++, (int)in += m->colorstep)
                {
-                       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++)
        {
@@ -1432,339 +1119,335 @@ void R_Mesh_Draw(const rmeshinfo_t *m)
        #endif
 }
 
-void R_Mesh_DrawPolygon(rmeshinfo_t *m, int numverts)
-{
-       m->index = polyindexarray;
-       m->numverts = numverts;
-       m->numtriangles = numverts - 2;
-       if (m->numtriangles < 1)
-       {
-               Con_Printf("R_Mesh_DrawPolygon: invalid vertex count\n");
-               return;
-       }
-       if (m->numtriangles >= 256)
-       {
-               Con_Printf("R_Mesh_DrawPolygon: only up to 256 triangles (258 verts) supported\n");
-               return;
-       }
-       R_Mesh_Draw(m);
-}
-
-// LordHavoc: this thing is evil, but necessary because decals account for so much overhead
-void R_Mesh_DrawDecal(const rmeshinfo_t *m)
+void R_Mesh_Draw_NativeOnly(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 float cr, cg, cb, ca;
-#endif
+       static int i, j, overbright, *index;
+       static float *in, scaler;
        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;
+       static buf_texcoord_t *texcoord[MAX_TEXTUREUNITS];
 
        if (!backendactive)
                Sys_Error("R_Mesh_Draw: called when backend is not active\n");
 
-       scaler = 1;
-       if (m->tex[0])
+       if (m->index == NULL
+        || !m->numtriangles
+        || m->vertex == NULL
+        || !m->numverts)
+               Host_Error("R_Mesh_Draw: no triangles or verts\n");
+
+       // ignore meaningless alpha meshs
+       if (!m->depthwrite && m->blendfunc1 == GL_SRC_ALPHA && (m->blendfunc2 == GL_ONE || m->blendfunc2 == GL_ONE_MINUS_SRC_ALPHA))
        {
-               overbright = gl_combine.integer;
-               if (overbright)
-                       scaler *= 0.25f;
+               if (m->color)
+               {
+                       for (i = 0, in = m->color + 3;i < m->numverts;i++, (int)in += m->colorstep)
+                               if (*in >= 0.01f)
+                                       break;
+                       if (i == m->numverts)
+                               return;
+               }
+               else if (m->ca < 0.01f)
+                       return;
        }
-       scaler *= overbrightscale;
 
        if (m->transparent)
        {
-               if (currenttransmesh >= max_meshs || (currenttranstriangle + 2) > max_meshs || (currenttransvertex + 4) > max_verts)
+               if (currenttransmesh >= max_meshs || (currenttranstriangle + m->numtriangles) > max_meshs || (currenttransvertex + m->numverts) > max_verts)
                {
                        if (!transranout)
                        {
-                               Con_Printf("R_Mesh_Draw: ran out of room for transparent meshs\n");
+                               Con_Printf("R_Mesh_Draw_NativeOnly: ran out of room for transparent meshs\n");
                                transranout = true;
                        }
                        return;
                }
 
                c_transmeshs++;
-               c_transtris += 2;
+               c_transtris += m->numtriangles;
                vert = &buf_transvertex[currenttransvertex];
-#if FLOATCOLORS
                fcolor = &buf_transfcolor[currenttransvertex];
-#endif
-               bcolor = &buf_transbcolor[currenttransvertex];
-               texcoord = &buf_transtexcoord[0][currenttransvertex];
+               for (i = 0;i < backendunits;i++)
+                       texcoord[i] = &buf_transtexcoord[i][currenttransvertex];
 
                // transmesh is only for storage of transparent meshs until they
                // are inserted into the main mesh array
                mesh = &buf_transmesh[currenttransmesh++];
-               mesh->blendfunc1 = m->blendfunc1;
-               mesh->blendfunc2 = m->blendfunc2;
-               mesh->depthmask = false;
-               mesh->depthtest = true;
-               mesh->triangles = 2;
-               mesh->textures[0] = m->tex[0];
-               mesh->texturergbscale[0] = overbright ? 4 : 1;
-               for (i = 1;i < backendunits;i++)
-               {
-                       mesh->textures[i] = 0;
-                       mesh->texturergbscale[i] = 1;
-               }
-               mesh->chain = NULL;
-
-               // transparent meshs are broken up into individual triangles which can
-               // be sorted by depth
-               index = m->index;
-               tri = &buf_transtri[currenttranstriangle++];
-               tri->mesh = mesh;
-               tri->index[0] = 0 + currenttransvertex;
-               tri->index[1] = 1 + currenttransvertex;
-               tri->index[2] = 2 + currenttransvertex;
-               tri = &buf_transtri[currenttranstriangle++];
-               tri->mesh = mesh;
-               tri->index[0] = 0 + currenttransvertex;
-               tri->index[1] = 2 + currenttransvertex;
-               tri->index[2] = 3 + currenttransvertex;
-
+               mesh->firsttriangle = currenttranstriangle;
                mesh->firstvert = currenttransvertex;
-               mesh->lastvert = currenttransvertex + 3;
-               currenttransvertex += 4;
+               index = &buf_transtri[currenttranstriangle].index[0];
+               currenttranstriangle += m->numtriangles;
+               currenttransvertex += m->numverts;
        }
        else
        {
-               if (2 > max_meshs || 4 > max_verts)
+               if (m->numtriangles > max_meshs || m->numverts > max_verts)
                {
-                       Con_Printf("R_Mesh_Draw: mesh too big for buffers\n");
+                       Con_Printf("R_Mesh_Draw_NativeOnly: mesh too big for buffers\n");
                        return;
                }
 
-               if (currentmesh >= max_meshs || (currenttriangle + 2) > max_batch || (currentvertex + 4) > max_verts)
+               if (currentmesh >= max_meshs || (currenttriangle + m->numtriangles) > max_batch || (currentvertex + m->numverts) > max_verts)
                        R_Mesh_Render();
 
                c_meshs++;
-               c_meshtris += 2;
+               c_meshtris += m->numtriangles;
                vert = &buf_vertex[currentvertex];
-#if FLOATCOLORS
                fcolor = &buf_fcolor[currentvertex];
-#endif
-               bcolor = &buf_bcolor[currentvertex];
-               texcoord = &buf_texcoord[0][currentvertex];
+               for (i = 0;i < backendunits;i++)
+                       texcoord[i] = &buf_texcoord[i][currentvertex];
 
+               // opaque meshs are rendered directly
                mesh = &buf_mesh[currentmesh++];
-               mesh->blendfunc1 = m->blendfunc1;
-               mesh->blendfunc2 = m->blendfunc2;
-               mesh->depthmask = false;
-               mesh->depthtest = !m->depthdisable;
                mesh->firsttriangle = currenttriangle;
-               mesh->triangles = 2;
-               mesh->textures[0] = m->tex[0];
-               mesh->texturergbscale[0] = overbright ? 4 : 1;
-               for (i = 1;i < backendunits;i++)
-               {
-                       mesh->textures[i] = 0;
-                       mesh->texturergbscale[i] = 1;
-               }
-
-               // opaque meshs are rendered directly
-               index = (int *)&buf_tri[currenttriangle];
-               index[0] = 0 + currentvertex;
-               index[1] = 1 + currentvertex;
-               index[2] = 2 + currentvertex;
-               index[3] = 0 + currentvertex;
-               index[4] = 2 + currentvertex;
-               index[5] = 3 + currentvertex;
                mesh->firstvert = currentvertex;
-               mesh->lastvert = currentvertex + 3;
-               currenttriangle += 2;
-               currentvertex += 4;
+               index = &buf_tri[currenttriangle].index[0];
+               currenttriangle += m->numtriangles;
+               currentvertex += m->numverts;
        }
 
-       // buf_vertex_t must match the size of the decal vertex array (or vice versa)
-       memcpy(vert, m->vertex, 4 * sizeof(buf_vertex_t));
+       // code shared for transparent and opaque meshs
+       memcpy(index, m->index, sizeof(int[3]) * m->numtriangles);
+       mesh->blendfunc1 = m->blendfunc1;
+       mesh->blendfunc2 = m->blendfunc2;
+       mesh->depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite);
+       mesh->depthtest = !m->depthdisable;
+       mesh->triangles = m->numtriangles;
+       mesh->verts = m->numverts;
 
-#if FLOATCOLORS
-       if (floatcolors)
+       overbright = false;
+       scaler = 1;
+       if (m->blendfunc2 == GL_SRC_COLOR)
        {
-               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;
+               if (m->blendfunc1 == GL_DST_COLOR) // 2x modulate with framebuffer
+                       scaler *= 0.5f;
        }
        else
        {
-#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
+               if (m->tex[0])
+               {
+                       overbright = gl_combine.integer;
+                       if (overbright)
+                               scaler *= 0.25f;
+               }
+               scaler *= overbrightscale;
        }
-#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));
-}
+       j = -1;
+       for (i = 0;i < backendunits;i++)
+       {
+               if ((mesh->textures[i] = m->tex[i]))
+                       j = i;
+               mesh->texturergbscale[i] = m->texrgbscale[i];
+               if (mesh->texturergbscale[i] != 1 && mesh->texturergbscale[i] != 2 && mesh->texturergbscale[i] != 4)
+                       mesh->texturergbscale[i] = 1;
+       }
+       if (overbright && j >= 0)
+               mesh->texturergbscale[j] = 4;
 
-/*
-==============================================================================
+       if (m->vertexstep != sizeof(buf_vertex_t))
+               Host_Error("R_Mesh_Draw_NativeOnly: unsupported vertexstep\n");
+       if (m->colorstep != sizeof(buf_fcolor_t))
+               Host_Error("R_Mesh_Draw_NativeOnly: unsupported colorstep\n");
+       if (m->color == NULL)
+               Host_Error("R_Mesh_Draw_NativeOnly: must provide color array\n");
+       for (j = 0;j < MAX_TEXTUREUNITS && m->tex[j];j++)
+       {
+               if (j >= backendunits)
+                       Sys_Error("R_Mesh_Draw_NativeOnly: texture %i supplied when there are only %i texture units\n", j + 1, backendunits);
+               if (m->texcoordstep[j] != sizeof(buf_texcoord_t))
+                       Host_Error("R_Mesh_Draw_NativeOnly: unsupported texcoordstep\n");
+       }
 
-                                               SCREEN SHOTS
+       memcpy(vert, m->vertex, m->numverts * sizeof(buf_vertex_t));
+       for (j = 0;j < MAX_TEXTUREUNITS && m->tex[j];j++)
+               memcpy(&texcoord[j][0].t[0], m->texcoords[j], m->numverts * sizeof(buf_texcoord_t));
+       #if 0
+       for (;j < backendunits;j++)
+               memset(&texcoord[j][0].t[0], 0, m->numverts * sizeof(buf_texcoord_t));
+       #endif
 
-==============================================================================
-*/
+       memcpy(fcolor, m->color, m->numverts * sizeof(buf_fcolor_t));
 
-float CalcFov (float fov_x, float width, float height);
-void R_ClearScreen(void);
+       // do this as a second step because memcpy preloaded the cache, which we can't easily do
+       if (scaler != 1)
+       {
+               for (i = 0;i < m->numverts;i++)
+               {
+                       fcolor[i].c[0] *= scaler;
+                       fcolor[i].c[1] *= scaler;
+                       fcolor[i].c[2] *= scaler;
+               }
+       }
+}
 
-void SCR_ScreenShot(char *filename, int x, int y, int width, int height)
+// allocates space in geometry buffers, and fills in pointers to the buffers in passsed struct
+// (this is used for very high speed rendering, no copying)
+int R_Mesh_Draw_GetBuffer(rmeshbufferinfo_t *m)
 {
-       int i;
-       byte *buffer;
+       // these are static because gcc runs out of virtual registers otherwise
+       int i, j, overbright;
+       float scaler;
+       buf_mesh_t *mesh;
 
-       buffer = Mem_Alloc(tempmempool, width*height*3);
-       glReadPixels (x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer);
-       CHECKGLERROR
+       if (!backendactive)
+               Sys_Error("R_Mesh_Draw: called when backend is not active\n");
 
-       // 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;
+       if (!m->numtriangles
+        || !m->numverts)
+               Host_Error("R_Mesh_Draw: no triangles or verts\n");
 
-       Image_WriteTGARGB_preflipped(filename, width, height, buffer);
+       if (m->transparent)
+       {
+               if (currenttransmesh >= max_meshs || (currenttranstriangle + m->numtriangles) > max_meshs || (currenttransvertex + m->numverts) > max_verts)
+               {
+                       if (!transranout)
+                       {
+                               Con_Printf("R_Mesh_Draw: ran out of room for transparent meshs\n");
+                               transranout = true;
+                       }
+                       return false;
+               }
 
-       Mem_Free(buffer);
-}
+               c_transmeshs++;
+               c_transtris += m->numtriangles;
+               m->index = &buf_transtri[currenttranstriangle].index[0];
+               m->vertex = &buf_transvertex[currenttransvertex].v[0];
+               m->color = &buf_transfcolor[currenttransvertex].c[0];
+               for (i = 0;i < backendunits;i++)
+                       m->texcoords[i] = &buf_transtexcoord[i][currenttransvertex].t[0];
 
-/*
-==================
-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++)
-       {
-               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
+               // transmesh is only for storage of transparent meshs until they
+               // are inserted into the main mesh array
+               mesh = &buf_transmesh[currenttransmesh++];
+               mesh->firsttriangle = currenttranstriangle;
+               mesh->firstvert = currenttransvertex;
+               currenttranstriangle += m->numtriangles;
+               currenttransvertex += m->numverts;
        }
-       if (i==10000)
+       else
        {
-               Con_Printf ("SCR_ScreenShot_f: Couldn't create a TGA file\n");
-               return;
-       }
+               if (m->numtriangles > max_meshs || m->numverts > max_verts)
+               {
+                       Con_Printf("R_Mesh_Draw_GetBuffer: mesh too big for buffers\n");
+                       return false;
+               }
 
-       SCR_ScreenShot(filename, vid.realx, vid.realy, vid.realwidth, vid.realheight);
-       Con_Printf ("Wrote %s\n", filename);
-}
+               if (currentmesh >= max_meshs || (currenttriangle + m->numtriangles) > max_batch || (currentvertex + m->numverts) > max_verts)
+                       R_Mesh_Render();
 
-/*
-===============
-R_Envmap_f
+               c_meshs++;
+               c_meshtris += m->numtriangles;
+               m->index = &buf_tri[currenttriangle].index[0];
+               m->vertex = &buf_vertex[currentvertex].v[0];
+               m->color = &buf_fcolor[currentvertex].c[0];
+               for (i = 0;i < backendunits;i++)
+                       m->texcoords[i] = &buf_texcoord[i][currentvertex].t[0];
 
-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];
+               // opaque meshs are rendered directly
+               mesh = &buf_mesh[currentmesh++];
+               mesh->firsttriangle = currenttriangle;
+               mesh->firstvert = currentvertex;
+               currenttriangle += m->numtriangles;
+               currentvertex += m->numverts;
+       }
 
-       if (Cmd_Argc() != 3)
+       // code shared for transparent and opaque meshs
+       mesh->blendfunc1 = m->blendfunc1;
+       mesh->blendfunc2 = m->blendfunc2;
+       mesh->depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite);
+       mesh->depthtest = !m->depthdisable;
+       mesh->triangles = m->numtriangles;
+       mesh->verts = m->numverts;
+
+       overbright = false;
+       scaler = 1;
+       if (m->blendfunc2 == GL_SRC_COLOR)
        {
-               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;
+               if (m->blendfunc1 == GL_DST_COLOR) // 2x modulate with framebuffer
+                       scaler *= 0.5f;
        }
+       else
+       {
+               if (m->tex[0])
+               {
+                       overbright = gl_combine.integer;
+                       if (overbright)
+                               scaler *= 0.25f;
+               }
+               scaler *= overbrightscale;
+       }
+       m->colorscale = scaler;
 
-       if (!r_render.integer)
-               return;
+       j = -1;
+       for (i = 0;i < MAX_TEXTUREUNITS;i++)
+       {
+               if ((mesh->textures[i] = m->tex[i]))
+               {
+                       j = i;
+                       if (i >= backendunits)
+                               Sys_Error("R_Mesh_Draw_GetBuffer: texture %i supplied when there are only %i texture units\n", j + 1, backendunits);
+               }
+               mesh->texturergbscale[i] = m->texrgbscale[i];
+               if (mesh->texturergbscale[i] != 1 && mesh->texturergbscale[i] != 2 && mesh->texturergbscale[i] != 4)
+                       mesh->texturergbscale[i] = 1;
+       }
+       if (overbright && j >= 0)
+               mesh->texturergbscale[j] = 4;
+
+       return true;
+}
 
-       strcpy(basename, Cmd_Argv(1));
-       size = atoi(Cmd_Argv(2));
-       if (size != 128 && size != 256 && size != 512 && size != 1024)
+void R_Mesh_DrawPolygon(rmeshinfo_t *m, int numverts)
+{
+       m->index = polyindexarray;
+       m->numverts = numverts;
+       m->numtriangles = numverts - 2;
+       if (m->numtriangles < 1)
        {
-               Con_Printf("envmap: size must be one of 128, 256, 512, or 1024\n");
+               Con_Printf("R_Mesh_DrawPolygon: invalid vertex count\n");
                return;
        }
-       if (size > vid.realwidth || size > vid.realheight)
+       if (m->numtriangles >= 256)
        {
-               Con_Printf("envmap: your resolution is not big enough to render that size\n");
+               Con_Printf("R_Mesh_DrawPolygon: only up to 256 triangles (258 verts) supported\n");
                return;
        }
+       R_Mesh_Draw(m);
+}
 
-       envmap = true;
+/*
+==============================================================================
 
-       r_refdef.x = 0;
-       r_refdef.y = 0;
-       r_refdef.width = size;
-       r_refdef.height = size;
+                                               SCREEN SHOTS
 
-       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);
-       }
+qboolean SCR_ScreenShot(char *filename, int x, int y, int width, int height)
+{
+       qboolean ret;
+       int i;
+       qbyte *buffer;
+
+       if (!r_render.integer)
+               return false;
 
-       envmap = false;
+       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;
+
+       ret = Image_WriteTGARGB_preflipped(filename, width, height, buffer);
+
+       Mem_Free(buffer);
+       return ret;
 }
 
 //=============================================================================
@@ -1773,15 +1456,19 @@ 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
+               // clear to black
+               glClearColor(0,0,0,0);CHECKGLERROR
+               // clear the screen
+               glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);CHECKGLERROR
+               // set dithering mode
                if (gl_dither.integer)
-                       glEnable(GL_DITHER);
+               {
+                       glEnable(GL_DITHER);CHECKGLERROR
+               }
                else
-                       glDisable(GL_DITHER);
-               CHECKGLERROR
+               {
+                       glDisable(GL_DITHER);CHECKGLERROR
+               }
        }
 }
 
@@ -1798,9 +1485,6 @@ void SCR_UpdateScreen (void)
        //Mem_CheckSentinelsGlobal();
        //R_TimeReport("memtest");
 
-       glFinish ();
-       CHECKGLERROR
-
        VID_Finish ();
 
        R_TimeReport("finish");
@@ -1826,4 +1510,8 @@ void SCR_UpdateScreen (void)
 
        // draw 2D stuff
        R_DrawQueue();
+
+       // tell driver to commit it's partially full geometry queue to the rendering queue
+       // (this doesn't wait for the commands themselves to complete)
+       glFlush();
 }