fixed glOrtho call
[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[2]));
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 static struct
257 {
258         int blendfunc1;
259         int blendfunc2;
260         int blend;
261         GLboolean depthmask;
262         int depthdisable;
263         int unit;
264         int clientunit;
265         int texture[MAX_TEXTUREUNITS];
266         float texturergbscale[MAX_TEXTUREUNITS];
267 }
268 gl_state;
269
270 void GL_SetupTextureState(void)
271 {
272         int i;
273         if (backendunits > 1)
274         {
275                 for (i = 0;i < backendunits;i++)
276                 {
277                         qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
278                         qglBindTexture(GL_TEXTURE_2D, gl_state.texture[i]);CHECKGLERROR
279                         if (gl_combine.integer)
280                         {
281                                 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);CHECKGLERROR
282                                 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);CHECKGLERROR
283                                 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);CHECKGLERROR
284                                 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
285                                 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_CONSTANT_ARB);CHECKGLERROR
286                                 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
287                                 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
288                                 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);CHECKGLERROR
289                                 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);CHECKGLERROR
290                                 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);CHECKGLERROR
291                                 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
292                                 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, GL_CONSTANT_ARB);CHECKGLERROR
293                                 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
294                                 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
295                                 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
296                                 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, gl_state.texturergbscale[i]);CHECKGLERROR
297                                 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
298                         }
299                         else
300                         {
301                                 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
302                         }
303                         if (gl_state.texture[i])
304                         {
305                                 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
306                         }
307                         else
308                         {
309                                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
310                         }
311                         qglClientActiveTexture(GL_TEXTURE0_ARB + (gl_state.clientunit = i));CHECKGLERROR
312                         qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), varray_texcoord[i]);CHECKGLERROR
313                         if (gl_state.texture[i])
314                         {
315                                 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
316                         }
317                         else
318                         {
319                                 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
320                         }
321                 }
322         }
323         else
324         {
325                 qglBindTexture(GL_TEXTURE_2D, gl_state.texture[0]);CHECKGLERROR
326                 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
327                 if (gl_state.texture[0])
328                 {
329                         qglEnable(GL_TEXTURE_2D);CHECKGLERROR
330                 }
331                 else
332                 {
333                         qglDisable(GL_TEXTURE_2D);CHECKGLERROR
334                 }
335                 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), varray_texcoord[0]);CHECKGLERROR
336                 if (gl_state.texture[0])
337                 {
338                         qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
339                 }
340                 else
341                 {
342                         qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
343                 }
344         }
345 }
346
347 int usedarrays;
348 void GL_Backend_ResetState(void)
349 {
350         int i;
351         gl_state.unit = 0;
352         gl_state.clientunit = 0;
353
354         for (i = 0;i < backendunits;i++)
355         {
356                 gl_state.texture[i] = 0;
357                 gl_state.texturergbscale[i] = 1;
358         }
359
360         qglEnable(GL_CULL_FACE);CHECKGLERROR
361         qglCullFace(GL_FRONT);CHECKGLERROR
362
363         gl_state.depthdisable = false;
364         qglEnable(GL_DEPTH_TEST);CHECKGLERROR
365
366         gl_state.blendfunc1 = GL_ONE;
367         gl_state.blendfunc2 = GL_ZERO;
368         qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
369
370         gl_state.blend = 0;
371         qglDisable(GL_BLEND);CHECKGLERROR
372
373         gl_state.depthmask = GL_TRUE;
374         qglDepthMask(gl_state.depthmask);CHECKGLERROR
375
376         usedarrays = false;
377         usedarrays = true;
378         qglVertexPointer(3, GL_FLOAT, sizeof(float[4]), varray_vertex);CHECKGLERROR
379         qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
380         if (gl_mesh_floatcolors.integer)
381         {
382                 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), varray_color);CHECKGLERROR
383         }
384         else
385         {
386                 qglColorPointer(4, GL_UNSIGNED_BYTE, sizeof(qbyte[4]), varray_bcolor);CHECKGLERROR
387         }
388         qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
389
390         GL_SetupTextureState();
391 }
392
393 // called at beginning of frame
394 void R_Mesh_Start(void)
395 {
396         BACKENDACTIVECHECK
397
398         CHECKGLERROR
399
400         GL_Backend_CheckCvars();
401         if (mesh_maxverts != gl_mesh_maxverts.integer)
402                 GL_Backend_ResizeArrays(gl_mesh_maxverts.integer);
403
404         GL_Backend_ResetState();
405 }
406
407 int gl_backend_rebindtextures;
408
409 void GL_ConvertColorsFloatToByte(int numverts)
410 {
411         int i, k, total;
412         // LordHavoc: to avoid problems with aliasing (treating memory as two
413         // different types - exactly what this is doing), these must be volatile
414         // (or a union)
415         volatile int *icolor;
416         volatile float *fcolor;
417         qbyte *bcolor;
418
419         total = numverts * 4;
420
421         // shift float to have 8bit fraction at base of number
422         fcolor = varray_color;
423         for (i = 0;i < total;)
424         {
425                 fcolor[i    ] += 32768.0f;
426                 fcolor[i + 1] += 32768.0f;
427                 fcolor[i + 2] += 32768.0f;
428                 fcolor[i + 3] += 32768.0f;
429                 i += 4;
430         }
431
432         // then read as integer and kill float bits...
433         icolor = (int *)varray_color;
434         bcolor = varray_bcolor;
435         for (i = 0;i < total;)
436         {
437                 k = icolor[i    ] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i    ] = (qbyte) k;
438                 k = icolor[i + 1] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i + 1] = (qbyte) k;
439                 k = icolor[i + 2] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i + 2] = (qbyte) k;
440                 k = icolor[i + 3] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i + 3] = (qbyte) k;
441                 i += 4;
442         }
443 }
444
445 /*
446 void GL_TransformVertices(int numverts)
447 {
448         int i;
449         float m[12], tempv[4], *v;
450         m[0] = backendmatrix.m[0][0];
451         m[1] = backendmatrix.m[0][1];
452         m[2] = backendmatrix.m[0][2];
453         m[3] = backendmatrix.m[0][3];
454         m[4] = backendmatrix.m[1][0];
455         m[5] = backendmatrix.m[1][1];
456         m[6] = backendmatrix.m[1][2];
457         m[7] = backendmatrix.m[1][3];
458         m[8] = backendmatrix.m[2][0];
459         m[9] = backendmatrix.m[2][1];
460         m[10] = backendmatrix.m[2][2];
461         m[11] = backendmatrix.m[2][3];
462         for (i = 0, v = varray_vertex;i < numverts;i++, v += 4)
463         {
464                 VectorCopy(v, tempv);
465                 v[0] = tempv[0] * m[0] + tempv[1] * m[1] + tempv[2] * m[2] + m[3];
466                 v[1] = tempv[0] * m[4] + tempv[1] * m[5] + tempv[2] * m[6] + m[7];
467                 v[2] = tempv[0] * m[8] + tempv[1] * m[9] + tempv[2] * m[10] + m[11];
468         }
469 }
470 */
471
472 void GL_DrawRangeElements(int firstvert, int endvert, int indexcount, int *index)
473 {
474         int arraylocked = false;
475         c_meshs++;
476         c_meshelements += indexcount;
477         if (gl_supportslockarrays && gl_lockarrays.integer)
478         {
479                 qglLockArraysEXT(firstvert, endvert - firstvert);
480                 CHECKGLERROR
481                 arraylocked = true;
482         }
483         if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
484                 qglDrawRangeElements(GL_TRIANGLES, firstvert, endvert, indexcount, GL_UNSIGNED_INT, (GLuint *) index);
485         else
486                 qglDrawElements(GL_TRIANGLES, indexcount, GL_UNSIGNED_INT, (GLuint *) index);
487         CHECKGLERROR
488         if (arraylocked)
489         {
490                 qglUnlockArraysEXT();
491                 CHECKGLERROR
492                 arraylocked = false;
493         }
494 }
495
496 // enlarges geometry buffers if they are too small
497 void _R_Mesh_ResizeCheck(int numverts)
498 {
499         if (numverts > mesh_maxverts)
500         {
501                 BACKENDACTIVECHECK
502                 GL_Backend_ResizeArrays(numverts + 100);
503                 GL_Backend_ResetState();
504         }
505 }
506
507 // renders the mesh
508 void R_Mesh_Draw(int numverts, int numtriangles, int *elements)
509 {
510         BACKENDACTIVECHECK
511
512         CHECKGLERROR
513
514         if (!gl_mesh_floatcolors.integer)
515                 GL_ConvertColorsFloatToByte(numverts);
516         //GL_TransformVertices(numverts);
517         if (!r_render.integer)
518                 return;
519         GL_DrawRangeElements(0, numverts, numtriangles * 3, elements);
520 }
521
522 // restores backend state, used when done with 3D rendering
523 void R_Mesh_Finish(void)
524 {
525         int i;
526         BACKENDACTIVECHECK
527
528         if (backendunits > 1)
529         {
530                 for (i = backendunits - 1;i >= 0;i--)
531                 {
532                         qglActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
533                         qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
534                         if (gl_combine.integer)
535                         {
536                                 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
537                         }
538                         if (i > 0)
539                         {
540                                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
541                         }
542                         else
543                         {
544                                 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
545                         }
546                         qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
547
548                         if (usedarrays)
549                         {
550                                 qglClientActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
551                                 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
552                         }
553                 }
554         }
555         else
556         {
557                 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
558                 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
559                 if (usedarrays)
560                 {
561                         qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
562                 }
563         }
564         if (usedarrays)
565         {
566                 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
567                 qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
568         }
569
570         qglDisable(GL_BLEND);CHECKGLERROR
571         qglEnable(GL_DEPTH_TEST);CHECKGLERROR
572         qglDepthMask(GL_TRUE);CHECKGLERROR
573         qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);CHECKGLERROR
574 }
575
576 void GL_DepthFunc(int value)
577 {
578         if (!r_render.integer)
579                 return;
580
581         qglDepthFunc (value);
582         CHECKGLERROR
583 }
584
585 void GL_ClearDepth(void)
586 {
587         BACKENDACTIVECHECK
588
589         if (!r_render.integer)
590                 return;
591
592         qglClear(GL_DEPTH_BUFFER_BIT);
593         CHECKGLERROR
594 }
595
596 void R_Mesh_Matrix(const matrix4x4_t *matrix)
597 {
598         if (memcmp(matrix, &backend_modelmatrix, sizeof(matrix4x4_t)))
599         {
600                 backend_modelmatrix = *matrix;
601                 Matrix4x4_Concat(&backend_modelviewmatrix, &backend_viewmatrix, matrix);
602                 Matrix4x4_Transpose(&backend_glmodelviewmatrix, &backend_modelviewmatrix);
603                 qglLoadMatrixf(&backend_glmodelviewmatrix.m[0][0]);
604         }
605 }
606
607 // sets up the requested state
608 void R_Mesh_MainState(const rmeshstate_t *m)
609 {
610         BACKENDACTIVECHECK
611
612         if (gl_state.blendfunc1 != m->blendfunc1 || gl_state.blendfunc2 != m->blendfunc2)
613         {
614                 qglBlendFunc(gl_state.blendfunc1 = m->blendfunc1, gl_state.blendfunc2 = m->blendfunc2);CHECKGLERROR
615                 if (gl_state.blendfunc2 == GL_ZERO)
616                 {
617                         if (gl_state.blendfunc1 == GL_ONE)
618                         {
619                                 if (gl_state.blend)
620                                 {
621                                         gl_state.blend = 0;
622                                         qglDisable(GL_BLEND);CHECKGLERROR
623                                 }
624                         }
625                         else
626                         {
627                                 if (!gl_state.blend)
628                                 {
629                                         gl_state.blend = 1;
630                                         qglEnable(GL_BLEND);CHECKGLERROR
631                                 }
632                         }
633                 }
634                 else
635                 {
636                         if (!gl_state.blend)
637                         {
638                                 gl_state.blend = 1;
639                                 qglEnable(GL_BLEND);CHECKGLERROR
640                         }
641                 }
642         }
643         if (gl_state.depthdisable != m->depthdisable)
644         {
645                 gl_state.depthdisable = m->depthdisable;
646                 if (gl_state.depthdisable)
647                         qglDisable(GL_DEPTH_TEST);
648                 else
649                         qglEnable(GL_DEPTH_TEST);
650         }
651         if (gl_state.depthmask != (m->blendfunc2 == GL_ZERO || m->depthwrite))
652         {
653                 qglDepthMask(gl_state.depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite));CHECKGLERROR
654         }
655 }
656
657 void R_Mesh_TextureState(const rmeshstate_t *m)
658 {
659         int i;
660         int texturergbscale[MAX_TEXTUREUNITS];
661
662         BACKENDACTIVECHECK
663
664         if (gl_backend_rebindtextures)
665         {
666                 gl_backend_rebindtextures = false;
667                 GL_SetupTextureState();
668         }
669
670         for (i = 0;i < backendunits;i++)
671         {
672                 if (m->texrgbscale[i])
673                         texturergbscale[i] = m->texrgbscale[i];
674                 else
675                         texturergbscale[i] = 1;
676         }
677
678         if (backendunits > 1)
679         {
680                 for (i = 0;i < backendunits;i++)
681                 {
682                         if (gl_state.texture[i] != m->tex[i])
683                         {
684                                 if (gl_state.unit != i)
685                                 {
686                                         qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
687                                 }
688                                 if (gl_state.texture[i] == 0)
689                                 {
690                                         qglEnable(GL_TEXTURE_2D);CHECKGLERROR
691                                         if (gl_state.clientunit != i)
692                                         {
693                                                 qglClientActiveTexture(GL_TEXTURE0_ARB + (gl_state.clientunit = i));CHECKGLERROR
694                                         }
695                                         qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
696                                 }
697                                 qglBindTexture(GL_TEXTURE_2D, (gl_state.texture[i] = m->tex[i]));CHECKGLERROR
698                                 if (gl_state.texture[i] == 0)
699                                 {
700                                         qglDisable(GL_TEXTURE_2D);CHECKGLERROR
701                                         if (gl_state.clientunit != i)
702                                         {
703                                                 qglClientActiveTexture(GL_TEXTURE0_ARB + (gl_state.clientunit = i));CHECKGLERROR
704                                         }
705                                         qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
706                                 }
707                         }
708                         if (gl_state.texturergbscale[i] != texturergbscale[i])
709                         {
710                                 if (gl_state.unit != i)
711                                 {
712                                         qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
713                                 }
714                                 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (gl_state.texturergbscale[i] = texturergbscale[i]));CHECKGLERROR
715                         }
716                 }
717         }
718         else
719         {
720                 if (gl_state.texture[0] != m->tex[0])
721                 {
722                         if (gl_state.texture[0] == 0)
723                         {
724                                 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
725                                 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
726                         }
727                         qglBindTexture(GL_TEXTURE_2D, (gl_state.texture[0] = m->tex[0]));CHECKGLERROR
728                         if (gl_state.texture[0] == 0)
729                         {
730                                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
731                                 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
732                         }
733                 }
734         }
735 }
736
737 void R_Mesh_State(const rmeshstate_t *m)
738 {
739         R_Mesh_MainState(m);
740         R_Mesh_TextureState(m);
741 }
742
743 /*
744 ==============================================================================
745
746                                                 SCREEN SHOTS
747
748 ==============================================================================
749 */
750
751 qboolean SCR_ScreenShot(char *filename, int x, int y, int width, int height)
752 {
753         qboolean ret;
754         int i;
755         qbyte *buffer;
756
757         if (!r_render.integer)
758                 return false;
759
760         buffer = Mem_Alloc(tempmempool, width*height*3);
761         qglReadPixels (x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer);
762         CHECKGLERROR
763
764         // LordHavoc: compensate for v_overbrightbits when using hardware gamma
765         if (v_hwgamma.integer)
766                 for (i = 0;i < width * height * 3;i++)
767                         buffer[i] <<= v_overbrightbits.integer;
768
769         ret = Image_WriteTGARGB_preflipped(filename, width, height, buffer);
770
771         Mem_Free(buffer);
772         return ret;
773 }
774
775 //=============================================================================
776
777 void R_ClearScreen(void)
778 {
779         if (r_render.integer)
780         {
781                 // clear to black
782                 qglClearColor(0,0,0,0);CHECKGLERROR
783                 // clear the screen
784                 qglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);CHECKGLERROR
785                 // set dithering mode
786                 if (gl_dither.integer)
787                 {
788                         qglEnable(GL_DITHER);CHECKGLERROR
789                 }
790                 else
791                 {
792                         qglDisable(GL_DITHER);CHECKGLERROR
793                 }
794         }
795 }
796
797 /*
798 ==================
799 SCR_UpdateScreen
800
801 This is called every frame, and can also be called explicitly to flush
802 text to the screen.
803 ==================
804 */
805 void SCR_UpdateScreen (void)
806 {
807         VID_Finish ();
808
809         R_TimeReport("finish");
810
811         if (r_textureunits.integer > gl_textureunits)
812                 Cvar_SetValueQuick(&r_textureunits, gl_textureunits);
813         if (r_textureunits.integer < 1)
814                 Cvar_SetValueQuick(&r_textureunits, 1);
815
816         if (gl_combine.integer && (!gl_combine_extension || r_textureunits.integer < 2))
817                 Cvar_SetValueQuick(&gl_combine, 0);
818
819         // lighting scale
820         r_colorscale = 1.0f / (float) (1 << v_overbrightbits.integer);
821
822         // lightmaps only
823         r_lightmapscalebit = v_overbrightbits.integer;
824         if (gl_combine.integer && r_textureunits.integer > 1)
825                 r_lightmapscalebit += 2;
826
827         R_TimeReport("setup");
828
829         R_ClearScreen();
830
831         R_TimeReport("clear");
832
833         if (scr_conlines < vid.conheight && cls.signon == SIGNONS)
834                 R_RenderView();
835
836         // draw 2D stuff
837         R_DrawQueue();
838
839         // tell driver to commit it's partially full geometry queue to the rendering queue
840         // (this doesn't wait for the commands themselves to complete)
841         qglFlush();
842 }
843