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