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