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");
787 if (!r_render.integer)
799 if (!gl_mesh_floatcolors.integer || gl_mesh_drawmode.integer == 0)
800 GL_ConvertColorsFloatToByte();
802 if (gl_backend_rebindtextures)
804 gl_backend_rebindtextures = false;
805 GL_SetupTextureState();
808 GL_MeshState(buf_mesh);
809 GL_LockArray(0, currentvertex);
810 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
812 if (currentmesh >= 2)
814 for (k = 1, mesh = buf_mesh + k;k < currentmesh;k++, mesh++)
818 firstvert = mesh->firstvert;
819 indexcount = mesh->triangles * 3;
820 index = (unsigned int *)&buf_tri[mesh->firsttriangle].index[0];
822 // if not using batching, skip the index adjustment
824 for (i = 0;i < indexcount;i++)
825 index[i] += firstvert;
827 GL_DrawRangeElements(firstvert, firstvert + mesh->verts, indexcount, index);CHECKGLERROR
835 GL_UnlockArray();CHECKGLERROR
838 // restores backend state, used when done with 3D rendering
839 void R_Mesh_Finish(void)
842 // flush any queued meshs
845 if (backendunits > 1)
847 for (i = backendunits - 1;i >= 0;i--)
849 qglActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
850 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
851 if (gl_combine.integer)
853 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
857 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
861 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
863 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
867 qglClientActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
868 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
874 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
875 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
878 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
883 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
884 qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
887 qglDisable(GL_BLEND);CHECKGLERROR
888 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
889 qglDepthMask(GL_TRUE);CHECKGLERROR
890 qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);CHECKGLERROR
893 void R_Mesh_ClearDepth(void)
895 R_Mesh_AddTransparent();
897 qglClear(GL_DEPTH_BUFFER_BIT);
901 void R_Mesh_AddTransparent(void)
904 float viewdistcompare, centerscaler, dist1, dist2, dist3, center, maxdist;
905 buf_vertex_t *vert1, *vert2, *vert3;
907 buf_mesh_t *mesh, *transmesh;
909 if (!currenttransmesh)
912 // convert index data to transtris for sorting
913 for (j = 0;j < currenttransmesh;j++)
915 mesh = buf_transmesh + j;
916 k = mesh->firsttriangle;
917 index = &buf_transtri[k].index[0];
918 for (i = 0;i < mesh->triangles;i++)
920 tri = &buf_sorttranstri[k++];
922 tri->index[0] = *index++;
923 tri->index[1] = *index++;
924 tri->index[2] = *index++;
928 // map farclip to 0-4095 list range
929 centerscaler = (TRANSDEPTHRES / r_farclip) * (1.0f / 3.0f);
930 viewdistcompare = viewdist + 4.0f;
932 memset(buf_sorttranstri_list, 0, TRANSDEPTHRES * sizeof(buf_transtri_t *));
935 for (j = 0;j < currenttranstriangle;j++)
937 tri = &buf_sorttranstri[j];
938 i = tri->mesh->firstvert;
940 vert1 = &buf_transvertex[tri->index[0] + i];
941 vert2 = &buf_transvertex[tri->index[1] + i];
942 vert3 = &buf_transvertex[tri->index[2] + i];
944 dist1 = DotProduct(vert1->v, vpn);
945 dist2 = DotProduct(vert2->v, vpn);
946 dist3 = DotProduct(vert3->v, vpn);
948 maxdist = max(dist1, max(dist2, dist3));
949 if (maxdist < viewdistcompare)
952 center = (dist1 + dist2 + dist3) * centerscaler - viewdist;
955 i = bound(0, i, (TRANSDEPTHRES - 1));
959 center += 8388608.0f;
960 i = *((int *)¢er) & 0x7FFFFF;
961 i = min(i, (TRANSDEPTHRES - 1));
963 tri->next = buf_sorttranstri_list[i];
964 buf_sorttranstri_list[i] = tri;
968 for (i = 0;i < currenttransmesh;i++)
969 buf_transmesh[i].transchain = NULL;
971 for (j = 0;j < TRANSDEPTHRES;j++)
973 if ((tri = buf_sorttranstri_list[j]))
975 for (;tri;tri = tri->next)
977 if (!tri->mesh->transchain)
979 tri->mesh->chain = transmesh;
980 transmesh = tri->mesh;
982 tri->meshsortchain = tri->mesh->transchain;
983 tri->mesh->transchain = tri;
988 for (;transmesh;transmesh = transmesh->chain)
990 if (currentmesh >= max_meshs || currenttriangle + transmesh->triangles > max_batch || currenttriangle + transmesh->triangles > 1024 || currentvertex + transmesh->verts > max_verts)
993 mesh = &buf_mesh[currentmesh++];
994 *mesh = *transmesh; // copy mesh properties
996 mesh->firstvert = currentvertex;
997 memcpy(&buf_vertex[currentvertex], &buf_transvertex[transmesh->firstvert], transmesh->verts * sizeof(buf_vertex_t));
998 memcpy(&buf_fcolor[currentvertex], &buf_transfcolor[transmesh->firstvert], transmesh->verts * sizeof(buf_fcolor_t));
999 for (i = 0;i < backendunits && transmesh->textures[i];i++)
1000 memcpy(&buf_texcoord[i][currentvertex], &buf_transtexcoord[i][transmesh->firstvert], transmesh->verts * sizeof(buf_texcoord_t));
1001 currentvertex += mesh->verts;
1003 mesh->firsttriangle = currenttriangle;
1004 for (tri = transmesh->transchain;tri;tri = tri->meshsortchain)
1006 buf_tri[currenttriangle].index[0] = tri->index[0];
1007 buf_tri[currenttriangle].index[1] = tri->index[1];
1008 buf_tri[currenttriangle].index[2] = tri->index[2];
1011 mesh->triangles = currenttriangle - mesh->firsttriangle;
1014 currenttransmesh = 0;
1015 currenttranstriangle = 0;
1016 currenttransvertex = 0;
1019 void R_Mesh_Draw(const rmeshinfo_t *m)
1021 // these are static because gcc runs out of virtual registers otherwise
1022 static int i, j, overbright, *index;
1023 static float *in, scaler;
1024 static float cr, cg, cb, ca;
1025 static buf_mesh_t *mesh;
1026 static buf_vertex_t *vert;
1027 static buf_fcolor_t *fcolor;
1028 static buf_texcoord_t *texcoord[MAX_TEXTUREUNITS];
1031 Sys_Error("R_Mesh_Draw: called when backend is not active\n");
1033 if (m->index == NULL
1035 || m->vertex == NULL
1037 Host_Error("R_Mesh_Draw: no triangles or verts\n");
1039 // ignore meaningless alpha meshs
1040 if (!m->depthwrite && m->blendfunc1 == GL_SRC_ALPHA && (m->blendfunc2 == GL_ONE || m->blendfunc2 == GL_ONE_MINUS_SRC_ALPHA))
1044 for (i = 0, in = m->color + 3;i < m->numverts;i++, (int)in += m->colorstep)
1047 if (i == m->numverts)
1050 else if (m->ca < 0.01f)
1055 Sys_Error("R_Mesh_Draw: called when backend is not active\n");
1058 for (i = 0;i < m->numtriangles * 3;i++)
1059 if ((unsigned int) m->index[i] >= (unsigned int) m->numverts)
1060 Host_Error("R_Mesh_Draw: invalid index (%i of %i verts)\n", m->index, m->numverts);
1063 // FIXME: we can work around this by falling back on non-array renderer if buffers are too big
1064 if (m->numtriangles > 1024 || m->numverts > 3072)
1066 Con_Printf("R_Mesh_Draw: mesh too big for 3DFX drivers, rejected\n");
1070 if (m->numtriangles > max_meshs || m->numverts > max_verts)
1072 Con_Printf("R_Mesh_Draw: mesh too big for current gl_mesh_maxtriangles setting, rejected\n");
1079 if (currenttransmesh >= max_transmeshs || (currenttranstriangle + m->numtriangles) > max_transmeshs || (currenttransvertex + m->numverts) > max_transverts)
1083 Con_Printf("R_Mesh_Draw: ran out of room for transparent meshs\n");
1090 c_transtris += m->numtriangles;
1091 vert = &buf_transvertex[currenttransvertex];
1092 fcolor = &buf_transfcolor[currenttransvertex];
1093 for (i = 0;i < backendunits;i++)
1094 texcoord[i] = &buf_transtexcoord[i][currenttransvertex];
1096 // transmesh is only for storage of transparent meshs until they
1097 // are inserted into the main mesh array
1098 mesh = &buf_transmesh[currenttransmesh++];
1099 mesh->firsttriangle = currenttranstriangle;
1100 mesh->firstvert = currenttransvertex;
1101 index = &buf_transtri[currenttranstriangle].index[0];
1103 currenttranstriangle += m->numtriangles;
1104 currenttransvertex += m->numverts;
1108 if (currentmesh >= max_meshs || (currenttriangle + m->numtriangles) > max_batch || (currentvertex + m->numverts) > max_verts)
1112 c_meshtris += m->numtriangles;
1113 vert = &buf_vertex[currentvertex];
1114 fcolor = &buf_fcolor[currentvertex];
1115 for (i = 0;i < backendunits;i++)
1116 texcoord[i] = &buf_texcoord[i][currentvertex];
1118 // opaque meshs are rendered directly
1119 mesh = &buf_mesh[currentmesh++];
1120 mesh->firsttriangle = currenttriangle;
1121 mesh->firstvert = currentvertex;
1122 index = &buf_tri[currenttriangle].index[0];
1124 currenttriangle += m->numtriangles;
1125 currentvertex += m->numverts;
1128 // code shared for transparent and opaque meshs
1129 memcpy(index, m->index, sizeof(int[3]) * m->numtriangles);
1130 mesh->blendfunc1 = m->blendfunc1;
1131 mesh->blendfunc2 = m->blendfunc2;
1132 mesh->depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite);
1133 mesh->depthtest = !m->depthdisable;
1134 mesh->triangles = m->numtriangles;
1135 mesh->verts = m->numverts;
1139 if (m->blendfunc2 == GL_SRC_COLOR)
1141 if (m->blendfunc1 == GL_DST_COLOR) // 2x modulate with framebuffer
1148 overbright = gl_combine.integer;
1152 scaler *= overbrightscale;
1157 for (i = 0;i < backendunits;i++)
1159 if ((mesh->textures[i] = m->tex[i]))
1161 mesh->texturergbscale[i] = m->texrgbscale[i];
1162 if (mesh->texturergbscale[i] != 1 && mesh->texturergbscale[i] != 2 && mesh->texturergbscale[i] != 4)
1163 mesh->texturergbscale[i] = 1;
1165 if (overbright && j >= 0)
1166 mesh->texturergbscale[j] = 4;
1168 if (m->vertexstep != sizeof(buf_vertex_t))
1170 for (i = 0, in = m->vertex;i < m->numverts;i++, (int)in += m->vertexstep)
1172 vert[i].v[0] = in[0];
1173 vert[i].v[1] = in[1];
1174 vert[i].v[2] = in[2];
1178 memcpy(vert, m->vertex, m->numverts * sizeof(buf_vertex_t));
1182 for (i = 0, in = m->color;i < m->numverts;i++, (int)in += m->colorstep)
1184 fcolor[i].c[0] = in[0] * scaler;
1185 fcolor[i].c[1] = in[1] * scaler;
1186 fcolor[i].c[2] = in[2] * scaler;
1187 fcolor[i].c[3] = in[3];
1192 cr = m->cr * scaler;
1193 cg = m->cg * scaler;
1194 cb = m->cb * scaler;
1196 for (i = 0;i < m->numverts;i++)
1198 fcolor[i].c[0] = cr;
1199 fcolor[i].c[1] = cg;
1200 fcolor[i].c[2] = cb;
1201 fcolor[i].c[3] = ca;
1205 for (j = 0;j < MAX_TEXTUREUNITS && m->tex[j];j++)
1207 if (j >= backendunits)
1208 Sys_Error("R_Mesh_Draw: texture %i supplied when there are only %i texture units\n", j + 1, backendunits);
1209 if (m->texcoordstep[j] != sizeof(buf_texcoord_t))
1211 for (i = 0, in = m->texcoords[j];i < m->numverts;i++, (int)in += m->texcoordstep[j])
1213 texcoord[j][i].t[0] = in[0];
1214 texcoord[j][i].t[1] = in[1];
1218 memcpy(&texcoord[j][0].t[0], m->texcoords[j], m->numverts * sizeof(buf_texcoord_t));
1221 if (currenttriangle >= max_batch)
1225 void R_Mesh_Draw_NativeOnly(const rmeshinfo_t *m)
1227 // these are static because gcc runs out of virtual registers otherwise
1228 static int i, j, overbright, *index;
1229 static float *in, scaler;
1230 static buf_mesh_t *mesh;
1231 static buf_vertex_t *vert;
1232 static buf_fcolor_t *fcolor;
1233 static buf_texcoord_t *texcoord[MAX_TEXTUREUNITS];
1236 Sys_Error("R_Mesh_Draw: called when backend is not active\n");
1238 if (m->index == NULL
1240 || m->vertex == NULL
1242 Host_Error("R_Mesh_Draw: no triangles or verts\n");
1244 // ignore meaningless alpha meshs
1245 if (!m->depthwrite && m->blendfunc1 == GL_SRC_ALPHA && (m->blendfunc2 == GL_ONE || m->blendfunc2 == GL_ONE_MINUS_SRC_ALPHA))
1249 for (i = 0, in = m->color + 3;i < m->numverts;i++, (int)in += m->colorstep)
1252 if (i == m->numverts)
1255 else if (m->ca < 0.01f)
1259 // FIXME: we can work around this by falling back on non-array renderer if buffers are too big
1260 if (m->numtriangles > 1024 || m->numverts > 3072)
1262 Con_Printf("R_Mesh_Draw_NativeOnly: mesh too big for 3DFX drivers, rejected\n");
1266 if (m->numtriangles > max_meshs || m->numverts > max_verts)
1268 Con_Printf("R_Mesh_Draw_NativeOnly: mesh too big for current gl_mesh_maxtriangles setting, rejected\n");
1275 if (currenttransmesh >= max_transmeshs || (currenttranstriangle + m->numtriangles) > max_transmeshs || (currenttransvertex + m->numverts) > max_transverts)
1279 Con_Printf("R_Mesh_Draw_NativeOnly: ran out of room for transparent meshs\n");
1286 c_transtris += m->numtriangles;
1287 vert = &buf_transvertex[currenttransvertex];
1288 fcolor = &buf_transfcolor[currenttransvertex];
1289 for (i = 0;i < backendunits;i++)
1290 texcoord[i] = &buf_transtexcoord[i][currenttransvertex];
1292 // transmesh is only for storage of transparent meshs until they
1293 // are inserted into the main mesh array
1294 mesh = &buf_transmesh[currenttransmesh++];
1295 mesh->firsttriangle = currenttranstriangle;
1296 mesh->firstvert = currenttransvertex;
1297 index = &buf_transtri[currenttranstriangle].index[0];
1298 currenttranstriangle += m->numtriangles;
1299 currenttransvertex += m->numverts;
1303 if (currentmesh >= max_meshs || (currenttriangle + m->numtriangles) > max_batch || (currentvertex + m->numverts) > max_verts)
1307 c_meshtris += m->numtriangles;
1308 vert = &buf_vertex[currentvertex];
1309 fcolor = &buf_fcolor[currentvertex];
1310 for (i = 0;i < backendunits;i++)
1311 texcoord[i] = &buf_texcoord[i][currentvertex];
1313 // opaque meshs are rendered directly
1314 mesh = &buf_mesh[currentmesh++];
1315 mesh->firsttriangle = currenttriangle;
1316 mesh->firstvert = currentvertex;
1317 index = &buf_tri[currenttriangle].index[0];
1318 currenttriangle += m->numtriangles;
1319 currentvertex += m->numverts;
1322 // code shared for transparent and opaque meshs
1323 memcpy(index, m->index, sizeof(int[3]) * m->numtriangles);
1324 mesh->blendfunc1 = m->blendfunc1;
1325 mesh->blendfunc2 = m->blendfunc2;
1326 mesh->depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite);
1327 mesh->depthtest = !m->depthdisable;
1328 mesh->triangles = m->numtriangles;
1329 mesh->verts = m->numverts;
1333 if (m->blendfunc2 == GL_SRC_COLOR)
1335 if (m->blendfunc1 == GL_DST_COLOR) // 2x modulate with framebuffer
1342 overbright = gl_combine.integer;
1346 scaler *= overbrightscale;
1350 for (i = 0;i < backendunits;i++)
1352 if ((mesh->textures[i] = m->tex[i]))
1354 mesh->texturergbscale[i] = m->texrgbscale[i];
1355 if (mesh->texturergbscale[i] != 1 && mesh->texturergbscale[i] != 2 && mesh->texturergbscale[i] != 4)
1356 mesh->texturergbscale[i] = 1;
1358 if (overbright && j >= 0)
1359 mesh->texturergbscale[j] = 4;
1361 if (m->vertexstep != sizeof(buf_vertex_t))
1362 Host_Error("R_Mesh_Draw_NativeOnly: unsupported vertexstep\n");
1363 if (m->colorstep != sizeof(buf_fcolor_t))
1364 Host_Error("R_Mesh_Draw_NativeOnly: unsupported colorstep\n");
1365 if (m->color == NULL)
1366 Host_Error("R_Mesh_Draw_NativeOnly: must provide color array\n");
1367 for (j = 0;j < MAX_TEXTUREUNITS && m->tex[j];j++)
1369 if (j >= backendunits)
1370 Sys_Error("R_Mesh_Draw_NativeOnly: texture %i supplied when there are only %i texture units\n", j + 1, backendunits);
1371 if (m->texcoordstep[j] != sizeof(buf_texcoord_t))
1372 Host_Error("R_Mesh_Draw_NativeOnly: unsupported texcoordstep\n");
1375 memcpy(vert, m->vertex, m->numverts * sizeof(buf_vertex_t));
1376 for (j = 0;j < MAX_TEXTUREUNITS && m->tex[j];j++)
1377 memcpy(&texcoord[j][0].t[0], m->texcoords[j], m->numverts * sizeof(buf_texcoord_t));
1379 memcpy(fcolor, m->color, m->numverts * sizeof(buf_fcolor_t));
1381 // do this as a second step because memcpy preloaded the cache, which we can't easily do
1384 for (i = 0;i < m->numverts;i++)
1386 fcolor[i].c[0] *= scaler;
1387 fcolor[i].c[1] *= scaler;
1388 fcolor[i].c[2] *= scaler;
1392 if (currenttriangle >= max_batch)
1396 // allocates space in geometry buffers, and fills in pointers to the buffers in passsed struct
1397 // (this is used for very high speed rendering, no copying)
1398 int R_Mesh_Draw_GetBuffer(rmeshbufferinfo_t *m)
1400 // these are static because gcc runs out of virtual registers otherwise
1401 int i, j, overbright;
1406 Sys_Error("R_Mesh_Draw: called when backend is not active\n");
1408 if (!m->numtriangles
1410 Host_Error("R_Mesh_Draw: no triangles or verts\n");
1412 // FIXME: we can work around this by falling back on non-array renderer if buffers are too big
1413 if (m->numtriangles > 1024 || m->numverts > 3072)
1415 Con_Printf("R_Mesh_Draw_GetBuffer: mesh too big for 3DFX drivers, rejected\n");
1419 if (m->numtriangles > max_meshs || m->numverts > max_verts)
1421 Con_Printf("R_Mesh_Draw_GetBuffer: mesh too big for current gl_mesh_maxtriangles setting, rejected\n");
1428 if (currenttransmesh >= max_transmeshs || (currenttranstriangle + m->numtriangles) > max_transmeshs || (currenttransvertex + m->numverts) > max_transverts)
1432 Con_Printf("R_Mesh_Draw: ran out of room for transparent meshs\n");
1439 c_transtris += m->numtriangles;
1440 m->index = &buf_transtri[currenttranstriangle].index[0];
1441 m->vertex = &buf_transvertex[currenttransvertex].v[0];
1442 m->color = &buf_transfcolor[currenttransvertex].c[0];
1443 for (i = 0;i < backendunits;i++)
1444 m->texcoords[i] = &buf_transtexcoord[i][currenttransvertex].t[0];
1446 // transmesh is only for storage of transparent meshs until they
1447 // are inserted into the main mesh array
1448 mesh = &buf_transmesh[currenttransmesh++];
1449 mesh->firsttriangle = currenttranstriangle;
1450 mesh->firstvert = currenttransvertex;
1451 currenttranstriangle += m->numtriangles;
1452 currenttransvertex += m->numverts;
1456 if (currentmesh >= max_meshs || (currenttriangle + m->numtriangles) > max_batch || (currentvertex + m->numverts) > max_verts)
1460 c_meshtris += m->numtriangles;
1461 m->index = &buf_tri[currenttriangle].index[0];
1462 m->vertex = &buf_vertex[currentvertex].v[0];
1463 m->color = &buf_fcolor[currentvertex].c[0];
1464 for (i = 0;i < backendunits;i++)
1465 m->texcoords[i] = &buf_texcoord[i][currentvertex].t[0];
1467 // opaque meshs are rendered directly
1468 mesh = &buf_mesh[currentmesh++];
1469 mesh->firsttriangle = currenttriangle;
1470 mesh->firstvert = currentvertex;
1471 currenttriangle += m->numtriangles;
1472 currentvertex += m->numverts;
1475 // code shared for transparent and opaque meshs
1476 mesh->blendfunc1 = m->blendfunc1;
1477 mesh->blendfunc2 = m->blendfunc2;
1478 mesh->depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite);
1479 mesh->depthtest = !m->depthdisable;
1480 mesh->triangles = m->numtriangles;
1481 mesh->verts = m->numverts;
1485 if (m->blendfunc2 == GL_SRC_COLOR)
1487 if (m->blendfunc1 == GL_DST_COLOR) // 2x modulate with framebuffer
1494 overbright = gl_combine.integer;
1498 scaler *= overbrightscale;
1500 m->colorscale = scaler;
1503 for (i = 0;i < MAX_TEXTUREUNITS;i++)
1505 if ((mesh->textures[i] = m->tex[i]))
1508 if (i >= backendunits)
1509 Sys_Error("R_Mesh_Draw_GetBuffer: texture %i supplied when there are only %i texture units\n", j + 1, backendunits);
1511 mesh->texturergbscale[i] = m->texrgbscale[i];
1512 if (mesh->texturergbscale[i] != 1 && mesh->texturergbscale[i] != 2 && mesh->texturergbscale[i] != 4)
1513 mesh->texturergbscale[i] = 1;
1515 if (overbright && j >= 0)
1516 mesh->texturergbscale[j] = 4;
1521 void R_Mesh_DrawPolygon(rmeshinfo_t *m, int numverts)
1523 m->index = polyindexarray;
1524 m->numverts = numverts;
1525 m->numtriangles = numverts - 2;
1526 if (m->numtriangles < 1)
1528 Con_Printf("R_Mesh_DrawPolygon: invalid vertex count\n");
1531 if (m->numtriangles >= 256)
1533 Con_Printf("R_Mesh_DrawPolygon: only up to 256 triangles (258 verts) supported\n");
1540 ==============================================================================
1544 ==============================================================================
1547 qboolean SCR_ScreenShot(char *filename, int x, int y, int width, int height)
1553 if (!r_render.integer)
1556 buffer = Mem_Alloc(tempmempool, width*height*3);
1557 qglReadPixels (x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer);
1560 // LordHavoc: compensate for v_overbrightbits when using hardware gamma
1561 if (v_hwgamma.integer)
1562 for (i = 0;i < width * height * 3;i++)
1563 buffer[i] <<= v_overbrightbits.integer;
1565 ret = Image_WriteTGARGB_preflipped(filename, width, height, buffer);
1571 //=============================================================================
1573 void R_ClearScreen(void)
1575 if (r_render.integer)
1578 qglClearColor(0,0,0,0);CHECKGLERROR
1580 qglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);CHECKGLERROR
1581 // set dithering mode
1582 if (gl_dither.integer)
1584 qglEnable(GL_DITHER);CHECKGLERROR
1588 qglDisable(GL_DITHER);CHECKGLERROR
1597 This is called every frame, and can also be called explicitly to flush
1601 void SCR_UpdateScreen (void)
1605 R_TimeReport("finish");
1607 if (gl_combine.integer && !gl_combine_extension)
1608 Cvar_SetValue("gl_combine", 0);
1610 lightscalebit = v_overbrightbits.integer;
1611 if (gl_combine.integer && r_multitexture.integer)
1614 lightscale = 1.0f / (float) (1 << lightscalebit);
1615 overbrightscale = 1.0f / (float) (1 << v_overbrightbits.integer);
1617 R_TimeReport("setup");
1621 R_TimeReport("clear");
1623 if (scr_conlines < vid.conheight)
1629 // tell driver to commit it's partially full geometry queue to the rendering queue
1630 // (this doesn't wait for the commands themselves to complete)