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