6 static int max_verts; // always max_meshs * 3
7 #define TRANSDEPTHRES 4096
9 static cvar_t gl_mesh_maxtriangles = {0, "gl_mesh_maxtriangles", "21760"};
10 static cvar_t gl_mesh_batchtriangles = {0, "gl_mesh_batchtriangles", "1024"};
11 static cvar_t gl_mesh_merge = {0, "gl_mesh_merge", "1"};
12 static cvar_t gl_mesh_floatcolors = {0, "gl_mesh_floatcolors", "1"};
14 typedef struct buf_mesh_s
16 struct buf_mesh_s *next;
19 int blendfunc1, blendfunc2;
20 int textures[MAX_TEXTUREUNITS];
21 float texturergbscale[MAX_TEXTUREUNITS];
29 typedef struct buf_transtri_s
31 struct buf_transtri_s *next;
37 typedef struct buf_tri_s
67 static float meshfarclip;
68 static int currentmesh, currenttriangle, currentvertex, backendunits, backendactive, meshmerge, floatcolors, transranout;
69 static buf_mesh_t *buf_mesh;
70 static buf_tri_t *buf_tri;
71 static buf_vertex_t *buf_vertex;
72 static buf_fcolor_t *buf_fcolor;
73 static buf_bcolor_t *buf_bcolor;
74 static buf_texcoord_t *buf_texcoord[MAX_TEXTUREUNITS];
76 static int currenttransmesh, currenttransvertex, currenttranstriangle;
77 static buf_mesh_t *buf_transmesh;
78 static buf_transtri_t *buf_transtri;
79 static buf_transtri_t **buf_transtri_list;
80 static buf_vertex_t *buf_transvertex;
81 static buf_fcolor_t *buf_transfcolor;
82 static buf_bcolor_t *buf_transbcolor;
83 static buf_texcoord_t *buf_transtexcoord[MAX_TEXTUREUNITS];
85 static mempool_t *gl_backend_mempool;
87 static void gl_backend_start(void)
91 max_verts = max_meshs * 3;
93 gl_backend_mempool = Mem_AllocPool("GL_Backend");
95 #define BACKENDALLOC(var, count, sizeofstruct)\
97 var = Mem_Alloc(gl_backend_mempool, count * sizeof(sizeofstruct));\
99 Sys_Error("gl_backend_start: unable to allocate memory\n");\
100 memset(var, 0, count * sizeof(sizeofstruct));\
103 BACKENDALLOC(buf_mesh, max_meshs, buf_mesh_t)
104 BACKENDALLOC(buf_tri, max_meshs, buf_tri_t)
105 BACKENDALLOC(buf_vertex, max_verts, buf_vertex_t)
106 BACKENDALLOC(buf_fcolor, max_verts, buf_fcolor_t)
107 BACKENDALLOC(buf_bcolor, max_verts, buf_bcolor_t)
109 BACKENDALLOC(buf_transmesh, max_meshs, buf_mesh_t)
110 BACKENDALLOC(buf_transtri, max_meshs, buf_transtri_t)
111 BACKENDALLOC(buf_transtri_list, TRANSDEPTHRES, buf_transtri_t *)
112 BACKENDALLOC(buf_transvertex, max_verts, buf_vertex_t)
113 BACKENDALLOC(buf_transfcolor, max_verts, buf_fcolor_t)
114 BACKENDALLOC(buf_transbcolor, max_verts, buf_bcolor_t)
116 for (i = 0;i < MAX_TEXTUREUNITS;i++)
118 // only allocate as many texcoord arrays as we need
119 if (i < gl_textureunits)
121 BACKENDALLOC(buf_texcoord[i], max_verts, buf_texcoord_t)
122 BACKENDALLOC(buf_transtexcoord[i], max_verts, buf_texcoord_t)
126 buf_texcoord[i] = NULL;
127 buf_transtexcoord[i] = NULL;
130 backendunits = min(MAX_TEXTUREUNITS, gl_textureunits);
131 backendactive = true;
134 static void gl_backend_shutdown(void)
138 #define BACKENDFREE(var)\
145 #define BACKENDFREE(var) var = NULL;
147 BACKENDFREE(buf_mesh)
149 BACKENDFREE(buf_vertex)
150 BACKENDFREE(buf_fcolor)
151 BACKENDFREE(buf_bcolor)
153 BACKENDFREE(buf_transmesh)
154 BACKENDFREE(buf_transtri)
155 BACKENDFREE(buf_transtri_list)
156 BACKENDFREE(buf_transvertex)
157 BACKENDFREE(buf_transfcolor)
158 BACKENDFREE(buf_transbcolor)
160 for (i = 0;i < MAX_TEXTUREUNITS;i++)
162 BACKENDFREE(buf_texcoord[i])
163 BACKENDFREE(buf_transtexcoord[i])
166 Mem_FreePool(&gl_backend_mempool);
169 backendactive = false;
172 static void gl_backend_bufferchanges(int init)
174 // 21760 is (65536 / 3) rounded off to a multiple of 128
175 if (gl_mesh_maxtriangles.integer < 256)
176 Cvar_SetValue("gl_mesh_maxtriangles", 256);
177 if (gl_mesh_maxtriangles.integer > 21760)
178 Cvar_SetValue("gl_mesh_maxtriangles", 21760);
180 if (gl_mesh_batchtriangles.integer < 0)
181 Cvar_SetValue("gl_mesh_batchtriangles", 0);
182 if (gl_mesh_batchtriangles.integer > gl_mesh_maxtriangles.integer)
183 Cvar_SetValue("gl_mesh_batchtriangles", gl_mesh_maxtriangles.integer);
185 max_batch = gl_mesh_batchtriangles.integer;
187 if (max_meshs != gl_mesh_maxtriangles.integer)
189 max_meshs = gl_mesh_maxtriangles.integer;
193 gl_backend_shutdown();
199 float r_farclip, r_newfarclip;
201 static void gl_backend_newmap(void)
203 r_farclip = r_newfarclip = 2048.0f;
206 int polyindexarray[768];
208 void gl_backend_init(void)
211 Cvar_RegisterVariable(&gl_mesh_maxtriangles);
212 Cvar_RegisterVariable(&gl_mesh_batchtriangles);
213 Cvar_RegisterVariable(&gl_mesh_merge);
214 Cvar_RegisterVariable(&gl_mesh_floatcolors);
215 R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap);
216 gl_backend_bufferchanges(true);
217 for (i = 0;i < 256;i++)
219 polyindexarray[i*3+0] = 0;
220 polyindexarray[i*3+1] = i + 1;
221 polyindexarray[i*3+2] = i + 2;
225 static float viewdist;
229 // called at beginning of frame
230 void R_Mesh_Clear(void)
233 Sys_Error("R_Mesh_Clear: called when backend is not active\n");
235 gl_backend_bufferchanges(false);
240 currenttransmesh = 0;
241 currenttranstriangle = 0;
242 currenttransvertex = 0;
244 meshmerge = gl_mesh_merge.integer;
245 floatcolors = gl_mesh_floatcolors.integer;
247 viewdist = DotProduct(r_origin, vpn);
253 void GL_PrintError(int errornumber, char *filename, int linenumber)
257 case GL_INVALID_ENUM:
258 Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
260 case GL_INVALID_VALUE:
261 Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
263 case GL_INVALID_OPERATION:
264 Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
266 case GL_STACK_OVERFLOW:
267 Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
269 case GL_STACK_UNDERFLOW:
270 Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
272 case GL_OUT_OF_MEMORY:
273 Con_Printf("GL_OUT_OF_MEMORY at %s:%i\n", filename, linenumber);
275 case GL_TABLE_TOO_LARGE:
276 Con_Printf("GL_TABLE_TOO_LARGE at %s:%i\n", filename, linenumber);
279 Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
287 // renders mesh buffers, called to flush buffers when full
288 void R_Mesh_Render(void)
290 int i, k, blendfunc1, blendfunc2, blend, depthmask, depthtest, unit = 0, clientunit = 0, firsttriangle, triangles, firstvert, lastvert, texture[MAX_TEXTUREUNITS];
291 float farclip, texturergbscale[MAX_TEXTUREUNITS];
294 Sys_Error("R_Mesh_Render: called when backend is not active\n");
300 farclip = meshfarclip + 256.0f - viewdist; // + 256 just to be safe
302 // push out farclip for next frame
303 if (farclip > r_newfarclip)
304 r_newfarclip = ceil((farclip + 255) / 256) * 256 + 256;
306 for (i = 0;i < backendunits;i++)
307 texturergbscale[i] = 1;
309 glEnable(GL_CULL_FACE);
311 glCullFace(GL_FRONT);
314 glEnable(GL_DEPTH_TEST);
317 blendfunc2 = GL_ZERO;
318 glBlendFunc(blendfunc1, blendfunc2);
324 glDepthMask((GLboolean) depthmask);
328 glVertexPointer(3, GL_FLOAT, sizeof(buf_vertex_t), buf_vertex);
330 glEnableClientState(GL_VERTEX_ARRAY);
334 glColorPointer(4, GL_FLOAT, sizeof(buf_fcolor_t), buf_fcolor);
339 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(buf_bcolor_t), buf_bcolor);
342 glEnableClientState(GL_COLOR_ARRAY);
345 if (backendunits > 1)
347 for (i = 0;i < backendunits;i++)
349 qglActiveTexture(GL_TEXTURE0_ARB + (unit = i));
351 glBindTexture(GL_TEXTURE_2D, (texture[i] = 0));
353 glDisable(GL_TEXTURE_2D);
355 if (gl_combine.integer)
357 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
359 glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);
361 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);
363 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);
365 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_CONSTANT_ARB);
367 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
369 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);
371 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);
373 glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);
375 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);
377 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS_ARB);
379 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, GL_CONSTANT_ARB);
381 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);
383 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA);
385 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, GL_SRC_ALPHA);
387 glTexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1.0f);
389 glTexEnvf(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1.0f);
394 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
398 qglClientActiveTexture(GL_TEXTURE0_ARB + (clientunit = i));
400 glTexCoordPointer(2, GL_FLOAT, sizeof(buf_texcoord_t), buf_texcoord[i]);
402 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
408 glBindTexture(GL_TEXTURE_2D, (texture[0] = 0));
410 glDisable(GL_TEXTURE_2D);
412 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
415 glTexCoordPointer(2, GL_FLOAT, sizeof(buf_texcoord_t), buf_texcoord[0]);
417 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
421 // lock as early as possible
422 GL_LockArray(0, currentvertex);
425 for (k = 0;k < currentmesh;)
429 if (backendunits > 1)
432 for (i = 0;i < backendunits;i++)
434 if (texture[i] != mesh->textures[i])
438 qglActiveTexture(GL_TEXTURE0_ARB + (unit = i));
443 glEnable(GL_TEXTURE_2D);
445 // have to disable texcoord array on disabled texture
446 // units due to NVIDIA driver bug with
447 // compiled_vertex_array
450 qglClientActiveTexture(GL_TEXTURE0_ARB + (clientunit = i));
453 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
456 glBindTexture(GL_TEXTURE_2D, (texture[i] = mesh->textures[i]));
460 glDisable(GL_TEXTURE_2D);
462 // have to disable texcoord array on disabled texture
463 // units due to NVIDIA driver bug with
464 // compiled_vertex_array
467 qglClientActiveTexture(GL_TEXTURE0_ARB + (clientunit = i));
470 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
474 if (texturergbscale[i] != mesh->texturergbscale[i])
478 qglActiveTexture(GL_TEXTURE0_ARB + (unit = i));
481 glTexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (texturergbscale[i] = mesh->texturergbscale[i]));
487 // if (unit != topunit)
489 // qglActiveTexture(GL_TEXTURE0_ARB + (unit = topunit));
495 if (texture[0] != mesh->textures[0])
499 glEnable(GL_TEXTURE_2D);
501 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
504 glBindTexture(GL_TEXTURE_2D, (texture[0] = mesh->textures[0]));
508 glDisable(GL_TEXTURE_2D);
510 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
515 if (blendfunc1 != mesh->blendfunc1 || blendfunc2 != mesh->blendfunc2)
517 blendfunc1 = mesh->blendfunc1;
518 blendfunc2 = mesh->blendfunc2;
519 glBlendFunc(blendfunc1, blendfunc2);
521 if (blendfunc2 == GL_ZERO)
523 if (blendfunc1 == GL_ONE)
552 if (depthtest != mesh->depthtest)
554 depthtest = mesh->depthtest;
556 glEnable(GL_DEPTH_TEST);
558 glDisable(GL_DEPTH_TEST);
560 if (depthmask != mesh->depthmask)
562 depthmask = mesh->depthmask;
563 glDepthMask((GLboolean) depthmask);
567 firsttriangle = mesh->firsttriangle;
568 triangles = mesh->triangles;
569 firstvert = mesh->firstvert;
570 lastvert = mesh->lastvert;
571 mesh = &buf_mesh[++k];
575 #if MAX_TEXTUREUNITS != 4
576 #error update this code
578 while (k < currentmesh
579 && mesh->blendfunc1 == blendfunc1
580 && mesh->blendfunc2 == blendfunc2
581 && mesh->depthtest == depthtest
582 && mesh->depthmask == depthmask
583 && mesh->textures[0] == texture[0]
584 && mesh->textures[1] == texture[1]
585 && mesh->textures[2] == texture[2]
586 && mesh->textures[3] == texture[3]
587 && mesh->texturergbscale[0] == texturergbscale[0]
588 && mesh->texturergbscale[1] == texturergbscale[1]
589 && mesh->texturergbscale[2] == texturergbscale[2]
590 && mesh->texturergbscale[3] == texturergbscale[3])
592 triangles += mesh->triangles;
593 if (firstvert > mesh->firstvert)
594 firstvert = mesh->firstvert;
595 if (lastvert < mesh->lastvert)
596 lastvert = mesh->lastvert;
597 mesh = &buf_mesh[++k];
602 // FIXME: dynamic link to GL so we can get DrawRangeElements on WIN32
603 glDrawElements(GL_TRIANGLES, triangles * 3, GL_UNSIGNED_INT, (unsigned int *)&buf_tri[firsttriangle]);
605 glDrawRangeElements(GL_TRIANGLES, firstvert, lastvert + 1, triangles * 3, GL_UNSIGNED_INT, (unsigned int *)&buf_tri[firsttriangle]);
617 if (backendunits > 1)
619 for (i = backendunits - 1;i >= 0;i--)
621 qglActiveTexture(GL_TEXTURE0_ARB + (unit = i));
623 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
625 if (gl_combine.integer)
627 glTexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1.0f);
632 glDisable(GL_TEXTURE_2D);
637 glEnable(GL_TEXTURE_2D);
640 glBindTexture(GL_TEXTURE_2D, 0);
643 qglClientActiveTexture(GL_TEXTURE0_ARB + (clientunit = i));
645 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
651 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
653 glEnable(GL_TEXTURE_2D);
655 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
658 glDisableClientState(GL_COLOR_ARRAY);
660 glDisableClientState(GL_VERTEX_ARRAY);
665 glEnable(GL_DEPTH_TEST);
669 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
673 void R_Mesh_AddTransparent(void)
676 float viewdistcompare, centerscaler, dist1, dist2, dist3, center, maxdist;
677 buf_vertex_t *vert1, *vert2, *vert3;
681 // process and add transparent mesh triangles
682 if (!currenttranstriangle)
685 // map farclip to 0-4095 list range
686 centerscaler = (TRANSDEPTHRES / r_farclip) * (1.0f / 3.0f);
687 viewdistcompare = viewdist + 4.0f;
689 memset(buf_transtri_list, 0, TRANSDEPTHRES * sizeof(buf_transtri_t *));
691 // process in reverse because transtri_list adding code is in reverse as well
693 for (j = currenttranstriangle - 1;j >= 0;j--)
695 tri = &buf_transtri[j];
697 vert1 = &buf_transvertex[tri->index[0]];
698 vert2 = &buf_transvertex[tri->index[1]];
699 vert3 = &buf_transvertex[tri->index[2]];
701 dist1 = DotProduct(vert1->v, vpn);
702 dist2 = DotProduct(vert2->v, vpn);
703 dist3 = DotProduct(vert3->v, vpn);
705 maxdist = max(dist1, max(dist2, dist3));
706 if (maxdist < viewdistcompare)
709 center = (dist1 + dist2 + dist3) * centerscaler - viewdist;
712 i = bound(0, i, (TRANSDEPTHRES - 1));
716 center += 8388608.0f;
717 i = *((long *)¢er) & 0x7FFFFF;
718 i = min(i, (TRANSDEPTHRES - 1));
720 tri->next = buf_transtri_list[i];
721 buf_transtri_list[i] = tri;
725 if (currentmesh + k > max_meshs || currenttriangle + k > max_batch || currentvertex + currenttransvertex > max_verts)
728 // note: can't batch these because they can be rendered in any order
729 // there can never be more transparent triangles than fit in main buffers
730 memcpy(&buf_vertex[currentvertex], &buf_transvertex[0], currenttransvertex * sizeof(buf_vertex_t));
732 memcpy(&buf_fcolor[currentvertex], &buf_transfcolor[0], currenttransvertex * sizeof(buf_fcolor_t));
734 memcpy(&buf_fcolor[currentvertex], &buf_transbcolor[0], currenttransvertex * sizeof(buf_bcolor_t));
735 for (i = 0;i < backendunits;i++)
736 memcpy(&buf_texcoord[i][currentvertex], &buf_transtexcoord[i][0], currenttransvertex * sizeof(buf_texcoord_t));
738 for (j = TRANSDEPTHRES - 1;j >= 0;j--)
740 if ((tri = buf_transtri_list[j]))
744 mesh = &buf_mesh[currentmesh++];
745 *mesh = *tri->mesh; // copy mesh properties
746 buf_tri[currenttriangle].index[0] = tri->index[0] + currentvertex;
747 buf_tri[currenttriangle].index[1] = tri->index[1] + currentvertex;
748 buf_tri[currenttriangle].index[2] = tri->index[2] + currentvertex;
749 mesh->firstvert = min(buf_tri[currenttriangle].index[0], min(buf_tri[currenttriangle].index[1], buf_tri[currenttriangle].index[2]));
750 mesh->lastvert = max(buf_tri[currenttriangle].index[0], max(buf_tri[currenttriangle].index[1], buf_tri[currenttriangle].index[2]));
751 mesh->firsttriangle = currenttriangle++;
757 currentvertex += currenttransvertex;
758 currenttransmesh = 0;
759 currenttranstriangle = 0;
760 currenttransvertex = 0;
763 void R_Mesh_Draw(const rmeshinfo_t *m)
765 static int i, j, *index, overbright;
766 static float c, *in, scaler, cr, cg, cb, ca;
767 static buf_mesh_t *mesh;
768 static buf_vertex_t *vert;
769 static buf_fcolor_t *fcolor;
770 static buf_bcolor_t *bcolor;
771 static buf_texcoord_t *texcoord[MAX_TEXTUREUNITS];
772 static buf_transtri_t *tri;
773 static byte br, bg, bb, ba;
780 // ignore meaningless alpha meshs
781 if (!m->depthwrite && m->blendfunc1 == GL_SRC_ALPHA && (m->blendfunc2 == GL_ONE || m->blendfunc2 == GL_ONE_MINUS_SRC_ALPHA))
785 for (i = 0, in = m->color + 3;i < m->numverts;i++, (int)in += m->colorstep)
788 if (i == m->numverts)
791 else if (m->ca < 0.01f)
796 Sys_Error("R_DrawMesh: called when backend is not active\n");
800 if (currenttransmesh >= max_meshs || (currenttranstriangle + m->numtriangles) > max_meshs || (currenttransvertex + m->numverts) > max_verts)
804 Con_Printf("R_DrawMesh: ran out of room for transparent meshs\n");
810 vert = &buf_transvertex[currenttransvertex];
811 fcolor = &buf_transfcolor[currenttransvertex];
812 bcolor = &buf_transbcolor[currenttransvertex];
813 for (i = 0;i < backendunits;i++)
814 texcoord[i] = &buf_transtexcoord[i][currenttransvertex];
818 if (m->numtriangles > max_meshs || m->numverts > max_verts)
820 Con_Printf("R_DrawMesh: mesh too big for buffers\n");
824 if (currentmesh >= max_meshs || (currenttriangle + m->numtriangles) > max_batch || (currentvertex + m->numverts) > max_verts)
827 vert = &buf_vertex[currentvertex];
828 fcolor = &buf_fcolor[currentvertex];
829 bcolor = &buf_bcolor[currentvertex];
830 for (i = 0;i < backendunits;i++)
831 texcoord[i] = &buf_texcoord[i][currentvertex];
834 // vertex array code is shared for transparent and opaque meshs
836 for (i = 0, in = m->vertex;i < m->numverts;i++, (int)in += m->vertexstep)
838 vert[i].v[0] = in[0];
839 vert[i].v[1] = in[1];
840 vert[i].v[2] = in[2];
841 // push out farclip based on vertices encountered
842 c = DotProduct(vert[i].v, vpn);
848 if (m->blendfunc2 == GL_SRC_COLOR)
850 if (m->blendfunc1 == GL_DST_COLOR) // 2x modulate with framebuffer
857 overbright = gl_combine.integer;
869 for (i = 0, in = m->color;i < m->numverts;i++, (int)in += m->colorstep)
871 fcolor[i].c[0] = in[0] * scaler;
872 fcolor[i].c[1] = in[1] * scaler;
873 fcolor[i].c[2] = in[2] * scaler;
874 fcolor[i].c[3] = in[3];
883 for (i = 0;i < m->numverts;i++)
896 for (i = 0, in = m->color;i < m->numverts;i++, (int)in += m->colorstep)
898 // shift float to have 8bit fraction at base of number,
899 // then read as integer and kill float bits...
900 c = in[0] * scaler + 32768.0f;j = (*((long *)&c) & 0x7FFFFF);if (j > 255) j = 255;bcolor[i].c[0] = (byte) j;
901 c = in[1] * scaler + 32768.0f;j = (*((long *)&c) & 0x7FFFFF);if (j > 255) j = 255;bcolor[i].c[1] = (byte) j;
902 c = in[2] * scaler + 32768.0f;j = (*((long *)&c) & 0x7FFFFF);if (j > 255) j = 255;bcolor[i].c[2] = (byte) j;
903 c = in[3] + 32768.0f;j = (*((long *)&c) & 0x7FFFFF);if (j > 255) j = 255;bcolor[i].c[3] = (byte) j;
908 c = in[0] * scaler + 32768.0f;j = (*((long *)&c) & 0x7FFFFF);if (j > 255) j = 255;br = (byte) j;
909 c = in[1] * scaler + 32768.0f;j = (*((long *)&c) & 0x7FFFFF);if (j > 255) j = 255;bg = (byte) j;
910 c = in[2] * scaler + 32768.0f;j = (*((long *)&c) & 0x7FFFFF);if (j > 255) j = 255;bb = (byte) j;
911 c = in[3] + 32768.0f;j = (*((long *)&c) & 0x7FFFFF);if (j > 255) j = 255;ba = (byte) j;
912 for (i = 0;i < m->numverts;i++)
922 for (j = 0;j < MAX_TEXTUREUNITS && m->tex[j];j++)
924 if (j >= backendunits)
925 Sys_Error("R_DrawMesh: texture %i supplied when there are only %i texture units\n", j + 1, backendunits);
926 for (i = 0, in = m->texcoords[j];i < m->numverts;i++, (int)in += m->texcoordstep[j])
928 texcoord[j][i].t[0] = in[0];
929 texcoord[j][i].t[1] = in[1];
932 for (;j < backendunits;j++)
934 for (i = 0;i < m->numverts;i++)
936 texcoord[j][i].t[0] = 0;
937 texcoord[j][i].t[1] = 0;
943 // transmesh is only for storage of tranparent meshs until they
944 // are inserted into the main mesh array
945 mesh = &buf_transmesh[currenttransmesh++];
946 mesh->blendfunc1 = m->blendfunc1;
947 mesh->blendfunc2 = m->blendfunc2;
948 mesh->depthmask = false;
949 mesh->depthtest = !m->depthdisable;
951 for (i = 0;i < backendunits;i++)
953 if ((mesh->textures[i] = m->tex[i]))
955 mesh->texturergbscale[i] = m->texrgbscale[i];
956 if (mesh->texturergbscale[i] != 1 && mesh->texturergbscale[i] != 2 && mesh->texturergbscale[i] != 4)
957 mesh->texturergbscale[i] = 1;
959 if (overbright && j >= 0)
960 mesh->texturergbscale[j] = 4;
962 // transparent meshs are broken up into individual triangles which can
963 // be sorted by depth
965 for (i = 0;i < m->numtriangles;i++)
967 tri = &buf_transtri[currenttranstriangle++];
969 tri->index[0] = *index++ + currenttransvertex;
970 tri->index[1] = *index++ + currenttransvertex;
971 tri->index[2] = *index++ + currenttransvertex;
973 currenttransvertex += m->numverts;
977 mesh = &buf_mesh[currentmesh++];
978 mesh->blendfunc1 = m->blendfunc1;
979 mesh->blendfunc2 = m->blendfunc2;
980 mesh->depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite);
981 mesh->depthtest = !m->depthdisable;
982 mesh->firsttriangle = currenttriangle;
983 mesh->triangles = m->numtriangles;
985 for (i = 0;i < backendunits;i++)
987 if ((mesh->textures[i] = m->tex[i]))
989 mesh->texturergbscale[i] = m->texrgbscale[i];
990 if (mesh->texturergbscale[i] != 1 && mesh->texturergbscale[i] != 2 && mesh->texturergbscale[i] != 4)
991 mesh->texturergbscale[i] = 1;
993 if (overbright && j >= 0)
994 mesh->texturergbscale[j] = 4;
996 // opaque meshs are rendered directly
997 index = (int *)&buf_tri[currenttriangle];
998 for (i = 0;i < m->numtriangles * 3;i++)
999 index[i] = m->index[i] + currentvertex;
1000 mesh->firstvert = currentvertex;
1001 currenttriangle += m->numtriangles;
1002 currentvertex += m->numverts;
1003 mesh->lastvert = currentvertex - 1;
1006 c_meshtris += m->numtriangles;
1009 void R_Mesh_DrawPolygon(rmeshinfo_t *m, int numverts)
1011 m->index = polyindexarray;
1012 m->numverts = numverts;
1013 m->numtriangles = numverts - 2;
1014 if (m->numtriangles < 1)
1016 Con_Printf("R_Mesh_DrawPolygon: invalid vertex count\n");
1019 if (m->numtriangles >= 256)
1021 Con_Printf("R_Mesh_DrawPolygon: only up to 256 triangles (258 verts) supported\n");