varray_* rewritten to remove padding (varray_vertex3f, varray_texcoord2f, varray_texc...
[xonotic/darkplaces.git] / gl_backend.c
1
2 #include "quakedef.h"
3 #include "image.h"
4
5 cvar_t gl_mesh_maxverts = {0, "gl_mesh_maxverts", "21760"};
6 cvar_t gl_mesh_floatcolors = {0, "gl_mesh_floatcolors", "1"};
7 cvar_t gl_mesh_drawrangeelements = {0, "gl_mesh_drawrangeelements", "1"};
8 cvar_t gl_mesh_vertex_array_range = {0, "gl_mesh_vertex_array_range", "0"};
9 cvar_t gl_mesh_vertex_array_range_readfrequency = {0, "gl_mesh_vertex_array_range_readfrequency", "0.2"};
10 cvar_t gl_mesh_vertex_array_range_writefrequency = {0, "gl_mesh_vertex_array_range_writefrequency", "0.2"};
11 cvar_t gl_mesh_vertex_array_range_priority = {0, "gl_mesh_vertex_array_range_priority", "0.7"};
12 cvar_t gl_delayfinish = {CVAR_SAVE, "gl_delayfinish", "0"};
13
14 cvar_t r_render = {0, "r_render", "1"};
15 cvar_t gl_dither = {CVAR_SAVE, "gl_dither", "1"}; // whether or not to use dithering
16 cvar_t gl_lockarrays = {0, "gl_lockarrays", "1"};
17
18 int gl_maxdrawrangeelementsvertices;
19 int gl_maxdrawrangeelementsindices;
20
21 #ifdef DEBUGGL
22 int errornumber = 0;
23
24 void GL_PrintError(int errornumber, char *filename, int linenumber)
25 {
26         switch(errornumber)
27         {
28 #ifdef GL_INVALID_ENUM
29         case GL_INVALID_ENUM:
30                 Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
31                 break;
32 #endif
33 #ifdef GL_INVALID_VALUE
34         case GL_INVALID_VALUE:
35                 Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
36                 break;
37 #endif
38 #ifdef GL_INVALID_OPERATION
39         case GL_INVALID_OPERATION:
40                 Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
41                 break;
42 #endif
43 #ifdef GL_STACK_OVERFLOW
44         case GL_STACK_OVERFLOW:
45                 Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
46                 break;
47 #endif
48 #ifdef GL_STACK_UNDERFLOW
49         case GL_STACK_UNDERFLOW:
50                 Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
51                 break;
52 #endif
53 #ifdef GL_OUT_OF_MEMORY
54         case GL_OUT_OF_MEMORY:
55                 Con_Printf("GL_OUT_OF_MEMORY at %s:%i\n", filename, linenumber);
56                 break;
57 #endif
58 #ifdef GL_TABLE_TOO_LARGE
59         case GL_TABLE_TOO_LARGE:
60                 Con_Printf("GL_TABLE_TOO_LARGE at %s:%i\n", filename, linenumber);
61                 break;
62 #endif
63         default:
64                 Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
65                 break;
66         }
67 }
68 #endif
69
70 #define BACKENDACTIVECHECK if (!backendactive) Sys_Error("GL backend function called when backend is not active\n");
71
72 int c_meshs, c_meshelements;
73
74 void SCR_ScreenShot_f (void);
75
76 // these are externally accessible
77 int r_lightmapscalebit;
78 float r_colorscale;
79 GLfloat *varray_vertex3f, *varray_buf_vertex3f;
80 GLfloat *varray_color4f, *varray_buf_color4f;
81 GLfloat *varray_texcoord3f[MAX_TEXTUREUNITS], *varray_buf_texcoord3f[MAX_TEXTUREUNITS];
82 GLfloat *varray_texcoord2f[MAX_TEXTUREUNITS], *varray_buf_texcoord2f[MAX_TEXTUREUNITS];
83 static qbyte *varray_color4b, *varray_buf_color4b;
84 int mesh_maxverts;
85 int mesh_var;
86 float mesh_var_readfrequency;
87 float mesh_var_writefrequency;
88 float mesh_var_priority;
89 int varray_offset = 0, varray_offsetnext = 0;
90 GLuint *varray_buf_elements3i;
91 int mesh_maxelements = 3072;
92
93 static matrix4x4_t backend_viewmatrix;
94 static matrix4x4_t backend_modelmatrix;
95 static matrix4x4_t backend_modelviewmatrix;
96 static matrix4x4_t backend_glmodelviewmatrix;
97 static matrix4x4_t backend_projectmatrix;
98
99 static int backendunits, backendactive;
100 static mempool_t *gl_backend_mempool;
101
102 /*
103 note: here's strip order for a terrain row:
104 0--1--2--3--4
105 |\ |\ |\ |\ |
106 | \| \| \| \|
107 A--B--C--D--E
108
109 A0B, 01B, B1C, 12C, C2D, 23D, D3E, 34E
110
111 *elements++ = i + row;
112 *elements++ = i;
113 *elements++ = i + row + 1;
114 *elements++ = i;
115 *elements++ = i + 1;
116 *elements++ = i + row + 1;
117 */
118
119 void GL_Backend_AllocElementsArray(void)
120 {
121         if (varray_buf_elements3i)
122                 Mem_Free(varray_buf_elements3i);
123         varray_buf_elements3i = Mem_Alloc(gl_backend_mempool, mesh_maxelements * sizeof(GLuint));
124 }
125
126 void GL_Backend_FreeElementArray(void)
127 {
128         if (varray_buf_elements3i)
129                 Mem_Free(varray_buf_elements3i);
130         varray_buf_elements3i = NULL;
131 }
132
133 void GL_Backend_CheckCvars(void)
134 {
135         // 21760 is (65536 / 3) rounded off to a multiple of 128
136         if (gl_mesh_maxverts.integer < 1024)
137                 Cvar_SetValueQuick(&gl_mesh_maxverts, 1024);
138         if (gl_mesh_maxverts.integer > 21760)
139                 Cvar_SetValueQuick(&gl_mesh_maxverts, 21760);
140         if (gl_mesh_vertex_array_range.integer && !gl_support_var)
141                 Cvar_SetValueQuick(&gl_mesh_vertex_array_range, 0);
142         if (gl_mesh_vertex_array_range_readfrequency.value < 0)
143                 Cvar_SetValueQuick(&gl_mesh_vertex_array_range_readfrequency, 0);
144         if (gl_mesh_vertex_array_range_readfrequency.value > 1)
145                 Cvar_SetValueQuick(&gl_mesh_vertex_array_range_readfrequency, 1);
146         if (gl_mesh_vertex_array_range_writefrequency.value < 0)
147                 Cvar_SetValueQuick(&gl_mesh_vertex_array_range_writefrequency, 0);
148         if (gl_mesh_vertex_array_range_writefrequency.value > 1)
149                 Cvar_SetValueQuick(&gl_mesh_vertex_array_range_writefrequency, 1);
150         if (gl_mesh_vertex_array_range_priority.value < 0)
151                 Cvar_SetValueQuick(&gl_mesh_vertex_array_range_priority, 0);
152         if (gl_mesh_vertex_array_range_priority.value > 1)
153                 Cvar_SetValueQuick(&gl_mesh_vertex_array_range_priority, 1);
154 }
155
156 int polygonelements[768];
157
158 void GL_Backend_AllocArrays(void)
159 {
160         int i, size;
161         qbyte *data;
162
163         if (!gl_backend_mempool)
164         {
165                 gl_backend_mempool = Mem_AllocPool("GL_Backend");
166                 varray_buf_vertex3f = NULL;
167                 varray_buf_color4f = NULL;
168                 varray_buf_color4b = NULL;
169                 varray_buf_elements3i = NULL;
170                 for (i = 0;i < MAX_TEXTUREUNITS;i++)
171                         varray_buf_texcoord3f[i] = varray_buf_texcoord2f[i] = NULL;
172         }
173
174         mesh_maxverts = gl_mesh_maxverts.integer;
175         mesh_var = gl_mesh_vertex_array_range.integer;
176         mesh_var_readfrequency = gl_mesh_vertex_array_range_readfrequency.value;
177         mesh_var_writefrequency = gl_mesh_vertex_array_range_writefrequency.value;
178         mesh_var_priority = gl_mesh_vertex_array_range_priority.value;
179
180         if (varray_buf_vertex3f)
181                 VID_FreeVertexArrays(varray_buf_vertex3f);
182         varray_buf_vertex3f = NULL;
183         varray_buf_color4f = NULL;
184         varray_buf_color4b = NULL;
185         for (i = 0;i < MAX_TEXTUREUNITS;i++)
186                 varray_buf_texcoord3f[i] = varray_buf_texcoord2f[i] = NULL;
187
188         size = mesh_maxverts * (sizeof(float[3]) + sizeof(float[4]) + sizeof(qbyte[4]) + (sizeof(float[3]) + sizeof(float[2])) * backendunits);
189         data = VID_AllocVertexArrays(gl_backend_mempool, size, gl_mesh_vertex_array_range.integer, gl_mesh_vertex_array_range_readfrequency.value, gl_mesh_vertex_array_range_writefrequency.value, gl_mesh_vertex_array_range_priority.value);
190         varray_buf_vertex3f = (void *)data;data += sizeof(float[3]) * mesh_maxverts;
191         varray_buf_color4f = (void *)data;data += sizeof(float[4]) * mesh_maxverts;
192         for (i = 0;i < backendunits;i++)
193         {
194                 varray_buf_texcoord3f[i] = (void *)data;data += sizeof(float[3]) * mesh_maxverts;
195                 varray_buf_texcoord2f[i] = (void *)data;data += sizeof(float[2]) * mesh_maxverts;
196         }
197         for (;i < MAX_TEXTUREUNITS;i++)
198                 varray_buf_texcoord3f[i] = varray_buf_texcoord2f[i] = NULL;
199         varray_buf_color4b = (void *)data;data += sizeof(qbyte[4]) * mesh_maxverts;
200
201         GL_Backend_AllocElementsArray();
202
203         if (gl_support_var)
204         {
205                 CHECKGLERROR
206                 qglVertexArrayRangeNV(size, varray_buf_vertex3f);
207                 CHECKGLERROR
208                 qglEnableClientState(GL_VERTEX_ARRAY_RANGE_NV);
209                 CHECKGLERROR
210         }
211 }
212
213 void GL_Backend_FreeArrays(void)
214 {
215         int i;
216
217         if (gl_support_var)
218         {
219                 CHECKGLERROR
220                 qglDisableClientState(GL_VERTEX_ARRAY_RANGE_NV);
221                 CHECKGLERROR
222         }
223
224         if (varray_buf_vertex3f)
225                 VID_FreeVertexArrays(varray_buf_vertex3f);
226         varray_buf_vertex3f = NULL;
227         varray_buf_color4f = NULL;
228         varray_buf_color4b = NULL;
229         for (i = 0;i < MAX_TEXTUREUNITS;i++)
230                 varray_buf_texcoord3f[i] = varray_buf_texcoord2f[i] = NULL;
231         varray_buf_elements3i = NULL;
232
233         Mem_FreePool(&gl_backend_mempool);
234 }
235
236 static void gl_backend_start(void)
237 {
238         GL_Backend_CheckCvars();
239
240         Con_Printf("OpenGL Backend started with gl_mesh_maxverts %i\n", gl_mesh_maxverts.integer);
241         if (qglDrawRangeElements != NULL)
242         {
243                 CHECKGLERROR
244                 qglGetIntegerv(GL_MAX_ELEMENTS_VERTICES, &gl_maxdrawrangeelementsvertices);
245                 CHECKGLERROR
246                 qglGetIntegerv(GL_MAX_ELEMENTS_INDICES, &gl_maxdrawrangeelementsindices);
247                 CHECKGLERROR
248                 Con_Printf("glDrawRangeElements detected (max vertices %i, max indices %i)\n", gl_maxdrawrangeelementsvertices, gl_maxdrawrangeelementsindices);
249         }
250         if (strstr(gl_renderer, "3Dfx"))
251         {
252                 Con_Printf("3Dfx driver detected, forcing gl_mesh_floatcolors to 0 to prevent crashs\n");
253                 Cvar_SetValueQuick(&gl_mesh_floatcolors, 0);
254         }
255
256         backendunits = min(MAX_TEXTUREUNITS, gl_textureunits);
257
258         GL_Backend_AllocArrays();
259
260         backendactive = true;
261 }
262
263 static void gl_backend_shutdown(void)
264 {
265         backendunits = 0;
266         backendactive = false;
267
268         Con_Printf("OpenGL Backend shutting down\n");
269
270         GL_Backend_FreeArrays();
271 }
272
273 void GL_Backend_ResizeArrays(int numvertices)
274 {
275         Cvar_SetValueQuick(&gl_mesh_maxverts, numvertices);
276         GL_Backend_CheckCvars();
277         mesh_maxverts = gl_mesh_maxverts.integer;
278         GL_Backend_AllocArrays();
279 }
280
281 static void gl_backend_newmap(void)
282 {
283 }
284
285 void gl_backend_init(void)
286 {
287         int i;
288
289         for (i = 0;i < POLYGONELEMENTS_MAXPOINTS - 2;i++)
290         {
291                 polygonelements[i * 3 + 0] = 0;
292                 polygonelements[i * 3 + 1] = i + 1;
293                 polygonelements[i * 3 + 2] = i + 2;
294         }
295
296         Cvar_RegisterVariable(&r_render);
297         Cvar_RegisterVariable(&gl_dither);
298         Cvar_RegisterVariable(&gl_lockarrays);
299         Cvar_RegisterVariable(&gl_delayfinish);
300 #ifdef NORENDER
301         Cvar_SetValue("r_render", 0);
302 #endif
303
304         Cvar_RegisterVariable(&gl_mesh_maxverts);
305         Cvar_RegisterVariable(&gl_mesh_floatcolors);
306         Cvar_RegisterVariable(&gl_mesh_drawrangeelements);
307         Cvar_RegisterVariable(&gl_mesh_vertex_array_range);
308         Cvar_RegisterVariable(&gl_mesh_vertex_array_range_readfrequency);
309         Cvar_RegisterVariable(&gl_mesh_vertex_array_range_writefrequency);
310         Cvar_RegisterVariable(&gl_mesh_vertex_array_range_priority);
311         R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap);
312 }
313
314 void GL_SetupView_ViewPort (int x, int y, int width, int height)
315 {
316         if (!r_render.integer)
317                 return;
318
319         // y is weird beause OpenGL is bottom to top, we use top to bottom
320         qglViewport(x, vid.realheight - (y + height), width, height);
321         CHECKGLERROR
322 }
323
324 void GL_SetupView_Orientation_Identity (void)
325 {
326         Matrix4x4_CreateIdentity(&backend_viewmatrix);
327         memset(&backend_modelmatrix, 0, sizeof(backend_modelmatrix));
328 }
329
330 void GL_SetupView_Orientation_FromEntity (vec3_t origin, vec3_t angles)
331 {
332         Matrix4x4_CreateRotate(&backend_viewmatrix, -90, 1, 0, 0);
333         Matrix4x4_ConcatRotate(&backend_viewmatrix, 90, 0, 0, 1);
334         Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[2], 1, 0, 0);
335         Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[0], 0, 1, 0);
336         Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[1], 0, 0, 1);
337         Matrix4x4_ConcatTranslate(&backend_viewmatrix, -origin[0], -origin[1], -origin[2]);
338         memset(&backend_modelmatrix, 0, sizeof(backend_modelmatrix));
339 }
340
341 void GL_SetupView_Mode_Perspective (double fovx, double fovy, double zNear, double zFar)
342 {
343         double xmax, ymax;
344
345         if (!r_render.integer)
346                 return;
347
348         // set up viewpoint
349         qglMatrixMode(GL_PROJECTION);CHECKGLERROR
350         qglLoadIdentity();CHECKGLERROR
351         // pyramid slopes
352         xmax = zNear * tan(fovx * M_PI / 360.0);
353         ymax = zNear * tan(fovy * M_PI / 360.0);
354         // set view pyramid
355         qglFrustum(-xmax, xmax, -ymax, ymax, zNear, zFar);CHECKGLERROR
356         qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
357         GL_SetupView_Orientation_Identity();
358 }
359
360 void GL_SetupView_Mode_PerspectiveInfiniteFarClip (double fovx, double fovy, double zNear)
361 {
362         float nudge, m[16];
363
364         if (!r_render.integer)
365                 return;
366
367         // set up viewpoint
368         qglMatrixMode(GL_PROJECTION);CHECKGLERROR
369         qglLoadIdentity();CHECKGLERROR
370         // set view pyramid
371         nudge = 1.0 - 1.0 / (1<<23);
372         m[ 0] = 1.0 / tan(fovx * M_PI / 360.0);
373         m[ 1] = 0;
374         m[ 2] = 0;
375         m[ 3] = 0;
376         m[ 4] = 0;
377         m[ 5] = 1.0 / tan(fovy * M_PI / 360.0);
378         m[ 6] = 0;
379         m[ 7] = 0;
380         m[ 8] = 0;
381         m[ 9] = 0;
382         m[10] = -1 * nudge;
383         m[11] = -1 * nudge;
384         m[12] = 0;
385         m[13] = 0;
386         m[14] = -2 * zNear * nudge;
387         m[15] = 0;
388         qglLoadMatrixf(m);
389         qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
390         GL_SetupView_Orientation_Identity();
391         backend_projectmatrix.m[0][0] = m[0];
392         backend_projectmatrix.m[1][0] = m[1];
393         backend_projectmatrix.m[2][0] = m[2];
394         backend_projectmatrix.m[3][0] = m[3];
395         backend_projectmatrix.m[0][1] = m[4];
396         backend_projectmatrix.m[1][1] = m[5];
397         backend_projectmatrix.m[2][1] = m[6];
398         backend_projectmatrix.m[3][1] = m[7];
399         backend_projectmatrix.m[0][2] = m[8];
400         backend_projectmatrix.m[1][2] = m[9];
401         backend_projectmatrix.m[2][2] = m[10];
402         backend_projectmatrix.m[3][2] = m[11];
403         backend_projectmatrix.m[0][3] = m[12];
404         backend_projectmatrix.m[1][3] = m[13];
405         backend_projectmatrix.m[2][3] = m[14];
406         backend_projectmatrix.m[3][3] = m[15];
407 }
408
409 void GL_SetupView_Mode_Ortho (double x1, double y1, double x2, double y2, double zNear, double zFar)
410 {
411         if (!r_render.integer)
412                 return;
413
414         // set up viewpoint
415         qglMatrixMode(GL_PROJECTION);CHECKGLERROR
416         qglLoadIdentity();CHECKGLERROR
417         qglOrtho(x1, x2, y2, y1, zNear, zFar);
418         qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
419         GL_SetupView_Orientation_Identity();
420 }
421
422 typedef struct gltextureunit_s
423 {
424         int t1d, t2d, t3d, tcubemap;
425         int arrayenabled, arrayis3d;
426         float rgbscale, alphascale;
427         int combinergb, combinealpha;
428         // FIXME: add more combine stuff
429 }
430 gltextureunit_t;
431
432 static struct
433 {
434         int blendfunc1;
435         int blendfunc2;
436         int blend;
437         GLboolean depthmask;
438         int depthdisable;
439         int unit;
440         int clientunit;
441         gltextureunit_t units[MAX_TEXTUREUNITS];
442         int colorarray;
443 }
444 gl_state;
445
446 void GL_SetupTextureState(void)
447 {
448         int i;
449         gltextureunit_t *unit;
450         for (i = 0;i < backendunits;i++)
451         {
452                 if (qglActiveTexture)
453                         qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
454                 if (qglClientActiveTexture)
455                         qglClientActiveTexture(GL_TEXTURE0_ARB + (gl_state.clientunit = i));CHECKGLERROR
456                 unit = gl_state.units + i;
457                 unit->t1d = 0;
458                 unit->t2d = 0;
459                 unit->t3d = 0;
460                 unit->tcubemap = 0;
461                 unit->rgbscale = 1;
462                 unit->alphascale = 1;
463                 unit->combinergb = GL_MODULATE;
464                 unit->combinealpha = GL_MODULATE;
465                 unit->arrayenabled = false;
466                 unit->arrayis3d = false;
467                 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
468                 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), varray_buf_texcoord2f[i]);CHECKGLERROR
469                 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
470                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
471                 if (gl_texture3d)
472                 {
473                         qglDisable(GL_TEXTURE_3D);CHECKGLERROR
474                 }
475                 if (gl_texturecubemap)
476                 {
477                         qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
478                 }
479                 if (gl_combine.integer)
480                 {
481                         qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);CHECKGLERROR
482                         qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);CHECKGLERROR
483                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);CHECKGLERROR
484                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
485                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_CONSTANT_ARB);CHECKGLERROR
486                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
487                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
488                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);CHECKGLERROR
489                         qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);CHECKGLERROR
490                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);CHECKGLERROR
491                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
492                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, GL_CONSTANT_ARB);CHECKGLERROR
493                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
494                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
495                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
496                         qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
497                         qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
498                 }
499                 else
500                 {
501                         qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
502                 }
503         }
504 }
505
506 void GL_Backend_ResetState(void)
507 {
508         memset(&gl_state, 0, sizeof(gl_state));
509         gl_state.depthdisable = false;
510         gl_state.blendfunc1 = GL_ONE;
511         gl_state.blendfunc2 = GL_ZERO;
512         gl_state.blend = false;
513         gl_state.depthmask = GL_TRUE;
514         gl_state.colorarray = false;
515
516         qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
517         qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
518
519         qglEnable(GL_CULL_FACE);CHECKGLERROR
520         qglCullFace(GL_FRONT);CHECKGLERROR
521         qglEnable(GL_DEPTH_TEST);CHECKGLERROR
522         qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
523         qglDisable(GL_BLEND);CHECKGLERROR
524         qglDepthMask(gl_state.depthmask);CHECKGLERROR
525         qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), varray_buf_vertex3f);CHECKGLERROR
526         qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
527         if (gl_mesh_floatcolors.integer)
528         {
529                 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), varray_buf_color4f);CHECKGLERROR
530         }
531         else
532         {
533                 qglColorPointer(4, GL_UNSIGNED_BYTE, sizeof(qbyte[4]), varray_buf_color4b);CHECKGLERROR
534         }
535         GL_Color(1, 1, 1, 1);
536
537         GL_SetupTextureState();
538 }
539
540 void GL_UseColorArray(void)
541 {
542         if (!gl_state.colorarray)
543         {
544                 gl_state.colorarray = true;
545                 qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
546         }
547 }
548
549 void GL_Color(float cr, float cg, float cb, float ca)
550 {
551         if (gl_state.colorarray)
552         {
553                 gl_state.colorarray = false;
554                 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
555         }
556         qglColor4f(cr, cg, cb, ca);
557 }
558
559 void GL_TransformToScreen(const vec4_t in, vec4_t out)
560 {
561         vec4_t temp;
562         float iw;
563         Matrix4x4_Transform4 (&backend_viewmatrix, in, temp);
564         Matrix4x4_Transform4 (&backend_projectmatrix, temp, out);
565         iw = 1.0f / out[3];
566         out[0] = r_refdef.x + (out[0] * iw + 1.0f) * r_refdef.width * 0.5f;
567         out[1] = r_refdef.y + (out[1] * iw + 1.0f) * r_refdef.height * 0.5f;
568         out[2] = out[2] * iw;
569 }
570
571 // called at beginning of frame
572 void R_Mesh_Start(void)
573 {
574         BACKENDACTIVECHECK
575
576         CHECKGLERROR
577
578         GL_Backend_CheckCvars();
579         if (mesh_maxverts != gl_mesh_maxverts.integer
580          || mesh_var != gl_mesh_vertex_array_range.integer
581          || mesh_var_readfrequency != gl_mesh_vertex_array_range_readfrequency.value
582          || mesh_var_writefrequency != gl_mesh_vertex_array_range_writefrequency.value
583          || mesh_var_priority != gl_mesh_vertex_array_range_priority.value)
584                 GL_Backend_ResizeArrays(gl_mesh_maxverts.integer);
585
586         GL_Backend_ResetState();
587
588         if (mesh_var)
589         {
590                 CHECKGLERROR
591                 qglFlushVertexArrayRangeNV();
592                 CHECKGLERROR
593         }
594         varray_offset = 0;
595 }
596
597 int gl_backend_rebindtextures;
598
599 void GL_ConvertColorsFloatToByte(int numverts)
600 {
601         int i, k, total;
602         // LordHavoc: to avoid problems with aliasing (treating memory as two
603         // different types - exactly what this is doing), these must be volatile
604         // (or a union)
605         volatile int *icolor;
606         volatile float *fcolor;
607         GLubyte *bcolor;
608
609         total = numverts * 4;
610
611         // shift float to have 8bit fraction at base of number
612         fcolor = varray_buf_color4f;
613         for (i = 0;i < total;)
614         {
615                 fcolor[i    ] += 32768.0f;
616                 fcolor[i + 1] += 32768.0f;
617                 fcolor[i + 2] += 32768.0f;
618                 fcolor[i + 3] += 32768.0f;
619                 i += 4;
620         }
621
622         // then read as integer and kill float bits...
623         icolor = (int *)varray_buf_color4f;
624         bcolor = varray_buf_color4b;
625         for (i = 0;i < total;)
626         {
627                 k = icolor[i    ] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i    ] = (GLubyte) k;
628                 k = icolor[i + 1] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i + 1] = (GLubyte) k;
629                 k = icolor[i + 2] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i + 2] = (GLubyte) k;
630                 k = icolor[i + 3] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i + 3] = (GLubyte) k;
631                 i += 4;
632         }
633 }
634
635 /*
636 // enlarges geometry buffers if they are too small
637 void _R_Mesh_ResizeCheck(int numverts)
638 {
639         if (numverts > mesh_maxverts)
640         {
641                 BACKENDACTIVECHECK
642                 GL_Backend_ResizeArrays(numverts + 100);
643                 GL_Backend_ResetState();
644         }
645 }
646 */
647
648 void GL_Backend_RenumberElements(int numelements, const int *in, int offset)
649 {
650         int i;
651         for (i = 0;i < numelements;i++)
652                 varray_buf_elements3i[i] = in[i] + offset;
653 }
654
655 // gets geometry space for a mesh
656 void R_Mesh_GetSpace(int numverts)
657 {
658         int i;
659
660         varray_offset = varray_offsetnext;
661         if (varray_offset + numverts > mesh_maxverts)
662                 varray_offset = 0;
663         if (numverts > mesh_maxverts)
664         {
665                 BACKENDACTIVECHECK
666                 GL_Backend_ResizeArrays(numverts + 100);
667                 GL_Backend_ResetState();
668                 varray_offset = 0;
669         }
670
671         if (varray_offset == 0 && mesh_var)
672         {
673                 CHECKGLERROR
674                 qglFlushVertexArrayRangeNV();
675                 CHECKGLERROR
676         }
677
678         // for debugging
679         //if (!mesh_var)
680         //      varray_offset = rand() % (mesh_maxverts - numverts);
681
682         varray_vertex3f = varray_buf_vertex3f + varray_offset * 3;
683         varray_color4f = varray_buf_color4f + varray_offset * 4;
684         varray_color4b = varray_buf_color4b + varray_offset * 4;
685         for (i = 0;i < backendunits;i++)
686         {
687                 varray_texcoord3f[i] = varray_buf_texcoord3f[i] + varray_offset * 3;
688                 varray_texcoord2f[i] = varray_buf_texcoord2f[i] + varray_offset * 2;
689         }
690
691         varray_offsetnext = varray_offset + numverts;
692 }
693
694 // renders the current mesh
695 void R_Mesh_Draw(int numverts, int numtriangles, const int *elements)
696 {
697         int numelements;
698         if (numtriangles == 0 || numverts == 0)
699         {
700                 Con_Printf("R_Mesh_Draw(%d, %d, %08p);\n", numverts, numtriangles, elements);
701                 return;
702         }
703         numelements = numtriangles * 3;
704         if (mesh_maxelements < numelements)
705         {
706                 mesh_maxelements = numelements;
707                 GL_Backend_AllocElementsArray();
708         }
709         GL_Backend_RenumberElements(numelements, elements, varray_offset);
710         c_meshs++;
711         c_meshelements += numelements;
712         if (gl_state.colorarray && !gl_mesh_floatcolors.integer)
713                 GL_ConvertColorsFloatToByte(numverts);
714         if (r_render.integer)
715         {
716                 if (gl_supportslockarrays && gl_lockarrays.integer)
717                 {
718                         qglLockArraysEXT(varray_offset, numverts);
719                         CHECKGLERROR
720                         if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
721                         {
722                                 qglDrawRangeElements(GL_TRIANGLES, varray_offset, varray_offset + numverts, numelements, GL_UNSIGNED_INT, (const GLuint *) varray_buf_elements3i);
723                                 CHECKGLERROR
724                         }
725                         else
726                         {
727                                 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, (const GLuint *) varray_buf_elements3i);
728                                 CHECKGLERROR
729                         }
730                         qglUnlockArraysEXT();
731                         CHECKGLERROR
732                 }
733                 else
734                 {
735                         qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, (const GLuint *) varray_buf_elements3i);
736                         CHECKGLERROR
737                 }
738         }
739 }
740
741 // restores backend state, used when done with 3D rendering
742 void R_Mesh_Finish(void)
743 {
744         int i;
745         BACKENDACTIVECHECK
746         if (mesh_var)
747         {
748                 CHECKGLERROR
749                 qglFlushVertexArrayRangeNV();
750                 CHECKGLERROR
751         }
752         varray_offset = 0;
753
754         for (i = backendunits - 1;i >= 0;i--)
755         {
756                 if (qglActiveTexture)
757                         qglActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
758                 if (qglClientActiveTexture)
759                         qglClientActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
760                 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
761                 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
762                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
763                 if (gl_texture3d)
764                 {
765                         qglDisable(GL_TEXTURE_3D);CHECKGLERROR
766                 }
767                 if (gl_texturecubemap)
768                 {
769                         qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
770                 }
771                 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
772                 if (gl_combine.integer)
773                 {
774                         qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
775                         qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
776                 }
777         }
778         qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
779         qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
780
781         qglDisable(GL_BLEND);CHECKGLERROR
782         qglEnable(GL_DEPTH_TEST);CHECKGLERROR
783         qglDepthMask(GL_TRUE);CHECKGLERROR
784         qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);CHECKGLERROR
785 }
786
787 void R_Mesh_Matrix(const matrix4x4_t *matrix)
788 {
789         if (memcmp(matrix, &backend_modelmatrix, sizeof(matrix4x4_t)))
790         {
791                 backend_modelmatrix = *matrix;
792                 Matrix4x4_Concat(&backend_modelviewmatrix, &backend_viewmatrix, matrix);
793                 Matrix4x4_Transpose(&backend_glmodelviewmatrix, &backend_modelviewmatrix);
794                 qglLoadMatrixf(&backend_glmodelviewmatrix.m[0][0]);
795         }
796 }
797
798 // sets up the requested state
799 void R_Mesh_MainState(const rmeshstate_t *m)
800 {
801         BACKENDACTIVECHECK
802
803         if (gl_state.blendfunc1 != m->blendfunc1 || gl_state.blendfunc2 != m->blendfunc2)
804         {
805                 qglBlendFunc(gl_state.blendfunc1 = m->blendfunc1, gl_state.blendfunc2 = m->blendfunc2);CHECKGLERROR
806                 if (gl_state.blendfunc2 == GL_ZERO)
807                 {
808                         if (gl_state.blendfunc1 == GL_ONE)
809                         {
810                                 if (gl_state.blend)
811                                 {
812                                         gl_state.blend = 0;
813                                         qglDisable(GL_BLEND);CHECKGLERROR
814                                 }
815                         }
816                         else
817                         {
818                                 if (!gl_state.blend)
819                                 {
820                                         gl_state.blend = 1;
821                                         qglEnable(GL_BLEND);CHECKGLERROR
822                                 }
823                         }
824                 }
825                 else
826                 {
827                         if (!gl_state.blend)
828                         {
829                                 gl_state.blend = 1;
830                                 qglEnable(GL_BLEND);CHECKGLERROR
831                         }
832                 }
833         }
834         if (gl_state.depthdisable != m->depthdisable)
835         {
836                 gl_state.depthdisable = m->depthdisable;
837                 if (gl_state.depthdisable)
838                         qglDisable(GL_DEPTH_TEST);
839                 else
840                         qglEnable(GL_DEPTH_TEST);
841         }
842         if (gl_state.depthmask != (m->blendfunc2 == GL_ZERO || m->depthwrite))
843         {
844                 qglDepthMask(gl_state.depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite));CHECKGLERROR
845         }
846 }
847
848 void R_Mesh_TextureState(const rmeshstate_t *m)
849 {
850         int i, combinergb, combinealpha;
851         float scale;
852         gltextureunit_t *unit;
853
854         BACKENDACTIVECHECK
855
856         if (gl_backend_rebindtextures)
857         {
858                 gl_backend_rebindtextures = false;
859                 GL_SetupTextureState();
860         }
861
862         for (i = 0;i < backendunits;i++)
863         {
864                 unit = gl_state.units + i;
865                 if (unit->t1d != m->tex1d[i] || unit->t2d != m->tex[i] || unit->t3d != m->tex3d[i] || unit->tcubemap != m->texcubemap[i])
866                 {
867                         if (m->tex3d[i] || m->texcubemap[i])
868                         {
869                                 if (!unit->arrayis3d)
870                                 {
871                                         unit->arrayis3d = true;
872                                         if (gl_state.clientunit != i)
873                                         {
874                                                 qglClientActiveTexture(GL_TEXTURE0_ARB + (gl_state.clientunit = i));CHECKGLERROR
875                                         }
876                                         qglTexCoordPointer(3, GL_FLOAT, sizeof(float[3]), varray_buf_texcoord3f[i]);
877                                 }
878                                 if (!unit->arrayenabled)
879                                 {
880                                         unit->arrayenabled = true;
881                                         if (gl_state.clientunit != i)
882                                         {
883                                                 qglClientActiveTexture(GL_TEXTURE0_ARB + (gl_state.clientunit = i));CHECKGLERROR
884                                         }
885                                         qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
886                                 }
887                         }
888                         else if (m->tex1d[i] || m->tex[i])
889                         {
890                                 if (unit->arrayis3d)
891                                 {
892                                         unit->arrayis3d = false;
893                                         if (gl_state.clientunit != i)
894                                         {
895                                                 qglClientActiveTexture(GL_TEXTURE0_ARB + (gl_state.clientunit = i));CHECKGLERROR
896                                         }
897                                         qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), varray_buf_texcoord2f[i]);
898                                 }
899                                 if (!unit->arrayenabled)
900                                 {
901                                         unit->arrayenabled = true;
902                                         if (gl_state.clientunit != i)
903                                         {
904                                                 qglClientActiveTexture(GL_TEXTURE0_ARB + (gl_state.clientunit = i));CHECKGLERROR
905                                         }
906                                         qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
907                                 }
908                         }
909                         else
910                         {
911                                 if (unit->arrayenabled)
912                                 {
913                                         unit->arrayenabled = false;
914                                         if (gl_state.clientunit != i)
915                                         {
916                                                 qglClientActiveTexture(GL_TEXTURE0_ARB + (gl_state.clientunit = i));CHECKGLERROR
917                                         }
918                                         qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
919                                 }
920                         }
921                         if (unit->t1d != m->tex1d[i])
922                         {
923                                 if (gl_state.unit != i)
924                                 {
925                                         qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
926                                 }
927                                 if (m->tex1d[i])
928                                 {
929                                         if (unit->t1d == 0)
930                                                 qglEnable(GL_TEXTURE_1D);CHECKGLERROR
931                                 }
932                                 else
933                                 {
934                                         if (unit->t1d)
935                                                 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
936                                 }
937                                 qglBindTexture(GL_TEXTURE_1D, (unit->t1d = m->tex1d[i]));CHECKGLERROR
938                         }
939                         if (unit->t2d != m->tex[i])
940                         {
941                                 if (gl_state.unit != i)
942                                 {
943                                         qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
944                                 }
945                                 if (m->tex[i])
946                                 {
947                                         if (unit->t2d == 0)
948                                                 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
949                                 }
950                                 else
951                                 {
952                                         if (unit->t2d)
953                                                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
954                                 }
955                                 qglBindTexture(GL_TEXTURE_2D, (unit->t2d = m->tex[i]));CHECKGLERROR
956                         }
957                         if (unit->t3d != m->tex3d[i])
958                         {
959                                 if (gl_state.unit != i)
960                                 {
961                                         qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
962                                 }
963                                 if (m->tex3d[i])
964                                 {
965                                         if (unit->t3d == 0)
966                                                 qglEnable(GL_TEXTURE_3D);CHECKGLERROR
967                                 }
968                                 else
969                                 {
970                                         if (unit->t3d)
971                                                 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
972                                 }
973                                 qglBindTexture(GL_TEXTURE_3D, (unit->t3d = m->tex3d[i]));CHECKGLERROR
974                         }
975                         if (unit->tcubemap != m->texcubemap[i])
976                         {
977                                 if (gl_state.unit != i)
978                                 {
979                                         qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
980                                 }
981                                 if (m->texcubemap[i])
982                                 {
983                                         if (unit->tcubemap == 0)
984                                                 qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
985                                 }
986                                 else
987                                 {
988                                         if (unit->tcubemap)
989                                                 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
990                                 }
991                                 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, (unit->tcubemap = m->texcubemap[i]));CHECKGLERROR
992                         }
993                 }
994                 combinergb = m->texcombinergb[i];
995                 if (!combinergb)
996                         combinergb = GL_MODULATE;
997                 if (unit->combinergb != combinergb)
998                 {
999                         if (gl_state.unit != i)
1000                         {
1001                                 qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
1002                         }
1003                         unit->combinergb = combinergb;
1004                         if (gl_combine.integer)
1005                         {
1006                                 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
1007                         }
1008                         else
1009                         {
1010                                 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combinergb);CHECKGLERROR
1011                         }
1012                 }
1013                 combinealpha = m->texcombinealpha[i];
1014                 if (!combinealpha)
1015                         combinealpha = GL_MODULATE;
1016                 if (unit->combinealpha != combinealpha)
1017                 {
1018                         if (gl_state.unit != i)
1019                         {
1020                                 qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
1021                         }
1022                         unit->combinealpha = combinealpha;
1023                         if (gl_combine.integer)
1024                         {
1025                                 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
1026                         }
1027                 }
1028                 scale = max(m->texrgbscale[i], 1);
1029                 if (gl_state.units[i].rgbscale != scale)
1030                 {
1031                         if (gl_state.unit != i)
1032                         {
1033                                 qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
1034                         }
1035                         qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (gl_state.units[i].rgbscale = scale));CHECKGLERROR
1036                 }
1037                 scale = max(m->texalphascale[i], 1);
1038                 if (gl_state.units[i].alphascale != scale)
1039                 {
1040                         if (gl_state.unit != i)
1041                         {
1042                                 qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
1043                         }
1044                         qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, (gl_state.units[i].alphascale = scale));CHECKGLERROR
1045                 }
1046         }
1047 }
1048
1049 void R_Mesh_State(const rmeshstate_t *m)
1050 {
1051         R_Mesh_MainState(m);
1052         R_Mesh_TextureState(m);
1053 }
1054
1055 /*
1056 ==============================================================================
1057
1058                                                 SCREEN SHOTS
1059
1060 ==============================================================================
1061 */
1062
1063 qboolean SCR_ScreenShot(char *filename, int x, int y, int width, int height)
1064 {
1065         qboolean ret;
1066         int i, j;
1067         qbyte *buffer;
1068
1069         if (!r_render.integer)
1070                 return false;
1071
1072         buffer = Mem_Alloc(tempmempool, width*height*3);
1073         qglReadPixels (x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer);
1074         CHECKGLERROR
1075
1076         // LordHavoc: compensate for v_overbrightbits when using hardware gamma
1077         if (v_hwgamma.integer)
1078         {
1079                 for (i = 0;i < width * height * 3;i++)
1080                 {
1081                         j = buffer[i] << v_overbrightbits.integer;
1082                         buffer[i] = (qbyte) (bound(0, j, 255));
1083                 }
1084         }
1085
1086         ret = Image_WriteTGARGB_preflipped(filename, width, height, buffer);
1087
1088         Mem_Free(buffer);
1089         return ret;
1090 }
1091
1092 //=============================================================================
1093
1094 void R_ClearScreen(void)
1095 {
1096         if (r_render.integer)
1097         {
1098                 // clear to black
1099                 qglClearColor(0,0,0,0);CHECKGLERROR
1100                 qglClearDepth(1);CHECKGLERROR
1101                 if (gl_stencil)
1102                 {
1103                         // LordHavoc: we use a stencil centered around 128 instead of 0,
1104                         // to avoid clamping interfering with strange shadow volume
1105                         // drawing orders
1106                         qglClearStencil(128);CHECKGLERROR
1107                 }
1108                 // clear the screen
1109                 qglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | (gl_stencil ? GL_STENCIL_BUFFER_BIT : 0));CHECKGLERROR
1110                 // set dithering mode
1111                 if (gl_dither.integer)
1112                 {
1113                         qglEnable(GL_DITHER);CHECKGLERROR
1114                 }
1115                 else
1116                 {
1117                         qglDisable(GL_DITHER);CHECKGLERROR
1118                 }
1119         }
1120 }
1121
1122 /*
1123 ==================
1124 SCR_UpdateScreen
1125
1126 This is called every frame, and can also be called explicitly to flush
1127 text to the screen.
1128 ==================
1129 */
1130 void SCR_UpdateScreen (void)
1131 {
1132         if (gl_delayfinish.integer)
1133         {
1134                 VID_Finish ();
1135
1136                 R_TimeReport("finish");
1137         }
1138
1139         if (r_textureunits.integer > gl_textureunits)
1140                 Cvar_SetValueQuick(&r_textureunits, gl_textureunits);
1141         if (r_textureunits.integer < 1)
1142                 Cvar_SetValueQuick(&r_textureunits, 1);
1143
1144         if (gl_combine.integer && (!gl_combine_extension || r_textureunits.integer < 2))
1145                 Cvar_SetValueQuick(&gl_combine, 0);
1146
1147         // lighting scale
1148         r_colorscale = 1.0f / (float) (1 << v_overbrightbits.integer);
1149
1150         // lightmaps only
1151         r_lightmapscalebit = v_overbrightbits.integer;
1152         if (gl_combine.integer && r_textureunits.integer > 1)
1153                 r_lightmapscalebit += 2;
1154
1155         R_TimeReport("setup");
1156
1157         R_ClearScreen();
1158
1159         R_TimeReport("clear");
1160
1161         if (scr_conlines < vid.conheight && cls.signon == SIGNONS)
1162                 R_RenderView();
1163
1164         // draw 2D stuff
1165         R_DrawQueue();
1166
1167         if (gl_delayfinish.integer)
1168         {
1169                 // tell driver to commit it's partially full geometry queue to the rendering queue
1170                 // (this doesn't wait for the commands themselves to complete)
1171                 qglFlush();
1172         }
1173         else
1174         {
1175                 VID_Finish ();
1176
1177                 R_TimeReport("finish");
1178         }
1179 }
1180
1181 // utility functions
1182
1183 void R_Mesh_CopyVertex3f(const float *vertex3f, int numverts)
1184 {
1185         if (mesh_var)
1186         {
1187                 float *out = varray_vertex3f;
1188                 while (--numverts)
1189                 {
1190                         *out++ = *vertex3f++;
1191                         *out++ = *vertex3f++;
1192                         *out++ = *vertex3f++;
1193                 }
1194         }
1195         else
1196                 memcpy(varray_vertex3f, vertex3f, numverts * sizeof(float[3]));
1197 }
1198
1199 void R_Mesh_CopyTexCoord2f(int tmu, const float *texcoord2f, int numverts)
1200 {
1201         if (mesh_var)
1202         {
1203                 float *out = varray_texcoord2f[tmu];
1204                 while (--numverts)
1205                 {
1206                         *out++ = *texcoord2f++;
1207                         *out++ = *texcoord2f++;
1208                 }
1209         }
1210         else
1211                 memcpy(varray_texcoord2f[tmu], texcoord2f, numverts * sizeof(float[2]));
1212 }
1213
1214 void R_Mesh_CopyColor4f(const float *color4f, int numverts)
1215 {
1216         if (mesh_var)
1217         {
1218                 float *out = varray_color4f;
1219                 while (--numverts)
1220                 {
1221                         *out++ = *color4f++;
1222                         *out++ = *color4f++;
1223                         *out++ = *color4f++;
1224                         *out++ = *color4f++;
1225                 }
1226         }
1227         else
1228                 memcpy(varray_color4f, color4f, numverts * sizeof(float[4]));
1229 }
1230
1231