]> icculus.org git repositories - divverent/darkplaces.git/blob - gl_backend.c
removed the flawed detection of modelspace deluxemaps (it was always concluding they...
[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 scissortest;
400         unsigned int unit;
401         unsigned int clientunit;
402         gltextureunit_t units[MAX_TEXTUREUNITS];
403         float color4f[4];
404         int lockrange_first;
405         int lockrange_count;
406         const void *pointer_vertex;
407         const void *pointer_color;
408 }
409 gl_state;
410
411 void GL_SetupTextureState(void)
412 {
413         unsigned int i;
414         gltextureunit_t *unit;
415         CHECKGLERROR
416         gl_state.unit = MAX_TEXTUREUNITS;
417         gl_state.clientunit = MAX_TEXTUREUNITS;
418         for (i = 0;i < MAX_TEXTUREUNITS;i++)
419         {
420                 unit = gl_state.units + i;
421                 unit->t1d = 0;
422                 unit->t2d = 0;
423                 unit->t3d = 0;
424                 unit->tcubemap = 0;
425                 unit->arrayenabled = false;
426                 unit->arraycomponents = 0;
427                 unit->pointer_texcoord = NULL;
428                 unit->rgbscale = 1;
429                 unit->alphascale = 1;
430                 unit->combinergb = GL_MODULATE;
431                 unit->combinealpha = GL_MODULATE;
432                 unit->texmatrixenabled = false;
433                 unit->matrix = identitymatrix;
434         }
435
436         for (i = 0;i < backendimageunits;i++)
437         {
438                 GL_ActiveTexture(i);
439                 qglBindTexture(GL_TEXTURE_1D, 0);CHECKGLERROR
440                 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
441                 if (gl_texture3d)
442                 {
443                         qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
444                 }
445                 if (gl_texturecubemap)
446                 {
447                         qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
448                 }
449         }
450
451         for (i = 0;i < backendarrayunits;i++)
452         {
453                 GL_ClientActiveTexture(i);
454                 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
455                 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
456         }
457
458         for (i = 0;i < backendunits;i++)
459         {
460                 GL_ActiveTexture(i);
461                 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
462                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
463                 if (gl_texture3d)
464                 {
465                         qglDisable(GL_TEXTURE_3D);CHECKGLERROR
466                 }
467                 if (gl_texturecubemap)
468                 {
469                         qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
470                 }
471                 qglMatrixMode(GL_TEXTURE);
472                 qglLoadIdentity();
473                 qglMatrixMode(GL_MODELVIEW);
474                 if (gl_combine.integer)
475                 {
476                         qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);CHECKGLERROR
477                         qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);CHECKGLERROR
478                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);CHECKGLERROR
479                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
480                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_TEXTURE);CHECKGLERROR // for GL_INTERPOLATE_ARB mode
481                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
482                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
483                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);CHECKGLERROR
484                         qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);CHECKGLERROR
485                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);CHECKGLERROR
486                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
487                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, GL_CONSTANT_ARB);CHECKGLERROR
488                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
489                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
490                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
491                         qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
492                         qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
493                 }
494                 else
495                 {
496                         qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
497                 }
498         }
499         CHECKGLERROR
500 }
501
502 void GL_Backend_ResetState(void)
503 {
504         memset(&gl_state, 0, sizeof(gl_state));
505         gl_state.depthtest = true;
506         gl_state.blendfunc1 = GL_ONE;
507         gl_state.blendfunc2 = GL_ZERO;
508         gl_state.blend = false;
509         gl_state.depthmask = GL_TRUE;
510         gl_state.colormask = 15;
511         gl_state.color4f[0] = gl_state.color4f[1] = gl_state.color4f[2] = gl_state.color4f[3] = 1;
512         gl_state.lockrange_first = 0;
513         gl_state.lockrange_count = 0;
514         gl_state.pointer_vertex = NULL;
515         gl_state.pointer_color = NULL;
516
517         CHECKGLERROR
518
519         qglColorMask(1, 1, 1, 1);
520         qglAlphaFunc(GL_GEQUAL, 0.5);CHECKGLERROR
521         qglDisable(GL_ALPHA_TEST);CHECKGLERROR
522         qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
523         qglDisable(GL_BLEND);CHECKGLERROR
524         qglCullFace(GL_FRONT);CHECKGLERROR
525         qglEnable(GL_CULL_FACE);CHECKGLERROR
526         qglDepthFunc(GL_LEQUAL);CHECKGLERROR
527         qglEnable(GL_DEPTH_TEST);CHECKGLERROR
528         qglDepthMask(gl_state.depthmask);CHECKGLERROR
529
530         qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), NULL);CHECKGLERROR
531         qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
532
533         qglColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL);CHECKGLERROR
534         qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
535
536         GL_Color(0, 0, 0, 0);
537         GL_Color(1, 1, 1, 1);
538
539         GL_SetupTextureState();
540 }
541
542 void GL_ActiveTexture(unsigned int num)
543 {
544         if (gl_state.unit != num)
545         {
546                 gl_state.unit = num;
547                 if (qglActiveTexture)
548                 {
549                         qglActiveTexture(GL_TEXTURE0_ARB + gl_state.unit);
550                         CHECKGLERROR
551                 }
552         }
553 }
554
555 void GL_ClientActiveTexture(unsigned int num)
556 {
557         if (gl_state.clientunit != num)
558         {
559                 gl_state.clientunit = num;
560                 if (qglActiveTexture)
561                 {
562                         qglClientActiveTexture(GL_TEXTURE0_ARB + gl_state.clientunit);
563                         CHECKGLERROR
564                 }
565         }
566 }
567
568 void GL_BlendFunc(int blendfunc1, int blendfunc2)
569 {
570         if (gl_state.blendfunc1 != blendfunc1 || gl_state.blendfunc2 != blendfunc2)
571         {
572                 qglBlendFunc(gl_state.blendfunc1 = blendfunc1, gl_state.blendfunc2 = blendfunc2);CHECKGLERROR
573                 if (gl_state.blendfunc2 == GL_ZERO)
574                 {
575                         if (gl_state.blendfunc1 == GL_ONE)
576                         {
577                                 if (gl_state.blend)
578                                 {
579                                         gl_state.blend = 0;
580                                         qglDisable(GL_BLEND);CHECKGLERROR
581                                 }
582                         }
583                         else
584                         {
585                                 if (!gl_state.blend)
586                                 {
587                                         gl_state.blend = 1;
588                                         qglEnable(GL_BLEND);CHECKGLERROR
589                                 }
590                         }
591                 }
592                 else
593                 {
594                         if (!gl_state.blend)
595                         {
596                                 gl_state.blend = 1;
597                                 qglEnable(GL_BLEND);CHECKGLERROR
598                         }
599                 }
600         }
601 }
602
603 void GL_DepthMask(int state)
604 {
605         if (gl_state.depthmask != state)
606         {
607                 qglDepthMask(gl_state.depthmask = state);CHECKGLERROR
608         }
609 }
610
611 void GL_DepthTest(int state)
612 {
613         if (gl_state.depthtest != state)
614         {
615                 gl_state.depthtest = state;
616                 if (gl_state.depthtest)
617                 {
618                         qglEnable(GL_DEPTH_TEST);CHECKGLERROR
619                 }
620                 else
621                 {
622                         qglDisable(GL_DEPTH_TEST);CHECKGLERROR
623                 }
624         }
625 }
626
627 void GL_ColorMask(int r, int g, int b, int a)
628 {
629         int state = r*8 + g*4 + b*2 + a*1;
630         if (gl_state.colormask != state)
631         {
632                 gl_state.colormask = state;
633                 qglColorMask((GLboolean)r, (GLboolean)g, (GLboolean)b, (GLboolean)a);CHECKGLERROR
634         }
635 }
636
637 void GL_Color(float cr, float cg, float cb, float ca)
638 {
639         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)
640         {
641                 gl_state.color4f[0] = cr;
642                 gl_state.color4f[1] = cg;
643                 gl_state.color4f[2] = cb;
644                 gl_state.color4f[3] = ca;
645                 CHECKGLERROR
646                 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);
647                 CHECKGLERROR
648         }
649 }
650
651 void GL_LockArrays(int first, int count)
652 {
653         if (gl_state.lockrange_count != count || gl_state.lockrange_first != first)
654         {
655                 if (gl_state.lockrange_count)
656                 {
657                         gl_state.lockrange_count = 0;
658                         CHECKGLERROR
659                         qglUnlockArraysEXT();
660                         CHECKGLERROR
661                 }
662                 if (count && gl_supportslockarrays && gl_lockarrays.integer && r_render.integer)
663                 {
664                         gl_state.lockrange_first = first;
665                         gl_state.lockrange_count = count;
666                         CHECKGLERROR
667                         qglLockArraysEXT(first, count);
668                         CHECKGLERROR
669                 }
670         }
671 }
672
673 void GL_Scissor (int x, int y, int width, int height)
674 {
675         CHECKGLERROR
676         qglScissor(x, vid.height - (y + height),width,height);
677         CHECKGLERROR
678 }
679
680 void GL_ScissorTest(int state)
681 {
682         if(gl_state.scissortest == state)
683                 return;
684
685         CHECKGLERROR
686         if((gl_state.scissortest = state))
687                 qglEnable(GL_SCISSOR_TEST);
688         else
689                 qglDisable(GL_SCISSOR_TEST);
690         CHECKGLERROR
691 }
692
693 void GL_Clear(int mask)
694 {
695         qglClear(mask);CHECKGLERROR
696 }
697
698 void GL_TransformToScreen(const vec4_t in, vec4_t out)
699 {
700         vec4_t temp;
701         float iw;
702         Matrix4x4_Transform4 (&backend_viewmatrix, in, temp);
703         Matrix4x4_Transform4 (&backend_projectmatrix, temp, out);
704         iw = 1.0f / out[3];
705         out[0] = r_view_x + (out[0] * iw + 1.0f) * r_view_width * 0.5f;
706         out[1] = r_view_y + (out[1] * iw + 1.0f) * r_view_height * 0.5f;
707         out[2] = r_view_z + (out[2] * iw + 1.0f) * r_view_depth * 0.5f;
708 }
709
710 // called at beginning of frame
711 void R_Mesh_Start(void)
712 {
713         BACKENDACTIVECHECK
714         CHECKGLERROR
715         GL_Backend_ResetState();
716 }
717
718 unsigned int GL_Backend_CompileProgram(int vertexstrings_count, const char **vertexstrings_list, int fragmentstrings_count, const char **fragmentstrings_list)
719 {
720         GLint vertexshadercompiled, fragmentshadercompiled, programlinked;
721         GLuint vertexshaderobject, fragmentshaderobject, programobject = 0;
722         char compilelog[MAX_INPUTLINE];
723         CHECKGLERROR
724
725         programobject = qglCreateProgramObjectARB();
726         CHECKGLERROR
727         if (!programobject)
728                 return 0;
729
730         if (developer.integer >= 100)
731         {
732                 int i;
733                 Con_Printf("Compiling shader:\n");
734                 if (vertexstrings_count)
735                 {
736                         Con_Printf("------ VERTEX SHADER ------\n");
737                         for (i = 0;i < vertexstrings_count;i++)
738                                 Con_Print(vertexstrings_list[i]);
739                         Con_Print("\n");
740                 }
741                 if (fragmentstrings_count)
742                 {
743                         Con_Printf("------ FRAGMENT SHADER ------\n");
744                         for (i = 0;i < fragmentstrings_count;i++)
745                                 Con_Print(fragmentstrings_list[i]);
746                         Con_Print("\n");
747                 }
748         }
749
750         if (vertexstrings_count)
751         {
752                 CHECKGLERROR
753                 vertexshaderobject = qglCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
754                 if (!vertexshaderobject)
755                 {
756                         qglDeleteObjectARB(programobject);
757                         CHECKGLERROR
758                         return 0;
759                 }
760                 qglShaderSourceARB(vertexshaderobject, vertexstrings_count, vertexstrings_list, NULL);
761                 qglCompileShaderARB(vertexshaderobject);
762                 CHECKGLERROR
763                 qglGetObjectParameterivARB(vertexshaderobject, GL_OBJECT_COMPILE_STATUS_ARB, &vertexshadercompiled);
764                 qglGetInfoLogARB(vertexshaderobject, sizeof(compilelog), NULL, compilelog);
765                 if (compilelog[0])
766                         Con_DPrintf("vertex shader compile log:\n%s\n", compilelog);
767                 if (!vertexshadercompiled)
768                 {
769                         qglDeleteObjectARB(programobject);
770                         qglDeleteObjectARB(vertexshaderobject);
771                         CHECKGLERROR
772                         return 0;
773                 }
774                 qglAttachObjectARB(programobject, vertexshaderobject);
775                 qglDeleteObjectARB(vertexshaderobject);
776                 CHECKGLERROR
777         }
778
779         if (fragmentstrings_count)
780         {
781                 CHECKGLERROR
782                 fragmentshaderobject = qglCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
783                 if (!fragmentshaderobject)
784                 {
785                         qglDeleteObjectARB(programobject);
786                         CHECKGLERROR
787                         return 0;
788                 }
789                 qglShaderSourceARB(fragmentshaderobject, fragmentstrings_count, fragmentstrings_list, NULL);
790                 qglCompileShaderARB(fragmentshaderobject);
791                 CHECKGLERROR
792                 qglGetObjectParameterivARB(fragmentshaderobject, GL_OBJECT_COMPILE_STATUS_ARB, &fragmentshadercompiled);
793                 qglGetInfoLogARB(fragmentshaderobject, sizeof(compilelog), NULL, compilelog);
794                 if (compilelog[0])
795                         Con_DPrintf("fragment shader compile log:\n%s\n", compilelog);
796                 if (!fragmentshadercompiled)
797                 {
798                         qglDeleteObjectARB(programobject);
799                         qglDeleteObjectARB(fragmentshaderobject);
800                         CHECKGLERROR
801                         return 0;
802                 }
803                 qglAttachObjectARB(programobject, fragmentshaderobject);
804                 qglDeleteObjectARB(fragmentshaderobject);
805                 CHECKGLERROR
806         }
807
808         qglLinkProgramARB(programobject);
809         CHECKGLERROR
810         qglGetObjectParameterivARB(programobject, GL_OBJECT_LINK_STATUS_ARB, &programlinked);
811         qglGetInfoLogARB(programobject, sizeof(compilelog), NULL, compilelog);
812         if (compilelog[0])
813         {
814                 Con_DPrintf("program link log:\n%s\n", compilelog);
815                 // software vertex shader is ok but software fragment shader is WAY
816                 // too slow, fail program if so.
817                 // NOTE: this string might be ATI specific, but that's ok because the
818                 // ATI R300 chip (Radeon 9500-9800/X300) is the most likely to use a
819                 // software fragment shader due to low instruction and dependent
820                 // texture limits.
821                 if (strstr(compilelog, "fragment shader will run in software"))
822                         programlinked = false;
823         }
824         CHECKGLERROR
825         if (!programlinked)
826         {
827                 qglDeleteObjectARB(programobject);
828                 return 0;
829         }
830         CHECKGLERROR
831         return programobject;
832 }
833
834 void GL_Backend_FreeProgram(unsigned int prog)
835 {
836         CHECKGLERROR
837         qglDeleteObjectARB(prog);
838         CHECKGLERROR
839 }
840
841 int gl_backend_rebindtextures;
842
843 void GL_Backend_RenumberElements(int *out, int count, const int *in, int offset)
844 {
845         int i;
846         if (offset)
847         {
848                 for (i = 0;i < count;i++)
849                         *out++ = *in++ + offset;
850         }
851         else
852                 memcpy(out, in, sizeof(*out) * count);
853 }
854
855 // renders triangles using vertices from the active arrays
856 int paranoidblah = 0;
857 void R_Mesh_Draw(int firstvertex, int numvertices, int numtriangles, const int *elements)
858 {
859         unsigned int numelements = numtriangles * 3;
860         if (numvertices < 3 || numtriangles < 1)
861         {
862                 Con_Printf("R_Mesh_Draw(%d, %d, %d, %08p);\n", firstvertex, numvertices, numtriangles, elements);
863                 return;
864         }
865         //CHECKGLERROR
866         renderstats.meshes++;
867         renderstats.meshes_elements += numelements;
868         if (gl_paranoid.integer)
869         {
870                 unsigned int i, j, size;
871                 const int *p;
872                 if (!qglIsEnabled(GL_VERTEX_ARRAY))
873                         Con_Print("R_Mesh_Draw: vertex array not enabled\n");
874                 for (j = 0, size = numvertices * 3, p = (int *)((float *)gl_state.pointer_vertex + firstvertex * 3);j < size;j++, p++)
875                         paranoidblah += *p;
876                 if (gl_state.pointer_color)
877                 {
878                         if (!qglIsEnabled(GL_COLOR_ARRAY))
879                                 Con_Print("R_Mesh_Draw: color array set but not enabled\n");
880                         for (j = 0, size = numvertices * 4, p = (int *)((float *)gl_state.pointer_color + firstvertex * 4);j < size;j++, p++)
881                                 paranoidblah += *p;
882                 }
883                 for (i = 0;i < backendarrayunits;i++)
884                 {
885                         if (gl_state.units[i].arrayenabled)
886                         {
887                                 GL_ClientActiveTexture(i);
888                                 if (!qglIsEnabled(GL_TEXTURE_COORD_ARRAY))
889                                         Con_Print("R_Mesh_Draw: texcoord array set but not enabled\n");
890                                 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++)
891                                         paranoidblah += *p;
892                         }
893                 }
894                 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
895                 {
896                         if (elements[i] < firstvertex || elements[i] >= firstvertex + numvertices)
897                         {
898                                 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in elements list\n", elements[i], firstvertex, firstvertex + numvertices);
899                                 return;
900                         }
901                 }
902                 CHECKGLERROR
903         }
904         if (r_render.integer)
905         {
906                 CHECKGLERROR
907                 if (gl_mesh_testmanualfeeding.integer)
908                 {
909                         unsigned int i, j;
910                         const GLfloat *p;
911                         qglBegin(GL_TRIANGLES);
912                         for (i = 0;i < (unsigned int) numtriangles * 3;i++)
913                         {
914                                 for (j = 0;j < backendarrayunits;j++)
915                                 {
916                                         if (gl_state.units[j].pointer_texcoord)
917                                         {
918                                                 if (backendarrayunits > 1)
919                                                 {
920                                                         if (gl_state.units[j].arraycomponents == 4)
921                                                         {
922                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 4;
923                                                                 qglMultiTexCoord4f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2], p[3]);
924                                                         }
925                                                         else if (gl_state.units[j].arraycomponents == 3)
926                                                         {
927                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 3;
928                                                                 qglMultiTexCoord3f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2]);
929                                                         }
930                                                         else if (gl_state.units[j].arraycomponents == 2)
931                                                         {
932                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 2;
933                                                                 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, p[0], p[1]);
934                                                         }
935                                                         else
936                                                         {
937                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 1;
938                                                                 qglMultiTexCoord1f(GL_TEXTURE0_ARB + j, p[0]);
939                                                         }
940                                                 }
941                                                 else
942                                                 {
943                                                         if (gl_state.units[j].arraycomponents == 4)
944                                                         {
945                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 4;
946                                                                 qglTexCoord4f(p[0], p[1], p[2], p[3]);
947                                                         }
948                                                         else if (gl_state.units[j].arraycomponents == 3)
949                                                         {
950                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 3;
951                                                                 qglTexCoord3f(p[0], p[1], p[2]);
952                                                         }
953                                                         else if (gl_state.units[j].arraycomponents == 2)
954                                                         {
955                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 2;
956                                                                 qglTexCoord2f(p[0], p[1]);
957                                                         }
958                                                         else
959                                                         {
960                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 1;
961                                                                 qglTexCoord1f(p[0]);
962                                                         }
963                                                 }
964                                         }
965                                 }
966                                 if (gl_state.pointer_color)
967                                 {
968                                         p = ((const GLfloat *)(gl_state.pointer_color)) + elements[i] * 4;
969                                         qglColor4f(p[0], p[1], p[2], p[3]);
970                                 }
971                                 p = ((const GLfloat *)(gl_state.pointer_vertex)) + elements[i] * 3;
972                                 qglVertex3f(p[0], p[1], p[2]);
973                         }
974                         qglEnd();
975                         CHECKGLERROR
976                 }
977                 else if (gl_mesh_testarrayelement.integer)
978                 {
979                         int i;
980                         qglBegin(GL_TRIANGLES);
981                         for (i = 0;i < numtriangles * 3;i++)
982                         {
983                                 qglArrayElement(elements[i]);
984                         }
985                         qglEnd();
986                         CHECKGLERROR
987                 }
988                 else if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
989                 {
990                         qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices, numelements, GL_UNSIGNED_INT, elements);
991                         CHECKGLERROR
992                 }
993                 else
994                 {
995                         qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, elements);
996                         CHECKGLERROR
997                 }
998         }
999 }
1000
1001 // restores backend state, used when done with 3D rendering
1002 void R_Mesh_Finish(void)
1003 {
1004         unsigned int i;
1005         BACKENDACTIVECHECK
1006         CHECKGLERROR
1007         GL_LockArrays(0, 0);
1008         CHECKGLERROR
1009
1010         for (i = 0;i < backendimageunits;i++)
1011         {
1012                 GL_ActiveTexture(i);
1013                 qglBindTexture(GL_TEXTURE_1D, 0);CHECKGLERROR
1014                 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
1015                 if (gl_texture3d)
1016                 {
1017                         qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
1018                 }
1019                 if (gl_texturecubemap)
1020                 {
1021                         qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
1022                 }
1023         }
1024         for (i = 0;i < backendarrayunits;i++)
1025         {
1026                 GL_ActiveTexture(backendarrayunits - 1 - i);
1027                 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1028         }
1029         for (i = 0;i < backendunits;i++)
1030         {
1031                 GL_ActiveTexture(backendunits - 1 - i);
1032                 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1033                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1034                 if (gl_texture3d)
1035                 {
1036                         qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1037                 }
1038                 if (gl_texturecubemap)
1039                 {
1040                         qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1041                 }
1042                 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
1043                 if (gl_combine.integer)
1044                 {
1045                         qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
1046                         qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
1047                 }
1048         }
1049         qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1050         qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
1051
1052         qglDisable(GL_BLEND);CHECKGLERROR
1053         qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1054         qglDepthMask(GL_TRUE);CHECKGLERROR
1055         qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);CHECKGLERROR
1056 }
1057
1058 void R_Mesh_Matrix(const matrix4x4_t *matrix)
1059 {
1060         if (memcmp(matrix, &backend_modelmatrix, sizeof(matrix4x4_t)))
1061         {
1062                 backend_modelmatrix = *matrix;
1063                 Matrix4x4_Concat(&backend_modelviewmatrix, &backend_viewmatrix, matrix);
1064                 Matrix4x4_Transpose(&backend_glmodelviewmatrix, &backend_modelviewmatrix);
1065                 qglLoadMatrixf(&backend_glmodelviewmatrix.m[0][0]);
1066         }
1067 }
1068
1069 void R_Mesh_VertexPointer(const float *vertex3f)
1070 {
1071         if (gl_state.pointer_vertex != vertex3f)
1072         {
1073                 gl_state.pointer_vertex = vertex3f;
1074                 CHECKGLERROR
1075                 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), gl_state.pointer_vertex);
1076                 CHECKGLERROR
1077         }
1078 }
1079
1080 void R_Mesh_ColorPointer(const float *color4f)
1081 {
1082         if (gl_state.pointer_color != color4f)
1083         {
1084                 CHECKGLERROR
1085                 if (!gl_state.pointer_color)
1086                 {
1087                         qglEnableClientState(GL_COLOR_ARRAY);
1088                         CHECKGLERROR
1089                 }
1090                 else if (!color4f)
1091                 {
1092                         qglDisableClientState(GL_COLOR_ARRAY);
1093                         CHECKGLERROR
1094                         // when color array is on the glColor gets trashed, set it again
1095                         qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);
1096                         CHECKGLERROR
1097                 }
1098                 gl_state.pointer_color = color4f;
1099                 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), gl_state.pointer_color);
1100                 CHECKGLERROR
1101         }
1102 }
1103
1104 void R_Mesh_TexCoordPointer(unsigned int unitnum, unsigned int numcomponents, const float *texcoord)
1105 {
1106         gltextureunit_t *unit = gl_state.units + unitnum;
1107         // update array settings
1108         if (texcoord)
1109         {
1110                 // texcoord array
1111                 if (unit->pointer_texcoord != texcoord || unit->arraycomponents != numcomponents)
1112                 {
1113                         unit->pointer_texcoord = texcoord;
1114                         unit->arraycomponents = numcomponents;
1115                         GL_ClientActiveTexture(unitnum);
1116                         qglTexCoordPointer(unit->arraycomponents, GL_FLOAT, sizeof(float) * unit->arraycomponents, unit->pointer_texcoord);
1117                         CHECKGLERROR
1118                 }
1119                 // texture array unit is enabled, enable the array
1120                 if (!unit->arrayenabled)
1121                 {
1122                         unit->arrayenabled = true;
1123                         GL_ClientActiveTexture(unitnum);
1124                         qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1125                 }
1126         }
1127         else
1128         {
1129                 // texture array unit is disabled, disable the array
1130                 if (unit->arrayenabled)
1131                 {
1132                         unit->arrayenabled = false;
1133                         GL_ClientActiveTexture(unitnum);
1134                         qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1135                 }
1136         }
1137 }
1138
1139 void R_Mesh_TexBindAll(unsigned int unitnum, int tex1d, int tex2d, int tex3d, int texcubemap)
1140 {
1141         gltextureunit_t *unit = gl_state.units + unitnum;
1142         if (unitnum >= backendimageunits)
1143                 return;
1144         // update 1d texture binding
1145         if (unit->t1d != tex1d)
1146         {
1147                 GL_ActiveTexture(unitnum);
1148                 if (unitnum < backendunits)
1149                 {
1150                         if (tex1d)
1151                         {
1152                                 if (unit->t1d == 0)
1153                                         qglEnable(GL_TEXTURE_1D);
1154                         }
1155                         else
1156                         {
1157                                 if (unit->t1d)
1158                                         qglDisable(GL_TEXTURE_1D);
1159                         }
1160                 }
1161                 unit->t1d = tex1d;
1162                 qglBindTexture(GL_TEXTURE_1D, unit->t1d);
1163                 CHECKGLERROR
1164         }
1165         // update 2d texture binding
1166         if (unit->t2d != tex2d)
1167         {
1168                 GL_ActiveTexture(unitnum);
1169                 if (unitnum < backendunits)
1170                 {
1171                         if (tex2d)
1172                         {
1173                                 if (unit->t2d == 0)
1174                                         qglEnable(GL_TEXTURE_2D);
1175                         }
1176                         else
1177                         {
1178                                 if (unit->t2d)
1179                                         qglDisable(GL_TEXTURE_2D);
1180                         }
1181                 }
1182                 unit->t2d = tex2d;
1183                 qglBindTexture(GL_TEXTURE_2D, unit->t2d);
1184                 CHECKGLERROR
1185         }
1186         // update 3d texture binding
1187         if (unit->t3d != tex3d)
1188         {
1189                 GL_ActiveTexture(unitnum);
1190                 if (unitnum < backendunits)
1191                 {
1192                         if (tex3d)
1193                         {
1194                                 if (unit->t3d == 0)
1195                                         qglEnable(GL_TEXTURE_3D);
1196                         }
1197                         else
1198                         {
1199                                 if (unit->t3d)
1200                                         qglDisable(GL_TEXTURE_3D);
1201                         }
1202                 }
1203                 unit->t3d = tex3d;
1204                 qglBindTexture(GL_TEXTURE_3D, unit->t3d);
1205                 CHECKGLERROR
1206         }
1207         // update cubemap texture binding
1208         if (unit->tcubemap != texcubemap)
1209         {
1210                 GL_ActiveTexture(unitnum);
1211                 if (unitnum < backendunits)
1212                 {
1213                         if (texcubemap)
1214                         {
1215                                 if (unit->tcubemap == 0)
1216                                         qglEnable(GL_TEXTURE_CUBE_MAP_ARB);
1217                         }
1218                         else
1219                         {
1220                                 if (unit->tcubemap)
1221                                         qglDisable(GL_TEXTURE_CUBE_MAP_ARB);
1222                         }
1223                 }
1224                 unit->tcubemap = texcubemap;
1225                 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);
1226                 CHECKGLERROR
1227         }
1228 }
1229
1230 void R_Mesh_TexBind1D(unsigned int unitnum, int texnum)
1231 {
1232         gltextureunit_t *unit = gl_state.units + unitnum;
1233         if (unitnum >= backendimageunits)
1234                 return;
1235         // update 1d texture binding
1236         if (unit->t1d != texnum)
1237         {
1238                 GL_ActiveTexture(unitnum);
1239                 if (unitnum < backendunits)
1240                 {
1241                         if (texnum)
1242                         {
1243                                 if (unit->t1d == 0)
1244                                         qglEnable(GL_TEXTURE_1D);
1245                         }
1246                         else
1247                         {
1248                                 if (unit->t1d)
1249                                         qglDisable(GL_TEXTURE_1D);
1250                         }
1251                 }
1252                 unit->t1d = texnum;
1253                 qglBindTexture(GL_TEXTURE_1D, unit->t1d);
1254                 CHECKGLERROR
1255         }
1256         // update 2d texture binding
1257         if (unit->t2d)
1258         {
1259                 GL_ActiveTexture(unitnum);
1260                 if (unitnum < backendunits)
1261                 {
1262                         if (unit->t2d)
1263                                 qglDisable(GL_TEXTURE_2D);
1264                 }
1265                 unit->t2d = 0;
1266                 qglBindTexture(GL_TEXTURE_2D, unit->t2d);
1267                 CHECKGLERROR
1268         }
1269         // update 3d texture binding
1270         if (unit->t3d)
1271         {
1272                 GL_ActiveTexture(unitnum);
1273                 if (unitnum < backendunits)
1274                 {
1275                         if (unit->t3d)
1276                                 qglDisable(GL_TEXTURE_3D);
1277                 }
1278                 unit->t3d = 0;
1279                 qglBindTexture(GL_TEXTURE_3D, unit->t3d);
1280                 CHECKGLERROR
1281         }
1282         // update cubemap texture binding
1283         if (unit->tcubemap)
1284         {
1285                 GL_ActiveTexture(unitnum);
1286                 if (unitnum < backendunits)
1287                 {
1288                         if (unit->tcubemap)
1289                                 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);
1290                 }
1291                 unit->tcubemap = 0;
1292                 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);
1293                 CHECKGLERROR
1294         }
1295 }
1296
1297 void R_Mesh_TexBind(unsigned int unitnum, int texnum)
1298 {
1299         gltextureunit_t *unit = gl_state.units + unitnum;
1300         if (unitnum >= backendimageunits)
1301                 return;
1302         // update 1d texture binding
1303         if (unit->t1d)
1304         {
1305                 GL_ActiveTexture(unitnum);
1306                 if (unitnum < backendunits)
1307                 {
1308                         if (unit->t1d)
1309                                 qglDisable(GL_TEXTURE_1D);
1310                 }
1311                 unit->t1d = 0;
1312                 qglBindTexture(GL_TEXTURE_1D, unit->t1d);
1313                 CHECKGLERROR
1314         }
1315         // update 2d texture binding
1316         if (unit->t2d != texnum)
1317         {
1318                 GL_ActiveTexture(unitnum);
1319                 if (unitnum < backendunits)
1320                 {
1321                         if (texnum)
1322                         {
1323                                 if (unit->t2d == 0)
1324                                         qglEnable(GL_TEXTURE_2D);
1325                         }
1326                         else
1327                         {
1328                                 if (unit->t2d)
1329                                         qglDisable(GL_TEXTURE_2D);
1330                         }
1331                 }
1332                 unit->t2d = texnum;
1333                 qglBindTexture(GL_TEXTURE_2D, unit->t2d);
1334                 CHECKGLERROR
1335         }
1336         // update 3d texture binding
1337         if (unit->t3d)
1338         {
1339                 GL_ActiveTexture(unitnum);
1340                 if (unitnum < backendunits)
1341                 {
1342                         if (unit->t3d)
1343                                 qglDisable(GL_TEXTURE_3D);
1344                 }
1345                 unit->t3d = 0;
1346                 qglBindTexture(GL_TEXTURE_3D, unit->t3d);
1347                 CHECKGLERROR
1348         }
1349         // update cubemap texture binding
1350         if (unit->tcubemap != 0)
1351         {
1352                 GL_ActiveTexture(unitnum);
1353                 if (unitnum < backendunits)
1354                 {
1355                         if (unit->tcubemap)
1356                                 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);
1357                 }
1358                 unit->tcubemap = 0;
1359                 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);
1360                 CHECKGLERROR
1361         }
1362 }
1363
1364 void R_Mesh_TexBind3D(unsigned int unitnum, int texnum)
1365 {
1366         gltextureunit_t *unit = gl_state.units + unitnum;
1367         if (unitnum >= backendimageunits)
1368                 return;
1369         // update 1d texture binding
1370         if (unit->t1d)
1371         {
1372                 GL_ActiveTexture(unitnum);
1373                 if (unitnum < backendunits)
1374                 {
1375                         if (unit->t1d)
1376                                 qglDisable(GL_TEXTURE_1D);
1377                 }
1378                 unit->t1d = 0;
1379                 qglBindTexture(GL_TEXTURE_1D, unit->t1d);
1380                 CHECKGLERROR
1381         }
1382         // update 2d texture binding
1383         if (unit->t2d)
1384         {
1385                 GL_ActiveTexture(unitnum);
1386                 if (unitnum < backendunits)
1387                 {
1388                         if (unit->t2d)
1389                                 qglDisable(GL_TEXTURE_2D);
1390                 }
1391                 unit->t2d = 0;
1392                 qglBindTexture(GL_TEXTURE_2D, unit->t2d);
1393                 CHECKGLERROR
1394         }
1395         // update 3d texture binding
1396         if (unit->t3d != texnum)
1397         {
1398                 GL_ActiveTexture(unitnum);
1399                 if (unitnum < backendunits)
1400                 {
1401                         if (texnum)
1402                         {
1403                                 if (unit->t3d == 0)
1404                                         qglEnable(GL_TEXTURE_3D);
1405                         }
1406                         else
1407                         {
1408                                 if (unit->t3d)
1409                                         qglDisable(GL_TEXTURE_3D);
1410                         }
1411                 }
1412                 unit->t3d = texnum;
1413                 qglBindTexture(GL_TEXTURE_3D, unit->t3d);
1414                 CHECKGLERROR
1415         }
1416         // update cubemap texture binding
1417         if (unit->tcubemap != 0)
1418         {
1419                 GL_ActiveTexture(unitnum);
1420                 if (unitnum < backendunits)
1421                 {
1422                         if (unit->tcubemap)
1423                                 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);
1424                 }
1425                 unit->tcubemap = 0;
1426                 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);
1427                 CHECKGLERROR
1428         }
1429 }
1430
1431 void R_Mesh_TexBindCubeMap(unsigned int unitnum, int texnum)
1432 {
1433         gltextureunit_t *unit = gl_state.units + unitnum;
1434         if (unitnum >= backendimageunits)
1435                 return;
1436         // update 1d texture binding
1437         if (unit->t1d)
1438         {
1439                 GL_ActiveTexture(unitnum);
1440                 if (unitnum < backendunits)
1441                 {
1442                         if (unit->t1d)
1443                                 qglDisable(GL_TEXTURE_1D);
1444                 }
1445                 unit->t1d = 0;
1446                 qglBindTexture(GL_TEXTURE_1D, unit->t1d);
1447                 CHECKGLERROR
1448         }
1449         // update 2d texture binding
1450         if (unit->t2d)
1451         {
1452                 GL_ActiveTexture(unitnum);
1453                 if (unitnum < backendunits)
1454                 {
1455                         if (unit->t2d)
1456                                 qglDisable(GL_TEXTURE_2D);
1457                 }
1458                 unit->t2d = 0;
1459                 qglBindTexture(GL_TEXTURE_2D, unit->t2d);
1460                 CHECKGLERROR
1461         }
1462         // update 3d texture binding
1463         if (unit->t3d)
1464         {
1465                 GL_ActiveTexture(unitnum);
1466                 if (unitnum < backendunits)
1467                 {
1468                         if (unit->t3d)
1469                                 qglDisable(GL_TEXTURE_3D);
1470                 }
1471                 unit->t3d = 0;
1472                 qglBindTexture(GL_TEXTURE_3D, unit->t3d);
1473                 CHECKGLERROR
1474         }
1475         // update cubemap texture binding
1476         if (unit->tcubemap != texnum)
1477         {
1478                 GL_ActiveTexture(unitnum);
1479                 if (unitnum < backendunits)
1480                 {
1481                         if (texnum)
1482                         {
1483                                 if (unit->tcubemap == 0)
1484                                         qglEnable(GL_TEXTURE_CUBE_MAP_ARB);
1485                         }
1486                         else
1487                         {
1488                                 if (unit->tcubemap)
1489                                         qglDisable(GL_TEXTURE_CUBE_MAP_ARB);
1490                         }
1491                 }
1492                 unit->tcubemap = texnum;
1493                 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);
1494                 CHECKGLERROR
1495         }
1496 }
1497
1498 void R_Mesh_TexMatrix(unsigned int unitnum, const matrix4x4_t *matrix)
1499 {
1500         gltextureunit_t *unit = gl_state.units + unitnum;
1501         if (matrix->m[3][3])
1502         {
1503                 // texmatrix specified, check if it is different
1504                 if (!unit->texmatrixenabled || memcmp(&unit->matrix, matrix, sizeof(matrix4x4_t)))
1505                 {
1506                         matrix4x4_t tempmatrix;
1507                         unit->texmatrixenabled = true;
1508                         unit->matrix = *matrix;
1509                         Matrix4x4_Transpose(&tempmatrix, &unit->matrix);
1510                         qglMatrixMode(GL_TEXTURE);
1511                         GL_ActiveTexture(unitnum);
1512                         qglLoadMatrixf(&tempmatrix.m[0][0]);
1513                         qglMatrixMode(GL_MODELVIEW);
1514                 }
1515         }
1516         else
1517         {
1518                 // no texmatrix specified, revert to identity
1519                 if (unit->texmatrixenabled)
1520                 {
1521                         unit->texmatrixenabled = false;
1522                         qglMatrixMode(GL_TEXTURE);
1523                         GL_ActiveTexture(unitnum);
1524                         qglLoadIdentity();
1525                         qglMatrixMode(GL_MODELVIEW);
1526                 }
1527         }
1528 }
1529
1530 void R_Mesh_TexCombine(unsigned int unitnum, int combinergb, int combinealpha, int rgbscale, int alphascale)
1531 {
1532         gltextureunit_t *unit = gl_state.units + unitnum;
1533         if (gl_combine.integer)
1534         {
1535                 // GL_ARB_texture_env_combine
1536                 if (!combinergb)
1537                         combinergb = GL_MODULATE;
1538                 if (!combinealpha)
1539                         combinealpha = GL_MODULATE;
1540                 if (!rgbscale)
1541                         rgbscale = 1;
1542                 if (!alphascale)
1543                         alphascale = 1;
1544                 if (unit->combinergb != combinergb)
1545                 {
1546                         unit->combinergb = combinergb;
1547                         GL_ActiveTexture(unitnum);
1548                         qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
1549                 }
1550                 if (unit->combinealpha != combinealpha)
1551                 {
1552                         unit->combinealpha = combinealpha;
1553                         GL_ActiveTexture(unitnum);
1554                         qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
1555                 }
1556                 if (unit->rgbscale != rgbscale)
1557                 {
1558                         GL_ActiveTexture(unitnum);
1559                         qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (unit->rgbscale = rgbscale));CHECKGLERROR
1560                 }
1561                 if (unit->alphascale != alphascale)
1562                 {
1563                         GL_ActiveTexture(unitnum);
1564                         qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, (unit->alphascale = alphascale));CHECKGLERROR
1565                 }
1566         }
1567         else
1568         {
1569                 // normal GL texenv
1570                 if (!combinergb)
1571                         combinergb = GL_MODULATE;
1572                 if (unit->combinergb != combinergb)
1573                 {
1574                         unit->combinergb = combinergb;
1575                         GL_ActiveTexture(unitnum);
1576                         qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combinergb);CHECKGLERROR
1577                 }
1578         }
1579 }
1580
1581 void R_Mesh_TextureState(const rmeshstate_t *m)
1582 {
1583         unsigned int i;
1584
1585         BACKENDACTIVECHECK
1586
1587         if (gl_backend_rebindtextures)
1588         {
1589                 gl_backend_rebindtextures = false;
1590                 GL_SetupTextureState();
1591         }
1592
1593         for (i = 0;i < backendimageunits;i++)
1594                 R_Mesh_TexBindAll(i, m->tex1d[i], m->tex[i], m->tex3d[i], m->texcubemap[i]);
1595         for (i = 0;i < backendarrayunits;i++)
1596         {
1597                 if (m->pointer_texcoord3f[i])
1598                         R_Mesh_TexCoordPointer(i, 3, m->pointer_texcoord3f[i]);
1599                 else
1600                         R_Mesh_TexCoordPointer(i, 2, m->pointer_texcoord[i]);
1601         }
1602         for (i = 0;i < backendunits;i++)
1603         {
1604                 R_Mesh_TexMatrix(i, &m->texmatrix[i]);
1605                 R_Mesh_TexCombine(i, m->texcombinergb[i], m->texcombinealpha[i], m->texrgbscale[i], m->texalphascale[i]);
1606         }
1607 }
1608
1609 void R_Mesh_ResetTextureState(void)
1610 {
1611         unsigned int unitnum;
1612
1613         BACKENDACTIVECHECK
1614
1615         if (gl_backend_rebindtextures)
1616         {
1617                 gl_backend_rebindtextures = false;
1618                 GL_SetupTextureState();
1619         }
1620
1621         for (unitnum = 0;unitnum < backendimageunits;unitnum++)
1622         {
1623                 gltextureunit_t *unit = gl_state.units + unitnum;
1624                 // update 1d texture binding
1625                 if (unit->t1d)
1626                 {
1627                         GL_ActiveTexture(unitnum);
1628                         if (unitnum < backendunits)
1629                                 qglDisable(GL_TEXTURE_1D);
1630                         unit->t1d = 0;
1631                         qglBindTexture(GL_TEXTURE_1D, unit->t1d);
1632                         CHECKGLERROR
1633                 }
1634                 // update 2d texture binding
1635                 if (unit->t2d)
1636                 {
1637                         GL_ActiveTexture(unitnum);
1638                         if (unitnum < backendunits)
1639                                 qglDisable(GL_TEXTURE_2D);
1640                         unit->t2d = 0;
1641                         qglBindTexture(GL_TEXTURE_2D, unit->t2d);
1642                         CHECKGLERROR
1643                 }
1644                 // update 3d texture binding
1645                 if (unit->t3d)
1646                 {
1647                         GL_ActiveTexture(unitnum);
1648                         if (unitnum < backendunits)
1649                                 qglDisable(GL_TEXTURE_3D);
1650                         unit->t3d = 0;
1651                         qglBindTexture(GL_TEXTURE_3D, unit->t3d);
1652                         CHECKGLERROR
1653                 }
1654                 // update cubemap texture binding
1655                 if (unit->tcubemap)
1656                 {
1657                         GL_ActiveTexture(unitnum);
1658                         if (unitnum < backendunits)
1659                                 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);
1660                         unit->tcubemap = 0;
1661                         qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);
1662                         CHECKGLERROR
1663                 }
1664         }
1665         for (unitnum = 0;unitnum < backendarrayunits;unitnum++)
1666         {
1667                 gltextureunit_t *unit = gl_state.units + unitnum;
1668                 // texture array unit is disabled, disable the array
1669                 if (unit->arrayenabled)
1670                 {
1671                         unit->arrayenabled = false;
1672                         GL_ClientActiveTexture(unitnum);
1673                         qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1674                 }
1675         }
1676         for (unitnum = 0;unitnum < backendunits;unitnum++)
1677         {
1678                 gltextureunit_t *unit = gl_state.units + unitnum;
1679                 // no texmatrix specified, revert to identity
1680                 if (unit->texmatrixenabled)
1681                 {
1682                         unit->texmatrixenabled = false;
1683                         qglMatrixMode(GL_TEXTURE);
1684                         GL_ActiveTexture(unitnum);
1685                         qglLoadIdentity();
1686                         qglMatrixMode(GL_MODELVIEW);
1687                 }
1688                 if (gl_combine.integer)
1689                 {
1690                         // GL_ARB_texture_env_combine
1691                         if (unit->combinergb != GL_MODULATE)
1692                         {
1693                                 unit->combinergb = GL_MODULATE;
1694                                 GL_ActiveTexture(unitnum);
1695                                 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
1696                         }
1697                         if (unit->combinealpha != GL_MODULATE)
1698                         {
1699                                 unit->combinealpha = GL_MODULATE;
1700                                 GL_ActiveTexture(unitnum);
1701                                 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
1702                         }
1703                         if (unit->rgbscale != 1)
1704                         {
1705                                 GL_ActiveTexture(unitnum);
1706                                 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (unit->rgbscale = 1));CHECKGLERROR
1707                         }
1708                         if (unit->alphascale != 1)
1709                         {
1710                                 GL_ActiveTexture(unitnum);
1711                                 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, (unit->alphascale = 1));CHECKGLERROR
1712                         }
1713                 }
1714                 else
1715                 {
1716                         // normal GL texenv
1717                         if (unit->combinergb != GL_MODULATE)
1718                         {
1719                                 unit->combinergb = GL_MODULATE;
1720                                 GL_ActiveTexture(unitnum);
1721                                 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combinergb);CHECKGLERROR
1722                         }
1723                 }
1724         }
1725 }
1726
1727 void R_Mesh_Draw_ShowTris(int firstvertex, int numvertices, int numtriangles, const int *elements)
1728 {
1729         qglBegin(GL_LINES);
1730         for (;numtriangles;numtriangles--, elements += 3)
1731         {
1732                 qglArrayElement(elements[0]);qglArrayElement(elements[1]);
1733                 qglArrayElement(elements[1]);qglArrayElement(elements[2]);
1734                 qglArrayElement(elements[2]);qglArrayElement(elements[0]);
1735         }
1736         qglEnd();
1737         CHECKGLERROR
1738 }