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_workaround_mac_texmatrix = {0, "gl_workaround_mac_texmatrix", "0", "if set to 1 this always calls glClientActiveTexture when calling glActiveTexture, to work around mistargeted texture matrix updates on Mac OSX drivers"};
10 cvar_t gl_paranoid = {0, "gl_paranoid", "0", "enables OpenGL error checking and other tests"};
11 cvar_t gl_printcheckerror = {0, "gl_printcheckerror", "0", "prints all OpenGL error checks, useful to identify location of driver crashes"};
13 cvar_t r_render = {0, "r_render", "1", "enables rendering calls (you want this on!)"};
14 cvar_t r_waterwarp = {CVAR_SAVE, "r_waterwarp", "1", "warp view while underwater"};
15 cvar_t gl_polyblend = {CVAR_SAVE, "gl_polyblend", "1", "tints view while underwater, hurt, etc"};
16 cvar_t gl_dither = {CVAR_SAVE, "gl_dither", "1", "enables OpenGL dithering (16bit looks bad with this off)"};
17 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"};
18 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"};
19 cvar_t gl_vbo = {CVAR_SAVE, "gl_vbo", "1", "make use of GL_ARB_vertex_buffer_object extension to store static geometry in video memory for faster rendering"};
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);
70 Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
76 #define BACKENDACTIVECHECK if (!backendactive) Sys_Error("GL backend function called when backend is not active");
78 void SCR_ScreenShot_f (void);
80 static matrix4x4_t backend_viewmatrix;
81 static matrix4x4_t backend_modelmatrix;
82 static matrix4x4_t backend_modelviewmatrix;
83 static matrix4x4_t backend_projectmatrix;
85 static unsigned int backendunits, backendimageunits, backendarrayunits, backendactive;
88 note: here's strip order for a terrain row:
95 A0B, 01B, B1C, 12C, C2D, 23D, D3E, 34E
97 *elements++ = i + row;
99 *elements++ = i + row + 1;
102 *elements++ = i + row + 1;
105 for (y = 0;y < rows - 1;y++)
107 for (x = 0;x < columns - 1;x++)
110 *elements++ = i + columns;
112 *elements++ = i + columns + 1;
115 *elements++ = i + columns + 1;
126 for (y = 0;y < rows - 1;y++)
128 for (x = 0;x < columns - 1;x++)
132 *elements++ = i + columns;
133 *elements++ = i + columns + 1;
134 *elements++ = i + columns;
135 *elements++ = i + columns + 1;
141 unsigned short polygonelements[(POLYGONELEMENTS_MAXPOINTS-2)*3];
142 unsigned short quadelements[QUADELEMENTS_MAXQUADS*6];
144 void GL_Backend_AllocArrays(void)
148 void GL_Backend_FreeArrays(void)
152 void GL_VBOStats_f(void)
154 GL_Mesh_ListVBOs(true);
157 typedef struct gl_bufferobjectinfo_s
162 char name[MAX_QPATH];
164 gl_bufferobjectinfo_t;
166 memexpandablearray_t gl_bufferobjectinfoarray;
168 static void gl_backend_start(void)
172 if (qglDrawRangeElements != NULL)
175 qglGetIntegerv(GL_MAX_ELEMENTS_VERTICES, &gl_maxdrawrangeelementsvertices);
177 qglGetIntegerv(GL_MAX_ELEMENTS_INDICES, &gl_maxdrawrangeelementsindices);
179 Con_DPrintf("GL_MAX_ELEMENTS_VERTICES = %i\nGL_MAX_ELEMENTS_INDICES = %i\n", gl_maxdrawrangeelementsvertices, gl_maxdrawrangeelementsindices);
182 backendunits = bound(1, gl_textureunits, MAX_TEXTUREUNITS);
183 backendimageunits = backendunits;
184 backendarrayunits = backendunits;
185 if (gl_support_fragment_shader)
188 qglGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS_ARB, (int *)&backendimageunits);
190 qglGetIntegerv(GL_MAX_TEXTURE_COORDS_ARB, (int *)&backendarrayunits);
192 Con_DPrintf("GLSL shader support detected: texture units = %i texenv, %i image, %i array\n", backendunits, backendimageunits, backendarrayunits);
193 backendimageunits = bound(1, backendimageunits, MAX_TEXTUREUNITS);
194 backendarrayunits = bound(1, backendarrayunits, MAX_TEXTUREUNITS);
197 Con_DPrintf("GL_MAX_TEXTUREUNITS = %i\n", backendunits);
199 GL_Backend_AllocArrays();
201 Mem_ExpandableArray_NewArray(&gl_bufferobjectinfoarray, r_main_mempool, sizeof(gl_bufferobjectinfo_t), 128);
203 Con_DPrintf("OpenGL backend started.\n");
207 backendactive = true;
210 static void gl_backend_shutdown(void)
213 backendimageunits = 0;
214 backendarrayunits = 0;
215 backendactive = false;
217 Con_DPrint("OpenGL Backend shutting down\n");
219 Mem_ExpandableArray_FreeArray(&gl_bufferobjectinfoarray);
221 GL_Backend_FreeArrays();
224 static void gl_backend_newmap(void)
228 void gl_backend_init(void)
232 for (i = 0;i < POLYGONELEMENTS_MAXPOINTS - 2;i++)
234 polygonelements[i * 3 + 0] = 0;
235 polygonelements[i * 3 + 1] = i + 1;
236 polygonelements[i * 3 + 2] = i + 2;
238 // elements for rendering a series of quads as triangles
239 for (i = 0;i < QUADELEMENTS_MAXQUADS;i++)
241 quadelements[i * 6 + 0] = i * 4;
242 quadelements[i * 6 + 1] = i * 4 + 1;
243 quadelements[i * 6 + 2] = i * 4 + 2;
244 quadelements[i * 6 + 3] = i * 4;
245 quadelements[i * 6 + 4] = i * 4 + 2;
246 quadelements[i * 6 + 5] = i * 4 + 3;
249 Cvar_RegisterVariable(&r_render);
250 Cvar_RegisterVariable(&r_waterwarp);
251 Cvar_RegisterVariable(&gl_polyblend);
252 Cvar_RegisterVariable(&v_flipped);
253 Cvar_RegisterVariable(&gl_dither);
254 Cvar_RegisterVariable(&gl_lockarrays);
255 Cvar_RegisterVariable(&gl_lockarrays_minimumvertices);
256 Cvar_RegisterVariable(&gl_vbo);
257 Cvar_RegisterVariable(&gl_paranoid);
258 Cvar_RegisterVariable(&gl_printcheckerror);
259 Cvar_RegisterVariable(&gl_workaround_mac_texmatrix);
261 Cvar_SetValue("r_render", 0);
264 Cvar_RegisterVariable(&gl_mesh_drawrangeelements);
265 Cvar_RegisterVariable(&gl_mesh_testarrayelement);
266 Cvar_RegisterVariable(&gl_mesh_testmanualfeeding);
267 Cvar_RegisterVariable(&gl_mesh_prefer_short_elements);
269 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");
271 R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap);
274 void GL_SetMirrorState(qboolean state);
276 void GL_SetupView_Orientation_Identity (void)
278 backend_viewmatrix = identitymatrix;
279 GL_SetMirrorState(false);
280 memset(&backend_modelmatrix, 0, sizeof(backend_modelmatrix));
283 void GL_SetupView_Orientation_FromEntity(const matrix4x4_t *matrix)
285 matrix4x4_t tempmatrix, basematrix;
286 Matrix4x4_Invert_Full(&tempmatrix, matrix);
287 Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
288 Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
289 Matrix4x4_Concat(&backend_viewmatrix, &basematrix, &tempmatrix);
291 GL_SetMirrorState(v_flipped.integer);
294 Matrix4x4_Transpose(&basematrix, &backend_viewmatrix);
295 Matrix4x4_ConcatScale3(&basematrix, -1, 1, 1);
296 Matrix4x4_Transpose(&backend_viewmatrix, &basematrix);
299 //Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[2], 1, 0, 0);
300 //Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[0], 0, 1, 0);
301 //Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[1], 0, 0, 1);
302 //Matrix4x4_ConcatTranslate(&backend_viewmatrix, -origin[0], -origin[1], -origin[2]);
304 // force an update of the model matrix by copying it off, resetting it, and then calling the R_Mesh_Matrix function with it
305 tempmatrix = backend_modelmatrix;
306 memset(&backend_modelmatrix, 0, sizeof(backend_modelmatrix));
307 R_Mesh_Matrix(&tempmatrix);
310 static void GL_BuildFrustum(double m[16], double left, double right, double bottom, double top, double nearVal, double farVal)
312 m[0] = 2 * nearVal / (right - left);
318 m[5] = 2 * nearVal / (top - bottom);
322 m[8] = (right + left) / (right - left);
323 m[9] = (top + bottom) / (top - bottom);
324 m[10] = - (farVal + nearVal) / (farVal - nearVal);
329 m[14] = - 2 * farVal * nearVal / (farVal - nearVal);
333 void GL_SetupView_Mode_Perspective (double frustumx, double frustumy, double zNear, double zFar)
339 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
342 // avoid glGetDoublev whenever possible, it may stall the render pipeline
343 // in the tested cases (nvidia) no measurable fps difference, but it sure
344 // makes a difference over a network line with GLX
345 GL_BuildFrustum(m, -frustumx * zNear, frustumx * zNear, -frustumy * zNear, frustumy * zNear, zNear, zFar);
346 qglLoadMatrixd(m);CHECKGLERROR
348 qglLoadIdentity();CHECKGLERROR
349 qglFrustum(-frustumx * zNear, frustumx * zNear, -frustumy * zNear, frustumy * zNear, zNear, zFar);CHECKGLERROR
350 qglGetDoublev(GL_PROJECTION_MATRIX, m);CHECKGLERROR
352 Matrix4x4_FromArrayDoubleGL(&backend_projectmatrix, m);
353 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
354 GL_SetupView_Orientation_Identity();
358 void GL_SetupView_Mode_PerspectiveInfiniteFarClip (double frustumx, double frustumy, double zNear)
364 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
365 qglLoadIdentity();CHECKGLERROR
367 nudge = 1.0 - 1.0 / (1<<23);
368 m[ 0] = 1.0 / frustumx;
373 m[ 5] = 1.0 / frustumy;
382 m[14] = -2 * zNear * nudge;
384 qglLoadMatrixd(m);CHECKGLERROR
385 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
386 GL_SetupView_Orientation_Identity();
388 Matrix4x4_FromArrayDoubleGL(&backend_projectmatrix, m);
391 static void GL_BuildOrtho(double m[16], double left, double right, double bottom, double top, double zNear, double zFar)
393 m[0] = 2/(right - left);
399 m[5] = 2/(top - bottom);
405 m[10] = -2/(zFar - zNear);
408 m[12] = - (right + left)/(right - left);
409 m[13] = - (top + bottom)/(top - bottom);
410 m[14] = - (zFar + zNear)/(zFar - zNear);
414 void GL_SetupView_Mode_Ortho (double x1, double y1, double x2, double y2, double zNear, double zFar)
420 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
422 // avoid glGetDoublev whenever possible, it may stall the render pipeline
423 // in the tested cases (nvidia) no measurable fps difference, but it sure
424 // makes a difference over a network line with GLX
425 GL_BuildOrtho(m, x1, x2, y2, y1, zNear, zFar);
426 qglLoadMatrixd(m);CHECKGLERROR
428 qglLoadIdentity();CHECKGLERROR
429 qglOrtho(x1, x2, y2, y1, zNear, zFar);CHECKGLERROR
430 qglGetDoublev(GL_PROJECTION_MATRIX, m);CHECKGLERROR
432 Matrix4x4_FromArrayDoubleGL(&backend_projectmatrix, m);
433 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
434 GL_SetupView_Orientation_Identity();
438 void GL_SetupView_ApplyCustomNearClipPlane(double normalx, double normaly, double normalz, double dist)
443 float clipPlane[4], v3[3], v4[3];
446 // This is Olique Depth Projection from http://www.terathon.com/code/oblique.php
447 // modified to fit in this codebase.
449 VectorSet(normal, normalx, normaly, normalz);
450 Matrix4x4_Transform3x3(&backend_viewmatrix, normal, clipPlane);
451 VectorScale(normal, dist, v3);
452 Matrix4x4_Transform(&backend_viewmatrix, v3, v4);
453 // FIXME: LordHavoc: I think this can be done more efficiently somehow but I can't remember the technique
454 clipPlane[3] = -DotProduct(v4, clipPlane);
458 // testing code for comparing results
460 VectorCopy4(clipPlane, clipPlane2);
461 R_Mesh_Matrix(&identitymatrix);
462 VectorSet(q, normal[0], normal[1], normal[2], -dist);
463 qglClipPlane(GL_CLIP_PLANE0, q);
464 qglGetClipPlane(GL_CLIP_PLANE0, q);
465 VectorCopy4(q, clipPlane);
469 // Calculate the clip-space corner point opposite the clipping plane
470 // as (sgn(clipPlane.x), sgn(clipPlane.y), 1, 1) and
471 // transform it into camera space by multiplying it
472 // by the inverse of the projection matrix
473 Matrix4x4_ToArrayDoubleGL(&backend_projectmatrix, matrix);
475 q[0] = ((clipPlane[0] < 0.0f ? -1.0f : clipPlane[0] > 0.0f ? 1.0f : 0.0f) + matrix[8]) / matrix[0];
476 q[1] = ((clipPlane[1] < 0.0f ? -1.0f : clipPlane[1] > 0.0f ? 1.0f : 0.0f) + matrix[9]) / matrix[5];
478 q[3] = (1.0f + matrix[10]) / matrix[14];
480 // Calculate the scaled plane vector
481 d = 2.0f / DotProduct4(clipPlane, q);
483 // Replace the third row of the projection matrix
484 matrix[2] = clipPlane[0] * d;
485 matrix[6] = clipPlane[1] * d;
486 matrix[10] = clipPlane[2] * d + 1.0f;
487 matrix[14] = clipPlane[3] * d;
489 // Load it back into OpenGL
490 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
491 qglLoadMatrixd(matrix);CHECKGLERROR
492 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
494 Matrix4x4_FromArrayDoubleGL(&backend_projectmatrix, matrix);
497 typedef struct gltextureunit_s
499 const void *pointer_texcoord;
500 size_t pointer_texcoord_offset;
501 int pointer_texcoord_buffer;
502 int t1d, t2d, t3d, tcubemap;
504 unsigned int arraycomponents;
505 int rgbscale, alphascale;
506 int combinergb, combinealpha;
507 // FIXME: add more combine stuff
508 // texmatrixenabled exists only to avoid unnecessary texmatrix compares
509 int texmatrixenabled;
514 static struct gl_state_s
522 int colormask; // stored as bottom 4 bits: r g b a (3 2 1 0 order)
525 float polygonoffset[2];
529 unsigned int clientunit;
530 gltextureunit_t units[MAX_TEXTUREUNITS];
534 int vertexbufferobject;
535 int elementbufferobject;
536 qboolean pointer_color_enabled;
537 const void *pointer_vertex;
538 const void *pointer_color;
539 size_t pointer_vertex_offset;
540 size_t pointer_color_offset;
541 int pointer_vertex_buffer;
542 int pointer_color_buffer;
546 static void GL_BindVBO(int bufferobject)
548 if (gl_state.vertexbufferobject != bufferobject)
550 gl_state.vertexbufferobject = bufferobject;
552 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, bufferobject);
557 static void GL_BindEBO(int bufferobject)
559 if (gl_state.elementbufferobject != bufferobject)
561 gl_state.elementbufferobject = bufferobject;
563 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, bufferobject);
568 void GL_SetupTextureState(void)
571 gltextureunit_t *unit;
573 gl_state.unit = MAX_TEXTUREUNITS;
574 gl_state.clientunit = MAX_TEXTUREUNITS;
575 for (i = 0;i < MAX_TEXTUREUNITS;i++)
577 unit = gl_state.units + i;
582 unit->arrayenabled = false;
583 unit->arraycomponents = 0;
584 unit->pointer_texcoord = NULL;
585 unit->pointer_texcoord_buffer = 0;
586 unit->pointer_texcoord_offset = 0;
588 unit->alphascale = 1;
589 unit->combinergb = GL_MODULATE;
590 unit->combinealpha = GL_MODULATE;
591 unit->texmatrixenabled = false;
592 unit->matrix = identitymatrix;
595 for (i = 0;i < backendimageunits;i++)
598 qglBindTexture(GL_TEXTURE_1D, 0);CHECKGLERROR
599 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
602 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
604 if (gl_texturecubemap)
606 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
610 for (i = 0;i < backendarrayunits;i++)
612 GL_ClientActiveTexture(i);
614 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
615 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
618 for (i = 0;i < backendunits;i++)
621 if (gl_workaround_mac_texmatrix.integer)
622 GL_ClientActiveTexture(i);
623 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
624 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
627 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
629 if (gl_texturecubemap)
631 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
633 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
634 qglLoadIdentity();CHECKGLERROR
635 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
636 if (gl_combine.integer)
638 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);CHECKGLERROR
639 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);CHECKGLERROR
640 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);CHECKGLERROR
641 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
642 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_TEXTURE);CHECKGLERROR // for GL_INTERPOLATE_ARB mode
643 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
644 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
645 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);CHECKGLERROR
646 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);CHECKGLERROR
647 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);CHECKGLERROR
648 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
649 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, GL_CONSTANT_ARB);CHECKGLERROR
650 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
651 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
652 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
653 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
654 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
658 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
665 void GL_Backend_ResetState(void)
667 memset(&gl_state, 0, sizeof(gl_state));
668 gl_state.depthtest = true;
669 gl_state.alphatest = false;
670 gl_state.blendfunc1 = GL_ONE;
671 gl_state.blendfunc2 = GL_ZERO;
672 gl_state.blend = false;
673 gl_state.depthmask = GL_TRUE;
674 gl_state.colormask = 15;
675 gl_state.color4f[0] = gl_state.color4f[1] = gl_state.color4f[2] = gl_state.color4f[3] = 1;
676 gl_state.lockrange_first = 0;
677 gl_state.lockrange_count = 0;
678 gl_state.cullface = v_flipped_state ? GL_BACK : GL_FRONT; // quake is backwards, this culls back faces
679 gl_state.cullfaceenable = true;
680 gl_state.polygonoffset[0] = 0;
681 gl_state.polygonoffset[1] = 0;
685 qglColorMask(1, 1, 1, 1);
686 qglAlphaFunc(GL_GEQUAL, 0.5);CHECKGLERROR
687 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
688 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
689 qglDisable(GL_BLEND);CHECKGLERROR
690 qglCullFace(gl_state.cullface);CHECKGLERROR
691 qglEnable(GL_CULL_FACE);CHECKGLERROR
692 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
693 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
694 qglDepthMask(gl_state.depthmask);CHECKGLERROR
695 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
697 if (gl_support_arb_vertex_buffer_object)
699 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
700 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
703 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), NULL);CHECKGLERROR
704 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
706 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL);CHECKGLERROR
707 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
709 GL_Color(0, 0, 0, 0);
710 GL_Color(1, 1, 1, 1);
712 GL_SetupTextureState();
715 void GL_ActiveTexture(unsigned int num)
717 if (gl_state.unit != num)
720 if (qglActiveTexture)
723 qglActiveTexture(GL_TEXTURE0_ARB + gl_state.unit);
729 void GL_ClientActiveTexture(unsigned int num)
731 if (gl_state.clientunit != num)
733 gl_state.clientunit = num;
734 if (qglActiveTexture)
737 qglClientActiveTexture(GL_TEXTURE0_ARB + gl_state.clientunit);
743 void GL_BlendFunc(int blendfunc1, int blendfunc2)
745 if (gl_state.blendfunc1 != blendfunc1 || gl_state.blendfunc2 != blendfunc2)
748 qglBlendFunc(gl_state.blendfunc1 = blendfunc1, gl_state.blendfunc2 = blendfunc2);CHECKGLERROR
749 if (gl_state.blendfunc2 == GL_ZERO)
751 if (gl_state.blendfunc1 == GL_ONE)
756 qglDisable(GL_BLEND);CHECKGLERROR
764 qglEnable(GL_BLEND);CHECKGLERROR
773 qglEnable(GL_BLEND);CHECKGLERROR
779 void GL_DepthMask(int state)
781 if (gl_state.depthmask != state)
784 qglDepthMask(gl_state.depthmask = state);CHECKGLERROR
788 void GL_DepthTest(int state)
790 if (gl_state.depthtest != state)
792 gl_state.depthtest = state;
794 if (gl_state.depthtest)
796 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
800 qglDisable(GL_DEPTH_TEST);CHECKGLERROR
805 void GL_DepthRange(float nearfrac, float farfrac)
807 if (gl_state.depthrange[0] != nearfrac || gl_state.depthrange[1] != farfrac)
809 gl_state.depthrange[0] = nearfrac;
810 gl_state.depthrange[1] = farfrac;
811 qglDepthRange(nearfrac, farfrac);
815 void GL_PolygonOffset(float planeoffset, float depthoffset)
817 if (gl_state.polygonoffset[0] != planeoffset || gl_state.polygonoffset[1] != depthoffset)
819 gl_state.polygonoffset[0] = planeoffset;
820 gl_state.polygonoffset[1] = depthoffset;
821 qglPolygonOffset(planeoffset, depthoffset);
825 void GL_SetMirrorState(qboolean state)
827 if(!state != !v_flipped_state)
829 // change cull face mode!
830 if(gl_state.cullface == GL_BACK)
831 qglCullFace((gl_state.cullface = GL_FRONT));
832 else if(gl_state.cullface == GL_FRONT)
833 qglCullFace((gl_state.cullface = GL_BACK));
835 v_flipped_state = state;
838 void GL_CullFace(int state)
844 if(state == GL_FRONT)
846 else if(state == GL_BACK)
850 if (state != GL_NONE)
852 if (!gl_state.cullfaceenable)
854 gl_state.cullfaceenable = true;
855 qglEnable(GL_CULL_FACE);CHECKGLERROR
857 if (gl_state.cullface != state)
859 gl_state.cullface = state;
860 qglCullFace(gl_state.cullface);CHECKGLERROR
865 if (gl_state.cullfaceenable)
867 gl_state.cullfaceenable = false;
868 qglDisable(GL_CULL_FACE);CHECKGLERROR
873 void GL_AlphaTest(int state)
875 if (gl_state.alphatest != state)
877 gl_state.alphatest = state;
879 if (gl_state.alphatest)
881 qglEnable(GL_ALPHA_TEST);CHECKGLERROR
885 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
890 void GL_ColorMask(int r, int g, int b, int a)
892 int state = r*8 + g*4 + b*2 + a*1;
893 if (gl_state.colormask != state)
895 gl_state.colormask = state;
897 qglColorMask((GLboolean)r, (GLboolean)g, (GLboolean)b, (GLboolean)a);CHECKGLERROR
901 void GL_Color(float cr, float cg, float cb, float ca)
903 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)
905 gl_state.color4f[0] = cr;
906 gl_state.color4f[1] = cg;
907 gl_state.color4f[2] = cb;
908 gl_state.color4f[3] = ca;
910 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);
915 void GL_LockArrays(int first, int count)
917 if (count < gl_lockarrays_minimumvertices.integer)
922 if (gl_state.lockrange_count != count || gl_state.lockrange_first != first)
924 if (gl_state.lockrange_count)
926 gl_state.lockrange_count = 0;
928 qglUnlockArraysEXT();
931 if (count && gl_supportslockarrays && gl_lockarrays.integer && r_render.integer)
933 gl_state.lockrange_first = first;
934 gl_state.lockrange_count = count;
936 qglLockArraysEXT(first, count);
942 void GL_Scissor (int x, int y, int width, int height)
945 qglScissor(x, vid.height - (y + height),width,height);
949 void GL_ScissorTest(int state)
951 if(gl_state.scissortest == state)
955 if((gl_state.scissortest = state))
956 qglEnable(GL_SCISSOR_TEST);
958 qglDisable(GL_SCISSOR_TEST);
962 void GL_Clear(int mask)
965 qglClear(mask);CHECKGLERROR
968 void GL_TransformToScreen(const vec4_t in, vec4_t out)
972 Matrix4x4_Transform4 (&backend_viewmatrix, in, temp);
973 Matrix4x4_Transform4 (&backend_projectmatrix, temp, out);
975 out[0] = r_refdef.view.x + (out[0] * iw + 1.0f) * r_refdef.view.width * 0.5f;
976 out[1] = r_refdef.view.y + r_refdef.view.height - (out[1] * iw + 1.0f) * r_refdef.view.height * 0.5f;
977 out[2] = r_refdef.view.z + (out[2] * iw + 1.0f) * r_refdef.view.depth * 0.5f;
980 // called at beginning of frame
981 void R_Mesh_Start(void)
985 if (gl_printcheckerror.integer && !gl_paranoid.integer)
987 Con_Printf("WARNING: gl_printcheckerror is on but gl_paranoid is off, turning it on...\n");
988 Cvar_SetValueQuick(&gl_paranoid, 1);
990 GL_Backend_ResetState();
993 qboolean GL_Backend_CompileShader(int programobject, GLenum shadertypeenum, const char *shadertype, int numstrings, const char **strings)
997 char compilelog[MAX_INPUTLINE];
998 shaderobject = qglCreateShaderObjectARB(shadertypeenum);CHECKGLERROR
1001 qglShaderSourceARB(shaderobject, numstrings, strings, NULL);CHECKGLERROR
1002 qglCompileShaderARB(shaderobject);CHECKGLERROR
1003 qglGetObjectParameterivARB(shaderobject, GL_OBJECT_COMPILE_STATUS_ARB, &shadercompiled);CHECKGLERROR
1004 qglGetInfoLogARB(shaderobject, sizeof(compilelog), NULL, compilelog);CHECKGLERROR
1005 if (compilelog[0] && developer.integer > 0)
1007 int i, j, pretextlines = 0;
1008 for (i = 0;i < numstrings - 1;i++)
1009 for (j = 0;strings[i][j];j++)
1010 if (strings[i][j] == '\n')
1012 Con_DPrintf("%s shader compile log:\n%s\n(line offset for any above warnings/errors: %i)\n", shadertype, compilelog, pretextlines);
1014 if (!shadercompiled)
1016 qglDeleteObjectARB(shaderobject);CHECKGLERROR
1019 qglAttachObjectARB(programobject, shaderobject);CHECKGLERROR
1020 qglDeleteObjectARB(shaderobject);CHECKGLERROR
1024 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)
1026 GLint programlinked;
1027 GLuint programobject = 0;
1028 char linklog[MAX_INPUTLINE];
1031 programobject = qglCreateProgramObjectARB();CHECKGLERROR
1035 if (vertexstrings_count && !GL_Backend_CompileShader(programobject, GL_VERTEX_SHADER_ARB, "vertex", vertexstrings_count, vertexstrings_list))
1038 #ifdef GL_GEOMETRY_SHADER_ARB
1039 if (geometrystrings_count && !GL_Backend_CompileShader(programobject, GL_GEOMETRY_SHADER_ARB, "geometry", geometrystrings_count, geometrystrings_list))
1043 if (fragmentstrings_count && !GL_Backend_CompileShader(programobject, GL_FRAGMENT_SHADER_ARB, "fragment", fragmentstrings_count, fragmentstrings_list))
1046 qglLinkProgramARB(programobject);CHECKGLERROR
1047 qglGetObjectParameterivARB(programobject, GL_OBJECT_LINK_STATUS_ARB, &programlinked);CHECKGLERROR
1048 qglGetInfoLogARB(programobject, sizeof(linklog), NULL, linklog);CHECKGLERROR
1051 Con_DPrintf("program link log:\n%s\n", linklog);
1052 // software vertex shader is ok but software fragment shader is WAY
1053 // too slow, fail program if so.
1054 // NOTE: this string might be ATI specific, but that's ok because the
1055 // ATI R300 chip (Radeon 9500-9800/X300) is the most likely to use a
1056 // software fragment shader due to low instruction and dependent
1058 if (strstr(linklog, "fragment shader will run in software"))
1059 programlinked = false;
1063 return programobject;
1065 qglDeleteObjectARB(programobject);CHECKGLERROR
1069 void GL_Backend_FreeProgram(unsigned int prog)
1072 qglDeleteObjectARB(prog);
1076 int gl_backend_rebindtextures;
1078 void GL_Backend_RenumberElements(int *out, int count, const int *in, int offset)
1083 for (i = 0;i < count;i++)
1084 *out++ = *in++ + offset;
1087 memcpy(out, in, sizeof(*out) * count);
1090 // renders triangles using vertices from the active arrays
1091 int paranoidblah = 0;
1092 void R_Mesh_Draw(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const unsigned short *element3s, int bufferobject3i, int bufferobject3s)
1094 unsigned int numelements = numtriangles * 3;
1095 if (numvertices < 3 || numtriangles < 1)
1097 Con_Printf("R_Mesh_Draw(%d, %d, %d, %d, %8p, %8p, %i, %i);\n", firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, bufferobject3i, bufferobject3s);
1100 if (!gl_mesh_prefer_short_elements.integer)
1108 element3i += firsttriangle * 3;
1110 element3s += firsttriangle * 3;
1111 switch (gl_vbo.integer)
1116 bufferobject3i = bufferobject3s = 0;
1122 bufferobject3i = bufferobject3s = 0;
1126 r_refdef.stats.meshes++;
1127 r_refdef.stats.meshes_elements += numelements;
1128 if (gl_paranoid.integer)
1130 unsigned int i, j, size;
1132 // note: there's no validation done here on buffer objects because it
1133 // is somewhat difficult to get at the data, and gl_paranoid can be
1134 // used without buffer objects if the need arises
1135 // (the data could be gotten using glMapBuffer but it would be very
1136 // slow due to uncachable video memory reads)
1137 if (!qglIsEnabled(GL_VERTEX_ARRAY))
1138 Con_Print("R_Mesh_Draw: vertex array not enabled\n");
1140 if (gl_state.pointer_vertex)
1141 for (j = 0, size = numvertices * 3, p = (int *)((float *)gl_state.pointer_vertex + firstvertex * 3);j < size;j++, p++)
1143 if (gl_state.pointer_color_enabled)
1145 if (!qglIsEnabled(GL_COLOR_ARRAY))
1146 Con_Print("R_Mesh_Draw: color array set but not enabled\n");
1148 if (gl_state.pointer_color && gl_state.pointer_color_enabled)
1149 for (j = 0, size = numvertices * 4, p = (int *)((float *)gl_state.pointer_color + firstvertex * 4);j < size;j++, p++)
1152 for (i = 0;i < backendarrayunits;i++)
1154 if (gl_state.units[i].arrayenabled)
1156 GL_ClientActiveTexture(i);
1157 if (!qglIsEnabled(GL_TEXTURE_COORD_ARRAY))
1158 Con_Print("R_Mesh_Draw: texcoord array set but not enabled\n");
1160 if (gl_state.units[i].pointer_texcoord && gl_state.units[i].arrayenabled)
1161 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++)
1167 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1169 if (element3i[i] < firstvertex || element3i[i] >= firstvertex + numvertices)
1171 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3i array\n", element3i[i], firstvertex, firstvertex + numvertices);
1178 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1180 if (element3s[i] < firstvertex || element3s[i] >= firstvertex + numvertices)
1182 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3s array\n", element3s[i], firstvertex, firstvertex + numvertices);
1189 if (r_render.integer)
1192 if (gl_mesh_testmanualfeeding.integer)
1194 unsigned int i, j, element;
1196 qglBegin(GL_TRIANGLES);
1197 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1199 element = element3i ? element3i[i] : element3s[i];
1200 for (j = 0;j < backendarrayunits;j++)
1202 if (gl_state.units[j].pointer_texcoord && gl_state.units[j].arrayenabled)
1204 if (backendarrayunits > 1)
1206 if (gl_state.units[j].arraycomponents == 4)
1208 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 4;
1209 qglMultiTexCoord4f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2], p[3]);
1211 else if (gl_state.units[j].arraycomponents == 3)
1213 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 3;
1214 qglMultiTexCoord3f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2]);
1216 else if (gl_state.units[j].arraycomponents == 2)
1218 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 2;
1219 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, p[0], p[1]);
1223 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 1;
1224 qglMultiTexCoord1f(GL_TEXTURE0_ARB + j, p[0]);
1229 if (gl_state.units[j].arraycomponents == 4)
1231 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 4;
1232 qglTexCoord4f(p[0], p[1], p[2], p[3]);
1234 else if (gl_state.units[j].arraycomponents == 3)
1236 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 3;
1237 qglTexCoord3f(p[0], p[1], p[2]);
1239 else if (gl_state.units[j].arraycomponents == 2)
1241 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 2;
1242 qglTexCoord2f(p[0], p[1]);
1246 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 1;
1247 qglTexCoord1f(p[0]);
1252 if (gl_state.pointer_color && gl_state.pointer_color_enabled)
1254 p = ((const GLfloat *)(gl_state.pointer_color)) + element * 4;
1255 qglColor4f(p[0], p[1], p[2], p[3]);
1257 p = ((const GLfloat *)(gl_state.pointer_vertex)) + element * 3;
1258 qglVertex3f(p[0], p[1], p[2]);
1263 else if (gl_mesh_testarrayelement.integer)
1266 qglBegin(GL_TRIANGLES);
1269 for (i = 0;i < numtriangles * 3;i++)
1270 qglArrayElement(element3i[i]);
1274 for (i = 0;i < numtriangles * 3;i++)
1275 qglArrayElement(element3s[i]);
1280 else if (bufferobject3s)
1282 GL_BindEBO(bufferobject3s);
1283 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1285 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices, numelements, GL_UNSIGNED_SHORT, (void *)(firsttriangle * sizeof(unsigned short[3])));
1290 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, (void *)(firsttriangle * sizeof(unsigned short[3])));
1294 else if (bufferobject3i)
1296 GL_BindEBO(bufferobject3i);
1297 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1299 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices, numelements, GL_UNSIGNED_INT, (void *)(firsttriangle * sizeof(unsigned int[3])));
1304 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, (void *)(firsttriangle * sizeof(unsigned int[3])));
1311 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1313 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices, numelements, GL_UNSIGNED_SHORT, element3s);
1318 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, element3s);
1325 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1327 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices, numelements, GL_UNSIGNED_INT, element3i);
1332 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, element3i);
1339 // restores backend state, used when done with 3D rendering
1340 void R_Mesh_Finish(void)
1345 GL_LockArrays(0, 0);
1348 for (i = 0;i < backendimageunits;i++)
1350 GL_ActiveTexture(i);
1351 qglBindTexture(GL_TEXTURE_1D, 0);CHECKGLERROR
1352 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
1355 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
1357 if (gl_texturecubemap)
1359 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
1362 for (i = 0;i < backendarrayunits;i++)
1364 GL_ActiveTexture(backendarrayunits - 1 - i);
1365 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1367 for (i = 0;i < backendunits;i++)
1369 GL_ActiveTexture(backendunits - 1 - i);
1370 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1371 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1374 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1376 if (gl_texturecubemap)
1378 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1380 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
1381 if (gl_combine.integer)
1383 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
1384 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
1387 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1388 qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
1390 qglDisable(GL_BLEND);CHECKGLERROR
1391 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1392 qglDepthMask(GL_TRUE);CHECKGLERROR
1393 qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);CHECKGLERROR
1396 int R_Mesh_CreateStaticBufferObject(unsigned int target, void *data, size_t size, const char *name)
1398 gl_bufferobjectinfo_t *info;
1399 GLuint bufferobject;
1401 if (!gl_vbo.integer)
1404 qglGenBuffersARB(1, &bufferobject);
1407 case GL_ELEMENT_ARRAY_BUFFER_ARB: GL_BindEBO(bufferobject);break;
1408 case GL_ARRAY_BUFFER_ARB: GL_BindVBO(bufferobject);break;
1409 default: Sys_Error("R_Mesh_CreateStaticBufferObject: unknown target type %i\n", target);return 0;
1411 qglBufferDataARB(target, size, data, GL_STATIC_DRAW_ARB);
1413 info = Mem_ExpandableArray_AllocRecord(&gl_bufferobjectinfoarray);
1414 memset(info, 0, sizeof(*info));
1415 info->target = target;
1416 info->object = bufferobject;
1418 strlcpy(info->name, name, sizeof(info->name));
1420 return (int)bufferobject;
1423 void R_Mesh_DestroyBufferObject(int bufferobject)
1426 gl_bufferobjectinfo_t *info;
1428 qglDeleteBuffersARB(1, (GLuint *)&bufferobject);
1430 endindex = Mem_ExpandableArray_IndexRange(&gl_bufferobjectinfoarray);
1431 for (i = 0;i < endindex;i++)
1433 info = Mem_ExpandableArray_RecordAtIndex(&gl_bufferobjectinfoarray, i);
1436 if (info->object == bufferobject)
1438 Mem_ExpandableArray_FreeRecord(&gl_bufferobjectinfoarray, (void *)info);
1444 void GL_Mesh_ListVBOs(qboolean printeach)
1447 size_t ebocount = 0, ebomemory = 0;
1448 size_t vbocount = 0, vbomemory = 0;
1449 gl_bufferobjectinfo_t *info;
1450 endindex = Mem_ExpandableArray_IndexRange(&gl_bufferobjectinfoarray);
1451 for (i = 0;i < endindex;i++)
1453 info = Mem_ExpandableArray_RecordAtIndex(&gl_bufferobjectinfoarray, i);
1456 switch(info->target)
1458 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;
1459 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;
1460 default: Con_Printf("gl_vbostats: unknown target type %i\n", info->target);break;
1463 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);
1466 void R_Mesh_Matrix(const matrix4x4_t *matrix)
1468 if (memcmp(matrix, &backend_modelmatrix, sizeof(matrix4x4_t)))
1470 double glmatrix[16];
1471 backend_modelmatrix = *matrix;
1472 Matrix4x4_Concat(&backend_modelviewmatrix, &backend_viewmatrix, matrix);
1473 Matrix4x4_ToArrayDoubleGL(&backend_modelviewmatrix, glmatrix);
1475 qglLoadMatrixd(glmatrix);CHECKGLERROR
1479 void R_Mesh_VertexPointer(const float *vertex3f, int bufferobject, size_t bufferoffset)
1481 if (!gl_vbo.integer)
1483 if (gl_state.pointer_vertex != vertex3f || gl_state.pointer_vertex_buffer != bufferobject || gl_state.pointer_vertex_offset != bufferoffset)
1485 gl_state.pointer_vertex = vertex3f;
1486 gl_state.pointer_vertex_buffer = bufferobject;
1487 gl_state.pointer_vertex_offset = bufferoffset;
1489 GL_BindVBO(bufferobject);
1490 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), bufferobject ? (void *)bufferoffset : vertex3f);CHECKGLERROR
1494 void R_Mesh_ColorPointer(const float *color4f, int bufferobject, size_t bufferoffset)
1496 // note: this can not rely on bufferobject to decide whether a color array
1497 // is supplied, because surfmesh_t shares one vbo for all arrays, which
1498 // means that a valid vbo may be supplied even if there is no color array.
1501 if (!gl_vbo.integer)
1503 // caller wants color array enabled
1504 if (!gl_state.pointer_color_enabled)
1506 gl_state.pointer_color_enabled = true;
1508 qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1510 if (gl_state.pointer_color != color4f || gl_state.pointer_color_buffer != bufferobject || gl_state.pointer_color_offset != bufferoffset)
1512 gl_state.pointer_color = color4f;
1513 gl_state.pointer_color_buffer = bufferobject;
1514 gl_state.pointer_color_offset = bufferoffset;
1516 GL_BindVBO(bufferobject);
1517 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), bufferobject ? (void *)bufferoffset : color4f);CHECKGLERROR
1522 // caller wants color array disabled
1523 if (gl_state.pointer_color_enabled)
1525 gl_state.pointer_color_enabled = false;
1527 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1528 // when color array is on the glColor gets trashed, set it again
1529 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);CHECKGLERROR
1534 void R_Mesh_TexCoordPointer(unsigned int unitnum, unsigned int numcomponents, const float *texcoord, int bufferobject, size_t bufferoffset)
1536 gltextureunit_t *unit = gl_state.units + unitnum;
1537 // update array settings
1539 // note: there is no need to check bufferobject here because all cases
1540 // that involve a valid bufferobject also supply a texcoord array
1543 if (!gl_vbo.integer)
1545 // texture array unit is enabled, enable the array
1546 if (!unit->arrayenabled)
1548 unit->arrayenabled = true;
1549 GL_ClientActiveTexture(unitnum);
1550 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1553 if (unit->pointer_texcoord != texcoord || unit->pointer_texcoord_buffer != bufferobject || unit->pointer_texcoord_offset != bufferoffset || unit->arraycomponents != numcomponents)
1555 unit->pointer_texcoord = texcoord;
1556 unit->pointer_texcoord_buffer = bufferobject;
1557 unit->pointer_texcoord_offset = bufferoffset;
1558 unit->arraycomponents = numcomponents;
1559 GL_ClientActiveTexture(unitnum);
1560 GL_BindVBO(bufferobject);
1561 qglTexCoordPointer(unit->arraycomponents, GL_FLOAT, sizeof(float) * unit->arraycomponents, bufferobject ? (void *)bufferoffset : texcoord);CHECKGLERROR
1566 // texture array unit is disabled, disable the array
1567 if (unit->arrayenabled)
1569 unit->arrayenabled = false;
1570 GL_ClientActiveTexture(unitnum);
1571 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1576 void R_Mesh_TexBindAll(unsigned int unitnum, int tex1d, int tex2d, int tex3d, int texcubemap)
1578 gltextureunit_t *unit = gl_state.units + unitnum;
1579 if (unitnum >= backendimageunits)
1581 // update 1d texture binding
1582 if (unit->t1d != tex1d)
1584 GL_ActiveTexture(unitnum);
1585 if (unitnum < backendunits)
1591 qglEnable(GL_TEXTURE_1D);CHECKGLERROR
1598 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1603 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1605 // update 2d texture binding
1606 if (unit->t2d != tex2d)
1608 GL_ActiveTexture(unitnum);
1609 if (unitnum < backendunits)
1615 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
1622 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1627 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1629 // update 3d texture binding
1630 if (unit->t3d != tex3d)
1632 GL_ActiveTexture(unitnum);
1633 if (unitnum < backendunits)
1639 qglEnable(GL_TEXTURE_3D);CHECKGLERROR
1646 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1651 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1653 // update cubemap texture binding
1654 if (unit->tcubemap != texcubemap)
1656 GL_ActiveTexture(unitnum);
1657 if (unitnum < backendunits)
1661 if (unit->tcubemap == 0)
1663 qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1670 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1674 unit->tcubemap = texcubemap;
1675 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1679 void R_Mesh_TexBind1D(unsigned int unitnum, int texnum)
1681 gltextureunit_t *unit = gl_state.units + unitnum;
1682 if (unitnum >= backendimageunits)
1684 // update 1d texture binding
1685 if (unit->t1d != texnum)
1687 GL_ActiveTexture(unitnum);
1688 if (unitnum < backendunits)
1694 qglEnable(GL_TEXTURE_1D);CHECKGLERROR
1701 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1706 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1708 // update 2d texture binding
1711 GL_ActiveTexture(unitnum);
1712 if (unitnum < backendunits)
1716 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1720 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1722 // update 3d texture binding
1725 GL_ActiveTexture(unitnum);
1726 if (unitnum < backendunits)
1730 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1734 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1736 // update cubemap texture binding
1739 GL_ActiveTexture(unitnum);
1740 if (unitnum < backendunits)
1744 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1748 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1752 void R_Mesh_TexBind(unsigned int unitnum, int texnum)
1754 gltextureunit_t *unit = gl_state.units + unitnum;
1755 if (unitnum >= backendimageunits)
1757 // update 1d texture binding
1760 GL_ActiveTexture(unitnum);
1761 if (unitnum < backendunits)
1765 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1769 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1771 // update 2d texture binding
1772 if (unit->t2d != texnum)
1774 GL_ActiveTexture(unitnum);
1775 if (unitnum < backendunits)
1781 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
1788 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1793 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1795 // update 3d texture binding
1798 GL_ActiveTexture(unitnum);
1799 if (unitnum < backendunits)
1803 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1807 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1809 // update cubemap texture binding
1810 if (unit->tcubemap != 0)
1812 GL_ActiveTexture(unitnum);
1813 if (unitnum < backendunits)
1817 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1821 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1825 void R_Mesh_TexBind3D(unsigned int unitnum, int texnum)
1827 gltextureunit_t *unit = gl_state.units + unitnum;
1828 if (unitnum >= backendimageunits)
1830 // update 1d texture binding
1833 GL_ActiveTexture(unitnum);
1834 if (unitnum < backendunits)
1838 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1842 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1844 // update 2d texture binding
1847 GL_ActiveTexture(unitnum);
1848 if (unitnum < backendunits)
1852 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1856 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1858 // update 3d texture binding
1859 if (unit->t3d != texnum)
1861 GL_ActiveTexture(unitnum);
1862 if (unitnum < backendunits)
1868 qglEnable(GL_TEXTURE_3D);CHECKGLERROR
1875 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1880 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1882 // update cubemap texture binding
1883 if (unit->tcubemap != 0)
1885 GL_ActiveTexture(unitnum);
1886 if (unitnum < backendunits)
1890 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1894 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1898 void R_Mesh_TexBindCubeMap(unsigned int unitnum, int texnum)
1900 gltextureunit_t *unit = gl_state.units + unitnum;
1901 if (unitnum >= backendimageunits)
1903 // update 1d texture binding
1906 GL_ActiveTexture(unitnum);
1907 if (unitnum < backendunits)
1911 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1915 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1917 // update 2d texture binding
1920 GL_ActiveTexture(unitnum);
1921 if (unitnum < backendunits)
1925 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1929 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1931 // update 3d texture binding
1934 GL_ActiveTexture(unitnum);
1935 if (unitnum < backendunits)
1939 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1943 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1945 // update cubemap texture binding
1946 if (unit->tcubemap != texnum)
1948 GL_ActiveTexture(unitnum);
1949 if (unitnum < backendunits)
1953 if (unit->tcubemap == 0)
1955 qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1962 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1966 unit->tcubemap = texnum;
1967 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1971 static const double gl_identitymatrix[16] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
1973 void R_Mesh_TexMatrix(unsigned int unitnum, const matrix4x4_t *matrix)
1975 gltextureunit_t *unit = gl_state.units + unitnum;
1976 if (matrix->m[3][3])
1978 // texmatrix specified, check if it is different
1979 if (!unit->texmatrixenabled || memcmp(&unit->matrix, matrix, sizeof(matrix4x4_t)))
1981 double glmatrix[16];
1982 unit->texmatrixenabled = true;
1983 unit->matrix = *matrix;
1985 Matrix4x4_ToArrayDoubleGL(&unit->matrix, glmatrix);
1986 GL_ActiveTexture(unitnum);
1987 if (gl_workaround_mac_texmatrix.integer)
1988 GL_ClientActiveTexture(unitnum);
1989 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1990 qglLoadMatrixd(glmatrix);CHECKGLERROR
1991 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1996 // no texmatrix specified, revert to identity
1997 if (unit->texmatrixenabled)
1999 unit->texmatrixenabled = false;
2000 unit->matrix = identitymatrix;
2002 GL_ActiveTexture(unitnum);
2003 if (gl_workaround_mac_texmatrix.integer)
2004 GL_ClientActiveTexture(unitnum);
2005 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
2006 qglLoadIdentity();CHECKGLERROR
2007 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
2012 void R_Mesh_TexCombine(unsigned int unitnum, int combinergb, int combinealpha, int rgbscale, int alphascale)
2014 gltextureunit_t *unit = gl_state.units + unitnum;
2016 if (gl_combine.integer)
2018 // GL_ARB_texture_env_combine
2020 combinergb = GL_MODULATE;
2022 combinealpha = GL_MODULATE;
2027 if (unit->combinergb != combinergb)
2029 unit->combinergb = combinergb;
2030 GL_ActiveTexture(unitnum);
2031 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
2033 if (unit->combinealpha != combinealpha)
2035 unit->combinealpha = combinealpha;
2036 GL_ActiveTexture(unitnum);
2037 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
2039 if (unit->rgbscale != rgbscale)
2041 GL_ActiveTexture(unitnum);
2042 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (unit->rgbscale = rgbscale));CHECKGLERROR
2044 if (unit->alphascale != alphascale)
2046 GL_ActiveTexture(unitnum);
2047 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, (unit->alphascale = alphascale));CHECKGLERROR
2054 combinergb = GL_MODULATE;
2055 if (unit->combinergb != combinergb)
2057 unit->combinergb = combinergb;
2058 GL_ActiveTexture(unitnum);
2059 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combinergb);CHECKGLERROR
2064 void R_Mesh_TextureState(const rmeshstate_t *m)
2071 if (gl_backend_rebindtextures)
2073 gl_backend_rebindtextures = false;
2074 GL_SetupTextureState();
2078 for (i = 0;i < backendimageunits;i++)
2079 R_Mesh_TexBindAll(i, m->tex1d[i], m->tex[i], m->tex3d[i], m->texcubemap[i]);
2080 for (i = 0;i < backendarrayunits;i++)
2082 if (m->pointer_texcoord3f[i])
2083 R_Mesh_TexCoordPointer(i, 3, m->pointer_texcoord3f[i], m->pointer_texcoord_bufferobject[i], m->pointer_texcoord_bufferoffset[i]);
2085 R_Mesh_TexCoordPointer(i, 2, m->pointer_texcoord[i], m->pointer_texcoord_bufferobject[i], m->pointer_texcoord_bufferoffset[i]);
2087 for (i = 0;i < backendunits;i++)
2089 R_Mesh_TexMatrix(i, &m->texmatrix[i]);
2090 R_Mesh_TexCombine(i, m->texcombinergb[i], m->texcombinealpha[i], m->texrgbscale[i], m->texalphascale[i]);
2095 void R_Mesh_ResetTextureState(void)
2097 unsigned int unitnum;
2102 if (gl_backend_rebindtextures)
2104 gl_backend_rebindtextures = false;
2105 GL_SetupTextureState();
2109 for (unitnum = 0;unitnum < backendimageunits;unitnum++)
2111 gltextureunit_t *unit = gl_state.units + unitnum;
2112 // update 1d texture binding
2115 GL_ActiveTexture(unitnum);
2116 if (unitnum < backendunits)
2118 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
2121 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
2123 // update 2d texture binding
2126 GL_ActiveTexture(unitnum);
2127 if (unitnum < backendunits)
2129 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
2132 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
2134 // update 3d texture binding
2137 GL_ActiveTexture(unitnum);
2138 if (unitnum < backendunits)
2140 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
2143 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
2145 // update cubemap texture binding
2148 GL_ActiveTexture(unitnum);
2149 if (unitnum < backendunits)
2151 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
2154 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
2157 for (unitnum = 0;unitnum < backendarrayunits;unitnum++)
2159 gltextureunit_t *unit = gl_state.units + unitnum;
2160 // texture array unit is disabled, disable the array
2161 if (unit->arrayenabled)
2163 unit->arrayenabled = false;
2164 GL_ClientActiveTexture(unitnum);
2165 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
2168 for (unitnum = 0;unitnum < backendunits;unitnum++)
2170 gltextureunit_t *unit = gl_state.units + unitnum;
2171 // no texmatrix specified, revert to identity
2172 if (unit->texmatrixenabled)
2174 unit->texmatrixenabled = false;
2175 unit->matrix = identitymatrix;
2177 GL_ActiveTexture(unitnum);
2178 if (gl_workaround_mac_texmatrix.integer)
2179 GL_ClientActiveTexture(unitnum);
2180 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
2181 qglLoadIdentity();CHECKGLERROR
2182 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
2184 if (gl_combine.integer)
2186 // GL_ARB_texture_env_combine
2187 if (unit->combinergb != GL_MODULATE)
2189 unit->combinergb = GL_MODULATE;
2190 GL_ActiveTexture(unitnum);
2191 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
2193 if (unit->combinealpha != GL_MODULATE)
2195 unit->combinealpha = GL_MODULATE;
2196 GL_ActiveTexture(unitnum);
2197 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
2199 if (unit->rgbscale != 1)
2201 GL_ActiveTexture(unitnum);
2202 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (unit->rgbscale = 1));CHECKGLERROR
2204 if (unit->alphascale != 1)
2206 GL_ActiveTexture(unitnum);
2207 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, (unit->alphascale = 1));CHECKGLERROR
2213 if (unit->combinergb != GL_MODULATE)
2215 unit->combinergb = GL_MODULATE;
2216 GL_ActiveTexture(unitnum);
2217 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combinergb);CHECKGLERROR