corrected function names reported in various backend not active errors (by using...
[xonotic/darkplaces.git] / gl_backend.c
1
2 #include "quakedef.h"
3
4 cvar_t gl_mesh_maxtriangles = {0, "gl_mesh_maxtriangles", "1024"};
5 cvar_t gl_mesh_floatcolors = {0, "gl_mesh_floatcolors", "1"};
6 cvar_t gl_mesh_drawmode = {CVAR_SAVE, "gl_mesh_drawmode", "3"};
7
8 cvar_t r_render = {0, "r_render", "1"};
9 cvar_t gl_dither = {CVAR_SAVE, "gl_dither", "1"}; // whether or not to use dithering
10 cvar_t gl_lockarrays = {0, "gl_lockarrays", "1"};
11
12 // this is used to increase gl_mesh_maxtriangles automatically if a mesh was
13 // too large for the buffers in the previous frame
14 int overflowedverts = 0;
15
16 int gl_maxdrawrangeelementsvertices;
17 int gl_maxdrawrangeelementsindices;
18
19 #ifdef DEBUGGL
20 int errornumber = 0;
21
22 void GL_PrintError(int errornumber, char *filename, int linenumber)
23 {
24         switch(errornumber)
25         {
26 #ifdef GL_INVALID_ENUM
27         case GL_INVALID_ENUM:
28                 Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
29                 break;
30 #endif
31 #ifdef GL_INVALID_VALUE
32         case GL_INVALID_VALUE:
33                 Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
34                 break;
35 #endif
36 #ifdef GL_INVALID_OPERATION
37         case GL_INVALID_OPERATION:
38                 Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
39                 break;
40 #endif
41 #ifdef GL_STACK_OVERFLOW
42         case GL_STACK_OVERFLOW:
43                 Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
44                 break;
45 #endif
46 #ifdef GL_STACK_UNDERFLOW
47         case GL_STACK_UNDERFLOW:
48                 Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
49                 break;
50 #endif
51 #ifdef GL_OUT_OF_MEMORY
52         case GL_OUT_OF_MEMORY:
53                 Con_Printf("GL_OUT_OF_MEMORY at %s:%i\n", filename, linenumber);
54                 break;
55 #endif
56 #ifdef GL_TABLE_TOO_LARGE
57     case GL_TABLE_TOO_LARGE:
58                 Con_Printf("GL_TABLE_TOO_LARGE at %s:%i\n", filename, linenumber);
59                 break;
60 #endif
61         default:
62                 Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
63                 break;
64         }
65 }
66 #endif
67
68 #define BACKENDACTIVECHECK if (!backendactive) Sys_Error(__func__ " called when backend is not active\n");
69
70 float r_mesh_farclip;
71
72 static float viewdist;
73 // sign bits (true if negative) for vpn[] entries, so quick integer compares can be used instead of float compares
74 static int vpnbit0, vpnbit1, vpnbit2;
75
76 int c_meshs, c_meshtris;
77
78 int lightscalebit;
79 float lightscale;
80 float overbrightscale;
81
82 void SCR_ScreenShot_f (void);
83
84 // these are externally accessible
85 float mesh_colorscale;
86 int *varray_element;
87 float *varray_vertex;
88 float *varray_color;
89 float *varray_texcoord[MAX_TEXTUREUNITS];
90 int mesh_maxtris;
91 int mesh_maxverts; // always mesh_maxtris * 3
92
93 static matrix4x4_t backendmatrix;
94
95 static int backendunits, backendactive;
96 static qbyte *varray_bcolor;
97 static mempool_t *gl_backend_mempool;
98
99 void GL_Backend_AllocArrays(void)
100 {
101         int i;
102
103         if (!gl_backend_mempool)
104                 gl_backend_mempool = Mem_AllocPool("GL_Backend");
105
106         mesh_maxverts = mesh_maxtris * 3;
107
108         varray_element = Mem_Alloc(gl_backend_mempool, mesh_maxtris * sizeof(int[3]));
109         varray_vertex = Mem_Alloc(gl_backend_mempool, mesh_maxverts * sizeof(float[4]));
110         varray_color = Mem_Alloc(gl_backend_mempool, mesh_maxverts * sizeof(float[4]));
111         varray_bcolor = Mem_Alloc(gl_backend_mempool, mesh_maxverts * sizeof(qbyte[4]));
112         for (i = 0;i < backendunits;i++)
113                 varray_texcoord[i] = Mem_Alloc(gl_backend_mempool, mesh_maxverts * sizeof(float[2]));
114         for (;i < MAX_TEXTUREUNITS;i++)
115                 varray_texcoord[i] = NULL;
116 }
117
118 void GL_Backend_FreeArrays(int resizingbuffers)
119 {
120         int i;
121         if (resizingbuffers)
122                 Mem_EmptyPool(gl_backend_mempool);
123         else
124                 Mem_FreePool(&gl_backend_mempool);
125         varray_element = NULL;
126         varray_vertex = NULL;
127         varray_color = NULL;
128         varray_bcolor = NULL;
129         for (i = 0;i < MAX_TEXTUREUNITS;i++)
130                 varray_texcoord[i] = NULL;
131 }
132
133 static void gl_backend_start(void)
134 {
135         Con_Printf("OpenGL Backend started with gl_mesh_maxtriangles %i\n", gl_mesh_maxtriangles.integer);
136         if (qglDrawRangeElements != NULL)
137         {
138                 qglGetIntegerv(GL_MAX_ELEMENTS_VERTICES, &gl_maxdrawrangeelementsvertices);
139                 qglGetIntegerv(GL_MAX_ELEMENTS_INDICES, &gl_maxdrawrangeelementsindices);
140                 CHECKGLERROR
141                 Con_Printf("glDrawRangeElements detected (max vertices %i, max indices %i)\n", gl_maxdrawrangeelementsvertices, gl_maxdrawrangeelementsindices);
142         }
143         if (strstr(gl_renderer, "3Dfx"))
144         {
145                 Con_Printf("3Dfx driver detected, forcing gl_mesh_floatcolors to 0 to prevent crashs\n");
146                 Cvar_SetValueQuick(&gl_mesh_floatcolors, 0);
147         }
148
149         backendunits = min(MAX_TEXTUREUNITS, gl_textureunits);
150
151         GL_Backend_AllocArrays();
152
153         backendactive = true;
154 }
155
156 static void gl_backend_shutdown(void)
157 {
158         backendunits = 0;
159         backendactive = false;
160
161         Con_Printf("OpenGL Backend shutting down\n");
162
163         GL_Backend_FreeArrays(false);
164 }
165
166 void GL_Backend_CheckCvars(void)
167 {
168         if (gl_mesh_drawmode.integer < 0)
169                 Cvar_SetValueQuick(&gl_mesh_drawmode, 0);
170         if (gl_mesh_drawmode.integer > 3)
171                 Cvar_SetValueQuick(&gl_mesh_drawmode, 3);
172
173         // change drawmode 3 to 2 if 3 won't work
174         if (gl_mesh_drawmode.integer >= 3 && qglDrawRangeElements == NULL)
175                 Cvar_SetValueQuick(&gl_mesh_drawmode, 2);
176
177         // 21760 is (65536 / 3) rounded off to a multiple of 128
178         if (gl_mesh_maxtriangles.integer < 1024)
179                 Cvar_SetValueQuick(&gl_mesh_maxtriangles, 1024);
180         if (gl_mesh_maxtriangles.integer > 21760)
181                 Cvar_SetValueQuick(&gl_mesh_maxtriangles, 21760);
182 }
183
184 void GL_Backend_ResizeArrays(int numtriangles)
185 {
186         Cvar_SetValueQuick(&gl_mesh_maxtriangles, numtriangles);
187         GL_Backend_CheckCvars();
188         mesh_maxtris = gl_mesh_maxtriangles.integer;
189         GL_Backend_FreeArrays(true);
190         GL_Backend_AllocArrays();
191 }
192
193 static void gl_backend_newmap(void)
194 {
195 }
196
197 void gl_backend_init(void)
198 {
199         Cvar_RegisterVariable(&r_render);
200         Cvar_RegisterVariable(&gl_dither);
201         Cvar_RegisterVariable(&gl_lockarrays);
202 #ifdef NORENDER
203         Cvar_SetValue("r_render", 0);
204 #endif
205
206         Cvar_RegisterVariable(&gl_mesh_maxtriangles);
207         Cvar_RegisterVariable(&gl_mesh_floatcolors);
208         Cvar_RegisterVariable(&gl_mesh_drawmode);
209         GL_Backend_CheckCvars();
210         R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap);
211 }
212
213 int arraylocked = false;
214
215 void GL_LockArray(int first, int count)
216 {
217         if (!arraylocked && gl_supportslockarrays && gl_lockarrays.integer && gl_mesh_drawmode.integer > 0)
218         {
219                 qglLockArraysEXT(first, count);
220                 CHECKGLERROR
221                 arraylocked = true;
222         }
223 }
224
225 void GL_UnlockArray(void)
226 {
227         if (arraylocked)
228         {
229                 qglUnlockArraysEXT();
230                 CHECKGLERROR
231                 arraylocked = false;
232         }
233 }
234
235 /*
236 =============
237 GL_SetupFrame
238 =============
239 */
240 static void GL_SetupFrame (void)
241 {
242         double xmax, ymax;
243         double fovx, fovy, zNear, zFar, aspect;
244
245         if (!r_render.integer)
246                 return;
247
248         qglDepthFunc (GL_LEQUAL);CHECKGLERROR
249
250         // set up viewpoint
251         qglMatrixMode(GL_PROJECTION);CHECKGLERROR
252         qglLoadIdentity ();CHECKGLERROR
253
254         // y is weird beause OpenGL is bottom to top, we use top to bottom
255         qglViewport(r_refdef.x, vid.realheight - (r_refdef.y + r_refdef.height), r_refdef.width, r_refdef.height);CHECKGLERROR
256
257         // depth range
258         zNear = 1.0;
259         zFar = r_mesh_farclip;
260         if (zFar < 64)
261                 zFar = 64;
262
263         // fov angles
264         fovx = r_refdef.fov_x;
265         fovy = r_refdef.fov_y;
266         aspect = r_refdef.width / r_refdef.height;
267
268         // pyramid slopes
269         xmax = zNear * tan(fovx * M_PI / 360.0) * aspect;
270         ymax = zNear * tan(fovy * M_PI / 360.0);
271
272         // set view pyramid
273         qglFrustum(-xmax, xmax, -ymax, ymax, zNear, zFar);CHECKGLERROR
274
275         qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
276         qglLoadIdentity ();CHECKGLERROR
277
278         // put Z going up
279         qglRotatef (-90,  1, 0, 0);CHECKGLERROR
280         qglRotatef (90,  0, 0, 1);CHECKGLERROR
281         // camera rotation
282         qglRotatef (-r_refdef.viewangles[2],  1, 0, 0);CHECKGLERROR
283         qglRotatef (-r_refdef.viewangles[0],  0, 1, 0);CHECKGLERROR
284         qglRotatef (-r_refdef.viewangles[1],  0, 0, 1);CHECKGLERROR
285         // camera location
286         qglTranslatef (-r_refdef.vieworg[0],  -r_refdef.vieworg[1],  -r_refdef.vieworg[2]);CHECKGLERROR
287 }
288
289 static struct
290 {
291         int blendfunc1;
292         int blendfunc2;
293         int blend;
294         GLboolean depthmask;
295         int depthdisable;
296         int unit;
297         int clientunit;
298         int texture[MAX_TEXTUREUNITS];
299         float texturergbscale[MAX_TEXTUREUNITS];
300 }
301 gl_state;
302
303 void GL_SetupTextureState(void)
304 {
305         int i;
306         if (backendunits > 1)
307         {
308                 for (i = 0;i < backendunits;i++)
309                 {
310                         qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
311                         qglBindTexture(GL_TEXTURE_2D, gl_state.texture[i]);CHECKGLERROR
312                         if (gl_combine.integer)
313                         {
314                                 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);CHECKGLERROR
315                                 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);CHECKGLERROR
316                                 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);CHECKGLERROR
317                                 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
318                                 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_CONSTANT_ARB);CHECKGLERROR
319                                 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
320                                 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
321                                 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);CHECKGLERROR
322                                 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);CHECKGLERROR
323                                 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);CHECKGLERROR
324                                 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
325                                 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, GL_CONSTANT_ARB);CHECKGLERROR
326                                 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
327                                 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
328                                 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
329                                 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, gl_state.texturergbscale[i]);CHECKGLERROR
330                                 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
331                         }
332                         else
333                         {
334                                 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
335                         }
336                         if (gl_state.texture[i])
337                         {
338                                 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
339                         }
340                         else
341                         {
342                                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
343                         }
344                         if (gl_mesh_drawmode.integer > 0)
345                         {
346                                 qglClientActiveTexture(GL_TEXTURE0_ARB + (gl_state.clientunit = i));CHECKGLERROR
347                                 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), varray_texcoord[i]);CHECKGLERROR
348                                 if (gl_state.texture[i])
349                                 {
350                                         qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
351                                 }
352                                 else
353                                 {
354                                         qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
355                                 }
356                         }
357                 }
358         }
359         else
360         {
361                 qglBindTexture(GL_TEXTURE_2D, gl_state.texture[0]);CHECKGLERROR
362                 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
363                 if (gl_state.texture[0])
364                 {
365                         qglEnable(GL_TEXTURE_2D);CHECKGLERROR
366                 }
367                 else
368                 {
369                         qglDisable(GL_TEXTURE_2D);CHECKGLERROR
370                 }
371                 if (gl_mesh_drawmode.integer > 0)
372                 {
373                         qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), varray_texcoord[0]);CHECKGLERROR
374                         if (gl_state.texture[0])
375                         {
376                                 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
377                         }
378                         else
379                         {
380                                 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
381                         }
382                 }
383         }
384 }
385
386 int usedarrays;
387 void GL_Backend_ResetState(void)
388 {
389         int i;
390         gl_state.unit = 0;
391         gl_state.clientunit = 0;
392
393         for (i = 0;i < backendunits;i++)
394         {
395                 gl_state.texture[i] = 0;
396                 gl_state.texturergbscale[i] = 1;
397         }
398
399         qglEnable(GL_CULL_FACE);CHECKGLERROR
400         qglCullFace(GL_FRONT);CHECKGLERROR
401
402         gl_state.depthdisable = false;
403         qglEnable(GL_DEPTH_TEST);CHECKGLERROR
404
405         gl_state.blendfunc1 = GL_ONE;
406         gl_state.blendfunc2 = GL_ZERO;
407         qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
408
409         gl_state.blend = 0;
410         qglDisable(GL_BLEND);CHECKGLERROR
411
412         gl_state.depthmask = GL_TRUE;
413         qglDepthMask(gl_state.depthmask);CHECKGLERROR
414
415         usedarrays = false;
416         if (gl_mesh_drawmode.integer > 0)
417         {
418                 usedarrays = true;
419                 qglVertexPointer(3, GL_FLOAT, sizeof(float[4]), varray_vertex);CHECKGLERROR
420                 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
421                 if (gl_mesh_floatcolors.integer)
422                 {
423                         qglColorPointer(4, GL_FLOAT, sizeof(float[4]), varray_color);CHECKGLERROR
424                 }
425                 else
426                 {
427                         qglColorPointer(4, GL_UNSIGNED_BYTE, sizeof(qbyte[4]), varray_bcolor);CHECKGLERROR
428                 }
429                 qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
430         }
431
432         GL_SetupTextureState();
433 }
434
435 // called at beginning of frame
436 void R_Mesh_Start(float farclip)
437 {
438         BACKENDACTIVECHECK
439
440         CHECKGLERROR
441
442         r_mesh_farclip = farclip;
443         viewdist = DotProduct(r_origin, vpn);
444         vpnbit0 = vpn[0] < 0;
445         vpnbit1 = vpn[1] < 0;
446         vpnbit2 = vpn[2] < 0;
447
448         c_meshs = 0;
449         c_meshtris = 0;
450
451         GL_Backend_CheckCvars();
452         if (mesh_maxtris != gl_mesh_maxtriangles.integer)
453                 GL_Backend_ResizeArrays(gl_mesh_maxtriangles.integer);
454
455         GL_SetupFrame();
456
457         GL_Backend_ResetState();
458 }
459
460 int gl_backend_rebindtextures;
461
462 void GL_ConvertColorsFloatToByte(int numverts)
463 {
464         int i, k, total;
465         // LordHavoc: to avoid problems with aliasing (treating memory as two
466         // different types - exactly what this is doing), these must be volatile
467         // (or a union)
468         volatile int *icolor;
469         volatile float *fcolor;
470         qbyte *bcolor;
471
472         total = numverts * 4;
473
474         // shift float to have 8bit fraction at base of number
475         fcolor = varray_color;
476         for (i = 0;i < total;)
477         {
478                 fcolor[i    ] += 32768.0f;
479                 fcolor[i + 1] += 32768.0f;
480                 fcolor[i + 2] += 32768.0f;
481                 fcolor[i + 3] += 32768.0f;
482                 i += 4;
483         }
484
485         // then read as integer and kill float bits...
486         icolor = (int *)varray_color;
487         bcolor = varray_bcolor;
488         for (i = 0;i < total;)
489         {
490                 k = icolor[i    ] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i    ] = (qbyte) k;
491                 k = icolor[i + 1] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i + 1] = (qbyte) k;
492                 k = icolor[i + 2] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i + 2] = (qbyte) k;
493                 k = icolor[i + 3] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i + 3] = (qbyte) k;
494                 i += 4;
495         }
496 }
497
498 void GL_TransformVertices(int numverts)
499 {
500         int i;
501         float m[12], tempv[4], *v;
502         m[0] = backendmatrix.m[0][0];
503         m[1] = backendmatrix.m[0][1];
504         m[2] = backendmatrix.m[0][2];
505         m[3] = backendmatrix.m[0][3];
506         m[4] = backendmatrix.m[1][0];
507         m[5] = backendmatrix.m[1][1];
508         m[6] = backendmatrix.m[1][2];
509         m[7] = backendmatrix.m[1][3];
510         m[8] = backendmatrix.m[2][0];
511         m[9] = backendmatrix.m[2][1];
512         m[10] = backendmatrix.m[2][2];
513         m[11] = backendmatrix.m[2][3];
514         for (i = 0, v = varray_vertex;i < numverts;i++, v += 4)
515         {
516                 VectorCopy(v, tempv);
517                 v[0] = tempv[0] * m[0] + tempv[1] * m[1] + tempv[2] * m[2] + m[3];
518                 v[1] = tempv[0] * m[4] + tempv[1] * m[5] + tempv[2] * m[6] + m[7];
519                 v[2] = tempv[0] * m[8] + tempv[1] * m[9] + tempv[2] * m[10] + m[11];
520         }
521 }
522
523 void GL_DrawRangeElements(int firstvert, int endvert, int indexcount, GLuint *index)
524 {
525         unsigned int i, j, in;
526         qbyte *c;
527         float *v;
528         GL_LockArray(firstvert, endvert - firstvert);
529         if (gl_mesh_drawmode.integer >= 3/* && (endvert - firstvert) <= gl_maxdrawrangeelementsvertices && (indexcount) <= gl_maxdrawrangeelementsindices*/)
530         {
531                 // GL 1.2 or GL 1.1 with extension
532                 qglDrawRangeElements(GL_TRIANGLES, firstvert, endvert, indexcount, GL_UNSIGNED_INT, index);
533                 CHECKGLERROR
534         }
535         else if (gl_mesh_drawmode.integer >= 2)
536         {
537                 // GL 1.1
538                 qglDrawElements(GL_TRIANGLES, indexcount, GL_UNSIGNED_INT, index);
539                 CHECKGLERROR
540         }
541         else if (gl_mesh_drawmode.integer >= 1)
542         {
543                 // GL 1.1
544                 // feed it manually using glArrayElement
545                 qglBegin(GL_TRIANGLES);
546                 for (i = 0;i < indexcount;i++)
547                         qglArrayElement(index[i]);
548                 qglEnd();
549                 CHECKGLERROR
550         }
551         else
552         {
553                 // GL 1.1 but not using vertex arrays - 3dfx glquake minigl driver
554                 // feed it manually
555                 qglBegin(GL_TRIANGLES);
556                 if (gl_state.texture[1]) // if the mesh uses multiple textures
557                 {
558                         // the minigl doesn't have this (because it does not have ARB_multitexture)
559                         for (i = 0;i < indexcount;i++)
560                         {
561                                 in = index[i];
562                                 c = varray_bcolor + in * 4;
563                                 qglColor4ub(c[0], c[1], c[2], c[3]);
564                                 for (j = 0;j < backendunits;j++)
565                                 {
566                                         if (gl_state.texture[j])
567                                         {
568                                                 v = varray_texcoord[j] + in * 2;
569                                                 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, v[0], v[1]);
570                                         }
571                                 }
572                                 v = varray_vertex + in * 4;
573                                 qglVertex3f(v[0], v[1], v[2]);
574                         }
575                 }
576                 else
577                 {
578                         for (i = 0;i < indexcount;i++)
579                         {
580                                 in = index[i];
581                                 c = varray_bcolor + in * 4;
582                                 qglColor4ub(c[0], c[1], c[2], c[3]);
583                                 if (gl_state.texture[0])
584                                 {
585                                         v = varray_texcoord[j] + in * 2;
586                                         qglTexCoord2f(v[0], v[1]);
587                                 }
588                                 v = varray_vertex + in * 4;
589                                 qglVertex3f(v[0], v[1], v[2]);
590                         }
591                 }
592                 qglEnd();
593                 CHECKGLERROR
594         }
595         GL_UnlockArray();
596 }
597
598 // enlarges geometry buffers if they are too small
599 void _R_Mesh_ResizeCheck(int numverts, int numtriangles)
600 {
601         if (numtriangles > mesh_maxtris || numverts > mesh_maxverts)
602         {
603                 BACKENDACTIVECHECK
604                 GL_Backend_ResizeArrays(max(numtriangles, (numverts + 2) / 3) + 100);
605                 GL_Backend_ResetState();
606         }
607 }
608
609 // renders the mesh
610 void R_Mesh_Draw(int numverts, int numtriangles)
611 {
612         BACKENDACTIVECHECK
613
614         c_meshs++;
615         c_meshtris += numtriangles;
616
617         CHECKGLERROR
618
619         // drawmode 0 always uses byte colors
620         if (!gl_mesh_floatcolors.integer || gl_mesh_drawmode.integer <= 0)
621                 GL_ConvertColorsFloatToByte(numverts);
622         GL_TransformVertices(numverts);
623         if (!r_render.integer)
624                 return;
625         GL_DrawRangeElements(0, numverts, numtriangles * 3, varray_element);
626 }
627
628 // restores backend state, used when done with 3D rendering
629 void R_Mesh_Finish(void)
630 {
631         int i;
632         BACKENDACTIVECHECK
633
634         if (backendunits > 1)
635         {
636                 for (i = backendunits - 1;i >= 0;i--)
637                 {
638                         qglActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
639                         qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
640                         if (gl_combine.integer)
641                         {
642                                 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
643                         }
644                         if (i > 0)
645                         {
646                                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
647                         }
648                         else
649                         {
650                                 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
651                         }
652                         qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
653
654                         if (usedarrays)
655                         {
656                                 qglClientActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
657                                 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
658                         }
659                 }
660         }
661         else
662         {
663                 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
664                 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
665                 if (usedarrays)
666                 {
667                         qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
668                 }
669         }
670         if (usedarrays)
671         {
672                 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
673                 qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
674         }
675
676         qglDisable(GL_BLEND);CHECKGLERROR
677         qglEnable(GL_DEPTH_TEST);CHECKGLERROR
678         qglDepthMask(GL_TRUE);CHECKGLERROR
679         qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);CHECKGLERROR
680 }
681
682 void R_Mesh_ClearDepth(void)
683 {
684         BACKENDACTIVECHECK
685
686         R_Mesh_Finish();
687         qglClear(GL_DEPTH_BUFFER_BIT);
688         R_Mesh_Start(r_mesh_farclip);
689 }
690
691 // sets up the requested state
692 void R_Mesh_State(const rmeshstate_t *m)
693 {
694         int i, overbright;
695         int texturergbscale[MAX_TEXTUREUNITS];
696         float scaler;
697
698         BACKENDACTIVECHECK
699
700         if (gl_backend_rebindtextures)
701         {
702                 gl_backend_rebindtextures = false;
703                 GL_SetupTextureState();
704         }
705
706         backendmatrix = m->matrix; // this copies the struct
707
708         overbright = false;
709         scaler = 1;
710         if (m->blendfunc1 == GL_DST_COLOR)
711         {
712                 // check if it is a 2x modulate with framebuffer
713                 if (m->blendfunc2 == GL_SRC_COLOR)
714                         scaler *= 0.5f;
715         }
716         else if (m->blendfunc2 != GL_SRC_COLOR)
717         {
718                 if (m->tex[0])
719                 {
720                         overbright = m->wantoverbright && gl_combine.integer;
721                         if (overbright)
722                                 scaler *= 0.25f;
723                 }
724                 scaler *= overbrightscale;
725         }
726         mesh_colorscale = scaler;
727
728         if (gl_state.blendfunc1 != m->blendfunc1 || gl_state.blendfunc2 != m->blendfunc2)
729         {
730                 qglBlendFunc(gl_state.blendfunc1 = m->blendfunc1, gl_state.blendfunc2 = m->blendfunc2);CHECKGLERROR
731                 if (gl_state.blendfunc2 == GL_ZERO)
732                 {
733                         if (gl_state.blendfunc1 == GL_ONE)
734                         {
735                                 if (gl_state.blend)
736                                 {
737                                         gl_state.blend = 0;
738                                         qglDisable(GL_BLEND);CHECKGLERROR
739                                 }
740                         }
741                         else
742                         {
743                                 if (!gl_state.blend)
744                                 {
745                                         gl_state.blend = 1;
746                                         qglEnable(GL_BLEND);CHECKGLERROR
747                                 }
748                         }
749                 }
750                 else
751                 {
752                         if (!gl_state.blend)
753                         {
754                                 gl_state.blend = 1;
755                                 qglEnable(GL_BLEND);CHECKGLERROR
756                         }
757                 }
758         }
759         if (gl_state.depthdisable != m->depthdisable)
760         {
761                 gl_state.depthdisable = m->depthdisable;
762                 if (gl_state.depthdisable)
763                         qglDisable(GL_DEPTH_TEST);
764                 else
765                         qglEnable(GL_DEPTH_TEST);
766         }
767         if (gl_state.depthmask != (m->blendfunc2 == GL_ZERO || m->depthwrite))
768         {
769                 qglDepthMask(gl_state.depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite));CHECKGLERROR
770         }
771
772         for (i = 0;i < backendunits;i++)
773         {
774                 if (m->texrgbscale[i])
775                         texturergbscale[i] = m->texrgbscale[i];
776                 else
777                         texturergbscale[i] = 1;
778         }
779         if (overbright)
780                 for (i = backendunits - 1;i >= 0;i--)
781                         if (m->tex[i])
782                                 texturergbscale[i] = 4;
783
784         if (backendunits > 1)
785         {
786                 for (i = 0;i < backendunits;i++)
787                 {
788                         if (gl_state.texture[i] != m->tex[i])
789                         {
790                                 if (gl_state.unit != i)
791                                 {
792                                         qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
793                                 }
794                                 if (gl_state.texture[i] == 0)
795                                 {
796                                         qglEnable(GL_TEXTURE_2D);CHECKGLERROR
797                                         if (gl_state.clientunit != i)
798                                         {
799                                                 qglClientActiveTexture(GL_TEXTURE0_ARB + (gl_state.clientunit = i));CHECKGLERROR
800                                         }
801                                         qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
802                                 }
803                                 qglBindTexture(GL_TEXTURE_2D, (gl_state.texture[i] = m->tex[i]));CHECKGLERROR
804                                 if (gl_state.texture[i] == 0)
805                                 {
806                                         qglDisable(GL_TEXTURE_2D);CHECKGLERROR
807                                         if (gl_state.clientunit != i)
808                                         {
809                                                 qglClientActiveTexture(GL_TEXTURE0_ARB + (gl_state.clientunit = i));CHECKGLERROR
810                                         }
811                                         qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
812                                 }
813                         }
814                         if (gl_state.texturergbscale[i] != texturergbscale[i])
815                         {
816                                 if (gl_state.unit != i)
817                                 {
818                                         qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
819                                 }
820                                 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (gl_state.texturergbscale[i] = texturergbscale[i]));CHECKGLERROR
821                         }
822                 }
823         }
824         else
825         {
826                 if (gl_state.texture[0] != m->tex[0])
827                 {
828                         if (gl_state.texture[0] == 0)
829                         {
830                                 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
831                                 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
832                         }
833                         qglBindTexture(GL_TEXTURE_2D, (gl_state.texture[0] = m->tex[0]));CHECKGLERROR
834                         if (gl_state.texture[0] == 0)
835                         {
836                                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
837                                 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
838                         }
839                 }
840         }
841 }
842
843 /*
844 ==============================================================================
845
846                                                 SCREEN SHOTS
847
848 ==============================================================================
849 */
850
851 qboolean SCR_ScreenShot(char *filename, int x, int y, int width, int height)
852 {
853         qboolean ret;
854         int i;
855         qbyte *buffer;
856
857         if (!r_render.integer)
858                 return false;
859
860         buffer = Mem_Alloc(tempmempool, width*height*3);
861         qglReadPixels (x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer);
862         CHECKGLERROR
863
864         // LordHavoc: compensate for v_overbrightbits when using hardware gamma
865         if (v_hwgamma.integer)
866                 for (i = 0;i < width * height * 3;i++)
867                         buffer[i] <<= v_overbrightbits.integer;
868
869         ret = Image_WriteTGARGB_preflipped(filename, width, height, buffer);
870
871         Mem_Free(buffer);
872         return ret;
873 }
874
875 //=============================================================================
876
877 void R_ClearScreen(void)
878 {
879         if (r_render.integer)
880         {
881                 // clear to black
882                 qglClearColor(0,0,0,0);CHECKGLERROR
883                 // clear the screen
884                 qglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);CHECKGLERROR
885                 // set dithering mode
886                 if (gl_dither.integer)
887                 {
888                         qglEnable(GL_DITHER);CHECKGLERROR
889                 }
890                 else
891                 {
892                         qglDisable(GL_DITHER);CHECKGLERROR
893                 }
894         }
895 }
896
897 /*
898 ==================
899 SCR_UpdateScreen
900
901 This is called every frame, and can also be called explicitly to flush
902 text to the screen.
903 ==================
904 */
905 void SCR_UpdateScreen (void)
906 {
907         VID_Finish ();
908
909         R_TimeReport("finish");
910
911         if (r_textureunits.integer > gl_textureunits)
912                 Cvar_SetValueQuick(&r_textureunits, gl_textureunits);
913         if (r_textureunits.integer < 1)
914                 Cvar_SetValueQuick(&r_textureunits, 1);
915
916         if (gl_combine.integer && (!gl_combine_extension || r_textureunits.integer < 2))
917                 Cvar_SetValueQuick(&gl_combine, 0);
918
919         // lighting scale
920         overbrightscale = 1.0f / (float) (1 << v_overbrightbits.integer);
921
922         // lightmaps only
923         lightscalebit = v_overbrightbits.integer;
924         if (gl_combine.integer && r_textureunits.integer > 1)
925                 lightscalebit += 2;
926         lightscale = 1.0f / (float) (1 << lightscalebit);
927
928         R_TimeReport("setup");
929
930         R_ClearScreen();
931
932         R_TimeReport("clear");
933
934         if (scr_conlines < vid.conheight && cls.signon == SIGNONS)
935                 R_RenderView();
936
937         // draw 2D stuff
938         R_DrawQueue();
939
940         // tell driver to commit it's partially full geometry queue to the rendering queue
941         // (this doesn't wait for the commands themselves to complete)
942         qglFlush();
943 }
944