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
19 //static cvar_t gl_mesh_maxtriangles = {0, "gl_mesh_maxtriangles", "21760"};
20 static cvar_t gl_mesh_maxtriangles = {0, "gl_mesh_maxtriangles", "8192"};
21 static cvar_t gl_mesh_batchtriangles = {0, "gl_mesh_batchtriangles", "1024"};
22 static cvar_t gl_mesh_merge = {0, "gl_mesh_merge", "1"};
23 static cvar_t gl_mesh_floatcolors = {0, "gl_mesh_floatcolors", "0"};
25 typedef struct buf_mesh_s
29 int blendfunc1, blendfunc2;
30 int textures[MAX_TEXTUREUNITS];
31 float texturergbscale[MAX_TEXTUREUNITS];
36 struct buf_mesh_s *chain;
37 struct buf_transtri_s *transchain;
41 typedef struct buf_transtri_s
43 struct buf_transtri_s *next;
44 struct buf_transtri_s *meshsortchain;
50 typedef struct buf_tri_s
80 static float meshfarclip;
81 static int currentmesh, currenttriangle, currentvertex, backendunits, backendactive, transranout;
82 static buf_mesh_t *buf_mesh;
83 static buf_tri_t *buf_tri;
84 static buf_vertex_t *buf_vertex;
85 static buf_fcolor_t *buf_fcolor;
86 static buf_bcolor_t *buf_bcolor;
87 static buf_texcoord_t *buf_texcoord[MAX_TEXTUREUNITS];
89 static int currenttransmesh, currenttransvertex, currenttranstriangle;
90 static buf_mesh_t *buf_transmesh;
91 static buf_transtri_t *buf_sorttranstri;
92 static buf_transtri_t **buf_sorttranstri_list;
93 static buf_tri_t *buf_transtri;
94 static buf_vertex_t *buf_transvertex;
95 static buf_fcolor_t *buf_transfcolor;
96 static buf_texcoord_t *buf_transtexcoord[MAX_TEXTUREUNITS];
98 static mempool_t *gl_backend_mempool;
99 static int resizingbuffers = false;
101 static void gl_backend_start(void)
105 max_verts = max_meshs * 3;
107 if (!gl_backend_mempool)
108 gl_backend_mempool = Mem_AllocPool("GL_Backend");
110 #define BACKENDALLOC(var, count, sizeofstruct)\
112 var = Mem_Alloc(gl_backend_mempool, count * sizeof(sizeofstruct));\
114 Sys_Error("gl_backend_start: unable to allocate memory\n");\
115 memset(var, 0, count * sizeof(sizeofstruct));\
118 BACKENDALLOC(buf_mesh, max_meshs, buf_mesh_t)
119 BACKENDALLOC(buf_tri, max_meshs, buf_tri_t)
120 BACKENDALLOC(buf_vertex, max_verts, buf_vertex_t)
121 BACKENDALLOC(buf_fcolor, max_verts, buf_fcolor_t)
122 BACKENDALLOC(buf_bcolor, max_verts, buf_bcolor_t)
124 BACKENDALLOC(buf_transmesh, max_meshs, buf_mesh_t)
125 BACKENDALLOC(buf_sorttranstri, max_meshs, buf_transtri_t)
126 BACKENDALLOC(buf_sorttranstri_list, TRANSDEPTHRES, buf_transtri_t *)
127 BACKENDALLOC(buf_transtri, max_meshs, buf_tri_t)
128 BACKENDALLOC(buf_transvertex, max_verts, buf_vertex_t)
129 BACKENDALLOC(buf_transfcolor, max_verts, buf_fcolor_t)
131 for (i = 0;i < MAX_TEXTUREUNITS;i++)
133 // only allocate as many texcoord arrays as we need
134 if (i < gl_textureunits)
136 BACKENDALLOC(buf_texcoord[i], max_verts, buf_texcoord_t)
137 BACKENDALLOC(buf_transtexcoord[i], max_verts, buf_texcoord_t)
141 buf_texcoord[i] = NULL;
142 buf_transtexcoord[i] = NULL;
145 backendunits = min(MAX_TEXTUREUNITS, gl_textureunits);
146 backendactive = true;
149 static void gl_backend_shutdown(void)
152 Mem_EmptyPool(gl_backend_mempool);
154 Mem_FreePool(&gl_backend_mempool);
157 backendactive = false;
160 static void gl_backend_bufferchanges(int init)
162 // 21760 is (65536 / 3) rounded off to a multiple of 128
163 if (gl_mesh_maxtriangles.integer < 256)
164 Cvar_SetValue("gl_mesh_maxtriangles", 256);
165 if (gl_mesh_maxtriangles.integer > 21760)
166 Cvar_SetValue("gl_mesh_maxtriangles", 21760);
168 if (gl_mesh_batchtriangles.integer < 0)
169 Cvar_SetValue("gl_mesh_batchtriangles", 0);
170 if (gl_mesh_batchtriangles.integer > gl_mesh_maxtriangles.integer)
171 Cvar_SetValue("gl_mesh_batchtriangles", gl_mesh_maxtriangles.integer);
173 max_batch = gl_mesh_batchtriangles.integer;
175 if (max_meshs != gl_mesh_maxtriangles.integer)
177 max_meshs = gl_mesh_maxtriangles.integer;
181 resizingbuffers = true;
182 gl_backend_shutdown();
184 resizingbuffers = false;
189 float r_farclip, r_newfarclip;
191 static void gl_backend_newmap(void)
193 r_farclip = r_newfarclip = 2048.0f;
196 int polyindexarray[768];
198 void gl_backend_init(void)
202 Cvar_RegisterVariable (&r_render);
203 Cvar_RegisterVariable (&gl_dither);
205 Cvar_SetValue("r_render", 0);
208 Cmd_AddCommand ("screenshot",SCR_ScreenShot_f);
209 Cmd_AddCommand ("envmap", R_Envmap_f);
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 void MYgluPerspective(GLdouble fovx, GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar )
229 xmax = zNear * tan( fovx * M_PI / 360.0 ) * aspect;
230 ymax = zNear * tan( fovy * M_PI / 360.0 );
232 glFrustum(-xmax, xmax, -ymax, ymax, zNear, zFar );
241 static void GL_SetupFrame (void)
243 if (!r_render.integer)
246 // glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // LordHavoc: moved to SCR_UpdateScreen
249 glDepthFunc (GL_LEQUAL);
251 glDepthRange (gldepthmin, gldepthmax);
253 // update farclip based on previous frame
254 r_farclip = r_newfarclip;
257 glMatrixMode(GL_PROJECTION);
260 // y is weird beause OpenGL is bottom to top, we use top to bottom
261 glViewport(r_refdef.x, vid.realheight - (r_refdef.y + r_refdef.height), r_refdef.width, r_refdef.height);
262 // yfov = 2*atan((float)r_refdef.height/r_refdef.width)*180/M_PI;
263 MYgluPerspective (r_refdef.fov_x, r_refdef.fov_y, r_refdef.width/r_refdef.height, 4.0 / 3.0, r_farclip);
265 glCullFace(GL_FRONT);
267 glMatrixMode(GL_MODELVIEW);
270 glRotatef (-90, 1, 0, 0); // put Z going up
271 glRotatef (90, 0, 0, 1); // put Z going up
272 glRotatef (-r_refdef.viewangles[2], 1, 0, 0);
273 glRotatef (-r_refdef.viewangles[0], 0, 1, 0);
274 glRotatef (-r_refdef.viewangles[1], 0, 0, 1);
275 glTranslatef (-r_refdef.vieworg[0], -r_refdef.vieworg[1], -r_refdef.vieworg[2]);
277 // glGetFloatv (GL_MODELVIEW_MATRIX, r_world_matrix);
282 // if (gl_cull.integer)
283 glEnable(GL_CULL_FACE);
285 // glDisable(GL_CULL_FACE);
287 glEnable(GL_BLEND); // was Disable
288 glEnable(GL_DEPTH_TEST);
292 static float viewdist;
294 int c_meshs, c_meshtris, c_transmeshs, c_transtris;
296 // called at beginning of frame
297 void R_Mesh_Clear(void)
300 Sys_Error("R_Mesh_Clear: called when backend is not active\n");
302 gl_backend_bufferchanges(false);
307 currenttransmesh = 0;
308 currenttranstriangle = 0;
309 currenttransvertex = 0;
312 viewdist = DotProduct(r_origin, vpn);
323 void GL_PrintError(int errornumber, char *filename, int linenumber)
327 case GL_INVALID_ENUM:
328 Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
330 case GL_INVALID_VALUE:
331 Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
333 case GL_INVALID_OPERATION:
334 Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
336 case GL_STACK_OVERFLOW:
337 Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
339 case GL_STACK_UNDERFLOW:
340 Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
342 case GL_OUT_OF_MEMORY:
343 Con_Printf("GL_OUT_OF_MEMORY at %s:%i\n", filename, linenumber);
345 #ifdef GL_TABLE_TOO_LARGE
346 case GL_TABLE_TOO_LARGE:
347 Con_Printf("GL_TABLE_TOO_LARGE at %s:%i\n", filename, linenumber);
351 Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
359 // renders mesh buffers, called to flush buffers when full
360 void R_Mesh_Render(void)
362 int i, k, blendfunc1, blendfunc2, blend, depthmask, depthtest, unit = 0, clientunit = 0, firsttriangle, endtriangle, indexcount, firstvert, endvert, texture[MAX_TEXTUREUNITS];
363 float farclip, texturergbscale[MAX_TEXTUREUNITS];
366 // float to byte color conversion
371 Sys_Error("R_Mesh_Render: called when backend is not active\n");
377 // push out farclip based on vertices
378 for (i = 0;i < currentvertex;i++)
380 farclip = DotProduct(buf_vertex[i].v, vpn);
381 if (meshfarclip < farclip)
382 meshfarclip = farclip;
385 farclip = meshfarclip + 256.0f - viewdist; // + 256 just to be safe
387 // push out farclip for next frame
388 if (farclip > r_newfarclip)
389 r_newfarclip = ceil((farclip + 255) / 256) * 256 + 256;
391 for (i = 0;i < backendunits;i++)
392 texturergbscale[i] = 1;
394 glEnable(GL_CULL_FACE);
396 glCullFace(GL_FRONT);
399 glEnable(GL_DEPTH_TEST);
402 blendfunc2 = GL_ZERO;
403 glBlendFunc(blendfunc1, blendfunc2);
409 glDepthMask((GLboolean) depthmask);
412 glVertexPointer(3, GL_FLOAT, sizeof(buf_vertex_t), &buf_vertex[0].v[0]);
414 glEnableClientState(GL_VERTEX_ARRAY);
416 if (gl_mesh_floatcolors.integer)
418 glColorPointer(4, GL_FLOAT, sizeof(buf_fcolor_t), &buf_fcolor[0].c[0]);
423 // shift float to have 8bit fraction at base of number
424 for (i = 0, fcolor = &buf_fcolor->c[0];i < currentvertex;i++)
426 *fcolor++ += 32768.0f;
427 *fcolor++ += 32768.0f;
428 *fcolor++ += 32768.0f;
429 *fcolor++ += 32768.0f;
431 // then read as integer and kill float bits...
432 for (i = 0, icolor = (int *)&buf_fcolor->c[0], bcolor = &buf_bcolor->c[0];i < currentvertex;i++)
434 k = (*icolor++) & 0x7FFFFF;*bcolor++ = k > 255 ? 255 : k;
435 k = (*icolor++) & 0x7FFFFF;*bcolor++ = k > 255 ? 255 : k;
436 k = (*icolor++) & 0x7FFFFF;*bcolor++ = k > 255 ? 255 : k;
437 k = (*icolor++) & 0x7FFFFF;*bcolor++ = k > 255 ? 255 : k;
439 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(buf_bcolor_t), &buf_bcolor[0].c[0]);
442 glEnableClientState(GL_COLOR_ARRAY);
445 if (backendunits > 1)
447 for (i = 0;i < backendunits;i++)
449 qglActiveTexture(GL_TEXTURE0_ARB + (unit = i));
451 glBindTexture(GL_TEXTURE_2D, (texture[i] = 0));
453 glDisable(GL_TEXTURE_2D);
455 if (gl_combine.integer)
457 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);
459 glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);
461 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);
463 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);
465 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_CONSTANT_ARB);
467 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
469 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);
471 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);
473 glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);
475 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);
477 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS_ARB);
479 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, GL_CONSTANT_ARB);
481 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);
483 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA);
485 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, GL_SRC_ALPHA);
487 glTexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1.0f);
489 glTexEnvf(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1.0f);
494 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
498 qglClientActiveTexture(GL_TEXTURE0_ARB + (clientunit = i));
500 glTexCoordPointer(2, GL_FLOAT, sizeof(buf_texcoord_t), buf_texcoord[i]);
502 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
508 glBindTexture(GL_TEXTURE_2D, (texture[0] = 0));
510 glDisable(GL_TEXTURE_2D);
512 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
515 glTexCoordPointer(2, GL_FLOAT, sizeof(buf_texcoord_t), buf_texcoord[0]);
517 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
521 // lock as early as possible
522 GL_LockArray(0, currentvertex);
525 for (k = 0;k < currentmesh;)
529 if (backendunits > 1)
532 for (i = 0;i < backendunits;i++)
534 if (texture[i] != mesh->textures[i])
538 qglActiveTexture(GL_TEXTURE0_ARB + (unit = i));
543 glEnable(GL_TEXTURE_2D);
545 // have to disable texcoord array on disabled texture
546 // units due to NVIDIA driver bug with
547 // compiled_vertex_array
550 qglClientActiveTexture(GL_TEXTURE0_ARB + (clientunit = i));
553 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
556 glBindTexture(GL_TEXTURE_2D, (texture[i] = mesh->textures[i]));
560 glDisable(GL_TEXTURE_2D);
562 // have to disable texcoord array on disabled texture
563 // units due to NVIDIA driver bug with
564 // compiled_vertex_array
567 qglClientActiveTexture(GL_TEXTURE0_ARB + (clientunit = i));
570 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
574 if (texturergbscale[i] != mesh->texturergbscale[i])
578 qglActiveTexture(GL_TEXTURE0_ARB + (unit = i));
581 glTexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (texturergbscale[i] = mesh->texturergbscale[i]));
587 // if (unit != topunit)
589 // qglActiveTexture(GL_TEXTURE0_ARB + (unit = topunit));
595 if (texture[0] != mesh->textures[0])
599 glEnable(GL_TEXTURE_2D);
601 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
604 glBindTexture(GL_TEXTURE_2D, (texture[0] = mesh->textures[0]));
608 glDisable(GL_TEXTURE_2D);
610 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
615 if (blendfunc1 != mesh->blendfunc1 || blendfunc2 != mesh->blendfunc2)
617 blendfunc1 = mesh->blendfunc1;
618 blendfunc2 = mesh->blendfunc2;
619 glBlendFunc(blendfunc1, blendfunc2);
621 if (blendfunc2 == GL_ZERO)
623 if (blendfunc1 == GL_ONE)
652 if (depthtest != mesh->depthtest)
654 depthtest = mesh->depthtest;
656 glEnable(GL_DEPTH_TEST);
658 glDisable(GL_DEPTH_TEST);
660 if (depthmask != mesh->depthmask)
662 depthmask = mesh->depthmask;
663 glDepthMask((GLboolean) depthmask);
667 firsttriangle = mesh->firsttriangle;
668 firstvert = mesh->firstvert;
669 endtriangle = firsttriangle + mesh->triangles;
670 endvert = firstvert + mesh->verts;
672 mesh = &buf_mesh[++k];
673 if (gl_mesh_merge.integer)
675 #if MAX_TEXTUREUNITS != 4
676 #error update this code
678 while (k < currentmesh
679 && mesh->firsttriangle == endtriangle
680 && mesh->firstvert == endvert
681 && mesh->depthmask == depthmask
682 && mesh->depthtest == depthtest
683 && mesh->blendfunc1 == blendfunc1
684 && mesh->blendfunc2 == blendfunc2
685 && mesh->textures[0] == texture[0]
686 && mesh->textures[1] == texture[1]
687 && mesh->textures[2] == texture[2]
688 && mesh->textures[3] == texture[3]
689 && mesh->texturergbscale[0] == texturergbscale[0]
690 && mesh->texturergbscale[1] == texturergbscale[1]
691 && mesh->texturergbscale[2] == texturergbscale[2]
692 && mesh->texturergbscale[3] == texturergbscale[3])
694 endtriangle += mesh->triangles;
695 endvert += mesh->verts;
696 mesh = &buf_mesh[++k];
700 indexcount = (endtriangle - firsttriangle) * 3;
701 index = (unsigned int *)&buf_tri[firsttriangle].index[0];
702 for (i = 0;i < indexcount;i++)
703 index[i] += firstvert;
706 // FIXME: dynamic link to GL so we can get DrawRangeElements on WIN32
707 glDrawElements(GL_TRIANGLES, indexcount, GL_UNSIGNED_INT, index);
709 glDrawRangeElements(GL_TRIANGLES, firstvert, endvert, indexcount, GL_UNSIGNED_INT, index);
721 if (backendunits > 1)
723 for (i = backendunits - 1;i >= 0;i--)
725 qglActiveTexture(GL_TEXTURE0_ARB + (unit = i));
727 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
729 if (gl_combine.integer)
731 glTexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1.0f);
736 glDisable(GL_TEXTURE_2D);
741 glEnable(GL_TEXTURE_2D);
744 glBindTexture(GL_TEXTURE_2D, 0);
747 qglClientActiveTexture(GL_TEXTURE0_ARB + (clientunit = i));
749 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
755 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
757 glEnable(GL_TEXTURE_2D);
759 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
762 glDisableClientState(GL_COLOR_ARRAY);
764 glDisableClientState(GL_VERTEX_ARRAY);
769 glEnable(GL_DEPTH_TEST);
773 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
777 void R_Mesh_AddTransparent(void)
780 float viewdistcompare, centerscaler, dist1, dist2, dist3, center, maxdist;
781 buf_vertex_t *vert1, *vert2, *vert3;
783 buf_mesh_t *mesh, *transmesh;
785 if (!currenttransmesh)
788 // convert index data to transtris for sorting
789 for (j = 0;j < currenttransmesh;j++)
791 mesh = buf_transmesh + j;
792 k = mesh->firsttriangle;
793 index = &buf_transtri[k].index[0];
794 for (i = 0;i < mesh->triangles;i++)
796 tri = &buf_sorttranstri[k++];
798 tri->index[0] = *index++;
799 tri->index[1] = *index++;
800 tri->index[2] = *index++;
804 // map farclip to 0-4095 list range
805 centerscaler = (TRANSDEPTHRES / r_farclip) * (1.0f / 3.0f);
806 viewdistcompare = viewdist + 4.0f;
808 memset(buf_sorttranstri_list, 0, TRANSDEPTHRES * sizeof(buf_transtri_t *));
811 for (j = 0;j < currenttranstriangle;j++)
813 tri = &buf_sorttranstri[j];
814 i = tri->mesh->firstvert;
816 vert1 = &buf_transvertex[tri->index[0] + i];
817 vert2 = &buf_transvertex[tri->index[1] + i];
818 vert3 = &buf_transvertex[tri->index[2] + i];
820 dist1 = DotProduct(vert1->v, vpn);
821 dist2 = DotProduct(vert2->v, vpn);
822 dist3 = DotProduct(vert3->v, vpn);
824 maxdist = max(dist1, max(dist2, dist3));
825 if (maxdist < viewdistcompare)
828 center = (dist1 + dist2 + dist3) * centerscaler - viewdist;
831 i = bound(0, i, (TRANSDEPTHRES - 1));
835 center += 8388608.0f;
836 i = *((long *)¢er) & 0x7FFFFF;
837 i = min(i, (TRANSDEPTHRES - 1));
839 tri->next = buf_sorttranstri_list[i];
840 buf_sorttranstri_list[i] = tri;
844 for (i = 0;i < currenttransmesh;i++)
845 buf_transmesh[i].transchain = NULL;
847 for (j = 0;j < TRANSDEPTHRES;j++)
849 if ((tri = buf_sorttranstri_list[j]))
851 for (;tri;tri = tri->next)
853 if (!tri->mesh->transchain)
855 tri->mesh->chain = transmesh;
856 transmesh = tri->mesh;
858 tri->meshsortchain = tri->mesh->transchain;
859 tri->mesh->transchain = tri;
864 for (;transmesh;transmesh = transmesh->chain)
866 if (currentmesh >= max_meshs || currenttriangle + transmesh->triangles > max_batch || currentvertex + transmesh->verts > max_verts)
869 mesh = &buf_mesh[currentmesh++];
870 *mesh = *transmesh; // copy mesh properties
872 mesh->firstvert = currentvertex;
873 memcpy(&buf_vertex[currentvertex], &buf_transvertex[transmesh->firstvert], transmesh->verts * sizeof(buf_vertex_t));
874 memcpy(&buf_fcolor[currentvertex], &buf_transfcolor[transmesh->firstvert], transmesh->verts * sizeof(buf_fcolor_t));
875 for (i = 0;i < backendunits && transmesh->textures[i];i++)
876 memcpy(&buf_texcoord[i][currentvertex], &buf_transtexcoord[i][transmesh->firstvert], transmesh->verts * sizeof(buf_texcoord_t));
877 currentvertex += mesh->verts;
879 mesh->firsttriangle = currenttriangle;
880 for (tri = transmesh->transchain;tri;tri = tri->meshsortchain)
882 buf_tri[currenttriangle].index[0] = tri->index[0];
883 buf_tri[currenttriangle].index[1] = tri->index[1];
884 buf_tri[currenttriangle].index[2] = tri->index[2];
887 mesh->triangles = currenttriangle - mesh->firsttriangle;
890 currenttransmesh = 0;
891 currenttranstriangle = 0;
892 currenttransvertex = 0;
895 void R_Mesh_Draw(const rmeshinfo_t *m)
897 // these are static because gcc runs out of virtual registers otherwise
898 static int i, j, overbright, *index;
899 static float *in, scaler;
900 static float cr, cg, cb, ca;
901 static buf_mesh_t *mesh;
902 static buf_vertex_t *vert;
903 static buf_fcolor_t *fcolor;
904 static buf_texcoord_t *texcoord[MAX_TEXTUREUNITS];
907 Sys_Error("R_Mesh_Draw: called when backend is not active\n");
913 Host_Error("R_Mesh_Draw: no triangles or verts\n");
915 // ignore meaningless alpha meshs
916 if (!m->depthwrite && m->blendfunc1 == GL_SRC_ALPHA && (m->blendfunc2 == GL_ONE || m->blendfunc2 == GL_ONE_MINUS_SRC_ALPHA))
920 for (i = 0, in = m->color + 3;i < m->numverts;i++, (int)in += m->colorstep)
923 if (i == m->numverts)
926 else if (m->ca < 0.01f)
931 Sys_Error("R_Mesh_Draw: called when backend is not active\n");
934 for (i = 0;i < m->numtriangles * 3;i++)
935 if ((unsigned int) m->index[i] >= (unsigned int) m->numverts)
936 Host_Error("R_Mesh_Draw: invalid index (%i of %i verts)\n", m->index, m->numverts);
941 if (currenttransmesh >= max_meshs || (currenttranstriangle + m->numtriangles) > max_meshs || (currenttransvertex + m->numverts) > max_verts)
945 Con_Printf("R_Mesh_Draw: ran out of room for transparent meshs\n");
952 c_transtris += m->numtriangles;
953 vert = &buf_transvertex[currenttransvertex];
954 fcolor = &buf_transfcolor[currenttransvertex];
955 for (i = 0;i < backendunits;i++)
956 texcoord[i] = &buf_transtexcoord[i][currenttransvertex];
958 // transmesh is only for storage of transparent meshs until they
959 // are inserted into the main mesh array
960 mesh = &buf_transmesh[currenttransmesh++];
961 mesh->firsttriangle = currenttranstriangle;
962 mesh->firstvert = currenttransvertex;
963 index = &buf_transtri[currenttranstriangle].index[0];
965 currenttranstriangle += m->numtriangles;
966 currenttransvertex += m->numverts;
970 if (m->numtriangles > max_meshs || m->numverts > max_verts)
972 Con_Printf("R_Mesh_Draw: mesh too big for buffers\n");
976 if (currentmesh >= max_meshs || (currenttriangle + m->numtriangles) > max_batch || (currentvertex + m->numverts) > max_verts)
980 c_meshtris += m->numtriangles;
981 vert = &buf_vertex[currentvertex];
982 fcolor = &buf_fcolor[currentvertex];
983 for (i = 0;i < backendunits;i++)
984 texcoord[i] = &buf_texcoord[i][currentvertex];
986 // opaque meshs are rendered directly
987 mesh = &buf_mesh[currentmesh++];
988 mesh->firsttriangle = currenttriangle;
989 mesh->firstvert = currentvertex;
990 index = &buf_tri[currenttriangle].index[0];
992 currenttriangle += m->numtriangles;
993 currentvertex += m->numverts;
996 // code shared for transparent and opaque meshs
997 memcpy(index, m->index, sizeof(int[3]) * m->numtriangles);
998 mesh->blendfunc1 = m->blendfunc1;
999 mesh->blendfunc2 = m->blendfunc2;
1000 mesh->depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite);
1001 mesh->depthtest = !m->depthdisable;
1002 mesh->triangles = m->numtriangles;
1003 mesh->verts = m->numverts;
1007 if (m->blendfunc2 == GL_SRC_COLOR)
1009 if (m->blendfunc1 == GL_DST_COLOR) // 2x modulate with framebuffer
1016 overbright = gl_combine.integer;
1020 scaler *= overbrightscale;
1025 for (i = 0;i < backendunits;i++)
1027 if ((mesh->textures[i] = m->tex[i]))
1029 mesh->texturergbscale[i] = m->texrgbscale[i];
1030 if (mesh->texturergbscale[i] != 1 && mesh->texturergbscale[i] != 2 && mesh->texturergbscale[i] != 4)
1031 mesh->texturergbscale[i] = 1;
1033 if (overbright && j >= 0)
1034 mesh->texturergbscale[j] = 4;
1036 if (m->vertexstep != sizeof(buf_vertex_t))
1038 for (i = 0, in = m->vertex;i < m->numverts;i++, (int)in += m->vertexstep)
1040 vert[i].v[0] = in[0];
1041 vert[i].v[1] = in[1];
1042 vert[i].v[2] = in[2];
1046 memcpy(vert, m->vertex, m->numverts * sizeof(buf_vertex_t));
1050 for (i = 0, in = m->color;i < m->numverts;i++, (int)in += m->colorstep)
1052 fcolor[i].c[0] = in[0] * scaler;
1053 fcolor[i].c[1] = in[1] * scaler;
1054 fcolor[i].c[2] = in[2] * scaler;
1055 fcolor[i].c[3] = in[3];
1060 cr = m->cr * scaler;
1061 cg = m->cg * scaler;
1062 cb = m->cb * scaler;
1064 for (i = 0;i < m->numverts;i++)
1066 fcolor[i].c[0] = cr;
1067 fcolor[i].c[1] = cg;
1068 fcolor[i].c[2] = cb;
1069 fcolor[i].c[3] = ca;
1073 for (j = 0;j < MAX_TEXTUREUNITS && m->tex[j];j++)
1075 if (j >= backendunits)
1076 Sys_Error("R_Mesh_Draw: texture %i supplied when there are only %i texture units\n", j + 1, backendunits);
1077 if (m->texcoordstep[j] != sizeof(buf_texcoord_t))
1079 for (i = 0, in = m->texcoords[j];i < m->numverts;i++, (int)in += m->texcoordstep[j])
1081 texcoord[j][i].t[0] = in[0];
1082 texcoord[j][i].t[1] = in[1];
1086 memcpy(&texcoord[j][0].t[0], m->texcoords[j], m->numverts * sizeof(buf_texcoord_t));
1089 for (;j < backendunits;j++)
1090 memset(&texcoord[j][0].t[0], 0, m->numverts * sizeof(buf_texcoord_t));
1094 void R_Mesh_Draw_NativeOnly(const rmeshinfo_t *m)
1096 // these are static because gcc runs out of virtual registers otherwise
1097 static int i, j, overbright, *index;
1098 static float *in, scaler;
1099 static buf_mesh_t *mesh;
1100 static buf_vertex_t *vert;
1101 static buf_fcolor_t *fcolor;
1102 static buf_texcoord_t *texcoord[MAX_TEXTUREUNITS];
1105 Sys_Error("R_Mesh_Draw: called when backend is not active\n");
1107 if (m->index == NULL
1109 || m->vertex == NULL
1111 Host_Error("R_Mesh_Draw: no triangles or verts\n");
1113 // ignore meaningless alpha meshs
1114 if (!m->depthwrite && m->blendfunc1 == GL_SRC_ALPHA && (m->blendfunc2 == GL_ONE || m->blendfunc2 == GL_ONE_MINUS_SRC_ALPHA))
1118 for (i = 0, in = m->color + 3;i < m->numverts;i++, (int)in += m->colorstep)
1121 if (i == m->numverts)
1124 else if (m->ca < 0.01f)
1130 if (currenttransmesh >= max_meshs || (currenttranstriangle + m->numtriangles) > max_meshs || (currenttransvertex + m->numverts) > max_verts)
1134 Con_Printf("R_Mesh_Draw_NativeOnly: ran out of room for transparent meshs\n");
1141 c_transtris += m->numtriangles;
1142 vert = &buf_transvertex[currenttransvertex];
1143 fcolor = &buf_transfcolor[currenttransvertex];
1144 for (i = 0;i < backendunits;i++)
1145 texcoord[i] = &buf_transtexcoord[i][currenttransvertex];
1147 // transmesh is only for storage of transparent meshs until they
1148 // are inserted into the main mesh array
1149 mesh = &buf_transmesh[currenttransmesh++];
1150 mesh->firsttriangle = currenttranstriangle;
1151 mesh->firstvert = currenttransvertex;
1152 index = &buf_transtri[currenttranstriangle].index[0];
1153 currenttranstriangle += m->numtriangles;
1154 currenttransvertex += m->numverts;
1158 if (m->numtriangles > max_meshs || m->numverts > max_verts)
1160 Con_Printf("R_Mesh_Draw_NativeOnly: mesh too big for buffers\n");
1164 if (currentmesh >= max_meshs || (currenttriangle + m->numtriangles) > max_batch || (currentvertex + m->numverts) > max_verts)
1168 c_meshtris += m->numtriangles;
1169 vert = &buf_vertex[currentvertex];
1170 fcolor = &buf_fcolor[currentvertex];
1171 for (i = 0;i < backendunits;i++)
1172 texcoord[i] = &buf_texcoord[i][currentvertex];
1174 // opaque meshs are rendered directly
1175 mesh = &buf_mesh[currentmesh++];
1176 mesh->firsttriangle = currenttriangle;
1177 mesh->firstvert = currentvertex;
1178 index = &buf_tri[currenttriangle].index[0];
1179 currenttriangle += m->numtriangles;
1180 currentvertex += m->numverts;
1183 // code shared for transparent and opaque meshs
1184 memcpy(index, m->index, sizeof(int[3]) * m->numtriangles);
1185 mesh->blendfunc1 = m->blendfunc1;
1186 mesh->blendfunc2 = m->blendfunc2;
1187 mesh->depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite);
1188 mesh->depthtest = !m->depthdisable;
1189 mesh->triangles = m->numtriangles;
1190 mesh->verts = m->numverts;
1194 if (m->blendfunc2 == GL_SRC_COLOR)
1196 if (m->blendfunc1 == GL_DST_COLOR) // 2x modulate with framebuffer
1203 overbright = gl_combine.integer;
1207 scaler *= overbrightscale;
1211 for (i = 0;i < backendunits;i++)
1213 if ((mesh->textures[i] = m->tex[i]))
1215 mesh->texturergbscale[i] = m->texrgbscale[i];
1216 if (mesh->texturergbscale[i] != 1 && mesh->texturergbscale[i] != 2 && mesh->texturergbscale[i] != 4)
1217 mesh->texturergbscale[i] = 1;
1219 if (overbright && j >= 0)
1220 mesh->texturergbscale[j] = 4;
1222 if (m->vertexstep != sizeof(buf_vertex_t))
1223 Host_Error("R_Mesh_Draw_NativeOnly: unsupported vertexstep\n");
1224 if (m->colorstep != sizeof(buf_fcolor_t))
1225 Host_Error("R_Mesh_Draw_NativeOnly: unsupported colorstep\n");
1226 if (m->color == NULL)
1227 Host_Error("R_Mesh_Draw_NativeOnly: must provide color array\n");
1228 for (j = 0;j < MAX_TEXTUREUNITS && m->tex[j];j++)
1230 if (j >= backendunits)
1231 Sys_Error("R_Mesh_Draw_NativeOnly: texture %i supplied when there are only %i texture units\n", j + 1, backendunits);
1232 if (m->texcoordstep[j] != sizeof(buf_texcoord_t))
1233 Host_Error("R_Mesh_Draw_NativeOnly: unsupported texcoordstep\n");
1236 memcpy(vert, m->vertex, m->numverts * sizeof(buf_vertex_t));
1237 for (j = 0;j < MAX_TEXTUREUNITS && m->tex[j];j++)
1238 memcpy(&texcoord[j][0].t[0], m->texcoords[j], m->numverts * sizeof(buf_texcoord_t));
1240 for (;j < backendunits;j++)
1241 memset(&texcoord[j][0].t[0], 0, m->numverts * sizeof(buf_texcoord_t));
1244 memcpy(fcolor, m->color, m->numverts * sizeof(buf_fcolor_t));
1246 // do this as a second step because memcpy preloaded the cache, which we can't easily do
1249 for (i = 0;i < m->numverts;i++)
1251 fcolor[i].c[0] *= scaler;
1252 fcolor[i].c[1] *= scaler;
1253 fcolor[i].c[2] *= scaler;
1258 // allocates space in geometry buffers, and fills in pointers to the buffers in passsed struct
1259 // (this is used for very high speed rendering, no copying)
1260 int R_Mesh_Draw_GetBuffer(rmeshbufferinfo_t *m)
1262 // these are static because gcc runs out of virtual registers otherwise
1263 int i, j, overbright;
1268 Sys_Error("R_Mesh_Draw: called when backend is not active\n");
1270 if (!m->numtriangles
1272 Host_Error("R_Mesh_Draw: no triangles or verts\n");
1276 if (currenttransmesh >= max_meshs || (currenttranstriangle + m->numtriangles) > max_meshs || (currenttransvertex + m->numverts) > max_verts)
1280 Con_Printf("R_Mesh_Draw: ran out of room for transparent meshs\n");
1287 c_transtris += m->numtriangles;
1288 m->index = &buf_transtri[currenttranstriangle].index[0];
1289 m->vertex = &buf_transvertex[currenttransvertex].v[0];
1290 m->color = &buf_transfcolor[currenttransvertex].c[0];
1291 for (i = 0;i < backendunits;i++)
1292 m->texcoords[i] = &buf_transtexcoord[i][currenttransvertex].t[0];
1294 // transmesh is only for storage of transparent meshs until they
1295 // are inserted into the main mesh array
1296 mesh = &buf_transmesh[currenttransmesh++];
1297 mesh->firsttriangle = currenttranstriangle;
1298 mesh->firstvert = currenttransvertex;
1299 currenttranstriangle += m->numtriangles;
1300 currenttransvertex += m->numverts;
1304 if (m->numtriangles > max_meshs || m->numverts > max_verts)
1306 Con_Printf("R_Mesh_Draw_GetBuffer: mesh too big for buffers\n");
1310 if (currentmesh >= max_meshs || (currenttriangle + m->numtriangles) > max_batch || (currentvertex + m->numverts) > max_verts)
1314 c_meshtris += m->numtriangles;
1315 m->index = &buf_tri[currenttriangle].index[0];
1316 m->vertex = &buf_vertex[currentvertex].v[0];
1317 m->color = &buf_fcolor[currentvertex].c[0];
1318 for (i = 0;i < backendunits;i++)
1319 m->texcoords[i] = &buf_texcoord[i][currentvertex].t[0];
1321 // opaque meshs are rendered directly
1322 mesh = &buf_mesh[currentmesh++];
1323 mesh->firsttriangle = currenttriangle;
1324 mesh->firstvert = currentvertex;
1325 currenttriangle += m->numtriangles;
1326 currentvertex += m->numverts;
1329 // code shared for transparent and opaque meshs
1330 mesh->blendfunc1 = m->blendfunc1;
1331 mesh->blendfunc2 = m->blendfunc2;
1332 mesh->depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite);
1333 mesh->depthtest = !m->depthdisable;
1334 mesh->triangles = m->numtriangles;
1335 mesh->verts = m->numverts;
1339 if (m->blendfunc2 == GL_SRC_COLOR)
1341 if (m->blendfunc1 == GL_DST_COLOR) // 2x modulate with framebuffer
1348 overbright = gl_combine.integer;
1352 scaler *= overbrightscale;
1354 m->colorscale = scaler;
1357 for (i = 0;i < MAX_TEXTUREUNITS;i++)
1359 if ((mesh->textures[i] = m->tex[i]))
1362 if (i >= backendunits)
1363 Sys_Error("R_Mesh_Draw_GetBuffer: texture %i supplied when there are only %i texture units\n", j + 1, backendunits);
1365 mesh->texturergbscale[i] = m->texrgbscale[i];
1366 if (mesh->texturergbscale[i] != 1 && mesh->texturergbscale[i] != 2 && mesh->texturergbscale[i] != 4)
1367 mesh->texturergbscale[i] = 1;
1369 if (overbright && j >= 0)
1370 mesh->texturergbscale[j] = 4;
1375 void R_Mesh_DrawPolygon(rmeshinfo_t *m, int numverts)
1377 m->index = polyindexarray;
1378 m->numverts = numverts;
1379 m->numtriangles = numverts - 2;
1380 if (m->numtriangles < 1)
1382 Con_Printf("R_Mesh_DrawPolygon: invalid vertex count\n");
1385 if (m->numtriangles >= 256)
1387 Con_Printf("R_Mesh_DrawPolygon: only up to 256 triangles (258 verts) supported\n");
1394 ==============================================================================
1398 ==============================================================================
1401 float CalcFov (float fov_x, float width, float height);
1402 void R_ClearScreen(void);
1404 void SCR_ScreenShot(char *filename, int x, int y, int width, int height)
1409 buffer = Mem_Alloc(tempmempool, width*height*3);
1410 glReadPixels (x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer);
1413 // LordHavoc: compensate for v_overbrightbits when using hardware gamma
1414 if (v_hwgamma.integer)
1415 for (i = 0;i < width * height * 3;i++)
1416 buffer[i] <<= v_overbrightbits.integer;
1418 Image_WriteTGARGB_preflipped(filename, width, height, buffer);
1428 void SCR_ScreenShot_f (void)
1432 char checkname[MAX_OSPATH];
1434 // find a file name to save it to
1436 strcpy(filename, "dp0000.tga");
1438 for (i=0 ; i<=9999 ; i++)
1440 filename[2] = (i/1000)%10 + '0';
1441 filename[3] = (i/ 100)%10 + '0';
1442 filename[4] = (i/ 10)%10 + '0';
1443 filename[5] = (i/ 1)%10 + '0';
1444 sprintf (checkname, "%s/%s", com_gamedir, filename);
1445 if (Sys_FileTime(checkname) == -1)
1446 break; // file doesn't exist
1450 Con_Printf ("SCR_ScreenShot_f: Couldn't create a TGA file\n");
1454 SCR_ScreenShot(filename, vid.realx, vid.realy, vid.realwidth, vid.realheight);
1455 Con_Printf ("Wrote %s\n", filename);
1462 Grab six views for environment mapping tests
1473 {{ 0, 90, 0}, "rt"},
1474 {{ 0, 180, 0}, "bk"},
1475 {{ 0, 270, 0}, "lf"},
1476 {{-90, 90, 0}, "up"},
1477 {{ 90, 90, 0}, "dn"}
1479 static void R_Envmap_f (void)
1482 char filename[256], basename[256];
1484 if (Cmd_Argc() != 3)
1486 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");
1490 if (!r_render.integer)
1493 strcpy(basename, Cmd_Argv(1));
1494 size = atoi(Cmd_Argv(2));
1495 if (size != 128 && size != 256 && size != 512 && size != 1024)
1497 Con_Printf("envmap: size must be one of 128, 256, 512, or 1024\n");
1500 if (size > vid.realwidth || size > vid.realheight)
1502 Con_Printf("envmap: your resolution is not big enough to render that size\n");
1510 r_refdef.width = size;
1511 r_refdef.height = size;
1513 r_refdef.fov_x = 90;
1514 r_refdef.fov_y = 90;
1516 for (j = 0;j < 6;j++)
1518 sprintf(filename, "env/%s%s.tga", basename, envmapinfo[j].name);
1519 VectorCopy(envmapinfo[j].angles, r_refdef.viewangles);
1522 SCR_ScreenShot(filename, vid.realx, vid.realy, size, size);
1528 //=============================================================================
1530 void R_ClearScreen(void)
1532 if (r_render.integer)
1534 glClearColor(0,0,0,0);
1536 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // LordHavoc: clear the screen (around the view as well)
1538 if (gl_dither.integer)
1539 glEnable(GL_DITHER);
1541 glDisable(GL_DITHER);
1550 This is called every frame, and can also be called explicitly to flush
1554 void SCR_UpdateScreen (void)
1556 //Mem_CheckSentinelsGlobal();
1557 //R_TimeReport("memtest");
1564 R_TimeReport("finish");
1566 if (gl_combine.integer && !gl_combine_extension)
1567 Cvar_SetValue("gl_combine", 0);
1569 lightscalebit = v_overbrightbits.integer;
1570 if (gl_combine.integer && r_multitexture.integer)
1573 lightscale = 1.0f / (float) (1 << lightscalebit);
1574 overbrightscale = 1.0f / (float) (1 << v_overbrightbits.integer);
1576 R_TimeReport("setup");
1580 R_TimeReport("clear");
1582 if (scr_conlines < vid.conheight)