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_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)"};
18 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"};
20 cvar_t v_flipped = {0, "v_flipped", "0", "mirror the screen (poor man's left handed mode)"};
21 qboolean v_flipped_state = false;
23 r_viewport_t gl_viewport;
24 matrix4x4_t gl_modelmatrix;
25 matrix4x4_t gl_viewmatrix;
26 matrix4x4_t gl_modelviewmatrix;
27 matrix4x4_t gl_projectionmatrix;
28 matrix4x4_t gl_modelviewprojectionmatrix;
29 float gl_modelview16f[16];
30 float gl_modelviewprojection16f[16];
31 qboolean gl_modelmatrixchanged;
33 int gl_maxdrawrangeelementsvertices;
34 int gl_maxdrawrangeelementsindices;
39 void GL_PrintError(int errornumber, char *filename, int linenumber)
43 #ifdef GL_INVALID_ENUM
45 Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
48 #ifdef GL_INVALID_VALUE
49 case GL_INVALID_VALUE:
50 Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
53 #ifdef GL_INVALID_OPERATION
54 case GL_INVALID_OPERATION:
55 Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
58 #ifdef GL_STACK_OVERFLOW
59 case GL_STACK_OVERFLOW:
60 Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
63 #ifdef GL_STACK_UNDERFLOW
64 case GL_STACK_UNDERFLOW:
65 Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
68 #ifdef GL_OUT_OF_MEMORY
69 case GL_OUT_OF_MEMORY:
70 Con_Printf("GL_OUT_OF_MEMORY at %s:%i\n", filename, linenumber);
73 #ifdef GL_TABLE_TOO_LARGE
74 case GL_TABLE_TOO_LARGE:
75 Con_Printf("GL_TABLE_TOO_LARGE at %s:%i\n", filename, linenumber);
78 #ifdef GL_INVALID_FRAMEBUFFER_OPERATION_EXT
79 case GL_INVALID_FRAMEBUFFER_OPERATION_EXT:
80 Con_Printf("GL_INVALID_FRAMEBUFFER_OPERATION at %s:%i\n", filename, linenumber);
84 Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
90 #define BACKENDACTIVECHECK if (!gl_state.active) Sys_Error("GL backend function called when backend is not active");
92 void SCR_ScreenShot_f (void);
94 typedef struct gl_bufferobjectinfo_s
101 gl_bufferobjectinfo_t;
103 typedef struct gltextureunit_s
105 const void *pointer_texcoord;
106 size_t pointer_texcoord_offset;
107 int pointer_texcoord_buffer;
108 int t2d, t3d, tcubemap, trectangle;
110 unsigned int arraycomponents;
111 int rgbscale, alphascale;
113 int combinergb, combinealpha;
114 // texmatrixenabled exists only to avoid unnecessary texmatrix compares
115 int texmatrixenabled;
120 typedef struct gl_state_s
128 int colormask; // stored as bottom 4 bits: r g b a (3 2 1 0 order)
131 float polygonoffset[2];
135 unsigned int clientunit;
136 gltextureunit_t units[MAX_TEXTUREUNITS];
140 int vertexbufferobject;
141 int elementbufferobject;
142 qboolean pointer_color_enabled;
143 const void *pointer_vertex;
144 const void *pointer_color;
145 size_t pointer_vertex_offset;
146 size_t pointer_color_offset;
147 int pointer_vertex_buffer;
148 int pointer_color_buffer;
150 memexpandablearray_t bufferobjectinfoarray;
156 static gl_state_t gl_state;
160 note: here's strip order for a terrain row:
167 A0B, 01B, B1C, 12C, C2D, 23D, D3E, 34E
169 *elements++ = i + row;
171 *elements++ = i + row + 1;
174 *elements++ = i + row + 1;
177 for (y = 0;y < rows - 1;y++)
179 for (x = 0;x < columns - 1;x++)
182 *elements++ = i + columns;
184 *elements++ = i + columns + 1;
187 *elements++ = i + columns + 1;
198 for (y = 0;y < rows - 1;y++)
200 for (x = 0;x < columns - 1;x++)
204 *elements++ = i + columns;
205 *elements++ = i + columns + 1;
206 *elements++ = i + columns;
207 *elements++ = i + columns + 1;
213 int polygonelement3i[(POLYGONELEMENTS_MAXPOINTS-2)*3];
214 unsigned short polygonelement3s[(POLYGONELEMENTS_MAXPOINTS-2)*3];
215 int quadelement3i[QUADELEMENTS_MAXQUADS*6];
216 unsigned short quadelement3s[QUADELEMENTS_MAXQUADS*6];
218 void GL_VBOStats_f(void)
220 GL_Mesh_ListVBOs(true);
223 static void GL_Backend_ResetState(void);
225 static void gl_backend_start(void)
227 memset(&gl_state, 0, sizeof(gl_state));
229 Mem_ExpandableArray_NewArray(&gl_state.bufferobjectinfoarray, r_main_mempool, sizeof(gl_bufferobjectinfo_t), 128);
231 Con_DPrintf("OpenGL backend started.\n");
235 GL_Backend_ResetState();
238 static void gl_backend_shutdown(void)
240 Con_DPrint("OpenGL Backend shutting down\n");
242 Mem_ExpandableArray_FreeArray(&gl_state.bufferobjectinfoarray);
244 memset(&gl_state, 0, sizeof(gl_state));
247 static void gl_backend_newmap(void)
251 void gl_backend_init(void)
255 for (i = 0;i < POLYGONELEMENTS_MAXPOINTS - 2;i++)
257 polygonelement3s[i * 3 + 0] = 0;
258 polygonelement3s[i * 3 + 1] = i + 1;
259 polygonelement3s[i * 3 + 2] = i + 2;
261 // elements for rendering a series of quads as triangles
262 for (i = 0;i < QUADELEMENTS_MAXQUADS;i++)
264 quadelement3s[i * 6 + 0] = i * 4;
265 quadelement3s[i * 6 + 1] = i * 4 + 1;
266 quadelement3s[i * 6 + 2] = i * 4 + 2;
267 quadelement3s[i * 6 + 3] = i * 4;
268 quadelement3s[i * 6 + 4] = i * 4 + 2;
269 quadelement3s[i * 6 + 5] = i * 4 + 3;
272 for (i = 0;i < (POLYGONELEMENTS_MAXPOINTS - 2)*3;i++)
273 polygonelement3i[i] = polygonelement3s[i];
274 for (i = 0;i < QUADELEMENTS_MAXQUADS*3;i++)
275 quadelement3i[i] = quadelement3s[i];
277 Cvar_RegisterVariable(&r_render);
278 Cvar_RegisterVariable(&r_renderview);
279 Cvar_RegisterVariable(&r_waterwarp);
280 Cvar_RegisterVariable(&gl_polyblend);
281 Cvar_RegisterVariable(&v_flipped);
282 Cvar_RegisterVariable(&gl_dither);
283 Cvar_RegisterVariable(&gl_vbo);
284 Cvar_RegisterVariable(&gl_paranoid);
285 Cvar_RegisterVariable(&gl_printcheckerror);
287 Cvar_RegisterVariable(&gl_mesh_drawrangeelements);
288 Cvar_RegisterVariable(&gl_mesh_testarrayelement);
289 Cvar_RegisterVariable(&gl_mesh_testmanualfeeding);
290 Cvar_RegisterVariable(&gl_mesh_prefer_short_elements);
292 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");
294 R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap);
297 void GL_SetMirrorState(qboolean state);
299 void R_Viewport_TransformToScreen(const r_viewport_t *v, const vec4_t in, vec4_t out)
303 Matrix4x4_Transform4 (&v->viewmatrix, in, temp);
304 Matrix4x4_Transform4 (&v->projectmatrix, temp, out);
306 out[0] = v->x + (out[0] * iw + 1.0f) * v->width * 0.5f;
307 out[1] = v->y + v->height - (out[1] * iw + 1.0f) * v->height * 0.5f;
308 out[2] = v->z + (out[2] * iw + 1.0f) * v->depth * 0.5f;
311 static void R_Viewport_ApplyNearClipPlaneFloatGL(const r_viewport_t *v, float *m, float normalx, float normaly, float normalz, float dist)
315 float clipPlane[4], v3[3], v4[3];
318 // This is inspired by Oblique Depth Projection from http://www.terathon.com/code/oblique.php
320 VectorSet(normal, normalx, normaly, normalz);
321 Matrix4x4_Transform3x3(&v->viewmatrix, normal, clipPlane);
322 VectorScale(normal, dist, v3);
323 Matrix4x4_Transform(&v->viewmatrix, v3, v4);
324 // FIXME: LordHavoc: I think this can be done more efficiently somehow but I can't remember the technique
325 clipPlane[3] = -DotProduct(v4, clipPlane);
329 // testing code for comparing results
331 VectorCopy4(clipPlane, clipPlane2);
332 R_EntityMatrix(&identitymatrix);
333 VectorSet(q, normal[0], normal[1], normal[2], -dist);
334 qglClipPlane(GL_CLIP_PLANE0, q);
335 qglGetClipPlane(GL_CLIP_PLANE0, q);
336 VectorCopy4(q, clipPlane);
340 // Calculate the clip-space corner point opposite the clipping plane
341 // as (sgn(clipPlane.x), sgn(clipPlane.y), 1, 1) and
342 // transform it into camera space by multiplying it
343 // by the inverse of the projection matrix
344 q[0] = ((clipPlane[0] < 0.0f ? -1.0f : clipPlane[0] > 0.0f ? 1.0f : 0.0f) + m[8]) / m[0];
345 q[1] = ((clipPlane[1] < 0.0f ? -1.0f : clipPlane[1] > 0.0f ? 1.0f : 0.0f) + m[9]) / m[5];
347 q[3] = (1.0f + m[10]) / m[14];
349 // Calculate the scaled plane vector
350 d = 2.0f / DotProduct4(clipPlane, q);
352 // Replace the third row of the projection matrix
353 m[2] = clipPlane[0] * d;
354 m[6] = clipPlane[1] * d;
355 m[10] = clipPlane[2] * d + 1.0f;
356 m[14] = clipPlane[3] * d;
359 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)
361 float left = x1, right = x2, bottom = y2, top = y1, zNear = nearclip, zFar = farclip;
363 memset(v, 0, sizeof(*v));
364 v->type = R_VIEWPORTTYPE_ORTHO;
365 v->cameramatrix = *cameramatrix;
372 memset(m, 0, sizeof(m));
373 m[0] = 2/(right - left);
374 m[5] = 2/(top - bottom);
375 m[10] = -2/(zFar - zNear);
376 m[12] = - (right + left)/(right - left);
377 m[13] = - (top + bottom)/(top - bottom);
378 m[14] = - (zFar + zNear)/(zFar - zNear);
380 v->screentodepth[0] = -farclip / (farclip - nearclip);
381 v->screentodepth[1] = farclip * nearclip / (farclip - nearclip);
383 Matrix4x4_Invert_Full(&v->viewmatrix, &v->cameramatrix);
386 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
388 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
394 Vector4Set(test1, (x1+x2)*0.5f, (y1+y2)*0.5f, 0.0f, 1.0f);
395 R_Viewport_TransformToScreen(v, test1, test2);
396 Con_Printf("%f %f %f -> %f %f %f\n", test1[0], test1[1], test1[2], test2[0], test2[1], test2[2]);
401 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)
403 matrix4x4_t tempmatrix, basematrix;
405 memset(v, 0, sizeof(*v));
407 if(v_flipped.integer)
408 frustumx = -frustumx;
410 v->type = R_VIEWPORTTYPE_PERSPECTIVE;
411 v->cameramatrix = *cameramatrix;
418 memset(m, 0, sizeof(m));
419 m[0] = 1.0 / frustumx;
420 m[5] = 1.0 / frustumy;
421 m[10] = -(farclip + nearclip) / (farclip - nearclip);
423 m[14] = -2 * nearclip * farclip / (farclip - nearclip);
424 v->screentodepth[0] = -farclip / (farclip - nearclip);
425 v->screentodepth[1] = farclip * nearclip / (farclip - nearclip);
427 Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
428 Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
429 Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
430 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
433 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
435 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
438 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)
440 matrix4x4_t tempmatrix, basematrix;
441 const float nudge = 1.0 - 1.0 / (1<<23);
443 memset(v, 0, sizeof(*v));
445 if(v_flipped.integer)
446 frustumx = -frustumx;
448 v->type = R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP;
449 v->cameramatrix = *cameramatrix;
456 memset(m, 0, sizeof(m));
457 m[ 0] = 1.0 / frustumx;
458 m[ 5] = 1.0 / frustumy;
461 m[14] = -2 * nearclip * nudge;
462 v->screentodepth[0] = (m[10] + 1) * 0.5 - 1;
463 v->screentodepth[1] = m[14] * -0.5;
465 Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
466 Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
467 Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
468 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
471 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
473 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
476 float cubeviewmatrix[6][16] =
478 // standard cubemap projections
516 float rectviewmatrix[6][16] =
518 // sign-preserving cubemap projections
557 void R_Viewport_InitCubeSideView(r_viewport_t *v, const matrix4x4_t *cameramatrix, int side, int size, float nearclip, float farclip, const float *nearplane)
559 matrix4x4_t tempmatrix, basematrix;
561 memset(v, 0, sizeof(*v));
562 v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
563 v->cameramatrix = *cameramatrix;
567 memset(m, 0, sizeof(m));
569 m[10] = -(farclip + nearclip) / (farclip - nearclip);
571 m[14] = -2 * nearclip * farclip / (farclip - nearclip);
573 Matrix4x4_FromArrayFloatGL(&basematrix, cubeviewmatrix[side]);
574 Matrix4x4_Invert_Simple(&tempmatrix, &v->cameramatrix);
575 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
578 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
580 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
583 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)
585 matrix4x4_t tempmatrix, basematrix;
587 memset(v, 0, sizeof(*v));
588 v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
589 v->cameramatrix = *cameramatrix;
590 v->x = (side & 1) * size;
591 v->y = (side >> 1) * size;
595 memset(m, 0, sizeof(m));
596 m[0] = m[5] = 1.0f * ((float)size - border) / size;
597 m[10] = -(farclip + nearclip) / (farclip - nearclip);
599 m[14] = -2 * nearclip * farclip / (farclip - nearclip);
601 Matrix4x4_FromArrayFloatGL(&basematrix, rectviewmatrix[side]);
602 Matrix4x4_Invert_Simple(&tempmatrix, &v->cameramatrix);
603 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
606 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
608 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
611 void R_SetViewport(const r_viewport_t *v)
617 qglViewport(v->x, v->y, v->width, v->height);CHECKGLERROR
619 // FIXME: v_flipped_state is evil, this probably breaks somewhere
620 GL_SetMirrorState(v_flipped.integer && (v->type == R_VIEWPORTTYPE_PERSPECTIVE || v->type == R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP));
622 // copy over the matrices to our state
623 gl_viewmatrix = v->viewmatrix;
624 gl_projectionmatrix = v->projectmatrix;
626 switch(vid.renderpath)
628 case RENDERPATH_GL20:
629 case RENDERPATH_CGGL:
631 case RENDERPATH_GL13:
632 case RENDERPATH_GL11:
633 // Load the projection matrix into OpenGL
634 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
635 Matrix4x4_ToArrayFloatGL(&gl_projectionmatrix, m);
636 qglLoadMatrixf(m);CHECKGLERROR
637 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
641 // force an update of the derived matrices
642 gl_modelmatrixchanged = true;
643 R_EntityMatrix(&gl_modelmatrix);
646 void R_GetViewport(r_viewport_t *v)
651 static void GL_BindVBO(int bufferobject)
653 if (gl_state.vertexbufferobject != bufferobject)
655 gl_state.vertexbufferobject = bufferobject;
657 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, bufferobject);
662 static void GL_BindEBO(int bufferobject)
664 if (gl_state.elementbufferobject != bufferobject)
666 gl_state.elementbufferobject = bufferobject;
668 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, bufferobject);
673 static void GL_Backend_ResetState(void)
676 gl_state.active = true;
677 gl_state.depthtest = true;
678 gl_state.alphatest = false;
679 gl_state.blendfunc1 = GL_ONE;
680 gl_state.blendfunc2 = GL_ZERO;
681 gl_state.blend = false;
682 gl_state.depthmask = GL_TRUE;
683 gl_state.colormask = 15;
684 gl_state.color4f[0] = gl_state.color4f[1] = gl_state.color4f[2] = gl_state.color4f[3] = 1;
685 gl_state.lockrange_first = 0;
686 gl_state.lockrange_count = 0;
687 gl_state.cullface = v_flipped_state ? GL_BACK : GL_FRONT; // quake is backwards, this culls back faces
688 gl_state.cullfaceenable = true;
689 gl_state.polygonoffset[0] = 0;
690 gl_state.polygonoffset[1] = 0;
694 qglColorMask(1, 1, 1, 1);
695 qglAlphaFunc(GL_GEQUAL, 0.5);CHECKGLERROR
696 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
697 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
698 qglDisable(GL_BLEND);CHECKGLERROR
699 qglCullFace(gl_state.cullface);CHECKGLERROR
700 qglEnable(GL_CULL_FACE);CHECKGLERROR
701 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
702 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
703 qglDepthMask(gl_state.depthmask);CHECKGLERROR
704 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
706 if (vid.support.arb_vertex_buffer_object)
708 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
709 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
712 if (vid.support.ext_framebuffer_object)
714 qglBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
715 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
718 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), NULL);CHECKGLERROR
719 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
721 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL);CHECKGLERROR
722 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
724 GL_Color(0, 0, 0, 0);
725 GL_Color(1, 1, 1, 1);
727 gl_state.unit = MAX_TEXTUREUNITS;
728 gl_state.clientunit = MAX_TEXTUREUNITS;
729 switch(vid.renderpath)
731 case RENDERPATH_GL20:
732 case RENDERPATH_CGGL:
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
760 case RENDERPATH_GL13:
761 case RENDERPATH_GL11:
762 for (i = 0;i < vid.texunits;i++)
765 GL_ClientActiveTexture(i);
766 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
767 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
768 if (vid.support.ext_texture_3d)
770 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
771 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
773 if (vid.support.arb_texture_cube_map)
775 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
776 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
778 if (vid.support.arb_texture_rectangle)
780 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
781 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);CHECKGLERROR
784 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
785 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
786 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
787 qglLoadIdentity();CHECKGLERROR
788 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
789 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
796 void GL_ActiveTexture(unsigned int num)
798 if (gl_state.unit != num)
801 if (qglActiveTexture)
804 qglActiveTexture(GL_TEXTURE0_ARB + gl_state.unit);
810 void GL_ClientActiveTexture(unsigned int num)
812 if (gl_state.clientunit != num)
814 gl_state.clientunit = num;
815 if (qglActiveTexture)
818 qglClientActiveTexture(GL_TEXTURE0_ARB + gl_state.clientunit);
824 void GL_BlendFunc(int blendfunc1, int blendfunc2)
826 if (gl_state.blendfunc1 != blendfunc1 || gl_state.blendfunc2 != blendfunc2)
829 qglBlendFunc(gl_state.blendfunc1 = blendfunc1, gl_state.blendfunc2 = blendfunc2);CHECKGLERROR
830 if (gl_state.blendfunc2 == GL_ZERO)
832 if (gl_state.blendfunc1 == GL_ONE)
837 qglDisable(GL_BLEND);CHECKGLERROR
845 qglEnable(GL_BLEND);CHECKGLERROR
854 qglEnable(GL_BLEND);CHECKGLERROR
860 void GL_DepthMask(int state)
862 if (gl_state.depthmask != state)
865 qglDepthMask(gl_state.depthmask = state);CHECKGLERROR
869 void GL_DepthTest(int state)
871 if (gl_state.depthtest != state)
873 gl_state.depthtest = state;
875 if (gl_state.depthtest)
877 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
881 qglDisable(GL_DEPTH_TEST);CHECKGLERROR
886 void GL_DepthRange(float nearfrac, float farfrac)
888 if (gl_state.depthrange[0] != nearfrac || gl_state.depthrange[1] != farfrac)
890 gl_state.depthrange[0] = nearfrac;
891 gl_state.depthrange[1] = farfrac;
892 qglDepthRange(nearfrac, farfrac);
896 void GL_PolygonOffset(float planeoffset, float depthoffset)
898 if (gl_state.polygonoffset[0] != planeoffset || gl_state.polygonoffset[1] != depthoffset)
900 gl_state.polygonoffset[0] = planeoffset;
901 gl_state.polygonoffset[1] = depthoffset;
902 qglPolygonOffset(planeoffset, depthoffset);
906 void GL_SetMirrorState(qboolean state)
908 if(!state != !v_flipped_state)
910 // change cull face mode!
911 if(gl_state.cullface == GL_BACK)
912 qglCullFace((gl_state.cullface = GL_FRONT));
913 else if(gl_state.cullface == GL_FRONT)
914 qglCullFace((gl_state.cullface = GL_BACK));
916 v_flipped_state = state;
919 void GL_CullFace(int state)
925 if(state == GL_FRONT)
927 else if(state == GL_BACK)
931 if (state != GL_NONE)
933 if (!gl_state.cullfaceenable)
935 gl_state.cullfaceenable = true;
936 qglEnable(GL_CULL_FACE);CHECKGLERROR
938 if (gl_state.cullface != state)
940 gl_state.cullface = state;
941 qglCullFace(gl_state.cullface);CHECKGLERROR
946 if (gl_state.cullfaceenable)
948 gl_state.cullfaceenable = false;
949 qglDisable(GL_CULL_FACE);CHECKGLERROR
954 void GL_AlphaTest(int state)
956 if (gl_state.alphatest != state)
958 gl_state.alphatest = state;
960 if (gl_state.alphatest)
962 qglEnable(GL_ALPHA_TEST);CHECKGLERROR
966 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
971 void GL_ColorMask(int r, int g, int b, int a)
973 int state = r*8 + g*4 + b*2 + a*1;
974 if (gl_state.colormask != state)
976 gl_state.colormask = state;
978 qglColorMask((GLboolean)r, (GLboolean)g, (GLboolean)b, (GLboolean)a);CHECKGLERROR
982 void GL_Color(float cr, float cg, float cb, float ca)
984 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)
986 gl_state.color4f[0] = cr;
987 gl_state.color4f[1] = cg;
988 gl_state.color4f[2] = cb;
989 gl_state.color4f[3] = ca;
991 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);
996 void GL_Scissor (int x, int y, int width, int height)
999 qglScissor(x, y,width,height);
1003 void GL_ScissorTest(int state)
1005 if(gl_state.scissortest == state)
1009 if((gl_state.scissortest = state))
1010 qglEnable(GL_SCISSOR_TEST);
1012 qglDisable(GL_SCISSOR_TEST);
1016 void GL_Clear(int mask)
1019 qglClear(mask);CHECKGLERROR
1022 // called at beginning of frame
1023 void R_Mesh_Start(void)
1027 if (gl_printcheckerror.integer && !gl_paranoid.integer)
1029 Con_Printf("WARNING: gl_printcheckerror is on but gl_paranoid is off, turning it on...\n");
1030 Cvar_SetValueQuick(&gl_paranoid, 1);
1034 qboolean GL_Backend_CompileShader(int programobject, GLenum shadertypeenum, const char *shadertype, int numstrings, const char **strings)
1038 char compilelog[MAX_INPUTLINE];
1039 shaderobject = qglCreateShaderObjectARB(shadertypeenum);CHECKGLERROR
1042 qglShaderSourceARB(shaderobject, numstrings, strings, NULL);CHECKGLERROR
1043 qglCompileShaderARB(shaderobject);CHECKGLERROR
1044 qglGetObjectParameterivARB(shaderobject, GL_OBJECT_COMPILE_STATUS_ARB, &shadercompiled);CHECKGLERROR
1045 qglGetInfoLogARB(shaderobject, sizeof(compilelog), NULL, compilelog);CHECKGLERROR
1046 if (compilelog[0] && (strstr(compilelog, "error") || strstr(compilelog, "ERROR") || strstr(compilelog, "Error") || strstr(compilelog, "WARNING") || strstr(compilelog, "warning") || strstr(compilelog, "Warning")))
1048 int i, j, pretextlines = 0;
1049 for (i = 0;i < numstrings - 1;i++)
1050 for (j = 0;strings[i][j];j++)
1051 if (strings[i][j] == '\n')
1053 Con_Printf("%s shader compile log:\n%s\n(line offset for any above warnings/errors: %i)\n", shadertype, compilelog, pretextlines);
1055 if (!shadercompiled)
1057 qglDeleteObjectARB(shaderobject);CHECKGLERROR
1060 qglAttachObjectARB(programobject, shaderobject);CHECKGLERROR
1061 qglDeleteObjectARB(shaderobject);CHECKGLERROR
1065 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)
1067 GLint programlinked;
1068 GLuint programobject = 0;
1069 char linklog[MAX_INPUTLINE];
1072 programobject = qglCreateProgramObjectARB();CHECKGLERROR
1076 if (vertexstrings_count && !GL_Backend_CompileShader(programobject, GL_VERTEX_SHADER_ARB, "vertex", vertexstrings_count, vertexstrings_list))
1079 #ifdef GL_GEOMETRY_SHADER_ARB
1080 if (geometrystrings_count && !GL_Backend_CompileShader(programobject, GL_GEOMETRY_SHADER_ARB, "geometry", geometrystrings_count, geometrystrings_list))
1084 if (fragmentstrings_count && !GL_Backend_CompileShader(programobject, GL_FRAGMENT_SHADER_ARB, "fragment", fragmentstrings_count, fragmentstrings_list))
1087 qglLinkProgramARB(programobject);CHECKGLERROR
1088 qglGetObjectParameterivARB(programobject, GL_OBJECT_LINK_STATUS_ARB, &programlinked);CHECKGLERROR
1089 qglGetInfoLogARB(programobject, sizeof(linklog), NULL, linklog);CHECKGLERROR
1092 if (strstr(linklog, "error") || strstr(linklog, "ERROR") || strstr(linklog, "Error") || strstr(linklog, "WARNING") || strstr(linklog, "warning") || strstr(linklog, "Warning"))
1093 Con_DPrintf("program link log:\n%s\n", linklog);
1094 // software vertex shader is ok but software fragment shader is WAY
1095 // too slow, fail program if so.
1096 // NOTE: this string might be ATI specific, but that's ok because the
1097 // ATI R300 chip (Radeon 9500-9800/X300) is the most likely to use a
1098 // software fragment shader due to low instruction and dependent
1100 if (strstr(linklog, "fragment shader will run in software"))
1101 programlinked = false;
1105 return programobject;
1107 qglDeleteObjectARB(programobject);CHECKGLERROR
1111 void GL_Backend_FreeProgram(unsigned int prog)
1114 qglDeleteObjectARB(prog);
1118 void GL_Backend_RenumberElements(int *out, int count, const int *in, int offset)
1123 for (i = 0;i < count;i++)
1124 *out++ = *in++ + offset;
1127 memcpy(out, in, sizeof(*out) * count);
1130 // renders triangles using vertices from the active arrays
1131 int paranoidblah = 0;
1132 void R_Mesh_Draw(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const unsigned short *element3s, int bufferobject3i, int bufferobject3s)
1134 unsigned int numelements = numtriangles * 3;
1135 if (numvertices < 3 || numtriangles < 1)
1137 if (numvertices < 0 || numtriangles < 0 || developer_extra.integer)
1138 Con_DPrintf("R_Mesh_Draw(%d, %d, %d, %d, %8p, %8p, %i, %i);\n", firstvertex, numvertices, firsttriangle, numtriangles, (void *)element3i, (void *)element3s, bufferobject3i, bufferobject3s);
1141 if (!gl_mesh_prefer_short_elements.integer)
1149 element3i += firsttriangle * 3;
1151 element3s += firsttriangle * 3;
1152 switch (gl_vbo.integer)
1157 bufferobject3i = bufferobject3s = 0;
1163 bufferobject3i = bufferobject3s = 0;
1167 r_refdef.stats.meshes++;
1168 r_refdef.stats.meshes_elements += numelements;
1169 if (gl_paranoid.integer)
1171 unsigned int i, j, size;
1173 // note: there's no validation done here on buffer objects because it
1174 // is somewhat difficult to get at the data, and gl_paranoid can be
1175 // used without buffer objects if the need arises
1176 // (the data could be gotten using glMapBuffer but it would be very
1177 // slow due to uncachable video memory reads)
1178 if (!qglIsEnabled(GL_VERTEX_ARRAY))
1179 Con_Print("R_Mesh_Draw: vertex array not enabled\n");
1181 if (gl_state.pointer_vertex)
1182 for (j = 0, size = numvertices * 3, p = (int *)((float *)gl_state.pointer_vertex + firstvertex * 3);j < size;j++, p++)
1184 if (gl_state.pointer_color_enabled)
1186 if (!qglIsEnabled(GL_COLOR_ARRAY))
1187 Con_Print("R_Mesh_Draw: color array set but not enabled\n");
1189 if (gl_state.pointer_color && gl_state.pointer_color_enabled)
1190 for (j = 0, size = numvertices * 4, p = (int *)((float *)gl_state.pointer_color + firstvertex * 4);j < size;j++, p++)
1193 for (i = 0;i < vid.texarrayunits;i++)
1195 if (gl_state.units[i].arrayenabled)
1197 GL_ClientActiveTexture(i);
1198 if (!qglIsEnabled(GL_TEXTURE_COORD_ARRAY))
1199 Con_Print("R_Mesh_Draw: texcoord array set but not enabled\n");
1201 if (gl_state.units[i].pointer_texcoord && gl_state.units[i].arrayenabled)
1202 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++)
1208 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1210 if (element3i[i] < firstvertex || element3i[i] >= firstvertex + numvertices)
1212 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3i array\n", element3i[i], firstvertex, firstvertex + numvertices);
1219 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1221 if (element3s[i] < firstvertex || element3s[i] >= firstvertex + numvertices)
1223 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3s array\n", element3s[i], firstvertex, firstvertex + numvertices);
1230 if (r_render.integer || r_refdef.draw2dstage)
1233 if (gl_mesh_testmanualfeeding.integer)
1235 unsigned int i, j, element;
1237 qglBegin(GL_TRIANGLES);
1238 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1240 element = element3i ? element3i[i] : element3s[i];
1241 for (j = 0;j < vid.texarrayunits;j++)
1243 if (gl_state.units[j].pointer_texcoord && gl_state.units[j].arrayenabled)
1245 if (vid.texarrayunits > 1)
1247 if (gl_state.units[j].arraycomponents == 4)
1249 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 4;
1250 qglMultiTexCoord4f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2], p[3]);
1252 else if (gl_state.units[j].arraycomponents == 3)
1254 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 3;
1255 qglMultiTexCoord3f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2]);
1257 else if (gl_state.units[j].arraycomponents == 2)
1259 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 2;
1260 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, p[0], p[1]);
1264 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 1;
1265 qglMultiTexCoord1f(GL_TEXTURE0_ARB + j, p[0]);
1270 if (gl_state.units[j].arraycomponents == 4)
1272 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 4;
1273 qglTexCoord4f(p[0], p[1], p[2], p[3]);
1275 else if (gl_state.units[j].arraycomponents == 3)
1277 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 3;
1278 qglTexCoord3f(p[0], p[1], p[2]);
1280 else if (gl_state.units[j].arraycomponents == 2)
1282 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 2;
1283 qglTexCoord2f(p[0], p[1]);
1287 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 1;
1288 qglTexCoord1f(p[0]);
1293 if (gl_state.pointer_color && gl_state.pointer_color_enabled)
1295 p = ((const GLfloat *)(gl_state.pointer_color)) + element * 4;
1296 qglColor4f(p[0], p[1], p[2], p[3]);
1298 p = ((const GLfloat *)(gl_state.pointer_vertex)) + element * 3;
1299 qglVertex3f(p[0], p[1], p[2]);
1304 else if (gl_mesh_testarrayelement.integer)
1307 qglBegin(GL_TRIANGLES);
1310 for (i = 0;i < numtriangles * 3;i++)
1311 qglArrayElement(element3i[i]);
1315 for (i = 0;i < numtriangles * 3;i++)
1316 qglArrayElement(element3s[i]);
1321 else if (bufferobject3s)
1323 GL_BindEBO(bufferobject3s);
1324 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1326 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, (void *)(firsttriangle * sizeof(unsigned short[3])));
1331 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, (void *)(firsttriangle * sizeof(unsigned short[3])));
1335 else if (bufferobject3i)
1337 GL_BindEBO(bufferobject3i);
1338 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1340 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, (void *)(firsttriangle * sizeof(unsigned int[3])));
1345 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, (void *)(firsttriangle * sizeof(unsigned int[3])));
1352 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1354 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, element3s);
1359 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, element3s);
1366 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1368 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, element3i);
1373 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, element3i);
1380 // restores backend state, used when done with 3D rendering
1381 void R_Mesh_Finish(void)
1385 int R_Mesh_CreateStaticBufferObject(unsigned int target, void *data, size_t size, const char *name)
1387 gl_bufferobjectinfo_t *info;
1388 GLuint bufferobject;
1390 if (!gl_vbo.integer)
1393 qglGenBuffersARB(1, &bufferobject);
1396 case GL_ELEMENT_ARRAY_BUFFER_ARB: GL_BindEBO(bufferobject);break;
1397 case GL_ARRAY_BUFFER_ARB: GL_BindVBO(bufferobject);break;
1398 default: Sys_Error("R_Mesh_CreateStaticBufferObject: unknown target type %i\n", target);return 0;
1400 qglBufferDataARB(target, size, data, GL_STATIC_DRAW_ARB);
1402 info = (gl_bufferobjectinfo_t *) Mem_ExpandableArray_AllocRecord(&gl_state.bufferobjectinfoarray);
1403 memset(info, 0, sizeof(*info));
1404 info->target = target;
1405 info->object = bufferobject;
1407 strlcpy(info->name, name, sizeof(info->name));
1409 return (int)bufferobject;
1412 void R_Mesh_DestroyBufferObject(int bufferobject)
1415 gl_bufferobjectinfo_t *info;
1417 qglDeleteBuffersARB(1, (GLuint *)&bufferobject);
1419 endindex = Mem_ExpandableArray_IndexRange(&gl_state.bufferobjectinfoarray);
1420 for (i = 0;i < endindex;i++)
1422 info = (gl_bufferobjectinfo_t *) Mem_ExpandableArray_RecordAtIndex(&gl_state.bufferobjectinfoarray, i);
1425 if (info->object == bufferobject)
1427 Mem_ExpandableArray_FreeRecord(&gl_state.bufferobjectinfoarray, (void *)info);
1433 void GL_Mesh_ListVBOs(qboolean printeach)
1436 size_t ebocount = 0, ebomemory = 0;
1437 size_t vbocount = 0, vbomemory = 0;
1438 gl_bufferobjectinfo_t *info;
1439 endindex = Mem_ExpandableArray_IndexRange(&gl_state.bufferobjectinfoarray);
1440 for (i = 0;i < endindex;i++)
1442 info = (gl_bufferobjectinfo_t *) Mem_ExpandableArray_RecordAtIndex(&gl_state.bufferobjectinfoarray, i);
1445 switch(info->target)
1447 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;
1448 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;
1449 default: Con_Printf("gl_vbostats: unknown target type %i\n", info->target);break;
1452 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);
1455 void R_Mesh_VertexPointer(const float *vertex3f, int bufferobject, size_t bufferoffset)
1457 if (!gl_vbo.integer || gl_mesh_testarrayelement.integer)
1459 if (gl_state.pointer_vertex != vertex3f || gl_state.pointer_vertex_buffer != bufferobject || gl_state.pointer_vertex_offset != bufferoffset)
1461 gl_state.pointer_vertex = vertex3f;
1462 gl_state.pointer_vertex_buffer = bufferobject;
1463 gl_state.pointer_vertex_offset = bufferoffset;
1465 GL_BindVBO(bufferobject);
1466 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), bufferobject ? (void *)bufferoffset : vertex3f);CHECKGLERROR
1470 void R_Mesh_ColorPointer(const float *color4f, int bufferobject, size_t bufferoffset)
1472 // note: this can not rely on bufferobject to decide whether a color array
1473 // is supplied, because surfmesh_t shares one vbo for all arrays, which
1474 // means that a valid vbo may be supplied even if there is no color array.
1477 if (!gl_vbo.integer || gl_mesh_testarrayelement.integer)
1479 // caller wants color array enabled
1480 if (!gl_state.pointer_color_enabled)
1482 gl_state.pointer_color_enabled = true;
1484 qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1486 if (gl_state.pointer_color != color4f || gl_state.pointer_color_buffer != bufferobject || gl_state.pointer_color_offset != bufferoffset)
1488 gl_state.pointer_color = color4f;
1489 gl_state.pointer_color_buffer = bufferobject;
1490 gl_state.pointer_color_offset = bufferoffset;
1492 GL_BindVBO(bufferobject);
1493 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), bufferobject ? (void *)bufferoffset : color4f);CHECKGLERROR
1498 // caller wants color array disabled
1499 if (gl_state.pointer_color_enabled)
1501 gl_state.pointer_color_enabled = false;
1503 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1504 // when color array is on the glColor gets trashed, set it again
1505 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);CHECKGLERROR
1510 void R_Mesh_TexCoordPointer(unsigned int unitnum, unsigned int numcomponents, const float *texcoord, int bufferobject, size_t bufferoffset)
1512 gltextureunit_t *unit = gl_state.units + unitnum;
1513 // update array settings
1515 // note: there is no need to check bufferobject here because all cases
1516 // that involve a valid bufferobject also supply a texcoord array
1519 if (!gl_vbo.integer || gl_mesh_testarrayelement.integer)
1521 // texture array unit is enabled, enable the array
1522 if (!unit->arrayenabled)
1524 unit->arrayenabled = true;
1525 GL_ClientActiveTexture(unitnum);
1526 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1529 if (unit->pointer_texcoord != texcoord || unit->pointer_texcoord_buffer != bufferobject || unit->pointer_texcoord_offset != bufferoffset || unit->arraycomponents != numcomponents)
1531 unit->pointer_texcoord = texcoord;
1532 unit->pointer_texcoord_buffer = bufferobject;
1533 unit->pointer_texcoord_offset = bufferoffset;
1534 unit->arraycomponents = numcomponents;
1535 GL_ClientActiveTexture(unitnum);
1536 GL_BindVBO(bufferobject);
1537 qglTexCoordPointer(unit->arraycomponents, GL_FLOAT, sizeof(float) * unit->arraycomponents, bufferobject ? (void *)bufferoffset : texcoord);CHECKGLERROR
1542 // texture array unit is disabled, disable the array
1543 if (unit->arrayenabled)
1545 unit->arrayenabled = false;
1546 GL_ClientActiveTexture(unitnum);
1547 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1552 int R_Mesh_TexBound(unsigned int unitnum, int id)
1554 gltextureunit_t *unit = gl_state.units + unitnum;
1555 if (unitnum >= vid.teximageunits)
1557 if (id == GL_TEXTURE_2D)
1559 if (id == GL_TEXTURE_3D)
1561 if (id == GL_TEXTURE_CUBE_MAP_ARB)
1562 return unit->tcubemap;
1563 if (id == GL_TEXTURE_RECTANGLE_ARB)
1564 return unit->trectangle;
1568 void R_Mesh_CopyToTexture(rtexture_t *tex, int tx, int ty, int sx, int sy, int width, int height)
1570 R_Mesh_TexBind(0, tex);
1571 GL_ActiveTexture(0);CHECKGLERROR
1572 qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, tx, ty, sx, sy, width, height);CHECKGLERROR
1575 void R_Mesh_TexBind(unsigned int unitnum, rtexture_t *tex)
1577 gltextureunit_t *unit = gl_state.units + unitnum;
1578 int tex2d, tex3d, texcubemap, texnum;
1579 if (unitnum >= vid.teximageunits)
1581 switch(vid.renderpath)
1583 case RENDERPATH_GL20:
1584 case RENDERPATH_CGGL:
1586 tex = r_texture_white;
1587 texnum = R_GetTexture(tex);
1588 switch(tex->gltexturetypeenum)
1590 case GL_TEXTURE_2D: if (unit->t2d != texnum) {GL_ActiveTexture(unitnum);unit->t2d = texnum;qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR}break;
1591 case GL_TEXTURE_3D: if (unit->t3d != texnum) {GL_ActiveTexture(unitnum);unit->t3d = texnum;qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR}break;
1592 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;
1593 case GL_TEXTURE_RECTANGLE_ARB: if (unit->trectangle != texnum) {GL_ActiveTexture(unitnum);unit->trectangle = texnum;qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR}break;
1596 case RENDERPATH_GL13:
1597 case RENDERPATH_GL11:
1603 texnum = R_GetTexture(tex);
1604 switch(tex->gltexturetypeenum)
1612 case GL_TEXTURE_CUBE_MAP_ARB:
1613 texcubemap = texnum;
1617 // update 2d texture binding
1618 if (unit->t2d != tex2d)
1620 GL_ActiveTexture(unitnum);
1625 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
1632 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1636 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1638 // update 3d texture binding
1639 if (unit->t3d != tex3d)
1641 GL_ActiveTexture(unitnum);
1646 qglEnable(GL_TEXTURE_3D);CHECKGLERROR
1653 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1657 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1659 // update cubemap texture binding
1660 if (unit->tcubemap != texcubemap)
1662 GL_ActiveTexture(unitnum);
1665 if (unit->tcubemap == 0)
1667 qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1674 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1677 unit->tcubemap = texcubemap;
1678 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1684 void R_Mesh_TexMatrix(unsigned int unitnum, const matrix4x4_t *matrix)
1686 gltextureunit_t *unit = gl_state.units + unitnum;
1687 if (matrix && matrix->m[3][3])
1689 // texmatrix specified, check if it is different
1690 if (!unit->texmatrixenabled || memcmp(&unit->matrix, matrix, sizeof(matrix4x4_t)))
1693 unit->texmatrixenabled = true;
1694 unit->matrix = *matrix;
1696 Matrix4x4_ToArrayFloatGL(&unit->matrix, glmatrix);
1697 GL_ActiveTexture(unitnum);
1698 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1699 qglLoadMatrixf(glmatrix);CHECKGLERROR
1700 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1705 // no texmatrix specified, revert to identity
1706 if (unit->texmatrixenabled)
1708 unit->texmatrixenabled = false;
1709 unit->matrix = identitymatrix;
1711 GL_ActiveTexture(unitnum);
1712 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1713 qglLoadIdentity();CHECKGLERROR
1714 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1719 void R_Mesh_TexCombine(unsigned int unitnum, int combinergb, int combinealpha, int rgbscale, int alphascale)
1721 gltextureunit_t *unit = gl_state.units + unitnum;
1723 switch(vid.renderpath)
1725 case RENDERPATH_GL20:
1726 case RENDERPATH_CGGL:
1729 case RENDERPATH_GL13:
1730 // GL_ARB_texture_env_combine
1732 combinergb = GL_MODULATE;
1734 combinealpha = GL_MODULATE;
1739 if (combinergb != combinealpha || rgbscale != 1 || alphascale != 1)
1741 if (combinergb == GL_DECAL)
1742 combinergb = GL_INTERPOLATE_ARB;
1743 if (unit->combine != GL_COMBINE_ARB)
1745 unit->combine = GL_COMBINE_ARB;
1746 GL_ActiveTexture(unitnum);
1747 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);CHECKGLERROR
1748 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_TEXTURE);CHECKGLERROR // for GL_INTERPOLATE_ARB mode
1750 if (unit->combinergb != combinergb)
1752 unit->combinergb = combinergb;
1753 GL_ActiveTexture(unitnum);
1754 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
1756 if (unit->combinealpha != combinealpha)
1758 unit->combinealpha = combinealpha;
1759 GL_ActiveTexture(unitnum);
1760 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
1762 if (unit->rgbscale != rgbscale)
1764 unit->rgbscale = rgbscale;
1765 GL_ActiveTexture(unitnum);
1766 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, unit->rgbscale);CHECKGLERROR
1768 if (unit->alphascale != alphascale)
1770 unit->alphascale = alphascale;
1771 GL_ActiveTexture(unitnum);
1772 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, unit->alphascale);CHECKGLERROR
1777 if (unit->combine != combinergb)
1779 unit->combine = combinergb;
1780 GL_ActiveTexture(unitnum);
1781 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combine);CHECKGLERROR
1785 case RENDERPATH_GL11:
1788 combinergb = GL_MODULATE;
1789 if (unit->combine != combinergb)
1791 unit->combine = combinergb;
1792 GL_ActiveTexture(unitnum);
1793 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combine);CHECKGLERROR
1799 void R_Mesh_ResetTextureState(void)
1801 unsigned int unitnum;
1806 switch(vid.renderpath)
1808 case RENDERPATH_GL20:
1809 case RENDERPATH_CGGL:
1810 for (unitnum = 0;unitnum < vid.teximageunits;unitnum++)
1812 gltextureunit_t *unit = gl_state.units + unitnum;
1816 GL_ActiveTexture(unitnum);
1817 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1822 GL_ActiveTexture(unitnum);
1823 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1828 GL_ActiveTexture(unitnum);
1829 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1831 if (unit->trectangle)
1833 unit->trectangle = 0;
1834 GL_ActiveTexture(unitnum);
1835 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
1838 for (unitnum = 0;unitnum < vid.texarrayunits;unitnum++)
1840 gltextureunit_t *unit = gl_state.units + unitnum;
1841 if (unit->arrayenabled)
1843 unit->arrayenabled = false;
1844 GL_ClientActiveTexture(unitnum);
1845 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1848 for (unitnum = 0;unitnum < vid.texunits;unitnum++)
1850 gltextureunit_t *unit = gl_state.units + unitnum;
1851 if (unit->texmatrixenabled)
1853 unit->texmatrixenabled = false;
1854 unit->matrix = identitymatrix;
1856 GL_ActiveTexture(unitnum);
1857 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1858 qglLoadIdentity();CHECKGLERROR
1859 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1863 case RENDERPATH_GL13:
1864 case RENDERPATH_GL11:
1865 for (unitnum = 0;unitnum < vid.texunits;unitnum++)
1867 gltextureunit_t *unit = gl_state.units + unitnum;
1871 GL_ActiveTexture(unitnum);
1872 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1873 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1878 GL_ActiveTexture(unitnum);
1879 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1880 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1885 GL_ActiveTexture(unitnum);
1886 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1887 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1889 if (unit->trectangle)
1891 unit->trectangle = 0;
1892 GL_ActiveTexture(unitnum);
1893 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
1894 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
1896 if (unit->arrayenabled)
1898 unit->arrayenabled = false;
1899 GL_ClientActiveTexture(unitnum);
1900 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1902 if (unit->texmatrixenabled)
1904 unit->texmatrixenabled = false;
1905 unit->matrix = identitymatrix;
1907 GL_ActiveTexture(unitnum);
1908 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1909 qglLoadIdentity();CHECKGLERROR
1910 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1912 if (unit->combine != GL_MODULATE)
1914 unit->combine = GL_MODULATE;
1915 GL_ActiveTexture(unitnum);
1916 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combine);CHECKGLERROR