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