5 cvar_t gl_mesh_maxverts = {0, "gl_mesh_maxverts", "1024"};
6 cvar_t gl_mesh_floatcolors = {0, "gl_mesh_floatcolors", "1"};
7 cvar_t gl_mesh_drawrangeelements = {0, "gl_mesh_drawrangeelements", "1"};
9 cvar_t r_render = {0, "r_render", "1"};
10 cvar_t gl_dither = {CVAR_SAVE, "gl_dither", "1"}; // whether or not to use dithering
11 cvar_t gl_lockarrays = {0, "gl_lockarrays", "1"};
13 int gl_maxdrawrangeelementsvertices;
14 int gl_maxdrawrangeelementsindices;
19 void GL_PrintError(int errornumber, char *filename, int linenumber)
23 #ifdef GL_INVALID_ENUM
25 Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
28 #ifdef GL_INVALID_VALUE
29 case GL_INVALID_VALUE:
30 Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
33 #ifdef GL_INVALID_OPERATION
34 case GL_INVALID_OPERATION:
35 Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
38 #ifdef GL_STACK_OVERFLOW
39 case GL_STACK_OVERFLOW:
40 Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
43 #ifdef GL_STACK_UNDERFLOW
44 case GL_STACK_UNDERFLOW:
45 Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
48 #ifdef GL_OUT_OF_MEMORY
49 case GL_OUT_OF_MEMORY:
50 Con_Printf("GL_OUT_OF_MEMORY at %s:%i\n", filename, linenumber);
53 #ifdef GL_TABLE_TOO_LARGE
54 case GL_TABLE_TOO_LARGE:
55 Con_Printf("GL_TABLE_TOO_LARGE at %s:%i\n", filename, linenumber);
59 Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
65 #define BACKENDACTIVECHECK if (!backendactive) Sys_Error("GL backend function called when backend is not active\n");
67 int c_meshs, c_meshelements;
69 void SCR_ScreenShot_f (void);
71 // these are externally accessible
72 int r_lightmapscalebit;
76 float *varray_texcoord[MAX_TEXTUREUNITS];
79 static matrix4x4_t backend_viewmatrix;
80 static matrix4x4_t backend_modelmatrix;
81 static matrix4x4_t backend_modelviewmatrix;
82 static matrix4x4_t backend_glmodelviewmatrix;
83 static matrix4x4_t backend_projectmatrix;
85 static int backendunits, backendactive;
86 static qbyte *varray_bcolor;
87 static mempool_t *gl_backend_mempool;
90 note: here's strip order for a terrain row:
96 A0B, 01B, B1C, 12C, C2D, 23D, D3E, 34E
98 *elements++ = i + row;
100 *elements++ = i + row + 1;
103 *elements++ = i + row + 1;
106 int polygonelements[768];
108 void GL_Backend_AllocArrays(void)
112 for (i = 0;i < POLYGONELEMENTS_MAXPOINTS - 2;i++)
114 polygonelements[i * 3 + 0] = 0;
115 polygonelements[i * 3 + 1] = i + 1;
116 polygonelements[i * 3 + 2] = i + 2;
119 if (!gl_backend_mempool)
120 gl_backend_mempool = Mem_AllocPool("GL_Backend");
122 varray_vertex = Mem_Alloc(gl_backend_mempool, mesh_maxverts * sizeof(float[4]));
123 varray_color = Mem_Alloc(gl_backend_mempool, mesh_maxverts * sizeof(float[4]));
124 varray_bcolor = Mem_Alloc(gl_backend_mempool, mesh_maxverts * sizeof(qbyte[4]));
125 for (i = 0;i < backendunits;i++)
126 varray_texcoord[i] = Mem_Alloc(gl_backend_mempool, mesh_maxverts * sizeof(float[4]));
127 for (;i < MAX_TEXTUREUNITS;i++)
128 varray_texcoord[i] = NULL;
131 void GL_Backend_FreeArrays(int resizingbuffers)
135 Mem_EmptyPool(gl_backend_mempool);
137 Mem_FreePool(&gl_backend_mempool);
138 varray_vertex = NULL;
140 varray_bcolor = NULL;
141 for (i = 0;i < MAX_TEXTUREUNITS;i++)
142 varray_texcoord[i] = NULL;
145 static void gl_backend_start(void)
147 Con_Printf("OpenGL Backend started with gl_mesh_maxverts %i\n", gl_mesh_maxverts.integer);
148 if (qglDrawRangeElements != NULL)
150 qglGetIntegerv(GL_MAX_ELEMENTS_VERTICES, &gl_maxdrawrangeelementsvertices);
151 qglGetIntegerv(GL_MAX_ELEMENTS_INDICES, &gl_maxdrawrangeelementsindices);
153 Con_Printf("glDrawRangeElements detected (max vertices %i, max indices %i)\n", gl_maxdrawrangeelementsvertices, gl_maxdrawrangeelementsindices);
155 if (strstr(gl_renderer, "3Dfx"))
157 Con_Printf("3Dfx driver detected, forcing gl_mesh_floatcolors to 0 to prevent crashs\n");
158 Cvar_SetValueQuick(&gl_mesh_floatcolors, 0);
161 backendunits = min(MAX_TEXTUREUNITS, gl_textureunits);
163 GL_Backend_AllocArrays();
165 backendactive = true;
168 static void gl_backend_shutdown(void)
171 backendactive = false;
173 Con_Printf("OpenGL Backend shutting down\n");
175 GL_Backend_FreeArrays(false);
178 void GL_Backend_CheckCvars(void)
180 // 21760 is (65536 / 3) rounded off to a multiple of 128
181 if (gl_mesh_maxverts.integer < 1024)
182 Cvar_SetValueQuick(&gl_mesh_maxverts, 1024);
183 if (gl_mesh_maxverts.integer > 21760)
184 Cvar_SetValueQuick(&gl_mesh_maxverts, 21760);
187 void GL_Backend_ResizeArrays(int numtriangles)
189 Cvar_SetValueQuick(&gl_mesh_maxverts, numtriangles);
190 GL_Backend_CheckCvars();
191 mesh_maxverts = gl_mesh_maxverts.integer;
192 GL_Backend_FreeArrays(true);
193 GL_Backend_AllocArrays();
196 static void gl_backend_newmap(void)
200 void gl_backend_init(void)
202 Cvar_RegisterVariable(&r_render);
203 Cvar_RegisterVariable(&gl_dither);
204 Cvar_RegisterVariable(&gl_lockarrays);
206 Cvar_SetValue("r_render", 0);
209 Cvar_RegisterVariable(&gl_mesh_maxverts);
210 Cvar_RegisterVariable(&gl_mesh_floatcolors);
211 Cvar_RegisterVariable(&gl_mesh_drawrangeelements);
212 GL_Backend_CheckCvars();
213 R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap);
216 void GL_SetupView_ViewPort (int x, int y, int width, int height)
218 if (!r_render.integer)
221 // y is weird beause OpenGL is bottom to top, we use top to bottom
222 qglViewport(x, vid.realheight - (y + height), width, height);
226 void GL_SetupView_Orientation_Identity (void)
228 Matrix4x4_CreateIdentity(&backend_viewmatrix);
229 memset(&backend_modelmatrix, 0, sizeof(backend_modelmatrix));
232 void GL_SetupView_Orientation_FromEntity (vec3_t origin, vec3_t angles)
234 Matrix4x4_CreateRotate(&backend_viewmatrix, -90, 1, 0, 0);
235 Matrix4x4_ConcatRotate(&backend_viewmatrix, 90, 0, 0, 1);
236 Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[2], 1, 0, 0);
237 Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[0], 0, 1, 0);
238 Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[1], 0, 0, 1);
239 Matrix4x4_ConcatTranslate(&backend_viewmatrix, -origin[0], -origin[1], -origin[2]);
240 memset(&backend_modelmatrix, 0, sizeof(backend_modelmatrix));
243 void GL_SetupView_Mode_Perspective (double fovx, double fovy, double zNear, double zFar)
247 if (!r_render.integer)
251 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
252 qglLoadIdentity ();CHECKGLERROR
254 xmax = zNear * tan(fovx * M_PI / 360.0);
255 ymax = zNear * tan(fovy * M_PI / 360.0);
257 qglFrustum(-xmax, xmax, -ymax, ymax, zNear, zFar);CHECKGLERROR
258 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
259 GL_SetupView_Orientation_Identity();
262 void GL_SetupView_Mode_PerspectiveInfiniteFarClip (double fovx, double fovy, double zNear)
266 if (!r_render.integer)
270 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
271 qglLoadIdentity ();CHECKGLERROR
273 nudge = 1.0 - 1.0 / (1<<23);
274 m[ 0] = 1.0 / tan(fovx * M_PI / 360.0);
279 m[ 5] = 1.0 / tan(fovy * M_PI / 360.0);
288 m[14] = -2 * zNear * nudge;
291 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
292 GL_SetupView_Orientation_Identity();
293 backend_projectmatrix.m[0][0] = m[0];
294 backend_projectmatrix.m[1][0] = m[1];
295 backend_projectmatrix.m[2][0] = m[2];
296 backend_projectmatrix.m[3][0] = m[3];
297 backend_projectmatrix.m[0][1] = m[4];
298 backend_projectmatrix.m[1][1] = m[5];
299 backend_projectmatrix.m[2][1] = m[6];
300 backend_projectmatrix.m[3][1] = m[7];
301 backend_projectmatrix.m[0][2] = m[8];
302 backend_projectmatrix.m[1][2] = m[9];
303 backend_projectmatrix.m[2][2] = m[10];
304 backend_projectmatrix.m[3][2] = m[11];
305 backend_projectmatrix.m[0][3] = m[12];
306 backend_projectmatrix.m[1][3] = m[13];
307 backend_projectmatrix.m[2][3] = m[14];
308 backend_projectmatrix.m[3][3] = m[15];
311 void GL_SetupView_Mode_Ortho (double x1, double y1, double x2, double y2, double zNear, double zFar)
313 if (!r_render.integer)
317 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
318 qglLoadIdentity ();CHECKGLERROR
319 qglOrtho(x1, x2, y2, y1, zNear, zFar);
320 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
321 GL_SetupView_Orientation_Identity();
324 typedef struct gltextureunit_s
326 int t1d, t2d, t3d, tcubemap;
328 float rgbscale, alphascale;
329 int combinergb, combinealpha;
330 // FIXME: add more combine stuff
343 gltextureunit_t units[MAX_TEXTUREUNITS];
348 void GL_SetupTextureState(void)
351 gltextureunit_t *unit;
352 for (i = 0;i < backendunits;i++)
354 if (qglActiveTexture)
355 qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
356 if (qglClientActiveTexture)
357 qglClientActiveTexture(GL_TEXTURE0_ARB + (gl_state.clientunit = i));CHECKGLERROR
358 unit = gl_state.units + i;
364 unit->alphascale = 1;
365 unit->combinergb = GL_MODULATE;
366 unit->combinealpha = GL_MODULATE;
367 unit->arrayenabled = false;
368 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
369 if (gl_texture3d || gl_texturecubemap)
371 qglTexCoordPointer(3, GL_FLOAT, sizeof(float[4]), varray_texcoord[i]);CHECKGLERROR
375 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[4]), varray_texcoord[i]);CHECKGLERROR
377 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
378 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
381 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
383 if (gl_texturecubemap)
385 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
387 if (gl_combine.integer)
389 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);CHECKGLERROR
390 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);CHECKGLERROR
391 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);CHECKGLERROR
392 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
393 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_CONSTANT_ARB);CHECKGLERROR
394 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
395 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
396 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);CHECKGLERROR
397 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);CHECKGLERROR
398 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);CHECKGLERROR
399 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
400 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, GL_CONSTANT_ARB);CHECKGLERROR
401 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
402 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
403 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
404 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
405 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
409 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
414 void GL_Backend_ResetState(void)
416 memset(&gl_state, 0, sizeof(gl_state));
417 gl_state.depthdisable = false;
418 gl_state.blendfunc1 = GL_ONE;
419 gl_state.blendfunc2 = GL_ZERO;
420 gl_state.blend = false;
421 gl_state.depthmask = GL_TRUE;
422 gl_state.colorarray = false;
424 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
425 qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
427 qglEnable(GL_CULL_FACE);CHECKGLERROR
428 qglCullFace(GL_FRONT);CHECKGLERROR
429 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
430 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
431 qglDisable(GL_BLEND);CHECKGLERROR
432 qglDepthMask(gl_state.depthmask);CHECKGLERROR
433 qglVertexPointer(3, GL_FLOAT, sizeof(float[4]), varray_vertex);CHECKGLERROR
434 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
435 if (gl_mesh_floatcolors.integer)
437 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), varray_color);CHECKGLERROR
441 qglColorPointer(4, GL_UNSIGNED_BYTE, sizeof(qbyte[4]), varray_bcolor);CHECKGLERROR
443 GL_Color(1, 1, 1, 1);
445 GL_SetupTextureState();
448 void GL_UseColorArray(void)
450 if (!gl_state.colorarray)
452 gl_state.colorarray = true;
453 qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
457 void GL_Color(float cr, float cg, float cb, float ca)
459 if (gl_state.colorarray)
461 gl_state.colorarray = false;
462 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
464 qglColor4f(cr, cg, cb, ca);
467 void GL_TransformToScreen(const vec4_t in, vec4_t out)
471 Matrix4x4_Transform4 (&backend_viewmatrix, in, temp);
472 Matrix4x4_Transform4 (&backend_projectmatrix, temp, out);
474 out[0] = r_refdef.x + (out[0] * iw + 1.0f) * r_refdef.width * 0.5f;
475 out[1] = r_refdef.y + (out[1] * iw + 1.0f) * r_refdef.height * 0.5f;
476 out[2] = out[2] * iw;
479 // called at beginning of frame
480 void R_Mesh_Start(void)
486 GL_Backend_CheckCvars();
487 if (mesh_maxverts != gl_mesh_maxverts.integer)
488 GL_Backend_ResizeArrays(gl_mesh_maxverts.integer);
490 GL_Backend_ResetState();
493 int gl_backend_rebindtextures;
495 void GL_ConvertColorsFloatToByte(int numverts)
498 // LordHavoc: to avoid problems with aliasing (treating memory as two
499 // different types - exactly what this is doing), these must be volatile
501 volatile int *icolor;
502 volatile float *fcolor;
505 total = numverts * 4;
507 // shift float to have 8bit fraction at base of number
508 fcolor = varray_color;
509 for (i = 0;i < total;)
511 fcolor[i ] += 32768.0f;
512 fcolor[i + 1] += 32768.0f;
513 fcolor[i + 2] += 32768.0f;
514 fcolor[i + 3] += 32768.0f;
518 // then read as integer and kill float bits...
519 icolor = (int *)varray_color;
520 bcolor = varray_bcolor;
521 for (i = 0;i < total;)
523 k = icolor[i ] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i ] = (qbyte) k;
524 k = icolor[i + 1] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i + 1] = (qbyte) k;
525 k = icolor[i + 2] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i + 2] = (qbyte) k;
526 k = icolor[i + 3] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i + 3] = (qbyte) k;
531 // enlarges geometry buffers if they are too small
532 void _R_Mesh_ResizeCheck(int numverts)
534 if (numverts > mesh_maxverts)
537 GL_Backend_ResizeArrays(numverts + 100);
538 GL_Backend_ResetState();
543 void R_Mesh_Draw(int numverts, int numtriangles, const int *elements)
546 if (numtriangles == 0 || numverts == 0)
548 Con_Printf("R_Mesh_Draw(%d, %d, %08p);\n", numverts, numtriangles, elements);
551 numelements = numtriangles * 3;
553 c_meshelements += numelements;
554 if (gl_state.colorarray && !gl_mesh_floatcolors.integer)
555 GL_ConvertColorsFloatToByte(numverts);
556 if (!r_render.integer)
558 if (gl_supportslockarrays && gl_lockarrays.integer)
560 qglLockArraysEXT(0, numverts);
562 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
563 qglDrawRangeElements(GL_TRIANGLES, 0, numverts, numelements, GL_UNSIGNED_INT, (const GLuint *) elements);
565 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, (const GLuint *) elements);
567 qglUnlockArraysEXT();
572 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, (const GLuint *) elements);
577 // restores backend state, used when done with 3D rendering
578 void R_Mesh_Finish(void)
583 for (i = backendunits - 1;i >= 0;i--)
585 if (qglActiveTexture)
586 qglActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
587 if (qglClientActiveTexture)
588 qglClientActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
589 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
590 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
591 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
594 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
596 if (gl_texturecubemap)
598 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
600 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
601 if (gl_combine.integer)
603 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
604 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
607 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
608 qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
610 qglDisable(GL_BLEND);CHECKGLERROR
611 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
612 qglDepthMask(GL_TRUE);CHECKGLERROR
613 qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);CHECKGLERROR
616 void R_Mesh_Matrix(const matrix4x4_t *matrix)
618 if (memcmp(matrix, &backend_modelmatrix, sizeof(matrix4x4_t)))
620 backend_modelmatrix = *matrix;
621 Matrix4x4_Concat(&backend_modelviewmatrix, &backend_viewmatrix, matrix);
622 Matrix4x4_Transpose(&backend_glmodelviewmatrix, &backend_modelviewmatrix);
623 qglLoadMatrixf(&backend_glmodelviewmatrix.m[0][0]);
627 // sets up the requested state
628 void R_Mesh_MainState(const rmeshstate_t *m)
632 if (gl_state.blendfunc1 != m->blendfunc1 || gl_state.blendfunc2 != m->blendfunc2)
634 qglBlendFunc(gl_state.blendfunc1 = m->blendfunc1, gl_state.blendfunc2 = m->blendfunc2);CHECKGLERROR
635 if (gl_state.blendfunc2 == GL_ZERO)
637 if (gl_state.blendfunc1 == GL_ONE)
642 qglDisable(GL_BLEND);CHECKGLERROR
650 qglEnable(GL_BLEND);CHECKGLERROR
659 qglEnable(GL_BLEND);CHECKGLERROR
663 if (gl_state.depthdisable != m->depthdisable)
665 gl_state.depthdisable = m->depthdisable;
666 if (gl_state.depthdisable)
667 qglDisable(GL_DEPTH_TEST);
669 qglEnable(GL_DEPTH_TEST);
671 if (gl_state.depthmask != (m->blendfunc2 == GL_ZERO || m->depthwrite))
673 qglDepthMask(gl_state.depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite));CHECKGLERROR
677 void R_Mesh_TextureState(const rmeshstate_t *m)
679 int i, combinergb, combinealpha;
681 gltextureunit_t *unit;
685 if (gl_backend_rebindtextures)
687 gl_backend_rebindtextures = false;
688 GL_SetupTextureState();
691 for (i = 0;i < backendunits;i++)
693 unit = gl_state.units + i;
694 if (unit->t1d != m->tex1d[i] || unit->t2d != m->tex[i] || unit->t3d != m->tex3d[i] || unit->tcubemap != m->texcubemap[i])
696 if (gl_state.unit != i)
698 qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
700 if (m->tex1d[i] || m->tex[i] || m->tex3d[i] || m->texcubemap[i])
702 if (!unit->arrayenabled)
704 unit->arrayenabled = true;
705 if (gl_state.clientunit != i)
707 qglClientActiveTexture(GL_TEXTURE0_ARB + (gl_state.clientunit = i));CHECKGLERROR
709 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
714 if (unit->arrayenabled)
716 unit->arrayenabled = false;
717 if (gl_state.clientunit != i)
719 qglClientActiveTexture(GL_TEXTURE0_ARB + (gl_state.clientunit = i));CHECKGLERROR
721 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
724 combinergb = m->texcombinergb[i];
725 combinealpha = m->texcombinealpha[i];
727 combinergb = GL_MODULATE;
729 combinealpha = GL_MODULATE;
730 if (unit->combinergb != combinergb)
732 unit->combinergb = combinergb;
733 if (gl_combine.integer)
735 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
739 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combinergb);CHECKGLERROR
742 if (unit->combinealpha != combinealpha)
744 unit->combinealpha = combinealpha;
745 if (gl_combine.integer)
747 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
750 if (unit->t1d != m->tex1d[i])
755 qglEnable(GL_TEXTURE_1D);CHECKGLERROR
760 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
762 qglBindTexture(GL_TEXTURE_1D, (unit->t1d = m->tex1d[i]));CHECKGLERROR
764 if (unit->t2d != m->tex[i])
769 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
774 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
776 qglBindTexture(GL_TEXTURE_2D, (unit->t2d = m->tex[i]));CHECKGLERROR
778 if (unit->t3d != m->tex3d[i])
783 qglEnable(GL_TEXTURE_3D);CHECKGLERROR
788 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
790 qglBindTexture(GL_TEXTURE_3D, (unit->t3d = m->tex3d[i]));CHECKGLERROR
792 if (unit->tcubemap != m->texcubemap[i])
794 if (m->texcubemap[i])
796 if (unit->tcubemap == 0)
797 qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
802 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
804 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, (unit->tcubemap = m->texcubemap[i]));CHECKGLERROR
807 scale = max(m->texrgbscale[i], 1);
808 if (gl_state.units[i].rgbscale != scale)
810 if (gl_state.unit != i)
812 qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
814 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (gl_state.units[i].rgbscale = scale));CHECKGLERROR
816 scale = max(m->texalphascale[i], 1);
817 if (gl_state.units[i].alphascale != scale)
819 if (gl_state.unit != i)
821 qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
823 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, (gl_state.units[i].alphascale = scale));CHECKGLERROR
828 void R_Mesh_State(const rmeshstate_t *m)
831 R_Mesh_TextureState(m);
835 ==============================================================================
839 ==============================================================================
842 qboolean SCR_ScreenShot(char *filename, int x, int y, int width, int height)
848 if (!r_render.integer)
851 buffer = Mem_Alloc(tempmempool, width*height*3);
852 qglReadPixels (x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer);
855 // LordHavoc: compensate for v_overbrightbits when using hardware gamma
856 if (v_hwgamma.integer)
857 for (i = 0;i < width * height * 3;i++)
858 buffer[i] <<= v_overbrightbits.integer;
860 ret = Image_WriteTGARGB_preflipped(filename, width, height, buffer);
866 //=============================================================================
868 void R_ClearScreen(void)
870 if (r_render.integer)
873 qglClearColor(0,0,0,0);CHECKGLERROR
874 qglClearDepth(1);CHECKGLERROR
877 // LordHavoc: we use a stencil centered around 128 instead of 0,
878 // to avoid clamping interfering with strange shadow volume
880 qglClearStencil(128);CHECKGLERROR
883 qglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | (gl_stencil ? GL_STENCIL_BUFFER_BIT : 0));CHECKGLERROR
884 // set dithering mode
885 if (gl_dither.integer)
887 qglEnable(GL_DITHER);CHECKGLERROR
891 qglDisable(GL_DITHER);CHECKGLERROR
900 This is called every frame, and can also be called explicitly to flush
904 void SCR_UpdateScreen (void)
908 R_TimeReport("finish");
910 if (r_textureunits.integer > gl_textureunits)
911 Cvar_SetValueQuick(&r_textureunits, gl_textureunits);
912 if (r_textureunits.integer < 1)
913 Cvar_SetValueQuick(&r_textureunits, 1);
915 if (gl_combine.integer && (!gl_combine_extension || r_textureunits.integer < 2))
916 Cvar_SetValueQuick(&gl_combine, 0);
919 r_colorscale = 1.0f / (float) (1 << v_overbrightbits.integer);
922 r_lightmapscalebit = v_overbrightbits.integer;
923 if (gl_combine.integer && r_textureunits.integer > 1)
924 r_lightmapscalebit += 2;
926 R_TimeReport("setup");
930 R_TimeReport("clear");
932 if (scr_conlines < vid.conheight && cls.signon == SIGNONS)
938 // tell driver to commit it's partially full geometry queue to the rendering queue
939 // (this doesn't wait for the commands themselves to complete)