4 cvar_t gl_mesh_maxtriangles = {0, "gl_mesh_maxtriangles", "1024"};
5 cvar_t gl_mesh_floatcolors = {0, "gl_mesh_floatcolors", "1"};
6 cvar_t gl_mesh_drawmode = {CVAR_SAVE, "gl_mesh_drawmode", "3"};
8 cvar_t r_render = {0, "r_render", "1"};
9 cvar_t gl_dither = {CVAR_SAVE, "gl_dither", "1"}; // whether or not to use dithering
10 cvar_t gl_lockarrays = {0, "gl_lockarrays", "1"};
12 // this is used to increase gl_mesh_maxtriangles automatically if a mesh was
13 // too large for the buffers in the previous frame
14 int overflowedverts = 0;
16 int gl_maxdrawrangeelementsvertices;
17 int gl_maxdrawrangeelementsindices;
22 void GL_PrintError(int errornumber, char *filename, int linenumber)
26 #ifdef GL_INVALID_ENUM
28 Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
31 #ifdef GL_INVALID_VALUE
32 case GL_INVALID_VALUE:
33 Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
36 #ifdef GL_INVALID_OPERATION
37 case GL_INVALID_OPERATION:
38 Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
41 #ifdef GL_STACK_OVERFLOW
42 case GL_STACK_OVERFLOW:
43 Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
46 #ifdef GL_STACK_UNDERFLOW
47 case GL_STACK_UNDERFLOW:
48 Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
51 #ifdef GL_OUT_OF_MEMORY
52 case GL_OUT_OF_MEMORY:
53 Con_Printf("GL_OUT_OF_MEMORY at %s:%i\n", filename, linenumber);
56 #ifdef GL_TABLE_TOO_LARGE
57 case GL_TABLE_TOO_LARGE:
58 Con_Printf("GL_TABLE_TOO_LARGE at %s:%i\n", filename, linenumber);
62 Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
70 static float viewdist;
71 // sign bits (true if negative) for vpn[] entries, so quick integer compares can be used instead of float compares
72 static int vpnbit0, vpnbit1, vpnbit2;
74 int c_meshs, c_meshtris;
78 float overbrightscale;
80 void SCR_ScreenShot_f (void);
83 static int max_verts; // always max_meshs * 3
85 typedef struct buf_mesh_s
89 int blendfunc1, blendfunc2;
90 int textures[MAX_TEXTUREUNITS];
91 int texturergbscale[MAX_TEXTUREUNITS];
97 struct buf_mesh_s *chain;
101 typedef struct buf_tri_s
131 static int currentmesh, currenttriangle, currentvertex, backendunits, backendactive;
132 static buf_mesh_t *buf_mesh;
133 static buf_tri_t *buf_tri;
134 static buf_vertex_t *buf_vertex;
135 static buf_fcolor_t *buf_fcolor;
136 static buf_bcolor_t *buf_bcolor;
137 static buf_texcoord_t *buf_texcoord[MAX_TEXTUREUNITS];
139 static mempool_t *gl_backend_mempool;
140 static int resizingbuffers = false;
142 static void gl_backend_start(void)
146 qglGetIntegerv(GL_MAX_ELEMENTS_VERTICES, &gl_maxdrawrangeelementsvertices);
147 qglGetIntegerv(GL_MAX_ELEMENTS_INDICES, &gl_maxdrawrangeelementsindices);
149 Con_Printf("OpenGL Backend started with gl_mesh_maxtriangles %i\n", gl_mesh_maxtriangles.integer);
150 if (qglDrawRangeElements != NULL)
151 Con_Printf("glDrawRangeElements detected (max vertices %i, max indices %i)\n", gl_maxdrawrangeelementsvertices, gl_maxdrawrangeelementsindices);
152 if (strstr(gl_renderer, "3Dfx"))
154 Con_Printf("3Dfx driver detected, forcing gl_mesh_floatcolors to 0 to prevent crashs\n");
155 Cvar_SetValueQuick(&gl_mesh_floatcolors, 0);
159 max_verts = max_meshs * 3;
161 if (!gl_backend_mempool)
162 gl_backend_mempool = Mem_AllocPool("GL_Backend");
164 #define BACKENDALLOC(var, count, sizeofstruct, varname)\
166 var = Mem_Alloc(gl_backend_mempool, count * sizeof(sizeofstruct));\
168 Sys_Error("gl_backend_start: unable to allocate memory for %s (%d bytes)\n", (varname), count * sizeof(sizeofstruct));\
169 memset(var, 0, count * sizeof(sizeofstruct));\
172 BACKENDALLOC(buf_mesh, max_meshs, buf_mesh_t, "buf_mesh")
173 BACKENDALLOC(buf_tri, max_meshs, buf_tri_t, "buf_tri")
174 BACKENDALLOC(buf_vertex, max_verts, buf_vertex_t, "buf_vertex")
175 BACKENDALLOC(buf_fcolor, max_verts, buf_fcolor_t, "buf_fcolor")
176 BACKENDALLOC(buf_bcolor, max_verts, buf_bcolor_t, "buf_bcolor")
178 for (i = 0;i < MAX_TEXTUREUNITS;i++)
180 // only allocate as many texcoord arrays as we need
181 if (i < gl_textureunits)
183 BACKENDALLOC(buf_texcoord[i], max_verts, buf_texcoord_t, va("buf_texcoord[%d]", i))
187 buf_texcoord[i] = NULL;
190 backendunits = min(MAX_TEXTUREUNITS, gl_textureunits);
191 backendactive = true;
194 static void gl_backend_shutdown(void)
196 Con_Printf("OpenGL Backend shutting down\n");
199 Mem_EmptyPool(gl_backend_mempool);
201 Mem_FreePool(&gl_backend_mempool);
204 backendactive = false;
207 static void gl_backend_bufferchanges(int init)
209 if (overflowedverts > gl_mesh_maxtriangles.integer * 3)
210 Cvar_SetValueQuick(&gl_mesh_maxtriangles, (int) ((overflowedverts + 2) / 3));
213 if (gl_mesh_drawmode.integer < 0)
214 Cvar_SetValueQuick(&gl_mesh_drawmode, 0);
215 if (gl_mesh_drawmode.integer > 3)
216 Cvar_SetValueQuick(&gl_mesh_drawmode, 3);
218 if (gl_mesh_drawmode.integer >= 3 && qglDrawRangeElements == NULL)
220 // change drawmode 3 to 2 if 3 won't work at all
221 Cvar_SetValueQuick(&gl_mesh_drawmode, 2);
224 // 21760 is (65536 / 3) rounded off to a multiple of 128
225 if (gl_mesh_maxtriangles.integer < 1024)
226 Cvar_SetValueQuick(&gl_mesh_maxtriangles, 1024);
227 if (gl_mesh_maxtriangles.integer > 21760)
228 Cvar_SetValueQuick(&gl_mesh_maxtriangles, 21760);
230 if (max_meshs != gl_mesh_maxtriangles.integer)
232 max_meshs = gl_mesh_maxtriangles.integer;
236 resizingbuffers = true;
237 gl_backend_shutdown();
239 resizingbuffers = false;
244 static void gl_backend_newmap(void)
248 void gl_backend_init(void)
250 Cvar_RegisterVariable(&r_render);
251 Cvar_RegisterVariable(&gl_dither);
252 Cvar_RegisterVariable(&gl_lockarrays);
254 Cvar_SetValue("r_render", 0);
257 Cvar_RegisterVariable(&gl_mesh_maxtriangles);
258 Cvar_RegisterVariable(&gl_mesh_floatcolors);
259 Cvar_RegisterVariable(&gl_mesh_drawmode);
260 R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap);
261 gl_backend_bufferchanges(true);
264 int arraylocked = false;
266 void GL_LockArray(int first, int count)
268 if (!arraylocked && gl_supportslockarrays && gl_lockarrays.integer && gl_mesh_drawmode.integer > 0)
270 qglLockArraysEXT(first, count);
276 void GL_UnlockArray(void)
280 qglUnlockArraysEXT();
291 static void GL_SetupFrame (void)
294 double fovx, fovy, zNear, zFar, aspect;
296 if (!r_render.integer)
299 qglDepthFunc (GL_LEQUAL);CHECKGLERROR
302 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
303 qglLoadIdentity ();CHECKGLERROR
305 // y is weird beause OpenGL is bottom to top, we use top to bottom
306 qglViewport(r_refdef.x, vid.realheight - (r_refdef.y + r_refdef.height), r_refdef.width, r_refdef.height);CHECKGLERROR
310 zFar = r_mesh_farclip;
313 fovx = r_refdef.fov_x;
314 fovy = r_refdef.fov_y;
315 aspect = r_refdef.width / r_refdef.height;
318 xmax = zNear * tan(fovx * M_PI / 360.0) * aspect;
319 ymax = zNear * tan(fovy * M_PI / 360.0);
322 qglFrustum(-xmax, xmax, -ymax, ymax, zNear, zFar);CHECKGLERROR
324 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
325 qglLoadIdentity ();CHECKGLERROR
328 qglRotatef (-90, 1, 0, 0);CHECKGLERROR
329 qglRotatef (90, 0, 0, 1);CHECKGLERROR
331 qglRotatef (-r_refdef.viewangles[2], 1, 0, 0);CHECKGLERROR
332 qglRotatef (-r_refdef.viewangles[0], 0, 1, 0);CHECKGLERROR
333 qglRotatef (-r_refdef.viewangles[1], 0, 0, 1);CHECKGLERROR
335 qglTranslatef (-r_refdef.vieworg[0], -r_refdef.vieworg[1], -r_refdef.vieworg[2]);CHECKGLERROR
347 int texture[MAX_TEXTUREUNITS];
348 float texturergbscale[MAX_TEXTUREUNITS];
352 void GL_SetupTextureState(void)
355 if (backendunits > 1)
357 for (i = 0;i < backendunits;i++)
359 qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
360 qglBindTexture(GL_TEXTURE_2D, gl_state.texture[i]);CHECKGLERROR
361 if (gl_combine.integer)
363 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);CHECKGLERROR
364 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);CHECKGLERROR
365 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);CHECKGLERROR
366 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
367 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_CONSTANT_ARB);CHECKGLERROR
368 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
369 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
370 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);CHECKGLERROR
371 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);CHECKGLERROR
372 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);CHECKGLERROR
373 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
374 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, GL_CONSTANT_ARB);CHECKGLERROR
375 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
376 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
377 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
378 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, gl_state.texturergbscale[i]);CHECKGLERROR
379 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
383 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
385 if (gl_state.texture[i])
387 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
391 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
393 if (gl_mesh_drawmode.integer > 0)
395 qglClientActiveTexture(GL_TEXTURE0_ARB + (gl_state.clientunit = i));CHECKGLERROR
396 qglTexCoordPointer(2, GL_FLOAT, sizeof(buf_texcoord_t), buf_texcoord[i]);CHECKGLERROR
397 if (gl_state.texture[i])
399 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
403 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
410 qglBindTexture(GL_TEXTURE_2D, gl_state.texture[0]);CHECKGLERROR
411 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
412 if (gl_state.texture[0])
414 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
418 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
420 if (gl_mesh_drawmode.integer > 0)
422 qglTexCoordPointer(2, GL_FLOAT, sizeof(buf_texcoord_t), buf_texcoord[0]);CHECKGLERROR
423 if (gl_state.texture[0])
425 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
429 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
435 // called at beginning of frame
437 void R_Mesh_Start(float farclip)
441 Sys_Error("R_Mesh_Clear: called when backend is not active\n");
445 gl_backend_bufferchanges(false);
450 r_mesh_farclip = farclip;
451 viewdist = DotProduct(r_origin, vpn);
452 vpnbit0 = vpn[0] < 0;
453 vpnbit1 = vpn[1] < 0;
454 vpnbit2 = vpn[2] < 0;
462 gl_state.clientunit = 0;
464 for (i = 0;i < backendunits;i++)
466 gl_state.texture[i] = 0;
467 gl_state.texturergbscale[i] = 1;
470 qglEnable(GL_CULL_FACE);CHECKGLERROR
471 qglCullFace(GL_FRONT);CHECKGLERROR
473 gl_state.depthtest = true;
474 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
476 gl_state.blendfunc1 = GL_ONE;
477 gl_state.blendfunc2 = GL_ZERO;
478 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
481 qglDisable(GL_BLEND);CHECKGLERROR
483 gl_state.depthmask = GL_TRUE;
484 qglDepthMask(gl_state.depthmask);CHECKGLERROR
487 if (gl_mesh_drawmode.integer > 0)
490 qglVertexPointer(3, GL_FLOAT, sizeof(buf_vertex_t), &buf_vertex[0].v[0]);CHECKGLERROR
491 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
492 if (gl_mesh_floatcolors.integer)
494 qglColorPointer(4, GL_FLOAT, sizeof(buf_fcolor_t), &buf_fcolor[0].c[0]);CHECKGLERROR
498 qglColorPointer(4, GL_UNSIGNED_BYTE, sizeof(buf_bcolor_t), &buf_bcolor[0].c[0]);CHECKGLERROR
500 qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
503 GL_SetupTextureState();
506 int gl_backend_rebindtextures;
508 void GL_ConvertColorsFloatToByte(void)
511 // LordHavoc: to avoid problems with aliasing (treating memory as two
512 // different types - exactly what this is doing), these must be volatile
514 volatile int *icolor;
515 volatile float *fcolor;
518 total = currentvertex * 4;
520 // shift float to have 8bit fraction at base of number
521 fcolor = &buf_fcolor->c[0];
522 for (i = 0;i < total;)
524 fcolor[i ] += 32768.0f;
525 fcolor[i + 1] += 32768.0f;
526 fcolor[i + 2] += 32768.0f;
527 fcolor[i + 3] += 32768.0f;
531 // then read as integer and kill float bits...
532 icolor = (int *)&buf_fcolor->c[0];
533 bcolor = &buf_bcolor->c[0];
534 for (i = 0;i < total;)
536 k = icolor[i ] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i ] = (qbyte) k;
537 k = icolor[i + 1] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i + 1] = (qbyte) k;
538 k = icolor[i + 2] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i + 2] = (qbyte) k;
539 k = icolor[i + 3] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i + 3] = (qbyte) k;
544 void GL_MeshState(buf_mesh_t *mesh)
547 if (backendunits > 1)
549 for (i = 0;i < backendunits;i++)
551 if (gl_state.texture[i] != mesh->textures[i])
553 if (gl_state.unit != i)
555 qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
557 if (gl_state.texture[i] == 0)
559 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
560 // have to disable texcoord array on disabled texture
561 // units due to NVIDIA driver bug with
562 // compiled_vertex_array
563 if (gl_state.clientunit != i)
565 qglClientActiveTexture(GL_TEXTURE0_ARB + (gl_state.clientunit = i));CHECKGLERROR
567 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
569 qglBindTexture(GL_TEXTURE_2D, (gl_state.texture[i] = mesh->textures[i]));CHECKGLERROR
570 if (gl_state.texture[i] == 0)
572 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
573 // have to disable texcoord array on disabled texture
574 // units due to NVIDIA driver bug with
575 // compiled_vertex_array
576 if (gl_state.clientunit != i)
578 qglClientActiveTexture(GL_TEXTURE0_ARB + (gl_state.clientunit = i));CHECKGLERROR
580 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
583 if (gl_state.texturergbscale[i] != mesh->texturergbscale[i])
585 if (gl_state.unit != i)
587 qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
589 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (gl_state.texturergbscale[i] = mesh->texturergbscale[i]));CHECKGLERROR
595 if (gl_state.texture[0] != mesh->textures[0])
597 if (gl_state.texture[0] == 0)
599 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
600 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
602 qglBindTexture(GL_TEXTURE_2D, (gl_state.texture[0] = mesh->textures[0]));CHECKGLERROR
603 if (gl_state.texture[0] == 0)
605 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
606 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
610 if (gl_state.blendfunc1 != mesh->blendfunc1 || gl_state.blendfunc2 != mesh->blendfunc2)
612 qglBlendFunc(gl_state.blendfunc1 = mesh->blendfunc1, gl_state.blendfunc2 = mesh->blendfunc2);CHECKGLERROR
613 if (gl_state.blendfunc2 == GL_ZERO)
615 if (gl_state.blendfunc1 == GL_ONE)
620 qglDisable(GL_BLEND);CHECKGLERROR
628 qglEnable(GL_BLEND);CHECKGLERROR
637 qglEnable(GL_BLEND);CHECKGLERROR
641 if (gl_state.depthtest != mesh->depthtest)
643 gl_state.depthtest = mesh->depthtest;
644 if (gl_state.depthtest)
645 qglEnable(GL_DEPTH_TEST);
647 qglDisable(GL_DEPTH_TEST);
649 if (gl_state.depthmask != mesh->depthmask)
651 qglDepthMask(gl_state.depthmask = mesh->depthmask);CHECKGLERROR
655 void GL_DrawRangeElements(int firstvert, int endvert, int indexcount, GLuint *index)
657 unsigned int i, j, in;
658 if (gl_mesh_drawmode.integer >= 3/* && (endvert - firstvert) <= gl_maxdrawrangeelementsvertices && (indexcount) <= gl_maxdrawrangeelementsindices*/)
660 // GL 1.2 or GL 1.1 with extension
661 qglDrawRangeElements(GL_TRIANGLES, firstvert, endvert, indexcount, GL_UNSIGNED_INT, index);
663 else if (gl_mesh_drawmode.integer >= 2)
666 qglDrawElements(GL_TRIANGLES, indexcount, GL_UNSIGNED_INT, index);
668 else if (gl_mesh_drawmode.integer >= 1)
671 // feed it manually using glArrayElement
672 qglBegin(GL_TRIANGLES);
673 for (i = 0;i < indexcount;i++)
674 qglArrayElement(index[i]);
679 // GL 1.1 but not using vertex arrays - 3dfx glquake minigl driver
681 qglBegin(GL_TRIANGLES);
682 if (gl_state.texture[1]) // if the mesh uses multiple textures
684 // the minigl doesn't have this (because it does not have ARB_multitexture)
685 for (i = 0;i < indexcount;i++)
688 qglColor4ub(buf_bcolor[in].c[0], buf_bcolor[in].c[1], buf_bcolor[in].c[2], buf_bcolor[in].c[3]);
689 for (j = 0;j < backendunits;j++)
690 if (gl_state.texture[j])
691 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, buf_texcoord[j][in].t[0], buf_texcoord[j][in].t[1]);
692 qglVertex3f(buf_vertex[in].v[0], buf_vertex[in].v[1], buf_vertex[in].v[2]);
697 for (i = 0;i < indexcount;i++)
700 qglColor4ub(buf_bcolor[in].c[0], buf_bcolor[in].c[1], buf_bcolor[in].c[2], buf_bcolor[in].c[3]);
701 if (gl_state.texture[0])
702 qglTexCoord2f(buf_texcoord[0][in].t[0], buf_texcoord[0][in].t[1]);
703 qglVertex3f(buf_vertex[in].v[0], buf_vertex[in].v[1], buf_vertex[in].v[2]);
710 // renders mesh buffers, called to flush buffers when full
711 void R_Mesh_Render(void)
718 Sys_Error("R_Mesh_Render: called when backend is not active\n");
723 if (!r_render.integer)
733 // drawmode 0 always uses byte colors
734 if (!gl_mesh_floatcolors.integer || gl_mesh_drawmode.integer <= 0)
735 GL_ConvertColorsFloatToByte();
737 if (gl_backend_rebindtextures)
739 gl_backend_rebindtextures = false;
740 GL_SetupTextureState();
743 GL_MeshState(buf_mesh);
744 for (k = 0, mesh = buf_mesh;k < currentmesh;k++, mesh++)
746 for (i = 0, v = buf_vertex[mesh->firstvert].v;i < mesh->verts;i++, v += 4)
748 VectorCopy(v, tempv);
749 Matrix4x4_Transform(&mesh->matrix, tempv, v);
752 GL_LockArray(0, currentvertex);
753 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
755 if (currentmesh >= 2)
757 for (k = 1, mesh = buf_mesh + k;k < currentmesh;k++, mesh++)
760 GL_DrawRangeElements(mesh->firstvert, mesh->firstvert + mesh->verts, mesh->triangles * 3, buf_tri[mesh->firsttriangle].index);CHECKGLERROR
768 GL_UnlockArray();CHECKGLERROR
771 // restores backend state, used when done with 3D rendering
772 void R_Mesh_Finish(void)
775 // flush any queued meshs
779 if (backendunits > 1)
781 for (i = backendunits - 1;i >= 0;i--)
783 qglActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
784 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
785 if (gl_combine.integer)
787 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
791 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
795 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
797 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
801 qglClientActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
802 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
808 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
809 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
812 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
817 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
818 qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
821 qglDisable(GL_BLEND);CHECKGLERROR
822 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
823 qglDepthMask(GL_TRUE);CHECKGLERROR
824 qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);CHECKGLERROR
827 void R_Mesh_ClearDepth(void)
832 qglClear(GL_DEPTH_BUFFER_BIT);
833 R_Mesh_Start(r_mesh_farclip);
836 // allocates space in geometry buffers, and fills in pointers to the buffers in passsed struct
837 // (this is used for very high speed rendering, no copying)
838 int R_Mesh_Draw_GetBuffer(rmeshbufferinfo_t *m, int wantoverbright)
840 // these are static because gcc runs out of virtual registers otherwise
841 int i, j, overbright;
846 Sys_Error("R_Mesh_Draw_GetBuffer: called when backend is not active\n");
850 Host_Error("R_Mesh_Draw_GetBuffer: no triangles or verts\n");
852 // LordHavoc: removed this error condition because with floatcolors 0,
853 // the 3DFX driver works with very large meshs
854 // FIXME: we can work around this by falling back on non-array renderer if buffers are too big
855 //if (m->numtriangles > 1024 || m->numverts > 3072)
857 // Con_Printf("R_Mesh_Draw_GetBuffer: mesh too big for 3DFX drivers, rejected\n");
861 i = max(m->numtriangles * 3, m->numverts);
862 if (overflowedverts < i)
865 if (m->numtriangles > max_meshs || m->numverts > max_verts)
867 Con_Printf("R_Mesh_Draw_GetBuffer: mesh too big for current gl_mesh_maxtriangles setting, increasing limits\n");
874 Con_Printf("mesh queue not empty, flushing.\n");
878 c_meshtris += m->numtriangles;
879 m->index = &buf_tri[currenttriangle].index[0];
880 m->vertex = &buf_vertex[currentvertex].v[0];
881 m->color = &buf_fcolor[currentvertex].c[0];
882 for (i = 0;i < backendunits;i++)
883 m->texcoords[i] = &buf_texcoord[i][currentvertex].t[0];
885 // opaque meshs are rendered directly
886 mesh = &buf_mesh[currentmesh++];
887 mesh->firsttriangle = currenttriangle;
888 mesh->firstvert = currentvertex;
889 currenttriangle += m->numtriangles;
890 currentvertex += m->numverts;
892 mesh->blendfunc1 = m->blendfunc1;
893 mesh->blendfunc2 = m->blendfunc2;
894 mesh->depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite);
895 mesh->depthtest = !m->depthdisable;
896 mesh->triangles = m->numtriangles;
897 mesh->verts = m->numverts;
898 mesh->matrix = m->matrix; // this copies the struct
902 if (m->blendfunc1 == GL_DST_COLOR)
904 // check if it is a 2x modulate with framebuffer
905 if (m->blendfunc2 == GL_SRC_COLOR)
908 else if (m->blendfunc2 != GL_SRC_COLOR)
912 overbright = wantoverbright && gl_combine.integer;
916 scaler *= overbrightscale;
918 m->colorscale = scaler;
921 for (i = 0;i < MAX_TEXTUREUNITS;i++)
923 if ((mesh->textures[i] = m->tex[i]))
926 if (i >= backendunits)
927 Sys_Error("R_Mesh_Draw_GetBuffer: texture %i supplied when there are only %i texture units\n", j + 1, backendunits);
929 mesh->texturergbscale[i] = m->texrgbscale[i];
930 if (mesh->texturergbscale[i] != 1 && mesh->texturergbscale[i] != 2 && mesh->texturergbscale[i] != 4)
931 mesh->texturergbscale[i] = 1;
933 if (overbright && j >= 0)
934 mesh->texturergbscale[j] = 4;
940 ==============================================================================
944 ==============================================================================
947 qboolean SCR_ScreenShot(char *filename, int x, int y, int width, int height)
953 if (!r_render.integer)
956 buffer = Mem_Alloc(tempmempool, width*height*3);
957 qglReadPixels (x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer);
960 // LordHavoc: compensate for v_overbrightbits when using hardware gamma
961 if (v_hwgamma.integer)
962 for (i = 0;i < width * height * 3;i++)
963 buffer[i] <<= v_overbrightbits.integer;
965 ret = Image_WriteTGARGB_preflipped(filename, width, height, buffer);
971 //=============================================================================
973 void R_ClearScreen(void)
975 if (r_render.integer)
978 qglClearColor(0,0,0,0);CHECKGLERROR
980 qglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);CHECKGLERROR
981 // set dithering mode
982 if (gl_dither.integer)
984 qglEnable(GL_DITHER);CHECKGLERROR
988 qglDisable(GL_DITHER);CHECKGLERROR
997 This is called every frame, and can also be called explicitly to flush
1001 void SCR_UpdateScreen (void)
1005 R_TimeReport("finish");
1007 if (r_textureunits.integer > gl_textureunits)
1008 Cvar_SetValueQuick(&r_textureunits, gl_textureunits);
1009 if (r_textureunits.integer < 1)
1010 Cvar_SetValueQuick(&r_textureunits, 1);
1012 if (gl_combine.integer && (!gl_combine_extension || r_textureunits.integer < 2))
1013 Cvar_SetValueQuick(&gl_combine, 0);
1016 overbrightscale = 1.0f / (float) (1 << v_overbrightbits.integer);
1019 lightscalebit = v_overbrightbits.integer;
1020 if (gl_combine.integer && r_textureunits.integer > 1)
1022 lightscale = 1.0f / (float) (1 << lightscalebit);
1024 R_TimeReport("setup");
1028 R_TimeReport("clear");
1030 if (scr_conlines < vid.conheight)
1036 // tell driver to commit it's partially full geometry queue to the rendering queue
1037 // (this doesn't wait for the commands themselves to complete)