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 uses glLoadMatrixd followed by glLoadIdentity to clear a texture matrix (normally glLoadIdentity is sufficient by itself), if set to 2 it uses glLoadMatrixd without glLoadIdentity"};
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 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 for (i = 0;i < MAX_TEXTUREUNITS;i++)
575 unit = gl_state.units + i;
580 unit->arrayenabled = false;
581 unit->arraycomponents = 0;
582 unit->pointer_texcoord = NULL;
583 unit->pointer_texcoord_buffer = 0;
584 unit->pointer_texcoord_offset = 0;
586 unit->alphascale = 1;
587 unit->combinergb = GL_MODULATE;
588 unit->combinealpha = GL_MODULATE;
589 unit->texmatrixenabled = false;
590 unit->matrix = identitymatrix;
593 for (i = 0;i < backendimageunits;i++)
596 qglBindTexture(GL_TEXTURE_1D, 0);CHECKGLERROR
597 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
600 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
602 if (gl_texturecubemap)
604 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
608 for (i = 0;i < backendarrayunits;i++)
612 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
613 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
616 for (i = 0;i < backendunits;i++)
619 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
620 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
623 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
625 if (gl_texturecubemap)
627 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
629 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
630 qglLoadIdentity();CHECKGLERROR
631 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
632 if (gl_combine.integer)
634 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);CHECKGLERROR
635 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);CHECKGLERROR
636 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);CHECKGLERROR
637 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
638 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_TEXTURE);CHECKGLERROR // for GL_INTERPOLATE_ARB mode
639 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
640 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
641 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);CHECKGLERROR
642 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);CHECKGLERROR
643 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);CHECKGLERROR
644 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
645 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, GL_CONSTANT_ARB);CHECKGLERROR
646 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
647 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
648 qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
649 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
650 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
654 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
661 void GL_Backend_ResetState(void)
663 memset(&gl_state, 0, sizeof(gl_state));
664 gl_state.depthtest = true;
665 gl_state.alphatest = false;
666 gl_state.blendfunc1 = GL_ONE;
667 gl_state.blendfunc2 = GL_ZERO;
668 gl_state.blend = false;
669 gl_state.depthmask = GL_TRUE;
670 gl_state.colormask = 15;
671 gl_state.color4f[0] = gl_state.color4f[1] = gl_state.color4f[2] = gl_state.color4f[3] = 1;
672 gl_state.lockrange_first = 0;
673 gl_state.lockrange_count = 0;
674 gl_state.cullface = v_flipped_state ? GL_BACK : GL_FRONT; // quake is backwards, this culls back faces
675 gl_state.cullfaceenable = true;
676 gl_state.polygonoffset[0] = 0;
677 gl_state.polygonoffset[1] = 0;
681 qglColorMask(1, 1, 1, 1);
682 qglAlphaFunc(GL_GEQUAL, 0.5);CHECKGLERROR
683 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
684 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
685 qglDisable(GL_BLEND);CHECKGLERROR
686 qglCullFace(gl_state.cullface);CHECKGLERROR
687 qglEnable(GL_CULL_FACE);CHECKGLERROR
688 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
689 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
690 qglDepthMask(gl_state.depthmask);CHECKGLERROR
691 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
693 if (gl_support_arb_vertex_buffer_object)
695 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
696 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
699 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), NULL);CHECKGLERROR
700 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
702 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL);CHECKGLERROR
703 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
705 GL_Color(0, 0, 0, 0);
706 GL_Color(1, 1, 1, 1);
708 GL_SetupTextureState();
711 void GL_ActiveTexture(unsigned int num)
713 if (gl_state.unit != num)
716 if (qglActiveTexture)
719 qglActiveTexture(GL_TEXTURE0_ARB + gl_state.unit);
720 qglClientActiveTexture(GL_TEXTURE0_ARB + gl_state.unit);
726 void GL_BlendFunc(int blendfunc1, int blendfunc2)
728 if (gl_state.blendfunc1 != blendfunc1 || gl_state.blendfunc2 != blendfunc2)
731 qglBlendFunc(gl_state.blendfunc1 = blendfunc1, gl_state.blendfunc2 = blendfunc2);CHECKGLERROR
732 if (gl_state.blendfunc2 == GL_ZERO)
734 if (gl_state.blendfunc1 == GL_ONE)
739 qglDisable(GL_BLEND);CHECKGLERROR
747 qglEnable(GL_BLEND);CHECKGLERROR
756 qglEnable(GL_BLEND);CHECKGLERROR
762 void GL_DepthMask(int state)
764 if (gl_state.depthmask != state)
767 qglDepthMask(gl_state.depthmask = state);CHECKGLERROR
771 void GL_DepthTest(int state)
773 if (gl_state.depthtest != state)
775 gl_state.depthtest = state;
777 if (gl_state.depthtest)
779 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
783 qglDisable(GL_DEPTH_TEST);CHECKGLERROR
788 void GL_DepthRange(float nearfrac, float farfrac)
790 if (gl_state.depthrange[0] != nearfrac || gl_state.depthrange[1] != farfrac)
792 gl_state.depthrange[0] = nearfrac;
793 gl_state.depthrange[1] = farfrac;
794 qglDepthRange(nearfrac, farfrac);
798 void GL_PolygonOffset(float planeoffset, float depthoffset)
800 if (gl_state.polygonoffset[0] != planeoffset || gl_state.polygonoffset[1] != depthoffset)
802 gl_state.polygonoffset[0] = planeoffset;
803 gl_state.polygonoffset[1] = depthoffset;
804 qglPolygonOffset(planeoffset, depthoffset);
808 void GL_SetMirrorState(qboolean state)
810 if(!state != !v_flipped_state)
812 // change cull face mode!
813 if(gl_state.cullface == GL_BACK)
814 qglCullFace((gl_state.cullface = GL_FRONT));
815 else if(gl_state.cullface == GL_FRONT)
816 qglCullFace((gl_state.cullface = GL_BACK));
818 v_flipped_state = state;
821 void GL_CullFace(int state)
827 if(state == GL_FRONT)
829 else if(state == GL_BACK)
833 if (state != GL_NONE)
835 if (!gl_state.cullfaceenable)
837 gl_state.cullfaceenable = true;
838 qglEnable(GL_CULL_FACE);CHECKGLERROR
840 if (gl_state.cullface != state)
842 gl_state.cullface = state;
843 qglCullFace(gl_state.cullface);CHECKGLERROR
848 if (gl_state.cullfaceenable)
850 gl_state.cullfaceenable = false;
851 qglDisable(GL_CULL_FACE);CHECKGLERROR
856 void GL_AlphaTest(int state)
858 if (gl_state.alphatest != state)
860 gl_state.alphatest = state;
862 if (gl_state.alphatest)
864 qglEnable(GL_ALPHA_TEST);CHECKGLERROR
868 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
873 void GL_ColorMask(int r, int g, int b, int a)
875 int state = r*8 + g*4 + b*2 + a*1;
876 if (gl_state.colormask != state)
878 gl_state.colormask = state;
880 qglColorMask((GLboolean)r, (GLboolean)g, (GLboolean)b, (GLboolean)a);CHECKGLERROR
884 void GL_Color(float cr, float cg, float cb, float ca)
886 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)
888 gl_state.color4f[0] = cr;
889 gl_state.color4f[1] = cg;
890 gl_state.color4f[2] = cb;
891 gl_state.color4f[3] = ca;
893 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);
898 void GL_LockArrays(int first, int count)
900 if (count < gl_lockarrays_minimumvertices.integer)
905 if (gl_state.lockrange_count != count || gl_state.lockrange_first != first)
907 if (gl_state.lockrange_count)
909 gl_state.lockrange_count = 0;
911 qglUnlockArraysEXT();
914 if (count && gl_supportslockarrays && gl_lockarrays.integer && r_render.integer)
916 gl_state.lockrange_first = first;
917 gl_state.lockrange_count = count;
919 qglLockArraysEXT(first, count);
925 void GL_Scissor (int x, int y, int width, int height)
928 qglScissor(x, vid.height - (y + height),width,height);
932 void GL_ScissorTest(int state)
934 if(gl_state.scissortest == state)
938 if((gl_state.scissortest = state))
939 qglEnable(GL_SCISSOR_TEST);
941 qglDisable(GL_SCISSOR_TEST);
945 void GL_Clear(int mask)
948 qglClear(mask);CHECKGLERROR
951 void GL_TransformToScreen(const vec4_t in, vec4_t out)
955 Matrix4x4_Transform4 (&backend_viewmatrix, in, temp);
956 Matrix4x4_Transform4 (&backend_projectmatrix, temp, out);
958 out[0] = r_refdef.view.x + (out[0] * iw + 1.0f) * r_refdef.view.width * 0.5f;
959 out[1] = r_refdef.view.y + r_refdef.view.height - (out[1] * iw + 1.0f) * r_refdef.view.height * 0.5f;
960 out[2] = r_refdef.view.z + (out[2] * iw + 1.0f) * r_refdef.view.depth * 0.5f;
963 // called at beginning of frame
964 void R_Mesh_Start(void)
968 if (gl_printcheckerror.integer && !gl_paranoid.integer)
970 Con_Printf("WARNING: gl_printcheckerror is on but gl_paranoid is off, turning it on...\n");
971 Cvar_SetValueQuick(&gl_paranoid, 1);
973 GL_Backend_ResetState();
976 qboolean GL_Backend_CompileShader(int programobject, GLenum shadertypeenum, const char *shadertype, int numstrings, const char **strings)
980 char compilelog[MAX_INPUTLINE];
981 shaderobject = qglCreateShaderObjectARB(shadertypeenum);CHECKGLERROR
984 qglShaderSourceARB(shaderobject, numstrings, strings, NULL);CHECKGLERROR
985 qglCompileShaderARB(shaderobject);CHECKGLERROR
986 qglGetObjectParameterivARB(shaderobject, GL_OBJECT_COMPILE_STATUS_ARB, &shadercompiled);CHECKGLERROR
987 qglGetInfoLogARB(shaderobject, sizeof(compilelog), NULL, compilelog);CHECKGLERROR
988 if (compilelog[0] && developer.integer > 0)
990 int i, j, pretextlines = 0;
991 for (i = 0;i < numstrings - 1;i++)
992 for (j = 0;strings[i][j];j++)
993 if (strings[i][j] == '\n')
995 Con_DPrintf("%s shader compile log:\n%s\n(line offset for any above warnings/errors: %i)\n", shadertype, compilelog, pretextlines);
999 qglDeleteObjectARB(shaderobject);CHECKGLERROR
1002 qglAttachObjectARB(programobject, shaderobject);CHECKGLERROR
1003 qglDeleteObjectARB(shaderobject);CHECKGLERROR
1007 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)
1009 GLint programlinked;
1010 GLuint programobject = 0;
1011 char linklog[MAX_INPUTLINE];
1014 programobject = qglCreateProgramObjectARB();CHECKGLERROR
1018 if (vertexstrings_count && !GL_Backend_CompileShader(programobject, GL_VERTEX_SHADER_ARB, "vertex", vertexstrings_count, vertexstrings_list))
1021 #ifdef GL_GEOMETRY_SHADER_ARB
1022 if (geometrystrings_count && !GL_Backend_CompileShader(programobject, GL_GEOMETRY_SHADER_ARB, "geometry", geometrystrings_count, geometrystrings_list))
1026 if (fragmentstrings_count && !GL_Backend_CompileShader(programobject, GL_FRAGMENT_SHADER_ARB, "fragment", fragmentstrings_count, fragmentstrings_list))
1029 qglLinkProgramARB(programobject);CHECKGLERROR
1030 qglGetObjectParameterivARB(programobject, GL_OBJECT_LINK_STATUS_ARB, &programlinked);CHECKGLERROR
1031 qglGetInfoLogARB(programobject, sizeof(linklog), NULL, linklog);CHECKGLERROR
1034 Con_DPrintf("program link log:\n%s\n", linklog);
1035 // software vertex shader is ok but software fragment shader is WAY
1036 // too slow, fail program if so.
1037 // NOTE: this string might be ATI specific, but that's ok because the
1038 // ATI R300 chip (Radeon 9500-9800/X300) is the most likely to use a
1039 // software fragment shader due to low instruction and dependent
1041 if (strstr(linklog, "fragment shader will run in software"))
1042 programlinked = false;
1046 return programobject;
1048 qglDeleteObjectARB(programobject);CHECKGLERROR
1052 void GL_Backend_FreeProgram(unsigned int prog)
1055 qglDeleteObjectARB(prog);
1059 int gl_backend_rebindtextures;
1061 void GL_Backend_RenumberElements(int *out, int count, const int *in, int offset)
1066 for (i = 0;i < count;i++)
1067 *out++ = *in++ + offset;
1070 memcpy(out, in, sizeof(*out) * count);
1073 // renders triangles using vertices from the active arrays
1074 int paranoidblah = 0;
1075 void R_Mesh_Draw(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const unsigned short *element3s, int bufferobject3i, int bufferobject3s)
1077 unsigned int numelements = numtriangles * 3;
1078 if (numvertices < 3 || numtriangles < 1)
1080 Con_Printf("R_Mesh_Draw(%d, %d, %d, %d, %8p, %8p, %i, %i);\n", firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, bufferobject3i, bufferobject3s);
1083 if (!gl_mesh_prefer_short_elements.integer)
1091 element3i += firsttriangle * 3;
1093 element3s += firsttriangle * 3;
1094 switch (gl_vbo.integer)
1099 bufferobject3i = bufferobject3s = 0;
1105 bufferobject3i = bufferobject3s = 0;
1109 r_refdef.stats.meshes++;
1110 r_refdef.stats.meshes_elements += numelements;
1111 if (gl_paranoid.integer)
1113 unsigned int i, j, size;
1115 // note: there's no validation done here on buffer objects because it
1116 // is somewhat difficult to get at the data, and gl_paranoid can be
1117 // used without buffer objects if the need arises
1118 // (the data could be gotten using glMapBuffer but it would be very
1119 // slow due to uncachable video memory reads)
1120 if (!qglIsEnabled(GL_VERTEX_ARRAY))
1121 Con_Print("R_Mesh_Draw: vertex array not enabled\n");
1123 if (gl_state.pointer_vertex)
1124 for (j = 0, size = numvertices * 3, p = (int *)((float *)gl_state.pointer_vertex + firstvertex * 3);j < size;j++, p++)
1126 if (gl_state.pointer_color_enabled)
1128 if (!qglIsEnabled(GL_COLOR_ARRAY))
1129 Con_Print("R_Mesh_Draw: color array set but not enabled\n");
1131 if (gl_state.pointer_color && gl_state.pointer_color_enabled)
1132 for (j = 0, size = numvertices * 4, p = (int *)((float *)gl_state.pointer_color + firstvertex * 4);j < size;j++, p++)
1135 for (i = 0;i < backendarrayunits;i++)
1137 if (gl_state.units[i].arrayenabled)
1139 GL_ActiveTexture(i);
1140 if (!qglIsEnabled(GL_TEXTURE_COORD_ARRAY))
1141 Con_Print("R_Mesh_Draw: texcoord array set but not enabled\n");
1143 if (gl_state.units[i].pointer_texcoord && gl_state.units[i].arrayenabled)
1144 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++)
1150 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1152 if (element3i[i] < firstvertex || element3i[i] >= firstvertex + numvertices)
1154 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3i array\n", element3i[i], firstvertex, firstvertex + numvertices);
1161 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1163 if (element3s[i] < firstvertex || element3s[i] >= firstvertex + numvertices)
1165 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3s array\n", element3s[i], firstvertex, firstvertex + numvertices);
1172 if (r_render.integer)
1175 if (gl_mesh_testmanualfeeding.integer)
1177 unsigned int i, j, element;
1179 qglBegin(GL_TRIANGLES);
1180 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1182 element = element3i ? element3i[i] : element3s[i];
1183 for (j = 0;j < backendarrayunits;j++)
1185 if (gl_state.units[j].pointer_texcoord && gl_state.units[j].arrayenabled)
1187 if (backendarrayunits > 1)
1189 if (gl_state.units[j].arraycomponents == 4)
1191 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 4;
1192 qglMultiTexCoord4f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2], p[3]);
1194 else if (gl_state.units[j].arraycomponents == 3)
1196 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 3;
1197 qglMultiTexCoord3f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2]);
1199 else if (gl_state.units[j].arraycomponents == 2)
1201 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 2;
1202 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, p[0], p[1]);
1206 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 1;
1207 qglMultiTexCoord1f(GL_TEXTURE0_ARB + j, p[0]);
1212 if (gl_state.units[j].arraycomponents == 4)
1214 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 4;
1215 qglTexCoord4f(p[0], p[1], p[2], p[3]);
1217 else if (gl_state.units[j].arraycomponents == 3)
1219 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 3;
1220 qglTexCoord3f(p[0], p[1], p[2]);
1222 else if (gl_state.units[j].arraycomponents == 2)
1224 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 2;
1225 qglTexCoord2f(p[0], p[1]);
1229 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 1;
1230 qglTexCoord1f(p[0]);
1235 if (gl_state.pointer_color && gl_state.pointer_color_enabled)
1237 p = ((const GLfloat *)(gl_state.pointer_color)) + element * 4;
1238 qglColor4f(p[0], p[1], p[2], p[3]);
1240 p = ((const GLfloat *)(gl_state.pointer_vertex)) + element * 3;
1241 qglVertex3f(p[0], p[1], p[2]);
1246 else if (gl_mesh_testarrayelement.integer)
1249 qglBegin(GL_TRIANGLES);
1252 for (i = 0;i < numtriangles * 3;i++)
1253 qglArrayElement(element3i[i]);
1257 for (i = 0;i < numtriangles * 3;i++)
1258 qglArrayElement(element3s[i]);
1263 else if (bufferobject3s)
1265 GL_BindEBO(bufferobject3s);
1266 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1268 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices, numelements, GL_UNSIGNED_SHORT, (void *)(firsttriangle * sizeof(unsigned short[3])));
1273 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, (void *)(firsttriangle * sizeof(unsigned short[3])));
1277 else if (bufferobject3i)
1279 GL_BindEBO(bufferobject3i);
1280 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1282 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices, numelements, GL_UNSIGNED_INT, (void *)(firsttriangle * sizeof(unsigned int[3])));
1287 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, (void *)(firsttriangle * sizeof(unsigned int[3])));
1294 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1296 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices, numelements, GL_UNSIGNED_SHORT, element3s);
1301 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, element3s);
1308 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1310 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices, numelements, GL_UNSIGNED_INT, element3i);
1315 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, element3i);
1322 // restores backend state, used when done with 3D rendering
1323 void R_Mesh_Finish(void)
1328 GL_LockArrays(0, 0);
1331 for (i = 0;i < backendimageunits;i++)
1333 GL_ActiveTexture(i);
1334 qglBindTexture(GL_TEXTURE_1D, 0);CHECKGLERROR
1335 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
1338 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
1340 if (gl_texturecubemap)
1342 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
1345 for (i = 0;i < backendarrayunits;i++)
1347 GL_ActiveTexture(backendarrayunits - 1 - i);
1348 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1350 for (i = 0;i < backendunits;i++)
1352 GL_ActiveTexture(backendunits - 1 - i);
1353 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1354 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1357 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1359 if (gl_texturecubemap)
1361 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1363 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
1364 if (gl_combine.integer)
1366 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
1367 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
1370 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1371 qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
1373 qglDisable(GL_BLEND);CHECKGLERROR
1374 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1375 qglDepthMask(GL_TRUE);CHECKGLERROR
1376 qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);CHECKGLERROR
1379 int R_Mesh_CreateStaticBufferObject(unsigned int target, void *data, size_t size, const char *name)
1381 gl_bufferobjectinfo_t *info;
1382 GLuint bufferobject;
1384 if (!gl_vbo.integer)
1387 qglGenBuffersARB(1, &bufferobject);
1390 case GL_ELEMENT_ARRAY_BUFFER_ARB: GL_BindEBO(bufferobject);break;
1391 case GL_ARRAY_BUFFER_ARB: GL_BindVBO(bufferobject);break;
1392 default: Sys_Error("R_Mesh_CreateStaticBufferObject: unknown target type %i\n", target);return 0;
1394 qglBufferDataARB(target, size, data, GL_STATIC_DRAW_ARB);
1396 info = Mem_ExpandableArray_AllocRecord(&gl_bufferobjectinfoarray);
1397 memset(info, 0, sizeof(*info));
1398 info->target = target;
1399 info->object = bufferobject;
1401 strlcpy(info->name, name, sizeof(info->name));
1403 return (int)bufferobject;
1406 void R_Mesh_DestroyBufferObject(int bufferobject)
1409 gl_bufferobjectinfo_t *info;
1411 qglDeleteBuffersARB(1, (GLuint *)&bufferobject);
1413 endindex = Mem_ExpandableArray_IndexRange(&gl_bufferobjectinfoarray);
1414 for (i = 0;i < endindex;i++)
1416 info = Mem_ExpandableArray_RecordAtIndex(&gl_bufferobjectinfoarray, i);
1419 if (info->object == bufferobject)
1421 Mem_ExpandableArray_FreeRecord(&gl_bufferobjectinfoarray, (void *)info);
1427 void GL_Mesh_ListVBOs(qboolean printeach)
1430 size_t ebocount = 0, ebomemory = 0;
1431 size_t vbocount = 0, vbomemory = 0;
1432 gl_bufferobjectinfo_t *info;
1433 endindex = Mem_ExpandableArray_IndexRange(&gl_bufferobjectinfoarray);
1434 for (i = 0;i < endindex;i++)
1436 info = Mem_ExpandableArray_RecordAtIndex(&gl_bufferobjectinfoarray, i);
1439 switch(info->target)
1441 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;
1442 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;
1443 default: Con_Printf("gl_vbostats: unknown target type %i\n", info->target);break;
1446 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);
1449 void R_Mesh_Matrix(const matrix4x4_t *matrix)
1451 if (memcmp(matrix, &backend_modelmatrix, sizeof(matrix4x4_t)))
1453 double glmatrix[16];
1454 backend_modelmatrix = *matrix;
1455 Matrix4x4_Concat(&backend_modelviewmatrix, &backend_viewmatrix, matrix);
1456 Matrix4x4_ToArrayDoubleGL(&backend_modelviewmatrix, glmatrix);
1458 qglLoadMatrixd(glmatrix);CHECKGLERROR
1462 void R_Mesh_VertexPointer(const float *vertex3f, int bufferobject, size_t bufferoffset)
1464 if (!gl_vbo.integer)
1466 if (gl_state.pointer_vertex != vertex3f || gl_state.pointer_vertex_buffer != bufferobject || gl_state.pointer_vertex_offset != bufferoffset)
1468 gl_state.pointer_vertex = vertex3f;
1469 gl_state.pointer_vertex_buffer = bufferobject;
1470 gl_state.pointer_vertex_offset = bufferoffset;
1472 GL_BindVBO(bufferobject);
1473 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), bufferobject ? (void *)bufferoffset : vertex3f);CHECKGLERROR
1477 void R_Mesh_ColorPointer(const float *color4f, int bufferobject, size_t bufferoffset)
1479 // note: this can not rely on bufferobject to decide whether a color array
1480 // is supplied, because surfmesh_t shares one vbo for all arrays, which
1481 // means that a valid vbo may be supplied even if there is no color array.
1484 if (!gl_vbo.integer)
1486 // caller wants color array enabled
1487 if (!gl_state.pointer_color_enabled)
1489 gl_state.pointer_color_enabled = true;
1491 qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1493 if (gl_state.pointer_color != color4f || gl_state.pointer_color_buffer != bufferobject || gl_state.pointer_color_offset != bufferoffset)
1495 gl_state.pointer_color = color4f;
1496 gl_state.pointer_color_buffer = bufferobject;
1497 gl_state.pointer_color_offset = bufferoffset;
1499 GL_BindVBO(bufferobject);
1500 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), bufferobject ? (void *)bufferoffset : color4f);CHECKGLERROR
1505 // caller wants color array disabled
1506 if (gl_state.pointer_color_enabled)
1508 gl_state.pointer_color_enabled = false;
1510 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1511 // when color array is on the glColor gets trashed, set it again
1512 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);CHECKGLERROR
1517 void R_Mesh_TexCoordPointer(unsigned int unitnum, unsigned int numcomponents, const float *texcoord, int bufferobject, size_t bufferoffset)
1519 gltextureunit_t *unit = gl_state.units + unitnum;
1520 // update array settings
1522 // note: there is no need to check bufferobject here because all cases
1523 // that involve a valid bufferobject also supply a texcoord array
1526 if (!gl_vbo.integer)
1528 // texture array unit is enabled, enable the array
1529 if (!unit->arrayenabled)
1531 unit->arrayenabled = true;
1532 GL_ActiveTexture(unitnum);
1533 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1536 if (unit->pointer_texcoord != texcoord || unit->pointer_texcoord_buffer != bufferobject || unit->pointer_texcoord_offset != bufferoffset || unit->arraycomponents != numcomponents)
1538 unit->pointer_texcoord = texcoord;
1539 unit->pointer_texcoord_buffer = bufferobject;
1540 unit->pointer_texcoord_offset = bufferoffset;
1541 unit->arraycomponents = numcomponents;
1542 GL_ActiveTexture(unitnum);
1543 GL_BindVBO(bufferobject);
1544 qglTexCoordPointer(unit->arraycomponents, GL_FLOAT, sizeof(float) * unit->arraycomponents, bufferobject ? (void *)bufferoffset : texcoord);CHECKGLERROR
1549 // texture array unit is disabled, disable the array
1550 if (unit->arrayenabled)
1552 unit->arrayenabled = false;
1553 GL_ActiveTexture(unitnum);
1554 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1559 void R_Mesh_TexBindAll(unsigned int unitnum, int tex1d, int tex2d, int tex3d, int texcubemap)
1561 gltextureunit_t *unit = gl_state.units + unitnum;
1562 if (unitnum >= backendimageunits)
1564 // update 1d texture binding
1565 if (unit->t1d != tex1d)
1567 GL_ActiveTexture(unitnum);
1568 if (unitnum < backendunits)
1574 qglEnable(GL_TEXTURE_1D);CHECKGLERROR
1581 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1586 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1588 // update 2d texture binding
1589 if (unit->t2d != tex2d)
1591 GL_ActiveTexture(unitnum);
1592 if (unitnum < backendunits)
1598 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
1605 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1610 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1612 // update 3d texture binding
1613 if (unit->t3d != tex3d)
1615 GL_ActiveTexture(unitnum);
1616 if (unitnum < backendunits)
1622 qglEnable(GL_TEXTURE_3D);CHECKGLERROR
1629 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1634 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1636 // update cubemap texture binding
1637 if (unit->tcubemap != texcubemap)
1639 GL_ActiveTexture(unitnum);
1640 if (unitnum < backendunits)
1644 if (unit->tcubemap == 0)
1646 qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1653 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1657 unit->tcubemap = texcubemap;
1658 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1662 void R_Mesh_TexBind1D(unsigned int unitnum, int texnum)
1664 gltextureunit_t *unit = gl_state.units + unitnum;
1665 if (unitnum >= backendimageunits)
1667 // update 1d texture binding
1668 if (unit->t1d != texnum)
1670 GL_ActiveTexture(unitnum);
1671 if (unitnum < backendunits)
1677 qglEnable(GL_TEXTURE_1D);CHECKGLERROR
1684 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1689 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1691 // update 2d texture binding
1694 GL_ActiveTexture(unitnum);
1695 if (unitnum < backendunits)
1699 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1703 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1705 // update 3d texture binding
1708 GL_ActiveTexture(unitnum);
1709 if (unitnum < backendunits)
1713 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1717 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1719 // update cubemap texture binding
1722 GL_ActiveTexture(unitnum);
1723 if (unitnum < backendunits)
1727 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1731 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1735 void R_Mesh_TexBind(unsigned int unitnum, int texnum)
1737 gltextureunit_t *unit = gl_state.units + unitnum;
1738 if (unitnum >= backendimageunits)
1740 // update 1d texture binding
1743 GL_ActiveTexture(unitnum);
1744 if (unitnum < backendunits)
1748 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1752 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1754 // update 2d texture binding
1755 if (unit->t2d != texnum)
1757 GL_ActiveTexture(unitnum);
1758 if (unitnum < backendunits)
1764 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
1771 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1776 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1778 // update 3d texture binding
1781 GL_ActiveTexture(unitnum);
1782 if (unitnum < backendunits)
1786 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1790 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1792 // update cubemap texture binding
1793 if (unit->tcubemap != 0)
1795 GL_ActiveTexture(unitnum);
1796 if (unitnum < backendunits)
1800 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1804 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1808 void R_Mesh_TexBind3D(unsigned int unitnum, int texnum)
1810 gltextureunit_t *unit = gl_state.units + unitnum;
1811 if (unitnum >= backendimageunits)
1813 // update 1d texture binding
1816 GL_ActiveTexture(unitnum);
1817 if (unitnum < backendunits)
1821 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1825 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1827 // update 2d texture binding
1830 GL_ActiveTexture(unitnum);
1831 if (unitnum < backendunits)
1835 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1839 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1841 // update 3d texture binding
1842 if (unit->t3d != texnum)
1844 GL_ActiveTexture(unitnum);
1845 if (unitnum < backendunits)
1851 qglEnable(GL_TEXTURE_3D);CHECKGLERROR
1858 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1863 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1865 // update cubemap texture binding
1866 if (unit->tcubemap != 0)
1868 GL_ActiveTexture(unitnum);
1869 if (unitnum < backendunits)
1873 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1877 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1881 void R_Mesh_TexBindCubeMap(unsigned int unitnum, int texnum)
1883 gltextureunit_t *unit = gl_state.units + unitnum;
1884 if (unitnum >= backendimageunits)
1886 // update 1d texture binding
1889 GL_ActiveTexture(unitnum);
1890 if (unitnum < backendunits)
1894 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
1898 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
1900 // update 2d texture binding
1903 GL_ActiveTexture(unitnum);
1904 if (unitnum < backendunits)
1908 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1912 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1914 // update 3d texture binding
1917 GL_ActiveTexture(unitnum);
1918 if (unitnum < backendunits)
1922 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1926 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1928 // update cubemap texture binding
1929 if (unit->tcubemap != texnum)
1931 GL_ActiveTexture(unitnum);
1932 if (unitnum < backendunits)
1936 if (unit->tcubemap == 0)
1938 qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1945 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1949 unit->tcubemap = texnum;
1950 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1954 static const double gl_identitymatrix[16] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
1956 void R_Mesh_TexMatrix(unsigned int unitnum, const matrix4x4_t *matrix)
1958 gltextureunit_t *unit = gl_state.units + unitnum;
1959 if (matrix->m[3][3])
1961 // texmatrix specified, check if it is different
1962 if (!unit->texmatrixenabled || memcmp(&unit->matrix, matrix, sizeof(matrix4x4_t)))
1964 double glmatrix[16];
1965 unit->texmatrixenabled = true;
1966 unit->matrix = *matrix;
1968 Matrix4x4_ToArrayDoubleGL(&unit->matrix, glmatrix);
1969 GL_ActiveTexture(unitnum);
1970 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1971 if (gl_workaround_mac_texmatrix.integer & 4)
1973 qglActiveTexture(GL_TEXTURE0_ARB + gl_state.unit);
1974 qglClientActiveTexture(GL_TEXTURE0_ARB + gl_state.unit);
1976 qglLoadMatrixd(glmatrix);CHECKGLERROR
1977 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1982 // no texmatrix specified, revert to identity
1983 if (unit->texmatrixenabled)
1985 unit->texmatrixenabled = false;
1986 unit->matrix = identitymatrix;
1988 GL_ActiveTexture(unitnum);
1989 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1990 qglLoadIdentity();CHECKGLERROR
1991 if (gl_workaround_mac_texmatrix.integer)
1993 qglActiveTexture(GL_TEXTURE0_ARB + gl_state.unit);
1994 qglClientActiveTexture(GL_TEXTURE0_ARB + gl_state.unit);
1995 qglLoadMatrixd(gl_identitymatrix);CHECKGLERROR
1996 if (gl_workaround_mac_texmatrix.integer & 1)
1997 qglLoadIdentity();CHECKGLERROR
1999 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
2004 void R_Mesh_TexCombine(unsigned int unitnum, int combinergb, int combinealpha, int rgbscale, int alphascale)
2006 gltextureunit_t *unit = gl_state.units + unitnum;
2008 if (gl_combine.integer)
2010 // GL_ARB_texture_env_combine
2012 combinergb = GL_MODULATE;
2014 combinealpha = GL_MODULATE;
2019 if (unit->combinergb != combinergb)
2021 unit->combinergb = combinergb;
2022 GL_ActiveTexture(unitnum);
2023 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
2025 if (unit->combinealpha != combinealpha)
2027 unit->combinealpha = combinealpha;
2028 GL_ActiveTexture(unitnum);
2029 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
2031 if (unit->rgbscale != rgbscale)
2033 GL_ActiveTexture(unitnum);
2034 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (unit->rgbscale = rgbscale));CHECKGLERROR
2036 if (unit->alphascale != alphascale)
2038 GL_ActiveTexture(unitnum);
2039 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, (unit->alphascale = alphascale));CHECKGLERROR
2046 combinergb = GL_MODULATE;
2047 if (unit->combinergb != combinergb)
2049 unit->combinergb = combinergb;
2050 GL_ActiveTexture(unitnum);
2051 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combinergb);CHECKGLERROR
2056 void R_Mesh_TextureState(const rmeshstate_t *m)
2063 if (gl_backend_rebindtextures)
2065 gl_backend_rebindtextures = false;
2066 GL_SetupTextureState();
2070 for (i = 0;i < backendimageunits;i++)
2071 R_Mesh_TexBindAll(i, m->tex1d[i], m->tex[i], m->tex3d[i], m->texcubemap[i]);
2072 for (i = 0;i < backendarrayunits;i++)
2074 if (m->pointer_texcoord3f[i])
2075 R_Mesh_TexCoordPointer(i, 3, m->pointer_texcoord3f[i], m->pointer_texcoord_bufferobject[i], m->pointer_texcoord_bufferoffset[i]);
2077 R_Mesh_TexCoordPointer(i, 2, m->pointer_texcoord[i], m->pointer_texcoord_bufferobject[i], m->pointer_texcoord_bufferoffset[i]);
2079 for (i = 0;i < backendunits;i++)
2081 R_Mesh_TexMatrix(i, &m->texmatrix[i]);
2082 R_Mesh_TexCombine(i, m->texcombinergb[i], m->texcombinealpha[i], m->texrgbscale[i], m->texalphascale[i]);
2087 void R_Mesh_ResetTextureState(void)
2089 unsigned int unitnum;
2094 if (gl_backend_rebindtextures)
2096 gl_backend_rebindtextures = false;
2097 GL_SetupTextureState();
2101 for (unitnum = 0;unitnum < backendimageunits;unitnum++)
2103 gltextureunit_t *unit = gl_state.units + unitnum;
2104 // update 1d texture binding
2107 GL_ActiveTexture(unitnum);
2108 if (unitnum < backendunits)
2110 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
2113 qglBindTexture(GL_TEXTURE_1D, unit->t1d);CHECKGLERROR
2115 // update 2d texture binding
2118 GL_ActiveTexture(unitnum);
2119 if (unitnum < backendunits)
2121 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
2124 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
2126 // update 3d texture binding
2129 GL_ActiveTexture(unitnum);
2130 if (unitnum < backendunits)
2132 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
2135 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
2137 // update cubemap texture binding
2140 GL_ActiveTexture(unitnum);
2141 if (unitnum < backendunits)
2143 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
2146 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
2149 for (unitnum = 0;unitnum < backendarrayunits;unitnum++)
2151 gltextureunit_t *unit = gl_state.units + unitnum;
2152 // texture array unit is disabled, disable the array
2153 if (unit->arrayenabled)
2155 unit->arrayenabled = false;
2156 GL_ActiveTexture(unitnum);
2157 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
2160 for (unitnum = 0;unitnum < backendunits;unitnum++)
2162 gltextureunit_t *unit = gl_state.units + unitnum;
2163 // no texmatrix specified, revert to identity
2164 if (unit->texmatrixenabled)
2166 unit->texmatrixenabled = false;
2167 unit->matrix = identitymatrix;
2169 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
2170 GL_ActiveTexture(unitnum);
2171 qglLoadIdentity();CHECKGLERROR
2172 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
2174 if (gl_combine.integer)
2176 // GL_ARB_texture_env_combine
2177 if (unit->combinergb != GL_MODULATE)
2179 unit->combinergb = GL_MODULATE;
2180 GL_ActiveTexture(unitnum);
2181 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
2183 if (unit->combinealpha != GL_MODULATE)
2185 unit->combinealpha = GL_MODULATE;
2186 GL_ActiveTexture(unitnum);
2187 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
2189 if (unit->rgbscale != 1)
2191 GL_ActiveTexture(unitnum);
2192 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (unit->rgbscale = 1));CHECKGLERROR
2194 if (unit->alphascale != 1)
2196 GL_ActiveTexture(unitnum);
2197 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, (unit->alphascale = 1));CHECKGLERROR
2203 if (unit->combinergb != GL_MODULATE)
2205 unit->combinergb = GL_MODULATE;
2206 GL_ActiveTexture(unitnum);
2207 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combinergb);CHECKGLERROR