3 #include "cl_collision.h"
5 cvar_t gl_mesh_drawrangeelements = {0, "gl_mesh_drawrangeelements", "1", "use glDrawRangeElements function if available instead of glDrawElements (for performance comparisons or bug testing)"};
6 cvar_t gl_mesh_testarrayelement = {0, "gl_mesh_testarrayelement", "0", "use glBegin(GL_TRIANGLES);glArrayElement();glEnd(); primitives instead of glDrawElements (useful to test for driver bugs with glDrawElements)"};
7 cvar_t gl_mesh_testmanualfeeding = {0, "gl_mesh_testmanualfeeding", "0", "use glBegin(GL_TRIANGLES);glTexCoord2f();glVertex3f();glEnd(); primitives instead of glDrawElements (useful to test for driver bugs with glDrawElements)"};
8 cvar_t gl_mesh_prefer_short_elements = {0, "gl_mesh_prefer_short_elements", "1", "use GL_UNSIGNED_SHORT element arrays instead of GL_UNSIGNED_INT"};
9 cvar_t gl_paranoid = {0, "gl_paranoid", "0", "enables OpenGL error checking and other tests"};
10 cvar_t gl_printcheckerror = {0, "gl_printcheckerror", "0", "prints all OpenGL error checks, useful to identify location of driver crashes"};
12 cvar_t r_render = {0, "r_render", "1", "enables rendering calls (you want this on!)"};
13 cvar_t r_waterwarp = {CVAR_SAVE, "r_waterwarp", "1", "warp view while underwater"};
14 cvar_t gl_polyblend = {CVAR_SAVE, "gl_polyblend", "1", "tints view while underwater, hurt, etc"};
15 cvar_t gl_dither = {CVAR_SAVE, "gl_dither", "1", "enables OpenGL dithering (16bit looks bad with this off)"};
16 cvar_t gl_lockarrays = {0, "gl_lockarrays", "0", "enables use of glLockArraysEXT, may cause glitches with some broken drivers, and may be slower than normal"};
17 cvar_t gl_lockarrays_minimumvertices = {0, "gl_lockarrays_minimumvertices", "1", "minimum number of vertices required for use of glLockArraysEXT, setting this too low may reduce performance"};
18 cvar_t gl_vbo = {CVAR_SAVE, "gl_vbo", "3", "make use of GL_ARB_vertex_buffer_object extension to store static geometry in video memory for faster rendering, 0 disables VBO allocation or use, 1 enables VBOs for vertex and triangle data, 2 only for vertex data, 3 for vertex data and triangle data of simple meshes (ones with only one surface)"};
19 cvar_t gl_fbo = {CVAR_SAVE, "gl_fbo", "1", "make use of GL_ARB_framebuffer_object extension to enable shadowmaps and other features using pixel formats different from the framebuffer"};
21 cvar_t v_flipped = {0, "v_flipped", "0", "mirror the screen (poor man's left handed mode)"};
22 qboolean v_flipped_state = false;
24 int gl_maxdrawrangeelementsvertices;
25 int gl_maxdrawrangeelementsindices;
30 void GL_PrintError(int errornumber, char *filename, int linenumber)
34 #ifdef GL_INVALID_ENUM
36 Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
39 #ifdef GL_INVALID_VALUE
40 case GL_INVALID_VALUE:
41 Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
44 #ifdef GL_INVALID_OPERATION
45 case GL_INVALID_OPERATION:
46 Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
49 #ifdef GL_STACK_OVERFLOW
50 case GL_STACK_OVERFLOW:
51 Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
54 #ifdef GL_STACK_UNDERFLOW
55 case GL_STACK_UNDERFLOW:
56 Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
59 #ifdef GL_OUT_OF_MEMORY
60 case GL_OUT_OF_MEMORY:
61 Con_Printf("GL_OUT_OF_MEMORY at %s:%i\n", filename, linenumber);
64 #ifdef GL_TABLE_TOO_LARGE
65 case GL_TABLE_TOO_LARGE:
66 Con_Printf("GL_TABLE_TOO_LARGE at %s:%i\n", filename, linenumber);
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);
260 Cvar_SetValue("r_render", 0);
263 Cvar_RegisterVariable(&gl_mesh_drawrangeelements);
264 Cvar_RegisterVariable(&gl_mesh_testarrayelement);
265 Cvar_RegisterVariable(&gl_mesh_testmanualfeeding);
266 Cvar_RegisterVariable(&gl_mesh_prefer_short_elements);
268 Cmd_AddCommand("gl_vbostats", GL_VBOStats_f, "prints a list of all buffer objects (vertex data and triangle elements) and total video memory used by them");
270 R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap);
273 void GL_SetMirrorState(qboolean state);
275 void GL_SetupView_Orientation_Identity (void)
277 backend_viewmatrix = identitymatrix;
278 GL_SetMirrorState(false);
279 memset(&backend_modelmatrix, 0, sizeof(backend_modelmatrix));
282 void GL_SetupView_Orientation_FromEntity(const matrix4x4_t *matrix)
284 matrix4x4_t tempmatrix, basematrix;
285 Matrix4x4_Invert_Full(&tempmatrix, matrix);
286 Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
287 Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
288 Matrix4x4_Concat(&backend_viewmatrix, &basematrix, &tempmatrix);
290 GL_SetMirrorState(v_flipped.integer != 0);
293 Matrix4x4_Transpose(&basematrix, &backend_viewmatrix);
294 Matrix4x4_ConcatScale3(&basematrix, -1, 1, 1);
295 Matrix4x4_Transpose(&backend_viewmatrix, &basematrix);
298 //Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[2], 1, 0, 0);
299 //Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[0], 0, 1, 0);
300 //Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[1], 0, 0, 1);
301 //Matrix4x4_ConcatTranslate(&backend_viewmatrix, -origin[0], -origin[1], -origin[2]);
303 // force an update of the model matrix by copying it off, resetting it, and then calling the R_Mesh_Matrix function with it
304 tempmatrix = backend_modelmatrix;
305 memset(&backend_modelmatrix, 0, sizeof(backend_modelmatrix));
306 R_Mesh_Matrix(&tempmatrix);
309 static void GL_BuildFrustum(double m[16], double left, double right, double bottom, double top, double nearVal, double farVal)
311 m[0] = 2 * nearVal / (right - left);
317 m[5] = 2 * nearVal / (top - bottom);
321 m[8] = (right + left) / (right - left);
322 m[9] = (top + bottom) / (top - bottom);
323 m[10] = - (farVal + nearVal) / (farVal - nearVal);
328 m[14] = - 2 * farVal * nearVal / (farVal - nearVal);
332 void GL_SetupView_Mode_Perspective (double frustumx, double frustumy, double zNear, double zFar)
338 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
341 // avoid glGetDoublev whenever possible, it may stall the render pipeline
342 // in the tested cases (nvidia) no measurable fps difference, but it sure
343 // makes a difference over a network line with GLX
344 GL_BuildFrustum(m, -frustumx * zNear, frustumx * zNear, -frustumy * zNear, frustumy * zNear, zNear, zFar);
345 qglLoadMatrixd(m);CHECKGLERROR
347 qglLoadIdentity();CHECKGLERROR
348 qglFrustum(-frustumx * zNear, frustumx * zNear, -frustumy * zNear, frustumy * zNear, zNear, zFar);CHECKGLERROR
349 qglGetDoublev(GL_PROJECTION_MATRIX, m);CHECKGLERROR
351 Matrix4x4_FromArrayDoubleGL(&backend_projectmatrix, m);
352 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
353 GL_SetupView_Orientation_Identity();
357 void GL_SetupView_Mode_PerspectiveInfiniteFarClip (double frustumx, double frustumy, double zNear)
363 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
364 qglLoadIdentity();CHECKGLERROR
366 nudge = 1.0 - 1.0 / (1<<23);
367 m[ 0] = 1.0 / frustumx;
372 m[ 5] = 1.0 / frustumy;
381 m[14] = -2 * zNear * nudge;
383 qglLoadMatrixd(m);CHECKGLERROR
384 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
385 GL_SetupView_Orientation_Identity();
387 Matrix4x4_FromArrayDoubleGL(&backend_projectmatrix, m);
390 static void GL_BuildOrtho(double m[16], double left, double right, double bottom, double top, double zNear, double zFar)
392 m[0] = 2/(right - left);
398 m[5] = 2/(top - bottom);
404 m[10] = -2/(zFar - zNear);
407 m[12] = - (right + left)/(right - left);
408 m[13] = - (top + bottom)/(top - bottom);
409 m[14] = - (zFar + zNear)/(zFar - zNear);
413 void GL_SetupView_Mode_Ortho (double x1, double y1, double x2, double y2, double zNear, double zFar)
419 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
421 // avoid glGetDoublev whenever possible, it may stall the render pipeline
422 // in the tested cases (nvidia) no measurable fps difference, but it sure
423 // makes a difference over a network line with GLX
424 GL_BuildOrtho(m, x1, x2, y2, y1, zNear, zFar);
425 qglLoadMatrixd(m);CHECKGLERROR
427 qglLoadIdentity();CHECKGLERROR
428 qglOrtho(x1, x2, y2, y1, zNear, zFar);CHECKGLERROR
429 qglGetDoublev(GL_PROJECTION_MATRIX, m);CHECKGLERROR
431 Matrix4x4_FromArrayDoubleGL(&backend_projectmatrix, m);
432 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
433 GL_SetupView_Orientation_Identity();
437 void GL_SetupView_ApplyCustomNearClipPlane(double normalx, double normaly, double normalz, double dist)
442 float clipPlane[4], v3[3], v4[3];
445 // This is Olique Depth Projection from http://www.terathon.com/code/oblique.php
446 // modified to fit in this codebase.
448 VectorSet(normal, normalx, normaly, normalz);
449 Matrix4x4_Transform3x3(&backend_viewmatrix, normal, clipPlane);
450 VectorScale(normal, dist, v3);
451 Matrix4x4_Transform(&backend_viewmatrix, v3, v4);
452 // FIXME: LordHavoc: I think this can be done more efficiently somehow but I can't remember the technique
453 clipPlane[3] = -DotProduct(v4, clipPlane);
457 // testing code for comparing results
459 VectorCopy4(clipPlane, clipPlane2);
460 R_Mesh_Matrix(&identitymatrix);
461 VectorSet(q, normal[0], normal[1], normal[2], -dist);
462 qglClipPlane(GL_CLIP_PLANE0, q);
463 qglGetClipPlane(GL_CLIP_PLANE0, q);
464 VectorCopy4(q, clipPlane);
468 // Calculate the clip-space corner point opposite the clipping plane
469 // as (sgn(clipPlane.x), sgn(clipPlane.y), 1, 1) and
470 // transform it into camera space by multiplying it
471 // by the inverse of the projection matrix
472 Matrix4x4_ToArrayDoubleGL(&backend_projectmatrix, matrix);
474 q[0] = ((clipPlane[0] < 0.0f ? -1.0f : clipPlane[0] > 0.0f ? 1.0f : 0.0f) + matrix[8]) / matrix[0];
475 q[1] = ((clipPlane[1] < 0.0f ? -1.0f : clipPlane[1] > 0.0f ? 1.0f : 0.0f) + matrix[9]) / matrix[5];
477 q[3] = (1.0f + matrix[10]) / matrix[14];
479 // Calculate the scaled plane vector
480 d = 2.0f / DotProduct4(clipPlane, q);
482 // Replace the third row of the projection matrix
483 matrix[2] = clipPlane[0] * d;
484 matrix[6] = clipPlane[1] * d;
485 matrix[10] = clipPlane[2] * d + 1.0f;
486 matrix[14] = clipPlane[3] * d;
488 // Load it back into OpenGL
489 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
490 qglLoadMatrixd(matrix);CHECKGLERROR
491 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
493 Matrix4x4_FromArrayDoubleGL(&backend_projectmatrix, matrix);
496 typedef struct gltextureunit_s
498 const void *pointer_texcoord;
499 size_t pointer_texcoord_offset;
500 int pointer_texcoord_buffer;
501 int t1d, t2d, t3d, tcubemap;
503 unsigned int arraycomponents;
504 int rgbscale, alphascale;
505 int combinergb, combinealpha;
506 // FIXME: add more combine stuff
507 // texmatrixenabled exists only to avoid unnecessary texmatrix compares
508 int texmatrixenabled;
513 static struct gl_state_s
521 int colormask; // stored as bottom 4 bits: r g b a (3 2 1 0 order)
524 float polygonoffset[2];
528 unsigned int clientunit;
529 gltextureunit_t units[MAX_TEXTUREUNITS];
533 int vertexbufferobject;
534 int elementbufferobject;
535 qboolean pointer_color_enabled;
536 const void *pointer_vertex;
537 const void *pointer_color;
538 size_t pointer_vertex_offset;
539 size_t pointer_color_offset;
540 int pointer_vertex_buffer;
541 int pointer_color_buffer;
545 static void GL_BindVBO(int bufferobject)
547 if (gl_state.vertexbufferobject != bufferobject)
549 gl_state.vertexbufferobject = bufferobject;
551 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, bufferobject);
556 static void GL_BindEBO(int bufferobject)
558 if (gl_state.elementbufferobject != bufferobject)
560 gl_state.elementbufferobject = bufferobject;
562 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, bufferobject);
567 void GL_SetupTextureState(void)
570 gltextureunit_t *unit;
572 gl_state.unit = MAX_TEXTUREUNITS;
573 gl_state.clientunit = MAX_TEXTUREUNITS;
574 for (i = 0;i < MAX_TEXTUREUNITS;i++)
576 unit = gl_state.units + i;
581 unit->arrayenabled = false;
582 unit->arraycomponents = 0;
583 unit->pointer_texcoord = NULL;
584 unit->pointer_texcoord_buffer = 0;
585 unit->pointer_texcoord_offset = 0;
587 unit->alphascale = 1;
588 unit->combinergb = GL_MODULATE;
589 unit->combinealpha = GL_MODULATE;
590 unit->texmatrixenabled = false;
591 unit->matrix = identitymatrix;
594 for (i = 0;i < backendimageunits;i++)
597 qglBindTexture(GL_TEXTURE_1D, 0);CHECKGLERROR
598 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
601 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
603 if (gl_texturecubemap)
605 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
609 for (i = 0;i < backendarrayunits;i++)
611 GL_ClientActiveTexture(i);
613 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
614 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
617 for (i = 0;i < backendunits;i++)
620 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
621 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
624 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
626 if (gl_texturecubemap)
628 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
630 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
631 qglLoadIdentity();CHECKGLERROR
632 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
633 if (gl_combine.integer)
635 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);CHECKGLERROR
636 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);CHECKGLERROR
637 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);CHECKGLERROR
638 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
639 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_TEXTURE);CHECKGLERROR // for GL_INTERPOLATE_ARB mode
640 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
641 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
642 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);CHECKGLERROR
643 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);CHECKGLERROR
644 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);CHECKGLERROR
645 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
646 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, GL_CONSTANT_ARB);CHECKGLERROR
647 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
648 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
649 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
650 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
651 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
655 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
662 void GL_Backend_ResetState(void)
664 memset(&gl_state, 0, sizeof(gl_state));
665 gl_state.depthtest = true;
666 gl_state.alphatest = false;
667 gl_state.blendfunc1 = GL_ONE;
668 gl_state.blendfunc2 = GL_ZERO;
669 gl_state.blend = false;
670 gl_state.depthmask = GL_TRUE;
671 gl_state.colormask = 15;
672 gl_state.color4f[0] = gl_state.color4f[1] = gl_state.color4f[2] = gl_state.color4f[3] = 1;
673 gl_state.lockrange_first = 0;
674 gl_state.lockrange_count = 0;
675 gl_state.cullface = v_flipped_state ? GL_BACK : GL_FRONT; // quake is backwards, this culls back faces
676 gl_state.cullfaceenable = true;
677 gl_state.polygonoffset[0] = 0;
678 gl_state.polygonoffset[1] = 0;
682 qglColorMask(1, 1, 1, 1);
683 qglAlphaFunc(GL_GEQUAL, 0.5);CHECKGLERROR
684 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
685 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
686 qglDisable(GL_BLEND);CHECKGLERROR
687 qglCullFace(gl_state.cullface);CHECKGLERROR
688 qglEnable(GL_CULL_FACE);CHECKGLERROR
689 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
690 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
691 qglDepthMask(gl_state.depthmask);CHECKGLERROR
692 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
694 if (gl_support_arb_vertex_buffer_object)
696 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
697 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
700 if (gl_support_ext_framebuffer_object)
702 qglBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
703 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
706 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), NULL);CHECKGLERROR
707 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
709 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL);CHECKGLERROR
710 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
712 GL_Color(0, 0, 0, 0);
713 GL_Color(1, 1, 1, 1);
715 GL_SetupTextureState();
718 void GL_ActiveTexture(unsigned int num)
720 if (gl_state.unit != num)
723 if (qglActiveTexture)
726 qglActiveTexture(GL_TEXTURE0_ARB + gl_state.unit);
732 void GL_ClientActiveTexture(unsigned int num)
734 if (gl_state.clientunit != num)
736 gl_state.clientunit = num;
737 if (qglActiveTexture)
740 qglClientActiveTexture(GL_TEXTURE0_ARB + gl_state.clientunit);
746 void GL_BlendFunc(int blendfunc1, int blendfunc2)
748 if (gl_state.blendfunc1 != blendfunc1 || gl_state.blendfunc2 != blendfunc2)
751 qglBlendFunc(gl_state.blendfunc1 = blendfunc1, gl_state.blendfunc2 = blendfunc2);CHECKGLERROR
752 if (gl_state.blendfunc2 == GL_ZERO)
754 if (gl_state.blendfunc1 == GL_ONE)
759 qglDisable(GL_BLEND);CHECKGLERROR
767 qglEnable(GL_BLEND);CHECKGLERROR
776 qglEnable(GL_BLEND);CHECKGLERROR
782 void GL_DepthMask(int state)
784 if (gl_state.depthmask != state)
787 qglDepthMask(gl_state.depthmask = state);CHECKGLERROR
791 void GL_DepthTest(int state)
793 if (gl_state.depthtest != state)
795 gl_state.depthtest = state;
797 if (gl_state.depthtest)
799 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
803 qglDisable(GL_DEPTH_TEST);CHECKGLERROR
808 void GL_DepthRange(float nearfrac, float farfrac)
810 if (gl_state.depthrange[0] != nearfrac || gl_state.depthrange[1] != farfrac)
812 gl_state.depthrange[0] = nearfrac;
813 gl_state.depthrange[1] = farfrac;
814 qglDepthRange(nearfrac, farfrac);
818 void GL_PolygonOffset(float planeoffset, float depthoffset)
820 if (gl_state.polygonoffset[0] != planeoffset || gl_state.polygonoffset[1] != depthoffset)
822 gl_state.polygonoffset[0] = planeoffset;
823 gl_state.polygonoffset[1] = depthoffset;
824 qglPolygonOffset(planeoffset, depthoffset);
828 void GL_SetMirrorState(qboolean state)
830 if(!state != !v_flipped_state)
832 // change cull face mode!
833 if(gl_state.cullface == GL_BACK)
834 qglCullFace((gl_state.cullface = GL_FRONT));
835 else if(gl_state.cullface == GL_FRONT)
836 qglCullFace((gl_state.cullface = GL_BACK));
838 v_flipped_state = state;
841 void GL_CullFace(int state)
847 if(state == GL_FRONT)
849 else if(state == GL_BACK)
853 if (state != GL_NONE)
855 if (!gl_state.cullfaceenable)
857 gl_state.cullfaceenable = true;
858 qglEnable(GL_CULL_FACE);CHECKGLERROR
860 if (gl_state.cullface != state)
862 gl_state.cullface = state;
863 qglCullFace(gl_state.cullface);CHECKGLERROR
868 if (gl_state.cullfaceenable)
870 gl_state.cullfaceenable = false;
871 qglDisable(GL_CULL_FACE);CHECKGLERROR
876 void GL_AlphaTest(int state)
878 if (gl_state.alphatest != state)
880 gl_state.alphatest = state;
882 if (gl_state.alphatest)
884 qglEnable(GL_ALPHA_TEST);CHECKGLERROR
888 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
893 void GL_ColorMask(int r, int g, int b, int a)
895 int state = r*8 + g*4 + b*2 + a*1;
896 if (gl_state.colormask != state)
898 gl_state.colormask = state;
900 qglColorMask((GLboolean)r, (GLboolean)g, (GLboolean)b, (GLboolean)a);CHECKGLERROR
904 void GL_Color(float cr, float cg, float cb, float ca)
906 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)
908 gl_state.color4f[0] = cr;
909 gl_state.color4f[1] = cg;
910 gl_state.color4f[2] = cb;
911 gl_state.color4f[3] = ca;
913 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);
918 void GL_LockArrays(int first, int count)
920 if (count < gl_lockarrays_minimumvertices.integer)
925 if (gl_state.lockrange_count != count || gl_state.lockrange_first != first)
927 if (gl_state.lockrange_count)
929 gl_state.lockrange_count = 0;
931 qglUnlockArraysEXT();
934 if (count && gl_supportslockarrays && gl_lockarrays.integer && r_render.integer)
936 gl_state.lockrange_first = first;
937 gl_state.lockrange_count = count;
939 qglLockArraysEXT(first, count);
945 void GL_Scissor (int x, int y, int width, int height)
948 qglScissor(x, vid.height - (y + height),width,height);
952 void GL_ScissorTest(int state)
954 if(gl_state.scissortest == state)
958 if((gl_state.scissortest = state))
959 qglEnable(GL_SCISSOR_TEST);
961 qglDisable(GL_SCISSOR_TEST);
965 void GL_Clear(int mask)
968 qglClear(mask);CHECKGLERROR
971 void GL_TransformToScreen(const vec4_t in, vec4_t out)
975 Matrix4x4_Transform4 (&backend_viewmatrix, in, temp);
976 Matrix4x4_Transform4 (&backend_projectmatrix, temp, out);
978 out[0] = r_refdef.view.x + (out[0] * iw + 1.0f) * r_refdef.view.width * 0.5f;
979 out[1] = r_refdef.view.y + r_refdef.view.height - (out[1] * iw + 1.0f) * r_refdef.view.height * 0.5f;
980 out[2] = r_refdef.view.z + (out[2] * iw + 1.0f) * r_refdef.view.depth * 0.5f;
983 // called at beginning of frame
984 void R_Mesh_Start(void)
988 if (gl_printcheckerror.integer && !gl_paranoid.integer)
990 Con_Printf("WARNING: gl_printcheckerror is on but gl_paranoid is off, turning it on...\n");
991 Cvar_SetValueQuick(&gl_paranoid, 1);
993 GL_Backend_ResetState();
996 qboolean GL_Backend_CompileShader(int programobject, GLenum shadertypeenum, const char *shadertype, int numstrings, const char **strings)
1000 char compilelog[MAX_INPUTLINE];
1001 shaderobject = qglCreateShaderObjectARB(shadertypeenum);CHECKGLERROR
1004 qglShaderSourceARB(shaderobject, numstrings, strings, NULL);CHECKGLERROR
1005 qglCompileShaderARB(shaderobject);CHECKGLERROR
1006 qglGetObjectParameterivARB(shaderobject, GL_OBJECT_COMPILE_STATUS_ARB, &shadercompiled);CHECKGLERROR
1007 qglGetInfoLogARB(shaderobject, sizeof(compilelog), NULL, compilelog);CHECKGLERROR
1008 if (compilelog[0] && developer.integer > 0)
1010 int i, j, pretextlines = 0;
1011 for (i = 0;i < numstrings - 1;i++)
1012 for (j = 0;strings[i][j];j++)
1013 if (strings[i][j] == '\n')
1015 Con_DPrintf("%s shader compile log:\n%s\n(line offset for any above warnings/errors: %i)\n", shadertype, compilelog, pretextlines);
1017 if (!shadercompiled)
1019 qglDeleteObjectARB(shaderobject);CHECKGLERROR
1022 qglAttachObjectARB(programobject, shaderobject);CHECKGLERROR
1023 qglDeleteObjectARB(shaderobject);CHECKGLERROR
1027 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)
1029 GLint programlinked;
1030 GLuint programobject = 0;
1031 char linklog[MAX_INPUTLINE];
1034 programobject = qglCreateProgramObjectARB();CHECKGLERROR
1038 if (vertexstrings_count && !GL_Backend_CompileShader(programobject, GL_VERTEX_SHADER_ARB, "vertex", vertexstrings_count, vertexstrings_list))
1041 #ifdef GL_GEOMETRY_SHADER_ARB
1042 if (geometrystrings_count && !GL_Backend_CompileShader(programobject, GL_GEOMETRY_SHADER_ARB, "geometry", geometrystrings_count, geometrystrings_list))
1046 if (fragmentstrings_count && !GL_Backend_CompileShader(programobject, GL_FRAGMENT_SHADER_ARB, "fragment", fragmentstrings_count, fragmentstrings_list))
1049 qglLinkProgramARB(programobject);CHECKGLERROR
1050 qglGetObjectParameterivARB(programobject, GL_OBJECT_LINK_STATUS_ARB, &programlinked);CHECKGLERROR
1051 qglGetInfoLogARB(programobject, sizeof(linklog), NULL, linklog);CHECKGLERROR
1054 Con_DPrintf("program link log:\n%s\n", linklog);
1055 // software vertex shader is ok but software fragment shader is WAY
1056 // too slow, fail program if so.
1057 // NOTE: this string might be ATI specific, but that's ok because the
1058 // ATI R300 chip (Radeon 9500-9800/X300) is the most likely to use a
1059 // software fragment shader due to low instruction and dependent
1061 if (strstr(linklog, "fragment shader will run in software"))
1062 programlinked = false;
1066 return programobject;
1068 qglDeleteObjectARB(programobject);CHECKGLERROR
1072 void GL_Backend_FreeProgram(unsigned int prog)
1075 qglDeleteObjectARB(prog);
1079 int gl_backend_rebindtextures;
1081 void GL_Backend_RenumberElements(int *out, int count, const int *in, int offset)
1086 for (i = 0;i < count;i++)
1087 *out++ = *in++ + offset;
1090 memcpy(out, in, sizeof(*out) * count);
1093 // renders triangles using vertices from the active arrays
1094 int paranoidblah = 0;
1095 void R_Mesh_Draw(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const unsigned short *element3s, int bufferobject3i, int bufferobject3s)
1097 unsigned int numelements = numtriangles * 3;
1098 if (numvertices < 3 || numtriangles < 1)
1100 if (numvertices < 0 || numtriangles < 0 || developer.integer >= 100)
1101 Con_Printf("R_Mesh_Draw(%d, %d, %d, %d, %8p, %8p, %i, %i);\n", firstvertex, numvertices, firsttriangle, numtriangles, (void *)element3i, (void *)element3s, bufferobject3i, bufferobject3s);
1104 if (!gl_mesh_prefer_short_elements.integer)
1112 element3i += firsttriangle * 3;
1114 element3s += firsttriangle * 3;
1115 switch (gl_vbo.integer)
1120 bufferobject3i = bufferobject3s = 0;
1126 bufferobject3i = bufferobject3s = 0;
1130 r_refdef.stats.meshes++;
1131 r_refdef.stats.meshes_elements += numelements;
1132 if (gl_paranoid.integer)
1134 unsigned int i, j, size;
1136 // note: there's no validation done here on buffer objects because it
1137 // is somewhat difficult to get at the data, and gl_paranoid can be
1138 // used without buffer objects if the need arises
1139 // (the data could be gotten using glMapBuffer but it would be very
1140 // slow due to uncachable video memory reads)
1141 if (!qglIsEnabled(GL_VERTEX_ARRAY))
1142 Con_Print("R_Mesh_Draw: vertex array not enabled\n");
1144 if (gl_state.pointer_vertex)
1145 for (j = 0, size = numvertices * 3, p = (int *)((float *)gl_state.pointer_vertex + firstvertex * 3);j < size;j++, p++)
1147 if (gl_state.pointer_color_enabled)
1149 if (!qglIsEnabled(GL_COLOR_ARRAY))
1150 Con_Print("R_Mesh_Draw: color array set but not enabled\n");
1152 if (gl_state.pointer_color && gl_state.pointer_color_enabled)
1153 for (j = 0, size = numvertices * 4, p = (int *)((float *)gl_state.pointer_color + firstvertex * 4);j < size;j++, p++)
1156 for (i = 0;i < backendarrayunits;i++)
1158 if (gl_state.units[i].arrayenabled)
1160 GL_ClientActiveTexture(i);
1161 if (!qglIsEnabled(GL_TEXTURE_COORD_ARRAY))
1162 Con_Print("R_Mesh_Draw: texcoord array set but not enabled\n");
1164 if (gl_state.units[i].pointer_texcoord && gl_state.units[i].arrayenabled)
1165 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++)
1171 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1173 if (element3i[i] < firstvertex || element3i[i] >= firstvertex + numvertices)
1175 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3i array\n", element3i[i], firstvertex, firstvertex + numvertices);
1182 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1184 if (element3s[i] < firstvertex || element3s[i] >= firstvertex + numvertices)
1186 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3s array\n", element3s[i], firstvertex, firstvertex + numvertices);
1193 if (r_render.integer)
1196 if (gl_mesh_testmanualfeeding.integer)
1198 unsigned int i, j, element;
1200 qglBegin(GL_TRIANGLES);
1201 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1203 element = element3i ? element3i[i] : element3s[i];
1204 for (j = 0;j < backendarrayunits;j++)
1206 if (gl_state.units[j].pointer_texcoord && gl_state.units[j].arrayenabled)
1208 if (backendarrayunits > 1)
1210 if (gl_state.units[j].arraycomponents == 4)
1212 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 4;
1213 qglMultiTexCoord4f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2], p[3]);
1215 else if (gl_state.units[j].arraycomponents == 3)
1217 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 3;
1218 qglMultiTexCoord3f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2]);
1220 else if (gl_state.units[j].arraycomponents == 2)
1222 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 2;
1223 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, p[0], p[1]);
1227 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 1;
1228 qglMultiTexCoord1f(GL_TEXTURE0_ARB + j, p[0]);
1233 if (gl_state.units[j].arraycomponents == 4)
1235 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 4;
1236 qglTexCoord4f(p[0], p[1], p[2], p[3]);
1238 else if (gl_state.units[j].arraycomponents == 3)
1240 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 3;
1241 qglTexCoord3f(p[0], p[1], p[2]);
1243 else if (gl_state.units[j].arraycomponents == 2)
1245 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 2;
1246 qglTexCoord2f(p[0], p[1]);
1250 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 1;
1251 qglTexCoord1f(p[0]);
1256 if (gl_state.pointer_color && gl_state.pointer_color_enabled)
1258 p = ((const GLfloat *)(gl_state.pointer_color)) + element * 4;
1259 qglColor4f(p[0], p[1], p[2], p[3]);
1261 p = ((const GLfloat *)(gl_state.pointer_vertex)) + element * 3;
1262 qglVertex3f(p[0], p[1], p[2]);
1267 else if (gl_mesh_testarrayelement.integer)
1270 qglBegin(GL_TRIANGLES);
1273 for (i = 0;i < numtriangles * 3;i++)
1274 qglArrayElement(element3i[i]);
1278 for (i = 0;i < numtriangles * 3;i++)
1279 qglArrayElement(element3s[i]);
1284 else if (bufferobject3s)
1286 GL_BindEBO(bufferobject3s);
1287 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1289 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, (void *)(firsttriangle * sizeof(unsigned short[3])));
1294 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, (void *)(firsttriangle * sizeof(unsigned short[3])));
1298 else if (bufferobject3i)
1300 GL_BindEBO(bufferobject3i);
1301 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1303 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, (void *)(firsttriangle * sizeof(unsigned int[3])));
1308 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, (void *)(firsttriangle * sizeof(unsigned int[3])));
1315 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1317 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, element3s);
1322 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, element3s);
1329 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1331 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, element3i);
1336 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, element3i);
1343 // restores backend state, used when done with 3D rendering
1344 void R_Mesh_Finish(void)
1349 GL_LockArrays(0, 0);
1352 for (i = 0;i < backendimageunits;i++)
1354 GL_ActiveTexture(i);
1355 qglBindTexture(GL_TEXTURE_1D, 0);CHECKGLERROR
1356 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
1359 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
1361 if (gl_texturecubemap)
1363 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
1366 for (i = 0;i < backendarrayunits;i++)
1368 GL_ActiveTexture(backendarrayunits - 1 - i);
1369 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1371 for (i = 0;i < backendunits;i++)
1373 GL_ActiveTexture(backendunits - 1 - i);
1374 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1375 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1378 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1380 if (gl_texturecubemap)
1382 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1384 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
1385 if (gl_combine.integer)
1387 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
1388 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
1391 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1392 qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
1394 qglDisable(GL_BLEND);CHECKGLERROR
1395 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1396 qglDepthMask(GL_TRUE);CHECKGLERROR
1397 qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);CHECKGLERROR
1400 int R_Mesh_CreateStaticBufferObject(unsigned int target, void *data, size_t size, const char *name)
1402 gl_bufferobjectinfo_t *info;
1403 GLuint bufferobject;
1405 if (!gl_vbo.integer)
1408 qglGenBuffersARB(1, &bufferobject);
1411 case GL_ELEMENT_ARRAY_BUFFER_ARB: GL_BindEBO(bufferobject);break;
1412 case GL_ARRAY_BUFFER_ARB: GL_BindVBO(bufferobject);break;
1413 default: Sys_Error("R_Mesh_CreateStaticBufferObject: unknown target type %i\n", target);return 0;
1415 qglBufferDataARB(target, size, data, GL_STATIC_DRAW_ARB);
1417 info = (gl_bufferobjectinfo_t *) Mem_ExpandableArray_AllocRecord(&gl_bufferobjectinfoarray);
1418 memset(info, 0, sizeof(*info));
1419 info->target = target;
1420 info->object = bufferobject;
1422 strlcpy(info->name, name, sizeof(info->name));
1424 return (int)bufferobject;
1427 void R_Mesh_DestroyBufferObject(int bufferobject)
1430 gl_bufferobjectinfo_t *info;
1432 qglDeleteBuffersARB(1, (GLuint *)&bufferobject);
1434 endindex = Mem_ExpandableArray_IndexRange(&gl_bufferobjectinfoarray);
1435 for (i = 0;i < endindex;i++)
1437 info = (gl_bufferobjectinfo_t *) Mem_ExpandableArray_RecordAtIndex(&gl_bufferobjectinfoarray, i);
1440 if (info->object == bufferobject)
1442 Mem_ExpandableArray_FreeRecord(&gl_bufferobjectinfoarray, (void *)info);
1448 void GL_Mesh_ListVBOs(qboolean printeach)
1451 size_t ebocount = 0, ebomemory = 0;
1452 size_t vbocount = 0, vbomemory = 0;
1453 gl_bufferobjectinfo_t *info;
1454 endindex = Mem_ExpandableArray_IndexRange(&gl_bufferobjectinfoarray);
1455 for (i = 0;i < endindex;i++)
1457 info = (gl_bufferobjectinfo_t *) Mem_ExpandableArray_RecordAtIndex(&gl_bufferobjectinfoarray, i);
1460 switch(info->target)
1462 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;
1463 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;
1464 default: Con_Printf("gl_vbostats: unknown target type %i\n", info->target);break;
1467 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);
1470 void R_Mesh_Matrix(const matrix4x4_t *matrix)
1472 if (memcmp(matrix, &backend_modelmatrix, sizeof(matrix4x4_t)))
1474 double glmatrix[16];
1475 backend_modelmatrix = *matrix;
1476 Matrix4x4_Concat(&backend_modelviewmatrix, &backend_viewmatrix, matrix);
1477 Matrix4x4_ToArrayDoubleGL(&backend_modelviewmatrix, glmatrix);
1479 qglLoadMatrixd(glmatrix);CHECKGLERROR
1483 void R_Mesh_VertexPointer(const float *vertex3f, int bufferobject, size_t bufferoffset)
1485 if (!gl_vbo.integer)
1487 if (gl_state.pointer_vertex != vertex3f || gl_state.pointer_vertex_buffer != bufferobject || gl_state.pointer_vertex_offset != bufferoffset)
1489 gl_state.pointer_vertex = vertex3f;
1490 gl_state.pointer_vertex_buffer = bufferobject;
1491 gl_state.pointer_vertex_offset = bufferoffset;
1493 GL_BindVBO(bufferobject);
1494 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), bufferobject ? (void *)bufferoffset : vertex3f);CHECKGLERROR
1498 void R_Mesh_ColorPointer(const float *color4f, int bufferobject, size_t bufferoffset)
1500 // note: this can not rely on bufferobject to decide whether a color array
1501 // is supplied, because surfmesh_t shares one vbo for all arrays, which
1502 // means that a valid vbo may be supplied even if there is no color array.
1505 if (!gl_vbo.integer)
1507 // caller wants color array enabled
1508 if (!gl_state.pointer_color_enabled)
1510 gl_state.pointer_color_enabled = true;
1512 qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1514 if (gl_state.pointer_color != color4f || gl_state.pointer_color_buffer != bufferobject || gl_state.pointer_color_offset != bufferoffset)
1516 gl_state.pointer_color = color4f;
1517 gl_state.pointer_color_buffer = bufferobject;
1518 gl_state.pointer_color_offset = bufferoffset;
1520 GL_BindVBO(bufferobject);
1521 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), bufferobject ? (void *)bufferoffset : color4f);CHECKGLERROR
1526 // caller wants color array disabled
1527 if (gl_state.pointer_color_enabled)
1529 gl_state.pointer_color_enabled = false;
1531 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1532 // when color array is on the glColor gets trashed, set it again
1533 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);CHECKGLERROR
1538 void R_Mesh_TexCoordPointer(unsigned int unitnum, unsigned int numcomponents, const float *texcoord, int bufferobject, size_t bufferoffset)
1540 gltextureunit_t *unit = gl_state.units + unitnum;
1541 // update array settings
1543 // note: there is no need to check bufferobject here because all cases
1544 // that involve a valid bufferobject also supply a texcoord array
1547 if (!gl_vbo.integer)
1549 // texture array unit is enabled, enable the array
1550 if (!unit->arrayenabled)
1552 unit->arrayenabled = true;
1553 GL_ClientActiveTexture(unitnum);
1554 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1557 if (unit->pointer_texcoord != texcoord || unit->pointer_texcoord_buffer != bufferobject || unit->pointer_texcoord_offset != bufferoffset || unit->arraycomponents != numcomponents)
1559 unit->pointer_texcoord = texcoord;
1560 unit->pointer_texcoord_buffer = bufferobject;
1561 unit->pointer_texcoord_offset = bufferoffset;
1562 unit->arraycomponents = numcomponents;
1563 GL_ClientActiveTexture(unitnum);
1564 GL_BindVBO(bufferobject);
1565 qglTexCoordPointer(unit->arraycomponents, GL_FLOAT, sizeof(float) * unit->arraycomponents, bufferobject ? (void *)bufferoffset : texcoord);CHECKGLERROR
1570 // texture array unit is disabled, disable the array
1571 if (unit->arrayenabled)
1573 unit->arrayenabled = false;
1574 GL_ClientActiveTexture(unitnum);
1575 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1580 void R_Mesh_TexBindAll(unsigned int unitnum, int tex1d, int tex2d, int tex3d, int texcubemap)
1582 gltextureunit_t *unit = gl_state.units + unitnum;
1583 if (unitnum >= backendimageunits)
1585 // update 1d texture binding
1586 if (unit->t1d != tex1d)
1588 GL_ActiveTexture(unitnum);
1589 if (unitnum < backendunits)
1595 qglEnable(GL_TEXTURE_1D);CHECKGLERROR
1602 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1607 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1609 // update 2d texture binding
1610 if (unit->t2d != tex2d)
1612 GL_ActiveTexture(unitnum);
1613 if (unitnum < backendunits)
1619 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
1626 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1631 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1633 // update 3d texture binding
1634 if (unit->t3d != tex3d)
1636 GL_ActiveTexture(unitnum);
1637 if (unitnum < backendunits)
1643 qglEnable(GL_TEXTURE_3D);CHECKGLERROR
1650 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1655 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1657 // update cubemap texture binding
1658 if (unit->tcubemap != texcubemap)
1660 GL_ActiveTexture(unitnum);
1661 if (unitnum < backendunits)
1665 if (unit->tcubemap == 0)
1667 qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1674 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1678 unit->tcubemap = texcubemap;
1679 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1683 void R_Mesh_TexBind1D(unsigned int unitnum, int texnum)
1685 gltextureunit_t *unit = gl_state.units + unitnum;
1686 if (unitnum >= backendimageunits)
1688 // update 1d texture binding
1689 if (unit->t1d != texnum)
1691 GL_ActiveTexture(unitnum);
1692 if (unitnum < backendunits)
1698 qglEnable(GL_TEXTURE_1D);CHECKGLERROR
1705 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1710 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1712 // update 2d texture binding
1715 GL_ActiveTexture(unitnum);
1716 if (unitnum < backendunits)
1720 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1724 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1726 // update 3d texture binding
1729 GL_ActiveTexture(unitnum);
1730 if (unitnum < backendunits)
1734 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1738 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1740 // update cubemap texture binding
1743 GL_ActiveTexture(unitnum);
1744 if (unitnum < backendunits)
1748 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1752 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1756 void R_Mesh_TexBind(unsigned int unitnum, int texnum)
1758 gltextureunit_t *unit = gl_state.units + unitnum;
1759 if (unitnum >= backendimageunits)
1761 // update 1d texture binding
1764 GL_ActiveTexture(unitnum);
1765 if (unitnum < backendunits)
1769 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1773 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1775 // update 2d texture binding
1776 if (unit->t2d != texnum)
1778 GL_ActiveTexture(unitnum);
1779 if (unitnum < backendunits)
1785 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
1792 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1797 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1799 // update 3d texture binding
1802 GL_ActiveTexture(unitnum);
1803 if (unitnum < backendunits)
1807 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1811 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1813 // update cubemap texture binding
1814 if (unit->tcubemap != 0)
1816 GL_ActiveTexture(unitnum);
1817 if (unitnum < backendunits)
1821 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1825 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1829 void R_Mesh_TexBind3D(unsigned int unitnum, int texnum)
1831 gltextureunit_t *unit = gl_state.units + unitnum;
1832 if (unitnum >= backendimageunits)
1834 // update 1d texture binding
1837 GL_ActiveTexture(unitnum);
1838 if (unitnum < backendunits)
1842 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1846 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1848 // update 2d texture binding
1851 GL_ActiveTexture(unitnum);
1852 if (unitnum < backendunits)
1856 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1860 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1862 // update 3d texture binding
1863 if (unit->t3d != texnum)
1865 GL_ActiveTexture(unitnum);
1866 if (unitnum < backendunits)
1872 qglEnable(GL_TEXTURE_3D);CHECKGLERROR
1879 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1884 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1886 // update cubemap texture binding
1887 if (unit->tcubemap != 0)
1889 GL_ActiveTexture(unitnum);
1890 if (unitnum < backendunits)
1894 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1898 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1902 void R_Mesh_TexBindCubeMap(unsigned int unitnum, int texnum)
1904 gltextureunit_t *unit = gl_state.units + unitnum;
1905 if (unitnum >= backendimageunits)
1907 // update 1d texture binding
1910 GL_ActiveTexture(unitnum);
1911 if (unitnum < backendunits)
1915 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1919 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1921 // update 2d texture binding
1924 GL_ActiveTexture(unitnum);
1925 if (unitnum < backendunits)
1929 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1933 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1935 // update 3d texture binding
1938 GL_ActiveTexture(unitnum);
1939 if (unitnum < backendunits)
1943 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1947 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1949 // update cubemap texture binding
1950 if (unit->tcubemap != texnum)
1952 GL_ActiveTexture(unitnum);
1953 if (unitnum < backendunits)
1957 if (unit->tcubemap == 0)
1959 qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1966 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1970 unit->tcubemap = texnum;
1971 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1975 static const double gl_identitymatrix[16] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
1977 void R_Mesh_TexMatrix(unsigned int unitnum, const matrix4x4_t *matrix)
1979 gltextureunit_t *unit = gl_state.units + unitnum;
1980 if (matrix->m[3][3])
1982 // texmatrix specified, check if it is different
1983 if (!unit->texmatrixenabled || memcmp(&unit->matrix, matrix, sizeof(matrix4x4_t)))
1985 double glmatrix[16];
1986 unit->texmatrixenabled = true;
1987 unit->matrix = *matrix;
1989 Matrix4x4_ToArrayDoubleGL(&unit->matrix, glmatrix);
1990 GL_ActiveTexture(unitnum);
1991 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1992 qglLoadMatrixd(glmatrix);CHECKGLERROR
1993 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1998 // no texmatrix specified, revert to identity
1999 if (unit->texmatrixenabled)
2001 unit->texmatrixenabled = false;
2002 unit->matrix = identitymatrix;
2004 GL_ActiveTexture(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 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
2179 qglLoadIdentity();CHECKGLERROR
2180 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
2182 if (gl_combine.integer)
2184 // GL_ARB_texture_env_combine
2185 if (unit->combinergb != GL_MODULATE)
2187 unit->combinergb = GL_MODULATE;
2188 GL_ActiveTexture(unitnum);
2189 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
2191 if (unit->combinealpha != GL_MODULATE)
2193 unit->combinealpha = GL_MODULATE;
2194 GL_ActiveTexture(unitnum);
2195 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
2197 if (unit->rgbscale != 1)
2199 GL_ActiveTexture(unitnum);
2200 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (unit->rgbscale = 1));CHECKGLERROR
2202 if (unit->alphascale != 1)
2204 GL_ActiveTexture(unitnum);
2205 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, (unit->alphascale = 1));CHECKGLERROR
2211 if (unit->combinergb != GL_MODULATE)
2213 unit->combinergb = GL_MODULATE;
2214 GL_ActiveTexture(unitnum);
2215 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combinergb);CHECKGLERROR