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