]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - gl_backend.c
R_GetTexture is now named R_RealGetTexture, R_GetTexture is a macro which hardly...
[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_batchtriangles = {0, "gl_mesh_batchtriangles", "0"};
6 cvar_t gl_mesh_transtriangles = {0, "gl_mesh_transtriangles", "16384"};
7 cvar_t gl_mesh_floatcolors = {0, "gl_mesh_floatcolors", "1"};
8 cvar_t gl_mesh_drawmode = {CVAR_SAVE, "gl_mesh_drawmode", "3"};
9
10 cvar_t r_render = {0, "r_render", "1"};
11 cvar_t gl_dither = {CVAR_SAVE, "gl_dither", "1"}; // whether or not to use dithering
12 cvar_t gl_lockarrays = {0, "gl_lockarrays", "1"};
13
14 // this is used to increase gl_mesh_maxtriangles automatically if a mesh was
15 // too large for the buffers in the previous frame
16 int overflowedverts = 0;
17 // increase transtriangles automatically too
18 int overflowedtransverts = 0;
19
20 int gl_maxdrawrangeelementsvertices;
21 int gl_maxdrawrangeelementsindices;
22
23 #ifdef DEBUGGL
24 int errornumber = 0;
25
26 void GL_PrintError(int errornumber, char *filename, int linenumber)
27 {
28         switch(errornumber)
29         {
30 #ifdef GL_INVALID_ENUM
31         case GL_INVALID_ENUM:
32                 Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
33                 break;
34 #endif
35 #ifdef GL_INVALID_VALUE
36         case GL_INVALID_VALUE:
37                 Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
38                 break;
39 #endif
40 #ifdef GL_INVALID_OPERATION
41         case GL_INVALID_OPERATION:
42                 Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
43                 break;
44 #endif
45 #ifdef GL_STACK_OVERFLOW
46         case GL_STACK_OVERFLOW:
47                 Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
48                 break;
49 #endif
50 #ifdef GL_STACK_UNDERFLOW
51         case GL_STACK_UNDERFLOW:
52                 Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
53                 break;
54 #endif
55 #ifdef GL_OUT_OF_MEMORY
56         case GL_OUT_OF_MEMORY:
57                 Con_Printf("GL_OUT_OF_MEMORY at %s:%i\n", filename, linenumber);
58                 break;
59 #endif
60 #ifdef GL_TABLE_TOO_LARGE
61     case GL_TABLE_TOO_LARGE:
62                 Con_Printf("GL_TABLE_TOO_LARGE at %s:%i\n", filename, linenumber);
63                 break;
64 #endif
65         default:
66                 Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
67                 break;
68         }
69 }
70 #endif
71
72 float r_farclip, r_newfarclip;
73
74 int polyindexarray[768];
75
76 static float viewdist;
77
78 int c_meshs, c_meshtris, c_transmeshs, c_transtris;
79
80 int                     lightscalebit;
81 float           lightscale;
82 float           overbrightscale;
83
84 void SCR_ScreenShot_f (void);
85
86 static int max_meshs;
87 static int max_transmeshs;
88 static int max_batch;
89 static int max_verts; // always max_meshs * 3
90 static int max_transverts; // always max_transmeshs * 3
91 #define TRANSDEPTHRES 4096
92
93 typedef struct buf_mesh_s
94 {
95         int depthmask;
96         int depthtest;
97         int blendfunc1, blendfunc2;
98         int textures[MAX_TEXTUREUNITS];
99         int texturergbscale[MAX_TEXTUREUNITS];
100         int firsttriangle;
101         int triangles;
102         int firstvert;
103         int verts;
104         struct buf_mesh_s *chain;
105         struct buf_transtri_s *transchain;
106 }
107 buf_mesh_t;
108
109 typedef struct buf_transtri_s
110 {
111         struct buf_transtri_s *next;
112         struct buf_transtri_s *meshsortchain;
113         buf_mesh_t *mesh;
114         int index[3];
115 }
116 buf_transtri_t;
117
118 typedef struct buf_tri_s
119 {
120         int index[3];
121 }
122 buf_tri_t;
123
124 typedef struct
125 {
126         float v[4];
127 }
128 buf_vertex_t;
129
130 typedef struct
131 {
132         float c[4];
133 }
134 buf_fcolor_t;
135
136 typedef struct
137 {
138         qbyte c[4];
139 }
140 buf_bcolor_t;
141
142 typedef struct
143 {
144         float t[2];
145 }
146 buf_texcoord_t;
147
148 static float meshfarclip;
149 static int currentmesh, currenttriangle, currentvertex, backendunits, backendactive, transranout;
150 static buf_mesh_t *buf_mesh;
151 static buf_tri_t *buf_tri;
152 static buf_vertex_t *buf_vertex;
153 static buf_fcolor_t *buf_fcolor;
154 static buf_bcolor_t *buf_bcolor;
155 static buf_texcoord_t *buf_texcoord[MAX_TEXTUREUNITS];
156
157 static int currenttransmesh, currenttransvertex, currenttranstriangle;
158 static buf_mesh_t *buf_transmesh;
159 static buf_transtri_t *buf_sorttranstri;
160 static buf_transtri_t **buf_sorttranstri_list;
161 static buf_tri_t *buf_transtri;
162 static buf_vertex_t *buf_transvertex;
163 static buf_fcolor_t *buf_transfcolor;
164 static buf_texcoord_t *buf_transtexcoord[MAX_TEXTUREUNITS];
165
166 static mempool_t *gl_backend_mempool;
167 static int resizingbuffers = false;
168
169 static void gl_backend_start(void)
170 {
171         int i;
172
173         qglGetIntegerv(GL_MAX_ELEMENTS_VERTICES, &gl_maxdrawrangeelementsvertices);
174         qglGetIntegerv(GL_MAX_ELEMENTS_INDICES, &gl_maxdrawrangeelementsindices);
175
176         Con_Printf("OpenGL Backend started with gl_mesh_maxtriangles %i, gl_mesh_transtriangles %i\n", gl_mesh_maxtriangles.integer, gl_mesh_transtriangles.integer);
177         if (qglDrawRangeElements != NULL)
178                 Con_Printf("glDrawRangeElements detected (max vertices %i, max indices %i)\n", gl_maxdrawrangeelementsvertices, gl_maxdrawrangeelementsindices);
179         if (strstr(gl_renderer, "3Dfx"))
180         {
181                 Con_Printf("3Dfx driver detected, forcing gl_mesh_floatcolors to 0 to prevent crashs\n");
182                 Cvar_SetValueQuick(&gl_mesh_floatcolors, 0);
183         }
184         Con_Printf("\n");
185
186         max_verts = max_meshs * 3;
187         max_transverts = max_transmeshs * 3;
188
189         if (!gl_backend_mempool)
190                 gl_backend_mempool = Mem_AllocPool("GL_Backend");
191
192 #define BACKENDALLOC(var, count, sizeofstruct, varname)\
193         {\
194                 var = Mem_Alloc(gl_backend_mempool, count * sizeof(sizeofstruct));\
195                 if (var == NULL)\
196                         Sys_Error("gl_backend_start: unable to allocate memory for %s (%d bytes)\n", (varname), count * sizeof(sizeofstruct));\
197                 memset(var, 0, count * sizeof(sizeofstruct));\
198         }
199
200         BACKENDALLOC(buf_mesh, max_meshs, buf_mesh_t, "buf_mesh")
201         BACKENDALLOC(buf_tri, max_meshs, buf_tri_t, "buf_tri")
202         BACKENDALLOC(buf_vertex, max_verts, buf_vertex_t, "buf_vertex")
203         BACKENDALLOC(buf_fcolor, max_verts, buf_fcolor_t, "buf_fcolor")
204         BACKENDALLOC(buf_bcolor, max_verts, buf_bcolor_t, "buf_bcolor")
205
206         BACKENDALLOC(buf_transmesh, max_transmeshs, buf_mesh_t, "buf_transmesh")
207         BACKENDALLOC(buf_sorttranstri, max_transmeshs, buf_transtri_t, "buf_sorttranstri")
208         BACKENDALLOC(buf_sorttranstri_list, TRANSDEPTHRES, buf_transtri_t *, "buf_sorttranstri_list")
209         BACKENDALLOC(buf_transtri, max_transmeshs, buf_tri_t, "buf_transtri")
210         BACKENDALLOC(buf_transvertex, max_transverts, buf_vertex_t, "buf_vertex")
211         BACKENDALLOC(buf_transfcolor, max_transverts, buf_fcolor_t, "buf_fcolor")
212
213         for (i = 0;i < MAX_TEXTUREUNITS;i++)
214         {
215                 // only allocate as many texcoord arrays as we need
216                 if (i < gl_textureunits)
217                 {
218                         BACKENDALLOC(buf_texcoord[i], max_verts, buf_texcoord_t, va("buf_texcoord[%d]", i))
219                         BACKENDALLOC(buf_transtexcoord[i], max_transverts, buf_texcoord_t, va("buf_transtexcoord[%d]", i))
220                 }
221                 else
222                 {
223                         buf_texcoord[i] = NULL;
224                         buf_transtexcoord[i] = NULL;
225                 }
226         }
227         backendunits = min(MAX_TEXTUREUNITS, gl_textureunits);
228         backendactive = true;
229 }
230
231 static void gl_backend_shutdown(void)
232 {
233         Con_Printf("OpenGL Backend shutting down\n");
234
235         if (resizingbuffers)
236                 Mem_EmptyPool(gl_backend_mempool);
237         else
238                 Mem_FreePool(&gl_backend_mempool);
239
240         backendunits = 0;
241         backendactive = false;
242 }
243
244 static void gl_backend_bufferchanges(int init)
245 {
246         if (overflowedverts > gl_mesh_maxtriangles.integer * 3)
247                 Cvar_SetValueQuick(&gl_mesh_maxtriangles, (int) ((overflowedverts + 2) / 3));
248         overflowedverts = 0;
249
250         if (overflowedtransverts > gl_mesh_transtriangles.integer * 3)
251                 Cvar_SetValueQuick(&gl_mesh_transtriangles, (int) ((overflowedtransverts + 2) / 3));
252         overflowedtransverts = 0;
253
254         if (gl_mesh_drawmode.integer < 0)
255                 Cvar_SetValueQuick(&gl_mesh_drawmode, 0);
256         if (gl_mesh_drawmode.integer > 3)
257                 Cvar_SetValueQuick(&gl_mesh_drawmode, 3);
258
259         if (gl_mesh_drawmode.integer >= 3 && qglDrawRangeElements == NULL)
260         {
261                 // change drawmode 3 to 2 if 3 won't work at all
262                 Cvar_SetValueQuick(&gl_mesh_drawmode, 2);
263         }
264
265         // 21760 is (65536 / 3) rounded off to a multiple of 128
266         if (gl_mesh_maxtriangles.integer < 1024)
267                 Cvar_SetValueQuick(&gl_mesh_maxtriangles, 1024);
268         if (gl_mesh_maxtriangles.integer > 21760)
269                 Cvar_SetValueQuick(&gl_mesh_maxtriangles, 21760);
270
271         if (gl_mesh_transtriangles.integer < 1024)
272                 Cvar_SetValueQuick(&gl_mesh_transtriangles, 1024);
273         if (gl_mesh_transtriangles.integer > 65536)
274                 Cvar_SetValueQuick(&gl_mesh_transtriangles, 65536);
275
276         if (gl_mesh_batchtriangles.integer < 1)
277                 Cvar_SetValueQuick(&gl_mesh_batchtriangles, 1);
278         if (gl_mesh_batchtriangles.integer > gl_mesh_maxtriangles.integer)
279                 Cvar_SetValueQuick(&gl_mesh_batchtriangles, gl_mesh_maxtriangles.integer);
280
281         max_batch = gl_mesh_batchtriangles.integer;
282
283         if (max_meshs != gl_mesh_maxtriangles.integer || max_transmeshs != gl_mesh_transtriangles.integer)
284         {
285                 max_meshs = gl_mesh_maxtriangles.integer;
286                 max_transmeshs = gl_mesh_transtriangles.integer;
287
288                 if (!init)
289                 {
290                         resizingbuffers = true;
291                         gl_backend_shutdown();
292                         gl_backend_start();
293                         resizingbuffers = false;
294                 }
295         }
296 }
297
298 static void gl_backend_newmap(void)
299 {
300         r_farclip = r_newfarclip = 2048.0f;
301 }
302
303 void gl_backend_init(void)
304 {
305         int i;
306
307         Cvar_RegisterVariable(&r_render);
308         Cvar_RegisterVariable(&gl_dither);
309         Cvar_RegisterVariable(&gl_lockarrays);
310 #ifdef NORENDER
311         Cvar_SetValue("r_render", 0);
312 #endif
313
314         Cvar_RegisterVariable(&gl_mesh_maxtriangles);
315         Cvar_RegisterVariable(&gl_mesh_transtriangles);
316         Cvar_RegisterVariable(&gl_mesh_batchtriangles);
317         Cvar_RegisterVariable(&gl_mesh_floatcolors);
318         Cvar_RegisterVariable(&gl_mesh_drawmode);
319         R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap);
320         gl_backend_bufferchanges(true);
321         for (i = 0;i < 256;i++)
322         {
323                 polyindexarray[i*3+0] = 0;
324                 polyindexarray[i*3+1] = i + 1;
325                 polyindexarray[i*3+2] = i + 2;
326         }
327 }
328
329 int arraylocked = false;
330
331 void GL_LockArray(int first, int count)
332 {
333         if (!arraylocked && gl_supportslockarrays && gl_lockarrays.integer && gl_mesh_drawmode.integer > 0)
334         {
335                 qglLockArraysEXT(first, count);
336                 CHECKGLERROR
337                 arraylocked = true;
338         }
339 }
340
341 void GL_UnlockArray(void)
342 {
343         if (arraylocked)
344         {
345                 qglUnlockArraysEXT();
346                 CHECKGLERROR
347                 arraylocked = false;
348         }
349 }
350
351 /*
352 =============
353 GL_SetupFrame
354 =============
355 */
356 static void GL_SetupFrame (void)
357 {
358         double xmax, ymax;
359         double fovx, fovy, zNear, zFar, aspect;
360
361         // update farclip based on previous frame
362         r_farclip = r_newfarclip;
363
364         if (!r_render.integer)
365                 return;
366
367         qglDepthFunc (GL_LEQUAL);CHECKGLERROR
368
369         // set up viewpoint
370         qglMatrixMode(GL_PROJECTION);CHECKGLERROR
371         qglLoadIdentity ();CHECKGLERROR
372
373         // y is weird beause OpenGL is bottom to top, we use top to bottom
374         qglViewport(r_refdef.x, vid.realheight - (r_refdef.y + r_refdef.height), r_refdef.width, r_refdef.height);CHECKGLERROR
375
376         // depth range
377         zNear = 1.0;
378         zFar = r_farclip;
379
380         // fov angles
381         fovx = r_refdef.fov_x;
382         fovy = r_refdef.fov_y;
383         aspect = r_refdef.width / r_refdef.height;
384
385         // pyramid slopes
386         xmax = zNear * tan(fovx * M_PI / 360.0) * aspect;
387         ymax = zNear * tan(fovy * M_PI / 360.0);
388
389         // set view pyramid
390         qglFrustum(-xmax, xmax, -ymax, ymax, zNear, zFar);CHECKGLERROR
391
392         qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
393         qglLoadIdentity ();CHECKGLERROR
394
395         // put Z going up
396         qglRotatef (-90,  1, 0, 0);CHECKGLERROR
397         qglRotatef (90,  0, 0, 1);CHECKGLERROR
398         // camera rotation
399         qglRotatef (-r_refdef.viewangles[2],  1, 0, 0);CHECKGLERROR
400         qglRotatef (-r_refdef.viewangles[0],  0, 1, 0);CHECKGLERROR
401         qglRotatef (-r_refdef.viewangles[1],  0, 0, 1);CHECKGLERROR
402         // camera location
403         qglTranslatef (-r_refdef.vieworg[0],  -r_refdef.vieworg[1],  -r_refdef.vieworg[2]);CHECKGLERROR
404 }
405
406 static int mesh_blendfunc1;
407 static int mesh_blendfunc2;
408 static int mesh_blend;
409 static GLboolean mesh_depthmask;
410 static int mesh_depthtest;
411 static int mesh_unit;
412 static int mesh_clientunit;
413 static int mesh_texture[MAX_TEXTUREUNITS];
414 static float mesh_texturergbscale[MAX_TEXTUREUNITS];
415
416 void GL_SetupTextureState(void)
417 {
418         int i;
419         if (backendunits > 1)
420         {
421                 for (i = 0;i < backendunits;i++)
422                 {
423                         qglActiveTexture(GL_TEXTURE0_ARB + (mesh_unit = i));CHECKGLERROR
424                         qglBindTexture(GL_TEXTURE_2D, mesh_texture[i]);CHECKGLERROR
425                         if (gl_combine.integer)
426                         {
427                                 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);CHECKGLERROR
428                                 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);CHECKGLERROR
429                                 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);CHECKGLERROR
430                                 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
431                                 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_CONSTANT_ARB);CHECKGLERROR
432                                 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
433                                 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
434                                 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);CHECKGLERROR
435                                 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);CHECKGLERROR
436                                 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);CHECKGLERROR
437                                 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
438                                 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, GL_CONSTANT_ARB);CHECKGLERROR
439                                 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
440                                 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
441                                 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
442                                 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, mesh_texturergbscale[i]);CHECKGLERROR
443                                 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
444                         }
445                         else
446                         {
447                                 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
448                         }
449                         if (mesh_texture[i])
450                         {
451                                 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
452                         }
453                         else
454                         {
455                                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
456                         }
457                         if (gl_mesh_drawmode.integer > 0)
458                         {
459                                 qglClientActiveTexture(GL_TEXTURE0_ARB + (mesh_clientunit = i));CHECKGLERROR
460                                 qglTexCoordPointer(2, GL_FLOAT, sizeof(buf_texcoord_t), buf_texcoord[i]);CHECKGLERROR
461                                 if (mesh_texture[i])
462                                 {
463                                         qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
464                                 }
465                                 else
466                                 {
467                                         qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
468                                 }
469                         }
470                 }
471         }
472         else
473         {
474                 qglBindTexture(GL_TEXTURE_2D, mesh_texture[0]);CHECKGLERROR
475                 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
476                 if (mesh_texture[0])
477                 {
478                         qglEnable(GL_TEXTURE_2D);CHECKGLERROR
479                 }
480                 else
481                 {
482                         qglDisable(GL_TEXTURE_2D);CHECKGLERROR
483                 }
484                 if (gl_mesh_drawmode.integer > 0)
485                 {
486                         qglTexCoordPointer(2, GL_FLOAT, sizeof(buf_texcoord_t), buf_texcoord[0]);CHECKGLERROR
487                         if (mesh_texture[0])
488                         {
489                                 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
490                         }
491                         else
492                         {
493                                 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
494                         }
495                 }
496         }
497 }
498
499 // called at beginning of frame
500 int usedarrays;
501 void R_Mesh_Start(void)
502 {
503         int i;
504         if (!backendactive)
505                 Sys_Error("R_Mesh_Clear: called when backend is not active\n");
506
507         CHECKGLERROR
508
509         gl_backend_bufferchanges(false);
510
511         currentmesh = 0;
512         currenttriangle = 0;
513         currentvertex = 0;
514         currenttransmesh = 0;
515         currenttranstriangle = 0;
516         currenttransvertex = 0;
517         meshfarclip = 0;
518         transranout = false;
519         viewdist = DotProduct(r_origin, vpn);
520
521         c_meshs = 0;
522         c_meshtris = 0;
523         c_transmeshs = 0;
524         c_transtris = 0;
525
526         GL_SetupFrame();
527
528         mesh_unit = 0;
529         mesh_clientunit = 0;
530
531         for (i = 0;i < backendunits;i++)
532         {
533                 mesh_texture[i] = 0;
534                 mesh_texturergbscale[i] = 1;
535         }
536
537         qglEnable(GL_CULL_FACE);CHECKGLERROR
538         qglCullFace(GL_FRONT);CHECKGLERROR
539
540         mesh_depthtest = true;
541         qglEnable(GL_DEPTH_TEST);CHECKGLERROR
542
543         mesh_blendfunc1 = GL_ONE;
544         mesh_blendfunc2 = GL_ZERO;
545         qglBlendFunc(mesh_blendfunc1, mesh_blendfunc2);CHECKGLERROR
546
547         mesh_blend = 0;
548         qglDisable(GL_BLEND);CHECKGLERROR
549
550         mesh_depthmask = GL_TRUE;
551         qglDepthMask(mesh_depthmask);CHECKGLERROR
552
553         usedarrays = false;
554         if (gl_mesh_drawmode.integer > 0)
555         {
556                 usedarrays = true;
557                 qglVertexPointer(3, GL_FLOAT, sizeof(buf_vertex_t), &buf_vertex[0].v[0]);CHECKGLERROR
558                 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
559                 if (gl_mesh_floatcolors.integer)
560                 {
561                         qglColorPointer(4, GL_FLOAT, sizeof(buf_fcolor_t), &buf_fcolor[0].c[0]);CHECKGLERROR
562                 }
563                 else
564                 {
565                         qglColorPointer(4, GL_UNSIGNED_BYTE, sizeof(buf_bcolor_t), &buf_bcolor[0].c[0]);CHECKGLERROR
566                 }
567                 qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
568         }
569
570         GL_SetupTextureState();
571 }
572
573 int gl_backend_rebindtextures;
574
575 void GL_UpdateFarclip(void)
576 {
577         int i;
578         float farclip;
579
580         // push out farclip based on vertices
581         // FIXME: wouldn't this be slow when using matrix transforms?
582         for (i = 0;i < currentvertex;i++)
583         {
584                 farclip = DotProduct(buf_vertex[i].v, vpn);
585                 if (meshfarclip < farclip)
586                         meshfarclip = farclip;
587         }
588
589         farclip = meshfarclip + 256.0f - viewdist; // + 256 just to be safe
590
591         // push out farclip for next frame
592         if (farclip > r_newfarclip)
593                 r_newfarclip = ceil((farclip + 255) / 256) * 256 + 256;
594 }
595
596 void GL_ConvertColorsFloatToByte(void)
597 {
598         int i, k, total;
599         // LordHavoc: to avoid problems with aliasing (treating memory as two
600         // different types - exactly what this is doing), these must be volatile
601         // (or a union)
602         volatile int *icolor;
603         volatile float *fcolor;
604         qbyte *bcolor;
605
606         total = currentvertex * 4;
607
608         // shift float to have 8bit fraction at base of number
609         fcolor = &buf_fcolor->c[0];
610         for (i = 0;i < total;)
611         {
612                 fcolor[i    ] += 32768.0f;
613                 fcolor[i + 1] += 32768.0f;
614                 fcolor[i + 2] += 32768.0f;
615                 fcolor[i + 3] += 32768.0f;
616                 i += 4;
617         }
618
619         // then read as integer and kill float bits...
620         icolor = (int *)&buf_fcolor->c[0];
621         bcolor = &buf_bcolor->c[0];
622         for (i = 0;i < total;)
623         {
624                 k = icolor[i    ] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i    ] = (qbyte) k;
625                 k = icolor[i + 1] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i + 1] = (qbyte) k;
626                 k = icolor[i + 2] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i + 2] = (qbyte) k;
627                 k = icolor[i + 3] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i + 3] = (qbyte) k;
628                 i += 4;
629         }
630 }
631
632 void GL_MeshState(buf_mesh_t *mesh)
633 {
634         int i;
635         if (backendunits > 1)
636         {
637                 for (i = 0;i < backendunits;i++)
638                 {
639                         if (mesh_texture[i] != mesh->textures[i])
640                         {
641                                 if (mesh_unit != i)
642                                 {
643                                         qglActiveTexture(GL_TEXTURE0_ARB + (mesh_unit = i));CHECKGLERROR
644                                 }
645                                 if (mesh_texture[i] == 0)
646                                 {
647                                         qglEnable(GL_TEXTURE_2D);CHECKGLERROR
648                                         // have to disable texcoord array on disabled texture
649                                         // units due to NVIDIA driver bug with
650                                         // compiled_vertex_array
651                                         if (mesh_clientunit != i)
652                                         {
653                                                 qglClientActiveTexture(GL_TEXTURE0_ARB + (mesh_clientunit = i));CHECKGLERROR
654                                         }
655                                         qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
656                                 }
657                                 qglBindTexture(GL_TEXTURE_2D, (mesh_texture[i] = mesh->textures[i]));CHECKGLERROR
658                                 if (mesh_texture[i] == 0)
659                                 {
660                                         qglDisable(GL_TEXTURE_2D);CHECKGLERROR
661                                         // have to disable texcoord array on disabled texture
662                                         // units due to NVIDIA driver bug with
663                                         // compiled_vertex_array
664                                         if (mesh_clientunit != i)
665                                         {
666                                                 qglClientActiveTexture(GL_TEXTURE0_ARB + (mesh_clientunit = i));CHECKGLERROR
667                                         }
668                                         qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
669                                 }
670                         }
671                         if (mesh_texturergbscale[i] != mesh->texturergbscale[i])
672                         {
673                                 if (mesh_unit != i)
674                                 {
675                                         qglActiveTexture(GL_TEXTURE0_ARB + (mesh_unit = i));CHECKGLERROR
676                                 }
677                                 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (mesh_texturergbscale[i] = mesh->texturergbscale[i]));CHECKGLERROR
678                         }
679                 }
680         }
681         else
682         {
683                 if (mesh_texture[0] != mesh->textures[0])
684                 {
685                         if (mesh_texture[0] == 0)
686                         {
687                                 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
688                                 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
689                         }
690                         qglBindTexture(GL_TEXTURE_2D, (mesh_texture[0] = mesh->textures[0]));CHECKGLERROR
691                         if (mesh_texture[0] == 0)
692                         {
693                                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
694                                 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
695                         }
696                 }
697         }
698         if (mesh_blendfunc1 != mesh->blendfunc1 || mesh_blendfunc2 != mesh->blendfunc2)
699         {
700                 qglBlendFunc(mesh_blendfunc1 = mesh->blendfunc1, mesh_blendfunc2 = mesh->blendfunc2);CHECKGLERROR
701                 if (mesh_blendfunc2 == GL_ZERO)
702                 {
703                         if (mesh_blendfunc1 == GL_ONE)
704                         {
705                                 if (mesh_blend)
706                                 {
707                                         mesh_blend = 0;
708                                         qglDisable(GL_BLEND);CHECKGLERROR
709                                 }
710                         }
711                         else
712                         {
713                                 if (!mesh_blend)
714                                 {
715                                         mesh_blend = 1;
716                                         qglEnable(GL_BLEND);CHECKGLERROR
717                                 }
718                         }
719                 }
720                 else
721                 {
722                         if (!mesh_blend)
723                         {
724                                 mesh_blend = 1;
725                                 qglEnable(GL_BLEND);CHECKGLERROR
726                         }
727                 }
728         }
729         if (mesh_depthtest != mesh->depthtest)
730         {
731                 mesh_depthtest = mesh->depthtest;
732                 if (mesh_depthtest)
733                         qglEnable(GL_DEPTH_TEST);
734                 else
735                         qglDisable(GL_DEPTH_TEST);
736         }
737         if (mesh_depthmask != mesh->depthmask)
738         {
739                 qglDepthMask(mesh_depthmask = mesh->depthmask);CHECKGLERROR
740         }
741 }
742
743 void GL_DrawRangeElements(int firstvert, int endvert, int indexcount, GLuint *index)
744 {
745         unsigned int i, j, in;
746         if (gl_mesh_drawmode.integer >= 3/* && (endvert - firstvert) <= gl_maxdrawrangeelementsvertices && (indexcount) <= gl_maxdrawrangeelementsindices*/)
747         {
748                 // GL 1.2 or GL 1.1 with extension
749                 qglDrawRangeElements(GL_TRIANGLES, firstvert, endvert, indexcount, GL_UNSIGNED_INT, index);
750         }
751         else if (gl_mesh_drawmode.integer >= 2)
752         {
753                 // GL 1.1
754                 qglDrawElements(GL_TRIANGLES, indexcount, GL_UNSIGNED_INT, index);
755         }
756         else if (gl_mesh_drawmode.integer >= 1)
757         {
758                 // GL 1.1
759                 // feed it manually using glArrayElement
760                 qglBegin(GL_TRIANGLES);
761                 for (i = 0;i < indexcount;i++)
762                         qglArrayElement(index[i]);
763                 qglEnd();
764         }
765         else
766         {
767                 // GL 1.1 but not using vertex arrays - 3dfx glquake minigl driver
768                 // feed it manually
769                 qglBegin(GL_TRIANGLES);
770                 if (r_multitexture.integer)
771                 {
772                         // the minigl doesn't have this (because it does not have ARB_multitexture)
773                         for (i = 0;i < indexcount;i++)
774                         {
775                                 in = index[i];
776                                 qglColor4ub(buf_bcolor[in].c[0], buf_bcolor[in].c[1], buf_bcolor[in].c[2], buf_bcolor[in].c[3]);
777                                 for (j = 0;j < backendunits;j++)
778                                         if (mesh_texture[j])
779                                                 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, buf_texcoord[j][in].t[0], buf_texcoord[j][in].t[1]);
780                                 qglVertex3f(buf_vertex[in].v[0], buf_vertex[in].v[1], buf_vertex[in].v[2]);
781                         }
782                 }
783                 else
784                 {
785                         for (i = 0;i < indexcount;i++)
786                         {
787                                 in = index[i];
788                                 qglColor4ub(buf_bcolor[in].c[0], buf_bcolor[in].c[1], buf_bcolor[in].c[2], buf_bcolor[in].c[3]);
789                                 if (mesh_texture[0])
790                                         qglTexCoord2f(buf_texcoord[0][in].t[0], buf_texcoord[0][in].t[1]);
791                                 qglVertex3f(buf_vertex[in].v[0], buf_vertex[in].v[1], buf_vertex[in].v[2]);
792                         }
793                 }
794                 qglEnd();
795         }
796 }
797
798 // renders mesh buffers, called to flush buffers when full
799 void R_Mesh_Render(void)
800 {
801         int i;
802         int k;
803         int indexcount;
804         int firstvert;
805         buf_mesh_t *mesh;
806         unsigned int *index;
807
808         if (!backendactive)
809                 Sys_Error("R_Mesh_Render: called when backend is not active\n");
810
811         if (!currentmesh)
812                 return;
813
814         if (!r_render.integer)
815         {
816                 currentmesh = 0;
817                 currenttriangle = 0;
818                 currentvertex = 0;
819                 return;
820         }
821
822         CHECKGLERROR
823
824         GL_UpdateFarclip();
825
826         // drawmode 0 always uses byte colors
827         if (!gl_mesh_floatcolors.integer || gl_mesh_drawmode.integer <= 0)
828                 GL_ConvertColorsFloatToByte();
829
830         if (gl_backend_rebindtextures)
831         {
832                 gl_backend_rebindtextures = false;
833                 GL_SetupTextureState();
834         }
835
836         GL_MeshState(buf_mesh);
837         GL_LockArray(0, currentvertex);
838         GL_DrawRangeElements(buf_mesh->firstvert, buf_mesh->firstvert + buf_mesh->verts, buf_mesh->triangles * 3, (unsigned int *)&buf_tri[buf_mesh->firsttriangle].index[0]);CHECKGLERROR
839
840         if (currentmesh >= 2)
841         {
842                 for (k = 1, mesh = buf_mesh + k;k < currentmesh;k++, mesh++)
843                 {
844                         GL_MeshState(mesh);
845
846                         firstvert = mesh->firstvert;
847                         indexcount = mesh->triangles * 3;
848                         index = (unsigned int *)&buf_tri[mesh->firsttriangle].index[0];
849
850                         // if not using batching, skip the index adjustment
851                         if (firstvert != 0)
852                                 for (i = 0;i < indexcount;i++)
853                                         index[i] += firstvert;
854
855                         GL_DrawRangeElements(firstvert, firstvert + mesh->verts, indexcount, index);CHECKGLERROR
856                 }
857         }
858
859         currentmesh = 0;
860         currenttriangle = 0;
861         currentvertex = 0;
862
863         GL_UnlockArray();CHECKGLERROR
864 }
865
866 // restores backend state, used when done with 3D rendering
867 void R_Mesh_Finish(void)
868 {
869         int i;
870         // flush any queued meshs
871         if (currentmesh)
872                 R_Mesh_Render();
873
874         if (backendunits > 1)
875         {
876                 for (i = backendunits - 1;i >= 0;i--)
877                 {
878                         qglActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
879                         qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
880                         if (gl_combine.integer)
881                         {
882                                 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
883                         }
884                         if (i > 0)
885                         {
886                                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
887                         }
888                         else
889                         {
890                                 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
891                         }
892                         qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
893
894                         if (usedarrays)
895                         {
896                                 qglClientActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
897                                 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
898                         }
899                 }
900         }
901         else
902         {
903                 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
904                 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
905                 if (usedarrays)
906                 {
907                         qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
908                 }
909         }
910         if (usedarrays)
911         {
912                 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
913                 qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
914         }
915
916         qglDisable(GL_BLEND);CHECKGLERROR
917         qglEnable(GL_DEPTH_TEST);CHECKGLERROR
918         qglDepthMask(GL_TRUE);CHECKGLERROR
919         qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);CHECKGLERROR
920 }
921
922 void R_Mesh_ClearDepth(void)
923 {
924         R_Mesh_AddTransparent();
925         R_Mesh_Finish();
926         qglClear(GL_DEPTH_BUFFER_BIT);
927         R_Mesh_Start();
928 }
929
930 void R_Mesh_AddTransparent(void)
931 {
932         int i, j, k, *index;
933         float viewdistcompare, centerscaler, dist1, dist2, dist3, center, maxdist;
934         buf_vertex_t *vert1, *vert2, *vert3;
935         buf_transtri_t *tri;
936         buf_mesh_t *mesh, *transmesh;
937
938         if (!currenttransmesh)
939                 return;
940
941         // convert index data to transtris for sorting
942         for (j = 0;j < currenttransmesh;j++)
943         {
944                 mesh = buf_transmesh + j;
945                 k = mesh->firsttriangle;
946                 index = &buf_transtri[k].index[0];
947                 for (i = 0;i < mesh->triangles;i++)
948                 {
949                         tri = &buf_sorttranstri[k++];
950                         tri->mesh = mesh;
951                         tri->index[0] = *index++;
952                         tri->index[1] = *index++;
953                         tri->index[2] = *index++;
954                 }
955         }
956
957         // map farclip to 0-4095 list range
958         centerscaler = (TRANSDEPTHRES / r_farclip) * (1.0f / 3.0f);
959         viewdistcompare = viewdist + 4.0f;
960
961         memset(buf_sorttranstri_list, 0, TRANSDEPTHRES * sizeof(buf_transtri_t *));
962
963         k = 0;
964         for (j = 0;j < currenttranstriangle;j++)
965         {
966                 tri = &buf_sorttranstri[j];
967                 i = tri->mesh->firstvert;
968
969                 vert1 = &buf_transvertex[tri->index[0] + i];
970                 vert2 = &buf_transvertex[tri->index[1] + i];
971                 vert3 = &buf_transvertex[tri->index[2] + i];
972
973                 dist1 = DotProduct(vert1->v, vpn);
974                 dist2 = DotProduct(vert2->v, vpn);
975                 dist3 = DotProduct(vert3->v, vpn);
976
977                 maxdist = max(dist1, max(dist2, dist3));
978                 if (maxdist < viewdistcompare)
979                         continue;
980
981                 center = (dist1 + dist2 + dist3) * centerscaler - viewdist;
982 #if SLOWMATH
983                 i = (int) center;
984                 i = bound(0, i, (TRANSDEPTHRES - 1));
985 #else
986                 if (center < 0.0f)
987                         center = 0.0f;
988                 center += 8388608.0f;
989                 i = *((int *)&center) & 0x7FFFFF;
990                 i = min(i, (TRANSDEPTHRES - 1));
991 #endif
992                 tri->next = buf_sorttranstri_list[i];
993                 buf_sorttranstri_list[i] = tri;
994                 k++;
995         }
996
997         for (i = 0;i < currenttransmesh;i++)
998                 buf_transmesh[i].transchain = NULL;
999         transmesh = NULL;
1000         for (j = 0;j < TRANSDEPTHRES;j++)
1001         {
1002                 if ((tri = buf_sorttranstri_list[j]))
1003                 {
1004                         for (;tri;tri = tri->next)
1005                         {
1006                                 if (!tri->mesh->transchain)
1007                                 {
1008                                         tri->mesh->chain = transmesh;
1009                                         transmesh = tri->mesh;
1010                                 }
1011                                 tri->meshsortchain = tri->mesh->transchain;
1012                                 tri->mesh->transchain = tri;
1013                         }
1014                 }
1015         }
1016
1017         for (;transmesh;transmesh = transmesh->chain)
1018         {
1019                 if (currentmesh >= max_meshs || currenttriangle + transmesh->triangles > max_batch || currenttriangle + transmesh->triangles > 1024 || currentvertex + transmesh->verts > max_verts)
1020                         R_Mesh_Render();
1021
1022                 mesh = &buf_mesh[currentmesh++];
1023                 *mesh = *transmesh; // copy mesh properties
1024
1025                 mesh->firstvert = currentvertex;
1026                 memcpy(&buf_vertex[currentvertex], &buf_transvertex[transmesh->firstvert], transmesh->verts * sizeof(buf_vertex_t));
1027                 memcpy(&buf_fcolor[currentvertex], &buf_transfcolor[transmesh->firstvert], transmesh->verts * sizeof(buf_fcolor_t));
1028                 for (i = 0;i < backendunits && transmesh->textures[i];i++)
1029                         memcpy(&buf_texcoord[i][currentvertex], &buf_transtexcoord[i][transmesh->firstvert], transmesh->verts * sizeof(buf_texcoord_t));
1030                 currentvertex += mesh->verts;
1031
1032                 mesh->firsttriangle = currenttriangle;
1033                 for (tri = transmesh->transchain;tri;tri = tri->meshsortchain)
1034                 {
1035                         buf_tri[currenttriangle].index[0] = tri->index[0];
1036                         buf_tri[currenttriangle].index[1] = tri->index[1];
1037                         buf_tri[currenttriangle].index[2] = tri->index[2];
1038                         currenttriangle++;
1039                 }
1040                 mesh->triangles = currenttriangle - mesh->firsttriangle;
1041         }
1042
1043         currenttransmesh = 0;
1044         currenttranstriangle = 0;
1045         currenttransvertex = 0;
1046 }
1047
1048 void R_Mesh_Draw(const rmeshinfo_t *m)
1049 {
1050         // these are static because gcc runs out of virtual registers otherwise
1051         static int i, j, overbright, *index;
1052         static float *in, scaler;
1053         static float cr, cg, cb, ca;
1054         static buf_mesh_t *mesh;
1055         static buf_vertex_t *vert;
1056         static buf_fcolor_t *fcolor;
1057         static buf_texcoord_t *texcoord[MAX_TEXTUREUNITS];
1058
1059         if (!backendactive)
1060                 Sys_Error("R_Mesh_Draw: called when backend is not active\n");
1061
1062         if (m->index == NULL
1063          || !m->numtriangles
1064          || m->vertex == NULL
1065          || !m->numverts)
1066                 Host_Error("R_Mesh_Draw: no triangles or verts\n");
1067
1068         // ignore meaningless alpha meshs
1069         if (!m->depthwrite && m->blendfunc1 == GL_SRC_ALPHA && (m->blendfunc2 == GL_ONE || m->blendfunc2 == GL_ONE_MINUS_SRC_ALPHA))
1070         {
1071                 if (m->color)
1072                 {
1073                         for (i = 0, in = m->color + 3;i < m->numverts;i++, (int)in += m->colorstep)
1074                                 if (*in >= 0.01f)
1075                                         break;
1076                         if (i == m->numverts)
1077                                 return;
1078                 }
1079                 else if (m->ca < 0.01f)
1080                         return;
1081         }
1082
1083         if (!backendactive)
1084                 Sys_Error("R_Mesh_Draw: called when backend is not active\n");
1085
1086 #ifdef DEBUGGL
1087         for (i = 0;i < m->numtriangles * 3;i++)
1088                 if ((unsigned int) m->index[i] >= (unsigned int) m->numverts)
1089                         Host_Error("R_Mesh_Draw: invalid index (%i of %i verts)\n", m->index, m->numverts);
1090 #endif
1091
1092         // LordHavoc: removed this error condition because with floatcolors 0,
1093         // the 3DFX driver works with very large meshs
1094         // FIXME: we can work around this by falling back on non-array renderer if buffers are too big
1095         //if (m->numtriangles > 1024 || m->numverts > 3072)
1096         //{
1097         //      Con_Printf("R_Mesh_Draw: mesh too big for 3DFX drivers, rejected\n");
1098         //      return;
1099         //}
1100
1101         i = max(m->numtriangles * 3, m->numverts);
1102         if (overflowedverts < i)
1103                 overflowedverts = i;
1104
1105         if (m->numtriangles > max_meshs || m->numverts > max_verts)
1106         {
1107                 Con_Printf("R_Mesh_Draw: mesh too big for current gl_mesh_maxtriangles setting, increasing limits\n");
1108                 return;
1109         }
1110
1111         if (m->transparent)
1112         {
1113                 overflowedtransverts += max(m->numtriangles * 3, m->numverts);
1114                 if (currenttransmesh >= max_transmeshs || (currenttranstriangle + m->numtriangles) > max_transmeshs || (currenttransvertex + m->numverts) > max_transverts)
1115                 {
1116                         if (!transranout)
1117                         {
1118                                 Con_Printf("R_Mesh_Draw: ran out of room for transparent meshs\n");
1119                                 transranout = true;
1120                         }
1121                         return;
1122                 }
1123
1124                 c_transmeshs++;
1125                 c_transtris += m->numtriangles;
1126                 vert = &buf_transvertex[currenttransvertex];
1127                 fcolor = &buf_transfcolor[currenttransvertex];
1128                 for (i = 0;i < backendunits;i++)
1129                         texcoord[i] = &buf_transtexcoord[i][currenttransvertex];
1130
1131                 // transmesh is only for storage of transparent meshs until they
1132                 // are inserted into the main mesh array
1133                 mesh = &buf_transmesh[currenttransmesh++];
1134                 mesh->firsttriangle = currenttranstriangle;
1135                 mesh->firstvert = currenttransvertex;
1136                 index = &buf_transtri[currenttranstriangle].index[0];
1137
1138                 currenttranstriangle += m->numtriangles;
1139                 currenttransvertex += m->numverts;
1140         }
1141         else
1142         {
1143                 if (currentmesh >= max_meshs || (currenttriangle + m->numtriangles) > max_batch || (currentvertex + m->numverts) > max_verts)
1144                         R_Mesh_Render();
1145
1146                 c_meshs++;
1147                 c_meshtris += m->numtriangles;
1148                 vert = &buf_vertex[currentvertex];
1149                 fcolor = &buf_fcolor[currentvertex];
1150                 for (i = 0;i < backendunits;i++)
1151                         texcoord[i] = &buf_texcoord[i][currentvertex];
1152
1153                 // opaque meshs are rendered directly
1154                 mesh = &buf_mesh[currentmesh++];
1155                 mesh->firsttriangle = currenttriangle;
1156                 mesh->firstvert = currentvertex;
1157                 index = &buf_tri[currenttriangle].index[0];
1158
1159                 currenttriangle += m->numtriangles;
1160                 currentvertex += m->numverts;
1161         }
1162
1163         // code shared for transparent and opaque meshs
1164         memcpy(index, m->index, sizeof(int[3]) * m->numtriangles);
1165         mesh->blendfunc1 = m->blendfunc1;
1166         mesh->blendfunc2 = m->blendfunc2;
1167         mesh->depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite);
1168         mesh->depthtest = !m->depthdisable;
1169         mesh->triangles = m->numtriangles;
1170         mesh->verts = m->numverts;
1171
1172         overbright = false;
1173         scaler = 1;
1174         if (m->blendfunc1 == GL_DST_COLOR)
1175         {
1176                 // check if it is a 2x modulate with framebuffer
1177                 if (m->blendfunc2 == GL_SRC_COLOR)
1178                         scaler *= 0.5f;
1179         }
1180         else if (m->blendfunc2 != GL_SRC_COLOR)
1181         {
1182                 if (m->tex[0])
1183                 {
1184                         overbright = gl_combine.integer;
1185                         if (overbright)
1186                                 scaler *= 0.25f;
1187                 }
1188                 scaler *= overbrightscale;
1189         }
1190
1191
1192         j = -1;
1193         for (i = 0;i < backendunits;i++)
1194         {
1195                 if ((mesh->textures[i] = m->tex[i]))
1196                         j = i;
1197                 mesh->texturergbscale[i] = m->texrgbscale[i];
1198                 if (mesh->texturergbscale[i] != 1 && mesh->texturergbscale[i] != 2 && mesh->texturergbscale[i] != 4)
1199                         mesh->texturergbscale[i] = 1;
1200         }
1201         if (overbright && j >= 0)
1202                 mesh->texturergbscale[j] = 4;
1203
1204         if (m->vertexstep != sizeof(buf_vertex_t))
1205         {
1206                 for (i = 0, in = m->vertex;i < m->numverts;i++, (int)in += m->vertexstep)
1207                 {
1208                         vert[i].v[0] = in[0];
1209                         vert[i].v[1] = in[1];
1210                         vert[i].v[2] = in[2];
1211                 }
1212         }
1213         else
1214                 memcpy(vert, m->vertex, m->numverts * sizeof(buf_vertex_t));
1215
1216         if (m->color)
1217         {
1218                 for (i = 0, in = m->color;i < m->numverts;i++, (int)in += m->colorstep)
1219                 {
1220                         fcolor[i].c[0] = in[0] * scaler;
1221                         fcolor[i].c[1] = in[1] * scaler;
1222                         fcolor[i].c[2] = in[2] * scaler;
1223                         fcolor[i].c[3] = in[3];
1224                 }
1225         }
1226         else
1227         {
1228                 cr = m->cr * scaler;
1229                 cg = m->cg * scaler;
1230                 cb = m->cb * scaler;
1231                 ca = m->ca;
1232                 for (i = 0;i < m->numverts;i++)
1233                 {
1234                         fcolor[i].c[0] = cr;
1235                         fcolor[i].c[1] = cg;
1236                         fcolor[i].c[2] = cb;
1237                         fcolor[i].c[3] = ca;
1238                 }
1239         }
1240
1241         for (j = 0;j < MAX_TEXTUREUNITS && m->tex[j];j++)
1242         {
1243                 if (j >= backendunits)
1244                         Sys_Error("R_Mesh_Draw: texture %i supplied when there are only %i texture units\n", j + 1, backendunits);
1245                 if (m->texcoordstep[j] != sizeof(buf_texcoord_t))
1246                 {
1247                         for (i = 0, in = m->texcoords[j];i < m->numverts;i++, (int)in += m->texcoordstep[j])
1248                         {
1249                                 texcoord[j][i].t[0] = in[0];
1250                                 texcoord[j][i].t[1] = in[1];
1251                         }
1252                 }
1253                 else
1254                         memcpy(&texcoord[j][0].t[0], m->texcoords[j], m->numverts * sizeof(buf_texcoord_t));
1255         }
1256
1257         if (currenttriangle >= max_batch)
1258                 R_Mesh_Render();
1259 }
1260
1261 // allocates space in geometry buffers, and fills in pointers to the buffers in passsed struct
1262 // (this is used for very high speed rendering, no copying)
1263 int R_Mesh_Draw_GetBuffer(rmeshbufferinfo_t *m)
1264 {
1265         // these are static because gcc runs out of virtual registers otherwise
1266         int i, j, overbright;
1267         float scaler;
1268         buf_mesh_t *mesh;
1269
1270         if (!backendactive)
1271                 Sys_Error("R_Mesh_Draw: called when backend is not active\n");
1272
1273         if (!m->numtriangles
1274          || !m->numverts)
1275                 Host_Error("R_Mesh_Draw: no triangles or verts\n");
1276
1277         // LordHavoc: removed this error condition because with floatcolors 0,
1278         // the 3DFX driver works with very large meshs
1279         // FIXME: we can work around this by falling back on non-array renderer if buffers are too big
1280         //if (m->numtriangles > 1024 || m->numverts > 3072)
1281         //{
1282         //      Con_Printf("R_Mesh_Draw_GetBuffer: mesh too big for 3DFX drivers, rejected\n");
1283         //      return false;
1284         //}
1285
1286         i = max(m->numtriangles * 3, m->numverts);
1287         if (overflowedverts < i)
1288                 overflowedverts = i;
1289
1290         if (m->numtriangles > max_meshs || m->numverts > max_verts)
1291         {
1292                 Con_Printf("R_Mesh_Draw_GetBuffer: mesh too big for current gl_mesh_maxtriangles setting, increasing limits\n");
1293                 return false;
1294         }
1295
1296         if (m->transparent)
1297         {
1298                 overflowedtransverts += max(m->numtriangles * 3, m->numverts);
1299                 if (currenttransmesh >= max_transmeshs || (currenttranstriangle + m->numtriangles) > max_transmeshs || (currenttransvertex + m->numverts) > max_transverts)
1300                 {
1301                         if (!transranout)
1302                         {
1303                                 Con_Printf("R_Mesh_Draw: ran out of room for transparent meshs\n");
1304                                 transranout = true;
1305                         }
1306                         return false;
1307                 }
1308
1309                 c_transmeshs++;
1310                 c_transtris += m->numtriangles;
1311                 m->index = &buf_transtri[currenttranstriangle].index[0];
1312                 m->vertex = &buf_transvertex[currenttransvertex].v[0];
1313                 m->color = &buf_transfcolor[currenttransvertex].c[0];
1314                 for (i = 0;i < backendunits;i++)
1315                         m->texcoords[i] = &buf_transtexcoord[i][currenttransvertex].t[0];
1316
1317                 // transmesh is only for storage of transparent meshs until they
1318                 // are inserted into the main mesh array
1319                 mesh = &buf_transmesh[currenttransmesh++];
1320                 mesh->firsttriangle = currenttranstriangle;
1321                 mesh->firstvert = currenttransvertex;
1322                 currenttranstriangle += m->numtriangles;
1323                 currenttransvertex += m->numverts;
1324         }
1325         else
1326         {
1327                 if (currentmesh >= max_meshs || (currenttriangle + m->numtriangles) > max_batch || (currentvertex + m->numverts) > max_verts)
1328                         R_Mesh_Render();
1329
1330                 c_meshs++;
1331                 c_meshtris += m->numtriangles;
1332                 m->index = &buf_tri[currenttriangle].index[0];
1333                 m->vertex = &buf_vertex[currentvertex].v[0];
1334                 m->color = &buf_fcolor[currentvertex].c[0];
1335                 for (i = 0;i < backendunits;i++)
1336                         m->texcoords[i] = &buf_texcoord[i][currentvertex].t[0];
1337
1338                 // opaque meshs are rendered directly
1339                 mesh = &buf_mesh[currentmesh++];
1340                 mesh->firsttriangle = currenttriangle;
1341                 mesh->firstvert = currentvertex;
1342                 currenttriangle += m->numtriangles;
1343                 currentvertex += m->numverts;
1344         }
1345
1346         // code shared for transparent and opaque meshs
1347         mesh->blendfunc1 = m->blendfunc1;
1348         mesh->blendfunc2 = m->blendfunc2;
1349         mesh->depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite);
1350         mesh->depthtest = !m->depthdisable;
1351         mesh->triangles = m->numtriangles;
1352         mesh->verts = m->numverts;
1353
1354         overbright = false;
1355         scaler = 1;
1356         if (m->blendfunc1 == GL_DST_COLOR)
1357         {
1358                 // check if it is a 2x modulate with framebuffer
1359                 if (m->blendfunc2 == GL_SRC_COLOR)
1360                         scaler *= 0.5f;
1361         }
1362         else if (m->blendfunc2 != GL_SRC_COLOR)
1363         {
1364                 if (m->tex[0])
1365                 {
1366                         overbright = gl_combine.integer;
1367                         if (overbright)
1368                                 scaler *= 0.25f;
1369                 }
1370                 scaler *= overbrightscale;
1371         }
1372         m->colorscale = scaler;
1373
1374         j = -1;
1375         for (i = 0;i < MAX_TEXTUREUNITS;i++)
1376         {
1377                 if ((mesh->textures[i] = m->tex[i]))
1378                 {
1379                         j = i;
1380                         if (i >= backendunits)
1381                                 Sys_Error("R_Mesh_Draw_GetBuffer: texture %i supplied when there are only %i texture units\n", j + 1, backendunits);
1382                 }
1383                 mesh->texturergbscale[i] = m->texrgbscale[i];
1384                 if (mesh->texturergbscale[i] != 1 && mesh->texturergbscale[i] != 2 && mesh->texturergbscale[i] != 4)
1385                         mesh->texturergbscale[i] = 1;
1386         }
1387         if (overbright && j >= 0)
1388                 mesh->texturergbscale[j] = 4;
1389
1390         return true;
1391 }
1392
1393 void R_Mesh_DrawPolygon(rmeshinfo_t *m, int numverts)
1394 {
1395         m->index = polyindexarray;
1396         m->numverts = numverts;
1397         m->numtriangles = numverts - 2;
1398         if (m->numtriangles < 1)
1399         {
1400                 Con_Printf("R_Mesh_DrawPolygon: invalid vertex count\n");
1401                 return;
1402         }
1403         if (m->numtriangles >= 256)
1404         {
1405                 Con_Printf("R_Mesh_DrawPolygon: only up to 256 triangles (258 verts) supported\n");
1406                 return;
1407         }
1408         R_Mesh_Draw(m);
1409 }
1410
1411 /*
1412 ==============================================================================
1413
1414                                                 SCREEN SHOTS
1415
1416 ==============================================================================
1417 */
1418
1419 qboolean SCR_ScreenShot(char *filename, int x, int y, int width, int height)
1420 {
1421         qboolean ret;
1422         int i;
1423         qbyte *buffer;
1424
1425         if (!r_render.integer)
1426                 return false;
1427
1428         buffer = Mem_Alloc(tempmempool, width*height*3);
1429         qglReadPixels (x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer);
1430         CHECKGLERROR
1431
1432         // LordHavoc: compensate for v_overbrightbits when using hardware gamma
1433         if (v_hwgamma.integer)
1434                 for (i = 0;i < width * height * 3;i++)
1435                         buffer[i] <<= v_overbrightbits.integer;
1436
1437         ret = Image_WriteTGARGB_preflipped(filename, width, height, buffer);
1438
1439         Mem_Free(buffer);
1440         return ret;
1441 }
1442
1443 //=============================================================================
1444
1445 void R_ClearScreen(void)
1446 {
1447         if (r_render.integer)
1448         {
1449                 // clear to black
1450                 qglClearColor(0,0,0,0);CHECKGLERROR
1451                 // clear the screen
1452                 qglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);CHECKGLERROR
1453                 // set dithering mode
1454                 if (gl_dither.integer)
1455                 {
1456                         qglEnable(GL_DITHER);CHECKGLERROR
1457                 }
1458                 else
1459                 {
1460                         qglDisable(GL_DITHER);CHECKGLERROR
1461                 }
1462         }
1463 }
1464
1465 /*
1466 ==================
1467 SCR_UpdateScreen
1468
1469 This is called every frame, and can also be called explicitly to flush
1470 text to the screen.
1471 ==================
1472 */
1473 void SCR_UpdateScreen (void)
1474 {
1475         VID_Finish ();
1476
1477         R_TimeReport("finish");
1478
1479         if (gl_combine.integer && !gl_combine_extension)
1480                 Cvar_SetValue("gl_combine", 0);
1481
1482         lightscalebit = v_overbrightbits.integer;
1483         if (gl_combine.integer && r_multitexture.integer)
1484                 lightscalebit += 2;
1485
1486         lightscale = 1.0f / (float) (1 << lightscalebit);
1487         overbrightscale = 1.0f / (float) (1 << v_overbrightbits.integer);
1488
1489         R_TimeReport("setup");
1490
1491         R_ClearScreen();
1492
1493         R_TimeReport("clear");
1494
1495         if (scr_conlines < vid.conheight)
1496                 R_RenderView();
1497
1498         // draw 2D stuff
1499         R_DrawQueue();
1500
1501         // tell driver to commit it's partially full geometry queue to the rendering queue
1502         // (this doesn't wait for the commands themselves to complete)
1503         qglFlush();
1504 }
1505