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"};
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"};
14 // this is used to increase gl_mesh_maxtriangles automatically if a mesh was
15 // too large for the buffers in the previous frame
16 int overflowedmeshtris = 0;
18 int gl_maxdrawrangeelementsvertices;
19 int gl_maxdrawrangeelementsindices;
24 void GL_PrintError(int errornumber, char *filename, int linenumber)
28 #ifdef GL_INVALID_ENUM
30 Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
33 #ifdef GL_INVALID_VALUE
34 case GL_INVALID_VALUE:
35 Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
38 #ifdef GL_INVALID_OPERATION
39 case GL_INVALID_OPERATION:
40 Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
43 #ifdef GL_STACK_OVERFLOW
44 case GL_STACK_OVERFLOW:
45 Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
48 #ifdef GL_STACK_UNDERFLOW
49 case GL_STACK_UNDERFLOW:
50 Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
53 #ifdef GL_OUT_OF_MEMORY
54 case GL_OUT_OF_MEMORY:
55 Con_Printf("GL_OUT_OF_MEMORY at %s:%i\n", filename, linenumber);
58 #ifdef GL_TABLE_TOO_LARGE
59 case GL_TABLE_TOO_LARGE:
60 Con_Printf("GL_TABLE_TOO_LARGE at %s:%i\n", filename, linenumber);
64 Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
70 float r_farclip, r_newfarclip;
72 int polyindexarray[768];
74 static float viewdist;
76 int c_meshs, c_meshtris, c_transmeshs, c_transtris;
80 float overbrightscale;
82 void SCR_ScreenShot_f (void);
85 static int max_transmeshs;
87 static int max_verts; // always max_meshs * 3
88 static int max_transverts; // always max_transmeshs * 3
89 #define TRANSDEPTHRES 4096
91 typedef struct buf_mesh_s
95 int blendfunc1, blendfunc2;
96 int textures[MAX_TEXTUREUNITS];
97 int texturergbscale[MAX_TEXTUREUNITS];
102 struct buf_mesh_s *chain;
103 struct buf_transtri_s *transchain;
107 typedef struct buf_transtri_s
109 struct buf_transtri_s *next;
110 struct buf_transtri_s *meshsortchain;
116 typedef struct buf_tri_s
146 static float meshfarclip;
147 static int currentmesh, currenttriangle, currentvertex, backendunits, backendactive, transranout;
148 static buf_mesh_t *buf_mesh;
149 static buf_tri_t *buf_tri;
150 static buf_vertex_t *buf_vertex;
151 static buf_fcolor_t *buf_fcolor;
152 static buf_bcolor_t *buf_bcolor;
153 static buf_texcoord_t *buf_texcoord[MAX_TEXTUREUNITS];
155 static int currenttransmesh, currenttransvertex, currenttranstriangle;
156 static buf_mesh_t *buf_transmesh;
157 static buf_transtri_t *buf_sorttranstri;
158 static buf_transtri_t **buf_sorttranstri_list;
159 static buf_tri_t *buf_transtri;
160 static buf_vertex_t *buf_transvertex;
161 static buf_fcolor_t *buf_transfcolor;
162 static buf_texcoord_t *buf_transtexcoord[MAX_TEXTUREUNITS];
164 static mempool_t *gl_backend_mempool;
165 static int resizingbuffers = false;
167 static void gl_backend_start(void)
171 max_verts = max_meshs * 3;
172 max_transverts = max_transmeshs * 3;
174 if (!gl_backend_mempool)
175 gl_backend_mempool = Mem_AllocPool("GL_Backend");
177 #define BACKENDALLOC(var, count, sizeofstruct, varname)\
179 var = Mem_Alloc(gl_backend_mempool, count * sizeof(sizeofstruct));\
181 Sys_Error("gl_backend_start: unable to allocate memory for %s (%d bytes)\n", (varname), count * sizeof(sizeofstruct));\
182 memset(var, 0, count * sizeof(sizeofstruct));\
185 BACKENDALLOC(buf_mesh, max_meshs, buf_mesh_t, "buf_mesh")
186 BACKENDALLOC(buf_tri, max_meshs, buf_tri_t, "buf_tri")
187 BACKENDALLOC(buf_vertex, max_verts, buf_vertex_t, "buf_vertex")
188 BACKENDALLOC(buf_fcolor, max_verts, buf_fcolor_t, "buf_fcolor")
189 BACKENDALLOC(buf_bcolor, max_verts, buf_bcolor_t, "buf_bcolor")
191 BACKENDALLOC(buf_transmesh, max_transmeshs, buf_mesh_t, "buf_transmesh")
192 BACKENDALLOC(buf_sorttranstri, max_transmeshs, buf_transtri_t, "buf_sorttranstri")
193 BACKENDALLOC(buf_sorttranstri_list, TRANSDEPTHRES, buf_transtri_t *, "buf_sorttranstri_list")
194 BACKENDALLOC(buf_transtri, max_transmeshs, buf_tri_t, "buf_transtri")
195 BACKENDALLOC(buf_transvertex, max_transverts, buf_vertex_t, "buf_vertex")
196 BACKENDALLOC(buf_transfcolor, max_transverts, buf_fcolor_t, "buf_fcolor")
198 for (i = 0;i < MAX_TEXTUREUNITS;i++)
200 // only allocate as many texcoord arrays as we need
201 if (i < gl_textureunits)
203 BACKENDALLOC(buf_texcoord[i], max_verts, buf_texcoord_t, va("buf_texcoord[%d]", i))
204 BACKENDALLOC(buf_transtexcoord[i], max_transverts, buf_texcoord_t, va("buf_transtexcoord[%d]", i))
208 buf_texcoord[i] = NULL;
209 buf_transtexcoord[i] = NULL;
212 backendunits = min(MAX_TEXTUREUNITS, gl_textureunits);
213 backendactive = true;
216 static void gl_backend_shutdown(void)
219 Mem_EmptyPool(gl_backend_mempool);
221 Mem_FreePool(&gl_backend_mempool);
224 backendactive = false;
227 static void gl_backend_bufferchanges(int init)
229 if (overflowedmeshtris > gl_mesh_maxtriangles.integer)
230 Cvar_SetValueQuick(&gl_mesh_maxtriangles, overflowedmeshtris);
231 overflowedmeshtris = 0;
233 if (gl_mesh_drawmode.integer < 0)
234 Cvar_SetValueQuick(&gl_mesh_drawmode, 0);
235 if (gl_mesh_drawmode.integer > 3)
236 Cvar_SetValueQuick(&gl_mesh_drawmode, 3);
238 if (gl_mesh_drawmode.integer >= 3)
240 if (qglDrawRangeElements != NULL && gl_maxdrawrangeelementsindices >= 3072 && gl_maxdrawrangeelementsvertices >= 3072)
242 // make sure we don't exceed the DrawRangeElements limits
243 if (gl_mesh_maxtriangles.integer * 3 > gl_maxdrawrangeelementsindices)
244 Cvar_SetValueQuick(&gl_mesh_maxtriangles, (int) (gl_maxdrawrangeelementsindices / 3));
245 if (gl_mesh_maxtriangles.integer * 3 > gl_maxdrawrangeelementsvertices)
246 Cvar_SetValueQuick(&gl_mesh_maxtriangles, (int) (gl_maxdrawrangeelementsvertices / 3));
250 // change drawmode 3 to 2 if 3 won't work
251 Cvar_SetValueQuick(&gl_mesh_drawmode, 2);
255 // 21760 is (65536 / 3) rounded off to a multiple of 128
256 if (gl_mesh_maxtriangles.integer < 1024)
257 Cvar_SetValueQuick(&gl_mesh_maxtriangles, 1024);
258 if (gl_mesh_maxtriangles.integer > 21760)
259 Cvar_SetValueQuick(&gl_mesh_maxtriangles, 21760);
261 if (gl_mesh_transtriangles.integer < 1024)
262 Cvar_SetValueQuick(&gl_mesh_transtriangles, 1024);
263 if (gl_mesh_transtriangles.integer > 65536)
264 Cvar_SetValueQuick(&gl_mesh_transtriangles, 65536);
266 if (gl_mesh_batchtriangles.integer < 0)
267 Cvar_SetValueQuick(&gl_mesh_batchtriangles, 0);
268 if (gl_mesh_batchtriangles.integer > gl_mesh_maxtriangles.integer)
269 Cvar_SetValueQuick(&gl_mesh_batchtriangles, gl_mesh_maxtriangles.integer);
271 max_batch = gl_mesh_batchtriangles.integer;
273 if (max_meshs != gl_mesh_maxtriangles.integer || max_transmeshs != gl_mesh_transtriangles.integer)
275 max_meshs = gl_mesh_maxtriangles.integer;
276 max_transmeshs = gl_mesh_transtriangles.integer;
280 resizingbuffers = true;
281 gl_backend_shutdown();
283 resizingbuffers = false;
288 static void gl_backend_newmap(void)
290 r_farclip = r_newfarclip = 2048.0f;
293 void gl_backend_init(void)
297 Cvar_RegisterVariable(&r_render);
298 Cvar_RegisterVariable(&gl_dither);
299 Cvar_RegisterVariable(&gl_lockarrays);
301 Cvar_SetValue("r_render", 0);
304 Cvar_RegisterVariable(&gl_mesh_maxtriangles);
305 Cvar_RegisterVariable(&gl_mesh_transtriangles);
306 Cvar_RegisterVariable(&gl_mesh_batchtriangles);
307 Cvar_RegisterVariable(&gl_mesh_floatcolors);
308 Cvar_RegisterVariable(&gl_mesh_drawmode);
309 R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap);
310 gl_backend_bufferchanges(true);
311 for (i = 0;i < 256;i++)
313 polyindexarray[i*3+0] = 0;
314 polyindexarray[i*3+1] = i + 1;
315 polyindexarray[i*3+2] = i + 2;
319 int arraylocked = false;
321 void GL_LockArray(int first, int count)
323 if (!arraylocked && gl_supportslockarrays && gl_lockarrays.integer && gl_mesh_drawmode.integer > 0)
325 qglLockArraysEXT(first, count);
331 void GL_UnlockArray(void)
335 qglUnlockArraysEXT();
346 static void GL_SetupFrame (void)
349 double fovx, fovy, zNear, zFar, aspect;
351 // update farclip based on previous frame
352 r_farclip = r_newfarclip;
354 if (!r_render.integer)
357 qglDepthFunc (GL_LEQUAL);CHECKGLERROR
360 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
361 qglLoadIdentity ();CHECKGLERROR
363 // y is weird beause OpenGL is bottom to top, we use top to bottom
364 qglViewport(r_refdef.x, vid.realheight - (r_refdef.y + r_refdef.height), r_refdef.width, r_refdef.height);CHECKGLERROR
371 fovx = r_refdef.fov_x;
372 fovy = r_refdef.fov_y;
373 aspect = r_refdef.width / r_refdef.height;
376 xmax = zNear * tan(fovx * M_PI / 360.0) * aspect;
377 ymax = zNear * tan(fovy * M_PI / 360.0);
380 qglFrustum(-xmax, xmax, -ymax, ymax, zNear, zFar);CHECKGLERROR
382 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
383 qglLoadIdentity ();CHECKGLERROR
386 qglRotatef (-90, 1, 0, 0);CHECKGLERROR
387 qglRotatef (90, 0, 0, 1);CHECKGLERROR
389 qglRotatef (-r_refdef.viewangles[2], 1, 0, 0);CHECKGLERROR
390 qglRotatef (-r_refdef.viewangles[0], 0, 1, 0);CHECKGLERROR
391 qglRotatef (-r_refdef.viewangles[1], 0, 0, 1);CHECKGLERROR
393 qglTranslatef (-r_refdef.vieworg[0], -r_refdef.vieworg[1], -r_refdef.vieworg[2]);CHECKGLERROR
396 static int mesh_blendfunc1;
397 static int mesh_blendfunc2;
398 static int mesh_blend;
399 static GLboolean mesh_depthmask;
400 static int mesh_depthtest;
401 static int mesh_unit;
402 static int mesh_clientunit;
403 static int mesh_texture[MAX_TEXTUREUNITS];
404 static float mesh_texturergbscale[MAX_TEXTUREUNITS];
406 void GL_SetupTextureState(void)
409 if (backendunits > 1)
411 for (i = 0;i < backendunits;i++)
413 qglActiveTexture(GL_TEXTURE0_ARB + (mesh_unit = i));CHECKGLERROR
414 qglBindTexture(GL_TEXTURE_2D, mesh_texture[i]);CHECKGLERROR
415 if (gl_combine.integer)
417 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);CHECKGLERROR
418 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);CHECKGLERROR
419 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);CHECKGLERROR
420 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
421 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_CONSTANT_ARB);CHECKGLERROR
422 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
423 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
424 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);CHECKGLERROR
425 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);CHECKGLERROR
426 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);CHECKGLERROR
427 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
428 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, GL_CONSTANT_ARB);CHECKGLERROR
429 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
430 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
431 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
432 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, mesh_texturergbscale[i]);CHECKGLERROR
433 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
437 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
441 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
445 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
447 if (gl_mesh_drawmode.integer > 0)
449 qglClientActiveTexture(GL_TEXTURE0_ARB + (mesh_clientunit = i));CHECKGLERROR
450 qglTexCoordPointer(2, GL_FLOAT, sizeof(buf_texcoord_t), buf_texcoord[i]);CHECKGLERROR
453 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
457 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
464 qglBindTexture(GL_TEXTURE_2D, mesh_texture[0]);CHECKGLERROR
465 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
468 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
472 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
474 if (gl_mesh_drawmode.integer > 0)
476 qglTexCoordPointer(2, GL_FLOAT, sizeof(buf_texcoord_t), buf_texcoord[0]);CHECKGLERROR
479 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
483 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
489 // called at beginning of frame
491 void R_Mesh_Start(void)
495 Sys_Error("R_Mesh_Clear: called when backend is not active\n");
499 gl_backend_bufferchanges(false);
504 currenttransmesh = 0;
505 currenttranstriangle = 0;
506 currenttransvertex = 0;
509 viewdist = DotProduct(r_origin, vpn);
521 for (i = 0;i < backendunits;i++)
524 mesh_texturergbscale[i] = 1;
527 qglEnable(GL_CULL_FACE);CHECKGLERROR
528 qglCullFace(GL_FRONT);CHECKGLERROR
530 mesh_depthtest = true;
531 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
533 mesh_blendfunc1 = GL_ONE;
534 mesh_blendfunc2 = GL_ZERO;
535 qglBlendFunc(mesh_blendfunc1, mesh_blendfunc2);CHECKGLERROR
538 qglDisable(GL_BLEND);CHECKGLERROR
540 mesh_depthmask = GL_TRUE;
541 qglDepthMask(mesh_depthmask);CHECKGLERROR
544 if (gl_mesh_drawmode.integer > 0)
547 qglVertexPointer(3, GL_FLOAT, sizeof(buf_vertex_t), &buf_vertex[0].v[0]);CHECKGLERROR
548 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
549 if (gl_mesh_floatcolors.integer)
551 qglColorPointer(4, GL_FLOAT, sizeof(buf_fcolor_t), &buf_fcolor[0].c[0]);CHECKGLERROR
555 qglColorPointer(4, GL_UNSIGNED_BYTE, sizeof(buf_bcolor_t), &buf_bcolor[0].c[0]);CHECKGLERROR
557 qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
560 GL_SetupTextureState();
563 int gl_backend_rebindtextures;
565 void GL_UpdateFarclip(void)
570 // push out farclip based on vertices
571 // FIXME: wouldn't this be slow when using matrix transforms?
572 for (i = 0;i < currentvertex;i++)
574 farclip = DotProduct(buf_vertex[i].v, vpn);
575 if (meshfarclip < farclip)
576 meshfarclip = farclip;
579 farclip = meshfarclip + 256.0f - viewdist; // + 256 just to be safe
581 // push out farclip for next frame
582 if (farclip > r_newfarclip)
583 r_newfarclip = ceil((farclip + 255) / 256) * 256 + 256;
586 void GL_ConvertColorsFloatToByte(void)
589 // LordHavoc: to avoid problems with aliasing (treating memory as two
590 // different types - exactly what this is doing), these must be volatile
592 volatile int *icolor;
593 volatile float *fcolor;
596 total = currentvertex * 4;
598 // shift float to have 8bit fraction at base of number
599 fcolor = &buf_fcolor->c[0];
600 for (i = 0;i < total;)
602 fcolor[i ] += 32768.0f;
603 fcolor[i + 1] += 32768.0f;
604 fcolor[i + 2] += 32768.0f;
605 fcolor[i + 3] += 32768.0f;
609 // then read as integer and kill float bits...
610 icolor = (int *)&buf_fcolor->c[0];
611 bcolor = &buf_bcolor->c[0];
612 for (i = 0;i < total;)
614 k = icolor[i ] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i ] = (qbyte) k;
615 k = icolor[i + 1] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i + 1] = (qbyte) k;
616 k = icolor[i + 2] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i + 2] = (qbyte) k;
617 k = icolor[i + 3] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i + 3] = (qbyte) k;
622 void GL_MeshState(buf_mesh_t *mesh)
625 if (backendunits > 1)
627 for (i = 0;i < backendunits;i++)
629 if (mesh_texture[i] != mesh->textures[i])
633 qglActiveTexture(GL_TEXTURE0_ARB + (mesh_unit = i));CHECKGLERROR
635 if (mesh_texture[i] == 0)
637 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
638 // have to disable texcoord array on disabled texture
639 // units due to NVIDIA driver bug with
640 // compiled_vertex_array
641 if (mesh_clientunit != i)
643 qglClientActiveTexture(GL_TEXTURE0_ARB + (mesh_clientunit = i));CHECKGLERROR
645 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
647 qglBindTexture(GL_TEXTURE_2D, (mesh_texture[i] = mesh->textures[i]));CHECKGLERROR
648 if (mesh_texture[i] == 0)
650 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
651 // have to disable texcoord array on disabled texture
652 // units due to NVIDIA driver bug with
653 // compiled_vertex_array
654 if (mesh_clientunit != i)
656 qglClientActiveTexture(GL_TEXTURE0_ARB + (mesh_clientunit = i));CHECKGLERROR
658 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
661 if (mesh_texturergbscale[i] != mesh->texturergbscale[i])
665 qglActiveTexture(GL_TEXTURE0_ARB + (mesh_unit = i));CHECKGLERROR
667 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (mesh_texturergbscale[i] = mesh->texturergbscale[i]));CHECKGLERROR
673 if (mesh_texture[0] != mesh->textures[0])
675 if (mesh_texture[0] == 0)
677 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
678 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
680 qglBindTexture(GL_TEXTURE_2D, (mesh_texture[0] = mesh->textures[0]));CHECKGLERROR
681 if (mesh_texture[0] == 0)
683 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
684 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
688 if (mesh_blendfunc1 != mesh->blendfunc1 || mesh_blendfunc2 != mesh->blendfunc2)
690 qglBlendFunc(mesh_blendfunc1 = mesh->blendfunc1, mesh_blendfunc2 = mesh->blendfunc2);CHECKGLERROR
691 if (mesh_blendfunc2 == GL_ZERO)
693 if (mesh_blendfunc1 == GL_ONE)
698 qglDisable(GL_BLEND);CHECKGLERROR
706 qglEnable(GL_BLEND);CHECKGLERROR
715 qglEnable(GL_BLEND);CHECKGLERROR
719 if (mesh_depthtest != mesh->depthtest)
721 mesh_depthtest = mesh->depthtest;
723 qglEnable(GL_DEPTH_TEST);
725 qglDisable(GL_DEPTH_TEST);
727 if (mesh_depthmask != mesh->depthmask)
729 qglDepthMask(mesh_depthmask = mesh->depthmask);CHECKGLERROR
733 void GL_DrawRangeElements(int firstvert, int endvert, int indexcount, GLuint *index)
735 unsigned int i, j, in;
736 if (gl_mesh_drawmode.integer >= 3)
738 // GL 1.2 or GL 1.1 with extension
739 qglDrawRangeElements(GL_TRIANGLES, firstvert, endvert, indexcount, GL_UNSIGNED_INT, index);
741 else if (gl_mesh_drawmode.integer >= 2)
744 qglDrawElements(GL_TRIANGLES, indexcount, GL_UNSIGNED_INT, index);
746 else if (gl_mesh_drawmode.integer >= 1)
749 // feed it manually using glArrayElement
750 qglBegin(GL_TRIANGLES);
751 for (i = 0;i < indexcount;i++)
752 qglArrayElement(index[i]);
757 // GL 1.1 but not using vertex arrays - 3dfx glquake minigl driver
759 qglBegin(GL_TRIANGLES);
760 if (r_multitexture.integer)
762 // the minigl doesn't have this (because it does not have ARB_multitexture)
763 for (i = 0;i < indexcount;i++)
766 qglColor4ub(buf_bcolor[in].c[0], buf_bcolor[in].c[1], buf_bcolor[in].c[2], buf_bcolor[in].c[3]);
767 for (j = 0;j < backendunits;j++)
769 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, buf_texcoord[j][in].t[0], buf_texcoord[j][in].t[1]);
770 qglVertex3f(buf_vertex[in].v[0], buf_vertex[in].v[1], buf_vertex[in].v[2]);
775 for (i = 0;i < indexcount;i++)
778 qglColor4ub(buf_bcolor[in].c[0], buf_bcolor[in].c[1], buf_bcolor[in].c[2], buf_bcolor[in].c[3]);
780 qglTexCoord2f(buf_texcoord[0][in].t[0], buf_texcoord[0][in].t[1]);
781 qglVertex3f(buf_vertex[in].v[0], buf_vertex[in].v[1], buf_vertex[in].v[2]);
788 // renders mesh buffers, called to flush buffers when full
789 void R_Mesh_Render(void)
799 Sys_Error("R_Mesh_Render: called when backend is not active\n");
804 if (!r_render.integer)
816 // drawmode 0 always uses byte colors
817 if (!gl_mesh_floatcolors.integer || gl_mesh_drawmode.integer <= 0)
818 GL_ConvertColorsFloatToByte();
820 if (gl_backend_rebindtextures)
822 gl_backend_rebindtextures = false;
823 GL_SetupTextureState();
826 GL_MeshState(buf_mesh);
827 GL_LockArray(0, currentvertex);
828 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
830 if (currentmesh >= 2)
832 for (k = 1, mesh = buf_mesh + k;k < currentmesh;k++, mesh++)
836 firstvert = mesh->firstvert;
837 indexcount = mesh->triangles * 3;
838 index = (unsigned int *)&buf_tri[mesh->firsttriangle].index[0];
840 // if not using batching, skip the index adjustment
842 for (i = 0;i < indexcount;i++)
843 index[i] += firstvert;
845 GL_DrawRangeElements(firstvert, firstvert + mesh->verts, indexcount, index);CHECKGLERROR
853 GL_UnlockArray();CHECKGLERROR
856 // restores backend state, used when done with 3D rendering
857 void R_Mesh_Finish(void)
860 // flush any queued meshs
863 if (backendunits > 1)
865 for (i = backendunits - 1;i >= 0;i--)
867 qglActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
868 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
869 if (gl_combine.integer)
871 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
875 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
879 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
881 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
885 qglClientActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
886 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
892 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
893 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
896 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
901 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
902 qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
905 qglDisable(GL_BLEND);CHECKGLERROR
906 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
907 qglDepthMask(GL_TRUE);CHECKGLERROR
908 qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);CHECKGLERROR
911 void R_Mesh_ClearDepth(void)
913 R_Mesh_AddTransparent();
915 qglClear(GL_DEPTH_BUFFER_BIT);
919 void R_Mesh_AddTransparent(void)
922 float viewdistcompare, centerscaler, dist1, dist2, dist3, center, maxdist;
923 buf_vertex_t *vert1, *vert2, *vert3;
925 buf_mesh_t *mesh, *transmesh;
927 if (!currenttransmesh)
930 // convert index data to transtris for sorting
931 for (j = 0;j < currenttransmesh;j++)
933 mesh = buf_transmesh + j;
934 k = mesh->firsttriangle;
935 index = &buf_transtri[k].index[0];
936 for (i = 0;i < mesh->triangles;i++)
938 tri = &buf_sorttranstri[k++];
940 tri->index[0] = *index++;
941 tri->index[1] = *index++;
942 tri->index[2] = *index++;
946 // map farclip to 0-4095 list range
947 centerscaler = (TRANSDEPTHRES / r_farclip) * (1.0f / 3.0f);
948 viewdistcompare = viewdist + 4.0f;
950 memset(buf_sorttranstri_list, 0, TRANSDEPTHRES * sizeof(buf_transtri_t *));
953 for (j = 0;j < currenttranstriangle;j++)
955 tri = &buf_sorttranstri[j];
956 i = tri->mesh->firstvert;
958 vert1 = &buf_transvertex[tri->index[0] + i];
959 vert2 = &buf_transvertex[tri->index[1] + i];
960 vert3 = &buf_transvertex[tri->index[2] + i];
962 dist1 = DotProduct(vert1->v, vpn);
963 dist2 = DotProduct(vert2->v, vpn);
964 dist3 = DotProduct(vert3->v, vpn);
966 maxdist = max(dist1, max(dist2, dist3));
967 if (maxdist < viewdistcompare)
970 center = (dist1 + dist2 + dist3) * centerscaler - viewdist;
973 i = bound(0, i, (TRANSDEPTHRES - 1));
977 center += 8388608.0f;
978 i = *((int *)¢er) & 0x7FFFFF;
979 i = min(i, (TRANSDEPTHRES - 1));
981 tri->next = buf_sorttranstri_list[i];
982 buf_sorttranstri_list[i] = tri;
986 for (i = 0;i < currenttransmesh;i++)
987 buf_transmesh[i].transchain = NULL;
989 for (j = 0;j < TRANSDEPTHRES;j++)
991 if ((tri = buf_sorttranstri_list[j]))
993 for (;tri;tri = tri->next)
995 if (!tri->mesh->transchain)
997 tri->mesh->chain = transmesh;
998 transmesh = tri->mesh;
1000 tri->meshsortchain = tri->mesh->transchain;
1001 tri->mesh->transchain = tri;
1006 for (;transmesh;transmesh = transmesh->chain)
1008 if (currentmesh >= max_meshs || currenttriangle + transmesh->triangles > max_batch || currenttriangle + transmesh->triangles > 1024 || currentvertex + transmesh->verts > max_verts)
1011 mesh = &buf_mesh[currentmesh++];
1012 *mesh = *transmesh; // copy mesh properties
1014 mesh->firstvert = currentvertex;
1015 memcpy(&buf_vertex[currentvertex], &buf_transvertex[transmesh->firstvert], transmesh->verts * sizeof(buf_vertex_t));
1016 memcpy(&buf_fcolor[currentvertex], &buf_transfcolor[transmesh->firstvert], transmesh->verts * sizeof(buf_fcolor_t));
1017 for (i = 0;i < backendunits && transmesh->textures[i];i++)
1018 memcpy(&buf_texcoord[i][currentvertex], &buf_transtexcoord[i][transmesh->firstvert], transmesh->verts * sizeof(buf_texcoord_t));
1019 currentvertex += mesh->verts;
1021 mesh->firsttriangle = currenttriangle;
1022 for (tri = transmesh->transchain;tri;tri = tri->meshsortchain)
1024 buf_tri[currenttriangle].index[0] = tri->index[0];
1025 buf_tri[currenttriangle].index[1] = tri->index[1];
1026 buf_tri[currenttriangle].index[2] = tri->index[2];
1029 mesh->triangles = currenttriangle - mesh->firsttriangle;
1032 currenttransmesh = 0;
1033 currenttranstriangle = 0;
1034 currenttransvertex = 0;
1037 void R_Mesh_Draw(const rmeshinfo_t *m)
1039 // these are static because gcc runs out of virtual registers otherwise
1040 static int i, j, overbright, *index;
1041 static float *in, scaler;
1042 static float cr, cg, cb, ca;
1043 static buf_mesh_t *mesh;
1044 static buf_vertex_t *vert;
1045 static buf_fcolor_t *fcolor;
1046 static buf_texcoord_t *texcoord[MAX_TEXTUREUNITS];
1049 Sys_Error("R_Mesh_Draw: called when backend is not active\n");
1051 if (m->index == NULL
1053 || m->vertex == NULL
1055 Host_Error("R_Mesh_Draw: no triangles or verts\n");
1057 // ignore meaningless alpha meshs
1058 if (!m->depthwrite && m->blendfunc1 == GL_SRC_ALPHA && (m->blendfunc2 == GL_ONE || m->blendfunc2 == GL_ONE_MINUS_SRC_ALPHA))
1062 for (i = 0, in = m->color + 3;i < m->numverts;i++, (int)in += m->colorstep)
1065 if (i == m->numverts)
1068 else if (m->ca < 0.01f)
1073 Sys_Error("R_Mesh_Draw: called when backend is not active\n");
1076 for (i = 0;i < m->numtriangles * 3;i++)
1077 if ((unsigned int) m->index[i] >= (unsigned int) m->numverts)
1078 Host_Error("R_Mesh_Draw: invalid index (%i of %i verts)\n", m->index, m->numverts);
1081 // LordHavoc: removed this error condition because with floatcolors 0,
1082 // the 3DFX driver works with very large meshs
1083 // FIXME: we can work around this by falling back on non-array renderer if buffers are too big
1084 //if (m->numtriangles > 1024 || m->numverts > 3072)
1086 // Con_Printf("R_Mesh_Draw: mesh too big for 3DFX drivers, rejected\n");
1090 if (m->numtriangles > max_meshs || m->numverts > max_verts)
1092 Con_Printf("R_Mesh_Draw: mesh too big for current gl_mesh_maxtriangles setting, increasing limits\n");
1093 if (m->numtriangles > overflowedmeshtris)
1094 overflowedmeshtris = m->numtriangles;
1095 if (((m->numverts + 2) / 3) > overflowedmeshtris)
1096 overflowedmeshtris = (m->numverts + 2) / 3;
1102 if (currenttransmesh >= max_transmeshs || (currenttranstriangle + m->numtriangles) > max_transmeshs || (currenttransvertex + m->numverts) > max_transverts)
1106 Con_Printf("R_Mesh_Draw: ran out of room for transparent meshs\n");
1113 c_transtris += m->numtriangles;
1114 vert = &buf_transvertex[currenttransvertex];
1115 fcolor = &buf_transfcolor[currenttransvertex];
1116 for (i = 0;i < backendunits;i++)
1117 texcoord[i] = &buf_transtexcoord[i][currenttransvertex];
1119 // transmesh is only for storage of transparent meshs until they
1120 // are inserted into the main mesh array
1121 mesh = &buf_transmesh[currenttransmesh++];
1122 mesh->firsttriangle = currenttranstriangle;
1123 mesh->firstvert = currenttransvertex;
1124 index = &buf_transtri[currenttranstriangle].index[0];
1126 currenttranstriangle += m->numtriangles;
1127 currenttransvertex += m->numverts;
1131 if (currentmesh >= max_meshs || (currenttriangle + m->numtriangles) > max_batch || (currentvertex + m->numverts) > max_verts)
1135 c_meshtris += m->numtriangles;
1136 vert = &buf_vertex[currentvertex];
1137 fcolor = &buf_fcolor[currentvertex];
1138 for (i = 0;i < backendunits;i++)
1139 texcoord[i] = &buf_texcoord[i][currentvertex];
1141 // opaque meshs are rendered directly
1142 mesh = &buf_mesh[currentmesh++];
1143 mesh->firsttriangle = currenttriangle;
1144 mesh->firstvert = currentvertex;
1145 index = &buf_tri[currenttriangle].index[0];
1147 currenttriangle += m->numtriangles;
1148 currentvertex += m->numverts;
1151 // code shared for transparent and opaque meshs
1152 memcpy(index, m->index, sizeof(int[3]) * m->numtriangles);
1153 mesh->blendfunc1 = m->blendfunc1;
1154 mesh->blendfunc2 = m->blendfunc2;
1155 mesh->depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite);
1156 mesh->depthtest = !m->depthdisable;
1157 mesh->triangles = m->numtriangles;
1158 mesh->verts = m->numverts;
1162 if (m->blendfunc2 == GL_SRC_COLOR)
1164 if (m->blendfunc1 == GL_DST_COLOR) // 2x modulate with framebuffer
1171 overbright = gl_combine.integer;
1175 scaler *= overbrightscale;
1180 for (i = 0;i < backendunits;i++)
1182 if ((mesh->textures[i] = m->tex[i]))
1184 mesh->texturergbscale[i] = m->texrgbscale[i];
1185 if (mesh->texturergbscale[i] != 1 && mesh->texturergbscale[i] != 2 && mesh->texturergbscale[i] != 4)
1186 mesh->texturergbscale[i] = 1;
1188 if (overbright && j >= 0)
1189 mesh->texturergbscale[j] = 4;
1191 if (m->vertexstep != sizeof(buf_vertex_t))
1193 for (i = 0, in = m->vertex;i < m->numverts;i++, (int)in += m->vertexstep)
1195 vert[i].v[0] = in[0];
1196 vert[i].v[1] = in[1];
1197 vert[i].v[2] = in[2];
1201 memcpy(vert, m->vertex, m->numverts * sizeof(buf_vertex_t));
1205 for (i = 0, in = m->color;i < m->numverts;i++, (int)in += m->colorstep)
1207 fcolor[i].c[0] = in[0] * scaler;
1208 fcolor[i].c[1] = in[1] * scaler;
1209 fcolor[i].c[2] = in[2] * scaler;
1210 fcolor[i].c[3] = in[3];
1215 cr = m->cr * scaler;
1216 cg = m->cg * scaler;
1217 cb = m->cb * scaler;
1219 for (i = 0;i < m->numverts;i++)
1221 fcolor[i].c[0] = cr;
1222 fcolor[i].c[1] = cg;
1223 fcolor[i].c[2] = cb;
1224 fcolor[i].c[3] = ca;
1228 for (j = 0;j < MAX_TEXTUREUNITS && m->tex[j];j++)
1230 if (j >= backendunits)
1231 Sys_Error("R_Mesh_Draw: texture %i supplied when there are only %i texture units\n", j + 1, backendunits);
1232 if (m->texcoordstep[j] != sizeof(buf_texcoord_t))
1234 for (i = 0, in = m->texcoords[j];i < m->numverts;i++, (int)in += m->texcoordstep[j])
1236 texcoord[j][i].t[0] = in[0];
1237 texcoord[j][i].t[1] = in[1];
1241 memcpy(&texcoord[j][0].t[0], m->texcoords[j], m->numverts * sizeof(buf_texcoord_t));
1244 if (currenttriangle >= max_batch)
1248 void R_Mesh_Draw_NativeOnly(const rmeshinfo_t *m)
1250 // these are static because gcc runs out of virtual registers otherwise
1251 static int i, j, overbright, *index;
1252 static float *in, scaler;
1253 static buf_mesh_t *mesh;
1254 static buf_vertex_t *vert;
1255 static buf_fcolor_t *fcolor;
1256 static buf_texcoord_t *texcoord[MAX_TEXTUREUNITS];
1259 Sys_Error("R_Mesh_Draw: called when backend is not active\n");
1261 if (m->index == NULL
1263 || m->vertex == NULL
1265 Host_Error("R_Mesh_Draw: no triangles or verts\n");
1267 // ignore meaningless alpha meshs
1268 if (!m->depthwrite && m->blendfunc1 == GL_SRC_ALPHA && (m->blendfunc2 == GL_ONE || m->blendfunc2 == GL_ONE_MINUS_SRC_ALPHA))
1272 for (i = 0, in = m->color + 3;i < m->numverts;i++, (int)in += m->colorstep)
1275 if (i == m->numverts)
1278 else if (m->ca < 0.01f)
1282 // LordHavoc: removed this error condition because with floatcolors 0,
1283 // the 3DFX driver works with very large meshs
1284 // FIXME: we can work around this by falling back on non-array renderer if buffers are too big
1285 //if (m->numtriangles > 1024 || m->numverts > 3072)
1287 // Con_Printf("R_Mesh_Draw_NativeOnly: mesh too big for 3DFX drivers, rejected\n");
1291 if (m->numtriangles > max_meshs || m->numverts > max_verts)
1293 Con_Printf("R_Mesh_Draw_NativeOnly: mesh too big for current gl_mesh_maxtriangles setting, increasing limits\n");
1294 if (m->numtriangles > overflowedmeshtris)
1295 overflowedmeshtris = m->numtriangles;
1296 if (((m->numverts + 2) / 3) > overflowedmeshtris)
1297 overflowedmeshtris = (m->numverts + 2) / 3;
1303 if (currenttransmesh >= max_transmeshs || (currenttranstriangle + m->numtriangles) > max_transmeshs || (currenttransvertex + m->numverts) > max_transverts)
1307 Con_Printf("R_Mesh_Draw_NativeOnly: ran out of room for transparent meshs\n");
1314 c_transtris += m->numtriangles;
1315 vert = &buf_transvertex[currenttransvertex];
1316 fcolor = &buf_transfcolor[currenttransvertex];
1317 for (i = 0;i < backendunits;i++)
1318 texcoord[i] = &buf_transtexcoord[i][currenttransvertex];
1320 // transmesh is only for storage of transparent meshs until they
1321 // are inserted into the main mesh array
1322 mesh = &buf_transmesh[currenttransmesh++];
1323 mesh->firsttriangle = currenttranstriangle;
1324 mesh->firstvert = currenttransvertex;
1325 index = &buf_transtri[currenttranstriangle].index[0];
1326 currenttranstriangle += m->numtriangles;
1327 currenttransvertex += m->numverts;
1331 if (currentmesh >= max_meshs || (currenttriangle + m->numtriangles) > max_batch || (currentvertex + m->numverts) > max_verts)
1335 c_meshtris += m->numtriangles;
1336 vert = &buf_vertex[currentvertex];
1337 fcolor = &buf_fcolor[currentvertex];
1338 for (i = 0;i < backendunits;i++)
1339 texcoord[i] = &buf_texcoord[i][currentvertex];
1341 // opaque meshs are rendered directly
1342 mesh = &buf_mesh[currentmesh++];
1343 mesh->firsttriangle = currenttriangle;
1344 mesh->firstvert = currentvertex;
1345 index = &buf_tri[currenttriangle].index[0];
1346 currenttriangle += m->numtriangles;
1347 currentvertex += m->numverts;
1350 // code shared for transparent and opaque meshs
1351 memcpy(index, m->index, sizeof(int[3]) * m->numtriangles);
1352 mesh->blendfunc1 = m->blendfunc1;
1353 mesh->blendfunc2 = m->blendfunc2;
1354 mesh->depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite);
1355 mesh->depthtest = !m->depthdisable;
1356 mesh->triangles = m->numtriangles;
1357 mesh->verts = m->numverts;
1361 if (m->blendfunc2 == GL_SRC_COLOR)
1363 if (m->blendfunc1 == GL_DST_COLOR) // 2x modulate with framebuffer
1370 overbright = gl_combine.integer;
1374 scaler *= overbrightscale;
1378 for (i = 0;i < backendunits;i++)
1380 if ((mesh->textures[i] = m->tex[i]))
1382 mesh->texturergbscale[i] = m->texrgbscale[i];
1383 if (mesh->texturergbscale[i] != 1 && mesh->texturergbscale[i] != 2 && mesh->texturergbscale[i] != 4)
1384 mesh->texturergbscale[i] = 1;
1386 if (overbright && j >= 0)
1387 mesh->texturergbscale[j] = 4;
1389 if (m->vertexstep != sizeof(buf_vertex_t))
1390 Host_Error("R_Mesh_Draw_NativeOnly: unsupported vertexstep\n");
1391 if (m->colorstep != sizeof(buf_fcolor_t))
1392 Host_Error("R_Mesh_Draw_NativeOnly: unsupported colorstep\n");
1393 if (m->color == NULL)
1394 Host_Error("R_Mesh_Draw_NativeOnly: must provide color array\n");
1395 for (j = 0;j < MAX_TEXTUREUNITS && m->tex[j];j++)
1397 if (j >= backendunits)
1398 Sys_Error("R_Mesh_Draw_NativeOnly: texture %i supplied when there are only %i texture units\n", j + 1, backendunits);
1399 if (m->texcoordstep[j] != sizeof(buf_texcoord_t))
1400 Host_Error("R_Mesh_Draw_NativeOnly: unsupported texcoordstep\n");
1403 memcpy(vert, m->vertex, m->numverts * sizeof(buf_vertex_t));
1404 for (j = 0;j < MAX_TEXTUREUNITS && m->tex[j];j++)
1405 memcpy(&texcoord[j][0].t[0], m->texcoords[j], m->numverts * sizeof(buf_texcoord_t));
1407 memcpy(fcolor, m->color, m->numverts * sizeof(buf_fcolor_t));
1409 // do this as a second step because memcpy preloaded the cache, which we can't easily do
1412 for (i = 0;i < m->numverts;i++)
1414 fcolor[i].c[0] *= scaler;
1415 fcolor[i].c[1] *= scaler;
1416 fcolor[i].c[2] *= scaler;
1420 if (currenttriangle >= max_batch)
1424 // allocates space in geometry buffers, and fills in pointers to the buffers in passsed struct
1425 // (this is used for very high speed rendering, no copying)
1426 int R_Mesh_Draw_GetBuffer(rmeshbufferinfo_t *m)
1428 // these are static because gcc runs out of virtual registers otherwise
1429 int i, j, overbright;
1434 Sys_Error("R_Mesh_Draw: called when backend is not active\n");
1436 if (!m->numtriangles
1438 Host_Error("R_Mesh_Draw: no triangles or verts\n");
1440 // LordHavoc: removed this error condition because with floatcolors 0,
1441 // the 3DFX driver works with very large meshs
1442 // FIXME: we can work around this by falling back on non-array renderer if buffers are too big
1443 //if (m->numtriangles > 1024 || m->numverts > 3072)
1445 // Con_Printf("R_Mesh_Draw_GetBuffer: mesh too big for 3DFX drivers, rejected\n");
1449 if (m->numtriangles > max_meshs || m->numverts > max_verts)
1451 Con_Printf("R_Mesh_Draw_GetBuffer: mesh too big for current gl_mesh_maxtriangles setting, increasing limits\n");
1452 if (m->numtriangles > overflowedmeshtris)
1453 overflowedmeshtris = m->numtriangles;
1454 if (((m->numverts + 2) / 3) > overflowedmeshtris)
1455 overflowedmeshtris = (m->numverts + 2) / 3;
1461 if (currenttransmesh >= max_transmeshs || (currenttranstriangle + m->numtriangles) > max_transmeshs || (currenttransvertex + m->numverts) > max_transverts)
1465 Con_Printf("R_Mesh_Draw: ran out of room for transparent meshs\n");
1472 c_transtris += m->numtriangles;
1473 m->index = &buf_transtri[currenttranstriangle].index[0];
1474 m->vertex = &buf_transvertex[currenttransvertex].v[0];
1475 m->color = &buf_transfcolor[currenttransvertex].c[0];
1476 for (i = 0;i < backendunits;i++)
1477 m->texcoords[i] = &buf_transtexcoord[i][currenttransvertex].t[0];
1479 // transmesh is only for storage of transparent meshs until they
1480 // are inserted into the main mesh array
1481 mesh = &buf_transmesh[currenttransmesh++];
1482 mesh->firsttriangle = currenttranstriangle;
1483 mesh->firstvert = currenttransvertex;
1484 currenttranstriangle += m->numtriangles;
1485 currenttransvertex += m->numverts;
1489 if (currentmesh >= max_meshs || (currenttriangle + m->numtriangles) > max_batch || (currentvertex + m->numverts) > max_verts)
1493 c_meshtris += m->numtriangles;
1494 m->index = &buf_tri[currenttriangle].index[0];
1495 m->vertex = &buf_vertex[currentvertex].v[0];
1496 m->color = &buf_fcolor[currentvertex].c[0];
1497 for (i = 0;i < backendunits;i++)
1498 m->texcoords[i] = &buf_texcoord[i][currentvertex].t[0];
1500 // opaque meshs are rendered directly
1501 mesh = &buf_mesh[currentmesh++];
1502 mesh->firsttriangle = currenttriangle;
1503 mesh->firstvert = currentvertex;
1504 currenttriangle += m->numtriangles;
1505 currentvertex += m->numverts;
1508 // code shared for transparent and opaque meshs
1509 mesh->blendfunc1 = m->blendfunc1;
1510 mesh->blendfunc2 = m->blendfunc2;
1511 mesh->depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite);
1512 mesh->depthtest = !m->depthdisable;
1513 mesh->triangles = m->numtriangles;
1514 mesh->verts = m->numverts;
1518 if (m->blendfunc2 == GL_SRC_COLOR)
1520 if (m->blendfunc1 == GL_DST_COLOR) // 2x modulate with framebuffer
1527 overbright = gl_combine.integer;
1531 scaler *= overbrightscale;
1533 m->colorscale = scaler;
1536 for (i = 0;i < MAX_TEXTUREUNITS;i++)
1538 if ((mesh->textures[i] = m->tex[i]))
1541 if (i >= backendunits)
1542 Sys_Error("R_Mesh_Draw_GetBuffer: texture %i supplied when there are only %i texture units\n", j + 1, backendunits);
1544 mesh->texturergbscale[i] = m->texrgbscale[i];
1545 if (mesh->texturergbscale[i] != 1 && mesh->texturergbscale[i] != 2 && mesh->texturergbscale[i] != 4)
1546 mesh->texturergbscale[i] = 1;
1548 if (overbright && j >= 0)
1549 mesh->texturergbscale[j] = 4;
1554 void R_Mesh_DrawPolygon(rmeshinfo_t *m, int numverts)
1556 m->index = polyindexarray;
1557 m->numverts = numverts;
1558 m->numtriangles = numverts - 2;
1559 if (m->numtriangles < 1)
1561 Con_Printf("R_Mesh_DrawPolygon: invalid vertex count\n");
1564 if (m->numtriangles >= 256)
1566 Con_Printf("R_Mesh_DrawPolygon: only up to 256 triangles (258 verts) supported\n");
1573 ==============================================================================
1577 ==============================================================================
1580 qboolean SCR_ScreenShot(char *filename, int x, int y, int width, int height)
1586 if (!r_render.integer)
1589 buffer = Mem_Alloc(tempmempool, width*height*3);
1590 qglReadPixels (x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer);
1593 // LordHavoc: compensate for v_overbrightbits when using hardware gamma
1594 if (v_hwgamma.integer)
1595 for (i = 0;i < width * height * 3;i++)
1596 buffer[i] <<= v_overbrightbits.integer;
1598 ret = Image_WriteTGARGB_preflipped(filename, width, height, buffer);
1604 //=============================================================================
1606 void R_ClearScreen(void)
1608 if (r_render.integer)
1611 qglClearColor(0,0,0,0);CHECKGLERROR
1613 qglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);CHECKGLERROR
1614 // set dithering mode
1615 if (gl_dither.integer)
1617 qglEnable(GL_DITHER);CHECKGLERROR
1621 qglDisable(GL_DITHER);CHECKGLERROR
1630 This is called every frame, and can also be called explicitly to flush
1634 void SCR_UpdateScreen (void)
1638 R_TimeReport("finish");
1640 if (gl_combine.integer && !gl_combine_extension)
1641 Cvar_SetValue("gl_combine", 0);
1643 lightscalebit = v_overbrightbits.integer;
1644 if (gl_combine.integer && r_multitexture.integer)
1647 lightscale = 1.0f / (float) (1 << lightscalebit);
1648 overbrightscale = 1.0f / (float) (1 << v_overbrightbits.integer);
1650 R_TimeReport("setup");
1654 R_TimeReport("clear");
1656 if (scr_conlines < vid.conheight)
1662 // tell driver to commit it's partially full geometry queue to the rendering queue
1663 // (this doesn't wait for the commands themselves to complete)