]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_backend.c
redesigned drawing of loading plaque, it's now a separate refresh function which...
[xonotic/darkplaces.git] / gl_backend.c
index 9c18e68b00d10aa100e8e4c9ef91cb480a9aac24..3d3567373bec8e0a663524ee940d3a5da9e96345 100644 (file)
@@ -7,11 +7,17 @@
 cvar_t gl_mesh_drawrangeelements = {0, "gl_mesh_drawrangeelements", "1"};
 cvar_t gl_mesh_testarrayelement = {0, "gl_mesh_testarrayelement", "0"};
 cvar_t gl_mesh_testmanualfeeding = {0, "gl_mesh_testmanualfeeding", "0"};
-cvar_t gl_delayfinish = {CVAR_SAVE, "gl_delayfinish", "0"};
 cvar_t gl_paranoid = {0, "gl_paranoid", "0"};
 cvar_t gl_printcheckerror = {0, "gl_printcheckerror", "0"};
+cvar_t r_stereo_separation = {0, "r_stereo_separation", "4"};
+cvar_t r_stereo_sidebyside = {0, "r_stereo_sidebyside", "0"};
+cvar_t r_stereo_redblue = {0, "r_stereo_redblue", "0"};
+cvar_t r_stereo_redcyan = {0, "r_stereo_redcyan", "0"};
+cvar_t r_stereo_redgreen = {0, "r_stereo_redgreen", "0"};
 
 cvar_t r_render = {0, "r_render", "1"};
+cvar_t r_waterwarp = {CVAR_SAVE, "r_waterwarp", "1"};
+cvar_t gl_polyblend = {CVAR_SAVE, "gl_polyblend", "1"};
 cvar_t gl_dither = {CVAR_SAVE, "gl_dither", "1"}; // whether or not to use dithering
 cvar_t gl_lockarrays = {0, "gl_lockarrays", "1"};
 
@@ -143,7 +149,7 @@ static void R_Mesh_CacheArray_Shutdown(void);
 void GL_Backend_AllocArrays(void)
 {
        if (!gl_backend_mempool)
-               gl_backend_mempool = Mem_AllocPool("GL_Backend");
+               gl_backend_mempool = Mem_AllocPool("GL_Backend", 0, NULL);
        R_Mesh_CacheArray_Startup();
 }
 
@@ -204,9 +210,15 @@ void gl_backend_init(void)
        }
 
        Cvar_RegisterVariable(&r_render);
+       Cvar_RegisterVariable(&r_waterwarp);
+       Cvar_RegisterVariable(&r_stereo_separation);
+       Cvar_RegisterVariable(&r_stereo_sidebyside);
+       Cvar_RegisterVariable(&r_stereo_redblue);
+       Cvar_RegisterVariable(&r_stereo_redcyan);
+       Cvar_RegisterVariable(&r_stereo_redgreen);
+       Cvar_RegisterVariable(&gl_polyblend);
        Cvar_RegisterVariable(&gl_dither);
        Cvar_RegisterVariable(&gl_lockarrays);
-       Cvar_RegisterVariable(&gl_delayfinish);
        Cvar_RegisterVariable(&gl_paranoid);
        Cvar_RegisterVariable(&gl_printcheckerror);
 #ifdef NORENDER
@@ -248,6 +260,7 @@ void GL_SetupView_Orientation_FromEntity(matrix4x4_t *matrix)
 void GL_SetupView_Mode_Perspective (double fovx, double fovy, double zNear, double zFar)
 {
        double xmax, ymax;
+       double m[16];
 
        if (!r_render.integer)
                return;
@@ -260,13 +273,30 @@ void GL_SetupView_Mode_Perspective (double fovx, double fovy, double zNear, doub
        ymax = zNear * tan(fovy * M_PI / 360.0);
        // set view pyramid
        qglFrustum(-xmax, xmax, -ymax, ymax, zNear, zFar);CHECKGLERROR
+       qglGetDoublev(GL_PROJECTION_MATRIX, m);
+       backend_projectmatrix.m[0][0] = m[0];
+       backend_projectmatrix.m[1][0] = m[1];
+       backend_projectmatrix.m[2][0] = m[2];
+       backend_projectmatrix.m[3][0] = m[3];
+       backend_projectmatrix.m[0][1] = m[4];
+       backend_projectmatrix.m[1][1] = m[5];
+       backend_projectmatrix.m[2][1] = m[6];
+       backend_projectmatrix.m[3][1] = m[7];
+       backend_projectmatrix.m[0][2] = m[8];
+       backend_projectmatrix.m[1][2] = m[9];
+       backend_projectmatrix.m[2][2] = m[10];
+       backend_projectmatrix.m[3][2] = m[11];
+       backend_projectmatrix.m[0][3] = m[12];
+       backend_projectmatrix.m[1][3] = m[13];
+       backend_projectmatrix.m[2][3] = m[14];
+       backend_projectmatrix.m[3][3] = m[15];
        qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
        GL_SetupView_Orientation_Identity();
 }
 
 void GL_SetupView_Mode_PerspectiveInfiniteFarClip (double fovx, double fovy, double zNear)
 {
-       float nudge, m[16];
+       double nudge, m[16];
 
        if (!r_render.integer)
                return;
@@ -292,7 +322,7 @@ void GL_SetupView_Mode_PerspectiveInfiniteFarClip (double fovx, double fovy, dou
        m[13] = 0;
        m[14] = -2 * zNear * nudge;
        m[15] = 0;
-       qglLoadMatrixf(m);
+       qglLoadMatrixd(m);
        qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
        GL_SetupView_Orientation_Identity();
        backend_projectmatrix.m[0][0] = m[0];
@@ -315,6 +345,8 @@ void GL_SetupView_Mode_PerspectiveInfiniteFarClip (double fovx, double fovy, dou
 
 void GL_SetupView_Mode_Ortho (double x1, double y1, double x2, double y2, double zNear, double zFar)
 {
+       double m[16];
+
        if (!r_render.integer)
                return;
 
@@ -322,6 +354,23 @@ void GL_SetupView_Mode_Ortho (double x1, double y1, double x2, double y2, double
        qglMatrixMode(GL_PROJECTION);CHECKGLERROR
        qglLoadIdentity();CHECKGLERROR
        qglOrtho(x1, x2, y2, y1, zNear, zFar);
+       qglGetDoublev(GL_PROJECTION_MATRIX, m);
+       backend_projectmatrix.m[0][0] = m[0];
+       backend_projectmatrix.m[1][0] = m[1];
+       backend_projectmatrix.m[2][0] = m[2];
+       backend_projectmatrix.m[3][0] = m[3];
+       backend_projectmatrix.m[0][1] = m[4];
+       backend_projectmatrix.m[1][1] = m[5];
+       backend_projectmatrix.m[2][1] = m[6];
+       backend_projectmatrix.m[3][1] = m[7];
+       backend_projectmatrix.m[0][2] = m[8];
+       backend_projectmatrix.m[1][2] = m[9];
+       backend_projectmatrix.m[2][2] = m[10];
+       backend_projectmatrix.m[3][2] = m[11];
+       backend_projectmatrix.m[0][3] = m[12];
+       backend_projectmatrix.m[1][3] = m[13];
+       backend_projectmatrix.m[2][3] = m[14];
+       backend_projectmatrix.m[3][3] = m[15];
        qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
        GL_SetupView_Orientation_Identity();
 }
@@ -335,6 +384,8 @@ typedef struct gltextureunit_s
        float rgbscale, alphascale;
        int combinergb, combinealpha;
        // FIXME: add more combine stuff
+       // texmatrixenabled exists only to avoid unnecessary texmatrix compares
+       int texmatrixenabled;
        matrix4x4_t matrix;
 }
 gltextureunit_t;
@@ -382,7 +433,11 @@ void GL_SetupTextureState(void)
                unit->alphascale = 1;
                unit->combinergb = GL_MODULATE;
                unit->combinealpha = GL_MODULATE;
+               unit->texmatrixenabled = false;
                unit->matrix = r_identitymatrix;
+               qglMatrixMode(GL_TEXTURE);
+               qglLoadIdentity();
+               qglMatrixMode(GL_MODELVIEW);
 
                qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
                qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
@@ -649,7 +704,7 @@ void GL_TransformToScreen(const vec4_t in, vec4_t out)
        iw = 1.0f / out[3];
        out[0] = r_view_x + (out[0] * iw + 1.0f) * r_view_width * 0.5f;
        out[1] = r_view_y + (out[1] * iw + 1.0f) * r_view_height * 0.5f;
-       out[2] = out[2] * iw;
+       out[2] = r_view_z + (out[2] * iw + 1.0f) * r_view_depth * 0.5f;
 }
 
 // called at beginning of frame
@@ -861,11 +916,9 @@ void R_Mesh_Matrix(const matrix4x4_t *matrix)
 
 void R_Mesh_State(const rmeshstate_t *m)
 {
-       int i, combinergb, combinealpha, scale, arrayis3d;
+       int i, combinergb, combinealpha, scale;
        gltextureunit_t *unit;
-       const matrix4x4_t *texmatrix;
        matrix4x4_t tempmatrix;
-       const float *texcoords;
 
        BACKENDACTIVECHECK
 
@@ -909,158 +962,217 @@ void R_Mesh_State(const rmeshstate_t *m)
 
        for (i = 0, unit = gl_state.units;i < backendunits;i++, unit++)
        {
+               // update 1d texture binding
                if (unit->t1d != m->tex1d[i])
                {
-                       GL_ActiveTexture(i);
                        if (m->tex1d[i])
                        {
                                if (unit->t1d == 0)
+                               {
+                                       GL_ActiveTexture(i);
                                        qglEnable(GL_TEXTURE_1D);CHECKGLERROR
+                               }
+                               unit->t1d = m->tex1d[i];
+                               GL_ActiveTexture(i);
+                               qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
                        }
                        else
                        {
                                if (unit->t1d)
+                               {
+                                       unit->t1d = 0;
+                                       GL_ActiveTexture(i);
                                        qglDisable(GL_TEXTURE_1D);CHECKGLERROR
+                               }
                        }
-                       qglBindTexture(GL_TEXTURE_1D, (unit->t1d = m->tex1d[i]));CHECKGLERROR
                }
+               // update 2d texture binding
                if (unit->t2d != m->tex[i])
                {
-                       GL_ActiveTexture(i);
                        if (m->tex[i])
                        {
                                if (unit->t2d == 0)
+                               {
+                                       GL_ActiveTexture(i);
                                        qglEnable(GL_TEXTURE_2D);CHECKGLERROR
+                               }
+                               unit->t2d = m->tex[i];
+                               GL_ActiveTexture(i);
+                               qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
                        }
                        else
                        {
                                if (unit->t2d)
+                               {
+                                       unit->t2d = 0;
+                                       GL_ActiveTexture(i);
                                        qglDisable(GL_TEXTURE_2D);CHECKGLERROR
+                               }
                        }
-                       qglBindTexture(GL_TEXTURE_2D, (unit->t2d = m->tex[i]));CHECKGLERROR
                }
+               // update 3d texture binding
                if (unit->t3d != m->tex3d[i])
                {
-                       GL_ActiveTexture(i);
                        if (m->tex3d[i])
                        {
                                if (unit->t3d == 0)
+                               {
+                                       GL_ActiveTexture(i);
                                        qglEnable(GL_TEXTURE_3D);CHECKGLERROR
+                               }
+                               unit->t3d = m->tex3d[i];
+                               GL_ActiveTexture(i);
+                               qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
                        }
                        else
                        {
                                if (unit->t3d)
+                               {
+                                       unit->t3d = 0;
+                                       GL_ActiveTexture(i);
                                        qglDisable(GL_TEXTURE_3D);CHECKGLERROR
+                               }
                        }
-                       qglBindTexture(GL_TEXTURE_3D, (unit->t3d = m->tex3d[i]));CHECKGLERROR
                }
+               // update cubemap texture binding
                if (unit->tcubemap != m->texcubemap[i])
                {
-                       GL_ActiveTexture(i);
                        if (m->texcubemap[i])
                        {
                                if (unit->tcubemap == 0)
+                               {
+                                       GL_ActiveTexture(i);
                                        qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
+                               }
+                               unit->tcubemap = m->texcubemap[i];
+                               GL_ActiveTexture(i);
+                               qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
                        }
                        else
                        {
                                if (unit->tcubemap)
+                               {
+                                       unit->tcubemap = 0;
+                                       GL_ActiveTexture(i);
                                        qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
+                               }
                        }
-                       qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, (unit->tcubemap = m->texcubemap[i]));CHECKGLERROR
                }
-               combinergb = m->texcombinergb[i];
-               if (!combinergb)
-                       combinergb = GL_MODULATE;
-               if (unit->combinergb != combinergb)
+               // update texture unit settings if the unit is enabled
+               if (unit->t1d || unit->t2d || unit->t3d || unit->tcubemap)
                {
-                       GL_ActiveTexture(i);
-                       unit->combinergb = combinergb;
-                       if (gl_combine.integer) 
+                       // texture unit is enabled, enable the array
+                       if (!unit->arrayenabled)
                        {
-                               qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
+                               unit->arrayenabled = true;
+                               GL_ClientActiveTexture(i);
+                               qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
                        }
-                       else
+                       // update combine settings
+                       if (gl_combine.integer)
                        {
-                               qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combinergb);CHECKGLERROR
+                               // GL_ARB_texture_env_combine
+                               combinergb = m->texcombinergb[i] ? m->texcombinergb[i] : GL_MODULATE;
+                               if (unit->combinergb != combinergb)
+                               {
+                                       unit->combinergb = combinergb;
+                                       GL_ActiveTexture(i);
+                                       qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
+                               }
+                               combinealpha = m->texcombinealpha[i] ? m->texcombinealpha[i] : GL_MODULATE;
+                               if (unit->combinealpha != combinealpha)
+                               {
+                                       unit->combinealpha = combinealpha;
+                                       GL_ActiveTexture(i);
+                                       qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
+                               }
+                               scale = max(m->texrgbscale[i], 1);
+                               if (unit->rgbscale != scale)
+                               {
+                                       GL_ActiveTexture(i);
+                                       qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (unit->rgbscale = scale));CHECKGLERROR
+                               }
+                               scale = max(m->texalphascale[i], 1);
+                               if (unit->alphascale != scale)
+                               {
+                                       GL_ActiveTexture(i);
+                                       qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, (unit->alphascale = scale));CHECKGLERROR
+                               }
                        }
-               }
-               combinealpha = m->texcombinealpha[i];
-               if (!combinealpha)
-                       combinealpha = GL_MODULATE;
-               if (unit->combinealpha != combinealpha)
-               {
-                       GL_ActiveTexture(i);
-                       unit->combinealpha = combinealpha;
-                       if (gl_combine.integer)
+                       else
                        {
-                               qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
+                               // normal GL texenv
+                               combinergb = m->texcombinergb[i] ? m->texcombinergb[i] : GL_MODULATE;
+                               if (unit->combinergb != combinergb)
+                               {
+                                       unit->combinergb = combinergb;
+                                       GL_ActiveTexture(i);
+                                       qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combinergb);CHECKGLERROR
+                               }
                        }
-               }
-               scale = max(m->texrgbscale[i], 1);
-               if (unit->rgbscale != scale)
-               {
-                       GL_ActiveTexture(i);
-                       qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (unit->rgbscale = scale));CHECKGLERROR
-               }
-               scale = max(m->texalphascale[i], 1);
-               if (unit->alphascale != scale)
-               {
-                       GL_ActiveTexture(i);
-                       qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, (unit->alphascale = scale));CHECKGLERROR
-               }
-               if (m->pointer_texcoord3f[i])
-               {
-                       arrayis3d = true;
-                       texcoords = m->pointer_texcoord3f[i];
-               }
-               else
-               {
-                       arrayis3d = false;
-                       texcoords = m->pointer_texcoord[i];
-               }
-               if (texcoords && !unit->t1d && !unit->t2d && !unit->t3d && !unit->tcubemap)
-                       texcoords = NULL;
-               if (unit->pointer_texcoord != texcoords || unit->arrayis3d != arrayis3d)
-               {
-                       GL_ClientActiveTexture(i);
-                       if (texcoords)
+                       // update array settings
+                       if (m->pointer_texcoord3f[i])
                        {
-                               if (!unit->arrayenabled)
+                               // 3d texcoord array
+                               if (unit->pointer_texcoord != m->pointer_texcoord3f[i] || !unit->arrayis3d)
                                {
-                                       unit->arrayenabled = true;
-                                       qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
+                                       unit->pointer_texcoord = m->pointer_texcoord3f[i];
+                                       unit->arrayis3d = true;
+                                       GL_ClientActiveTexture(i);
+                                       qglTexCoordPointer(3, GL_FLOAT, sizeof(float[3]), unit->pointer_texcoord);
+                                       CHECKGLERROR
                                }
                        }
                        else
                        {
-                               if (unit->arrayenabled)
+                               // 2d texcoord array
+                               if (unit->pointer_texcoord != m->pointer_texcoord[i] || unit->arrayis3d)
+                               {
+                                       unit->pointer_texcoord = m->pointer_texcoord[i];
+                                       unit->arrayis3d = false;
+                                       GL_ClientActiveTexture(i);
+                                       qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), unit->pointer_texcoord);
+                                       CHECKGLERROR
+                               }
+                       }
+                       // update texmatrix
+                       if (m->texmatrix[i].m[3][3])
+                       {
+                               // texmatrix specified, check if it is different
+                               if (!unit->texmatrixenabled || memcmp(&unit->matrix, &m->texmatrix[i], sizeof(matrix4x4_t)))
                                {
-                                       unit->arrayenabled = false;
-                                       qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
+                                       unit->texmatrixenabled = true;
+                                       unit->matrix = m->texmatrix[i];
+                                       Matrix4x4_Transpose(&tempmatrix, &unit->matrix);
+                                       qglMatrixMode(GL_TEXTURE);
+                                       GL_ActiveTexture(i);
+                                       qglLoadMatrixf(&tempmatrix.m[0][0]);
+                                       qglMatrixMode(GL_MODELVIEW);
                                }
                        }
-                       unit->pointer_texcoord = texcoords;
-                       unit->arrayis3d = arrayis3d;
-                       if (unit->arrayis3d)
-                               qglTexCoordPointer(3, GL_FLOAT, sizeof(float[3]), unit->pointer_texcoord);
                        else
-                               qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), unit->pointer_texcoord);
-                       CHECKGLERROR
+                       {
+                               // no texmatrix specified, revert to identity
+                               if (unit->texmatrixenabled)
+                               {
+                                       unit->texmatrixenabled = false;
+                                       qglMatrixMode(GL_TEXTURE);
+                                       GL_ActiveTexture(i);
+                                       qglLoadIdentity();
+                                       qglMatrixMode(GL_MODELVIEW);
+                               }
+                       }
                }
-               // if texmatrix is not set (3,3 should always be 1 in a texture matrix) just compare to the identity matrix
-               if (m->texmatrix[i].m[3][3])
-                       texmatrix = &m->texmatrix[i];
                else
-                       texmatrix = &r_identitymatrix;
-               if (memcmp(&unit->matrix, texmatrix, sizeof(matrix4x4_t)))
                {
-                       unit->matrix = *texmatrix;
-                       Matrix4x4_Transpose(&tempmatrix, &unit->matrix);
-                       qglMatrixMode(GL_TEXTURE);
-                       GL_ActiveTexture(i);
-                       qglLoadMatrixf(&tempmatrix.m[0][0]);
-                       qglMatrixMode(GL_MODELVIEW);
+                       // texture unit is disabled, disable the array
+                       if (unit->arrayenabled)
+                       {
+                               unit->arrayenabled = false;
+                               GL_ClientActiveTexture(i);
+                               qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
+                       }
+                       // no need to update most settings on a disabled texture unit
                }
        }
 }
@@ -1086,24 +1198,24 @@ void R_Mesh_Draw_ShowTris(int numverts, int numtriangles, const int *elements)
 ==============================================================================
 */
 
-qboolean SCR_ScreenShot(char *filename, int x, int y, int width, int height, qboolean jpeg)
+qboolean SCR_ScreenShot(char *filename, qbyte *buffer1, qbyte *buffer2, qbyte *buffer3, int x, int y, int width, int height, qboolean flipx, qboolean flipy, qboolean flipdiagonal, qboolean jpeg)
 {
+       int     indices[3] = {0,1,2};
        qboolean ret;
-       qbyte *buffer;
 
        if (!r_render.integer)
                return false;
 
-       buffer = Mem_Alloc(tempmempool, width*height*3);
-       qglReadPixels (x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer);
+       qglReadPixels (x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer1);
        CHECKGLERROR
 
+       Image_CopyMux (buffer2, buffer1, width, height, flipx, flipy, flipdiagonal, 3, 3, indices);
+
        if (jpeg)
-               ret = JPEG_SaveImage_preflipped (filename, width, height, buffer);
+               ret = JPEG_SaveImage_preflipped (filename, width, height, buffer2);
        else
-               ret = Image_WriteTGARGB_preflipped (filename, width, height, buffer);
+               ret = Image_WriteTGARGB_preflipped (filename, width, height, buffer2, buffer3);
 
-       Mem_Free(buffer);
        return ret;
 }
 
@@ -1145,7 +1257,145 @@ CalcFov
 float CalcFov (float fov_x, float width, float height)
 {
        // calculate vision size and alter by aspect, then convert back to angle
-       return atan (height / (width / tan(fov_x/360*M_PI))) * 360 / M_PI;
+       return atan (((height/width)/vid_pixelaspect.value)*tan(fov_x/360.0*M_PI))*360.0/M_PI; 
+}
+
+int r_stereo_side;
+
+void SCR_DrawScreen (void)
+{
+       for (r_showtrispass = 0;r_showtrispass <= (r_showtris.value > 0);r_showtrispass++)
+       {
+               R_Mesh_Start();
+
+               R_TimeReport("setup");
+
+               if (r_showtrispass)
+               {
+                       rmeshstate_t m;
+                       r_showtrispass = 0;
+                       GL_BlendFunc(GL_ONE, GL_ONE);
+                       GL_DepthTest(GL_FALSE);
+                       GL_DepthMask(GL_FALSE);
+                       memset(&m, 0, sizeof(m));
+                       R_Mesh_State(&m);
+                       GL_ShowTrisColor(0.2,0.2,0.2,1);
+                       r_showtrispass = 1;
+               }
+
+               if (scr_conlines < vid.conheight && cls.signon == SIGNONS)
+               {
+                       float size;
+
+                       size = scr_viewsize.value * (1.0 / 100.0);
+                       size = min(size, 1);
+
+                       if (r_stereo_sidebyside.integer)
+                       {
+                               r_refdef.width = vid.realwidth * size / 2.5;
+                               r_refdef.height = vid.realheight * size / 2.5 * (1 - bound(0, r_letterbox.value, 100) / 100);
+                               r_refdef.x = (vid.realwidth - r_refdef.width * 2.5) * 0.5;
+                               r_refdef.y = (vid.realheight - r_refdef.height)/2;
+                               if (r_stereo_side)
+                                       r_refdef.x += r_refdef.width * 1.5;
+                       }
+                       else
+                       {
+                               r_refdef.width = vid.realwidth * size;
+                               r_refdef.height = vid.realheight * size * (1 - bound(0, r_letterbox.value, 100) / 100);
+                               r_refdef.x = (vid.realwidth - r_refdef.width)/2;
+                               r_refdef.y = (vid.realheight - r_refdef.height)/2;
+                       }
+
+                       // LordHavoc: viewzoom (zoom in for sniper rifles, etc)
+                       r_refdef.fov_x = scr_fov.value * cl.viewzoom * r_refdef.fovscale_x;
+                       r_refdef.fov_y = CalcFov (r_refdef.fov_x, r_refdef.width, r_refdef.height) * r_refdef.fovscale_y;
+
+                       R_RenderView();
+
+                       if (scr_zoomwindow.integer)
+                       {
+                               float sizex = bound(10, scr_zoomwindow_viewsizex.value, 100) / 100.0;
+                               float sizey = bound(10, scr_zoomwindow_viewsizey.value, 100) / 100.0;
+                               r_refdef.width = vid.realwidth * sizex;
+                               r_refdef.height = vid.realheight * sizey;
+                               r_refdef.x = (vid.realwidth - r_refdef.width)/2;
+                               r_refdef.y = 0;
+                               r_refdef.fov_x = scr_zoomwindow_fov.value * r_refdef.fovscale_x;
+                               r_refdef.fov_y = CalcFov(r_refdef.fov_x, r_refdef.width, r_refdef.height) * r_refdef.fovscale_y;
+
+                               R_RenderView();
+                       }
+               }
+
+               if (!r_stereo_sidebyside.integer)
+               {
+                       r_refdef.width = vid.realwidth;
+                       r_refdef.height = vid.realheight;
+                       r_refdef.x = 0;
+                       r_refdef.y = 0;
+               }
+
+               // draw 2D stuff
+               R_DrawQueue();
+
+               R_Mesh_Finish();
+
+               R_TimeReport("meshfinish");
+       }
+       r_showtrispass = 0;
+}
+
+void SCR_UpdateLoadingScreen (void)
+{
+       float x, y;
+       cachepic_t *pic;
+       rmeshstate_t m;
+       // don't do anything if not initialized yet
+       if (vid_hidden)
+               return;
+       r_showtrispass = 0;
+       VID_GetWindowSize(&vid.realx, &vid.realy, &vid.realwidth, &vid.realheight);
+       VID_UpdateGamma(false);
+       qglViewport(0, 0, vid.realwidth, vid.realheight);
+       //qglDisable(GL_SCISSOR_TEST);
+       //qglDepthMask(1);
+       qglColorMask(1,1,1,1);
+       //qglClearColor(0,0,0,0);
+       //qglClear(GL_COLOR_BUFFER_BIT);
+       //qglCullFace(GL_FRONT);
+       //qglDisable(GL_CULL_FACE);
+       //R_ClearScreen();
+       R_Textures_Frame();
+       GL_SetupView_Mode_Ortho(0, 0, vid_conwidth.integer, vid_conheight.integer, -10, 100);
+       R_Mesh_Start();
+       R_Mesh_Matrix(&r_identitymatrix);
+       // draw the loading plaque
+       pic = Draw_CachePic("gfx/loading.lmp");
+       x = (vid_conwidth.integer - pic->width)/2;
+       y = (vid_conheight.integer - pic->height)/2;
+       GL_Color(1,1,1,1);
+       GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+       GL_DepthTest(false);
+       memset(&m, 0, sizeof(m));
+       m.pointer_vertex = varray_vertex3f;
+       m.pointer_texcoord[0] = varray_texcoord2f[0];
+       m.tex[0] = R_GetTexture(pic->tex);
+       R_Mesh_State(&m);
+       varray_vertex3f[0] = varray_vertex3f[9] = x;
+       varray_vertex3f[1] = varray_vertex3f[4] = y;
+       varray_vertex3f[3] = varray_vertex3f[6] = x + pic->width;
+       varray_vertex3f[7] = varray_vertex3f[10] = y + pic->height;
+       varray_texcoord2f[0][0] = 0;varray_texcoord2f[0][1] = 0;
+       varray_texcoord2f[0][2] = 1;varray_texcoord2f[0][3] = 0;
+       varray_texcoord2f[0][4] = 1;varray_texcoord2f[0][5] = 1;
+       varray_texcoord2f[0][6] = 0;varray_texcoord2f[0][7] = 1;
+       GL_LockArrays(0, 4);
+       R_Mesh_Draw(4, 2, polygonelements);
+       GL_LockArrays(0, 0);
+       R_Mesh_Finish();
+       // refresh
+       VID_Finish();
 }
 
 /*
@@ -1158,15 +1408,8 @@ text to the screen.
 */
 void SCR_UpdateScreen (void)
 {
-       if (gl_delayfinish.integer)
-       {
-               R_Mesh_Finish();
-               R_TimeReport("meshfinish");
-               VID_Finish();
-               R_TimeReport("finish");
-       }
-
-       R_Mesh_Start();
+       if (vid_hidden)
+               return;
 
        if (r_textureunits.integer > gl_textureunits)
                Cvar_SetValueQuick(&r_textureunits, gl_textureunits);
@@ -1176,116 +1419,126 @@ void SCR_UpdateScreen (void)
        if (gl_combine.integer && (!gl_combine_extension || r_textureunits.integer < 2))
                Cvar_SetValueQuick(&gl_combine, 0);
 
-showtris:
-       R_TimeReport("setup");
-
-       R_ClearScreen();
-
-       R_TimeReport("clear");
+       // don't allow cheats in multiplayer
+       if (!cl.islocalgame && cl.worldmodel)
+       {
+               if (r_fullbright.integer != 0)
+                       Cvar_Set ("r_fullbright", "0");
+               if (r_ambient.value != 0)
+                       Cvar_Set ("r_ambient", "0");
+       }
 
-       if (scr_conlines < vid.conheight && cls.signon == SIGNONS)
+       // bound viewsize
+       if (scr_viewsize.value < 30)
+               Cvar_Set ("viewsize","30");
+       if (scr_viewsize.value > 120)
+               Cvar_Set ("viewsize","120");
+
+       // bound field of view
+       if (scr_fov.value < 1)
+               Cvar_Set ("fov","1");
+       if (scr_fov.value > 170)
+               Cvar_Set ("fov","170");
+
+       // intermission is always full screen
+       if (cl.intermission)
+               sb_lines = 0;
+       else
        {
-               float size;
-               int contents;
-
-               // bound viewsize
-               if (scr_viewsize.value < 30)
-                       Cvar_Set ("viewsize","30");
-               if (scr_viewsize.value > 120)
-                       Cvar_Set ("viewsize","120");
-               
-               // bound field of view
-               if (scr_fov.value < 1)
-                       Cvar_Set ("fov","1");
-               if (scr_fov.value > 170)
-                       Cvar_Set ("fov","170");
-       
-               // intermission is always full screen
-               if (cl.intermission)
-               {
-                       size = 1;
-                       sb_lines = 0;
-               }
+               if (scr_viewsize.value >= 120)
+                       sb_lines = 0;           // no status bar at all
+               else if (scr_viewsize.value >= 110)
+                       sb_lines = 24;          // no inventory
                else
+                       sb_lines = 24+16+8;
+       }
+
+       r_refdef.fovscale_x = 1;
+       r_refdef.fovscale_y = 1;
+       if (r_waterwarp.value > 0 && cl.worldmodel)
+       {
+               Mod_CheckLoaded(cl.worldmodel);
+               if (CL_PointSuperContents(r_vieworigin) & SUPERCONTENTS_LIQUIDSMASK)
                {
-                       if (scr_viewsize.value >= 120)
-                               sb_lines = 0;           // no status bar at all
-                       else if (scr_viewsize.value >= 110)
-                               sb_lines = 24;          // no inventory
-                       else
-                               sb_lines = 24+16+8;
-                       size = scr_viewsize.value * (1.0 / 100.0);
-                       size = min(size, 1);
+                       r_refdef.fovscale_x = 1 - (((sin(cl.time * 4.7) + 1) * 0.015) * r_waterwarp.value);
+                       r_refdef.fovscale_y = 1 - (((sin(cl.time * 3.0) + 1) * 0.015) * r_waterwarp.value);
                }
-       
-               r_refdef.width = vid.realwidth * size;
-               r_refdef.height = vid.realheight * size;
-               r_refdef.x = (vid.realwidth - r_refdef.width)/2;
-               r_refdef.y = (vid.realheight - r_refdef.height)/2;
-       
-               // LordHavoc: viewzoom (zoom in for sniper rifles, etc)
-               r_refdef.fov_x = scr_fov.value * cl.viewzoom;
-               r_refdef.fov_y = CalcFov (r_refdef.fov_x, r_refdef.width, r_refdef.height);
-       
-               if (cl.worldmodel)
+       }
+
+       r_refdef.colormask[0] = 1;
+       r_refdef.colormask[1] = 1;
+       r_refdef.colormask[2] = 1;
+
+       CHECKGLERROR
+       qglViewport(0, 0, vid.realwidth, vid.realheight);
+       qglDisable(GL_SCISSOR_TEST);
+       qglDepthMask(1);
+       qglColorMask(1,1,1,1);
+       qglClearColor(0,0,0,0);
+       qglClear(GL_COLOR_BUFFER_BIT);
+       CHECKGLERROR
+
+       R_TimeReport("clear");
+
+       if (r_stereo_redblue.integer || r_stereo_redgreen.integer || r_stereo_redcyan.integer || r_stereo_sidebyside.integer)
+       {
+               matrix4x4_t originalmatrix = r_refdef.viewentitymatrix;
+               r_refdef.viewentitymatrix.m[0][3] = originalmatrix.m[0][3] + r_stereo_separation.value * -0.5f * r_refdef.viewentitymatrix.m[0][1];
+               r_refdef.viewentitymatrix.m[1][3] = originalmatrix.m[1][3] + r_stereo_separation.value * -0.5f * r_refdef.viewentitymatrix.m[1][1];
+               r_refdef.viewentitymatrix.m[2][3] = originalmatrix.m[2][3] + r_stereo_separation.value * -0.5f * r_refdef.viewentitymatrix.m[2][1];
+
+               if (r_stereo_sidebyside.integer)
+                       r_stereo_side = 0;
+
+               if (r_stereo_redblue.integer || r_stereo_redgreen.integer || r_stereo_redcyan.integer)
                {
-                       Mod_CheckLoaded(cl.worldmodel);
-                       contents = CL_PointSuperContents(r_vieworigin);
-                       if (contents & SUPERCONTENTS_LIQUIDSMASK)
-                       {
-                               r_refdef.fov_x *= (sin(cl.time * 4.7) * 0.015 + 0.985);
-                               r_refdef.fov_y *= (sin(cl.time * 3.0) * 0.015 + 0.985);
-                       }
+                       r_refdef.colormask[0] = 1;
+                       r_refdef.colormask[1] = 0;
+                       r_refdef.colormask[2] = 0;
                }
 
-               R_RenderView();
+               SCR_DrawScreen();
 
-               if (scr_zoomwindow.integer)
-               {
-                       float sizex = bound(10, scr_zoomwindow_viewsizex.value, 100) / 100.0;
-                       float sizey = bound(10, scr_zoomwindow_viewsizey.value, 100) / 100.0;
-                       r_refdef.width = vid.realwidth * sizex;
-                       r_refdef.height = vid.realheight * sizey;
-                       r_refdef.x = (vid.realwidth - r_refdef.width)/2;
-                       r_refdef.y = 0;
-                       r_refdef.fov_x = scr_zoomwindow_fov.value;
-                       r_refdef.fov_y = CalcFov(r_refdef.fov_x, r_refdef.width, r_refdef.height);
+               r_refdef.viewentitymatrix.m[0][3] = originalmatrix.m[0][3] + r_stereo_separation.value * 0.5f * r_refdef.viewentitymatrix.m[0][1];
+               r_refdef.viewentitymatrix.m[1][3] = originalmatrix.m[1][3] + r_stereo_separation.value * 0.5f * r_refdef.viewentitymatrix.m[1][1];
+               r_refdef.viewentitymatrix.m[2][3] = originalmatrix.m[2][3] + r_stereo_separation.value * 0.5f * r_refdef.viewentitymatrix.m[2][1];
 
-                       R_RenderView();
-               }
-       }
+               if (r_stereo_sidebyside.integer)
+                       r_stereo_side = 1;
 
-       // draw 2D stuff
-       R_DrawQueue();
+               if (r_stereo_redblue.integer || r_stereo_redgreen.integer || r_stereo_redcyan.integer)
+               {
+                       r_refdef.colormask[0] = 0;
+                       r_refdef.colormask[1] = r_stereo_redcyan.integer || r_stereo_redgreen.integer;
+                       r_refdef.colormask[2] = r_stereo_redcyan.integer || r_stereo_redblue.integer;
+               }
 
-       if (r_showtrispass)
-               r_showtrispass = false;
-       else if (r_showtris.value > 0)
-       {
-               rmeshstate_t m;
-               GL_BlendFunc(GL_ONE, GL_ONE);
-               GL_DepthTest(GL_FALSE);
-               GL_DepthMask(GL_FALSE);
-               memset(&m, 0, sizeof(m));
-               R_Mesh_State(&m);
-               r_showtrispass = true;
-               GL_ShowTrisColor(0.2,0.2,0.2,1);
-               goto showtris;
-       }
+               SCR_DrawScreen();
 
-       if (gl_delayfinish.integer)
-       {
-               // tell driver to commit it's partially full geometry queue to the rendering queue
-               // (this doesn't wait for the commands themselves to complete)
-               qglFlush();
+               r_refdef.viewentitymatrix = originalmatrix;
        }
        else
        {
-               R_Mesh_Finish();
-               R_TimeReport("meshfinish");
-               VID_Finish();
-               R_TimeReport("finish");
+               r_showtrispass = false;
+               SCR_DrawScreen();
+       
+               if (r_showtris.value > 0)
+               {
+                       rmeshstate_t m;
+                       GL_BlendFunc(GL_ONE, GL_ONE);
+                       GL_DepthTest(GL_FALSE);
+                       GL_DepthMask(GL_FALSE);
+                       memset(&m, 0, sizeof(m));
+                       R_Mesh_State(&m);
+                       r_showtrispass = true;
+                       GL_ShowTrisColor(0.2,0.2,0.2,1);
+                       SCR_DrawScreen();
+                       r_showtrispass = false;
+               }
        }
+
+       VID_Finish();
+       R_TimeReport("finish");
 }
 
 
@@ -1293,12 +1546,16 @@ showtris:
 // dynamic vertex array buffer subsystem
 //===========================================================================
 
+// FIXME: someday this should be dynamically allocated and resized?
 float varray_vertex3f[65536*3];
+float varray_svector3f[65536*3];
+float varray_tvector3f[65536*3];
+float varray_normal3f[65536*3];
 float varray_color4f[65536*4];
 float varray_texcoord2f[4][65536*2];
 float varray_texcoord3f[4][65536*3];
-float varray_normal3f[65536*3];
 int earray_element3i[65536];
+float varray_vertex3f2[65536*3];
 
 //===========================================================================
 // vertex array caching subsystem