GL_DrawRangeElements now reports if it got a bogus indexcount and vertex range
[xonotic/darkplaces.git] / gl_backend.c
1
2 #include "quakedef.h"
3
4 cvar_t gl_mesh_maxverts = {0, "gl_mesh_maxverts", "1024"};
5 cvar_t gl_mesh_floatcolors = {0, "gl_mesh_floatcolors", "1"};
6 cvar_t gl_mesh_drawrangeelements = {0, "gl_mesh_drawrangeelements", "1"};
7
8 cvar_t r_render = {0, "r_render", "1"};
9 cvar_t gl_dither = {CVAR_SAVE, "gl_dither", "1"}; // whether or not to use dithering
10 cvar_t gl_lockarrays = {0, "gl_lockarrays", "1"};
11
12 int gl_maxdrawrangeelementsvertices;
13 int gl_maxdrawrangeelementsindices;
14
15 #ifdef DEBUGGL
16 int errornumber = 0;
17
18 void GL_PrintError(int errornumber, char *filename, int linenumber)
19 {
20         switch(errornumber)
21         {
22 #ifdef GL_INVALID_ENUM
23         case GL_INVALID_ENUM:
24                 Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
25                 break;
26 #endif
27 #ifdef GL_INVALID_VALUE
28         case GL_INVALID_VALUE:
29                 Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
30                 break;
31 #endif
32 #ifdef GL_INVALID_OPERATION
33         case GL_INVALID_OPERATION:
34                 Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
35                 break;
36 #endif
37 #ifdef GL_STACK_OVERFLOW
38         case GL_STACK_OVERFLOW:
39                 Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
40                 break;
41 #endif
42 #ifdef GL_STACK_UNDERFLOW
43         case GL_STACK_UNDERFLOW:
44                 Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
45                 break;
46 #endif
47 #ifdef GL_OUT_OF_MEMORY
48         case GL_OUT_OF_MEMORY:
49                 Con_Printf("GL_OUT_OF_MEMORY at %s:%i\n", filename, linenumber);
50                 break;
51 #endif
52 #ifdef GL_TABLE_TOO_LARGE
53     case GL_TABLE_TOO_LARGE:
54                 Con_Printf("GL_TABLE_TOO_LARGE at %s:%i\n", filename, linenumber);
55                 break;
56 #endif
57         default:
58                 Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
59                 break;
60         }
61 }
62 #endif
63
64 #define BACKENDACTIVECHECK if (!backendactive) Sys_Error("GL backend function called when backend is not active\n");
65
66 int c_meshs, c_meshelements;
67
68 void SCR_ScreenShot_f (void);
69
70 // these are externally accessible
71 int r_lightmapscalebit;
72 float r_colorscale;
73 float *varray_vertex;
74 float *varray_color;
75 float *varray_texcoord[MAX_TEXTUREUNITS];
76 int mesh_maxverts;
77
78 static matrix4x4_t backend_viewmatrix;
79 static matrix4x4_t backend_modelmatrix;
80 static matrix4x4_t backend_modelviewmatrix;
81 static matrix4x4_t backend_glmodelviewmatrix;
82
83 static int backendunits, backendactive;
84 static qbyte *varray_bcolor;
85 static mempool_t *gl_backend_mempool;
86
87 int polygonelements[768];
88
89 void GL_Backend_AllocArrays(void)
90 {
91         int i;
92
93         for (i = 0;i < POLYGONELEMENTS_MAXPOINTS - 2;i++)
94         {
95                 polygonelements[i * 3 + 0] = 0;
96                 polygonelements[i * 3 + 1] = i + 1;
97                 polygonelements[i * 3 + 2] = i + 2;
98         }
99
100         if (!gl_backend_mempool)
101                 gl_backend_mempool = Mem_AllocPool("GL_Backend");
102
103         varray_vertex = Mem_Alloc(gl_backend_mempool, mesh_maxverts * sizeof(float[4]));
104         varray_color = Mem_Alloc(gl_backend_mempool, mesh_maxverts * sizeof(float[4]));
105         varray_bcolor = Mem_Alloc(gl_backend_mempool, mesh_maxverts * sizeof(qbyte[4]));
106         for (i = 0;i < backendunits;i++)
107                 varray_texcoord[i] = Mem_Alloc(gl_backend_mempool, mesh_maxverts * sizeof(float[4]));
108         for (;i < MAX_TEXTUREUNITS;i++)
109                 varray_texcoord[i] = NULL;
110 }
111
112 void GL_Backend_FreeArrays(int resizingbuffers)
113 {
114         int i;
115         if (resizingbuffers)
116                 Mem_EmptyPool(gl_backend_mempool);
117         else
118                 Mem_FreePool(&gl_backend_mempool);
119         varray_vertex = NULL;
120         varray_color = NULL;
121         varray_bcolor = NULL;
122         for (i = 0;i < MAX_TEXTUREUNITS;i++)
123                 varray_texcoord[i] = NULL;
124 }
125
126 static void gl_backend_start(void)
127 {
128         Con_Printf("OpenGL Backend started with gl_mesh_maxverts %i\n", gl_mesh_maxverts.integer);
129         if (qglDrawRangeElements != NULL)
130         {
131                 qglGetIntegerv(GL_MAX_ELEMENTS_VERTICES, &gl_maxdrawrangeelementsvertices);
132                 qglGetIntegerv(GL_MAX_ELEMENTS_INDICES, &gl_maxdrawrangeelementsindices);
133                 CHECKGLERROR
134                 Con_Printf("glDrawRangeElements detected (max vertices %i, max indices %i)\n", gl_maxdrawrangeelementsvertices, gl_maxdrawrangeelementsindices);
135         }
136         if (strstr(gl_renderer, "3Dfx"))
137         {
138                 Con_Printf("3Dfx driver detected, forcing gl_mesh_floatcolors to 0 to prevent crashs\n");
139                 Cvar_SetValueQuick(&gl_mesh_floatcolors, 0);
140         }
141
142         backendunits = min(MAX_TEXTUREUNITS, gl_textureunits);
143
144         GL_Backend_AllocArrays();
145
146         backendactive = true;
147 }
148
149 static void gl_backend_shutdown(void)
150 {
151         backendunits = 0;
152         backendactive = false;
153
154         Con_Printf("OpenGL Backend shutting down\n");
155
156         GL_Backend_FreeArrays(false);
157 }
158
159 void GL_Backend_CheckCvars(void)
160 {
161         // 21760 is (65536 / 3) rounded off to a multiple of 128
162         if (gl_mesh_maxverts.integer < 1024)
163                 Cvar_SetValueQuick(&gl_mesh_maxverts, 1024);
164         if (gl_mesh_maxverts.integer > 21760)
165                 Cvar_SetValueQuick(&gl_mesh_maxverts, 21760);
166 }
167
168 void GL_Backend_ResizeArrays(int numtriangles)
169 {
170         Cvar_SetValueQuick(&gl_mesh_maxverts, numtriangles);
171         GL_Backend_CheckCvars();
172         mesh_maxverts = gl_mesh_maxverts.integer;
173         GL_Backend_FreeArrays(true);
174         GL_Backend_AllocArrays();
175 }
176
177 static void gl_backend_newmap(void)
178 {
179 }
180
181 void gl_backend_init(void)
182 {
183         Cvar_RegisterVariable(&r_render);
184         Cvar_RegisterVariable(&gl_dither);
185         Cvar_RegisterVariable(&gl_lockarrays);
186 #ifdef NORENDER
187         Cvar_SetValue("r_render", 0);
188 #endif
189
190         Cvar_RegisterVariable(&gl_mesh_maxverts);
191         Cvar_RegisterVariable(&gl_mesh_floatcolors);
192         Cvar_RegisterVariable(&gl_mesh_drawrangeelements);
193         GL_Backend_CheckCvars();
194         R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap);
195 }
196
197 void GL_SetupView_ViewPort (int x, int y, int width, int height)
198 {
199         if (!r_render.integer)
200                 return;
201
202         // y is weird beause OpenGL is bottom to top, we use top to bottom
203         qglViewport(x, vid.realheight - (y + height), width, height);
204         CHECKGLERROR
205 }
206
207 void GL_SetupView_Orientation_Identity (void)
208 {
209         Matrix4x4_CreateIdentity(&backend_viewmatrix);
210         memset(&backend_modelmatrix, 0, sizeof(backend_modelmatrix));
211 }
212
213 void GL_SetupView_Orientation_FromEntity (vec3_t origin, vec3_t angles)
214 {
215         Matrix4x4_CreateRotate(&backend_viewmatrix, -90, 1, 0, 0);
216         Matrix4x4_ConcatRotate(&backend_viewmatrix, 90, 0, 0, 1);
217         Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[2], 1, 0, 0);
218         Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[0], 0, 1, 0);
219         Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[1], 0, 0, 1);
220         Matrix4x4_ConcatTranslate(&backend_viewmatrix, -origin[0], -origin[1], -origin[2]);
221         memset(&backend_modelmatrix, 0, sizeof(backend_modelmatrix));
222 }
223
224 void GL_SetupView_Mode_Perspective (double aspect, double fovx, double fovy, double zNear, double zFar)
225 {
226         double xmax, ymax;
227
228         if (!r_render.integer)
229                 return;
230
231         // set up viewpoint
232         qglMatrixMode(GL_PROJECTION);CHECKGLERROR
233         qglLoadIdentity ();CHECKGLERROR
234         // pyramid slopes
235         xmax = zNear * tan(fovx * M_PI / 360.0);
236         ymax = zNear * tan(fovy * M_PI / 360.0);
237         // set view pyramid
238         qglFrustum(-xmax, xmax, -ymax, ymax, zNear, zFar);CHECKGLERROR
239         qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
240         GL_SetupView_Orientation_Identity();
241 }
242
243 void GL_SetupView_Mode_Ortho (double x1, double y1, double x2, double y2, double zNear, double zFar)
244 {
245         if (!r_render.integer)
246                 return;
247
248         // set up viewpoint
249         qglMatrixMode(GL_PROJECTION);CHECKGLERROR
250         qglLoadIdentity ();CHECKGLERROR
251         qglOrtho(x1, x2, y2, y1, zNear, zFar);
252         qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
253         GL_SetupView_Orientation_Identity();
254 }
255
256 typedef struct gltextureunit_s
257 {
258         unsigned int t1d, t2d, t3d, tcubemap;
259         unsigned int arrayenabled;
260         float rgbscale;
261         // FIXME: add more combine stuff
262 }
263 gltextureunit_t;
264
265 static struct
266 {
267         int blendfunc1;
268         int blendfunc2;
269         int blend;
270         GLboolean depthmask;
271         int depthdisable;
272         int unit;
273         int clientunit;
274         gltextureunit_t units[MAX_TEXTUREUNITS];
275         int colorarray;
276 }
277 gl_state;
278
279 void GL_SetupTextureState(void)
280 {
281         int i;
282         gltextureunit_t *unit;
283         for (i = 0;i < backendunits;i++)
284         {
285                 if (qglActiveTexture)
286                         qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
287                 if (qglClientActiveTexture)
288                         qglClientActiveTexture(GL_TEXTURE0_ARB + (gl_state.clientunit = i));CHECKGLERROR
289                 unit = gl_state.units + i;
290                 unit->t1d = 0;
291                 unit->t2d = 0;
292                 unit->t3d = 0;
293                 unit->tcubemap = 0;
294                 unit->rgbscale = 1;
295                 unit->arrayenabled = false;
296                 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
297                 if (gl_texture3d)
298                 {
299                         qglTexCoordPointer(3, GL_FLOAT, sizeof(float[4]), varray_texcoord[i]);CHECKGLERROR
300                 }
301                 else
302                 {
303                         qglTexCoordPointer(2, GL_FLOAT, sizeof(float[4]), varray_texcoord[i]);CHECKGLERROR
304                 }
305                 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
306                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
307                 if (gl_texture3d)
308                 {
309                         qglDisable(GL_TEXTURE_3D);CHECKGLERROR
310                 }
311                 if (gl_texturecubemap)
312                 {
313                         qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
314                 }
315                 if (gl_combine.integer)
316                 {
317                         qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);CHECKGLERROR
318                         qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);CHECKGLERROR
319                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);CHECKGLERROR
320                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
321                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_CONSTANT_ARB);CHECKGLERROR
322                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
323                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
324                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);CHECKGLERROR
325                         qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);CHECKGLERROR
326                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);CHECKGLERROR
327                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
328                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, GL_CONSTANT_ARB);CHECKGLERROR
329                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
330                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
331                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
332                         qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
333                         qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
334                 }
335                 else
336                 {
337                         qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
338                 }
339         }
340 }
341
342 void GL_Backend_ResetState(void)
343 {
344         memset(&gl_state, 0, sizeof(gl_state));
345         gl_state.depthdisable = false;
346         gl_state.blendfunc1 = GL_ONE;
347         gl_state.blendfunc2 = GL_ZERO;
348         gl_state.blend = false;
349         gl_state.depthmask = GL_TRUE;
350         gl_state.colorarray = false;
351
352         qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
353         qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
354
355         qglEnable(GL_CULL_FACE);CHECKGLERROR
356         qglCullFace(GL_FRONT);CHECKGLERROR
357         qglEnable(GL_DEPTH_TEST);CHECKGLERROR
358         qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
359         qglDisable(GL_BLEND);CHECKGLERROR
360         qglDepthMask(gl_state.depthmask);CHECKGLERROR
361         qglVertexPointer(3, GL_FLOAT, sizeof(float[4]), varray_vertex);CHECKGLERROR
362         qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
363         if (gl_mesh_floatcolors.integer)
364         {
365                 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), varray_color);CHECKGLERROR
366         }
367         else
368         {
369                 qglColorPointer(4, GL_UNSIGNED_BYTE, sizeof(qbyte[4]), varray_bcolor);CHECKGLERROR
370         }
371         GL_Color(1, 1, 1, 1);
372
373         GL_SetupTextureState();
374 }
375
376 void GL_UseColorArray(void)
377 {
378         if (!gl_state.colorarray)
379         {
380                 gl_state.colorarray = true;
381                 qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
382         }
383 }
384
385 void GL_Color(float cr, float cg, float cb, float ca)
386 {
387         if (gl_state.colorarray)
388         {
389                 gl_state.colorarray = false;
390                 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
391         }
392         qglColor4f(cr, cg, cb, ca);
393 }
394
395 // called at beginning of frame
396 void R_Mesh_Start(void)
397 {
398         BACKENDACTIVECHECK
399
400         CHECKGLERROR
401
402         GL_Backend_CheckCvars();
403         if (mesh_maxverts != gl_mesh_maxverts.integer)
404                 GL_Backend_ResizeArrays(gl_mesh_maxverts.integer);
405
406         GL_Backend_ResetState();
407 }
408
409 int gl_backend_rebindtextures;
410
411 void GL_ConvertColorsFloatToByte(int numverts)
412 {
413         int i, k, total;
414         // LordHavoc: to avoid problems with aliasing (treating memory as two
415         // different types - exactly what this is doing), these must be volatile
416         // (or a union)
417         volatile int *icolor;
418         volatile float *fcolor;
419         qbyte *bcolor;
420
421         total = numverts * 4;
422
423         // shift float to have 8bit fraction at base of number
424         fcolor = varray_color;
425         for (i = 0;i < total;)
426         {
427                 fcolor[i    ] += 32768.0f;
428                 fcolor[i + 1] += 32768.0f;
429                 fcolor[i + 2] += 32768.0f;
430                 fcolor[i + 3] += 32768.0f;
431                 i += 4;
432         }
433
434         // then read as integer and kill float bits...
435         icolor = (int *)varray_color;
436         bcolor = varray_bcolor;
437         for (i = 0;i < total;)
438         {
439                 k = icolor[i    ] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i    ] = (qbyte) k;
440                 k = icolor[i + 1] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i + 1] = (qbyte) k;
441                 k = icolor[i + 2] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i + 2] = (qbyte) k;
442                 k = icolor[i + 3] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i + 3] = (qbyte) k;
443                 i += 4;
444         }
445 }
446
447 void GL_DrawRangeElements(int firstvert, int endvert, int indexcount, int *index)
448 {
449         int arraylocked = false;
450         c_meshs++;
451         c_meshelements += indexcount;
452         if (indexcount == 0 || endvert == firstvert)
453         {
454                 Con_Printf("GL_DrawRangeElements(%d, %d, %d, %08p);\n", firstvert, endvert, indexcount, index);
455                 return;
456         }
457         if (gl_supportslockarrays && gl_lockarrays.integer)
458         {
459                 qglLockArraysEXT(firstvert, endvert - firstvert);
460                 CHECKGLERROR
461                 arraylocked = true;
462         }
463         if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
464                 qglDrawRangeElements(GL_TRIANGLES, firstvert, endvert, indexcount, GL_UNSIGNED_INT, (GLuint *) index);
465         else
466                 qglDrawElements(GL_TRIANGLES, indexcount, GL_UNSIGNED_INT, (GLuint *) index);
467         CHECKGLERROR
468         if (arraylocked)
469         {
470                 qglUnlockArraysEXT();
471                 CHECKGLERROR
472                 arraylocked = false;
473         }
474 }
475
476 // enlarges geometry buffers if they are too small
477 void _R_Mesh_ResizeCheck(int numverts)
478 {
479         if (numverts > mesh_maxverts)
480         {
481                 BACKENDACTIVECHECK
482                 GL_Backend_ResizeArrays(numverts + 100);
483                 GL_Backend_ResetState();
484         }
485 }
486
487 // renders the mesh
488 void R_Mesh_Draw(int numverts, int numtriangles, int *elements)
489 {
490         BACKENDACTIVECHECK
491
492         CHECKGLERROR
493
494         if (gl_state.colorarray && !gl_mesh_floatcolors.integer)
495                 GL_ConvertColorsFloatToByte(numverts);
496         if (!r_render.integer)
497                 return;
498         GL_DrawRangeElements(0, numverts, numtriangles * 3, elements);
499 }
500
501 // restores backend state, used when done with 3D rendering
502 void R_Mesh_Finish(void)
503 {
504         int i;
505         BACKENDACTIVECHECK
506
507         for (i = backendunits - 1;i >= 0;i--)
508         {
509                 if (qglActiveTexture)
510                         qglActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
511                 if (qglClientActiveTexture)
512                         qglClientActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
513                 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
514                 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
515                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
516                 if (gl_texture3d)
517                 {
518                         qglDisable(GL_TEXTURE_3D);CHECKGLERROR
519                 }
520                 if (gl_texturecubemap)
521                 {
522                         qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
523                 }
524                 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
525                 if (gl_combine.integer)
526                 {
527                         qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
528                 }
529         }
530         qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
531         qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
532
533         qglDisable(GL_BLEND);CHECKGLERROR
534         qglEnable(GL_DEPTH_TEST);CHECKGLERROR
535         qglDepthMask(GL_TRUE);CHECKGLERROR
536         qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);CHECKGLERROR
537 }
538
539 void R_Mesh_Matrix(const matrix4x4_t *matrix)
540 {
541         if (memcmp(matrix, &backend_modelmatrix, sizeof(matrix4x4_t)))
542         {
543                 backend_modelmatrix = *matrix;
544                 Matrix4x4_Concat(&backend_modelviewmatrix, &backend_viewmatrix, matrix);
545                 Matrix4x4_Transpose(&backend_glmodelviewmatrix, &backend_modelviewmatrix);
546                 qglLoadMatrixf(&backend_glmodelviewmatrix.m[0][0]);
547         }
548 }
549
550 // sets up the requested state
551 void R_Mesh_MainState(const rmeshstate_t *m)
552 {
553         BACKENDACTIVECHECK
554
555         if (gl_state.blendfunc1 != m->blendfunc1 || gl_state.blendfunc2 != m->blendfunc2)
556         {
557                 qglBlendFunc(gl_state.blendfunc1 = m->blendfunc1, gl_state.blendfunc2 = m->blendfunc2);CHECKGLERROR
558                 if (gl_state.blendfunc2 == GL_ZERO)
559                 {
560                         if (gl_state.blendfunc1 == GL_ONE)
561                         {
562                                 if (gl_state.blend)
563                                 {
564                                         gl_state.blend = 0;
565                                         qglDisable(GL_BLEND);CHECKGLERROR
566                                 }
567                         }
568                         else
569                         {
570                                 if (!gl_state.blend)
571                                 {
572                                         gl_state.blend = 1;
573                                         qglEnable(GL_BLEND);CHECKGLERROR
574                                 }
575                         }
576                 }
577                 else
578                 {
579                         if (!gl_state.blend)
580                         {
581                                 gl_state.blend = 1;
582                                 qglEnable(GL_BLEND);CHECKGLERROR
583                         }
584                 }
585         }
586         if (gl_state.depthdisable != m->depthdisable)
587         {
588                 gl_state.depthdisable = m->depthdisable;
589                 if (gl_state.depthdisable)
590                         qglDisable(GL_DEPTH_TEST);
591                 else
592                         qglEnable(GL_DEPTH_TEST);
593         }
594         if (gl_state.depthmask != (m->blendfunc2 == GL_ZERO || m->depthwrite))
595         {
596                 qglDepthMask(gl_state.depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite));CHECKGLERROR
597         }
598 }
599
600 void R_Mesh_TextureState(const rmeshstate_t *m)
601 {
602         int i;
603         float rgbscale;
604         gltextureunit_t *unit;
605
606         BACKENDACTIVECHECK
607
608         if (gl_backend_rebindtextures)
609         {
610                 gl_backend_rebindtextures = false;
611                 GL_SetupTextureState();
612         }
613
614         for (i = 0;i < backendunits;i++)
615         {
616                 unit = gl_state.units + i;
617                 if (unit->t1d != m->tex1d[i] || unit->t2d != m->tex[i] || unit->t3d != m->tex3d[i] || unit->tcubemap != m->texcubemap[i])
618                 {
619                         if (gl_state.unit != i)
620                         {
621                                 qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
622                         }
623                         if (m->tex1d[i] || m->tex[i] || m->tex3d[i] || m->texcubemap[i])
624                         {
625                                 if (!unit->arrayenabled)
626                                 {
627                                         unit->arrayenabled = true;
628                                         if (gl_state.clientunit != i)
629                                         {
630                                                 qglClientActiveTexture(GL_TEXTURE0_ARB + (gl_state.clientunit = i));CHECKGLERROR
631                                         }
632                                         qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
633                                 }
634                         }
635                         else
636                         {
637                                 if (unit->arrayenabled)
638                                 {
639                                         unit->arrayenabled = false;
640                                         if (gl_state.clientunit != i)
641                                         {
642                                                 qglClientActiveTexture(GL_TEXTURE0_ARB + (gl_state.clientunit = i));CHECKGLERROR
643                                         }
644                                         qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
645                                 }
646                         }
647                         if (unit->t1d != m->tex1d[i])
648                         {
649                                 if (m->tex1d[i])
650                                 {
651                                         if (unit->t1d == 0)
652                                                 qglEnable(GL_TEXTURE_1D);CHECKGLERROR
653                                 }
654                                 else
655                                 {
656                                         if (unit->t1d)
657                                                 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
658                                 }
659                                 qglBindTexture(GL_TEXTURE_1D, (unit->t1d = m->tex1d[i]));CHECKGLERROR
660                         }
661                         if (unit->t2d != m->tex[i])
662                         {
663                                 if (m->tex[i])
664                                 {
665                                         if (unit->t2d == 0)
666                                                 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
667                                 }
668                                 else
669                                 {
670                                         if (unit->t2d)
671                                                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
672                                 }
673                                 qglBindTexture(GL_TEXTURE_2D, (unit->t2d = m->tex[i]));CHECKGLERROR
674                         }
675                         if (unit->t3d != m->tex3d[i])
676                         {
677                                 if (m->tex3d[i])
678                                 {
679                                         if (unit->t3d == 0)
680                                                 qglEnable(GL_TEXTURE_3D);CHECKGLERROR
681                                 }
682                                 else
683                                 {
684                                         if (unit->t3d)
685                                                 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
686                                 }
687                                 qglBindTexture(GL_TEXTURE_3D, (unit->t3d = m->tex3d[i]));CHECKGLERROR
688                         }
689                         if (unit->tcubemap != m->texcubemap[i])
690                         {
691                                 if (m->texcubemap[i])
692                                 {
693                                         if (unit->tcubemap == 0)
694                                                 qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
695                                 }
696                                 else
697                                 {
698                                         if (unit->tcubemap)
699                                                 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
700                                 }
701                                 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, (unit->tcubemap = m->texcubemap[i]));CHECKGLERROR
702                         }
703                 }
704                 rgbscale = max(m->texrgbscale[i], 1);
705                 if (gl_state.units[i].rgbscale != rgbscale)
706                 {
707                         if (gl_state.unit != i)
708                         {
709                                 qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
710                         }
711                         qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (gl_state.units[i].rgbscale = rgbscale));CHECKGLERROR
712                 }
713         }
714 }
715
716 void R_Mesh_State(const rmeshstate_t *m)
717 {
718         R_Mesh_MainState(m);
719         R_Mesh_TextureState(m);
720 }
721
722 /*
723 ==============================================================================
724
725                                                 SCREEN SHOTS
726
727 ==============================================================================
728 */
729
730 qboolean SCR_ScreenShot(char *filename, int x, int y, int width, int height)
731 {
732         qboolean ret;
733         int i;
734         qbyte *buffer;
735
736         if (!r_render.integer)
737                 return false;
738
739         buffer = Mem_Alloc(tempmempool, width*height*3);
740         qglReadPixels (x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer);
741         CHECKGLERROR
742
743         // LordHavoc: compensate for v_overbrightbits when using hardware gamma
744         if (v_hwgamma.integer)
745                 for (i = 0;i < width * height * 3;i++)
746                         buffer[i] <<= v_overbrightbits.integer;
747
748         ret = Image_WriteTGARGB_preflipped(filename, width, height, buffer);
749
750         Mem_Free(buffer);
751         return ret;
752 }
753
754 //=============================================================================
755
756 void R_ClearScreen(void)
757 {
758         if (r_render.integer)
759         {
760                 // clear to black
761                 qglClearColor(0,0,0,0);CHECKGLERROR
762                 // clear the screen
763                 qglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);CHECKGLERROR
764                 // set dithering mode
765                 if (gl_dither.integer)
766                 {
767                         qglEnable(GL_DITHER);CHECKGLERROR
768                 }
769                 else
770                 {
771                         qglDisable(GL_DITHER);CHECKGLERROR
772                 }
773         }
774 }
775
776 /*
777 ==================
778 SCR_UpdateScreen
779
780 This is called every frame, and can also be called explicitly to flush
781 text to the screen.
782 ==================
783 */
784 void SCR_UpdateScreen (void)
785 {
786         VID_Finish ();
787
788         R_TimeReport("finish");
789
790         if (r_textureunits.integer > gl_textureunits)
791                 Cvar_SetValueQuick(&r_textureunits, gl_textureunits);
792         if (r_textureunits.integer < 1)
793                 Cvar_SetValueQuick(&r_textureunits, 1);
794
795         if (gl_combine.integer && (!gl_combine_extension || r_textureunits.integer < 2))
796                 Cvar_SetValueQuick(&gl_combine, 0);
797
798         // lighting scale
799         r_colorscale = 1.0f / (float) (1 << v_overbrightbits.integer);
800
801         // lightmaps only
802         r_lightmapscalebit = v_overbrightbits.integer;
803         if (gl_combine.integer && r_textureunits.integer > 1)
804                 r_lightmapscalebit += 2;
805
806         R_TimeReport("setup");
807
808         R_ClearScreen();
809
810         R_TimeReport("clear");
811
812         if (scr_conlines < vid.conheight && cls.signon == SIGNONS)
813                 R_RenderView();
814
815         // draw 2D stuff
816         R_DrawQueue();
817
818         // tell driver to commit it's partially full geometry queue to the rendering queue
819         // (this doesn't wait for the commands themselves to complete)
820         qglFlush();
821 }
822