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