4 //cvar_t gl_mesh_maxtriangles = {0, "gl_mesh_maxtriangles", "21760"};
5 cvar_t gl_mesh_maxtriangles = {0, "gl_mesh_maxtriangles", "8192"};
6 //cvar_t gl_mesh_batchtriangles = {0, "gl_mesh_batchtriangles", "1024"};
7 cvar_t gl_mesh_batchtriangles = {0, "gl_mesh_batchtriangles", "0"};
8 cvar_t gl_mesh_floatcolors = {0, "gl_mesh_floatcolors", "0"};
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"};
17 void GL_PrintError(int errornumber, char *filename, int linenumber)
21 #ifdef GL_INVALID_ENUM
23 Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
26 #ifdef GL_INVALID_VALUE
27 case GL_INVALID_VALUE:
28 Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
31 #ifdef GL_INVALID_OPERATION
32 case GL_INVALID_OPERATION:
33 Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
36 #ifdef GL_STACK_OVERFLOW
37 case GL_STACK_OVERFLOW:
38 Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
41 #ifdef GL_STACK_UNDERFLOW
42 case GL_STACK_UNDERFLOW:
43 Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
46 #ifdef GL_OUT_OF_MEMORY
47 case GL_OUT_OF_MEMORY:
48 Con_Printf("GL_OUT_OF_MEMORY at %s:%i\n", filename, linenumber);
51 #ifdef GL_TABLE_TOO_LARGE
52 case GL_TABLE_TOO_LARGE:
53 Con_Printf("GL_TABLE_TOO_LARGE at %s:%i\n", filename, linenumber);
57 Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
63 float r_farclip, r_newfarclip;
65 int polyindexarray[768];
67 static float viewdist;
69 int c_meshs, c_meshtris, c_transmeshs, c_transtris;
73 float overbrightscale;
75 void SCR_ScreenShot_f (void);
79 static int max_verts; // always max_meshs * 3
80 #define TRANSDEPTHRES 4096
82 typedef struct buf_mesh_s
86 int blendfunc1, blendfunc2;
87 int textures[MAX_TEXTUREUNITS];
88 float texturergbscale[MAX_TEXTUREUNITS];
93 struct buf_mesh_s *chain;
94 struct buf_transtri_s *transchain;
98 typedef struct buf_transtri_s
100 struct buf_transtri_s *next;
101 struct buf_transtri_s *meshsortchain;
107 typedef struct buf_tri_s
137 static float meshfarclip;
138 static int currentmesh, currenttriangle, currentvertex, backendunits, backendactive, transranout;
139 static buf_mesh_t *buf_mesh;
140 static buf_tri_t *buf_tri;
141 static buf_vertex_t *buf_vertex;
142 static buf_fcolor_t *buf_fcolor;
143 static buf_bcolor_t *buf_bcolor;
144 static buf_texcoord_t *buf_texcoord[MAX_TEXTUREUNITS];
146 static int currenttransmesh, currenttransvertex, currenttranstriangle;
147 static buf_mesh_t *buf_transmesh;
148 static buf_transtri_t *buf_sorttranstri;
149 static buf_transtri_t **buf_sorttranstri_list;
150 static buf_tri_t *buf_transtri;
151 static buf_vertex_t *buf_transvertex;
152 static buf_fcolor_t *buf_transfcolor;
153 static buf_texcoord_t *buf_transtexcoord[MAX_TEXTUREUNITS];
155 static mempool_t *gl_backend_mempool;
156 static int resizingbuffers = false;
158 static void gl_backend_start(void)
162 max_verts = max_meshs * 3;
164 if (!gl_backend_mempool)
165 gl_backend_mempool = Mem_AllocPool("GL_Backend");
167 #define BACKENDALLOC(var, count, sizeofstruct)\
169 var = Mem_Alloc(gl_backend_mempool, count * sizeof(sizeofstruct));\
171 Sys_Error("gl_backend_start: unable to allocate memory\n");\
172 memset(var, 0, count * sizeof(sizeofstruct));\
175 BACKENDALLOC(buf_mesh, max_meshs, buf_mesh_t)
176 BACKENDALLOC(buf_tri, max_meshs, buf_tri_t)
177 BACKENDALLOC(buf_vertex, max_verts, buf_vertex_t)
178 BACKENDALLOC(buf_fcolor, max_verts, buf_fcolor_t)
179 BACKENDALLOC(buf_bcolor, max_verts, buf_bcolor_t)
181 BACKENDALLOC(buf_transmesh, max_meshs, buf_mesh_t)
182 BACKENDALLOC(buf_sorttranstri, max_meshs, buf_transtri_t)
183 BACKENDALLOC(buf_sorttranstri_list, TRANSDEPTHRES, buf_transtri_t *)
184 BACKENDALLOC(buf_transtri, max_meshs, buf_tri_t)
185 BACKENDALLOC(buf_transvertex, max_verts, buf_vertex_t)
186 BACKENDALLOC(buf_transfcolor, max_verts, buf_fcolor_t)
188 for (i = 0;i < MAX_TEXTUREUNITS;i++)
190 // only allocate as many texcoord arrays as we need
191 if (i < gl_textureunits)
193 BACKENDALLOC(buf_texcoord[i], max_verts, buf_texcoord_t)
194 BACKENDALLOC(buf_transtexcoord[i], max_verts, buf_texcoord_t)
198 buf_texcoord[i] = NULL;
199 buf_transtexcoord[i] = NULL;
202 backendunits = min(MAX_TEXTUREUNITS, gl_textureunits);
203 backendactive = true;
206 static void gl_backend_shutdown(void)
209 Mem_EmptyPool(gl_backend_mempool);
211 Mem_FreePool(&gl_backend_mempool);
214 backendactive = false;
217 static void gl_backend_bufferchanges(int init)
219 // 21760 is (65536 / 3) rounded off to a multiple of 128
220 if (gl_mesh_maxtriangles.integer < 256)
221 Cvar_SetValue("gl_mesh_maxtriangles", 256);
222 if (gl_mesh_maxtriangles.integer > 21760)
223 Cvar_SetValue("gl_mesh_maxtriangles", 21760);
225 if (gl_mesh_batchtriangles.integer < 0)
226 Cvar_SetValue("gl_mesh_batchtriangles", 0);
227 if (gl_mesh_batchtriangles.integer > gl_mesh_maxtriangles.integer)
228 Cvar_SetValue("gl_mesh_batchtriangles", gl_mesh_maxtriangles.integer);
230 max_batch = gl_mesh_batchtriangles.integer;
232 if (max_meshs != gl_mesh_maxtriangles.integer)
234 max_meshs = gl_mesh_maxtriangles.integer;
238 resizingbuffers = true;
239 gl_backend_shutdown();
241 resizingbuffers = false;
246 static void gl_backend_newmap(void)
248 r_farclip = r_newfarclip = 2048.0f;
251 void gl_backend_init(void)
255 Cvar_RegisterVariable(&r_render);
256 Cvar_RegisterVariable(&gl_dither);
257 Cvar_RegisterVariable(&gl_lockarrays);
259 Cvar_SetValue("r_render", 0);
262 Cvar_RegisterVariable(&gl_mesh_maxtriangles);
263 Cvar_RegisterVariable(&gl_mesh_batchtriangles);
264 Cvar_RegisterVariable(&gl_mesh_floatcolors);
265 R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap);
266 gl_backend_bufferchanges(true);
267 for (i = 0;i < 256;i++)
269 polyindexarray[i*3+0] = 0;
270 polyindexarray[i*3+1] = i + 1;
271 polyindexarray[i*3+2] = i + 2;
275 int arraylocked = false;
277 void GL_LockArray(int first, int count)
279 if (!arraylocked && gl_supportslockarrays && gl_lockarrays.integer)
281 qglLockArraysEXT(first, count);
287 void GL_UnlockArray(void)
291 qglUnlockArraysEXT();
297 //static float gldepthmin, gldepthmax;
304 static void GL_SetupFrame (void)
307 double fovx, fovy, zNear, zFar, aspect;
309 // update farclip based on previous frame
310 r_farclip = r_newfarclip;
312 if (!r_render.integer)
315 // glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // LordHavoc: moved to SCR_UpdateScreen
318 glDepthFunc (GL_LEQUAL);CHECKGLERROR
320 // glDepthRange (gldepthmin, gldepthmax);CHECKGLERROR
323 glMatrixMode(GL_PROJECTION);CHECKGLERROR
324 glLoadIdentity ();CHECKGLERROR
326 // y is weird beause OpenGL is bottom to top, we use top to bottom
327 glViewport(r_refdef.x, vid.realheight - (r_refdef.y + r_refdef.height), r_refdef.width, r_refdef.height);CHECKGLERROR
334 fovx = r_refdef.fov_x;
335 fovy = r_refdef.fov_y;
336 aspect = r_refdef.width / r_refdef.height;
339 xmax = zNear * tan(fovx * M_PI / 360.0) * aspect;
340 ymax = zNear * tan(fovy * M_PI / 360.0);
343 glFrustum(-xmax, xmax, -ymax, ymax, zNear, zFar);CHECKGLERROR
345 // glCullFace(GL_FRONT);CHECKGLERROR
347 glMatrixMode(GL_MODELVIEW);CHECKGLERROR
348 glLoadIdentity ();CHECKGLERROR
351 glRotatef (-90, 1, 0, 0);CHECKGLERROR
352 glRotatef (90, 0, 0, 1);CHECKGLERROR
354 glRotatef (-r_refdef.viewangles[2], 1, 0, 0);CHECKGLERROR
355 glRotatef (-r_refdef.viewangles[0], 0, 1, 0);CHECKGLERROR
356 glRotatef (-r_refdef.viewangles[1], 0, 0, 1);CHECKGLERROR
358 glTranslatef (-r_refdef.vieworg[0], -r_refdef.vieworg[1], -r_refdef.vieworg[2]);CHECKGLERROR
360 // glGetFloatv (GL_MODELVIEW_MATRIX, r_world_matrix);
365 // if (gl_cull.integer)
367 // glEnable(GL_CULL_FACE);CHECKGLERROR
371 // glDisable(GL_CULL_FACE);CHECKGLERROR
374 // glEnable(GL_BLEND);CHECKGLERROR
375 // glEnable(GL_DEPTH_TEST);CHECKGLERROR
376 // glDepthMask(1);CHECKGLERROR
379 static int mesh_blendfunc1;
380 static int mesh_blendfunc2;
381 static int mesh_blend;
382 static GLboolean mesh_depthmask;
383 static int mesh_depthtest;
384 static int mesh_unit;
385 static int mesh_clientunit;
386 static int mesh_texture[MAX_TEXTUREUNITS];
387 static float mesh_texturergbscale[MAX_TEXTUREUNITS];
389 // called at beginning of frame
390 void R_Mesh_Start(void)
394 Sys_Error("R_Mesh_Clear: called when backend is not active\n");
398 gl_backend_bufferchanges(false);
403 currenttransmesh = 0;
404 currenttranstriangle = 0;
405 currenttransvertex = 0;
408 viewdist = DotProduct(r_origin, vpn);
420 for (i = 0;i < backendunits;i++)
423 mesh_texturergbscale[i] = 1;
426 glEnable(GL_CULL_FACE);CHECKGLERROR
427 glCullFace(GL_FRONT);CHECKGLERROR
429 mesh_depthtest = true;
430 glEnable(GL_DEPTH_TEST);CHECKGLERROR
432 mesh_blendfunc1 = GL_ONE;
433 mesh_blendfunc2 = GL_ZERO;
434 glBlendFunc(mesh_blendfunc1, mesh_blendfunc2);CHECKGLERROR
437 glDisable(GL_BLEND);CHECKGLERROR
439 mesh_depthmask = GL_TRUE;
440 glDepthMask(mesh_depthmask);CHECKGLERROR
442 glVertexPointer(3, GL_FLOAT, sizeof(buf_vertex_t), &buf_vertex[0].v[0]);CHECKGLERROR
443 glEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
444 if (gl_mesh_floatcolors.integer)
446 glColorPointer(4, GL_FLOAT, sizeof(buf_fcolor_t), &buf_fcolor[0].c[0]);CHECKGLERROR
450 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(buf_bcolor_t), &buf_bcolor[0].c[0]);CHECKGLERROR
452 glEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
454 if (backendunits > 1)
456 for (i = 0;i < backendunits;i++)
458 qglActiveTexture(GL_TEXTURE0_ARB + (mesh_unit = i));CHECKGLERROR
459 glBindTexture(GL_TEXTURE_2D, mesh_texture[i]);CHECKGLERROR
460 glDisable(GL_TEXTURE_2D);CHECKGLERROR
461 if (gl_combine.integer)
463 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);CHECKGLERROR
464 glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);CHECKGLERROR
465 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);CHECKGLERROR
466 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
467 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_CONSTANT_ARB);CHECKGLERROR
468 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
469 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
470 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);CHECKGLERROR
471 glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);CHECKGLERROR
472 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);CHECKGLERROR
473 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
474 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, GL_CONSTANT_ARB);CHECKGLERROR
475 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
476 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
477 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
478 glTexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, mesh_texturergbscale[i]);CHECKGLERROR
479 glTexEnvf(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1.0f);CHECKGLERROR
483 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
486 qglClientActiveTexture(GL_TEXTURE0_ARB + (mesh_clientunit = i));CHECKGLERROR
487 glTexCoordPointer(2, GL_FLOAT, sizeof(buf_texcoord_t), buf_texcoord[i]);CHECKGLERROR
488 glEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
493 glBindTexture(GL_TEXTURE_2D, (mesh_texture[0] = 0));CHECKGLERROR
494 glDisable(GL_TEXTURE_2D);CHECKGLERROR
495 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
497 glTexCoordPointer(2, GL_FLOAT, sizeof(buf_texcoord_t), buf_texcoord[0]);CHECKGLERROR
498 glEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
502 // renders mesh buffers, called to flush buffers when full
503 void R_Mesh_Render(void)
515 // float to byte color conversion
521 Sys_Error("R_Mesh_Render: called when backend is not active\n");
528 // push out farclip based on vertices
529 // FIXME: wouldn't this be a little slow when using matrix transforms?
530 for (i = 0;i < currentvertex;i++)
532 farclip = DotProduct(buf_vertex[i].v, vpn);
533 if (meshfarclip < farclip)
534 meshfarclip = farclip;
537 farclip = meshfarclip + 256.0f - viewdist; // + 256 just to be safe
539 // push out farclip for next frame
540 if (farclip > r_newfarclip)
541 r_newfarclip = ceil((farclip + 255) / 256) * 256 + 256;
543 if (!gl_mesh_floatcolors.integer)
545 // shift float to have 8bit fraction at base of number
546 for (i = 0, fcolor = &buf_fcolor->c[0];i < currentvertex;i++)
548 *fcolor++ += 32768.0f;
549 *fcolor++ += 32768.0f;
550 *fcolor++ += 32768.0f;
551 *fcolor++ += 32768.0f;
553 // then read as integer and kill float bits...
554 for (i = 0, icolor = (int *)&buf_fcolor->c[0], bcolor = &buf_bcolor->c[0];i < currentvertex;i++)
556 k = (*icolor++) & 0x7FFFFF;*bcolor++ = k > 255 ? 255 : k;
557 k = (*icolor++) & 0x7FFFFF;*bcolor++ = k > 255 ? 255 : k;
558 k = (*icolor++) & 0x7FFFFF;*bcolor++ = k > 255 ? 255 : k;
559 k = (*icolor++) & 0x7FFFFF;*bcolor++ = k > 255 ? 255 : k;
563 // lock the arrays now that they will have no further modifications
564 //GL_LockArray(0, currentvertex);CHECKGLERROR
566 for (k = 0, mesh = buf_mesh;k < currentmesh;k++, mesh++)
568 if (backendunits > 1)
570 for (i = 0;i < backendunits;i++)
572 if (mesh_texture[i] != mesh->textures[i])
576 qglActiveTexture(GL_TEXTURE0_ARB + (mesh_unit = i));CHECKGLERROR
578 if (mesh_texture[i] == 0)
580 glEnable(GL_TEXTURE_2D);CHECKGLERROR
581 // have to disable texcoord array on disabled texture
582 // units due to NVIDIA driver bug with
583 // compiled_vertex_array
584 if (mesh_clientunit != i)
586 qglClientActiveTexture(GL_TEXTURE0_ARB + (mesh_clientunit = i));CHECKGLERROR
588 glEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
590 glBindTexture(GL_TEXTURE_2D, (mesh_texture[i] = mesh->textures[i]));CHECKGLERROR
591 if (mesh_texture[i] == 0)
593 glDisable(GL_TEXTURE_2D);CHECKGLERROR
594 // have to disable texcoord array on disabled texture
595 // units due to NVIDIA driver bug with
596 // compiled_vertex_array
597 if (mesh_clientunit != i)
599 qglClientActiveTexture(GL_TEXTURE0_ARB + (mesh_clientunit = i));CHECKGLERROR
601 glDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
604 if (mesh_texturergbscale[i] != mesh->texturergbscale[i])
608 qglActiveTexture(GL_TEXTURE0_ARB + (mesh_unit = i));CHECKGLERROR
610 glTexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (mesh_texturergbscale[i] = mesh->texturergbscale[i]));CHECKGLERROR
616 if (mesh_texture[0] != mesh->textures[0])
618 if (mesh_texture[0] == 0)
620 glEnable(GL_TEXTURE_2D);CHECKGLERROR
621 glEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
623 glBindTexture(GL_TEXTURE_2D, (mesh_texture[0] = mesh->textures[0]));CHECKGLERROR
624 if (mesh_texture[0] == 0)
626 glDisable(GL_TEXTURE_2D);CHECKGLERROR
627 glDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
631 if (mesh_blendfunc1 != mesh->blendfunc1 || mesh_blendfunc2 != mesh->blendfunc2)
633 glBlendFunc(mesh_blendfunc1 = mesh->blendfunc1, mesh_blendfunc2 = mesh->blendfunc2);CHECKGLERROR
634 if (mesh_blendfunc2 == GL_ZERO)
636 if (mesh_blendfunc1 == GL_ONE)
641 glDisable(GL_BLEND);CHECKGLERROR
649 glEnable(GL_BLEND);CHECKGLERROR
658 glEnable(GL_BLEND);CHECKGLERROR
662 if (mesh_depthtest != mesh->depthtest)
664 mesh_depthtest = mesh->depthtest;
666 glEnable(GL_DEPTH_TEST);
668 glDisable(GL_DEPTH_TEST);
670 if (mesh_depthmask != mesh->depthmask)
672 glDepthMask(mesh_depthmask = mesh->depthmask);CHECKGLERROR
675 firsttriangle = mesh->firsttriangle;
676 firstvert = mesh->firstvert;
677 endtriangle = firsttriangle + mesh->triangles;
678 endvert = firstvert + mesh->verts;
680 indexcount = (endtriangle - firsttriangle) * 3;
681 index = (unsigned int *)&buf_tri[firsttriangle].index[0];
683 // if not using batching, skip the index adjustment
685 for (i = 0;i < indexcount;i++)
686 index[i] += firstvert;
688 // lock arrays (this is ignored if already locked)
690 GL_LockArray(0, currentvertex);
692 // FIXME: dynamic link to GL so we can get DrawRangeElements on WIN32
693 glDrawElements(GL_TRIANGLES, indexcount, GL_UNSIGNED_INT, index);CHECKGLERROR
695 glDrawRangeElements(GL_TRIANGLES, firstvert, endvert, indexcount, GL_UNSIGNED_INT, index);CHECKGLERROR
703 GL_UnlockArray();CHECKGLERROR
706 // restores backend state, used when done with 3D rendering
707 void R_Mesh_Finish(void)
710 // flush any queued meshs
713 if (backendunits > 1)
715 for (i = backendunits - 1;i >= 0;i--)
717 qglActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
718 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
719 if (gl_combine.integer)
721 glTexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1.0f);CHECKGLERROR
725 glDisable(GL_TEXTURE_2D);CHECKGLERROR
729 glEnable(GL_TEXTURE_2D);CHECKGLERROR
731 glBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
733 qglClientActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
734 glDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
739 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
740 glEnable(GL_TEXTURE_2D);CHECKGLERROR
741 glDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
743 glDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
744 glDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
746 glDisable(GL_BLEND);CHECKGLERROR
747 glEnable(GL_DEPTH_TEST);CHECKGLERROR
748 glDepthMask(true);CHECKGLERROR
749 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);CHECKGLERROR
752 void R_Mesh_ClearDepth(void)
754 R_Mesh_AddTransparent();
756 glClear(GL_DEPTH_BUFFER_BIT);
760 void R_Mesh_AddTransparent(void)
763 float viewdistcompare, centerscaler, dist1, dist2, dist3, center, maxdist;
764 buf_vertex_t *vert1, *vert2, *vert3;
766 buf_mesh_t *mesh, *transmesh;
768 if (!currenttransmesh)
771 // convert index data to transtris for sorting
772 for (j = 0;j < currenttransmesh;j++)
774 mesh = buf_transmesh + j;
775 k = mesh->firsttriangle;
776 index = &buf_transtri[k].index[0];
777 for (i = 0;i < mesh->triangles;i++)
779 tri = &buf_sorttranstri[k++];
781 tri->index[0] = *index++;
782 tri->index[1] = *index++;
783 tri->index[2] = *index++;
787 // map farclip to 0-4095 list range
788 centerscaler = (TRANSDEPTHRES / r_farclip) * (1.0f / 3.0f);
789 viewdistcompare = viewdist + 4.0f;
791 memset(buf_sorttranstri_list, 0, TRANSDEPTHRES * sizeof(buf_transtri_t *));
794 for (j = 0;j < currenttranstriangle;j++)
796 tri = &buf_sorttranstri[j];
797 i = tri->mesh->firstvert;
799 vert1 = &buf_transvertex[tri->index[0] + i];
800 vert2 = &buf_transvertex[tri->index[1] + i];
801 vert3 = &buf_transvertex[tri->index[2] + i];
803 dist1 = DotProduct(vert1->v, vpn);
804 dist2 = DotProduct(vert2->v, vpn);
805 dist3 = DotProduct(vert3->v, vpn);
807 maxdist = max(dist1, max(dist2, dist3));
808 if (maxdist < viewdistcompare)
811 center = (dist1 + dist2 + dist3) * centerscaler - viewdist;
814 i = bound(0, i, (TRANSDEPTHRES - 1));
818 center += 8388608.0f;
819 i = *((long *)¢er) & 0x7FFFFF;
820 i = min(i, (TRANSDEPTHRES - 1));
822 tri->next = buf_sorttranstri_list[i];
823 buf_sorttranstri_list[i] = tri;
827 for (i = 0;i < currenttransmesh;i++)
828 buf_transmesh[i].transchain = NULL;
830 for (j = 0;j < TRANSDEPTHRES;j++)
832 if ((tri = buf_sorttranstri_list[j]))
834 for (;tri;tri = tri->next)
836 if (!tri->mesh->transchain)
838 tri->mesh->chain = transmesh;
839 transmesh = tri->mesh;
841 tri->meshsortchain = tri->mesh->transchain;
842 tri->mesh->transchain = tri;
847 for (;transmesh;transmesh = transmesh->chain)
849 if (currentmesh >= max_meshs || currenttriangle + transmesh->triangles > max_batch || currentvertex + transmesh->verts > max_verts)
852 mesh = &buf_mesh[currentmesh++];
853 *mesh = *transmesh; // copy mesh properties
855 mesh->firstvert = currentvertex;
856 memcpy(&buf_vertex[currentvertex], &buf_transvertex[transmesh->firstvert], transmesh->verts * sizeof(buf_vertex_t));
857 memcpy(&buf_fcolor[currentvertex], &buf_transfcolor[transmesh->firstvert], transmesh->verts * sizeof(buf_fcolor_t));
858 for (i = 0;i < backendunits && transmesh->textures[i];i++)
859 memcpy(&buf_texcoord[i][currentvertex], &buf_transtexcoord[i][transmesh->firstvert], transmesh->verts * sizeof(buf_texcoord_t));
860 currentvertex += mesh->verts;
862 mesh->firsttriangle = currenttriangle;
863 for (tri = transmesh->transchain;tri;tri = tri->meshsortchain)
865 buf_tri[currenttriangle].index[0] = tri->index[0];
866 buf_tri[currenttriangle].index[1] = tri->index[1];
867 buf_tri[currenttriangle].index[2] = tri->index[2];
870 mesh->triangles = currenttriangle - mesh->firsttriangle;
873 currenttransmesh = 0;
874 currenttranstriangle = 0;
875 currenttransvertex = 0;
878 void R_Mesh_Draw(const rmeshinfo_t *m)
880 // these are static because gcc runs out of virtual registers otherwise
881 static int i, j, overbright, *index;
882 static float *in, scaler;
883 static float cr, cg, cb, ca;
884 static buf_mesh_t *mesh;
885 static buf_vertex_t *vert;
886 static buf_fcolor_t *fcolor;
887 static buf_texcoord_t *texcoord[MAX_TEXTUREUNITS];
890 Sys_Error("R_Mesh_Draw: called when backend is not active\n");
896 Host_Error("R_Mesh_Draw: no triangles or verts\n");
898 // ignore meaningless alpha meshs
899 if (!m->depthwrite && m->blendfunc1 == GL_SRC_ALPHA && (m->blendfunc2 == GL_ONE || m->blendfunc2 == GL_ONE_MINUS_SRC_ALPHA))
903 for (i = 0, in = m->color + 3;i < m->numverts;i++, (int)in += m->colorstep)
906 if (i == m->numverts)
909 else if (m->ca < 0.01f)
914 Sys_Error("R_Mesh_Draw: called when backend is not active\n");
917 for (i = 0;i < m->numtriangles * 3;i++)
918 if ((unsigned int) m->index[i] >= (unsigned int) m->numverts)
919 Host_Error("R_Mesh_Draw: invalid index (%i of %i verts)\n", m->index, m->numverts);
924 if (currenttransmesh >= max_meshs || (currenttranstriangle + m->numtriangles) > max_meshs || (currenttransvertex + m->numverts) > max_verts)
928 Con_Printf("R_Mesh_Draw: ran out of room for transparent meshs\n");
935 c_transtris += m->numtriangles;
936 vert = &buf_transvertex[currenttransvertex];
937 fcolor = &buf_transfcolor[currenttransvertex];
938 for (i = 0;i < backendunits;i++)
939 texcoord[i] = &buf_transtexcoord[i][currenttransvertex];
941 // transmesh is only for storage of transparent meshs until they
942 // are inserted into the main mesh array
943 mesh = &buf_transmesh[currenttransmesh++];
944 mesh->firsttriangle = currenttranstriangle;
945 mesh->firstvert = currenttransvertex;
946 index = &buf_transtri[currenttranstriangle].index[0];
948 currenttranstriangle += m->numtriangles;
949 currenttransvertex += m->numverts;
953 if (m->numtriangles > max_meshs || m->numverts > max_verts)
955 Con_Printf("R_Mesh_Draw: mesh too big for buffers\n");
959 if (currentmesh >= max_meshs || (currenttriangle + m->numtriangles) > max_batch || (currentvertex + m->numverts) > max_verts)
963 c_meshtris += m->numtriangles;
964 vert = &buf_vertex[currentvertex];
965 fcolor = &buf_fcolor[currentvertex];
966 for (i = 0;i < backendunits;i++)
967 texcoord[i] = &buf_texcoord[i][currentvertex];
969 // opaque meshs are rendered directly
970 mesh = &buf_mesh[currentmesh++];
971 mesh->firsttriangle = currenttriangle;
972 mesh->firstvert = currentvertex;
973 index = &buf_tri[currenttriangle].index[0];
975 currenttriangle += m->numtriangles;
976 currentvertex += m->numverts;
979 // code shared for transparent and opaque meshs
980 memcpy(index, m->index, sizeof(int[3]) * m->numtriangles);
981 mesh->blendfunc1 = m->blendfunc1;
982 mesh->blendfunc2 = m->blendfunc2;
983 mesh->depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite);
984 mesh->depthtest = !m->depthdisable;
985 mesh->triangles = m->numtriangles;
986 mesh->verts = m->numverts;
990 if (m->blendfunc2 == GL_SRC_COLOR)
992 if (m->blendfunc1 == GL_DST_COLOR) // 2x modulate with framebuffer
999 overbright = gl_combine.integer;
1003 scaler *= overbrightscale;
1008 for (i = 0;i < backendunits;i++)
1010 if ((mesh->textures[i] = m->tex[i]))
1012 mesh->texturergbscale[i] = m->texrgbscale[i];
1013 if (mesh->texturergbscale[i] != 1 && mesh->texturergbscale[i] != 2 && mesh->texturergbscale[i] != 4)
1014 mesh->texturergbscale[i] = 1;
1016 if (overbright && j >= 0)
1017 mesh->texturergbscale[j] = 4;
1019 if (m->vertexstep != sizeof(buf_vertex_t))
1021 for (i = 0, in = m->vertex;i < m->numverts;i++, (int)in += m->vertexstep)
1023 vert[i].v[0] = in[0];
1024 vert[i].v[1] = in[1];
1025 vert[i].v[2] = in[2];
1029 memcpy(vert, m->vertex, m->numverts * sizeof(buf_vertex_t));
1033 for (i = 0, in = m->color;i < m->numverts;i++, (int)in += m->colorstep)
1035 fcolor[i].c[0] = in[0] * scaler;
1036 fcolor[i].c[1] = in[1] * scaler;
1037 fcolor[i].c[2] = in[2] * scaler;
1038 fcolor[i].c[3] = in[3];
1043 cr = m->cr * scaler;
1044 cg = m->cg * scaler;
1045 cb = m->cb * scaler;
1047 for (i = 0;i < m->numverts;i++)
1049 fcolor[i].c[0] = cr;
1050 fcolor[i].c[1] = cg;
1051 fcolor[i].c[2] = cb;
1052 fcolor[i].c[3] = ca;
1056 for (j = 0;j < MAX_TEXTUREUNITS && m->tex[j];j++)
1058 if (j >= backendunits)
1059 Sys_Error("R_Mesh_Draw: texture %i supplied when there are only %i texture units\n", j + 1, backendunits);
1060 if (m->texcoordstep[j] != sizeof(buf_texcoord_t))
1062 for (i = 0, in = m->texcoords[j];i < m->numverts;i++, (int)in += m->texcoordstep[j])
1064 texcoord[j][i].t[0] = in[0];
1065 texcoord[j][i].t[1] = in[1];
1069 memcpy(&texcoord[j][0].t[0], m->texcoords[j], m->numverts * sizeof(buf_texcoord_t));
1072 for (;j < backendunits;j++)
1073 memset(&texcoord[j][0].t[0], 0, m->numverts * sizeof(buf_texcoord_t));
1077 void R_Mesh_Draw_NativeOnly(const rmeshinfo_t *m)
1079 // these are static because gcc runs out of virtual registers otherwise
1080 static int i, j, overbright, *index;
1081 static float *in, scaler;
1082 static buf_mesh_t *mesh;
1083 static buf_vertex_t *vert;
1084 static buf_fcolor_t *fcolor;
1085 static buf_texcoord_t *texcoord[MAX_TEXTUREUNITS];
1088 Sys_Error("R_Mesh_Draw: called when backend is not active\n");
1090 if (m->index == NULL
1092 || m->vertex == NULL
1094 Host_Error("R_Mesh_Draw: no triangles or verts\n");
1096 // ignore meaningless alpha meshs
1097 if (!m->depthwrite && m->blendfunc1 == GL_SRC_ALPHA && (m->blendfunc2 == GL_ONE || m->blendfunc2 == GL_ONE_MINUS_SRC_ALPHA))
1101 for (i = 0, in = m->color + 3;i < m->numverts;i++, (int)in += m->colorstep)
1104 if (i == m->numverts)
1107 else if (m->ca < 0.01f)
1113 if (currenttransmesh >= max_meshs || (currenttranstriangle + m->numtriangles) > max_meshs || (currenttransvertex + m->numverts) > max_verts)
1117 Con_Printf("R_Mesh_Draw_NativeOnly: ran out of room for transparent meshs\n");
1124 c_transtris += m->numtriangles;
1125 vert = &buf_transvertex[currenttransvertex];
1126 fcolor = &buf_transfcolor[currenttransvertex];
1127 for (i = 0;i < backendunits;i++)
1128 texcoord[i] = &buf_transtexcoord[i][currenttransvertex];
1130 // transmesh is only for storage of transparent meshs until they
1131 // are inserted into the main mesh array
1132 mesh = &buf_transmesh[currenttransmesh++];
1133 mesh->firsttriangle = currenttranstriangle;
1134 mesh->firstvert = currenttransvertex;
1135 index = &buf_transtri[currenttranstriangle].index[0];
1136 currenttranstriangle += m->numtriangles;
1137 currenttransvertex += m->numverts;
1141 if (m->numtriangles > max_meshs || m->numverts > max_verts)
1143 Con_Printf("R_Mesh_Draw_NativeOnly: mesh too big for buffers\n");
1147 if (currentmesh >= max_meshs || (currenttriangle + m->numtriangles) > max_batch || (currentvertex + m->numverts) > max_verts)
1151 c_meshtris += m->numtriangles;
1152 vert = &buf_vertex[currentvertex];
1153 fcolor = &buf_fcolor[currentvertex];
1154 for (i = 0;i < backendunits;i++)
1155 texcoord[i] = &buf_texcoord[i][currentvertex];
1157 // opaque meshs are rendered directly
1158 mesh = &buf_mesh[currentmesh++];
1159 mesh->firsttriangle = currenttriangle;
1160 mesh->firstvert = currentvertex;
1161 index = &buf_tri[currenttriangle].index[0];
1162 currenttriangle += m->numtriangles;
1163 currentvertex += m->numverts;
1166 // code shared for transparent and opaque meshs
1167 memcpy(index, m->index, sizeof(int[3]) * m->numtriangles);
1168 mesh->blendfunc1 = m->blendfunc1;
1169 mesh->blendfunc2 = m->blendfunc2;
1170 mesh->depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite);
1171 mesh->depthtest = !m->depthdisable;
1172 mesh->triangles = m->numtriangles;
1173 mesh->verts = m->numverts;
1177 if (m->blendfunc2 == GL_SRC_COLOR)
1179 if (m->blendfunc1 == GL_DST_COLOR) // 2x modulate with framebuffer
1186 overbright = gl_combine.integer;
1190 scaler *= overbrightscale;
1194 for (i = 0;i < backendunits;i++)
1196 if ((mesh->textures[i] = m->tex[i]))
1198 mesh->texturergbscale[i] = m->texrgbscale[i];
1199 if (mesh->texturergbscale[i] != 1 && mesh->texturergbscale[i] != 2 && mesh->texturergbscale[i] != 4)
1200 mesh->texturergbscale[i] = 1;
1202 if (overbright && j >= 0)
1203 mesh->texturergbscale[j] = 4;
1205 if (m->vertexstep != sizeof(buf_vertex_t))
1206 Host_Error("R_Mesh_Draw_NativeOnly: unsupported vertexstep\n");
1207 if (m->colorstep != sizeof(buf_fcolor_t))
1208 Host_Error("R_Mesh_Draw_NativeOnly: unsupported colorstep\n");
1209 if (m->color == NULL)
1210 Host_Error("R_Mesh_Draw_NativeOnly: must provide color array\n");
1211 for (j = 0;j < MAX_TEXTUREUNITS && m->tex[j];j++)
1213 if (j >= backendunits)
1214 Sys_Error("R_Mesh_Draw_NativeOnly: texture %i supplied when there are only %i texture units\n", j + 1, backendunits);
1215 if (m->texcoordstep[j] != sizeof(buf_texcoord_t))
1216 Host_Error("R_Mesh_Draw_NativeOnly: unsupported texcoordstep\n");
1219 memcpy(vert, m->vertex, m->numverts * sizeof(buf_vertex_t));
1220 for (j = 0;j < MAX_TEXTUREUNITS && m->tex[j];j++)
1221 memcpy(&texcoord[j][0].t[0], m->texcoords[j], m->numverts * sizeof(buf_texcoord_t));
1223 for (;j < backendunits;j++)
1224 memset(&texcoord[j][0].t[0], 0, m->numverts * sizeof(buf_texcoord_t));
1227 memcpy(fcolor, m->color, m->numverts * sizeof(buf_fcolor_t));
1229 // do this as a second step because memcpy preloaded the cache, which we can't easily do
1232 for (i = 0;i < m->numverts;i++)
1234 fcolor[i].c[0] *= scaler;
1235 fcolor[i].c[1] *= scaler;
1236 fcolor[i].c[2] *= scaler;
1241 // allocates space in geometry buffers, and fills in pointers to the buffers in passsed struct
1242 // (this is used for very high speed rendering, no copying)
1243 int R_Mesh_Draw_GetBuffer(rmeshbufferinfo_t *m)
1245 // these are static because gcc runs out of virtual registers otherwise
1246 int i, j, overbright;
1251 Sys_Error("R_Mesh_Draw: called when backend is not active\n");
1253 if (!m->numtriangles
1255 Host_Error("R_Mesh_Draw: no triangles or verts\n");
1259 if (currenttransmesh >= max_meshs || (currenttranstriangle + m->numtriangles) > max_meshs || (currenttransvertex + m->numverts) > max_verts)
1263 Con_Printf("R_Mesh_Draw: ran out of room for transparent meshs\n");
1270 c_transtris += m->numtriangles;
1271 m->index = &buf_transtri[currenttranstriangle].index[0];
1272 m->vertex = &buf_transvertex[currenttransvertex].v[0];
1273 m->color = &buf_transfcolor[currenttransvertex].c[0];
1274 for (i = 0;i < backendunits;i++)
1275 m->texcoords[i] = &buf_transtexcoord[i][currenttransvertex].t[0];
1277 // transmesh is only for storage of transparent meshs until they
1278 // are inserted into the main mesh array
1279 mesh = &buf_transmesh[currenttransmesh++];
1280 mesh->firsttriangle = currenttranstriangle;
1281 mesh->firstvert = currenttransvertex;
1282 currenttranstriangle += m->numtriangles;
1283 currenttransvertex += m->numverts;
1287 if (m->numtriangles > max_meshs || m->numverts > max_verts)
1289 Con_Printf("R_Mesh_Draw_GetBuffer: mesh too big for buffers\n");
1293 if (currentmesh >= max_meshs || (currenttriangle + m->numtriangles) > max_batch || (currentvertex + m->numverts) > max_verts)
1297 c_meshtris += m->numtriangles;
1298 m->index = &buf_tri[currenttriangle].index[0];
1299 m->vertex = &buf_vertex[currentvertex].v[0];
1300 m->color = &buf_fcolor[currentvertex].c[0];
1301 for (i = 0;i < backendunits;i++)
1302 m->texcoords[i] = &buf_texcoord[i][currentvertex].t[0];
1304 // opaque meshs are rendered directly
1305 mesh = &buf_mesh[currentmesh++];
1306 mesh->firsttriangle = currenttriangle;
1307 mesh->firstvert = currentvertex;
1308 currenttriangle += m->numtriangles;
1309 currentvertex += m->numverts;
1312 // code shared for transparent and opaque meshs
1313 mesh->blendfunc1 = m->blendfunc1;
1314 mesh->blendfunc2 = m->blendfunc2;
1315 mesh->depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite);
1316 mesh->depthtest = !m->depthdisable;
1317 mesh->triangles = m->numtriangles;
1318 mesh->verts = m->numverts;
1322 if (m->blendfunc2 == GL_SRC_COLOR)
1324 if (m->blendfunc1 == GL_DST_COLOR) // 2x modulate with framebuffer
1331 overbright = gl_combine.integer;
1335 scaler *= overbrightscale;
1337 m->colorscale = scaler;
1340 for (i = 0;i < MAX_TEXTUREUNITS;i++)
1342 if ((mesh->textures[i] = m->tex[i]))
1345 if (i >= backendunits)
1346 Sys_Error("R_Mesh_Draw_GetBuffer: texture %i supplied when there are only %i texture units\n", j + 1, backendunits);
1348 mesh->texturergbscale[i] = m->texrgbscale[i];
1349 if (mesh->texturergbscale[i] != 1 && mesh->texturergbscale[i] != 2 && mesh->texturergbscale[i] != 4)
1350 mesh->texturergbscale[i] = 1;
1352 if (overbright && j >= 0)
1353 mesh->texturergbscale[j] = 4;
1358 void R_Mesh_DrawPolygon(rmeshinfo_t *m, int numverts)
1360 m->index = polyindexarray;
1361 m->numverts = numverts;
1362 m->numtriangles = numverts - 2;
1363 if (m->numtriangles < 1)
1365 Con_Printf("R_Mesh_DrawPolygon: invalid vertex count\n");
1368 if (m->numtriangles >= 256)
1370 Con_Printf("R_Mesh_DrawPolygon: only up to 256 triangles (258 verts) supported\n");
1377 ==============================================================================
1381 ==============================================================================
1384 void SCR_ScreenShot(char *filename, int x, int y, int width, int height)
1389 if (!r_render.integer)
1392 buffer = Mem_Alloc(tempmempool, width*height*3);
1393 glReadPixels (x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer);
1396 // LordHavoc: compensate for v_overbrightbits when using hardware gamma
1397 if (v_hwgamma.integer)
1398 for (i = 0;i < width * height * 3;i++)
1399 buffer[i] <<= v_overbrightbits.integer;
1401 Image_WriteTGARGB_preflipped(filename, width, height, buffer);
1406 //=============================================================================
1408 void R_ClearScreen(void)
1410 if (r_render.integer)
1413 glClearColor(0,0,0,0);CHECKGLERROR
1415 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);CHECKGLERROR
1416 // set dithering mode
1417 if (gl_dither.integer)
1419 glEnable(GL_DITHER);CHECKGLERROR
1423 glDisable(GL_DITHER);CHECKGLERROR
1432 This is called every frame, and can also be called explicitly to flush
1436 void SCR_UpdateScreen (void)
1438 //Mem_CheckSentinelsGlobal();
1439 //R_TimeReport("memtest");
1443 R_TimeReport("finish");
1445 if (gl_combine.integer && !gl_combine_extension)
1446 Cvar_SetValue("gl_combine", 0);
1448 lightscalebit = v_overbrightbits.integer;
1449 if (gl_combine.integer && r_multitexture.integer)
1452 lightscale = 1.0f / (float) (1 << lightscalebit);
1453 overbrightscale = 1.0f / (float) (1 << v_overbrightbits.integer);
1455 R_TimeReport("setup");
1459 R_TimeReport("clear");
1461 if (scr_conlines < vid.conheight)
1467 // tell driver to commit it's partially full geometry queue to the rendering queue
1468 // (this doesn't wait for the commands themselves to complete)