9 // 65536 is the max addressable on a Geforce 256 up until Geforce3
10 // (excluding MX), seems a reasonable number...
11 cvar_t gl_mesh_maxverts = {0, "gl_mesh_maxverts", "65536"};
12 cvar_t gl_mesh_floatcolors = {0, "gl_mesh_floatcolors", "1"};
13 cvar_t gl_mesh_drawrangeelements = {0, "gl_mesh_drawrangeelements", "1"};
15 cvar_t gl_mesh_vertex_array_range = {0, "gl_mesh_vertex_array_range", "0"};
16 cvar_t gl_mesh_vertex_array_range_readfrequency = {0, "gl_mesh_vertex_array_range_readfrequency", "0.2"};
17 cvar_t gl_mesh_vertex_array_range_writefrequency = {0, "gl_mesh_vertex_array_range_writefrequency", "0.2"};
18 cvar_t gl_mesh_vertex_array_range_priority = {0, "gl_mesh_vertex_array_range_priority", "0.7"};
21 cvar_t gl_mesh_batching = {0, "gl_mesh_batching", "1"};
23 cvar_t gl_mesh_copyarrays = {0, "gl_mesh_copyarrays", "1"};
24 cvar_t gl_delayfinish = {CVAR_SAVE, "gl_delayfinish", "0"};
26 cvar_t r_render = {0, "r_render", "1"};
27 cvar_t gl_dither = {CVAR_SAVE, "gl_dither", "1"}; // whether or not to use dithering
28 cvar_t gl_lockarrays = {0, "gl_lockarrays", "1"};
30 int gl_maxdrawrangeelementsvertices;
31 int gl_maxdrawrangeelementsindices;
36 void GL_PrintError(int errornumber, char *filename, int linenumber)
40 #ifdef GL_INVALID_ENUM
42 Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
45 #ifdef GL_INVALID_VALUE
46 case GL_INVALID_VALUE:
47 Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
50 #ifdef GL_INVALID_OPERATION
51 case GL_INVALID_OPERATION:
52 Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
55 #ifdef GL_STACK_OVERFLOW
56 case GL_STACK_OVERFLOW:
57 Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
60 #ifdef GL_STACK_UNDERFLOW
61 case GL_STACK_UNDERFLOW:
62 Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
65 #ifdef GL_OUT_OF_MEMORY
66 case GL_OUT_OF_MEMORY:
67 Con_Printf("GL_OUT_OF_MEMORY at %s:%i\n", filename, linenumber);
70 #ifdef GL_TABLE_TOO_LARGE
71 case GL_TABLE_TOO_LARGE:
72 Con_Printf("GL_TABLE_TOO_LARGE at %s:%i\n", filename, linenumber);
76 Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
82 #define BACKENDACTIVECHECK if (!backendactive) Sys_Error("GL backend function called when backend is not active\n");
84 int c_meshs, c_meshelements;
86 void SCR_ScreenShot_f (void);
88 // these are externally accessible
89 int r_lightmapscalebit;
91 GLfloat *varray_vertex3f, *varray_buf_vertex3f;
92 GLfloat *varray_color4f, *varray_buf_color4f;
93 GLfloat *varray_texcoord3f[MAX_TEXTUREUNITS], *varray_buf_texcoord3f[MAX_TEXTUREUNITS];
94 GLfloat *varray_texcoord2f[MAX_TEXTUREUNITS], *varray_buf_texcoord2f[MAX_TEXTUREUNITS];
95 static qbyte *varray_buf_color4b;
99 float mesh_var_readfrequency;
100 float mesh_var_writefrequency;
101 float mesh_var_priority;
103 int varray_offset = 0, varray_offsetnext = 0;
104 GLuint *varray_buf_elements3i;
105 int mesh_maxelements = 32768;
107 int gl_batchvertexfirst = 0;
108 int gl_batchvertexcount = 0;
109 int gl_batchelementcount = 0;
112 static matrix4x4_t backend_viewmatrix;
113 static matrix4x4_t backend_modelmatrix;
114 static matrix4x4_t backend_modelviewmatrix;
115 static matrix4x4_t backend_glmodelviewmatrix;
116 static matrix4x4_t backend_projectmatrix;
118 static int backendunits, backendactive;
119 static mempool_t *gl_backend_mempool;
122 note: here's strip order for a terrain row:
128 A0B, 01B, B1C, 12C, C2D, 23D, D3E, 34E
130 *elements++ = i + row;
132 *elements++ = i + row + 1;
135 *elements++ = i + row + 1;
138 void GL_Backend_AllocElementsArray(void)
140 if (varray_buf_elements3i)
141 Mem_Free(varray_buf_elements3i);
142 varray_buf_elements3i = Mem_Alloc(gl_backend_mempool, mesh_maxelements * sizeof(GLuint));
145 void GL_Backend_FreeElementArray(void)
147 if (varray_buf_elements3i)
148 Mem_Free(varray_buf_elements3i);
149 varray_buf_elements3i = NULL;
152 void GL_Backend_CheckCvars(void)
154 if (gl_mesh_maxverts.integer < 1024)
155 Cvar_SetValueQuick(&gl_mesh_maxverts, 1024);
156 if (gl_mesh_maxverts.integer > 65536)
157 Cvar_SetValueQuick(&gl_mesh_maxverts, 65536);
159 if (gl_mesh_vertex_array_range.integer && !gl_support_var)
160 Cvar_SetValueQuick(&gl_mesh_vertex_array_range, 0);
161 if (gl_mesh_vertex_array_range_readfrequency.value < 0)
162 Cvar_SetValueQuick(&gl_mesh_vertex_array_range_readfrequency, 0);
163 if (gl_mesh_vertex_array_range_readfrequency.value > 1)
164 Cvar_SetValueQuick(&gl_mesh_vertex_array_range_readfrequency, 1);
165 if (gl_mesh_vertex_array_range_writefrequency.value < 0)
166 Cvar_SetValueQuick(&gl_mesh_vertex_array_range_writefrequency, 0);
167 if (gl_mesh_vertex_array_range_writefrequency.value > 1)
168 Cvar_SetValueQuick(&gl_mesh_vertex_array_range_writefrequency, 1);
169 if (gl_mesh_vertex_array_range_priority.value < 0)
170 Cvar_SetValueQuick(&gl_mesh_vertex_array_range_priority, 0);
171 if (gl_mesh_vertex_array_range_priority.value > 1)
172 Cvar_SetValueQuick(&gl_mesh_vertex_array_range_priority, 1);
176 int polygonelements[768];
178 static void R_Mesh_CacheArray_Startup(void);
179 static void R_Mesh_CacheArray_Shutdown(void);
180 void GL_Backend_AllocArrays(void)
185 if (!gl_backend_mempool)
187 gl_backend_mempool = Mem_AllocPool("GL_Backend");
188 varray_buf_vertex3f = NULL;
189 varray_buf_color4f = NULL;
190 varray_buf_color4b = NULL;
191 varray_buf_elements3i = NULL;
192 for (i = 0;i < MAX_TEXTUREUNITS;i++)
193 varray_buf_texcoord3f[i] = varray_buf_texcoord2f[i] = NULL;
196 if (varray_buf_vertex3f)
198 VID_FreeVertexArrays(varray_buf_vertex3f);
200 Mem_Free(varray_buf_vertex3f);
202 varray_buf_vertex3f = NULL;
203 varray_buf_color4f = NULL;
204 varray_buf_color4b = NULL;
205 for (i = 0;i < MAX_TEXTUREUNITS;i++)
206 varray_buf_texcoord3f[i] = varray_buf_texcoord2f[i] = NULL;
208 mesh_maxverts = gl_mesh_maxverts.integer;
209 size = mesh_maxverts * (sizeof(float[3]) + sizeof(float[4]) + sizeof(qbyte[4]) + (sizeof(float[3]) + sizeof(float[2])) * backendunits);
211 mesh_var = gl_mesh_vertex_array_range.integer && gl_support_var;
212 mesh_var_readfrequency = gl_mesh_vertex_array_range_readfrequency.value;
213 mesh_var_writefrequency = gl_mesh_vertex_array_range_writefrequency.value;
214 mesh_var_priority = gl_mesh_vertex_array_range_priority.value;
215 data = VID_AllocVertexArrays(gl_backend_mempool, size, gl_mesh_vertex_array_range.integer, gl_mesh_vertex_array_range_readfrequency.value, gl_mesh_vertex_array_range_writefrequency.value, gl_mesh_vertex_array_range_priority.value);
217 data = Mem_Alloc(gl_backend_mempool, size);
220 varray_buf_vertex3f = (void *)data;data += sizeof(float[3]) * mesh_maxverts;
221 varray_buf_color4f = (void *)data;data += sizeof(float[4]) * mesh_maxverts;
222 for (i = 0;i < backendunits;i++)
224 varray_buf_texcoord3f[i] = (void *)data;data += sizeof(float[3]) * mesh_maxverts;
225 varray_buf_texcoord2f[i] = (void *)data;data += sizeof(float[2]) * mesh_maxverts;
227 for (;i < MAX_TEXTUREUNITS;i++)
228 varray_buf_texcoord3f[i] = varray_buf_texcoord2f[i] = NULL;
229 varray_buf_color4b = (void *)data;data += sizeof(qbyte[4]) * mesh_maxverts;
231 GL_Backend_AllocElementsArray();
237 qglVertexArrayRangeNV(size, varray_buf_vertex3f);
242 R_Mesh_CacheArray_Startup();
245 void GL_Backend_FreeArrays(void)
249 R_Mesh_CacheArray_Shutdown();
255 qglDisableClientState(GL_VERTEX_ARRAY_RANGE_NV);
260 if (varray_buf_vertex3f)
262 VID_FreeVertexArrays(varray_buf_vertex3f);
264 Mem_Free(varray_buf_vertex3f);
266 varray_buf_vertex3f = NULL;
267 varray_buf_color4f = NULL;
268 varray_buf_color4b = NULL;
269 for (i = 0;i < MAX_TEXTUREUNITS;i++)
270 varray_buf_texcoord3f[i] = varray_buf_texcoord2f[i] = NULL;
271 varray_buf_elements3i = NULL;
273 Mem_FreePool(&gl_backend_mempool);
276 static void gl_backend_start(void)
278 GL_Backend_CheckCvars();
280 Con_Printf("OpenGL Backend started with gl_mesh_maxverts %i\n", gl_mesh_maxverts.integer);
281 if (qglDrawRangeElements != NULL)
284 qglGetIntegerv(GL_MAX_ELEMENTS_VERTICES, &gl_maxdrawrangeelementsvertices);
286 qglGetIntegerv(GL_MAX_ELEMENTS_INDICES, &gl_maxdrawrangeelementsindices);
288 Con_Printf("glDrawRangeElements detected (max vertices %i, max indices %i)\n", gl_maxdrawrangeelementsvertices, gl_maxdrawrangeelementsindices);
290 if (strstr(gl_renderer, "3Dfx"))
292 Con_Printf("3Dfx driver detected, forcing gl_mesh_floatcolors to 0 to prevent crashs\n");
293 Cvar_SetValueQuick(&gl_mesh_floatcolors, 0);
296 backendunits = min(MAX_TEXTUREUNITS, gl_textureunits);
298 GL_Backend_AllocArrays();
304 qglEnableClientState(GL_VERTEX_ARRAY_RANGE_NV);
308 varray_offset = varray_offsetnext = 0;
310 gl_batchvertexfirst = 0;
311 gl_batchvertexcount = 0;
312 gl_batchelementcount = 0;
315 backendactive = true;
318 static void gl_backend_shutdown(void)
321 backendactive = false;
323 Con_Printf("OpenGL Backend shutting down\n");
329 qglDisableClientState(GL_VERTEX_ARRAY_RANGE_NV);
334 GL_Backend_FreeArrays();
337 void GL_Backend_ResizeArrays(int numvertices)
339 Cvar_SetValueQuick(&gl_mesh_maxverts, numvertices);
340 GL_Backend_CheckCvars();
341 mesh_maxverts = gl_mesh_maxverts.integer;
342 GL_Backend_AllocArrays();
345 static void gl_backend_newmap(void)
349 void gl_backend_init(void)
353 for (i = 0;i < POLYGONELEMENTS_MAXPOINTS - 2;i++)
355 polygonelements[i * 3 + 0] = 0;
356 polygonelements[i * 3 + 1] = i + 1;
357 polygonelements[i * 3 + 2] = i + 2;
360 Cvar_RegisterVariable(&r_render);
361 Cvar_RegisterVariable(&gl_dither);
362 Cvar_RegisterVariable(&gl_lockarrays);
363 Cvar_RegisterVariable(&gl_delayfinish);
365 Cvar_SetValue("r_render", 0);
368 Cvar_RegisterVariable(&gl_mesh_maxverts);
369 Cvar_RegisterVariable(&gl_mesh_floatcolors);
370 Cvar_RegisterVariable(&gl_mesh_drawrangeelements);
372 Cvar_RegisterVariable(&gl_mesh_vertex_array_range);
373 Cvar_RegisterVariable(&gl_mesh_vertex_array_range_readfrequency);
374 Cvar_RegisterVariable(&gl_mesh_vertex_array_range_writefrequency);
375 Cvar_RegisterVariable(&gl_mesh_vertex_array_range_priority);
378 Cvar_RegisterVariable(&gl_mesh_batching);
380 Cvar_RegisterVariable(&gl_mesh_copyarrays);
381 R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap);
384 void GL_SetupView_ViewPort (int x, int y, int width, int height)
386 if (!r_render.integer)
389 // y is weird beause OpenGL is bottom to top, we use top to bottom
390 qglViewport(x, vid.realheight - (y + height), width, height);
394 void GL_SetupView_Orientation_Identity (void)
396 Matrix4x4_CreateIdentity(&backend_viewmatrix);
397 memset(&backend_modelmatrix, 0, sizeof(backend_modelmatrix));
400 void GL_SetupView_Orientation_FromEntity (vec3_t origin, vec3_t angles)
402 Matrix4x4_CreateRotate(&backend_viewmatrix, -90, 1, 0, 0);
403 Matrix4x4_ConcatRotate(&backend_viewmatrix, 90, 0, 0, 1);
404 Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[2], 1, 0, 0);
405 Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[0], 0, 1, 0);
406 Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[1], 0, 0, 1);
407 Matrix4x4_ConcatTranslate(&backend_viewmatrix, -origin[0], -origin[1], -origin[2]);
408 memset(&backend_modelmatrix, 0, sizeof(backend_modelmatrix));
411 void GL_SetupView_Mode_Perspective (double fovx, double fovy, double zNear, double zFar)
415 if (!r_render.integer)
419 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
420 qglLoadIdentity();CHECKGLERROR
422 xmax = zNear * tan(fovx * M_PI / 360.0);
423 ymax = zNear * tan(fovy * M_PI / 360.0);
425 qglFrustum(-xmax, xmax, -ymax, ymax, zNear, zFar);CHECKGLERROR
426 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
427 GL_SetupView_Orientation_Identity();
430 void GL_SetupView_Mode_PerspectiveInfiniteFarClip (double fovx, double fovy, double zNear)
434 if (!r_render.integer)
438 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
439 qglLoadIdentity();CHECKGLERROR
441 nudge = 1.0 - 1.0 / (1<<23);
442 m[ 0] = 1.0 / tan(fovx * M_PI / 360.0);
447 m[ 5] = 1.0 / tan(fovy * M_PI / 360.0);
456 m[14] = -2 * zNear * nudge;
459 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
460 GL_SetupView_Orientation_Identity();
461 backend_projectmatrix.m[0][0] = m[0];
462 backend_projectmatrix.m[1][0] = m[1];
463 backend_projectmatrix.m[2][0] = m[2];
464 backend_projectmatrix.m[3][0] = m[3];
465 backend_projectmatrix.m[0][1] = m[4];
466 backend_projectmatrix.m[1][1] = m[5];
467 backend_projectmatrix.m[2][1] = m[6];
468 backend_projectmatrix.m[3][1] = m[7];
469 backend_projectmatrix.m[0][2] = m[8];
470 backend_projectmatrix.m[1][2] = m[9];
471 backend_projectmatrix.m[2][2] = m[10];
472 backend_projectmatrix.m[3][2] = m[11];
473 backend_projectmatrix.m[0][3] = m[12];
474 backend_projectmatrix.m[1][3] = m[13];
475 backend_projectmatrix.m[2][3] = m[14];
476 backend_projectmatrix.m[3][3] = m[15];
479 void GL_SetupView_Mode_Ortho (double x1, double y1, double x2, double y2, double zNear, double zFar)
481 if (!r_render.integer)
485 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
486 qglLoadIdentity();CHECKGLERROR
487 qglOrtho(x1, x2, y2, y1, zNear, zFar);
488 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
489 GL_SetupView_Orientation_Identity();
492 typedef struct gltextureunit_s
494 int t1d, t2d, t3d, tcubemap;
495 int arrayenabled, arrayis3d;
496 void *pointer_texcoord;
497 float rgbscale, alphascale;
498 int combinergb, combinealpha;
499 // FIXME: add more combine stuff
512 gltextureunit_t units[MAX_TEXTUREUNITS];
517 int pointervertexcount;
518 void *pointer_vertex;
523 void GL_SetupTextureState(void)
526 gltextureunit_t *unit;
527 for (i = 0;i < backendunits;i++)
529 if (qglActiveTexture)
530 qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
531 if (qglClientActiveTexture)
532 qglClientActiveTexture(GL_TEXTURE0_ARB + (gl_state.clientunit = i));CHECKGLERROR
533 unit = gl_state.units + i;
538 unit->arrayenabled = false;
539 unit->arrayis3d = false;
540 unit->pointer_texcoord = NULL;
542 unit->alphascale = 1;
543 unit->combinergb = GL_MODULATE;
544 unit->combinealpha = GL_MODULATE;
545 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
546 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), varray_buf_texcoord2f[i]);CHECKGLERROR
547 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
548 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
551 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
553 if (gl_texturecubemap)
555 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
557 if (gl_combine.integer)
559 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);CHECKGLERROR
560 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);CHECKGLERROR
561 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);CHECKGLERROR
562 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
563 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_CONSTANT_ARB);CHECKGLERROR
564 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
565 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
566 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);CHECKGLERROR
567 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);CHECKGLERROR
568 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);CHECKGLERROR
569 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
570 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, GL_CONSTANT_ARB);CHECKGLERROR
571 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
572 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
573 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
574 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
575 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
579 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
584 void GL_Backend_ResetState(void)
586 memset(&gl_state, 0, sizeof(gl_state));
587 gl_state.depthdisable = false;
588 gl_state.blendfunc1 = GL_ONE;
589 gl_state.blendfunc2 = GL_ZERO;
590 gl_state.blend = false;
591 gl_state.depthmask = GL_TRUE;
592 gl_state.colorarray = false;
593 gl_state.color4f[0] = gl_state.color4f[1] = gl_state.color4f[2] = gl_state.color4f[3] = 1;
594 gl_state.lockrange_first = 0;
595 gl_state.lockrange_count = 0;
596 gl_state.pointervertexcount = 0;
597 gl_state.pointer_vertex = NULL;
598 gl_state.pointer_color = NULL;
601 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
602 qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
604 qglEnable(GL_CULL_FACE);CHECKGLERROR
605 qglCullFace(GL_FRONT);CHECKGLERROR
606 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
607 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
608 qglDisable(GL_BLEND);CHECKGLERROR
609 qglDepthMask(gl_state.depthmask);CHECKGLERROR
610 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), varray_buf_vertex3f);CHECKGLERROR
611 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
612 if (gl_mesh_floatcolors.integer)
614 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), varray_buf_color4f);CHECKGLERROR
618 qglColorPointer(4, GL_UNSIGNED_BYTE, sizeof(qbyte[4]), varray_buf_color4b);CHECKGLERROR
620 GL_Color(0, 0, 0, 0);
621 GL_Color(1, 1, 1, 1);
623 GL_SetupTextureState();
626 void GL_UseColorArray(void)
628 if (!gl_state.colorarray)
631 if (gl_batchelementcount)
634 gl_state.colorarray = true;
635 qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
639 void GL_Color(float cr, float cg, float cb, float ca)
641 if (gl_state.colorarray)
644 if (gl_batchelementcount)
647 gl_state.colorarray = false;
648 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
649 gl_state.color4f[0] = cr;
650 gl_state.color4f[1] = cg;
651 gl_state.color4f[2] = cb;
652 gl_state.color4f[3] = ca;
653 qglColor4f(cr, cg, cb, ca);
657 if (gl_state.color4f[0] != cr || gl_state.color4f[1] != cg || gl_state.color4f[2] != cb || gl_state.color4f[3] != ca)
660 if (gl_batchelementcount)
663 gl_state.color4f[0] = cr;
664 gl_state.color4f[1] = cg;
665 gl_state.color4f[2] = cb;
666 gl_state.color4f[3] = ca;
667 qglColor4f(cr, cg, cb, ca);
672 void GL_LockArrays(int first, int count)
674 if (gl_state.lockrange_count != count || gl_state.lockrange_first != first)
676 if (gl_state.lockrange_count)
678 gl_state.lockrange_count = 0;
680 qglUnlockArraysEXT();
683 if (count && gl_supportslockarrays && gl_lockarrays.integer)
685 gl_state.lockrange_first = first;
686 gl_state.lockrange_count = count;
688 qglLockArraysEXT(first, count);
694 void GL_TransformToScreen(const vec4_t in, vec4_t out)
698 Matrix4x4_Transform4 (&backend_viewmatrix, in, temp);
699 Matrix4x4_Transform4 (&backend_projectmatrix, temp, out);
701 out[0] = r_refdef.x + (out[0] * iw + 1.0f) * r_refdef.width * 0.5f;
702 out[1] = r_refdef.y + (out[1] * iw + 1.0f) * r_refdef.height * 0.5f;
703 out[2] = out[2] * iw;
706 // called at beginning of frame
707 void R_Mesh_Start(void)
713 GL_Backend_CheckCvars();
714 if (mesh_maxverts != gl_mesh_maxverts.integer
716 || mesh_var != (gl_mesh_vertex_array_range.integer && gl_support_var)
717 || mesh_var_readfrequency != gl_mesh_vertex_array_range_readfrequency.value
718 || mesh_var_writefrequency != gl_mesh_vertex_array_range_writefrequency.value
719 || mesh_var_priority != gl_mesh_vertex_array_range_priority.value
722 GL_Backend_ResizeArrays(gl_mesh_maxverts.integer);
724 GL_Backend_ResetState();
728 gl_batchvertexfirst = gl_batchvertexcount = gl_batchelementcount = 0;
729 varray_offset = varray_offsetnext = 0;
732 varray_offset = varray_offsetnext = 0;
736 int gl_backend_rebindtextures;
738 void GL_ConvertColorsFloatToByte(int first, int count)
741 union {float f[4];int i[4];} *color4fi;
742 struct {GLubyte c[4];} *color4b;
744 // shift float to have 8bit fraction at base of number
745 color4fi = (void *)(varray_buf_color4f + first * 4);
746 for (i = 0;i < count;i++, color4fi++)
748 color4fi->f[0] += 32768.0f;
749 color4fi->f[1] += 32768.0f;
750 color4fi->f[2] += 32768.0f;
751 color4fi->f[3] += 32768.0f;
754 // then read as integer and kill float bits...
755 color4fi = (void *)(varray_buf_color4f + first * 4);
756 color4b = (void *)(varray_buf_color4b + first * 4);
757 for (i = 0;i < count;i++, color4fi++, color4b++)
759 k = color4fi->i[0] & 0x7FFFFF;color4b->c[0] = (GLubyte) min(k, 255);
760 k = color4fi->i[1] & 0x7FFFFF;color4b->c[1] = (GLubyte) min(k, 255);
761 k = color4fi->i[2] & 0x7FFFFF;color4b->c[2] = (GLubyte) min(k, 255);
762 k = color4fi->i[3] & 0x7FFFFF;color4b->c[3] = (GLubyte) min(k, 255);
767 // enlarges geometry buffers if they are too small
768 void _R_Mesh_ResizeCheck(int numverts)
770 if (numverts > mesh_maxverts)
773 GL_Backend_ResizeArrays(numverts + 100);
774 GL_Backend_ResetState();
779 void R_Mesh_EndBatch(void)
782 if (gl_batchelementcount)
784 if (gl_state.pointervertexcount)
785 Host_Error("R_Mesh_EndBatch: called with pointers enabled\n");
787 if (gl_state.colorarray && !gl_mesh_floatcolors.integer && gl_state.pointer_color == NULL)
788 GL_ConvertColorsFloatToByte(gl_batchvertexfirst, gl_batchvertexcount);
789 if (r_render.integer)
791 //int i;for (i = 0;i < gl_batchelementcount;i++) if (varray_buf_elements3i[i] < gl_batchvertexfirst || varray_buf_elements3i[i] >= (gl_batchvertexfirst + gl_batchvertexcount)) Host_Error("R_Mesh_EndBatch: invalid element #%i (value %i) outside range %i-%i\n", i, varray_buf_elements3i[i], gl_batchvertexfirst, gl_batchvertexfirst + gl_batchvertexcount);
793 GL_LockArrays(gl_batchvertexfirst, gl_batchvertexcount);
794 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
796 qglDrawRangeElements(GL_TRIANGLES, gl_batchvertexfirst, gl_batchvertexfirst + gl_batchvertexcount, gl_batchelementcount, GL_UNSIGNED_INT, (const GLuint *) varray_buf_elements3i);CHECKGLERROR
800 qglDrawElements(GL_TRIANGLES, gl_batchelementcount, GL_UNSIGNED_INT, (const GLuint *) varray_buf_elements3i);CHECKGLERROR
804 gl_batchelementcount = 0;
805 gl_batchvertexcount = 0;
810 void GL_Backend_RenumberElements(int *out, int count, const int *in, int offset)
814 for (i = 0;i < count;i++)
815 *out++ = *in++ + offset;
817 // memcpy(out, in, sizeof(*out) * count);
820 // gets vertex buffer space for use with a following R_Mesh_Draw
821 // (can be multiple Draw calls per GetSpace)
822 void R_Mesh_GetSpace(int numverts)
826 if (gl_state.pointervertexcount)
827 Host_Error("R_Mesh_GetSpace: called with pointers enabled\n");
828 if (gl_state.lockrange_count)
829 Host_Error("R_Mesh_GetSpace: called with arrays locked\n");
831 varray_offset = varray_offsetnext;
832 if (varray_offset + numverts > mesh_maxverts)
834 //Con_Printf("R_Mesh_GetSpace: vertex buffer wrap\n");
836 if (gl_batchelementcount)
844 qglFlushVertexArrayRangeNV();
848 if (numverts > mesh_maxverts)
850 GL_Backend_ResizeArrays(numverts + 100);
851 GL_Backend_ResetState();
857 if (gl_mesh_batching.integer)
859 if (gl_batchvertexcount == 0)
860 gl_batchvertexfirst = varray_offset;
861 gl_batchvertexcount += numverts;
865 varray_vertex3f = varray_buf_vertex3f + varray_offset * 3;
866 varray_color4f = varray_buf_color4f + varray_offset * 4;
867 for (i = 0;i < backendunits;i++)
869 varray_texcoord3f[i] = varray_buf_texcoord3f[i] + varray_offset * 3;
870 varray_texcoord2f[i] = varray_buf_texcoord2f[i] + varray_offset * 2;
873 varray_offsetnext = varray_offset + numverts;
876 // renders triangles using vertices from the most recent GetSpace call
877 // (can be multiple Draw calls per GetSpace)
878 void R_Mesh_Draw(int numverts, int numtriangles, const int *elements)
880 int numelements = numtriangles * 3;
881 if (numtriangles == 0 || numverts == 0)
883 Con_Printf("R_Mesh_Draw(%d, %d, %08p);\n", numverts, numtriangles, elements);
887 c_meshelements += numelements;
889 if (gl_state.pointervertexcount)
892 if (gl_batchelementcount)
895 if (r_render.integer)
897 GL_LockArrays(0, gl_state.pointervertexcount);
898 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
900 qglDrawRangeElements(GL_TRIANGLES, 0, gl_state.pointervertexcount, numelements, GL_UNSIGNED_INT, elements);CHECKGLERROR
904 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, elements);CHECKGLERROR
910 else if (gl_mesh_batching.integer)
912 if (mesh_maxelements < gl_batchelementcount + numelements)
914 //Con_Printf("R_Mesh_Draw: enlarging elements array\n");
915 if (gl_batchelementcount)
917 // round up to a multiple of 1024 and add another 1024 just for good measure
918 mesh_maxelements = (gl_batchelementcount + numelements + 1024 + 1023) & ~1023;
919 GL_Backend_AllocElementsArray();
921 if (varray_offset < gl_batchvertexfirst && gl_batchelementcount)
923 if (gl_batchelementcount == 0)
925 gl_batchvertexfirst = varray_offset;
926 gl_batchvertexcount = 0;
928 if (gl_batchvertexcount < varray_offsetnext - gl_batchvertexfirst)
929 gl_batchvertexcount = varray_offsetnext - gl_batchvertexfirst;
930 GL_Backend_RenumberElements(varray_buf_elements3i + gl_batchelementcount, numelements, elements, varray_offset);
931 //Con_Printf("off %i:%i, vertex %i:%i, element %i:%i\n", varray_offset, varray_offsetnext, gl_batchvertexfirst, gl_batchvertexfirst + gl_batchvertexcount, gl_batchelementcount, gl_batchelementcount + numelements);
932 gl_batchelementcount += numelements;
933 //{int i;for (i = 0;i < gl_batchelementcount;i++) if (varray_buf_elements3i[i] < gl_batchvertexfirst || varray_buf_elements3i[i] >= (gl_batchvertexfirst + gl_batchvertexcount)) Host_Error("R_Mesh_EndBatch: invalid element #%i (value %i) outside range %i-%i, there were previously %i elements and there are now %i elements, varray_offset is %i\n", i, varray_buf_elements3i[i], gl_batchvertexfirst, gl_batchvertexfirst + gl_batchvertexcount, gl_batchelementcount - numelements, gl_batchelementcount, varray_offset);}
938 GL_Backend_RenumberElements(varray_buf_elements3i, numelements, elements, varray_offset);
939 if (r_render.integer)
941 GL_LockArrays(varray_offset, numverts);
942 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
944 qglDrawRangeElements(GL_TRIANGLES, varray_offset, varray_offset + numverts, numelements, GL_UNSIGNED_INT, varray_buf_elements3i);CHECKGLERROR
948 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, varray_buf_elements3i);CHECKGLERROR
955 // restores backend state, used when done with 3D rendering
956 void R_Mesh_Finish(void)
961 if (gl_batchelementcount)
966 for (i = backendunits - 1;i >= 0;i--)
968 if (qglActiveTexture)
969 qglActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
970 if (qglClientActiveTexture)
971 qglClientActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
972 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
973 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
974 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
977 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
979 if (gl_texturecubemap)
981 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
983 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
984 if (gl_combine.integer)
986 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
987 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
990 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
991 qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
993 qglDisable(GL_BLEND);CHECKGLERROR
994 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
995 qglDepthMask(GL_TRUE);CHECKGLERROR
996 qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);CHECKGLERROR
999 void R_Mesh_Matrix(const matrix4x4_t *matrix)
1001 if (memcmp(matrix, &backend_modelmatrix, sizeof(matrix4x4_t)))
1004 if (gl_batchelementcount)
1007 backend_modelmatrix = *matrix;
1008 Matrix4x4_Concat(&backend_modelviewmatrix, &backend_viewmatrix, matrix);
1009 Matrix4x4_Transpose(&backend_glmodelviewmatrix, &backend_modelviewmatrix);
1010 qglLoadMatrixf(&backend_glmodelviewmatrix.m[0][0]);
1014 // sets up the requested state
1015 void R_Mesh_MainState(const rmeshstate_t *m)
1020 if (gl_state.blendfunc1 != m->blendfunc1 || gl_state.blendfunc2 != m->blendfunc2)
1023 if (gl_batchelementcount)
1026 qglBlendFunc(gl_state.blendfunc1 = m->blendfunc1, gl_state.blendfunc2 = m->blendfunc2);CHECKGLERROR
1027 if (gl_state.blendfunc2 == GL_ZERO)
1029 if (gl_state.blendfunc1 == GL_ONE)
1034 qglDisable(GL_BLEND);CHECKGLERROR
1039 if (!gl_state.blend)
1042 qglEnable(GL_BLEND);CHECKGLERROR
1048 if (!gl_state.blend)
1051 qglEnable(GL_BLEND);CHECKGLERROR
1055 if (gl_state.depthdisable != m->depthdisable)
1058 if (gl_batchelementcount)
1061 gl_state.depthdisable = m->depthdisable;
1062 if (gl_state.depthdisable)
1063 qglDisable(GL_DEPTH_TEST);
1065 qglEnable(GL_DEPTH_TEST);
1067 if (gl_state.depthmask != (m->blendfunc2 == GL_ZERO || m->depthwrite))
1070 if (gl_batchelementcount)
1073 qglDepthMask(gl_state.depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite));CHECKGLERROR
1076 if (gl_state.pointervertexcount != m->pointervertexcount)
1079 if (gl_batchelementcount)
1082 gl_state.pointervertexcount = m->pointervertexcount;
1085 p = gl_state.pointervertexcount ? m->pointer_vertex : NULL;
1086 if (gl_state.pointer_vertex != p)
1089 if (gl_batchelementcount)
1092 gl_state.pointer_vertex = p;
1093 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), p ? p : varray_buf_vertex3f);CHECKGLERROR
1096 if (gl_state.colorarray)
1098 p = gl_state.pointervertexcount ? m->pointer_color : NULL;
1099 if (gl_state.pointer_color != p)
1102 if (gl_batchelementcount)
1105 gl_state.pointer_color = p;
1106 if (p || gl_mesh_floatcolors.integer)
1107 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), p ? p : varray_buf_color4f);
1109 qglColorPointer(4, GL_UNSIGNED_BYTE, sizeof(GLubyte[4]), p ? p : varray_buf_color4b);
1115 void R_Mesh_TextureState(const rmeshstate_t *m)
1117 int i, combinergb, combinealpha;
1119 gltextureunit_t *unit;
1124 if (gl_backend_rebindtextures)
1126 gl_backend_rebindtextures = false;
1127 GL_SetupTextureState();
1130 for (i = 0;i < backendunits;i++)
1132 unit = gl_state.units + i;
1133 if (unit->t1d != m->tex1d[i] || unit->t2d != m->tex[i] || unit->t3d != m->tex3d[i] || unit->tcubemap != m->texcubemap[i])
1135 if (m->tex3d[i] || m->texcubemap[i])
1137 if (!unit->arrayis3d)
1140 if (gl_batchelementcount)
1143 unit->arrayis3d = true;
1144 if (gl_state.clientunit != i)
1146 qglClientActiveTexture(GL_TEXTURE0_ARB + (gl_state.clientunit = i));CHECKGLERROR
1148 qglTexCoordPointer(3, GL_FLOAT, sizeof(float[3]), varray_buf_texcoord3f[i]);
1150 if (!unit->arrayenabled)
1153 if (gl_batchelementcount)
1156 unit->arrayenabled = true;
1157 if (gl_state.clientunit != i)
1159 qglClientActiveTexture(GL_TEXTURE0_ARB + (gl_state.clientunit = i));CHECKGLERROR
1161 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1164 else if (m->tex1d[i] || m->tex[i])
1166 if (unit->arrayis3d)
1169 if (gl_batchelementcount)
1172 unit->arrayis3d = false;
1173 if (gl_state.clientunit != i)
1175 qglClientActiveTexture(GL_TEXTURE0_ARB + (gl_state.clientunit = i));CHECKGLERROR
1177 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), varray_buf_texcoord2f[i]);
1179 if (!unit->arrayenabled)
1182 if (gl_batchelementcount)
1185 unit->arrayenabled = true;
1186 if (gl_state.clientunit != i)
1188 qglClientActiveTexture(GL_TEXTURE0_ARB + (gl_state.clientunit = i));CHECKGLERROR
1190 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1195 if (unit->arrayenabled)
1198 if (gl_batchelementcount)
1201 unit->arrayenabled = false;
1202 if (gl_state.clientunit != i)
1204 qglClientActiveTexture(GL_TEXTURE0_ARB + (gl_state.clientunit = i));CHECKGLERROR
1206 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1209 if (unit->t1d != m->tex1d[i])
1212 if (gl_batchelementcount)
1215 if (gl_state.unit != i)
1217 qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
1222 qglEnable(GL_TEXTURE_1D);CHECKGLERROR
1227 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1229 qglBindTexture(GL_TEXTURE_1D, (unit->t1d = m->tex1d[i]));CHECKGLERROR
1231 if (unit->t2d != m->tex[i])
1234 if (gl_batchelementcount)
1237 if (gl_state.unit != i)
1239 qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
1244 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
1249 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1251 qglBindTexture(GL_TEXTURE_2D, (unit->t2d = m->tex[i]));CHECKGLERROR
1253 if (unit->t3d != m->tex3d[i])
1256 if (gl_batchelementcount)
1259 if (gl_state.unit != i)
1261 qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
1266 qglEnable(GL_TEXTURE_3D);CHECKGLERROR
1271 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1273 qglBindTexture(GL_TEXTURE_3D, (unit->t3d = m->tex3d[i]));CHECKGLERROR
1275 if (unit->tcubemap != m->texcubemap[i])
1278 if (gl_batchelementcount)
1281 if (gl_state.unit != i)
1283 qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
1285 if (m->texcubemap[i])
1287 if (unit->tcubemap == 0)
1288 qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1293 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1295 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, (unit->tcubemap = m->texcubemap[i]));CHECKGLERROR
1298 combinergb = m->texcombinergb[i];
1300 combinergb = GL_MODULATE;
1301 if (unit->combinergb != combinergb)
1304 if (gl_batchelementcount)
1307 if (gl_state.unit != i)
1309 qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
1311 unit->combinergb = combinergb;
1312 if (gl_combine.integer)
1314 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
1318 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combinergb);CHECKGLERROR
1321 combinealpha = m->texcombinealpha[i];
1323 combinealpha = GL_MODULATE;
1324 if (unit->combinealpha != combinealpha)
1327 if (gl_batchelementcount)
1330 if (gl_state.unit != i)
1332 qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
1334 unit->combinealpha = combinealpha;
1335 if (gl_combine.integer)
1337 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
1340 scale = max(m->texrgbscale[i], 1);
1341 if (gl_state.units[i].rgbscale != scale)
1344 if (gl_batchelementcount)
1347 if (gl_state.unit != i)
1349 qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
1351 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (gl_state.units[i].rgbscale = scale));CHECKGLERROR
1353 scale = max(m->texalphascale[i], 1);
1354 if (gl_state.units[i].alphascale != scale)
1357 if (gl_batchelementcount)
1360 if (gl_state.unit != i)
1362 qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
1364 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, (gl_state.units[i].alphascale = scale));CHECKGLERROR
1366 if (unit->arrayenabled)
1368 p = gl_state.pointervertexcount ? m->pointer_texcoord[i] : NULL;
1369 if (unit->pointer_texcoord != p)
1372 if (gl_batchelementcount)
1375 unit->pointer_texcoord = p;
1376 if (gl_state.clientunit != i)
1378 qglClientActiveTexture(GL_TEXTURE0_ARB + (gl_state.clientunit = i));CHECKGLERROR
1380 if (unit->arrayis3d)
1381 qglTexCoordPointer(3, GL_FLOAT, sizeof(float[3]), p ? p : varray_buf_texcoord3f[i]);
1383 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), p ? p : varray_buf_texcoord2f[i]);
1390 void R_Mesh_State(const rmeshstate_t *m)
1392 R_Mesh_MainState(m);
1393 R_Mesh_TextureState(m);
1397 ==============================================================================
1401 ==============================================================================
1404 qboolean SCR_ScreenShot(char *filename, int x, int y, int width, int height, qboolean jpeg)
1410 if (!r_render.integer)
1413 buffer = Mem_Alloc(tempmempool, width*height*3);
1414 qglReadPixels (x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer);
1417 // LordHavoc: compensate for v_overbrightbits when using hardware gamma
1418 if (v_hwgamma.integer)
1420 for (i = 0;i < width * height * 3;i++)
1422 j = buffer[i] << v_overbrightbits.integer;
1423 buffer[i] = (qbyte) (bound(0, j, 255));
1428 ret = JPEG_SaveImage_preflipped (filename, width, height, buffer);
1430 ret = Image_WriteTGARGB_preflipped (filename, width, height, buffer);
1436 //=============================================================================
1438 void R_ClearScreen(void)
1440 if (r_render.integer)
1443 qglClearColor(0,0,0,0);CHECKGLERROR
1444 qglClearDepth(1);CHECKGLERROR
1447 // LordHavoc: we use a stencil centered around 128 instead of 0,
1448 // to avoid clamping interfering with strange shadow volume
1450 qglClearStencil(128);CHECKGLERROR
1453 qglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | (gl_stencil ? GL_STENCIL_BUFFER_BIT : 0));CHECKGLERROR
1454 // set dithering mode
1455 if (gl_dither.integer)
1457 qglEnable(GL_DITHER);CHECKGLERROR
1461 qglDisable(GL_DITHER);CHECKGLERROR
1470 This is called every frame, and can also be called explicitly to flush
1474 void SCR_UpdateScreen (void)
1476 if (gl_delayfinish.integer)
1480 R_TimeReport("finish");
1483 if (r_textureunits.integer > gl_textureunits)
1484 Cvar_SetValueQuick(&r_textureunits, gl_textureunits);
1485 if (r_textureunits.integer < 1)
1486 Cvar_SetValueQuick(&r_textureunits, 1);
1488 if (gl_combine.integer && (!gl_combine_extension || r_textureunits.integer < 2))
1489 Cvar_SetValueQuick(&gl_combine, 0);
1492 r_colorscale = 1.0f / (float) (1 << v_overbrightbits.integer);
1495 r_lightmapscalebit = v_overbrightbits.integer;
1496 if (gl_combine.integer && r_textureunits.integer > 1)
1497 r_lightmapscalebit += 2;
1499 R_TimeReport("setup");
1503 R_TimeReport("clear");
1505 if (scr_conlines < vid.conheight && cls.signon == SIGNONS)
1511 if (gl_delayfinish.integer)
1513 // tell driver to commit it's partially full geometry queue to the rendering queue
1514 // (this doesn't wait for the commands themselves to complete)
1521 R_TimeReport("finish");
1525 // utility functions
1527 void R_Mesh_CopyVertex3f(const float *vertex3f, int numverts)
1532 float *out = varray_vertex3f;
1535 *out++ = *vertex3f++;
1536 *out++ = *vertex3f++;
1537 *out++ = *vertex3f++;
1542 memcpy(varray_vertex3f, vertex3f, numverts * sizeof(float[3]));
1545 void R_Mesh_CopyTexCoord2f(int tmu, const float *texcoord2f, int numverts)
1550 float *out = varray_texcoord2f[tmu];
1553 *out++ = *texcoord2f++;
1554 *out++ = *texcoord2f++;
1559 memcpy(varray_texcoord2f[tmu], texcoord2f, numverts * sizeof(float[2]));
1562 void R_Mesh_CopyColor4f(const float *color4f, int numverts)
1567 float *out = varray_color4f;
1570 *out++ = *color4f++;
1571 *out++ = *color4f++;
1572 *out++ = *color4f++;
1573 *out++ = *color4f++;
1578 memcpy(varray_color4f, color4f, numverts * sizeof(float[4]));
1581 //===========================================================================
1582 // vertex array caching subsystem
1583 //===========================================================================
1585 typedef struct rcachearraylink_s
1587 struct rcachearraylink_s *next, *prev;
1588 struct rcachearrayitem_s *data;
1592 typedef struct rcachearrayitem_s
1594 // the original request structure
1595 rcachearrayrequest_t request;
1598 // offset into r_mesh_rcachedata
1600 // for linking this into the sequential list
1601 rcachearraylink_t sequentiallink;
1602 // for linking this into the lookup list
1603 rcachearraylink_t hashlink;
1607 #define RCACHEARRAY_HASHSIZE 65536
1608 #define RCACHEARRAY_ITEMS 4096
1609 #define RCACHEARRAY_DEFAULTSIZE (4 << 20)
1611 // all active items are linked into this chain in sorted order
1612 static rcachearraylink_t r_mesh_rcachesequentialchain;
1613 // all inactive items are linked into this chain in unknown order
1614 static rcachearraylink_t r_mesh_rcachefreechain;
1615 // all active items are also linked into these chains (using their hashlink)
1616 static rcachearraylink_t r_mesh_rcachechain[RCACHEARRAY_HASHSIZE];
1618 // all items are stored here, whether active or inactive
1619 static rcachearrayitem_t r_mesh_rcacheitems[RCACHEARRAY_ITEMS];
1621 // size of data buffer
1622 static int r_mesh_rcachedata_size = RCACHEARRAY_DEFAULTSIZE;
1624 static qbyte r_mesh_rcachedata[RCACHEARRAY_DEFAULTSIZE];
1627 static int r_mesh_rcachedata_offset;
1628 static rcachearraylink_t *r_mesh_rcachesequentialchain_current;
1630 static void R_Mesh_CacheArray_Startup(void)
1633 rcachearraylink_t *l;
1634 // prepare all the linked lists
1635 l = &r_mesh_rcachesequentialchain;l->next = l->prev = l;l->data = NULL;
1636 l = &r_mesh_rcachefreechain;l->next = l->prev = l;l->data = NULL;
1637 memset(&r_mesh_rcachechain, 0, sizeof(r_mesh_rcachechain));
1638 for (i = 0;i < RCACHEARRAY_HASHSIZE;i++)
1640 l = &r_mesh_rcachechain[i];
1641 l->next = l->prev = l;
1644 memset(&r_mesh_rcacheitems, 0, sizeof(r_mesh_rcacheitems));
1645 for (i = 0;i < RCACHEARRAY_ITEMS;i++)
1647 r_mesh_rcacheitems[i].hashlink.data = r_mesh_rcacheitems[i].sequentiallink.data = &r_mesh_rcacheitems[i];
1648 l = &r_mesh_rcacheitems[i].sequentiallink;
1649 l->next = &r_mesh_rcachefreechain;
1650 l->prev = l->next->prev;
1651 l->next->prev = l->prev->next = l;
1653 // clear other state
1654 r_mesh_rcachedata_offset = 0;
1655 r_mesh_rcachesequentialchain_current = &r_mesh_rcachesequentialchain;
1658 static void R_Mesh_CacheArray_Shutdown(void)
1663 static void R_Mesh_CacheArray_ValidateState(int num)
1665 rcachearraylink_t *l, *lhead;
1666 lhead = &r_mesh_rcachesequentialchain;
1667 if (r_mesh_rcachesequentialchain_current == lhead)
1669 for (l = lhead->next;l != lhead;l = l->next)
1670 if (r_mesh_rcachesequentialchain_current == l)
1672 Sys_Error("%i", num);
1676 int R_Mesh_CacheArray(rcachearrayrequest_t *r)
1678 rcachearraylink_t *l, *lhead, *lnext;
1679 rcachearrayitem_t *d;
1680 int hashindex, offset, offsetend;
1682 //R_Mesh_CacheArray_ValidateState(3);
1683 // calculate a hashindex to choose a cache chain
1685 hashindex = CRC_Block((void *)r, sizeof(*r)) % RCACHEARRAY_HASHSIZE;
1687 // is it already cached?
1688 for (lhead = &r_mesh_rcachechain[hashindex], l = lhead->next;l != lhead;l = l->next)
1690 if (!memcmp(&l->data->request, r, sizeof(l->data->request)))
1692 // we have it cached already
1693 r->data = r_mesh_rcachedata + l->data->offset;
1698 // we need to add a new cache item, this means finding a place for the new
1699 // data and making sure we have a free item available, lots of work...
1701 // check if buffer needs to wrap
1702 if (r_mesh_rcachedata_offset + r->data_size > r_mesh_rcachedata_size)
1705 if (r->data_size * 10 > r_mesh_rcachedata_size)
1707 // realloc whole cache
1710 // reset back to start
1711 r_mesh_rcachedata_offset = 0;
1712 r_mesh_rcachesequentialchain_current = &r_mesh_rcachesequentialchain;
1714 offset = r_mesh_rcachedata_offset;
1715 r_mesh_rcachedata_offset += r->data_size;
1716 offsetend = r_mesh_rcachedata_offset;
1717 //R_Mesh_CacheArray_ValidateState(4);
1722 for (lhead = &r_mesh_rcachesequentialchain, l = lhead->next, n = 0;l != lhead;l = l->next, n++);
1723 Con_Printf("R_Mesh_CacheArray: new data range %i:%i, %i items are already linked\n", offset, offsetend, n);
1727 // make room for the new data (remove old items)
1728 lhead = &r_mesh_rcachesequentialchain;
1729 l = r_mesh_rcachesequentialchain_current;
1734 while (l->data->offset < offsetend && l->data->offset + l->data->request.data_size > offset)
1736 //r_mesh_rcachesequentialchain_current = l;
1737 //R_Mesh_CacheArray_ValidateState(8);
1739 // if at the end of the chain, wrap around
1741 lnext = lnext->next;
1742 //r_mesh_rcachesequentialchain_current = lnext;
1743 //R_Mesh_CacheArray_ValidateState(10);
1745 // unlink from sequential chain
1746 l->next->prev = l->prev;
1747 l->prev->next = l->next;
1748 //R_Mesh_CacheArray_ValidateState(11);
1749 // link into free chain
1750 l->next = &r_mesh_rcachefreechain;
1751 l->prev = l->next->prev;
1752 l->next->prev = l->prev->next = l;
1753 //R_Mesh_CacheArray_ValidateState(12);
1755 l = &l->data->hashlink;
1756 // unlink from hash chain
1757 l->next->prev = l->prev;
1758 l->prev->next = l->next;
1761 //r_mesh_rcachesequentialchain_current = l;
1762 //R_Mesh_CacheArray_ValidateState(9);
1765 //r_mesh_rcachesequentialchain_current = l;
1766 //R_Mesh_CacheArray_ValidateState(5);
1767 // gobble an extra item if we have no free items available
1768 if (r_mesh_rcachefreechain.next == &r_mesh_rcachefreechain)
1772 // unlink from sequential chain
1773 l->next->prev = l->prev;
1774 l->prev->next = l->next;
1775 // link into free chain
1776 l->next = &r_mesh_rcachefreechain;
1777 l->prev = l->next->prev;
1778 l->next->prev = l->prev->next = l;
1780 l = &l->data->hashlink;
1781 // unlink from hash chain
1782 l->next->prev = l->prev;
1783 l->prev->next = l->next;
1787 r_mesh_rcachesequentialchain_current = l;
1788 //R_Mesh_CacheArray_ValidateState(6);
1790 // now take an item from the free chain
1791 l = r_mesh_rcachefreechain.next;
1797 l->next->prev = l->prev;
1798 l->prev->next = l->next;
1799 // relink to sequential
1800 l->next = r_mesh_rcachesequentialchain_current->prev;
1801 l->prev = l->next->prev;
1802 while (l->next->data && l->data && l->next->data->offset <= d->offset)
1804 //Con_Printf(">\n");
1805 l->next = l->next->next;
1806 l->prev = l->prev->next;
1808 while (l->prev->data && l->data && l->prev->data->offset >= d->offset)
1810 //Con_Printf("<\n");
1811 l->prev = l->prev->prev;
1812 l->next = l->next->prev;
1814 l->next->prev = l->prev->next = l;
1815 // also link into hash chain
1816 l = &l->data->hashlink;
1817 l->next = &r_mesh_rcachechain[hashindex];
1818 l->prev = l->next->prev;
1820 l->next->prev = l->prev->next = l;
1823 //r_mesh_rcachesequentialchain_current = d->sequentiallink.next;
1825 //R_Mesh_CacheArray_ValidateState(7);
1826 // and finally set the data pointer
1827 r->data = r_mesh_rcachedata + d->offset;
1828 // and tell the caller to fill the array