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