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