]> git.xonotic.org Git - xonotic/darkplaces.git/blob - gl_backend.c
fix a bunch of warnings
[xonotic/darkplaces.git] / gl_backend.c
1
2 #include "quakedef.h"
3 #include "cl_collision.h"
4
5 cvar_t gl_mesh_drawrangeelements = {0, "gl_mesh_drawrangeelements", "1", "use glDrawRangeElements function if available instead of glDrawElements (for performance comparisons or bug testing)"};
6 cvar_t gl_mesh_testarrayelement = {0, "gl_mesh_testarrayelement", "0", "use glBegin(GL_TRIANGLES);glArrayElement();glEnd(); primitives instead of glDrawElements (useful to test for driver bugs with glDrawElements)"};
7 cvar_t gl_mesh_testmanualfeeding = {0, "gl_mesh_testmanualfeeding", "0", "use glBegin(GL_TRIANGLES);glTexCoord2f();glVertex3f();glEnd(); primitives instead of glDrawElements (useful to test for driver bugs with glDrawElements)"};
8 cvar_t gl_mesh_prefer_short_elements = {0, "gl_mesh_prefer_short_elements", "1", "use GL_UNSIGNED_SHORT element arrays instead of GL_UNSIGNED_INT"};
9 cvar_t gl_paranoid = {0, "gl_paranoid", "0", "enables OpenGL error checking and other tests"};
10 cvar_t gl_printcheckerror = {0, "gl_printcheckerror", "0", "prints all OpenGL error checks, useful to identify location of driver crashes"};
11
12 cvar_t r_render = {0, "r_render", "1", "enables rendering 3D views (you want this on!)"};
13 cvar_t r_renderview = {0, "r_renderview", "1", "enables rendering 3D views (you want this on!)"};
14 cvar_t r_waterwarp = {CVAR_SAVE, "r_waterwarp", "1", "warp view while underwater"};
15 cvar_t gl_polyblend = {CVAR_SAVE, "gl_polyblend", "1", "tints view while underwater, hurt, etc"};
16 cvar_t gl_dither = {CVAR_SAVE, "gl_dither", "1", "enables OpenGL dithering (16bit looks bad with this off)"};
17 cvar_t gl_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)"};
18 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"};
19
20 cvar_t v_flipped = {0, "v_flipped", "0", "mirror the screen (poor man's left handed mode)"};
21 qboolean v_flipped_state = false;
22
23 r_viewport_t gl_viewport;
24 matrix4x4_t gl_modelmatrix;
25 matrix4x4_t gl_viewmatrix;
26 matrix4x4_t gl_modelviewmatrix;
27 matrix4x4_t gl_projectionmatrix;
28 matrix4x4_t gl_modelviewprojectionmatrix;
29 float gl_modelview16f[16];
30 float gl_modelviewprojection16f[16];
31 qboolean gl_modelmatrixchanged;
32
33 int gl_maxdrawrangeelementsvertices;
34 int gl_maxdrawrangeelementsindices;
35
36 #ifdef DEBUGGL
37 int errornumber = 0;
38
39 void GL_PrintError(int errornumber, char *filename, int linenumber)
40 {
41         switch(errornumber)
42         {
43 #ifdef GL_INVALID_ENUM
44         case GL_INVALID_ENUM:
45                 Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
46                 break;
47 #endif
48 #ifdef GL_INVALID_VALUE
49         case GL_INVALID_VALUE:
50                 Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
51                 break;
52 #endif
53 #ifdef GL_INVALID_OPERATION
54         case GL_INVALID_OPERATION:
55                 Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
56                 break;
57 #endif
58 #ifdef GL_STACK_OVERFLOW
59         case GL_STACK_OVERFLOW:
60                 Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
61                 break;
62 #endif
63 #ifdef GL_STACK_UNDERFLOW
64         case GL_STACK_UNDERFLOW:
65                 Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
66                 break;
67 #endif
68 #ifdef GL_OUT_OF_MEMORY
69         case GL_OUT_OF_MEMORY:
70                 Con_Printf("GL_OUT_OF_MEMORY at %s:%i\n", filename, linenumber);
71                 break;
72 #endif
73 #ifdef GL_TABLE_TOO_LARGE
74         case GL_TABLE_TOO_LARGE:
75                 Con_Printf("GL_TABLE_TOO_LARGE at %s:%i\n", filename, linenumber);
76                 break;
77 #endif
78 #ifdef GL_INVALID_FRAMEBUFFER_OPERATION_EXT
79         case GL_INVALID_FRAMEBUFFER_OPERATION_EXT:
80                 Con_Printf("GL_INVALID_FRAMEBUFFER_OPERATION at %s:%i\n", filename, linenumber);
81                 break;
82 #endif
83         default:
84                 Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
85                 break;
86         }
87 }
88 #endif
89
90 #define BACKENDACTIVECHECK if (!gl_state.active) Sys_Error("GL backend function called when backend is not active");
91
92 void SCR_ScreenShot_f (void);
93
94 typedef struct gl_bufferobjectinfo_s
95 {
96         int target;
97         int object;
98         size_t size;
99         char name[MAX_QPATH];
100 }
101 gl_bufferobjectinfo_t;
102
103 typedef struct gltextureunit_s
104 {
105         const void *pointer_texcoord;
106         size_t pointer_texcoord_offset;
107         int pointer_texcoord_buffer;
108         int t2d, t3d, tcubemap, trectangle;
109         int arrayenabled;
110         unsigned int arraycomponents;
111         int rgbscale, alphascale;
112         int combine;
113         int combinergb, combinealpha;
114         // texmatrixenabled exists only to avoid unnecessary texmatrix compares
115         int texmatrixenabled;
116         matrix4x4_t matrix;
117 }
118 gltextureunit_t;
119
120 typedef struct gl_state_s
121 {
122         int cullface;
123         int cullfaceenable;
124         int blendfunc1;
125         int blendfunc2;
126         int blend;
127         GLboolean depthmask;
128         int colormask; // stored as bottom 4 bits: r g b a (3 2 1 0 order)
129         int depthtest;
130         float depthrange[2];
131         float polygonoffset[2];
132         int alphatest;
133         int scissortest;
134         unsigned int unit;
135         unsigned int clientunit;
136         gltextureunit_t units[MAX_TEXTUREUNITS];
137         float color4f[4];
138         int lockrange_first;
139         int lockrange_count;
140         int vertexbufferobject;
141         int elementbufferobject;
142         qboolean pointer_color_enabled;
143         const void *pointer_vertex;
144         const void *pointer_color;
145         size_t pointer_vertex_offset;
146         size_t pointer_color_offset;
147         int pointer_vertex_buffer;
148         int pointer_color_buffer;
149
150         memexpandablearray_t bufferobjectinfoarray;
151
152         qboolean active;
153 }
154 gl_state_t;
155
156 static gl_state_t gl_state;
157
158
159 /*
160 note: here's strip order for a terrain row:
161 0--1--2--3--4
162 |\ |\ |\ |\ |
163 | \| \| \| \|
164 A--B--C--D--E
165 clockwise
166
167 A0B, 01B, B1C, 12C, C2D, 23D, D3E, 34E
168
169 *elements++ = i + row;
170 *elements++ = i;
171 *elements++ = i + row + 1;
172 *elements++ = i;
173 *elements++ = i + 1;
174 *elements++ = i + row + 1;
175
176
177 for (y = 0;y < rows - 1;y++)
178 {
179         for (x = 0;x < columns - 1;x++)
180         {
181                 i = y * rows + x;
182                 *elements++ = i + columns;
183                 *elements++ = i;
184                 *elements++ = i + columns + 1;
185                 *elements++ = i;
186                 *elements++ = i + 1;
187                 *elements++ = i + columns + 1;
188         }
189 }
190
191 alternative:
192 0--1--2--3--4
193 | /| /|\ | /|
194 |/ |/ | \|/ |
195 A--B--C--D--E
196 counterclockwise
197
198 for (y = 0;y < rows - 1;y++)
199 {
200         for (x = 0;x < columns - 1;x++)
201         {
202                 i = y * rows + x;
203                 *elements++ = i;
204                 *elements++ = i + columns;
205                 *elements++ = i + columns + 1;
206                 *elements++ = i + columns;
207                 *elements++ = i + columns + 1;
208                 *elements++ = i + 1;
209         }
210 }
211 */
212
213 int polygonelement3i[(POLYGONELEMENTS_MAXPOINTS-2)*3];
214 unsigned short polygonelement3s[(POLYGONELEMENTS_MAXPOINTS-2)*3];
215 int quadelement3i[QUADELEMENTS_MAXQUADS*6];
216 unsigned short quadelement3s[QUADELEMENTS_MAXQUADS*6];
217
218 void GL_VBOStats_f(void)
219 {
220         GL_Mesh_ListVBOs(true);
221 }
222
223 static void GL_Backend_ResetState(void);
224
225 static void gl_backend_start(void)
226 {
227         memset(&gl_state, 0, sizeof(gl_state));
228
229         Mem_ExpandableArray_NewArray(&gl_state.bufferobjectinfoarray, r_main_mempool, sizeof(gl_bufferobjectinfo_t), 128);
230
231         Con_DPrintf("OpenGL backend started.\n");
232
233         CHECKGLERROR
234
235         GL_Backend_ResetState();
236 }
237
238 static void gl_backend_shutdown(void)
239 {
240         Con_DPrint("OpenGL Backend shutting down\n");
241
242         Mem_ExpandableArray_FreeArray(&gl_state.bufferobjectinfoarray);
243
244         memset(&gl_state, 0, sizeof(gl_state));
245 }
246
247 static void gl_backend_newmap(void)
248 {
249 }
250
251 void gl_backend_init(void)
252 {
253         int i;
254
255         for (i = 0;i < POLYGONELEMENTS_MAXPOINTS - 2;i++)
256         {
257                 polygonelement3s[i * 3 + 0] = 0;
258                 polygonelement3s[i * 3 + 1] = i + 1;
259                 polygonelement3s[i * 3 + 2] = i + 2;
260         }
261         // elements for rendering a series of quads as triangles
262         for (i = 0;i < QUADELEMENTS_MAXQUADS;i++)
263         {
264                 quadelement3s[i * 6 + 0] = i * 4;
265                 quadelement3s[i * 6 + 1] = i * 4 + 1;
266                 quadelement3s[i * 6 + 2] = i * 4 + 2;
267                 quadelement3s[i * 6 + 3] = i * 4;
268                 quadelement3s[i * 6 + 4] = i * 4 + 2;
269                 quadelement3s[i * 6 + 5] = i * 4 + 3;
270         }
271
272         for (i = 0;i < (POLYGONELEMENTS_MAXPOINTS - 2)*3;i++)
273                 polygonelement3i[i] = polygonelement3s[i];
274         for (i = 0;i < QUADELEMENTS_MAXQUADS*3;i++)
275                 quadelement3i[i] = quadelement3s[i];
276
277         Cvar_RegisterVariable(&r_render);
278         Cvar_RegisterVariable(&r_renderview);
279         Cvar_RegisterVariable(&r_waterwarp);
280         Cvar_RegisterVariable(&gl_polyblend);
281         Cvar_RegisterVariable(&v_flipped);
282         Cvar_RegisterVariable(&gl_dither);
283         Cvar_RegisterVariable(&gl_vbo);
284         Cvar_RegisterVariable(&gl_paranoid);
285         Cvar_RegisterVariable(&gl_printcheckerror);
286
287         Cvar_RegisterVariable(&gl_mesh_drawrangeelements);
288         Cvar_RegisterVariable(&gl_mesh_testarrayelement);
289         Cvar_RegisterVariable(&gl_mesh_testmanualfeeding);
290         Cvar_RegisterVariable(&gl_mesh_prefer_short_elements);
291
292         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");
293
294         R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap);
295 }
296
297 void GL_SetMirrorState(qboolean state);
298
299 void R_Viewport_TransformToScreen(const r_viewport_t *v, const vec4_t in, vec4_t out)
300 {
301         vec4_t temp;
302         float iw;
303         Matrix4x4_Transform4 (&v->viewmatrix, in, temp);
304         Matrix4x4_Transform4 (&v->projectmatrix, temp, out);
305         iw = 1.0f / out[3];
306         out[0] = v->x + (out[0] * iw + 1.0f) * v->width * 0.5f;
307         out[1] = v->y + v->height - (out[1] * iw + 1.0f) * v->height * 0.5f;
308         out[2] = v->z + (out[2] * iw + 1.0f) * v->depth * 0.5f;
309 }
310
311 static void R_Viewport_ApplyNearClipPlaneFloatGL(const r_viewport_t *v, float *m, float normalx, float normaly, float normalz, float dist)
312 {
313         float q[4];
314         float d;
315         float clipPlane[4], v3[3], v4[3];
316         float normal[3];
317
318         // This is inspired by Oblique Depth Projection from http://www.terathon.com/code/oblique.php
319
320         VectorSet(normal, normalx, normaly, normalz);
321         Matrix4x4_Transform3x3(&v->viewmatrix, normal, clipPlane);
322         VectorScale(normal, dist, v3);
323         Matrix4x4_Transform(&v->viewmatrix, v3, v4);
324         // FIXME: LordHavoc: I think this can be done more efficiently somehow but I can't remember the technique
325         clipPlane[3] = -DotProduct(v4, clipPlane);
326
327 #if 0
328 {
329         // testing code for comparing results
330         float clipPlane2[4];
331         VectorCopy4(clipPlane, clipPlane2);
332         R_EntityMatrix(&identitymatrix);
333         VectorSet(q, normal[0], normal[1], normal[2], -dist);
334         qglClipPlane(GL_CLIP_PLANE0, q);
335         qglGetClipPlane(GL_CLIP_PLANE0, q);
336         VectorCopy4(q, clipPlane);
337 }
338 #endif
339
340         // Calculate the clip-space corner point opposite the clipping plane
341         // as (sgn(clipPlane.x), sgn(clipPlane.y), 1, 1) and
342         // transform it into camera space by multiplying it
343         // by the inverse of the projection matrix
344         q[0] = ((clipPlane[0] < 0.0f ? -1.0f : clipPlane[0] > 0.0f ? 1.0f : 0.0f) + m[8]) / m[0];
345         q[1] = ((clipPlane[1] < 0.0f ? -1.0f : clipPlane[1] > 0.0f ? 1.0f : 0.0f) + m[9]) / m[5];
346         q[2] = -1.0f;
347         q[3] = (1.0f + m[10]) / m[14];
348
349         // Calculate the scaled plane vector
350         d = 2.0f / DotProduct4(clipPlane, q);
351
352         // Replace the third row of the projection matrix
353         m[2] = clipPlane[0] * d;
354         m[6] = clipPlane[1] * d;
355         m[10] = clipPlane[2] * d + 1.0f;
356         m[14] = clipPlane[3] * d;
357 }
358
359 void R_Viewport_InitOrtho(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, float x1, float y1, float x2, float y2, float nearclip, float farclip, const float *nearplane)
360 {
361         float left = x1, right = x2, bottom = y2, top = y1, zNear = nearclip, zFar = farclip;
362         float m[16];
363         memset(v, 0, sizeof(*v));
364         v->type = R_VIEWPORTTYPE_ORTHO;
365         v->cameramatrix = *cameramatrix;
366         v->x = x;
367         v->y = y;
368         v->z = 0;
369         v->width = width;
370         v->height = height;
371         v->depth = 1;
372         memset(m, 0, sizeof(m));
373         m[0]  = 2/(right - left);
374         m[5]  = 2/(top - bottom);
375         m[10] = -2/(zFar - zNear);
376         m[12] = - (right + left)/(right - left);
377         m[13] = - (top + bottom)/(top - bottom);
378         m[14] = - (zFar + zNear)/(zFar - zNear);
379         m[15] = 1;
380         v->screentodepth[0] = -farclip / (farclip - nearclip);
381         v->screentodepth[1] = farclip * nearclip / (farclip - nearclip);
382
383         Matrix4x4_Invert_Full(&v->viewmatrix, &v->cameramatrix);
384
385         if (nearplane)
386                 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
387
388         Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
389
390 #if 0
391         {
392                 vec4_t test1;
393                 vec4_t test2;
394                 Vector4Set(test1, (x1+x2)*0.5f, (y1+y2)*0.5f, 0.0f, 1.0f);
395                 R_Viewport_TransformToScreen(v, test1, test2);
396                 Con_Printf("%f %f %f -> %f %f %f\n", test1[0], test1[1], test1[2], test2[0], test2[1], test2[2]);
397         }
398 #endif
399 }
400
401 void R_Viewport_InitPerspective(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, float frustumx, float frustumy, float nearclip, float farclip, const float *nearplane)
402 {
403         matrix4x4_t tempmatrix, basematrix;
404         float m[16];
405         memset(v, 0, sizeof(*v));
406
407         if(v_flipped.integer)
408                 frustumx = -frustumx;
409
410         v->type = R_VIEWPORTTYPE_PERSPECTIVE;
411         v->cameramatrix = *cameramatrix;
412         v->x = x;
413         v->y = y;
414         v->z = 0;
415         v->width = width;
416         v->height = height;
417         v->depth = 1;
418         memset(m, 0, sizeof(m));
419         m[0]  = 1.0 / frustumx;
420         m[5]  = 1.0 / frustumy;
421         m[10] = -(farclip + nearclip) / (farclip - nearclip);
422         m[11] = -1;
423         m[14] = -2 * nearclip * farclip / (farclip - nearclip);
424         v->screentodepth[0] = -farclip / (farclip - nearclip);
425         v->screentodepth[1] = farclip * nearclip / (farclip - nearclip);
426
427         Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
428         Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
429         Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
430         Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
431
432         if (nearplane)
433                 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
434
435         Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
436 }
437
438 void R_Viewport_InitPerspectiveInfinite(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, float frustumx, float frustumy, float nearclip, const float *nearplane)
439 {
440         matrix4x4_t tempmatrix, basematrix;
441         const float nudge = 1.0 - 1.0 / (1<<23);
442         float m[16];
443         memset(v, 0, sizeof(*v));
444
445         if(v_flipped.integer)
446                 frustumx = -frustumx;
447
448         v->type = R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP;
449         v->cameramatrix = *cameramatrix;
450         v->x = x;
451         v->y = y;
452         v->z = 0;
453         v->width = width;
454         v->height = height;
455         v->depth = 1;
456         memset(m, 0, sizeof(m));
457         m[ 0] = 1.0 / frustumx;
458         m[ 5] = 1.0 / frustumy;
459         m[10] = -nudge;
460         m[11] = -1;
461         m[14] = -2 * nearclip * nudge;
462         v->screentodepth[0] = (m[10] + 1) * 0.5 - 1;
463         v->screentodepth[1] = m[14] * -0.5;
464
465         Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
466         Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
467         Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
468         Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
469
470         if (nearplane)
471                 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
472
473         Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
474 }
475
476 float cubeviewmatrix[6][16] =
477 {
478     // standard cubemap projections
479     { // +X
480          0, 0,-1, 0,
481          0,-1, 0, 0,
482         -1, 0, 0, 0,
483          0, 0, 0, 1,
484     },
485     { // -X
486          0, 0, 1, 0,
487          0,-1, 0, 0,
488          1, 0, 0, 0,
489          0, 0, 0, 1,
490     },
491     { // +Y
492          1, 0, 0, 0,
493          0, 0,-1, 0,
494          0, 1, 0, 0,
495          0, 0, 0, 1,
496     },
497     { // -Y
498          1, 0, 0, 0,
499          0, 0, 1, 0,
500          0,-1, 0, 0,
501          0, 0, 0, 1,
502     },
503     { // +Z
504          1, 0, 0, 0,
505          0,-1, 0, 0,
506          0, 0,-1, 0,
507          0, 0, 0, 1,
508     },
509     { // -Z
510         -1, 0, 0, 0,
511          0,-1, 0, 0,
512          0, 0, 1, 0,
513          0, 0, 0, 1,
514     },
515 };
516 float rectviewmatrix[6][16] =
517 {
518     // sign-preserving cubemap projections
519     { // +X
520          0, 0,-1, 0,
521          0, 1, 0, 0,
522          1, 0, 0, 0,
523          0, 0, 0, 1,
524     },
525     { // -X
526          0, 0, 1, 0,
527          0, 1, 0, 0,
528          1, 0, 0, 0,
529          0, 0, 0, 1,
530     },
531     { // +Y
532          1, 0, 0, 0,
533          0, 0,-1, 0,
534          0, 1, 0, 0,
535          0, 0, 0, 1,
536     },
537     { // -Y
538          1, 0, 0, 0,
539          0, 0, 1, 0,
540          0, 1, 0, 0,
541          0, 0, 0, 1,
542     },
543     { // +Z
544          1, 0, 0, 0,
545          0, 1, 0, 0,
546          0, 0,-1, 0,
547          0, 0, 0, 1,
548     },
549     { // -Z
550          1, 0, 0, 0,
551          0, 1, 0, 0,
552          0, 0, 1, 0,
553          0, 0, 0, 1,
554     },
555 };
556
557 void R_Viewport_InitCubeSideView(r_viewport_t *v, const matrix4x4_t *cameramatrix, int side, int size, float nearclip, float farclip, const float *nearplane)
558 {
559         matrix4x4_t tempmatrix, basematrix;
560         float m[16];
561         memset(v, 0, sizeof(*v));
562         v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
563         v->cameramatrix = *cameramatrix;
564         v->width = size;
565         v->height = size;
566         v->depth = 1;
567         memset(m, 0, sizeof(m));
568         m[0] = m[5] = 1.0f;
569         m[10] = -(farclip + nearclip) / (farclip - nearclip);
570         m[11] = -1;
571         m[14] = -2 * nearclip * farclip / (farclip - nearclip);
572
573         Matrix4x4_FromArrayFloatGL(&basematrix, cubeviewmatrix[side]);
574         Matrix4x4_Invert_Simple(&tempmatrix, &v->cameramatrix);
575         Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
576
577         if (nearplane)
578                 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
579
580         Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
581 }
582
583 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)
584 {
585         matrix4x4_t tempmatrix, basematrix;
586         float m[16];
587         memset(v, 0, sizeof(*v));
588         v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
589         v->cameramatrix = *cameramatrix;
590         v->x = (side & 1) * size;
591         v->y = (side >> 1) * size;
592         v->width = size;
593         v->height = size;
594         v->depth = 1;
595         memset(m, 0, sizeof(m));
596         m[0] = m[5] = 1.0f * ((float)size - border) / size;
597         m[10] = -(farclip + nearclip) / (farclip - nearclip);
598         m[11] = -1;
599         m[14] = -2 * nearclip * farclip / (farclip - nearclip);
600
601         Matrix4x4_FromArrayFloatGL(&basematrix, rectviewmatrix[side]);
602         Matrix4x4_Invert_Simple(&tempmatrix, &v->cameramatrix);
603         Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
604
605         if (nearplane)
606                 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
607
608         Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
609 }
610
611 void R_SetViewport(const r_viewport_t *v)
612 {
613         float m[16];
614         gl_viewport = *v;
615
616         CHECKGLERROR
617         qglViewport(v->x, v->y, v->width, v->height);CHECKGLERROR
618
619         // FIXME: v_flipped_state is evil, this probably breaks somewhere
620         GL_SetMirrorState(v_flipped.integer && (v->type == R_VIEWPORTTYPE_PERSPECTIVE || v->type == R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP));
621
622         // copy over the matrices to our state
623         gl_viewmatrix = v->viewmatrix;
624         gl_projectionmatrix = v->projectmatrix;
625
626         switch(vid.renderpath)
627         {
628         case RENDERPATH_GL20:
629         case RENDERPATH_CGGL:
630 //              break;
631         case RENDERPATH_GL13:
632         case RENDERPATH_GL11:
633                 // Load the projection matrix into OpenGL
634                 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
635                 Matrix4x4_ToArrayFloatGL(&gl_projectionmatrix, m);
636                 qglLoadMatrixf(m);CHECKGLERROR
637                 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
638                 break;
639         }
640
641         // force an update of the derived matrices
642         gl_modelmatrixchanged = true;
643         R_EntityMatrix(&gl_modelmatrix);
644 }
645
646 void R_GetViewport(r_viewport_t *v)
647 {
648         *v = gl_viewport;
649 }
650
651 static void GL_BindVBO(int bufferobject)
652 {
653         if (gl_state.vertexbufferobject != bufferobject)
654         {
655                 gl_state.vertexbufferobject = bufferobject;
656                 CHECKGLERROR
657                 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, bufferobject);
658                 CHECKGLERROR
659         }
660 }
661
662 static void GL_BindEBO(int bufferobject)
663 {
664         if (gl_state.elementbufferobject != bufferobject)
665         {
666                 gl_state.elementbufferobject = bufferobject;
667                 CHECKGLERROR
668                 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, bufferobject);
669                 CHECKGLERROR
670         }
671 }
672
673 static void GL_Backend_ResetState(void)
674 {
675         unsigned int i;
676         gl_state.active = true;
677         gl_state.depthtest = true;
678         gl_state.alphatest = false;
679         gl_state.blendfunc1 = GL_ONE;
680         gl_state.blendfunc2 = GL_ZERO;
681         gl_state.blend = false;
682         gl_state.depthmask = GL_TRUE;
683         gl_state.colormask = 15;
684         gl_state.color4f[0] = gl_state.color4f[1] = gl_state.color4f[2] = gl_state.color4f[3] = 1;
685         gl_state.lockrange_first = 0;
686         gl_state.lockrange_count = 0;
687         gl_state.cullface = v_flipped_state ? GL_BACK : GL_FRONT; // quake is backwards, this culls back faces
688         gl_state.cullfaceenable = true;
689         gl_state.polygonoffset[0] = 0;
690         gl_state.polygonoffset[1] = 0;
691
692         CHECKGLERROR
693
694         qglColorMask(1, 1, 1, 1);
695         qglAlphaFunc(GL_GEQUAL, 0.5);CHECKGLERROR
696         qglDisable(GL_ALPHA_TEST);CHECKGLERROR
697         qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
698         qglDisable(GL_BLEND);CHECKGLERROR
699         qglCullFace(gl_state.cullface);CHECKGLERROR
700         qglEnable(GL_CULL_FACE);CHECKGLERROR
701         qglDepthFunc(GL_LEQUAL);CHECKGLERROR
702         qglEnable(GL_DEPTH_TEST);CHECKGLERROR
703         qglDepthMask(gl_state.depthmask);CHECKGLERROR
704         qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
705
706         if (vid.support.arb_vertex_buffer_object)
707         {
708                 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
709                 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
710         }
711
712         if (vid.support.ext_framebuffer_object)
713         {
714                 qglBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
715                 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
716         }
717
718         qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), NULL);CHECKGLERROR
719         qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
720
721         qglColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL);CHECKGLERROR
722         qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
723
724         GL_Color(0, 0, 0, 0);
725         GL_Color(1, 1, 1, 1);
726
727         gl_state.unit = MAX_TEXTUREUNITS;
728         gl_state.clientunit = MAX_TEXTUREUNITS;
729         switch(vid.renderpath)
730         {
731         case RENDERPATH_GL20:
732         case RENDERPATH_CGGL:
733                 for (i = 0;i < vid.teximageunits;i++)
734                 {
735                         GL_ActiveTexture(i);
736                         qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
737                         if (vid.support.ext_texture_3d)
738                         {
739                                 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
740                         }
741                         if (vid.support.arb_texture_cube_map)
742                         {
743                                 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
744                         }
745                         if (vid.support.arb_texture_rectangle)
746                         {
747                                 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);CHECKGLERROR
748                         }
749                 }
750
751                 for (i = 0;i < vid.texarrayunits;i++)
752                 {
753                         GL_ClientActiveTexture(i);
754                         GL_BindVBO(0);
755                         qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
756                         qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
757                 }
758                 CHECKGLERROR
759                 break;
760         case RENDERPATH_GL13:
761         case RENDERPATH_GL11:
762                 for (i = 0;i < vid.texunits;i++)
763                 {
764                         GL_ActiveTexture(i);
765                         GL_ClientActiveTexture(i);
766                         qglDisable(GL_TEXTURE_2D);CHECKGLERROR
767                         qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
768                         if (vid.support.ext_texture_3d)
769                         {
770                                 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
771                                 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
772                         }
773                         if (vid.support.arb_texture_cube_map)
774                         {
775                                 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
776                                 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
777                         }
778                         if (vid.support.arb_texture_rectangle)
779                         {
780                                 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
781                                 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);CHECKGLERROR
782                         }
783                         GL_BindVBO(0);
784                         qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
785                         qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
786                         qglMatrixMode(GL_TEXTURE);CHECKGLERROR
787                         qglLoadIdentity();CHECKGLERROR
788                         qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
789                         qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
790                 }
791                 CHECKGLERROR
792                 break;
793         }
794 }
795
796 void GL_ActiveTexture(unsigned int num)
797 {
798         if (gl_state.unit != num)
799         {
800                 gl_state.unit = num;
801                 if (qglActiveTexture)
802                 {
803                         CHECKGLERROR
804                         qglActiveTexture(GL_TEXTURE0_ARB + gl_state.unit);
805                         CHECKGLERROR
806                 }
807         }
808 }
809
810 void GL_ClientActiveTexture(unsigned int num)
811 {
812         if (gl_state.clientunit != num)
813         {
814                 gl_state.clientunit = num;
815                 if (qglActiveTexture)
816                 {
817                         CHECKGLERROR
818                         qglClientActiveTexture(GL_TEXTURE0_ARB + gl_state.clientunit);
819                         CHECKGLERROR
820                 }
821         }
822 }
823
824 void GL_BlendFunc(int blendfunc1, int blendfunc2)
825 {
826         if (gl_state.blendfunc1 != blendfunc1 || gl_state.blendfunc2 != blendfunc2)
827         {
828                 CHECKGLERROR
829                 qglBlendFunc(gl_state.blendfunc1 = blendfunc1, gl_state.blendfunc2 = blendfunc2);CHECKGLERROR
830                 if (gl_state.blendfunc2 == GL_ZERO)
831                 {
832                         if (gl_state.blendfunc1 == GL_ONE)
833                         {
834                                 if (gl_state.blend)
835                                 {
836                                         gl_state.blend = 0;
837                                         qglDisable(GL_BLEND);CHECKGLERROR
838                                 }
839                         }
840                         else
841                         {
842                                 if (!gl_state.blend)
843                                 {
844                                         gl_state.blend = 1;
845                                         qglEnable(GL_BLEND);CHECKGLERROR
846                                 }
847                         }
848                 }
849                 else
850                 {
851                         if (!gl_state.blend)
852                         {
853                                 gl_state.blend = 1;
854                                 qglEnable(GL_BLEND);CHECKGLERROR
855                         }
856                 }
857         }
858 }
859
860 void GL_DepthMask(int state)
861 {
862         if (gl_state.depthmask != state)
863         {
864                 CHECKGLERROR
865                 qglDepthMask(gl_state.depthmask = state);CHECKGLERROR
866         }
867 }
868
869 void GL_DepthTest(int state)
870 {
871         if (gl_state.depthtest != state)
872         {
873                 gl_state.depthtest = state;
874                 CHECKGLERROR
875                 if (gl_state.depthtest)
876                 {
877                         qglEnable(GL_DEPTH_TEST);CHECKGLERROR
878                 }
879                 else
880                 {
881                         qglDisable(GL_DEPTH_TEST);CHECKGLERROR
882                 }
883         }
884 }
885
886 void GL_DepthRange(float nearfrac, float farfrac)
887 {
888         if (gl_state.depthrange[0] != nearfrac || gl_state.depthrange[1] != farfrac)
889         {
890                 gl_state.depthrange[0] = nearfrac;
891                 gl_state.depthrange[1] = farfrac;
892                 qglDepthRange(nearfrac, farfrac);
893         }
894 }
895
896 void GL_PolygonOffset(float planeoffset, float depthoffset)
897 {
898         if (gl_state.polygonoffset[0] != planeoffset || gl_state.polygonoffset[1] != depthoffset)
899         {
900                 gl_state.polygonoffset[0] = planeoffset;
901                 gl_state.polygonoffset[1] = depthoffset;
902                 qglPolygonOffset(planeoffset, depthoffset);
903         }
904 }
905
906 void GL_SetMirrorState(qboolean state)
907 {
908         if(!state != !v_flipped_state)
909         {
910                 // change cull face mode!
911                 if(gl_state.cullface == GL_BACK)
912                         qglCullFace((gl_state.cullface = GL_FRONT));
913                 else if(gl_state.cullface == GL_FRONT)
914                         qglCullFace((gl_state.cullface = GL_BACK));
915         }
916         v_flipped_state = state;
917 }
918
919 void GL_CullFace(int state)
920 {
921         CHECKGLERROR
922
923         if(v_flipped_state)
924         {
925                 if(state == GL_FRONT)
926                         state = GL_BACK;
927                 else if(state == GL_BACK)
928                         state = GL_FRONT;
929         }
930
931         if (state != GL_NONE)
932         {
933                 if (!gl_state.cullfaceenable)
934                 {
935                         gl_state.cullfaceenable = true;
936                         qglEnable(GL_CULL_FACE);CHECKGLERROR
937                 }
938                 if (gl_state.cullface != state)
939                 {
940                         gl_state.cullface = state;
941                         qglCullFace(gl_state.cullface);CHECKGLERROR
942                 }
943         }
944         else
945         {
946                 if (gl_state.cullfaceenable)
947                 {
948                         gl_state.cullfaceenable = false;
949                         qglDisable(GL_CULL_FACE);CHECKGLERROR
950                 }
951         }
952 }
953
954 void GL_AlphaTest(int state)
955 {
956         if (gl_state.alphatest != state)
957         {
958                 gl_state.alphatest = state;
959                 CHECKGLERROR
960                 if (gl_state.alphatest)
961                 {
962                         qglEnable(GL_ALPHA_TEST);CHECKGLERROR
963                 }
964                 else
965                 {
966                         qglDisable(GL_ALPHA_TEST);CHECKGLERROR
967                 }
968         }
969 }
970
971 void GL_ColorMask(int r, int g, int b, int a)
972 {
973         int state = r*8 + g*4 + b*2 + a*1;
974         if (gl_state.colormask != state)
975         {
976                 gl_state.colormask = state;
977                 CHECKGLERROR
978                 qglColorMask((GLboolean)r, (GLboolean)g, (GLboolean)b, (GLboolean)a);CHECKGLERROR
979         }
980 }
981
982 void GL_Color(float cr, float cg, float cb, float ca)
983 {
984         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)
985         {
986                 gl_state.color4f[0] = cr;
987                 gl_state.color4f[1] = cg;
988                 gl_state.color4f[2] = cb;
989                 gl_state.color4f[3] = ca;
990                 CHECKGLERROR
991                 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);
992                 CHECKGLERROR
993         }
994 }
995
996 void GL_Scissor (int x, int y, int width, int height)
997 {
998         CHECKGLERROR
999         qglScissor(x, y,width,height);
1000         CHECKGLERROR
1001 }
1002
1003 void GL_ScissorTest(int state)
1004 {
1005         if(gl_state.scissortest == state)
1006                 return;
1007
1008         CHECKGLERROR
1009         if((gl_state.scissortest = state))
1010                 qglEnable(GL_SCISSOR_TEST);
1011         else
1012                 qglDisable(GL_SCISSOR_TEST);
1013         CHECKGLERROR
1014 }
1015
1016 void GL_Clear(int mask)
1017 {
1018         CHECKGLERROR
1019         qglClear(mask);CHECKGLERROR
1020 }
1021
1022 // called at beginning of frame
1023 void R_Mesh_Start(void)
1024 {
1025         BACKENDACTIVECHECK
1026         CHECKGLERROR
1027         if (gl_printcheckerror.integer && !gl_paranoid.integer)
1028         {
1029                 Con_Printf("WARNING: gl_printcheckerror is on but gl_paranoid is off, turning it on...\n");
1030                 Cvar_SetValueQuick(&gl_paranoid, 1);
1031         }
1032 }
1033
1034 qboolean GL_Backend_CompileShader(int programobject, GLenum shadertypeenum, const char *shadertype, int numstrings, const char **strings)
1035 {
1036         int shaderobject;
1037         int shadercompiled;
1038         char compilelog[MAX_INPUTLINE];
1039         shaderobject = qglCreateShaderObjectARB(shadertypeenum);CHECKGLERROR
1040         if (!shaderobject)
1041                 return false;
1042         qglShaderSourceARB(shaderobject, numstrings, strings, NULL);CHECKGLERROR
1043         qglCompileShaderARB(shaderobject);CHECKGLERROR
1044         qglGetObjectParameterivARB(shaderobject, GL_OBJECT_COMPILE_STATUS_ARB, &shadercompiled);CHECKGLERROR
1045         qglGetInfoLogARB(shaderobject, sizeof(compilelog), NULL, compilelog);CHECKGLERROR
1046         if (compilelog[0] && (strstr(compilelog, "error") || strstr(compilelog, "ERROR") || strstr(compilelog, "Error") || strstr(compilelog, "WARNING") || strstr(compilelog, "warning") || strstr(compilelog, "Warning")))
1047         {
1048                 int i, j, pretextlines = 0;
1049                 for (i = 0;i < numstrings - 1;i++)
1050                         for (j = 0;strings[i][j];j++)
1051                                 if (strings[i][j] == '\n')
1052                                         pretextlines++;
1053                 Con_Printf("%s shader compile log:\n%s\n(line offset for any above warnings/errors: %i)\n", shadertype, compilelog, pretextlines);
1054         }
1055         if (!shadercompiled)
1056         {
1057                 qglDeleteObjectARB(shaderobject);CHECKGLERROR
1058                 return false;
1059         }
1060         qglAttachObjectARB(programobject, shaderobject);CHECKGLERROR
1061         qglDeleteObjectARB(shaderobject);CHECKGLERROR
1062         return true;
1063 }
1064
1065 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)
1066 {
1067         GLint programlinked;
1068         GLuint programobject = 0;
1069         char linklog[MAX_INPUTLINE];
1070         CHECKGLERROR
1071
1072         programobject = qglCreateProgramObjectARB();CHECKGLERROR
1073         if (!programobject)
1074                 return 0;
1075
1076         if (vertexstrings_count && !GL_Backend_CompileShader(programobject, GL_VERTEX_SHADER_ARB, "vertex", vertexstrings_count, vertexstrings_list))
1077                 goto cleanup;
1078
1079 #ifdef GL_GEOMETRY_SHADER_ARB
1080         if (geometrystrings_count && !GL_Backend_CompileShader(programobject, GL_GEOMETRY_SHADER_ARB, "geometry", geometrystrings_count, geometrystrings_list))
1081                 goto cleanup;
1082 #endif
1083
1084         if (fragmentstrings_count && !GL_Backend_CompileShader(programobject, GL_FRAGMENT_SHADER_ARB, "fragment", fragmentstrings_count, fragmentstrings_list))
1085                 goto cleanup;
1086
1087         qglLinkProgramARB(programobject);CHECKGLERROR
1088         qglGetObjectParameterivARB(programobject, GL_OBJECT_LINK_STATUS_ARB, &programlinked);CHECKGLERROR
1089         qglGetInfoLogARB(programobject, sizeof(linklog), NULL, linklog);CHECKGLERROR
1090         if (linklog[0])
1091         {
1092                 if (strstr(linklog, "error") || strstr(linklog, "ERROR") || strstr(linklog, "Error") || strstr(linklog, "WARNING") || strstr(linklog, "warning") || strstr(linklog, "Warning"))
1093                         Con_DPrintf("program link log:\n%s\n", linklog);
1094                 // software vertex shader is ok but software fragment shader is WAY
1095                 // too slow, fail program if so.
1096                 // NOTE: this string might be ATI specific, but that's ok because the
1097                 // ATI R300 chip (Radeon 9500-9800/X300) is the most likely to use a
1098                 // software fragment shader due to low instruction and dependent
1099                 // texture limits.
1100                 if (strstr(linklog, "fragment shader will run in software"))
1101                         programlinked = false;
1102         }
1103         if (!programlinked)
1104                 goto cleanup;
1105         return programobject;
1106 cleanup:
1107         qglDeleteObjectARB(programobject);CHECKGLERROR
1108         return 0;
1109 }
1110
1111 void GL_Backend_FreeProgram(unsigned int prog)
1112 {
1113         CHECKGLERROR
1114         qglDeleteObjectARB(prog);
1115         CHECKGLERROR
1116 }
1117
1118 void GL_Backend_RenumberElements(int *out, int count, const int *in, int offset)
1119 {
1120         int i;
1121         if (offset)
1122         {
1123                 for (i = 0;i < count;i++)
1124                         *out++ = *in++ + offset;
1125         }
1126         else
1127                 memcpy(out, in, sizeof(*out) * count);
1128 }
1129
1130 // renders triangles using vertices from the active arrays
1131 int paranoidblah = 0;
1132 void R_Mesh_Draw(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const unsigned short *element3s, int bufferobject3i, int bufferobject3s)
1133 {
1134         unsigned int numelements = numtriangles * 3;
1135         if (numvertices < 3 || numtriangles < 1)
1136         {
1137                 if (numvertices < 0 || numtriangles < 0 || developer_extra.integer)
1138                         Con_DPrintf("R_Mesh_Draw(%d, %d, %d, %d, %8p, %8p, %i, %i);\n", firstvertex, numvertices, firsttriangle, numtriangles, (void *)element3i, (void *)element3s, bufferobject3i, bufferobject3s);
1139                 return;
1140         }
1141         if (!gl_mesh_prefer_short_elements.integer)
1142         {
1143                 if (element3i)
1144                         element3s = NULL;
1145                 if (bufferobject3i)
1146                         bufferobject3s = 0;
1147         }
1148         if (element3i)
1149                 element3i += firsttriangle * 3;
1150         if (element3s)
1151                 element3s += firsttriangle * 3;
1152         switch (gl_vbo.integer)
1153         {
1154         default:
1155         case 0:
1156         case 2:
1157                 bufferobject3i = bufferobject3s = 0;
1158                 break;
1159         case 1:
1160                 break;
1161         case 3:
1162                 if (firsttriangle)
1163                         bufferobject3i = bufferobject3s = 0;
1164                 break;
1165         }
1166         CHECKGLERROR
1167         r_refdef.stats.meshes++;
1168         r_refdef.stats.meshes_elements += numelements;
1169         if (gl_paranoid.integer)
1170         {
1171                 unsigned int i, j, size;
1172                 const int *p;
1173                 // note: there's no validation done here on buffer objects because it
1174                 // is somewhat difficult to get at the data, and gl_paranoid can be
1175                 // used without buffer objects if the need arises
1176                 // (the data could be gotten using glMapBuffer but it would be very
1177                 //  slow due to uncachable video memory reads)
1178                 if (!qglIsEnabled(GL_VERTEX_ARRAY))
1179                         Con_Print("R_Mesh_Draw: vertex array not enabled\n");
1180                 CHECKGLERROR
1181                 if (gl_state.pointer_vertex)
1182                         for (j = 0, size = numvertices * 3, p = (int *)((float *)gl_state.pointer_vertex + firstvertex * 3);j < size;j++, p++)
1183                                 paranoidblah += *p;
1184                 if (gl_state.pointer_color_enabled)
1185                 {
1186                         if (!qglIsEnabled(GL_COLOR_ARRAY))
1187                                 Con_Print("R_Mesh_Draw: color array set but not enabled\n");
1188                         CHECKGLERROR
1189                         if (gl_state.pointer_color && gl_state.pointer_color_enabled)
1190                                 for (j = 0, size = numvertices * 4, p = (int *)((float *)gl_state.pointer_color + firstvertex * 4);j < size;j++, p++)
1191                                         paranoidblah += *p;
1192                 }
1193                 for (i = 0;i < vid.texarrayunits;i++)
1194                 {
1195                         if (gl_state.units[i].arrayenabled)
1196                         {
1197                                 GL_ClientActiveTexture(i);
1198                                 if (!qglIsEnabled(GL_TEXTURE_COORD_ARRAY))
1199                                         Con_Print("R_Mesh_Draw: texcoord array set but not enabled\n");
1200                                 CHECKGLERROR
1201                                 if (gl_state.units[i].pointer_texcoord && gl_state.units[i].arrayenabled)
1202                                         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++)
1203                                                 paranoidblah += *p;
1204                         }
1205                 }
1206                 if (element3i)
1207                 {
1208                         for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1209                         {
1210                                 if (element3i[i] < firstvertex || element3i[i] >= firstvertex + numvertices)
1211                                 {
1212                                         Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3i array\n", element3i[i], firstvertex, firstvertex + numvertices);
1213                                         return;
1214                                 }
1215                         }
1216                 }
1217                 if (element3s)
1218                 {
1219                         for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1220                         {
1221                                 if (element3s[i] < firstvertex || element3s[i] >= firstvertex + numvertices)
1222                                 {
1223                                         Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3s array\n", element3s[i], firstvertex, firstvertex + numvertices);
1224                                         return;
1225                                 }
1226                         }
1227                 }
1228                 CHECKGLERROR
1229         }
1230         if (r_render.integer || r_refdef.draw2dstage)
1231         {
1232                 CHECKGLERROR
1233                 if (gl_mesh_testmanualfeeding.integer)
1234                 {
1235                         unsigned int i, j, element;
1236                         const GLfloat *p;
1237                         qglBegin(GL_TRIANGLES);
1238                         for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1239                         {
1240                                 element = element3i ? element3i[i] : element3s[i];
1241                                 for (j = 0;j < vid.texarrayunits;j++)
1242                                 {
1243                                         if (gl_state.units[j].pointer_texcoord && gl_state.units[j].arrayenabled)
1244                                         {
1245                                                 if (vid.texarrayunits > 1)
1246                                                 {
1247                                                         if (gl_state.units[j].arraycomponents == 4)
1248                                                         {
1249                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 4;
1250                                                                 qglMultiTexCoord4f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2], p[3]);
1251                                                         }
1252                                                         else if (gl_state.units[j].arraycomponents == 3)
1253                                                         {
1254                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 3;
1255                                                                 qglMultiTexCoord3f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2]);
1256                                                         }
1257                                                         else if (gl_state.units[j].arraycomponents == 2)
1258                                                         {
1259                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 2;
1260                                                                 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, p[0], p[1]);
1261                                                         }
1262                                                         else
1263                                                         {
1264                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 1;
1265                                                                 qglMultiTexCoord1f(GL_TEXTURE0_ARB + j, p[0]);
1266                                                         }
1267                                                 }
1268                                                 else
1269                                                 {
1270                                                         if (gl_state.units[j].arraycomponents == 4)
1271                                                         {
1272                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 4;
1273                                                                 qglTexCoord4f(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                                                                 qglTexCoord3f(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                                                                 qglTexCoord2f(p[0], p[1]);
1284                                                         }
1285                                                         else
1286                                                         {
1287                                                                 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 1;
1288                                                                 qglTexCoord1f(p[0]);
1289                                                         }
1290                                                 }
1291                                         }
1292                                 }
1293                                 if (gl_state.pointer_color && gl_state.pointer_color_enabled)
1294                                 {
1295                                         p = ((const GLfloat *)(gl_state.pointer_color)) + element * 4;
1296                                         qglColor4f(p[0], p[1], p[2], p[3]);
1297                                 }
1298                                 p = ((const GLfloat *)(gl_state.pointer_vertex)) + element * 3;
1299                                 qglVertex3f(p[0], p[1], p[2]);
1300                         }
1301                         qglEnd();
1302                         CHECKGLERROR
1303                 }
1304                 else if (gl_mesh_testarrayelement.integer)
1305                 {
1306                         int i;
1307                         qglBegin(GL_TRIANGLES);
1308                         if (element3i)
1309                         {
1310                                 for (i = 0;i < numtriangles * 3;i++)
1311                                         qglArrayElement(element3i[i]);
1312                         }
1313                         else if (element3s)
1314                         {
1315                                 for (i = 0;i < numtriangles * 3;i++)
1316                                         qglArrayElement(element3s[i]);
1317                         }
1318                         qglEnd();
1319                         CHECKGLERROR
1320                 }
1321                 else if (bufferobject3s)
1322                 {
1323                         GL_BindEBO(bufferobject3s);
1324                         if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1325                         {
1326                                 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, (void *)(firsttriangle * sizeof(unsigned short[3])));
1327                                 CHECKGLERROR
1328                         }
1329                         else
1330                         {
1331                                 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, (void *)(firsttriangle * sizeof(unsigned short[3])));
1332                                 CHECKGLERROR
1333                         }
1334                 }
1335                 else if (bufferobject3i)
1336                 {
1337                         GL_BindEBO(bufferobject3i);
1338                         if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1339                         {
1340                                 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, (void *)(firsttriangle * sizeof(unsigned int[3])));
1341                                 CHECKGLERROR
1342                         }
1343                         else
1344                         {
1345                                 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, (void *)(firsttriangle * sizeof(unsigned int[3])));
1346                                 CHECKGLERROR
1347                         }
1348                 }
1349                 else if (element3s)
1350                 {
1351                         GL_BindEBO(0);
1352                         if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1353                         {
1354                                 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, element3s);
1355                                 CHECKGLERROR
1356                         }
1357                         else
1358                         {
1359                                 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, element3s);
1360                                 CHECKGLERROR
1361                         }
1362                 }
1363                 else if (element3i)
1364                 {
1365                         GL_BindEBO(0);
1366                         if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1367                         {
1368                                 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, element3i);
1369                                 CHECKGLERROR
1370                         }
1371                         else
1372                         {
1373                                 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, element3i);
1374                                 CHECKGLERROR
1375                         }
1376                 }
1377         }
1378 }
1379
1380 // restores backend state, used when done with 3D rendering
1381 void R_Mesh_Finish(void)
1382 {
1383 }
1384
1385 int R_Mesh_CreateStaticBufferObject(unsigned int target, void *data, size_t size, const char *name)
1386 {
1387         gl_bufferobjectinfo_t *info;
1388         GLuint bufferobject;
1389
1390         if (!gl_vbo.integer)
1391                 return 0;
1392
1393         qglGenBuffersARB(1, &bufferobject);
1394         switch(target)
1395         {
1396         case GL_ELEMENT_ARRAY_BUFFER_ARB: GL_BindEBO(bufferobject);break;
1397         case GL_ARRAY_BUFFER_ARB: GL_BindVBO(bufferobject);break;
1398         default: Sys_Error("R_Mesh_CreateStaticBufferObject: unknown target type %i\n", target);return 0;
1399         }
1400         qglBufferDataARB(target, size, data, GL_STATIC_DRAW_ARB);
1401
1402         info = (gl_bufferobjectinfo_t *) Mem_ExpandableArray_AllocRecord(&gl_state.bufferobjectinfoarray);
1403         memset(info, 0, sizeof(*info));
1404         info->target = target;
1405         info->object = bufferobject;
1406         info->size = size;
1407         strlcpy(info->name, name, sizeof(info->name));
1408
1409         return (int)bufferobject;
1410 }
1411
1412 void R_Mesh_DestroyBufferObject(int bufferobject)
1413 {
1414         int i, endindex;
1415         gl_bufferobjectinfo_t *info;
1416
1417         qglDeleteBuffersARB(1, (GLuint *)&bufferobject);
1418
1419         endindex = Mem_ExpandableArray_IndexRange(&gl_state.bufferobjectinfoarray);
1420         for (i = 0;i < endindex;i++)
1421         {
1422                 info = (gl_bufferobjectinfo_t *) Mem_ExpandableArray_RecordAtIndex(&gl_state.bufferobjectinfoarray, i);
1423                 if (!info)
1424                         continue;
1425                 if (info->object == bufferobject)
1426                 {
1427                         Mem_ExpandableArray_FreeRecord(&gl_state.bufferobjectinfoarray, (void *)info);
1428                         break;
1429                 }
1430         }
1431 }
1432
1433 void GL_Mesh_ListVBOs(qboolean printeach)
1434 {
1435         int i, endindex;
1436         size_t ebocount = 0, ebomemory = 0;
1437         size_t vbocount = 0, vbomemory = 0;
1438         gl_bufferobjectinfo_t *info;
1439         endindex = Mem_ExpandableArray_IndexRange(&gl_state.bufferobjectinfoarray);
1440         for (i = 0;i < endindex;i++)
1441         {
1442                 info = (gl_bufferobjectinfo_t *) Mem_ExpandableArray_RecordAtIndex(&gl_state.bufferobjectinfoarray, i);
1443                 if (!info)
1444                         continue;
1445                 switch(info->target)
1446                 {
1447                 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;
1448                 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;
1449                 default: Con_Printf("gl_vbostats: unknown target type %i\n", info->target);break;
1450                 }
1451         }
1452         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);
1453 }
1454
1455 void R_Mesh_VertexPointer(const float *vertex3f, int bufferobject, size_t bufferoffset)
1456 {
1457         if (!gl_vbo.integer || gl_mesh_testarrayelement.integer)
1458                 bufferobject = 0;
1459         if (gl_state.pointer_vertex != vertex3f || gl_state.pointer_vertex_buffer != bufferobject || gl_state.pointer_vertex_offset != bufferoffset)
1460         {
1461                 gl_state.pointer_vertex = vertex3f;
1462                 gl_state.pointer_vertex_buffer = bufferobject;
1463                 gl_state.pointer_vertex_offset = bufferoffset;
1464                 CHECKGLERROR
1465                 GL_BindVBO(bufferobject);
1466                 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), bufferobject ? (void *)bufferoffset : vertex3f);CHECKGLERROR
1467         }
1468 }
1469
1470 void R_Mesh_ColorPointer(const float *color4f, int bufferobject, size_t bufferoffset)
1471 {
1472         // note: this can not rely on bufferobject to decide whether a color array
1473         // is supplied, because surfmesh_t shares one vbo for all arrays, which
1474         // means that a valid vbo may be supplied even if there is no color array.
1475         if (color4f)
1476         {
1477                 if (!gl_vbo.integer || gl_mesh_testarrayelement.integer)
1478                         bufferobject = 0;
1479                 // caller wants color array enabled
1480                 if (!gl_state.pointer_color_enabled)
1481                 {
1482                         gl_state.pointer_color_enabled = true;
1483                         CHECKGLERROR
1484                         qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1485                 }
1486                 if (gl_state.pointer_color != color4f || gl_state.pointer_color_buffer != bufferobject || gl_state.pointer_color_offset != bufferoffset)
1487                 {
1488                         gl_state.pointer_color = color4f;
1489                         gl_state.pointer_color_buffer = bufferobject;
1490                         gl_state.pointer_color_offset = bufferoffset;
1491                         CHECKGLERROR
1492                         GL_BindVBO(bufferobject);
1493                         qglColorPointer(4, GL_FLOAT, sizeof(float[4]), bufferobject ? (void *)bufferoffset : color4f);CHECKGLERROR
1494                 }
1495         }
1496         else
1497         {
1498                 // caller wants color array disabled
1499                 if (gl_state.pointer_color_enabled)
1500                 {
1501                         gl_state.pointer_color_enabled = false;
1502                         CHECKGLERROR
1503                         qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1504                         // when color array is on the glColor gets trashed, set it again
1505                         qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);CHECKGLERROR
1506                 }
1507         }
1508 }
1509
1510 void R_Mesh_TexCoordPointer(unsigned int unitnum, unsigned int numcomponents, const float *texcoord, int bufferobject, size_t bufferoffset)
1511 {
1512         gltextureunit_t *unit = gl_state.units + unitnum;
1513         // update array settings
1514         CHECKGLERROR
1515         // note: there is no need to check bufferobject here because all cases
1516         // that involve a valid bufferobject also supply a texcoord array
1517         if (texcoord)
1518         {
1519                 if (!gl_vbo.integer || gl_mesh_testarrayelement.integer)
1520                         bufferobject = 0;
1521                 // texture array unit is enabled, enable the array
1522                 if (!unit->arrayenabled)
1523                 {
1524                         unit->arrayenabled = true;
1525                         GL_ClientActiveTexture(unitnum);
1526                         qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1527                 }
1528                 // texcoord array
1529                 if (unit->pointer_texcoord != texcoord || unit->pointer_texcoord_buffer != bufferobject || unit->pointer_texcoord_offset != bufferoffset || unit->arraycomponents != numcomponents)
1530                 {
1531                         unit->pointer_texcoord = texcoord;
1532                         unit->pointer_texcoord_buffer = bufferobject;
1533                         unit->pointer_texcoord_offset = bufferoffset;
1534                         unit->arraycomponents = numcomponents;
1535                         GL_ClientActiveTexture(unitnum);
1536                         GL_BindVBO(bufferobject);
1537                         qglTexCoordPointer(unit->arraycomponents, GL_FLOAT, sizeof(float) * unit->arraycomponents, bufferobject ? (void *)bufferoffset : texcoord);CHECKGLERROR
1538                 }
1539         }
1540         else
1541         {
1542                 // texture array unit is disabled, disable the array
1543                 if (unit->arrayenabled)
1544                 {
1545                         unit->arrayenabled = false;
1546                         GL_ClientActiveTexture(unitnum);
1547                         qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1548                 }
1549         }
1550 }
1551
1552 int R_Mesh_TexBound(unsigned int unitnum, int id)
1553 {
1554         gltextureunit_t *unit = gl_state.units + unitnum;
1555         if (unitnum >= vid.teximageunits)
1556                 return 0;
1557         if (id == GL_TEXTURE_2D)
1558                 return unit->t2d;
1559         if (id == GL_TEXTURE_3D)
1560                 return unit->t3d;
1561         if (id == GL_TEXTURE_CUBE_MAP_ARB)
1562                 return unit->tcubemap;
1563         if (id == GL_TEXTURE_RECTANGLE_ARB)
1564                 return unit->trectangle;
1565         return 0;
1566 }
1567
1568 void R_Mesh_CopyToTexture(rtexture_t *tex, int tx, int ty, int sx, int sy, int width, int height)
1569 {
1570         R_Mesh_TexBind(0, tex);
1571         GL_ActiveTexture(0);CHECKGLERROR
1572         qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, tx, ty, sx, sy, width, height);CHECKGLERROR
1573 }
1574
1575 void R_Mesh_TexBind(unsigned int unitnum, rtexture_t *tex)
1576 {
1577         gltextureunit_t *unit = gl_state.units + unitnum;
1578         int tex2d, tex3d, texcubemap, texnum;
1579         if (unitnum >= vid.teximageunits)
1580                 return;
1581         switch(vid.renderpath)
1582         {
1583         case RENDERPATH_GL20:
1584         case RENDERPATH_CGGL:
1585                 if (!tex)
1586                         tex = r_texture_white;
1587                 texnum = R_GetTexture(tex);
1588                 switch(tex->gltexturetypeenum)
1589                 {
1590                 case GL_TEXTURE_2D: if (unit->t2d != texnum) {GL_ActiveTexture(unitnum);unit->t2d = texnum;qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR}break;
1591                 case GL_TEXTURE_3D: if (unit->t3d != texnum) {GL_ActiveTexture(unitnum);unit->t3d = texnum;qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR}break;
1592                 case GL_TEXTURE_CUBE_MAP_ARB: if (unit->tcubemap != texnum) {GL_ActiveTexture(unitnum);unit->tcubemap = texnum;qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR}break;
1593                 case GL_TEXTURE_RECTANGLE_ARB: if (unit->trectangle != texnum) {GL_ActiveTexture(unitnum);unit->trectangle = texnum;qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR}break;
1594                 }
1595                 break;
1596         case RENDERPATH_GL13:
1597         case RENDERPATH_GL11:
1598                 tex2d = 0;
1599                 tex3d = 0;
1600                 texcubemap = 0;
1601                 if (tex)
1602                 {
1603                         texnum = R_GetTexture(tex);
1604                         switch(tex->gltexturetypeenum)
1605                         {
1606                         case GL_TEXTURE_2D:
1607                                 tex2d = texnum;
1608                                 break;
1609                         case GL_TEXTURE_3D:
1610                                 tex3d = texnum;
1611                                 break;
1612                         case GL_TEXTURE_CUBE_MAP_ARB:
1613                                 texcubemap = texnum;
1614                                 break;
1615                         }
1616                 }
1617                 // update 2d texture binding
1618                 if (unit->t2d != tex2d)
1619                 {
1620                         GL_ActiveTexture(unitnum);
1621                         if (tex2d)
1622                         {
1623                                 if (unit->t2d == 0)
1624                                 {
1625                                         qglEnable(GL_TEXTURE_2D);CHECKGLERROR
1626                                 }
1627                         }
1628                         else
1629                         {
1630                                 if (unit->t2d)
1631                                 {
1632                                         qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1633                                 }
1634                         }
1635                         unit->t2d = tex2d;
1636                         qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1637                 }
1638                 // update 3d texture binding
1639                 if (unit->t3d != tex3d)
1640                 {
1641                         GL_ActiveTexture(unitnum);
1642                         if (tex3d)
1643                         {
1644                                 if (unit->t3d == 0)
1645                                 {
1646                                         qglEnable(GL_TEXTURE_3D);CHECKGLERROR
1647                                 }
1648                         }
1649                         else
1650                         {
1651                                 if (unit->t3d)
1652                                 {
1653                                         qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1654                                 }
1655                         }
1656                         unit->t3d = tex3d;
1657                         qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1658                 }
1659                 // update cubemap texture binding
1660                 if (unit->tcubemap != texcubemap)
1661                 {
1662                         GL_ActiveTexture(unitnum);
1663                         if (texcubemap)
1664                         {
1665                                 if (unit->tcubemap == 0)
1666                                 {
1667                                         qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1668                                 }
1669                         }
1670                         else
1671                         {
1672                                 if (unit->tcubemap)
1673                                 {
1674                                         qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1675                                 }
1676                         }
1677                         unit->tcubemap = texcubemap;
1678                         qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1679                 }
1680                 break;
1681         }
1682 }
1683
1684 void R_Mesh_TexMatrix(unsigned int unitnum, const matrix4x4_t *matrix)
1685 {
1686         gltextureunit_t *unit = gl_state.units + unitnum;
1687         if (matrix && matrix->m[3][3])
1688         {
1689                 // texmatrix specified, check if it is different
1690                 if (!unit->texmatrixenabled || memcmp(&unit->matrix, matrix, sizeof(matrix4x4_t)))
1691                 {
1692                         float glmatrix[16];
1693                         unit->texmatrixenabled = true;
1694                         unit->matrix = *matrix;
1695                         CHECKGLERROR
1696                         Matrix4x4_ToArrayFloatGL(&unit->matrix, glmatrix);
1697                         GL_ActiveTexture(unitnum);
1698                         qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1699                         qglLoadMatrixf(glmatrix);CHECKGLERROR
1700                         qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1701                 }
1702         }
1703         else
1704         {
1705                 // no texmatrix specified, revert to identity
1706                 if (unit->texmatrixenabled)
1707                 {
1708                         unit->texmatrixenabled = false;
1709                         unit->matrix = identitymatrix;
1710                         CHECKGLERROR
1711                         GL_ActiveTexture(unitnum);
1712                         qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1713                         qglLoadIdentity();CHECKGLERROR
1714                         qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1715                 }
1716         }
1717 }
1718
1719 void R_Mesh_TexCombine(unsigned int unitnum, int combinergb, int combinealpha, int rgbscale, int alphascale)
1720 {
1721         gltextureunit_t *unit = gl_state.units + unitnum;
1722         CHECKGLERROR
1723         switch(vid.renderpath)
1724         {
1725         case RENDERPATH_GL20:
1726         case RENDERPATH_CGGL:
1727                 // do nothing
1728                 break;
1729         case RENDERPATH_GL13:
1730                 // GL_ARB_texture_env_combine
1731                 if (!combinergb)
1732                         combinergb = GL_MODULATE;
1733                 if (!combinealpha)
1734                         combinealpha = GL_MODULATE;
1735                 if (!rgbscale)
1736                         rgbscale = 1;
1737                 if (!alphascale)
1738                         alphascale = 1;
1739                 if (combinergb != combinealpha || rgbscale != 1 || alphascale != 1)
1740                 {
1741                         if (combinergb == GL_DECAL)
1742                                 combinergb = GL_INTERPOLATE_ARB;
1743                         if (unit->combine != GL_COMBINE_ARB)
1744                         {
1745                                 unit->combine = GL_COMBINE_ARB;
1746                                 GL_ActiveTexture(unitnum);
1747                                 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);CHECKGLERROR
1748                                 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_TEXTURE);CHECKGLERROR // for GL_INTERPOLATE_ARB mode
1749                         }
1750                         if (unit->combinergb != combinergb)
1751                         {
1752                                 unit->combinergb = combinergb;
1753                                 GL_ActiveTexture(unitnum);
1754                                 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
1755                         }
1756                         if (unit->combinealpha != combinealpha)
1757                         {
1758                                 unit->combinealpha = combinealpha;
1759                                 GL_ActiveTexture(unitnum);
1760                                 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
1761                         }
1762                         if (unit->rgbscale != rgbscale)
1763                         {
1764                                 unit->rgbscale = rgbscale;
1765                                 GL_ActiveTexture(unitnum);
1766                                 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, unit->rgbscale);CHECKGLERROR
1767                         }
1768                         if (unit->alphascale != alphascale)
1769                         {
1770                                 unit->alphascale = alphascale;
1771                                 GL_ActiveTexture(unitnum);
1772                                 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, unit->alphascale);CHECKGLERROR
1773                         }
1774                 }
1775                 else
1776                 {
1777                         if (unit->combine != combinergb)
1778                         {
1779                                 unit->combine = combinergb;
1780                                 GL_ActiveTexture(unitnum);
1781                                 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combine);CHECKGLERROR
1782                         }
1783                 }
1784                 break;
1785         case RENDERPATH_GL11:
1786                 // normal GL texenv
1787                 if (!combinergb)
1788                         combinergb = GL_MODULATE;
1789                 if (unit->combine != combinergb)
1790                 {
1791                         unit->combine = combinergb;
1792                         GL_ActiveTexture(unitnum);
1793                         qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combine);CHECKGLERROR
1794                 }
1795                 break;
1796         }
1797 }
1798
1799 void R_Mesh_ResetTextureState(void)
1800 {
1801         unsigned int unitnum;
1802
1803         BACKENDACTIVECHECK
1804
1805         CHECKGLERROR
1806         switch(vid.renderpath)
1807         {
1808         case RENDERPATH_GL20:
1809         case RENDERPATH_CGGL:
1810                 for (unitnum = 0;unitnum < vid.teximageunits;unitnum++)
1811                 {
1812                         gltextureunit_t *unit = gl_state.units + unitnum;
1813                         if (unit->t2d)
1814                         {
1815                                 unit->t2d = 0;
1816                                 GL_ActiveTexture(unitnum);
1817                                 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1818                         }
1819                         if (unit->t3d)
1820                         {
1821                                 unit->t3d = 0;
1822                                 GL_ActiveTexture(unitnum);
1823                                 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1824                         }
1825                         if (unit->tcubemap)
1826                         {
1827                                 unit->tcubemap = 0;
1828                                 GL_ActiveTexture(unitnum);
1829                                 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1830                         }
1831                         if (unit->trectangle)
1832                         {
1833                                 unit->trectangle = 0;
1834                                 GL_ActiveTexture(unitnum);
1835                                 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
1836                         }
1837                 }
1838                 for (unitnum = 0;unitnum < vid.texarrayunits;unitnum++)
1839                 {
1840                         gltextureunit_t *unit = gl_state.units + unitnum;
1841                         if (unit->arrayenabled)
1842                         {
1843                                 unit->arrayenabled = false;
1844                                 GL_ClientActiveTexture(unitnum);
1845                                 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1846                         }
1847                 }
1848                 for (unitnum = 0;unitnum < vid.texunits;unitnum++)
1849                 {
1850                         gltextureunit_t *unit = gl_state.units + unitnum;
1851                         if (unit->texmatrixenabled)
1852                         {
1853                                 unit->texmatrixenabled = false;
1854                                 unit->matrix = identitymatrix;
1855                                 CHECKGLERROR
1856                                 GL_ActiveTexture(unitnum);
1857                                 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1858                                 qglLoadIdentity();CHECKGLERROR
1859                                 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1860                         }
1861                 }
1862                 break;
1863         case RENDERPATH_GL13:
1864         case RENDERPATH_GL11:
1865                 for (unitnum = 0;unitnum < vid.texunits;unitnum++)
1866                 {
1867                         gltextureunit_t *unit = gl_state.units + unitnum;
1868                         if (unit->t2d)
1869                         {
1870                                 unit->t2d = 0;
1871                                 GL_ActiveTexture(unitnum);
1872                                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1873                                 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1874                         }
1875                         if (unit->t3d)
1876                         {
1877                                 unit->t3d = 0;
1878                                 GL_ActiveTexture(unitnum);
1879                                 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1880                                 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1881                         }
1882                         if (unit->tcubemap)
1883                         {
1884                                 unit->tcubemap = 0;
1885                                 GL_ActiveTexture(unitnum);
1886                                 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1887                                 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1888                         }
1889                         if (unit->trectangle)
1890                         {
1891                                 unit->trectangle = 0;
1892                                 GL_ActiveTexture(unitnum);
1893                                 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
1894                                 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
1895                         }
1896                         if (unit->arrayenabled)
1897                         {
1898                                 unit->arrayenabled = false;
1899                                 GL_ClientActiveTexture(unitnum);
1900                                 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1901                         }
1902                         if (unit->texmatrixenabled)
1903                         {
1904                                 unit->texmatrixenabled = false;
1905                                 unit->matrix = identitymatrix;
1906                                 CHECKGLERROR
1907                                 GL_ActiveTexture(unitnum);
1908                                 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1909                                 qglLoadIdentity();CHECKGLERROR
1910                                 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1911                         }
1912                         if (unit->combine != GL_MODULATE)
1913                         {
1914                                 unit->combine = GL_MODULATE;
1915                                 GL_ActiveTexture(unitnum);
1916                                 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combine);CHECKGLERROR
1917                         }
1918                 }
1919                 break;
1920         }
1921 }