]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_backend.c
be more clear in the overflow message of OGG_FetchSound
[xonotic/darkplaces.git] / gl_backend.c
index 529401fbdf95cde3f22b2b77dde62a7b82811805..82b245fa52107b000bfe81c8789c0aef2f22ab30 100644 (file)
@@ -14,14 +14,22 @@ cvar_t r_renderview = {0, "r_renderview", "1", "enables rendering 3D views (you
 cvar_t r_waterwarp = {CVAR_SAVE, "r_waterwarp", "1", "warp view while underwater"};
 cvar_t gl_polyblend = {CVAR_SAVE, "gl_polyblend", "1", "tints view while underwater, hurt, etc"};
 cvar_t gl_dither = {CVAR_SAVE, "gl_dither", "1", "enables OpenGL dithering (16bit looks bad with this off)"};
-cvar_t gl_lockarrays = {0, "gl_lockarrays", "0", "enables use of glLockArraysEXT, may cause glitches with some broken drivers, and may be slower than normal"};
-cvar_t gl_lockarrays_minimumvertices = {0, "gl_lockarrays_minimumvertices", "1", "minimum number of vertices required for use of glLockArraysEXT, setting this too low may reduce performance"};
 cvar_t gl_vbo = {CVAR_SAVE, "gl_vbo", "3", "make use of GL_ARB_vertex_buffer_object extension to store static geometry in video memory for faster rendering, 0 disables VBO allocation or use, 1 enables VBOs for vertex and triangle data, 2 only for vertex data, 3 for vertex data and triangle data of simple meshes (ones with only one surface)"};
 cvar_t gl_fbo = {CVAR_SAVE, "gl_fbo", "1", "make use of GL_ARB_framebuffer_object extension to enable shadowmaps and other features using pixel formats different from the framebuffer"};
 
 cvar_t v_flipped = {0, "v_flipped", "0", "mirror the screen (poor man's left handed mode)"};
 qboolean v_flipped_state = false;
 
+r_viewport_t gl_viewport;
+matrix4x4_t gl_modelmatrix;
+matrix4x4_t gl_viewmatrix;
+matrix4x4_t gl_modelviewmatrix;
+matrix4x4_t gl_projectionmatrix;
+matrix4x4_t gl_modelviewprojectionmatrix;
+float gl_modelview16f[16];
+float gl_modelviewprojection16f[16];
+qboolean gl_modelmatrixchanged;
+
 int gl_maxdrawrangeelementsvertices;
 int gl_maxdrawrangeelementsindices;
 
@@ -141,10 +149,6 @@ typedef struct gl_state_s
 
        memexpandablearray_t bufferobjectinfoarray;
 
-       r_viewport_t viewport;
-       matrix4x4_t modelmatrix;
-       matrix4x4_t modelviewmatrix;
-
        qboolean active;
 }
 gl_state_t;
@@ -276,8 +280,6 @@ void gl_backend_init(void)
        Cvar_RegisterVariable(&gl_polyblend);
        Cvar_RegisterVariable(&v_flipped);
        Cvar_RegisterVariable(&gl_dither);
-       Cvar_RegisterVariable(&gl_lockarrays);
-       Cvar_RegisterVariable(&gl_lockarrays_minimumvertices);
        Cvar_RegisterVariable(&gl_vbo);
        Cvar_RegisterVariable(&gl_paranoid);
        Cvar_RegisterVariable(&gl_printcheckerror);
@@ -289,7 +291,7 @@ void gl_backend_init(void)
 
        Cmd_AddCommand("gl_vbostats", GL_VBOStats_f, "prints a list of all buffer objects (vertex data and triangle elements) and total video memory used by them");
 
-       R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap);
+       R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap, NULL, NULL);
 }
 
 void GL_SetMirrorState(qboolean state);
@@ -302,14 +304,153 @@ void R_Viewport_TransformToScreen(const r_viewport_t *v, const vec4_t in, vec4_t
        Matrix4x4_Transform4 (&v->projectmatrix, temp, out);
        iw = 1.0f / out[3];
        out[0] = v->x + (out[0] * iw + 1.0f) * v->width * 0.5f;
-       out[1] = v->y + v->height - (out[1] * iw + 1.0f) * v->height * 0.5f;
+
+       // for an odd reason, inverting this is wrong for R_Shadow_ScissorForBBox (we then get badly scissored lights)
+       //out[1] = v->y + v->height - (out[1] * iw + 1.0f) * v->height * 0.5f;
+       out[1] = v->y + (out[1] * iw + 1.0f) * v->height * 0.5f;
+
        out[2] = v->z + (out[2] * iw + 1.0f) * v->depth * 0.5f;
 }
 
-static void R_Viewport_ApplyNearClipPlane(r_viewport_t *v, double normalx, double normaly, double normalz, double dist)
+static int bboxedges[12][2] =
+{
+       // top
+       {0, 1}, // +X
+       {0, 2}, // +Y
+       {1, 3}, // Y, +X
+       {2, 3}, // X, +Y
+       // bottom
+       {4, 5}, // +X
+       {4, 6}, // +Y
+       {5, 7}, // Y, +X
+       {6, 7}, // X, +Y
+       // verticals
+       {0, 4}, // +Z
+       {1, 5}, // X, +Z
+       {2, 6}, // Y, +Z
+       {3, 7}, // XY, +Z
+};
+
+qboolean R_ScissorForBBox(const float *mins, const float *maxs, int *scissor)
+{
+       int i, ix1, iy1, ix2, iy2;
+       float x1, y1, x2, y2;
+       vec4_t v, v2;
+       float vertex[20][3];
+       int j, k;
+       vec4_t plane4f;
+       int numvertices;
+       float corner[8][4];
+       float dist[8];
+       int sign[8];
+       float f;
+
+       scissor[0] = r_refdef.view.viewport.x;
+       scissor[1] = r_refdef.view.viewport.y;
+       scissor[2] = r_refdef.view.viewport.width;
+       scissor[3] = r_refdef.view.viewport.height;
+
+       // if view is inside the box, just say yes it's visible
+       if (BoxesOverlap(r_refdef.view.origin, r_refdef.view.origin, mins, maxs))
+               return false;
+
+       x1 = y1 = x2 = y2 = 0;
+
+       // transform all corners that are infront of the nearclip plane
+       VectorNegate(r_refdef.view.frustum[4].normal, plane4f);
+       plane4f[3] = r_refdef.view.frustum[4].dist;
+       numvertices = 0;
+       for (i = 0;i < 8;i++)
+       {
+               Vector4Set(corner[i], (i & 1) ? maxs[0] : mins[0], (i & 2) ? maxs[1] : mins[1], (i & 4) ? maxs[2] : mins[2], 1);
+               dist[i] = DotProduct4(corner[i], plane4f);
+               sign[i] = dist[i] > 0;
+               if (!sign[i])
+               {
+                       VectorCopy(corner[i], vertex[numvertices]);
+                       numvertices++;
+               }
+       }
+       // if some points are behind the nearclip, add clipped edge points to make
+       // sure that the scissor boundary is complete
+       if (numvertices > 0 && numvertices < 8)
+       {
+               // add clipped edge points
+               for (i = 0;i < 12;i++)
+               {
+                       j = bboxedges[i][0];
+                       k = bboxedges[i][1];
+                       if (sign[j] != sign[k])
+                       {
+                               f = dist[j] / (dist[j] - dist[k]);
+                               VectorLerp(corner[j], f, corner[k], vertex[numvertices]);
+                               numvertices++;
+                       }
+               }
+       }
+
+       // if we have no points to check, it is behind the view plane
+       if (!numvertices)
+               return true;
+
+       // if we have some points to transform, check what screen area is covered
+       x1 = y1 = x2 = y2 = 0;
+       v[3] = 1.0f;
+       //Con_Printf("%i vertices to transform...\n", numvertices);
+       for (i = 0;i < numvertices;i++)
+       {
+               VectorCopy(vertex[i], v);
+               R_Viewport_TransformToScreen(&r_refdef.view.viewport, v, v2);
+               //Con_Printf("%.3f %.3f %.3f %.3f transformed to %.3f %.3f %.3f %.3f\n", v[0], v[1], v[2], v[3], v2[0], v2[1], v2[2], v2[3]);
+               if (i)
+               {
+                       if (x1 > v2[0]) x1 = v2[0];
+                       if (x2 < v2[0]) x2 = v2[0];
+                       if (y1 > v2[1]) y1 = v2[1];
+                       if (y2 < v2[1]) y2 = v2[1];
+               }
+               else
+               {
+                       x1 = x2 = v2[0];
+                       y1 = y2 = v2[1];
+               }
+       }
+
+       // now convert the scissor rectangle to integer screen coordinates
+       ix1 = (int)(x1 - 1.0f);
+       //iy1 = vid.height - (int)(y2 - 1.0f);
+       //iy1 = r_refdef.view.viewport.width + 2 * r_refdef.view.viewport.x - (int)(y2 - 1.0f);
+       iy1 = (int)(y1 - 1.0f);
+       ix2 = (int)(x2 + 1.0f);
+       //iy2 = vid.height - (int)(y1 + 1.0f);
+       //iy2 = r_refdef.view.viewport.height + 2 * r_refdef.view.viewport.y - (int)(y1 + 1.0f);
+       iy2 = (int)(y2 + 1.0f);
+       //Con_Printf("%f %f %f %f\n", x1, y1, x2, y2);
+
+       // clamp it to the screen
+       if (ix1 < r_refdef.view.viewport.x) ix1 = r_refdef.view.viewport.x;
+       if (iy1 < r_refdef.view.viewport.y) iy1 = r_refdef.view.viewport.y;
+       if (ix2 > r_refdef.view.viewport.x + r_refdef.view.viewport.width) ix2 = r_refdef.view.viewport.x + r_refdef.view.viewport.width;
+       if (iy2 > r_refdef.view.viewport.y + r_refdef.view.viewport.height) iy2 = r_refdef.view.viewport.y + r_refdef.view.viewport.height;
+
+       // if it is inside out, it's not visible
+       if (ix2 <= ix1 || iy2 <= iy1)
+               return true;
+
+       // the light area is visible, set up the scissor rectangle
+       scissor[0] = ix1;
+       scissor[1] = iy1;
+       scissor[2] = ix2 - ix1;
+       scissor[3] = iy2 - iy1;
+
+       return false;
+}
+
+
+static void R_Viewport_ApplyNearClipPlaneFloatGL(const r_viewport_t *v, float *m, float normalx, float normaly, float normalz, float dist)
 {
-       double q[4];
-       double d;
+       float q[4];
+       float d;
        float clipPlane[4], v3[3], v4[3];
        float normal[3];
 
@@ -327,7 +468,7 @@ static void R_Viewport_ApplyNearClipPlane(r_viewport_t *v, double normalx, doubl
        // testing code for comparing results
        float clipPlane2[4];
        VectorCopy4(clipPlane, clipPlane2);
-       R_Mesh_Matrix(&identitymatrix);
+       R_EntityMatrix(&identitymatrix);
        VectorSet(q, normal[0], normal[1], normal[2], -dist);
        qglClipPlane(GL_CLIP_PLANE0, q);
        qglGetClipPlane(GL_CLIP_PLANE0, q);
@@ -339,24 +480,25 @@ static void R_Viewport_ApplyNearClipPlane(r_viewport_t *v, double normalx, doubl
        // as (sgn(clipPlane.x), sgn(clipPlane.y), 1, 1) and
        // transform it into camera space by multiplying it
        // by the inverse of the projection matrix
-       q[0] = ((clipPlane[0] < 0.0f ? -1.0f : clipPlane[0] > 0.0f ? 1.0f : 0.0f) + v->m[8]) / v->m[0];
-       q[1] = ((clipPlane[1] < 0.0f ? -1.0f : clipPlane[1] > 0.0f ? 1.0f : 0.0f) + v->m[9]) / v->m[5];
+       q[0] = ((clipPlane[0] < 0.0f ? -1.0f : clipPlane[0] > 0.0f ? 1.0f : 0.0f) + m[8]) / m[0];
+       q[1] = ((clipPlane[1] < 0.0f ? -1.0f : clipPlane[1] > 0.0f ? 1.0f : 0.0f) + m[9]) / m[5];
        q[2] = -1.0f;
-       q[3] = (1.0f + v->m[10]) / v->m[14];
+       q[3] = (1.0f + m[10]) / m[14];
 
        // Calculate the scaled plane vector
        d = 2.0f / DotProduct4(clipPlane, q);
 
        // Replace the third row of the projection matrix
-       v->m[2] = clipPlane[0] * d;
-       v->m[6] = clipPlane[1] * d;
-       v->m[10] = clipPlane[2] * d + 1.0f;
-       v->m[14] = clipPlane[3] * d;
+       m[2] = clipPlane[0] * d;
+       m[6] = clipPlane[1] * d;
+       m[10] = clipPlane[2] * d + 1.0f;
+       m[14] = clipPlane[3] * d;
 }
 
-void R_Viewport_InitOrtho(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, double x1, double y1, double x2, double y2, double nearclip, double farclip, const double *nearplane)
+void R_Viewport_InitOrtho(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, float x1, float y1, float x2, float y2, float nearclip, float farclip, const float *nearplane)
 {
        float left = x1, right = x2, bottom = y2, top = y1, zNear = nearclip, zFar = farclip;
+       float m[16];
        memset(v, 0, sizeof(*v));
        v->type = R_VIEWPORTTYPE_ORTHO;
        v->cameramatrix = *cameramatrix;
@@ -366,19 +508,23 @@ void R_Viewport_InitOrtho(r_viewport_t *v, const matrix4x4_t *cameramatrix, int
        v->width = width;
        v->height = height;
        v->depth = 1;
-       v->m[0]  = 2/(right - left);
-       v->m[5]  = 2/(top - bottom);
-       v->m[10] = -2/(zFar - zNear);
-       v->m[12] = - (right + left)/(right - left);
-       v->m[13] = - (top + bottom)/(top - bottom);
-       v->m[14] = - (zFar + zNear)/(zFar - zNear);
-       v->m[15] = 1;
+       memset(m, 0, sizeof(m));
+       m[0]  = 2/(right - left);
+       m[5]  = 2/(top - bottom);
+       m[10] = -2/(zFar - zNear);
+       m[12] = - (right + left)/(right - left);
+       m[13] = - (top + bottom)/(top - bottom);
+       m[14] = - (zFar + zNear)/(zFar - zNear);
+       m[15] = 1;
+       v->screentodepth[0] = -farclip / (farclip - nearclip);
+       v->screentodepth[1] = farclip * nearclip / (farclip - nearclip);
 
        Matrix4x4_Invert_Full(&v->viewmatrix, &v->cameramatrix);
-       Matrix4x4_FromArrayDoubleGL(&v->projectmatrix, v->m);
 
        if (nearplane)
-               R_Viewport_ApplyNearClipPlane(v, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
+               R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
+
+       Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
 
 #if 0
        {
@@ -391,14 +537,12 @@ void R_Viewport_InitOrtho(r_viewport_t *v, const matrix4x4_t *cameramatrix, int
 #endif
 }
 
-void R_Viewport_InitPerspective(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, double frustumx, double frustumy, double nearclip, double farclip, const double *nearplane)
+void R_Viewport_InitPerspective(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, float frustumx, float frustumy, float nearclip, float farclip, const float *nearplane)
 {
        matrix4x4_t tempmatrix, basematrix;
+       float m[16];
        memset(v, 0, sizeof(*v));
 
-       if(v_flipped.integer)
-               frustumx = -frustumx;
-
        v->type = R_VIEWPORTTYPE_PERSPECTIVE;
        v->cameramatrix = *cameramatrix;
        v->x = x;
@@ -407,32 +551,41 @@ void R_Viewport_InitPerspective(r_viewport_t *v, const matrix4x4_t *cameramatrix
        v->width = width;
        v->height = height;
        v->depth = 1;
-       v->m[0]  = 1.0 / frustumx;
-       v->m[5]  = 1.0 / frustumy;
-       v->m[10] = -(farclip + nearclip) / (farclip - nearclip);
-       v->m[11] = -1;
-       v->m[14] = -2 * nearclip * farclip / (farclip - nearclip);
+       memset(m, 0, sizeof(m));
+       m[0]  = 1.0 / frustumx;
+       m[5]  = 1.0 / frustumy;
+       m[10] = -(farclip + nearclip) / (farclip - nearclip);
+       m[11] = -1;
+       m[14] = -2 * nearclip * farclip / (farclip - nearclip);
+       v->screentodepth[0] = -farclip / (farclip - nearclip);
+       v->screentodepth[1] = farclip * nearclip / (farclip - nearclip);
 
        Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
        Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
        Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
        Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
 
-       Matrix4x4_FromArrayDoubleGL(&v->projectmatrix, v->m);
-
        if (nearplane)
-               R_Viewport_ApplyNearClipPlane(v, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
+               R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
+
+       if(v_flipped.integer)
+       {
+               m[0] = -m[0];
+               m[4] = -m[4];
+               m[8] = -m[8];
+               m[12] = -m[12];
+       }
+
+       Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
 }
 
-void R_Viewport_InitPerspectiveInfinite(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, double frustumx, double frustumy, double nearclip, const double *nearplane)
+void R_Viewport_InitPerspectiveInfinite(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, float frustumx, float frustumy, float nearclip, const float *nearplane)
 {
        matrix4x4_t tempmatrix, basematrix;
-       const double nudge = 1.0 - 1.0 / (1<<23);
+       const float nudge = 1.0 - 1.0 / (1<<23);
+       float m[16];
        memset(v, 0, sizeof(*v));
 
-       if(v_flipped.integer)
-               frustumx = -frustumx;
-
        v->type = R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP;
        v->cameramatrix = *cameramatrix;
        v->x = x;
@@ -441,21 +594,32 @@ void R_Viewport_InitPerspectiveInfinite(r_viewport_t *v, const matrix4x4_t *came
        v->width = width;
        v->height = height;
        v->depth = 1;
-       v->m[ 0] = 1.0 / frustumx;
-       v->m[ 5] = 1.0 / frustumy;
-       v->m[10] = -nudge;
-       v->m[11] = -1;
-       v->m[14] = -2 * nearclip * nudge;
+       memset(m, 0, sizeof(m));
+       m[ 0] = 1.0 / frustumx;
+       m[ 5] = 1.0 / frustumy;
+       m[10] = -nudge;
+       m[11] = -1;
+       m[14] = -2 * nearclip * nudge;
+       v->screentodepth[0] = (m[10] + 1) * 0.5 - 1;
+       v->screentodepth[1] = m[14] * -0.5;
 
        Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
        Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
        Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
        Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
 
-       Matrix4x4_FromArrayDoubleGL(&v->projectmatrix, v->m);
-
        if (nearplane)
-               R_Viewport_ApplyNearClipPlane(v, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
+               R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
+
+       if(v_flipped.integer)
+       {
+               m[0] = -m[0];
+               m[4] = -m[4];
+               m[8] = -m[8];
+               m[12] = -m[12];
+       }
+
+       Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
 }
 
 float cubeviewmatrix[6][16] =
@@ -542,29 +706,33 @@ float rectviewmatrix[6][16] =
 void R_Viewport_InitCubeSideView(r_viewport_t *v, const matrix4x4_t *cameramatrix, int side, int size, float nearclip, float farclip, const float *nearplane)
 {
        matrix4x4_t tempmatrix, basematrix;
+       float m[16];
        memset(v, 0, sizeof(*v));
        v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
        v->cameramatrix = *cameramatrix;
        v->width = size;
        v->height = size;
        v->depth = 1;
-       v->m[0] = v->m[5] = 1.0f;
-       v->m[10] = -(farclip + nearclip) / (farclip - nearclip);
-       v->m[11] = -1;
-       v->m[14] = -2 * nearclip * farclip / (farclip - nearclip);
+       memset(m, 0, sizeof(m));
+       m[0] = m[5] = 1.0f;
+       m[10] = -(farclip + nearclip) / (farclip - nearclip);
+       m[11] = -1;
+       m[14] = -2 * nearclip * farclip / (farclip - nearclip);
 
        Matrix4x4_FromArrayFloatGL(&basematrix, cubeviewmatrix[side]);
        Matrix4x4_Invert_Simple(&tempmatrix, &v->cameramatrix);
        Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
-       Matrix4x4_FromArrayDoubleGL(&v->projectmatrix, v->m);
 
        if (nearplane)
-               R_Viewport_ApplyNearClipPlane(v, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
+               R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
+
+       Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
 }
 
 void R_Viewport_InitRectSideView(r_viewport_t *v, const matrix4x4_t *cameramatrix, int side, int size, int border, float nearclip, float farclip, const float *nearplane)
 {
        matrix4x4_t tempmatrix, basematrix;
+       float m[16];
        memset(v, 0, sizeof(*v));
        v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
        v->cameramatrix = *cameramatrix;
@@ -573,45 +741,60 @@ void R_Viewport_InitRectSideView(r_viewport_t *v, const matrix4x4_t *cameramatri
        v->width = size;
        v->height = size;
        v->depth = 1;
-       v->m[0] = v->m[5] = 1.0f * ((float)size - border) / size;
-       v->m[10] = -(farclip + nearclip) / (farclip - nearclip);
-       v->m[11] = -1;
-       v->m[14] = -2 * nearclip * farclip / (farclip - nearclip);
+       memset(m, 0, sizeof(m));
+       m[0] = m[5] = 1.0f * ((float)size - border) / size;
+       m[10] = -(farclip + nearclip) / (farclip - nearclip);
+       m[11] = -1;
+       m[14] = -2 * nearclip * farclip / (farclip - nearclip);
 
        Matrix4x4_FromArrayFloatGL(&basematrix, rectviewmatrix[side]);
        Matrix4x4_Invert_Simple(&tempmatrix, &v->cameramatrix);
        Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
-       Matrix4x4_FromArrayDoubleGL(&v->projectmatrix, v->m);
 
        if (nearplane)
-               R_Viewport_ApplyNearClipPlane(v, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
+               R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
+
+       Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
 }
 
 void R_SetViewport(const r_viewport_t *v)
 {
-       float glmatrix[16];
-       gl_state.viewport = *v;
+       float m[16];
+       gl_viewport = *v;
 
        CHECKGLERROR
        qglViewport(v->x, v->y, v->width, v->height);CHECKGLERROR
 
-       // Load the projection matrix into OpenGL
-       qglMatrixMode(GL_PROJECTION);CHECKGLERROR
-       qglLoadMatrixd(gl_state.viewport.m);CHECKGLERROR
-       qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
-
        // FIXME: v_flipped_state is evil, this probably breaks somewhere
        GL_SetMirrorState(v_flipped.integer && (v->type == R_VIEWPORTTYPE_PERSPECTIVE || v->type == R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP));
 
-       // directly force an update of the modelview matrix
-       Matrix4x4_Concat(&gl_state.modelviewmatrix, &gl_state.viewport.viewmatrix, &gl_state.modelmatrix);
-       Matrix4x4_ToArrayFloatGL(&gl_state.modelviewmatrix, glmatrix);
-       qglLoadMatrixf(glmatrix);CHECKGLERROR
+       // copy over the matrices to our state
+       gl_viewmatrix = v->viewmatrix;
+       gl_projectionmatrix = v->projectmatrix;
+
+       switch(vid.renderpath)
+       {
+       case RENDERPATH_GL20:
+       case RENDERPATH_CGGL:
+//             break;
+       case RENDERPATH_GL13:
+       case RENDERPATH_GL11:
+               // Load the projection matrix into OpenGL
+               qglMatrixMode(GL_PROJECTION);CHECKGLERROR
+               Matrix4x4_ToArrayFloatGL(&gl_projectionmatrix, m);
+               qglLoadMatrixf(m);CHECKGLERROR
+               qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
+               break;
+       }
+
+       // force an update of the derived matrices
+       gl_modelmatrixchanged = true;
+       R_EntityMatrix(&gl_modelmatrix);
 }
 
 void R_GetViewport(r_viewport_t *v)
 {
-       *v = gl_state.viewport;
+       *v = gl_viewport;
 }
 
 static void GL_BindVBO(int bufferobject)
@@ -692,55 +875,71 @@ static void GL_Backend_ResetState(void)
 
        gl_state.unit = MAX_TEXTUREUNITS;
        gl_state.clientunit = MAX_TEXTUREUNITS;
-       for (i = 0;i < vid.teximageunits;i++)
+       switch(vid.renderpath)
        {
-               GL_ActiveTexture(i);
-               qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
-               if (vid.support.ext_texture_3d)
-               {
-                       qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
-               }
-               if (vid.support.arb_texture_cube_map)
-               {
-                       qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
-               }
-               if (vid.support.arb_texture_rectangle)
+       case RENDERPATH_GL20:
+       case RENDERPATH_CGGL:
+               for (i = 0;i < vid.teximageunits;i++)
                {
-                       qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);CHECKGLERROR
+                       GL_ActiveTexture(i);
+                       qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
+                       if (vid.support.ext_texture_3d)
+                       {
+                               qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
+                       }
+                       if (vid.support.arb_texture_cube_map)
+                       {
+                               qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
+                       }
+                       if (vid.support.arb_texture_rectangle)
+                       {
+                               qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);CHECKGLERROR
+                       }
                }
-       }
-
-       for (i = 0;i < vid.texarrayunits;i++)
-       {
-               GL_ClientActiveTexture(i);
-               GL_BindVBO(0);
-               qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
-               qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
-       }
 
-       for (i = 0;i < vid.texunits;i++)
-       {
-               GL_ActiveTexture(i);
-               qglDisable(GL_TEXTURE_2D);CHECKGLERROR
-               if (vid.support.ext_texture_3d)
-               {
-                       qglDisable(GL_TEXTURE_3D);CHECKGLERROR
-               }
-               if (vid.support.arb_texture_cube_map)
+               for (i = 0;i < vid.texarrayunits;i++)
                {
-                       qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
+                       GL_ClientActiveTexture(i);
+                       GL_BindVBO(0);
+                       qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
+                       qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
                }
-               if (vid.support.arb_texture_rectangle)
+               CHECKGLERROR
+               break;
+       case RENDERPATH_GL13:
+       case RENDERPATH_GL11:
+               for (i = 0;i < vid.texunits;i++)
                {
-                       qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
+                       GL_ActiveTexture(i);
+                       GL_ClientActiveTexture(i);
+                       qglDisable(GL_TEXTURE_2D);CHECKGLERROR
+                       qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
+                       if (vid.support.ext_texture_3d)
+                       {
+                               qglDisable(GL_TEXTURE_3D);CHECKGLERROR
+                               qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
+                       }
+                       if (vid.support.arb_texture_cube_map)
+                       {
+                               qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
+                               qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
+                       }
+                       if (vid.support.arb_texture_rectangle)
+                       {
+                               qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
+                               qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);CHECKGLERROR
+                       }
+                       GL_BindVBO(0);
+                       qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
+                       qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
+                       qglMatrixMode(GL_TEXTURE);CHECKGLERROR
+                       qglLoadIdentity();CHECKGLERROR
+                       qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
+                       qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
                }
-               qglMatrixMode(GL_TEXTURE);CHECKGLERROR
-               qglLoadIdentity();CHECKGLERROR
-               qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
-               qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
                CHECKGLERROR
+               break;
        }
-       CHECKGLERROR
 }
 
 void GL_ActiveTexture(unsigned int num)
@@ -943,33 +1142,6 @@ void GL_Color(float cr, float cg, float cb, float ca)
        }
 }
 
-void GL_LockArrays(int first, int count)
-{
-       if (count < gl_lockarrays_minimumvertices.integer)
-       {
-               first = 0;
-               count = 0;
-       }
-       if (gl_state.lockrange_count != count || gl_state.lockrange_first != first)
-       {
-               if (gl_state.lockrange_count)
-               {
-                       gl_state.lockrange_count = 0;
-                       CHECKGLERROR
-                       qglUnlockArraysEXT();
-                       CHECKGLERROR
-               }
-               if (count && vid.support.ext_compiled_vertex_array && gl_lockarrays.integer)
-               {
-                       gl_state.lockrange_first = first;
-                       gl_state.lockrange_count = count;
-                       CHECKGLERROR
-                       qglLockArraysEXT(first, count);
-                       CHECKGLERROR
-               }
-       }
-}
-
 void GL_Scissor (int x, int y, int width, int height)
 {
        CHECKGLERROR
@@ -1020,14 +1192,14 @@ qboolean GL_Backend_CompileShader(int programobject, GLenum shadertypeenum, cons
        qglCompileShaderARB(shaderobject);CHECKGLERROR
        qglGetObjectParameterivARB(shaderobject, GL_OBJECT_COMPILE_STATUS_ARB, &shadercompiled);CHECKGLERROR
        qglGetInfoLogARB(shaderobject, sizeof(compilelog), NULL, compilelog);CHECKGLERROR
-       if (compilelog[0] && developer.integer > 0)
+       if (compilelog[0] && (strstr(compilelog, "error") || strstr(compilelog, "ERROR") || strstr(compilelog, "Error") || strstr(compilelog, "WARNING") || strstr(compilelog, "warning") || strstr(compilelog, "Warning")))
        {
                int i, j, pretextlines = 0;
                for (i = 0;i < numstrings - 1;i++)
                        for (j = 0;strings[i][j];j++)
                                if (strings[i][j] == '\n')
                                        pretextlines++;
-               Con_DPrintf("%s shader compile log:\n%s\n(line offset for any above warnings/errors: %i)\n", shadertype, compilelog, pretextlines);
+               Con_Printf("%s shader compile log:\n%s\n(line offset for any above warnings/errors: %i)\n", shadertype, compilelog, pretextlines);
        }
        if (!shadercompiled)
        {
@@ -1066,7 +1238,8 @@ unsigned int GL_Backend_CompileProgram(int vertexstrings_count, const char **ver
        qglGetInfoLogARB(programobject, sizeof(linklog), NULL, linklog);CHECKGLERROR
        if (linklog[0])
        {
-               Con_DPrintf("program link log:\n%s\n", linklog);
+               if (strstr(linklog, "error") || strstr(linklog, "ERROR") || strstr(linklog, "Error") || strstr(linklog, "WARNING") || strstr(linklog, "warning") || strstr(linklog, "Warning"))
+                       Con_DPrintf("program link log:\n%s\n", linklog);
                // software vertex shader is ok but software fragment shader is WAY
                // too slow, fail program if so.
                // NOTE: this string might be ATI specific, but that's ok because the
@@ -1110,8 +1283,8 @@ void R_Mesh_Draw(int firstvertex, int numvertices, int firsttriangle, int numtri
        unsigned int numelements = numtriangles * 3;
        if (numvertices < 3 || numtriangles < 1)
        {
-               if (numvertices < 0 || numtriangles < 0 || developer.integer >= 100)
-                       Con_Printf("R_Mesh_Draw(%d, %d, %d, %d, %8p, %8p, %i, %i);\n", firstvertex, numvertices, firsttriangle, numtriangles, (void *)element3i, (void *)element3s, bufferobject3i, bufferobject3s);
+               if (numvertices < 0 || numtriangles < 0 || developer_extra.integer)
+                       Con_DPrintf("R_Mesh_Draw(%d, %d, %d, %d, %8p, %8p, %i, %i);\n", firstvertex, numvertices, firsttriangle, numtriangles, (void *)element3i, (void *)element3s, bufferobject3i, bufferobject3s);
                return;
        }
        if (!gl_mesh_prefer_short_elements.integer)
@@ -1428,19 +1601,6 @@ void GL_Mesh_ListVBOs(qboolean printeach)
        Con_Printf("vertex buffers: %i element buffers totalling %i bytes (%.3f MB), %i vertex buffers totalling %i bytes (%.3f MB), combined %i bytes (%.3fMB)\n", (int)ebocount, (int)ebomemory, ebomemory / 1048576.0, (int)vbocount, (int)vbomemory, vbomemory / 1048576.0, (int)(ebomemory + vbomemory), (ebomemory + vbomemory) / 1048576.0);
 }
 
-void R_Mesh_Matrix(const matrix4x4_t *matrix)
-{
-       if (memcmp(matrix, &gl_state.modelmatrix, sizeof(matrix4x4_t)))
-       {
-               float glmatrix[16];
-               gl_state.modelmatrix = *matrix;
-               Matrix4x4_Concat(&gl_state.modelviewmatrix, &gl_state.viewport.viewmatrix, &gl_state.modelmatrix);
-               Matrix4x4_ToArrayFloatGL(&gl_state.modelviewmatrix, glmatrix);
-               CHECKGLERROR
-               qglLoadMatrixf(glmatrix);CHECKGLERROR
-       }
-}
-
 void R_Mesh_VertexPointer(const float *vertex3f, int bufferobject, size_t bufferoffset)
 {
        if (!gl_vbo.integer || gl_mesh_testarrayelement.integer)
@@ -1538,17 +1698,75 @@ void R_Mesh_TexCoordPointer(unsigned int unitnum, unsigned int numcomponents, co
        }
 }
 
-void R_Mesh_TexBindAll(unsigned int unitnum, int tex2d, int tex3d, int texcubemap, int texrectangle)
+int R_Mesh_TexBound(unsigned int unitnum, int id)
 {
        gltextureunit_t *unit = gl_state.units + unitnum;
+       if (unitnum >= vid.teximageunits)
+               return 0;
+       if (id == GL_TEXTURE_2D)
+               return unit->t2d;
+       if (id == GL_TEXTURE_3D)
+               return unit->t3d;
+       if (id == GL_TEXTURE_CUBE_MAP_ARB)
+               return unit->tcubemap;
+       if (id == GL_TEXTURE_RECTANGLE_ARB)
+               return unit->trectangle;
+       return 0;
+}
+
+void R_Mesh_CopyToTexture(rtexture_t *tex, int tx, int ty, int sx, int sy, int width, int height)
+{
+       R_Mesh_TexBind(0, tex);
+       GL_ActiveTexture(0);CHECKGLERROR
+       qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, tx, ty, sx, sy, width, height);CHECKGLERROR
+}
+
+void R_Mesh_TexBind(unsigned int unitnum, rtexture_t *tex)
+{
+       gltextureunit_t *unit = gl_state.units + unitnum;
+       int tex2d, tex3d, texcubemap, texnum;
        if (unitnum >= vid.teximageunits)
                return;
-       // update 2d texture binding
-       if (unit->t2d != tex2d)
+       switch(vid.renderpath)
        {
-               GL_ActiveTexture(unitnum);
-               if (unitnum < vid.texunits)
+       case RENDERPATH_GL20:
+       case RENDERPATH_CGGL:
+               if (!tex)
+                       tex = r_texture_white;
+               texnum = R_GetTexture(tex);
+               switch(tex->gltexturetypeenum)
+               {
+               case GL_TEXTURE_2D: if (unit->t2d != texnum) {GL_ActiveTexture(unitnum);unit->t2d = texnum;qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR}break;
+               case GL_TEXTURE_3D: if (unit->t3d != texnum) {GL_ActiveTexture(unitnum);unit->t3d = texnum;qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR}break;
+               case GL_TEXTURE_CUBE_MAP_ARB: if (unit->tcubemap != texnum) {GL_ActiveTexture(unitnum);unit->tcubemap = texnum;qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR}break;
+               case GL_TEXTURE_RECTANGLE_ARB: if (unit->trectangle != texnum) {GL_ActiveTexture(unitnum);unit->trectangle = texnum;qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR}break;
+               }
+               break;
+       case RENDERPATH_GL13:
+       case RENDERPATH_GL11:
+               tex2d = 0;
+               tex3d = 0;
+               texcubemap = 0;
+               if (tex)
+               {
+                       texnum = R_GetTexture(tex);
+                       switch(tex->gltexturetypeenum)
+                       {
+                       case GL_TEXTURE_2D:
+                               tex2d = texnum;
+                               break;
+                       case GL_TEXTURE_3D:
+                               tex3d = texnum;
+                               break;
+                       case GL_TEXTURE_CUBE_MAP_ARB:
+                               texcubemap = texnum;
+                               break;
+                       }
+               }
+               // update 2d texture binding
+               if (unit->t2d != tex2d)
                {
+                       GL_ActiveTexture(unitnum);
                        if (tex2d)
                        {
                                if (unit->t2d == 0)
@@ -1563,16 +1781,13 @@ void R_Mesh_TexBindAll(unsigned int unitnum, int tex2d, int tex3d, int texcubema
                                        qglDisable(GL_TEXTURE_2D);CHECKGLERROR
                                }
                        }
+                       unit->t2d = tex2d;
+                       qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
                }
-               unit->t2d = tex2d;
-               qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
-       }
-       // update 3d texture binding
-       if (unit->t3d != tex3d)
-       {
-               GL_ActiveTexture(unitnum);
-               if (unitnum < vid.texunits)
+               // update 3d texture binding
+               if (unit->t3d != tex3d)
                {
+                       GL_ActiveTexture(unitnum);
                        if (tex3d)
                        {
                                if (unit->t3d == 0)
@@ -1587,16 +1802,13 @@ void R_Mesh_TexBindAll(unsigned int unitnum, int tex2d, int tex3d, int texcubema
                                        qglDisable(GL_TEXTURE_3D);CHECKGLERROR
                                }
                        }
+                       unit->t3d = tex3d;
+                       qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
                }
-               unit->t3d = tex3d;
-               qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
-       }
-       // update cubemap texture binding
-       if (unit->tcubemap != texcubemap)
-       {
-               GL_ActiveTexture(unitnum);
-               if (unitnum < vid.texunits)
+               // update cubemap texture binding
+               if (unit->tcubemap != texcubemap)
                {
+                       GL_ActiveTexture(unitnum);
                        if (texcubemap)
                        {
                                if (unit->tcubemap == 0)
@@ -1611,115 +1823,17 @@ void R_Mesh_TexBindAll(unsigned int unitnum, int tex2d, int tex3d, int texcubema
                                        qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
                                }
                        }
+                       unit->tcubemap = texcubemap;
+                       qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
                }
-               unit->tcubemap = texcubemap;
-               qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
-       }
-       // update rectangle texture binding
-       if (unit->trectangle != texrectangle)
-       {
-               GL_ActiveTexture(unitnum);
-               if (unitnum < vid.texunits)
-               {
-                       if (texrectangle)
-                       {
-                               if (unit->trectangle == 0)
-                               {
-                                       qglEnable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
-                               }
-                       }
-                       else
-                       {
-                               if (unit->trectangle)
-                               {
-                                       qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
-                               }
-                       }
-               }
-               unit->trectangle = texrectangle;
-               qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
-       }
-}
-
-void R_Mesh_TexBind(unsigned int unitnum, int texnum)
-{
-       gltextureunit_t *unit = gl_state.units + unitnum;
-       if (unitnum >= vid.teximageunits)
-               return;
-       // update 2d texture binding
-       if (unit->t2d != texnum)
-       {
-               GL_ActiveTexture(unitnum);
-               if (unitnum < vid.texunits)
-               {
-                       if (texnum)
-                       {
-                               if (unit->t2d == 0)
-                               {
-                                       qglEnable(GL_TEXTURE_2D);CHECKGLERROR
-                               }
-                       }
-                       else
-                       {
-                               if (unit->t2d)
-                               {
-                                       qglDisable(GL_TEXTURE_2D);CHECKGLERROR
-                               }
-                       }
-               }
-               unit->t2d = texnum;
-               qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
-       }
-       // update 3d texture binding
-       if (unit->t3d)
-       {
-               GL_ActiveTexture(unitnum);
-               if (unitnum < vid.texunits)
-               {
-                       if (unit->t3d)
-                       {
-                               qglDisable(GL_TEXTURE_3D);CHECKGLERROR
-                       }
-               }
-               unit->t3d = 0;
-               qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
-       }
-       // update cubemap texture binding
-       if (unit->tcubemap != 0)
-       {
-               GL_ActiveTexture(unitnum);
-               if (unitnum < vid.texunits)
-               {
-                       if (unit->tcubemap)
-                       {
-                               qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
-                       }
-               }
-               unit->tcubemap = 0;
-               qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
-       }
-       // update rectangle texture binding
-       if (unit->trectangle != 0)
-       {
-               GL_ActiveTexture(unitnum);
-               if (unitnum < vid.texunits)
-               {
-                       if (unit->trectangle)
-                       {
-                               qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
-                       }
-               }
-               unit->trectangle = 0;
-               qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
+               break;
        }
 }
 
-static const float gl_identitymatrix[16] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
-
 void R_Mesh_TexMatrix(unsigned int unitnum, const matrix4x4_t *matrix)
 {
        gltextureunit_t *unit = gl_state.units + unitnum;
-       if (matrix->m[3][3])
+       if (matrix && matrix->m[3][3])
        {
                // texmatrix specified, check if it is different
                if (!unit->texmatrixenabled || memcmp(&unit->matrix, matrix, sizeof(matrix4x4_t)))
@@ -1758,6 +1872,7 @@ void R_Mesh_TexCombine(unsigned int unitnum, int combinergb, int combinealpha, i
        switch(vid.renderpath)
        {
        case RENDERPATH_GL20:
+       case RENDERPATH_CGGL:
                // do nothing
                break;
        case RENDERPATH_GL13:
@@ -1770,7 +1885,7 @@ void R_Mesh_TexCombine(unsigned int unitnum, int combinergb, int combinealpha, i
                        rgbscale = 1;
                if (!alphascale)
                        alphascale = 1;
-               if (combinergb != combinealpha || rgbscale != 1 || alphascale != 1 || combinergb == GL_DOT3_RGBA_ARB || combinergb == GL_DOT3_RGB_ARB)
+               if (combinergb != combinealpha || rgbscale != 1 || alphascale != 1)
                {
                        if (combinergb == GL_DECAL)
                                combinergb = GL_INTERPOLATE_ARB;
@@ -1830,30 +1945,6 @@ void R_Mesh_TexCombine(unsigned int unitnum, int combinergb, int combinealpha, i
        }
 }
 
-void R_Mesh_TextureState(const rmeshstate_t *m)
-{
-       unsigned int i;
-
-       BACKENDACTIVECHECK
-
-       CHECKGLERROR
-       for (i = 0;i < vid.teximageunits;i++)
-               R_Mesh_TexBindAll(i, m->tex[i], m->tex3d[i], m->texcubemap[i], m->texrectangle[i]);
-       for (i = 0;i < vid.texarrayunits;i++)
-       {
-               if (m->pointer_texcoord3f[i])
-                       R_Mesh_TexCoordPointer(i, 3, m->pointer_texcoord3f[i], m->pointer_texcoord_bufferobject[i], m->pointer_texcoord_bufferoffset[i]);
-               else
-                       R_Mesh_TexCoordPointer(i, 2, m->pointer_texcoord[i], m->pointer_texcoord_bufferobject[i], m->pointer_texcoord_bufferoffset[i]);
-       }
-       for (i = 0;i < vid.texunits;i++)
-       {
-               R_Mesh_TexMatrix(i, &m->texmatrix[i]);
-               R_Mesh_TexCombine(i, m->texcombinergb[i], m->texcombinealpha[i], m->texrgbscale[i], m->texalphascale[i]);
-       }
-       CHECKGLERROR
-}
-
 void R_Mesh_ResetTextureState(void)
 {
        unsigned int unitnum;
@@ -1864,6 +1955,7 @@ void R_Mesh_ResetTextureState(void)
        switch(vid.renderpath)
        {
        case RENDERPATH_GL20:
+       case RENDERPATH_CGGL:
                for (unitnum = 0;unitnum < vid.teximageunits;unitnum++)
                {
                        gltextureunit_t *unit = gl_state.units + unitnum;