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