3 #include "cl_collision.h"
5 cvar_t gl_mesh_drawrangeelements = {0, "gl_mesh_drawrangeelements", "1", "use glDrawRangeElements function if available instead of glDrawElements (for performance comparisons or bug testing)"};
6 cvar_t gl_mesh_testarrayelement = {0, "gl_mesh_testarrayelement", "0", "use glBegin(GL_TRIANGLES);glArrayElement();glEnd(); primitives instead of glDrawElements (useful to test for driver bugs with glDrawElements)"};
7 cvar_t gl_mesh_testmanualfeeding = {0, "gl_mesh_testmanualfeeding", "0", "use glBegin(GL_TRIANGLES);glTexCoord2f();glVertex3f();glEnd(); primitives instead of glDrawElements (useful to test for driver bugs with glDrawElements)"};
8 cvar_t gl_paranoid = {0, "gl_paranoid", "0", "enables OpenGL error checking and other tests"};
9 cvar_t gl_printcheckerror = {0, "gl_printcheckerror", "0", "prints all OpenGL error checks, useful to identify location of driver crashes"};
11 cvar_t r_render = {0, "r_render", "1", "enables rendering calls (you want this on!)"};
12 cvar_t r_waterwarp = {CVAR_SAVE, "r_waterwarp", "1", "warp view while underwater"};
13 cvar_t gl_polyblend = {CVAR_SAVE, "gl_polyblend", "1", "tints view while underwater, hurt, etc"};
14 cvar_t gl_dither = {CVAR_SAVE, "gl_dither", "1", "enables OpenGL dithering (16bit looks bad with this off)"};
15 cvar_t gl_lockarrays = {0, "gl_lockarrays", "0", "enables use of glLockArraysEXT, may cause glitches with some broken drivers, and may be slower than normal"};
16 cvar_t gl_lockarrays_minimumvertices = {0, "gl_lockarrays_minimumvertices", "1", "minimum number of vertices required for use of glLockArraysEXT, setting this too low may reduce performance"};
17 cvar_t gl_vbo = {CVAR_SAVE, "gl_vbo", "1", "make use of GL_ARB_vertex_buffer_object extension to store static geometry in video memory for faster rendering"};
19 cvar_t v_flipped = {0, "v_flipped", "0", "mirror the screen (poor man's left handed mode)"};
20 qboolean v_flipped_state = false;
22 int gl_maxdrawrangeelementsvertices;
23 int gl_maxdrawrangeelementsindices;
28 void GL_PrintError(int errornumber, char *filename, int linenumber)
32 #ifdef GL_INVALID_ENUM
34 Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
37 #ifdef GL_INVALID_VALUE
38 case GL_INVALID_VALUE:
39 Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
42 #ifdef GL_INVALID_OPERATION
43 case GL_INVALID_OPERATION:
44 Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
47 #ifdef GL_STACK_OVERFLOW
48 case GL_STACK_OVERFLOW:
49 Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
52 #ifdef GL_STACK_UNDERFLOW
53 case GL_STACK_UNDERFLOW:
54 Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
57 #ifdef GL_OUT_OF_MEMORY
58 case GL_OUT_OF_MEMORY:
59 Con_Printf("GL_OUT_OF_MEMORY at %s:%i\n", filename, linenumber);
62 #ifdef GL_TABLE_TOO_LARGE
63 case GL_TABLE_TOO_LARGE:
64 Con_Printf("GL_TABLE_TOO_LARGE at %s:%i\n", filename, linenumber);
68 Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
74 #define BACKENDACTIVECHECK if (!backendactive) Sys_Error("GL backend function called when backend is not active");
76 void SCR_ScreenShot_f (void);
78 static matrix4x4_t backend_viewmatrix;
79 static matrix4x4_t backend_modelmatrix;
80 static matrix4x4_t backend_modelviewmatrix;
81 static matrix4x4_t backend_projectmatrix;
83 static unsigned int backendunits, backendimageunits, backendarrayunits, backendactive;
86 note: here's strip order for a terrain row:
93 A0B, 01B, B1C, 12C, C2D, 23D, D3E, 34E
95 *elements++ = i + row;
97 *elements++ = i + row + 1;
100 *elements++ = i + row + 1;
103 for (y = 0;y < rows - 1;y++)
105 for (x = 0;x < columns - 1;x++)
108 *elements++ = i + columns;
110 *elements++ = i + columns + 1;
113 *elements++ = i + columns + 1;
124 for (y = 0;y < rows - 1;y++)
126 for (x = 0;x < columns - 1;x++)
130 *elements++ = i + columns;
131 *elements++ = i + columns + 1;
132 *elements++ = i + columns;
133 *elements++ = i + columns + 1;
139 int polygonelements[(POLYGONELEMENTS_MAXPOINTS-2)*3];
140 int quadelements[QUADELEMENTS_MAXQUADS*6];
142 void GL_Backend_AllocArrays(void)
146 void GL_Backend_FreeArrays(void)
150 void GL_VBOStats_f(void)
152 GL_Mesh_ListVBOs(true);
155 typedef struct gl_bufferobjectinfo_s
160 char name[MAX_QPATH];
162 gl_bufferobjectinfo_t;
164 memexpandablearray_t gl_bufferobjectinfoarray;
166 static void gl_backend_start(void)
168 Con_Print("OpenGL Backend starting...\n");
171 if (qglDrawRangeElements != NULL)
174 qglGetIntegerv(GL_MAX_ELEMENTS_VERTICES, &gl_maxdrawrangeelementsvertices);
176 qglGetIntegerv(GL_MAX_ELEMENTS_INDICES, &gl_maxdrawrangeelementsindices);
178 Con_Printf("glDrawRangeElements detected (max vertices %i, max indices %i)\n", gl_maxdrawrangeelementsvertices, gl_maxdrawrangeelementsindices);
181 backendunits = bound(1, gl_textureunits, MAX_TEXTUREUNITS);
182 backendimageunits = backendunits;
183 backendarrayunits = backendunits;
184 if (gl_support_fragment_shader)
187 qglGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS_ARB, (int *)&backendimageunits);
189 qglGetIntegerv(GL_MAX_TEXTURE_COORDS_ARB, (int *)&backendarrayunits);
191 Con_Printf("GLSL shader support detected: texture units = %i texenv, %i image, %i array\n", backendunits, backendimageunits, backendarrayunits);
192 backendimageunits = bound(1, backendimageunits, MAX_TEXTUREUNITS);
193 backendarrayunits = bound(1, backendarrayunits, MAX_TEXTUREUNITS);
195 else if (backendunits > 1)
196 Con_Printf("multitexture detected: texture units = %i\n", backendunits);
198 Con_Printf("singletexture\n");
200 GL_Backend_AllocArrays();
202 Mem_ExpandableArray_NewArray(&gl_bufferobjectinfoarray, r_main_mempool, sizeof(gl_bufferobjectinfo_t), 128);
204 Con_Printf("OpenGL backend started.\n");
208 backendactive = true;
211 static void gl_backend_shutdown(void)
214 backendimageunits = 0;
215 backendarrayunits = 0;
216 backendactive = false;
218 Con_Print("OpenGL Backend shutting down\n");
220 Mem_ExpandableArray_FreeArray(&gl_bufferobjectinfoarray);
222 GL_Backend_FreeArrays();
225 static void gl_backend_newmap(void)
229 void gl_backend_init(void)
233 for (i = 0;i < POLYGONELEMENTS_MAXPOINTS - 2;i++)
235 polygonelements[i * 3 + 0] = 0;
236 polygonelements[i * 3 + 1] = i + 1;
237 polygonelements[i * 3 + 2] = i + 2;
239 // elements for rendering a series of quads as triangles
240 for (i = 0;i < QUADELEMENTS_MAXQUADS;i++)
242 quadelements[i * 6 + 0] = i * 4;
243 quadelements[i * 6 + 1] = i * 4 + 1;
244 quadelements[i * 6 + 2] = i * 4 + 2;
245 quadelements[i * 6 + 3] = i * 4;
246 quadelements[i * 6 + 4] = i * 4 + 2;
247 quadelements[i * 6 + 5] = i * 4 + 3;
250 Cvar_RegisterVariable(&r_render);
251 Cvar_RegisterVariable(&r_waterwarp);
252 Cvar_RegisterVariable(&gl_polyblend);
253 Cvar_RegisterVariable(&v_flipped);
254 Cvar_RegisterVariable(&gl_dither);
255 Cvar_RegisterVariable(&gl_lockarrays);
256 Cvar_RegisterVariable(&gl_lockarrays_minimumvertices);
257 Cvar_RegisterVariable(&gl_vbo);
258 Cvar_RegisterVariable(&gl_paranoid);
259 Cvar_RegisterVariable(&gl_printcheckerror);
261 Cvar_SetValue("r_render", 0);
264 Cvar_RegisterVariable(&gl_mesh_drawrangeelements);
265 Cvar_RegisterVariable(&gl_mesh_testarrayelement);
266 Cvar_RegisterVariable(&gl_mesh_testmanualfeeding);
268 Cmd_AddCommand("gl_vbostats", GL_VBOStats_f, "prints a list of all buffer objects (vertex data and triangle elements) and total video memory used by them");
270 R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap);
273 void GL_SetMirrorState(qboolean state);
275 void GL_SetupView_Orientation_Identity (void)
277 backend_viewmatrix = identitymatrix;
278 GL_SetMirrorState(false);
279 memset(&backend_modelmatrix, 0, sizeof(backend_modelmatrix));
282 void GL_SetupView_Orientation_FromEntity(const matrix4x4_t *matrix)
284 matrix4x4_t tempmatrix, basematrix;
285 Matrix4x4_Invert_Full(&tempmatrix, matrix);
286 Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
287 Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
288 Matrix4x4_Concat(&backend_viewmatrix, &basematrix, &tempmatrix);
290 GL_SetMirrorState(v_flipped.integer);
293 Matrix4x4_Transpose(&basematrix, &backend_viewmatrix);
294 Matrix4x4_ConcatScale3(&basematrix, -1, 1, 1);
295 Matrix4x4_Transpose(&backend_viewmatrix, &basematrix);
298 //Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[2], 1, 0, 0);
299 //Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[0], 0, 1, 0);
300 //Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[1], 0, 0, 1);
301 //Matrix4x4_ConcatTranslate(&backend_viewmatrix, -origin[0], -origin[1], -origin[2]);
303 // force an update of the model matrix by copying it off, resetting it, and then calling the R_Mesh_Matrix function with it
304 tempmatrix = backend_modelmatrix;
305 memset(&backend_modelmatrix, 0, sizeof(backend_modelmatrix));
306 R_Mesh_Matrix(&tempmatrix);
309 void GL_SetupView_Mode_Perspective (double frustumx, double frustumy, double zNear, double zFar)
315 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
316 qglLoadIdentity();CHECKGLERROR
318 qglFrustum(-frustumx * zNear, frustumx * zNear, -frustumy * zNear, frustumy * zNear, zNear, zFar);CHECKGLERROR
319 qglGetDoublev(GL_PROJECTION_MATRIX, m);CHECKGLERROR
320 Matrix4x4_FromArrayDoubleGL(&backend_projectmatrix, m);
321 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
322 GL_SetupView_Orientation_Identity();
326 void GL_SetupView_Mode_PerspectiveInfiniteFarClip (double frustumx, double frustumy, double zNear)
332 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
333 qglLoadIdentity();CHECKGLERROR
335 nudge = 1.0 - 1.0 / (1<<23);
336 m[ 0] = 1.0 / frustumx;
341 m[ 5] = 1.0 / frustumy;
350 m[14] = -2 * zNear * nudge;
352 qglLoadMatrixd(m);CHECKGLERROR
353 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
354 GL_SetupView_Orientation_Identity();
356 Matrix4x4_FromArrayDoubleGL(&backend_projectmatrix, m);
359 void GL_SetupView_Mode_Ortho (double x1, double y1, double x2, double y2, double zNear, double zFar)
365 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
366 qglLoadIdentity();CHECKGLERROR
367 qglOrtho(x1, x2, y2, y1, zNear, zFar);CHECKGLERROR
368 qglGetDoublev(GL_PROJECTION_MATRIX, m);CHECKGLERROR
369 Matrix4x4_FromArrayDoubleGL(&backend_projectmatrix, m);
370 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
371 GL_SetupView_Orientation_Identity();
375 void GL_SetupView_ApplyCustomNearClipPlane(double normalx, double normaly, double normalz, double dist)
380 float clipPlane[4], v3[3], v4[3];
383 // This is Olique Depth Projection from http://www.terathon.com/code/oblique.php
384 // modified to fit in this codebase.
386 VectorSet(normal, normalx, normaly, normalz);
387 Matrix4x4_Transform3x3(&backend_viewmatrix, normal, clipPlane);
388 VectorScale(normal, dist, v3);
389 Matrix4x4_Transform(&backend_viewmatrix, v3, v4);
390 // FIXME: LordHavoc: I think this can be done more efficiently somehow but I can't remember the technique
391 clipPlane[3] = -DotProduct(v4, clipPlane);
395 // testing code for comparing results
397 VectorCopy4(clipPlane, clipPlane2);
398 R_Mesh_Matrix(&identitymatrix);
399 VectorSet(q, normal[0], normal[1], normal[2], -dist);
400 qglClipPlane(GL_CLIP_PLANE0, q);
401 qglGetClipPlane(GL_CLIP_PLANE0, q);
402 VectorCopy4(q, clipPlane);
406 // Calculate the clip-space corner point opposite the clipping plane
407 // as (sgn(clipPlane.x), sgn(clipPlane.y), 1, 1) and
408 // transform it into camera space by multiplying it
409 // by the inverse of the projection matrix
410 Matrix4x4_ToArrayDoubleGL(&backend_projectmatrix, matrix);
412 q[0] = ((clipPlane[0] < 0.0f ? -1.0f : clipPlane[0] > 0.0f ? 1.0f : 0.0f) + matrix[8]) / matrix[0];
413 q[1] = ((clipPlane[1] < 0.0f ? -1.0f : clipPlane[1] > 0.0f ? 1.0f : 0.0f) + matrix[9]) / matrix[5];
415 q[3] = (1.0f + matrix[10]) / matrix[14];
417 // Calculate the scaled plane vector
418 d = 2.0f / DotProduct4(clipPlane, q);
420 // Replace the third row of the projection matrix
421 matrix[2] = clipPlane[0] * d;
422 matrix[6] = clipPlane[1] * d;
423 matrix[10] = clipPlane[2] * d + 1.0f;
424 matrix[14] = clipPlane[3] * d;
426 // Load it back into OpenGL
427 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
428 qglLoadMatrixd(matrix);CHECKGLERROR
429 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
431 Matrix4x4_FromArrayDoubleGL(&backend_projectmatrix, matrix);
434 typedef struct gltextureunit_s
436 const void *pointer_texcoord;
437 size_t pointer_texcoord_offset;
438 int pointer_texcoord_buffer;
439 int t1d, t2d, t3d, tcubemap;
441 unsigned int arraycomponents;
442 int rgbscale, alphascale;
443 int combinergb, combinealpha;
444 // FIXME: add more combine stuff
445 // texmatrixenabled exists only to avoid unnecessary texmatrix compares
446 int texmatrixenabled;
451 static struct gl_state_s
459 int colormask; // stored as bottom 4 bits: r g b a (3 2 1 0 order)
462 float polygonoffset[2];
466 unsigned int clientunit;
467 gltextureunit_t units[MAX_TEXTUREUNITS];
471 int vertexbufferobject;
472 int elementbufferobject;
473 qboolean pointer_color_enabled;
474 const void *pointer_vertex;
475 const void *pointer_color;
476 size_t pointer_vertex_offset;
477 size_t pointer_color_offset;
478 int pointer_vertex_buffer;
479 int pointer_color_buffer;
483 static void GL_BindVBO(int bufferobject)
485 if (gl_state.vertexbufferobject != bufferobject)
487 gl_state.vertexbufferobject = bufferobject;
489 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, bufferobject);
494 static void GL_BindEBO(int bufferobject)
496 if (gl_state.elementbufferobject != bufferobject)
498 gl_state.elementbufferobject = bufferobject;
500 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, bufferobject);
505 void GL_SetupTextureState(void)
508 gltextureunit_t *unit;
510 gl_state.unit = MAX_TEXTUREUNITS;
511 gl_state.clientunit = MAX_TEXTUREUNITS;
512 for (i = 0;i < MAX_TEXTUREUNITS;i++)
514 unit = gl_state.units + i;
519 unit->arrayenabled = false;
520 unit->arraycomponents = 0;
521 unit->pointer_texcoord = NULL;
522 unit->pointer_texcoord_buffer = 0;
523 unit->pointer_texcoord_offset = 0;
525 unit->alphascale = 1;
526 unit->combinergb = GL_MODULATE;
527 unit->combinealpha = GL_MODULATE;
528 unit->texmatrixenabled = false;
529 unit->matrix = identitymatrix;
532 for (i = 0;i < backendimageunits;i++)
535 qglBindTexture(GL_TEXTURE_1D, 0);CHECKGLERROR
536 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
539 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
541 if (gl_texturecubemap)
543 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
547 for (i = 0;i < backendarrayunits;i++)
549 GL_ClientActiveTexture(i);
551 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
552 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
555 for (i = 0;i < backendunits;i++)
558 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
559 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
562 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
564 if (gl_texturecubemap)
566 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
568 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
569 qglLoadIdentity();CHECKGLERROR
570 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
571 if (gl_combine.integer)
573 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);CHECKGLERROR
574 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);CHECKGLERROR
575 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);CHECKGLERROR
576 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
577 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_TEXTURE);CHECKGLERROR // for GL_INTERPOLATE_ARB mode
578 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
579 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
580 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);CHECKGLERROR
581 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);CHECKGLERROR
582 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);CHECKGLERROR
583 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
584 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, GL_CONSTANT_ARB);CHECKGLERROR
585 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
586 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
587 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
588 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
589 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
593 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
600 void GL_Backend_ResetState(void)
602 memset(&gl_state, 0, sizeof(gl_state));
603 gl_state.depthtest = true;
604 gl_state.alphatest = false;
605 gl_state.blendfunc1 = GL_ONE;
606 gl_state.blendfunc2 = GL_ZERO;
607 gl_state.blend = false;
608 gl_state.depthmask = GL_TRUE;
609 gl_state.colormask = 15;
610 gl_state.color4f[0] = gl_state.color4f[1] = gl_state.color4f[2] = gl_state.color4f[3] = 1;
611 gl_state.lockrange_first = 0;
612 gl_state.lockrange_count = 0;
613 gl_state.cullface = v_flipped_state ? GL_BACK : GL_FRONT; // quake is backwards, this culls back faces
614 gl_state.cullfaceenable = true;
615 gl_state.polygonoffset[0] = 0;
616 gl_state.polygonoffset[1] = 0;
620 qglColorMask(1, 1, 1, 1);
621 qglAlphaFunc(GL_GEQUAL, 0.5);CHECKGLERROR
622 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
623 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
624 qglDisable(GL_BLEND);CHECKGLERROR
625 qglCullFace(gl_state.cullface);CHECKGLERROR
626 qglEnable(GL_CULL_FACE);CHECKGLERROR
627 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
628 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
629 qglDepthMask(gl_state.depthmask);CHECKGLERROR
630 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
632 if (gl_support_arb_vertex_buffer_object)
634 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
635 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
638 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), NULL);CHECKGLERROR
639 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
641 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL);CHECKGLERROR
642 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
644 GL_Color(0, 0, 0, 0);
645 GL_Color(1, 1, 1, 1);
647 GL_SetupTextureState();
650 void GL_ActiveTexture(unsigned int num)
652 if (gl_state.unit != num)
655 if (qglActiveTexture)
658 qglActiveTexture(GL_TEXTURE0_ARB + gl_state.unit);
664 void GL_ClientActiveTexture(unsigned int num)
666 if (gl_state.clientunit != num)
668 gl_state.clientunit = num;
669 if (qglActiveTexture)
672 qglClientActiveTexture(GL_TEXTURE0_ARB + gl_state.clientunit);
678 void GL_BlendFunc(int blendfunc1, int blendfunc2)
680 if (gl_state.blendfunc1 != blendfunc1 || gl_state.blendfunc2 != blendfunc2)
683 qglBlendFunc(gl_state.blendfunc1 = blendfunc1, gl_state.blendfunc2 = blendfunc2);CHECKGLERROR
684 if (gl_state.blendfunc2 == GL_ZERO)
686 if (gl_state.blendfunc1 == GL_ONE)
691 qglDisable(GL_BLEND);CHECKGLERROR
699 qglEnable(GL_BLEND);CHECKGLERROR
708 qglEnable(GL_BLEND);CHECKGLERROR
714 void GL_DepthMask(int state)
716 if (gl_state.depthmask != state)
719 qglDepthMask(gl_state.depthmask = state);CHECKGLERROR
723 void GL_DepthTest(int state)
725 if (gl_state.depthtest != state)
727 gl_state.depthtest = state;
729 if (gl_state.depthtest)
731 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
735 qglDisable(GL_DEPTH_TEST);CHECKGLERROR
740 void GL_DepthRange(float nearfrac, float farfrac)
742 if (gl_state.depthrange[0] != nearfrac || gl_state.depthrange[1] != farfrac)
744 gl_state.depthrange[0] = nearfrac;
745 gl_state.depthrange[1] = farfrac;
746 qglDepthRange(nearfrac, farfrac);
750 void GL_PolygonOffset(float planeoffset, float depthoffset)
752 if (gl_state.polygonoffset[0] != planeoffset || gl_state.polygonoffset[1] != depthoffset)
754 gl_state.polygonoffset[0] = planeoffset;
755 gl_state.polygonoffset[1] = depthoffset;
756 qglPolygonOffset(planeoffset, depthoffset);
760 void GL_SetMirrorState(qboolean state)
762 if(!state != !v_flipped_state)
764 // change cull face mode!
765 if(gl_state.cullface == GL_BACK)
766 qglCullFace((gl_state.cullface = GL_FRONT));
767 else if(gl_state.cullface == GL_FRONT)
768 qglCullFace((gl_state.cullface = GL_BACK));
770 v_flipped_state = state;
773 void GL_CullFace(int state)
779 if(state == GL_FRONT)
781 else if(state == GL_BACK)
785 if (state != GL_NONE)
787 if (!gl_state.cullfaceenable)
789 gl_state.cullfaceenable = true;
790 qglEnable(GL_CULL_FACE);CHECKGLERROR
792 if (gl_state.cullface != state)
794 gl_state.cullface = state;
795 qglCullFace(gl_state.cullface);CHECKGLERROR
800 if (gl_state.cullfaceenable)
802 gl_state.cullfaceenable = false;
803 qglDisable(GL_CULL_FACE);CHECKGLERROR
808 void GL_AlphaTest(int state)
810 if (gl_state.alphatest != state)
812 gl_state.alphatest = state;
814 if (gl_state.alphatest)
816 qglEnable(GL_ALPHA_TEST);CHECKGLERROR
820 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
825 void GL_ColorMask(int r, int g, int b, int a)
827 int state = r*8 + g*4 + b*2 + a*1;
828 if (gl_state.colormask != state)
830 gl_state.colormask = state;
832 qglColorMask((GLboolean)r, (GLboolean)g, (GLboolean)b, (GLboolean)a);CHECKGLERROR
836 void GL_Color(float cr, float cg, float cb, float ca)
838 if (gl_state.pointer_color_enabled || gl_state.color4f[0] != cr || gl_state.color4f[1] != cg || gl_state.color4f[2] != cb || gl_state.color4f[3] != ca)
840 gl_state.color4f[0] = cr;
841 gl_state.color4f[1] = cg;
842 gl_state.color4f[2] = cb;
843 gl_state.color4f[3] = ca;
845 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);
850 void GL_LockArrays(int first, int count)
852 if (count < gl_lockarrays_minimumvertices.integer)
857 if (gl_state.lockrange_count != count || gl_state.lockrange_first != first)
859 if (gl_state.lockrange_count)
861 gl_state.lockrange_count = 0;
863 qglUnlockArraysEXT();
866 if (count && gl_supportslockarrays && gl_lockarrays.integer && r_render.integer)
868 gl_state.lockrange_first = first;
869 gl_state.lockrange_count = count;
871 qglLockArraysEXT(first, count);
877 void GL_Scissor (int x, int y, int width, int height)
880 qglScissor(x, vid.height - (y + height),width,height);
884 void GL_ScissorTest(int state)
886 if(gl_state.scissortest == state)
890 if((gl_state.scissortest = state))
891 qglEnable(GL_SCISSOR_TEST);
893 qglDisable(GL_SCISSOR_TEST);
897 void GL_Clear(int mask)
900 qglClear(mask);CHECKGLERROR
903 void GL_TransformToScreen(const vec4_t in, vec4_t out)
907 Matrix4x4_Transform4 (&backend_viewmatrix, in, temp);
908 Matrix4x4_Transform4 (&backend_projectmatrix, temp, out);
910 out[0] = r_refdef.view.x + (out[0] * iw + 1.0f) * r_refdef.view.width * 0.5f;
911 out[1] = r_refdef.view.y + r_refdef.view.height - (out[1] * iw + 1.0f) * r_refdef.view.height * 0.5f;
912 out[2] = r_refdef.view.z + (out[2] * iw + 1.0f) * r_refdef.view.depth * 0.5f;
915 // called at beginning of frame
916 void R_Mesh_Start(void)
920 if (gl_printcheckerror.integer && !gl_paranoid.integer)
922 Con_Printf("WARNING: gl_printcheckerror is on but gl_paranoid is off, turning it on...\n");
923 Cvar_SetValueQuick(&gl_paranoid, 1);
925 GL_Backend_ResetState();
928 qboolean GL_Backend_CompileShader(int programobject, GLenum shadertypeenum, const char *shadertype, int numstrings, const char **strings)
932 char compilelog[MAX_INPUTLINE];
933 shaderobject = qglCreateShaderObjectARB(shadertypeenum);CHECKGLERROR
936 qglShaderSourceARB(shaderobject, numstrings, strings, NULL);CHECKGLERROR
937 qglCompileShaderARB(shaderobject);CHECKGLERROR
938 qglGetObjectParameterivARB(shaderobject, GL_OBJECT_COMPILE_STATUS_ARB, &shadercompiled);CHECKGLERROR
939 qglGetInfoLogARB(shaderobject, sizeof(compilelog), NULL, compilelog);CHECKGLERROR
940 if (compilelog[0] && developer.integer > 0)
942 int i, j, pretextlines = 0;
943 for (i = 0;i < numstrings - 1;i++)
944 for (j = 0;strings[i][j];j++)
945 if (strings[i][j] == '\n')
947 Con_DPrintf("%s shader compile log:\n%s\n(line offset for any above warnings/errors: %i)\n", shadertype, compilelog, pretextlines);
951 qglDeleteObjectARB(shaderobject);CHECKGLERROR
954 qglAttachObjectARB(programobject, shaderobject);CHECKGLERROR
955 qglDeleteObjectARB(shaderobject);CHECKGLERROR
959 unsigned int GL_Backend_CompileProgram(int vertexstrings_count, const char **vertexstrings_list, int geometrystrings_count, const char **geometrystrings_list, int fragmentstrings_count, const char **fragmentstrings_list)
962 GLuint programobject = 0;
963 char linklog[MAX_INPUTLINE];
966 programobject = qglCreateProgramObjectARB();CHECKGLERROR
970 if (vertexstrings_count && !GL_Backend_CompileShader(programobject, GL_VERTEX_SHADER_ARB, "vertex", vertexstrings_count, vertexstrings_list))
973 #ifdef GL_GEOMETRY_SHADER_ARB
974 if (geometrystrings_count && !GL_Backend_CompileShader(programobject, GL_GEOMETRY_SHADER_ARB, "geometry", geometrystrings_count, geometrystrings_list))
978 if (fragmentstrings_count && !GL_Backend_CompileShader(programobject, GL_FRAGMENT_SHADER_ARB, "fragment", fragmentstrings_count, fragmentstrings_list))
981 qglLinkProgramARB(programobject);CHECKGLERROR
982 qglGetObjectParameterivARB(programobject, GL_OBJECT_LINK_STATUS_ARB, &programlinked);CHECKGLERROR
983 qglGetInfoLogARB(programobject, sizeof(linklog), NULL, linklog);CHECKGLERROR
986 Con_DPrintf("program link log:\n%s\n", linklog);
987 // software vertex shader is ok but software fragment shader is WAY
988 // too slow, fail program if so.
989 // NOTE: this string might be ATI specific, but that's ok because the
990 // ATI R300 chip (Radeon 9500-9800/X300) is the most likely to use a
991 // software fragment shader due to low instruction and dependent
993 if (strstr(linklog, "fragment shader will run in software"))
994 programlinked = false;
998 return programobject;
1000 qglDeleteObjectARB(programobject);CHECKGLERROR
1004 void GL_Backend_FreeProgram(unsigned int prog)
1007 qglDeleteObjectARB(prog);
1011 int gl_backend_rebindtextures;
1013 void GL_Backend_RenumberElements(int *out, int count, const int *in, int offset)
1018 for (i = 0;i < count;i++)
1019 *out++ = *in++ + offset;
1022 memcpy(out, in, sizeof(*out) * count);
1025 // renders triangles using vertices from the active arrays
1026 int paranoidblah = 0;
1027 void R_Mesh_Draw(int firstvertex, int numvertices, int numtriangles, const int *elements, int bufferobject, size_t bufferoffset)
1029 unsigned int numelements = numtriangles * 3;
1030 if (numvertices < 3 || numtriangles < 1)
1032 Con_Printf("R_Mesh_Draw(%d, %d, %d, %8p, %i, %p);\n", firstvertex, numvertices, numtriangles, elements, bufferobject, (void *)bufferoffset);
1035 if (gl_vbo.integer != 1)
1038 r_refdef.stats.meshes++;
1039 r_refdef.stats.meshes_elements += numelements;
1040 if (gl_paranoid.integer)
1042 unsigned int i, j, size;
1044 // note: there's no validation done here on buffer objects because it
1045 // is somewhat difficult to get at the data, and gl_paranoid can be
1046 // used without buffer objects if the need arises
1047 // (the data could be gotten using glMapBuffer but it would be very
1048 // slow due to uncachable video memory reads)
1049 if (!qglIsEnabled(GL_VERTEX_ARRAY))
1050 Con_Print("R_Mesh_Draw: vertex array not enabled\n");
1052 if (gl_state.pointer_vertex)
1053 for (j = 0, size = numvertices * 3, p = (int *)((float *)gl_state.pointer_vertex + firstvertex * 3);j < size;j++, p++)
1055 if (gl_state.pointer_color_enabled)
1057 if (!qglIsEnabled(GL_COLOR_ARRAY))
1058 Con_Print("R_Mesh_Draw: color array set but not enabled\n");
1060 if (gl_state.pointer_color && gl_state.pointer_color_enabled)
1061 for (j = 0, size = numvertices * 4, p = (int *)((float *)gl_state.pointer_color + firstvertex * 4);j < size;j++, p++)
1064 for (i = 0;i < backendarrayunits;i++)
1066 if (gl_state.units[i].arrayenabled)
1068 GL_ClientActiveTexture(i);
1069 if (!qglIsEnabled(GL_TEXTURE_COORD_ARRAY))
1070 Con_Print("R_Mesh_Draw: texcoord array set but not enabled\n");
1072 if (gl_state.units[i].pointer_texcoord && gl_state.units[i].arrayenabled)
1073 for (j = 0, size = numvertices * gl_state.units[i].arraycomponents, p = (int *)((float *)gl_state.units[i].pointer_texcoord + firstvertex * gl_state.units[i].arraycomponents);j < size;j++, p++)
1077 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1079 if (elements[i] < firstvertex || elements[i] >= firstvertex + numvertices)
1081 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in elements list\n", elements[i], firstvertex, firstvertex + numvertices);
1087 if (r_render.integer)
1090 if (gl_mesh_testmanualfeeding.integer)
1094 qglBegin(GL_TRIANGLES);
1095 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1097 for (j = 0;j < backendarrayunits;j++)
1099 if (gl_state.units[j].pointer_texcoord && gl_state.units[j].arrayenabled)
1101 if (backendarrayunits > 1)
1103 if (gl_state.units[j].arraycomponents == 4)
1105 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 4;
1106 qglMultiTexCoord4f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2], p[3]);
1108 else if (gl_state.units[j].arraycomponents == 3)
1110 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 3;
1111 qglMultiTexCoord3f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2]);
1113 else if (gl_state.units[j].arraycomponents == 2)
1115 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 2;
1116 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, p[0], p[1]);
1120 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 1;
1121 qglMultiTexCoord1f(GL_TEXTURE0_ARB + j, p[0]);
1126 if (gl_state.units[j].arraycomponents == 4)
1128 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 4;
1129 qglTexCoord4f(p[0], p[1], p[2], p[3]);
1131 else if (gl_state.units[j].arraycomponents == 3)
1133 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 3;
1134 qglTexCoord3f(p[0], p[1], p[2]);
1136 else if (gl_state.units[j].arraycomponents == 2)
1138 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 2;
1139 qglTexCoord2f(p[0], p[1]);
1143 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 1;
1144 qglTexCoord1f(p[0]);
1149 if (gl_state.pointer_color && gl_state.pointer_color_enabled)
1151 p = ((const GLfloat *)(gl_state.pointer_color)) + elements[i] * 4;
1152 qglColor4f(p[0], p[1], p[2], p[3]);
1154 p = ((const GLfloat *)(gl_state.pointer_vertex)) + elements[i] * 3;
1155 qglVertex3f(p[0], p[1], p[2]);
1160 else if (gl_mesh_testarrayelement.integer)
1163 qglBegin(GL_TRIANGLES);
1164 for (i = 0;i < numtriangles * 3;i++)
1166 qglArrayElement(elements[i]);
1171 else if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1173 GL_BindEBO(bufferobject);
1174 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices, numelements, GL_UNSIGNED_INT, bufferobject ? (void *)bufferoffset : elements);
1179 GL_BindEBO(bufferobject);
1180 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, bufferobject ? (void *)bufferoffset : elements);
1186 // restores backend state, used when done with 3D rendering
1187 void R_Mesh_Finish(void)
1192 GL_LockArrays(0, 0);
1195 for (i = 0;i < backendimageunits;i++)
1197 GL_ActiveTexture(i);
1198 qglBindTexture(GL_TEXTURE_1D, 0);CHECKGLERROR
1199 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
1202 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
1204 if (gl_texturecubemap)
1206 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
1209 for (i = 0;i < backendarrayunits;i++)
1211 GL_ActiveTexture(backendarrayunits - 1 - i);
1212 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1214 for (i = 0;i < backendunits;i++)
1216 GL_ActiveTexture(backendunits - 1 - i);
1217 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1218 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1221 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1223 if (gl_texturecubemap)
1225 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1227 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
1228 if (gl_combine.integer)
1230 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
1231 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
1234 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1235 qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
1237 qglDisable(GL_BLEND);CHECKGLERROR
1238 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1239 qglDepthMask(GL_TRUE);CHECKGLERROR
1240 qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);CHECKGLERROR
1243 int R_Mesh_CreateStaticBufferObject(unsigned int target, void *data, size_t size, const char *name)
1245 gl_bufferobjectinfo_t *info;
1246 GLuint bufferobject;
1248 if (!gl_vbo.integer)
1251 qglGenBuffersARB(1, &bufferobject);
1254 case GL_ELEMENT_ARRAY_BUFFER_ARB: GL_BindEBO(bufferobject);break;
1255 case GL_ARRAY_BUFFER_ARB: GL_BindVBO(bufferobject);break;
1256 default: Sys_Error("R_Mesh_CreateStaticBufferObject: unknown target type %i\n", target);return 0;
1258 qglBufferDataARB(target, size, data, GL_STATIC_DRAW_ARB);
1260 info = Mem_ExpandableArray_AllocRecord(&gl_bufferobjectinfoarray);
1261 memset(info, 0, sizeof(*info));
1262 info->target = target;
1263 info->object = bufferobject;
1265 strlcpy(info->name, name, sizeof(info->name));
1267 return (int)bufferobject;
1270 void R_Mesh_DestroyBufferObject(int bufferobject)
1273 gl_bufferobjectinfo_t *info;
1275 qglDeleteBuffersARB(1, (GLuint *)&bufferobject);
1277 endindex = Mem_ExpandableArray_IndexRange(&gl_bufferobjectinfoarray);
1278 for (i = 0;i < endindex;i++)
1280 info = Mem_ExpandableArray_RecordAtIndex(&gl_bufferobjectinfoarray, i);
1283 if (info->object == bufferobject)
1285 Mem_ExpandableArray_FreeRecord(&gl_bufferobjectinfoarray, (void *)info);
1291 void GL_Mesh_ListVBOs(qboolean printeach)
1294 size_t ebocount = 0, ebomemory = 0;
1295 size_t vbocount = 0, vbomemory = 0;
1296 gl_bufferobjectinfo_t *info;
1297 endindex = Mem_ExpandableArray_IndexRange(&gl_bufferobjectinfoarray);
1298 for (i = 0;i < endindex;i++)
1300 info = Mem_ExpandableArray_RecordAtIndex(&gl_bufferobjectinfoarray, i);
1303 switch(info->target)
1305 case GL_ELEMENT_ARRAY_BUFFER_ARB: ebocount++;ebomemory += info->size;if (printeach) Con_Printf("EBO #%i %s = %i bytes\n", info->object, info->name, (int)info->size);break;
1306 case GL_ARRAY_BUFFER_ARB: vbocount++;vbomemory += info->size;if (printeach) Con_Printf("VBO #%i %s = %i bytes\n", info->object, info->name, (int)info->size);break;
1307 default: Con_Printf("gl_vbostats: unknown target type %i\n", info->target);break;
1310 Con_Printf("vertex buffers: %i element buffers totalling %i bytes (%.3f MB), %i vertex buffers totalling %i bytes (%.3f MB), combined %i bytes (%.3fMB)\n", (int)ebocount, (int)ebomemory, ebomemory / 1048576.0, (int)vbocount, (int)vbomemory, vbomemory / 1048576.0, (int)(ebomemory + vbomemory), (ebomemory + vbomemory) / 1048576.0);
1313 void R_Mesh_Matrix(const matrix4x4_t *matrix)
1315 if (memcmp(matrix, &backend_modelmatrix, sizeof(matrix4x4_t)))
1317 double glmatrix[16];
1318 backend_modelmatrix = *matrix;
1319 Matrix4x4_Concat(&backend_modelviewmatrix, &backend_viewmatrix, matrix);
1320 Matrix4x4_ToArrayDoubleGL(&backend_modelviewmatrix, glmatrix);
1322 qglLoadMatrixd(glmatrix);CHECKGLERROR
1326 void R_Mesh_VertexPointer(const float *vertex3f, int bufferobject, size_t bufferoffset)
1328 if (!gl_vbo.integer)
1330 if (gl_state.pointer_vertex != vertex3f || gl_state.pointer_vertex_buffer != bufferobject || gl_state.pointer_vertex_offset != bufferoffset)
1332 gl_state.pointer_vertex = vertex3f;
1333 gl_state.pointer_vertex_buffer = bufferobject;
1334 gl_state.pointer_vertex_offset = bufferoffset;
1336 GL_BindVBO(bufferobject);
1337 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), bufferobject ? (void *)bufferoffset : vertex3f);CHECKGLERROR
1341 void R_Mesh_ColorPointer(const float *color4f, int bufferobject, size_t bufferoffset)
1343 // note: this can not rely on bufferobject to decide whether a color array
1344 // is supplied, because surfmesh_t shares one vbo for all arrays, which
1345 // means that a valid vbo may be supplied even if there is no color array.
1348 if (!gl_vbo.integer)
1350 // caller wants color array enabled
1351 if (!gl_state.pointer_color_enabled)
1353 gl_state.pointer_color_enabled = true;
1355 qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1357 if (gl_state.pointer_color != color4f || gl_state.pointer_color_buffer != bufferobject || gl_state.pointer_color_offset != bufferoffset)
1359 gl_state.pointer_color = color4f;
1360 gl_state.pointer_color_buffer = bufferobject;
1361 gl_state.pointer_color_offset = bufferoffset;
1363 GL_BindVBO(bufferobject);
1364 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), bufferobject ? (void *)bufferoffset : color4f);CHECKGLERROR
1369 // caller wants color array disabled
1370 if (gl_state.pointer_color_enabled)
1372 gl_state.pointer_color_enabled = false;
1374 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1375 // when color array is on the glColor gets trashed, set it again
1376 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);CHECKGLERROR
1381 void R_Mesh_TexCoordPointer(unsigned int unitnum, unsigned int numcomponents, const float *texcoord, int bufferobject, size_t bufferoffset)
1383 gltextureunit_t *unit = gl_state.units + unitnum;
1384 // update array settings
1386 // note: there is no need to check bufferobject here because all cases
1387 // that involve a valid bufferobject also supply a texcoord array
1390 if (!gl_vbo.integer)
1392 // texture array unit is enabled, enable the array
1393 if (!unit->arrayenabled)
1395 unit->arrayenabled = true;
1396 GL_ClientActiveTexture(unitnum);
1397 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1400 if (unit->pointer_texcoord != texcoord || unit->pointer_texcoord_buffer != bufferobject || unit->pointer_texcoord_offset != bufferoffset || unit->arraycomponents != numcomponents)
1402 unit->pointer_texcoord = texcoord;
1403 unit->pointer_texcoord_buffer = bufferobject;
1404 unit->pointer_texcoord_offset = bufferoffset;
1405 unit->arraycomponents = numcomponents;
1406 GL_ClientActiveTexture(unitnum);
1407 GL_BindVBO(bufferobject);
1408 qglTexCoordPointer(unit->arraycomponents, GL_FLOAT, sizeof(float) * unit->arraycomponents, bufferobject ? (void *)bufferoffset : texcoord);CHECKGLERROR
1413 // texture array unit is disabled, disable the array
1414 if (unit->arrayenabled)
1416 unit->arrayenabled = false;
1417 GL_ClientActiveTexture(unitnum);
1418 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1423 void R_Mesh_TexBindAll(unsigned int unitnum, int tex1d, int tex2d, int tex3d, int texcubemap)
1425 gltextureunit_t *unit = gl_state.units + unitnum;
1426 if (unitnum >= backendimageunits)
1428 // update 1d texture binding
1429 if (unit->t1d != tex1d)
1431 GL_ActiveTexture(unitnum);
1432 if (unitnum < backendunits)
1438 qglEnable(GL_TEXTURE_1D);CHECKGLERROR
1445 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1450 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1452 // update 2d texture binding
1453 if (unit->t2d != tex2d)
1455 GL_ActiveTexture(unitnum);
1456 if (unitnum < backendunits)
1462 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
1469 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1474 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1476 // update 3d texture binding
1477 if (unit->t3d != tex3d)
1479 GL_ActiveTexture(unitnum);
1480 if (unitnum < backendunits)
1486 qglEnable(GL_TEXTURE_3D);CHECKGLERROR
1493 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1498 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1500 // update cubemap texture binding
1501 if (unit->tcubemap != texcubemap)
1503 GL_ActiveTexture(unitnum);
1504 if (unitnum < backendunits)
1508 if (unit->tcubemap == 0)
1510 qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1517 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1521 unit->tcubemap = texcubemap;
1522 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1526 void R_Mesh_TexBind1D(unsigned int unitnum, int texnum)
1528 gltextureunit_t *unit = gl_state.units + unitnum;
1529 if (unitnum >= backendimageunits)
1531 // update 1d texture binding
1532 if (unit->t1d != texnum)
1534 GL_ActiveTexture(unitnum);
1535 if (unitnum < backendunits)
1541 qglEnable(GL_TEXTURE_1D);CHECKGLERROR
1548 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1553 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1555 // update 2d texture binding
1558 GL_ActiveTexture(unitnum);
1559 if (unitnum < backendunits)
1563 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1567 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1569 // update 3d texture binding
1572 GL_ActiveTexture(unitnum);
1573 if (unitnum < backendunits)
1577 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1581 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1583 // update cubemap texture binding
1586 GL_ActiveTexture(unitnum);
1587 if (unitnum < backendunits)
1591 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1595 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1599 void R_Mesh_TexBind(unsigned int unitnum, int texnum)
1601 gltextureunit_t *unit = gl_state.units + unitnum;
1602 if (unitnum >= backendimageunits)
1604 // update 1d texture binding
1607 GL_ActiveTexture(unitnum);
1608 if (unitnum < backendunits)
1612 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1616 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1618 // update 2d texture binding
1619 if (unit->t2d != texnum)
1621 GL_ActiveTexture(unitnum);
1622 if (unitnum < backendunits)
1628 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
1635 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1640 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1642 // update 3d texture binding
1645 GL_ActiveTexture(unitnum);
1646 if (unitnum < backendunits)
1650 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1654 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1656 // update cubemap texture binding
1657 if (unit->tcubemap != 0)
1659 GL_ActiveTexture(unitnum);
1660 if (unitnum < backendunits)
1664 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1668 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1672 void R_Mesh_TexBind3D(unsigned int unitnum, int texnum)
1674 gltextureunit_t *unit = gl_state.units + unitnum;
1675 if (unitnum >= backendimageunits)
1677 // update 1d texture binding
1680 GL_ActiveTexture(unitnum);
1681 if (unitnum < backendunits)
1685 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1689 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1691 // update 2d texture binding
1694 GL_ActiveTexture(unitnum);
1695 if (unitnum < backendunits)
1699 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1703 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1705 // update 3d texture binding
1706 if (unit->t3d != texnum)
1708 GL_ActiveTexture(unitnum);
1709 if (unitnum < backendunits)
1715 qglEnable(GL_TEXTURE_3D);CHECKGLERROR
1722 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1727 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1729 // update cubemap texture binding
1730 if (unit->tcubemap != 0)
1732 GL_ActiveTexture(unitnum);
1733 if (unitnum < backendunits)
1737 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1741 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1745 void R_Mesh_TexBindCubeMap(unsigned int unitnum, int texnum)
1747 gltextureunit_t *unit = gl_state.units + unitnum;
1748 if (unitnum >= backendimageunits)
1750 // update 1d texture binding
1753 GL_ActiveTexture(unitnum);
1754 if (unitnum < backendunits)
1758 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1762 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1764 // update 2d texture binding
1767 GL_ActiveTexture(unitnum);
1768 if (unitnum < backendunits)
1772 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1776 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1778 // update 3d texture binding
1781 GL_ActiveTexture(unitnum);
1782 if (unitnum < backendunits)
1786 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1790 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1792 // update cubemap texture binding
1793 if (unit->tcubemap != texnum)
1795 GL_ActiveTexture(unitnum);
1796 if (unitnum < backendunits)
1800 if (unit->tcubemap == 0)
1802 qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1809 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1813 unit->tcubemap = texnum;
1814 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1818 void R_Mesh_TexMatrix(unsigned int unitnum, const matrix4x4_t *matrix)
1820 gltextureunit_t *unit = gl_state.units + unitnum;
1821 if (matrix->m[3][3])
1823 // texmatrix specified, check if it is different
1824 if (!unit->texmatrixenabled || memcmp(&unit->matrix, matrix, sizeof(matrix4x4_t)))
1826 double glmatrix[16];
1827 unit->texmatrixenabled = true;
1828 unit->matrix = *matrix;
1830 Matrix4x4_ToArrayDoubleGL(&unit->matrix, glmatrix);
1831 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1832 GL_ActiveTexture(unitnum);
1833 qglLoadMatrixd(glmatrix);CHECKGLERROR
1834 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1839 // no texmatrix specified, revert to identity
1840 if (unit->texmatrixenabled)
1842 unit->texmatrixenabled = false;
1844 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1845 GL_ActiveTexture(unitnum);
1846 qglLoadIdentity();CHECKGLERROR
1847 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1852 void R_Mesh_TexCombine(unsigned int unitnum, int combinergb, int combinealpha, int rgbscale, int alphascale)
1854 gltextureunit_t *unit = gl_state.units + unitnum;
1856 if (gl_combine.integer)
1858 // GL_ARB_texture_env_combine
1860 combinergb = GL_MODULATE;
1862 combinealpha = GL_MODULATE;
1867 if (unit->combinergb != combinergb)
1869 unit->combinergb = combinergb;
1870 GL_ActiveTexture(unitnum);
1871 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
1873 if (unit->combinealpha != combinealpha)
1875 unit->combinealpha = combinealpha;
1876 GL_ActiveTexture(unitnum);
1877 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
1879 if (unit->rgbscale != rgbscale)
1881 GL_ActiveTexture(unitnum);
1882 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (unit->rgbscale = rgbscale));CHECKGLERROR
1884 if (unit->alphascale != alphascale)
1886 GL_ActiveTexture(unitnum);
1887 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, (unit->alphascale = alphascale));CHECKGLERROR
1894 combinergb = GL_MODULATE;
1895 if (unit->combinergb != combinergb)
1897 unit->combinergb = combinergb;
1898 GL_ActiveTexture(unitnum);
1899 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combinergb);CHECKGLERROR
1904 void R_Mesh_TextureState(const rmeshstate_t *m)
1911 if (gl_backend_rebindtextures)
1913 gl_backend_rebindtextures = false;
1914 GL_SetupTextureState();
1918 for (i = 0;i < backendimageunits;i++)
1919 R_Mesh_TexBindAll(i, m->tex1d[i], m->tex[i], m->tex3d[i], m->texcubemap[i]);
1920 for (i = 0;i < backendarrayunits;i++)
1922 if (m->pointer_texcoord3f[i])
1923 R_Mesh_TexCoordPointer(i, 3, m->pointer_texcoord3f[i], m->pointer_texcoord_bufferobject[i], m->pointer_texcoord_bufferoffset[i]);
1925 R_Mesh_TexCoordPointer(i, 2, m->pointer_texcoord[i], m->pointer_texcoord_bufferobject[i], m->pointer_texcoord_bufferoffset[i]);
1927 for (i = 0;i < backendunits;i++)
1929 R_Mesh_TexMatrix(i, &m->texmatrix[i]);
1930 R_Mesh_TexCombine(i, m->texcombinergb[i], m->texcombinealpha[i], m->texrgbscale[i], m->texalphascale[i]);
1935 void R_Mesh_ResetTextureState(void)
1937 unsigned int unitnum;
1942 if (gl_backend_rebindtextures)
1944 gl_backend_rebindtextures = false;
1945 GL_SetupTextureState();
1949 for (unitnum = 0;unitnum < backendimageunits;unitnum++)
1951 gltextureunit_t *unit = gl_state.units + unitnum;
1952 // update 1d texture binding
1955 GL_ActiveTexture(unitnum);
1956 if (unitnum < backendunits)
1958 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1961 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1963 // update 2d texture binding
1966 GL_ActiveTexture(unitnum);
1967 if (unitnum < backendunits)
1969 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1972 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1974 // update 3d texture binding
1977 GL_ActiveTexture(unitnum);
1978 if (unitnum < backendunits)
1980 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1983 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1985 // update cubemap texture binding
1988 GL_ActiveTexture(unitnum);
1989 if (unitnum < backendunits)
1991 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1994 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1997 for (unitnum = 0;unitnum < backendarrayunits;unitnum++)
1999 gltextureunit_t *unit = gl_state.units + unitnum;
2000 // texture array unit is disabled, disable the array
2001 if (unit->arrayenabled)
2003 unit->arrayenabled = false;
2004 GL_ClientActiveTexture(unitnum);
2005 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
2008 for (unitnum = 0;unitnum < backendunits;unitnum++)
2010 gltextureunit_t *unit = gl_state.units + unitnum;
2011 // no texmatrix specified, revert to identity
2012 if (unit->texmatrixenabled)
2014 unit->texmatrixenabled = false;
2016 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
2017 GL_ActiveTexture(unitnum);
2018 qglLoadIdentity();CHECKGLERROR
2019 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
2021 if (gl_combine.integer)
2023 // GL_ARB_texture_env_combine
2024 if (unit->combinergb != GL_MODULATE)
2026 unit->combinergb = GL_MODULATE;
2027 GL_ActiveTexture(unitnum);
2028 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
2030 if (unit->combinealpha != GL_MODULATE)
2032 unit->combinealpha = GL_MODULATE;
2033 GL_ActiveTexture(unitnum);
2034 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
2036 if (unit->rgbscale != 1)
2038 GL_ActiveTexture(unitnum);
2039 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (unit->rgbscale = 1));CHECKGLERROR
2041 if (unit->alphascale != 1)
2043 GL_ActiveTexture(unitnum);
2044 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, (unit->alphascale = 1));CHECKGLERROR
2050 if (unit->combinergb != GL_MODULATE)
2052 unit->combinergb = GL_MODULATE;
2053 GL_ActiveTexture(unitnum);
2054 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combinergb);CHECKGLERROR
2060 void R_Mesh_Draw_ShowTris(int firstvertex, int numvertices, int numtriangles, const int *elements)
2064 for (;numtriangles;numtriangles--, elements += 3)
2066 qglArrayElement(elements[0]);qglArrayElement(elements[1]);
2067 qglArrayElement(elements[1]);qglArrayElement(elements[2]);
2068 qglArrayElement(elements[2]);qglArrayElement(elements[0]);