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