4 cvar_t r_render = {0, "r_render", "1"};
5 cvar_t gl_dither = {CVAR_SAVE, "gl_dither", "1"}; // whether or not to use dithering
11 void SCR_ScreenShot_f (void);
12 static void R_Envmap_f (void);
16 static int max_verts; // always max_meshs * 3
17 #define TRANSDEPTHRES 4096
21 //static cvar_t gl_mesh_maxtriangles = {0, "gl_mesh_maxtriangles", "21760"};
22 static cvar_t gl_mesh_maxtriangles = {0, "gl_mesh_maxtriangles", "8192"};
23 static cvar_t gl_mesh_batchtriangles = {0, "gl_mesh_batchtriangles", "1024"};
24 static cvar_t gl_mesh_merge = {0, "gl_mesh_merge", "1"};
26 static cvar_t gl_mesh_floatcolors = {0, "gl_mesh_floatcolors", "1"};
28 static cvar_t gl_mesh_dupetransverts = {0, "gl_mesh_dupetransverts", "0"};
29 static cvar_t gl_mesh_sorttransbymesh = {0, "gl_mesh_sorttransbymesh", "1"};
31 typedef struct buf_mesh_s
33 //struct buf_mesh_s *next;
36 int blendfunc1, blendfunc2;
37 int textures[MAX_TEXTUREUNITS];
38 float texturergbscale[MAX_TEXTUREUNITS];
43 struct buf_mesh_s *chain;
44 struct buf_transtri_s *transchain;
45 //struct buf_transtri_s **transchainpointer;
49 typedef struct buf_transtri_s
51 struct buf_transtri_s *next;
52 struct buf_transtri_s *meshsortchain;
58 typedef struct buf_tri_s
90 static float meshfarclip;
91 static int currentmesh, currenttriangle, currentvertex, backendunits, backendactive, meshmerge, transranout;
93 static int floatcolors;
95 static buf_mesh_t *buf_mesh;
96 static buf_tri_t *buf_tri;
97 static buf_vertex_t *buf_vertex;
99 static buf_fcolor_t *buf_fcolor;
101 static buf_bcolor_t *buf_bcolor;
102 static buf_texcoord_t *buf_texcoord[MAX_TEXTUREUNITS];
104 static int currenttransmesh, currenttransvertex, currenttranstriangle;
105 static buf_mesh_t *buf_transmesh;
106 static buf_transtri_t *buf_transtri;
107 static buf_transtri_t **buf_transtri_list;
108 static buf_vertex_t *buf_transvertex;
110 static buf_fcolor_t *buf_transfcolor;
112 static buf_bcolor_t *buf_transbcolor;
113 static buf_texcoord_t *buf_transtexcoord[MAX_TEXTUREUNITS];
115 static mempool_t *gl_backend_mempool;
116 static int resizingbuffers = false;
118 static void gl_backend_start(void)
122 max_verts = max_meshs * 3;
124 if (!gl_backend_mempool)
125 gl_backend_mempool = Mem_AllocPool("GL_Backend");
127 #define BACKENDALLOC(var, count, sizeofstruct)\
129 var = Mem_Alloc(gl_backend_mempool, count * sizeof(sizeofstruct));\
131 Sys_Error("gl_backend_start: unable to allocate memory\n");\
132 memset(var, 0, count * sizeof(sizeofstruct));\
135 BACKENDALLOC(buf_mesh, max_meshs, buf_mesh_t)
136 BACKENDALLOC(buf_tri, max_meshs, buf_tri_t)
137 BACKENDALLOC(buf_vertex, max_verts, buf_vertex_t)
139 BACKENDALLOC(buf_fcolor, max_verts, buf_fcolor_t)
141 BACKENDALLOC(buf_bcolor, max_verts, buf_bcolor_t)
143 BACKENDALLOC(buf_transmesh, max_meshs, buf_mesh_t)
144 BACKENDALLOC(buf_transtri, max_meshs, buf_transtri_t)
145 BACKENDALLOC(buf_transtri_list, TRANSDEPTHRES, buf_transtri_t *)
146 BACKENDALLOC(buf_transvertex, max_verts, buf_vertex_t)
148 BACKENDALLOC(buf_transfcolor, max_verts, buf_fcolor_t)
150 BACKENDALLOC(buf_transbcolor, max_verts, buf_bcolor_t)
152 for (i = 0;i < MAX_TEXTUREUNITS;i++)
154 // only allocate as many texcoord arrays as we need
155 if (i < gl_textureunits)
157 BACKENDALLOC(buf_texcoord[i], max_verts, buf_texcoord_t)
158 BACKENDALLOC(buf_transtexcoord[i], max_verts, buf_texcoord_t)
162 buf_texcoord[i] = NULL;
163 buf_transtexcoord[i] = NULL;
166 backendunits = min(MAX_TEXTUREUNITS, gl_textureunits);
167 backendactive = true;
170 static void gl_backend_shutdown(void)
174 #define BACKENDFREE(var)\
182 #define BACKENDFREE(var) var = NULL;
184 BACKENDFREE(buf_mesh)
186 BACKENDFREE(buf_vertex)
188 BACKENDFREE(buf_fcolor)
190 BACKENDFREE(buf_bcolor)
192 BACKENDFREE(buf_transmesh)
193 BACKENDFREE(buf_transtri)
194 BACKENDFREE(buf_transtri_list)
195 BACKENDFREE(buf_transvertex)
197 BACKENDFREE(buf_transfcolor)
199 BACKENDFREE(buf_transbcolor)
201 for (i = 0;i < MAX_TEXTUREUNITS;i++)
203 BACKENDFREE(buf_texcoord[i])
204 BACKENDFREE(buf_transtexcoord[i])
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 float r_farclip, r_newfarclip;
248 static void gl_backend_newmap(void)
250 r_farclip = r_newfarclip = 2048.0f;
253 int polyindexarray[768];
255 void gl_backend_init(void)
259 Cvar_RegisterVariable (&r_render);
260 Cvar_RegisterVariable (&gl_dither);
262 Cvar_SetValue("r_render", 0);
265 Cmd_AddCommand ("screenshot",SCR_ScreenShot_f);
266 Cmd_AddCommand ("envmap", R_Envmap_f);
268 Cvar_RegisterVariable(&gl_mesh_maxtriangles);
269 Cvar_RegisterVariable(&gl_mesh_batchtriangles);
270 Cvar_RegisterVariable(&gl_mesh_merge);
272 Cvar_RegisterVariable(&gl_mesh_floatcolors);
274 Cvar_RegisterVariable(&gl_mesh_dupetransverts);
275 Cvar_RegisterVariable(&gl_mesh_sorttransbymesh);
276 R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap);
277 gl_backend_bufferchanges(true);
278 for (i = 0;i < 256;i++)
280 polyindexarray[i*3+0] = 0;
281 polyindexarray[i*3+1] = i + 1;
282 polyindexarray[i*3+2] = i + 2;
286 static void MYgluPerspective(GLdouble fovx, GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar )
290 xmax = zNear * tan( fovx * M_PI / 360.0 ) * aspect;
291 ymax = zNear * tan( fovy * M_PI / 360.0 );
293 glFrustum(-xmax, xmax, -ymax, ymax, zNear, zFar );
302 static void GL_SetupFrame (void)
304 if (!r_render.integer)
307 // glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // LordHavoc: moved to SCR_UpdateScreen
310 glDepthFunc (GL_LEQUAL);
312 glDepthRange (gldepthmin, gldepthmax);
314 // update farclip based on previous frame
315 r_farclip = r_newfarclip;
318 glMatrixMode(GL_PROJECTION);
321 // y is weird beause OpenGL is bottom to top, we use top to bottom
322 glViewport(r_refdef.x, vid.realheight - (r_refdef.y + r_refdef.height), r_refdef.width, r_refdef.height);
323 // yfov = 2*atan((float)r_refdef.height/r_refdef.width)*180/M_PI;
324 MYgluPerspective (r_refdef.fov_x, r_refdef.fov_y, r_refdef.width/r_refdef.height, 4, r_farclip);
326 glCullFace(GL_FRONT);
328 glMatrixMode(GL_MODELVIEW);
331 glRotatef (-90, 1, 0, 0); // put Z going up
332 glRotatef (90, 0, 0, 1); // put Z going up
333 glRotatef (-r_refdef.viewangles[2], 1, 0, 0);
334 glRotatef (-r_refdef.viewangles[0], 0, 1, 0);
335 glRotatef (-r_refdef.viewangles[1], 0, 0, 1);
336 glTranslatef (-r_refdef.vieworg[0], -r_refdef.vieworg[1], -r_refdef.vieworg[2]);
338 // glGetFloatv (GL_MODELVIEW_MATRIX, r_world_matrix);
343 // if (gl_cull.integer)
344 glEnable(GL_CULL_FACE);
346 // glDisable(GL_CULL_FACE);
348 glEnable(GL_BLEND); // was Disable
349 glEnable(GL_DEPTH_TEST);
353 static float viewdist;
355 int c_meshs, c_meshtris, c_transmeshs, c_transtris;
357 // called at beginning of frame
358 void R_Mesh_Clear(void)
361 Sys_Error("R_Mesh_Clear: called when backend is not active\n");
363 gl_backend_bufferchanges(false);
368 currenttransmesh = 0;
369 currenttranstriangle = 0;
370 currenttransvertex = 0;
372 meshmerge = gl_mesh_merge.integer;
374 floatcolors = gl_mesh_floatcolors.integer;
377 viewdist = DotProduct(r_origin, vpn);
388 void GL_PrintError(int errornumber, char *filename, int linenumber)
392 case GL_INVALID_ENUM:
393 Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
395 case GL_INVALID_VALUE:
396 Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
398 case GL_INVALID_OPERATION:
399 Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
401 case GL_STACK_OVERFLOW:
402 Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
404 case GL_STACK_UNDERFLOW:
405 Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
407 case GL_OUT_OF_MEMORY:
408 Con_Printf("GL_OUT_OF_MEMORY at %s:%i\n", filename, linenumber);
410 #ifdef GL_TABLE_TOO_LARGE
411 case GL_TABLE_TOO_LARGE:
412 Con_Printf("GL_TABLE_TOO_LARGE at %s:%i\n", filename, linenumber);
416 Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
424 // renders mesh buffers, called to flush buffers when full
425 void R_Mesh_Render(void)
427 int i, k, blendfunc1, blendfunc2, blend, depthmask, depthtest, unit = 0, clientunit = 0, firsttriangle, triangles, firstvert, lastvert, texture[MAX_TEXTUREUNITS];
428 float farclip, texturergbscale[MAX_TEXTUREUNITS];
431 Sys_Error("R_Mesh_Render: called when backend is not active\n");
437 // push out farclip based on vertices
438 for (i = 0;i < currentvertex;i++)
440 farclip = DotProduct(buf_vertex[i].v, vpn);
441 if (meshfarclip < farclip)
442 meshfarclip = farclip;
445 farclip = meshfarclip + 256.0f - viewdist; // + 256 just to be safe
447 // push out farclip for next frame
448 if (farclip > r_newfarclip)
449 r_newfarclip = ceil((farclip + 255) / 256) * 256 + 256;
451 for (i = 0;i < backendunits;i++)
452 texturergbscale[i] = 1;
454 glEnable(GL_CULL_FACE);
456 glCullFace(GL_FRONT);
459 glEnable(GL_DEPTH_TEST);
462 blendfunc2 = GL_ZERO;
463 glBlendFunc(blendfunc1, blendfunc2);
469 glDepthMask((GLboolean) depthmask);
472 glVertexPointer(3, GL_FLOAT, sizeof(buf_vertex_t), buf_vertex);
474 glEnableClientState(GL_VERTEX_ARRAY);
479 glColorPointer(4, GL_FLOAT, sizeof(buf_fcolor_t), buf_fcolor);
485 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(buf_bcolor_t), buf_bcolor);
490 glEnableClientState(GL_COLOR_ARRAY);
493 if (backendunits > 1)
495 for (i = 0;i < backendunits;i++)
497 qglActiveTexture(GL_TEXTURE0_ARB + (unit = i));
499 glBindTexture(GL_TEXTURE_2D, (texture[i] = 0));
501 glDisable(GL_TEXTURE_2D);
503 if (gl_combine.integer)
505 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
507 glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);
509 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);
511 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);
513 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_CONSTANT_ARB);
515 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
517 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);
519 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);
521 glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);
523 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);
525 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS_ARB);
527 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, GL_CONSTANT_ARB);
529 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);
531 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA);
533 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, GL_SRC_ALPHA);
535 glTexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1.0f);
537 glTexEnvf(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1.0f);
542 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
546 qglClientActiveTexture(GL_TEXTURE0_ARB + (clientunit = i));
548 glTexCoordPointer(2, GL_FLOAT, sizeof(buf_texcoord_t), buf_texcoord[i]);
550 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
556 glBindTexture(GL_TEXTURE_2D, (texture[0] = 0));
558 glDisable(GL_TEXTURE_2D);
560 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
563 glTexCoordPointer(2, GL_FLOAT, sizeof(buf_texcoord_t), buf_texcoord[0]);
565 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
569 // lock as early as possible
570 GL_LockArray(0, currentvertex);
573 for (k = 0;k < currentmesh;)
577 if (backendunits > 1)
580 for (i = 0;i < backendunits;i++)
582 if (texture[i] != mesh->textures[i])
586 qglActiveTexture(GL_TEXTURE0_ARB + (unit = i));
591 glEnable(GL_TEXTURE_2D);
593 // have to disable texcoord array on disabled texture
594 // units due to NVIDIA driver bug with
595 // compiled_vertex_array
598 qglClientActiveTexture(GL_TEXTURE0_ARB + (clientunit = i));
601 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
604 glBindTexture(GL_TEXTURE_2D, (texture[i] = mesh->textures[i]));
608 glDisable(GL_TEXTURE_2D);
610 // have to disable texcoord array on disabled texture
611 // units due to NVIDIA driver bug with
612 // compiled_vertex_array
615 qglClientActiveTexture(GL_TEXTURE0_ARB + (clientunit = i));
618 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
622 if (texturergbscale[i] != mesh->texturergbscale[i])
626 qglActiveTexture(GL_TEXTURE0_ARB + (unit = i));
629 glTexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (texturergbscale[i] = mesh->texturergbscale[i]));
635 // if (unit != topunit)
637 // qglActiveTexture(GL_TEXTURE0_ARB + (unit = topunit));
643 if (texture[0] != mesh->textures[0])
647 glEnable(GL_TEXTURE_2D);
649 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
652 glBindTexture(GL_TEXTURE_2D, (texture[0] = mesh->textures[0]));
656 glDisable(GL_TEXTURE_2D);
658 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
663 if (blendfunc1 != mesh->blendfunc1 || blendfunc2 != mesh->blendfunc2)
665 blendfunc1 = mesh->blendfunc1;
666 blendfunc2 = mesh->blendfunc2;
667 glBlendFunc(blendfunc1, blendfunc2);
669 if (blendfunc2 == GL_ZERO)
671 if (blendfunc1 == GL_ONE)
700 if (depthtest != mesh->depthtest)
702 depthtest = mesh->depthtest;
704 glEnable(GL_DEPTH_TEST);
706 glDisable(GL_DEPTH_TEST);
708 if (depthmask != mesh->depthmask)
710 depthmask = mesh->depthmask;
711 glDepthMask((GLboolean) depthmask);
715 firsttriangle = mesh->firsttriangle;
716 triangles = mesh->triangles;
717 firstvert = mesh->firstvert;
718 lastvert = mesh->lastvert;
719 mesh = &buf_mesh[++k];
723 #if MAX_TEXTUREUNITS != 4
724 #error update this code
726 while (k < currentmesh
727 && mesh->blendfunc1 == blendfunc1
728 && mesh->blendfunc2 == blendfunc2
729 && mesh->depthtest == depthtest
730 && mesh->depthmask == depthmask
731 && mesh->textures[0] == texture[0]
732 && mesh->textures[1] == texture[1]
733 && mesh->textures[2] == texture[2]
734 && mesh->textures[3] == texture[3]
735 && mesh->texturergbscale[0] == texturergbscale[0]
736 && mesh->texturergbscale[1] == texturergbscale[1]
737 && mesh->texturergbscale[2] == texturergbscale[2]
738 && mesh->texturergbscale[3] == texturergbscale[3])
740 triangles += mesh->triangles;
741 if (firstvert > mesh->firstvert)
742 firstvert = mesh->firstvert;
743 if (lastvert < mesh->lastvert)
744 lastvert = mesh->lastvert;
745 mesh = &buf_mesh[++k];
750 // FIXME: dynamic link to GL so we can get DrawRangeElements on WIN32
751 glDrawElements(GL_TRIANGLES, triangles * 3, GL_UNSIGNED_INT, (unsigned int *)&buf_tri[firsttriangle]);
753 glDrawRangeElements(GL_TRIANGLES, firstvert, lastvert + 1, triangles * 3, GL_UNSIGNED_INT, (unsigned int *)&buf_tri[firsttriangle]);
765 if (backendunits > 1)
767 for (i = backendunits - 1;i >= 0;i--)
769 qglActiveTexture(GL_TEXTURE0_ARB + (unit = i));
771 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
773 if (gl_combine.integer)
775 glTexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1.0f);
780 glDisable(GL_TEXTURE_2D);
785 glEnable(GL_TEXTURE_2D);
788 glBindTexture(GL_TEXTURE_2D, 0);
791 qglClientActiveTexture(GL_TEXTURE0_ARB + (clientunit = i));
793 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
799 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
801 glEnable(GL_TEXTURE_2D);
803 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
806 glDisableClientState(GL_COLOR_ARRAY);
808 glDisableClientState(GL_VERTEX_ARRAY);
813 glEnable(GL_DEPTH_TEST);
817 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
821 void R_Mesh_AddTransparent(void)
823 if (gl_mesh_sorttransbymesh.integer)
826 float viewdistcompare, centerscaler, dist1, dist2, dist3, center, maxdist;
827 buf_vertex_t *vert1, *vert2, *vert3;
829 buf_mesh_t *mesh, *transmesh;
831 // process and add transparent mesh triangles
832 if (!currenttranstriangle)
835 // map farclip to 0-4095 list range
836 centerscaler = (TRANSDEPTHRES / r_farclip) * (1.0f / 3.0f);
837 viewdistcompare = viewdist + 4.0f;
839 memset(buf_transtri_list, 0, TRANSDEPTHRES * sizeof(buf_transtri_t *));
842 for (j = 0;j < currenttranstriangle;j++)
844 tri = &buf_transtri[j];
846 vert1 = &buf_transvertex[tri->index[0]];
847 vert2 = &buf_transvertex[tri->index[1]];
848 vert3 = &buf_transvertex[tri->index[2]];
850 dist1 = DotProduct(vert1->v, vpn);
851 dist2 = DotProduct(vert2->v, vpn);
852 dist3 = DotProduct(vert3->v, vpn);
854 maxdist = max(dist1, max(dist2, dist3));
855 if (maxdist < viewdistcompare)
858 center = (dist1 + dist2 + dist3) * centerscaler - viewdist;
861 i = bound(0, i, (TRANSDEPTHRES - 1));
865 center += 8388608.0f;
866 i = *((long *)¢er) & 0x7FFFFF;
867 i = min(i, (TRANSDEPTHRES - 1));
869 tri->next = buf_transtri_list[i];
870 buf_transtri_list[i] = tri;
875 if (currentmesh + k > max_meshs || currenttriangle + k > max_batch || currentvertex + currenttransvertex > max_verts)
878 // note: can't batch these because they can be rendered in any order
879 // there can never be more transparent triangles than fit in main buffers
880 memcpy(&buf_vertex[currentvertex], &buf_transvertex[0], currenttransvertex * sizeof(buf_vertex_t));
883 memcpy(&buf_fcolor[currentvertex], &buf_transfcolor[0], currenttransvertex * sizeof(buf_fcolor_t));
886 memcpy(&buf_bcolor[currentvertex], &buf_transbcolor[0], currenttransvertex * sizeof(buf_bcolor_t));
887 for (i = 0;i < backendunits;i++)
888 memcpy(&buf_texcoord[i][currentvertex], &buf_transtexcoord[i][0], currenttransvertex * sizeof(buf_texcoord_t));
891 for (i = 0;i < currenttransmesh;i++)
893 buf_transmesh[i].transchain = NULL;
894 //buf_transmesh[i].transchainpointer = &buf_transmesh[i].transchain;
895 buf_transmesh[i].triangles = 0;
898 for (j = 0;j < TRANSDEPTHRES;j++)
900 if ((tri = buf_transtri_list[j]))
902 for (;tri;tri = tri->next)
904 if (!tri->mesh->transchain)
906 tri->mesh->chain = transmesh;
907 transmesh = tri->mesh;
909 tri->meshsortchain = tri->mesh->transchain;
910 tri->mesh->transchain = tri;
912 *tri->mesh->transchainpointer = tri;
913 tri->meshsortchain = NULL;
914 tri->mesh->transchainpointer = &tri->meshsortchain;
916 tri->mesh->triangles++;
922 for (;transmesh;transmesh = transmesh->chain)
924 int meshvertexadjust;
925 int numverts = transmesh->lastvert - transmesh->firstvert + 1;
926 if (currentmesh >= max_meshs || currenttriangle + transmesh->triangles > max_batch || currentvertex + numverts > max_verts)
929 memcpy(&buf_vertex[currentvertex], &buf_transvertex[transmesh->firstvert], numverts * sizeof(buf_vertex_t));
932 memcpy(&buf_fcolor[currentvertex], &buf_transfcolor[transmesh->firstvert], numverts * sizeof(buf_fcolor_t));
935 memcpy(&buf_bcolor[currentvertex], &buf_transbcolor[transmesh->firstvert], numverts * sizeof(buf_bcolor_t));
936 for (i = 0;i < backendunits && transmesh->textures[i];i++)
937 memcpy(&buf_texcoord[i][currentvertex], &buf_transtexcoord[i][transmesh->firstvert], numverts * sizeof(buf_texcoord_t));
939 mesh = &buf_mesh[currentmesh++];
940 *mesh = *transmesh; // copy mesh properties
941 mesh->firstvert = currentvertex;
942 mesh->lastvert = currentvertex + numverts - 1;
943 currentvertex += numverts;
944 meshvertexadjust = mesh->firstvert - transmesh->firstvert;
945 mesh->firsttriangle = currenttriangle;
946 for (tri = transmesh->transchain;tri;tri = tri->meshsortchain)
948 buf_tri[currenttriangle].index[0] = tri->index[0] + meshvertexadjust;
949 buf_tri[currenttriangle].index[1] = tri->index[1] + meshvertexadjust;
950 buf_tri[currenttriangle].index[2] = tri->index[2] + meshvertexadjust;
952 if (tri->mesh != transmesh)
954 if ((unsigned int) buf_tri[currenttriangle].index[0] < (unsigned int) mesh->firstvert
955 || (unsigned int) buf_tri[currenttriangle].index[0] > (unsigned int) mesh->lastvert
956 || (unsigned int) buf_tri[currenttriangle].index[1] < (unsigned int) mesh->firstvert
957 || (unsigned int) buf_tri[currenttriangle].index[1] > (unsigned int) mesh->lastvert
958 || (unsigned int) buf_tri[currenttriangle].index[2] < (unsigned int) mesh->firstvert
959 || (unsigned int) buf_tri[currenttriangle].index[2] > (unsigned int) mesh->lastvert)
965 if (mesh->triangles != currenttriangle - mesh->firsttriangle)
970 for (;transmesh;transmesh = transmesh->chain)
972 mesh = &buf_mesh[currentmesh++];
973 *mesh = *transmesh; // copy mesh properties
974 mesh->firstvert += currentvertex;
975 mesh->lastvert += currentvertex;
976 mesh->firsttriangle = currenttriangle;
977 for (tri = transmesh->transchain;tri;tri = tri->meshsortchain)
979 buf_tri[currenttriangle].index[0] = tri->index[0] + currentvertex;
980 buf_tri[currenttriangle].index[1] = tri->index[1] + currentvertex;
981 buf_tri[currenttriangle].index[2] = tri->index[2] + currentvertex;
983 if (tri->mesh != transmesh)
985 if ((unsigned int) buf_tri[currenttriangle].index[0] < (unsigned int) mesh->firstvert
986 || (unsigned int) buf_tri[currenttriangle].index[0] > (unsigned int) mesh->lastvert
987 || (unsigned int) buf_tri[currenttriangle].index[1] < (unsigned int) mesh->firstvert
988 || (unsigned int) buf_tri[currenttriangle].index[1] > (unsigned int) mesh->lastvert
989 || (unsigned int) buf_tri[currenttriangle].index[2] < (unsigned int) mesh->firstvert
990 || (unsigned int) buf_tri[currenttriangle].index[2] > (unsigned int) mesh->lastvert)
996 if (mesh->triangles != currenttriangle - mesh->firsttriangle)
1000 currentvertex += currenttransvertex;
1003 currenttransmesh = 0;
1004 currenttranstriangle = 0;
1005 currenttransvertex = 0;
1007 else if (gl_mesh_dupetransverts.integer)
1010 float viewdistcompare, centerscaler, dist1, dist2, dist3, center, maxdist;
1011 buf_vertex_t *vert1, *vert2, *vert3;
1012 buf_transtri_t *tri;
1015 // process and add transparent mesh triangles
1016 if (!currenttranstriangle)
1019 // map farclip to 0-4095 list range
1020 centerscaler = (TRANSDEPTHRES / r_farclip) * (1.0f / 3.0f);
1021 viewdistcompare = viewdist + 4.0f;
1023 memset(buf_transtri_list, 0, TRANSDEPTHRES * sizeof(buf_transtri_t *));
1025 // process in reverse because transtri_list adding code is in reverse as well
1027 for (j = currenttranstriangle - 1;j >= 0;j--)
1029 tri = &buf_transtri[j];
1031 vert1 = &buf_transvertex[tri->index[0]];
1032 vert2 = &buf_transvertex[tri->index[1]];
1033 vert3 = &buf_transvertex[tri->index[2]];
1035 dist1 = DotProduct(vert1->v, vpn);
1036 dist2 = DotProduct(vert2->v, vpn);
1037 dist3 = DotProduct(vert3->v, vpn);
1039 maxdist = max(dist1, max(dist2, dist3));
1040 if (maxdist < viewdistcompare)
1043 center = (dist1 + dist2 + dist3) * centerscaler - viewdist;
1046 i = bound(0, i, (TRANSDEPTHRES - 1));
1050 center += 8388608.0f;
1051 i = *((long *)¢er) & 0x7FFFFF;
1052 i = min(i, (TRANSDEPTHRES - 1));
1054 tri->next = buf_transtri_list[i];
1055 buf_transtri_list[i] = tri;
1059 if (currentmesh + k > max_meshs || currenttriangle + k > max_batch || currentvertex + k * 3 > max_verts)
1062 currenttransmesh = 0;
1063 currenttranstriangle = 0;
1064 currenttransvertex = 0;
1066 // note: can't batch these because they can be rendered in any order
1067 // there can never be more transparent triangles than fit in main buffers
1068 for (j = TRANSDEPTHRES - 1;j >= 0;j--)
1070 if ((tri = buf_transtri_list[j]))
1074 mesh = &buf_mesh[currentmesh++];
1075 *mesh = *tri->mesh; // copy mesh properties
1076 mesh->firstvert = currentvertex;
1077 mesh->lastvert = currentvertex + 2;
1078 mesh->firsttriangle = currenttriangle;
1079 mesh->triangles = 1;
1080 for (k = 0;k < 3;k++)
1082 index = tri->index[k];
1083 buf_tri[currenttriangle].index[k] = currentvertex;
1084 memcpy(buf_vertex[currentvertex].v, buf_transvertex[index].v, sizeof(buf_vertex_t));
1087 memcpy(buf_fcolor[currentvertex].c, buf_transfcolor[index].c, sizeof(buf_fcolor_t));
1090 memcpy(buf_bcolor[currentvertex].c, buf_transbcolor[index].c, sizeof(buf_bcolor_t));
1091 for (i = 0;i < backendunits && tri->mesh->textures[i];i++)
1092 memcpy(buf_texcoord[i][currentvertex].t, buf_transtexcoord[i][index].t, sizeof(buf_texcoord_t));
1104 float viewdistcompare, centerscaler, dist1, dist2, dist3, center, maxdist;
1105 buf_vertex_t *vert1, *vert2, *vert3;
1106 buf_transtri_t *tri;
1109 // process and add transparent mesh triangles
1110 if (!currenttranstriangle)
1113 // map farclip to 0-4095 list range
1114 centerscaler = (TRANSDEPTHRES / r_farclip) * (1.0f / 3.0f);
1115 viewdistcompare = viewdist + 4.0f;
1117 memset(buf_transtri_list, 0, TRANSDEPTHRES * sizeof(buf_transtri_t *));
1119 // process in reverse because transtri_list adding code is in reverse as well
1121 for (j = currenttranstriangle - 1;j >= 0;j--)
1123 tri = &buf_transtri[j];
1125 vert1 = &buf_transvertex[tri->index[0]];
1126 vert2 = &buf_transvertex[tri->index[1]];
1127 vert3 = &buf_transvertex[tri->index[2]];
1129 dist1 = DotProduct(vert1->v, vpn);
1130 dist2 = DotProduct(vert2->v, vpn);
1131 dist3 = DotProduct(vert3->v, vpn);
1133 maxdist = max(dist1, max(dist2, dist3));
1134 if (maxdist < viewdistcompare)
1137 center = (dist1 + dist2 + dist3) * centerscaler - viewdist;
1140 i = bound(0, i, (TRANSDEPTHRES - 1));
1144 center += 8388608.0f;
1145 i = *((long *)¢er) & 0x7FFFFF;
1146 i = min(i, (TRANSDEPTHRES - 1));
1148 tri->next = buf_transtri_list[i];
1149 buf_transtri_list[i] = tri;
1153 if (currentmesh + k > max_meshs || currenttriangle + k > max_batch || currentvertex + currenttransvertex > max_verts)
1156 // note: can't batch these because they can be rendered in any order
1157 // there can never be more transparent triangles than fit in main buffers
1158 memcpy(&buf_vertex[currentvertex], &buf_transvertex[0], currenttransvertex * sizeof(buf_vertex_t));
1161 memcpy(&buf_fcolor[currentvertex], &buf_transfcolor[0], currenttransvertex * sizeof(buf_fcolor_t));
1164 memcpy(&buf_bcolor[currentvertex], &buf_transbcolor[0], currenttransvertex * sizeof(buf_bcolor_t));
1165 for (i = 0;i < backendunits;i++)
1166 memcpy(&buf_texcoord[i][currentvertex], &buf_transtexcoord[i][0], currenttransvertex * sizeof(buf_texcoord_t));
1168 for (j = TRANSDEPTHRES - 1;j >= 0;j--)
1170 if ((tri = buf_transtri_list[j]))
1174 mesh = &buf_mesh[currentmesh++];
1175 *mesh = *tri->mesh; // copy mesh properties
1176 buf_tri[currenttriangle].index[0] = tri->index[0] + currentvertex;
1177 buf_tri[currenttriangle].index[1] = tri->index[1] + currentvertex;
1178 buf_tri[currenttriangle].index[2] = tri->index[2] + currentvertex;
1179 mesh->firstvert = min(buf_tri[currenttriangle].index[0], min(buf_tri[currenttriangle].index[1], buf_tri[currenttriangle].index[2]));
1180 mesh->lastvert = max(buf_tri[currenttriangle].index[0], max(buf_tri[currenttriangle].index[1], buf_tri[currenttriangle].index[2]));
1181 mesh->firsttriangle = currenttriangle++;
1182 mesh->triangles = 1;
1187 currentvertex += currenttransvertex;
1188 currenttransmesh = 0;
1189 currenttranstriangle = 0;
1190 currenttransvertex = 0;
1194 void R_Mesh_Draw(const rmeshinfo_t *m)
1196 // these are static because gcc runs out of virtual registers otherwise
1197 static int i, j, *index, overbright;
1198 static float c, *in, scaler;
1200 static float cr, cg, cb, ca;
1202 static buf_mesh_t *mesh;
1203 static buf_vertex_t *vert;
1205 static buf_fcolor_t *fcolor;
1207 static buf_bcolor_t *bcolor;
1208 static buf_texcoord_t *texcoord[MAX_TEXTUREUNITS];
1209 static buf_transtri_t *tri;
1210 static buf_bcolor_t flatbcolor;
1212 if (m->index == NULL
1214 || m->vertex == NULL
1217 // ignore meaningless alpha meshs
1218 if (!m->depthwrite && m->blendfunc1 == GL_SRC_ALPHA && (m->blendfunc2 == GL_ONE || m->blendfunc2 == GL_ONE_MINUS_SRC_ALPHA))
1222 for (i = 0, in = m->color + 3;i < m->numverts;i++, (int)in += m->colorstep)
1225 if (i == m->numverts)
1228 else if (m->ca < 0.01f)
1233 Sys_Error("R_Mesh_Draw: called when backend is not active\n");
1236 if (m->blendfunc2 == GL_SRC_COLOR)
1238 if (m->blendfunc1 == GL_DST_COLOR) // 2x modulate with framebuffer
1245 overbright = gl_combine.integer;
1249 scaler *= overbrightscale;
1254 if (currenttransmesh >= max_meshs || (currenttranstriangle + m->numtriangles) > max_meshs || (currenttransvertex + m->numverts) > max_verts)
1258 Con_Printf("R_Mesh_Draw: ran out of room for transparent meshs\n");
1265 c_transtris += m->numtriangles;
1266 vert = &buf_transvertex[currenttransvertex];
1268 fcolor = &buf_transfcolor[currenttransvertex];
1270 bcolor = &buf_transbcolor[currenttransvertex];
1271 for (i = 0;i < backendunits;i++)
1272 texcoord[i] = &buf_transtexcoord[i][currenttransvertex];
1274 // transmesh is only for storage of transparent meshs until they
1275 // are inserted into the main mesh array
1276 mesh = &buf_transmesh[currenttransmesh++];
1278 // transparent meshs are broken up into individual triangles which can
1279 // be sorted by depth
1281 for (i = 0;i < m->numtriangles;i++)
1283 tri = &buf_transtri[currenttranstriangle++];
1285 tri->index[0] = *index++ + currenttransvertex;
1286 tri->index[1] = *index++ + currenttransvertex;
1287 tri->index[2] = *index++ + currenttransvertex;
1290 mesh->firstvert = currenttransvertex;
1291 mesh->lastvert = currenttransvertex + m->numverts - 1;
1292 currenttransvertex += m->numverts;
1296 if (m->numtriangles > max_meshs || m->numverts > max_verts)
1298 Con_Printf("R_Mesh_Draw: mesh too big for buffers\n");
1302 if (currentmesh >= max_meshs || (currenttriangle + m->numtriangles) > max_batch || (currentvertex + m->numverts) > max_verts)
1306 c_meshtris += m->numtriangles;
1307 vert = &buf_vertex[currentvertex];
1309 fcolor = &buf_fcolor[currentvertex];
1311 bcolor = &buf_bcolor[currentvertex];
1312 for (i = 0;i < backendunits;i++)
1313 texcoord[i] = &buf_texcoord[i][currentvertex];
1315 mesh = &buf_mesh[currentmesh++];
1316 // opaque meshs are rendered directly
1317 index = (int *)&buf_tri[currenttriangle];
1318 mesh->firsttriangle = currenttriangle;
1319 currenttriangle += m->numtriangles;
1320 for (i = 0;i < m->numtriangles * 3;i++)
1321 index[i] = m->index[i] + currentvertex;
1323 mesh->firstvert = currentvertex;
1324 mesh->lastvert = currentvertex + m->numverts - 1;
1325 currentvertex += m->numverts;
1328 // code shared for transparent and opaque meshs
1329 mesh->blendfunc1 = m->blendfunc1;
1330 mesh->blendfunc2 = m->blendfunc2;
1331 mesh->depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite);
1332 mesh->depthtest = !m->depthdisable;
1333 mesh->triangles = m->numtriangles;
1335 for (i = 0;i < backendunits;i++)
1337 if ((mesh->textures[i] = m->tex[i]))
1339 mesh->texturergbscale[i] = m->texrgbscale[i];
1340 if (mesh->texturergbscale[i] != 1 && mesh->texturergbscale[i] != 2 && mesh->texturergbscale[i] != 4)
1341 mesh->texturergbscale[i] = 1;
1343 if (overbright && j >= 0)
1344 mesh->texturergbscale[j] = 4;
1346 if (m->vertexstep != sizeof(buf_vertex_t))
1348 for (i = 0, in = m->vertex;i < m->numverts;i++, (int)in += m->vertexstep)
1350 vert[i].v[0] = in[0];
1351 vert[i].v[1] = in[1];
1352 vert[i].v[2] = in[2];
1356 memcpy(vert, m->vertex, m->numverts * sizeof(buf_vertex_t));
1363 for (i = 0, in = m->color;i < m->numverts;i++, (int)in += m->colorstep)
1365 fcolor[i].c[0] = in[0] * scaler;
1366 fcolor[i].c[1] = in[1] * scaler;
1367 fcolor[i].c[2] = in[2] * scaler;
1368 fcolor[i].c[3] = in[3];
1373 cr = m->cr * scaler;
1374 cg = m->cg * scaler;
1375 cb = m->cb * scaler;
1377 for (i = 0;i < m->numverts;i++)
1379 fcolor[i].c[0] = cr;
1380 fcolor[i].c[1] = cg;
1381 fcolor[i].c[2] = cb;
1382 fcolor[i].c[3] = ca;
1391 for (i = 0, in = m->color;i < m->numverts;i++, (int)in += m->colorstep)
1393 // shift float to have 8bit fraction at base of number,
1394 // then read as integer and kill float bits...
1395 c = in[0] * scaler + 32768.0f;j = (*((long *)&c) & 0x7FFFFF);if (j > 255) j = 255;bcolor[i].c[0] = (byte) j;
1396 c = in[1] * scaler + 32768.0f;j = (*((long *)&c) & 0x7FFFFF);if (j > 255) j = 255;bcolor[i].c[1] = (byte) j;
1397 c = in[2] * scaler + 32768.0f;j = (*((long *)&c) & 0x7FFFFF);if (j > 255) j = 255;bcolor[i].c[2] = (byte) j;
1398 c = in[3] + 32768.0f;j = (*((long *)&c) & 0x7FFFFF);if (j > 255) j = 255;bcolor[i].c[3] = (byte) j;
1403 c = m->cr * scaler + 32768.0f;j = (*((long *)&c) & 0x7FFFFF);if (j > 255) j = 255;flatbcolor.c[0] = (byte) j;
1404 c = m->cg * scaler + 32768.0f;j = (*((long *)&c) & 0x7FFFFF);if (j > 255) j = 255;flatbcolor.c[1] = (byte) j;
1405 c = m->cb * scaler + 32768.0f;j = (*((long *)&c) & 0x7FFFFF);if (j > 255) j = 255;flatbcolor.c[2] = (byte) j;
1406 c = m->ca + 32768.0f;j = (*((long *)&c) & 0x7FFFFF);if (j > 255) j = 255;flatbcolor.c[3] = (byte) j;
1407 for (i = 0;i < m->numverts;i++)
1408 bcolor[i] = flatbcolor;
1414 for (j = 0;j < MAX_TEXTUREUNITS && m->tex[j];j++)
1416 if (j >= backendunits)
1417 Sys_Error("R_Mesh_Draw: texture %i supplied when there are only %i texture units\n", j + 1, backendunits);
1418 if (m->texcoordstep[j] != sizeof(buf_texcoord_t))
1420 for (i = 0, in = m->texcoords[j];i < m->numverts;i++, (int)in += m->texcoordstep[j])
1422 texcoord[j][i].t[0] = in[0];
1423 texcoord[j][i].t[1] = in[1];
1427 memcpy(&texcoord[j][0].t[0], m->texcoords[j], m->numverts * sizeof(buf_texcoord_t));
1430 for (;j < backendunits;j++)
1431 memset(&texcoord[j][0].t[0], 0, m->numverts * sizeof(buf_texcoord_t));
1435 void R_Mesh_DrawPolygon(rmeshinfo_t *m, int numverts)
1437 m->index = polyindexarray;
1438 m->numverts = numverts;
1439 m->numtriangles = numverts - 2;
1440 if (m->numtriangles < 1)
1442 Con_Printf("R_Mesh_DrawPolygon: invalid vertex count\n");
1445 if (m->numtriangles >= 256)
1447 Con_Printf("R_Mesh_DrawPolygon: only up to 256 triangles (258 verts) supported\n");
1453 // LordHavoc: this thing is evil, but necessary because decals account for so much overhead
1454 void R_Mesh_DrawDecal(const rmeshinfo_t *m)
1456 // these are static because gcc runs out of virtual registers otherwise
1457 static int i, j, *index, overbright;
1458 static float c, scaler;
1460 static float cr, cg, cb, ca;
1462 static buf_mesh_t *mesh;
1463 static buf_vertex_t *vert;
1465 static buf_fcolor_t *fcolor;
1467 static buf_bcolor_t *bcolor;
1468 static buf_texcoord_t *texcoord;
1469 static buf_transtri_t *tri;
1470 static buf_bcolor_t flatbcolor;
1473 Sys_Error("R_Mesh_Draw: called when backend is not active\n");
1478 overbright = gl_combine.integer;
1482 scaler *= overbrightscale;
1486 if (currenttransmesh >= max_meshs || (currenttranstriangle + 2) > max_meshs || (currenttransvertex + 4) > max_verts)
1490 Con_Printf("R_Mesh_Draw: ran out of room for transparent meshs\n");
1498 vert = &buf_transvertex[currenttransvertex];
1500 fcolor = &buf_transfcolor[currenttransvertex];
1502 bcolor = &buf_transbcolor[currenttransvertex];
1503 texcoord = &buf_transtexcoord[0][currenttransvertex];
1505 // transmesh is only for storage of transparent meshs until they
1506 // are inserted into the main mesh array
1507 mesh = &buf_transmesh[currenttransmesh++];
1508 mesh->blendfunc1 = m->blendfunc1;
1509 mesh->blendfunc2 = m->blendfunc2;
1510 mesh->depthmask = false;
1511 mesh->depthtest = true;
1512 mesh->triangles = 2;
1513 mesh->textures[0] = m->tex[0];
1514 mesh->texturergbscale[0] = overbright ? 4 : 1;
1515 for (i = 1;i < backendunits;i++)
1517 mesh->textures[i] = 0;
1518 mesh->texturergbscale[i] = 1;
1522 // transparent meshs are broken up into individual triangles which can
1523 // be sorted by depth
1525 tri = &buf_transtri[currenttranstriangle++];
1527 tri->index[0] = 0 + currenttransvertex;
1528 tri->index[1] = 1 + currenttransvertex;
1529 tri->index[2] = 2 + currenttransvertex;
1530 tri = &buf_transtri[currenttranstriangle++];
1532 tri->index[0] = 0 + currenttransvertex;
1533 tri->index[1] = 2 + currenttransvertex;
1534 tri->index[2] = 3 + currenttransvertex;
1536 mesh->firstvert = currenttransvertex;
1537 mesh->lastvert = currenttransvertex + 3;
1538 currenttransvertex += 4;
1542 if (2 > max_meshs || 4 > max_verts)
1544 Con_Printf("R_Mesh_Draw: mesh too big for buffers\n");
1548 if (currentmesh >= max_meshs || (currenttriangle + 2) > max_batch || (currentvertex + 4) > max_verts)
1553 vert = &buf_vertex[currentvertex];
1555 fcolor = &buf_fcolor[currentvertex];
1557 bcolor = &buf_bcolor[currentvertex];
1558 texcoord = &buf_texcoord[0][currentvertex];
1560 mesh = &buf_mesh[currentmesh++];
1561 mesh->blendfunc1 = m->blendfunc1;
1562 mesh->blendfunc2 = m->blendfunc2;
1563 mesh->depthmask = false;
1564 mesh->depthtest = !m->depthdisable;
1565 mesh->firsttriangle = currenttriangle;
1566 mesh->triangles = 2;
1567 mesh->textures[0] = m->tex[0];
1568 mesh->texturergbscale[0] = overbright ? 4 : 1;
1569 for (i = 1;i < backendunits;i++)
1571 mesh->textures[i] = 0;
1572 mesh->texturergbscale[i] = 1;
1575 // opaque meshs are rendered directly
1576 index = (int *)&buf_tri[currenttriangle];
1577 index[0] = 0 + currentvertex;
1578 index[1] = 1 + currentvertex;
1579 index[2] = 2 + currentvertex;
1580 index[3] = 0 + currentvertex;
1581 index[4] = 2 + currentvertex;
1582 index[5] = 3 + currentvertex;
1583 mesh->firstvert = currentvertex;
1584 mesh->lastvert = currentvertex + 3;
1585 currenttriangle += 2;
1589 // buf_vertex_t must match the size of the decal vertex array (or vice versa)
1590 memcpy(vert, m->vertex, 4 * sizeof(buf_vertex_t));
1595 cr = m->cr * scaler;
1596 cg = m->cg * scaler;
1597 cb = m->cb * scaler;
1599 fcolor[0].c[0] = cr;
1600 fcolor[0].c[1] = cg;
1601 fcolor[0].c[2] = cb;
1602 fcolor[0].c[3] = ca;
1603 fcolor[1].c[0] = cr;
1604 fcolor[1].c[1] = cg;
1605 fcolor[1].c[2] = cb;
1606 fcolor[1].c[3] = ca;
1607 fcolor[2].c[0] = cr;
1608 fcolor[2].c[1] = cg;
1609 fcolor[2].c[2] = cb;
1610 fcolor[2].c[3] = ca;
1611 fcolor[3].c[0] = cr;
1612 fcolor[3].c[1] = cg;
1613 fcolor[3].c[2] = cb;
1614 fcolor[3].c[3] = ca;
1619 c = m->cr * scaler + 32768.0f;j = (*((long *)&c) & 0x7FFFFF);if (j > 255) j = 255;flatbcolor.c[0] = (byte) j;
1620 c = m->cg * scaler + 32768.0f;j = (*((long *)&c) & 0x7FFFFF);if (j > 255) j = 255;flatbcolor.c[1] = (byte) j;
1621 c = m->cb * scaler + 32768.0f;j = (*((long *)&c) & 0x7FFFFF);if (j > 255) j = 255;flatbcolor.c[2] = (byte) j;
1622 c = m->ca + 32768.0f;j = (*((long *)&c) & 0x7FFFFF);if (j > 255) j = 255;flatbcolor.c[3] = (byte) j;
1623 bcolor[0] = flatbcolor;
1624 bcolor[1] = flatbcolor;
1625 bcolor[2] = flatbcolor;
1626 bcolor[3] = flatbcolor;
1631 // buf_texcoord_t must be the same size as the decal texcoord array (or vice versa)
1632 memcpy(&texcoord[0].t[0], m->texcoords[0], 4 * sizeof(buf_texcoord_t));
1636 ==============================================================================
1640 ==============================================================================
1643 float CalcFov (float fov_x, float width, float height);
1644 void R_ClearScreen(void);
1646 void SCR_ScreenShot(char *filename, int x, int y, int width, int height)
1651 buffer = Mem_Alloc(tempmempool, width*height*3);
1652 glReadPixels (x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer);
1655 // LordHavoc: compensate for v_overbrightbits when using hardware gamma
1656 if (v_hwgamma.integer)
1657 for (i = 0;i < width * height * 3;i++)
1658 buffer[i] <<= v_overbrightbits.integer;
1660 Image_WriteTGARGB_preflipped(filename, width, height, buffer);
1670 void SCR_ScreenShot_f (void)
1674 char checkname[MAX_OSPATH];
1676 // find a file name to save it to
1678 strcpy(filename, "dp0000.tga");
1680 for (i=0 ; i<=9999 ; i++)
1682 filename[2] = (i/1000)%10 + '0';
1683 filename[3] = (i/ 100)%10 + '0';
1684 filename[4] = (i/ 10)%10 + '0';
1685 filename[5] = (i/ 1)%10 + '0';
1686 sprintf (checkname, "%s/%s", com_gamedir, filename);
1687 if (Sys_FileTime(checkname) == -1)
1688 break; // file doesn't exist
1692 Con_Printf ("SCR_ScreenShot_f: Couldn't create a TGA file\n");
1696 SCR_ScreenShot(filename, vid.realx, vid.realy, vid.realwidth, vid.realheight);
1697 Con_Printf ("Wrote %s\n", filename);
1704 Grab six views for environment mapping tests
1715 {{ 0, 90, 0}, "rt"},
1716 {{ 0, 180, 0}, "bk"},
1717 {{ 0, 270, 0}, "lf"},
1718 {{-90, 90, 0}, "up"},
1719 {{ 90, 90, 0}, "dn"}
1721 static void R_Envmap_f (void)
1724 char filename[256], basename[256];
1726 if (Cmd_Argc() != 3)
1728 Con_Printf ("envmap <basename> <size>: save out 6 cubic environment map images, usable with loadsky, note that size must one of 128, 256, 512, or 1024 and can't be bigger than your current resolution\n");
1732 if (!r_render.integer)
1735 strcpy(basename, Cmd_Argv(1));
1736 size = atoi(Cmd_Argv(2));
1737 if (size != 128 && size != 256 && size != 512 && size != 1024)
1739 Con_Printf("envmap: size must be one of 128, 256, 512, or 1024\n");
1742 if (size > vid.realwidth || size > vid.realheight)
1744 Con_Printf("envmap: your resolution is not big enough to render that size\n");
1752 r_refdef.width = size;
1753 r_refdef.height = size;
1755 r_refdef.fov_x = 90;
1756 r_refdef.fov_y = 90;
1758 for (j = 0;j < 6;j++)
1760 sprintf(filename, "env/%s%s.tga", basename, envmapinfo[j].name);
1761 VectorCopy(envmapinfo[j].angles, r_refdef.viewangles);
1764 SCR_ScreenShot(filename, vid.realx, vid.realy, size, size);
1770 //=============================================================================
1772 void R_ClearScreen(void)
1774 if (r_render.integer)
1776 glClearColor(0,0,0,0);
1778 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // LordHavoc: clear the screen (around the view as well)
1780 if (gl_dither.integer)
1781 glEnable(GL_DITHER);
1783 glDisable(GL_DITHER);
1792 This is called every frame, and can also be called explicitly to flush
1796 void SCR_UpdateScreen (void)
1798 //Mem_CheckSentinelsGlobal();
1799 //R_TimeReport("memtest");
1806 R_TimeReport("finish");
1808 if (gl_combine.integer && !gl_combine_extension)
1809 Cvar_SetValue("gl_combine", 0);
1811 lightscalebit = v_overbrightbits.integer;
1812 if (gl_combine.integer && r_multitexture.integer)
1815 lightscale = 1.0f / (float) (1 << lightscalebit);
1816 overbrightscale = 1.0f / (float) (1 << v_overbrightbits.integer);
1818 R_TimeReport("setup");
1822 R_TimeReport("clear");
1824 if (scr_conlines < vid.conheight)