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\n");
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]);
302 memset(&backend_modelmatrix, 0, sizeof(backend_modelmatrix));
305 void GL_SetupView_Mode_Perspective (double frustumx, double frustumy, double zNear, double zFar)
311 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
312 qglLoadIdentity();CHECKGLERROR
314 qglFrustum(-frustumx * zNear, frustumx * zNear, -frustumy * zNear, frustumy * zNear, zNear, zFar);CHECKGLERROR
315 qglGetDoublev(GL_PROJECTION_MATRIX, m);CHECKGLERROR
316 Matrix4x4_FromArrayDoubleGL(&backend_projectmatrix, m);
317 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
318 GL_SetupView_Orientation_Identity();
322 void GL_SetupView_Mode_PerspectiveInfiniteFarClip (double frustumx, double frustumy, double zNear)
328 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
329 qglLoadIdentity();CHECKGLERROR
331 nudge = 1.0 - 1.0 / (1<<23);
332 m[ 0] = 1.0 / frustumx;
337 m[ 5] = 1.0 / frustumy;
346 m[14] = -2 * zNear * nudge;
348 qglLoadMatrixd(m);CHECKGLERROR
349 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
350 GL_SetupView_Orientation_Identity();
352 Matrix4x4_FromArrayDoubleGL(&backend_projectmatrix, m);
355 void GL_SetupView_Mode_Ortho (double x1, double y1, double x2, double y2, double zNear, double zFar)
361 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
362 qglLoadIdentity();CHECKGLERROR
363 qglOrtho(x1, x2, y2, y1, zNear, zFar);CHECKGLERROR
364 qglGetDoublev(GL_PROJECTION_MATRIX, m);CHECKGLERROR
365 Matrix4x4_FromArrayDoubleGL(&backend_projectmatrix, m);
366 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
367 GL_SetupView_Orientation_Identity();
371 void GL_SetupView_ApplyCustomNearClipPlane(double normalx, double normaly, double normalz, double dist)
376 float clipPlane[4], v3[3], v4[3];
379 // This is Olique Depth Projection from http://www.terathon.com/code/oblique.php
380 // modified to fit in this codebase.
382 VectorSet(normal, normalx, normaly, normalz);
383 Matrix4x4_Transform3x3(&backend_viewmatrix, normal, clipPlane);
384 VectorScale(normal, dist, v3);
385 Matrix4x4_Transform(&backend_viewmatrix, v3, v4);
386 // FIXME: LordHavoc: I think this can be done more efficiently somehow but I can't remember the technique
387 clipPlane[3] = -DotProduct(v4, clipPlane);
391 // testing code for comparing results
393 VectorCopy4(clipPlane, clipPlane2);
394 R_Mesh_Matrix(&identitymatrix);
395 VectorSet(q, normal[0], normal[1], normal[2], -dist);
396 qglClipPlane(GL_CLIP_PLANE0, q);
397 qglGetClipPlane(GL_CLIP_PLANE0, q);
398 VectorCopy4(q, clipPlane);
402 // Calculate the clip-space corner point opposite the clipping plane
403 // as (sgn(clipPlane.x), sgn(clipPlane.y), 1, 1) and
404 // transform it into camera space by multiplying it
405 // by the inverse of the projection matrix
406 Matrix4x4_ToArrayDoubleGL(&backend_projectmatrix, matrix);
408 q[0] = ((clipPlane[0] < 0.0f ? -1.0f : clipPlane[0] > 0.0f ? 1.0f : 0.0f) + matrix[8]) / matrix[0];
409 q[1] = ((clipPlane[1] < 0.0f ? -1.0f : clipPlane[1] > 0.0f ? 1.0f : 0.0f) + matrix[9]) / matrix[5];
411 q[3] = (1.0f + matrix[10]) / matrix[14];
413 // Calculate the scaled plane vector
414 d = 2.0f / DotProduct4(clipPlane, q);
416 // Replace the third row of the projection matrix
417 matrix[2] = clipPlane[0] * d;
418 matrix[6] = clipPlane[1] * d;
419 matrix[10] = clipPlane[2] * d + 1.0f;
420 matrix[14] = clipPlane[3] * d;
422 // Load it back into OpenGL
423 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
424 qglLoadMatrixd(matrix);CHECKGLERROR
425 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
427 Matrix4x4_FromArrayDoubleGL(&backend_projectmatrix, matrix);
430 typedef struct gltextureunit_s
432 const void *pointer_texcoord;
433 size_t pointer_texcoord_offset;
434 int pointer_texcoord_buffer;
435 int t1d, t2d, t3d, tcubemap;
437 unsigned int arraycomponents;
438 int rgbscale, alphascale;
439 int combinergb, combinealpha;
440 // FIXME: add more combine stuff
441 // texmatrixenabled exists only to avoid unnecessary texmatrix compares
442 int texmatrixenabled;
447 static struct gl_state_s
455 int colormask; // stored as bottom 4 bits: r g b a (3 2 1 0 order)
458 float polygonoffset[2];
462 unsigned int clientunit;
463 gltextureunit_t units[MAX_TEXTUREUNITS];
467 int vertexbufferobject;
468 int elementbufferobject;
469 qboolean pointer_color_enabled;
470 const void *pointer_vertex;
471 const void *pointer_color;
472 size_t pointer_vertex_offset;
473 size_t pointer_color_offset;
474 int pointer_vertex_buffer;
475 int pointer_color_buffer;
479 static void GL_BindVBO(int bufferobject)
481 if (gl_state.vertexbufferobject != bufferobject)
483 gl_state.vertexbufferobject = bufferobject;
485 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, bufferobject);
490 static void GL_BindEBO(int bufferobject)
492 if (gl_state.elementbufferobject != bufferobject)
494 gl_state.elementbufferobject = bufferobject;
496 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, bufferobject);
501 void GL_SetupTextureState(void)
504 gltextureunit_t *unit;
506 gl_state.unit = MAX_TEXTUREUNITS;
507 gl_state.clientunit = MAX_TEXTUREUNITS;
508 for (i = 0;i < MAX_TEXTUREUNITS;i++)
510 unit = gl_state.units + i;
515 unit->arrayenabled = false;
516 unit->arraycomponents = 0;
517 unit->pointer_texcoord = NULL;
518 unit->pointer_texcoord_buffer = 0;
519 unit->pointer_texcoord_offset = 0;
521 unit->alphascale = 1;
522 unit->combinergb = GL_MODULATE;
523 unit->combinealpha = GL_MODULATE;
524 unit->texmatrixenabled = false;
525 unit->matrix = identitymatrix;
528 for (i = 0;i < backendimageunits;i++)
531 qglBindTexture(GL_TEXTURE_1D, 0);CHECKGLERROR
532 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
535 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
537 if (gl_texturecubemap)
539 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
543 for (i = 0;i < backendarrayunits;i++)
545 GL_ClientActiveTexture(i);
547 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
548 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
551 for (i = 0;i < backendunits;i++)
554 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
555 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
558 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
560 if (gl_texturecubemap)
562 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
564 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
565 qglLoadIdentity();CHECKGLERROR
566 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
567 if (gl_combine.integer)
569 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);CHECKGLERROR
570 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);CHECKGLERROR
571 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);CHECKGLERROR
572 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
573 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_TEXTURE);CHECKGLERROR // for GL_INTERPOLATE_ARB mode
574 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
575 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
576 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);CHECKGLERROR
577 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);CHECKGLERROR
578 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);CHECKGLERROR
579 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
580 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, GL_CONSTANT_ARB);CHECKGLERROR
581 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
582 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
583 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
584 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
585 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
589 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
596 void GL_Backend_ResetState(void)
598 memset(&gl_state, 0, sizeof(gl_state));
599 gl_state.depthtest = true;
600 gl_state.alphatest = false;
601 gl_state.blendfunc1 = GL_ONE;
602 gl_state.blendfunc2 = GL_ZERO;
603 gl_state.blend = false;
604 gl_state.depthmask = GL_TRUE;
605 gl_state.colormask = 15;
606 gl_state.color4f[0] = gl_state.color4f[1] = gl_state.color4f[2] = gl_state.color4f[3] = 1;
607 gl_state.lockrange_first = 0;
608 gl_state.lockrange_count = 0;
609 gl_state.cullface = v_flipped_state ? GL_BACK : GL_FRONT; // quake is backwards, this culls back faces
610 gl_state.cullfaceenable = true;
611 gl_state.polygonoffset[0] = 0;
612 gl_state.polygonoffset[1] = 0;
616 qglColorMask(1, 1, 1, 1);
617 qglAlphaFunc(GL_GEQUAL, 0.5);CHECKGLERROR
618 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
619 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
620 qglDisable(GL_BLEND);CHECKGLERROR
621 qglCullFace(gl_state.cullface);CHECKGLERROR
622 qglEnable(GL_CULL_FACE);CHECKGLERROR
623 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
624 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
625 qglDepthMask(gl_state.depthmask);CHECKGLERROR
626 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
628 if (gl_support_arb_vertex_buffer_object)
630 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
631 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
634 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), NULL);CHECKGLERROR
635 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
637 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL);CHECKGLERROR
638 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
640 GL_Color(0, 0, 0, 0);
641 GL_Color(1, 1, 1, 1);
643 GL_SetupTextureState();
646 void GL_ActiveTexture(unsigned int num)
648 if (gl_state.unit != num)
651 if (qglActiveTexture)
654 qglActiveTexture(GL_TEXTURE0_ARB + gl_state.unit);
660 void GL_ClientActiveTexture(unsigned int num)
662 if (gl_state.clientunit != num)
664 gl_state.clientunit = num;
665 if (qglActiveTexture)
668 qglClientActiveTexture(GL_TEXTURE0_ARB + gl_state.clientunit);
674 void GL_BlendFunc(int blendfunc1, int blendfunc2)
676 if (gl_state.blendfunc1 != blendfunc1 || gl_state.blendfunc2 != blendfunc2)
679 qglBlendFunc(gl_state.blendfunc1 = blendfunc1, gl_state.blendfunc2 = blendfunc2);CHECKGLERROR
680 if (gl_state.blendfunc2 == GL_ZERO)
682 if (gl_state.blendfunc1 == GL_ONE)
687 qglDisable(GL_BLEND);CHECKGLERROR
695 qglEnable(GL_BLEND);CHECKGLERROR
704 qglEnable(GL_BLEND);CHECKGLERROR
710 void GL_DepthMask(int state)
712 if (gl_state.depthmask != state)
715 qglDepthMask(gl_state.depthmask = state);CHECKGLERROR
719 void GL_DepthTest(int state)
721 if (gl_state.depthtest != state)
723 gl_state.depthtest = state;
725 if (gl_state.depthtest)
727 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
731 qglDisable(GL_DEPTH_TEST);CHECKGLERROR
736 void GL_DepthRange(float nearfrac, float farfrac)
738 if (gl_state.depthrange[0] != nearfrac || gl_state.depthrange[1] != farfrac)
740 gl_state.depthrange[0] = nearfrac;
741 gl_state.depthrange[1] = farfrac;
742 qglDepthRange(nearfrac, farfrac);
746 void GL_PolygonOffset(float planeoffset, float depthoffset)
748 if (gl_state.polygonoffset[0] != planeoffset || gl_state.polygonoffset[1] != depthoffset)
750 gl_state.polygonoffset[0] = planeoffset;
751 gl_state.polygonoffset[1] = depthoffset;
752 qglPolygonOffset(planeoffset, depthoffset);
756 void GL_SetMirrorState(qboolean state)
758 if(!state != !v_flipped_state)
760 // change cull face mode!
761 if(gl_state.cullface == GL_BACK)
762 qglCullFace((gl_state.cullface = GL_FRONT));
763 else if(gl_state.cullface == GL_FRONT)
764 qglCullFace((gl_state.cullface = GL_BACK));
766 v_flipped_state = state;
769 void GL_CullFace(int state)
775 if(state == GL_FRONT)
777 else if(state == GL_BACK)
781 if (state != GL_NONE)
783 if (!gl_state.cullfaceenable)
785 gl_state.cullfaceenable = true;
786 qglEnable(GL_CULL_FACE);CHECKGLERROR
788 if (gl_state.cullface != state)
790 gl_state.cullface = state;
791 qglCullFace(gl_state.cullface);CHECKGLERROR
796 if (gl_state.cullfaceenable)
798 gl_state.cullfaceenable = false;
799 qglDisable(GL_CULL_FACE);CHECKGLERROR
804 void GL_AlphaTest(int state)
806 if (gl_state.alphatest != state)
808 gl_state.alphatest = state;
810 if (gl_state.alphatest)
812 qglEnable(GL_ALPHA_TEST);CHECKGLERROR
816 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
821 void GL_ColorMask(int r, int g, int b, int a)
823 int state = r*8 + g*4 + b*2 + a*1;
824 if (gl_state.colormask != state)
826 gl_state.colormask = state;
828 qglColorMask((GLboolean)r, (GLboolean)g, (GLboolean)b, (GLboolean)a);CHECKGLERROR
832 void GL_Color(float cr, float cg, float cb, float ca)
834 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)
836 gl_state.color4f[0] = cr;
837 gl_state.color4f[1] = cg;
838 gl_state.color4f[2] = cb;
839 gl_state.color4f[3] = ca;
841 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);
846 void GL_LockArrays(int first, int count)
848 if (count < gl_lockarrays_minimumvertices.integer)
853 if (gl_state.lockrange_count != count || gl_state.lockrange_first != first)
855 if (gl_state.lockrange_count)
857 gl_state.lockrange_count = 0;
859 qglUnlockArraysEXT();
862 if (count && gl_supportslockarrays && gl_lockarrays.integer && r_render.integer)
864 gl_state.lockrange_first = first;
865 gl_state.lockrange_count = count;
867 qglLockArraysEXT(first, count);
873 void GL_Scissor (int x, int y, int width, int height)
876 qglScissor(x, vid.height - (y + height),width,height);
880 void GL_ScissorTest(int state)
882 if(gl_state.scissortest == state)
886 if((gl_state.scissortest = state))
887 qglEnable(GL_SCISSOR_TEST);
889 qglDisable(GL_SCISSOR_TEST);
893 void GL_Clear(int mask)
896 qglClear(mask);CHECKGLERROR
899 void GL_TransformToScreen(const vec4_t in, vec4_t out)
903 Matrix4x4_Transform4 (&backend_viewmatrix, in, temp);
904 Matrix4x4_Transform4 (&backend_projectmatrix, temp, out);
906 out[0] = r_refdef.view.x + (out[0] * iw + 1.0f) * r_refdef.view.width * 0.5f;
907 out[1] = r_refdef.view.y + r_refdef.view.height - (out[1] * iw + 1.0f) * r_refdef.view.height * 0.5f;
908 out[2] = r_refdef.view.z + (out[2] * iw + 1.0f) * r_refdef.view.depth * 0.5f;
911 // called at beginning of frame
912 void R_Mesh_Start(void)
916 if (gl_printcheckerror.integer && !gl_paranoid.integer)
918 Con_Printf("WARNING: gl_printcheckerror is on but gl_paranoid is off, turning it on...\n");
919 Cvar_SetValueQuick(&gl_paranoid, 1);
921 GL_Backend_ResetState();
924 qboolean GL_Backend_CompileShader(int programobject, GLenum shadertypeenum, const char *shadertype, int numstrings, const char **strings)
928 char compilelog[MAX_INPUTLINE];
929 shaderobject = qglCreateShaderObjectARB(shadertypeenum);CHECKGLERROR
932 qglShaderSourceARB(shaderobject, numstrings, strings, NULL);CHECKGLERROR
933 qglCompileShaderARB(shaderobject);CHECKGLERROR
934 qglGetObjectParameterivARB(shaderobject, GL_OBJECT_COMPILE_STATUS_ARB, &shadercompiled);CHECKGLERROR
935 qglGetInfoLogARB(shaderobject, sizeof(compilelog), NULL, compilelog);CHECKGLERROR
936 if (compilelog[0] && developer.integer > 0)
938 int i, j, pretextlines = 0;
939 for (i = 0;i < numstrings - 1;i++)
940 for (j = 0;strings[i][j];j++)
941 if (strings[i][j] == '\n')
943 Con_DPrintf("%s shader compile log:\n%s\n(line offset for any above warnings/errors: %i)\n", shadertype, compilelog, pretextlines);
947 qglDeleteObjectARB(shaderobject);CHECKGLERROR
950 qglAttachObjectARB(programobject, shaderobject);CHECKGLERROR
951 qglDeleteObjectARB(shaderobject);CHECKGLERROR
955 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)
958 GLuint programobject = 0;
959 char linklog[MAX_INPUTLINE];
962 programobject = qglCreateProgramObjectARB();CHECKGLERROR
966 if (vertexstrings_count && !GL_Backend_CompileShader(programobject, GL_VERTEX_SHADER_ARB, "vertex", vertexstrings_count, vertexstrings_list))
969 #ifdef GL_GEOMETRY_SHADER_ARB
970 if (geometrystrings_count && !GL_Backend_CompileShader(programobject, GL_GEOMETRY_SHADER_ARB, "geometry", geometrystrings_count, geometrystrings_list))
974 if (fragmentstrings_count && !GL_Backend_CompileShader(programobject, GL_FRAGMENT_SHADER_ARB, "fragment", fragmentstrings_count, fragmentstrings_list))
977 qglLinkProgramARB(programobject);CHECKGLERROR
978 qglGetObjectParameterivARB(programobject, GL_OBJECT_LINK_STATUS_ARB, &programlinked);CHECKGLERROR
979 qglGetInfoLogARB(programobject, sizeof(linklog), NULL, linklog);CHECKGLERROR
982 Con_DPrintf("program link log:\n%s\n", linklog);
983 // software vertex shader is ok but software fragment shader is WAY
984 // too slow, fail program if so.
985 // NOTE: this string might be ATI specific, but that's ok because the
986 // ATI R300 chip (Radeon 9500-9800/X300) is the most likely to use a
987 // software fragment shader due to low instruction and dependent
989 if (strstr(linklog, "fragment shader will run in software"))
990 programlinked = false;
994 return programobject;
996 qglDeleteObjectARB(programobject);CHECKGLERROR
1000 void GL_Backend_FreeProgram(unsigned int prog)
1003 qglDeleteObjectARB(prog);
1007 int gl_backend_rebindtextures;
1009 void GL_Backend_RenumberElements(int *out, int count, const int *in, int offset)
1014 for (i = 0;i < count;i++)
1015 *out++ = *in++ + offset;
1018 memcpy(out, in, sizeof(*out) * count);
1021 // renders triangles using vertices from the active arrays
1022 int paranoidblah = 0;
1023 void R_Mesh_Draw(int firstvertex, int numvertices, int numtriangles, const int *elements, int bufferobject, size_t bufferoffset)
1025 unsigned int numelements = numtriangles * 3;
1026 if (numvertices < 3 || numtriangles < 1)
1028 Con_Printf("R_Mesh_Draw(%d, %d, %d, %8p, %i, %p);\n", firstvertex, numvertices, numtriangles, elements, bufferobject, (void *)bufferoffset);
1031 if (gl_vbo.integer != 1)
1034 r_refdef.stats.meshes++;
1035 r_refdef.stats.meshes_elements += numelements;
1036 if (gl_paranoid.integer)
1038 unsigned int i, j, size;
1040 // note: there's no validation done here on buffer objects because it
1041 // is somewhat difficult to get at the data, and gl_paranoid can be
1042 // used without buffer objects if the need arises
1043 // (the data could be gotten using glMapBuffer but it would be very
1044 // slow due to uncachable video memory reads)
1045 if (!qglIsEnabled(GL_VERTEX_ARRAY))
1046 Con_Print("R_Mesh_Draw: vertex array not enabled\n");
1048 if (gl_state.pointer_vertex)
1049 for (j = 0, size = numvertices * 3, p = (int *)((float *)gl_state.pointer_vertex + firstvertex * 3);j < size;j++, p++)
1051 if (gl_state.pointer_color_enabled)
1053 if (!qglIsEnabled(GL_COLOR_ARRAY))
1054 Con_Print("R_Mesh_Draw: color array set but not enabled\n");
1056 if (gl_state.pointer_color && gl_state.pointer_color_enabled)
1057 for (j = 0, size = numvertices * 4, p = (int *)((float *)gl_state.pointer_color + firstvertex * 4);j < size;j++, p++)
1060 for (i = 0;i < backendarrayunits;i++)
1062 if (gl_state.units[i].arrayenabled)
1064 GL_ClientActiveTexture(i);
1065 if (!qglIsEnabled(GL_TEXTURE_COORD_ARRAY))
1066 Con_Print("R_Mesh_Draw: texcoord array set but not enabled\n");
1068 if (gl_state.units[i].pointer_texcoord && gl_state.units[i].arrayenabled)
1069 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++)
1073 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1075 if (elements[i] < firstvertex || elements[i] >= firstvertex + numvertices)
1077 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in elements list\n", elements[i], firstvertex, firstvertex + numvertices);
1083 if (r_render.integer)
1086 if (gl_mesh_testmanualfeeding.integer)
1090 qglBegin(GL_TRIANGLES);
1091 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1093 for (j = 0;j < backendarrayunits;j++)
1095 if (gl_state.units[j].pointer_texcoord && gl_state.units[j].arrayenabled)
1097 if (backendarrayunits > 1)
1099 if (gl_state.units[j].arraycomponents == 4)
1101 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 4;
1102 qglMultiTexCoord4f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2], p[3]);
1104 else if (gl_state.units[j].arraycomponents == 3)
1106 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 3;
1107 qglMultiTexCoord3f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2]);
1109 else if (gl_state.units[j].arraycomponents == 2)
1111 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 2;
1112 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, p[0], p[1]);
1116 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 1;
1117 qglMultiTexCoord1f(GL_TEXTURE0_ARB + j, p[0]);
1122 if (gl_state.units[j].arraycomponents == 4)
1124 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 4;
1125 qglTexCoord4f(p[0], p[1], p[2], p[3]);
1127 else if (gl_state.units[j].arraycomponents == 3)
1129 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 3;
1130 qglTexCoord3f(p[0], p[1], p[2]);
1132 else if (gl_state.units[j].arraycomponents == 2)
1134 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 2;
1135 qglTexCoord2f(p[0], p[1]);
1139 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 1;
1140 qglTexCoord1f(p[0]);
1145 if (gl_state.pointer_color && gl_state.pointer_color_enabled)
1147 p = ((const GLfloat *)(gl_state.pointer_color)) + elements[i] * 4;
1148 qglColor4f(p[0], p[1], p[2], p[3]);
1150 p = ((const GLfloat *)(gl_state.pointer_vertex)) + elements[i] * 3;
1151 qglVertex3f(p[0], p[1], p[2]);
1156 else if (gl_mesh_testarrayelement.integer)
1159 qglBegin(GL_TRIANGLES);
1160 for (i = 0;i < numtriangles * 3;i++)
1162 qglArrayElement(elements[i]);
1167 else if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1169 GL_BindEBO(bufferobject);
1170 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices, numelements, GL_UNSIGNED_INT, bufferobject ? (void *)bufferoffset : elements);
1175 GL_BindEBO(bufferobject);
1176 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, bufferobject ? (void *)bufferoffset : elements);
1182 // restores backend state, used when done with 3D rendering
1183 void R_Mesh_Finish(void)
1188 GL_LockArrays(0, 0);
1191 for (i = 0;i < backendimageunits;i++)
1193 GL_ActiveTexture(i);
1194 qglBindTexture(GL_TEXTURE_1D, 0);CHECKGLERROR
1195 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
1198 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
1200 if (gl_texturecubemap)
1202 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
1205 for (i = 0;i < backendarrayunits;i++)
1207 GL_ActiveTexture(backendarrayunits - 1 - i);
1208 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1210 for (i = 0;i < backendunits;i++)
1212 GL_ActiveTexture(backendunits - 1 - i);
1213 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1214 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1217 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1219 if (gl_texturecubemap)
1221 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1223 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
1224 if (gl_combine.integer)
1226 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
1227 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
1230 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1231 qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
1233 qglDisable(GL_BLEND);CHECKGLERROR
1234 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1235 qglDepthMask(GL_TRUE);CHECKGLERROR
1236 qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);CHECKGLERROR
1239 int R_Mesh_CreateStaticBufferObject(unsigned int target, void *data, size_t size, const char *name)
1241 gl_bufferobjectinfo_t *info;
1242 GLuint bufferobject;
1244 if (!gl_vbo.integer)
1247 qglGenBuffersARB(1, &bufferobject);
1250 case GL_ELEMENT_ARRAY_BUFFER_ARB: GL_BindEBO(bufferobject);break;
1251 case GL_ARRAY_BUFFER_ARB: GL_BindVBO(bufferobject);break;
1252 default: Sys_Error("R_Mesh_CreateStaticBufferObject: unknown target type %i\n", target);return 0;
1254 qglBufferDataARB(target, size, data, GL_STATIC_DRAW_ARB);
1256 info = Mem_ExpandableArray_AllocRecord(&gl_bufferobjectinfoarray);
1257 memset(info, 0, sizeof(*info));
1258 info->target = target;
1259 info->object = bufferobject;
1261 strlcpy(info->name, name, sizeof(info->name));
1263 return (int)bufferobject;
1266 void R_Mesh_DestroyBufferObject(int bufferobject)
1269 gl_bufferobjectinfo_t *info;
1271 qglDeleteBuffersARB(1, (GLuint *)&bufferobject);
1273 endindex = Mem_ExpandableArray_IndexRange(&gl_bufferobjectinfoarray);
1274 for (i = 0;i < endindex;i++)
1276 info = Mem_ExpandableArray_RecordAtIndex(&gl_bufferobjectinfoarray, i);
1279 if (info->object == bufferobject)
1281 Mem_ExpandableArray_FreeRecord(&gl_bufferobjectinfoarray, (void *)info);
1287 void GL_Mesh_ListVBOs(qboolean printeach)
1290 size_t ebocount = 0, ebomemory = 0;
1291 size_t vbocount = 0, vbomemory = 0;
1292 gl_bufferobjectinfo_t *info;
1293 endindex = Mem_ExpandableArray_IndexRange(&gl_bufferobjectinfoarray);
1294 for (i = 0;i < endindex;i++)
1296 info = Mem_ExpandableArray_RecordAtIndex(&gl_bufferobjectinfoarray, i);
1299 switch(info->target)
1301 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;
1302 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;
1303 default: Con_Printf("gl_vbostats: unknown target type %i\n", info->target);break;
1306 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);
1309 void R_Mesh_Matrix(const matrix4x4_t *matrix)
1311 if (memcmp(matrix, &backend_modelmatrix, sizeof(matrix4x4_t)))
1313 double glmatrix[16];
1314 backend_modelmatrix = *matrix;
1315 Matrix4x4_Concat(&backend_modelviewmatrix, &backend_viewmatrix, matrix);
1316 Matrix4x4_ToArrayDoubleGL(&backend_modelviewmatrix, glmatrix);
1318 qglLoadMatrixd(glmatrix);CHECKGLERROR
1322 void R_Mesh_VertexPointer(const float *vertex3f, int bufferobject, size_t bufferoffset)
1324 if (!gl_vbo.integer)
1326 if (gl_state.pointer_vertex != vertex3f || gl_state.pointer_vertex_buffer != bufferobject || gl_state.pointer_vertex_offset != bufferoffset)
1328 gl_state.pointer_vertex = vertex3f;
1329 gl_state.pointer_vertex_buffer = bufferobject;
1330 gl_state.pointer_vertex_offset = bufferoffset;
1332 GL_BindVBO(bufferobject);
1333 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), bufferobject ? (void *)bufferoffset : vertex3f);CHECKGLERROR
1337 void R_Mesh_ColorPointer(const float *color4f, int bufferobject, size_t bufferoffset)
1339 // note: this can not rely on bufferobject to decide whether a color array
1340 // is supplied, because surfmesh_t shares one vbo for all arrays, which
1341 // means that a valid vbo may be supplied even if there is no color array.
1344 if (!gl_vbo.integer)
1346 // caller wants color array enabled
1347 if (!gl_state.pointer_color_enabled)
1349 gl_state.pointer_color_enabled = true;
1351 qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1353 if (gl_state.pointer_color != color4f || gl_state.pointer_color_buffer != bufferobject || gl_state.pointer_color_offset != bufferoffset)
1355 gl_state.pointer_color = color4f;
1356 gl_state.pointer_color_buffer = bufferobject;
1357 gl_state.pointer_color_offset = bufferoffset;
1359 GL_BindVBO(bufferobject);
1360 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), bufferobject ? (void *)bufferoffset : color4f);CHECKGLERROR
1365 // caller wants color array disabled
1366 if (gl_state.pointer_color_enabled)
1368 gl_state.pointer_color_enabled = false;
1370 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1371 // when color array is on the glColor gets trashed, set it again
1372 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);CHECKGLERROR
1377 void R_Mesh_TexCoordPointer(unsigned int unitnum, unsigned int numcomponents, const float *texcoord, int bufferobject, size_t bufferoffset)
1379 gltextureunit_t *unit = gl_state.units + unitnum;
1380 // update array settings
1382 // note: there is no need to check bufferobject here because all cases
1383 // that involve a valid bufferobject also supply a texcoord array
1386 if (!gl_vbo.integer)
1388 // texture array unit is enabled, enable the array
1389 if (!unit->arrayenabled)
1391 unit->arrayenabled = true;
1392 GL_ClientActiveTexture(unitnum);
1393 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1396 if (unit->pointer_texcoord != texcoord || unit->pointer_texcoord_buffer != bufferobject || unit->pointer_texcoord_offset != bufferoffset || unit->arraycomponents != numcomponents)
1398 unit->pointer_texcoord = texcoord;
1399 unit->pointer_texcoord_buffer = bufferobject;
1400 unit->pointer_texcoord_offset = bufferoffset;
1401 unit->arraycomponents = numcomponents;
1402 GL_ClientActiveTexture(unitnum);
1403 GL_BindVBO(bufferobject);
1404 qglTexCoordPointer(unit->arraycomponents, GL_FLOAT, sizeof(float) * unit->arraycomponents, bufferobject ? (void *)bufferoffset : texcoord);CHECKGLERROR
1409 // texture array unit is disabled, disable the array
1410 if (unit->arrayenabled)
1412 unit->arrayenabled = false;
1413 GL_ClientActiveTexture(unitnum);
1414 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1419 void R_Mesh_TexBindAll(unsigned int unitnum, int tex1d, int tex2d, int tex3d, int texcubemap)
1421 gltextureunit_t *unit = gl_state.units + unitnum;
1422 if (unitnum >= backendimageunits)
1424 // update 1d texture binding
1425 if (unit->t1d != tex1d)
1427 GL_ActiveTexture(unitnum);
1428 if (unitnum < backendunits)
1434 qglEnable(GL_TEXTURE_1D);CHECKGLERROR
1441 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1446 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1448 // update 2d texture binding
1449 if (unit->t2d != tex2d)
1451 GL_ActiveTexture(unitnum);
1452 if (unitnum < backendunits)
1458 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
1465 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1470 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1472 // update 3d texture binding
1473 if (unit->t3d != tex3d)
1475 GL_ActiveTexture(unitnum);
1476 if (unitnum < backendunits)
1482 qglEnable(GL_TEXTURE_3D);CHECKGLERROR
1489 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1494 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1496 // update cubemap texture binding
1497 if (unit->tcubemap != texcubemap)
1499 GL_ActiveTexture(unitnum);
1500 if (unitnum < backendunits)
1504 if (unit->tcubemap == 0)
1506 qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1513 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1517 unit->tcubemap = texcubemap;
1518 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1522 void R_Mesh_TexBind1D(unsigned int unitnum, int texnum)
1524 gltextureunit_t *unit = gl_state.units + unitnum;
1525 if (unitnum >= backendimageunits)
1527 // update 1d texture binding
1528 if (unit->t1d != texnum)
1530 GL_ActiveTexture(unitnum);
1531 if (unitnum < backendunits)
1537 qglEnable(GL_TEXTURE_1D);CHECKGLERROR
1544 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1549 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1551 // update 2d texture binding
1554 GL_ActiveTexture(unitnum);
1555 if (unitnum < backendunits)
1559 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1563 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1565 // update 3d texture binding
1568 GL_ActiveTexture(unitnum);
1569 if (unitnum < backendunits)
1573 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1577 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1579 // update cubemap texture binding
1582 GL_ActiveTexture(unitnum);
1583 if (unitnum < backendunits)
1587 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1591 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1595 void R_Mesh_TexBind(unsigned int unitnum, int texnum)
1597 gltextureunit_t *unit = gl_state.units + unitnum;
1598 if (unitnum >= backendimageunits)
1600 // update 1d texture binding
1603 GL_ActiveTexture(unitnum);
1604 if (unitnum < backendunits)
1608 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1612 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1614 // update 2d texture binding
1615 if (unit->t2d != texnum)
1617 GL_ActiveTexture(unitnum);
1618 if (unitnum < backendunits)
1624 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
1631 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1636 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1638 // update 3d texture binding
1641 GL_ActiveTexture(unitnum);
1642 if (unitnum < backendunits)
1646 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1650 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1652 // update cubemap texture binding
1653 if (unit->tcubemap != 0)
1655 GL_ActiveTexture(unitnum);
1656 if (unitnum < backendunits)
1660 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1664 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1668 void R_Mesh_TexBind3D(unsigned int unitnum, int texnum)
1670 gltextureunit_t *unit = gl_state.units + unitnum;
1671 if (unitnum >= backendimageunits)
1673 // update 1d texture binding
1676 GL_ActiveTexture(unitnum);
1677 if (unitnum < backendunits)
1681 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1685 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1687 // update 2d texture binding
1690 GL_ActiveTexture(unitnum);
1691 if (unitnum < backendunits)
1695 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1699 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1701 // update 3d texture binding
1702 if (unit->t3d != texnum)
1704 GL_ActiveTexture(unitnum);
1705 if (unitnum < backendunits)
1711 qglEnable(GL_TEXTURE_3D);CHECKGLERROR
1718 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1723 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1725 // update cubemap texture binding
1726 if (unit->tcubemap != 0)
1728 GL_ActiveTexture(unitnum);
1729 if (unitnum < backendunits)
1733 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1737 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1741 void R_Mesh_TexBindCubeMap(unsigned int unitnum, int texnum)
1743 gltextureunit_t *unit = gl_state.units + unitnum;
1744 if (unitnum >= backendimageunits)
1746 // update 1d texture binding
1749 GL_ActiveTexture(unitnum);
1750 if (unitnum < backendunits)
1754 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1758 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1760 // update 2d texture binding
1763 GL_ActiveTexture(unitnum);
1764 if (unitnum < backendunits)
1768 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1772 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1774 // update 3d texture binding
1777 GL_ActiveTexture(unitnum);
1778 if (unitnum < backendunits)
1782 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1786 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1788 // update cubemap texture binding
1789 if (unit->tcubemap != texnum)
1791 GL_ActiveTexture(unitnum);
1792 if (unitnum < backendunits)
1796 if (unit->tcubemap == 0)
1798 qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1805 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1809 unit->tcubemap = texnum;
1810 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1814 void R_Mesh_TexMatrix(unsigned int unitnum, const matrix4x4_t *matrix)
1816 gltextureunit_t *unit = gl_state.units + unitnum;
1817 if (matrix->m[3][3])
1819 // texmatrix specified, check if it is different
1820 if (!unit->texmatrixenabled || memcmp(&unit->matrix, matrix, sizeof(matrix4x4_t)))
1822 double glmatrix[16];
1823 unit->texmatrixenabled = true;
1824 unit->matrix = *matrix;
1826 Matrix4x4_ToArrayDoubleGL(&unit->matrix, glmatrix);
1827 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1828 GL_ActiveTexture(unitnum);
1829 qglLoadMatrixd(glmatrix);CHECKGLERROR
1830 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1835 // no texmatrix specified, revert to identity
1836 if (unit->texmatrixenabled)
1838 unit->texmatrixenabled = false;
1840 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1841 GL_ActiveTexture(unitnum);
1842 qglLoadIdentity();CHECKGLERROR
1843 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1848 void R_Mesh_TexCombine(unsigned int unitnum, int combinergb, int combinealpha, int rgbscale, int alphascale)
1850 gltextureunit_t *unit = gl_state.units + unitnum;
1852 if (gl_combine.integer)
1854 // GL_ARB_texture_env_combine
1856 combinergb = GL_MODULATE;
1858 combinealpha = GL_MODULATE;
1863 if (unit->combinergb != combinergb)
1865 unit->combinergb = combinergb;
1866 GL_ActiveTexture(unitnum);
1867 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
1869 if (unit->combinealpha != combinealpha)
1871 unit->combinealpha = combinealpha;
1872 GL_ActiveTexture(unitnum);
1873 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
1875 if (unit->rgbscale != rgbscale)
1877 GL_ActiveTexture(unitnum);
1878 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (unit->rgbscale = rgbscale));CHECKGLERROR
1880 if (unit->alphascale != alphascale)
1882 GL_ActiveTexture(unitnum);
1883 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, (unit->alphascale = alphascale));CHECKGLERROR
1890 combinergb = GL_MODULATE;
1891 if (unit->combinergb != combinergb)
1893 unit->combinergb = combinergb;
1894 GL_ActiveTexture(unitnum);
1895 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combinergb);CHECKGLERROR
1900 void R_Mesh_TextureState(const rmeshstate_t *m)
1907 if (gl_backend_rebindtextures)
1909 gl_backend_rebindtextures = false;
1910 GL_SetupTextureState();
1914 for (i = 0;i < backendimageunits;i++)
1915 R_Mesh_TexBindAll(i, m->tex1d[i], m->tex[i], m->tex3d[i], m->texcubemap[i]);
1916 for (i = 0;i < backendarrayunits;i++)
1918 if (m->pointer_texcoord3f[i])
1919 R_Mesh_TexCoordPointer(i, 3, m->pointer_texcoord3f[i], m->pointer_texcoord_bufferobject[i], m->pointer_texcoord_bufferoffset[i]);
1921 R_Mesh_TexCoordPointer(i, 2, m->pointer_texcoord[i], m->pointer_texcoord_bufferobject[i], m->pointer_texcoord_bufferoffset[i]);
1923 for (i = 0;i < backendunits;i++)
1925 R_Mesh_TexMatrix(i, &m->texmatrix[i]);
1926 R_Mesh_TexCombine(i, m->texcombinergb[i], m->texcombinealpha[i], m->texrgbscale[i], m->texalphascale[i]);
1931 void R_Mesh_ResetTextureState(void)
1933 unsigned int unitnum;
1938 if (gl_backend_rebindtextures)
1940 gl_backend_rebindtextures = false;
1941 GL_SetupTextureState();
1945 for (unitnum = 0;unitnum < backendimageunits;unitnum++)
1947 gltextureunit_t *unit = gl_state.units + unitnum;
1948 // update 1d texture binding
1951 GL_ActiveTexture(unitnum);
1952 if (unitnum < backendunits)
1954 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1957 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1959 // update 2d texture binding
1962 GL_ActiveTexture(unitnum);
1963 if (unitnum < backendunits)
1965 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1968 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1970 // update 3d texture binding
1973 GL_ActiveTexture(unitnum);
1974 if (unitnum < backendunits)
1976 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1979 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1981 // update cubemap texture binding
1984 GL_ActiveTexture(unitnum);
1985 if (unitnum < backendunits)
1987 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1990 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1993 for (unitnum = 0;unitnum < backendarrayunits;unitnum++)
1995 gltextureunit_t *unit = gl_state.units + unitnum;
1996 // texture array unit is disabled, disable the array
1997 if (unit->arrayenabled)
1999 unit->arrayenabled = false;
2000 GL_ClientActiveTexture(unitnum);
2001 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
2004 for (unitnum = 0;unitnum < backendunits;unitnum++)
2006 gltextureunit_t *unit = gl_state.units + unitnum;
2007 // no texmatrix specified, revert to identity
2008 if (unit->texmatrixenabled)
2010 unit->texmatrixenabled = false;
2012 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
2013 GL_ActiveTexture(unitnum);
2014 qglLoadIdentity();CHECKGLERROR
2015 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
2017 if (gl_combine.integer)
2019 // GL_ARB_texture_env_combine
2020 if (unit->combinergb != GL_MODULATE)
2022 unit->combinergb = GL_MODULATE;
2023 GL_ActiveTexture(unitnum);
2024 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
2026 if (unit->combinealpha != GL_MODULATE)
2028 unit->combinealpha = GL_MODULATE;
2029 GL_ActiveTexture(unitnum);
2030 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
2032 if (unit->rgbscale != 1)
2034 GL_ActiveTexture(unitnum);
2035 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (unit->rgbscale = 1));CHECKGLERROR
2037 if (unit->alphascale != 1)
2039 GL_ActiveTexture(unitnum);
2040 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, (unit->alphascale = 1));CHECKGLERROR
2046 if (unit->combinergb != GL_MODULATE)
2048 unit->combinergb = GL_MODULATE;
2049 GL_ActiveTexture(unitnum);
2050 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combinergb);CHECKGLERROR
2056 void R_Mesh_Draw_ShowTris(int firstvertex, int numvertices, int numtriangles, const int *elements)
2060 for (;numtriangles;numtriangles--, elements += 3)
2062 qglArrayElement(elements[0]);qglArrayElement(elements[1]);
2063 qglArrayElement(elements[1]);qglArrayElement(elements[2]);
2064 qglArrayElement(elements[2]);qglArrayElement(elements[0]);