]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_backend.c
unconst something
[xonotic/darkplaces.git] / gl_backend.c
index 8d3a8c3fdad82b981086048e1844b89e1360de60..8ea6717062e08eeb2c748b291d5a69859f3d7b96 100644 (file)
@@ -165,7 +165,6 @@ memexpandablearray_t gl_bufferobjectinfoarray;
 
 static void gl_backend_start(void)
 {
-       Con_Print("OpenGL Backend starting...\n");
        CHECKGLERROR
 
        if (qglDrawRangeElements != NULL)
@@ -175,7 +174,7 @@ static void gl_backend_start(void)
                CHECKGLERROR
                qglGetIntegerv(GL_MAX_ELEMENTS_INDICES, &gl_maxdrawrangeelementsindices);
                CHECKGLERROR
-               Con_Printf("glDrawRangeElements detected (max vertices %i, max indices %i)\n", gl_maxdrawrangeelementsvertices, gl_maxdrawrangeelementsindices);
+               Con_DPrintf("GL_MAX_ELEMENTS_VERTICES = %i\nGL_MAX_ELEMENTS_INDICES = %i\n", gl_maxdrawrangeelementsvertices, gl_maxdrawrangeelementsindices);
        }
 
        backendunits = bound(1, gl_textureunits, MAX_TEXTUREUNITS);
@@ -188,20 +187,18 @@ static void gl_backend_start(void)
                CHECKGLERROR
                qglGetIntegerv(GL_MAX_TEXTURE_COORDS_ARB, (int *)&backendarrayunits);
                CHECKGLERROR
-               Con_Printf("GLSL shader support detected: texture units = %i texenv, %i image, %i array\n", backendunits, backendimageunits, backendarrayunits);
+               Con_DPrintf("GLSL shader support detected: texture units = %i texenv, %i image, %i array\n", backendunits, backendimageunits, backendarrayunits);
                backendimageunits = bound(1, backendimageunits, MAX_TEXTUREUNITS);
                backendarrayunits = bound(1, backendarrayunits, MAX_TEXTUREUNITS);
        }
-       else if (backendunits > 1)
-               Con_Printf("multitexture detected: texture units = %i\n", backendunits);
        else
-               Con_Printf("singletexture\n");
+               Con_DPrintf("GL_MAX_TEXTUREUNITS = %i\n", backendunits);
 
        GL_Backend_AllocArrays();
 
        Mem_ExpandableArray_NewArray(&gl_bufferobjectinfoarray, r_main_mempool, sizeof(gl_bufferobjectinfo_t), 128);
 
-       Con_Printf("OpenGL backend started.\n");
+       Con_DPrintf("OpenGL backend started.\n");
 
        CHECKGLERROR
 
@@ -215,7 +212,7 @@ static void gl_backend_shutdown(void)
        backendarrayunits = 0;
        backendactive = false;
 
-       Con_Print("OpenGL Backend shutting down\n");
+       Con_DPrint("OpenGL Backend shutting down\n");
 
        Mem_ExpandableArray_FreeArray(&gl_bufferobjectinfoarray);
 
@@ -265,7 +262,7 @@ void gl_backend_init(void)
        Cvar_RegisterVariable(&gl_mesh_testarrayelement);
        Cvar_RegisterVariable(&gl_mesh_testmanualfeeding);
 
-       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\n");
+       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);
 }
@@ -282,7 +279,7 @@ void GL_SetupView_Orientation_Identity (void)
 void GL_SetupView_Orientation_FromEntity(const matrix4x4_t *matrix)
 {
        matrix4x4_t tempmatrix, basematrix;
-       Matrix4x4_Invert_Simple(&tempmatrix, matrix);
+       Matrix4x4_Invert_Full(&tempmatrix, matrix);
        Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
        Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
        Matrix4x4_Concat(&backend_viewmatrix, &basematrix, &tempmatrix);
@@ -299,7 +296,11 @@ void GL_SetupView_Orientation_FromEntity(const matrix4x4_t *matrix)
        //Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[0], 0, 1, 0);
        //Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[1], 0, 0, 1);
        //Matrix4x4_ConcatTranslate(&backend_viewmatrix, -origin[0], -origin[1], -origin[2]);
+
+       // force an update of the model matrix by copying it off, resetting it, and then calling the R_Mesh_Matrix function with it
+       tempmatrix = backend_modelmatrix;
        memset(&backend_modelmatrix, 0, sizeof(backend_modelmatrix));
+       R_Mesh_Matrix(&tempmatrix);
 }
 
 void GL_SetupView_Mode_Perspective (double frustumx, double frustumy, double zNear, double zFar)
@@ -368,6 +369,65 @@ void GL_SetupView_Mode_Ortho (double x1, double y1, double x2, double y2, double
        CHECKGLERROR
 }
 
+void GL_SetupView_ApplyCustomNearClipPlane(double normalx, double normaly, double normalz, double dist)
+{
+       double matrix[16];
+       double q[4];
+       double d;
+       float clipPlane[4], v3[3], v4[3];
+       float normal[3];
+
+       // This is Olique Depth Projection from http://www.terathon.com/code/oblique.php
+       // modified to fit in this codebase.
+
+       VectorSet(normal, normalx, normaly, normalz);
+       Matrix4x4_Transform3x3(&backend_viewmatrix, normal, clipPlane);
+       VectorScale(normal, dist, v3);
+       Matrix4x4_Transform(&backend_viewmatrix, v3, v4);
+       // FIXME: LordHavoc: I think this can be done more efficiently somehow but I can't remember the technique
+       clipPlane[3] = -DotProduct(v4, clipPlane);
+
+#if 0
+{
+       // testing code for comparing results
+       float clipPlane2[4];
+       VectorCopy4(clipPlane, clipPlane2);
+       R_Mesh_Matrix(&identitymatrix);
+       VectorSet(q, normal[0], normal[1], normal[2], -dist);
+       qglClipPlane(GL_CLIP_PLANE0, q);
+       qglGetClipPlane(GL_CLIP_PLANE0, q);
+       VectorCopy4(q, clipPlane);
+}
+#endif
+
+       // Calculate the clip-space corner point opposite the clipping plane
+       // 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
+       Matrix4x4_ToArrayDoubleGL(&backend_projectmatrix, matrix);
+
+       q[0] = ((clipPlane[0] < 0.0f ? -1.0f : clipPlane[0] > 0.0f ? 1.0f : 0.0f) + matrix[8]) / matrix[0];
+       q[1] = ((clipPlane[1] < 0.0f ? -1.0f : clipPlane[1] > 0.0f ? 1.0f : 0.0f) + matrix[9]) / matrix[5];
+       q[2] = -1.0f;
+       q[3] = (1.0f + matrix[10]) / matrix[14];
+
+       // Calculate the scaled plane vector
+       d = 2.0f / DotProduct4(clipPlane, q);
+
+       // Replace the third row of the projection matrix
+       matrix[2] = clipPlane[0] * d;
+       matrix[6] = clipPlane[1] * d;
+       matrix[10] = clipPlane[2] * d + 1.0f;
+       matrix[14] = clipPlane[3] * d;
+
+       // Load it back into OpenGL
+       qglMatrixMode(GL_PROJECTION);CHECKGLERROR
+       qglLoadMatrixd(matrix);CHECKGLERROR
+       qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
+       CHECKGLERROR
+       Matrix4x4_FromArrayDoubleGL(&backend_projectmatrix, matrix);
+}
+
 typedef struct gltextureunit_s
 {
        const void *pointer_texcoord;
@@ -844,9 +904,9 @@ void GL_TransformToScreen(const vec4_t in, vec4_t out)
        Matrix4x4_Transform4 (&backend_viewmatrix, in, temp);
        Matrix4x4_Transform4 (&backend_projectmatrix, temp, 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 + r_view.height - (out[1] * iw + 1.0f) * r_view.height * 0.5f;
-       out[2] = r_view.z + (out[2] * iw + 1.0f) * r_view.depth * 0.5f;
+       out[0] = r_refdef.view.x + (out[0] * iw + 1.0f) * r_refdef.view.width * 0.5f;
+       out[1] = r_refdef.view.y + r_refdef.view.height - (out[1] * iw + 1.0f) * r_refdef.view.height * 0.5f;
+       out[2] = r_refdef.view.z + (out[2] * iw + 1.0f) * r_refdef.view.depth * 0.5f;
 }
 
 // called at beginning of frame
@@ -874,8 +934,15 @@ 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])
-               Con_DPrintf("%s shader compile log:\n%s\n", shadertype, compilelog);
+       if (compilelog[0] && developer.integer > 0)
+       {
+               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);
+       }
        if (!shadercompiled)
        {
                qglDeleteObjectARB(shaderobject);CHECKGLERROR
@@ -962,7 +1029,7 @@ void R_Mesh_Draw(int firstvertex, int numvertices, int numtriangles, const int *
                Con_Printf("R_Mesh_Draw(%d, %d, %d, %8p, %i, %p);\n", firstvertex, numvertices, numtriangles, elements, bufferobject, (void *)bufferoffset);
                return;
        }
-       if (!gl_vbo.integer)
+       if (gl_vbo.integer != 1)
                bufferobject = 0;
        CHECKGLERROR
        r_refdef.stats.meshes++;
@@ -1758,8 +1825,9 @@ void R_Mesh_TexMatrix(unsigned int unitnum, const matrix4x4_t *matrix)
                        unit->matrix = *matrix;
                        CHECKGLERROR
                        Matrix4x4_ToArrayDoubleGL(&unit->matrix, glmatrix);
-                       qglMatrixMode(GL_TEXTURE);CHECKGLERROR
                        GL_ActiveTexture(unitnum);
+                       GL_ClientActiveTexture(unitnum);
+                       qglMatrixMode(GL_TEXTURE);CHECKGLERROR
                        qglLoadMatrixd(glmatrix);CHECKGLERROR
                        qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
                }
@@ -1771,8 +1839,9 @@ void R_Mesh_TexMatrix(unsigned int unitnum, const matrix4x4_t *matrix)
                {
                        unit->texmatrixenabled = false;
                        CHECKGLERROR
-                       qglMatrixMode(GL_TEXTURE);CHECKGLERROR
                        GL_ActiveTexture(unitnum);
+                       GL_ClientActiveTexture(unitnum);
+                       qglMatrixMode(GL_TEXTURE);CHECKGLERROR
                        qglLoadIdentity();CHECKGLERROR
                        qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
                }