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_mesh_prefer_short_elements = {0, "gl_mesh_prefer_short_elements", "1", "use GL_UNSIGNED_SHORT element arrays instead of GL_UNSIGNED_INT"};
9 cvar_t gl_paranoid = {0, "gl_paranoid", "0", "enables OpenGL error checking and other tests"};
10 cvar_t gl_printcheckerror = {0, "gl_printcheckerror", "0", "prints all OpenGL error checks, useful to identify location of driver crashes"};
12 cvar_t r_render = {0, "r_render", "1", "enables rendering calls (you want this on!)"};
13 cvar_t r_waterwarp = {CVAR_SAVE, "r_waterwarp", "1", "warp view while underwater"};
14 cvar_t gl_polyblend = {CVAR_SAVE, "gl_polyblend", "1", "tints view while underwater, hurt, etc"};
15 cvar_t gl_dither = {CVAR_SAVE, "gl_dither", "1", "enables OpenGL dithering (16bit looks bad with this off)"};
16 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"};
17 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"};
18 cvar_t gl_vbo = {CVAR_SAVE, "gl_vbo", "3", "make use of GL_ARB_vertex_buffer_object extension to store static geometry in video memory for faster rendering, 0 disables VBO allocation or use, 1 enables VBOs for vertex and triangle data, 2 only for vertex data, 3 for vertex data and triangle data of simple meshes (ones with only one surface)"};
19 cvar_t gl_fbo = {CVAR_SAVE, "gl_fbo", "1", "make use of GL_ARB_framebuffer_object extension to enable shadowmaps and other features using pixel formats different from the framebuffer"};
21 cvar_t v_flipped = {0, "v_flipped", "0", "mirror the screen (poor man's left handed mode)"};
22 qboolean v_flipped_state = false;
24 int gl_maxdrawrangeelementsvertices;
25 int gl_maxdrawrangeelementsindices;
30 void GL_PrintError(int errornumber, char *filename, int linenumber)
34 #ifdef GL_INVALID_ENUM
36 Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
39 #ifdef GL_INVALID_VALUE
40 case GL_INVALID_VALUE:
41 Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
44 #ifdef GL_INVALID_OPERATION
45 case GL_INVALID_OPERATION:
46 Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
49 #ifdef GL_STACK_OVERFLOW
50 case GL_STACK_OVERFLOW:
51 Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
54 #ifdef GL_STACK_UNDERFLOW
55 case GL_STACK_UNDERFLOW:
56 Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
59 #ifdef GL_OUT_OF_MEMORY
60 case GL_OUT_OF_MEMORY:
61 Con_Printf("GL_OUT_OF_MEMORY at %s:%i\n", filename, linenumber);
64 #ifdef GL_TABLE_TOO_LARGE
65 case GL_TABLE_TOO_LARGE:
66 Con_Printf("GL_TABLE_TOO_LARGE at %s:%i\n", filename, linenumber);
69 #ifdef GL_INVALID_FRAMEBUFFER_OPERATION_EXT
70 case GL_INVALID_FRAMEBUFFER_OPERATION_EXT:
71 Con_Printf("GL_INVALID_FRAMEBUFFER_OPERATION at %s:%i\n", filename, linenumber);
75 Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
81 #define BACKENDACTIVECHECK if (!backendactive) Sys_Error("GL backend function called when backend is not active");
83 void SCR_ScreenShot_f (void);
85 static r_viewport_t backend_viewport;
86 static matrix4x4_t backend_modelmatrix;
87 static matrix4x4_t backend_modelviewmatrix;
89 static unsigned int backendunits, backendimageunits, backendarrayunits, backendactive;
92 note: here's strip order for a terrain row:
99 A0B, 01B, B1C, 12C, C2D, 23D, D3E, 34E
101 *elements++ = i + row;
103 *elements++ = i + row + 1;
106 *elements++ = i + row + 1;
109 for (y = 0;y < rows - 1;y++)
111 for (x = 0;x < columns - 1;x++)
114 *elements++ = i + columns;
116 *elements++ = i + columns + 1;
119 *elements++ = i + columns + 1;
130 for (y = 0;y < rows - 1;y++)
132 for (x = 0;x < columns - 1;x++)
136 *elements++ = i + columns;
137 *elements++ = i + columns + 1;
138 *elements++ = i + columns;
139 *elements++ = i + columns + 1;
145 unsigned short polygonelements[(POLYGONELEMENTS_MAXPOINTS-2)*3];
146 unsigned short quadelements[QUADELEMENTS_MAXQUADS*6];
148 void GL_Backend_AllocArrays(void)
152 void GL_Backend_FreeArrays(void)
156 void GL_VBOStats_f(void)
158 GL_Mesh_ListVBOs(true);
161 typedef struct gl_bufferobjectinfo_s
166 char name[MAX_QPATH];
168 gl_bufferobjectinfo_t;
170 memexpandablearray_t gl_bufferobjectinfoarray;
172 static void gl_backend_start(void)
176 if (qglDrawRangeElements != NULL)
179 qglGetIntegerv(GL_MAX_ELEMENTS_VERTICES, &gl_maxdrawrangeelementsvertices);
181 qglGetIntegerv(GL_MAX_ELEMENTS_INDICES, &gl_maxdrawrangeelementsindices);
183 Con_DPrintf("GL_MAX_ELEMENTS_VERTICES = %i\nGL_MAX_ELEMENTS_INDICES = %i\n", gl_maxdrawrangeelementsvertices, gl_maxdrawrangeelementsindices);
186 backendunits = bound(1, gl_textureunits, MAX_TEXTUREUNITS);
187 backendimageunits = backendunits;
188 backendarrayunits = backendunits;
189 if (gl_support_fragment_shader)
192 qglGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS_ARB, (int *)&backendimageunits);
194 qglGetIntegerv(GL_MAX_TEXTURE_COORDS_ARB, (int *)&backendarrayunits);
196 Con_DPrintf("GLSL shader support detected: texture units = %i texenv, %i image, %i array\n", backendunits, backendimageunits, backendarrayunits);
197 backendimageunits = bound(1, backendimageunits, MAX_TEXTUREUNITS);
198 backendarrayunits = bound(1, backendarrayunits, MAX_TEXTUREUNITS);
201 Con_DPrintf("GL_MAX_TEXTUREUNITS = %i\n", backendunits);
203 GL_Backend_AllocArrays();
205 Mem_ExpandableArray_NewArray(&gl_bufferobjectinfoarray, r_main_mempool, sizeof(gl_bufferobjectinfo_t), 128);
207 Con_DPrintf("OpenGL backend started.\n");
211 backendactive = true;
214 static void gl_backend_shutdown(void)
217 backendimageunits = 0;
218 backendarrayunits = 0;
219 backendactive = false;
221 Con_DPrint("OpenGL Backend shutting down\n");
223 Mem_ExpandableArray_FreeArray(&gl_bufferobjectinfoarray);
225 GL_Backend_FreeArrays();
228 static void gl_backend_newmap(void)
232 void gl_backend_init(void)
236 for (i = 0;i < POLYGONELEMENTS_MAXPOINTS - 2;i++)
238 polygonelements[i * 3 + 0] = 0;
239 polygonelements[i * 3 + 1] = i + 1;
240 polygonelements[i * 3 + 2] = i + 2;
242 // elements for rendering a series of quads as triangles
243 for (i = 0;i < QUADELEMENTS_MAXQUADS;i++)
245 quadelements[i * 6 + 0] = i * 4;
246 quadelements[i * 6 + 1] = i * 4 + 1;
247 quadelements[i * 6 + 2] = i * 4 + 2;
248 quadelements[i * 6 + 3] = i * 4;
249 quadelements[i * 6 + 4] = i * 4 + 2;
250 quadelements[i * 6 + 5] = i * 4 + 3;
253 Cvar_RegisterVariable(&r_render);
254 Cvar_RegisterVariable(&r_waterwarp);
255 Cvar_RegisterVariable(&gl_polyblend);
256 Cvar_RegisterVariable(&v_flipped);
257 Cvar_RegisterVariable(&gl_dither);
258 Cvar_RegisterVariable(&gl_lockarrays);
259 Cvar_RegisterVariable(&gl_lockarrays_minimumvertices);
260 Cvar_RegisterVariable(&gl_vbo);
261 Cvar_RegisterVariable(&gl_paranoid);
262 Cvar_RegisterVariable(&gl_printcheckerror);
264 Cvar_SetValue("r_render", 0);
267 Cvar_RegisterVariable(&gl_mesh_drawrangeelements);
268 Cvar_RegisterVariable(&gl_mesh_testarrayelement);
269 Cvar_RegisterVariable(&gl_mesh_testmanualfeeding);
270 Cvar_RegisterVariable(&gl_mesh_prefer_short_elements);
272 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");
274 R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap);
277 void GL_SetMirrorState(qboolean state);
279 void R_Viewport_TransformToScreen(const r_viewport_t *v, const vec4_t in, vec4_t out)
283 Matrix4x4_Transform4 (&v->viewmatrix, in, temp);
284 Matrix4x4_Transform4 (&v->projectmatrix, temp, out);
286 out[0] = v->x + (out[0] * iw + 1.0f) * v->width * 0.5f;
287 out[1] = v->y + v->height - (out[1] * iw + 1.0f) * v->height * 0.5f;
288 out[2] = v->z + (out[2] * iw + 1.0f) * v->depth * 0.5f;
291 static void R_Viewport_ApplyNearClipPlane(r_viewport_t *v, double normalx, double normaly, double normalz, double dist)
295 float clipPlane[4], v3[3], v4[3];
298 // This is inspired by Oblique Depth Projection from http://www.terathon.com/code/oblique.php
300 VectorSet(normal, normalx, normaly, normalz);
301 Matrix4x4_Transform3x3(&v->viewmatrix, normal, clipPlane);
302 VectorScale(normal, dist, v3);
303 Matrix4x4_Transform(&v->viewmatrix, v3, v4);
304 // FIXME: LordHavoc: I think this can be done more efficiently somehow but I can't remember the technique
305 clipPlane[3] = -DotProduct(v4, clipPlane);
309 // testing code for comparing results
311 VectorCopy4(clipPlane, clipPlane2);
312 R_Mesh_Matrix(&identitymatrix);
313 VectorSet(q, normal[0], normal[1], normal[2], -dist);
314 qglClipPlane(GL_CLIP_PLANE0, q);
315 qglGetClipPlane(GL_CLIP_PLANE0, q);
316 VectorCopy4(q, clipPlane);
320 // Calculate the clip-space corner point opposite the clipping plane
321 // as (sgn(clipPlane.x), sgn(clipPlane.y), 1, 1) and
322 // transform it into camera space by multiplying it
323 // by the inverse of the projection matrix
324 q[0] = ((clipPlane[0] < 0.0f ? -1.0f : clipPlane[0] > 0.0f ? 1.0f : 0.0f) + v->m[8]) / v->m[0];
325 q[1] = ((clipPlane[1] < 0.0f ? -1.0f : clipPlane[1] > 0.0f ? 1.0f : 0.0f) + v->m[9]) / v->m[5];
327 q[3] = (1.0f + v->m[10]) / v->m[14];
329 // Calculate the scaled plane vector
330 d = 2.0f / DotProduct4(clipPlane, q);
332 // Replace the third row of the projection matrix
333 v->m[2] = clipPlane[0] * d;
334 v->m[6] = clipPlane[1] * d;
335 v->m[10] = clipPlane[2] * d + 1.0f;
336 v->m[14] = clipPlane[3] * d;
339 void R_Viewport_InitOrtho(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, double x1, double y1, double x2, double y2, double nearclip, double farclip, const double *nearplane)
341 float left = x1, right = x2, bottom = y2, top = y1, zNear = nearclip, zFar = farclip;
342 memset(v, 0, sizeof(*v));
343 v->type = R_VIEWPORTTYPE_ORTHO;
344 v->cameramatrix = *cameramatrix;
351 v->m[0] = 2/(right - left);
352 v->m[5] = 2/(top - bottom);
353 v->m[10] = -2/(zFar - zNear);
354 v->m[12] = - (right + left)/(right - left);
355 v->m[13] = - (top + bottom)/(top - bottom);
356 v->m[14] = - (zFar + zNear)/(zFar - zNear);
359 Matrix4x4_Invert_Full(&v->viewmatrix, &v->cameramatrix);
360 Matrix4x4_FromArrayDoubleGL(&v->projectmatrix, v->m);
363 R_Viewport_ApplyNearClipPlane(v, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
369 Vector4Set(test1, (x1+x2)*0.5f, (y1+y2)*0.5f, 0.0f, 1.0f);
370 R_Viewport_TransformToScreen(v, test1, test2);
371 Con_Printf("%f %f %f -> %f %f %f\n", test1[0], test1[1], test1[2], test2[0], test2[1], test2[2]);
376 void R_Viewport_InitPerspective(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, double frustumx, double frustumy, double nearclip, double farclip, const double *nearplane)
378 matrix4x4_t tempmatrix, basematrix;
379 memset(v, 0, sizeof(*v));
381 if(v_flipped.integer)
382 frustumx = -frustumx;
384 v->type = R_VIEWPORTTYPE_PERSPECTIVE;
385 v->cameramatrix = *cameramatrix;
392 v->m[0] = 1.0 / frustumx;
393 v->m[5] = 1.0 / frustumy;
394 v->m[10] = -(farclip + nearclip) / (farclip - nearclip);
396 v->m[14] = -2 * nearclip * farclip / (farclip - nearclip);
398 Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
399 Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
400 Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
401 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
403 Matrix4x4_FromArrayDoubleGL(&v->projectmatrix, v->m);
406 R_Viewport_ApplyNearClipPlane(v, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
409 void R_Viewport_InitPerspectiveInfinite(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, double frustumx, double frustumy, double nearclip, const double *nearplane)
411 matrix4x4_t tempmatrix, basematrix;
412 const double nudge = 1.0 - 1.0 / (1<<23);
413 memset(v, 0, sizeof(*v));
415 if(v_flipped.integer)
416 frustumx = -frustumx;
418 v->type = R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP;
419 v->cameramatrix = *cameramatrix;
426 v->m[ 0] = 1.0 / frustumx;
427 v->m[ 5] = 1.0 / frustumy;
430 v->m[14] = -2 * nearclip * nudge;
432 Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
433 Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
434 Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
435 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
437 Matrix4x4_FromArrayDoubleGL(&v->projectmatrix, v->m);
440 R_Viewport_ApplyNearClipPlane(v, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
443 float cubeviewmatrix[6][16] =
483 void R_Viewport_InitCubeSideView(r_viewport_t *v, const matrix4x4_t *cameramatrix, int side, int size, float nearclip, float farclip, const float *nearplane)
485 matrix4x4_t tempmatrix, basematrix;
486 memset(v, 0, sizeof(*v));
487 v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
488 v->cameramatrix = *cameramatrix;
492 v->m[0] = v->m[5] = 1.0f;
493 v->m[10] = -(farclip + nearclip) / (farclip - nearclip);
495 v->m[14] = -2 * nearclip * farclip / (farclip - nearclip);
497 Matrix4x4_FromArrayFloatGL(&basematrix, cubeviewmatrix[side]);
498 Matrix4x4_Invert_Simple(&tempmatrix, &v->cameramatrix);
499 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
500 Matrix4x4_FromArrayDoubleGL(&v->projectmatrix, v->m);
503 R_Viewport_ApplyNearClipPlane(v, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
506 void R_Viewport_InitRectSideView(r_viewport_t *v, const matrix4x4_t *cameramatrix, int side, int size, int border, float nearclip, float farclip, const float *nearplane)
508 matrix4x4_t tempmatrix, basematrix;
509 if (border > size - 2)
511 memset(v, 0, sizeof(*v));
512 v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
513 v->cameramatrix = *cameramatrix;
514 v->x = (side & 1) * size;
515 v->y = (side >> 1) * size;
519 v->m[0] = v->m[5] = 1.0f * ((float)size - border) / size;
520 v->m[10] = -(farclip + nearclip) / (farclip - nearclip);
522 v->m[14] = -2 * nearclip * farclip / (farclip - nearclip);
524 Matrix4x4_FromArrayFloatGL(&basematrix, cubeviewmatrix[side]);
525 Matrix4x4_Invert_Simple(&tempmatrix, &v->cameramatrix);
526 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
527 Matrix4x4_FromArrayDoubleGL(&v->projectmatrix, v->m);
530 R_Viewport_ApplyNearClipPlane(v, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
533 void R_SetViewport(const r_viewport_t *v)
536 backend_viewport = *v;
539 qglViewport(v->x, v->y, v->width, v->height);CHECKGLERROR
541 // Load the projection matrix into OpenGL
542 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
543 qglLoadMatrixd(backend_viewport.m);CHECKGLERROR
544 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
546 // FIXME: v_flipped_state is evil, this probably breaks somewhere
547 GL_SetMirrorState(v_flipped.integer && (v->type == R_VIEWPORTTYPE_PERSPECTIVE || v->type == R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP));
549 // directly force an update of the modelview matrix
550 Matrix4x4_Concat(&backend_modelviewmatrix, &backend_viewport.viewmatrix, &backend_modelmatrix);
551 Matrix4x4_ToArrayFloatGL(&backend_modelviewmatrix, glmatrix);
552 qglLoadMatrixf(glmatrix);CHECKGLERROR
555 void R_GetViewport(r_viewport_t *v)
557 *v = backend_viewport;
560 typedef struct gltextureunit_s
562 const void *pointer_texcoord;
563 size_t pointer_texcoord_offset;
564 int pointer_texcoord_buffer;
565 int t1d, t2d, t3d, tcubemap, trectangle;
567 unsigned int arraycomponents;
568 int rgbscale, alphascale;
569 int combinergb, combinealpha;
570 // FIXME: add more combine stuff
571 // texmatrixenabled exists only to avoid unnecessary texmatrix compares
572 int texmatrixenabled;
577 static struct gl_state_s
585 int colormask; // stored as bottom 4 bits: r g b a (3 2 1 0 order)
588 float polygonoffset[2];
592 unsigned int clientunit;
593 gltextureunit_t units[MAX_TEXTUREUNITS];
597 int vertexbufferobject;
598 int elementbufferobject;
599 qboolean pointer_color_enabled;
600 const void *pointer_vertex;
601 const void *pointer_color;
602 size_t pointer_vertex_offset;
603 size_t pointer_color_offset;
604 int pointer_vertex_buffer;
605 int pointer_color_buffer;
609 static void GL_BindVBO(int bufferobject)
611 if (gl_state.vertexbufferobject != bufferobject)
613 gl_state.vertexbufferobject = bufferobject;
615 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, bufferobject);
620 static void GL_BindEBO(int bufferobject)
622 if (gl_state.elementbufferobject != bufferobject)
624 gl_state.elementbufferobject = bufferobject;
626 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, bufferobject);
631 void GL_SetupTextureState(void)
634 gltextureunit_t *unit;
636 gl_state.unit = MAX_TEXTUREUNITS;
637 gl_state.clientunit = MAX_TEXTUREUNITS;
638 for (i = 0;i < MAX_TEXTUREUNITS;i++)
640 unit = gl_state.units + i;
645 unit->arrayenabled = false;
646 unit->arraycomponents = 0;
647 unit->pointer_texcoord = NULL;
648 unit->pointer_texcoord_buffer = 0;
649 unit->pointer_texcoord_offset = 0;
651 unit->alphascale = 1;
652 unit->combinergb = GL_MODULATE;
653 unit->combinealpha = GL_MODULATE;
654 unit->texmatrixenabled = false;
655 unit->matrix = identitymatrix;
658 for (i = 0;i < backendimageunits;i++)
661 qglBindTexture(GL_TEXTURE_1D, 0);CHECKGLERROR
662 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
665 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
667 if (gl_texturecubemap)
669 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
671 if (gl_texturerectangle)
673 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);CHECKGLERROR
677 for (i = 0;i < backendarrayunits;i++)
679 GL_ClientActiveTexture(i);
681 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
682 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
685 for (i = 0;i < backendunits;i++)
688 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
689 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
692 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
694 if (gl_texturecubemap)
696 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
698 if (gl_texturerectangle)
700 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
702 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
703 qglLoadIdentity();CHECKGLERROR
704 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
705 if (gl_combine.integer)
707 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);CHECKGLERROR
708 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);CHECKGLERROR
709 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);CHECKGLERROR
710 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
711 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_TEXTURE);CHECKGLERROR // for GL_INTERPOLATE_ARB mode
712 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
713 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
714 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);CHECKGLERROR
715 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);CHECKGLERROR
716 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);CHECKGLERROR
717 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
718 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, GL_CONSTANT_ARB);CHECKGLERROR
719 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
720 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
721 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
722 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
723 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
727 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
734 void GL_Backend_ResetState(void)
736 memset(&gl_state, 0, sizeof(gl_state));
737 gl_state.depthtest = true;
738 gl_state.alphatest = false;
739 gl_state.blendfunc1 = GL_ONE;
740 gl_state.blendfunc2 = GL_ZERO;
741 gl_state.blend = false;
742 gl_state.depthmask = GL_TRUE;
743 gl_state.colormask = 15;
744 gl_state.color4f[0] = gl_state.color4f[1] = gl_state.color4f[2] = gl_state.color4f[3] = 1;
745 gl_state.lockrange_first = 0;
746 gl_state.lockrange_count = 0;
747 gl_state.cullface = v_flipped_state ? GL_BACK : GL_FRONT; // quake is backwards, this culls back faces
748 gl_state.cullfaceenable = true;
749 gl_state.polygonoffset[0] = 0;
750 gl_state.polygonoffset[1] = 0;
754 qglColorMask(1, 1, 1, 1);
755 qglAlphaFunc(GL_GEQUAL, 0.5);CHECKGLERROR
756 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
757 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
758 qglDisable(GL_BLEND);CHECKGLERROR
759 qglCullFace(gl_state.cullface);CHECKGLERROR
760 qglEnable(GL_CULL_FACE);CHECKGLERROR
761 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
762 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
763 qglDepthMask(gl_state.depthmask);CHECKGLERROR
764 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
766 if (gl_support_arb_vertex_buffer_object)
768 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
769 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
772 if (gl_support_ext_framebuffer_object)
774 qglBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
775 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
778 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), NULL);CHECKGLERROR
779 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
781 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL);CHECKGLERROR
782 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
784 GL_Color(0, 0, 0, 0);
785 GL_Color(1, 1, 1, 1);
787 GL_SetupTextureState();
790 void GL_ActiveTexture(unsigned int num)
792 if (gl_state.unit != num)
795 if (qglActiveTexture)
798 qglActiveTexture(GL_TEXTURE0_ARB + gl_state.unit);
804 void GL_ClientActiveTexture(unsigned int num)
806 if (gl_state.clientunit != num)
808 gl_state.clientunit = num;
809 if (qglActiveTexture)
812 qglClientActiveTexture(GL_TEXTURE0_ARB + gl_state.clientunit);
818 void GL_BlendFunc(int blendfunc1, int blendfunc2)
820 if (gl_state.blendfunc1 != blendfunc1 || gl_state.blendfunc2 != blendfunc2)
823 qglBlendFunc(gl_state.blendfunc1 = blendfunc1, gl_state.blendfunc2 = blendfunc2);CHECKGLERROR
824 if (gl_state.blendfunc2 == GL_ZERO)
826 if (gl_state.blendfunc1 == GL_ONE)
831 qglDisable(GL_BLEND);CHECKGLERROR
839 qglEnable(GL_BLEND);CHECKGLERROR
848 qglEnable(GL_BLEND);CHECKGLERROR
854 void GL_DepthMask(int state)
856 if (gl_state.depthmask != state)
859 qglDepthMask(gl_state.depthmask = state);CHECKGLERROR
863 void GL_DepthTest(int state)
865 if (gl_state.depthtest != state)
867 gl_state.depthtest = state;
869 if (gl_state.depthtest)
871 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
875 qglDisable(GL_DEPTH_TEST);CHECKGLERROR
880 void GL_DepthRange(float nearfrac, float farfrac)
882 if (gl_state.depthrange[0] != nearfrac || gl_state.depthrange[1] != farfrac)
884 gl_state.depthrange[0] = nearfrac;
885 gl_state.depthrange[1] = farfrac;
886 qglDepthRange(nearfrac, farfrac);
890 void GL_PolygonOffset(float planeoffset, float depthoffset)
892 if (gl_state.polygonoffset[0] != planeoffset || gl_state.polygonoffset[1] != depthoffset)
894 gl_state.polygonoffset[0] = planeoffset;
895 gl_state.polygonoffset[1] = depthoffset;
896 qglPolygonOffset(planeoffset, depthoffset);
900 void GL_SetMirrorState(qboolean state)
902 if(!state != !v_flipped_state)
904 // change cull face mode!
905 if(gl_state.cullface == GL_BACK)
906 qglCullFace((gl_state.cullface = GL_FRONT));
907 else if(gl_state.cullface == GL_FRONT)
908 qglCullFace((gl_state.cullface = GL_BACK));
910 v_flipped_state = state;
913 void GL_CullFace(int state)
919 if(state == GL_FRONT)
921 else if(state == GL_BACK)
925 if (state != GL_NONE)
927 if (!gl_state.cullfaceenable)
929 gl_state.cullfaceenable = true;
930 qglEnable(GL_CULL_FACE);CHECKGLERROR
932 if (gl_state.cullface != state)
934 gl_state.cullface = state;
935 qglCullFace(gl_state.cullface);CHECKGLERROR
940 if (gl_state.cullfaceenable)
942 gl_state.cullfaceenable = false;
943 qglDisable(GL_CULL_FACE);CHECKGLERROR
948 void GL_AlphaTest(int state)
950 if (gl_state.alphatest != state)
952 gl_state.alphatest = state;
954 if (gl_state.alphatest)
956 qglEnable(GL_ALPHA_TEST);CHECKGLERROR
960 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
965 void GL_ColorMask(int r, int g, int b, int a)
967 int state = r*8 + g*4 + b*2 + a*1;
968 if (gl_state.colormask != state)
970 gl_state.colormask = state;
972 qglColorMask((GLboolean)r, (GLboolean)g, (GLboolean)b, (GLboolean)a);CHECKGLERROR
976 void GL_Color(float cr, float cg, float cb, float ca)
978 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)
980 gl_state.color4f[0] = cr;
981 gl_state.color4f[1] = cg;
982 gl_state.color4f[2] = cb;
983 gl_state.color4f[3] = ca;
985 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);
990 void GL_LockArrays(int first, int count)
992 if (count < gl_lockarrays_minimumvertices.integer)
997 if (gl_state.lockrange_count != count || gl_state.lockrange_first != first)
999 if (gl_state.lockrange_count)
1001 gl_state.lockrange_count = 0;
1003 qglUnlockArraysEXT();
1006 if (count && gl_supportslockarrays && gl_lockarrays.integer && r_render.integer)
1008 gl_state.lockrange_first = first;
1009 gl_state.lockrange_count = count;
1011 qglLockArraysEXT(first, count);
1017 void GL_Scissor (int x, int y, int width, int height)
1020 qglScissor(x, y,width,height);
1024 void GL_ScissorTest(int state)
1026 if(gl_state.scissortest == state)
1030 if((gl_state.scissortest = state))
1031 qglEnable(GL_SCISSOR_TEST);
1033 qglDisable(GL_SCISSOR_TEST);
1037 void GL_Clear(int mask)
1040 qglClear(mask);CHECKGLERROR
1043 // called at beginning of frame
1044 void R_Mesh_Start(void)
1048 if (gl_printcheckerror.integer && !gl_paranoid.integer)
1050 Con_Printf("WARNING: gl_printcheckerror is on but gl_paranoid is off, turning it on...\n");
1051 Cvar_SetValueQuick(&gl_paranoid, 1);
1053 GL_Backend_ResetState();
1056 qboolean GL_Backend_CompileShader(int programobject, GLenum shadertypeenum, const char *shadertype, int numstrings, const char **strings)
1060 char compilelog[MAX_INPUTLINE];
1061 shaderobject = qglCreateShaderObjectARB(shadertypeenum);CHECKGLERROR
1064 qglShaderSourceARB(shaderobject, numstrings, strings, NULL);CHECKGLERROR
1065 qglCompileShaderARB(shaderobject);CHECKGLERROR
1066 qglGetObjectParameterivARB(shaderobject, GL_OBJECT_COMPILE_STATUS_ARB, &shadercompiled);CHECKGLERROR
1067 qglGetInfoLogARB(shaderobject, sizeof(compilelog), NULL, compilelog);CHECKGLERROR
1068 if (compilelog[0] && developer.integer > 0)
1070 int i, j, pretextlines = 0;
1071 for (i = 0;i < numstrings - 1;i++)
1072 for (j = 0;strings[i][j];j++)
1073 if (strings[i][j] == '\n')
1075 Con_DPrintf("%s shader compile log:\n%s\n(line offset for any above warnings/errors: %i)\n", shadertype, compilelog, pretextlines);
1077 if (!shadercompiled)
1079 qglDeleteObjectARB(shaderobject);CHECKGLERROR
1082 qglAttachObjectARB(programobject, shaderobject);CHECKGLERROR
1083 qglDeleteObjectARB(shaderobject);CHECKGLERROR
1087 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)
1089 GLint programlinked;
1090 GLuint programobject = 0;
1091 char linklog[MAX_INPUTLINE];
1094 programobject = qglCreateProgramObjectARB();CHECKGLERROR
1098 if (vertexstrings_count && !GL_Backend_CompileShader(programobject, GL_VERTEX_SHADER_ARB, "vertex", vertexstrings_count, vertexstrings_list))
1101 #ifdef GL_GEOMETRY_SHADER_ARB
1102 if (geometrystrings_count && !GL_Backend_CompileShader(programobject, GL_GEOMETRY_SHADER_ARB, "geometry", geometrystrings_count, geometrystrings_list))
1106 if (fragmentstrings_count && !GL_Backend_CompileShader(programobject, GL_FRAGMENT_SHADER_ARB, "fragment", fragmentstrings_count, fragmentstrings_list))
1109 qglLinkProgramARB(programobject);CHECKGLERROR
1110 qglGetObjectParameterivARB(programobject, GL_OBJECT_LINK_STATUS_ARB, &programlinked);CHECKGLERROR
1111 qglGetInfoLogARB(programobject, sizeof(linklog), NULL, linklog);CHECKGLERROR
1114 Con_DPrintf("program link log:\n%s\n", linklog);
1115 // software vertex shader is ok but software fragment shader is WAY
1116 // too slow, fail program if so.
1117 // NOTE: this string might be ATI specific, but that's ok because the
1118 // ATI R300 chip (Radeon 9500-9800/X300) is the most likely to use a
1119 // software fragment shader due to low instruction and dependent
1121 if (strstr(linklog, "fragment shader will run in software"))
1122 programlinked = false;
1126 return programobject;
1128 qglDeleteObjectARB(programobject);CHECKGLERROR
1132 void GL_Backend_FreeProgram(unsigned int prog)
1135 qglDeleteObjectARB(prog);
1139 int gl_backend_rebindtextures;
1141 void GL_Backend_RenumberElements(int *out, int count, const int *in, int offset)
1146 for (i = 0;i < count;i++)
1147 *out++ = *in++ + offset;
1150 memcpy(out, in, sizeof(*out) * count);
1153 // renders triangles using vertices from the active arrays
1154 int paranoidblah = 0;
1155 void R_Mesh_Draw(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const unsigned short *element3s, int bufferobject3i, int bufferobject3s)
1157 unsigned int numelements = numtriangles * 3;
1158 if (numvertices < 3 || numtriangles < 1)
1160 if (numvertices < 0 || numtriangles < 0 || developer.integer >= 100)
1161 Con_Printf("R_Mesh_Draw(%d, %d, %d, %d, %8p, %8p, %i, %i);\n", firstvertex, numvertices, firsttriangle, numtriangles, (void *)element3i, (void *)element3s, bufferobject3i, bufferobject3s);
1164 if (!gl_mesh_prefer_short_elements.integer)
1172 element3i += firsttriangle * 3;
1174 element3s += firsttriangle * 3;
1175 switch (gl_vbo.integer)
1180 bufferobject3i = bufferobject3s = 0;
1186 bufferobject3i = bufferobject3s = 0;
1190 r_refdef.stats.meshes++;
1191 r_refdef.stats.meshes_elements += numelements;
1192 if (gl_paranoid.integer)
1194 unsigned int i, j, size;
1196 // note: there's no validation done here on buffer objects because it
1197 // is somewhat difficult to get at the data, and gl_paranoid can be
1198 // used without buffer objects if the need arises
1199 // (the data could be gotten using glMapBuffer but it would be very
1200 // slow due to uncachable video memory reads)
1201 if (!qglIsEnabled(GL_VERTEX_ARRAY))
1202 Con_Print("R_Mesh_Draw: vertex array not enabled\n");
1204 if (gl_state.pointer_vertex)
1205 for (j = 0, size = numvertices * 3, p = (int *)((float *)gl_state.pointer_vertex + firstvertex * 3);j < size;j++, p++)
1207 if (gl_state.pointer_color_enabled)
1209 if (!qglIsEnabled(GL_COLOR_ARRAY))
1210 Con_Print("R_Mesh_Draw: color array set but not enabled\n");
1212 if (gl_state.pointer_color && gl_state.pointer_color_enabled)
1213 for (j = 0, size = numvertices * 4, p = (int *)((float *)gl_state.pointer_color + firstvertex * 4);j < size;j++, p++)
1216 for (i = 0;i < backendarrayunits;i++)
1218 if (gl_state.units[i].arrayenabled)
1220 GL_ClientActiveTexture(i);
1221 if (!qglIsEnabled(GL_TEXTURE_COORD_ARRAY))
1222 Con_Print("R_Mesh_Draw: texcoord array set but not enabled\n");
1224 if (gl_state.units[i].pointer_texcoord && gl_state.units[i].arrayenabled)
1225 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++)
1231 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1233 if (element3i[i] < firstvertex || element3i[i] >= firstvertex + numvertices)
1235 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3i array\n", element3i[i], firstvertex, firstvertex + numvertices);
1242 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1244 if (element3s[i] < firstvertex || element3s[i] >= firstvertex + numvertices)
1246 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3s array\n", element3s[i], firstvertex, firstvertex + numvertices);
1253 if (r_render.integer)
1256 if (gl_mesh_testmanualfeeding.integer)
1258 unsigned int i, j, element;
1260 qglBegin(GL_TRIANGLES);
1261 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1263 element = element3i ? element3i[i] : element3s[i];
1264 for (j = 0;j < backendarrayunits;j++)
1266 if (gl_state.units[j].pointer_texcoord && gl_state.units[j].arrayenabled)
1268 if (backendarrayunits > 1)
1270 if (gl_state.units[j].arraycomponents == 4)
1272 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 4;
1273 qglMultiTexCoord4f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2], p[3]);
1275 else if (gl_state.units[j].arraycomponents == 3)
1277 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 3;
1278 qglMultiTexCoord3f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2]);
1280 else if (gl_state.units[j].arraycomponents == 2)
1282 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 2;
1283 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, p[0], p[1]);
1287 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 1;
1288 qglMultiTexCoord1f(GL_TEXTURE0_ARB + j, p[0]);
1293 if (gl_state.units[j].arraycomponents == 4)
1295 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 4;
1296 qglTexCoord4f(p[0], p[1], p[2], p[3]);
1298 else if (gl_state.units[j].arraycomponents == 3)
1300 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 3;
1301 qglTexCoord3f(p[0], p[1], p[2]);
1303 else if (gl_state.units[j].arraycomponents == 2)
1305 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 2;
1306 qglTexCoord2f(p[0], p[1]);
1310 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 1;
1311 qglTexCoord1f(p[0]);
1316 if (gl_state.pointer_color && gl_state.pointer_color_enabled)
1318 p = ((const GLfloat *)(gl_state.pointer_color)) + element * 4;
1319 qglColor4f(p[0], p[1], p[2], p[3]);
1321 p = ((const GLfloat *)(gl_state.pointer_vertex)) + element * 3;
1322 qglVertex3f(p[0], p[1], p[2]);
1327 else if (gl_mesh_testarrayelement.integer)
1330 qglBegin(GL_TRIANGLES);
1333 for (i = 0;i < numtriangles * 3;i++)
1334 qglArrayElement(element3i[i]);
1338 for (i = 0;i < numtriangles * 3;i++)
1339 qglArrayElement(element3s[i]);
1344 else if (bufferobject3s)
1346 GL_BindEBO(bufferobject3s);
1347 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1349 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, (void *)(firsttriangle * sizeof(unsigned short[3])));
1354 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, (void *)(firsttriangle * sizeof(unsigned short[3])));
1358 else if (bufferobject3i)
1360 GL_BindEBO(bufferobject3i);
1361 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1363 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, (void *)(firsttriangle * sizeof(unsigned int[3])));
1368 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, (void *)(firsttriangle * sizeof(unsigned int[3])));
1375 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1377 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, element3s);
1382 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, element3s);
1389 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1391 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, element3i);
1396 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, element3i);
1403 // restores backend state, used when done with 3D rendering
1404 void R_Mesh_Finish(void)
1409 GL_LockArrays(0, 0);
1412 for (i = 0;i < backendimageunits;i++)
1414 GL_ActiveTexture(i);
1415 qglBindTexture(GL_TEXTURE_1D, 0);CHECKGLERROR
1416 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
1419 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
1421 if (gl_texturecubemap)
1423 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
1425 if (gl_texturerectangle)
1427 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);CHECKGLERROR
1430 for (i = 0;i < backendarrayunits;i++)
1432 GL_ActiveTexture(backendarrayunits - 1 - i);
1433 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1435 for (i = 0;i < backendunits;i++)
1437 GL_ActiveTexture(backendunits - 1 - i);
1438 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1439 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1442 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1444 if (gl_texturecubemap)
1446 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1448 if (gl_texturerectangle)
1450 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
1452 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
1453 if (gl_combine.integer)
1455 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
1456 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
1459 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1460 qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
1462 qglDisable(GL_BLEND);CHECKGLERROR
1463 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1464 qglDepthMask(GL_TRUE);CHECKGLERROR
1465 qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);CHECKGLERROR
1468 int R_Mesh_CreateStaticBufferObject(unsigned int target, void *data, size_t size, const char *name)
1470 gl_bufferobjectinfo_t *info;
1471 GLuint bufferobject;
1473 if (!gl_vbo.integer)
1476 qglGenBuffersARB(1, &bufferobject);
1479 case GL_ELEMENT_ARRAY_BUFFER_ARB: GL_BindEBO(bufferobject);break;
1480 case GL_ARRAY_BUFFER_ARB: GL_BindVBO(bufferobject);break;
1481 default: Sys_Error("R_Mesh_CreateStaticBufferObject: unknown target type %i\n", target);return 0;
1483 qglBufferDataARB(target, size, data, GL_STATIC_DRAW_ARB);
1485 info = (gl_bufferobjectinfo_t *) Mem_ExpandableArray_AllocRecord(&gl_bufferobjectinfoarray);
1486 memset(info, 0, sizeof(*info));
1487 info->target = target;
1488 info->object = bufferobject;
1490 strlcpy(info->name, name, sizeof(info->name));
1492 return (int)bufferobject;
1495 void R_Mesh_DestroyBufferObject(int bufferobject)
1498 gl_bufferobjectinfo_t *info;
1500 qglDeleteBuffersARB(1, (GLuint *)&bufferobject);
1502 endindex = Mem_ExpandableArray_IndexRange(&gl_bufferobjectinfoarray);
1503 for (i = 0;i < endindex;i++)
1505 info = (gl_bufferobjectinfo_t *) Mem_ExpandableArray_RecordAtIndex(&gl_bufferobjectinfoarray, i);
1508 if (info->object == bufferobject)
1510 Mem_ExpandableArray_FreeRecord(&gl_bufferobjectinfoarray, (void *)info);
1516 void GL_Mesh_ListVBOs(qboolean printeach)
1519 size_t ebocount = 0, ebomemory = 0;
1520 size_t vbocount = 0, vbomemory = 0;
1521 gl_bufferobjectinfo_t *info;
1522 endindex = Mem_ExpandableArray_IndexRange(&gl_bufferobjectinfoarray);
1523 for (i = 0;i < endindex;i++)
1525 info = (gl_bufferobjectinfo_t *) Mem_ExpandableArray_RecordAtIndex(&gl_bufferobjectinfoarray, i);
1528 switch(info->target)
1530 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;
1531 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;
1532 default: Con_Printf("gl_vbostats: unknown target type %i\n", info->target);break;
1535 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);
1538 void R_Mesh_Matrix(const matrix4x4_t *matrix)
1540 if (memcmp(matrix, &backend_modelmatrix, sizeof(matrix4x4_t)))
1543 backend_modelmatrix = *matrix;
1544 Matrix4x4_Concat(&backend_modelviewmatrix, &backend_viewport.viewmatrix, &backend_modelmatrix);
1545 Matrix4x4_ToArrayFloatGL(&backend_modelviewmatrix, glmatrix);
1547 qglLoadMatrixf(glmatrix);CHECKGLERROR
1551 void R_Mesh_VertexPointer(const float *vertex3f, int bufferobject, size_t bufferoffset)
1553 if (!gl_vbo.integer)
1555 if (gl_state.pointer_vertex != vertex3f || gl_state.pointer_vertex_buffer != bufferobject || gl_state.pointer_vertex_offset != bufferoffset)
1557 gl_state.pointer_vertex = vertex3f;
1558 gl_state.pointer_vertex_buffer = bufferobject;
1559 gl_state.pointer_vertex_offset = bufferoffset;
1561 GL_BindVBO(bufferobject);
1562 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), bufferobject ? (void *)bufferoffset : vertex3f);CHECKGLERROR
1566 void R_Mesh_ColorPointer(const float *color4f, int bufferobject, size_t bufferoffset)
1568 // note: this can not rely on bufferobject to decide whether a color array
1569 // is supplied, because surfmesh_t shares one vbo for all arrays, which
1570 // means that a valid vbo may be supplied even if there is no color array.
1573 if (!gl_vbo.integer)
1575 // caller wants color array enabled
1576 if (!gl_state.pointer_color_enabled)
1578 gl_state.pointer_color_enabled = true;
1580 qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1582 if (gl_state.pointer_color != color4f || gl_state.pointer_color_buffer != bufferobject || gl_state.pointer_color_offset != bufferoffset)
1584 gl_state.pointer_color = color4f;
1585 gl_state.pointer_color_buffer = bufferobject;
1586 gl_state.pointer_color_offset = bufferoffset;
1588 GL_BindVBO(bufferobject);
1589 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), bufferobject ? (void *)bufferoffset : color4f);CHECKGLERROR
1594 // caller wants color array disabled
1595 if (gl_state.pointer_color_enabled)
1597 gl_state.pointer_color_enabled = false;
1599 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1600 // when color array is on the glColor gets trashed, set it again
1601 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);CHECKGLERROR
1606 void R_Mesh_TexCoordPointer(unsigned int unitnum, unsigned int numcomponents, const float *texcoord, int bufferobject, size_t bufferoffset)
1608 gltextureunit_t *unit = gl_state.units + unitnum;
1609 // update array settings
1611 // note: there is no need to check bufferobject here because all cases
1612 // that involve a valid bufferobject also supply a texcoord array
1615 if (!gl_vbo.integer)
1617 // texture array unit is enabled, enable the array
1618 if (!unit->arrayenabled)
1620 unit->arrayenabled = true;
1621 GL_ClientActiveTexture(unitnum);
1622 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1625 if (unit->pointer_texcoord != texcoord || unit->pointer_texcoord_buffer != bufferobject || unit->pointer_texcoord_offset != bufferoffset || unit->arraycomponents != numcomponents)
1627 unit->pointer_texcoord = texcoord;
1628 unit->pointer_texcoord_buffer = bufferobject;
1629 unit->pointer_texcoord_offset = bufferoffset;
1630 unit->arraycomponents = numcomponents;
1631 GL_ClientActiveTexture(unitnum);
1632 GL_BindVBO(bufferobject);
1633 qglTexCoordPointer(unit->arraycomponents, GL_FLOAT, sizeof(float) * unit->arraycomponents, bufferobject ? (void *)bufferoffset : texcoord);CHECKGLERROR
1638 // texture array unit is disabled, disable the array
1639 if (unit->arrayenabled)
1641 unit->arrayenabled = false;
1642 GL_ClientActiveTexture(unitnum);
1643 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1648 void R_Mesh_TexBindAll(unsigned int unitnum, int tex1d, int tex2d, int tex3d, int texcubemap, int texrectangle)
1650 gltextureunit_t *unit = gl_state.units + unitnum;
1651 if (unitnum >= backendimageunits)
1653 // update 1d texture binding
1654 if (unit->t1d != tex1d)
1656 GL_ActiveTexture(unitnum);
1657 if (unitnum < backendunits)
1663 qglEnable(GL_TEXTURE_1D);CHECKGLERROR
1670 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1675 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1677 // update 2d texture binding
1678 if (unit->t2d != tex2d)
1680 GL_ActiveTexture(unitnum);
1681 if (unitnum < backendunits)
1687 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
1694 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1699 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1701 // update 3d texture binding
1702 if (unit->t3d != tex3d)
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 != texcubemap)
1728 GL_ActiveTexture(unitnum);
1729 if (unitnum < backendunits)
1733 if (unit->tcubemap == 0)
1735 qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1742 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1746 unit->tcubemap = texcubemap;
1747 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1749 // update rectangle texture binding
1750 if (unit->trectangle != texrectangle)
1752 GL_ActiveTexture(unitnum);
1753 if (unitnum < backendunits)
1757 if (unit->trectangle == 0)
1759 qglEnable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
1764 if (unit->trectangle)
1766 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
1770 unit->trectangle = texrectangle;
1771 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
1775 void R_Mesh_TexBind1D(unsigned int unitnum, int texnum)
1777 gltextureunit_t *unit = gl_state.units + unitnum;
1778 if (unitnum >= backendimageunits)
1780 // update 1d texture binding
1781 if (unit->t1d != texnum)
1783 GL_ActiveTexture(unitnum);
1784 if (unitnum < backendunits)
1790 qglEnable(GL_TEXTURE_1D);CHECKGLERROR
1797 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1802 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1804 // update 2d texture binding
1807 GL_ActiveTexture(unitnum);
1808 if (unitnum < backendunits)
1812 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1816 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1818 // update 3d texture binding
1821 GL_ActiveTexture(unitnum);
1822 if (unitnum < backendunits)
1826 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1830 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1832 // update cubemap texture binding
1835 GL_ActiveTexture(unitnum);
1836 if (unitnum < backendunits)
1840 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1844 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1846 // update rectangle texture binding
1847 if (unit->trectangle)
1849 GL_ActiveTexture(unitnum);
1850 if (unitnum < backendunits)
1852 if (unit->trectangle)
1854 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
1857 unit->trectangle = 0;
1858 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
1862 void R_Mesh_TexBind(unsigned int unitnum, int texnum)
1864 gltextureunit_t *unit = gl_state.units + unitnum;
1865 if (unitnum >= backendimageunits)
1867 // update 1d texture binding
1870 GL_ActiveTexture(unitnum);
1871 if (unitnum < backendunits)
1875 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1879 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1881 // update 2d texture binding
1882 if (unit->t2d != texnum)
1884 GL_ActiveTexture(unitnum);
1885 if (unitnum < backendunits)
1891 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
1898 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1903 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1905 // update 3d texture binding
1908 GL_ActiveTexture(unitnum);
1909 if (unitnum < backendunits)
1913 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1917 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1919 // update cubemap texture binding
1920 if (unit->tcubemap != 0)
1922 GL_ActiveTexture(unitnum);
1923 if (unitnum < backendunits)
1927 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1931 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1933 // update rectangle texture binding
1934 if (unit->trectangle != 0)
1936 GL_ActiveTexture(unitnum);
1937 if (unitnum < backendunits)
1939 if (unit->trectangle)
1941 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
1944 unit->trectangle = 0;
1945 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
1949 void R_Mesh_TexBind3D(unsigned int unitnum, int texnum)
1951 gltextureunit_t *unit = gl_state.units + unitnum;
1952 if (unitnum >= backendimageunits)
1954 // update 1d texture binding
1957 GL_ActiveTexture(unitnum);
1958 if (unitnum < backendunits)
1962 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1966 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1968 // update 2d texture binding
1971 GL_ActiveTexture(unitnum);
1972 if (unitnum < backendunits)
1976 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1980 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1982 // update 3d texture binding
1983 if (unit->t3d != texnum)
1985 GL_ActiveTexture(unitnum);
1986 if (unitnum < backendunits)
1992 qglEnable(GL_TEXTURE_3D);CHECKGLERROR
1999 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
2004 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
2006 // update cubemap texture binding
2007 if (unit->tcubemap != 0)
2009 GL_ActiveTexture(unitnum);
2010 if (unitnum < backendunits)
2014 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
2018 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
2020 // update rectangle texture binding
2021 if (unit->trectangle != 0)
2023 GL_ActiveTexture(unitnum);
2024 if (unitnum < backendunits)
2026 if (unit->trectangle)
2028 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
2031 unit->trectangle = 0;
2032 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
2036 void R_Mesh_TexBindCubeMap(unsigned int unitnum, int texnum)
2038 gltextureunit_t *unit = gl_state.units + unitnum;
2039 if (unitnum >= backendimageunits)
2041 // update 1d texture binding
2044 GL_ActiveTexture(unitnum);
2045 if (unitnum < backendunits)
2049 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
2053 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
2055 // update 2d texture binding
2058 GL_ActiveTexture(unitnum);
2059 if (unitnum < backendunits)
2063 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
2067 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
2069 // update 3d texture binding
2072 GL_ActiveTexture(unitnum);
2073 if (unitnum < backendunits)
2077 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
2081 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
2083 // update cubemap texture binding
2084 if (unit->tcubemap != texnum)
2086 GL_ActiveTexture(unitnum);
2087 if (unitnum < backendunits)
2091 if (unit->tcubemap == 0)
2093 qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
2100 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
2104 unit->tcubemap = texnum;
2105 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
2107 // update rectangle texture binding
2108 if (unit->trectangle != 0)
2110 GL_ActiveTexture(unitnum);
2111 if (unitnum < backendunits)
2113 if (unit->trectangle)
2115 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
2118 unit->trectangle = 0;
2119 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
2123 void R_Mesh_TexBindRectangle(unsigned int unitnum, int texnum)
2125 gltextureunit_t *unit = gl_state.units + unitnum;
2126 if (unitnum >= backendimageunits)
2128 // update 1d texture binding
2131 GL_ActiveTexture(unitnum);
2132 if (unitnum < backendunits)
2136 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
2140 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
2142 // update 2d texture binding
2145 GL_ActiveTexture(unitnum);
2146 if (unitnum < backendunits)
2150 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
2154 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
2156 // update 3d texture binding
2159 GL_ActiveTexture(unitnum);
2160 if (unitnum < backendunits)
2164 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
2168 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
2170 // update cubemap texture binding
2171 if (unit->tcubemap != 0)
2173 GL_ActiveTexture(unitnum);
2174 if (unitnum < backendunits)
2178 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
2182 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
2184 // update rectangle texture binding
2185 if (unit->trectangle != texnum)
2187 GL_ActiveTexture(unitnum);
2188 if (unitnum < backendunits)
2192 if (unit->trectangle == 0)
2194 qglEnable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
2199 if (unit->trectangle)
2201 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
2205 unit->trectangle = texnum;
2206 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
2210 static const double gl_identitymatrix[16] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
2212 void R_Mesh_TexMatrix(unsigned int unitnum, const matrix4x4_t *matrix)
2214 gltextureunit_t *unit = gl_state.units + unitnum;
2215 if (matrix->m[3][3])
2217 // texmatrix specified, check if it is different
2218 if (!unit->texmatrixenabled || memcmp(&unit->matrix, matrix, sizeof(matrix4x4_t)))
2220 double glmatrix[16];
2221 unit->texmatrixenabled = true;
2222 unit->matrix = *matrix;
2224 Matrix4x4_ToArrayDoubleGL(&unit->matrix, glmatrix);
2225 GL_ActiveTexture(unitnum);
2226 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
2227 qglLoadMatrixd(glmatrix);CHECKGLERROR
2228 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
2233 // no texmatrix specified, revert to identity
2234 if (unit->texmatrixenabled)
2236 unit->texmatrixenabled = false;
2237 unit->matrix = identitymatrix;
2239 GL_ActiveTexture(unitnum);
2240 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
2241 qglLoadIdentity();CHECKGLERROR
2242 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
2247 void R_Mesh_TexCombine(unsigned int unitnum, int combinergb, int combinealpha, int rgbscale, int alphascale)
2249 gltextureunit_t *unit = gl_state.units + unitnum;
2251 if (gl_combine.integer)
2253 // GL_ARB_texture_env_combine
2255 combinergb = GL_MODULATE;
2257 combinealpha = GL_MODULATE;
2262 if (unit->combinergb != combinergb)
2264 unit->combinergb = combinergb;
2265 GL_ActiveTexture(unitnum);
2266 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
2268 if (unit->combinealpha != combinealpha)
2270 unit->combinealpha = combinealpha;
2271 GL_ActiveTexture(unitnum);
2272 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
2274 if (unit->rgbscale != rgbscale)
2276 GL_ActiveTexture(unitnum);
2277 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (unit->rgbscale = rgbscale));CHECKGLERROR
2279 if (unit->alphascale != alphascale)
2281 GL_ActiveTexture(unitnum);
2282 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, (unit->alphascale = alphascale));CHECKGLERROR
2289 combinergb = GL_MODULATE;
2290 if (unit->combinergb != combinergb)
2292 unit->combinergb = combinergb;
2293 GL_ActiveTexture(unitnum);
2294 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combinergb);CHECKGLERROR
2299 void R_Mesh_TextureState(const rmeshstate_t *m)
2306 if (gl_backend_rebindtextures)
2308 gl_backend_rebindtextures = false;
2309 GL_SetupTextureState();
2313 for (i = 0;i < backendimageunits;i++)
2314 R_Mesh_TexBindAll(i, m->tex1d[i], m->tex[i], m->tex3d[i], m->texcubemap[i], m->texrectangle[i]);
2315 for (i = 0;i < backendarrayunits;i++)
2317 if (m->pointer_texcoord3f[i])
2318 R_Mesh_TexCoordPointer(i, 3, m->pointer_texcoord3f[i], m->pointer_texcoord_bufferobject[i], m->pointer_texcoord_bufferoffset[i]);
2320 R_Mesh_TexCoordPointer(i, 2, m->pointer_texcoord[i], m->pointer_texcoord_bufferobject[i], m->pointer_texcoord_bufferoffset[i]);
2322 for (i = 0;i < backendunits;i++)
2324 R_Mesh_TexMatrix(i, &m->texmatrix[i]);
2325 R_Mesh_TexCombine(i, m->texcombinergb[i], m->texcombinealpha[i], m->texrgbscale[i], m->texalphascale[i]);
2330 void R_Mesh_ResetTextureState(void)
2332 unsigned int unitnum;
2337 if (gl_backend_rebindtextures)
2339 gl_backend_rebindtextures = false;
2340 GL_SetupTextureState();
2344 for (unitnum = 0;unitnum < backendimageunits;unitnum++)
2346 gltextureunit_t *unit = gl_state.units + unitnum;
2347 // update 1d texture binding
2350 GL_ActiveTexture(unitnum);
2351 if (unitnum < backendunits)
2353 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
2356 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
2358 // update 2d texture binding
2361 GL_ActiveTexture(unitnum);
2362 if (unitnum < backendunits)
2364 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
2367 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
2369 // update 3d texture binding
2372 GL_ActiveTexture(unitnum);
2373 if (unitnum < backendunits)
2375 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
2378 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
2380 // update cubemap texture binding
2383 GL_ActiveTexture(unitnum);
2384 if (unitnum < backendunits)
2386 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
2389 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
2391 // update rectangle texture binding
2392 if (unit->trectangle)
2394 GL_ActiveTexture(unitnum);
2395 if (unitnum < backendunits)
2397 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
2399 unit->trectangle = 0;
2400 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
2403 for (unitnum = 0;unitnum < backendarrayunits;unitnum++)
2405 gltextureunit_t *unit = gl_state.units + unitnum;
2406 // texture array unit is disabled, disable the array
2407 if (unit->arrayenabled)
2409 unit->arrayenabled = false;
2410 GL_ClientActiveTexture(unitnum);
2411 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
2414 for (unitnum = 0;unitnum < backendunits;unitnum++)
2416 gltextureunit_t *unit = gl_state.units + unitnum;
2417 // no texmatrix specified, revert to identity
2418 if (unit->texmatrixenabled)
2420 unit->texmatrixenabled = false;
2421 unit->matrix = identitymatrix;
2423 GL_ActiveTexture(unitnum);
2424 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
2425 qglLoadIdentity();CHECKGLERROR
2426 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
2428 if (gl_combine.integer)
2430 // GL_ARB_texture_env_combine
2431 if (unit->combinergb != GL_MODULATE)
2433 unit->combinergb = GL_MODULATE;
2434 GL_ActiveTexture(unitnum);
2435 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
2437 if (unit->combinealpha != GL_MODULATE)
2439 unit->combinealpha = GL_MODULATE;
2440 GL_ActiveTexture(unitnum);
2441 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
2443 if (unit->rgbscale != 1)
2445 GL_ActiveTexture(unitnum);
2446 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (unit->rgbscale = 1));CHECKGLERROR
2448 if (unit->alphascale != 1)
2450 GL_ActiveTexture(unitnum);
2451 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, (unit->alphascale = 1));CHECKGLERROR
2457 if (unit->combinergb != GL_MODULATE)
2459 unit->combinergb = GL_MODULATE;
2460 GL_ActiveTexture(unitnum);
2461 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combinergb);CHECKGLERROR