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