3 #include "cl_collision.h"
5 cvar_t gl_mesh_drawrangeelements = {0, "gl_mesh_drawrangeelements", "1", "use glDrawRangeElements function if available instead of glDrawElements (for performance comparisons or bug testing)"};
6 cvar_t gl_mesh_testarrayelement = {0, "gl_mesh_testarrayelement", "0", "use glBegin(GL_TRIANGLES);glArrayElement();glEnd(); primitives instead of glDrawElements (useful to test for driver bugs with glDrawElements)"};
7 cvar_t gl_mesh_testmanualfeeding = {0, "gl_mesh_testmanualfeeding", "0", "use glBegin(GL_TRIANGLES);glTexCoord2f();glVertex3f();glEnd(); primitives instead of glDrawElements (useful to test for driver bugs with glDrawElements)"};
8 cvar_t gl_paranoid = {0, "gl_paranoid", "0", "enables OpenGL error checking and other tests"};
9 cvar_t gl_printcheckerror = {0, "gl_printcheckerror", "0", "prints all OpenGL error checks, useful to identify location of driver crashes"};
11 cvar_t r_render = {0, "r_render", "1", "enables rendering calls (you want this on!)"};
12 cvar_t r_waterwarp = {CVAR_SAVE, "r_waterwarp", "1", "warp view while underwater"};
13 cvar_t gl_polyblend = {CVAR_SAVE, "gl_polyblend", "1", "tints view while underwater, hurt, etc"};
14 cvar_t gl_dither = {CVAR_SAVE, "gl_dither", "1", "enables OpenGL dithering (16bit looks bad with this off)"};
15 cvar_t gl_lockarrays = {0, "gl_lockarrays", "0", "enables use of glLockArraysEXT, may cause glitches with some broken drivers, and may be slower than normal"};
16 cvar_t gl_lockarrays_minimumvertices = {0, "gl_lockarrays_minimumvertices", "1", "minimum number of vertices required for use of glLockArraysEXT, setting this too low may reduce performance"};
18 int gl_maxdrawrangeelementsvertices;
19 int gl_maxdrawrangeelementsindices;
24 void GL_PrintError(int errornumber, char *filename, int linenumber)
28 #ifdef GL_INVALID_ENUM
30 Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
33 #ifdef GL_INVALID_VALUE
34 case GL_INVALID_VALUE:
35 Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
38 #ifdef GL_INVALID_OPERATION
39 case GL_INVALID_OPERATION:
40 Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
43 #ifdef GL_STACK_OVERFLOW
44 case GL_STACK_OVERFLOW:
45 Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
48 #ifdef GL_STACK_UNDERFLOW
49 case GL_STACK_UNDERFLOW:
50 Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
53 #ifdef GL_OUT_OF_MEMORY
54 case GL_OUT_OF_MEMORY:
55 Con_Printf("GL_OUT_OF_MEMORY at %s:%i\n", filename, linenumber);
58 #ifdef GL_TABLE_TOO_LARGE
59 case GL_TABLE_TOO_LARGE:
60 Con_Printf("GL_TABLE_TOO_LARGE at %s:%i\n", filename, linenumber);
64 Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
70 #define BACKENDACTIVECHECK if (!backendactive) Sys_Error("GL backend function called when backend is not active");
72 void SCR_ScreenShot_f (void);
74 static matrix4x4_t backend_viewmatrix;
75 static matrix4x4_t backend_modelmatrix;
76 static matrix4x4_t backend_modelviewmatrix;
77 static matrix4x4_t backend_projectmatrix;
79 static unsigned int backendunits, backendimageunits, backendarrayunits, backendactive;
82 note: here's strip order for a terrain row:
89 A0B, 01B, B1C, 12C, C2D, 23D, D3E, 34E
91 *elements++ = i + row;
93 *elements++ = i + row + 1;
96 *elements++ = i + row + 1;
99 for (y = 0;y < rows - 1;y++)
101 for (x = 0;x < columns - 1;x++)
104 *elements++ = i + columns;
106 *elements++ = i + columns + 1;
109 *elements++ = i + columns + 1;
120 for (y = 0;y < rows - 1;y++)
122 for (x = 0;x < columns - 1;x++)
126 *elements++ = i + columns;
127 *elements++ = i + columns + 1;
128 *elements++ = i + columns;
129 *elements++ = i + columns + 1;
135 int polygonelements[(POLYGONELEMENTS_MAXPOINTS-2)*3];
136 int quadelements[QUADELEMENTS_MAXQUADS*6];
138 void GL_Backend_AllocArrays(void)
142 void GL_Backend_FreeArrays(void)
146 static void gl_backend_start(void)
148 Con_Print("OpenGL Backend starting...\n");
151 if (qglDrawRangeElements != NULL)
154 qglGetIntegerv(GL_MAX_ELEMENTS_VERTICES, &gl_maxdrawrangeelementsvertices);
156 qglGetIntegerv(GL_MAX_ELEMENTS_INDICES, &gl_maxdrawrangeelementsindices);
158 Con_Printf("glDrawRangeElements detected (max vertices %i, max indices %i)\n", gl_maxdrawrangeelementsvertices, gl_maxdrawrangeelementsindices);
161 backendunits = bound(1, gl_textureunits, MAX_TEXTUREUNITS);
162 backendimageunits = backendunits;
163 backendarrayunits = backendunits;
164 if (gl_support_fragment_shader)
167 qglGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS_ARB, (int *)&backendimageunits);
169 qglGetIntegerv(GL_MAX_TEXTURE_COORDS_ARB, (int *)&backendarrayunits);
171 Con_Printf("GLSL shader support detected: texture units = %i texenv, %i image, %i array\n", backendunits, backendimageunits, backendarrayunits);
172 backendimageunits = bound(1, backendimageunits, MAX_TEXTUREUNITS);
173 backendarrayunits = bound(1, backendarrayunits, MAX_TEXTUREUNITS);
175 else if (backendunits > 1)
176 Con_Printf("multitexture detected: texture units = %i\n", backendunits);
178 Con_Printf("singletexture\n");
180 GL_Backend_AllocArrays();
182 Con_Printf("OpenGL backend started.\n");
186 backendactive = true;
189 static void gl_backend_shutdown(void)
192 backendimageunits = 0;
193 backendarrayunits = 0;
194 backendactive = false;
196 Con_Print("OpenGL Backend shutting down\n");
198 GL_Backend_FreeArrays();
201 static void gl_backend_newmap(void)
205 void gl_backend_init(void)
209 for (i = 0;i < POLYGONELEMENTS_MAXPOINTS - 2;i++)
211 polygonelements[i * 3 + 0] = 0;
212 polygonelements[i * 3 + 1] = i + 1;
213 polygonelements[i * 3 + 2] = i + 2;
215 // elements for rendering a series of quads as triangles
216 for (i = 0;i < QUADELEMENTS_MAXQUADS;i++)
218 quadelements[i * 6 + 0] = i * 4;
219 quadelements[i * 6 + 1] = i * 4 + 1;
220 quadelements[i * 6 + 2] = i * 4 + 2;
221 quadelements[i * 6 + 3] = i * 4;
222 quadelements[i * 6 + 4] = i * 4 + 2;
223 quadelements[i * 6 + 5] = i * 4 + 3;
226 Cvar_RegisterVariable(&r_render);
227 Cvar_RegisterVariable(&r_waterwarp);
228 Cvar_RegisterVariable(&gl_polyblend);
229 Cvar_RegisterVariable(&gl_dither);
230 Cvar_RegisterVariable(&gl_lockarrays);
231 Cvar_RegisterVariable(&gl_lockarrays_minimumvertices);
232 Cvar_RegisterVariable(&gl_paranoid);
233 Cvar_RegisterVariable(&gl_printcheckerror);
235 Cvar_SetValue("r_render", 0);
238 Cvar_RegisterVariable(&gl_mesh_drawrangeelements);
239 Cvar_RegisterVariable(&gl_mesh_testarrayelement);
240 Cvar_RegisterVariable(&gl_mesh_testmanualfeeding);
242 R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap);
245 void GL_SetupView_Orientation_Identity (void)
247 backend_viewmatrix = identitymatrix;
248 memset(&backend_modelmatrix, 0, sizeof(backend_modelmatrix));
251 void GL_SetupView_Orientation_FromEntity(const matrix4x4_t *matrix)
253 matrix4x4_t tempmatrix, basematrix;
254 Matrix4x4_Invert_Simple(&tempmatrix, matrix);
255 Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
256 Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
257 Matrix4x4_Concat(&backend_viewmatrix, &basematrix, &tempmatrix);
258 //Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[2], 1, 0, 0);
259 //Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[0], 0, 1, 0);
260 //Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[1], 0, 0, 1);
261 //Matrix4x4_ConcatTranslate(&backend_viewmatrix, -origin[0], -origin[1], -origin[2]);
262 memset(&backend_modelmatrix, 0, sizeof(backend_modelmatrix));
265 void GL_SetupView_Mode_Perspective (double frustumx, double frustumy, double zNear, double zFar)
271 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
272 qglLoadIdentity();CHECKGLERROR
274 qglFrustum(-frustumx * zNear, frustumx * zNear, -frustumy * zNear, frustumy * zNear, zNear, zFar);CHECKGLERROR
275 qglGetDoublev(GL_PROJECTION_MATRIX, m);CHECKGLERROR
276 Matrix4x4_FromArrayDoubleGL(&backend_projectmatrix, m);
277 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
278 GL_SetupView_Orientation_Identity();
282 void GL_SetupView_Mode_PerspectiveInfiniteFarClip (double frustumx, double frustumy, double zNear)
288 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
289 qglLoadIdentity();CHECKGLERROR
291 nudge = 1.0 - 1.0 / (1<<23);
292 m[ 0] = 1.0 / frustumx;
297 m[ 5] = 1.0 / frustumy;
306 m[14] = -2 * zNear * nudge;
308 qglLoadMatrixd(m);CHECKGLERROR
309 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
310 GL_SetupView_Orientation_Identity();
312 Matrix4x4_FromArrayDoubleGL(&backend_projectmatrix, m);
315 void GL_SetupView_Mode_Ortho (double x1, double y1, double x2, double y2, double zNear, double zFar)
321 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
322 qglLoadIdentity();CHECKGLERROR
323 qglOrtho(x1, x2, y2, y1, zNear, zFar);CHECKGLERROR
324 qglGetDoublev(GL_PROJECTION_MATRIX, m);CHECKGLERROR
325 Matrix4x4_FromArrayDoubleGL(&backend_projectmatrix, m);
326 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
327 GL_SetupView_Orientation_Identity();
331 typedef struct gltextureunit_s
333 const void *pointer_texcoord;
334 size_t pointer_texcoord_offset;
335 int pointer_texcoord_buffer;
336 int t1d, t2d, t3d, tcubemap;
338 unsigned int arraycomponents;
339 int rgbscale, alphascale;
340 int combinergb, combinealpha;
341 // FIXME: add more combine stuff
342 // texmatrixenabled exists only to avoid unnecessary texmatrix compares
343 int texmatrixenabled;
348 static struct gl_state_s
356 int colormask; // stored as bottom 4 bits: r g b a (3 2 1 0 order)
361 unsigned int clientunit;
362 gltextureunit_t units[MAX_TEXTUREUNITS];
366 int vertexbufferobject;
367 int elementbufferobject;
368 qboolean pointer_color_enabled;
369 const void *pointer_vertex;
370 const void *pointer_color;
371 size_t pointer_vertex_offset;
372 size_t pointer_color_offset;
373 int pointer_vertex_buffer;
374 int pointer_color_buffer;
378 static void GL_BindVBO(int bufferobject)
380 if (gl_state.vertexbufferobject != bufferobject)
382 gl_state.vertexbufferobject = bufferobject;
384 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, bufferobject);
389 static void GL_BindEBO(int bufferobject)
391 if (gl_state.elementbufferobject != bufferobject)
393 gl_state.elementbufferobject = bufferobject;
395 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, bufferobject);
400 void GL_SetupTextureState(void)
403 gltextureunit_t *unit;
405 gl_state.unit = MAX_TEXTUREUNITS;
406 gl_state.clientunit = MAX_TEXTUREUNITS;
407 for (i = 0;i < MAX_TEXTUREUNITS;i++)
409 unit = gl_state.units + i;
414 unit->arrayenabled = false;
415 unit->arraycomponents = 0;
416 unit->pointer_texcoord = NULL;
417 unit->pointer_texcoord_buffer = 0;
418 unit->pointer_texcoord_offset = 0;
420 unit->alphascale = 1;
421 unit->combinergb = GL_MODULATE;
422 unit->combinealpha = GL_MODULATE;
423 unit->texmatrixenabled = false;
424 unit->matrix = identitymatrix;
427 for (i = 0;i < backendimageunits;i++)
430 qglBindTexture(GL_TEXTURE_1D, 0);CHECKGLERROR
431 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
434 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
436 if (gl_texturecubemap)
438 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
442 for (i = 0;i < backendarrayunits;i++)
444 GL_ClientActiveTexture(i);
446 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
447 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
450 for (i = 0;i < backendunits;i++)
453 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
454 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
457 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
459 if (gl_texturecubemap)
461 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
463 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
464 qglLoadIdentity();CHECKGLERROR
465 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
466 if (gl_combine.integer)
468 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);CHECKGLERROR
469 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);CHECKGLERROR
470 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);CHECKGLERROR
471 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
472 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_TEXTURE);CHECKGLERROR // for GL_INTERPOLATE_ARB mode
473 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
474 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
475 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);CHECKGLERROR
476 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);CHECKGLERROR
477 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);CHECKGLERROR
478 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
479 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, GL_CONSTANT_ARB);CHECKGLERROR
480 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
481 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
482 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
483 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
484 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
488 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
495 void GL_Backend_ResetState(void)
497 memset(&gl_state, 0, sizeof(gl_state));
498 gl_state.depthtest = true;
499 gl_state.alphatest = false;
500 gl_state.blendfunc1 = GL_ONE;
501 gl_state.blendfunc2 = GL_ZERO;
502 gl_state.blend = false;
503 gl_state.depthmask = GL_TRUE;
504 gl_state.colormask = 15;
505 gl_state.color4f[0] = gl_state.color4f[1] = gl_state.color4f[2] = gl_state.color4f[3] = 1;
506 gl_state.lockrange_first = 0;
507 gl_state.lockrange_count = 0;
508 gl_state.cullface = GL_FRONT; // quake is backwards, this culls back faces
509 gl_state.cullfaceenable = true;
513 qglColorMask(1, 1, 1, 1);
514 qglAlphaFunc(GL_GEQUAL, 0.5);CHECKGLERROR
515 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
516 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
517 qglDisable(GL_BLEND);CHECKGLERROR
518 qglCullFace(gl_state.cullface);CHECKGLERROR
519 qglEnable(GL_CULL_FACE);CHECKGLERROR
520 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
521 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
522 qglDepthMask(gl_state.depthmask);CHECKGLERROR
524 if (gl_support_arb_vertex_buffer_object)
526 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
527 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
530 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), NULL);CHECKGLERROR
531 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
533 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL);CHECKGLERROR
534 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
536 GL_Color(0, 0, 0, 0);
537 GL_Color(1, 1, 1, 1);
539 GL_SetupTextureState();
542 void GL_ActiveTexture(unsigned int num)
544 if (gl_state.unit != num)
547 if (qglActiveTexture)
550 qglActiveTexture(GL_TEXTURE0_ARB + gl_state.unit);
556 void GL_ClientActiveTexture(unsigned int num)
558 if (gl_state.clientunit != num)
560 gl_state.clientunit = num;
561 if (qglActiveTexture)
564 qglClientActiveTexture(GL_TEXTURE0_ARB + gl_state.clientunit);
570 void GL_BlendFunc(int blendfunc1, int blendfunc2)
572 if (gl_state.blendfunc1 != blendfunc1 || gl_state.blendfunc2 != blendfunc2)
575 qglBlendFunc(gl_state.blendfunc1 = blendfunc1, gl_state.blendfunc2 = blendfunc2);CHECKGLERROR
576 if (gl_state.blendfunc2 == GL_ZERO)
578 if (gl_state.blendfunc1 == GL_ONE)
583 qglDisable(GL_BLEND);CHECKGLERROR
591 qglEnable(GL_BLEND);CHECKGLERROR
600 qglEnable(GL_BLEND);CHECKGLERROR
606 void GL_DepthMask(int state)
608 if (gl_state.depthmask != state)
611 qglDepthMask(gl_state.depthmask = state);CHECKGLERROR
615 void GL_DepthTest(int state)
617 if (gl_state.depthtest != state)
619 gl_state.depthtest = state;
621 if (gl_state.depthtest)
623 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
627 qglDisable(GL_DEPTH_TEST);CHECKGLERROR
632 void GL_CullFace(int state)
635 if (state != GL_NONE)
637 if (!gl_state.cullfaceenable)
639 gl_state.cullfaceenable = true;
640 qglEnable(GL_CULL_FACE);CHECKGLERROR
642 if (gl_state.cullface != state)
644 gl_state.cullface = state;
645 qglCullFace(gl_state.cullface);CHECKGLERROR
650 if (gl_state.cullfaceenable)
652 gl_state.cullfaceenable = false;
653 qglDisable(GL_CULL_FACE);CHECKGLERROR
658 void GL_AlphaTest(int state)
660 if (gl_state.alphatest != state)
662 gl_state.alphatest = state;
664 if (gl_state.alphatest)
666 qglEnable(GL_ALPHA_TEST);CHECKGLERROR
670 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
675 void GL_ColorMask(int r, int g, int b, int a)
677 int state = r*8 + g*4 + b*2 + a*1;
678 if (gl_state.colormask != state)
680 gl_state.colormask = state;
682 qglColorMask((GLboolean)r, (GLboolean)g, (GLboolean)b, (GLboolean)a);CHECKGLERROR
686 void GL_Color(float cr, float cg, float cb, float ca)
688 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)
690 gl_state.color4f[0] = cr;
691 gl_state.color4f[1] = cg;
692 gl_state.color4f[2] = cb;
693 gl_state.color4f[3] = ca;
695 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);
700 void GL_LockArrays(int first, int count)
702 if (count < gl_lockarrays_minimumvertices.integer)
707 if (gl_state.lockrange_count != count || gl_state.lockrange_first != first)
709 if (gl_state.lockrange_count)
711 gl_state.lockrange_count = 0;
713 qglUnlockArraysEXT();
716 if (count && gl_supportslockarrays && gl_lockarrays.integer && r_render.integer)
718 gl_state.lockrange_first = first;
719 gl_state.lockrange_count = count;
721 qglLockArraysEXT(first, count);
727 void GL_Scissor (int x, int y, int width, int height)
730 qglScissor(x, vid.height - (y + height),width,height);
734 void GL_ScissorTest(int state)
736 if(gl_state.scissortest == state)
740 if((gl_state.scissortest = state))
741 qglEnable(GL_SCISSOR_TEST);
743 qglDisable(GL_SCISSOR_TEST);
747 void GL_Clear(int mask)
750 qglClear(mask);CHECKGLERROR
753 void GL_TransformToScreen(const vec4_t in, vec4_t out)
757 Matrix4x4_Transform4 (&backend_viewmatrix, in, temp);
758 Matrix4x4_Transform4 (&backend_projectmatrix, temp, out);
760 out[0] = r_view.x + (out[0] * iw + 1.0f) * r_view.width * 0.5f;
761 out[1] = r_view.y + r_view.height - (out[1] * iw + 1.0f) * r_view.height * 0.5f;
762 out[2] = r_view.z + (out[2] * iw + 1.0f) * r_view.depth * 0.5f;
765 // called at beginning of frame
766 void R_Mesh_Start(void)
770 if (gl_printcheckerror.integer && !gl_paranoid.integer)
772 Con_Printf("WARNING: gl_printcheckerror is on but gl_paranoid is off, turning it on...\n");
773 Cvar_SetValueQuick(&gl_paranoid, 1);
775 GL_Backend_ResetState();
778 qboolean GL_Backend_CompileShader(int programobject, GLenum shadertypeenum, const char *shadertype, int numstrings, const char **strings)
782 char compilelog[MAX_INPUTLINE];
783 shaderobject = qglCreateShaderObjectARB(shadertypeenum);CHECKGLERROR
786 qglShaderSourceARB(shaderobject, numstrings, strings, NULL);CHECKGLERROR
787 qglCompileShaderARB(shaderobject);CHECKGLERROR
788 qglGetObjectParameterivARB(shaderobject, GL_OBJECT_COMPILE_STATUS_ARB, &shadercompiled);CHECKGLERROR
789 qglGetInfoLogARB(shaderobject, sizeof(compilelog), NULL, compilelog);CHECKGLERROR
791 Con_DPrintf("%s shader compile log:\n%s\n", shadertype, compilelog);
794 qglDeleteObjectARB(shaderobject);CHECKGLERROR
797 qglAttachObjectARB(programobject, shaderobject);CHECKGLERROR
798 qglDeleteObjectARB(shaderobject);CHECKGLERROR
802 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)
805 GLuint programobject = 0;
806 char linklog[MAX_INPUTLINE];
809 programobject = qglCreateProgramObjectARB();CHECKGLERROR
813 if (vertexstrings_count && !GL_Backend_CompileShader(programobject, GL_VERTEX_SHADER_ARB, "vertex", vertexstrings_count, vertexstrings_list))
816 #ifdef GL_GEOMETRY_SHADER_ARB
817 if (geometrystrings_count && !GL_Backend_CompileShader(programobject, GL_GEOMETRY_SHADER_ARB, "geometry", geometrystrings_count, geometrystrings_list))
821 if (fragmentstrings_count && !GL_Backend_CompileShader(programobject, GL_FRAGMENT_SHADER_ARB, "fragment", fragmentstrings_count, fragmentstrings_list))
824 qglLinkProgramARB(programobject);CHECKGLERROR
825 qglGetObjectParameterivARB(programobject, GL_OBJECT_LINK_STATUS_ARB, &programlinked);CHECKGLERROR
826 qglGetInfoLogARB(programobject, sizeof(linklog), NULL, linklog);CHECKGLERROR
829 Con_DPrintf("program link log:\n%s\n", linklog);
830 // software vertex shader is ok but software fragment shader is WAY
831 // too slow, fail program if so.
832 // NOTE: this string might be ATI specific, but that's ok because the
833 // ATI R300 chip (Radeon 9500-9800/X300) is the most likely to use a
834 // software fragment shader due to low instruction and dependent
836 if (strstr(linklog, "fragment shader will run in software"))
837 programlinked = false;
841 return programobject;
843 qglDeleteObjectARB(programobject);CHECKGLERROR
847 void GL_Backend_FreeProgram(unsigned int prog)
850 qglDeleteObjectARB(prog);
854 int gl_backend_rebindtextures;
856 void GL_Backend_RenumberElements(int *out, int count, const int *in, int offset)
861 for (i = 0;i < count;i++)
862 *out++ = *in++ + offset;
865 memcpy(out, in, sizeof(*out) * count);
868 // renders triangles using vertices from the active arrays
869 int paranoidblah = 0;
870 void R_Mesh_Draw(int firstvertex, int numvertices, int numtriangles, const int *elements, int bufferobject, size_t bufferoffset)
872 unsigned int numelements = numtriangles * 3;
873 if (numvertices < 3 || numtriangles < 1)
875 Con_Printf("R_Mesh_Draw(%d, %d, %d, %8p);\n", firstvertex, numvertices, numtriangles, elements);
879 r_refdef.stats.meshes++;
880 r_refdef.stats.meshes_elements += numelements;
881 if (gl_paranoid.integer)
883 unsigned int i, j, size;
885 // note: there's no validation done here on buffer objects because it
886 // is somewhat difficult to get at the data, and gl_paranoid can be
887 // used without buffer objects if the need arises
888 // (the data could be gotten using glMapBuffer but it would be very
889 // slow due to uncachable video memory reads)
890 if (!qglIsEnabled(GL_VERTEX_ARRAY))
891 Con_Print("R_Mesh_Draw: vertex array not enabled\n");
893 if (gl_state.pointer_vertex)
894 for (j = 0, size = numvertices * 3, p = (int *)((float *)gl_state.pointer_vertex + firstvertex * 3);j < size;j++, p++)
896 if (gl_state.pointer_color_enabled)
898 if (!qglIsEnabled(GL_COLOR_ARRAY))
899 Con_Print("R_Mesh_Draw: color array set but not enabled\n");
901 if (gl_state.pointer_color && gl_state.pointer_color_enabled)
902 for (j = 0, size = numvertices * 4, p = (int *)((float *)gl_state.pointer_color + firstvertex * 4);j < size;j++, p++)
905 for (i = 0;i < backendarrayunits;i++)
907 if (gl_state.units[i].arrayenabled)
909 GL_ClientActiveTexture(i);
910 if (!qglIsEnabled(GL_TEXTURE_COORD_ARRAY))
911 Con_Print("R_Mesh_Draw: texcoord array set but not enabled\n");
913 if (gl_state.units[i].pointer_texcoord && gl_state.units[i].arrayenabled)
914 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++)
918 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
920 if (elements[i] < firstvertex || elements[i] >= firstvertex + numvertices)
922 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in elements list\n", elements[i], firstvertex, firstvertex + numvertices);
928 if (r_render.integer)
931 if (gl_mesh_testmanualfeeding.integer)
935 qglBegin(GL_TRIANGLES);
936 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
938 for (j = 0;j < backendarrayunits;j++)
940 if (gl_state.units[j].pointer_texcoord && gl_state.units[j].arrayenabled)
942 if (backendarrayunits > 1)
944 if (gl_state.units[j].arraycomponents == 4)
946 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 4;
947 qglMultiTexCoord4f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2], p[3]);
949 else if (gl_state.units[j].arraycomponents == 3)
951 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 3;
952 qglMultiTexCoord3f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2]);
954 else if (gl_state.units[j].arraycomponents == 2)
956 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 2;
957 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, p[0], p[1]);
961 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 1;
962 qglMultiTexCoord1f(GL_TEXTURE0_ARB + j, p[0]);
967 if (gl_state.units[j].arraycomponents == 4)
969 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 4;
970 qglTexCoord4f(p[0], p[1], p[2], p[3]);
972 else if (gl_state.units[j].arraycomponents == 3)
974 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 3;
975 qglTexCoord3f(p[0], p[1], p[2]);
977 else if (gl_state.units[j].arraycomponents == 2)
979 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 2;
980 qglTexCoord2f(p[0], p[1]);
984 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + elements[i] * 1;
990 if (gl_state.pointer_color && gl_state.pointer_color_enabled)
992 p = ((const GLfloat *)(gl_state.pointer_color)) + elements[i] * 4;
993 qglColor4f(p[0], p[1], p[2], p[3]);
995 p = ((const GLfloat *)(gl_state.pointer_vertex)) + elements[i] * 3;
996 qglVertex3f(p[0], p[1], p[2]);
1001 else if (gl_mesh_testarrayelement.integer)
1004 qglBegin(GL_TRIANGLES);
1005 for (i = 0;i < numtriangles * 3;i++)
1007 qglArrayElement(elements[i]);
1012 else if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1014 GL_BindEBO(bufferobject);
1015 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices, numelements, GL_UNSIGNED_INT, bufferobject ? (void *)bufferoffset : elements);
1020 GL_BindEBO(bufferobject);
1021 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, bufferobject ? (void *)bufferoffset : elements);
1027 // restores backend state, used when done with 3D rendering
1028 void R_Mesh_Finish(void)
1033 GL_LockArrays(0, 0);
1036 for (i = 0;i < backendimageunits;i++)
1038 GL_ActiveTexture(i);
1039 qglBindTexture(GL_TEXTURE_1D, 0);CHECKGLERROR
1040 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
1043 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
1045 if (gl_texturecubemap)
1047 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
1050 for (i = 0;i < backendarrayunits;i++)
1052 GL_ActiveTexture(backendarrayunits - 1 - i);
1053 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1055 for (i = 0;i < backendunits;i++)
1057 GL_ActiveTexture(backendunits - 1 - i);
1058 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1059 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1062 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1064 if (gl_texturecubemap)
1066 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1068 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
1069 if (gl_combine.integer)
1071 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
1072 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
1075 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1076 qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
1078 qglDisable(GL_BLEND);CHECKGLERROR
1079 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1080 qglDepthMask(GL_TRUE);CHECKGLERROR
1081 qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);CHECKGLERROR
1084 int R_Mesh_CreateStaticEBO(void *data, size_t size)
1086 GLuint bufferobject;
1087 qglGenBuffersARB(1, &bufferobject);
1088 GL_BindEBO(bufferobject);
1089 qglBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, size, data, GL_STATIC_DRAW_ARB);
1090 return (int)bufferobject;
1093 void R_Mesh_DestroyEBO(int bufferobject)
1095 qglDeleteBuffersARB(1, (GLuint *)&bufferobject);
1098 int R_Mesh_CreateStaticVBO(void *data, size_t size)
1100 GLuint bufferobject;
1101 qglGenBuffersARB(1, &bufferobject);
1102 GL_BindVBO(bufferobject);
1103 qglBufferDataARB(GL_ARRAY_BUFFER_ARB, size, data, GL_STATIC_DRAW_ARB);
1104 return (int)bufferobject;
1107 void R_Mesh_DestroyVBO(int bufferobject)
1109 qglDeleteBuffersARB(1, (GLuint *)&bufferobject);
1112 void R_Mesh_Matrix(const matrix4x4_t *matrix)
1114 if (memcmp(matrix, &backend_modelmatrix, sizeof(matrix4x4_t)))
1116 double glmatrix[16];
1117 backend_modelmatrix = *matrix;
1118 Matrix4x4_Concat(&backend_modelviewmatrix, &backend_viewmatrix, matrix);
1119 Matrix4x4_ToArrayDoubleGL(&backend_modelviewmatrix, glmatrix);
1121 qglLoadMatrixd(glmatrix);CHECKGLERROR
1125 void R_Mesh_VertexPointer(const float *vertex3f, int bufferobject, size_t bufferoffset)
1127 if (gl_state.pointer_vertex != vertex3f || gl_state.pointer_vertex_buffer != bufferobject || gl_state.pointer_vertex_offset != bufferoffset)
1129 gl_state.pointer_vertex = vertex3f;
1130 gl_state.pointer_vertex_buffer = bufferobject;
1131 gl_state.pointer_vertex_offset = bufferoffset;
1133 GL_BindVBO(bufferobject);
1134 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), bufferobject ? (void *)bufferoffset : vertex3f);CHECKGLERROR
1138 void R_Mesh_ColorPointer(const float *color4f, int bufferobject, size_t bufferoffset)
1140 if (color4f || bufferobject)
1142 // caller wants color array enabled
1143 if (!gl_state.pointer_color_enabled)
1145 gl_state.pointer_color_enabled = true;
1147 qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1149 if (gl_state.pointer_color != color4f || gl_state.pointer_color_buffer != bufferobject || gl_state.pointer_color_offset != bufferoffset)
1151 gl_state.pointer_color = color4f;
1152 gl_state.pointer_color_buffer = bufferobject;
1153 gl_state.pointer_color_offset = bufferoffset;
1155 GL_BindVBO(bufferobject);
1156 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), bufferobject ? (void *)bufferoffset : color4f);CHECKGLERROR
1161 // caller wants color array disabled
1162 if (gl_state.pointer_color_enabled)
1164 gl_state.pointer_color_enabled = false;
1166 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1167 // when color array is on the glColor gets trashed, set it again
1168 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);CHECKGLERROR
1173 void R_Mesh_TexCoordPointer(unsigned int unitnum, unsigned int numcomponents, const float *texcoord, int bufferobject, size_t bufferoffset)
1175 gltextureunit_t *unit = gl_state.units + unitnum;
1176 // update array settings
1178 if (texcoord || bufferobject)
1180 // texture array unit is enabled, enable the array
1181 if (!unit->arrayenabled)
1183 unit->arrayenabled = true;
1184 GL_ClientActiveTexture(unitnum);
1185 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1188 if (unit->pointer_texcoord != texcoord || unit->pointer_texcoord_buffer != bufferobject || unit->pointer_texcoord_offset != bufferoffset || unit->arraycomponents != numcomponents)
1190 unit->pointer_texcoord = texcoord;
1191 unit->pointer_texcoord_buffer = bufferobject;
1192 unit->pointer_texcoord_offset = bufferoffset;
1193 unit->arraycomponents = numcomponents;
1194 GL_ClientActiveTexture(unitnum);
1195 GL_BindVBO(bufferobject);
1196 qglTexCoordPointer(unit->arraycomponents, GL_FLOAT, sizeof(float) * unit->arraycomponents, bufferobject ? (void *)bufferoffset : texcoord);CHECKGLERROR
1201 // texture array unit is disabled, disable the array
1202 if (unit->arrayenabled)
1204 unit->arrayenabled = false;
1205 GL_ClientActiveTexture(unitnum);
1206 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1211 void R_Mesh_TexBindAll(unsigned int unitnum, int tex1d, int tex2d, int tex3d, int texcubemap)
1213 gltextureunit_t *unit = gl_state.units + unitnum;
1214 if (unitnum >= backendimageunits)
1216 // update 1d texture binding
1217 if (unit->t1d != tex1d)
1219 GL_ActiveTexture(unitnum);
1220 if (unitnum < backendunits)
1226 qglEnable(GL_TEXTURE_1D);CHECKGLERROR
1233 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1238 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1240 // update 2d texture binding
1241 if (unit->t2d != tex2d)
1243 GL_ActiveTexture(unitnum);
1244 if (unitnum < backendunits)
1250 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
1257 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1262 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1264 // update 3d texture binding
1265 if (unit->t3d != tex3d)
1267 GL_ActiveTexture(unitnum);
1268 if (unitnum < backendunits)
1274 qglEnable(GL_TEXTURE_3D);CHECKGLERROR
1281 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1286 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1288 // update cubemap texture binding
1289 if (unit->tcubemap != texcubemap)
1291 GL_ActiveTexture(unitnum);
1292 if (unitnum < backendunits)
1296 if (unit->tcubemap == 0)
1298 qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1305 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1309 unit->tcubemap = texcubemap;
1310 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1314 void R_Mesh_TexBind1D(unsigned int unitnum, int texnum)
1316 gltextureunit_t *unit = gl_state.units + unitnum;
1317 if (unitnum >= backendimageunits)
1319 // update 1d texture binding
1320 if (unit->t1d != texnum)
1322 GL_ActiveTexture(unitnum);
1323 if (unitnum < backendunits)
1329 qglEnable(GL_TEXTURE_1D);CHECKGLERROR
1336 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1341 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1343 // update 2d texture binding
1346 GL_ActiveTexture(unitnum);
1347 if (unitnum < backendunits)
1351 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1355 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1357 // update 3d texture binding
1360 GL_ActiveTexture(unitnum);
1361 if (unitnum < backendunits)
1365 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1369 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1371 // update cubemap texture binding
1374 GL_ActiveTexture(unitnum);
1375 if (unitnum < backendunits)
1379 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1383 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1387 void R_Mesh_TexBind(unsigned int unitnum, int texnum)
1389 gltextureunit_t *unit = gl_state.units + unitnum;
1390 if (unitnum >= backendimageunits)
1392 // update 1d texture binding
1395 GL_ActiveTexture(unitnum);
1396 if (unitnum < backendunits)
1400 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1404 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1406 // update 2d texture binding
1407 if (unit->t2d != texnum)
1409 GL_ActiveTexture(unitnum);
1410 if (unitnum < backendunits)
1416 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
1423 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1428 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1430 // update 3d texture binding
1433 GL_ActiveTexture(unitnum);
1434 if (unitnum < backendunits)
1438 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1442 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1444 // update cubemap texture binding
1445 if (unit->tcubemap != 0)
1447 GL_ActiveTexture(unitnum);
1448 if (unitnum < backendunits)
1452 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1456 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1460 void R_Mesh_TexBind3D(unsigned int unitnum, int texnum)
1462 gltextureunit_t *unit = gl_state.units + unitnum;
1463 if (unitnum >= backendimageunits)
1465 // update 1d texture binding
1468 GL_ActiveTexture(unitnum);
1469 if (unitnum < backendunits)
1473 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1477 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1479 // update 2d texture binding
1482 GL_ActiveTexture(unitnum);
1483 if (unitnum < backendunits)
1487 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1491 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1493 // update 3d texture binding
1494 if (unit->t3d != texnum)
1496 GL_ActiveTexture(unitnum);
1497 if (unitnum < backendunits)
1503 qglEnable(GL_TEXTURE_3D);CHECKGLERROR
1510 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1515 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1517 // update cubemap texture binding
1518 if (unit->tcubemap != 0)
1520 GL_ActiveTexture(unitnum);
1521 if (unitnum < backendunits)
1525 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1529 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1533 void R_Mesh_TexBindCubeMap(unsigned int unitnum, int texnum)
1535 gltextureunit_t *unit = gl_state.units + unitnum;
1536 if (unitnum >= backendimageunits)
1538 // update 1d texture binding
1541 GL_ActiveTexture(unitnum);
1542 if (unitnum < backendunits)
1546 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1550 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1552 // update 2d texture binding
1555 GL_ActiveTexture(unitnum);
1556 if (unitnum < backendunits)
1560 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1564 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1566 // update 3d texture binding
1569 GL_ActiveTexture(unitnum);
1570 if (unitnum < backendunits)
1574 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1578 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1580 // update cubemap texture binding
1581 if (unit->tcubemap != texnum)
1583 GL_ActiveTexture(unitnum);
1584 if (unitnum < backendunits)
1588 if (unit->tcubemap == 0)
1590 qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1597 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1601 unit->tcubemap = texnum;
1602 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1606 void R_Mesh_TexMatrix(unsigned int unitnum, const matrix4x4_t *matrix)
1608 gltextureunit_t *unit = gl_state.units + unitnum;
1609 if (matrix->m[3][3])
1611 // texmatrix specified, check if it is different
1612 if (!unit->texmatrixenabled || memcmp(&unit->matrix, matrix, sizeof(matrix4x4_t)))
1614 double glmatrix[16];
1615 unit->texmatrixenabled = true;
1616 unit->matrix = *matrix;
1618 Matrix4x4_ToArrayDoubleGL(&unit->matrix, glmatrix);
1619 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1620 GL_ActiveTexture(unitnum);
1621 qglLoadMatrixd(glmatrix);CHECKGLERROR
1622 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1627 // no texmatrix specified, revert to identity
1628 if (unit->texmatrixenabled)
1630 unit->texmatrixenabled = false;
1632 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1633 GL_ActiveTexture(unitnum);
1634 qglLoadIdentity();CHECKGLERROR
1635 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1640 void R_Mesh_TexCombine(unsigned int unitnum, int combinergb, int combinealpha, int rgbscale, int alphascale)
1642 gltextureunit_t *unit = gl_state.units + unitnum;
1644 if (gl_combine.integer)
1646 // GL_ARB_texture_env_combine
1648 combinergb = GL_MODULATE;
1650 combinealpha = GL_MODULATE;
1655 if (unit->combinergb != combinergb)
1657 unit->combinergb = combinergb;
1658 GL_ActiveTexture(unitnum);
1659 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
1661 if (unit->combinealpha != combinealpha)
1663 unit->combinealpha = combinealpha;
1664 GL_ActiveTexture(unitnum);
1665 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
1667 if (unit->rgbscale != rgbscale)
1669 GL_ActiveTexture(unitnum);
1670 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (unit->rgbscale = rgbscale));CHECKGLERROR
1672 if (unit->alphascale != alphascale)
1674 GL_ActiveTexture(unitnum);
1675 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, (unit->alphascale = alphascale));CHECKGLERROR
1682 combinergb = GL_MODULATE;
1683 if (unit->combinergb != combinergb)
1685 unit->combinergb = combinergb;
1686 GL_ActiveTexture(unitnum);
1687 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combinergb);CHECKGLERROR
1692 void R_Mesh_TextureState(const rmeshstate_t *m)
1699 if (gl_backend_rebindtextures)
1701 gl_backend_rebindtextures = false;
1702 GL_SetupTextureState();
1706 for (i = 0;i < backendimageunits;i++)
1707 R_Mesh_TexBindAll(i, m->tex1d[i], m->tex[i], m->tex3d[i], m->texcubemap[i]);
1708 for (i = 0;i < backendarrayunits;i++)
1710 if (m->pointer_texcoord3f[i])
1711 R_Mesh_TexCoordPointer(i, 3, m->pointer_texcoord3f[i], 0, 0);
1713 R_Mesh_TexCoordPointer(i, 2, m->pointer_texcoord[i], 0, 0);
1715 for (i = 0;i < backendunits;i++)
1717 R_Mesh_TexMatrix(i, &m->texmatrix[i]);
1718 R_Mesh_TexCombine(i, m->texcombinergb[i], m->texcombinealpha[i], m->texrgbscale[i], m->texalphascale[i]);
1723 void R_Mesh_ResetTextureState(void)
1725 unsigned int unitnum;
1730 if (gl_backend_rebindtextures)
1732 gl_backend_rebindtextures = false;
1733 GL_SetupTextureState();
1737 for (unitnum = 0;unitnum < backendimageunits;unitnum++)
1739 gltextureunit_t *unit = gl_state.units + unitnum;
1740 // update 1d texture binding
1743 GL_ActiveTexture(unitnum);
1744 if (unitnum < backendunits)
1746 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1749 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1751 // update 2d texture binding
1754 GL_ActiveTexture(unitnum);
1755 if (unitnum < backendunits)
1757 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1760 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1762 // update 3d texture binding
1765 GL_ActiveTexture(unitnum);
1766 if (unitnum < backendunits)
1768 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1771 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1773 // update cubemap texture binding
1776 GL_ActiveTexture(unitnum);
1777 if (unitnum < backendunits)
1779 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1782 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1785 for (unitnum = 0;unitnum < backendarrayunits;unitnum++)
1787 gltextureunit_t *unit = gl_state.units + unitnum;
1788 // texture array unit is disabled, disable the array
1789 if (unit->arrayenabled)
1791 unit->arrayenabled = false;
1792 GL_ClientActiveTexture(unitnum);
1793 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1796 for (unitnum = 0;unitnum < backendunits;unitnum++)
1798 gltextureunit_t *unit = gl_state.units + unitnum;
1799 // no texmatrix specified, revert to identity
1800 if (unit->texmatrixenabled)
1802 unit->texmatrixenabled = false;
1804 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1805 GL_ActiveTexture(unitnum);
1806 qglLoadIdentity();CHECKGLERROR
1807 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1809 if (gl_combine.integer)
1811 // GL_ARB_texture_env_combine
1812 if (unit->combinergb != GL_MODULATE)
1814 unit->combinergb = GL_MODULATE;
1815 GL_ActiveTexture(unitnum);
1816 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
1818 if (unit->combinealpha != GL_MODULATE)
1820 unit->combinealpha = GL_MODULATE;
1821 GL_ActiveTexture(unitnum);
1822 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
1824 if (unit->rgbscale != 1)
1826 GL_ActiveTexture(unitnum);
1827 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (unit->rgbscale = 1));CHECKGLERROR
1829 if (unit->alphascale != 1)
1831 GL_ActiveTexture(unitnum);
1832 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, (unit->alphascale = 1));CHECKGLERROR
1838 if (unit->combinergb != GL_MODULATE)
1840 unit->combinergb = GL_MODULATE;
1841 GL_ActiveTexture(unitnum);
1842 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combinergb);CHECKGLERROR
1848 void R_Mesh_Draw_ShowTris(int firstvertex, int numvertices, int numtriangles, const int *elements)
1852 for (;numtriangles;numtriangles--, elements += 3)
1854 qglArrayElement(elements[0]);qglArrayElement(elements[1]);
1855 qglArrayElement(elements[1]);qglArrayElement(elements[2]);
1856 qglArrayElement(elements[2]);qglArrayElement(elements[0]);