]> git.xonotic.org Git - xonotic/darkplaces.git/blob - gl_backend.c
renamed polygonelements, skyboxelements, skysphereelements, to add 3s
[xonotic/darkplaces.git] / gl_backend.c
1
2 #include "quakedef.h"
3 #include "cl_collision.h"
4
5 cvar_t gl_mesh_drawrangeelements = {0, "gl_mesh_drawrangeelements", "1", "use glDrawRangeElements function if available instead of glDrawElements (for performance comparisons or bug testing)"};
6 cvar_t gl_mesh_testarrayelement = {0, "gl_mesh_testarrayelement", "0", "use glBegin(GL_TRIANGLES);glArrayElement();glEnd(); primitives instead of glDrawElements (useful to test for driver bugs with glDrawElements)"};
7 cvar_t gl_mesh_testmanualfeeding = {0, "gl_mesh_testmanualfeeding", "0", "use glBegin(GL_TRIANGLES);glTexCoord2f();glVertex3f();glEnd(); primitives instead of glDrawElements (useful to test for driver bugs with glDrawElements)"};
8 cvar_t gl_mesh_prefer_short_elements = {0, "gl_mesh_prefer_short_elements", "1", "use GL_UNSIGNED_SHORT element arrays instead of GL_UNSIGNED_INT"};
9 cvar_t gl_paranoid = {0, "gl_paranoid", "0", "enables OpenGL error checking and other tests"};
10 cvar_t gl_printcheckerror = {0, "gl_printcheckerror", "0", "prints all OpenGL error checks, useful to identify location of driver crashes"};
11
12 cvar_t r_render = {0, "r_render", "1", "enables rendering 3D views (you want this on!)"};
13 cvar_t r_renderview = {0, "r_renderview", "1", "enables rendering 3D views (you want this on!)"};
14 cvar_t r_waterwarp = {CVAR_SAVE, "r_waterwarp", "1", "warp view while underwater"};
15 cvar_t gl_polyblend = {CVAR_SAVE, "gl_polyblend", "1", "tints view while underwater, hurt, etc"};
16 cvar_t gl_dither = {CVAR_SAVE, "gl_dither", "1", "enables OpenGL dithering (16bit looks bad with this off)"};
17 cvar_t gl_lockarrays = {0, "gl_lockarrays", "0", "enables use of glLockArraysEXT, may cause glitches with some broken drivers, and may be slower than normal"};
18 cvar_t gl_lockarrays_minimumvertices = {0, "gl_lockarrays_minimumvertices", "1", "minimum number of vertices required for use of glLockArraysEXT, setting this too low may reduce performance"};
19 cvar_t gl_vbo = {CVAR_SAVE, "gl_vbo", "3", "make use of GL_ARB_vertex_buffer_object extension to store static geometry in video memory for faster rendering, 0 disables VBO allocation or use, 1 enables VBOs for vertex and triangle data, 2 only for vertex data, 3 for vertex data and triangle data of simple meshes (ones with only one surface)"};
20 cvar_t gl_fbo = {CVAR_SAVE, "gl_fbo", "1", "make use of GL_ARB_framebuffer_object extension to enable shadowmaps and other features using pixel formats different from the framebuffer"};
21
22 cvar_t v_flipped = {0, "v_flipped", "0", "mirror the screen (poor man's left handed mode)"};
23 qboolean v_flipped_state = false;
24
25 int gl_maxdrawrangeelementsvertices;
26 int gl_maxdrawrangeelementsindices;
27
28 #ifdef DEBUGGL
29 int errornumber = 0;
30
31 void GL_PrintError(int errornumber, char *filename, int linenumber)
32 {
33         switch(errornumber)
34         {
35 #ifdef GL_INVALID_ENUM
36         case GL_INVALID_ENUM:
37                 Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
38                 break;
39 #endif
40 #ifdef GL_INVALID_VALUE
41         case GL_INVALID_VALUE:
42                 Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
43                 break;
44 #endif
45 #ifdef GL_INVALID_OPERATION
46         case GL_INVALID_OPERATION:
47                 Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
48                 break;
49 #endif
50 #ifdef GL_STACK_OVERFLOW
51         case GL_STACK_OVERFLOW:
52                 Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
53                 break;
54 #endif
55 #ifdef GL_STACK_UNDERFLOW
56         case GL_STACK_UNDERFLOW:
57                 Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
58                 break;
59 #endif
60 #ifdef GL_OUT_OF_MEMORY
61         case GL_OUT_OF_MEMORY:
62                 Con_Printf("GL_OUT_OF_MEMORY at %s:%i\n", filename, linenumber);
63                 break;
64 #endif
65 #ifdef GL_TABLE_TOO_LARGE
66         case GL_TABLE_TOO_LARGE:
67                 Con_Printf("GL_TABLE_TOO_LARGE at %s:%i\n", filename, linenumber);
68                 break;
69 #endif
70 #ifdef GL_INVALID_FRAMEBUFFER_OPERATION_EXT
71         case GL_INVALID_FRAMEBUFFER_OPERATION_EXT:
72                 Con_Printf("GL_INVALID_FRAMEBUFFER_OPERATION at %s:%i\n", filename, linenumber);
73                 break;
74 #endif
75         default:
76                 Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
77                 break;
78         }
79 }
80 #endif
81
82 #define BACKENDACTIVECHECK if (!backendactive) Sys_Error("GL backend function called when backend is not active");
83
84 void SCR_ScreenShot_f (void);
85
86 static r_viewport_t backend_viewport;
87 static matrix4x4_t backend_modelmatrix;
88 static matrix4x4_t backend_modelviewmatrix;
89
90 static unsigned int backendunits, backendimageunits, backendarrayunits, backendactive;
91
92 /*
93 note: here's strip order for a terrain row:
94 0--1--2--3--4
95 |\ |\ |\ |\ |
96 | \| \| \| \|
97 A--B--C--D--E
98 clockwise
99
100 A0B, 01B, B1C, 12C, C2D, 23D, D3E, 34E
101
102 *elements++ = i + row;
103 *elements++ = i;
104 *elements++ = i + row + 1;
105 *elements++ = i;
106 *elements++ = i + 1;
107 *elements++ = i + row + 1;
108
109
110 for (y = 0;y < rows - 1;y++)
111 {
112         for (x = 0;x < columns - 1;x++)
113         {
114                 i = y * rows + x;
115                 *elements++ = i + columns;
116                 *elements++ = i;
117                 *elements++ = i + columns + 1;
118                 *elements++ = i;
119                 *elements++ = i + 1;
120                 *elements++ = i + columns + 1;
121         }
122 }
123
124 alternative:
125 0--1--2--3--4
126 | /| /|\ | /|
127 |/ |/ | \|/ |
128 A--B--C--D--E
129 counterclockwise
130
131 for (y = 0;y < rows - 1;y++)
132 {
133         for (x = 0;x < columns - 1;x++)
134         {
135                 i = y * rows + x;
136                 *elements++ = i;
137                 *elements++ = i + columns;
138                 *elements++ = i + columns + 1;
139                 *elements++ = i + columns;
140                 *elements++ = i + columns + 1;
141                 *elements++ = i + 1;
142         }
143 }
144 */
145
146 int polygonelement3i[(POLYGONELEMENTS_MAXPOINTS-2)*3];
147 unsigned short polygonelement3s[(POLYGONELEMENTS_MAXPOINTS-2)*3];
148 int quadelement3i[QUADELEMENTS_MAXQUADS*6];
149 unsigned short quadelement3s[QUADELEMENTS_MAXQUADS*6];
150
151 void GL_Backend_AllocArrays(void)
152 {
153 }
154
155 void GL_Backend_FreeArrays(void)
156 {
157 }
158
159 void GL_VBOStats_f(void)
160 {
161         GL_Mesh_ListVBOs(true);
162 }
163
164 typedef struct gl_bufferobjectinfo_s
165 {
166         int target;
167         int object;
168         size_t size;
169         char name[MAX_QPATH];
170 }
171 gl_bufferobjectinfo_t;
172
173 memexpandablearray_t gl_bufferobjectinfoarray;
174
175 static void gl_backend_start(void)
176 {
177         CHECKGLERROR
178
179         if (qglDrawRangeElements != NULL)
180         {
181                 CHECKGLERROR
182                 qglGetIntegerv(GL_MAX_ELEMENTS_VERTICES, &gl_maxdrawrangeelementsvertices);
183                 CHECKGLERROR
184                 qglGetIntegerv(GL_MAX_ELEMENTS_INDICES, &gl_maxdrawrangeelementsindices);
185                 CHECKGLERROR
186                 Con_DPrintf("GL_MAX_ELEMENTS_VERTICES = %i\nGL_MAX_ELEMENTS_INDICES = %i\n", gl_maxdrawrangeelementsvertices, gl_maxdrawrangeelementsindices);
187         }
188
189         backendunits = bound(1, gl_textureunits, MAX_TEXTUREUNITS);
190         backendimageunits = backendunits;
191         backendarrayunits = backendunits;
192         if (gl_support_fragment_shader)
193         {
194                 CHECKGLERROR
195                 qglGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS_ARB, (int *)&backendimageunits);
196                 CHECKGLERROR
197                 qglGetIntegerv(GL_MAX_TEXTURE_COORDS_ARB, (int *)&backendarrayunits);
198                 CHECKGLERROR
199                 Con_DPrintf("GLSL shader support detected: texture units = %i texenv, %i image, %i array\n", backendunits, backendimageunits, backendarrayunits);
200                 backendimageunits = bound(1, backendimageunits, MAX_TEXTUREUNITS);
201                 backendarrayunits = bound(1, backendarrayunits, MAX_TEXTUREUNITS);
202         }
203         else
204                 Con_DPrintf("GL_MAX_TEXTUREUNITS = %i\n", backendunits);
205
206         GL_Backend_AllocArrays();
207
208         Mem_ExpandableArray_NewArray(&gl_bufferobjectinfoarray, r_main_mempool, sizeof(gl_bufferobjectinfo_t), 128);
209
210         Con_DPrintf("OpenGL backend started.\n");
211
212         CHECKGLERROR
213
214         backendactive = true;
215 }
216
217 static void gl_backend_shutdown(void)
218 {
219         backendunits = 0;
220         backendimageunits = 0;
221         backendarrayunits = 0;
222         backendactive = false;
223
224         Con_DPrint("OpenGL Backend shutting down\n");
225
226         Mem_ExpandableArray_FreeArray(&gl_bufferobjectinfoarray);
227
228         GL_Backend_FreeArrays();
229 }
230
231 static void gl_backend_newmap(void)
232 {
233 }
234
235 void gl_backend_init(void)
236 {
237         int i;
238
239         for (i = 0;i < POLYGONELEMENTS_MAXPOINTS - 2;i++)
240         {
241                 polygonelement3s[i * 3 + 0] = 0;
242                 polygonelement3s[i * 3 + 1] = i + 1;
243                 polygonelement3s[i * 3 + 2] = i + 2;
244         }
245         // elements for rendering a series of quads as triangles
246         for (i = 0;i < QUADELEMENTS_MAXQUADS;i++)
247         {
248                 quadelement3s[i * 6 + 0] = i * 4;
249                 quadelement3s[i * 6 + 1] = i * 4 + 1;
250                 quadelement3s[i * 6 + 2] = i * 4 + 2;
251                 quadelement3s[i * 6 + 3] = i * 4;
252                 quadelement3s[i * 6 + 4] = i * 4 + 2;
253                 quadelement3s[i * 6 + 5] = i * 4 + 3;
254         }
255
256         for (i = 0;i < (POLYGONELEMENTS_MAXPOINTS - 2)*3;i++)
257                 polygonelement3i[i] = polygonelement3s[i];
258         for (i = 0;i < QUADELEMENTS_MAXQUADS*3;i++)
259                 quadelement3i[i] = quadelement3s[i];
260
261         Cvar_RegisterVariable(&r_render);
262         Cvar_RegisterVariable(&r_renderview);
263         Cvar_RegisterVariable(&r_waterwarp);
264         Cvar_RegisterVariable(&gl_polyblend);
265         Cvar_RegisterVariable(&v_flipped);
266         Cvar_RegisterVariable(&gl_dither);
267         Cvar_RegisterVariable(&gl_lockarrays);
268         Cvar_RegisterVariable(&gl_lockarrays_minimumvertices);
269         Cvar_RegisterVariable(&gl_vbo);
270         Cvar_RegisterVariable(&gl_paranoid);
271         Cvar_RegisterVariable(&gl_printcheckerror);
272
273         Cvar_RegisterVariable(&gl_mesh_drawrangeelements);
274         Cvar_RegisterVariable(&gl_mesh_testarrayelement);
275         Cvar_RegisterVariable(&gl_mesh_testmanualfeeding);
276         Cvar_RegisterVariable(&gl_mesh_prefer_short_elements);
277
278         Cmd_AddCommand("gl_vbostats", GL_VBOStats_f, "prints a list of all buffer objects (vertex data and triangle elements) and total video memory used by them");
279
280         R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap);
281 }
282
283 void GL_SetMirrorState(qboolean state);
284
285 void R_Viewport_TransformToScreen(const r_viewport_t *v, const vec4_t in, vec4_t out)
286 {
287         vec4_t temp;
288         float iw;
289         Matrix4x4_Transform4 (&v->viewmatrix, in, temp);
290         Matrix4x4_Transform4 (&v->projectmatrix, temp, out);
291         iw = 1.0f / out[3];
292         out[0] = v->x + (out[0] * iw + 1.0f) * v->width * 0.5f;
293         out[1] = v->y + v->height - (out[1] * iw + 1.0f) * v->height * 0.5f;
294         out[2] = v->z + (out[2] * iw + 1.0f) * v->depth * 0.5f;
295 }
296
297 static void R_Viewport_ApplyNearClipPlane(r_viewport_t *v, double normalx, double normaly, double normalz, double dist)
298 {
299         double q[4];
300         double d;
301         float clipPlane[4], v3[3], v4[3];
302         float normal[3];
303
304         // This is inspired by Oblique Depth Projection from http://www.terathon.com/code/oblique.php
305
306         VectorSet(normal, normalx, normaly, normalz);
307         Matrix4x4_Transform3x3(&v->viewmatrix, normal, clipPlane);
308         VectorScale(normal, dist, v3);
309         Matrix4x4_Transform(&v->viewmatrix, v3, v4);
310         // FIXME: LordHavoc: I think this can be done more efficiently somehow but I can't remember the technique
311         clipPlane[3] = -DotProduct(v4, clipPlane);
312
313 #if 0
314 {
315         // testing code for comparing results
316         float clipPlane2[4];
317         VectorCopy4(clipPlane, clipPlane2);
318         R_Mesh_Matrix(&identitymatrix);
319         VectorSet(q, normal[0], normal[1], normal[2], -dist);
320         qglClipPlane(GL_CLIP_PLANE0, q);
321         qglGetClipPlane(GL_CLIP_PLANE0, q);
322         VectorCopy4(q, clipPlane);
323 }
324 #endif
325
326         // Calculate the clip-space corner point opposite the clipping plane
327         // as (sgn(clipPlane.x), sgn(clipPlane.y), 1, 1) and
328         // transform it into camera space by multiplying it
329         // by the inverse of the projection matrix
330         q[0] = ((clipPlane[0] < 0.0f ? -1.0f : clipPlane[0] > 0.0f ? 1.0f : 0.0f) + v->m[8]) / v->m[0];
331         q[1] = ((clipPlane[1] < 0.0f ? -1.0f : clipPlane[1] > 0.0f ? 1.0f : 0.0f) + v->m[9]) / v->m[5];
332         q[2] = -1.0f;
333         q[3] = (1.0f + v->m[10]) / v->m[14];
334
335         // Calculate the scaled plane vector
336         d = 2.0f / DotProduct4(clipPlane, q);
337
338         // Replace the third row of the projection matrix
339         v->m[2] = clipPlane[0] * d;
340         v->m[6] = clipPlane[1] * d;
341         v->m[10] = clipPlane[2] * d + 1.0f;
342         v->m[14] = clipPlane[3] * d;
343 }
344
345 void R_Viewport_InitOrtho(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, double x1, double y1, double x2, double y2, double nearclip, double farclip, const double *nearplane)
346 {
347         float left = x1, right = x2, bottom = y2, top = y1, zNear = nearclip, zFar = farclip;
348         memset(v, 0, sizeof(*v));
349         v->type = R_VIEWPORTTYPE_ORTHO;
350         v->cameramatrix = *cameramatrix;
351         v->x = x;
352         v->y = y;
353         v->z = 0;
354         v->width = width;
355         v->height = height;
356         v->depth = 1;
357         v->m[0]  = 2/(right - left);
358         v->m[5]  = 2/(top - bottom);
359         v->m[10] = -2/(zFar - zNear);
360         v->m[12] = - (right + left)/(right - left);
361         v->m[13] = - (top + bottom)/(top - bottom);
362         v->m[14] = - (zFar + zNear)/(zFar - zNear);
363         v->m[15] = 1;
364
365         Matrix4x4_Invert_Full(&v->viewmatrix, &v->cameramatrix);
366         Matrix4x4_FromArrayDoubleGL(&v->projectmatrix, v->m);
367
368         if (nearplane)
369                 R_Viewport_ApplyNearClipPlane(v, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
370
371 #if 0
372         {
373                 vec4_t test1;
374                 vec4_t test2;
375                 Vector4Set(test1, (x1+x2)*0.5f, (y1+y2)*0.5f, 0.0f, 1.0f);
376                 R_Viewport_TransformToScreen(v, test1, test2);
377                 Con_Printf("%f %f %f -> %f %f %f\n", test1[0], test1[1], test1[2], test2[0], test2[1], test2[2]);
378         }
379 #endif
380 }
381
382 void R_Viewport_InitPerspective(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, double frustumx, double frustumy, double nearclip, double farclip, const double *nearplane)
383 {
384         matrix4x4_t tempmatrix, basematrix;
385         memset(v, 0, sizeof(*v));
386
387         if(v_flipped.integer)
388                 frustumx = -frustumx;
389
390         v->type = R_VIEWPORTTYPE_PERSPECTIVE;
391         v->cameramatrix = *cameramatrix;
392         v->x = x;
393         v->y = y;
394         v->z = 0;
395         v->width = width;
396         v->height = height;
397         v->depth = 1;
398         v->m[0]  = 1.0 / frustumx;
399         v->m[5]  = 1.0 / frustumy;
400         v->m[10] = -(farclip + nearclip) / (farclip - nearclip);
401         v->m[11] = -1;
402         v->m[14] = -2 * nearclip * farclip / (farclip - nearclip);
403
404         Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
405         Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
406         Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
407         Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
408
409         Matrix4x4_FromArrayDoubleGL(&v->projectmatrix, v->m);
410
411         if (nearplane)
412                 R_Viewport_ApplyNearClipPlane(v, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
413 }
414
415 void R_Viewport_InitPerspectiveInfinite(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, double frustumx, double frustumy, double nearclip, const double *nearplane)
416 {
417         matrix4x4_t tempmatrix, basematrix;
418         const double nudge = 1.0 - 1.0 / (1<<23);
419         memset(v, 0, sizeof(*v));
420
421         if(v_flipped.integer)
422                 frustumx = -frustumx;
423
424         v->type = R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP;
425         v->cameramatrix = *cameramatrix;
426         v->x = x;
427         v->y = y;
428         v->z = 0;
429         v->width = width;
430         v->height = height;
431         v->depth = 1;
432         v->m[ 0] = 1.0 / frustumx;
433         v->m[ 5] = 1.0 / frustumy;
434         v->m[10] = -nudge;
435         v->m[11] = -1;
436         v->m[14] = -2 * nearclip * nudge;
437
438         Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
439         Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
440         Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
441         Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
442
443         Matrix4x4_FromArrayDoubleGL(&v->projectmatrix, v->m);
444
445         if (nearplane)
446                 R_Viewport_ApplyNearClipPlane(v, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
447 }
448
449 float cubeviewmatrix[6][16] =
450 {
451     // standard cubemap projections
452     { // +X
453          0, 0,-1, 0,
454          0,-1, 0, 0,
455         -1, 0, 0, 0,
456          0, 0, 0, 1,
457     },
458     { // -X
459          0, 0, 1, 0,
460          0,-1, 0, 0,
461          1, 0, 0, 0,
462          0, 0, 0, 1,
463     },
464     { // +Y
465          1, 0, 0, 0,
466          0, 0,-1, 0,
467          0, 1, 0, 0,
468          0, 0, 0, 1,
469     },
470     { // -Y
471          1, 0, 0, 0,
472          0, 0, 1, 0,
473          0,-1, 0, 0,
474          0, 0, 0, 1,
475     },
476     { // +Z
477          1, 0, 0, 0,
478          0,-1, 0, 0,
479          0, 0,-1, 0,
480          0, 0, 0, 1,
481     },
482     { // -Z
483         -1, 0, 0, 0,
484          0,-1, 0, 0,
485          0, 0, 1, 0,
486          0, 0, 0, 1,
487     },
488 };
489 float rectviewmatrix[6][16] =
490 {
491     // sign-preserving cubemap projections
492     { // +X
493          0, 0,-1, 0,
494          0, 1, 0, 0,
495          1, 0, 0, 0,
496          0, 0, 0, 1,
497     },
498     { // -X
499          0, 0, 1, 0,
500          0, 1, 0, 0,
501          1, 0, 0, 0,
502          0, 0, 0, 1,
503     },
504     { // +Y
505          1, 0, 0, 0,
506          0, 0,-1, 0,
507          0, 1, 0, 0,
508          0, 0, 0, 1,
509     },
510     { // -Y
511          1, 0, 0, 0,
512          0, 0, 1, 0,
513          0, 1, 0, 0,
514          0, 0, 0, 1,
515     },
516     { // +Z
517          1, 0, 0, 0,
518          0, 1, 0, 0,
519          0, 0,-1, 0,
520          0, 0, 0, 1,
521     },
522     { // -Z
523          1, 0, 0, 0,
524          0, 1, 0, 0,
525          0, 0, 1, 0,
526          0, 0, 0, 1,
527     },
528 };
529
530 void R_Viewport_InitCubeSideView(r_viewport_t *v, const matrix4x4_t *cameramatrix, int side, int size, float nearclip, float farclip, const float *nearplane)
531 {
532         matrix4x4_t tempmatrix, basematrix;
533         memset(v, 0, sizeof(*v));
534         v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
535         v->cameramatrix = *cameramatrix;
536         v->width = size;
537         v->height = size;
538         v->depth = 1;
539         v->m[0] = v->m[5] = 1.0f;
540         v->m[10] = -(farclip + nearclip) / (farclip - nearclip);
541         v->m[11] = -1;
542         v->m[14] = -2 * nearclip * farclip / (farclip - nearclip);
543
544         Matrix4x4_FromArrayFloatGL(&basematrix, cubeviewmatrix[side]);
545         Matrix4x4_Invert_Simple(&tempmatrix, &v->cameramatrix);
546         Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
547         Matrix4x4_FromArrayDoubleGL(&v->projectmatrix, v->m);
548
549         if (nearplane)
550                 R_Viewport_ApplyNearClipPlane(v, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
551 }
552
553 void R_Viewport_InitRectSideView(r_viewport_t *v, const matrix4x4_t *cameramatrix, int side, int size, int border, float nearclip, float farclip, const float *nearplane)
554 {
555         matrix4x4_t tempmatrix, basematrix;
556         memset(v, 0, sizeof(*v));
557         v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
558         v->cameramatrix = *cameramatrix;
559         v->x = (side & 1) * size;
560         v->y = (side >> 1) * size;
561         v->width = size;
562         v->height = size;
563         v->depth = 1;
564         v->m[0] = v->m[5] = 1.0f * ((float)size - border) / size;
565         v->m[10] = -(farclip + nearclip) / (farclip - nearclip);
566         v->m[11] = -1;
567         v->m[14] = -2 * nearclip * farclip / (farclip - nearclip);
568
569         Matrix4x4_FromArrayFloatGL(&basematrix, rectviewmatrix[side]);
570         Matrix4x4_Invert_Simple(&tempmatrix, &v->cameramatrix);
571         Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
572         Matrix4x4_FromArrayDoubleGL(&v->projectmatrix, v->m);
573
574         if (nearplane)
575                 R_Viewport_ApplyNearClipPlane(v, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
576 }
577
578 void R_SetViewport(const r_viewport_t *v)
579 {
580         float glmatrix[16];
581         backend_viewport = *v;
582
583         CHECKGLERROR
584         qglViewport(v->x, v->y, v->width, v->height);CHECKGLERROR
585
586         // Load the projection matrix into OpenGL
587         qglMatrixMode(GL_PROJECTION);CHECKGLERROR
588         qglLoadMatrixd(backend_viewport.m);CHECKGLERROR
589         qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
590
591         // FIXME: v_flipped_state is evil, this probably breaks somewhere
592         GL_SetMirrorState(v_flipped.integer && (v->type == R_VIEWPORTTYPE_PERSPECTIVE || v->type == R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP));
593
594         // directly force an update of the modelview matrix
595         Matrix4x4_Concat(&backend_modelviewmatrix, &backend_viewport.viewmatrix, &backend_modelmatrix);
596         Matrix4x4_ToArrayFloatGL(&backend_modelviewmatrix, glmatrix);
597         qglLoadMatrixf(glmatrix);CHECKGLERROR
598 }
599
600 void R_GetViewport(r_viewport_t *v)
601 {
602         *v = backend_viewport;
603 }
604
605 typedef struct gltextureunit_s
606 {
607         const void *pointer_texcoord;
608         size_t pointer_texcoord_offset;
609         int pointer_texcoord_buffer;
610         int t1d, t2d, t3d, tcubemap, trectangle;
611         int arrayenabled;
612         unsigned int arraycomponents;
613         int rgbscale, alphascale;
614         int combinergb, combinealpha;
615         // FIXME: add more combine stuff
616         // texmatrixenabled exists only to avoid unnecessary texmatrix compares
617         int texmatrixenabled;
618         matrix4x4_t matrix;
619 }
620 gltextureunit_t;
621
622 static struct gl_state_s
623 {
624         int cullface;
625         int cullfaceenable;
626         int blendfunc1;
627         int blendfunc2;
628         int blend;
629         GLboolean depthmask;
630         int colormask; // stored as bottom 4 bits: r g b a (3 2 1 0 order)
631         int depthtest;
632         float depthrange[2];
633         float polygonoffset[2];
634         int alphatest;
635         int scissortest;
636         unsigned int unit;
637         unsigned int clientunit;
638         gltextureunit_t units[MAX_TEXTUREUNITS];
639         float color4f[4];
640         int lockrange_first;
641         int lockrange_count;
642         int vertexbufferobject;
643         int elementbufferobject;
644         qboolean pointer_color_enabled;
645         const void *pointer_vertex;
646         const void *pointer_color;
647         size_t pointer_vertex_offset;
648         size_t pointer_color_offset;
649         int pointer_vertex_buffer;
650         int pointer_color_buffer;
651 }
652 gl_state;
653
654 static void GL_BindVBO(int bufferobject)
655 {
656         if (gl_state.vertexbufferobject != bufferobject)
657         {
658                 gl_state.vertexbufferobject = bufferobject;
659                 CHECKGLERROR
660                 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, bufferobject);
661                 CHECKGLERROR
662         }
663 }
664
665 static void GL_BindEBO(int bufferobject)
666 {
667         if (gl_state.elementbufferobject != bufferobject)
668         {
669                 gl_state.elementbufferobject = bufferobject;
670                 CHECKGLERROR
671                 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, bufferobject);
672                 CHECKGLERROR
673         }
674 }
675
676 void GL_SetupTextureState(void)
677 {
678         unsigned int i;
679         gltextureunit_t *unit;
680         CHECKGLERROR
681         gl_state.unit = MAX_TEXTUREUNITS;
682         gl_state.clientunit = MAX_TEXTUREUNITS;
683         for (i = 0;i < MAX_TEXTUREUNITS;i++)
684         {
685                 unit = gl_state.units + i;
686                 unit->t1d = 0;
687                 unit->t2d = 0;
688                 unit->t3d = 0;
689                 unit->tcubemap = 0;
690                 unit->arrayenabled = false;
691                 unit->arraycomponents = 0;
692                 unit->pointer_texcoord = NULL;
693                 unit->pointer_texcoord_buffer = 0;
694                 unit->pointer_texcoord_offset = 0;
695                 unit->rgbscale = 1;
696                 unit->alphascale = 1;
697                 unit->combinergb = GL_MODULATE;
698                 unit->combinealpha = GL_MODULATE;
699                 unit->texmatrixenabled = false;
700                 unit->matrix = identitymatrix;
701         }
702
703         for (i = 0;i < backendimageunits;i++)
704         {
705                 GL_ActiveTexture(i);
706                 qglBindTexture(GL_TEXTURE_1D, 0);CHECKGLERROR
707                 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
708                 if (gl_texture3d)
709                 {
710                         qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
711                 }
712                 if (gl_texturecubemap)
713                 {
714                         qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
715                 }
716                 if (gl_texturerectangle)
717                 {
718                         qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);CHECKGLERROR
719                 }
720         }
721
722         for (i = 0;i < backendarrayunits;i++)
723         {
724                 GL_ClientActiveTexture(i);
725                 GL_BindVBO(0);
726                 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
727                 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
728         }
729
730         for (i = 0;i < backendunits;i++)
731         {
732                 GL_ActiveTexture(i);
733                 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
734                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
735                 if (gl_texture3d)
736                 {
737                         qglDisable(GL_TEXTURE_3D);CHECKGLERROR
738                 }
739                 if (gl_texturecubemap)
740                 {
741                         qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
742                 }
743                 if (gl_texturerectangle)
744                 {
745                         qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
746                 }
747                 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
748                 qglLoadIdentity();CHECKGLERROR
749                 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
750                 if (gl_combine.integer)
751                 {
752                         qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);CHECKGLERROR
753                         qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);CHECKGLERROR
754                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);CHECKGLERROR
755                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
756                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_TEXTURE);CHECKGLERROR // for GL_INTERPOLATE_ARB mode
757                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
758                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
759                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);CHECKGLERROR
760                         qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);CHECKGLERROR
761                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);CHECKGLERROR
762                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
763                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, GL_CONSTANT_ARB);CHECKGLERROR
764                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
765                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
766                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
767                         qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
768                         qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
769                 }
770                 else
771                 {
772                         qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
773                 }
774                 CHECKGLERROR
775         }
776         CHECKGLERROR
777 }
778
779 void GL_Backend_ResetState(void)
780 {
781         memset(&gl_state, 0, sizeof(gl_state));
782         gl_state.depthtest = true;
783         gl_state.alphatest = false;
784         gl_state.blendfunc1 = GL_ONE;
785         gl_state.blendfunc2 = GL_ZERO;
786         gl_state.blend = false;
787         gl_state.depthmask = GL_TRUE;
788         gl_state.colormask = 15;
789         gl_state.color4f[0] = gl_state.color4f[1] = gl_state.color4f[2] = gl_state.color4f[3] = 1;
790         gl_state.lockrange_first = 0;
791         gl_state.lockrange_count = 0;
792         gl_state.cullface = v_flipped_state ? GL_BACK : GL_FRONT; // quake is backwards, this culls back faces
793         gl_state.cullfaceenable = true;
794         gl_state.polygonoffset[0] = 0;
795         gl_state.polygonoffset[1] = 0;
796
797         CHECKGLERROR
798
799         qglColorMask(1, 1, 1, 1);
800         qglAlphaFunc(GL_GEQUAL, 0.5);CHECKGLERROR
801         qglDisable(GL_ALPHA_TEST);CHECKGLERROR
802         qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
803         qglDisable(GL_BLEND);CHECKGLERROR
804         qglCullFace(gl_state.cullface);CHECKGLERROR
805         qglEnable(GL_CULL_FACE);CHECKGLERROR
806         qglDepthFunc(GL_LEQUAL);CHECKGLERROR
807         qglEnable(GL_DEPTH_TEST);CHECKGLERROR
808         qglDepthMask(gl_state.depthmask);CHECKGLERROR
809         qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
810
811         if (gl_support_arb_vertex_buffer_object)
812         {
813                 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
814                 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
815         }
816
817         if (gl_support_ext_framebuffer_object)
818         {
819                 qglBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
820                 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
821         }
822
823         qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), NULL);CHECKGLERROR
824         qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
825
826         qglColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL);CHECKGLERROR
827         qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
828
829         GL_Color(0, 0, 0, 0);
830         GL_Color(1, 1, 1, 1);
831
832         GL_SetupTextureState();
833 }
834
835 void GL_ActiveTexture(unsigned int num)
836 {
837         if (gl_state.unit != num)
838         {
839                 gl_state.unit = num;
840                 if (qglActiveTexture)
841                 {
842                         CHECKGLERROR
843                         qglActiveTexture(GL_TEXTURE0_ARB + gl_state.unit);
844                         CHECKGLERROR
845                 }
846         }
847 }
848
849 void GL_ClientActiveTexture(unsigned int num)
850 {
851         if (gl_state.clientunit != num)
852         {
853                 gl_state.clientunit = num;
854                 if (qglActiveTexture)
855                 {
856                         CHECKGLERROR
857                         qglClientActiveTexture(GL_TEXTURE0_ARB + gl_state.clientunit);
858                         CHECKGLERROR
859                 }
860         }
861 }
862
863 void GL_BlendFunc(int blendfunc1, int blendfunc2)
864 {
865         if (gl_state.blendfunc1 != blendfunc1 || gl_state.blendfunc2 != blendfunc2)
866         {
867                 CHECKGLERROR
868                 qglBlendFunc(gl_state.blendfunc1 = blendfunc1, gl_state.blendfunc2 = blendfunc2);CHECKGLERROR
869                 if (gl_state.blendfunc2 == GL_ZERO)
870                 {
871                         if (gl_state.blendfunc1 == GL_ONE)
872                         {
873                                 if (gl_state.blend)
874                                 {
875                                         gl_state.blend = 0;
876                                         qglDisable(GL_BLEND);CHECKGLERROR
877                                 }
878                         }
879                         else
880                         {
881                                 if (!gl_state.blend)
882                                 {
883                                         gl_state.blend = 1;
884                                         qglEnable(GL_BLEND);CHECKGLERROR
885                                 }
886                         }
887                 }
888                 else
889                 {
890                         if (!gl_state.blend)
891                         {
892                                 gl_state.blend = 1;
893                                 qglEnable(GL_BLEND);CHECKGLERROR
894                         }
895                 }
896         }
897 }
898
899 void GL_DepthMask(int state)
900 {
901         if (gl_state.depthmask != state)
902         {
903                 CHECKGLERROR
904                 qglDepthMask(gl_state.depthmask = state);CHECKGLERROR
905         }
906 }
907
908 void GL_DepthTest(int state)
909 {
910         if (gl_state.depthtest != state)
911         {
912                 gl_state.depthtest = state;
913                 CHECKGLERROR
914                 if (gl_state.depthtest)
915                 {
916                         qglEnable(GL_DEPTH_TEST);CHECKGLERROR
917                 }
918                 else
919                 {
920                         qglDisable(GL_DEPTH_TEST);CHECKGLERROR
921                 }
922         }
923 }
924
925 void GL_DepthRange(float nearfrac, float farfrac)
926 {
927         if (gl_state.depthrange[0] != nearfrac || gl_state.depthrange[1] != farfrac)
928         {
929                 gl_state.depthrange[0] = nearfrac;
930                 gl_state.depthrange[1] = farfrac;
931                 qglDepthRange(nearfrac, farfrac);
932         }
933 }
934
935 void GL_PolygonOffset(float planeoffset, float depthoffset)
936 {
937         if (gl_state.polygonoffset[0] != planeoffset || gl_state.polygonoffset[1] != depthoffset)
938         {
939                 gl_state.polygonoffset[0] = planeoffset;
940                 gl_state.polygonoffset[1] = depthoffset;
941                 qglPolygonOffset(planeoffset, depthoffset);
942         }
943 }
944
945 void GL_SetMirrorState(qboolean state)
946 {
947         if(!state != !v_flipped_state)
948         {
949                 // change cull face mode!
950                 if(gl_state.cullface == GL_BACK)
951                         qglCullFace((gl_state.cullface = GL_FRONT));
952                 else if(gl_state.cullface == GL_FRONT)
953                         qglCullFace((gl_state.cullface = GL_BACK));
954         }
955         v_flipped_state = state;
956 }
957
958 void GL_CullFace(int state)
959 {
960         CHECKGLERROR
961
962         if(v_flipped_state)
963         {
964                 if(state == GL_FRONT)
965                         state = GL_BACK;
966                 else if(state == GL_BACK)
967                         state = GL_FRONT;
968         }
969
970         if (state != GL_NONE)
971         {
972                 if (!gl_state.cullfaceenable)
973                 {
974                         gl_state.cullfaceenable = true;
975                         qglEnable(GL_CULL_FACE);CHECKGLERROR
976                 }
977                 if (gl_state.cullface != state)
978                 {
979                         gl_state.cullface = state;
980                         qglCullFace(gl_state.cullface);CHECKGLERROR
981                 }
982         }
983         else
984         {
985                 if (gl_state.cullfaceenable)
986                 {
987                         gl_state.cullfaceenable = false;
988                         qglDisable(GL_CULL_FACE);CHECKGLERROR
989                 }
990         }
991 }
992
993 void GL_AlphaTest(int state)
994 {
995         if (gl_state.alphatest != state)
996         {
997                 gl_state.alphatest = state;
998                 CHECKGLERROR
999                 if (gl_state.alphatest)
1000                 {
1001                         qglEnable(GL_ALPHA_TEST);CHECKGLERROR
1002                 }
1003                 else
1004                 {
1005                         qglDisable(GL_ALPHA_TEST);CHECKGLERROR
1006                 }
1007         }
1008 }
1009
1010 void GL_ColorMask(int r, int g, int b, int a)
1011 {
1012         int state = r*8 + g*4 + b*2 + a*1;
1013         if (gl_state.colormask != state)
1014         {
1015                 gl_state.colormask = state;
1016                 CHECKGLERROR
1017                 qglColorMask((GLboolean)r, (GLboolean)g, (GLboolean)b, (GLboolean)a);CHECKGLERROR
1018         }
1019 }
1020
1021 void GL_Color(float cr, float cg, float cb, float ca)
1022 {
1023         if (gl_state.pointer_color_enabled || gl_state.color4f[0] != cr || gl_state.color4f[1] != cg || gl_state.color4f[2] != cb || gl_state.color4f[3] != ca)
1024         {
1025                 gl_state.color4f[0] = cr;
1026                 gl_state.color4f[1] = cg;
1027                 gl_state.color4f[2] = cb;
1028                 gl_state.color4f[3] = ca;
1029                 CHECKGLERROR
1030                 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);
1031                 CHECKGLERROR
1032         }
1033 }
1034
1035 void GL_LockArrays(int first, int count)
1036 {
1037         if (count < gl_lockarrays_minimumvertices.integer)
1038         {
1039                 first = 0;
1040                 count = 0;
1041         }
1042         if (gl_state.lockrange_count != count || gl_state.lockrange_first != first)
1043         {
1044                 if (gl_state.lockrange_count)
1045                 {
1046                         gl_state.lockrange_count = 0;
1047                         CHECKGLERROR
1048                         qglUnlockArraysEXT();
1049                         CHECKGLERROR
1050                 }
1051                 if (count && gl_supportslockarrays && gl_lockarrays.integer)
1052                 {
1053                         gl_state.lockrange_first = first;
1054                         gl_state.lockrange_count = count;
1055                         CHECKGLERROR
1056                         qglLockArraysEXT(first, count);
1057                         CHECKGLERROR
1058                 }
1059         }
1060 }
1061
1062 void GL_Scissor (int x, int y, int width, int height)
1063 {
1064         CHECKGLERROR
1065         qglScissor(x, y,width,height);
1066         CHECKGLERROR
1067 }
1068
1069 void GL_ScissorTest(int state)
1070 {
1071         if(gl_state.scissortest == state)
1072                 return;
1073
1074         CHECKGLERROR
1075         if((gl_state.scissortest = state))
1076                 qglEnable(GL_SCISSOR_TEST);
1077         else
1078                 qglDisable(GL_SCISSOR_TEST);
1079         CHECKGLERROR
1080 }
1081
1082 void GL_Clear(int mask)
1083 {
1084         CHECKGLERROR
1085         qglClear(mask);CHECKGLERROR
1086 }
1087
1088 // called at beginning of frame
1089 void R_Mesh_Start(void)
1090 {
1091         BACKENDACTIVECHECK
1092         CHECKGLERROR
1093         if (gl_printcheckerror.integer && !gl_paranoid.integer)
1094         {
1095                 Con_Printf("WARNING: gl_printcheckerror is on but gl_paranoid is off, turning it on...\n");
1096                 Cvar_SetValueQuick(&gl_paranoid, 1);
1097         }
1098         GL_Backend_ResetState();
1099 }
1100
1101 qboolean GL_Backend_CompileShader(int programobject, GLenum shadertypeenum, const char *shadertype, int numstrings, const char **strings)
1102 {
1103         int shaderobject;
1104         int shadercompiled;
1105         char compilelog[MAX_INPUTLINE];
1106         shaderobject = qglCreateShaderObjectARB(shadertypeenum);CHECKGLERROR
1107         if (!shaderobject)
1108                 return false;
1109         qglShaderSourceARB(shaderobject, numstrings, strings, NULL);CHECKGLERROR
1110         qglCompileShaderARB(shaderobject);CHECKGLERROR
1111         qglGetObjectParameterivARB(shaderobject, GL_OBJECT_COMPILE_STATUS_ARB, &shadercompiled);CHECKGLERROR
1112         qglGetInfoLogARB(shaderobject, sizeof(compilelog), NULL, compilelog);CHECKGLERROR
1113         if (compilelog[0] && developer.integer > 0)
1114         {
1115                 int i, j, pretextlines = 0;
1116                 for (i = 0;i < numstrings - 1;i++)
1117                         for (j = 0;strings[i][j];j++)
1118                                 if (strings[i][j] == '\n')
1119                                         pretextlines++;
1120                 Con_DPrintf("%s shader compile log:\n%s\n(line offset for any above warnings/errors: %i)\n", shadertype, compilelog, pretextlines);
1121         }
1122         if (!shadercompiled)
1123         {
1124                 qglDeleteObjectARB(shaderobject);CHECKGLERROR
1125                 return false;
1126         }
1127         qglAttachObjectARB(programobject, shaderobject);CHECKGLERROR
1128         qglDeleteObjectARB(shaderobject);CHECKGLERROR
1129         return true;
1130 }
1131
1132 unsigned int GL_Backend_CompileProgram(int vertexstrings_count, const char **vertexstrings_list, int geometrystrings_count, const char **geometrystrings_list, int fragmentstrings_count, const char **fragmentstrings_list)
1133 {
1134         GLint programlinked;
1135         GLuint programobject = 0;
1136         char linklog[MAX_INPUTLINE];
1137         CHECKGLERROR
1138
1139         programobject = qglCreateProgramObjectARB();CHECKGLERROR
1140         if (!programobject)
1141                 return 0;
1142
1143         if (vertexstrings_count && !GL_Backend_CompileShader(programobject, GL_VERTEX_SHADER_ARB, "vertex", vertexstrings_count, vertexstrings_list))
1144                 goto cleanup;
1145
1146 #ifdef GL_GEOMETRY_SHADER_ARB
1147         if (geometrystrings_count && !GL_Backend_CompileShader(programobject, GL_GEOMETRY_SHADER_ARB, "geometry", geometrystrings_count, geometrystrings_list))
1148                 goto cleanup;
1149 #endif
1150
1151         if (fragmentstrings_count && !GL_Backend_CompileShader(programobject, GL_FRAGMENT_SHADER_ARB, "fragment", fragmentstrings_count, fragmentstrings_list))
1152                 goto cleanup;
1153
1154         qglLinkProgramARB(programobject);CHECKGLERROR
1155         qglGetObjectParameterivARB(programobject, GL_OBJECT_LINK_STATUS_ARB, &programlinked);CHECKGLERROR
1156         qglGetInfoLogARB(programobject, sizeof(linklog), NULL, linklog);CHECKGLERROR
1157         if (linklog[0])
1158         {
1159                 Con_DPrintf("program link log:\n%s\n", linklog);
1160                 // software vertex shader is ok but software fragment shader is WAY
1161                 // too slow, fail program if so.
1162                 // NOTE: this string might be ATI specific, but that's ok because the
1163                 // ATI R300 chip (Radeon 9500-9800/X300) is the most likely to use a
1164                 // software fragment shader due to low instruction and dependent
1165                 // texture limits.
1166                 if (strstr(linklog, "fragment shader will run in software"))
1167                         programlinked = false;
1168         }
1169         if (!programlinked)
1170                 goto cleanup;
1171         return programobject;
1172 cleanup:
1173         qglDeleteObjectARB(programobject);CHECKGLERROR
1174         return 0;
1175 }
1176
1177 void GL_Backend_FreeProgram(unsigned int prog)
1178 {
1179         CHECKGLERROR
1180         qglDeleteObjectARB(prog);
1181         CHECKGLERROR
1182 }
1183
1184 int gl_backend_rebindtextures;
1185
1186 void GL_Backend_RenumberElements(int *out, int count, const int *in, int offset)
1187 {
1188         int i;
1189         if (offset)
1190         {
1191                 for (i = 0;i < count;i++)
1192                         *out++ = *in++ + offset;
1193         }
1194         else
1195                 memcpy(out, in, sizeof(*out) * count);
1196 }
1197
1198 // renders triangles using vertices from the active arrays
1199 int paranoidblah = 0;
1200 void R_Mesh_Draw(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const unsigned short *element3s, int bufferobject3i, int bufferobject3s)
1201 {
1202         unsigned int numelements = numtriangles * 3;
1203         if (numvertices < 3 || numtriangles < 1)
1204         {
1205                 if (numvertices < 0 || numtriangles < 0 || developer.integer >= 100)
1206                         Con_Printf("R_Mesh_Draw(%d, %d, %d, %d, %8p, %8p, %i, %i);\n", firstvertex, numvertices, firsttriangle, numtriangles, (void *)element3i, (void *)element3s, bufferobject3i, bufferobject3s);
1207                 return;
1208         }
1209         if (!gl_mesh_prefer_short_elements.integer)
1210         {
1211                 if (element3i)
1212                         element3s = NULL;
1213                 if (bufferobject3i)
1214                         bufferobject3s = 0;
1215         }
1216         if (element3i)
1217                 element3i += firsttriangle * 3;
1218         if (element3s)
1219                 element3s += firsttriangle * 3;
1220         switch (gl_vbo.integer)
1221         {
1222         default:
1223         case 0:
1224         case 2:
1225                 bufferobject3i = bufferobject3s = 0;
1226                 break;
1227         case 1:
1228                 break;
1229         case 3:
1230                 if (firsttriangle)
1231                         bufferobject3i = bufferobject3s = 0;
1232                 break;
1233         }
1234         CHECKGLERROR
1235         r_refdef.stats.meshes++;
1236         r_refdef.stats.meshes_elements += numelements;
1237         if (gl_paranoid.integer)
1238         {
1239                 unsigned int i, j, size;
1240                 const int *p;
1241                 // note: there's no validation done here on buffer objects because it
1242                 // is somewhat difficult to get at the data, and gl_paranoid can be
1243                 // used without buffer objects if the need arises
1244                 // (the data could be gotten using glMapBuffer but it would be very
1245                 //  slow due to uncachable video memory reads)
1246                 if (!qglIsEnabled(GL_VERTEX_ARRAY))
1247                         Con_Print("R_Mesh_Draw: vertex array not enabled\n");
1248                 CHECKGLERROR
1249                 if (gl_state.pointer_vertex)
1250                         for (j = 0, size = numvertices * 3, p = (int *)((float *)gl_state.pointer_vertex + firstvertex * 3);j < size;j++, p++)
1251                                 paranoidblah += *p;
1252                 if (gl_state.pointer_color_enabled)
1253                 {
1254                         if (!qglIsEnabled(GL_COLOR_ARRAY))
1255                                 Con_Print("R_Mesh_Draw: color array set but not enabled\n");
1256                         CHECKGLERROR
1257                         if (gl_state.pointer_color && gl_state.pointer_color_enabled)
1258                                 for (j = 0, size = numvertices * 4, p = (int *)((float *)gl_state.pointer_color + firstvertex * 4);j < size;j++, p++)
1259                                         paranoidblah += *p;
1260                 }
1261                 for (i = 0;i < backendarrayunits;i++)
1262                 {
1263                         if (gl_state.units[i].arrayenabled)
1264                         {
1265                                 GL_ClientActiveTexture(i);
1266                                 if (!qglIsEnabled(GL_TEXTURE_COORD_ARRAY))
1267                                         Con_Print("R_Mesh_Draw: texcoord array set but not enabled\n");
1268                                 CHECKGLERROR
1269                                 if (gl_state.units[i].pointer_texcoord && gl_state.units[i].arrayenabled)
1270                                         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++)
1271                                                 paranoidblah += *p;
1272                         }
1273                 }
1274                 if (element3i)
1275                 {
1276                         for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1277                         {
1278                                 if (element3i[i] < firstvertex || element3i[i] >= firstvertex + numvertices)
1279                                 {
1280                                         Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3i array\n", element3i[i], firstvertex, firstvertex + numvertices);
1281                                         return;
1282                                 }
1283                         }
1284                 }
1285                 if (element3s)
1286                 {
1287                         for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1288                         {
1289                                 if (element3s[i] < firstvertex || element3s[i] >= firstvertex + numvertices)
1290                                 {
1291                                         Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3s array\n", element3s[i], firstvertex, firstvertex + numvertices);
1292                                         return;
1293                                 }
1294                         }
1295                 }
1296                 CHECKGLERROR
1297         }
1298         if (r_render.integer || r_refdef.draw2dstage)
1299         {
1300                 CHECKGLERROR
1301                 if (gl_mesh_testmanualfeeding.integer)
1302                 {
1303                         unsigned int i, j, element;
1304                         const GLfloat *p;
1305                         qglBegin(GL_TRIANGLES);
1306                         for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1307                         {
1308                                 element = element3i ? element3i[i] : element3s[i];
1309                                 for (j = 0;j < backendarrayunits;j++)
1310                                 {
1311                                         if (gl_state.units[j].pointer_texcoord && gl_state.units[j].arrayenabled)
1312                                         {
1313                                                 if (backendarrayunits > 1)
1314                                                 {
1315                                                         if (gl_state.units[j].arraycomponents == 4)
1316                                                         {
1317                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 4;
1318                                                                 qglMultiTexCoord4f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2], p[3]);
1319                                                         }
1320                                                         else if (gl_state.units[j].arraycomponents == 3)
1321                                                         {
1322                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 3;
1323                                                                 qglMultiTexCoord3f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2]);
1324                                                         }
1325                                                         else if (gl_state.units[j].arraycomponents == 2)
1326                                                         {
1327                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 2;
1328                                                                 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, p[0], p[1]);
1329                                                         }
1330                                                         else
1331                                                         {
1332                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 1;
1333                                                                 qglMultiTexCoord1f(GL_TEXTURE0_ARB + j, p[0]);
1334                                                         }
1335                                                 }
1336                                                 else
1337                                                 {
1338                                                         if (gl_state.units[j].arraycomponents == 4)
1339                                                         {
1340                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 4;
1341                                                                 qglTexCoord4f(p[0], p[1], p[2], p[3]);
1342                                                         }
1343                                                         else if (gl_state.units[j].arraycomponents == 3)
1344                                                         {
1345                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 3;
1346                                                                 qglTexCoord3f(p[0], p[1], p[2]);
1347                                                         }
1348                                                         else if (gl_state.units[j].arraycomponents == 2)
1349                                                         {
1350                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 2;
1351                                                                 qglTexCoord2f(p[0], p[1]);
1352                                                         }
1353                                                         else
1354                                                         {
1355                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 1;
1356                                                                 qglTexCoord1f(p[0]);
1357                                                         }
1358                                                 }
1359                                         }
1360                                 }
1361                                 if (gl_state.pointer_color && gl_state.pointer_color_enabled)
1362                                 {
1363                                         p = ((const GLfloat *)(gl_state.pointer_color)) + element * 4;
1364                                         qglColor4f(p[0], p[1], p[2], p[3]);
1365                                 }
1366                                 p = ((const GLfloat *)(gl_state.pointer_vertex)) + element * 3;
1367                                 qglVertex3f(p[0], p[1], p[2]);
1368                         }
1369                         qglEnd();
1370                         CHECKGLERROR
1371                 }
1372                 else if (gl_mesh_testarrayelement.integer)
1373                 {
1374                         int i;
1375                         qglBegin(GL_TRIANGLES);
1376                         if (element3i)
1377                         {
1378                                 for (i = 0;i < numtriangles * 3;i++)
1379                                         qglArrayElement(element3i[i]);
1380                         }
1381                         else if (element3s)
1382                         {
1383                                 for (i = 0;i < numtriangles * 3;i++)
1384                                         qglArrayElement(element3s[i]);
1385                         }
1386                         qglEnd();
1387                         CHECKGLERROR
1388                 }
1389                 else if (bufferobject3s)
1390                 {
1391                         GL_BindEBO(bufferobject3s);
1392                         if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1393                         {
1394                                 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, (void *)(firsttriangle * sizeof(unsigned short[3])));
1395                                 CHECKGLERROR
1396                         }
1397                         else
1398                         {
1399                                 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, (void *)(firsttriangle * sizeof(unsigned short[3])));
1400                                 CHECKGLERROR
1401                         }
1402                 }
1403                 else if (bufferobject3i)
1404                 {
1405                         GL_BindEBO(bufferobject3i);
1406                         if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1407                         {
1408                                 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, (void *)(firsttriangle * sizeof(unsigned int[3])));
1409                                 CHECKGLERROR
1410                         }
1411                         else
1412                         {
1413                                 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, (void *)(firsttriangle * sizeof(unsigned int[3])));
1414                                 CHECKGLERROR
1415                         }
1416                 }
1417                 else if (element3s)
1418                 {
1419                         GL_BindEBO(0);
1420                         if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1421                         {
1422                                 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, element3s);
1423                                 CHECKGLERROR
1424                         }
1425                         else
1426                         {
1427                                 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, element3s);
1428                                 CHECKGLERROR
1429                         }
1430                 }
1431                 else if (element3i)
1432                 {
1433                         GL_BindEBO(0);
1434                         if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1435                         {
1436                                 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, element3i);
1437                                 CHECKGLERROR
1438                         }
1439                         else
1440                         {
1441                                 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, element3i);
1442                                 CHECKGLERROR
1443                         }
1444                 }
1445         }
1446 }
1447
1448 // restores backend state, used when done with 3D rendering
1449 void R_Mesh_Finish(void)
1450 {
1451         unsigned int i;
1452         BACKENDACTIVECHECK
1453         CHECKGLERROR
1454         GL_LockArrays(0, 0);
1455         CHECKGLERROR
1456
1457         for (i = 0;i < backendimageunits;i++)
1458         {
1459                 GL_ActiveTexture(i);
1460                 qglBindTexture(GL_TEXTURE_1D, 0);CHECKGLERROR
1461                 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
1462                 if (gl_texture3d)
1463                 {
1464                         qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
1465                 }
1466                 if (gl_texturecubemap)
1467                 {
1468                         qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
1469                 }
1470                 if (gl_texturerectangle)
1471                 {
1472                         qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);CHECKGLERROR
1473                 }
1474         }
1475         for (i = 0;i < backendarrayunits;i++)
1476         {
1477                 GL_ActiveTexture(backendarrayunits - 1 - i);
1478                 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1479         }
1480         for (i = 0;i < backendunits;i++)
1481         {
1482                 GL_ActiveTexture(backendunits - 1 - i);
1483                 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1484                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1485                 if (gl_texture3d)
1486                 {
1487                         qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1488                 }
1489                 if (gl_texturecubemap)
1490                 {
1491                         qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1492                 }
1493                 if (gl_texturerectangle)
1494                 {
1495                         qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
1496                 }
1497                 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
1498                 if (gl_combine.integer)
1499                 {
1500                         qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
1501                         qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
1502                 }
1503         }
1504         qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1505         qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
1506
1507         qglDisable(GL_BLEND);CHECKGLERROR
1508         qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1509         qglDepthMask(GL_TRUE);CHECKGLERROR
1510         qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);CHECKGLERROR
1511 }
1512
1513 int R_Mesh_CreateStaticBufferObject(unsigned int target, void *data, size_t size, const char *name)
1514 {
1515         gl_bufferobjectinfo_t *info;
1516         GLuint bufferobject;
1517
1518         if (!gl_vbo.integer)
1519                 return 0;
1520
1521         qglGenBuffersARB(1, &bufferobject);
1522         switch(target)
1523         {
1524         case GL_ELEMENT_ARRAY_BUFFER_ARB: GL_BindEBO(bufferobject);break;
1525         case GL_ARRAY_BUFFER_ARB: GL_BindVBO(bufferobject);break;
1526         default: Sys_Error("R_Mesh_CreateStaticBufferObject: unknown target type %i\n", target);return 0;
1527         }
1528         qglBufferDataARB(target, size, data, GL_STATIC_DRAW_ARB);
1529
1530         info = (gl_bufferobjectinfo_t *) Mem_ExpandableArray_AllocRecord(&gl_bufferobjectinfoarray);
1531         memset(info, 0, sizeof(*info));
1532         info->target = target;
1533         info->object = bufferobject;
1534         info->size = size;
1535         strlcpy(info->name, name, sizeof(info->name));
1536
1537         return (int)bufferobject;
1538 }
1539
1540 void R_Mesh_DestroyBufferObject(int bufferobject)
1541 {
1542         int i, endindex;
1543         gl_bufferobjectinfo_t *info;
1544
1545         qglDeleteBuffersARB(1, (GLuint *)&bufferobject);
1546
1547         endindex = Mem_ExpandableArray_IndexRange(&gl_bufferobjectinfoarray);
1548         for (i = 0;i < endindex;i++)
1549         {
1550                 info = (gl_bufferobjectinfo_t *) Mem_ExpandableArray_RecordAtIndex(&gl_bufferobjectinfoarray, i);
1551                 if (!info)
1552                         continue;
1553                 if (info->object == bufferobject)
1554                 {
1555                         Mem_ExpandableArray_FreeRecord(&gl_bufferobjectinfoarray, (void *)info);
1556                         break;
1557                 }
1558         }
1559 }
1560
1561 void GL_Mesh_ListVBOs(qboolean printeach)
1562 {
1563         int i, endindex;
1564         size_t ebocount = 0, ebomemory = 0;
1565         size_t vbocount = 0, vbomemory = 0;
1566         gl_bufferobjectinfo_t *info;
1567         endindex = Mem_ExpandableArray_IndexRange(&gl_bufferobjectinfoarray);
1568         for (i = 0;i < endindex;i++)
1569         {
1570                 info = (gl_bufferobjectinfo_t *) Mem_ExpandableArray_RecordAtIndex(&gl_bufferobjectinfoarray, i);
1571                 if (!info)
1572                         continue;
1573                 switch(info->target)
1574                 {
1575                 case GL_ELEMENT_ARRAY_BUFFER_ARB: ebocount++;ebomemory += info->size;if (printeach) Con_Printf("EBO #%i %s = %i bytes\n", info->object, info->name, (int)info->size);break;
1576                 case GL_ARRAY_BUFFER_ARB: vbocount++;vbomemory += info->size;if (printeach) Con_Printf("VBO #%i %s = %i bytes\n", info->object, info->name, (int)info->size);break;
1577                 default: Con_Printf("gl_vbostats: unknown target type %i\n", info->target);break;
1578                 }
1579         }
1580         Con_Printf("vertex buffers: %i element buffers totalling %i bytes (%.3f MB), %i vertex buffers totalling %i bytes (%.3f MB), combined %i bytes (%.3fMB)\n", (int)ebocount, (int)ebomemory, ebomemory / 1048576.0, (int)vbocount, (int)vbomemory, vbomemory / 1048576.0, (int)(ebomemory + vbomemory), (ebomemory + vbomemory) / 1048576.0);
1581 }
1582
1583 void R_Mesh_Matrix(const matrix4x4_t *matrix)
1584 {
1585         if (memcmp(matrix, &backend_modelmatrix, sizeof(matrix4x4_t)))
1586         {
1587                 float glmatrix[16];
1588                 backend_modelmatrix = *matrix;
1589                 Matrix4x4_Concat(&backend_modelviewmatrix, &backend_viewport.viewmatrix, &backend_modelmatrix);
1590                 Matrix4x4_ToArrayFloatGL(&backend_modelviewmatrix, glmatrix);
1591                 CHECKGLERROR
1592                 qglLoadMatrixf(glmatrix);CHECKGLERROR
1593         }
1594 }
1595
1596 void R_Mesh_VertexPointer(const float *vertex3f, int bufferobject, size_t bufferoffset)
1597 {
1598         if (!gl_vbo.integer || gl_mesh_testarrayelement.integer)
1599                 bufferobject = 0;
1600         if (gl_state.pointer_vertex != vertex3f || gl_state.pointer_vertex_buffer != bufferobject || gl_state.pointer_vertex_offset != bufferoffset)
1601         {
1602                 gl_state.pointer_vertex = vertex3f;
1603                 gl_state.pointer_vertex_buffer = bufferobject;
1604                 gl_state.pointer_vertex_offset = bufferoffset;
1605                 CHECKGLERROR
1606                 GL_BindVBO(bufferobject);
1607                 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), bufferobject ? (void *)bufferoffset : vertex3f);CHECKGLERROR
1608         }
1609 }
1610
1611 void R_Mesh_ColorPointer(const float *color4f, int bufferobject, size_t bufferoffset)
1612 {
1613         // note: this can not rely on bufferobject to decide whether a color array
1614         // is supplied, because surfmesh_t shares one vbo for all arrays, which
1615         // means that a valid vbo may be supplied even if there is no color array.
1616         if (color4f)
1617         {
1618                 if (!gl_vbo.integer || gl_mesh_testarrayelement.integer)
1619                         bufferobject = 0;
1620                 // caller wants color array enabled
1621                 if (!gl_state.pointer_color_enabled)
1622                 {
1623                         gl_state.pointer_color_enabled = true;
1624                         CHECKGLERROR
1625                         qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1626                 }
1627                 if (gl_state.pointer_color != color4f || gl_state.pointer_color_buffer != bufferobject || gl_state.pointer_color_offset != bufferoffset)
1628                 {
1629                         gl_state.pointer_color = color4f;
1630                         gl_state.pointer_color_buffer = bufferobject;
1631                         gl_state.pointer_color_offset = bufferoffset;
1632                         CHECKGLERROR
1633                         GL_BindVBO(bufferobject);
1634                         qglColorPointer(4, GL_FLOAT, sizeof(float[4]), bufferobject ? (void *)bufferoffset : color4f);CHECKGLERROR
1635                 }
1636         }
1637         else
1638         {
1639                 // caller wants color array disabled
1640                 if (gl_state.pointer_color_enabled)
1641                 {
1642                         gl_state.pointer_color_enabled = false;
1643                         CHECKGLERROR
1644                         qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1645                         // when color array is on the glColor gets trashed, set it again
1646                         qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);CHECKGLERROR
1647                 }
1648         }
1649 }
1650
1651 void R_Mesh_TexCoordPointer(unsigned int unitnum, unsigned int numcomponents, const float *texcoord, int bufferobject, size_t bufferoffset)
1652 {
1653         gltextureunit_t *unit = gl_state.units + unitnum;
1654         // update array settings
1655         CHECKGLERROR
1656         // note: there is no need to check bufferobject here because all cases
1657         // that involve a valid bufferobject also supply a texcoord array
1658         if (texcoord)
1659         {
1660                 if (!gl_vbo.integer || gl_mesh_testarrayelement.integer)
1661                         bufferobject = 0;
1662                 // texture array unit is enabled, enable the array
1663                 if (!unit->arrayenabled)
1664                 {
1665                         unit->arrayenabled = true;
1666                         GL_ClientActiveTexture(unitnum);
1667                         qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1668                 }
1669                 // texcoord array
1670                 if (unit->pointer_texcoord != texcoord || unit->pointer_texcoord_buffer != bufferobject || unit->pointer_texcoord_offset != bufferoffset || unit->arraycomponents != numcomponents)
1671                 {
1672                         unit->pointer_texcoord = texcoord;
1673                         unit->pointer_texcoord_buffer = bufferobject;
1674                         unit->pointer_texcoord_offset = bufferoffset;
1675                         unit->arraycomponents = numcomponents;
1676                         GL_ClientActiveTexture(unitnum);
1677                         GL_BindVBO(bufferobject);
1678                         qglTexCoordPointer(unit->arraycomponents, GL_FLOAT, sizeof(float) * unit->arraycomponents, bufferobject ? (void *)bufferoffset : texcoord);CHECKGLERROR
1679                 }
1680         }
1681         else
1682         {
1683                 // texture array unit is disabled, disable the array
1684                 if (unit->arrayenabled)
1685                 {
1686                         unit->arrayenabled = false;
1687                         GL_ClientActiveTexture(unitnum);
1688                         qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1689                 }
1690         }
1691 }
1692
1693 void R_Mesh_TexBindAll(unsigned int unitnum, int tex1d, int tex2d, int tex3d, int texcubemap, int texrectangle)
1694 {
1695         gltextureunit_t *unit = gl_state.units + unitnum;
1696         if (unitnum >= backendimageunits)
1697                 return;
1698         // update 1d texture binding
1699         if (unit->t1d != tex1d)
1700         {
1701                 GL_ActiveTexture(unitnum);
1702                 if (unitnum < backendunits)
1703                 {
1704                         if (tex1d)
1705                         {
1706                                 if (unit->t1d == 0)
1707                                 {
1708                                         qglEnable(GL_TEXTURE_1D);CHECKGLERROR
1709                                 }
1710                         }
1711                         else
1712                         {
1713                                 if (unit->t1d)
1714                                 {
1715                                         qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1716                                 }
1717                         }
1718                 }
1719                 unit->t1d = tex1d;
1720                 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1721         }
1722         // update 2d texture binding
1723         if (unit->t2d != tex2d)
1724         {
1725                 GL_ActiveTexture(unitnum);
1726                 if (unitnum < backendunits)
1727                 {
1728                         if (tex2d)
1729                         {
1730                                 if (unit->t2d == 0)
1731                                 {
1732                                         qglEnable(GL_TEXTURE_2D);CHECKGLERROR
1733                                 }
1734                         }
1735                         else
1736                         {
1737                                 if (unit->t2d)
1738                                 {
1739                                         qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1740                                 }
1741                         }
1742                 }
1743                 unit->t2d = tex2d;
1744                 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1745         }
1746         // update 3d texture binding
1747         if (unit->t3d != tex3d)
1748         {
1749                 GL_ActiveTexture(unitnum);
1750                 if (unitnum < backendunits)
1751                 {
1752                         if (tex3d)
1753                         {
1754                                 if (unit->t3d == 0)
1755                                 {
1756                                         qglEnable(GL_TEXTURE_3D);CHECKGLERROR
1757                                 }
1758                         }
1759                         else
1760                         {
1761                                 if (unit->t3d)
1762                                 {
1763                                         qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1764                                 }
1765                         }
1766                 }
1767                 unit->t3d = tex3d;
1768                 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1769         }
1770         // update cubemap texture binding
1771         if (unit->tcubemap != texcubemap)
1772         {
1773                 GL_ActiveTexture(unitnum);
1774                 if (unitnum < backendunits)
1775                 {
1776                         if (texcubemap)
1777                         {
1778                                 if (unit->tcubemap == 0)
1779                                 {
1780                                         qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1781                                 }
1782                         }
1783                         else
1784                         {
1785                                 if (unit->tcubemap)
1786                                 {
1787                                         qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1788                                 }
1789                         }
1790                 }
1791                 unit->tcubemap = texcubemap;
1792                 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1793         }
1794         // update rectangle texture binding
1795         if (unit->trectangle != texrectangle)
1796         {
1797                 GL_ActiveTexture(unitnum);
1798                 if (unitnum < backendunits)
1799                 {
1800                         if (texrectangle)
1801                         {
1802                                 if (unit->trectangle == 0)
1803                                 {
1804                                         qglEnable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
1805                                 }
1806                         }
1807                         else
1808                         {
1809                                 if (unit->trectangle)
1810                                 {
1811                                         qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
1812                                 }
1813                         }
1814                 }
1815                 unit->trectangle = texrectangle;
1816                 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
1817         }
1818 }
1819
1820 void R_Mesh_TexBind1D(unsigned int unitnum, int texnum)
1821 {
1822         gltextureunit_t *unit = gl_state.units + unitnum;
1823         if (unitnum >= backendimageunits)
1824                 return;
1825         // update 1d texture binding
1826         if (unit->t1d != texnum)
1827         {
1828                 GL_ActiveTexture(unitnum);
1829                 if (unitnum < backendunits)
1830                 {
1831                         if (texnum)
1832                         {
1833                                 if (unit->t1d == 0)
1834                                 {
1835                                         qglEnable(GL_TEXTURE_1D);CHECKGLERROR
1836                                 }
1837                         }
1838                         else
1839                         {
1840                                 if (unit->t1d)
1841                                 {
1842                                         qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1843                                 }
1844                         }
1845                 }
1846                 unit->t1d = texnum;
1847                 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1848         }
1849         // update 2d texture binding
1850         if (unit->t2d)
1851         {
1852                 GL_ActiveTexture(unitnum);
1853                 if (unitnum < backendunits)
1854                 {
1855                         if (unit->t2d)
1856                         {
1857                                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1858                         }
1859                 }
1860                 unit->t2d = 0;
1861                 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1862         }
1863         // update 3d texture binding
1864         if (unit->t3d)
1865         {
1866                 GL_ActiveTexture(unitnum);
1867                 if (unitnum < backendunits)
1868                 {
1869                         if (unit->t3d)
1870                         {
1871                                 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1872                         }
1873                 }
1874                 unit->t3d = 0;
1875                 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1876         }
1877         // update cubemap texture binding
1878         if (unit->tcubemap)
1879         {
1880                 GL_ActiveTexture(unitnum);
1881                 if (unitnum < backendunits)
1882                 {
1883                         if (unit->tcubemap)
1884                         {
1885                                 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1886                         }
1887                 }
1888                 unit->tcubemap = 0;
1889                 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1890         }
1891         // update rectangle texture binding
1892         if (unit->trectangle)
1893         {
1894                 GL_ActiveTexture(unitnum);
1895                 if (unitnum < backendunits)
1896                 {
1897                         if (unit->trectangle)
1898                         {
1899                                 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
1900                         }
1901                 }
1902                 unit->trectangle = 0;
1903                 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
1904         }
1905 }
1906
1907 void R_Mesh_TexBind(unsigned int unitnum, int texnum)
1908 {
1909         gltextureunit_t *unit = gl_state.units + unitnum;
1910         if (unitnum >= backendimageunits)
1911                 return;
1912         // update 1d texture binding
1913         if (unit->t1d)
1914         {
1915                 GL_ActiveTexture(unitnum);
1916                 if (unitnum < backendunits)
1917                 {
1918                         if (unit->t1d)
1919                         {
1920                                 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1921                         }
1922                 }
1923                 unit->t1d = 0;
1924                 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1925         }
1926         // update 2d texture binding
1927         if (unit->t2d != texnum)
1928         {
1929                 GL_ActiveTexture(unitnum);
1930                 if (unitnum < backendunits)
1931                 {
1932                         if (texnum)
1933                         {
1934                                 if (unit->t2d == 0)
1935                                 {
1936                                         qglEnable(GL_TEXTURE_2D);CHECKGLERROR
1937                                 }
1938                         }
1939                         else
1940                         {
1941                                 if (unit->t2d)
1942                                 {
1943                                         qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1944                                 }
1945                         }
1946                 }
1947                 unit->t2d = texnum;
1948                 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1949         }
1950         // update 3d texture binding
1951         if (unit->t3d)
1952         {
1953                 GL_ActiveTexture(unitnum);
1954                 if (unitnum < backendunits)
1955                 {
1956                         if (unit->t3d)
1957                         {
1958                                 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1959                         }
1960                 }
1961                 unit->t3d = 0;
1962                 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1963         }
1964         // update cubemap texture binding
1965         if (unit->tcubemap != 0)
1966         {
1967                 GL_ActiveTexture(unitnum);
1968                 if (unitnum < backendunits)
1969                 {
1970                         if (unit->tcubemap)
1971                         {
1972                                 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1973                         }
1974                 }
1975                 unit->tcubemap = 0;
1976                 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1977         }
1978         // update rectangle texture binding
1979         if (unit->trectangle != 0)
1980         {
1981                 GL_ActiveTexture(unitnum);
1982                 if (unitnum < backendunits)
1983                 {
1984                         if (unit->trectangle)
1985                         {
1986                                 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
1987                         }
1988                 }
1989                 unit->trectangle = 0;
1990                 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
1991         }
1992 }
1993
1994 void R_Mesh_TexBind3D(unsigned int unitnum, int texnum)
1995 {
1996         gltextureunit_t *unit = gl_state.units + unitnum;
1997         if (unitnum >= backendimageunits)
1998                 return;
1999         // update 1d texture binding
2000         if (unit->t1d)
2001         {
2002                 GL_ActiveTexture(unitnum);
2003                 if (unitnum < backendunits)
2004                 {
2005                         if (unit->t1d)
2006                         {
2007                                 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
2008                         }
2009                 }
2010                 unit->t1d = 0;
2011                 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
2012         }
2013         // update 2d texture binding
2014         if (unit->t2d)
2015         {
2016                 GL_ActiveTexture(unitnum);
2017                 if (unitnum < backendunits)
2018                 {
2019                         if (unit->t2d)
2020                         {
2021                                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
2022                         }
2023                 }
2024                 unit->t2d = 0;
2025                 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
2026         }
2027         // update 3d texture binding
2028         if (unit->t3d != texnum)
2029         {
2030                 GL_ActiveTexture(unitnum);
2031                 if (unitnum < backendunits)
2032                 {
2033                         if (texnum)
2034                         {
2035                                 if (unit->t3d == 0)
2036                                 {
2037                                         qglEnable(GL_TEXTURE_3D);CHECKGLERROR
2038                                 }
2039                         }
2040                         else
2041                         {
2042                                 if (unit->t3d)
2043                                 {
2044                                         qglDisable(GL_TEXTURE_3D);CHECKGLERROR
2045                                 }
2046                         }
2047                 }
2048                 unit->t3d = texnum;
2049                 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
2050         }
2051         // update cubemap texture binding
2052         if (unit->tcubemap != 0)
2053         {
2054                 GL_ActiveTexture(unitnum);
2055                 if (unitnum < backendunits)
2056                 {
2057                         if (unit->tcubemap)
2058                         {
2059                                 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
2060                         }
2061                 }
2062                 unit->tcubemap = 0;
2063                 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
2064         }
2065         // update rectangle texture binding
2066         if (unit->trectangle != 0)
2067         {
2068                 GL_ActiveTexture(unitnum);
2069                 if (unitnum < backendunits)
2070                 {
2071                         if (unit->trectangle)
2072                         {
2073                                 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
2074                         }
2075                 }
2076                 unit->trectangle = 0;
2077                 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
2078         }
2079 }
2080
2081 void R_Mesh_TexBindCubeMap(unsigned int unitnum, int texnum)
2082 {
2083         gltextureunit_t *unit = gl_state.units + unitnum;
2084         if (unitnum >= backendimageunits)
2085                 return;
2086         // update 1d texture binding
2087         if (unit->t1d)
2088         {
2089                 GL_ActiveTexture(unitnum);
2090                 if (unitnum < backendunits)
2091                 {
2092                         if (unit->t1d)
2093                         {
2094                                 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
2095                         }
2096                 }
2097                 unit->t1d = 0;
2098                 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
2099         }
2100         // update 2d texture binding
2101         if (unit->t2d)
2102         {
2103                 GL_ActiveTexture(unitnum);
2104                 if (unitnum < backendunits)
2105                 {
2106                         if (unit->t2d)
2107                         {
2108                                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
2109                         }
2110                 }
2111                 unit->t2d = 0;
2112                 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
2113         }
2114         // update 3d texture binding
2115         if (unit->t3d)
2116         {
2117                 GL_ActiveTexture(unitnum);
2118                 if (unitnum < backendunits)
2119                 {
2120                         if (unit->t3d)
2121                         {
2122                                 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
2123                         }
2124                 }
2125                 unit->t3d = 0;
2126                 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
2127         }
2128         // update cubemap texture binding
2129         if (unit->tcubemap != texnum)
2130         {
2131                 GL_ActiveTexture(unitnum);
2132                 if (unitnum < backendunits)
2133                 {
2134                         if (texnum)
2135                         {
2136                                 if (unit->tcubemap == 0)
2137                                 {
2138                                         qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
2139                                 }
2140                         }
2141                         else
2142                         {
2143                                 if (unit->tcubemap)
2144                                 {
2145                                         qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
2146                                 }
2147                         }
2148                 }
2149                 unit->tcubemap = texnum;
2150                 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
2151         }
2152         // update rectangle texture binding
2153         if (unit->trectangle != 0)
2154         {
2155                 GL_ActiveTexture(unitnum);
2156                 if (unitnum < backendunits)
2157                 {
2158                         if (unit->trectangle)
2159                         {
2160                                 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
2161                         }
2162                 }
2163                 unit->trectangle = 0;
2164                 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
2165         }
2166 }
2167
2168 void R_Mesh_TexBindRectangle(unsigned int unitnum, int texnum)
2169 {
2170         gltextureunit_t *unit = gl_state.units + unitnum;
2171         if (unitnum >= backendimageunits)
2172                 return;
2173         // update 1d texture binding
2174         if (unit->t1d)
2175         {
2176                 GL_ActiveTexture(unitnum);
2177                 if (unitnum < backendunits)
2178                 {
2179                         if (unit->t1d)
2180                         {
2181                                 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
2182                         }
2183                 }
2184                 unit->t1d = 0;
2185                 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
2186         }
2187         // update 2d texture binding
2188         if (unit->t2d)
2189         {
2190                 GL_ActiveTexture(unitnum);
2191                 if (unitnum < backendunits)
2192                 {
2193                         if (unit->t2d)
2194                         {
2195                                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
2196                         }
2197                 }
2198                 unit->t2d = 0;
2199                 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
2200         }
2201         // update 3d texture binding
2202         if (unit->t3d)
2203         {
2204                 GL_ActiveTexture(unitnum);
2205                 if (unitnum < backendunits)
2206                 {
2207                         if (unit->t3d)
2208                         {
2209                                 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
2210                         }
2211                 }
2212                 unit->t3d = 0;
2213                 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
2214         }
2215         // update cubemap texture binding
2216         if (unit->tcubemap != 0)
2217         {
2218                 GL_ActiveTexture(unitnum);
2219                 if (unitnum < backendunits)
2220                 {
2221                         if (unit->tcubemap)
2222                         {
2223                                 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
2224                         }
2225                 }
2226                 unit->tcubemap = 0;
2227                 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
2228         }
2229         // update rectangle texture binding
2230         if (unit->trectangle != texnum)
2231         {
2232                 GL_ActiveTexture(unitnum);
2233                 if (unitnum < backendunits)
2234                 {
2235                         if (texnum)
2236                         {
2237                                 if (unit->trectangle == 0)
2238                                 {
2239                                         qglEnable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
2240                                 }
2241                         }
2242                         else
2243                         {
2244                                 if (unit->trectangle)
2245                                 {
2246                                         qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
2247                                 }
2248                         }
2249                 }
2250                 unit->trectangle = texnum;
2251                 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
2252         }
2253 }
2254
2255 static const double gl_identitymatrix[16] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
2256
2257 void R_Mesh_TexMatrix(unsigned int unitnum, const matrix4x4_t *matrix)
2258 {
2259         gltextureunit_t *unit = gl_state.units + unitnum;
2260         if (matrix->m[3][3])
2261         {
2262                 // texmatrix specified, check if it is different
2263                 if (!unit->texmatrixenabled || memcmp(&unit->matrix, matrix, sizeof(matrix4x4_t)))
2264                 {
2265                         double glmatrix[16];
2266                         unit->texmatrixenabled = true;
2267                         unit->matrix = *matrix;
2268                         CHECKGLERROR
2269                         Matrix4x4_ToArrayDoubleGL(&unit->matrix, glmatrix);
2270                         GL_ActiveTexture(unitnum);
2271                         qglMatrixMode(GL_TEXTURE);CHECKGLERROR
2272                         qglLoadMatrixd(glmatrix);CHECKGLERROR
2273                         qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
2274                 }
2275         }
2276         else
2277         {
2278                 // no texmatrix specified, revert to identity
2279                 if (unit->texmatrixenabled)
2280                 {
2281                         unit->texmatrixenabled = false;
2282                         unit->matrix = identitymatrix;
2283                         CHECKGLERROR
2284                         GL_ActiveTexture(unitnum);
2285                         qglMatrixMode(GL_TEXTURE);CHECKGLERROR
2286                         qglLoadIdentity();CHECKGLERROR
2287                         qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
2288                 }
2289         }
2290 }
2291
2292 void R_Mesh_TexCombine(unsigned int unitnum, int combinergb, int combinealpha, int rgbscale, int alphascale)
2293 {
2294         gltextureunit_t *unit = gl_state.units + unitnum;
2295         CHECKGLERROR
2296         if (gl_combine.integer)
2297         {
2298                 // GL_ARB_texture_env_combine
2299                 if (!combinergb)
2300                         combinergb = GL_MODULATE;
2301                 if (!combinealpha)
2302                         combinealpha = GL_MODULATE;
2303                 if (!rgbscale)
2304                         rgbscale = 1;
2305                 if (!alphascale)
2306                         alphascale = 1;
2307                 if (unit->combinergb != combinergb)
2308                 {
2309                         unit->combinergb = combinergb;
2310                         GL_ActiveTexture(unitnum);
2311                         qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
2312                 }
2313                 if (unit->combinealpha != combinealpha)
2314                 {
2315                         unit->combinealpha = combinealpha;
2316                         GL_ActiveTexture(unitnum);
2317                         qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
2318                 }
2319                 if (unit->rgbscale != rgbscale)
2320                 {
2321                         GL_ActiveTexture(unitnum);
2322                         qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (unit->rgbscale = rgbscale));CHECKGLERROR
2323                 }
2324                 if (unit->alphascale != alphascale)
2325                 {
2326                         GL_ActiveTexture(unitnum);
2327                         qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, (unit->alphascale = alphascale));CHECKGLERROR
2328                 }
2329         }
2330         else
2331         {
2332                 // normal GL texenv
2333                 if (!combinergb)
2334                         combinergb = GL_MODULATE;
2335                 if (unit->combinergb != combinergb)
2336                 {
2337                         unit->combinergb = combinergb;
2338                         GL_ActiveTexture(unitnum);
2339                         qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combinergb);CHECKGLERROR
2340                 }
2341         }
2342 }
2343
2344 void R_Mesh_TextureState(const rmeshstate_t *m)
2345 {
2346         unsigned int i;
2347
2348         BACKENDACTIVECHECK
2349
2350         CHECKGLERROR
2351         if (gl_backend_rebindtextures)
2352         {
2353                 gl_backend_rebindtextures = false;
2354                 GL_SetupTextureState();
2355                 CHECKGLERROR
2356         }
2357
2358         for (i = 0;i < backendimageunits;i++)
2359                 R_Mesh_TexBindAll(i, m->tex1d[i], m->tex[i], m->tex3d[i], m->texcubemap[i], m->texrectangle[i]);
2360         for (i = 0;i < backendarrayunits;i++)
2361         {
2362                 if (m->pointer_texcoord3f[i])
2363                         R_Mesh_TexCoordPointer(i, 3, m->pointer_texcoord3f[i], m->pointer_texcoord_bufferobject[i], m->pointer_texcoord_bufferoffset[i]);
2364                 else
2365                         R_Mesh_TexCoordPointer(i, 2, m->pointer_texcoord[i], m->pointer_texcoord_bufferobject[i], m->pointer_texcoord_bufferoffset[i]);
2366         }
2367         for (i = 0;i < backendunits;i++)
2368         {
2369                 R_Mesh_TexMatrix(i, &m->texmatrix[i]);
2370                 R_Mesh_TexCombine(i, m->texcombinergb[i], m->texcombinealpha[i], m->texrgbscale[i], m->texalphascale[i]);
2371         }
2372         CHECKGLERROR
2373 }
2374
2375 void R_Mesh_ResetTextureState(void)
2376 {
2377         unsigned int unitnum;
2378
2379         BACKENDACTIVECHECK
2380
2381         CHECKGLERROR
2382         if (gl_backend_rebindtextures)
2383         {
2384                 gl_backend_rebindtextures = false;
2385                 GL_SetupTextureState();
2386                 CHECKGLERROR
2387         }
2388
2389         for (unitnum = 0;unitnum < backendimageunits;unitnum++)
2390         {
2391                 gltextureunit_t *unit = gl_state.units + unitnum;
2392                 // update 1d texture binding
2393                 if (unit->t1d)
2394                 {
2395                         GL_ActiveTexture(unitnum);
2396                         if (unitnum < backendunits)
2397                         {
2398                                 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
2399                         }
2400                         unit->t1d = 0;
2401                         qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
2402                 }
2403                 // update 2d texture binding
2404                 if (unit->t2d)
2405                 {
2406                         GL_ActiveTexture(unitnum);
2407                         if (unitnum < backendunits)
2408                         {
2409                                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
2410                         }
2411                         unit->t2d = 0;
2412                         qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
2413                 }
2414                 // update 3d texture binding
2415                 if (unit->t3d)
2416                 {
2417                         GL_ActiveTexture(unitnum);
2418                         if (unitnum < backendunits)
2419                         {
2420                                 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
2421                         }
2422                         unit->t3d = 0;
2423                         qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
2424                 }
2425                 // update cubemap texture binding
2426                 if (unit->tcubemap)
2427                 {
2428                         GL_ActiveTexture(unitnum);
2429                         if (unitnum < backendunits)
2430                         {
2431                                 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
2432                         }
2433                         unit->tcubemap = 0;
2434                         qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
2435                 }
2436                 // update rectangle texture binding
2437                 if (unit->trectangle)
2438                 {
2439                         GL_ActiveTexture(unitnum);
2440                         if (unitnum < backendunits)
2441                         {
2442                                 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
2443                         }
2444                         unit->trectangle = 0;
2445                         qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
2446                 }
2447         }
2448         for (unitnum = 0;unitnum < backendarrayunits;unitnum++)
2449         {
2450                 gltextureunit_t *unit = gl_state.units + unitnum;
2451                 // texture array unit is disabled, disable the array
2452                 if (unit->arrayenabled)
2453                 {
2454                         unit->arrayenabled = false;
2455                         GL_ClientActiveTexture(unitnum);
2456                         qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
2457                 }
2458         }
2459         for (unitnum = 0;unitnum < backendunits;unitnum++)
2460         {
2461                 gltextureunit_t *unit = gl_state.units + unitnum;
2462                 // no texmatrix specified, revert to identity
2463                 if (unit->texmatrixenabled)
2464                 {
2465                         unit->texmatrixenabled = false;
2466                         unit->matrix = identitymatrix;
2467                         CHECKGLERROR
2468                         GL_ActiveTexture(unitnum);
2469                         qglMatrixMode(GL_TEXTURE);CHECKGLERROR
2470                         qglLoadIdentity();CHECKGLERROR
2471                         qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
2472                 }
2473                 if (gl_combine.integer)
2474                 {
2475                         // GL_ARB_texture_env_combine
2476                         if (unit->combinergb != GL_MODULATE)
2477                         {
2478                                 unit->combinergb = GL_MODULATE;
2479                                 GL_ActiveTexture(unitnum);
2480                                 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
2481                         }
2482                         if (unit->combinealpha != GL_MODULATE)
2483                         {
2484                                 unit->combinealpha = GL_MODULATE;
2485                                 GL_ActiveTexture(unitnum);
2486                                 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
2487                         }
2488                         if (unit->rgbscale != 1)
2489                         {
2490                                 GL_ActiveTexture(unitnum);
2491                                 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (unit->rgbscale = 1));CHECKGLERROR
2492                         }
2493                         if (unit->alphascale != 1)
2494                         {
2495                                 GL_ActiveTexture(unitnum);
2496                                 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, (unit->alphascale = 1));CHECKGLERROR
2497                         }
2498                 }
2499                 else
2500                 {
2501                         // normal GL texenv
2502                         if (unit->combinergb != GL_MODULATE)
2503                         {
2504                                 unit->combinergb = GL_MODULATE;
2505                                 GL_ActiveTexture(unitnum);
2506                                 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combinergb);CHECKGLERROR
2507                         }
2508                 }
2509         }
2510 }