]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_backend.c
unset command
[xonotic/darkplaces.git] / gl_backend.c
index bfcf1707a1e4b344ab5b75e434bc8d571e85793c..fd0cff613e8df66800017698f81f271da3ed2e52 100644 (file)
@@ -14,8 +14,6 @@ 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"};
 
@@ -282,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);
@@ -312,7 +308,7 @@ void R_Viewport_TransformToScreen(const r_viewport_t *v, const vec4_t in, vec4_t
        out[2] = v->z + (out[2] * iw + 1.0f) * v->depth * 0.5f;
 }
 
-static void R_Viewport_ApplyNearClipPlane(r_viewport_t *v, float normalx, float normaly, float normalz, float dist)
+static void R_Viewport_ApplyNearClipPlaneFloatGL(const r_viewport_t *v, float *m, float normalx, float normaly, float normalz, float dist)
 {
        float q[4];
        float d;
@@ -345,24 +341,25 @@ static void R_Viewport_ApplyNearClipPlane(r_viewport_t *v, float normalx, float
        // 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, 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;
@@ -372,21 +369,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_FromArrayFloatGL(&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
        {
@@ -402,6 +401,7 @@ void R_Viewport_InitOrtho(r_viewport_t *v, const matrix4x4_t *cameramatrix, int
 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)
@@ -415,11 +415,12 @@ 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);
 
@@ -428,16 +429,17 @@ void R_Viewport_InitPerspective(r_viewport_t *v, const matrix4x4_t *cameramatrix
        Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
        Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
 
-       Matrix4x4_FromArrayFloatGL(&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_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 float nudge = 1.0 - 1.0 / (1<<23);
+       float m[16];
        memset(v, 0, sizeof(*v));
 
        if(v_flipped.integer)
@@ -451,23 +453,24 @@ 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;
-       v->screentodepth[0] = (v->m[10] + 1) * 0.5 - 1;
-       v->screentodepth[1] = v->m[14] * -0.5;
+       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_FromArrayFloatGL(&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);
 }
 
 float cubeviewmatrix[6][16] =
@@ -554,29 +557,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_FromArrayFloatGL(&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;
@@ -585,22 +592,25 @@ 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_FromArrayFloatGL(&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 m[16];
        gl_viewport = *v;
 
        CHECKGLERROR
@@ -622,7 +632,8 @@ void R_SetViewport(const r_viewport_t *v)
        case RENDERPATH_GL11:
                // Load the projection matrix into OpenGL
                qglMatrixMode(GL_PROJECTION);CHECKGLERROR
-               qglLoadMatrixf(gl_viewport.m);CHECKGLERROR
+               Matrix4x4_ToArrayFloatGL(&gl_projectionmatrix, m);
+               qglLoadMatrixf(m);CHECKGLERROR
                qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
                break;
        }
@@ -715,55 +726,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)
@@ -966,33 +993,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
@@ -1681,8 +1681,6 @@ void R_Mesh_TexBind(unsigned int unitnum, rtexture_t *tex)
        }
 }
 
-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;