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] =
445 // standard cubemap projections
483 float rectviewmatrix[6][16] =
485 // sign-preserving cubemap projections
524 void R_Viewport_InitCubeSideView(r_viewport_t *v, const matrix4x4_t *cameramatrix, int side, int size, float nearclip, float farclip, const float *nearplane)
526 matrix4x4_t tempmatrix, basematrix;
527 memset(v, 0, sizeof(*v));
528 v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
529 v->cameramatrix = *cameramatrix;
533 v->m[0] = v->m[5] = 1.0f;
534 v->m[10] = -(farclip + nearclip) / (farclip - nearclip);
536 v->m[14] = -2 * nearclip * farclip / (farclip - nearclip);
538 Matrix4x4_FromArrayFloatGL(&basematrix, cubeviewmatrix[side]);
539 Matrix4x4_Invert_Simple(&tempmatrix, &v->cameramatrix);
540 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
541 Matrix4x4_FromArrayDoubleGL(&v->projectmatrix, v->m);
544 R_Viewport_ApplyNearClipPlane(v, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
547 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)
549 matrix4x4_t tempmatrix, basematrix;
550 if (border > size - 2)
552 memset(v, 0, sizeof(*v));
553 v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
554 v->cameramatrix = *cameramatrix;
555 v->x = (side & 1) * size;
556 v->y = (side >> 1) * size;
560 v->m[0] = v->m[5] = 1.0f * ((float)size - border) / size;
561 v->m[10] = -(farclip + nearclip) / (farclip - nearclip);
563 v->m[14] = -2 * nearclip * farclip / (farclip - nearclip);
565 Matrix4x4_FromArrayFloatGL(&basematrix, rectviewmatrix[side]);
566 Matrix4x4_Invert_Simple(&tempmatrix, &v->cameramatrix);
567 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
568 Matrix4x4_FromArrayDoubleGL(&v->projectmatrix, v->m);
571 R_Viewport_ApplyNearClipPlane(v, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
574 void R_SetViewport(const r_viewport_t *v)
577 backend_viewport = *v;
580 qglViewport(v->x, v->y, v->width, v->height);CHECKGLERROR
582 // Load the projection matrix into OpenGL
583 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
584 qglLoadMatrixd(backend_viewport.m);CHECKGLERROR
585 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
587 // FIXME: v_flipped_state is evil, this probably breaks somewhere
588 GL_SetMirrorState(v_flipped.integer && (v->type == R_VIEWPORTTYPE_PERSPECTIVE || v->type == R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP));
590 // directly force an update of the modelview matrix
591 Matrix4x4_Concat(&backend_modelviewmatrix, &backend_viewport.viewmatrix, &backend_modelmatrix);
592 Matrix4x4_ToArrayFloatGL(&backend_modelviewmatrix, glmatrix);
593 qglLoadMatrixf(glmatrix);CHECKGLERROR
596 void R_GetViewport(r_viewport_t *v)
598 *v = backend_viewport;
601 typedef struct gltextureunit_s
603 const void *pointer_texcoord;
604 size_t pointer_texcoord_offset;
605 int pointer_texcoord_buffer;
606 int t1d, t2d, t3d, tcubemap, trectangle;
608 unsigned int arraycomponents;
609 int rgbscale, alphascale;
610 int combinergb, combinealpha;
611 // FIXME: add more combine stuff
612 // texmatrixenabled exists only to avoid unnecessary texmatrix compares
613 int texmatrixenabled;
618 static struct gl_state_s
626 int colormask; // stored as bottom 4 bits: r g b a (3 2 1 0 order)
629 float polygonoffset[2];
633 unsigned int clientunit;
634 gltextureunit_t units[MAX_TEXTUREUNITS];
638 int vertexbufferobject;
639 int elementbufferobject;
640 qboolean pointer_color_enabled;
641 const void *pointer_vertex;
642 const void *pointer_color;
643 size_t pointer_vertex_offset;
644 size_t pointer_color_offset;
645 int pointer_vertex_buffer;
646 int pointer_color_buffer;
650 static void GL_BindVBO(int bufferobject)
652 if (gl_state.vertexbufferobject != bufferobject)
654 gl_state.vertexbufferobject = bufferobject;
656 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, bufferobject);
661 static void GL_BindEBO(int bufferobject)
663 if (gl_state.elementbufferobject != bufferobject)
665 gl_state.elementbufferobject = bufferobject;
667 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, bufferobject);
672 void GL_SetupTextureState(void)
675 gltextureunit_t *unit;
677 gl_state.unit = MAX_TEXTUREUNITS;
678 gl_state.clientunit = MAX_TEXTUREUNITS;
679 for (i = 0;i < MAX_TEXTUREUNITS;i++)
681 unit = gl_state.units + i;
686 unit->arrayenabled = false;
687 unit->arraycomponents = 0;
688 unit->pointer_texcoord = NULL;
689 unit->pointer_texcoord_buffer = 0;
690 unit->pointer_texcoord_offset = 0;
692 unit->alphascale = 1;
693 unit->combinergb = GL_MODULATE;
694 unit->combinealpha = GL_MODULATE;
695 unit->texmatrixenabled = false;
696 unit->matrix = identitymatrix;
699 for (i = 0;i < backendimageunits;i++)
702 qglBindTexture(GL_TEXTURE_1D, 0);CHECKGLERROR
703 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
706 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
708 if (gl_texturecubemap)
710 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
712 if (gl_texturerectangle)
714 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);CHECKGLERROR
718 for (i = 0;i < backendarrayunits;i++)
720 GL_ClientActiveTexture(i);
722 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
723 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
726 for (i = 0;i < backendunits;i++)
729 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
730 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
733 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
735 if (gl_texturecubemap)
737 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
739 if (gl_texturerectangle)
741 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
743 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
744 qglLoadIdentity();CHECKGLERROR
745 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
746 if (gl_combine.integer)
748 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);CHECKGLERROR
749 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);CHECKGLERROR
750 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);CHECKGLERROR
751 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
752 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_TEXTURE);CHECKGLERROR // for GL_INTERPOLATE_ARB mode
753 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
754 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
755 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);CHECKGLERROR
756 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);CHECKGLERROR
757 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);CHECKGLERROR
758 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
759 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, GL_CONSTANT_ARB);CHECKGLERROR
760 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
761 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
762 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
763 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
764 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
768 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
775 void GL_Backend_ResetState(void)
777 memset(&gl_state, 0, sizeof(gl_state));
778 gl_state.depthtest = true;
779 gl_state.alphatest = false;
780 gl_state.blendfunc1 = GL_ONE;
781 gl_state.blendfunc2 = GL_ZERO;
782 gl_state.blend = false;
783 gl_state.depthmask = GL_TRUE;
784 gl_state.colormask = 15;
785 gl_state.color4f[0] = gl_state.color4f[1] = gl_state.color4f[2] = gl_state.color4f[3] = 1;
786 gl_state.lockrange_first = 0;
787 gl_state.lockrange_count = 0;
788 gl_state.cullface = v_flipped_state ? GL_BACK : GL_FRONT; // quake is backwards, this culls back faces
789 gl_state.cullfaceenable = true;
790 gl_state.polygonoffset[0] = 0;
791 gl_state.polygonoffset[1] = 0;
795 qglColorMask(1, 1, 1, 1);
796 qglAlphaFunc(GL_GEQUAL, 0.5);CHECKGLERROR
797 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
798 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
799 qglDisable(GL_BLEND);CHECKGLERROR
800 qglCullFace(gl_state.cullface);CHECKGLERROR
801 qglEnable(GL_CULL_FACE);CHECKGLERROR
802 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
803 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
804 qglDepthMask(gl_state.depthmask);CHECKGLERROR
805 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
807 if (gl_support_arb_vertex_buffer_object)
809 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
810 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
813 if (gl_support_ext_framebuffer_object)
815 qglBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
816 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
819 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), NULL);CHECKGLERROR
820 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
822 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL);CHECKGLERROR
823 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
825 GL_Color(0, 0, 0, 0);
826 GL_Color(1, 1, 1, 1);
828 GL_SetupTextureState();
831 void GL_ActiveTexture(unsigned int num)
833 if (gl_state.unit != num)
836 if (qglActiveTexture)
839 qglActiveTexture(GL_TEXTURE0_ARB + gl_state.unit);
845 void GL_ClientActiveTexture(unsigned int num)
847 if (gl_state.clientunit != num)
849 gl_state.clientunit = num;
850 if (qglActiveTexture)
853 qglClientActiveTexture(GL_TEXTURE0_ARB + gl_state.clientunit);
859 void GL_BlendFunc(int blendfunc1, int blendfunc2)
861 if (gl_state.blendfunc1 != blendfunc1 || gl_state.blendfunc2 != blendfunc2)
864 qglBlendFunc(gl_state.blendfunc1 = blendfunc1, gl_state.blendfunc2 = blendfunc2);CHECKGLERROR
865 if (gl_state.blendfunc2 == GL_ZERO)
867 if (gl_state.blendfunc1 == GL_ONE)
872 qglDisable(GL_BLEND);CHECKGLERROR
880 qglEnable(GL_BLEND);CHECKGLERROR
889 qglEnable(GL_BLEND);CHECKGLERROR
895 void GL_DepthMask(int state)
897 if (gl_state.depthmask != state)
900 qglDepthMask(gl_state.depthmask = state);CHECKGLERROR
904 void GL_DepthTest(int state)
906 if (gl_state.depthtest != state)
908 gl_state.depthtest = state;
910 if (gl_state.depthtest)
912 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
916 qglDisable(GL_DEPTH_TEST);CHECKGLERROR
921 void GL_DepthRange(float nearfrac, float farfrac)
923 if (gl_state.depthrange[0] != nearfrac || gl_state.depthrange[1] != farfrac)
925 gl_state.depthrange[0] = nearfrac;
926 gl_state.depthrange[1] = farfrac;
927 qglDepthRange(nearfrac, farfrac);
931 void GL_PolygonOffset(float planeoffset, float depthoffset)
933 if (gl_state.polygonoffset[0] != planeoffset || gl_state.polygonoffset[1] != depthoffset)
935 gl_state.polygonoffset[0] = planeoffset;
936 gl_state.polygonoffset[1] = depthoffset;
937 qglPolygonOffset(planeoffset, depthoffset);
941 void GL_SetMirrorState(qboolean state)
943 if(!state != !v_flipped_state)
945 // change cull face mode!
946 if(gl_state.cullface == GL_BACK)
947 qglCullFace((gl_state.cullface = GL_FRONT));
948 else if(gl_state.cullface == GL_FRONT)
949 qglCullFace((gl_state.cullface = GL_BACK));
951 v_flipped_state = state;
954 void GL_CullFace(int state)
960 if(state == GL_FRONT)
962 else if(state == GL_BACK)
966 if (state != GL_NONE)
968 if (!gl_state.cullfaceenable)
970 gl_state.cullfaceenable = true;
971 qglEnable(GL_CULL_FACE);CHECKGLERROR
973 if (gl_state.cullface != state)
975 gl_state.cullface = state;
976 qglCullFace(gl_state.cullface);CHECKGLERROR
981 if (gl_state.cullfaceenable)
983 gl_state.cullfaceenable = false;
984 qglDisable(GL_CULL_FACE);CHECKGLERROR
989 void GL_AlphaTest(int state)
991 if (gl_state.alphatest != state)
993 gl_state.alphatest = state;
995 if (gl_state.alphatest)
997 qglEnable(GL_ALPHA_TEST);CHECKGLERROR
1001 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
1006 void GL_ColorMask(int r, int g, int b, int a)
1008 int state = r*8 + g*4 + b*2 + a*1;
1009 if (gl_state.colormask != state)
1011 gl_state.colormask = state;
1013 qglColorMask((GLboolean)r, (GLboolean)g, (GLboolean)b, (GLboolean)a);CHECKGLERROR
1017 void GL_Color(float cr, float cg, float cb, float ca)
1019 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)
1021 gl_state.color4f[0] = cr;
1022 gl_state.color4f[1] = cg;
1023 gl_state.color4f[2] = cb;
1024 gl_state.color4f[3] = ca;
1026 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);
1031 void GL_LockArrays(int first, int count)
1033 if (count < gl_lockarrays_minimumvertices.integer)
1038 if (gl_state.lockrange_count != count || gl_state.lockrange_first != first)
1040 if (gl_state.lockrange_count)
1042 gl_state.lockrange_count = 0;
1044 qglUnlockArraysEXT();
1047 if (count && gl_supportslockarrays && gl_lockarrays.integer && r_render.integer)
1049 gl_state.lockrange_first = first;
1050 gl_state.lockrange_count = count;
1052 qglLockArraysEXT(first, count);
1058 void GL_Scissor (int x, int y, int width, int height)
1061 qglScissor(x, y,width,height);
1065 void GL_ScissorTest(int state)
1067 if(gl_state.scissortest == state)
1071 if((gl_state.scissortest = state))
1072 qglEnable(GL_SCISSOR_TEST);
1074 qglDisable(GL_SCISSOR_TEST);
1078 void GL_Clear(int mask)
1081 qglClear(mask);CHECKGLERROR
1084 // called at beginning of frame
1085 void R_Mesh_Start(void)
1089 if (gl_printcheckerror.integer && !gl_paranoid.integer)
1091 Con_Printf("WARNING: gl_printcheckerror is on but gl_paranoid is off, turning it on...\n");
1092 Cvar_SetValueQuick(&gl_paranoid, 1);
1094 GL_Backend_ResetState();
1097 qboolean GL_Backend_CompileShader(int programobject, GLenum shadertypeenum, const char *shadertype, int numstrings, const char **strings)
1101 char compilelog[MAX_INPUTLINE];
1102 shaderobject = qglCreateShaderObjectARB(shadertypeenum);CHECKGLERROR
1105 qglShaderSourceARB(shaderobject, numstrings, strings, NULL);CHECKGLERROR
1106 qglCompileShaderARB(shaderobject);CHECKGLERROR
1107 qglGetObjectParameterivARB(shaderobject, GL_OBJECT_COMPILE_STATUS_ARB, &shadercompiled);CHECKGLERROR
1108 qglGetInfoLogARB(shaderobject, sizeof(compilelog), NULL, compilelog);CHECKGLERROR
1109 if (compilelog[0] && developer.integer > 0)
1111 int i, j, pretextlines = 0;
1112 for (i = 0;i < numstrings - 1;i++)
1113 for (j = 0;strings[i][j];j++)
1114 if (strings[i][j] == '\n')
1116 Con_DPrintf("%s shader compile log:\n%s\n(line offset for any above warnings/errors: %i)\n", shadertype, compilelog, pretextlines);
1118 if (!shadercompiled)
1120 qglDeleteObjectARB(shaderobject);CHECKGLERROR
1123 qglAttachObjectARB(programobject, shaderobject);CHECKGLERROR
1124 qglDeleteObjectARB(shaderobject);CHECKGLERROR
1128 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)
1130 GLint programlinked;
1131 GLuint programobject = 0;
1132 char linklog[MAX_INPUTLINE];
1135 programobject = qglCreateProgramObjectARB();CHECKGLERROR
1139 if (vertexstrings_count && !GL_Backend_CompileShader(programobject, GL_VERTEX_SHADER_ARB, "vertex", vertexstrings_count, vertexstrings_list))
1142 #ifdef GL_GEOMETRY_SHADER_ARB
1143 if (geometrystrings_count && !GL_Backend_CompileShader(programobject, GL_GEOMETRY_SHADER_ARB, "geometry", geometrystrings_count, geometrystrings_list))
1147 if (fragmentstrings_count && !GL_Backend_CompileShader(programobject, GL_FRAGMENT_SHADER_ARB, "fragment", fragmentstrings_count, fragmentstrings_list))
1150 qglLinkProgramARB(programobject);CHECKGLERROR
1151 qglGetObjectParameterivARB(programobject, GL_OBJECT_LINK_STATUS_ARB, &programlinked);CHECKGLERROR
1152 qglGetInfoLogARB(programobject, sizeof(linklog), NULL, linklog);CHECKGLERROR
1155 Con_DPrintf("program link log:\n%s\n", linklog);
1156 // software vertex shader is ok but software fragment shader is WAY
1157 // too slow, fail program if so.
1158 // NOTE: this string might be ATI specific, but that's ok because the
1159 // ATI R300 chip (Radeon 9500-9800/X300) is the most likely to use a
1160 // software fragment shader due to low instruction and dependent
1162 if (strstr(linklog, "fragment shader will run in software"))
1163 programlinked = false;
1167 return programobject;
1169 qglDeleteObjectARB(programobject);CHECKGLERROR
1173 void GL_Backend_FreeProgram(unsigned int prog)
1176 qglDeleteObjectARB(prog);
1180 int gl_backend_rebindtextures;
1182 void GL_Backend_RenumberElements(int *out, int count, const int *in, int offset)
1187 for (i = 0;i < count;i++)
1188 *out++ = *in++ + offset;
1191 memcpy(out, in, sizeof(*out) * count);
1194 // renders triangles using vertices from the active arrays
1195 int paranoidblah = 0;
1196 void R_Mesh_Draw(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const unsigned short *element3s, int bufferobject3i, int bufferobject3s)
1198 unsigned int numelements = numtriangles * 3;
1199 if (numvertices < 3 || numtriangles < 1)
1201 if (numvertices < 0 || numtriangles < 0 || developer.integer >= 100)
1202 Con_Printf("R_Mesh_Draw(%d, %d, %d, %d, %8p, %8p, %i, %i);\n", firstvertex, numvertices, firsttriangle, numtriangles, (void *)element3i, (void *)element3s, bufferobject3i, bufferobject3s);
1205 if (!gl_mesh_prefer_short_elements.integer)
1213 element3i += firsttriangle * 3;
1215 element3s += firsttriangle * 3;
1216 switch (gl_vbo.integer)
1221 bufferobject3i = bufferobject3s = 0;
1227 bufferobject3i = bufferobject3s = 0;
1231 r_refdef.stats.meshes++;
1232 r_refdef.stats.meshes_elements += numelements;
1233 if (gl_paranoid.integer)
1235 unsigned int i, j, size;
1237 // note: there's no validation done here on buffer objects because it
1238 // is somewhat difficult to get at the data, and gl_paranoid can be
1239 // used without buffer objects if the need arises
1240 // (the data could be gotten using glMapBuffer but it would be very
1241 // slow due to uncachable video memory reads)
1242 if (!qglIsEnabled(GL_VERTEX_ARRAY))
1243 Con_Print("R_Mesh_Draw: vertex array not enabled\n");
1245 if (gl_state.pointer_vertex)
1246 for (j = 0, size = numvertices * 3, p = (int *)((float *)gl_state.pointer_vertex + firstvertex * 3);j < size;j++, p++)
1248 if (gl_state.pointer_color_enabled)
1250 if (!qglIsEnabled(GL_COLOR_ARRAY))
1251 Con_Print("R_Mesh_Draw: color array set but not enabled\n");
1253 if (gl_state.pointer_color && gl_state.pointer_color_enabled)
1254 for (j = 0, size = numvertices * 4, p = (int *)((float *)gl_state.pointer_color + firstvertex * 4);j < size;j++, p++)
1257 for (i = 0;i < backendarrayunits;i++)
1259 if (gl_state.units[i].arrayenabled)
1261 GL_ClientActiveTexture(i);
1262 if (!qglIsEnabled(GL_TEXTURE_COORD_ARRAY))
1263 Con_Print("R_Mesh_Draw: texcoord array set but not enabled\n");
1265 if (gl_state.units[i].pointer_texcoord && gl_state.units[i].arrayenabled)
1266 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++)
1272 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1274 if (element3i[i] < firstvertex || element3i[i] >= firstvertex + numvertices)
1276 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3i array\n", element3i[i], firstvertex, firstvertex + numvertices);
1283 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1285 if (element3s[i] < firstvertex || element3s[i] >= firstvertex + numvertices)
1287 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3s array\n", element3s[i], firstvertex, firstvertex + numvertices);
1294 if (r_render.integer)
1297 if (gl_mesh_testmanualfeeding.integer)
1299 unsigned int i, j, element;
1301 qglBegin(GL_TRIANGLES);
1302 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1304 element = element3i ? element3i[i] : element3s[i];
1305 for (j = 0;j < backendarrayunits;j++)
1307 if (gl_state.units[j].pointer_texcoord && gl_state.units[j].arrayenabled)
1309 if (backendarrayunits > 1)
1311 if (gl_state.units[j].arraycomponents == 4)
1313 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 4;
1314 qglMultiTexCoord4f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2], p[3]);
1316 else if (gl_state.units[j].arraycomponents == 3)
1318 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 3;
1319 qglMultiTexCoord3f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2]);
1321 else if (gl_state.units[j].arraycomponents == 2)
1323 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 2;
1324 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, p[0], p[1]);
1328 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 1;
1329 qglMultiTexCoord1f(GL_TEXTURE0_ARB + j, p[0]);
1334 if (gl_state.units[j].arraycomponents == 4)
1336 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 4;
1337 qglTexCoord4f(p[0], p[1], p[2], p[3]);
1339 else if (gl_state.units[j].arraycomponents == 3)
1341 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 3;
1342 qglTexCoord3f(p[0], p[1], p[2]);
1344 else if (gl_state.units[j].arraycomponents == 2)
1346 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 2;
1347 qglTexCoord2f(p[0], p[1]);
1351 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 1;
1352 qglTexCoord1f(p[0]);
1357 if (gl_state.pointer_color && gl_state.pointer_color_enabled)
1359 p = ((const GLfloat *)(gl_state.pointer_color)) + element * 4;
1360 qglColor4f(p[0], p[1], p[2], p[3]);
1362 p = ((const GLfloat *)(gl_state.pointer_vertex)) + element * 3;
1363 qglVertex3f(p[0], p[1], p[2]);
1368 else if (gl_mesh_testarrayelement.integer)
1371 qglBegin(GL_TRIANGLES);
1374 for (i = 0;i < numtriangles * 3;i++)
1375 qglArrayElement(element3i[i]);
1379 for (i = 0;i < numtriangles * 3;i++)
1380 qglArrayElement(element3s[i]);
1385 else if (bufferobject3s)
1387 GL_BindEBO(bufferobject3s);
1388 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1390 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, (void *)(firsttriangle * sizeof(unsigned short[3])));
1395 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, (void *)(firsttriangle * sizeof(unsigned short[3])));
1399 else if (bufferobject3i)
1401 GL_BindEBO(bufferobject3i);
1402 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1404 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, (void *)(firsttriangle * sizeof(unsigned int[3])));
1409 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, (void *)(firsttriangle * sizeof(unsigned int[3])));
1416 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1418 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, element3s);
1423 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, element3s);
1430 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1432 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, element3i);
1437 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, element3i);
1444 // restores backend state, used when done with 3D rendering
1445 void R_Mesh_Finish(void)
1450 GL_LockArrays(0, 0);
1453 for (i = 0;i < backendimageunits;i++)
1455 GL_ActiveTexture(i);
1456 qglBindTexture(GL_TEXTURE_1D, 0);CHECKGLERROR
1457 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
1460 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
1462 if (gl_texturecubemap)
1464 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
1466 if (gl_texturerectangle)
1468 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);CHECKGLERROR
1471 for (i = 0;i < backendarrayunits;i++)
1473 GL_ActiveTexture(backendarrayunits - 1 - i);
1474 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1476 for (i = 0;i < backendunits;i++)
1478 GL_ActiveTexture(backendunits - 1 - i);
1479 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1480 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1483 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1485 if (gl_texturecubemap)
1487 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1489 if (gl_texturerectangle)
1491 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
1493 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
1494 if (gl_combine.integer)
1496 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
1497 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
1500 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1501 qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
1503 qglDisable(GL_BLEND);CHECKGLERROR
1504 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1505 qglDepthMask(GL_TRUE);CHECKGLERROR
1506 qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);CHECKGLERROR
1509 int R_Mesh_CreateStaticBufferObject(unsigned int target, void *data, size_t size, const char *name)
1511 gl_bufferobjectinfo_t *info;
1512 GLuint bufferobject;
1514 if (!gl_vbo.integer)
1517 qglGenBuffersARB(1, &bufferobject);
1520 case GL_ELEMENT_ARRAY_BUFFER_ARB: GL_BindEBO(bufferobject);break;
1521 case GL_ARRAY_BUFFER_ARB: GL_BindVBO(bufferobject);break;
1522 default: Sys_Error("R_Mesh_CreateStaticBufferObject: unknown target type %i\n", target);return 0;
1524 qglBufferDataARB(target, size, data, GL_STATIC_DRAW_ARB);
1526 info = (gl_bufferobjectinfo_t *) Mem_ExpandableArray_AllocRecord(&gl_bufferobjectinfoarray);
1527 memset(info, 0, sizeof(*info));
1528 info->target = target;
1529 info->object = bufferobject;
1531 strlcpy(info->name, name, sizeof(info->name));
1533 return (int)bufferobject;
1536 void R_Mesh_DestroyBufferObject(int bufferobject)
1539 gl_bufferobjectinfo_t *info;
1541 qglDeleteBuffersARB(1, (GLuint *)&bufferobject);
1543 endindex = Mem_ExpandableArray_IndexRange(&gl_bufferobjectinfoarray);
1544 for (i = 0;i < endindex;i++)
1546 info = (gl_bufferobjectinfo_t *) Mem_ExpandableArray_RecordAtIndex(&gl_bufferobjectinfoarray, i);
1549 if (info->object == bufferobject)
1551 Mem_ExpandableArray_FreeRecord(&gl_bufferobjectinfoarray, (void *)info);
1557 void GL_Mesh_ListVBOs(qboolean printeach)
1560 size_t ebocount = 0, ebomemory = 0;
1561 size_t vbocount = 0, vbomemory = 0;
1562 gl_bufferobjectinfo_t *info;
1563 endindex = Mem_ExpandableArray_IndexRange(&gl_bufferobjectinfoarray);
1564 for (i = 0;i < endindex;i++)
1566 info = (gl_bufferobjectinfo_t *) Mem_ExpandableArray_RecordAtIndex(&gl_bufferobjectinfoarray, i);
1569 switch(info->target)
1571 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;
1572 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;
1573 default: Con_Printf("gl_vbostats: unknown target type %i\n", info->target);break;
1576 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);
1579 void R_Mesh_Matrix(const matrix4x4_t *matrix)
1581 if (memcmp(matrix, &backend_modelmatrix, sizeof(matrix4x4_t)))
1584 backend_modelmatrix = *matrix;
1585 Matrix4x4_Concat(&backend_modelviewmatrix, &backend_viewport.viewmatrix, &backend_modelmatrix);
1586 Matrix4x4_ToArrayFloatGL(&backend_modelviewmatrix, glmatrix);
1588 qglLoadMatrixf(glmatrix);CHECKGLERROR
1592 void R_Mesh_VertexPointer(const float *vertex3f, int bufferobject, size_t bufferoffset)
1594 if (!gl_vbo.integer)
1596 if (gl_state.pointer_vertex != vertex3f || gl_state.pointer_vertex_buffer != bufferobject || gl_state.pointer_vertex_offset != bufferoffset)
1598 gl_state.pointer_vertex = vertex3f;
1599 gl_state.pointer_vertex_buffer = bufferobject;
1600 gl_state.pointer_vertex_offset = bufferoffset;
1602 GL_BindVBO(bufferobject);
1603 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), bufferobject ? (void *)bufferoffset : vertex3f);CHECKGLERROR
1607 void R_Mesh_ColorPointer(const float *color4f, int bufferobject, size_t bufferoffset)
1609 // note: this can not rely on bufferobject to decide whether a color array
1610 // is supplied, because surfmesh_t shares one vbo for all arrays, which
1611 // means that a valid vbo may be supplied even if there is no color array.
1614 if (!gl_vbo.integer)
1616 // caller wants color array enabled
1617 if (!gl_state.pointer_color_enabled)
1619 gl_state.pointer_color_enabled = true;
1621 qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1623 if (gl_state.pointer_color != color4f || gl_state.pointer_color_buffer != bufferobject || gl_state.pointer_color_offset != bufferoffset)
1625 gl_state.pointer_color = color4f;
1626 gl_state.pointer_color_buffer = bufferobject;
1627 gl_state.pointer_color_offset = bufferoffset;
1629 GL_BindVBO(bufferobject);
1630 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), bufferobject ? (void *)bufferoffset : color4f);CHECKGLERROR
1635 // caller wants color array disabled
1636 if (gl_state.pointer_color_enabled)
1638 gl_state.pointer_color_enabled = false;
1640 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1641 // when color array is on the glColor gets trashed, set it again
1642 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);CHECKGLERROR
1647 void R_Mesh_TexCoordPointer(unsigned int unitnum, unsigned int numcomponents, const float *texcoord, int bufferobject, size_t bufferoffset)
1649 gltextureunit_t *unit = gl_state.units + unitnum;
1650 // update array settings
1652 // note: there is no need to check bufferobject here because all cases
1653 // that involve a valid bufferobject also supply a texcoord array
1656 if (!gl_vbo.integer)
1658 // texture array unit is enabled, enable the array
1659 if (!unit->arrayenabled)
1661 unit->arrayenabled = true;
1662 GL_ClientActiveTexture(unitnum);
1663 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1666 if (unit->pointer_texcoord != texcoord || unit->pointer_texcoord_buffer != bufferobject || unit->pointer_texcoord_offset != bufferoffset || unit->arraycomponents != numcomponents)
1668 unit->pointer_texcoord = texcoord;
1669 unit->pointer_texcoord_buffer = bufferobject;
1670 unit->pointer_texcoord_offset = bufferoffset;
1671 unit->arraycomponents = numcomponents;
1672 GL_ClientActiveTexture(unitnum);
1673 GL_BindVBO(bufferobject);
1674 qglTexCoordPointer(unit->arraycomponents, GL_FLOAT, sizeof(float) * unit->arraycomponents, bufferobject ? (void *)bufferoffset : texcoord);CHECKGLERROR
1679 // texture array unit is disabled, disable the array
1680 if (unit->arrayenabled)
1682 unit->arrayenabled = false;
1683 GL_ClientActiveTexture(unitnum);
1684 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1689 void R_Mesh_TexBindAll(unsigned int unitnum, int tex1d, int tex2d, int tex3d, int texcubemap, int texrectangle)
1691 gltextureunit_t *unit = gl_state.units + unitnum;
1692 if (unitnum >= backendimageunits)
1694 // update 1d texture binding
1695 if (unit->t1d != tex1d)
1697 GL_ActiveTexture(unitnum);
1698 if (unitnum < backendunits)
1704 qglEnable(GL_TEXTURE_1D);CHECKGLERROR
1711 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1716 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1718 // update 2d texture binding
1719 if (unit->t2d != tex2d)
1721 GL_ActiveTexture(unitnum);
1722 if (unitnum < backendunits)
1728 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
1735 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1740 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1742 // update 3d texture binding
1743 if (unit->t3d != tex3d)
1745 GL_ActiveTexture(unitnum);
1746 if (unitnum < backendunits)
1752 qglEnable(GL_TEXTURE_3D);CHECKGLERROR
1759 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1764 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1766 // update cubemap texture binding
1767 if (unit->tcubemap != texcubemap)
1769 GL_ActiveTexture(unitnum);
1770 if (unitnum < backendunits)
1774 if (unit->tcubemap == 0)
1776 qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1783 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1787 unit->tcubemap = texcubemap;
1788 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1790 // update rectangle texture binding
1791 if (unit->trectangle != texrectangle)
1793 GL_ActiveTexture(unitnum);
1794 if (unitnum < backendunits)
1798 if (unit->trectangle == 0)
1800 qglEnable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
1805 if (unit->trectangle)
1807 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
1811 unit->trectangle = texrectangle;
1812 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
1816 void R_Mesh_TexBind1D(unsigned int unitnum, int texnum)
1818 gltextureunit_t *unit = gl_state.units + unitnum;
1819 if (unitnum >= backendimageunits)
1821 // update 1d texture binding
1822 if (unit->t1d != texnum)
1824 GL_ActiveTexture(unitnum);
1825 if (unitnum < backendunits)
1831 qglEnable(GL_TEXTURE_1D);CHECKGLERROR
1838 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1843 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1845 // update 2d texture binding
1848 GL_ActiveTexture(unitnum);
1849 if (unitnum < backendunits)
1853 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1857 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1859 // update 3d texture binding
1862 GL_ActiveTexture(unitnum);
1863 if (unitnum < backendunits)
1867 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1871 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1873 // update cubemap texture binding
1876 GL_ActiveTexture(unitnum);
1877 if (unitnum < backendunits)
1881 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1885 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1887 // update rectangle texture binding
1888 if (unit->trectangle)
1890 GL_ActiveTexture(unitnum);
1891 if (unitnum < backendunits)
1893 if (unit->trectangle)
1895 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
1898 unit->trectangle = 0;
1899 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
1903 void R_Mesh_TexBind(unsigned int unitnum, int texnum)
1905 gltextureunit_t *unit = gl_state.units + unitnum;
1906 if (unitnum >= backendimageunits)
1908 // update 1d texture binding
1911 GL_ActiveTexture(unitnum);
1912 if (unitnum < backendunits)
1916 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1920 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1922 // update 2d texture binding
1923 if (unit->t2d != texnum)
1925 GL_ActiveTexture(unitnum);
1926 if (unitnum < backendunits)
1932 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
1939 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1944 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1946 // update 3d texture binding
1949 GL_ActiveTexture(unitnum);
1950 if (unitnum < backendunits)
1954 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1958 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1960 // update cubemap texture binding
1961 if (unit->tcubemap != 0)
1963 GL_ActiveTexture(unitnum);
1964 if (unitnum < backendunits)
1968 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1972 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1974 // update rectangle texture binding
1975 if (unit->trectangle != 0)
1977 GL_ActiveTexture(unitnum);
1978 if (unitnum < backendunits)
1980 if (unit->trectangle)
1982 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
1985 unit->trectangle = 0;
1986 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
1990 void R_Mesh_TexBind3D(unsigned int unitnum, int texnum)
1992 gltextureunit_t *unit = gl_state.units + unitnum;
1993 if (unitnum >= backendimageunits)
1995 // update 1d texture binding
1998 GL_ActiveTexture(unitnum);
1999 if (unitnum < backendunits)
2003 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
2007 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
2009 // update 2d texture binding
2012 GL_ActiveTexture(unitnum);
2013 if (unitnum < backendunits)
2017 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
2021 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
2023 // update 3d texture binding
2024 if (unit->t3d != texnum)
2026 GL_ActiveTexture(unitnum);
2027 if (unitnum < backendunits)
2033 qglEnable(GL_TEXTURE_3D);CHECKGLERROR
2040 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
2045 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
2047 // update cubemap texture binding
2048 if (unit->tcubemap != 0)
2050 GL_ActiveTexture(unitnum);
2051 if (unitnum < backendunits)
2055 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
2059 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
2061 // update rectangle texture binding
2062 if (unit->trectangle != 0)
2064 GL_ActiveTexture(unitnum);
2065 if (unitnum < backendunits)
2067 if (unit->trectangle)
2069 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
2072 unit->trectangle = 0;
2073 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
2077 void R_Mesh_TexBindCubeMap(unsigned int unitnum, int texnum)
2079 gltextureunit_t *unit = gl_state.units + unitnum;
2080 if (unitnum >= backendimageunits)
2082 // update 1d texture binding
2085 GL_ActiveTexture(unitnum);
2086 if (unitnum < backendunits)
2090 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
2094 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
2096 // update 2d texture binding
2099 GL_ActiveTexture(unitnum);
2100 if (unitnum < backendunits)
2104 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
2108 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
2110 // update 3d texture binding
2113 GL_ActiveTexture(unitnum);
2114 if (unitnum < backendunits)
2118 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
2122 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
2124 // update cubemap texture binding
2125 if (unit->tcubemap != texnum)
2127 GL_ActiveTexture(unitnum);
2128 if (unitnum < backendunits)
2132 if (unit->tcubemap == 0)
2134 qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
2141 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
2145 unit->tcubemap = texnum;
2146 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
2148 // update rectangle texture binding
2149 if (unit->trectangle != 0)
2151 GL_ActiveTexture(unitnum);
2152 if (unitnum < backendunits)
2154 if (unit->trectangle)
2156 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
2159 unit->trectangle = 0;
2160 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
2164 void R_Mesh_TexBindRectangle(unsigned int unitnum, int texnum)
2166 gltextureunit_t *unit = gl_state.units + unitnum;
2167 if (unitnum >= backendimageunits)
2169 // update 1d texture binding
2172 GL_ActiveTexture(unitnum);
2173 if (unitnum < backendunits)
2177 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
2181 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
2183 // update 2d texture binding
2186 GL_ActiveTexture(unitnum);
2187 if (unitnum < backendunits)
2191 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
2195 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
2197 // update 3d texture binding
2200 GL_ActiveTexture(unitnum);
2201 if (unitnum < backendunits)
2205 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
2209 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
2211 // update cubemap texture binding
2212 if (unit->tcubemap != 0)
2214 GL_ActiveTexture(unitnum);
2215 if (unitnum < backendunits)
2219 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
2223 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
2225 // update rectangle texture binding
2226 if (unit->trectangle != texnum)
2228 GL_ActiveTexture(unitnum);
2229 if (unitnum < backendunits)
2233 if (unit->trectangle == 0)
2235 qglEnable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
2240 if (unit->trectangle)
2242 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
2246 unit->trectangle = texnum;
2247 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
2251 static const double gl_identitymatrix[16] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
2253 void R_Mesh_TexMatrix(unsigned int unitnum, const matrix4x4_t *matrix)
2255 gltextureunit_t *unit = gl_state.units + unitnum;
2256 if (matrix->m[3][3])
2258 // texmatrix specified, check if it is different
2259 if (!unit->texmatrixenabled || memcmp(&unit->matrix, matrix, sizeof(matrix4x4_t)))
2261 double glmatrix[16];
2262 unit->texmatrixenabled = true;
2263 unit->matrix = *matrix;
2265 Matrix4x4_ToArrayDoubleGL(&unit->matrix, glmatrix);
2266 GL_ActiveTexture(unitnum);
2267 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
2268 qglLoadMatrixd(glmatrix);CHECKGLERROR
2269 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
2274 // no texmatrix specified, revert to identity
2275 if (unit->texmatrixenabled)
2277 unit->texmatrixenabled = false;
2278 unit->matrix = identitymatrix;
2280 GL_ActiveTexture(unitnum);
2281 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
2282 qglLoadIdentity();CHECKGLERROR
2283 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
2288 void R_Mesh_TexCombine(unsigned int unitnum, int combinergb, int combinealpha, int rgbscale, int alphascale)
2290 gltextureunit_t *unit = gl_state.units + unitnum;
2292 if (gl_combine.integer)
2294 // GL_ARB_texture_env_combine
2296 combinergb = GL_MODULATE;
2298 combinealpha = GL_MODULATE;
2303 if (unit->combinergb != combinergb)
2305 unit->combinergb = combinergb;
2306 GL_ActiveTexture(unitnum);
2307 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
2309 if (unit->combinealpha != combinealpha)
2311 unit->combinealpha = combinealpha;
2312 GL_ActiveTexture(unitnum);
2313 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
2315 if (unit->rgbscale != rgbscale)
2317 GL_ActiveTexture(unitnum);
2318 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (unit->rgbscale = rgbscale));CHECKGLERROR
2320 if (unit->alphascale != alphascale)
2322 GL_ActiveTexture(unitnum);
2323 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, (unit->alphascale = alphascale));CHECKGLERROR
2330 combinergb = GL_MODULATE;
2331 if (unit->combinergb != combinergb)
2333 unit->combinergb = combinergb;
2334 GL_ActiveTexture(unitnum);
2335 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combinergb);CHECKGLERROR
2340 void R_Mesh_TextureState(const rmeshstate_t *m)
2347 if (gl_backend_rebindtextures)
2349 gl_backend_rebindtextures = false;
2350 GL_SetupTextureState();
2354 for (i = 0;i < backendimageunits;i++)
2355 R_Mesh_TexBindAll(i, m->tex1d[i], m->tex[i], m->tex3d[i], m->texcubemap[i], m->texrectangle[i]);
2356 for (i = 0;i < backendarrayunits;i++)
2358 if (m->pointer_texcoord3f[i])
2359 R_Mesh_TexCoordPointer(i, 3, m->pointer_texcoord3f[i], m->pointer_texcoord_bufferobject[i], m->pointer_texcoord_bufferoffset[i]);
2361 R_Mesh_TexCoordPointer(i, 2, m->pointer_texcoord[i], m->pointer_texcoord_bufferobject[i], m->pointer_texcoord_bufferoffset[i]);
2363 for (i = 0;i < backendunits;i++)
2365 R_Mesh_TexMatrix(i, &m->texmatrix[i]);
2366 R_Mesh_TexCombine(i, m->texcombinergb[i], m->texcombinealpha[i], m->texrgbscale[i], m->texalphascale[i]);
2371 void R_Mesh_ResetTextureState(void)
2373 unsigned int unitnum;
2378 if (gl_backend_rebindtextures)
2380 gl_backend_rebindtextures = false;
2381 GL_SetupTextureState();
2385 for (unitnum = 0;unitnum < backendimageunits;unitnum++)
2387 gltextureunit_t *unit = gl_state.units + unitnum;
2388 // update 1d texture binding
2391 GL_ActiveTexture(unitnum);
2392 if (unitnum < backendunits)
2394 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
2397 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
2399 // update 2d texture binding
2402 GL_ActiveTexture(unitnum);
2403 if (unitnum < backendunits)
2405 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
2408 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
2410 // update 3d texture binding
2413 GL_ActiveTexture(unitnum);
2414 if (unitnum < backendunits)
2416 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
2419 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
2421 // update cubemap texture binding
2424 GL_ActiveTexture(unitnum);
2425 if (unitnum < backendunits)
2427 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
2430 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
2432 // update rectangle texture binding
2433 if (unit->trectangle)
2435 GL_ActiveTexture(unitnum);
2436 if (unitnum < backendunits)
2438 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
2440 unit->trectangle = 0;
2441 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
2444 for (unitnum = 0;unitnum < backendarrayunits;unitnum++)
2446 gltextureunit_t *unit = gl_state.units + unitnum;
2447 // texture array unit is disabled, disable the array
2448 if (unit->arrayenabled)
2450 unit->arrayenabled = false;
2451 GL_ClientActiveTexture(unitnum);
2452 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
2455 for (unitnum = 0;unitnum < backendunits;unitnum++)
2457 gltextureunit_t *unit = gl_state.units + unitnum;
2458 // no texmatrix specified, revert to identity
2459 if (unit->texmatrixenabled)
2461 unit->texmatrixenabled = false;
2462 unit->matrix = identitymatrix;
2464 GL_ActiveTexture(unitnum);
2465 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
2466 qglLoadIdentity();CHECKGLERROR
2467 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
2469 if (gl_combine.integer)
2471 // GL_ARB_texture_env_combine
2472 if (unit->combinergb != GL_MODULATE)
2474 unit->combinergb = GL_MODULATE;
2475 GL_ActiveTexture(unitnum);
2476 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
2478 if (unit->combinealpha != GL_MODULATE)
2480 unit->combinealpha = GL_MODULATE;
2481 GL_ActiveTexture(unitnum);
2482 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
2484 if (unit->rgbscale != 1)
2486 GL_ActiveTexture(unitnum);
2487 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (unit->rgbscale = 1));CHECKGLERROR
2489 if (unit->alphascale != 1)
2491 GL_ActiveTexture(unitnum);
2492 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, (unit->alphascale = 1));CHECKGLERROR
2498 if (unit->combinergb != GL_MODULATE)
2500 unit->combinergb = GL_MODULATE;
2501 GL_ActiveTexture(unitnum);
2502 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combinergb);CHECKGLERROR