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 == 3 && qglDrawRangeElements != NULL)
235 if (gl_mesh_maxtriangles.integer * 3 > gl_maxdrawrangeelementsindices)
236 Cvar_SetValueQuick(&gl_mesh_maxtriangles, (int) (gl_maxdrawrangeelementsindices / 3));
237 if (gl_mesh_maxtriangles.integer * 3 > gl_maxdrawrangeelementsvertices)
238 Cvar_SetValueQuick(&gl_mesh_maxtriangles, (int) (gl_maxdrawrangeelementsvertices / 3));
241 // 21760 is (65536 / 3) rounded off to a multiple of 128
242 if (gl_mesh_maxtriangles.integer < 1024)
243 Cvar_SetValueQuick(&gl_mesh_maxtriangles, 1024);
244 if (gl_mesh_maxtriangles.integer > 21760)
245 Cvar_SetValueQuick(&gl_mesh_maxtriangles, 21760);
247 if (gl_mesh_transtriangles.integer < 1024)
248 Cvar_SetValueQuick(&gl_mesh_transtriangles, 1024);
249 if (gl_mesh_transtriangles.integer > 65536)
250 Cvar_SetValueQuick(&gl_mesh_transtriangles, 65536);
252 if (gl_mesh_batchtriangles.integer < 0)
253 Cvar_SetValueQuick(&gl_mesh_batchtriangles, 0);
254 if (gl_mesh_batchtriangles.integer > gl_mesh_maxtriangles.integer)
255 Cvar_SetValueQuick(&gl_mesh_batchtriangles, gl_mesh_maxtriangles.integer);
257 max_batch = gl_mesh_batchtriangles.integer;
259 if (max_meshs != gl_mesh_maxtriangles.integer || max_transmeshs != gl_mesh_transtriangles.integer)
261 max_meshs = gl_mesh_maxtriangles.integer;
262 max_transmeshs = gl_mesh_transtriangles.integer;
266 resizingbuffers = true;
267 gl_backend_shutdown();
269 resizingbuffers = false;
274 static void gl_backend_newmap(void)
276 r_farclip = r_newfarclip = 2048.0f;
279 void gl_backend_init(void)
283 Cvar_RegisterVariable(&r_render);
284 Cvar_RegisterVariable(&gl_dither);
285 Cvar_RegisterVariable(&gl_lockarrays);
287 Cvar_SetValue("r_render", 0);
290 Cvar_RegisterVariable(&gl_mesh_maxtriangles);
291 Cvar_RegisterVariable(&gl_mesh_transtriangles);
292 Cvar_RegisterVariable(&gl_mesh_batchtriangles);
293 Cvar_RegisterVariable(&gl_mesh_floatcolors);
294 Cvar_RegisterVariable(&gl_mesh_drawmode);
295 R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap);
296 gl_backend_bufferchanges(true);
297 for (i = 0;i < 256;i++)
299 polyindexarray[i*3+0] = 0;
300 polyindexarray[i*3+1] = i + 1;
301 polyindexarray[i*3+2] = i + 2;
305 int arraylocked = false;
307 void GL_LockArray(int first, int count)
309 if (!arraylocked && gl_supportslockarrays && gl_lockarrays.integer && gl_mesh_drawmode.integer != 0)
311 qglLockArraysEXT(first, count);
317 void GL_UnlockArray(void)
321 qglUnlockArraysEXT();
332 static void GL_SetupFrame (void)
335 double fovx, fovy, zNear, zFar, aspect;
337 // update farclip based on previous frame
338 r_farclip = r_newfarclip;
340 if (!r_render.integer)
343 qglDepthFunc (GL_LEQUAL);CHECKGLERROR
346 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
347 qglLoadIdentity ();CHECKGLERROR
349 // y is weird beause OpenGL is bottom to top, we use top to bottom
350 qglViewport(r_refdef.x, vid.realheight - (r_refdef.y + r_refdef.height), r_refdef.width, r_refdef.height);CHECKGLERROR
357 fovx = r_refdef.fov_x;
358 fovy = r_refdef.fov_y;
359 aspect = r_refdef.width / r_refdef.height;
362 xmax = zNear * tan(fovx * M_PI / 360.0) * aspect;
363 ymax = zNear * tan(fovy * M_PI / 360.0);
366 qglFrustum(-xmax, xmax, -ymax, ymax, zNear, zFar);CHECKGLERROR
368 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
369 qglLoadIdentity ();CHECKGLERROR
372 qglRotatef (-90, 1, 0, 0);CHECKGLERROR
373 qglRotatef (90, 0, 0, 1);CHECKGLERROR
375 qglRotatef (-r_refdef.viewangles[2], 1, 0, 0);CHECKGLERROR
376 qglRotatef (-r_refdef.viewangles[0], 0, 1, 0);CHECKGLERROR
377 qglRotatef (-r_refdef.viewangles[1], 0, 0, 1);CHECKGLERROR
379 qglTranslatef (-r_refdef.vieworg[0], -r_refdef.vieworg[1], -r_refdef.vieworg[2]);CHECKGLERROR
382 static int mesh_blendfunc1;
383 static int mesh_blendfunc2;
384 static int mesh_blend;
385 static GLboolean mesh_depthmask;
386 static int mesh_depthtest;
387 static int mesh_unit;
388 static int mesh_clientunit;
389 static int mesh_texture[MAX_TEXTUREUNITS];
390 static float mesh_texturergbscale[MAX_TEXTUREUNITS];
392 void GL_SetupTextureState(void)
395 if (backendunits > 1)
397 for (i = 0;i < backendunits;i++)
399 qglActiveTexture(GL_TEXTURE0_ARB + (mesh_unit = i));CHECKGLERROR
400 qglBindTexture(GL_TEXTURE_2D, mesh_texture[i]);CHECKGLERROR
401 if (gl_combine.integer)
403 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);CHECKGLERROR
404 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);CHECKGLERROR
405 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);CHECKGLERROR
406 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
407 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_CONSTANT_ARB);CHECKGLERROR
408 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
409 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
410 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);CHECKGLERROR
411 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);CHECKGLERROR
412 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);CHECKGLERROR
413 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
414 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, GL_CONSTANT_ARB);CHECKGLERROR
415 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
416 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
417 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
418 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, mesh_texturergbscale[i]);CHECKGLERROR
419 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
423 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
427 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
431 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
433 if (gl_mesh_drawmode.integer != 0)
435 qglClientActiveTexture(GL_TEXTURE0_ARB + (mesh_clientunit = i));CHECKGLERROR
436 qglTexCoordPointer(2, GL_FLOAT, sizeof(buf_texcoord_t), buf_texcoord[i]);CHECKGLERROR
439 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
443 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
450 qglBindTexture(GL_TEXTURE_2D, mesh_texture[0]);CHECKGLERROR
451 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
454 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
458 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
460 if (gl_mesh_drawmode.integer != 0)
462 qglTexCoordPointer(2, GL_FLOAT, sizeof(buf_texcoord_t), buf_texcoord[0]);CHECKGLERROR
465 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
469 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
475 // called at beginning of frame
477 void R_Mesh_Start(void)
481 Sys_Error("R_Mesh_Clear: called when backend is not active\n");
485 gl_backend_bufferchanges(false);
490 currenttransmesh = 0;
491 currenttranstriangle = 0;
492 currenttransvertex = 0;
495 viewdist = DotProduct(r_origin, vpn);
507 for (i = 0;i < backendunits;i++)
510 mesh_texturergbscale[i] = 1;
513 qglEnable(GL_CULL_FACE);CHECKGLERROR
514 qglCullFace(GL_FRONT);CHECKGLERROR
516 mesh_depthtest = true;
517 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
519 mesh_blendfunc1 = GL_ONE;
520 mesh_blendfunc2 = GL_ZERO;
521 qglBlendFunc(mesh_blendfunc1, mesh_blendfunc2);CHECKGLERROR
524 qglDisable(GL_BLEND);CHECKGLERROR
526 mesh_depthmask = GL_TRUE;
527 qglDepthMask(mesh_depthmask);CHECKGLERROR
530 if (gl_mesh_drawmode.integer != 0)
533 qglVertexPointer(3, GL_FLOAT, sizeof(buf_vertex_t), &buf_vertex[0].v[0]);CHECKGLERROR
534 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
535 if (gl_mesh_floatcolors.integer)
537 qglColorPointer(4, GL_FLOAT, sizeof(buf_fcolor_t), &buf_fcolor[0].c[0]);CHECKGLERROR
541 qglColorPointer(4, GL_UNSIGNED_BYTE, sizeof(buf_bcolor_t), &buf_bcolor[0].c[0]);CHECKGLERROR
543 qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
546 GL_SetupTextureState();
549 int gl_backend_rebindtextures;
551 void GL_UpdateFarclip(void)
556 // push out farclip based on vertices
557 // FIXME: wouldn't this be slow when using matrix transforms?
558 for (i = 0;i < currentvertex;i++)
560 farclip = DotProduct(buf_vertex[i].v, vpn);
561 if (meshfarclip < farclip)
562 meshfarclip = farclip;
565 farclip = meshfarclip + 256.0f - viewdist; // + 256 just to be safe
567 // push out farclip for next frame
568 if (farclip > r_newfarclip)
569 r_newfarclip = ceil((farclip + 255) / 256) * 256 + 256;
572 void GL_ConvertColorsFloatToByte(void)
575 // LordHavoc: to avoid problems with aliasing (treating memory as two
576 // different types - exactly what this is doing), these must be volatile
578 volatile int *icolor;
579 volatile float *fcolor;
582 total = currentvertex * 4;
584 // shift float to have 8bit fraction at base of number
585 fcolor = &buf_fcolor->c[0];
586 for (i = 0;i < total;)
588 fcolor[i ] += 32768.0f;
589 fcolor[i + 1] += 32768.0f;
590 fcolor[i + 2] += 32768.0f;
591 fcolor[i + 3] += 32768.0f;
595 // then read as integer and kill float bits...
596 icolor = (int *)&buf_fcolor->c[0];
597 bcolor = &buf_bcolor->c[0];
598 for (i = 0;i < total;)
600 k = icolor[i ] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i ] = (qbyte) k;
601 k = icolor[i + 1] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i + 1] = (qbyte) k;
602 k = icolor[i + 2] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i + 2] = (qbyte) k;
603 k = icolor[i + 3] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i + 3] = (qbyte) k;
608 void GL_MeshState(buf_mesh_t *mesh)
611 if (backendunits > 1)
613 for (i = 0;i < backendunits;i++)
615 if (mesh_texture[i] != mesh->textures[i])
619 qglActiveTexture(GL_TEXTURE0_ARB + (mesh_unit = i));CHECKGLERROR
621 if (mesh_texture[i] == 0)
623 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
624 // have to disable texcoord array on disabled texture
625 // units due to NVIDIA driver bug with
626 // compiled_vertex_array
627 if (mesh_clientunit != i)
629 qglClientActiveTexture(GL_TEXTURE0_ARB + (mesh_clientunit = i));CHECKGLERROR
631 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
633 qglBindTexture(GL_TEXTURE_2D, (mesh_texture[i] = mesh->textures[i]));CHECKGLERROR
634 if (mesh_texture[i] == 0)
636 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
637 // have to disable texcoord array on disabled texture
638 // units due to NVIDIA driver bug with
639 // compiled_vertex_array
640 if (mesh_clientunit != i)
642 qglClientActiveTexture(GL_TEXTURE0_ARB + (mesh_clientunit = i));CHECKGLERROR
644 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
647 if (mesh_texturergbscale[i] != mesh->texturergbscale[i])
651 qglActiveTexture(GL_TEXTURE0_ARB + (mesh_unit = i));CHECKGLERROR
653 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (mesh_texturergbscale[i] = mesh->texturergbscale[i]));CHECKGLERROR
659 if (mesh_texture[0] != mesh->textures[0])
661 if (mesh_texture[0] == 0)
663 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
664 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
666 qglBindTexture(GL_TEXTURE_2D, (mesh_texture[0] = mesh->textures[0]));CHECKGLERROR
667 if (mesh_texture[0] == 0)
669 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
670 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
674 if (mesh_blendfunc1 != mesh->blendfunc1 || mesh_blendfunc2 != mesh->blendfunc2)
676 qglBlendFunc(mesh_blendfunc1 = mesh->blendfunc1, mesh_blendfunc2 = mesh->blendfunc2);CHECKGLERROR
677 if (mesh_blendfunc2 == GL_ZERO)
679 if (mesh_blendfunc1 == GL_ONE)
684 qglDisable(GL_BLEND);CHECKGLERROR
692 qglEnable(GL_BLEND);CHECKGLERROR
701 qglEnable(GL_BLEND);CHECKGLERROR
705 if (mesh_depthtest != mesh->depthtest)
707 mesh_depthtest = mesh->depthtest;
709 qglEnable(GL_DEPTH_TEST);
711 qglDisable(GL_DEPTH_TEST);
713 if (mesh_depthmask != mesh->depthmask)
715 qglDepthMask(mesh_depthmask = mesh->depthmask);CHECKGLERROR
719 void GL_DrawRangeElements(int firstvert, int endvert, int indexcount, GLuint *index)
721 unsigned int i, j, in;
722 if (gl_mesh_drawmode.integer == 3 && qglDrawRangeElements == NULL)
723 Cvar_SetValueQuick(&gl_mesh_drawmode, 2);
725 if (gl_mesh_drawmode.integer == 3)
727 // GL 1.2 or GL 1.1 with extension
728 qglDrawRangeElements(GL_TRIANGLES, firstvert, endvert, indexcount, GL_UNSIGNED_INT, index);
730 else if (gl_mesh_drawmode.integer == 2)
733 qglDrawElements(GL_TRIANGLES, indexcount, GL_UNSIGNED_INT, index);
735 else if (gl_mesh_drawmode.integer == 1)
738 // feed it manually using glArrayElement
739 qglBegin(GL_TRIANGLES);
740 for (i = 0;i < indexcount;i++)
741 qglArrayElement(index[i]);
746 // GL 1.1 but not using vertex arrays - 3dfx glquake minigl driver
748 if (gl_mesh_drawmode.integer != 0)
749 Cvar_SetValueQuick(&gl_mesh_drawmode, 0);
750 qglBegin(GL_TRIANGLES);
751 if (r_multitexture.integer)
753 // the minigl doesn't have this (because it does not have ARB_multitexture)
754 for (i = 0;i < indexcount;i++)
757 qglColor4ub(buf_bcolor[in].c[0], buf_bcolor[in].c[1], buf_bcolor[in].c[2], buf_bcolor[in].c[3]);
758 for (j = 0;j < backendunits;j++)
760 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, buf_texcoord[j][in].t[0], buf_texcoord[j][in].t[1]);
761 qglVertex3f(buf_vertex[in].v[0], buf_vertex[in].v[1], buf_vertex[in].v[2]);
766 for (i = 0;i < indexcount;i++)
769 qglColor4ub(buf_bcolor[in].c[0], buf_bcolor[in].c[1], buf_bcolor[in].c[2], buf_bcolor[in].c[3]);
771 qglTexCoord2f(buf_texcoord[0][in].t[0], buf_texcoord[0][in].t[1]);
772 qglVertex3f(buf_vertex[in].v[0], buf_vertex[in].v[1], buf_vertex[in].v[2]);
779 // renders mesh buffers, called to flush buffers when full
780 void R_Mesh_Render(void)
790 Sys_Error("R_Mesh_Render: called when backend is not active\n");
795 if (!r_render.integer)
807 if (!gl_mesh_floatcolors.integer || gl_mesh_drawmode.integer == 0)
808 GL_ConvertColorsFloatToByte();
810 if (gl_backend_rebindtextures)
812 gl_backend_rebindtextures = false;
813 GL_SetupTextureState();
816 GL_MeshState(buf_mesh);
817 GL_LockArray(0, currentvertex);
818 GL_DrawRangeElements(buf_mesh->firstvert, buf_mesh->firstvert + buf_mesh->verts, buf_mesh->triangles * 3, (unsigned int *)&buf_tri[buf_mesh->firsttriangle].index[0]);CHECKGLERROR
820 if (currentmesh >= 2)
822 for (k = 1, mesh = buf_mesh + k;k < currentmesh;k++, mesh++)
826 firstvert = mesh->firstvert;
827 indexcount = mesh->triangles * 3;
828 index = (unsigned int *)&buf_tri[mesh->firsttriangle].index[0];
830 // if not using batching, skip the index adjustment
832 for (i = 0;i < indexcount;i++)
833 index[i] += firstvert;
835 GL_DrawRangeElements(firstvert, firstvert + mesh->verts, indexcount, index);CHECKGLERROR
843 GL_UnlockArray();CHECKGLERROR
846 // restores backend state, used when done with 3D rendering
847 void R_Mesh_Finish(void)
850 // flush any queued meshs
853 if (backendunits > 1)
855 for (i = backendunits - 1;i >= 0;i--)
857 qglActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
858 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
859 if (gl_combine.integer)
861 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
865 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
869 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
871 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
875 qglClientActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
876 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
882 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
883 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
886 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
891 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
892 qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
895 qglDisable(GL_BLEND);CHECKGLERROR
896 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
897 qglDepthMask(GL_TRUE);CHECKGLERROR
898 qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);CHECKGLERROR
901 void R_Mesh_ClearDepth(void)
903 R_Mesh_AddTransparent();
905 qglClear(GL_DEPTH_BUFFER_BIT);
909 void R_Mesh_AddTransparent(void)
912 float viewdistcompare, centerscaler, dist1, dist2, dist3, center, maxdist;
913 buf_vertex_t *vert1, *vert2, *vert3;
915 buf_mesh_t *mesh, *transmesh;
917 if (!currenttransmesh)
920 // convert index data to transtris for sorting
921 for (j = 0;j < currenttransmesh;j++)
923 mesh = buf_transmesh + j;
924 k = mesh->firsttriangle;
925 index = &buf_transtri[k].index[0];
926 for (i = 0;i < mesh->triangles;i++)
928 tri = &buf_sorttranstri[k++];
930 tri->index[0] = *index++;
931 tri->index[1] = *index++;
932 tri->index[2] = *index++;
936 // map farclip to 0-4095 list range
937 centerscaler = (TRANSDEPTHRES / r_farclip) * (1.0f / 3.0f);
938 viewdistcompare = viewdist + 4.0f;
940 memset(buf_sorttranstri_list, 0, TRANSDEPTHRES * sizeof(buf_transtri_t *));
943 for (j = 0;j < currenttranstriangle;j++)
945 tri = &buf_sorttranstri[j];
946 i = tri->mesh->firstvert;
948 vert1 = &buf_transvertex[tri->index[0] + i];
949 vert2 = &buf_transvertex[tri->index[1] + i];
950 vert3 = &buf_transvertex[tri->index[2] + i];
952 dist1 = DotProduct(vert1->v, vpn);
953 dist2 = DotProduct(vert2->v, vpn);
954 dist3 = DotProduct(vert3->v, vpn);
956 maxdist = max(dist1, max(dist2, dist3));
957 if (maxdist < viewdistcompare)
960 center = (dist1 + dist2 + dist3) * centerscaler - viewdist;
963 i = bound(0, i, (TRANSDEPTHRES - 1));
967 center += 8388608.0f;
968 i = *((int *)¢er) & 0x7FFFFF;
969 i = min(i, (TRANSDEPTHRES - 1));
971 tri->next = buf_sorttranstri_list[i];
972 buf_sorttranstri_list[i] = tri;
976 for (i = 0;i < currenttransmesh;i++)
977 buf_transmesh[i].transchain = NULL;
979 for (j = 0;j < TRANSDEPTHRES;j++)
981 if ((tri = buf_sorttranstri_list[j]))
983 for (;tri;tri = tri->next)
985 if (!tri->mesh->transchain)
987 tri->mesh->chain = transmesh;
988 transmesh = tri->mesh;
990 tri->meshsortchain = tri->mesh->transchain;
991 tri->mesh->transchain = tri;
996 for (;transmesh;transmesh = transmesh->chain)
998 if (currentmesh >= max_meshs || currenttriangle + transmesh->triangles > max_batch || currenttriangle + transmesh->triangles > 1024 || currentvertex + transmesh->verts > max_verts)
1001 mesh = &buf_mesh[currentmesh++];
1002 *mesh = *transmesh; // copy mesh properties
1004 mesh->firstvert = currentvertex;
1005 memcpy(&buf_vertex[currentvertex], &buf_transvertex[transmesh->firstvert], transmesh->verts * sizeof(buf_vertex_t));
1006 memcpy(&buf_fcolor[currentvertex], &buf_transfcolor[transmesh->firstvert], transmesh->verts * sizeof(buf_fcolor_t));
1007 for (i = 0;i < backendunits && transmesh->textures[i];i++)
1008 memcpy(&buf_texcoord[i][currentvertex], &buf_transtexcoord[i][transmesh->firstvert], transmesh->verts * sizeof(buf_texcoord_t));
1009 currentvertex += mesh->verts;
1011 mesh->firsttriangle = currenttriangle;
1012 for (tri = transmesh->transchain;tri;tri = tri->meshsortchain)
1014 buf_tri[currenttriangle].index[0] = tri->index[0];
1015 buf_tri[currenttriangle].index[1] = tri->index[1];
1016 buf_tri[currenttriangle].index[2] = tri->index[2];
1019 mesh->triangles = currenttriangle - mesh->firsttriangle;
1022 currenttransmesh = 0;
1023 currenttranstriangle = 0;
1024 currenttransvertex = 0;
1027 void R_Mesh_Draw(const rmeshinfo_t *m)
1029 // these are static because gcc runs out of virtual registers otherwise
1030 static int i, j, overbright, *index;
1031 static float *in, scaler;
1032 static float cr, cg, cb, ca;
1033 static buf_mesh_t *mesh;
1034 static buf_vertex_t *vert;
1035 static buf_fcolor_t *fcolor;
1036 static buf_texcoord_t *texcoord[MAX_TEXTUREUNITS];
1039 Sys_Error("R_Mesh_Draw: called when backend is not active\n");
1041 if (m->index == NULL
1043 || m->vertex == NULL
1045 Host_Error("R_Mesh_Draw: no triangles or verts\n");
1047 // ignore meaningless alpha meshs
1048 if (!m->depthwrite && m->blendfunc1 == GL_SRC_ALPHA && (m->blendfunc2 == GL_ONE || m->blendfunc2 == GL_ONE_MINUS_SRC_ALPHA))
1052 for (i = 0, in = m->color + 3;i < m->numverts;i++, (int)in += m->colorstep)
1055 if (i == m->numverts)
1058 else if (m->ca < 0.01f)
1063 Sys_Error("R_Mesh_Draw: called when backend is not active\n");
1066 for (i = 0;i < m->numtriangles * 3;i++)
1067 if ((unsigned int) m->index[i] >= (unsigned int) m->numverts)
1068 Host_Error("R_Mesh_Draw: invalid index (%i of %i verts)\n", m->index, m->numverts);
1071 // LordHavoc: removed this error condition because with floatcolors 0,
1072 // the 3DFX driver works with very large meshs
1073 // FIXME: we can work around this by falling back on non-array renderer if buffers are too big
1074 //if (m->numtriangles > 1024 || m->numverts > 3072)
1076 // Con_Printf("R_Mesh_Draw: mesh too big for 3DFX drivers, rejected\n");
1080 if (m->numtriangles > max_meshs || m->numverts > max_verts)
1082 Con_Printf("R_Mesh_Draw: mesh too big for current gl_mesh_maxtriangles setting, increasing limits\n");
1083 if (m->numtriangles > overflowedmeshtris)
1084 overflowedmeshtris = m->numtriangles;
1085 if (((m->numverts + 2) / 3) > overflowedmeshtris)
1086 overflowedmeshtris = (m->numverts + 2) / 3;
1092 if (currenttransmesh >= max_transmeshs || (currenttranstriangle + m->numtriangles) > max_transmeshs || (currenttransvertex + m->numverts) > max_transverts)
1096 Con_Printf("R_Mesh_Draw: ran out of room for transparent meshs\n");
1103 c_transtris += m->numtriangles;
1104 vert = &buf_transvertex[currenttransvertex];
1105 fcolor = &buf_transfcolor[currenttransvertex];
1106 for (i = 0;i < backendunits;i++)
1107 texcoord[i] = &buf_transtexcoord[i][currenttransvertex];
1109 // transmesh is only for storage of transparent meshs until they
1110 // are inserted into the main mesh array
1111 mesh = &buf_transmesh[currenttransmesh++];
1112 mesh->firsttriangle = currenttranstriangle;
1113 mesh->firstvert = currenttransvertex;
1114 index = &buf_transtri[currenttranstriangle].index[0];
1116 currenttranstriangle += m->numtriangles;
1117 currenttransvertex += m->numverts;
1121 if (currentmesh >= max_meshs || (currenttriangle + m->numtriangles) > max_batch || (currentvertex + m->numverts) > max_verts)
1125 c_meshtris += m->numtriangles;
1126 vert = &buf_vertex[currentvertex];
1127 fcolor = &buf_fcolor[currentvertex];
1128 for (i = 0;i < backendunits;i++)
1129 texcoord[i] = &buf_texcoord[i][currentvertex];
1131 // opaque meshs are rendered directly
1132 mesh = &buf_mesh[currentmesh++];
1133 mesh->firsttriangle = currenttriangle;
1134 mesh->firstvert = currentvertex;
1135 index = &buf_tri[currenttriangle].index[0];
1137 currenttriangle += m->numtriangles;
1138 currentvertex += m->numverts;
1141 // code shared for transparent and opaque meshs
1142 memcpy(index, m->index, sizeof(int[3]) * m->numtriangles);
1143 mesh->blendfunc1 = m->blendfunc1;
1144 mesh->blendfunc2 = m->blendfunc2;
1145 mesh->depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite);
1146 mesh->depthtest = !m->depthdisable;
1147 mesh->triangles = m->numtriangles;
1148 mesh->verts = m->numverts;
1152 if (m->blendfunc2 == GL_SRC_COLOR)
1154 if (m->blendfunc1 == GL_DST_COLOR) // 2x modulate with framebuffer
1161 overbright = gl_combine.integer;
1165 scaler *= overbrightscale;
1170 for (i = 0;i < backendunits;i++)
1172 if ((mesh->textures[i] = m->tex[i]))
1174 mesh->texturergbscale[i] = m->texrgbscale[i];
1175 if (mesh->texturergbscale[i] != 1 && mesh->texturergbscale[i] != 2 && mesh->texturergbscale[i] != 4)
1176 mesh->texturergbscale[i] = 1;
1178 if (overbright && j >= 0)
1179 mesh->texturergbscale[j] = 4;
1181 if (m->vertexstep != sizeof(buf_vertex_t))
1183 for (i = 0, in = m->vertex;i < m->numverts;i++, (int)in += m->vertexstep)
1185 vert[i].v[0] = in[0];
1186 vert[i].v[1] = in[1];
1187 vert[i].v[2] = in[2];
1191 memcpy(vert, m->vertex, m->numverts * sizeof(buf_vertex_t));
1195 for (i = 0, in = m->color;i < m->numverts;i++, (int)in += m->colorstep)
1197 fcolor[i].c[0] = in[0] * scaler;
1198 fcolor[i].c[1] = in[1] * scaler;
1199 fcolor[i].c[2] = in[2] * scaler;
1200 fcolor[i].c[3] = in[3];
1205 cr = m->cr * scaler;
1206 cg = m->cg * scaler;
1207 cb = m->cb * scaler;
1209 for (i = 0;i < m->numverts;i++)
1211 fcolor[i].c[0] = cr;
1212 fcolor[i].c[1] = cg;
1213 fcolor[i].c[2] = cb;
1214 fcolor[i].c[3] = ca;
1218 for (j = 0;j < MAX_TEXTUREUNITS && m->tex[j];j++)
1220 if (j >= backendunits)
1221 Sys_Error("R_Mesh_Draw: texture %i supplied when there are only %i texture units\n", j + 1, backendunits);
1222 if (m->texcoordstep[j] != sizeof(buf_texcoord_t))
1224 for (i = 0, in = m->texcoords[j];i < m->numverts;i++, (int)in += m->texcoordstep[j])
1226 texcoord[j][i].t[0] = in[0];
1227 texcoord[j][i].t[1] = in[1];
1231 memcpy(&texcoord[j][0].t[0], m->texcoords[j], m->numverts * sizeof(buf_texcoord_t));
1234 if (currenttriangle >= max_batch)
1238 void R_Mesh_Draw_NativeOnly(const rmeshinfo_t *m)
1240 // these are static because gcc runs out of virtual registers otherwise
1241 static int i, j, overbright, *index;
1242 static float *in, scaler;
1243 static buf_mesh_t *mesh;
1244 static buf_vertex_t *vert;
1245 static buf_fcolor_t *fcolor;
1246 static buf_texcoord_t *texcoord[MAX_TEXTUREUNITS];
1249 Sys_Error("R_Mesh_Draw: called when backend is not active\n");
1251 if (m->index == NULL
1253 || m->vertex == NULL
1255 Host_Error("R_Mesh_Draw: no triangles or verts\n");
1257 // ignore meaningless alpha meshs
1258 if (!m->depthwrite && m->blendfunc1 == GL_SRC_ALPHA && (m->blendfunc2 == GL_ONE || m->blendfunc2 == GL_ONE_MINUS_SRC_ALPHA))
1262 for (i = 0, in = m->color + 3;i < m->numverts;i++, (int)in += m->colorstep)
1265 if (i == m->numverts)
1268 else if (m->ca < 0.01f)
1272 // LordHavoc: removed this error condition because with floatcolors 0,
1273 // the 3DFX driver works with very large meshs
1274 // FIXME: we can work around this by falling back on non-array renderer if buffers are too big
1275 //if (m->numtriangles > 1024 || m->numverts > 3072)
1277 // Con_Printf("R_Mesh_Draw_NativeOnly: mesh too big for 3DFX drivers, rejected\n");
1281 if (m->numtriangles > max_meshs || m->numverts > max_verts)
1283 Con_Printf("R_Mesh_Draw_NativeOnly: mesh too big for current gl_mesh_maxtriangles setting, increasing limits\n");
1284 if (m->numtriangles > overflowedmeshtris)
1285 overflowedmeshtris = m->numtriangles;
1286 if (((m->numverts + 2) / 3) > overflowedmeshtris)
1287 overflowedmeshtris = (m->numverts + 2) / 3;
1293 if (currenttransmesh >= max_transmeshs || (currenttranstriangle + m->numtriangles) > max_transmeshs || (currenttransvertex + m->numverts) > max_transverts)
1297 Con_Printf("R_Mesh_Draw_NativeOnly: ran out of room for transparent meshs\n");
1304 c_transtris += m->numtriangles;
1305 vert = &buf_transvertex[currenttransvertex];
1306 fcolor = &buf_transfcolor[currenttransvertex];
1307 for (i = 0;i < backendunits;i++)
1308 texcoord[i] = &buf_transtexcoord[i][currenttransvertex];
1310 // transmesh is only for storage of transparent meshs until they
1311 // are inserted into the main mesh array
1312 mesh = &buf_transmesh[currenttransmesh++];
1313 mesh->firsttriangle = currenttranstriangle;
1314 mesh->firstvert = currenttransvertex;
1315 index = &buf_transtri[currenttranstriangle].index[0];
1316 currenttranstriangle += m->numtriangles;
1317 currenttransvertex += m->numverts;
1321 if (currentmesh >= max_meshs || (currenttriangle + m->numtriangles) > max_batch || (currentvertex + m->numverts) > max_verts)
1325 c_meshtris += m->numtriangles;
1326 vert = &buf_vertex[currentvertex];
1327 fcolor = &buf_fcolor[currentvertex];
1328 for (i = 0;i < backendunits;i++)
1329 texcoord[i] = &buf_texcoord[i][currentvertex];
1331 // opaque meshs are rendered directly
1332 mesh = &buf_mesh[currentmesh++];
1333 mesh->firsttriangle = currenttriangle;
1334 mesh->firstvert = currentvertex;
1335 index = &buf_tri[currenttriangle].index[0];
1336 currenttriangle += m->numtriangles;
1337 currentvertex += m->numverts;
1340 // code shared for transparent and opaque meshs
1341 memcpy(index, m->index, sizeof(int[3]) * m->numtriangles);
1342 mesh->blendfunc1 = m->blendfunc1;
1343 mesh->blendfunc2 = m->blendfunc2;
1344 mesh->depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite);
1345 mesh->depthtest = !m->depthdisable;
1346 mesh->triangles = m->numtriangles;
1347 mesh->verts = m->numverts;
1351 if (m->blendfunc2 == GL_SRC_COLOR)
1353 if (m->blendfunc1 == GL_DST_COLOR) // 2x modulate with framebuffer
1360 overbright = gl_combine.integer;
1364 scaler *= overbrightscale;
1368 for (i = 0;i < backendunits;i++)
1370 if ((mesh->textures[i] = m->tex[i]))
1372 mesh->texturergbscale[i] = m->texrgbscale[i];
1373 if (mesh->texturergbscale[i] != 1 && mesh->texturergbscale[i] != 2 && mesh->texturergbscale[i] != 4)
1374 mesh->texturergbscale[i] = 1;
1376 if (overbright && j >= 0)
1377 mesh->texturergbscale[j] = 4;
1379 if (m->vertexstep != sizeof(buf_vertex_t))
1380 Host_Error("R_Mesh_Draw_NativeOnly: unsupported vertexstep\n");
1381 if (m->colorstep != sizeof(buf_fcolor_t))
1382 Host_Error("R_Mesh_Draw_NativeOnly: unsupported colorstep\n");
1383 if (m->color == NULL)
1384 Host_Error("R_Mesh_Draw_NativeOnly: must provide color array\n");
1385 for (j = 0;j < MAX_TEXTUREUNITS && m->tex[j];j++)
1387 if (j >= backendunits)
1388 Sys_Error("R_Mesh_Draw_NativeOnly: texture %i supplied when there are only %i texture units\n", j + 1, backendunits);
1389 if (m->texcoordstep[j] != sizeof(buf_texcoord_t))
1390 Host_Error("R_Mesh_Draw_NativeOnly: unsupported texcoordstep\n");
1393 memcpy(vert, m->vertex, m->numverts * sizeof(buf_vertex_t));
1394 for (j = 0;j < MAX_TEXTUREUNITS && m->tex[j];j++)
1395 memcpy(&texcoord[j][0].t[0], m->texcoords[j], m->numverts * sizeof(buf_texcoord_t));
1397 memcpy(fcolor, m->color, m->numverts * sizeof(buf_fcolor_t));
1399 // do this as a second step because memcpy preloaded the cache, which we can't easily do
1402 for (i = 0;i < m->numverts;i++)
1404 fcolor[i].c[0] *= scaler;
1405 fcolor[i].c[1] *= scaler;
1406 fcolor[i].c[2] *= scaler;
1410 if (currenttriangle >= max_batch)
1414 // allocates space in geometry buffers, and fills in pointers to the buffers in passsed struct
1415 // (this is used for very high speed rendering, no copying)
1416 int R_Mesh_Draw_GetBuffer(rmeshbufferinfo_t *m)
1418 // these are static because gcc runs out of virtual registers otherwise
1419 int i, j, overbright;
1424 Sys_Error("R_Mesh_Draw: called when backend is not active\n");
1426 if (!m->numtriangles
1428 Host_Error("R_Mesh_Draw: no triangles or verts\n");
1430 // LordHavoc: removed this error condition because with floatcolors 0,
1431 // the 3DFX driver works with very large meshs
1432 // FIXME: we can work around this by falling back on non-array renderer if buffers are too big
1433 //if (m->numtriangles > 1024 || m->numverts > 3072)
1435 // Con_Printf("R_Mesh_Draw_GetBuffer: mesh too big for 3DFX drivers, rejected\n");
1439 if (m->numtriangles > max_meshs || m->numverts > max_verts)
1441 Con_Printf("R_Mesh_Draw_GetBuffer: mesh too big for current gl_mesh_maxtriangles setting, increasing limits\n");
1442 if (m->numtriangles > overflowedmeshtris)
1443 overflowedmeshtris = m->numtriangles;
1444 if (((m->numverts + 2) / 3) > overflowedmeshtris)
1445 overflowedmeshtris = (m->numverts + 2) / 3;
1451 if (currenttransmesh >= max_transmeshs || (currenttranstriangle + m->numtriangles) > max_transmeshs || (currenttransvertex + m->numverts) > max_transverts)
1455 Con_Printf("R_Mesh_Draw: ran out of room for transparent meshs\n");
1462 c_transtris += m->numtriangles;
1463 m->index = &buf_transtri[currenttranstriangle].index[0];
1464 m->vertex = &buf_transvertex[currenttransvertex].v[0];
1465 m->color = &buf_transfcolor[currenttransvertex].c[0];
1466 for (i = 0;i < backendunits;i++)
1467 m->texcoords[i] = &buf_transtexcoord[i][currenttransvertex].t[0];
1469 // transmesh is only for storage of transparent meshs until they
1470 // are inserted into the main mesh array
1471 mesh = &buf_transmesh[currenttransmesh++];
1472 mesh->firsttriangle = currenttranstriangle;
1473 mesh->firstvert = currenttransvertex;
1474 currenttranstriangle += m->numtriangles;
1475 currenttransvertex += m->numverts;
1479 if (currentmesh >= max_meshs || (currenttriangle + m->numtriangles) > max_batch || (currentvertex + m->numverts) > max_verts)
1483 c_meshtris += m->numtriangles;
1484 m->index = &buf_tri[currenttriangle].index[0];
1485 m->vertex = &buf_vertex[currentvertex].v[0];
1486 m->color = &buf_fcolor[currentvertex].c[0];
1487 for (i = 0;i < backendunits;i++)
1488 m->texcoords[i] = &buf_texcoord[i][currentvertex].t[0];
1490 // opaque meshs are rendered directly
1491 mesh = &buf_mesh[currentmesh++];
1492 mesh->firsttriangle = currenttriangle;
1493 mesh->firstvert = currentvertex;
1494 currenttriangle += m->numtriangles;
1495 currentvertex += m->numverts;
1498 // code shared for transparent and opaque meshs
1499 mesh->blendfunc1 = m->blendfunc1;
1500 mesh->blendfunc2 = m->blendfunc2;
1501 mesh->depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite);
1502 mesh->depthtest = !m->depthdisable;
1503 mesh->triangles = m->numtriangles;
1504 mesh->verts = m->numverts;
1508 if (m->blendfunc2 == GL_SRC_COLOR)
1510 if (m->blendfunc1 == GL_DST_COLOR) // 2x modulate with framebuffer
1517 overbright = gl_combine.integer;
1521 scaler *= overbrightscale;
1523 m->colorscale = scaler;
1526 for (i = 0;i < MAX_TEXTUREUNITS;i++)
1528 if ((mesh->textures[i] = m->tex[i]))
1531 if (i >= backendunits)
1532 Sys_Error("R_Mesh_Draw_GetBuffer: texture %i supplied when there are only %i texture units\n", j + 1, backendunits);
1534 mesh->texturergbscale[i] = m->texrgbscale[i];
1535 if (mesh->texturergbscale[i] != 1 && mesh->texturergbscale[i] != 2 && mesh->texturergbscale[i] != 4)
1536 mesh->texturergbscale[i] = 1;
1538 if (overbright && j >= 0)
1539 mesh->texturergbscale[j] = 4;
1544 void R_Mesh_DrawPolygon(rmeshinfo_t *m, int numverts)
1546 m->index = polyindexarray;
1547 m->numverts = numverts;
1548 m->numtriangles = numverts - 2;
1549 if (m->numtriangles < 1)
1551 Con_Printf("R_Mesh_DrawPolygon: invalid vertex count\n");
1554 if (m->numtriangles >= 256)
1556 Con_Printf("R_Mesh_DrawPolygon: only up to 256 triangles (258 verts) supported\n");
1563 ==============================================================================
1567 ==============================================================================
1570 qboolean SCR_ScreenShot(char *filename, int x, int y, int width, int height)
1576 if (!r_render.integer)
1579 buffer = Mem_Alloc(tempmempool, width*height*3);
1580 qglReadPixels (x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer);
1583 // LordHavoc: compensate for v_overbrightbits when using hardware gamma
1584 if (v_hwgamma.integer)
1585 for (i = 0;i < width * height * 3;i++)
1586 buffer[i] <<= v_overbrightbits.integer;
1588 ret = Image_WriteTGARGB_preflipped(filename, width, height, buffer);
1594 //=============================================================================
1596 void R_ClearScreen(void)
1598 if (r_render.integer)
1601 qglClearColor(0,0,0,0);CHECKGLERROR
1603 qglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);CHECKGLERROR
1604 // set dithering mode
1605 if (gl_dither.integer)
1607 qglEnable(GL_DITHER);CHECKGLERROR
1611 qglDisable(GL_DITHER);CHECKGLERROR
1620 This is called every frame, and can also be called explicitly to flush
1624 void SCR_UpdateScreen (void)
1628 R_TimeReport("finish");
1630 if (gl_combine.integer && !gl_combine_extension)
1631 Cvar_SetValue("gl_combine", 0);
1633 lightscalebit = v_overbrightbits.integer;
1634 if (gl_combine.integer && r_multitexture.integer)
1637 lightscale = 1.0f / (float) (1 << lightscalebit);
1638 overbrightscale = 1.0f / (float) (1 << v_overbrightbits.integer);
1640 R_TimeReport("setup");
1644 R_TimeReport("clear");
1646 if (scr_conlines < vid.conheight)
1652 // tell driver to commit it's partially full geometry queue to the rendering queue
1653 // (this doesn't wait for the commands themselves to complete)