manually inlined GL_LockArray and GL_UnlockArray in the only places they were used
[divverent/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("GL backend function 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 backend_viewmatrix;
94 static matrix4x4_t backend_modelmatrix;
95 static matrix4x4_t backend_modelviewmatrix;
96 static matrix4x4_t backend_glmodelviewmatrix;
97
98 static int backendunits, backendactive;
99 static qbyte *varray_bcolor;
100 static mempool_t *gl_backend_mempool;
101
102 void GL_Backend_AllocArrays(void)
103 {
104         int i;
105
106         if (!gl_backend_mempool)
107                 gl_backend_mempool = Mem_AllocPool("GL_Backend");
108
109         mesh_maxverts = mesh_maxtris * 3;
110
111         varray_element = Mem_Alloc(gl_backend_mempool, mesh_maxtris * sizeof(int[3]));
112         varray_vertex = Mem_Alloc(gl_backend_mempool, mesh_maxverts * sizeof(float[4]));
113         varray_color = Mem_Alloc(gl_backend_mempool, mesh_maxverts * sizeof(float[4]));
114         varray_bcolor = Mem_Alloc(gl_backend_mempool, mesh_maxverts * sizeof(qbyte[4]));
115         for (i = 0;i < backendunits;i++)
116                 varray_texcoord[i] = Mem_Alloc(gl_backend_mempool, mesh_maxverts * sizeof(float[2]));
117         for (;i < MAX_TEXTUREUNITS;i++)
118                 varray_texcoord[i] = NULL;
119 }
120
121 void GL_Backend_FreeArrays(int resizingbuffers)
122 {
123         int i;
124         if (resizingbuffers)
125                 Mem_EmptyPool(gl_backend_mempool);
126         else
127                 Mem_FreePool(&gl_backend_mempool);
128         varray_element = NULL;
129         varray_vertex = NULL;
130         varray_color = NULL;
131         varray_bcolor = NULL;
132         for (i = 0;i < MAX_TEXTUREUNITS;i++)
133                 varray_texcoord[i] = NULL;
134 }
135
136 static void gl_backend_start(void)
137 {
138         Con_Printf("OpenGL Backend started with gl_mesh_maxtriangles %i\n", gl_mesh_maxtriangles.integer);
139         if (qglDrawRangeElements != NULL)
140         {
141                 qglGetIntegerv(GL_MAX_ELEMENTS_VERTICES, &gl_maxdrawrangeelementsvertices);
142                 qglGetIntegerv(GL_MAX_ELEMENTS_INDICES, &gl_maxdrawrangeelementsindices);
143                 CHECKGLERROR
144                 Con_Printf("glDrawRangeElements detected (max vertices %i, max indices %i)\n", gl_maxdrawrangeelementsvertices, gl_maxdrawrangeelementsindices);
145         }
146         if (strstr(gl_renderer, "3Dfx"))
147         {
148                 Con_Printf("3Dfx driver detected, forcing gl_mesh_floatcolors to 0 to prevent crashs\n");
149                 Cvar_SetValueQuick(&gl_mesh_floatcolors, 0);
150         }
151
152         backendunits = min(MAX_TEXTUREUNITS, gl_textureunits);
153
154         GL_Backend_AllocArrays();
155
156         backendactive = true;
157 }
158
159 static void gl_backend_shutdown(void)
160 {
161         backendunits = 0;
162         backendactive = false;
163
164         Con_Printf("OpenGL Backend shutting down\n");
165
166         GL_Backend_FreeArrays(false);
167 }
168
169 void GL_Backend_CheckCvars(void)
170 {
171         if (gl_mesh_drawmode.integer < 0)
172                 Cvar_SetValueQuick(&gl_mesh_drawmode, 0);
173         if (gl_mesh_drawmode.integer > 3)
174                 Cvar_SetValueQuick(&gl_mesh_drawmode, 3);
175
176         // change drawmode 3 to 2 if 3 won't work
177         if (gl_mesh_drawmode.integer >= 3 && qglDrawRangeElements == NULL)
178                 Cvar_SetValueQuick(&gl_mesh_drawmode, 2);
179
180         // 21760 is (65536 / 3) rounded off to a multiple of 128
181         if (gl_mesh_maxtriangles.integer < 1024)
182                 Cvar_SetValueQuick(&gl_mesh_maxtriangles, 1024);
183         if (gl_mesh_maxtriangles.integer > 21760)
184                 Cvar_SetValueQuick(&gl_mesh_maxtriangles, 21760);
185 }
186
187 void GL_Backend_ResizeArrays(int numtriangles)
188 {
189         Cvar_SetValueQuick(&gl_mesh_maxtriangles, numtriangles);
190         GL_Backend_CheckCvars();
191         mesh_maxtris = gl_mesh_maxtriangles.integer;
192         GL_Backend_FreeArrays(true);
193         GL_Backend_AllocArrays();
194 }
195
196 static void gl_backend_newmap(void)
197 {
198 }
199
200 void gl_backend_init(void)
201 {
202         Cvar_RegisterVariable(&r_render);
203         Cvar_RegisterVariable(&gl_dither);
204         Cvar_RegisterVariable(&gl_lockarrays);
205 #ifdef NORENDER
206         Cvar_SetValue("r_render", 0);
207 #endif
208
209         Cvar_RegisterVariable(&gl_mesh_maxtriangles);
210         Cvar_RegisterVariable(&gl_mesh_floatcolors);
211         Cvar_RegisterVariable(&gl_mesh_drawmode);
212         GL_Backend_CheckCvars();
213         R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap);
214 }
215
216 /*
217 =============
218 GL_SetupFrame
219 =============
220 */
221 static void GL_SetupFrame (void)
222 {
223         double xmax, ymax;
224         double fovx, fovy, zNear, zFar, aspect;
225
226         if (!r_render.integer)
227                 return;
228
229         qglDepthFunc (GL_LEQUAL);CHECKGLERROR
230
231         // set up viewpoint
232         qglMatrixMode(GL_PROJECTION);CHECKGLERROR
233         qglLoadIdentity ();CHECKGLERROR
234
235         // y is weird beause OpenGL is bottom to top, we use top to bottom
236         qglViewport(r_refdef.x, vid.realheight - (r_refdef.y + r_refdef.height), r_refdef.width, r_refdef.height);CHECKGLERROR
237
238         // depth range
239         zNear = 1.0;
240         zFar = r_mesh_farclip;
241         if (zFar < 64)
242                 zFar = 64;
243
244         // fov angles
245         fovx = r_refdef.fov_x;
246         fovy = r_refdef.fov_y;
247         aspect = r_refdef.width / r_refdef.height;
248
249         // pyramid slopes
250         xmax = zNear * tan(fovx * M_PI / 360.0) * aspect;
251         ymax = zNear * tan(fovy * M_PI / 360.0);
252
253         // set view pyramid
254         qglFrustum(-xmax, xmax, -ymax, ymax, zNear, zFar);CHECKGLERROR
255
256         qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
257
258         Matrix4x4_CreateRotate(&backend_viewmatrix, -90, 1, 0, 0);
259         Matrix4x4_ConcatRotate(&backend_viewmatrix, 90, 0, 0, 1);
260         Matrix4x4_ConcatRotate(&backend_viewmatrix, -r_refdef.viewangles[2], 1, 0, 0);
261         Matrix4x4_ConcatRotate(&backend_viewmatrix, -r_refdef.viewangles[0], 0, 1, 0);
262         Matrix4x4_ConcatRotate(&backend_viewmatrix, -r_refdef.viewangles[1], 0, 0, 1);
263         Matrix4x4_ConcatTranslate(&backend_viewmatrix, -r_refdef.vieworg[0],  -r_refdef.vieworg[1],  -r_refdef.vieworg[2]);
264         //Con_Printf("Our Matrix:\n");
265         //Matrix4x4_Print(&backend_viewmatrix);
266
267         //Matrix4x4_Transpose(&backend_glmodelviewmatrix, &backend_viewmatrix);
268         //qglLoadMatrixf(&backend_glmodelviewmatrix.m[0][0]);CHECKGLERROR
269         memset(&backend_modelmatrix, 0, sizeof(backend_modelmatrix));
270
271         /*
272         // put Z going up
273         qglLoadIdentity ();CHECKGLERROR
274         qglRotatef (-90,  1, 0, 0);CHECKGLERROR
275         qglRotatef (90,  0, 0, 1);CHECKGLERROR
276         // camera rotation
277         qglRotatef (-r_refdef.viewangles[2],  1, 0, 0);CHECKGLERROR
278         qglRotatef (-r_refdef.viewangles[0],  0, 1, 0);CHECKGLERROR
279         qglRotatef (-r_refdef.viewangles[1],  0, 0, 1);CHECKGLERROR
280         // camera location
281         qglTranslatef (-r_refdef.vieworg[0],  -r_refdef.vieworg[1],  -r_refdef.vieworg[2]);CHECKGLERROR
282         qglGetFloatv (GL_MODELVIEW_MATRIX, &gl_viewmatrix.m[0][0]);
283         Matrix4x4_Transpose(&backend_viewmatrix, &gl_viewmatrix);
284         Con_Printf("GL Matrix:\n");
285         Matrix4x4_Print(&backend_viewmatrix);
286         */
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 /*
499 void GL_TransformVertices(int numverts)
500 {
501         int i;
502         float m[12], tempv[4], *v;
503         m[0] = backendmatrix.m[0][0];
504         m[1] = backendmatrix.m[0][1];
505         m[2] = backendmatrix.m[0][2];
506         m[3] = backendmatrix.m[0][3];
507         m[4] = backendmatrix.m[1][0];
508         m[5] = backendmatrix.m[1][1];
509         m[6] = backendmatrix.m[1][2];
510         m[7] = backendmatrix.m[1][3];
511         m[8] = backendmatrix.m[2][0];
512         m[9] = backendmatrix.m[2][1];
513         m[10] = backendmatrix.m[2][2];
514         m[11] = backendmatrix.m[2][3];
515         for (i = 0, v = varray_vertex;i < numverts;i++, v += 4)
516         {
517                 VectorCopy(v, tempv);
518                 v[0] = tempv[0] * m[0] + tempv[1] * m[1] + tempv[2] * m[2] + m[3];
519                 v[1] = tempv[0] * m[4] + tempv[1] * m[5] + tempv[2] * m[6] + m[7];
520                 v[2] = tempv[0] * m[8] + tempv[1] * m[9] + tempv[2] * m[10] + m[11];
521         }
522 }
523 */
524
525 void GL_DrawRangeElements(int firstvert, int endvert, int indexcount, GLuint *index)
526 {
527         unsigned int i, j, in;
528         qbyte *c;
529         float *v;
530         int arraylocked = false;
531         if (gl_supportslockarrays && gl_lockarrays.integer && gl_mesh_drawmode.integer > 0)
532         {
533                 qglLockArraysEXT(firstvert, endvert - firstvert);
534                 CHECKGLERROR
535                 arraylocked = true;
536         }
537         if (gl_mesh_drawmode.integer >= 3/* && (endvert - firstvert) <= gl_maxdrawrangeelementsvertices && (indexcount) <= gl_maxdrawrangeelementsindices*/)
538         {
539                 // GL 1.2 or GL 1.1 with extension
540                 qglDrawRangeElements(GL_TRIANGLES, firstvert, endvert, indexcount, GL_UNSIGNED_INT, index);
541                 CHECKGLERROR
542         }
543         else if (gl_mesh_drawmode.integer >= 2)
544         {
545                 // GL 1.1
546                 qglDrawElements(GL_TRIANGLES, indexcount, GL_UNSIGNED_INT, index);
547                 CHECKGLERROR
548         }
549         else if (gl_mesh_drawmode.integer >= 1)
550         {
551                 // GL 1.1
552                 // feed it manually using glArrayElement
553                 qglBegin(GL_TRIANGLES);
554                 for (i = 0;i < indexcount;i++)
555                         qglArrayElement(index[i]);
556                 qglEnd();
557                 CHECKGLERROR
558         }
559         else
560         {
561                 // GL 1.1 but not using vertex arrays - 3dfx glquake minigl driver
562                 // feed it manually
563                 qglBegin(GL_TRIANGLES);
564                 if (gl_state.texture[1]) // if the mesh uses multiple textures
565                 {
566                         // the minigl doesn't have this (because it does not have ARB_multitexture)
567                         for (i = 0;i < indexcount;i++)
568                         {
569                                 in = index[i];
570                                 c = varray_bcolor + in * 4;
571                                 qglColor4ub(c[0], c[1], c[2], c[3]);
572                                 for (j = 0;j < backendunits;j++)
573                                 {
574                                         if (gl_state.texture[j])
575                                         {
576                                                 v = varray_texcoord[j] + in * 2;
577                                                 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, v[0], v[1]);
578                                         }
579                                 }
580                                 v = varray_vertex + in * 4;
581                                 qglVertex3f(v[0], v[1], v[2]);
582                         }
583                 }
584                 else
585                 {
586                         for (i = 0;i < indexcount;i++)
587                         {
588                                 in = index[i];
589                                 c = varray_bcolor + in * 4;
590                                 qglColor4ub(c[0], c[1], c[2], c[3]);
591                                 if (gl_state.texture[0])
592                                 {
593                                         v = varray_texcoord[0] + in * 2;
594                                         qglTexCoord2f(v[0], v[1]);
595                                 }
596                                 v = varray_vertex + in * 4;
597                                 qglVertex3f(v[0], v[1], v[2]);
598                         }
599                 }
600                 qglEnd();
601                 CHECKGLERROR
602         }
603         if (arraylocked)
604         {
605                 qglUnlockArraysEXT();
606                 CHECKGLERROR
607                 arraylocked = false;
608         }
609 }
610
611 // enlarges geometry buffers if they are too small
612 void _R_Mesh_ResizeCheck(int numverts, int numtriangles)
613 {
614         if (numtriangles > mesh_maxtris || numverts > mesh_maxverts)
615         {
616                 BACKENDACTIVECHECK
617                 GL_Backend_ResizeArrays(max(numtriangles, (numverts + 2) / 3) + 100);
618                 GL_Backend_ResetState();
619         }
620 }
621
622 // renders the mesh
623 void R_Mesh_Draw(int numverts, int numtriangles)
624 {
625         BACKENDACTIVECHECK
626
627         c_meshs++;
628         c_meshtris += numtriangles;
629
630         CHECKGLERROR
631
632         // drawmode 0 always uses byte colors
633         if (!gl_mesh_floatcolors.integer || gl_mesh_drawmode.integer <= 0)
634                 GL_ConvertColorsFloatToByte(numverts);
635         //GL_TransformVertices(numverts);
636         if (!r_render.integer)
637                 return;
638         GL_DrawRangeElements(0, numverts, numtriangles * 3, varray_element);
639 }
640
641 // restores backend state, used when done with 3D rendering
642 void R_Mesh_Finish(void)
643 {
644         int i;
645         BACKENDACTIVECHECK
646
647         if (backendunits > 1)
648         {
649                 for (i = backendunits - 1;i >= 0;i--)
650                 {
651                         qglActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
652                         qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
653                         if (gl_combine.integer)
654                         {
655                                 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
656                         }
657                         if (i > 0)
658                         {
659                                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
660                         }
661                         else
662                         {
663                                 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
664                         }
665                         qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
666
667                         if (usedarrays)
668                         {
669                                 qglClientActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
670                                 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
671                         }
672                 }
673         }
674         else
675         {
676                 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
677                 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
678                 if (usedarrays)
679                 {
680                         qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
681                 }
682         }
683         if (usedarrays)
684         {
685                 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
686                 qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
687         }
688
689         qglDisable(GL_BLEND);CHECKGLERROR
690         qglEnable(GL_DEPTH_TEST);CHECKGLERROR
691         qglDepthMask(GL_TRUE);CHECKGLERROR
692         qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);CHECKGLERROR
693 }
694
695 void R_Mesh_ClearDepth(void)
696 {
697         BACKENDACTIVECHECK
698
699         R_Mesh_Finish();
700         qglClear(GL_DEPTH_BUFFER_BIT);
701         R_Mesh_Start(r_mesh_farclip);
702 }
703
704 void R_Mesh_Matrix(const matrix4x4_t *matrix)
705 {
706         if (memcmp(matrix, &backend_modelmatrix, sizeof(matrix4x4_t)))
707         {
708                 backend_modelmatrix = *matrix;
709                 Matrix4x4_Concat(&backend_modelviewmatrix, &backend_viewmatrix, matrix);
710                 Matrix4x4_Transpose(&backend_glmodelviewmatrix, &backend_modelviewmatrix);
711                 qglLoadMatrixf(&backend_glmodelviewmatrix.m[0][0]);
712         }
713 }
714
715 // sets up the requested state
716 void R_Mesh_State(const rmeshstate_t *m)
717 {
718         int i, overbright;
719         int texturergbscale[MAX_TEXTUREUNITS];
720         float scaler;
721
722         BACKENDACTIVECHECK
723
724         if (gl_backend_rebindtextures)
725         {
726                 gl_backend_rebindtextures = false;
727                 GL_SetupTextureState();
728         }
729
730         overbright = false;
731         scaler = 1;
732         if (m->blendfunc1 == GL_DST_COLOR)
733         {
734                 // check if it is a 2x modulate with framebuffer
735                 if (m->blendfunc2 == GL_SRC_COLOR)
736                         scaler *= 0.5f;
737         }
738         else if (m->blendfunc2 != GL_SRC_COLOR)
739         {
740                 if (m->tex[0])
741                 {
742                         overbright = m->wantoverbright && gl_combine.integer;
743                         if (overbright)
744                                 scaler *= 0.25f;
745                 }
746                 scaler *= overbrightscale;
747         }
748         mesh_colorscale = scaler;
749
750         if (gl_state.blendfunc1 != m->blendfunc1 || gl_state.blendfunc2 != m->blendfunc2)
751         {
752                 qglBlendFunc(gl_state.blendfunc1 = m->blendfunc1, gl_state.blendfunc2 = m->blendfunc2);CHECKGLERROR
753                 if (gl_state.blendfunc2 == GL_ZERO)
754                 {
755                         if (gl_state.blendfunc1 == GL_ONE)
756                         {
757                                 if (gl_state.blend)
758                                 {
759                                         gl_state.blend = 0;
760                                         qglDisable(GL_BLEND);CHECKGLERROR
761                                 }
762                         }
763                         else
764                         {
765                                 if (!gl_state.blend)
766                                 {
767                                         gl_state.blend = 1;
768                                         qglEnable(GL_BLEND);CHECKGLERROR
769                                 }
770                         }
771                 }
772                 else
773                 {
774                         if (!gl_state.blend)
775                         {
776                                 gl_state.blend = 1;
777                                 qglEnable(GL_BLEND);CHECKGLERROR
778                         }
779                 }
780         }
781         if (gl_state.depthdisable != m->depthdisable)
782         {
783                 gl_state.depthdisable = m->depthdisable;
784                 if (gl_state.depthdisable)
785                         qglDisable(GL_DEPTH_TEST);
786                 else
787                         qglEnable(GL_DEPTH_TEST);
788         }
789         if (gl_state.depthmask != (m->blendfunc2 == GL_ZERO || m->depthwrite))
790         {
791                 qglDepthMask(gl_state.depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite));CHECKGLERROR
792         }
793
794         for (i = 0;i < backendunits;i++)
795         {
796                 if (m->texrgbscale[i])
797                         texturergbscale[i] = m->texrgbscale[i];
798                 else
799                         texturergbscale[i] = 1;
800         }
801         if (overbright)
802         {
803                 for (i = backendunits - 1;i >= 0;i--)
804                 {
805                         if (m->tex[i])
806                         {
807                                 texturergbscale[i] = 4;
808                                 break;
809                         }
810                 }
811         }
812
813         if (backendunits > 1)
814         {
815                 for (i = 0;i < backendunits;i++)
816                 {
817                         if (gl_state.texture[i] != m->tex[i])
818                         {
819                                 if (gl_state.unit != i)
820                                 {
821                                         qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
822                                 }
823                                 if (gl_state.texture[i] == 0)
824                                 {
825                                         qglEnable(GL_TEXTURE_2D);CHECKGLERROR
826                                         if (gl_state.clientunit != i)
827                                         {
828                                                 qglClientActiveTexture(GL_TEXTURE0_ARB + (gl_state.clientunit = i));CHECKGLERROR
829                                         }
830                                         qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
831                                 }
832                                 qglBindTexture(GL_TEXTURE_2D, (gl_state.texture[i] = m->tex[i]));CHECKGLERROR
833                                 if (gl_state.texture[i] == 0)
834                                 {
835                                         qglDisable(GL_TEXTURE_2D);CHECKGLERROR
836                                         if (gl_state.clientunit != i)
837                                         {
838                                                 qglClientActiveTexture(GL_TEXTURE0_ARB + (gl_state.clientunit = i));CHECKGLERROR
839                                         }
840                                         qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
841                                 }
842                         }
843                         if (gl_state.texturergbscale[i] != texturergbscale[i])
844                         {
845                                 if (gl_state.unit != i)
846                                 {
847                                         qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
848                                 }
849                                 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (gl_state.texturergbscale[i] = texturergbscale[i]));CHECKGLERROR
850                         }
851                 }
852         }
853         else
854         {
855                 if (gl_state.texture[0] != m->tex[0])
856                 {
857                         if (gl_state.texture[0] == 0)
858                         {
859                                 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
860                                 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
861                         }
862                         qglBindTexture(GL_TEXTURE_2D, (gl_state.texture[0] = m->tex[0]));CHECKGLERROR
863                         if (gl_state.texture[0] == 0)
864                         {
865                                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
866                                 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
867                         }
868                 }
869         }
870 }
871
872 /*
873 ==============================================================================
874
875                                                 SCREEN SHOTS
876
877 ==============================================================================
878 */
879
880 qboolean SCR_ScreenShot(char *filename, int x, int y, int width, int height)
881 {
882         qboolean ret;
883         int i;
884         qbyte *buffer;
885
886         if (!r_render.integer)
887                 return false;
888
889         buffer = Mem_Alloc(tempmempool, width*height*3);
890         qglReadPixels (x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer);
891         CHECKGLERROR
892
893         // LordHavoc: compensate for v_overbrightbits when using hardware gamma
894         if (v_hwgamma.integer)
895                 for (i = 0;i < width * height * 3;i++)
896                         buffer[i] <<= v_overbrightbits.integer;
897
898         ret = Image_WriteTGARGB_preflipped(filename, width, height, buffer);
899
900         Mem_Free(buffer);
901         return ret;
902 }
903
904 //=============================================================================
905
906 void R_ClearScreen(void)
907 {
908         if (r_render.integer)
909         {
910                 // clear to black
911                 qglClearColor(0,0,0,0);CHECKGLERROR
912                 // clear the screen
913                 qglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);CHECKGLERROR
914                 // set dithering mode
915                 if (gl_dither.integer)
916                 {
917                         qglEnable(GL_DITHER);CHECKGLERROR
918                 }
919                 else
920                 {
921                         qglDisable(GL_DITHER);CHECKGLERROR
922                 }
923         }
924 }
925
926 /*
927 ==================
928 SCR_UpdateScreen
929
930 This is called every frame, and can also be called explicitly to flush
931 text to the screen.
932 ==================
933 */
934 void SCR_UpdateScreen (void)
935 {
936         VID_Finish ();
937
938         R_TimeReport("finish");
939
940         if (r_textureunits.integer > gl_textureunits)
941                 Cvar_SetValueQuick(&r_textureunits, gl_textureunits);
942         if (r_textureunits.integer < 1)
943                 Cvar_SetValueQuick(&r_textureunits, 1);
944
945         if (gl_combine.integer && (!gl_combine_extension || r_textureunits.integer < 2))
946                 Cvar_SetValueQuick(&gl_combine, 0);
947
948         // lighting scale
949         overbrightscale = 1.0f / (float) (1 << v_overbrightbits.integer);
950
951         // lightmaps only
952         lightscalebit = v_overbrightbits.integer;
953         if (gl_combine.integer && r_textureunits.integer > 1)
954                 lightscalebit += 2;
955         lightscale = 1.0f / (float) (1 << lightscalebit);
956
957         R_TimeReport("setup");
958
959         R_ClearScreen();
960
961         R_TimeReport("clear");
962
963         if (scr_conlines < vid.conheight && cls.signon == SIGNONS)
964                 R_RenderView();
965
966         // draw 2D stuff
967         R_DrawQueue();
968
969         // tell driver to commit it's partially full geometry queue to the rendering queue
970         // (this doesn't wait for the commands themselves to complete)
971         qglFlush();
972 }
973