added HDR bloom feature, not very different from bloom but looks better, runs a bit...
[divverent/darkplaces.git] / gl_backend.c
1
2 #include "quakedef.h"
3 #include "cl_collision.h"
4
5 cvar_t gl_mesh_drawrangeelements = {0, "gl_mesh_drawrangeelements", "1", "use glDrawRangeElements function if available instead of glDrawElements (for performance comparisons or bug testing)"};
6 cvar_t gl_mesh_testarrayelement = {0, "gl_mesh_testarrayelement", "0", "use glBegin(GL_TRIANGLES);glArrayElement();glEnd(); primitives instead of glDrawElements (useful to test for driver bugs with glDrawElements)"};
7 cvar_t gl_mesh_testmanualfeeding = {0, "gl_mesh_testmanualfeeding", "0", "use glBegin(GL_TRIANGLES);glTexCoord2f();glVertex3f();glEnd(); primitives instead of glDrawElements (useful to test for driver bugs with glDrawElements)"};
8 cvar_t gl_paranoid = {0, "gl_paranoid", "0", "enables OpenGL error checking and other tests"};
9 cvar_t gl_printcheckerror = {0, "gl_printcheckerror", "0", "prints all OpenGL error checks, useful to identify location of driver crashes"};
10
11 cvar_t r_render = {0, "r_render", "1", "enables rendering calls (you want this on!)"};
12 cvar_t r_waterwarp = {CVAR_SAVE, "r_waterwarp", "1", "warp view while underwater"};
13 cvar_t gl_polyblend = {CVAR_SAVE, "gl_polyblend", "1", "tints view while underwater, hurt, etc"};
14 cvar_t gl_dither = {CVAR_SAVE, "gl_dither", "1", "enables OpenGL dithering (16bit looks bad with this off)"};
15 cvar_t gl_lockarrays = {0, "gl_lockarrays", "1", "enables use of glLockArraysEXT, may cause glitches with some broken drivers"};
16
17 int gl_maxdrawrangeelementsvertices;
18 int gl_maxdrawrangeelementsindices;
19
20 #ifdef DEBUGGL
21 int errornumber = 0;
22
23 void GL_PrintError(int errornumber, char *filename, int linenumber)
24 {
25         switch(errornumber)
26         {
27 #ifdef GL_INVALID_ENUM
28         case GL_INVALID_ENUM:
29                 Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
30                 break;
31 #endif
32 #ifdef GL_INVALID_VALUE
33         case GL_INVALID_VALUE:
34                 Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
35                 break;
36 #endif
37 #ifdef GL_INVALID_OPERATION
38         case GL_INVALID_OPERATION:
39                 Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
40                 break;
41 #endif
42 #ifdef GL_STACK_OVERFLOW
43         case GL_STACK_OVERFLOW:
44                 Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
45                 break;
46 #endif
47 #ifdef GL_STACK_UNDERFLOW
48         case GL_STACK_UNDERFLOW:
49                 Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
50                 break;
51 #endif
52 #ifdef GL_OUT_OF_MEMORY
53         case GL_OUT_OF_MEMORY:
54                 Con_Printf("GL_OUT_OF_MEMORY at %s:%i\n", filename, linenumber);
55                 break;
56 #endif
57 #ifdef GL_TABLE_TOO_LARGE
58         case GL_TABLE_TOO_LARGE:
59                 Con_Printf("GL_TABLE_TOO_LARGE at %s:%i\n", filename, linenumber);
60                 break;
61 #endif
62         default:
63                 Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
64                 break;
65         }
66 }
67 #endif
68
69 #define BACKENDACTIVECHECK if (!backendactive) Sys_Error("GL backend function called when backend is not active");
70
71 void SCR_ScreenShot_f (void);
72
73 static matrix4x4_t backend_viewmatrix;
74 static matrix4x4_t backend_modelmatrix;
75 static matrix4x4_t backend_modelviewmatrix;
76 static matrix4x4_t backend_glmodelviewmatrix;
77 static matrix4x4_t backend_projectmatrix;
78
79 static unsigned int backendunits, backendimageunits, backendarrayunits, backendactive;
80
81 /*
82 note: here's strip order for a terrain row:
83 0--1--2--3--4
84 |\ |\ |\ |\ |
85 | \| \| \| \|
86 A--B--C--D--E
87 clockwise
88
89 A0B, 01B, B1C, 12C, C2D, 23D, D3E, 34E
90
91 *elements++ = i + row;
92 *elements++ = i;
93 *elements++ = i + row + 1;
94 *elements++ = i;
95 *elements++ = i + 1;
96 *elements++ = i + row + 1;
97
98
99 for (y = 0;y < rows - 1;y++)
100 {
101         for (x = 0;x < columns - 1;x++)
102         {
103                 i = y * rows + x;
104                 *elements++ = i + columns;
105                 *elements++ = i;
106                 *elements++ = i + columns + 1;
107                 *elements++ = i;
108                 *elements++ = i + 1;
109                 *elements++ = i + columns + 1;
110         }
111 }
112
113 alternative:
114 0--1--2--3--4
115 | /| /|\ | /|
116 |/ |/ | \|/ |
117 A--B--C--D--E
118 counterclockwise
119
120 for (y = 0;y < rows - 1;y++)
121 {
122         for (x = 0;x < columns - 1;x++)
123         {
124                 i = y * rows + x;
125                 *elements++ = i;
126                 *elements++ = i + columns;
127                 *elements++ = i + columns + 1;
128                 *elements++ = i + columns;
129                 *elements++ = i + columns + 1;
130                 *elements++ = i + 1;
131         }
132 }
133 */
134
135 int polygonelements[(POLYGONELEMENTS_MAXPOINTS-2)*3];
136 int quadelements[QUADELEMENTS_MAXQUADS*6];
137
138 void GL_Backend_AllocArrays(void)
139 {
140 }
141
142 void GL_Backend_FreeArrays(void)
143 {
144 }
145
146 static void gl_backend_start(void)
147 {
148         Con_Print("OpenGL Backend starting...\n");
149         CHECKGLERROR
150
151         if (qglDrawRangeElements != NULL)
152         {
153                 CHECKGLERROR
154                 qglGetIntegerv(GL_MAX_ELEMENTS_VERTICES, &gl_maxdrawrangeelementsvertices);
155                 CHECKGLERROR
156                 qglGetIntegerv(GL_MAX_ELEMENTS_INDICES, &gl_maxdrawrangeelementsindices);
157                 CHECKGLERROR
158                 Con_Printf("glDrawRangeElements detected (max vertices %i, max indices %i)\n", gl_maxdrawrangeelementsvertices, gl_maxdrawrangeelementsindices);
159         }
160
161         backendunits = min(MAX_TEXTUREUNITS, gl_textureunits);
162         backendimageunits = backendunits;
163         backendarrayunits = backendunits;
164         if (gl_support_fragment_shader)
165         {
166                 CHECKGLERROR
167                 qglGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS_ARB, (int *)&backendimageunits);
168                 CHECKGLERROR
169                 qglGetIntegerv(GL_MAX_TEXTURE_COORDS_ARB, (int *)&backendarrayunits);
170                 CHECKGLERROR
171                 Con_Printf("GLSL shader support detected: texture units = %i texenv, %i image, %i array\n", backendunits, backendimageunits, backendarrayunits);
172         }
173         else if (backendunits > 1)
174                 Con_Printf("multitexture detected: texture units = %i\n", backendunits);
175         else
176                 Con_Printf("singletexture\n");
177
178         GL_Backend_AllocArrays();
179
180         Con_Printf("OpenGL backend started.\n");
181
182         CHECKGLERROR
183
184         backendactive = true;
185 }
186
187 static void gl_backend_shutdown(void)
188 {
189         backendunits = 0;
190         backendimageunits = 0;
191         backendarrayunits = 0;
192         backendactive = false;
193
194         Con_Print("OpenGL Backend shutting down\n");
195
196         GL_Backend_FreeArrays();
197 }
198
199 static void gl_backend_newmap(void)
200 {
201 }
202
203 void gl_backend_init(void)
204 {
205         int i;
206
207         for (i = 0;i < POLYGONELEMENTS_MAXPOINTS - 2;i++)
208         {
209                 polygonelements[i * 3 + 0] = 0;
210                 polygonelements[i * 3 + 1] = i + 1;
211                 polygonelements[i * 3 + 2] = i + 2;
212         }
213         // elements for rendering a series of quads as triangles
214         for (i = 0;i < QUADELEMENTS_MAXQUADS;i++)
215         {
216                 quadelements[i * 6 + 0] = i * 4;
217                 quadelements[i * 6 + 1] = i * 4 + 1;
218                 quadelements[i * 6 + 2] = i * 4 + 2;
219                 quadelements[i * 6 + 3] = i * 4;
220                 quadelements[i * 6 + 4] = i * 4 + 2;
221                 quadelements[i * 6 + 5] = i * 4 + 3;
222         }
223
224         Cvar_RegisterVariable(&r_render);
225         Cvar_RegisterVariable(&r_waterwarp);
226         Cvar_RegisterVariable(&gl_polyblend);
227         Cvar_RegisterVariable(&gl_dither);
228         Cvar_RegisterVariable(&gl_lockarrays);
229         Cvar_RegisterVariable(&gl_paranoid);
230         Cvar_RegisterVariable(&gl_printcheckerror);
231 #ifdef NORENDER
232         Cvar_SetValue("r_render", 0);
233 #endif
234
235         Cvar_RegisterVariable(&gl_mesh_drawrangeelements);
236         Cvar_RegisterVariable(&gl_mesh_testarrayelement);
237         Cvar_RegisterVariable(&gl_mesh_testmanualfeeding);
238
239         R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap);
240 }
241
242 void GL_SetupView_Orientation_Identity (void)
243 {
244         backend_viewmatrix = identitymatrix;
245         memset(&backend_modelmatrix, 0, sizeof(backend_modelmatrix));
246 }
247
248 void GL_SetupView_Orientation_FromEntity(matrix4x4_t *matrix)
249 {
250         matrix4x4_t tempmatrix, basematrix;
251         Matrix4x4_Invert_Simple(&tempmatrix, matrix);
252         Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
253         Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
254         Matrix4x4_Concat(&backend_viewmatrix, &basematrix, &tempmatrix);
255         //Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[2], 1, 0, 0);
256         //Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[0], 0, 1, 0);
257         //Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[1], 0, 0, 1);
258         //Matrix4x4_ConcatTranslate(&backend_viewmatrix, -origin[0], -origin[1], -origin[2]);
259         memset(&backend_modelmatrix, 0, sizeof(backend_modelmatrix));
260 }
261
262 void GL_SetupView_Mode_Perspective (double frustumx, double frustumy, double zNear, double zFar)
263 {
264         double m[16];
265
266         // set up viewpoint
267         CHECKGLERROR
268         qglMatrixMode(GL_PROJECTION);CHECKGLERROR
269         qglLoadIdentity();CHECKGLERROR
270         // set view pyramid
271         qglFrustum(-frustumx * zNear, frustumx * zNear, -frustumy * zNear, frustumy * zNear, zNear, zFar);CHECKGLERROR
272         qglGetDoublev(GL_PROJECTION_MATRIX, m);CHECKGLERROR
273         backend_projectmatrix.m[0][0] = m[0];
274         backend_projectmatrix.m[1][0] = m[1];
275         backend_projectmatrix.m[2][0] = m[2];
276         backend_projectmatrix.m[3][0] = m[3];
277         backend_projectmatrix.m[0][1] = m[4];
278         backend_projectmatrix.m[1][1] = m[5];
279         backend_projectmatrix.m[2][1] = m[6];
280         backend_projectmatrix.m[3][1] = m[7];
281         backend_projectmatrix.m[0][2] = m[8];
282         backend_projectmatrix.m[1][2] = m[9];
283         backend_projectmatrix.m[2][2] = m[10];
284         backend_projectmatrix.m[3][2] = m[11];
285         backend_projectmatrix.m[0][3] = m[12];
286         backend_projectmatrix.m[1][3] = m[13];
287         backend_projectmatrix.m[2][3] = m[14];
288         backend_projectmatrix.m[3][3] = m[15];
289         qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
290         GL_SetupView_Orientation_Identity();
291         CHECKGLERROR
292 }
293
294 void GL_SetupView_Mode_PerspectiveInfiniteFarClip (double frustumx, double frustumy, double zNear)
295 {
296         double nudge, m[16];
297
298         // set up viewpoint
299         CHECKGLERROR
300         qglMatrixMode(GL_PROJECTION);CHECKGLERROR
301         qglLoadIdentity();CHECKGLERROR
302         // set view pyramid
303         nudge = 1.0 - 1.0 / (1<<23);
304         m[ 0] = 1.0 / frustumx;
305         m[ 1] = 0;
306         m[ 2] = 0;
307         m[ 3] = 0;
308         m[ 4] = 0;
309         m[ 5] = 1.0 / frustumy;
310         m[ 6] = 0;
311         m[ 7] = 0;
312         m[ 8] = 0;
313         m[ 9] = 0;
314         m[10] = -nudge;
315         m[11] = -1;
316         m[12] = 0;
317         m[13] = 0;
318         m[14] = -2 * zNear * nudge;
319         m[15] = 0;
320         qglLoadMatrixd(m);CHECKGLERROR
321         qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
322         GL_SetupView_Orientation_Identity();
323         CHECKGLERROR
324         backend_projectmatrix.m[0][0] = m[0];
325         backend_projectmatrix.m[1][0] = m[1];
326         backend_projectmatrix.m[2][0] = m[2];
327         backend_projectmatrix.m[3][0] = m[3];
328         backend_projectmatrix.m[0][1] = m[4];
329         backend_projectmatrix.m[1][1] = m[5];
330         backend_projectmatrix.m[2][1] = m[6];
331         backend_projectmatrix.m[3][1] = m[7];
332         backend_projectmatrix.m[0][2] = m[8];
333         backend_projectmatrix.m[1][2] = m[9];
334         backend_projectmatrix.m[2][2] = m[10];
335         backend_projectmatrix.m[3][2] = m[11];
336         backend_projectmatrix.m[0][3] = m[12];
337         backend_projectmatrix.m[1][3] = m[13];
338         backend_projectmatrix.m[2][3] = m[14];
339         backend_projectmatrix.m[3][3] = m[15];
340 }
341
342 void GL_SetupView_Mode_Ortho (double x1, double y1, double x2, double y2, double zNear, double zFar)
343 {
344         double m[16];
345
346         // set up viewpoint
347         CHECKGLERROR
348         qglMatrixMode(GL_PROJECTION);CHECKGLERROR
349         qglLoadIdentity();CHECKGLERROR
350         qglOrtho(x1, x2, y2, y1, zNear, zFar);CHECKGLERROR
351         qglGetDoublev(GL_PROJECTION_MATRIX, m);CHECKGLERROR
352         backend_projectmatrix.m[0][0] = m[0];
353         backend_projectmatrix.m[1][0] = m[1];
354         backend_projectmatrix.m[2][0] = m[2];
355         backend_projectmatrix.m[3][0] = m[3];
356         backend_projectmatrix.m[0][1] = m[4];
357         backend_projectmatrix.m[1][1] = m[5];
358         backend_projectmatrix.m[2][1] = m[6];
359         backend_projectmatrix.m[3][1] = m[7];
360         backend_projectmatrix.m[0][2] = m[8];
361         backend_projectmatrix.m[1][2] = m[9];
362         backend_projectmatrix.m[2][2] = m[10];
363         backend_projectmatrix.m[3][2] = m[11];
364         backend_projectmatrix.m[0][3] = m[12];
365         backend_projectmatrix.m[1][3] = m[13];
366         backend_projectmatrix.m[2][3] = m[14];
367         backend_projectmatrix.m[3][3] = m[15];
368         qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
369         GL_SetupView_Orientation_Identity();
370         CHECKGLERROR
371 }
372
373 typedef struct gltextureunit_s
374 {
375         int t1d, t2d, t3d, tcubemap;
376         int arrayenabled;
377         unsigned int arraycomponents;
378         const void *pointer_texcoord;
379         int rgbscale, alphascale;
380         int combinergb, combinealpha;
381         // FIXME: add more combine stuff
382         // texmatrixenabled exists only to avoid unnecessary texmatrix compares
383         int texmatrixenabled;
384         matrix4x4_t matrix;
385 }
386 gltextureunit_t;
387
388 static struct gl_state_s
389 {
390         int blendfunc1;
391         int blendfunc2;
392         int blend;
393         GLboolean depthmask;
394         int colormask; // stored as bottom 4 bits: r g b a (3 2 1 0 order)
395         int depthtest;
396         int alphatest;
397         int scissortest;
398         unsigned int unit;
399         unsigned int clientunit;
400         gltextureunit_t units[MAX_TEXTUREUNITS];
401         float color4f[4];
402         int lockrange_first;
403         int lockrange_count;
404         const void *pointer_vertex;
405         const void *pointer_color;
406 }
407 gl_state;
408
409 void GL_SetupTextureState(void)
410 {
411         unsigned int i;
412         gltextureunit_t *unit;
413         CHECKGLERROR
414         gl_state.unit = MAX_TEXTUREUNITS;
415         gl_state.clientunit = MAX_TEXTUREUNITS;
416         for (i = 0;i < MAX_TEXTUREUNITS;i++)
417         {
418                 unit = gl_state.units + i;
419                 unit->t1d = 0;
420                 unit->t2d = 0;
421                 unit->t3d = 0;
422                 unit->tcubemap = 0;
423                 unit->arrayenabled = false;
424                 unit->arraycomponents = 0;
425                 unit->pointer_texcoord = NULL;
426                 unit->rgbscale = 1;
427                 unit->alphascale = 1;
428                 unit->combinergb = GL_MODULATE;
429                 unit->combinealpha = GL_MODULATE;
430                 unit->texmatrixenabled = false;
431                 unit->matrix = identitymatrix;
432         }
433
434         for (i = 0;i < backendimageunits;i++)
435         {
436                 GL_ActiveTexture(i);
437                 qglBindTexture(GL_TEXTURE_1D, 0);CHECKGLERROR
438                 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
439                 if (gl_texture3d)
440                 {
441                         qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
442                 }
443                 if (gl_texturecubemap)
444                 {
445                         qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
446                 }
447         }
448
449         for (i = 0;i < backendarrayunits;i++)
450         {
451                 GL_ClientActiveTexture(i);
452                 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
453                 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
454         }
455
456         for (i = 0;i < backendunits;i++)
457         {
458                 GL_ActiveTexture(i);
459                 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
460                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
461                 if (gl_texture3d)
462                 {
463                         qglDisable(GL_TEXTURE_3D);CHECKGLERROR
464                 }
465                 if (gl_texturecubemap)
466                 {
467                         qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
468                 }
469                 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
470                 qglLoadIdentity();CHECKGLERROR
471                 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
472                 if (gl_combine.integer)
473                 {
474                         qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);CHECKGLERROR
475                         qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);CHECKGLERROR
476                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);CHECKGLERROR
477                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
478                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_TEXTURE);CHECKGLERROR // for GL_INTERPOLATE_ARB mode
479                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
480                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
481                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);CHECKGLERROR
482                         qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);CHECKGLERROR
483                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);CHECKGLERROR
484                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
485                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, GL_CONSTANT_ARB);CHECKGLERROR
486                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
487                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
488                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
489                         qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
490                         qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
491                 }
492                 else
493                 {
494                         qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
495                 }
496                 CHECKGLERROR
497         }
498         CHECKGLERROR
499 }
500
501 void GL_Backend_ResetState(void)
502 {
503         memset(&gl_state, 0, sizeof(gl_state));
504         gl_state.depthtest = true;
505         gl_state.alphatest = false;
506         gl_state.blendfunc1 = GL_ONE;
507         gl_state.blendfunc2 = GL_ZERO;
508         gl_state.blend = false;
509         gl_state.depthmask = GL_TRUE;
510         gl_state.colormask = 15;
511         gl_state.color4f[0] = gl_state.color4f[1] = gl_state.color4f[2] = gl_state.color4f[3] = 1;
512         gl_state.lockrange_first = 0;
513         gl_state.lockrange_count = 0;
514         gl_state.pointer_vertex = NULL;
515         gl_state.pointer_color = NULL;
516
517         CHECKGLERROR
518
519         qglColorMask(1, 1, 1, 1);
520         qglAlphaFunc(GL_GEQUAL, 0.5);CHECKGLERROR
521         qglDisable(GL_ALPHA_TEST);CHECKGLERROR
522         qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
523         qglDisable(GL_BLEND);CHECKGLERROR
524         qglCullFace(GL_FRONT);CHECKGLERROR
525         qglEnable(GL_CULL_FACE);CHECKGLERROR
526         qglDepthFunc(GL_LEQUAL);CHECKGLERROR
527         qglEnable(GL_DEPTH_TEST);CHECKGLERROR
528         qglDepthMask(gl_state.depthmask);CHECKGLERROR
529
530         qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), NULL);CHECKGLERROR
531         qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
532
533         qglColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL);CHECKGLERROR
534         qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
535
536         GL_Color(0, 0, 0, 0);
537         GL_Color(1, 1, 1, 1);
538
539         GL_SetupTextureState();
540 }
541
542 void GL_ActiveTexture(unsigned int num)
543 {
544         if (gl_state.unit != num)
545         {
546                 gl_state.unit = num;
547                 if (qglActiveTexture)
548                 {
549                         CHECKGLERROR
550                         qglActiveTexture(GL_TEXTURE0_ARB + gl_state.unit);
551                         CHECKGLERROR
552                 }
553         }
554 }
555
556 void GL_ClientActiveTexture(unsigned int num)
557 {
558         if (gl_state.clientunit != num)
559         {
560                 gl_state.clientunit = num;
561                 if (qglActiveTexture)
562                 {
563                         CHECKGLERROR
564                         qglClientActiveTexture(GL_TEXTURE0_ARB + gl_state.clientunit);
565                         CHECKGLERROR
566                 }
567         }
568 }
569
570 void GL_BlendFunc(int blendfunc1, int blendfunc2)
571 {
572         if (gl_state.blendfunc1 != blendfunc1 || gl_state.blendfunc2 != blendfunc2)
573         {
574                 CHECKGLERROR
575                 qglBlendFunc(gl_state.blendfunc1 = blendfunc1, gl_state.blendfunc2 = blendfunc2);CHECKGLERROR
576                 if (gl_state.blendfunc2 == GL_ZERO)
577                 {
578                         if (gl_state.blendfunc1 == GL_ONE)
579                         {
580                                 if (gl_state.blend)
581                                 {
582                                         gl_state.blend = 0;
583                                         qglDisable(GL_BLEND);CHECKGLERROR
584                                 }
585                         }
586                         else
587                         {
588                                 if (!gl_state.blend)
589                                 {
590                                         gl_state.blend = 1;
591                                         qglEnable(GL_BLEND);CHECKGLERROR
592                                 }
593                         }
594                 }
595                 else
596                 {
597                         if (!gl_state.blend)
598                         {
599                                 gl_state.blend = 1;
600                                 qglEnable(GL_BLEND);CHECKGLERROR
601                         }
602                 }
603         }
604 }
605
606 void GL_DepthMask(int state)
607 {
608         if (gl_state.depthmask != state)
609         {
610                 CHECKGLERROR
611                 qglDepthMask(gl_state.depthmask = state);CHECKGLERROR
612         }
613 }
614
615 void GL_DepthTest(int state)
616 {
617         if (gl_state.depthtest != state)
618         {
619                 gl_state.depthtest = state;
620                 CHECKGLERROR
621                 if (gl_state.depthtest)
622                 {
623                         qglEnable(GL_DEPTH_TEST);CHECKGLERROR
624                 }
625                 else
626                 {
627                         qglDisable(GL_DEPTH_TEST);CHECKGLERROR
628                 }
629         }
630 }
631
632 void GL_AlphaTest(int state)
633 {
634         if (gl_state.alphatest != state)
635         {
636                 gl_state.alphatest = state;
637                 CHECKGLERROR
638                 if (gl_state.alphatest)
639                 {
640                         qglEnable(GL_ALPHA_TEST);CHECKGLERROR
641                 }
642                 else
643                 {
644                         qglDisable(GL_ALPHA_TEST);CHECKGLERROR
645                 }
646         }
647 }
648
649 void GL_ColorMask(int r, int g, int b, int a)
650 {
651         int state = r*8 + g*4 + b*2 + a*1;
652         if (gl_state.colormask != state)
653         {
654                 gl_state.colormask = state;
655                 CHECKGLERROR
656                 qglColorMask((GLboolean)r, (GLboolean)g, (GLboolean)b, (GLboolean)a);CHECKGLERROR
657         }
658 }
659
660 void GL_Color(float cr, float cg, float cb, float ca)
661 {
662         if (gl_state.pointer_color || gl_state.color4f[0] != cr || gl_state.color4f[1] != cg || gl_state.color4f[2] != cb || gl_state.color4f[3] != ca)
663         {
664                 gl_state.color4f[0] = cr;
665                 gl_state.color4f[1] = cg;
666                 gl_state.color4f[2] = cb;
667                 gl_state.color4f[3] = ca;
668                 CHECKGLERROR
669                 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);
670                 CHECKGLERROR
671         }
672 }
673
674 void GL_LockArrays(int first, int count)
675 {
676         if (gl_state.lockrange_count != count || gl_state.lockrange_first != first)
677         {
678                 if (gl_state.lockrange_count)
679                 {
680                         gl_state.lockrange_count = 0;
681                         CHECKGLERROR
682                         qglUnlockArraysEXT();
683                         CHECKGLERROR
684                 }
685                 if (count && gl_supportslockarrays && gl_lockarrays.integer && r_render.integer)
686                 {
687                         gl_state.lockrange_first = first;
688                         gl_state.lockrange_count = count;
689                         CHECKGLERROR
690                         qglLockArraysEXT(first, count);
691                         CHECKGLERROR
692                 }
693         }
694 }
695
696 void GL_Scissor (int x, int y, int width, int height)
697 {
698         CHECKGLERROR
699         qglScissor(x, vid.height - (y + height),width,height);
700         CHECKGLERROR
701 }
702
703 void GL_ScissorTest(int state)
704 {
705         if(gl_state.scissortest == state)
706                 return;
707
708         CHECKGLERROR
709         if((gl_state.scissortest = state))
710                 qglEnable(GL_SCISSOR_TEST);
711         else
712                 qglDisable(GL_SCISSOR_TEST);
713         CHECKGLERROR
714 }
715
716 void GL_Clear(int mask)
717 {
718         CHECKGLERROR
719         qglClear(mask);CHECKGLERROR
720 }
721
722 void GL_TransformToScreen(const vec4_t in, vec4_t out)
723 {
724         vec4_t temp;
725         float iw;
726         Matrix4x4_Transform4 (&backend_viewmatrix, in, temp);
727         Matrix4x4_Transform4 (&backend_projectmatrix, temp, out);
728         iw = 1.0f / out[3];
729         out[0] = r_view.x + (out[0] * iw + 1.0f) * r_view.width * 0.5f;
730         out[1] = r_view.y + r_view.height - (out[1] * iw + 1.0f) * r_view.height * 0.5f;
731         out[2] = r_view.z + (out[2] * iw + 1.0f) * r_view.depth * 0.5f;
732 }
733
734 // called at beginning of frame
735 void R_Mesh_Start(void)
736 {
737         BACKENDACTIVECHECK
738         CHECKGLERROR
739         if (gl_printcheckerror.integer && !gl_paranoid.integer)
740         {
741                 Con_Printf("WARNING: gl_printcheckerror is on but gl_paranoid is off, turning it on...\n");
742                 Cvar_SetValueQuick(&gl_paranoid, 1);
743         }
744         GL_Backend_ResetState();
745 }
746
747 unsigned int GL_Backend_CompileProgram(int vertexstrings_count, const char **vertexstrings_list, int fragmentstrings_count, const char **fragmentstrings_list)
748 {
749         GLint vertexshadercompiled, fragmentshadercompiled, programlinked;
750         GLuint vertexshaderobject, fragmentshaderobject, programobject = 0;
751         char compilelog[MAX_INPUTLINE];
752         CHECKGLERROR
753
754         programobject = qglCreateProgramObjectARB();CHECKGLERROR
755         if (!programobject)
756                 return 0;
757
758         if (developer.integer >= 100)
759         {
760                 int i;
761                 Con_Printf("Compiling shader:\n");
762                 if (vertexstrings_count)
763                 {
764                         Con_Printf("------ VERTEX SHADER ------\n");
765                         for (i = 0;i < vertexstrings_count;i++)
766                                 Con_Print(vertexstrings_list[i]);
767                         Con_Print("\n");
768                 }
769                 if (fragmentstrings_count)
770                 {
771                         Con_Printf("------ FRAGMENT SHADER ------\n");
772                         for (i = 0;i < fragmentstrings_count;i++)
773                                 Con_Print(fragmentstrings_list[i]);
774                         Con_Print("\n");
775                 }
776         }
777
778         if (vertexstrings_count)
779         {
780                 vertexshaderobject = qglCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);CHECKGLERROR
781                 if (!vertexshaderobject)
782                 {
783                         qglDeleteObjectARB(programobject);
784                         CHECKGLERROR
785                         return 0;
786                 }
787                 qglShaderSourceARB(vertexshaderobject, vertexstrings_count, vertexstrings_list, NULL);CHECKGLERROR
788                 qglCompileShaderARB(vertexshaderobject);CHECKGLERROR
789                 qglGetObjectParameterivARB(vertexshaderobject, GL_OBJECT_COMPILE_STATUS_ARB, &vertexshadercompiled);CHECKGLERROR
790                 qglGetInfoLogARB(vertexshaderobject, sizeof(compilelog), NULL, compilelog);CHECKGLERROR
791                 if (compilelog[0])
792                         Con_DPrintf("vertex shader compile log:\n%s\n", compilelog);
793                 if (!vertexshadercompiled)
794                 {
795                         qglDeleteObjectARB(programobject);CHECKGLERROR
796                         qglDeleteObjectARB(vertexshaderobject);CHECKGLERROR
797                         return 0;
798                 }
799                 qglAttachObjectARB(programobject, vertexshaderobject);CHECKGLERROR
800                 qglDeleteObjectARB(vertexshaderobject);CHECKGLERROR
801         }
802
803         if (fragmentstrings_count)
804         {
805                 fragmentshaderobject = qglCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);CHECKGLERROR
806                 if (!fragmentshaderobject)
807                 {
808                         qglDeleteObjectARB(programobject);CHECKGLERROR
809                         return 0;
810                 }
811                 qglShaderSourceARB(fragmentshaderobject, fragmentstrings_count, fragmentstrings_list, NULL);CHECKGLERROR
812                 qglCompileShaderARB(fragmentshaderobject);CHECKGLERROR
813                 qglGetObjectParameterivARB(fragmentshaderobject, GL_OBJECT_COMPILE_STATUS_ARB, &fragmentshadercompiled);CHECKGLERROR
814                 qglGetInfoLogARB(fragmentshaderobject, sizeof(compilelog), NULL, compilelog);CHECKGLERROR
815                 if (compilelog[0])
816                         Con_DPrintf("fragment shader compile log:\n%s\n", compilelog);
817                 if (!fragmentshadercompiled)
818                 {
819                         qglDeleteObjectARB(programobject);CHECKGLERROR
820                         qglDeleteObjectARB(fragmentshaderobject);CHECKGLERROR
821                         return 0;
822                 }
823                 qglAttachObjectARB(programobject, fragmentshaderobject);CHECKGLERROR
824                 qglDeleteObjectARB(fragmentshaderobject);CHECKGLERROR
825         }
826
827         qglLinkProgramARB(programobject);CHECKGLERROR
828         qglGetObjectParameterivARB(programobject, GL_OBJECT_LINK_STATUS_ARB, &programlinked);CHECKGLERROR
829         qglGetInfoLogARB(programobject, sizeof(compilelog), NULL, compilelog);CHECKGLERROR
830         if (compilelog[0])
831         {
832                 Con_DPrintf("program link log:\n%s\n", compilelog);
833                 // software vertex shader is ok but software fragment shader is WAY
834                 // too slow, fail program if so.
835                 // NOTE: this string might be ATI specific, but that's ok because the
836                 // ATI R300 chip (Radeon 9500-9800/X300) is the most likely to use a
837                 // software fragment shader due to low instruction and dependent
838                 // texture limits.
839                 if (strstr(compilelog, "fragment shader will run in software"))
840                         programlinked = false;
841         }
842         if (!programlinked)
843         {
844                 qglDeleteObjectARB(programobject);CHECKGLERROR
845                 return 0;
846         }
847         CHECKGLERROR
848         return programobject;
849 }
850
851 void GL_Backend_FreeProgram(unsigned int prog)
852 {
853         CHECKGLERROR
854         qglDeleteObjectARB(prog);
855         CHECKGLERROR
856 }
857
858 int gl_backend_rebindtextures;
859
860 void GL_Backend_RenumberElements(int *out, int count, const int *in, int offset)
861 {
862         int i;
863         if (offset)
864         {
865                 for (i = 0;i < count;i++)
866                         *out++ = *in++ + offset;
867         }
868         else
869                 memcpy(out, in, sizeof(*out) * count);
870 }
871
872 // renders triangles using vertices from the active arrays
873 int paranoidblah = 0;
874 void R_Mesh_Draw(int firstvertex, int numvertices, int numtriangles, const int *elements)
875 {
876         unsigned int numelements = numtriangles * 3;
877         if (numvertices < 3 || numtriangles < 1)
878         {
879                 Con_Printf("R_Mesh_Draw(%d, %d, %d, %08p);\n", firstvertex, numvertices, numtriangles, elements);
880                 return;
881         }
882         CHECKGLERROR
883         r_refdef.stats.meshes++;
884         r_refdef.stats.meshes_elements += numelements;
885         if (gl_paranoid.integer)
886         {
887                 unsigned int i, j, size;
888                 const int *p;
889                 if (!qglIsEnabled(GL_VERTEX_ARRAY))
890                         Con_Print("R_Mesh_Draw: vertex array not enabled\n");
891                 CHECKGLERROR
892                 for (j = 0, size = numvertices * 3, p = (int *)((float *)gl_state.pointer_vertex + firstvertex * 3);j < size;j++, p++)
893                         paranoidblah += *p;
894                 if (gl_state.pointer_color)
895                 {
896                         if (!qglIsEnabled(GL_COLOR_ARRAY))
897                                 Con_Print("R_Mesh_Draw: color array set but not enabled\n");
898                         CHECKGLERROR
899                         for (j = 0, size = numvertices * 4, p = (int *)((float *)gl_state.pointer_color + firstvertex * 4);j < size;j++, p++)
900                                 paranoidblah += *p;
901                 }
902                 for (i = 0;i < backendarrayunits;i++)
903                 {
904                         if (gl_state.units[i].arrayenabled)
905                         {
906                                 GL_ClientActiveTexture(i);
907                                 if (!qglIsEnabled(GL_TEXTURE_COORD_ARRAY))
908                                         Con_Print("R_Mesh_Draw: texcoord array set but not enabled\n");
909                                 CHECKGLERROR
910                                 for (j = 0, size = numvertices * gl_state.units[i].arraycomponents, p = (int *)((float *)gl_state.units[i].pointer_texcoord + firstvertex * gl_state.units[i].arraycomponents);j < size;j++, p++)
911                                         paranoidblah += *p;
912                         }
913                 }
914                 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
915                 {
916                         if (elements[i] < firstvertex || elements[i] >= firstvertex + numvertices)
917                         {
918                                 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in elements list\n", elements[i], firstvertex, firstvertex + numvertices);
919                                 return;
920                         }
921                 }
922                 CHECKGLERROR
923         }
924         if (r_render.integer)
925         {
926                 CHECKGLERROR
927                 if (gl_mesh_testmanualfeeding.integer)
928                 {
929                         unsigned int i, j;
930                         const GLfloat *p;
931                         qglBegin(GL_TRIANGLES);
932                         for (i = 0;i < (unsigned int) numtriangles * 3;i++)
933                         {
934                                 for (j = 0;j < backendarrayunits;j++)
935                                 {
936                                         if (gl_state.units[j].pointer_texcoord)
937                                         {
938                                                 if (backendarrayunits > 1)
939                                                 {
940                                                         if (gl_state.units[j].arraycomponents == 4)
941                                                         {
942                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 4;
943                                                                 qglMultiTexCoord4f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2], p[3]);
944                                                         }
945                                                         else if (gl_state.units[j].arraycomponents == 3)
946                                                         {
947                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 3;
948                                                                 qglMultiTexCoord3f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2]);
949                                                         }
950                                                         else if (gl_state.units[j].arraycomponents == 2)
951                                                         {
952                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 2;
953                                                                 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, p[0], p[1]);
954                                                         }
955                                                         else
956                                                         {
957                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 1;
958                                                                 qglMultiTexCoord1f(GL_TEXTURE0_ARB + j, p[0]);
959                                                         }
960                                                 }
961                                                 else
962                                                 {
963                                                         if (gl_state.units[j].arraycomponents == 4)
964                                                         {
965                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 4;
966                                                                 qglTexCoord4f(p[0], p[1], p[2], p[3]);
967                                                         }
968                                                         else if (gl_state.units[j].arraycomponents == 3)
969                                                         {
970                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 3;
971                                                                 qglTexCoord3f(p[0], p[1], p[2]);
972                                                         }
973                                                         else if (gl_state.units[j].arraycomponents == 2)
974                                                         {
975                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 2;
976                                                                 qglTexCoord2f(p[0], p[1]);
977                                                         }
978                                                         else
979                                                         {
980                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 1;
981                                                                 qglTexCoord1f(p[0]);
982                                                         }
983                                                 }
984                                         }
985                                 }
986                                 if (gl_state.pointer_color)
987                                 {
988                                         p = ((const GLfloat *)(gl_state.pointer_color)) + elements[i] * 4;
989                                         qglColor4f(p[0], p[1], p[2], p[3]);
990                                 }
991                                 p = ((const GLfloat *)(gl_state.pointer_vertex)) + elements[i] * 3;
992                                 qglVertex3f(p[0], p[1], p[2]);
993                         }
994                         qglEnd();
995                         CHECKGLERROR
996                 }
997                 else if (gl_mesh_testarrayelement.integer)
998                 {
999                         int i;
1000                         qglBegin(GL_TRIANGLES);
1001                         for (i = 0;i < numtriangles * 3;i++)
1002                         {
1003                                 qglArrayElement(elements[i]);
1004                         }
1005                         qglEnd();
1006                         CHECKGLERROR
1007                 }
1008                 else if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1009                 {
1010                         qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices, numelements, GL_UNSIGNED_INT, elements);
1011                         CHECKGLERROR
1012                 }
1013                 else
1014                 {
1015                         qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, elements);
1016                         CHECKGLERROR
1017                 }
1018         }
1019 }
1020
1021 // restores backend state, used when done with 3D rendering
1022 void R_Mesh_Finish(void)
1023 {
1024         unsigned int i;
1025         BACKENDACTIVECHECK
1026         CHECKGLERROR
1027         GL_LockArrays(0, 0);
1028         CHECKGLERROR
1029
1030         for (i = 0;i < backendimageunits;i++)
1031         {
1032                 GL_ActiveTexture(i);
1033                 qglBindTexture(GL_TEXTURE_1D, 0);CHECKGLERROR
1034                 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
1035                 if (gl_texture3d)
1036                 {
1037                         qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
1038                 }
1039                 if (gl_texturecubemap)
1040                 {
1041                         qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
1042                 }
1043         }
1044         for (i = 0;i < backendarrayunits;i++)
1045         {
1046                 GL_ActiveTexture(backendarrayunits - 1 - i);
1047                 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1048         }
1049         for (i = 0;i < backendunits;i++)
1050         {
1051                 GL_ActiveTexture(backendunits - 1 - i);
1052                 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1053                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1054                 if (gl_texture3d)
1055                 {
1056                         qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1057                 }
1058                 if (gl_texturecubemap)
1059                 {
1060                         qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1061                 }
1062                 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
1063                 if (gl_combine.integer)
1064                 {
1065                         qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
1066                         qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
1067                 }
1068         }
1069         qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1070         qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
1071
1072         qglDisable(GL_BLEND);CHECKGLERROR
1073         qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1074         qglDepthMask(GL_TRUE);CHECKGLERROR
1075         qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);CHECKGLERROR
1076 }
1077
1078 void R_Mesh_Matrix(const matrix4x4_t *matrix)
1079 {
1080         if (memcmp(matrix, &backend_modelmatrix, sizeof(matrix4x4_t)))
1081         {
1082                 backend_modelmatrix = *matrix;
1083                 Matrix4x4_Concat(&backend_modelviewmatrix, &backend_viewmatrix, matrix);
1084                 Matrix4x4_Transpose(&backend_glmodelviewmatrix, &backend_modelviewmatrix);
1085                 CHECKGLERROR
1086                 qglLoadMatrixf(&backend_glmodelviewmatrix.m[0][0]);CHECKGLERROR
1087         }
1088 }
1089
1090 void R_Mesh_VertexPointer(const float *vertex3f)
1091 {
1092         if (gl_state.pointer_vertex != vertex3f)
1093         {
1094                 gl_state.pointer_vertex = vertex3f;
1095                 CHECKGLERROR
1096                 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), gl_state.pointer_vertex);
1097                 CHECKGLERROR
1098         }
1099 }
1100
1101 void R_Mesh_ColorPointer(const float *color4f)
1102 {
1103         if (gl_state.pointer_color != color4f)
1104         {
1105                 CHECKGLERROR
1106                 if (!gl_state.pointer_color)
1107                 {
1108                         qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1109                 }
1110                 else if (!color4f)
1111                 {
1112                         qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1113                         // when color array is on the glColor gets trashed, set it again
1114                         qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);CHECKGLERROR
1115                 }
1116                 gl_state.pointer_color = color4f;
1117                 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), gl_state.pointer_color);CHECKGLERROR
1118         }
1119 }
1120
1121 void R_Mesh_TexCoordPointer(unsigned int unitnum, unsigned int numcomponents, const float *texcoord)
1122 {
1123         gltextureunit_t *unit = gl_state.units + unitnum;
1124         // update array settings
1125         CHECKGLERROR
1126         if (texcoord)
1127         {
1128                 // texcoord array
1129                 if (unit->pointer_texcoord != texcoord || unit->arraycomponents != numcomponents)
1130                 {
1131                         unit->pointer_texcoord = texcoord;
1132                         unit->arraycomponents = numcomponents;
1133                         GL_ClientActiveTexture(unitnum);
1134                         qglTexCoordPointer(unit->arraycomponents, GL_FLOAT, sizeof(float) * unit->arraycomponents, unit->pointer_texcoord);CHECKGLERROR
1135                 }
1136                 // texture array unit is enabled, enable the array
1137                 if (!unit->arrayenabled)
1138                 {
1139                         unit->arrayenabled = true;
1140                         GL_ClientActiveTexture(unitnum);
1141                         qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1142                 }
1143         }
1144         else
1145         {
1146                 // texture array unit is disabled, disable the array
1147                 if (unit->arrayenabled)
1148                 {
1149                         unit->arrayenabled = false;
1150                         GL_ClientActiveTexture(unitnum);
1151                         qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1152                 }
1153         }
1154 }
1155
1156 void R_Mesh_TexBindAll(unsigned int unitnum, int tex1d, int tex2d, int tex3d, int texcubemap)
1157 {
1158         gltextureunit_t *unit = gl_state.units + unitnum;
1159         if (unitnum >= backendimageunits)
1160                 return;
1161         // update 1d texture binding
1162         if (unit->t1d != tex1d)
1163         {
1164                 GL_ActiveTexture(unitnum);
1165                 if (unitnum < backendunits)
1166                 {
1167                         if (tex1d)
1168                         {
1169                                 if (unit->t1d == 0)
1170                                 {
1171                                         qglEnable(GL_TEXTURE_1D);CHECKGLERROR
1172                                 }
1173                         }
1174                         else
1175                         {
1176                                 if (unit->t1d)
1177                                 {
1178                                         qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1179                                 }
1180                         }
1181                 }
1182                 unit->t1d = tex1d;
1183                 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1184         }
1185         // update 2d texture binding
1186         if (unit->t2d != tex2d)
1187         {
1188                 GL_ActiveTexture(unitnum);
1189                 if (unitnum < backendunits)
1190                 {
1191                         if (tex2d)
1192                         {
1193                                 if (unit->t2d == 0)
1194                                 {
1195                                         qglEnable(GL_TEXTURE_2D);CHECKGLERROR
1196                                 }
1197                         }
1198                         else
1199                         {
1200                                 if (unit->t2d)
1201                                 {
1202                                         qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1203                                 }
1204                         }
1205                 }
1206                 unit->t2d = tex2d;
1207                 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1208         }
1209         // update 3d texture binding
1210         if (unit->t3d != tex3d)
1211         {
1212                 GL_ActiveTexture(unitnum);
1213                 if (unitnum < backendunits)
1214                 {
1215                         if (tex3d)
1216                         {
1217                                 if (unit->t3d == 0)
1218                                 {
1219                                         qglEnable(GL_TEXTURE_3D);CHECKGLERROR
1220                                 }
1221                         }
1222                         else
1223                         {
1224                                 if (unit->t3d)
1225                                 {
1226                                         qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1227                                 }
1228                         }
1229                 }
1230                 unit->t3d = tex3d;
1231                 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1232         }
1233         // update cubemap texture binding
1234         if (unit->tcubemap != texcubemap)
1235         {
1236                 GL_ActiveTexture(unitnum);
1237                 if (unitnum < backendunits)
1238                 {
1239                         if (texcubemap)
1240                         {
1241                                 if (unit->tcubemap == 0)
1242                                 {
1243                                         qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1244                                 }
1245                         }
1246                         else
1247                         {
1248                                 if (unit->tcubemap)
1249                                 {
1250                                         qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1251                                 }
1252                         }
1253                 }
1254                 unit->tcubemap = texcubemap;
1255                 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1256         }
1257 }
1258
1259 void R_Mesh_TexBind1D(unsigned int unitnum, int texnum)
1260 {
1261         gltextureunit_t *unit = gl_state.units + unitnum;
1262         if (unitnum >= backendimageunits)
1263                 return;
1264         // update 1d texture binding
1265         if (unit->t1d != texnum)
1266         {
1267                 GL_ActiveTexture(unitnum);
1268                 if (unitnum < backendunits)
1269                 {
1270                         if (texnum)
1271                         {
1272                                 if (unit->t1d == 0)
1273                                 {
1274                                         qglEnable(GL_TEXTURE_1D);CHECKGLERROR
1275                                 }
1276                         }
1277                         else
1278                         {
1279                                 if (unit->t1d)
1280                                 {
1281                                         qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1282                                 }
1283                         }
1284                 }
1285                 unit->t1d = texnum;
1286                 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1287         }
1288         // update 2d texture binding
1289         if (unit->t2d)
1290         {
1291                 GL_ActiveTexture(unitnum);
1292                 if (unitnum < backendunits)
1293                 {
1294                         if (unit->t2d)
1295                         {
1296                                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1297                         }
1298                 }
1299                 unit->t2d = 0;
1300                 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1301         }
1302         // update 3d texture binding
1303         if (unit->t3d)
1304         {
1305                 GL_ActiveTexture(unitnum);
1306                 if (unitnum < backendunits)
1307                 {
1308                         if (unit->t3d)
1309                         {
1310                                 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1311                         }
1312                 }
1313                 unit->t3d = 0;
1314                 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1315         }
1316         // update cubemap texture binding
1317         if (unit->tcubemap)
1318         {
1319                 GL_ActiveTexture(unitnum);
1320                 if (unitnum < backendunits)
1321                 {
1322                         if (unit->tcubemap)
1323                         {
1324                                 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1325                         }
1326                 }
1327                 unit->tcubemap = 0;
1328                 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1329         }
1330 }
1331
1332 void R_Mesh_TexBind(unsigned int unitnum, int texnum)
1333 {
1334         gltextureunit_t *unit = gl_state.units + unitnum;
1335         if (unitnum >= backendimageunits)
1336                 return;
1337         // update 1d texture binding
1338         if (unit->t1d)
1339         {
1340                 GL_ActiveTexture(unitnum);
1341                 if (unitnum < backendunits)
1342                 {
1343                         if (unit->t1d)
1344                         {
1345                                 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1346                         }
1347                 }
1348                 unit->t1d = 0;
1349                 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1350         }
1351         // update 2d texture binding
1352         if (unit->t2d != texnum)
1353         {
1354                 GL_ActiveTexture(unitnum);
1355                 if (unitnum < backendunits)
1356                 {
1357                         if (texnum)
1358                         {
1359                                 if (unit->t2d == 0)
1360                                 {
1361                                         qglEnable(GL_TEXTURE_2D);CHECKGLERROR
1362                                 }
1363                         }
1364                         else
1365                         {
1366                                 if (unit->t2d)
1367                                 {
1368                                         qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1369                                 }
1370                         }
1371                 }
1372                 unit->t2d = texnum;
1373                 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1374         }
1375         // update 3d texture binding
1376         if (unit->t3d)
1377         {
1378                 GL_ActiveTexture(unitnum);
1379                 if (unitnum < backendunits)
1380                 {
1381                         if (unit->t3d)
1382                         {
1383                                 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1384                         }
1385                 }
1386                 unit->t3d = 0;
1387                 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1388         }
1389         // update cubemap texture binding
1390         if (unit->tcubemap != 0)
1391         {
1392                 GL_ActiveTexture(unitnum);
1393                 if (unitnum < backendunits)
1394                 {
1395                         if (unit->tcubemap)
1396                         {
1397                                 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1398                         }
1399                 }
1400                 unit->tcubemap = 0;
1401                 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1402         }
1403 }
1404
1405 void R_Mesh_TexBind3D(unsigned int unitnum, int texnum)
1406 {
1407         gltextureunit_t *unit = gl_state.units + unitnum;
1408         if (unitnum >= backendimageunits)
1409                 return;
1410         // update 1d texture binding
1411         if (unit->t1d)
1412         {
1413                 GL_ActiveTexture(unitnum);
1414                 if (unitnum < backendunits)
1415                 {
1416                         if (unit->t1d)
1417                         {
1418                                 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1419                         }
1420                 }
1421                 unit->t1d = 0;
1422                 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1423         }
1424         // update 2d texture binding
1425         if (unit->t2d)
1426         {
1427                 GL_ActiveTexture(unitnum);
1428                 if (unitnum < backendunits)
1429                 {
1430                         if (unit->t2d)
1431                         {
1432                                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1433                         }
1434                 }
1435                 unit->t2d = 0;
1436                 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1437         }
1438         // update 3d texture binding
1439         if (unit->t3d != texnum)
1440         {
1441                 GL_ActiveTexture(unitnum);
1442                 if (unitnum < backendunits)
1443                 {
1444                         if (texnum)
1445                         {
1446                                 if (unit->t3d == 0)
1447                                 {
1448                                         qglEnable(GL_TEXTURE_3D);CHECKGLERROR
1449                                 }
1450                         }
1451                         else
1452                         {
1453                                 if (unit->t3d)
1454                                 {
1455                                         qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1456                                 }
1457                         }
1458                 }
1459                 unit->t3d = texnum;
1460                 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1461         }
1462         // update cubemap texture binding
1463         if (unit->tcubemap != 0)
1464         {
1465                 GL_ActiveTexture(unitnum);
1466                 if (unitnum < backendunits)
1467                 {
1468                         if (unit->tcubemap)
1469                         {
1470                                 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1471                         }
1472                 }
1473                 unit->tcubemap = 0;
1474                 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1475         }
1476 }
1477
1478 void R_Mesh_TexBindCubeMap(unsigned int unitnum, int texnum)
1479 {
1480         gltextureunit_t *unit = gl_state.units + unitnum;
1481         if (unitnum >= backendimageunits)
1482                 return;
1483         // update 1d texture binding
1484         if (unit->t1d)
1485         {
1486                 GL_ActiveTexture(unitnum);
1487                 if (unitnum < backendunits)
1488                 {
1489                         if (unit->t1d)
1490                         {
1491                                 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1492                         }
1493                 }
1494                 unit->t1d = 0;
1495                 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1496         }
1497         // update 2d texture binding
1498         if (unit->t2d)
1499         {
1500                 GL_ActiveTexture(unitnum);
1501                 if (unitnum < backendunits)
1502                 {
1503                         if (unit->t2d)
1504                         {
1505                                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1506                         }
1507                 }
1508                 unit->t2d = 0;
1509                 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1510         }
1511         // update 3d texture binding
1512         if (unit->t3d)
1513         {
1514                 GL_ActiveTexture(unitnum);
1515                 if (unitnum < backendunits)
1516                 {
1517                         if (unit->t3d)
1518                         {
1519                                 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1520                         }
1521                 }
1522                 unit->t3d = 0;
1523                 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1524         }
1525         // update cubemap texture binding
1526         if (unit->tcubemap != texnum)
1527         {
1528                 GL_ActiveTexture(unitnum);
1529                 if (unitnum < backendunits)
1530                 {
1531                         if (texnum)
1532                         {
1533                                 if (unit->tcubemap == 0)
1534                                 {
1535                                         qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1536                                 }
1537                         }
1538                         else
1539                         {
1540                                 if (unit->tcubemap)
1541                                 {
1542                                         qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1543                                 }
1544                         }
1545                 }
1546                 unit->tcubemap = texnum;
1547                 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1548         }
1549 }
1550
1551 void R_Mesh_TexMatrix(unsigned int unitnum, const matrix4x4_t *matrix)
1552 {
1553         gltextureunit_t *unit = gl_state.units + unitnum;
1554         if (matrix->m[3][3])
1555         {
1556                 // texmatrix specified, check if it is different
1557                 if (!unit->texmatrixenabled || memcmp(&unit->matrix, matrix, sizeof(matrix4x4_t)))
1558                 {
1559                         matrix4x4_t tempmatrix;
1560                         unit->texmatrixenabled = true;
1561                         unit->matrix = *matrix;
1562                         CHECKGLERROR
1563                         Matrix4x4_Transpose(&tempmatrix, &unit->matrix);
1564                         qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1565                         GL_ActiveTexture(unitnum);
1566                         qglLoadMatrixf(&tempmatrix.m[0][0]);CHECKGLERROR
1567                         qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1568                 }
1569         }
1570         else
1571         {
1572                 // no texmatrix specified, revert to identity
1573                 if (unit->texmatrixenabled)
1574                 {
1575                         unit->texmatrixenabled = false;
1576                         CHECKGLERROR
1577                         qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1578                         GL_ActiveTexture(unitnum);
1579                         qglLoadIdentity();CHECKGLERROR
1580                         qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1581                 }
1582         }
1583 }
1584
1585 void R_Mesh_TexCombine(unsigned int unitnum, int combinergb, int combinealpha, int rgbscale, int alphascale)
1586 {
1587         gltextureunit_t *unit = gl_state.units + unitnum;
1588         CHECKGLERROR
1589         if (gl_combine.integer)
1590         {
1591                 // GL_ARB_texture_env_combine
1592                 if (!combinergb)
1593                         combinergb = GL_MODULATE;
1594                 if (!combinealpha)
1595                         combinealpha = GL_MODULATE;
1596                 if (!rgbscale)
1597                         rgbscale = 1;
1598                 if (!alphascale)
1599                         alphascale = 1;
1600                 if (unit->combinergb != combinergb)
1601                 {
1602                         unit->combinergb = combinergb;
1603                         GL_ActiveTexture(unitnum);
1604                         qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
1605                 }
1606                 if (unit->combinealpha != combinealpha)
1607                 {
1608                         unit->combinealpha = combinealpha;
1609                         GL_ActiveTexture(unitnum);
1610                         qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
1611                 }
1612                 if (unit->rgbscale != rgbscale)
1613                 {
1614                         GL_ActiveTexture(unitnum);
1615                         qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (unit->rgbscale = rgbscale));CHECKGLERROR
1616                 }
1617                 if (unit->alphascale != alphascale)
1618                 {
1619                         GL_ActiveTexture(unitnum);
1620                         qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, (unit->alphascale = alphascale));CHECKGLERROR
1621                 }
1622         }
1623         else
1624         {
1625                 // normal GL texenv
1626                 if (!combinergb)
1627                         combinergb = GL_MODULATE;
1628                 if (unit->combinergb != combinergb)
1629                 {
1630                         unit->combinergb = combinergb;
1631                         GL_ActiveTexture(unitnum);
1632                         qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combinergb);CHECKGLERROR
1633                 }
1634         }
1635 }
1636
1637 void R_Mesh_TextureState(const rmeshstate_t *m)
1638 {
1639         unsigned int i;
1640
1641         BACKENDACTIVECHECK
1642
1643         CHECKGLERROR
1644         if (gl_backend_rebindtextures)
1645         {
1646                 gl_backend_rebindtextures = false;
1647                 GL_SetupTextureState();
1648                 CHECKGLERROR
1649         }
1650
1651         for (i = 0;i < backendimageunits;i++)
1652                 R_Mesh_TexBindAll(i, m->tex1d[i], m->tex[i], m->tex3d[i], m->texcubemap[i]);
1653         for (i = 0;i < backendarrayunits;i++)
1654         {
1655                 if (m->pointer_texcoord3f[i])
1656                         R_Mesh_TexCoordPointer(i, 3, m->pointer_texcoord3f[i]);
1657                 else
1658                         R_Mesh_TexCoordPointer(i, 2, m->pointer_texcoord[i]);
1659         }
1660         for (i = 0;i < backendunits;i++)
1661         {
1662                 R_Mesh_TexMatrix(i, &m->texmatrix[i]);
1663                 R_Mesh_TexCombine(i, m->texcombinergb[i], m->texcombinealpha[i], m->texrgbscale[i], m->texalphascale[i]);
1664         }
1665         CHECKGLERROR
1666 }
1667
1668 void R_Mesh_ResetTextureState(void)
1669 {
1670         unsigned int unitnum;
1671
1672         BACKENDACTIVECHECK
1673
1674         CHECKGLERROR
1675         if (gl_backend_rebindtextures)
1676         {
1677                 gl_backend_rebindtextures = false;
1678                 GL_SetupTextureState();
1679                 CHECKGLERROR
1680         }
1681
1682         for (unitnum = 0;unitnum < backendimageunits;unitnum++)
1683         {
1684                 gltextureunit_t *unit = gl_state.units + unitnum;
1685                 // update 1d texture binding
1686                 if (unit->t1d)
1687                 {
1688                         GL_ActiveTexture(unitnum);
1689                         if (unitnum < backendunits)
1690                         {
1691                                 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1692                         }
1693                         unit->t1d = 0;
1694                         qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1695                 }
1696                 // update 2d texture binding
1697                 if (unit->t2d)
1698                 {
1699                         GL_ActiveTexture(unitnum);
1700                         if (unitnum < backendunits)
1701                         {
1702                                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1703                         }
1704                         unit->t2d = 0;
1705                         qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1706                 }
1707                 // update 3d texture binding
1708                 if (unit->t3d)
1709                 {
1710                         GL_ActiveTexture(unitnum);
1711                         if (unitnum < backendunits)
1712                         {
1713                                 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1714                         }
1715                         unit->t3d = 0;
1716                         qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1717                 }
1718                 // update cubemap texture binding
1719                 if (unit->tcubemap)
1720                 {
1721                         GL_ActiveTexture(unitnum);
1722                         if (unitnum < backendunits)
1723                         {
1724                                 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1725                         }
1726                         unit->tcubemap = 0;
1727                         qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1728                 }
1729         }
1730         for (unitnum = 0;unitnum < backendarrayunits;unitnum++)
1731         {
1732                 gltextureunit_t *unit = gl_state.units + unitnum;
1733                 // texture array unit is disabled, disable the array
1734                 if (unit->arrayenabled)
1735                 {
1736                         unit->arrayenabled = false;
1737                         GL_ClientActiveTexture(unitnum);
1738                         qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1739                 }
1740         }
1741         for (unitnum = 0;unitnum < backendunits;unitnum++)
1742         {
1743                 gltextureunit_t *unit = gl_state.units + unitnum;
1744                 // no texmatrix specified, revert to identity
1745                 if (unit->texmatrixenabled)
1746                 {
1747                         unit->texmatrixenabled = false;
1748                         CHECKGLERROR
1749                         qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1750                         GL_ActiveTexture(unitnum);
1751                         qglLoadIdentity();CHECKGLERROR
1752                         qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1753                 }
1754                 if (gl_combine.integer)
1755                 {
1756                         // GL_ARB_texture_env_combine
1757                         if (unit->combinergb != GL_MODULATE)
1758                         {
1759                                 unit->combinergb = GL_MODULATE;
1760                                 GL_ActiveTexture(unitnum);
1761                                 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
1762                         }
1763                         if (unit->combinealpha != GL_MODULATE)
1764                         {
1765                                 unit->combinealpha = GL_MODULATE;
1766                                 GL_ActiveTexture(unitnum);
1767                                 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
1768                         }
1769                         if (unit->rgbscale != 1)
1770                         {
1771                                 GL_ActiveTexture(unitnum);
1772                                 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (unit->rgbscale = 1));CHECKGLERROR
1773                         }
1774                         if (unit->alphascale != 1)
1775                         {
1776                                 GL_ActiveTexture(unitnum);
1777                                 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, (unit->alphascale = 1));CHECKGLERROR
1778                         }
1779                 }
1780                 else
1781                 {
1782                         // normal GL texenv
1783                         if (unit->combinergb != GL_MODULATE)
1784                         {
1785                                 unit->combinergb = GL_MODULATE;
1786                                 GL_ActiveTexture(unitnum);
1787                                 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combinergb);CHECKGLERROR
1788                         }
1789                 }
1790         }
1791 }
1792
1793 void R_Mesh_Draw_ShowTris(int firstvertex, int numvertices, int numtriangles, const int *elements)
1794 {
1795         CHECKGLERROR
1796         qglBegin(GL_LINES);
1797         for (;numtriangles;numtriangles--, elements += 3)
1798         {
1799                 qglArrayElement(elements[0]);qglArrayElement(elements[1]);
1800                 qglArrayElement(elements[1]);qglArrayElement(elements[2]);
1801                 qglArrayElement(elements[2]);qglArrayElement(elements[0]);
1802         }
1803         qglEnd();
1804         CHECKGLERROR
1805 }