]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_backend.c
video modes in menu now also set vid_pixelheight and vid_conwidth/vid_conheight
[xonotic/darkplaces.git] / gl_backend.c
index c281e91f43ece991672745bb92f1846befd450ce..1e7e788c6231f403899db61559279cd566c36d7a 100644 (file)
@@ -73,7 +73,7 @@ void GL_PrintError(int errornumber, char *filename, int linenumber)
 }
 #endif
 
-#define BACKENDACTIVECHECK if (!backendactive) Sys_Error("GL backend function called when backend is not active\n");
+#define BACKENDACTIVECHECK if (!backendactive) Sys_Error("GL backend function called when backend is not active");
 
 void SCR_ScreenShot_f (void);
 
@@ -174,9 +174,9 @@ static void gl_backend_start(void)
        if (gl_support_fragment_shader)
        {
                CHECKGLERROR
-               qglGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS_ARB, &backendimageunits);
+               qglGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS_ARB, (int *)&backendimageunits);
                CHECKGLERROR
-               qglGetIntegerv(GL_MAX_TEXTURE_COORDS_ARB, &backendarrayunits);
+               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);
        }
@@ -274,9 +274,8 @@ void GL_SetupView_Orientation_FromEntity(matrix4x4_t *matrix)
        memset(&backend_modelmatrix, 0, sizeof(backend_modelmatrix));
 }
 
-void GL_SetupView_Mode_Perspective (double fovx, double fovy, double zNear, double zFar)
+void GL_SetupView_Mode_Perspective (double frustumx, double frustumy, double zNear, double zFar)
 {
-       double xmax, ymax;
        double m[16];
 
        if (!r_render.integer)
@@ -285,11 +284,8 @@ void GL_SetupView_Mode_Perspective (double fovx, double fovy, double zNear, doub
        // set up viewpoint
        qglMatrixMode(GL_PROJECTION);CHECKGLERROR
        qglLoadIdentity();CHECKGLERROR
-       // pyramid slopes
-       xmax = zNear * tan(fovx * M_PI / 360.0);
-       ymax = zNear * tan(fovy * M_PI / 360.0);
        // set view pyramid
-       qglFrustum(-xmax, xmax, -ymax, ymax, zNear, zFar);CHECKGLERROR
+       qglFrustum(-frustumx * zNear, frustumx * zNear, -frustumy * zNear, frustumy * zNear, zNear, zFar);CHECKGLERROR
        qglGetDoublev(GL_PROJECTION_MATRIX, m);
        backend_projectmatrix.m[0][0] = m[0];
        backend_projectmatrix.m[1][0] = m[1];
@@ -311,7 +307,7 @@ void GL_SetupView_Mode_Perspective (double fovx, double fovy, double zNear, doub
        GL_SetupView_Orientation_Identity();
 }
 
-void GL_SetupView_Mode_PerspectiveInfiniteFarClip (double fovx, double fovy, double zNear)
+void GL_SetupView_Mode_PerspectiveInfiniteFarClip (double frustumx, double frustumy, double zNear)
 {
        double nudge, m[16];
 
@@ -323,12 +319,12 @@ void GL_SetupView_Mode_PerspectiveInfiniteFarClip (double fovx, double fovy, dou
        qglLoadIdentity();CHECKGLERROR
        // set view pyramid
        nudge = 1.0 - 1.0 / (1<<23);
-       m[ 0] = 1.0 / tan(fovx * M_PI / 360.0);
+       m[ 0] = 1.0 / frustumx;
        m[ 1] = 0;
        m[ 2] = 0;
        m[ 3] = 0;
        m[ 4] = 0;
-       m[ 5] = 1.0 / tan(fovy * M_PI / 360.0);
+       m[ 5] = 1.0 / frustumy;
        m[ 6] = 0;
        m[ 7] = 0;
        m[ 8] = 0;
@@ -407,7 +403,7 @@ typedef struct gltextureunit_s
 }
 gltextureunit_t;
 
-static struct
+static struct gl_state_s
 {
        int blendfunc1;
        int blendfunc2;
@@ -432,8 +428,8 @@ void GL_SetupTextureState(void)
        unsigned int i;
        gltextureunit_t *unit;
        CHECKGLERROR
-       gl_state.unit = -1;
-       gl_state.clientunit = -1;
+       gl_state.unit = MAX_TEXTUREUNITS;
+       gl_state.clientunit = MAX_TEXTUREUNITS;
        for (i = 0;i < MAX_TEXTUREUNITS;i++)
        {
                unit = gl_state.units + i;
@@ -757,7 +753,7 @@ unsigned int GL_Backend_CompileProgram(int vertexstrings_count, const char **ver
 {
        GLint vertexshadercompiled, fragmentshadercompiled, programlinked;
        GLuint vertexshaderobject, fragmentshaderobject, programobject = 0;
-       char compilelog[4096];
+       char compilelog[MAX_INPUTLINE];
        CHECKGLERROR
 
        programobject = qglCreateProgramObjectARB();
@@ -886,8 +882,8 @@ void R_Mesh_Draw(int firstvertex, int numvertices, int numtriangles, const int *
                R_Mesh_Draw_ShowTris(firstvertex, numvertices, numtriangles, elements);
                return;
        }
-       c_meshs++;
-       c_meshelements += numelements;
+       renderstats.meshes++;
+       renderstats.meshes_elements += numelements;
        if (gl_paranoid.integer)
        {
                unsigned int i, j, size;
@@ -1102,6 +1098,8 @@ void R_Mesh_VertexPointer(const float *vertex3f)
 
 void R_Mesh_ColorPointer(const float *color4f)
 {
+       if (r_showtrispass)
+               return;
        if (gl_state.pointer_color != color4f)
        {
                CHECKGLERROR
@@ -1127,6 +1125,8 @@ void R_Mesh_ColorPointer(const float *color4f)
 void R_Mesh_TexCoordPointer(unsigned int unitnum, unsigned int numcomponents, const float *texcoord)
 {
        gltextureunit_t *unit = gl_state.units + unitnum;
+       if (r_showtrispass)
+               return;
        // update array settings
        if (texcoord)
        {
@@ -1162,7 +1162,9 @@ void R_Mesh_TexCoordPointer(unsigned int unitnum, unsigned int numcomponents, co
 void R_Mesh_TexBindAll(unsigned int unitnum, int tex1d, int tex2d, int tex3d, int texcubemap)
 {
        gltextureunit_t *unit = gl_state.units + unitnum;
-       if (unitnum >= backendunits)
+       if (unitnum >= backendimageunits)
+               return;
+       if (r_showtrispass)
                return;
        // update 1d texture binding
        if (unit->t1d != tex1d)
@@ -1253,21 +1255,26 @@ void R_Mesh_TexBindAll(unsigned int unitnum, int tex1d, int tex2d, int tex3d, in
 void R_Mesh_TexBind1D(unsigned int unitnum, int texnum)
 {
        gltextureunit_t *unit = gl_state.units + unitnum;
-       if (unitnum >= backendunits)
+       if (unitnum >= backendimageunits)
+               return;
+       if (r_showtrispass)
                return;
        // update 1d texture binding
        if (unit->t1d != texnum)
        {
                GL_ActiveTexture(unitnum);
-               if (texnum)
-               {
-                       if (unit->t1d == 0)
-                               qglEnable(GL_TEXTURE_1D);
-               }
-               else
+               if (unitnum < backendunits)
                {
-                       if (unit->t1d)
-                               qglDisable(GL_TEXTURE_1D);
+                       if (texnum)
+                       {
+                               if (unit->t1d == 0)
+                                       qglEnable(GL_TEXTURE_1D);
+                       }
+                       else
+                       {
+                               if (unit->t1d)
+                                       qglDisable(GL_TEXTURE_1D);
+                       }
                }
                unit->t1d = texnum;
                qglBindTexture(GL_TEXTURE_1D, unit->t1d);
@@ -1277,8 +1284,11 @@ void R_Mesh_TexBind1D(unsigned int unitnum, int texnum)
        if (unit->t2d)
        {
                GL_ActiveTexture(unitnum);
-               if (unit->t2d)
-                       qglDisable(GL_TEXTURE_2D);
+               if (unitnum < backendunits)
+               {
+                       if (unit->t2d)
+                               qglDisable(GL_TEXTURE_2D);
+               }
                unit->t2d = 0;
                qglBindTexture(GL_TEXTURE_2D, unit->t2d);
                CHECKGLERROR
@@ -1287,8 +1297,11 @@ void R_Mesh_TexBind1D(unsigned int unitnum, int texnum)
        if (unit->t3d)
        {
                GL_ActiveTexture(unitnum);
-               if (unit->t3d)
-                       qglDisable(GL_TEXTURE_3D);
+               if (unitnum < backendunits)
+               {
+                       if (unit->t3d)
+                               qglDisable(GL_TEXTURE_3D);
+               }
                unit->t3d = 0;
                qglBindTexture(GL_TEXTURE_3D, unit->t3d);
                CHECKGLERROR
@@ -1297,8 +1310,11 @@ void R_Mesh_TexBind1D(unsigned int unitnum, int texnum)
        if (unit->tcubemap)
        {
                GL_ActiveTexture(unitnum);
-               if (unit->tcubemap)
-                       qglDisable(GL_TEXTURE_CUBE_MAP_ARB);
+               if (unitnum < backendunits)
+               {
+                       if (unit->tcubemap)
+                               qglDisable(GL_TEXTURE_CUBE_MAP_ARB);
+               }
                unit->tcubemap = 0;
                qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);
                CHECKGLERROR
@@ -1308,14 +1324,19 @@ void R_Mesh_TexBind1D(unsigned int unitnum, int texnum)
 void R_Mesh_TexBind(unsigned int unitnum, int texnum)
 {
        gltextureunit_t *unit = gl_state.units + unitnum;
-       if (unitnum >= backendunits)
+       if (unitnum >= backendimageunits)
+               return;
+       if (r_showtrispass)
                return;
        // update 1d texture binding
        if (unit->t1d)
        {
                GL_ActiveTexture(unitnum);
-               if (unit->t1d)
-                       qglDisable(GL_TEXTURE_1D);
+               if (unitnum < backendunits)
+               {
+                       if (unit->t1d)
+                               qglDisable(GL_TEXTURE_1D);
+               }
                unit->t1d = 0;
                qglBindTexture(GL_TEXTURE_1D, unit->t1d);
                CHECKGLERROR
@@ -1324,15 +1345,18 @@ void R_Mesh_TexBind(unsigned int unitnum, int texnum)
        if (unit->t2d != texnum)
        {
                GL_ActiveTexture(unitnum);
-               if (texnum)
-               {
-                       if (unit->t2d == 0)
-                               qglEnable(GL_TEXTURE_2D);
-               }
-               else
+               if (unitnum < backendunits)
                {
-                       if (unit->t2d)
-                               qglDisable(GL_TEXTURE_2D);
+                       if (texnum)
+                       {
+                               if (unit->t2d == 0)
+                                       qglEnable(GL_TEXTURE_2D);
+                       }
+                       else
+                       {
+                               if (unit->t2d)
+                                       qglDisable(GL_TEXTURE_2D);
+                       }
                }
                unit->t2d = texnum;
                qglBindTexture(GL_TEXTURE_2D, unit->t2d);
@@ -1342,8 +1366,11 @@ void R_Mesh_TexBind(unsigned int unitnum, int texnum)
        if (unit->t3d)
        {
                GL_ActiveTexture(unitnum);
-               if (unit->t3d)
-                       qglDisable(GL_TEXTURE_3D);
+               if (unitnum < backendunits)
+               {
+                       if (unit->t3d)
+                               qglDisable(GL_TEXTURE_3D);
+               }
                unit->t3d = 0;
                qglBindTexture(GL_TEXTURE_3D, unit->t3d);
                CHECKGLERROR
@@ -1352,8 +1379,11 @@ void R_Mesh_TexBind(unsigned int unitnum, int texnum)
        if (unit->tcubemap != 0)
        {
                GL_ActiveTexture(unitnum);
-               if (unit->tcubemap)
-                       qglDisable(GL_TEXTURE_CUBE_MAP_ARB);
+               if (unitnum < backendunits)
+               {
+                       if (unit->tcubemap)
+                               qglDisable(GL_TEXTURE_CUBE_MAP_ARB);
+               }
                unit->tcubemap = 0;
                qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);
                CHECKGLERROR
@@ -1363,14 +1393,19 @@ void R_Mesh_TexBind(unsigned int unitnum, int texnum)
 void R_Mesh_TexBind3D(unsigned int unitnum, int texnum)
 {
        gltextureunit_t *unit = gl_state.units + unitnum;
-       if (unitnum >= backendunits)
+       if (unitnum >= backendimageunits)
+               return;
+       if (r_showtrispass)
                return;
        // update 1d texture binding
        if (unit->t1d)
        {
                GL_ActiveTexture(unitnum);
-               if (unit->t1d)
-                       qglDisable(GL_TEXTURE_1D);
+               if (unitnum < backendunits)
+               {
+                       if (unit->t1d)
+                               qglDisable(GL_TEXTURE_1D);
+               }
                unit->t1d = 0;
                qglBindTexture(GL_TEXTURE_1D, unit->t1d);
                CHECKGLERROR
@@ -1379,8 +1414,11 @@ void R_Mesh_TexBind3D(unsigned int unitnum, int texnum)
        if (unit->t2d)
        {
                GL_ActiveTexture(unitnum);
-               if (unit->t2d)
-                       qglDisable(GL_TEXTURE_2D);
+               if (unitnum < backendunits)
+               {
+                       if (unit->t2d)
+                               qglDisable(GL_TEXTURE_2D);
+               }
                unit->t2d = 0;
                qglBindTexture(GL_TEXTURE_2D, unit->t2d);
                CHECKGLERROR
@@ -1389,15 +1427,18 @@ void R_Mesh_TexBind3D(unsigned int unitnum, int texnum)
        if (unit->t3d != texnum)
        {
                GL_ActiveTexture(unitnum);
-               if (texnum)
-               {
-                       if (unit->t3d == 0)
-                               qglEnable(GL_TEXTURE_3D);
-               }
-               else
+               if (unitnum < backendunits)
                {
-                       if (unit->t3d)
-                               qglDisable(GL_TEXTURE_3D);
+                       if (texnum)
+                       {
+                               if (unit->t3d == 0)
+                                       qglEnable(GL_TEXTURE_3D);
+                       }
+                       else
+                       {
+                               if (unit->t3d)
+                                       qglDisable(GL_TEXTURE_3D);
+                       }
                }
                unit->t3d = texnum;
                qglBindTexture(GL_TEXTURE_3D, unit->t3d);
@@ -1407,8 +1448,11 @@ void R_Mesh_TexBind3D(unsigned int unitnum, int texnum)
        if (unit->tcubemap != 0)
        {
                GL_ActiveTexture(unitnum);
-               if (unit->tcubemap)
-                       qglDisable(GL_TEXTURE_CUBE_MAP_ARB);
+               if (unitnum < backendunits)
+               {
+                       if (unit->tcubemap)
+                               qglDisable(GL_TEXTURE_CUBE_MAP_ARB);
+               }
                unit->tcubemap = 0;
                qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);
                CHECKGLERROR
@@ -1418,14 +1462,19 @@ void R_Mesh_TexBind3D(unsigned int unitnum, int texnum)
 void R_Mesh_TexBindCubeMap(unsigned int unitnum, int texnum)
 {
        gltextureunit_t *unit = gl_state.units + unitnum;
-       if (unitnum >= backendunits)
+       if (unitnum >= backendimageunits)
+               return;
+       if (r_showtrispass)
                return;
        // update 1d texture binding
        if (unit->t1d)
        {
                GL_ActiveTexture(unitnum);
-               if (unit->t1d)
-                       qglDisable(GL_TEXTURE_1D);
+               if (unitnum < backendunits)
+               {
+                       if (unit->t1d)
+                               qglDisable(GL_TEXTURE_1D);
+               }
                unit->t1d = 0;
                qglBindTexture(GL_TEXTURE_1D, unit->t1d);
                CHECKGLERROR
@@ -1434,8 +1483,11 @@ void R_Mesh_TexBindCubeMap(unsigned int unitnum, int texnum)
        if (unit->t2d)
        {
                GL_ActiveTexture(unitnum);
-               if (unit->t2d)
-                       qglDisable(GL_TEXTURE_2D);
+               if (unitnum < backendunits)
+               {
+                       if (unit->t2d)
+                               qglDisable(GL_TEXTURE_2D);
+               }
                unit->t2d = 0;
                qglBindTexture(GL_TEXTURE_2D, unit->t2d);
                CHECKGLERROR
@@ -1444,8 +1496,11 @@ void R_Mesh_TexBindCubeMap(unsigned int unitnum, int texnum)
        if (unit->t3d)
        {
                GL_ActiveTexture(unitnum);
-               if (unit->t3d)
-                       qglDisable(GL_TEXTURE_3D);
+               if (unitnum < backendunits)
+               {
+                       if (unit->t3d)
+                               qglDisable(GL_TEXTURE_3D);
+               }
                unit->t3d = 0;
                qglBindTexture(GL_TEXTURE_3D, unit->t3d);
                CHECKGLERROR
@@ -1454,15 +1509,18 @@ void R_Mesh_TexBindCubeMap(unsigned int unitnum, int texnum)
        if (unit->tcubemap != texnum)
        {
                GL_ActiveTexture(unitnum);
-               if (texnum)
-               {
-                       if (unit->tcubemap == 0)
-                               qglEnable(GL_TEXTURE_CUBE_MAP_ARB);
-               }
-               else
+               if (unitnum < backendunits)
                {
-                       if (unit->tcubemap)
-                               qglDisable(GL_TEXTURE_CUBE_MAP_ARB);
+                       if (texnum)
+                       {
+                               if (unit->tcubemap == 0)
+                                       qglEnable(GL_TEXTURE_CUBE_MAP_ARB);
+                       }
+                       else
+                       {
+                               if (unit->tcubemap)
+                                       qglDisable(GL_TEXTURE_CUBE_MAP_ARB);
+                       }
                }
                unit->tcubemap = texnum;
                qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);
@@ -1473,6 +1531,8 @@ void R_Mesh_TexBindCubeMap(unsigned int unitnum, int texnum)
 void R_Mesh_TexMatrix(unsigned int unitnum, const matrix4x4_t *matrix)
 {
        gltextureunit_t *unit = gl_state.units + unitnum;
+       if (r_showtrispass)
+               return;
        if (matrix->m[3][3])
        {
                // texmatrix specified, check if it is different
@@ -1505,6 +1565,8 @@ void R_Mesh_TexMatrix(unsigned int unitnum, const matrix4x4_t *matrix)
 void R_Mesh_TexCombine(unsigned int unitnum, int combinergb, int combinealpha, int rgbscale, int alphascale)
 {
        gltextureunit_t *unit = gl_state.units + unitnum;
+       if (r_showtrispass)
+               return;
        if (gl_combine.integer)
        {
                // GL_ARB_texture_env_combine
@@ -1560,10 +1622,6 @@ void R_Mesh_State(const rmeshstate_t *m)
        BACKENDACTIVECHECK
 
        R_Mesh_VertexPointer(m->pointer_vertex);
-
-       if (r_showtrispass)
-               return;
-
        R_Mesh_ColorPointer(m->pointer_color);
 
        if (gl_backend_rebindtextures)
@@ -1609,7 +1667,7 @@ void R_Mesh_Draw_ShowTris(int firstvertex, int numvertices, int numtriangles, co
 ==============================================================================
 */
 
-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, qboolean gammacorrect)
+qboolean SCR_ScreenShot(char *filename, unsigned char *buffer1, unsigned char *buffer2, unsigned char *buffer3, int x, int y, int width, int height, qboolean flipx, qboolean flipy, qboolean flipdiagonal, qboolean jpeg, qboolean gammacorrect)
 {
        int     indices[3] = {0,1,2};
        qboolean ret;
@@ -1675,17 +1733,6 @@ void R_ClearScreen(void)
        }
 }
 
-/*
-====================
-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)/vid_pixelaspect.value)*tan(fov_x/360.0*M_PI))*360.0/M_PI;
-}
-
 int r_stereo_side;
 
 void SCR_DrawScreen (void)
@@ -1735,8 +1782,16 @@ void SCR_DrawScreen (void)
                        }
 
                        // LordHavoc: viewzoom (zoom in for sniper rifles, etc)
-                       r_refdef.fov_x = scr_fov.value * r_refdef.fovscale_x;
-                       r_refdef.fov_y = CalcFov (scr_fov.value, r_refdef.width, r_refdef.height) * r_refdef.fovscale_y;
+                       // LordHavoc: this is designed to produce widescreen fov values
+                       // when the screen is wider than 4/3 width/height aspect, to do
+                       // this it simply assumes the requested fov is the vertical fov
+                       // for a 4x3 display, if the ratio is not 4x3 this makes the fov
+                       // higher/lower according to the ratio
+                       r_refdef.frustum_y = tan(scr_fov.value * cl.viewzoom * M_PI / 360.0) * (3.0/4.0);
+                       r_refdef.frustum_x = r_refdef.frustum_y * (float)r_refdef.width / (float)r_refdef.height / vid_pixelheight.value;
+
+                       r_refdef.frustum_x *= r_refdef.frustumscale_x;
+                       r_refdef.frustum_y *= r_refdef.frustumscale_y;
 
                        R_RenderView();
 
@@ -1748,8 +1803,12 @@ void SCR_DrawScreen (void)
                                r_refdef.height = vid.height * sizey;
                                r_refdef.x = (vid.width - 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(scr_zoomwindow_fov.value, r_refdef.width, r_refdef.height) * r_refdef.fovscale_y;
+
+                               r_refdef.frustum_y = tan(scr_zoomwindow_fov.value * cl.viewzoom * M_PI / 360.0) * (3.0/4.0);
+                               r_refdef.frustum_x = r_refdef.frustum_y * vid_pixelheight.value * (float)r_refdef.width / (float)r_refdef.height;
+
+                               r_refdef.frustum_x *= r_refdef.frustumscale_x;
+                               r_refdef.frustum_y *= r_refdef.frustumscale_y;
 
                                R_RenderView();
                        }
@@ -1977,7 +2036,7 @@ static rcachearrayitem_t r_mesh_rcacheitems[RCACHEARRAY_ITEMS];
 // size of data buffer
 static int r_mesh_rcachedata_size = RCACHEARRAY_DEFAULTSIZE;
 // data buffer
-static qbyte r_mesh_rcachedata[RCACHEARRAY_DEFAULTSIZE];
+static unsigned char r_mesh_rcachedata[RCACHEARRAY_DEFAULTSIZE];
 
 // current state
 static int r_mesh_rcachedata_offset;
@@ -2038,7 +2097,7 @@ int R_Mesh_CacheArray(rcachearrayrequest_t *r)
        //R_Mesh_CacheArray_ValidateState(3);
        // calculate a hashindex to choose a cache chain
        r->data = NULL;
-       hashindex = CRC_Block((void *)r, sizeof(*r)) % RCACHEARRAY_HASHSIZE;
+       hashindex = CRC_Block((unsigned char *)r, sizeof(*r)) % RCACHEARRAY_HASHSIZE;
 
        // is it already cached?
        for (lhead = &r_mesh_rcachechain[hashindex], l = lhead->next;l != lhead;l = l->next)