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 int gl_maxdrawrangeelementsvertices;
15 int gl_maxdrawrangeelementsindices;
20 void GL_PrintError(int errornumber, char *filename, int linenumber)
24 #ifdef GL_INVALID_ENUM
26 Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
29 #ifdef GL_INVALID_VALUE
30 case GL_INVALID_VALUE:
31 Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
34 #ifdef GL_INVALID_OPERATION
35 case GL_INVALID_OPERATION:
36 Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
39 #ifdef GL_STACK_OVERFLOW
40 case GL_STACK_OVERFLOW:
41 Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
44 #ifdef GL_STACK_UNDERFLOW
45 case GL_STACK_UNDERFLOW:
46 Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
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);
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);
60 Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
66 float r_farclip, r_newfarclip;
68 int polyindexarray[768];
70 static float viewdist;
72 int c_meshs, c_meshtris, c_transmeshs, c_transtris;
76 float overbrightscale;
78 void SCR_ScreenShot_f (void);
81 static int max_transmeshs;
83 static int max_verts; // always max_meshs * 3
84 static int max_transverts; // always max_transmeshs * 3
85 #define TRANSDEPTHRES 4096
87 typedef struct buf_mesh_s
91 int blendfunc1, blendfunc2;
92 int textures[MAX_TEXTUREUNITS];
93 int texturergbscale[MAX_TEXTUREUNITS];
98 struct buf_mesh_s *chain;
99 struct buf_transtri_s *transchain;
103 typedef struct buf_transtri_s
105 struct buf_transtri_s *next;
106 struct buf_transtri_s *meshsortchain;
112 typedef struct buf_tri_s
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];
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];
160 static mempool_t *gl_backend_mempool;
161 static int resizingbuffers = false;
163 static void gl_backend_start(void)
167 max_verts = max_meshs * 3;
168 max_transverts = max_transmeshs * 3;
170 if (!gl_backend_mempool)
171 gl_backend_mempool = Mem_AllocPool("GL_Backend");
173 #define BACKENDALLOC(var, count, sizeofstruct, varname)\
175 var = Mem_Alloc(gl_backend_mempool, count * sizeof(sizeofstruct));\
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));\
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")
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")
194 for (i = 0;i < MAX_TEXTUREUNITS;i++)
196 // only allocate as many texcoord arrays as we need
197 if (i < gl_textureunits)
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))
204 buf_texcoord[i] = NULL;
205 buf_transtexcoord[i] = NULL;
208 backendunits = min(MAX_TEXTUREUNITS, gl_textureunits);
209 backendactive = true;
212 static void gl_backend_shutdown(void)
215 Mem_EmptyPool(gl_backend_mempool);
217 Mem_FreePool(&gl_backend_mempool);
220 backendactive = false;
223 static void gl_backend_bufferchanges(int init)
225 if (gl_mesh_drawmode.integer == 3 && qglDrawRangeElements != NULL)
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));
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);
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);
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);
249 max_batch = gl_mesh_batchtriangles.integer;
251 if (max_meshs != gl_mesh_maxtriangles.integer || max_transmeshs != gl_mesh_transtriangles.integer)
253 max_meshs = gl_mesh_maxtriangles.integer;
254 max_transmeshs = gl_mesh_transtriangles.integer;
258 resizingbuffers = true;
259 gl_backend_shutdown();
261 resizingbuffers = false;
266 static void gl_backend_newmap(void)
268 r_farclip = r_newfarclip = 2048.0f;
271 void gl_backend_init(void)
275 Cvar_RegisterVariable(&r_render);
276 Cvar_RegisterVariable(&gl_dither);
277 Cvar_RegisterVariable(&gl_lockarrays);
279 Cvar_SetValue("r_render", 0);
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++)
291 polyindexarray[i*3+0] = 0;
292 polyindexarray[i*3+1] = i + 1;
293 polyindexarray[i*3+2] = i + 2;
297 int arraylocked = false;
299 void GL_LockArray(int first, int count)
301 if (!arraylocked && gl_supportslockarrays && gl_lockarrays.integer && gl_mesh_drawmode.integer != 0)
303 qglLockArraysEXT(first, count);
309 void GL_UnlockArray(void)
313 qglUnlockArraysEXT();
324 static void GL_SetupFrame (void)
327 double fovx, fovy, zNear, zFar, aspect;
329 // update farclip based on previous frame
330 r_farclip = r_newfarclip;
332 if (!r_render.integer)
335 qglDepthFunc (GL_LEQUAL);CHECKGLERROR
338 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
339 qglLoadIdentity ();CHECKGLERROR
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
349 fovx = r_refdef.fov_x;
350 fovy = r_refdef.fov_y;
351 aspect = r_refdef.width / r_refdef.height;
354 xmax = zNear * tan(fovx * M_PI / 360.0) * aspect;
355 ymax = zNear * tan(fovy * M_PI / 360.0);
358 qglFrustum(-xmax, xmax, -ymax, ymax, zNear, zFar);CHECKGLERROR
360 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
361 qglLoadIdentity ();CHECKGLERROR
364 qglRotatef (-90, 1, 0, 0);CHECKGLERROR
365 qglRotatef (90, 0, 0, 1);CHECKGLERROR
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
371 qglTranslatef (-r_refdef.vieworg[0], -r_refdef.vieworg[1], -r_refdef.vieworg[2]);CHECKGLERROR
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];
384 void GL_SetupTextureState(void)
387 if (backendunits > 1)
389 for (i = 0;i < backendunits;i++)
391 qglActiveTexture(GL_TEXTURE0_ARB + (mesh_unit = i));CHECKGLERROR
392 qglBindTexture(GL_TEXTURE_2D, mesh_texture[i]);CHECKGLERROR
393 if (gl_combine.integer)
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
415 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
419 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
423 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
425 if (gl_mesh_drawmode.integer != 0)
427 qglClientActiveTexture(GL_TEXTURE0_ARB + (mesh_clientunit = i));CHECKGLERROR
428 qglTexCoordPointer(2, GL_FLOAT, sizeof(buf_texcoord_t), buf_texcoord[i]);CHECKGLERROR
431 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
435 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
442 qglBindTexture(GL_TEXTURE_2D, mesh_texture[0]);CHECKGLERROR
443 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
446 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
450 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
452 if (gl_mesh_drawmode.integer != 0)
454 qglTexCoordPointer(2, GL_FLOAT, sizeof(buf_texcoord_t), buf_texcoord[0]);CHECKGLERROR
457 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
461 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
467 // called at beginning of frame
469 void R_Mesh_Start(void)
473 Sys_Error("R_Mesh_Clear: called when backend is not active\n");
477 gl_backend_bufferchanges(false);
482 currenttransmesh = 0;
483 currenttranstriangle = 0;
484 currenttransvertex = 0;
487 viewdist = DotProduct(r_origin, vpn);
499 for (i = 0;i < backendunits;i++)
502 mesh_texturergbscale[i] = 1;
505 qglEnable(GL_CULL_FACE);CHECKGLERROR
506 qglCullFace(GL_FRONT);CHECKGLERROR
508 mesh_depthtest = true;
509 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
511 mesh_blendfunc1 = GL_ONE;
512 mesh_blendfunc2 = GL_ZERO;
513 qglBlendFunc(mesh_blendfunc1, mesh_blendfunc2);CHECKGLERROR
516 qglDisable(GL_BLEND);CHECKGLERROR
518 mesh_depthmask = GL_TRUE;
519 qglDepthMask(mesh_depthmask);CHECKGLERROR
522 if (gl_mesh_drawmode.integer != 0)
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)
529 qglColorPointer(4, GL_FLOAT, sizeof(buf_fcolor_t), &buf_fcolor[0].c[0]);CHECKGLERROR
533 qglColorPointer(4, GL_UNSIGNED_BYTE, sizeof(buf_bcolor_t), &buf_bcolor[0].c[0]);CHECKGLERROR
535 qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
538 GL_SetupTextureState();
541 int gl_backend_rebindtextures;
543 void GL_UpdateFarclip(void)
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++)
552 farclip = DotProduct(buf_vertex[i].v, vpn);
553 if (meshfarclip < farclip)
554 meshfarclip = farclip;
557 farclip = meshfarclip + 256.0f - viewdist; // + 256 just to be safe
559 // push out farclip for next frame
560 if (farclip > r_newfarclip)
561 r_newfarclip = ceil((farclip + 255) / 256) * 256 + 256;
564 void GL_ConvertColorsFloatToByte(void)
567 // LordHavoc: to avoid problems with aliasing (treating memory as two
568 // different types - exactly what this is doing), these must be volatile
570 volatile int *icolor;
571 volatile float *fcolor;
574 total = currentvertex * 4;
576 // shift float to have 8bit fraction at base of number
577 fcolor = &buf_fcolor->c[0];
578 for (i = 0;i < total;)
580 fcolor[i ] += 32768.0f;
581 fcolor[i + 1] += 32768.0f;
582 fcolor[i + 2] += 32768.0f;
583 fcolor[i + 3] += 32768.0f;
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;)
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;
600 void GL_MeshState(buf_mesh_t *mesh)
603 if (backendunits > 1)
605 for (i = 0;i < backendunits;i++)
607 if (mesh_texture[i] != mesh->textures[i])
611 qglActiveTexture(GL_TEXTURE0_ARB + (mesh_unit = i));CHECKGLERROR
613 if (mesh_texture[i] == 0)
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)
621 qglClientActiveTexture(GL_TEXTURE0_ARB + (mesh_clientunit = i));CHECKGLERROR
623 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
625 qglBindTexture(GL_TEXTURE_2D, (mesh_texture[i] = mesh->textures[i]));CHECKGLERROR
626 if (mesh_texture[i] == 0)
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)
634 qglClientActiveTexture(GL_TEXTURE0_ARB + (mesh_clientunit = i));CHECKGLERROR
636 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
639 if (mesh_texturergbscale[i] != mesh->texturergbscale[i])
643 qglActiveTexture(GL_TEXTURE0_ARB + (mesh_unit = i));CHECKGLERROR
645 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (mesh_texturergbscale[i] = mesh->texturergbscale[i]));CHECKGLERROR
651 if (mesh_texture[0] != mesh->textures[0])
653 if (mesh_texture[0] == 0)
655 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
656 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
658 qglBindTexture(GL_TEXTURE_2D, (mesh_texture[0] = mesh->textures[0]));CHECKGLERROR
659 if (mesh_texture[0] == 0)
661 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
662 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
666 if (mesh_blendfunc1 != mesh->blendfunc1 || mesh_blendfunc2 != mesh->blendfunc2)
668 qglBlendFunc(mesh_blendfunc1 = mesh->blendfunc1, mesh_blendfunc2 = mesh->blendfunc2);CHECKGLERROR
669 if (mesh_blendfunc2 == GL_ZERO)
671 if (mesh_blendfunc1 == GL_ONE)
676 qglDisable(GL_BLEND);CHECKGLERROR
684 qglEnable(GL_BLEND);CHECKGLERROR
693 qglEnable(GL_BLEND);CHECKGLERROR
697 if (mesh_depthtest != mesh->depthtest)
699 mesh_depthtest = mesh->depthtest;
701 qglEnable(GL_DEPTH_TEST);
703 qglDisable(GL_DEPTH_TEST);
705 if (mesh_depthmask != mesh->depthmask)
707 qglDepthMask(mesh_depthmask = mesh->depthmask);CHECKGLERROR
711 void GL_DrawRangeElements(int firstvert, int endvert, int indexcount, GLuint *index)
713 unsigned int i, j, in;
714 if (gl_mesh_drawmode.integer == 3 && qglDrawRangeElements == NULL)
715 Cvar_SetValueQuick(&gl_mesh_drawmode, 2);
717 if (gl_mesh_drawmode.integer == 3)
719 // GL 1.2 or GL 1.1 with extension
720 qglDrawRangeElements(GL_TRIANGLES, firstvert, endvert, indexcount, GL_UNSIGNED_INT, index);
722 else if (gl_mesh_drawmode.integer == 2)
725 qglDrawElements(GL_TRIANGLES, indexcount, GL_UNSIGNED_INT, index);
727 else if (gl_mesh_drawmode.integer == 1)
730 // feed it manually using glArrayElement
731 qglBegin(GL_TRIANGLES);
732 for (i = 0;i < indexcount;i++)
733 qglArrayElement(index[i]);
738 // GL 1.1 but not using vertex arrays - 3dfx glquake minigl driver
740 if (gl_mesh_drawmode.integer != 0)
741 Cvar_SetValueQuick(&gl_mesh_drawmode, 0);
742 qglBegin(GL_TRIANGLES);
743 if (r_multitexture.integer)
745 // the minigl doesn't have this (because it does not have ARB_multitexture)
746 for (i = 0;i < indexcount;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++)
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]);
758 for (i = 0;i < indexcount;i++)
761 qglColor4ub(buf_bcolor[in].c[0], buf_bcolor[in].c[1], buf_bcolor[in].c[2], buf_bcolor[in].c[3]);
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]);
771 // renders mesh buffers, called to flush buffers when full
772 void R_Mesh_Render(void)
782 Sys_Error("R_Mesh_Render: called when backend is not active\n");
791 if (!gl_mesh_floatcolors.integer || gl_mesh_drawmode.integer == 0)
792 GL_ConvertColorsFloatToByte();
794 if (gl_backend_rebindtextures)
796 gl_backend_rebindtextures = false;
797 GL_SetupTextureState();
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
804 if (currentmesh >= 2)
806 for (k = 1, mesh = buf_mesh + k;k < currentmesh;k++, mesh++)
810 firstvert = mesh->firstvert;
811 indexcount = mesh->triangles * 3;
812 index = (unsigned int *)&buf_tri[mesh->firsttriangle].index[0];
814 // if not using batching, skip the index adjustment
816 for (i = 0;i < indexcount;i++)
817 index[i] += firstvert;
819 GL_DrawRangeElements(firstvert, firstvert + mesh->verts, indexcount, index);CHECKGLERROR
827 GL_UnlockArray();CHECKGLERROR
830 // restores backend state, used when done with 3D rendering
831 void R_Mesh_Finish(void)
834 // flush any queued meshs
837 if (backendunits > 1)
839 for (i = backendunits - 1;i >= 0;i--)
841 qglActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
842 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
843 if (gl_combine.integer)
845 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
849 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
853 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
855 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
859 qglClientActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
860 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
866 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
867 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
870 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
875 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
876 qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
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
885 void R_Mesh_ClearDepth(void)
887 R_Mesh_AddTransparent();
889 qglClear(GL_DEPTH_BUFFER_BIT);
893 void R_Mesh_AddTransparent(void)
896 float viewdistcompare, centerscaler, dist1, dist2, dist3, center, maxdist;
897 buf_vertex_t *vert1, *vert2, *vert3;
899 buf_mesh_t *mesh, *transmesh;
901 if (!currenttransmesh)
904 // convert index data to transtris for sorting
905 for (j = 0;j < currenttransmesh;j++)
907 mesh = buf_transmesh + j;
908 k = mesh->firsttriangle;
909 index = &buf_transtri[k].index[0];
910 for (i = 0;i < mesh->triangles;i++)
912 tri = &buf_sorttranstri[k++];
914 tri->index[0] = *index++;
915 tri->index[1] = *index++;
916 tri->index[2] = *index++;
920 // map farclip to 0-4095 list range
921 centerscaler = (TRANSDEPTHRES / r_farclip) * (1.0f / 3.0f);
922 viewdistcompare = viewdist + 4.0f;
924 memset(buf_sorttranstri_list, 0, TRANSDEPTHRES * sizeof(buf_transtri_t *));
927 for (j = 0;j < currenttranstriangle;j++)
929 tri = &buf_sorttranstri[j];
930 i = tri->mesh->firstvert;
932 vert1 = &buf_transvertex[tri->index[0] + i];
933 vert2 = &buf_transvertex[tri->index[1] + i];
934 vert3 = &buf_transvertex[tri->index[2] + i];
936 dist1 = DotProduct(vert1->v, vpn);
937 dist2 = DotProduct(vert2->v, vpn);
938 dist3 = DotProduct(vert3->v, vpn);
940 maxdist = max(dist1, max(dist2, dist3));
941 if (maxdist < viewdistcompare)
944 center = (dist1 + dist2 + dist3) * centerscaler - viewdist;
947 i = bound(0, i, (TRANSDEPTHRES - 1));
951 center += 8388608.0f;
952 i = *((int *)¢er) & 0x7FFFFF;
953 i = min(i, (TRANSDEPTHRES - 1));
955 tri->next = buf_sorttranstri_list[i];
956 buf_sorttranstri_list[i] = tri;
960 for (i = 0;i < currenttransmesh;i++)
961 buf_transmesh[i].transchain = NULL;
963 for (j = 0;j < TRANSDEPTHRES;j++)
965 if ((tri = buf_sorttranstri_list[j]))
967 for (;tri;tri = tri->next)
969 if (!tri->mesh->transchain)
971 tri->mesh->chain = transmesh;
972 transmesh = tri->mesh;
974 tri->meshsortchain = tri->mesh->transchain;
975 tri->mesh->transchain = tri;
980 for (;transmesh;transmesh = transmesh->chain)
982 if (currentmesh >= max_meshs || currenttriangle + transmesh->triangles > max_batch || currenttriangle + transmesh->triangles > 1024 || currentvertex + transmesh->verts > max_verts)
985 mesh = &buf_mesh[currentmesh++];
986 *mesh = *transmesh; // copy mesh properties
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;
995 mesh->firsttriangle = currenttriangle;
996 for (tri = transmesh->transchain;tri;tri = tri->meshsortchain)
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];
1003 mesh->triangles = currenttriangle - mesh->firsttriangle;
1006 currenttransmesh = 0;
1007 currenttranstriangle = 0;
1008 currenttransvertex = 0;
1011 void R_Mesh_Draw(const rmeshinfo_t *m)
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];
1023 Sys_Error("R_Mesh_Draw: called when backend is not active\n");
1025 if (m->index == NULL
1027 || m->vertex == NULL
1029 Host_Error("R_Mesh_Draw: no triangles or verts\n");
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))
1036 for (i = 0, in = m->color + 3;i < m->numverts;i++, (int)in += m->colorstep)
1039 if (i == m->numverts)
1042 else if (m->ca < 0.01f)
1047 Sys_Error("R_Mesh_Draw: called when backend is not active\n");
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);
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)
1058 Con_Printf("R_Mesh_Draw: mesh too big for 3DFX drivers, rejected\n");
1062 if (m->numtriangles > max_meshs || m->numverts > max_verts)
1064 Con_Printf("R_Mesh_Draw: mesh too big for current gl_mesh_maxtriangles setting, rejected\n");
1071 if (currenttransmesh >= max_transmeshs || (currenttranstriangle + m->numtriangles) > max_transmeshs || (currenttransvertex + m->numverts) > max_transverts)
1075 Con_Printf("R_Mesh_Draw: ran out of room for transparent meshs\n");
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];
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];
1095 currenttranstriangle += m->numtriangles;
1096 currenttransvertex += m->numverts;
1100 if (currentmesh >= max_meshs || (currenttriangle + m->numtriangles) > max_batch || (currentvertex + m->numverts) > max_verts)
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];
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];
1116 currenttriangle += m->numtriangles;
1117 currentvertex += m->numverts;
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;
1131 if (m->blendfunc2 == GL_SRC_COLOR)
1133 if (m->blendfunc1 == GL_DST_COLOR) // 2x modulate with framebuffer
1140 overbright = gl_combine.integer;
1144 scaler *= overbrightscale;
1149 for (i = 0;i < backendunits;i++)
1151 if ((mesh->textures[i] = m->tex[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;
1157 if (overbright && j >= 0)
1158 mesh->texturergbscale[j] = 4;
1160 if (m->vertexstep != sizeof(buf_vertex_t))
1162 for (i = 0, in = m->vertex;i < m->numverts;i++, (int)in += m->vertexstep)
1164 vert[i].v[0] = in[0];
1165 vert[i].v[1] = in[1];
1166 vert[i].v[2] = in[2];
1170 memcpy(vert, m->vertex, m->numverts * sizeof(buf_vertex_t));
1174 for (i = 0, in = m->color;i < m->numverts;i++, (int)in += m->colorstep)
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];
1184 cr = m->cr * scaler;
1185 cg = m->cg * scaler;
1186 cb = m->cb * scaler;
1188 for (i = 0;i < m->numverts;i++)
1190 fcolor[i].c[0] = cr;
1191 fcolor[i].c[1] = cg;
1192 fcolor[i].c[2] = cb;
1193 fcolor[i].c[3] = ca;
1197 for (j = 0;j < MAX_TEXTUREUNITS && m->tex[j];j++)
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))
1203 for (i = 0, in = m->texcoords[j];i < m->numverts;i++, (int)in += m->texcoordstep[j])
1205 texcoord[j][i].t[0] = in[0];
1206 texcoord[j][i].t[1] = in[1];
1210 memcpy(&texcoord[j][0].t[0], m->texcoords[j], m->numverts * sizeof(buf_texcoord_t));
1213 if (currenttriangle >= max_batch)
1217 void R_Mesh_Draw_NativeOnly(const rmeshinfo_t *m)
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];
1228 Sys_Error("R_Mesh_Draw: called when backend is not active\n");
1230 if (m->index == NULL
1232 || m->vertex == NULL
1234 Host_Error("R_Mesh_Draw: no triangles or verts\n");
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))
1241 for (i = 0, in = m->color + 3;i < m->numverts;i++, (int)in += m->colorstep)
1244 if (i == m->numverts)
1247 else if (m->ca < 0.01f)
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)
1254 Con_Printf("R_Mesh_Draw_NativeOnly: mesh too big for 3DFX drivers, rejected\n");
1258 if (m->numtriangles > max_meshs || m->numverts > max_verts)
1260 Con_Printf("R_Mesh_Draw_NativeOnly: mesh too big for current gl_mesh_maxtriangles setting, rejected\n");
1267 if (currenttransmesh >= max_transmeshs || (currenttranstriangle + m->numtriangles) > max_transmeshs || (currenttransvertex + m->numverts) > max_transverts)
1271 Con_Printf("R_Mesh_Draw_NativeOnly: ran out of room for transparent meshs\n");
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];
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;
1295 if (currentmesh >= max_meshs || (currenttriangle + m->numtriangles) > max_batch || (currentvertex + m->numverts) > max_verts)
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];
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;
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;
1325 if (m->blendfunc2 == GL_SRC_COLOR)
1327 if (m->blendfunc1 == GL_DST_COLOR) // 2x modulate with framebuffer
1334 overbright = gl_combine.integer;
1338 scaler *= overbrightscale;
1342 for (i = 0;i < backendunits;i++)
1344 if ((mesh->textures[i] = m->tex[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;
1350 if (overbright && j >= 0)
1351 mesh->texturergbscale[j] = 4;
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++)
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");
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));
1371 memcpy(fcolor, m->color, m->numverts * sizeof(buf_fcolor_t));
1373 // do this as a second step because memcpy preloaded the cache, which we can't easily do
1376 for (i = 0;i < m->numverts;i++)
1378 fcolor[i].c[0] *= scaler;
1379 fcolor[i].c[1] *= scaler;
1380 fcolor[i].c[2] *= scaler;
1384 if (currenttriangle >= max_batch)
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)
1392 // these are static because gcc runs out of virtual registers otherwise
1393 int i, j, overbright;
1398 Sys_Error("R_Mesh_Draw: called when backend is not active\n");
1400 if (!m->numtriangles
1402 Host_Error("R_Mesh_Draw: no triangles or verts\n");
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)
1407 Con_Printf("R_Mesh_Draw_GetBuffer: mesh too big for 3DFX drivers, rejected\n");
1411 if (m->numtriangles > max_meshs || m->numverts > max_verts)
1413 Con_Printf("R_Mesh_Draw_GetBuffer: mesh too big for current gl_mesh_maxtriangles setting, rejected\n");
1420 if (currenttransmesh >= max_transmeshs || (currenttranstriangle + m->numtriangles) > max_transmeshs || (currenttransvertex + m->numverts) > max_transverts)
1424 Con_Printf("R_Mesh_Draw: ran out of room for transparent meshs\n");
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];
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;
1448 if (currentmesh >= max_meshs || (currenttriangle + m->numtriangles) > max_batch || (currentvertex + m->numverts) > max_verts)
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];
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;
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;
1477 if (m->blendfunc2 == GL_SRC_COLOR)
1479 if (m->blendfunc1 == GL_DST_COLOR) // 2x modulate with framebuffer
1486 overbright = gl_combine.integer;
1490 scaler *= overbrightscale;
1492 m->colorscale = scaler;
1495 for (i = 0;i < MAX_TEXTUREUNITS;i++)
1497 if ((mesh->textures[i] = m->tex[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);
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;
1507 if (overbright && j >= 0)
1508 mesh->texturergbscale[j] = 4;
1513 void R_Mesh_DrawPolygon(rmeshinfo_t *m, int numverts)
1515 m->index = polyindexarray;
1516 m->numverts = numverts;
1517 m->numtriangles = numverts - 2;
1518 if (m->numtriangles < 1)
1520 Con_Printf("R_Mesh_DrawPolygon: invalid vertex count\n");
1523 if (m->numtriangles >= 256)
1525 Con_Printf("R_Mesh_DrawPolygon: only up to 256 triangles (258 verts) supported\n");
1532 ==============================================================================
1536 ==============================================================================
1539 qboolean SCR_ScreenShot(char *filename, int x, int y, int width, int height)
1545 if (!r_render.integer)
1548 buffer = Mem_Alloc(tempmempool, width*height*3);
1549 qglReadPixels (x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer);
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;
1557 ret = Image_WriteTGARGB_preflipped(filename, width, height, buffer);
1563 //=============================================================================
1565 void R_ClearScreen(void)
1567 if (r_render.integer)
1570 qglClearColor(0,0,0,0);CHECKGLERROR
1572 qglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);CHECKGLERROR
1573 // set dithering mode
1574 if (gl_dither.integer)
1576 qglEnable(GL_DITHER);CHECKGLERROR
1580 qglDisable(GL_DITHER);CHECKGLERROR
1589 This is called every frame, and can also be called explicitly to flush
1593 void SCR_UpdateScreen (void)
1597 R_TimeReport("finish");
1599 if (gl_combine.integer && !gl_combine_extension)
1600 Cvar_SetValue("gl_combine", 0);
1602 lightscalebit = v_overbrightbits.integer;
1603 if (gl_combine.integer && r_multitexture.integer)
1606 lightscale = 1.0f / (float) (1 << lightscalebit);
1607 overbrightscale = 1.0f / (float) (1 << v_overbrightbits.integer);
1609 R_TimeReport("setup");
1613 R_TimeReport("clear");
1615 if (scr_conlines < vid.conheight)
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)