]> icculus.org git repositories - divverent/darkplaces.git/blob - gl_backend.c
added bufferobject and bufferoffset parameters to all R_Mesh_*Pointer functions and...
[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", "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, int bufferobject, size_t bufferoffset)
871 {
872         unsigned int numelements = numtriangles * 3;
873         if (numvertices < 3 || numtriangles < 1)
874         {
875                 Con_Printf("R_Mesh_Draw(%d, %d, %d, %8p);\n", firstvertex, numvertices, numtriangles, elements);
876                 return;
877         }
878         CHECKGLERROR
879         r_refdef.stats.meshes++;
880         r_refdef.stats.meshes_elements += numelements;
881         if (gl_paranoid.integer)
882         {
883                 unsigned int i, j, size;
884                 const int *p;
885                 // note: there's no validation done here on buffer objects because it
886                 // is somewhat difficult to get at the data, and gl_paranoid can be
887                 // used without buffer objects if the need arises
888                 // (the data could be gotten using glMapBuffer but it would be very
889                 //  slow due to uncachable video memory reads)
890                 if (!qglIsEnabled(GL_VERTEX_ARRAY))
891                         Con_Print("R_Mesh_Draw: vertex array not enabled\n");
892                 CHECKGLERROR
893                 if (gl_state.pointer_vertex)
894                         for (j = 0, size = numvertices * 3, p = (int *)((float *)gl_state.pointer_vertex + firstvertex * 3);j < size;j++, p++)
895                                 paranoidblah += *p;
896                 if (gl_state.pointer_color_enabled)
897                 {
898                         if (!qglIsEnabled(GL_COLOR_ARRAY))
899                                 Con_Print("R_Mesh_Draw: color array set but not enabled\n");
900                         CHECKGLERROR
901                         if (gl_state.pointer_color && gl_state.pointer_color_enabled)
902                                 for (j = 0, size = numvertices * 4, p = (int *)((float *)gl_state.pointer_color + firstvertex * 4);j < size;j++, p++)
903                                         paranoidblah += *p;
904                 }
905                 for (i = 0;i < backendarrayunits;i++)
906                 {
907                         if (gl_state.units[i].arrayenabled)
908                         {
909                                 GL_ClientActiveTexture(i);
910                                 if (!qglIsEnabled(GL_TEXTURE_COORD_ARRAY))
911                                         Con_Print("R_Mesh_Draw: texcoord array set but not enabled\n");
912                                 CHECKGLERROR
913                                 if (gl_state.units[i].pointer_texcoord && gl_state.units[i].arrayenabled)
914                                         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++)
915                                                 paranoidblah += *p;
916                         }
917                 }
918                 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
919                 {
920                         if (elements[i] < firstvertex || elements[i] >= firstvertex + numvertices)
921                         {
922                                 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in elements list\n", elements[i], firstvertex, firstvertex + numvertices);
923                                 return;
924                         }
925                 }
926                 CHECKGLERROR
927         }
928         if (r_render.integer)
929         {
930                 CHECKGLERROR
931                 if (gl_mesh_testmanualfeeding.integer)
932                 {
933                         unsigned int i, j;
934                         const GLfloat *p;
935                         qglBegin(GL_TRIANGLES);
936                         for (i = 0;i < (unsigned int) numtriangles * 3;i++)
937                         {
938                                 for (j = 0;j < backendarrayunits;j++)
939                                 {
940                                         if (gl_state.units[j].pointer_texcoord && gl_state.units[j].arrayenabled)
941                                         {
942                                                 if (backendarrayunits > 1)
943                                                 {
944                                                         if (gl_state.units[j].arraycomponents == 4)
945                                                         {
946                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 4;
947                                                                 qglMultiTexCoord4f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2], p[3]);
948                                                         }
949                                                         else if (gl_state.units[j].arraycomponents == 3)
950                                                         {
951                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 3;
952                                                                 qglMultiTexCoord3f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2]);
953                                                         }
954                                                         else if (gl_state.units[j].arraycomponents == 2)
955                                                         {
956                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 2;
957                                                                 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, p[0], p[1]);
958                                                         }
959                                                         else
960                                                         {
961                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 1;
962                                                                 qglMultiTexCoord1f(GL_TEXTURE0_ARB + j, p[0]);
963                                                         }
964                                                 }
965                                                 else
966                                                 {
967                                                         if (gl_state.units[j].arraycomponents == 4)
968                                                         {
969                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 4;
970                                                                 qglTexCoord4f(p[0], p[1], p[2], p[3]);
971                                                         }
972                                                         else if (gl_state.units[j].arraycomponents == 3)
973                                                         {
974                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 3;
975                                                                 qglTexCoord3f(p[0], p[1], p[2]);
976                                                         }
977                                                         else if (gl_state.units[j].arraycomponents == 2)
978                                                         {
979                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 2;
980                                                                 qglTexCoord2f(p[0], p[1]);
981                                                         }
982                                                         else
983                                                         {
984                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 1;
985                                                                 qglTexCoord1f(p[0]);
986                                                         }
987                                                 }
988                                         }
989                                 }
990                                 if (gl_state.pointer_color && gl_state.pointer_color_enabled)
991                                 {
992                                         p = ((const GLfloat *)(gl_state.pointer_color)) + elements[i] * 4;
993                                         qglColor4f(p[0], p[1], p[2], p[3]);
994                                 }
995                                 p = ((const GLfloat *)(gl_state.pointer_vertex)) + elements[i] * 3;
996                                 qglVertex3f(p[0], p[1], p[2]);
997                         }
998                         qglEnd();
999                         CHECKGLERROR
1000                 }
1001                 else if (gl_mesh_testarrayelement.integer)
1002                 {
1003                         int i;
1004                         qglBegin(GL_TRIANGLES);
1005                         for (i = 0;i < numtriangles * 3;i++)
1006                         {
1007                                 qglArrayElement(elements[i]);
1008                         }
1009                         qglEnd();
1010                         CHECKGLERROR
1011                 }
1012                 else if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1013                 {
1014                         GL_BindEBO(bufferobject);
1015                         qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices, numelements, GL_UNSIGNED_INT, bufferobject ? (void *)bufferoffset : elements);
1016                         CHECKGLERROR
1017                 }
1018                 else
1019                 {
1020                         GL_BindEBO(bufferobject);
1021                         qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, bufferobject ? (void *)bufferoffset : elements);
1022                         CHECKGLERROR
1023                 }
1024         }
1025 }
1026
1027 // restores backend state, used when done with 3D rendering
1028 void R_Mesh_Finish(void)
1029 {
1030         unsigned int i;
1031         BACKENDACTIVECHECK
1032         CHECKGLERROR
1033         GL_LockArrays(0, 0);
1034         CHECKGLERROR
1035
1036         for (i = 0;i < backendimageunits;i++)
1037         {
1038                 GL_ActiveTexture(i);
1039                 qglBindTexture(GL_TEXTURE_1D, 0);CHECKGLERROR
1040                 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
1041                 if (gl_texture3d)
1042                 {
1043                         qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
1044                 }
1045                 if (gl_texturecubemap)
1046                 {
1047                         qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
1048                 }
1049         }
1050         for (i = 0;i < backendarrayunits;i++)
1051         {
1052                 GL_ActiveTexture(backendarrayunits - 1 - i);
1053                 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1054         }
1055         for (i = 0;i < backendunits;i++)
1056         {
1057                 GL_ActiveTexture(backendunits - 1 - i);
1058                 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1059                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1060                 if (gl_texture3d)
1061                 {
1062                         qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1063                 }
1064                 if (gl_texturecubemap)
1065                 {
1066                         qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1067                 }
1068                 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
1069                 if (gl_combine.integer)
1070                 {
1071                         qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
1072                         qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
1073                 }
1074         }
1075         qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1076         qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
1077
1078         qglDisable(GL_BLEND);CHECKGLERROR
1079         qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1080         qglDepthMask(GL_TRUE);CHECKGLERROR
1081         qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);CHECKGLERROR
1082 }
1083
1084 void R_Mesh_Matrix(const matrix4x4_t *matrix)
1085 {
1086         if (memcmp(matrix, &backend_modelmatrix, sizeof(matrix4x4_t)))
1087         {
1088                 double glmatrix[16];
1089                 backend_modelmatrix = *matrix;
1090                 Matrix4x4_Concat(&backend_modelviewmatrix, &backend_viewmatrix, matrix);
1091                 Matrix4x4_ToArrayDoubleGL(&backend_modelviewmatrix, glmatrix);
1092                 CHECKGLERROR
1093                 qglLoadMatrixd(glmatrix);CHECKGLERROR
1094         }
1095 }
1096
1097 void R_Mesh_VertexPointer(const float *vertex3f, int bufferobject, size_t bufferoffset)
1098 {
1099         if (gl_state.pointer_vertex != vertex3f || gl_state.pointer_vertex_buffer != bufferobject || gl_state.pointer_vertex_offset != bufferoffset)
1100         {
1101                 gl_state.pointer_vertex = vertex3f;
1102                 gl_state.pointer_vertex_buffer = bufferobject;
1103                 gl_state.pointer_vertex_offset = bufferoffset;
1104                 CHECKGLERROR
1105                 GL_BindVBO(bufferobject);
1106                 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), bufferobject ? (void *)bufferoffset : vertex3f);CHECKGLERROR
1107         }
1108 }
1109
1110 void R_Mesh_ColorPointer(const float *color4f, int bufferobject, size_t bufferoffset)
1111 {
1112         if (color4f || bufferobject)
1113         {
1114                 // caller wants color array enabled
1115                 if (!gl_state.pointer_color_enabled)
1116                 {
1117                         gl_state.pointer_color_enabled = true;
1118                         CHECKGLERROR
1119                         qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1120                 }
1121                 if (gl_state.pointer_color != color4f || gl_state.pointer_color_buffer != bufferobject || gl_state.pointer_color_offset != bufferoffset)
1122                 {
1123                         gl_state.pointer_color = color4f;
1124                         gl_state.pointer_color_buffer = bufferobject;
1125                         gl_state.pointer_color_offset = bufferoffset;
1126                         CHECKGLERROR
1127                         GL_BindVBO(bufferobject);
1128                         qglColorPointer(4, GL_FLOAT, sizeof(float[4]), bufferobject ? (void *)bufferoffset : color4f);CHECKGLERROR
1129                 }
1130         }
1131         else
1132         {
1133                 // caller wants color array disabled
1134                 if (gl_state.pointer_color_enabled)
1135                 {
1136                         gl_state.pointer_color_enabled = false;
1137                         CHECKGLERROR
1138                         qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1139                         // when color array is on the glColor gets trashed, set it again
1140                         qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);CHECKGLERROR
1141                 }
1142         }
1143 }
1144
1145 void R_Mesh_TexCoordPointer(unsigned int unitnum, unsigned int numcomponents, const float *texcoord, int bufferobject, size_t bufferoffset)
1146 {
1147         gltextureunit_t *unit = gl_state.units + unitnum;
1148         // update array settings
1149         CHECKGLERROR
1150         if (texcoord || bufferobject)
1151         {
1152                 // texture array unit is enabled, enable the array
1153                 if (!unit->arrayenabled)
1154                 {
1155                         unit->arrayenabled = true;
1156                         GL_ClientActiveTexture(unitnum);
1157                         qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1158                 }
1159                 // texcoord array
1160                 if (unit->pointer_texcoord != texcoord || unit->pointer_texcoord_buffer != bufferobject || unit->pointer_texcoord_offset != bufferoffset || unit->arraycomponents != numcomponents)
1161                 {
1162                         unit->pointer_texcoord = texcoord;
1163                         unit->pointer_texcoord_buffer = bufferobject;
1164                         unit->pointer_texcoord_offset = bufferoffset;
1165                         unit->arraycomponents = numcomponents;
1166                         GL_ClientActiveTexture(unitnum);
1167                         GL_BindVBO(bufferobject);
1168                         qglTexCoordPointer(unit->arraycomponents, GL_FLOAT, sizeof(float) * unit->arraycomponents, bufferobject ? (void *)bufferoffset : texcoord);CHECKGLERROR
1169                 }
1170         }
1171         else
1172         {
1173                 // texture array unit is disabled, disable the array
1174                 if (unit->arrayenabled)
1175                 {
1176                         unit->arrayenabled = false;
1177                         GL_ClientActiveTexture(unitnum);
1178                         qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1179                 }
1180         }
1181 }
1182
1183 void R_Mesh_TexBindAll(unsigned int unitnum, int tex1d, int tex2d, int tex3d, int texcubemap)
1184 {
1185         gltextureunit_t *unit = gl_state.units + unitnum;
1186         if (unitnum >= backendimageunits)
1187                 return;
1188         // update 1d texture binding
1189         if (unit->t1d != tex1d)
1190         {
1191                 GL_ActiveTexture(unitnum);
1192                 if (unitnum < backendunits)
1193                 {
1194                         if (tex1d)
1195                         {
1196                                 if (unit->t1d == 0)
1197                                 {
1198                                         qglEnable(GL_TEXTURE_1D);CHECKGLERROR
1199                                 }
1200                         }
1201                         else
1202                         {
1203                                 if (unit->t1d)
1204                                 {
1205                                         qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1206                                 }
1207                         }
1208                 }
1209                 unit->t1d = tex1d;
1210                 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1211         }
1212         // update 2d texture binding
1213         if (unit->t2d != tex2d)
1214         {
1215                 GL_ActiveTexture(unitnum);
1216                 if (unitnum < backendunits)
1217                 {
1218                         if (tex2d)
1219                         {
1220                                 if (unit->t2d == 0)
1221                                 {
1222                                         qglEnable(GL_TEXTURE_2D);CHECKGLERROR
1223                                 }
1224                         }
1225                         else
1226                         {
1227                                 if (unit->t2d)
1228                                 {
1229                                         qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1230                                 }
1231                         }
1232                 }
1233                 unit->t2d = tex2d;
1234                 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1235         }
1236         // update 3d texture binding
1237         if (unit->t3d != tex3d)
1238         {
1239                 GL_ActiveTexture(unitnum);
1240                 if (unitnum < backendunits)
1241                 {
1242                         if (tex3d)
1243                         {
1244                                 if (unit->t3d == 0)
1245                                 {
1246                                         qglEnable(GL_TEXTURE_3D);CHECKGLERROR
1247                                 }
1248                         }
1249                         else
1250                         {
1251                                 if (unit->t3d)
1252                                 {
1253                                         qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1254                                 }
1255                         }
1256                 }
1257                 unit->t3d = tex3d;
1258                 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1259         }
1260         // update cubemap texture binding
1261         if (unit->tcubemap != texcubemap)
1262         {
1263                 GL_ActiveTexture(unitnum);
1264                 if (unitnum < backendunits)
1265                 {
1266                         if (texcubemap)
1267                         {
1268                                 if (unit->tcubemap == 0)
1269                                 {
1270                                         qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1271                                 }
1272                         }
1273                         else
1274                         {
1275                                 if (unit->tcubemap)
1276                                 {
1277                                         qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1278                                 }
1279                         }
1280                 }
1281                 unit->tcubemap = texcubemap;
1282                 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1283         }
1284 }
1285
1286 void R_Mesh_TexBind1D(unsigned int unitnum, int texnum)
1287 {
1288         gltextureunit_t *unit = gl_state.units + unitnum;
1289         if (unitnum >= backendimageunits)
1290                 return;
1291         // update 1d texture binding
1292         if (unit->t1d != texnum)
1293         {
1294                 GL_ActiveTexture(unitnum);
1295                 if (unitnum < backendunits)
1296                 {
1297                         if (texnum)
1298                         {
1299                                 if (unit->t1d == 0)
1300                                 {
1301                                         qglEnable(GL_TEXTURE_1D);CHECKGLERROR
1302                                 }
1303                         }
1304                         else
1305                         {
1306                                 if (unit->t1d)
1307                                 {
1308                                         qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1309                                 }
1310                         }
1311                 }
1312                 unit->t1d = texnum;
1313                 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1314         }
1315         // update 2d texture binding
1316         if (unit->t2d)
1317         {
1318                 GL_ActiveTexture(unitnum);
1319                 if (unitnum < backendunits)
1320                 {
1321                         if (unit->t2d)
1322                         {
1323                                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1324                         }
1325                 }
1326                 unit->t2d = 0;
1327                 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1328         }
1329         // update 3d texture binding
1330         if (unit->t3d)
1331         {
1332                 GL_ActiveTexture(unitnum);
1333                 if (unitnum < backendunits)
1334                 {
1335                         if (unit->t3d)
1336                         {
1337                                 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1338                         }
1339                 }
1340                 unit->t3d = 0;
1341                 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1342         }
1343         // update cubemap texture binding
1344         if (unit->tcubemap)
1345         {
1346                 GL_ActiveTexture(unitnum);
1347                 if (unitnum < backendunits)
1348                 {
1349                         if (unit->tcubemap)
1350                         {
1351                                 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1352                         }
1353                 }
1354                 unit->tcubemap = 0;
1355                 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1356         }
1357 }
1358
1359 void R_Mesh_TexBind(unsigned int unitnum, int texnum)
1360 {
1361         gltextureunit_t *unit = gl_state.units + unitnum;
1362         if (unitnum >= backendimageunits)
1363                 return;
1364         // update 1d texture binding
1365         if (unit->t1d)
1366         {
1367                 GL_ActiveTexture(unitnum);
1368                 if (unitnum < backendunits)
1369                 {
1370                         if (unit->t1d)
1371                         {
1372                                 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1373                         }
1374                 }
1375                 unit->t1d = 0;
1376                 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1377         }
1378         // update 2d texture binding
1379         if (unit->t2d != texnum)
1380         {
1381                 GL_ActiveTexture(unitnum);
1382                 if (unitnum < backendunits)
1383                 {
1384                         if (texnum)
1385                         {
1386                                 if (unit->t2d == 0)
1387                                 {
1388                                         qglEnable(GL_TEXTURE_2D);CHECKGLERROR
1389                                 }
1390                         }
1391                         else
1392                         {
1393                                 if (unit->t2d)
1394                                 {
1395                                         qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1396                                 }
1397                         }
1398                 }
1399                 unit->t2d = texnum;
1400                 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1401         }
1402         // update 3d texture binding
1403         if (unit->t3d)
1404         {
1405                 GL_ActiveTexture(unitnum);
1406                 if (unitnum < backendunits)
1407                 {
1408                         if (unit->t3d)
1409                         {
1410                                 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1411                         }
1412                 }
1413                 unit->t3d = 0;
1414                 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1415         }
1416         // update cubemap texture binding
1417         if (unit->tcubemap != 0)
1418         {
1419                 GL_ActiveTexture(unitnum);
1420                 if (unitnum < backendunits)
1421                 {
1422                         if (unit->tcubemap)
1423                         {
1424                                 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1425                         }
1426                 }
1427                 unit->tcubemap = 0;
1428                 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1429         }
1430 }
1431
1432 void R_Mesh_TexBind3D(unsigned int unitnum, int texnum)
1433 {
1434         gltextureunit_t *unit = gl_state.units + unitnum;
1435         if (unitnum >= backendimageunits)
1436                 return;
1437         // update 1d texture binding
1438         if (unit->t1d)
1439         {
1440                 GL_ActiveTexture(unitnum);
1441                 if (unitnum < backendunits)
1442                 {
1443                         if (unit->t1d)
1444                         {
1445                                 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1446                         }
1447                 }
1448                 unit->t1d = 0;
1449                 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1450         }
1451         // update 2d texture binding
1452         if (unit->t2d)
1453         {
1454                 GL_ActiveTexture(unitnum);
1455                 if (unitnum < backendunits)
1456                 {
1457                         if (unit->t2d)
1458                         {
1459                                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1460                         }
1461                 }
1462                 unit->t2d = 0;
1463                 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1464         }
1465         // update 3d texture binding
1466         if (unit->t3d != texnum)
1467         {
1468                 GL_ActiveTexture(unitnum);
1469                 if (unitnum < backendunits)
1470                 {
1471                         if (texnum)
1472                         {
1473                                 if (unit->t3d == 0)
1474                                 {
1475                                         qglEnable(GL_TEXTURE_3D);CHECKGLERROR
1476                                 }
1477                         }
1478                         else
1479                         {
1480                                 if (unit->t3d)
1481                                 {
1482                                         qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1483                                 }
1484                         }
1485                 }
1486                 unit->t3d = texnum;
1487                 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1488         }
1489         // update cubemap texture binding
1490         if (unit->tcubemap != 0)
1491         {
1492                 GL_ActiveTexture(unitnum);
1493                 if (unitnum < backendunits)
1494                 {
1495                         if (unit->tcubemap)
1496                         {
1497                                 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1498                         }
1499                 }
1500                 unit->tcubemap = 0;
1501                 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1502         }
1503 }
1504
1505 void R_Mesh_TexBindCubeMap(unsigned int unitnum, int texnum)
1506 {
1507         gltextureunit_t *unit = gl_state.units + unitnum;
1508         if (unitnum >= backendimageunits)
1509                 return;
1510         // update 1d texture binding
1511         if (unit->t1d)
1512         {
1513                 GL_ActiveTexture(unitnum);
1514                 if (unitnum < backendunits)
1515                 {
1516                         if (unit->t1d)
1517                         {
1518                                 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1519                         }
1520                 }
1521                 unit->t1d = 0;
1522                 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1523         }
1524         // update 2d texture binding
1525         if (unit->t2d)
1526         {
1527                 GL_ActiveTexture(unitnum);
1528                 if (unitnum < backendunits)
1529                 {
1530                         if (unit->t2d)
1531                         {
1532                                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1533                         }
1534                 }
1535                 unit->t2d = 0;
1536                 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1537         }
1538         // update 3d texture binding
1539         if (unit->t3d)
1540         {
1541                 GL_ActiveTexture(unitnum);
1542                 if (unitnum < backendunits)
1543                 {
1544                         if (unit->t3d)
1545                         {
1546                                 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1547                         }
1548                 }
1549                 unit->t3d = 0;
1550                 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1551         }
1552         // update cubemap texture binding
1553         if (unit->tcubemap != texnum)
1554         {
1555                 GL_ActiveTexture(unitnum);
1556                 if (unitnum < backendunits)
1557                 {
1558                         if (texnum)
1559                         {
1560                                 if (unit->tcubemap == 0)
1561                                 {
1562                                         qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1563                                 }
1564                         }
1565                         else
1566                         {
1567                                 if (unit->tcubemap)
1568                                 {
1569                                         qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1570                                 }
1571                         }
1572                 }
1573                 unit->tcubemap = texnum;
1574                 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1575         }
1576 }
1577
1578 void R_Mesh_TexMatrix(unsigned int unitnum, const matrix4x4_t *matrix)
1579 {
1580         gltextureunit_t *unit = gl_state.units + unitnum;
1581         if (matrix->m[3][3])
1582         {
1583                 // texmatrix specified, check if it is different
1584                 if (!unit->texmatrixenabled || memcmp(&unit->matrix, matrix, sizeof(matrix4x4_t)))
1585                 {
1586                         double glmatrix[16];
1587                         unit->texmatrixenabled = true;
1588                         unit->matrix = *matrix;
1589                         CHECKGLERROR
1590                         Matrix4x4_ToArrayDoubleGL(&unit->matrix, glmatrix);
1591                         qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1592                         GL_ActiveTexture(unitnum);
1593                         qglLoadMatrixd(glmatrix);CHECKGLERROR
1594                         qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1595                 }
1596         }
1597         else
1598         {
1599                 // no texmatrix specified, revert to identity
1600                 if (unit->texmatrixenabled)
1601                 {
1602                         unit->texmatrixenabled = false;
1603                         CHECKGLERROR
1604                         qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1605                         GL_ActiveTexture(unitnum);
1606                         qglLoadIdentity();CHECKGLERROR
1607                         qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1608                 }
1609         }
1610 }
1611
1612 void R_Mesh_TexCombine(unsigned int unitnum, int combinergb, int combinealpha, int rgbscale, int alphascale)
1613 {
1614         gltextureunit_t *unit = gl_state.units + unitnum;
1615         CHECKGLERROR
1616         if (gl_combine.integer)
1617         {
1618                 // GL_ARB_texture_env_combine
1619                 if (!combinergb)
1620                         combinergb = GL_MODULATE;
1621                 if (!combinealpha)
1622                         combinealpha = GL_MODULATE;
1623                 if (!rgbscale)
1624                         rgbscale = 1;
1625                 if (!alphascale)
1626                         alphascale = 1;
1627                 if (unit->combinergb != combinergb)
1628                 {
1629                         unit->combinergb = combinergb;
1630                         GL_ActiveTexture(unitnum);
1631                         qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
1632                 }
1633                 if (unit->combinealpha != combinealpha)
1634                 {
1635                         unit->combinealpha = combinealpha;
1636                         GL_ActiveTexture(unitnum);
1637                         qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
1638                 }
1639                 if (unit->rgbscale != rgbscale)
1640                 {
1641                         GL_ActiveTexture(unitnum);
1642                         qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (unit->rgbscale = rgbscale));CHECKGLERROR
1643                 }
1644                 if (unit->alphascale != alphascale)
1645                 {
1646                         GL_ActiveTexture(unitnum);
1647                         qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, (unit->alphascale = alphascale));CHECKGLERROR
1648                 }
1649         }
1650         else
1651         {
1652                 // normal GL texenv
1653                 if (!combinergb)
1654                         combinergb = GL_MODULATE;
1655                 if (unit->combinergb != combinergb)
1656                 {
1657                         unit->combinergb = combinergb;
1658                         GL_ActiveTexture(unitnum);
1659                         qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combinergb);CHECKGLERROR
1660                 }
1661         }
1662 }
1663
1664 void R_Mesh_TextureState(const rmeshstate_t *m)
1665 {
1666         unsigned int i;
1667
1668         BACKENDACTIVECHECK
1669
1670         CHECKGLERROR
1671         if (gl_backend_rebindtextures)
1672         {
1673                 gl_backend_rebindtextures = false;
1674                 GL_SetupTextureState();
1675                 CHECKGLERROR
1676         }
1677
1678         for (i = 0;i < backendimageunits;i++)
1679                 R_Mesh_TexBindAll(i, m->tex1d[i], m->tex[i], m->tex3d[i], m->texcubemap[i]);
1680         for (i = 0;i < backendarrayunits;i++)
1681         {
1682                 if (m->pointer_texcoord3f[i])
1683                         R_Mesh_TexCoordPointer(i, 3, m->pointer_texcoord3f[i], 0, 0);
1684                 else
1685                         R_Mesh_TexCoordPointer(i, 2, m->pointer_texcoord[i], 0, 0);
1686         }
1687         for (i = 0;i < backendunits;i++)
1688         {
1689                 R_Mesh_TexMatrix(i, &m->texmatrix[i]);
1690                 R_Mesh_TexCombine(i, m->texcombinergb[i], m->texcombinealpha[i], m->texrgbscale[i], m->texalphascale[i]);
1691         }
1692         CHECKGLERROR
1693 }
1694
1695 void R_Mesh_ResetTextureState(void)
1696 {
1697         unsigned int unitnum;
1698
1699         BACKENDACTIVECHECK
1700
1701         CHECKGLERROR
1702         if (gl_backend_rebindtextures)
1703         {
1704                 gl_backend_rebindtextures = false;
1705                 GL_SetupTextureState();
1706                 CHECKGLERROR
1707         }
1708
1709         for (unitnum = 0;unitnum < backendimageunits;unitnum++)
1710         {
1711                 gltextureunit_t *unit = gl_state.units + unitnum;
1712                 // update 1d texture binding
1713                 if (unit->t1d)
1714                 {
1715                         GL_ActiveTexture(unitnum);
1716                         if (unitnum < backendunits)
1717                         {
1718                                 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1719                         }
1720                         unit->t1d = 0;
1721                         qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1722                 }
1723                 // update 2d texture binding
1724                 if (unit->t2d)
1725                 {
1726                         GL_ActiveTexture(unitnum);
1727                         if (unitnum < backendunits)
1728                         {
1729                                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1730                         }
1731                         unit->t2d = 0;
1732                         qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1733                 }
1734                 // update 3d texture binding
1735                 if (unit->t3d)
1736                 {
1737                         GL_ActiveTexture(unitnum);
1738                         if (unitnum < backendunits)
1739                         {
1740                                 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1741                         }
1742                         unit->t3d = 0;
1743                         qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1744                 }
1745                 // update cubemap texture binding
1746                 if (unit->tcubemap)
1747                 {
1748                         GL_ActiveTexture(unitnum);
1749                         if (unitnum < backendunits)
1750                         {
1751                                 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1752                         }
1753                         unit->tcubemap = 0;
1754                         qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1755                 }
1756         }
1757         for (unitnum = 0;unitnum < backendarrayunits;unitnum++)
1758         {
1759                 gltextureunit_t *unit = gl_state.units + unitnum;
1760                 // texture array unit is disabled, disable the array
1761                 if (unit->arrayenabled)
1762                 {
1763                         unit->arrayenabled = false;
1764                         GL_ClientActiveTexture(unitnum);
1765                         qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1766                 }
1767         }
1768         for (unitnum = 0;unitnum < backendunits;unitnum++)
1769         {
1770                 gltextureunit_t *unit = gl_state.units + unitnum;
1771                 // no texmatrix specified, revert to identity
1772                 if (unit->texmatrixenabled)
1773                 {
1774                         unit->texmatrixenabled = false;
1775                         CHECKGLERROR
1776                         qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1777                         GL_ActiveTexture(unitnum);
1778                         qglLoadIdentity();CHECKGLERROR
1779                         qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1780                 }
1781                 if (gl_combine.integer)
1782                 {
1783                         // GL_ARB_texture_env_combine
1784                         if (unit->combinergb != GL_MODULATE)
1785                         {
1786                                 unit->combinergb = GL_MODULATE;
1787                                 GL_ActiveTexture(unitnum);
1788                                 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
1789                         }
1790                         if (unit->combinealpha != GL_MODULATE)
1791                         {
1792                                 unit->combinealpha = GL_MODULATE;
1793                                 GL_ActiveTexture(unitnum);
1794                                 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
1795                         }
1796                         if (unit->rgbscale != 1)
1797                         {
1798                                 GL_ActiveTexture(unitnum);
1799                                 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (unit->rgbscale = 1));CHECKGLERROR
1800                         }
1801                         if (unit->alphascale != 1)
1802                         {
1803                                 GL_ActiveTexture(unitnum);
1804                                 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, (unit->alphascale = 1));CHECKGLERROR
1805                         }
1806                 }
1807                 else
1808                 {
1809                         // normal GL texenv
1810                         if (unit->combinergb != GL_MODULATE)
1811                         {
1812                                 unit->combinergb = GL_MODULATE;
1813                                 GL_ActiveTexture(unitnum);
1814                                 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combinergb);CHECKGLERROR
1815                         }
1816                 }
1817         }
1818 }
1819
1820 void R_Mesh_Draw_ShowTris(int firstvertex, int numvertices, int numtriangles, const int *elements)
1821 {
1822         CHECKGLERROR
1823         qglBegin(GL_LINES);
1824         for (;numtriangles;numtriangles--, elements += 3)
1825         {
1826                 qglArrayElement(elements[0]);qglArrayElement(elements[1]);
1827                 qglArrayElement(elements[1]);qglArrayElement(elements[2]);
1828                 qglArrayElement(elements[2]);qglArrayElement(elements[0]);
1829         }
1830         qglEnd();
1831         CHECKGLERROR
1832 }