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