4 //cvar_t gl_mesh_maxtriangles = {0, "gl_mesh_maxtriangles", "21760"};
5 cvar_t gl_mesh_maxtriangles = {0, "gl_mesh_maxtriangles", "8192"};
6 //cvar_t gl_mesh_batchtriangles = {0, "gl_mesh_batchtriangles", "1024"};
7 cvar_t gl_mesh_batchtriangles = {0, "gl_mesh_batchtriangles", "0"};
8 cvar_t gl_mesh_floatcolors = {0, "gl_mesh_floatcolors", "0"};
10 cvar_t r_render = {0, "r_render", "1"};
11 cvar_t gl_dither = {CVAR_SAVE, "gl_dither", "1"}; // whether or not to use dithering
12 cvar_t gl_lockarrays = {0, "gl_lockarrays", "1"};
17 void GL_PrintError(int errornumber, char *filename, int linenumber)
21 #ifdef GL_INVALID_ENUM
23 Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
26 #ifdef GL_INVALID_VALUE
27 case GL_INVALID_VALUE:
28 Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
31 #ifdef GL_INVALID_OPERATION
32 case GL_INVALID_OPERATION:
33 Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
36 #ifdef GL_STACK_OVERFLOW
37 case GL_STACK_OVERFLOW:
38 Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
41 #ifdef GL_STACK_UNDERFLOW
42 case GL_STACK_UNDERFLOW:
43 Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
46 #ifdef GL_OUT_OF_MEMORY
47 case GL_OUT_OF_MEMORY:
48 Con_Printf("GL_OUT_OF_MEMORY at %s:%i\n", filename, linenumber);
51 #ifdef GL_TABLE_TOO_LARGE
52 case GL_TABLE_TOO_LARGE:
53 Con_Printf("GL_TABLE_TOO_LARGE at %s:%i\n", filename, linenumber);
57 Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
63 float r_farclip, r_newfarclip;
65 int polyindexarray[768];
67 static float viewdist;
69 int c_meshs, c_meshtris, c_transmeshs, c_transtris;
73 float overbrightscale;
75 void SCR_ScreenShot_f (void);
79 static int max_verts; // always max_meshs * 3
80 #define TRANSDEPTHRES 4096
82 typedef struct buf_mesh_s
86 int blendfunc1, blendfunc2;
87 int textures[MAX_TEXTUREUNITS];
88 float texturergbscale[MAX_TEXTUREUNITS];
93 struct buf_mesh_s *chain;
94 struct buf_transtri_s *transchain;
98 typedef struct buf_transtri_s
100 struct buf_transtri_s *next;
101 struct buf_transtri_s *meshsortchain;
107 typedef struct buf_tri_s
137 static float meshfarclip;
138 static int currentmesh, currenttriangle, currentvertex, backendunits, backendactive, transranout;
139 static buf_mesh_t *buf_mesh;
140 static buf_tri_t *buf_tri;
141 static buf_vertex_t *buf_vertex;
142 static buf_fcolor_t *buf_fcolor;
143 static buf_bcolor_t *buf_bcolor;
144 static buf_texcoord_t *buf_texcoord[MAX_TEXTUREUNITS];
146 static int currenttransmesh, currenttransvertex, currenttranstriangle;
147 static buf_mesh_t *buf_transmesh;
148 static buf_transtri_t *buf_sorttranstri;
149 static buf_transtri_t **buf_sorttranstri_list;
150 static buf_tri_t *buf_transtri;
151 static buf_vertex_t *buf_transvertex;
152 static buf_fcolor_t *buf_transfcolor;
153 static buf_texcoord_t *buf_transtexcoord[MAX_TEXTUREUNITS];
155 static mempool_t *gl_backend_mempool;
156 static int resizingbuffers = false;
158 static void gl_backend_start(void)
162 max_verts = max_meshs * 3;
164 if (!gl_backend_mempool)
165 gl_backend_mempool = Mem_AllocPool("GL_Backend");
167 #define BACKENDALLOC(var, count, sizeofstruct)\
169 var = Mem_Alloc(gl_backend_mempool, count * sizeof(sizeofstruct));\
171 Sys_Error("gl_backend_start: unable to allocate memory\n");\
172 memset(var, 0, count * sizeof(sizeofstruct));\
175 BACKENDALLOC(buf_mesh, max_meshs, buf_mesh_t)
176 BACKENDALLOC(buf_tri, max_meshs, buf_tri_t)
177 BACKENDALLOC(buf_vertex, max_verts, buf_vertex_t)
178 BACKENDALLOC(buf_fcolor, max_verts, buf_fcolor_t)
179 BACKENDALLOC(buf_bcolor, max_verts, buf_bcolor_t)
181 BACKENDALLOC(buf_transmesh, max_meshs, buf_mesh_t)
182 BACKENDALLOC(buf_sorttranstri, max_meshs, buf_transtri_t)
183 BACKENDALLOC(buf_sorttranstri_list, TRANSDEPTHRES, buf_transtri_t *)
184 BACKENDALLOC(buf_transtri, max_meshs, buf_tri_t)
185 BACKENDALLOC(buf_transvertex, max_verts, buf_vertex_t)
186 BACKENDALLOC(buf_transfcolor, max_verts, buf_fcolor_t)
188 for (i = 0;i < MAX_TEXTUREUNITS;i++)
190 // only allocate as many texcoord arrays as we need
191 if (i < gl_textureunits)
193 BACKENDALLOC(buf_texcoord[i], max_verts, buf_texcoord_t)
194 BACKENDALLOC(buf_transtexcoord[i], max_verts, buf_texcoord_t)
198 buf_texcoord[i] = NULL;
199 buf_transtexcoord[i] = NULL;
202 backendunits = min(MAX_TEXTUREUNITS, gl_textureunits);
203 backendactive = true;
206 static void gl_backend_shutdown(void)
209 Mem_EmptyPool(gl_backend_mempool);
211 Mem_FreePool(&gl_backend_mempool);
214 backendactive = false;
217 static void gl_backend_bufferchanges(int init)
219 // 21760 is (65536 / 3) rounded off to a multiple of 128
220 if (gl_mesh_maxtriangles.integer < 256)
221 Cvar_SetValue("gl_mesh_maxtriangles", 256);
222 if (gl_mesh_maxtriangles.integer > 21760)
223 Cvar_SetValue("gl_mesh_maxtriangles", 21760);
225 if (gl_mesh_batchtriangles.integer < 0)
226 Cvar_SetValue("gl_mesh_batchtriangles", 0);
227 if (gl_mesh_batchtriangles.integer > gl_mesh_maxtriangles.integer)
228 Cvar_SetValue("gl_mesh_batchtriangles", gl_mesh_maxtriangles.integer);
230 max_batch = gl_mesh_batchtriangles.integer;
232 if (max_meshs != gl_mesh_maxtriangles.integer)
234 max_meshs = gl_mesh_maxtriangles.integer;
238 resizingbuffers = true;
239 gl_backend_shutdown();
241 resizingbuffers = false;
246 static void gl_backend_newmap(void)
248 r_farclip = r_newfarclip = 2048.0f;
251 void gl_backend_init(void)
255 Cvar_RegisterVariable(&r_render);
256 Cvar_RegisterVariable(&gl_dither);
257 Cvar_RegisterVariable(&gl_lockarrays);
259 Cvar_SetValue("r_render", 0);
262 Cvar_RegisterVariable(&gl_mesh_maxtriangles);
263 Cvar_RegisterVariable(&gl_mesh_batchtriangles);
264 Cvar_RegisterVariable(&gl_mesh_floatcolors);
265 R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap);
266 gl_backend_bufferchanges(true);
267 for (i = 0;i < 256;i++)
269 polyindexarray[i*3+0] = 0;
270 polyindexarray[i*3+1] = i + 1;
271 polyindexarray[i*3+2] = i + 2;
275 int arraylocked = false;
277 void GL_LockArray(int first, int count)
279 if (!arraylocked && gl_supportslockarrays && gl_lockarrays.integer)
281 qglLockArraysEXT(first, count);
287 void GL_UnlockArray(void)
291 qglUnlockArraysEXT();
297 //static float gldepthmin, gldepthmax;
304 static void GL_SetupFrame (void)
307 double fovx, fovy, zNear, zFar, aspect;
309 // update farclip based on previous frame
310 r_farclip = r_newfarclip;
312 if (!r_render.integer)
315 // glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // LordHavoc: moved to SCR_UpdateScreen
318 glDepthFunc (GL_LEQUAL);CHECKGLERROR
320 // glDepthRange (gldepthmin, gldepthmax);CHECKGLERROR
323 glMatrixMode(GL_PROJECTION);CHECKGLERROR
324 glLoadIdentity ();CHECKGLERROR
326 // y is weird beause OpenGL is bottom to top, we use top to bottom
327 glViewport(r_refdef.x, vid.realheight - (r_refdef.y + r_refdef.height), r_refdef.width, r_refdef.height);CHECKGLERROR
334 fovx = r_refdef.fov_x;
335 fovy = r_refdef.fov_y;
336 aspect = r_refdef.width / r_refdef.height;
339 xmax = zNear * tan(fovx * M_PI / 360.0) * aspect;
340 ymax = zNear * tan(fovy * M_PI / 360.0);
343 glFrustum(-xmax, xmax, -ymax, ymax, zNear, zFar);CHECKGLERROR
345 // glCullFace(GL_FRONT);CHECKGLERROR
347 glMatrixMode(GL_MODELVIEW);CHECKGLERROR
348 glLoadIdentity ();CHECKGLERROR
351 glRotatef (-90, 1, 0, 0);CHECKGLERROR
352 glRotatef (90, 0, 0, 1);CHECKGLERROR
354 glRotatef (-r_refdef.viewangles[2], 1, 0, 0);CHECKGLERROR
355 glRotatef (-r_refdef.viewangles[0], 0, 1, 0);CHECKGLERROR
356 glRotatef (-r_refdef.viewangles[1], 0, 0, 1);CHECKGLERROR
358 glTranslatef (-r_refdef.vieworg[0], -r_refdef.vieworg[1], -r_refdef.vieworg[2]);CHECKGLERROR
360 // glGetFloatv (GL_MODELVIEW_MATRIX, r_world_matrix);
365 // if (gl_cull.integer)
367 // glEnable(GL_CULL_FACE);CHECKGLERROR
371 // glDisable(GL_CULL_FACE);CHECKGLERROR
374 // glEnable(GL_BLEND);CHECKGLERROR
375 // glEnable(GL_DEPTH_TEST);CHECKGLERROR
376 // glDepthMask(1);CHECKGLERROR
379 static int mesh_blendfunc1;
380 static int mesh_blendfunc2;
381 static int mesh_blend;
382 static GLboolean mesh_depthmask;
383 static int mesh_depthtest;
384 static int mesh_unit;
385 static int mesh_clientunit;
386 static int mesh_texture[MAX_TEXTUREUNITS];
387 static float mesh_texturergbscale[MAX_TEXTUREUNITS];
389 void GL_SetupTextureState(void)
392 if (backendunits > 1)
394 for (i = 0;i < backendunits;i++)
396 qglActiveTexture(GL_TEXTURE0_ARB + (mesh_unit = i));CHECKGLERROR
397 glBindTexture(GL_TEXTURE_2D, mesh_texture[i]);CHECKGLERROR
398 if (gl_combine.integer)
400 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);CHECKGLERROR
401 glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);CHECKGLERROR
402 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);CHECKGLERROR
403 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
404 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_CONSTANT_ARB);CHECKGLERROR
405 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
406 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
407 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);CHECKGLERROR
408 glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);CHECKGLERROR
409 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);CHECKGLERROR
410 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
411 glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, GL_CONSTANT_ARB);CHECKGLERROR
412 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
413 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
414 glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
415 glTexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, mesh_texturergbscale[i]);CHECKGLERROR
416 glTexEnvf(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1.0f);CHECKGLERROR
420 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
422 qglClientActiveTexture(GL_TEXTURE0_ARB + (mesh_clientunit = i));CHECKGLERROR
423 glTexCoordPointer(2, GL_FLOAT, sizeof(buf_texcoord_t), buf_texcoord[i]);CHECKGLERROR
426 glEnable(GL_TEXTURE_2D);CHECKGLERROR
427 glEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
431 glDisable(GL_TEXTURE_2D);CHECKGLERROR
432 glDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
438 glBindTexture(GL_TEXTURE_2D, mesh_texture[0]);CHECKGLERROR
439 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
440 glTexCoordPointer(2, GL_FLOAT, sizeof(buf_texcoord_t), buf_texcoord[0]);CHECKGLERROR
443 glEnable(GL_TEXTURE_2D);CHECKGLERROR
444 glEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
448 glDisable(GL_TEXTURE_2D);CHECKGLERROR
449 glDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
454 // called at beginning of frame
455 void R_Mesh_Start(void)
459 Sys_Error("R_Mesh_Clear: called when backend is not active\n");
463 gl_backend_bufferchanges(false);
468 currenttransmesh = 0;
469 currenttranstriangle = 0;
470 currenttransvertex = 0;
473 viewdist = DotProduct(r_origin, vpn);
485 for (i = 0;i < backendunits;i++)
488 mesh_texturergbscale[i] = 1;
491 glEnable(GL_CULL_FACE);CHECKGLERROR
492 glCullFace(GL_FRONT);CHECKGLERROR
494 mesh_depthtest = true;
495 glEnable(GL_DEPTH_TEST);CHECKGLERROR
497 mesh_blendfunc1 = GL_ONE;
498 mesh_blendfunc2 = GL_ZERO;
499 glBlendFunc(mesh_blendfunc1, mesh_blendfunc2);CHECKGLERROR
502 glDisable(GL_BLEND);CHECKGLERROR
504 mesh_depthmask = GL_TRUE;
505 glDepthMask(mesh_depthmask);CHECKGLERROR
507 glVertexPointer(3, GL_FLOAT, sizeof(buf_vertex_t), &buf_vertex[0].v[0]);CHECKGLERROR
508 glEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
509 if (gl_mesh_floatcolors.integer)
511 glColorPointer(4, GL_FLOAT, sizeof(buf_fcolor_t), &buf_fcolor[0].c[0]);CHECKGLERROR
515 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(buf_bcolor_t), &buf_bcolor[0].c[0]);CHECKGLERROR
517 glEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
519 GL_SetupTextureState();
522 int gl_backend_rebindtextures;
524 void GL_UpdateFarclip(void)
529 // push out farclip based on vertices
530 // FIXME: wouldn't this be slow when using matrix transforms?
531 for (i = 0;i < currentvertex;i++)
533 farclip = DotProduct(buf_vertex[i].v, vpn);
534 if (meshfarclip < farclip)
535 meshfarclip = farclip;
538 farclip = meshfarclip + 256.0f - viewdist; // + 256 just to be safe
540 // push out farclip for next frame
541 if (farclip > r_newfarclip)
542 r_newfarclip = ceil((farclip + 255) / 256) * 256 + 256;
545 void GL_ConvertColorsFloatToByte(void)
551 // shift float to have 8bit fraction at base of number
552 for (i = 0, fcolor = &buf_fcolor->c[0];i < currentvertex;i++)
554 *fcolor++ += 32768.0f;
555 *fcolor++ += 32768.0f;
556 *fcolor++ += 32768.0f;
557 *fcolor++ += 32768.0f;
559 // then read as integer and kill float bits...
560 for (i = 0, icolor = (int *)&buf_fcolor->c[0], bcolor = &buf_bcolor->c[0];i < currentvertex;i++)
562 k = (*icolor++) & 0x7FFFFF;*bcolor++ = k > 255 ? 255 : k;
563 k = (*icolor++) & 0x7FFFFF;*bcolor++ = k > 255 ? 255 : k;
564 k = (*icolor++) & 0x7FFFFF;*bcolor++ = k > 255 ? 255 : k;
565 k = (*icolor++) & 0x7FFFFF;*bcolor++ = k > 255 ? 255 : k;
569 void GL_MeshState(buf_mesh_t *mesh)
572 if (backendunits > 1)
574 for (i = 0;i < backendunits;i++)
576 if (mesh_texture[i] != mesh->textures[i])
580 qglActiveTexture(GL_TEXTURE0_ARB + (mesh_unit = i));CHECKGLERROR
582 if (mesh_texture[i] == 0)
584 glEnable(GL_TEXTURE_2D);CHECKGLERROR
585 // have to disable texcoord array on disabled texture
586 // units due to NVIDIA driver bug with
587 // compiled_vertex_array
588 if (mesh_clientunit != i)
590 qglClientActiveTexture(GL_TEXTURE0_ARB + (mesh_clientunit = i));CHECKGLERROR
592 glEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
594 glBindTexture(GL_TEXTURE_2D, (mesh_texture[i] = mesh->textures[i]));CHECKGLERROR
595 if (mesh_texture[i] == 0)
597 glDisable(GL_TEXTURE_2D);CHECKGLERROR
598 // have to disable texcoord array on disabled texture
599 // units due to NVIDIA driver bug with
600 // compiled_vertex_array
601 if (mesh_clientunit != i)
603 qglClientActiveTexture(GL_TEXTURE0_ARB + (mesh_clientunit = i));CHECKGLERROR
605 glDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
608 if (mesh_texturergbscale[i] != mesh->texturergbscale[i])
612 qglActiveTexture(GL_TEXTURE0_ARB + (mesh_unit = i));CHECKGLERROR
614 glTexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (mesh_texturergbscale[i] = mesh->texturergbscale[i]));CHECKGLERROR
620 if (mesh_texture[0] != mesh->textures[0])
622 if (mesh_texture[0] == 0)
624 glEnable(GL_TEXTURE_2D);CHECKGLERROR
625 glEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
627 glBindTexture(GL_TEXTURE_2D, (mesh_texture[0] = mesh->textures[0]));CHECKGLERROR
628 if (mesh_texture[0] == 0)
630 glDisable(GL_TEXTURE_2D);CHECKGLERROR
631 glDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
635 if (mesh_blendfunc1 != mesh->blendfunc1 || mesh_blendfunc2 != mesh->blendfunc2)
637 glBlendFunc(mesh_blendfunc1 = mesh->blendfunc1, mesh_blendfunc2 = mesh->blendfunc2);CHECKGLERROR
638 if (mesh_blendfunc2 == GL_ZERO)
640 if (mesh_blendfunc1 == GL_ONE)
645 glDisable(GL_BLEND);CHECKGLERROR
653 glEnable(GL_BLEND);CHECKGLERROR
662 glEnable(GL_BLEND);CHECKGLERROR
666 if (mesh_depthtest != mesh->depthtest)
668 mesh_depthtest = mesh->depthtest;
670 glEnable(GL_DEPTH_TEST);
672 glDisable(GL_DEPTH_TEST);
674 if (mesh_depthmask != mesh->depthmask)
676 glDepthMask(mesh_depthmask = mesh->depthmask);CHECKGLERROR
680 // renders mesh buffers, called to flush buffers when full
681 void R_Mesh_Render(void)
691 Sys_Error("R_Mesh_Render: called when backend is not active\n");
700 if (!gl_mesh_floatcolors.integer)
701 GL_ConvertColorsFloatToByte();
703 // lock the arrays now that they will have no further modifications
704 //GL_LockArray(0, currentvertex);CHECKGLERROR
705 if (gl_backend_rebindtextures)
707 gl_backend_rebindtextures = false;
708 GL_SetupTextureState();
711 GL_MeshState(buf_mesh);
712 GL_LockArray(0, currentvertex);
714 // FIXME: dynamic link to GL so we can get DrawRangeElements on WIN32
715 glDrawElements(GL_TRIANGLES, buf_mesh->triangles * 3, GL_UNSIGNED_INT, (unsigned int *)&buf_tri[buf_mesh->firsttriangle].index[0]);CHECKGLERROR
717 glDrawRangeElements(GL_TRIANGLES, buf_mesh->firstvert, buf_mesh->firstvert + buf_mesh->verts, buf_mesh->triangles * 3, GL_UNSIGNED_INT, (unsigned int *)&buf_tri[buf_mesh->firsttriangle].index[0]);CHECKGLERROR
720 if (currentmesh >= 2)
722 for (k = 1, mesh = buf_mesh + k;k < currentmesh;k++, mesh++)
726 firstvert = mesh->firstvert;
727 indexcount = mesh->triangles * 3;
728 index = (unsigned int *)&buf_tri[mesh->firsttriangle].index[0];
730 // if not using batching, skip the index adjustment
732 for (i = 0;i < indexcount;i++)
733 index[i] += firstvert;
736 // FIXME: dynamic link to GL so we can get DrawRangeElements on WIN32
737 glDrawElements(GL_TRIANGLES, indexcount, GL_UNSIGNED_INT, index);CHECKGLERROR
739 glDrawRangeElements(GL_TRIANGLES, firstvert, firstvert + mesh->verts, indexcount, GL_UNSIGNED_INT, index);CHECKGLERROR
748 GL_UnlockArray();CHECKGLERROR
751 // restores backend state, used when done with 3D rendering
752 void R_Mesh_Finish(void)
755 // flush any queued meshs
758 if (backendunits > 1)
760 for (i = backendunits - 1;i >= 0;i--)
762 qglActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
763 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
764 if (gl_combine.integer)
766 glTexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1.0f);CHECKGLERROR
770 glDisable(GL_TEXTURE_2D);CHECKGLERROR
774 glEnable(GL_TEXTURE_2D);CHECKGLERROR
776 glBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
778 qglClientActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
779 glDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
784 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
785 glEnable(GL_TEXTURE_2D);CHECKGLERROR
786 glDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
788 glDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
789 glDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
791 glDisable(GL_BLEND);CHECKGLERROR
792 glEnable(GL_DEPTH_TEST);CHECKGLERROR
793 glDepthMask(true);CHECKGLERROR
794 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);CHECKGLERROR
797 void R_Mesh_ClearDepth(void)
799 R_Mesh_AddTransparent();
801 glClear(GL_DEPTH_BUFFER_BIT);
805 void R_Mesh_AddTransparent(void)
808 float viewdistcompare, centerscaler, dist1, dist2, dist3, center, maxdist;
809 buf_vertex_t *vert1, *vert2, *vert3;
811 buf_mesh_t *mesh, *transmesh;
813 if (!currenttransmesh)
816 // convert index data to transtris for sorting
817 for (j = 0;j < currenttransmesh;j++)
819 mesh = buf_transmesh + j;
820 k = mesh->firsttriangle;
821 index = &buf_transtri[k].index[0];
822 for (i = 0;i < mesh->triangles;i++)
824 tri = &buf_sorttranstri[k++];
826 tri->index[0] = *index++;
827 tri->index[1] = *index++;
828 tri->index[2] = *index++;
832 // map farclip to 0-4095 list range
833 centerscaler = (TRANSDEPTHRES / r_farclip) * (1.0f / 3.0f);
834 viewdistcompare = viewdist + 4.0f;
836 memset(buf_sorttranstri_list, 0, TRANSDEPTHRES * sizeof(buf_transtri_t *));
839 for (j = 0;j < currenttranstriangle;j++)
841 tri = &buf_sorttranstri[j];
842 i = tri->mesh->firstvert;
844 vert1 = &buf_transvertex[tri->index[0] + i];
845 vert2 = &buf_transvertex[tri->index[1] + i];
846 vert3 = &buf_transvertex[tri->index[2] + i];
848 dist1 = DotProduct(vert1->v, vpn);
849 dist2 = DotProduct(vert2->v, vpn);
850 dist3 = DotProduct(vert3->v, vpn);
852 maxdist = max(dist1, max(dist2, dist3));
853 if (maxdist < viewdistcompare)
856 center = (dist1 + dist2 + dist3) * centerscaler - viewdist;
859 i = bound(0, i, (TRANSDEPTHRES - 1));
863 center += 8388608.0f;
864 i = *((int *)¢er) & 0x7FFFFF;
865 i = min(i, (TRANSDEPTHRES - 1));
867 tri->next = buf_sorttranstri_list[i];
868 buf_sorttranstri_list[i] = tri;
872 for (i = 0;i < currenttransmesh;i++)
873 buf_transmesh[i].transchain = NULL;
875 for (j = 0;j < TRANSDEPTHRES;j++)
877 if ((tri = buf_sorttranstri_list[j]))
879 for (;tri;tri = tri->next)
881 if (!tri->mesh->transchain)
883 tri->mesh->chain = transmesh;
884 transmesh = tri->mesh;
886 tri->meshsortchain = tri->mesh->transchain;
887 tri->mesh->transchain = tri;
892 for (;transmesh;transmesh = transmesh->chain)
894 if (currentmesh >= max_meshs || currenttriangle + transmesh->triangles > max_batch || currentvertex + transmesh->verts > max_verts)
897 mesh = &buf_mesh[currentmesh++];
898 *mesh = *transmesh; // copy mesh properties
900 mesh->firstvert = currentvertex;
901 memcpy(&buf_vertex[currentvertex], &buf_transvertex[transmesh->firstvert], transmesh->verts * sizeof(buf_vertex_t));
902 memcpy(&buf_fcolor[currentvertex], &buf_transfcolor[transmesh->firstvert], transmesh->verts * sizeof(buf_fcolor_t));
903 for (i = 0;i < backendunits && transmesh->textures[i];i++)
904 memcpy(&buf_texcoord[i][currentvertex], &buf_transtexcoord[i][transmesh->firstvert], transmesh->verts * sizeof(buf_texcoord_t));
905 currentvertex += mesh->verts;
907 mesh->firsttriangle = currenttriangle;
908 for (tri = transmesh->transchain;tri;tri = tri->meshsortchain)
910 buf_tri[currenttriangle].index[0] = tri->index[0];
911 buf_tri[currenttriangle].index[1] = tri->index[1];
912 buf_tri[currenttriangle].index[2] = tri->index[2];
915 mesh->triangles = currenttriangle - mesh->firsttriangle;
918 currenttransmesh = 0;
919 currenttranstriangle = 0;
920 currenttransvertex = 0;
923 void R_Mesh_Draw(const rmeshinfo_t *m)
925 // these are static because gcc runs out of virtual registers otherwise
926 static int i, j, overbright, *index;
927 static float *in, scaler;
928 static float cr, cg, cb, ca;
929 static buf_mesh_t *mesh;
930 static buf_vertex_t *vert;
931 static buf_fcolor_t *fcolor;
932 static buf_texcoord_t *texcoord[MAX_TEXTUREUNITS];
935 Sys_Error("R_Mesh_Draw: called when backend is not active\n");
941 Host_Error("R_Mesh_Draw: no triangles or verts\n");
943 // ignore meaningless alpha meshs
944 if (!m->depthwrite && m->blendfunc1 == GL_SRC_ALPHA && (m->blendfunc2 == GL_ONE || m->blendfunc2 == GL_ONE_MINUS_SRC_ALPHA))
948 for (i = 0, in = m->color + 3;i < m->numverts;i++, (int)in += m->colorstep)
951 if (i == m->numverts)
954 else if (m->ca < 0.01f)
959 Sys_Error("R_Mesh_Draw: called when backend is not active\n");
962 for (i = 0;i < m->numtriangles * 3;i++)
963 if ((unsigned int) m->index[i] >= (unsigned int) m->numverts)
964 Host_Error("R_Mesh_Draw: invalid index (%i of %i verts)\n", m->index, m->numverts);
969 if (currenttransmesh >= max_meshs || (currenttranstriangle + m->numtriangles) > max_meshs || (currenttransvertex + m->numverts) > max_verts)
973 Con_Printf("R_Mesh_Draw: ran out of room for transparent meshs\n");
980 c_transtris += m->numtriangles;
981 vert = &buf_transvertex[currenttransvertex];
982 fcolor = &buf_transfcolor[currenttransvertex];
983 for (i = 0;i < backendunits;i++)
984 texcoord[i] = &buf_transtexcoord[i][currenttransvertex];
986 // transmesh is only for storage of transparent meshs until they
987 // are inserted into the main mesh array
988 mesh = &buf_transmesh[currenttransmesh++];
989 mesh->firsttriangle = currenttranstriangle;
990 mesh->firstvert = currenttransvertex;
991 index = &buf_transtri[currenttranstriangle].index[0];
993 currenttranstriangle += m->numtriangles;
994 currenttransvertex += m->numverts;
998 if (m->numtriangles > max_meshs || m->numverts > max_verts)
1000 Con_Printf("R_Mesh_Draw: mesh too big for buffers\n");
1004 if (currentmesh >= max_meshs || (currenttriangle + m->numtriangles) > max_batch || (currentvertex + m->numverts) > max_verts)
1008 c_meshtris += m->numtriangles;
1009 vert = &buf_vertex[currentvertex];
1010 fcolor = &buf_fcolor[currentvertex];
1011 for (i = 0;i < backendunits;i++)
1012 texcoord[i] = &buf_texcoord[i][currentvertex];
1014 // opaque meshs are rendered directly
1015 mesh = &buf_mesh[currentmesh++];
1016 mesh->firsttriangle = currenttriangle;
1017 mesh->firstvert = currentvertex;
1018 index = &buf_tri[currenttriangle].index[0];
1020 currenttriangle += m->numtriangles;
1021 currentvertex += m->numverts;
1024 // code shared for transparent and opaque meshs
1025 memcpy(index, m->index, sizeof(int[3]) * m->numtriangles);
1026 mesh->blendfunc1 = m->blendfunc1;
1027 mesh->blendfunc2 = m->blendfunc2;
1028 mesh->depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite);
1029 mesh->depthtest = !m->depthdisable;
1030 mesh->triangles = m->numtriangles;
1031 mesh->verts = m->numverts;
1035 if (m->blendfunc2 == GL_SRC_COLOR)
1037 if (m->blendfunc1 == GL_DST_COLOR) // 2x modulate with framebuffer
1044 overbright = gl_combine.integer;
1048 scaler *= overbrightscale;
1053 for (i = 0;i < backendunits;i++)
1055 if ((mesh->textures[i] = m->tex[i]))
1057 mesh->texturergbscale[i] = m->texrgbscale[i];
1058 if (mesh->texturergbscale[i] != 1 && mesh->texturergbscale[i] != 2 && mesh->texturergbscale[i] != 4)
1059 mesh->texturergbscale[i] = 1;
1061 if (overbright && j >= 0)
1062 mesh->texturergbscale[j] = 4;
1064 if (m->vertexstep != sizeof(buf_vertex_t))
1066 for (i = 0, in = m->vertex;i < m->numverts;i++, (int)in += m->vertexstep)
1068 vert[i].v[0] = in[0];
1069 vert[i].v[1] = in[1];
1070 vert[i].v[2] = in[2];
1074 memcpy(vert, m->vertex, m->numverts * sizeof(buf_vertex_t));
1078 for (i = 0, in = m->color;i < m->numverts;i++, (int)in += m->colorstep)
1080 fcolor[i].c[0] = in[0] * scaler;
1081 fcolor[i].c[1] = in[1] * scaler;
1082 fcolor[i].c[2] = in[2] * scaler;
1083 fcolor[i].c[3] = in[3];
1088 cr = m->cr * scaler;
1089 cg = m->cg * scaler;
1090 cb = m->cb * scaler;
1092 for (i = 0;i < m->numverts;i++)
1094 fcolor[i].c[0] = cr;
1095 fcolor[i].c[1] = cg;
1096 fcolor[i].c[2] = cb;
1097 fcolor[i].c[3] = ca;
1101 for (j = 0;j < MAX_TEXTUREUNITS && m->tex[j];j++)
1103 if (j >= backendunits)
1104 Sys_Error("R_Mesh_Draw: texture %i supplied when there are only %i texture units\n", j + 1, backendunits);
1105 if (m->texcoordstep[j] != sizeof(buf_texcoord_t))
1107 for (i = 0, in = m->texcoords[j];i < m->numverts;i++, (int)in += m->texcoordstep[j])
1109 texcoord[j][i].t[0] = in[0];
1110 texcoord[j][i].t[1] = in[1];
1114 memcpy(&texcoord[j][0].t[0], m->texcoords[j], m->numverts * sizeof(buf_texcoord_t));
1117 for (;j < backendunits;j++)
1118 memset(&texcoord[j][0].t[0], 0, m->numverts * sizeof(buf_texcoord_t));
1122 void R_Mesh_Draw_NativeOnly(const rmeshinfo_t *m)
1124 // these are static because gcc runs out of virtual registers otherwise
1125 static int i, j, overbright, *index;
1126 static float *in, scaler;
1127 static buf_mesh_t *mesh;
1128 static buf_vertex_t *vert;
1129 static buf_fcolor_t *fcolor;
1130 static buf_texcoord_t *texcoord[MAX_TEXTUREUNITS];
1133 Sys_Error("R_Mesh_Draw: called when backend is not active\n");
1135 if (m->index == NULL
1137 || m->vertex == NULL
1139 Host_Error("R_Mesh_Draw: no triangles or verts\n");
1141 // ignore meaningless alpha meshs
1142 if (!m->depthwrite && m->blendfunc1 == GL_SRC_ALPHA && (m->blendfunc2 == GL_ONE || m->blendfunc2 == GL_ONE_MINUS_SRC_ALPHA))
1146 for (i = 0, in = m->color + 3;i < m->numverts;i++, (int)in += m->colorstep)
1149 if (i == m->numverts)
1152 else if (m->ca < 0.01f)
1158 if (currenttransmesh >= max_meshs || (currenttranstriangle + m->numtriangles) > max_meshs || (currenttransvertex + m->numverts) > max_verts)
1162 Con_Printf("R_Mesh_Draw_NativeOnly: ran out of room for transparent meshs\n");
1169 c_transtris += m->numtriangles;
1170 vert = &buf_transvertex[currenttransvertex];
1171 fcolor = &buf_transfcolor[currenttransvertex];
1172 for (i = 0;i < backendunits;i++)
1173 texcoord[i] = &buf_transtexcoord[i][currenttransvertex];
1175 // transmesh is only for storage of transparent meshs until they
1176 // are inserted into the main mesh array
1177 mesh = &buf_transmesh[currenttransmesh++];
1178 mesh->firsttriangle = currenttranstriangle;
1179 mesh->firstvert = currenttransvertex;
1180 index = &buf_transtri[currenttranstriangle].index[0];
1181 currenttranstriangle += m->numtriangles;
1182 currenttransvertex += m->numverts;
1186 if (m->numtriangles > max_meshs || m->numverts > max_verts)
1188 Con_Printf("R_Mesh_Draw_NativeOnly: mesh too big for buffers\n");
1192 if (currentmesh >= max_meshs || (currenttriangle + m->numtriangles) > max_batch || (currentvertex + m->numverts) > max_verts)
1196 c_meshtris += m->numtriangles;
1197 vert = &buf_vertex[currentvertex];
1198 fcolor = &buf_fcolor[currentvertex];
1199 for (i = 0;i < backendunits;i++)
1200 texcoord[i] = &buf_texcoord[i][currentvertex];
1202 // opaque meshs are rendered directly
1203 mesh = &buf_mesh[currentmesh++];
1204 mesh->firsttriangle = currenttriangle;
1205 mesh->firstvert = currentvertex;
1206 index = &buf_tri[currenttriangle].index[0];
1207 currenttriangle += m->numtriangles;
1208 currentvertex += m->numverts;
1211 // code shared for transparent and opaque meshs
1212 memcpy(index, m->index, sizeof(int[3]) * m->numtriangles);
1213 mesh->blendfunc1 = m->blendfunc1;
1214 mesh->blendfunc2 = m->blendfunc2;
1215 mesh->depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite);
1216 mesh->depthtest = !m->depthdisable;
1217 mesh->triangles = m->numtriangles;
1218 mesh->verts = m->numverts;
1222 if (m->blendfunc2 == GL_SRC_COLOR)
1224 if (m->blendfunc1 == GL_DST_COLOR) // 2x modulate with framebuffer
1231 overbright = gl_combine.integer;
1235 scaler *= overbrightscale;
1239 for (i = 0;i < backendunits;i++)
1241 if ((mesh->textures[i] = m->tex[i]))
1243 mesh->texturergbscale[i] = m->texrgbscale[i];
1244 if (mesh->texturergbscale[i] != 1 && mesh->texturergbscale[i] != 2 && mesh->texturergbscale[i] != 4)
1245 mesh->texturergbscale[i] = 1;
1247 if (overbright && j >= 0)
1248 mesh->texturergbscale[j] = 4;
1250 if (m->vertexstep != sizeof(buf_vertex_t))
1251 Host_Error("R_Mesh_Draw_NativeOnly: unsupported vertexstep\n");
1252 if (m->colorstep != sizeof(buf_fcolor_t))
1253 Host_Error("R_Mesh_Draw_NativeOnly: unsupported colorstep\n");
1254 if (m->color == NULL)
1255 Host_Error("R_Mesh_Draw_NativeOnly: must provide color array\n");
1256 for (j = 0;j < MAX_TEXTUREUNITS && m->tex[j];j++)
1258 if (j >= backendunits)
1259 Sys_Error("R_Mesh_Draw_NativeOnly: texture %i supplied when there are only %i texture units\n", j + 1, backendunits);
1260 if (m->texcoordstep[j] != sizeof(buf_texcoord_t))
1261 Host_Error("R_Mesh_Draw_NativeOnly: unsupported texcoordstep\n");
1264 memcpy(vert, m->vertex, m->numverts * sizeof(buf_vertex_t));
1265 for (j = 0;j < MAX_TEXTUREUNITS && m->tex[j];j++)
1266 memcpy(&texcoord[j][0].t[0], m->texcoords[j], m->numverts * sizeof(buf_texcoord_t));
1268 for (;j < backendunits;j++)
1269 memset(&texcoord[j][0].t[0], 0, m->numverts * sizeof(buf_texcoord_t));
1272 memcpy(fcolor, m->color, m->numverts * sizeof(buf_fcolor_t));
1274 // do this as a second step because memcpy preloaded the cache, which we can't easily do
1277 for (i = 0;i < m->numverts;i++)
1279 fcolor[i].c[0] *= scaler;
1280 fcolor[i].c[1] *= scaler;
1281 fcolor[i].c[2] *= scaler;
1286 // allocates space in geometry buffers, and fills in pointers to the buffers in passsed struct
1287 // (this is used for very high speed rendering, no copying)
1288 int R_Mesh_Draw_GetBuffer(rmeshbufferinfo_t *m)
1290 // these are static because gcc runs out of virtual registers otherwise
1291 int i, j, overbright;
1296 Sys_Error("R_Mesh_Draw: called when backend is not active\n");
1298 if (!m->numtriangles
1300 Host_Error("R_Mesh_Draw: no triangles or verts\n");
1304 if (currenttransmesh >= max_meshs || (currenttranstriangle + m->numtriangles) > max_meshs || (currenttransvertex + m->numverts) > max_verts)
1308 Con_Printf("R_Mesh_Draw: ran out of room for transparent meshs\n");
1315 c_transtris += m->numtriangles;
1316 m->index = &buf_transtri[currenttranstriangle].index[0];
1317 m->vertex = &buf_transvertex[currenttransvertex].v[0];
1318 m->color = &buf_transfcolor[currenttransvertex].c[0];
1319 for (i = 0;i < backendunits;i++)
1320 m->texcoords[i] = &buf_transtexcoord[i][currenttransvertex].t[0];
1322 // transmesh is only for storage of transparent meshs until they
1323 // are inserted into the main mesh array
1324 mesh = &buf_transmesh[currenttransmesh++];
1325 mesh->firsttriangle = currenttranstriangle;
1326 mesh->firstvert = currenttransvertex;
1327 currenttranstriangle += m->numtriangles;
1328 currenttransvertex += m->numverts;
1332 if (m->numtriangles > max_meshs || m->numverts > max_verts)
1334 Con_Printf("R_Mesh_Draw_GetBuffer: mesh too big for buffers\n");
1338 if (currentmesh >= max_meshs || (currenttriangle + m->numtriangles) > max_batch || (currentvertex + m->numverts) > max_verts)
1342 c_meshtris += m->numtriangles;
1343 m->index = &buf_tri[currenttriangle].index[0];
1344 m->vertex = &buf_vertex[currentvertex].v[0];
1345 m->color = &buf_fcolor[currentvertex].c[0];
1346 for (i = 0;i < backendunits;i++)
1347 m->texcoords[i] = &buf_texcoord[i][currentvertex].t[0];
1349 // opaque meshs are rendered directly
1350 mesh = &buf_mesh[currentmesh++];
1351 mesh->firsttriangle = currenttriangle;
1352 mesh->firstvert = currentvertex;
1353 currenttriangle += m->numtriangles;
1354 currentvertex += m->numverts;
1357 // code shared for transparent and opaque meshs
1358 mesh->blendfunc1 = m->blendfunc1;
1359 mesh->blendfunc2 = m->blendfunc2;
1360 mesh->depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite);
1361 mesh->depthtest = !m->depthdisable;
1362 mesh->triangles = m->numtriangles;
1363 mesh->verts = m->numverts;
1367 if (m->blendfunc2 == GL_SRC_COLOR)
1369 if (m->blendfunc1 == GL_DST_COLOR) // 2x modulate with framebuffer
1376 overbright = gl_combine.integer;
1380 scaler *= overbrightscale;
1382 m->colorscale = scaler;
1385 for (i = 0;i < MAX_TEXTUREUNITS;i++)
1387 if ((mesh->textures[i] = m->tex[i]))
1390 if (i >= backendunits)
1391 Sys_Error("R_Mesh_Draw_GetBuffer: texture %i supplied when there are only %i texture units\n", j + 1, backendunits);
1393 mesh->texturergbscale[i] = m->texrgbscale[i];
1394 if (mesh->texturergbscale[i] != 1 && mesh->texturergbscale[i] != 2 && mesh->texturergbscale[i] != 4)
1395 mesh->texturergbscale[i] = 1;
1397 if (overbright && j >= 0)
1398 mesh->texturergbscale[j] = 4;
1403 void R_Mesh_DrawPolygon(rmeshinfo_t *m, int numverts)
1405 m->index = polyindexarray;
1406 m->numverts = numverts;
1407 m->numtriangles = numverts - 2;
1408 if (m->numtriangles < 1)
1410 Con_Printf("R_Mesh_DrawPolygon: invalid vertex count\n");
1413 if (m->numtriangles >= 256)
1415 Con_Printf("R_Mesh_DrawPolygon: only up to 256 triangles (258 verts) supported\n");
1422 ==============================================================================
1426 ==============================================================================
1429 qboolean SCR_ScreenShot(char *filename, int x, int y, int width, int height)
1435 if (!r_render.integer)
1438 buffer = Mem_Alloc(tempmempool, width*height*3);
1439 glReadPixels (x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer);
1442 // LordHavoc: compensate for v_overbrightbits when using hardware gamma
1443 if (v_hwgamma.integer)
1444 for (i = 0;i < width * height * 3;i++)
1445 buffer[i] <<= v_overbrightbits.integer;
1447 ret = Image_WriteTGARGB_preflipped(filename, width, height, buffer);
1453 //=============================================================================
1455 void R_ClearScreen(void)
1457 if (r_render.integer)
1460 glClearColor(0,0,0,0);CHECKGLERROR
1462 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);CHECKGLERROR
1463 // set dithering mode
1464 if (gl_dither.integer)
1466 glEnable(GL_DITHER);CHECKGLERROR
1470 glDisable(GL_DITHER);CHECKGLERROR
1479 This is called every frame, and can also be called explicitly to flush
1483 void SCR_UpdateScreen (void)
1485 //Mem_CheckSentinelsGlobal();
1486 //R_TimeReport("memtest");
1490 R_TimeReport("finish");
1492 if (gl_combine.integer && !gl_combine_extension)
1493 Cvar_SetValue("gl_combine", 0);
1495 lightscalebit = v_overbrightbits.integer;
1496 if (gl_combine.integer && r_multitexture.integer)
1499 lightscale = 1.0f / (float) (1 << lightscalebit);
1500 overbrightscale = 1.0f / (float) (1 << v_overbrightbits.integer);
1502 R_TimeReport("setup");
1506 R_TimeReport("clear");
1508 if (scr_conlines < vid.conheight)
1514 // tell driver to commit it's partially full geometry queue to the rendering queue
1515 // (this doesn't wait for the commands themselves to complete)