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 3D views (you want this on!)"};
13 cvar_t r_renderview = {0, "r_renderview", "1", "enables rendering 3D views (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", "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)"};
20 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"};
22 cvar_t v_flipped = {0, "v_flipped", "0", "mirror the screen (poor man's left handed mode)"};
23 qboolean v_flipped_state = false;
25 r_viewport_t gl_viewport;
26 matrix4x4_t gl_modelmatrix;
27 matrix4x4_t gl_viewmatrix;
28 matrix4x4_t gl_modelviewmatrix;
29 matrix4x4_t gl_projectionmatrix;
30 matrix4x4_t gl_modelviewprojectionmatrix;
31 float gl_modelview16f[16];
32 float gl_modelviewprojection16f[16];
33 qboolean gl_modelmatrixchanged;
35 int gl_maxdrawrangeelementsvertices;
36 int gl_maxdrawrangeelementsindices;
41 void GL_PrintError(int errornumber, char *filename, int linenumber)
45 #ifdef GL_INVALID_ENUM
47 Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
50 #ifdef GL_INVALID_VALUE
51 case GL_INVALID_VALUE:
52 Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
55 #ifdef GL_INVALID_OPERATION
56 case GL_INVALID_OPERATION:
57 Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
60 #ifdef GL_STACK_OVERFLOW
61 case GL_STACK_OVERFLOW:
62 Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
65 #ifdef GL_STACK_UNDERFLOW
66 case GL_STACK_UNDERFLOW:
67 Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
70 #ifdef GL_OUT_OF_MEMORY
71 case GL_OUT_OF_MEMORY:
72 Con_Printf("GL_OUT_OF_MEMORY at %s:%i\n", filename, linenumber);
75 #ifdef GL_TABLE_TOO_LARGE
76 case GL_TABLE_TOO_LARGE:
77 Con_Printf("GL_TABLE_TOO_LARGE at %s:%i\n", filename, linenumber);
80 #ifdef GL_INVALID_FRAMEBUFFER_OPERATION_EXT
81 case GL_INVALID_FRAMEBUFFER_OPERATION_EXT:
82 Con_Printf("GL_INVALID_FRAMEBUFFER_OPERATION at %s:%i\n", filename, linenumber);
86 Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
92 #define BACKENDACTIVECHECK if (!gl_state.active) Sys_Error("GL backend function called when backend is not active");
94 void SCR_ScreenShot_f (void);
96 typedef struct gl_bufferobjectinfo_s
101 char name[MAX_QPATH];
103 gl_bufferobjectinfo_t;
105 typedef struct gltextureunit_s
107 const void *pointer_texcoord;
108 size_t pointer_texcoord_offset;
109 int pointer_texcoord_buffer;
110 int t2d, t3d, tcubemap, trectangle;
112 unsigned int arraycomponents;
113 int rgbscale, alphascale;
115 int combinergb, combinealpha;
116 // texmatrixenabled exists only to avoid unnecessary texmatrix compares
117 int texmatrixenabled;
122 typedef struct gl_state_s
130 int colormask; // stored as bottom 4 bits: r g b a (3 2 1 0 order)
133 float polygonoffset[2];
137 unsigned int clientunit;
138 gltextureunit_t units[MAX_TEXTUREUNITS];
142 int vertexbufferobject;
143 int elementbufferobject;
144 qboolean pointer_color_enabled;
145 const void *pointer_vertex;
146 const void *pointer_color;
147 size_t pointer_vertex_offset;
148 size_t pointer_color_offset;
149 int pointer_vertex_buffer;
150 int pointer_color_buffer;
152 memexpandablearray_t bufferobjectinfoarray;
158 static gl_state_t gl_state;
162 note: here's strip order for a terrain row:
169 A0B, 01B, B1C, 12C, C2D, 23D, D3E, 34E
171 *elements++ = i + row;
173 *elements++ = i + row + 1;
176 *elements++ = i + row + 1;
179 for (y = 0;y < rows - 1;y++)
181 for (x = 0;x < columns - 1;x++)
184 *elements++ = i + columns;
186 *elements++ = i + columns + 1;
189 *elements++ = i + columns + 1;
200 for (y = 0;y < rows - 1;y++)
202 for (x = 0;x < columns - 1;x++)
206 *elements++ = i + columns;
207 *elements++ = i + columns + 1;
208 *elements++ = i + columns;
209 *elements++ = i + columns + 1;
215 int polygonelement3i[(POLYGONELEMENTS_MAXPOINTS-2)*3];
216 unsigned short polygonelement3s[(POLYGONELEMENTS_MAXPOINTS-2)*3];
217 int quadelement3i[QUADELEMENTS_MAXQUADS*6];
218 unsigned short quadelement3s[QUADELEMENTS_MAXQUADS*6];
220 void GL_VBOStats_f(void)
222 GL_Mesh_ListVBOs(true);
225 static void GL_Backend_ResetState(void);
227 static void gl_backend_start(void)
229 memset(&gl_state, 0, sizeof(gl_state));
231 Mem_ExpandableArray_NewArray(&gl_state.bufferobjectinfoarray, r_main_mempool, sizeof(gl_bufferobjectinfo_t), 128);
233 Con_DPrintf("OpenGL backend started.\n");
237 GL_Backend_ResetState();
240 static void gl_backend_shutdown(void)
242 Con_DPrint("OpenGL Backend shutting down\n");
244 Mem_ExpandableArray_FreeArray(&gl_state.bufferobjectinfoarray);
246 memset(&gl_state, 0, sizeof(gl_state));
249 static void gl_backend_newmap(void)
253 void gl_backend_init(void)
257 for (i = 0;i < POLYGONELEMENTS_MAXPOINTS - 2;i++)
259 polygonelement3s[i * 3 + 0] = 0;
260 polygonelement3s[i * 3 + 1] = i + 1;
261 polygonelement3s[i * 3 + 2] = i + 2;
263 // elements for rendering a series of quads as triangles
264 for (i = 0;i < QUADELEMENTS_MAXQUADS;i++)
266 quadelement3s[i * 6 + 0] = i * 4;
267 quadelement3s[i * 6 + 1] = i * 4 + 1;
268 quadelement3s[i * 6 + 2] = i * 4 + 2;
269 quadelement3s[i * 6 + 3] = i * 4;
270 quadelement3s[i * 6 + 4] = i * 4 + 2;
271 quadelement3s[i * 6 + 5] = i * 4 + 3;
274 for (i = 0;i < (POLYGONELEMENTS_MAXPOINTS - 2)*3;i++)
275 polygonelement3i[i] = polygonelement3s[i];
276 for (i = 0;i < QUADELEMENTS_MAXQUADS*3;i++)
277 quadelement3i[i] = quadelement3s[i];
279 Cvar_RegisterVariable(&r_render);
280 Cvar_RegisterVariable(&r_renderview);
281 Cvar_RegisterVariable(&r_waterwarp);
282 Cvar_RegisterVariable(&gl_polyblend);
283 Cvar_RegisterVariable(&v_flipped);
284 Cvar_RegisterVariable(&gl_dither);
285 Cvar_RegisterVariable(&gl_lockarrays);
286 Cvar_RegisterVariable(&gl_lockarrays_minimumvertices);
287 Cvar_RegisterVariable(&gl_vbo);
288 Cvar_RegisterVariable(&gl_paranoid);
289 Cvar_RegisterVariable(&gl_printcheckerror);
291 Cvar_RegisterVariable(&gl_mesh_drawrangeelements);
292 Cvar_RegisterVariable(&gl_mesh_testarrayelement);
293 Cvar_RegisterVariable(&gl_mesh_testmanualfeeding);
294 Cvar_RegisterVariable(&gl_mesh_prefer_short_elements);
296 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");
298 R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap);
301 void GL_SetMirrorState(qboolean state);
303 void R_Viewport_TransformToScreen(const r_viewport_t *v, const vec4_t in, vec4_t out)
307 Matrix4x4_Transform4 (&v->viewmatrix, in, temp);
308 Matrix4x4_Transform4 (&v->projectmatrix, temp, out);
310 out[0] = v->x + (out[0] * iw + 1.0f) * v->width * 0.5f;
311 out[1] = v->y + v->height - (out[1] * iw + 1.0f) * v->height * 0.5f;
312 out[2] = v->z + (out[2] * iw + 1.0f) * v->depth * 0.5f;
315 static void R_Viewport_ApplyNearClipPlaneFloatGL(const r_viewport_t *v, float *m, float normalx, float normaly, float normalz, float dist)
319 float clipPlane[4], v3[3], v4[3];
322 // This is inspired by Oblique Depth Projection from http://www.terathon.com/code/oblique.php
324 VectorSet(normal, normalx, normaly, normalz);
325 Matrix4x4_Transform3x3(&v->viewmatrix, normal, clipPlane);
326 VectorScale(normal, dist, v3);
327 Matrix4x4_Transform(&v->viewmatrix, v3, v4);
328 // FIXME: LordHavoc: I think this can be done more efficiently somehow but I can't remember the technique
329 clipPlane[3] = -DotProduct(v4, clipPlane);
333 // testing code for comparing results
335 VectorCopy4(clipPlane, clipPlane2);
336 R_EntityMatrix(&identitymatrix);
337 VectorSet(q, normal[0], normal[1], normal[2], -dist);
338 qglClipPlane(GL_CLIP_PLANE0, q);
339 qglGetClipPlane(GL_CLIP_PLANE0, q);
340 VectorCopy4(q, clipPlane);
344 // Calculate the clip-space corner point opposite the clipping plane
345 // as (sgn(clipPlane.x), sgn(clipPlane.y), 1, 1) and
346 // transform it into camera space by multiplying it
347 // by the inverse of the projection matrix
348 q[0] = ((clipPlane[0] < 0.0f ? -1.0f : clipPlane[0] > 0.0f ? 1.0f : 0.0f) + m[8]) / m[0];
349 q[1] = ((clipPlane[1] < 0.0f ? -1.0f : clipPlane[1] > 0.0f ? 1.0f : 0.0f) + m[9]) / m[5];
351 q[3] = (1.0f + m[10]) / m[14];
353 // Calculate the scaled plane vector
354 d = 2.0f / DotProduct4(clipPlane, q);
356 // Replace the third row of the projection matrix
357 m[2] = clipPlane[0] * d;
358 m[6] = clipPlane[1] * d;
359 m[10] = clipPlane[2] * d + 1.0f;
360 m[14] = clipPlane[3] * d;
363 void R_Viewport_InitOrtho(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, float x1, float y1, float x2, float y2, float nearclip, float farclip, const float *nearplane)
365 float left = x1, right = x2, bottom = y2, top = y1, zNear = nearclip, zFar = farclip;
367 memset(v, 0, sizeof(*v));
368 v->type = R_VIEWPORTTYPE_ORTHO;
369 v->cameramatrix = *cameramatrix;
376 memset(m, 0, sizeof(m));
377 m[0] = 2/(right - left);
378 m[5] = 2/(top - bottom);
379 m[10] = -2/(zFar - zNear);
380 m[12] = - (right + left)/(right - left);
381 m[13] = - (top + bottom)/(top - bottom);
382 m[14] = - (zFar + zNear)/(zFar - zNear);
384 v->screentodepth[0] = -farclip / (farclip - nearclip);
385 v->screentodepth[1] = farclip * nearclip / (farclip - nearclip);
387 Matrix4x4_Invert_Full(&v->viewmatrix, &v->cameramatrix);
390 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
392 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
398 Vector4Set(test1, (x1+x2)*0.5f, (y1+y2)*0.5f, 0.0f, 1.0f);
399 R_Viewport_TransformToScreen(v, test1, test2);
400 Con_Printf("%f %f %f -> %f %f %f\n", test1[0], test1[1], test1[2], test2[0], test2[1], test2[2]);
405 void R_Viewport_InitPerspective(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, float frustumx, float frustumy, float nearclip, float farclip, const float *nearplane)
407 matrix4x4_t tempmatrix, basematrix;
409 memset(v, 0, sizeof(*v));
411 if(v_flipped.integer)
412 frustumx = -frustumx;
414 v->type = R_VIEWPORTTYPE_PERSPECTIVE;
415 v->cameramatrix = *cameramatrix;
422 memset(m, 0, sizeof(m));
423 m[0] = 1.0 / frustumx;
424 m[5] = 1.0 / frustumy;
425 m[10] = -(farclip + nearclip) / (farclip - nearclip);
427 m[14] = -2 * nearclip * farclip / (farclip - nearclip);
428 v->screentodepth[0] = -farclip / (farclip - nearclip);
429 v->screentodepth[1] = farclip * nearclip / (farclip - nearclip);
431 Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
432 Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
433 Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
434 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
437 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
439 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
442 void R_Viewport_InitPerspectiveInfinite(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, float frustumx, float frustumy, float nearclip, const float *nearplane)
444 matrix4x4_t tempmatrix, basematrix;
445 const float nudge = 1.0 - 1.0 / (1<<23);
447 memset(v, 0, sizeof(*v));
449 if(v_flipped.integer)
450 frustumx = -frustumx;
452 v->type = R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP;
453 v->cameramatrix = *cameramatrix;
460 memset(m, 0, sizeof(m));
461 m[ 0] = 1.0 / frustumx;
462 m[ 5] = 1.0 / frustumy;
465 m[14] = -2 * nearclip * nudge;
466 v->screentodepth[0] = (m[10] + 1) * 0.5 - 1;
467 v->screentodepth[1] = m[14] * -0.5;
469 Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
470 Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
471 Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
472 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
475 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
477 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
480 float cubeviewmatrix[6][16] =
482 // standard cubemap projections
520 float rectviewmatrix[6][16] =
522 // sign-preserving cubemap projections
561 void R_Viewport_InitCubeSideView(r_viewport_t *v, const matrix4x4_t *cameramatrix, int side, int size, float nearclip, float farclip, const float *nearplane)
563 matrix4x4_t tempmatrix, basematrix;
565 memset(v, 0, sizeof(*v));
566 v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
567 v->cameramatrix = *cameramatrix;
571 memset(m, 0, sizeof(m));
573 m[10] = -(farclip + nearclip) / (farclip - nearclip);
575 m[14] = -2 * nearclip * farclip / (farclip - nearclip);
577 Matrix4x4_FromArrayFloatGL(&basematrix, cubeviewmatrix[side]);
578 Matrix4x4_Invert_Simple(&tempmatrix, &v->cameramatrix);
579 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
582 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
584 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
587 void R_Viewport_InitRectSideView(r_viewport_t *v, const matrix4x4_t *cameramatrix, int side, int size, int border, float nearclip, float farclip, const float *nearplane)
589 matrix4x4_t tempmatrix, basematrix;
591 memset(v, 0, sizeof(*v));
592 v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
593 v->cameramatrix = *cameramatrix;
594 v->x = (side & 1) * size;
595 v->y = (side >> 1) * size;
599 memset(m, 0, sizeof(m));
600 m[0] = m[5] = 1.0f * ((float)size - border) / size;
601 m[10] = -(farclip + nearclip) / (farclip - nearclip);
603 m[14] = -2 * nearclip * farclip / (farclip - nearclip);
605 Matrix4x4_FromArrayFloatGL(&basematrix, rectviewmatrix[side]);
606 Matrix4x4_Invert_Simple(&tempmatrix, &v->cameramatrix);
607 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
610 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
612 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
615 void R_SetViewport(const r_viewport_t *v)
621 qglViewport(v->x, v->y, v->width, v->height);CHECKGLERROR
623 // FIXME: v_flipped_state is evil, this probably breaks somewhere
624 GL_SetMirrorState(v_flipped.integer && (v->type == R_VIEWPORTTYPE_PERSPECTIVE || v->type == R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP));
626 // copy over the matrices to our state
627 gl_viewmatrix = v->viewmatrix;
628 gl_projectionmatrix = v->projectmatrix;
630 switch(vid.renderpath)
632 case RENDERPATH_GL20:
633 case RENDERPATH_CGGL:
635 case RENDERPATH_GL13:
636 case RENDERPATH_GL11:
637 // Load the projection matrix into OpenGL
638 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
639 Matrix4x4_ToArrayFloatGL(&gl_projectionmatrix, m);
640 qglLoadMatrixf(m);CHECKGLERROR
641 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
645 // force an update of the derived matrices
646 gl_modelmatrixchanged = true;
647 R_EntityMatrix(&gl_modelmatrix);
650 void R_GetViewport(r_viewport_t *v)
655 static void GL_BindVBO(int bufferobject)
657 if (gl_state.vertexbufferobject != bufferobject)
659 gl_state.vertexbufferobject = bufferobject;
661 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, bufferobject);
666 static void GL_BindEBO(int bufferobject)
668 if (gl_state.elementbufferobject != bufferobject)
670 gl_state.elementbufferobject = bufferobject;
672 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, bufferobject);
677 static void GL_Backend_ResetState(void)
680 gl_state.active = true;
681 gl_state.depthtest = true;
682 gl_state.alphatest = false;
683 gl_state.blendfunc1 = GL_ONE;
684 gl_state.blendfunc2 = GL_ZERO;
685 gl_state.blend = false;
686 gl_state.depthmask = GL_TRUE;
687 gl_state.colormask = 15;
688 gl_state.color4f[0] = gl_state.color4f[1] = gl_state.color4f[2] = gl_state.color4f[3] = 1;
689 gl_state.lockrange_first = 0;
690 gl_state.lockrange_count = 0;
691 gl_state.cullface = v_flipped_state ? GL_BACK : GL_FRONT; // quake is backwards, this culls back faces
692 gl_state.cullfaceenable = true;
693 gl_state.polygonoffset[0] = 0;
694 gl_state.polygonoffset[1] = 0;
698 qglColorMask(1, 1, 1, 1);
699 qglAlphaFunc(GL_GEQUAL, 0.5);CHECKGLERROR
700 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
701 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
702 qglDisable(GL_BLEND);CHECKGLERROR
703 qglCullFace(gl_state.cullface);CHECKGLERROR
704 qglEnable(GL_CULL_FACE);CHECKGLERROR
705 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
706 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
707 qglDepthMask(gl_state.depthmask);CHECKGLERROR
708 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
710 if (vid.support.arb_vertex_buffer_object)
712 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
713 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
716 if (vid.support.ext_framebuffer_object)
718 qglBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
719 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
722 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), NULL);CHECKGLERROR
723 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
725 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL);CHECKGLERROR
726 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
728 GL_Color(0, 0, 0, 0);
729 GL_Color(1, 1, 1, 1);
731 gl_state.unit = MAX_TEXTUREUNITS;
732 gl_state.clientunit = MAX_TEXTUREUNITS;
733 for (i = 0;i < vid.teximageunits;i++)
736 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
737 if (vid.support.ext_texture_3d)
739 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
741 if (vid.support.arb_texture_cube_map)
743 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
745 if (vid.support.arb_texture_rectangle)
747 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);CHECKGLERROR
751 for (i = 0;i < vid.texarrayunits;i++)
753 GL_ClientActiveTexture(i);
755 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
756 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
759 for (i = 0;i < vid.texunits;i++)
762 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
763 if (vid.support.ext_texture_3d)
765 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
767 if (vid.support.arb_texture_cube_map)
769 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
771 if (vid.support.arb_texture_rectangle)
773 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
775 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
776 qglLoadIdentity();CHECKGLERROR
777 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
778 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
784 void GL_ActiveTexture(unsigned int num)
786 if (gl_state.unit != num)
789 if (qglActiveTexture)
792 qglActiveTexture(GL_TEXTURE0_ARB + gl_state.unit);
798 void GL_ClientActiveTexture(unsigned int num)
800 if (gl_state.clientunit != num)
802 gl_state.clientunit = num;
803 if (qglActiveTexture)
806 qglClientActiveTexture(GL_TEXTURE0_ARB + gl_state.clientunit);
812 void GL_BlendFunc(int blendfunc1, int blendfunc2)
814 if (gl_state.blendfunc1 != blendfunc1 || gl_state.blendfunc2 != blendfunc2)
817 qglBlendFunc(gl_state.blendfunc1 = blendfunc1, gl_state.blendfunc2 = blendfunc2);CHECKGLERROR
818 if (gl_state.blendfunc2 == GL_ZERO)
820 if (gl_state.blendfunc1 == GL_ONE)
825 qglDisable(GL_BLEND);CHECKGLERROR
833 qglEnable(GL_BLEND);CHECKGLERROR
842 qglEnable(GL_BLEND);CHECKGLERROR
848 void GL_DepthMask(int state)
850 if (gl_state.depthmask != state)
853 qglDepthMask(gl_state.depthmask = state);CHECKGLERROR
857 void GL_DepthTest(int state)
859 if (gl_state.depthtest != state)
861 gl_state.depthtest = state;
863 if (gl_state.depthtest)
865 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
869 qglDisable(GL_DEPTH_TEST);CHECKGLERROR
874 void GL_DepthRange(float nearfrac, float farfrac)
876 if (gl_state.depthrange[0] != nearfrac || gl_state.depthrange[1] != farfrac)
878 gl_state.depthrange[0] = nearfrac;
879 gl_state.depthrange[1] = farfrac;
880 qglDepthRange(nearfrac, farfrac);
884 void GL_PolygonOffset(float planeoffset, float depthoffset)
886 if (gl_state.polygonoffset[0] != planeoffset || gl_state.polygonoffset[1] != depthoffset)
888 gl_state.polygonoffset[0] = planeoffset;
889 gl_state.polygonoffset[1] = depthoffset;
890 qglPolygonOffset(planeoffset, depthoffset);
894 void GL_SetMirrorState(qboolean state)
896 if(!state != !v_flipped_state)
898 // change cull face mode!
899 if(gl_state.cullface == GL_BACK)
900 qglCullFace((gl_state.cullface = GL_FRONT));
901 else if(gl_state.cullface == GL_FRONT)
902 qglCullFace((gl_state.cullface = GL_BACK));
904 v_flipped_state = state;
907 void GL_CullFace(int state)
913 if(state == GL_FRONT)
915 else if(state == GL_BACK)
919 if (state != GL_NONE)
921 if (!gl_state.cullfaceenable)
923 gl_state.cullfaceenable = true;
924 qglEnable(GL_CULL_FACE);CHECKGLERROR
926 if (gl_state.cullface != state)
928 gl_state.cullface = state;
929 qglCullFace(gl_state.cullface);CHECKGLERROR
934 if (gl_state.cullfaceenable)
936 gl_state.cullfaceenable = false;
937 qglDisable(GL_CULL_FACE);CHECKGLERROR
942 void GL_AlphaTest(int state)
944 if (gl_state.alphatest != state)
946 gl_state.alphatest = state;
948 if (gl_state.alphatest)
950 qglEnable(GL_ALPHA_TEST);CHECKGLERROR
954 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
959 void GL_ColorMask(int r, int g, int b, int a)
961 int state = r*8 + g*4 + b*2 + a*1;
962 if (gl_state.colormask != state)
964 gl_state.colormask = state;
966 qglColorMask((GLboolean)r, (GLboolean)g, (GLboolean)b, (GLboolean)a);CHECKGLERROR
970 void GL_Color(float cr, float cg, float cb, float ca)
972 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)
974 gl_state.color4f[0] = cr;
975 gl_state.color4f[1] = cg;
976 gl_state.color4f[2] = cb;
977 gl_state.color4f[3] = ca;
979 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);
984 void GL_LockArrays(int first, int count)
986 if (count < gl_lockarrays_minimumvertices.integer)
991 if (gl_state.lockrange_count != count || gl_state.lockrange_first != first)
993 if (gl_state.lockrange_count)
995 gl_state.lockrange_count = 0;
997 qglUnlockArraysEXT();
1000 if (count && vid.support.ext_compiled_vertex_array && gl_lockarrays.integer)
1002 gl_state.lockrange_first = first;
1003 gl_state.lockrange_count = count;
1005 qglLockArraysEXT(first, count);
1011 void GL_Scissor (int x, int y, int width, int height)
1014 qglScissor(x, y,width,height);
1018 void GL_ScissorTest(int state)
1020 if(gl_state.scissortest == state)
1024 if((gl_state.scissortest = state))
1025 qglEnable(GL_SCISSOR_TEST);
1027 qglDisable(GL_SCISSOR_TEST);
1031 void GL_Clear(int mask)
1034 qglClear(mask);CHECKGLERROR
1037 // called at beginning of frame
1038 void R_Mesh_Start(void)
1042 if (gl_printcheckerror.integer && !gl_paranoid.integer)
1044 Con_Printf("WARNING: gl_printcheckerror is on but gl_paranoid is off, turning it on...\n");
1045 Cvar_SetValueQuick(&gl_paranoid, 1);
1049 qboolean GL_Backend_CompileShader(int programobject, GLenum shadertypeenum, const char *shadertype, int numstrings, const char **strings)
1053 char compilelog[MAX_INPUTLINE];
1054 shaderobject = qglCreateShaderObjectARB(shadertypeenum);CHECKGLERROR
1057 qglShaderSourceARB(shaderobject, numstrings, strings, NULL);CHECKGLERROR
1058 qglCompileShaderARB(shaderobject);CHECKGLERROR
1059 qglGetObjectParameterivARB(shaderobject, GL_OBJECT_COMPILE_STATUS_ARB, &shadercompiled);CHECKGLERROR
1060 qglGetInfoLogARB(shaderobject, sizeof(compilelog), NULL, compilelog);CHECKGLERROR
1061 if (compilelog[0] && (strstr(compilelog, "error") || strstr(compilelog, "ERROR") || strstr(compilelog, "Error") || strstr(compilelog, "WARNING") || strstr(compilelog, "warning") || strstr(compilelog, "Warning")))
1063 int i, j, pretextlines = 0;
1064 for (i = 0;i < numstrings - 1;i++)
1065 for (j = 0;strings[i][j];j++)
1066 if (strings[i][j] == '\n')
1068 Con_Printf("%s shader compile log:\n%s\n(line offset for any above warnings/errors: %i)\n", shadertype, compilelog, pretextlines);
1070 if (!shadercompiled)
1072 qglDeleteObjectARB(shaderobject);CHECKGLERROR
1075 qglAttachObjectARB(programobject, shaderobject);CHECKGLERROR
1076 qglDeleteObjectARB(shaderobject);CHECKGLERROR
1080 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)
1082 GLint programlinked;
1083 GLuint programobject = 0;
1084 char linklog[MAX_INPUTLINE];
1087 programobject = qglCreateProgramObjectARB();CHECKGLERROR
1091 if (vertexstrings_count && !GL_Backend_CompileShader(programobject, GL_VERTEX_SHADER_ARB, "vertex", vertexstrings_count, vertexstrings_list))
1094 #ifdef GL_GEOMETRY_SHADER_ARB
1095 if (geometrystrings_count && !GL_Backend_CompileShader(programobject, GL_GEOMETRY_SHADER_ARB, "geometry", geometrystrings_count, geometrystrings_list))
1099 if (fragmentstrings_count && !GL_Backend_CompileShader(programobject, GL_FRAGMENT_SHADER_ARB, "fragment", fragmentstrings_count, fragmentstrings_list))
1102 qglLinkProgramARB(programobject);CHECKGLERROR
1103 qglGetObjectParameterivARB(programobject, GL_OBJECT_LINK_STATUS_ARB, &programlinked);CHECKGLERROR
1104 qglGetInfoLogARB(programobject, sizeof(linklog), NULL, linklog);CHECKGLERROR
1107 if (strstr(linklog, "error") || strstr(linklog, "ERROR") || strstr(linklog, "Error") || strstr(linklog, "WARNING") || strstr(linklog, "warning") || strstr(linklog, "Warning"))
1108 Con_DPrintf("program link log:\n%s\n", linklog);
1109 // software vertex shader is ok but software fragment shader is WAY
1110 // too slow, fail program if so.
1111 // NOTE: this string might be ATI specific, but that's ok because the
1112 // ATI R300 chip (Radeon 9500-9800/X300) is the most likely to use a
1113 // software fragment shader due to low instruction and dependent
1115 if (strstr(linklog, "fragment shader will run in software"))
1116 programlinked = false;
1120 return programobject;
1122 qglDeleteObjectARB(programobject);CHECKGLERROR
1126 void GL_Backend_FreeProgram(unsigned int prog)
1129 qglDeleteObjectARB(prog);
1133 void GL_Backend_RenumberElements(int *out, int count, const int *in, int offset)
1138 for (i = 0;i < count;i++)
1139 *out++ = *in++ + offset;
1142 memcpy(out, in, sizeof(*out) * count);
1145 // renders triangles using vertices from the active arrays
1146 int paranoidblah = 0;
1147 void R_Mesh_Draw(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const unsigned short *element3s, int bufferobject3i, int bufferobject3s)
1149 unsigned int numelements = numtriangles * 3;
1150 if (numvertices < 3 || numtriangles < 1)
1152 if (numvertices < 0 || numtriangles < 0 || developer_extra.integer)
1153 Con_DPrintf("R_Mesh_Draw(%d, %d, %d, %d, %8p, %8p, %i, %i);\n", firstvertex, numvertices, firsttriangle, numtriangles, (void *)element3i, (void *)element3s, bufferobject3i, bufferobject3s);
1156 if (!gl_mesh_prefer_short_elements.integer)
1164 element3i += firsttriangle * 3;
1166 element3s += firsttriangle * 3;
1167 switch (gl_vbo.integer)
1172 bufferobject3i = bufferobject3s = 0;
1178 bufferobject3i = bufferobject3s = 0;
1182 r_refdef.stats.meshes++;
1183 r_refdef.stats.meshes_elements += numelements;
1184 if (gl_paranoid.integer)
1186 unsigned int i, j, size;
1188 // note: there's no validation done here on buffer objects because it
1189 // is somewhat difficult to get at the data, and gl_paranoid can be
1190 // used without buffer objects if the need arises
1191 // (the data could be gotten using glMapBuffer but it would be very
1192 // slow due to uncachable video memory reads)
1193 if (!qglIsEnabled(GL_VERTEX_ARRAY))
1194 Con_Print("R_Mesh_Draw: vertex array not enabled\n");
1196 if (gl_state.pointer_vertex)
1197 for (j = 0, size = numvertices * 3, p = (int *)((float *)gl_state.pointer_vertex + firstvertex * 3);j < size;j++, p++)
1199 if (gl_state.pointer_color_enabled)
1201 if (!qglIsEnabled(GL_COLOR_ARRAY))
1202 Con_Print("R_Mesh_Draw: color array set but not enabled\n");
1204 if (gl_state.pointer_color && gl_state.pointer_color_enabled)
1205 for (j = 0, size = numvertices * 4, p = (int *)((float *)gl_state.pointer_color + firstvertex * 4);j < size;j++, p++)
1208 for (i = 0;i < vid.texarrayunits;i++)
1210 if (gl_state.units[i].arrayenabled)
1212 GL_ClientActiveTexture(i);
1213 if (!qglIsEnabled(GL_TEXTURE_COORD_ARRAY))
1214 Con_Print("R_Mesh_Draw: texcoord array set but not enabled\n");
1216 if (gl_state.units[i].pointer_texcoord && gl_state.units[i].arrayenabled)
1217 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++)
1223 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1225 if (element3i[i] < firstvertex || element3i[i] >= firstvertex + numvertices)
1227 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3i array\n", element3i[i], firstvertex, firstvertex + numvertices);
1234 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1236 if (element3s[i] < firstvertex || element3s[i] >= firstvertex + numvertices)
1238 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3s array\n", element3s[i], firstvertex, firstvertex + numvertices);
1245 if (r_render.integer || r_refdef.draw2dstage)
1248 if (gl_mesh_testmanualfeeding.integer)
1250 unsigned int i, j, element;
1252 qglBegin(GL_TRIANGLES);
1253 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1255 element = element3i ? element3i[i] : element3s[i];
1256 for (j = 0;j < vid.texarrayunits;j++)
1258 if (gl_state.units[j].pointer_texcoord && gl_state.units[j].arrayenabled)
1260 if (vid.texarrayunits > 1)
1262 if (gl_state.units[j].arraycomponents == 4)
1264 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 4;
1265 qglMultiTexCoord4f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2], p[3]);
1267 else if (gl_state.units[j].arraycomponents == 3)
1269 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 3;
1270 qglMultiTexCoord3f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2]);
1272 else if (gl_state.units[j].arraycomponents == 2)
1274 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 2;
1275 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, p[0], p[1]);
1279 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 1;
1280 qglMultiTexCoord1f(GL_TEXTURE0_ARB + j, p[0]);
1285 if (gl_state.units[j].arraycomponents == 4)
1287 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 4;
1288 qglTexCoord4f(p[0], p[1], p[2], p[3]);
1290 else if (gl_state.units[j].arraycomponents == 3)
1292 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 3;
1293 qglTexCoord3f(p[0], p[1], p[2]);
1295 else if (gl_state.units[j].arraycomponents == 2)
1297 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 2;
1298 qglTexCoord2f(p[0], p[1]);
1302 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 1;
1303 qglTexCoord1f(p[0]);
1308 if (gl_state.pointer_color && gl_state.pointer_color_enabled)
1310 p = ((const GLfloat *)(gl_state.pointer_color)) + element * 4;
1311 qglColor4f(p[0], p[1], p[2], p[3]);
1313 p = ((const GLfloat *)(gl_state.pointer_vertex)) + element * 3;
1314 qglVertex3f(p[0], p[1], p[2]);
1319 else if (gl_mesh_testarrayelement.integer)
1322 qglBegin(GL_TRIANGLES);
1325 for (i = 0;i < numtriangles * 3;i++)
1326 qglArrayElement(element3i[i]);
1330 for (i = 0;i < numtriangles * 3;i++)
1331 qglArrayElement(element3s[i]);
1336 else if (bufferobject3s)
1338 GL_BindEBO(bufferobject3s);
1339 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1341 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, (void *)(firsttriangle * sizeof(unsigned short[3])));
1346 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, (void *)(firsttriangle * sizeof(unsigned short[3])));
1350 else if (bufferobject3i)
1352 GL_BindEBO(bufferobject3i);
1353 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1355 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, (void *)(firsttriangle * sizeof(unsigned int[3])));
1360 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, (void *)(firsttriangle * sizeof(unsigned int[3])));
1367 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1369 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, element3s);
1374 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, element3s);
1381 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1383 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, element3i);
1388 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, element3i);
1395 // restores backend state, used when done with 3D rendering
1396 void R_Mesh_Finish(void)
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_state.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_state.bufferobjectinfoarray);
1435 for (i = 0;i < endindex;i++)
1437 info = (gl_bufferobjectinfo_t *) Mem_ExpandableArray_RecordAtIndex(&gl_state.bufferobjectinfoarray, i);
1440 if (info->object == bufferobject)
1442 Mem_ExpandableArray_FreeRecord(&gl_state.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_state.bufferobjectinfoarray);
1455 for (i = 0;i < endindex;i++)
1457 info = (gl_bufferobjectinfo_t *) Mem_ExpandableArray_RecordAtIndex(&gl_state.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_VertexPointer(const float *vertex3f, int bufferobject, size_t bufferoffset)
1472 if (!gl_vbo.integer || gl_mesh_testarrayelement.integer)
1474 if (gl_state.pointer_vertex != vertex3f || gl_state.pointer_vertex_buffer != bufferobject || gl_state.pointer_vertex_offset != bufferoffset)
1476 gl_state.pointer_vertex = vertex3f;
1477 gl_state.pointer_vertex_buffer = bufferobject;
1478 gl_state.pointer_vertex_offset = bufferoffset;
1480 GL_BindVBO(bufferobject);
1481 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), bufferobject ? (void *)bufferoffset : vertex3f);CHECKGLERROR
1485 void R_Mesh_ColorPointer(const float *color4f, int bufferobject, size_t bufferoffset)
1487 // note: this can not rely on bufferobject to decide whether a color array
1488 // is supplied, because surfmesh_t shares one vbo for all arrays, which
1489 // means that a valid vbo may be supplied even if there is no color array.
1492 if (!gl_vbo.integer || gl_mesh_testarrayelement.integer)
1494 // caller wants color array enabled
1495 if (!gl_state.pointer_color_enabled)
1497 gl_state.pointer_color_enabled = true;
1499 qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1501 if (gl_state.pointer_color != color4f || gl_state.pointer_color_buffer != bufferobject || gl_state.pointer_color_offset != bufferoffset)
1503 gl_state.pointer_color = color4f;
1504 gl_state.pointer_color_buffer = bufferobject;
1505 gl_state.pointer_color_offset = bufferoffset;
1507 GL_BindVBO(bufferobject);
1508 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), bufferobject ? (void *)bufferoffset : color4f);CHECKGLERROR
1513 // caller wants color array disabled
1514 if (gl_state.pointer_color_enabled)
1516 gl_state.pointer_color_enabled = false;
1518 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1519 // when color array is on the glColor gets trashed, set it again
1520 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);CHECKGLERROR
1525 void R_Mesh_TexCoordPointer(unsigned int unitnum, unsigned int numcomponents, const float *texcoord, int bufferobject, size_t bufferoffset)
1527 gltextureunit_t *unit = gl_state.units + unitnum;
1528 // update array settings
1530 // note: there is no need to check bufferobject here because all cases
1531 // that involve a valid bufferobject also supply a texcoord array
1534 if (!gl_vbo.integer || gl_mesh_testarrayelement.integer)
1536 // texture array unit is enabled, enable the array
1537 if (!unit->arrayenabled)
1539 unit->arrayenabled = true;
1540 GL_ClientActiveTexture(unitnum);
1541 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1544 if (unit->pointer_texcoord != texcoord || unit->pointer_texcoord_buffer != bufferobject || unit->pointer_texcoord_offset != bufferoffset || unit->arraycomponents != numcomponents)
1546 unit->pointer_texcoord = texcoord;
1547 unit->pointer_texcoord_buffer = bufferobject;
1548 unit->pointer_texcoord_offset = bufferoffset;
1549 unit->arraycomponents = numcomponents;
1550 GL_ClientActiveTexture(unitnum);
1551 GL_BindVBO(bufferobject);
1552 qglTexCoordPointer(unit->arraycomponents, GL_FLOAT, sizeof(float) * unit->arraycomponents, bufferobject ? (void *)bufferoffset : texcoord);CHECKGLERROR
1557 // texture array unit is disabled, disable the array
1558 if (unit->arrayenabled)
1560 unit->arrayenabled = false;
1561 GL_ClientActiveTexture(unitnum);
1562 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1567 int R_Mesh_TexBound(unsigned int unitnum, int id)
1569 gltextureunit_t *unit = gl_state.units + unitnum;
1570 if (unitnum >= vid.teximageunits)
1572 if (id == GL_TEXTURE_2D)
1574 if (id == GL_TEXTURE_3D)
1576 if (id == GL_TEXTURE_CUBE_MAP_ARB)
1577 return unit->tcubemap;
1578 if (id == GL_TEXTURE_RECTANGLE_ARB)
1579 return unit->trectangle;
1583 void R_Mesh_CopyToTexture(rtexture_t *tex, int tx, int ty, int sx, int sy, int width, int height)
1585 R_Mesh_TexBind(0, tex);
1586 GL_ActiveTexture(0);CHECKGLERROR
1587 qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, tx, ty, sx, sy, width, height);CHECKGLERROR
1590 void R_Mesh_TexBind(unsigned int unitnum, rtexture_t *tex)
1592 gltextureunit_t *unit = gl_state.units + unitnum;
1593 int tex2d, tex3d, texcubemap, texnum;
1594 if (unitnum >= vid.teximageunits)
1596 switch(vid.renderpath)
1598 case RENDERPATH_GL20:
1599 case RENDERPATH_CGGL:
1601 tex = r_texture_white;
1602 texnum = R_GetTexture(tex);
1603 switch(tex->gltexturetypeenum)
1605 case GL_TEXTURE_2D: if (unit->t2d != texnum) {GL_ActiveTexture(unitnum);unit->t2d = texnum;qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR}break;
1606 case GL_TEXTURE_3D: if (unit->t3d != texnum) {GL_ActiveTexture(unitnum);unit->t3d = texnum;qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR}break;
1607 case GL_TEXTURE_CUBE_MAP_ARB: if (unit->tcubemap != texnum) {GL_ActiveTexture(unitnum);unit->tcubemap = texnum;qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR}break;
1608 case GL_TEXTURE_RECTANGLE_ARB: if (unit->trectangle != texnum) {GL_ActiveTexture(unitnum);unit->trectangle = texnum;qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR}break;
1611 case RENDERPATH_GL13:
1612 case RENDERPATH_GL11:
1618 texnum = R_GetTexture(tex);
1619 switch(tex->gltexturetypeenum)
1627 case GL_TEXTURE_CUBE_MAP_ARB:
1628 texcubemap = texnum;
1632 // update 2d texture binding
1633 if (unit->t2d != tex2d)
1635 GL_ActiveTexture(unitnum);
1640 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
1647 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1651 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1653 // update 3d texture binding
1654 if (unit->t3d != tex3d)
1656 GL_ActiveTexture(unitnum);
1661 qglEnable(GL_TEXTURE_3D);CHECKGLERROR
1668 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1672 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1674 // update cubemap texture binding
1675 if (unit->tcubemap != texcubemap)
1677 GL_ActiveTexture(unitnum);
1680 if (unit->tcubemap == 0)
1682 qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1689 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1692 unit->tcubemap = texcubemap;
1693 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1699 static const float gl_identitymatrix[16] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
1701 void R_Mesh_TexMatrix(unsigned int unitnum, const matrix4x4_t *matrix)
1703 gltextureunit_t *unit = gl_state.units + unitnum;
1704 if (matrix && matrix->m[3][3])
1706 // texmatrix specified, check if it is different
1707 if (!unit->texmatrixenabled || memcmp(&unit->matrix, matrix, sizeof(matrix4x4_t)))
1710 unit->texmatrixenabled = true;
1711 unit->matrix = *matrix;
1713 Matrix4x4_ToArrayFloatGL(&unit->matrix, glmatrix);
1714 GL_ActiveTexture(unitnum);
1715 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1716 qglLoadMatrixf(glmatrix);CHECKGLERROR
1717 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1722 // no texmatrix specified, revert to identity
1723 if (unit->texmatrixenabled)
1725 unit->texmatrixenabled = false;
1726 unit->matrix = identitymatrix;
1728 GL_ActiveTexture(unitnum);
1729 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1730 qglLoadIdentity();CHECKGLERROR
1731 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1736 void R_Mesh_TexCombine(unsigned int unitnum, int combinergb, int combinealpha, int rgbscale, int alphascale)
1738 gltextureunit_t *unit = gl_state.units + unitnum;
1740 switch(vid.renderpath)
1742 case RENDERPATH_GL20:
1743 case RENDERPATH_CGGL:
1746 case RENDERPATH_GL13:
1747 // GL_ARB_texture_env_combine
1749 combinergb = GL_MODULATE;
1751 combinealpha = GL_MODULATE;
1756 if (combinergb != combinealpha || rgbscale != 1 || alphascale != 1)
1758 if (combinergb == GL_DECAL)
1759 combinergb = GL_INTERPOLATE_ARB;
1760 if (unit->combine != GL_COMBINE_ARB)
1762 unit->combine = GL_COMBINE_ARB;
1763 GL_ActiveTexture(unitnum);
1764 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);CHECKGLERROR
1765 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_TEXTURE);CHECKGLERROR // for GL_INTERPOLATE_ARB mode
1767 if (unit->combinergb != combinergb)
1769 unit->combinergb = combinergb;
1770 GL_ActiveTexture(unitnum);
1771 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
1773 if (unit->combinealpha != combinealpha)
1775 unit->combinealpha = combinealpha;
1776 GL_ActiveTexture(unitnum);
1777 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
1779 if (unit->rgbscale != rgbscale)
1781 unit->rgbscale = rgbscale;
1782 GL_ActiveTexture(unitnum);
1783 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, unit->rgbscale);CHECKGLERROR
1785 if (unit->alphascale != alphascale)
1787 unit->alphascale = alphascale;
1788 GL_ActiveTexture(unitnum);
1789 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, unit->alphascale);CHECKGLERROR
1794 if (unit->combine != combinergb)
1796 unit->combine = combinergb;
1797 GL_ActiveTexture(unitnum);
1798 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combine);CHECKGLERROR
1802 case RENDERPATH_GL11:
1805 combinergb = GL_MODULATE;
1806 if (unit->combine != combinergb)
1808 unit->combine = combinergb;
1809 GL_ActiveTexture(unitnum);
1810 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combine);CHECKGLERROR
1816 void R_Mesh_ResetTextureState(void)
1818 unsigned int unitnum;
1823 switch(vid.renderpath)
1825 case RENDERPATH_GL20:
1826 case RENDERPATH_CGGL:
1827 for (unitnum = 0;unitnum < vid.teximageunits;unitnum++)
1829 gltextureunit_t *unit = gl_state.units + unitnum;
1833 GL_ActiveTexture(unitnum);
1834 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1839 GL_ActiveTexture(unitnum);
1840 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1845 GL_ActiveTexture(unitnum);
1846 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1848 if (unit->trectangle)
1850 unit->trectangle = 0;
1851 GL_ActiveTexture(unitnum);
1852 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
1855 for (unitnum = 0;unitnum < vid.texarrayunits;unitnum++)
1857 gltextureunit_t *unit = gl_state.units + unitnum;
1858 if (unit->arrayenabled)
1860 unit->arrayenabled = false;
1861 GL_ClientActiveTexture(unitnum);
1862 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1865 for (unitnum = 0;unitnum < vid.texunits;unitnum++)
1867 gltextureunit_t *unit = gl_state.units + unitnum;
1868 if (unit->texmatrixenabled)
1870 unit->texmatrixenabled = false;
1871 unit->matrix = identitymatrix;
1873 GL_ActiveTexture(unitnum);
1874 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1875 qglLoadIdentity();CHECKGLERROR
1876 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1880 case RENDERPATH_GL13:
1881 case RENDERPATH_GL11:
1882 for (unitnum = 0;unitnum < vid.texunits;unitnum++)
1884 gltextureunit_t *unit = gl_state.units + unitnum;
1888 GL_ActiveTexture(unitnum);
1889 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1890 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1895 GL_ActiveTexture(unitnum);
1896 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1897 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1902 GL_ActiveTexture(unitnum);
1903 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1904 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1906 if (unit->trectangle)
1908 unit->trectangle = 0;
1909 GL_ActiveTexture(unitnum);
1910 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
1911 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
1913 if (unit->arrayenabled)
1915 unit->arrayenabled = false;
1916 GL_ClientActiveTexture(unitnum);
1917 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1919 if (unit->texmatrixenabled)
1921 unit->texmatrixenabled = false;
1922 unit->matrix = identitymatrix;
1924 GL_ActiveTexture(unitnum);
1925 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1926 qglLoadIdentity();CHECKGLERROR
1927 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1929 if (unit->combine != GL_MODULATE)
1931 unit->combine = GL_MODULATE;
1932 GL_ActiveTexture(unitnum);
1933 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combine);CHECKGLERROR