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 int gl_maxdrawrangeelementsvertices;
26 int gl_maxdrawrangeelementsindices;
31 void GL_PrintError(int errornumber, char *filename, int linenumber)
35 #ifdef GL_INVALID_ENUM
37 Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
40 #ifdef GL_INVALID_VALUE
41 case GL_INVALID_VALUE:
42 Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
45 #ifdef GL_INVALID_OPERATION
46 case GL_INVALID_OPERATION:
47 Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
50 #ifdef GL_STACK_OVERFLOW
51 case GL_STACK_OVERFLOW:
52 Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
55 #ifdef GL_STACK_UNDERFLOW
56 case GL_STACK_UNDERFLOW:
57 Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
60 #ifdef GL_OUT_OF_MEMORY
61 case GL_OUT_OF_MEMORY:
62 Con_Printf("GL_OUT_OF_MEMORY at %s:%i\n", filename, linenumber);
65 #ifdef GL_TABLE_TOO_LARGE
66 case GL_TABLE_TOO_LARGE:
67 Con_Printf("GL_TABLE_TOO_LARGE at %s:%i\n", filename, linenumber);
70 #ifdef GL_INVALID_FRAMEBUFFER_OPERATION_EXT
71 case GL_INVALID_FRAMEBUFFER_OPERATION_EXT:
72 Con_Printf("GL_INVALID_FRAMEBUFFER_OPERATION at %s:%i\n", filename, linenumber);
76 Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
82 #define BACKENDACTIVECHECK if (!gl_state.active) Sys_Error("GL backend function called when backend is not active");
84 void SCR_ScreenShot_f (void);
86 typedef struct gl_bufferobjectinfo_s
93 gl_bufferobjectinfo_t;
95 typedef struct gltextureunit_s
97 const void *pointer_texcoord;
98 size_t pointer_texcoord_offset;
99 int pointer_texcoord_buffer;
100 int t2d, t3d, tcubemap, trectangle;
102 unsigned int arraycomponents;
103 int rgbscale, alphascale;
105 int combinergb, combinealpha;
106 // texmatrixenabled exists only to avoid unnecessary texmatrix compares
107 int texmatrixenabled;
112 typedef struct gl_state_s
120 int colormask; // stored as bottom 4 bits: r g b a (3 2 1 0 order)
123 float polygonoffset[2];
127 unsigned int clientunit;
128 gltextureunit_t units[MAX_TEXTUREUNITS];
132 int vertexbufferobject;
133 int elementbufferobject;
134 qboolean pointer_color_enabled;
135 const void *pointer_vertex;
136 const void *pointer_color;
137 size_t pointer_vertex_offset;
138 size_t pointer_color_offset;
139 int pointer_vertex_buffer;
140 int pointer_color_buffer;
142 memexpandablearray_t bufferobjectinfoarray;
144 r_viewport_t viewport;
145 matrix4x4_t modelmatrix;
146 matrix4x4_t modelviewmatrix;
152 static gl_state_t gl_state;
156 note: here's strip order for a terrain row:
163 A0B, 01B, B1C, 12C, C2D, 23D, D3E, 34E
165 *elements++ = i + row;
167 *elements++ = i + row + 1;
170 *elements++ = i + row + 1;
173 for (y = 0;y < rows - 1;y++)
175 for (x = 0;x < columns - 1;x++)
178 *elements++ = i + columns;
180 *elements++ = i + columns + 1;
183 *elements++ = i + columns + 1;
194 for (y = 0;y < rows - 1;y++)
196 for (x = 0;x < columns - 1;x++)
200 *elements++ = i + columns;
201 *elements++ = i + columns + 1;
202 *elements++ = i + columns;
203 *elements++ = i + columns + 1;
209 int polygonelement3i[(POLYGONELEMENTS_MAXPOINTS-2)*3];
210 unsigned short polygonelement3s[(POLYGONELEMENTS_MAXPOINTS-2)*3];
211 int quadelement3i[QUADELEMENTS_MAXQUADS*6];
212 unsigned short quadelement3s[QUADELEMENTS_MAXQUADS*6];
214 void GL_VBOStats_f(void)
216 GL_Mesh_ListVBOs(true);
219 static void GL_Backend_ResetState(void);
221 static void gl_backend_start(void)
223 memset(&gl_state, 0, sizeof(gl_state));
225 Mem_ExpandableArray_NewArray(&gl_state.bufferobjectinfoarray, r_main_mempool, sizeof(gl_bufferobjectinfo_t), 128);
227 Con_DPrintf("OpenGL backend started.\n");
231 GL_Backend_ResetState();
234 static void gl_backend_shutdown(void)
236 Con_DPrint("OpenGL Backend shutting down\n");
238 Mem_ExpandableArray_FreeArray(&gl_state.bufferobjectinfoarray);
240 memset(&gl_state, 0, sizeof(gl_state));
243 static void gl_backend_newmap(void)
247 void gl_backend_init(void)
251 for (i = 0;i < POLYGONELEMENTS_MAXPOINTS - 2;i++)
253 polygonelement3s[i * 3 + 0] = 0;
254 polygonelement3s[i * 3 + 1] = i + 1;
255 polygonelement3s[i * 3 + 2] = i + 2;
257 // elements for rendering a series of quads as triangles
258 for (i = 0;i < QUADELEMENTS_MAXQUADS;i++)
260 quadelement3s[i * 6 + 0] = i * 4;
261 quadelement3s[i * 6 + 1] = i * 4 + 1;
262 quadelement3s[i * 6 + 2] = i * 4 + 2;
263 quadelement3s[i * 6 + 3] = i * 4;
264 quadelement3s[i * 6 + 4] = i * 4 + 2;
265 quadelement3s[i * 6 + 5] = i * 4 + 3;
268 for (i = 0;i < (POLYGONELEMENTS_MAXPOINTS - 2)*3;i++)
269 polygonelement3i[i] = polygonelement3s[i];
270 for (i = 0;i < QUADELEMENTS_MAXQUADS*3;i++)
271 quadelement3i[i] = quadelement3s[i];
273 Cvar_RegisterVariable(&r_render);
274 Cvar_RegisterVariable(&r_renderview);
275 Cvar_RegisterVariable(&r_waterwarp);
276 Cvar_RegisterVariable(&gl_polyblend);
277 Cvar_RegisterVariable(&v_flipped);
278 Cvar_RegisterVariable(&gl_dither);
279 Cvar_RegisterVariable(&gl_lockarrays);
280 Cvar_RegisterVariable(&gl_lockarrays_minimumvertices);
281 Cvar_RegisterVariable(&gl_vbo);
282 Cvar_RegisterVariable(&gl_paranoid);
283 Cvar_RegisterVariable(&gl_printcheckerror);
285 Cvar_RegisterVariable(&gl_mesh_drawrangeelements);
286 Cvar_RegisterVariable(&gl_mesh_testarrayelement);
287 Cvar_RegisterVariable(&gl_mesh_testmanualfeeding);
288 Cvar_RegisterVariable(&gl_mesh_prefer_short_elements);
290 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");
292 R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap);
295 void GL_SetMirrorState(qboolean state);
297 void R_Viewport_TransformToScreen(const r_viewport_t *v, const vec4_t in, vec4_t out)
301 Matrix4x4_Transform4 (&v->viewmatrix, in, temp);
302 Matrix4x4_Transform4 (&v->projectmatrix, temp, out);
304 out[0] = v->x + (out[0] * iw + 1.0f) * v->width * 0.5f;
305 out[1] = v->y + v->height - (out[1] * iw + 1.0f) * v->height * 0.5f;
306 out[2] = v->z + (out[2] * iw + 1.0f) * v->depth * 0.5f;
309 static void R_Viewport_ApplyNearClipPlane(r_viewport_t *v, double normalx, double normaly, double normalz, double dist)
313 float clipPlane[4], v3[3], v4[3];
316 // This is inspired by Oblique Depth Projection from http://www.terathon.com/code/oblique.php
318 VectorSet(normal, normalx, normaly, normalz);
319 Matrix4x4_Transform3x3(&v->viewmatrix, normal, clipPlane);
320 VectorScale(normal, dist, v3);
321 Matrix4x4_Transform(&v->viewmatrix, v3, v4);
322 // FIXME: LordHavoc: I think this can be done more efficiently somehow but I can't remember the technique
323 clipPlane[3] = -DotProduct(v4, clipPlane);
327 // testing code for comparing results
329 VectorCopy4(clipPlane, clipPlane2);
330 R_Mesh_Matrix(&identitymatrix);
331 VectorSet(q, normal[0], normal[1], normal[2], -dist);
332 qglClipPlane(GL_CLIP_PLANE0, q);
333 qglGetClipPlane(GL_CLIP_PLANE0, q);
334 VectorCopy4(q, clipPlane);
338 // Calculate the clip-space corner point opposite the clipping plane
339 // as (sgn(clipPlane.x), sgn(clipPlane.y), 1, 1) and
340 // transform it into camera space by multiplying it
341 // by the inverse of the projection matrix
342 q[0] = ((clipPlane[0] < 0.0f ? -1.0f : clipPlane[0] > 0.0f ? 1.0f : 0.0f) + v->m[8]) / v->m[0];
343 q[1] = ((clipPlane[1] < 0.0f ? -1.0f : clipPlane[1] > 0.0f ? 1.0f : 0.0f) + v->m[9]) / v->m[5];
345 q[3] = (1.0f + v->m[10]) / v->m[14];
347 // Calculate the scaled plane vector
348 d = 2.0f / DotProduct4(clipPlane, q);
350 // Replace the third row of the projection matrix
351 v->m[2] = clipPlane[0] * d;
352 v->m[6] = clipPlane[1] * d;
353 v->m[10] = clipPlane[2] * d + 1.0f;
354 v->m[14] = clipPlane[3] * d;
357 void R_Viewport_InitOrtho(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, double x1, double y1, double x2, double y2, double nearclip, double farclip, const double *nearplane)
359 float left = x1, right = x2, bottom = y2, top = y1, zNear = nearclip, zFar = farclip;
360 memset(v, 0, sizeof(*v));
361 v->type = R_VIEWPORTTYPE_ORTHO;
362 v->cameramatrix = *cameramatrix;
369 v->m[0] = 2/(right - left);
370 v->m[5] = 2/(top - bottom);
371 v->m[10] = -2/(zFar - zNear);
372 v->m[12] = - (right + left)/(right - left);
373 v->m[13] = - (top + bottom)/(top - bottom);
374 v->m[14] = - (zFar + zNear)/(zFar - zNear);
377 Matrix4x4_Invert_Full(&v->viewmatrix, &v->cameramatrix);
378 Matrix4x4_FromArrayDoubleGL(&v->projectmatrix, v->m);
381 R_Viewport_ApplyNearClipPlane(v, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
387 Vector4Set(test1, (x1+x2)*0.5f, (y1+y2)*0.5f, 0.0f, 1.0f);
388 R_Viewport_TransformToScreen(v, test1, test2);
389 Con_Printf("%f %f %f -> %f %f %f\n", test1[0], test1[1], test1[2], test2[0], test2[1], test2[2]);
394 void R_Viewport_InitPerspective(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, double frustumx, double frustumy, double nearclip, double farclip, const double *nearplane)
396 matrix4x4_t tempmatrix, basematrix;
397 memset(v, 0, sizeof(*v));
399 if(v_flipped.integer)
400 frustumx = -frustumx;
402 v->type = R_VIEWPORTTYPE_PERSPECTIVE;
403 v->cameramatrix = *cameramatrix;
410 v->m[0] = 1.0 / frustumx;
411 v->m[5] = 1.0 / frustumy;
412 v->m[10] = -(farclip + nearclip) / (farclip - nearclip);
414 v->m[14] = -2 * nearclip * farclip / (farclip - nearclip);
416 Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
417 Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
418 Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
419 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
421 Matrix4x4_FromArrayDoubleGL(&v->projectmatrix, v->m);
424 R_Viewport_ApplyNearClipPlane(v, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
427 void R_Viewport_InitPerspectiveInfinite(r_viewport_t *v, const matrix4x4_t *cameramatrix, int x, int y, int width, int height, double frustumx, double frustumy, double nearclip, const double *nearplane)
429 matrix4x4_t tempmatrix, basematrix;
430 const double nudge = 1.0 - 1.0 / (1<<23);
431 memset(v, 0, sizeof(*v));
433 if(v_flipped.integer)
434 frustumx = -frustumx;
436 v->type = R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP;
437 v->cameramatrix = *cameramatrix;
444 v->m[ 0] = 1.0 / frustumx;
445 v->m[ 5] = 1.0 / frustumy;
448 v->m[14] = -2 * nearclip * nudge;
450 Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
451 Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
452 Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
453 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
455 Matrix4x4_FromArrayDoubleGL(&v->projectmatrix, v->m);
458 R_Viewport_ApplyNearClipPlane(v, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
461 float cubeviewmatrix[6][16] =
463 // standard cubemap projections
501 float rectviewmatrix[6][16] =
503 // sign-preserving cubemap projections
542 void R_Viewport_InitCubeSideView(r_viewport_t *v, const matrix4x4_t *cameramatrix, int side, int size, float nearclip, float farclip, const float *nearplane)
544 matrix4x4_t tempmatrix, basematrix;
545 memset(v, 0, sizeof(*v));
546 v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
547 v->cameramatrix = *cameramatrix;
551 v->m[0] = v->m[5] = 1.0f;
552 v->m[10] = -(farclip + nearclip) / (farclip - nearclip);
554 v->m[14] = -2 * nearclip * farclip / (farclip - nearclip);
556 Matrix4x4_FromArrayFloatGL(&basematrix, cubeviewmatrix[side]);
557 Matrix4x4_Invert_Simple(&tempmatrix, &v->cameramatrix);
558 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
559 Matrix4x4_FromArrayDoubleGL(&v->projectmatrix, v->m);
562 R_Viewport_ApplyNearClipPlane(v, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
565 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)
567 matrix4x4_t tempmatrix, basematrix;
568 memset(v, 0, sizeof(*v));
569 v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
570 v->cameramatrix = *cameramatrix;
571 v->x = (side & 1) * size;
572 v->y = (side >> 1) * size;
576 v->m[0] = v->m[5] = 1.0f * ((float)size - border) / size;
577 v->m[10] = -(farclip + nearclip) / (farclip - nearclip);
579 v->m[14] = -2 * nearclip * farclip / (farclip - nearclip);
581 Matrix4x4_FromArrayFloatGL(&basematrix, rectviewmatrix[side]);
582 Matrix4x4_Invert_Simple(&tempmatrix, &v->cameramatrix);
583 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
584 Matrix4x4_FromArrayDoubleGL(&v->projectmatrix, v->m);
587 R_Viewport_ApplyNearClipPlane(v, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
590 void R_SetViewport(const r_viewport_t *v)
593 gl_state.viewport = *v;
596 qglViewport(v->x, v->y, v->width, v->height);CHECKGLERROR
598 // Load the projection matrix into OpenGL
599 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
600 qglLoadMatrixd(gl_state.viewport.m);CHECKGLERROR
601 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
603 // FIXME: v_flipped_state is evil, this probably breaks somewhere
604 GL_SetMirrorState(v_flipped.integer && (v->type == R_VIEWPORTTYPE_PERSPECTIVE || v->type == R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP));
606 // directly force an update of the modelview matrix
607 Matrix4x4_Concat(&gl_state.modelviewmatrix, &gl_state.viewport.viewmatrix, &gl_state.modelmatrix);
608 Matrix4x4_ToArrayFloatGL(&gl_state.modelviewmatrix, glmatrix);
609 qglLoadMatrixf(glmatrix);CHECKGLERROR
612 void R_GetViewport(r_viewport_t *v)
614 *v = gl_state.viewport;
617 static void GL_BindVBO(int bufferobject)
619 if (gl_state.vertexbufferobject != bufferobject)
621 gl_state.vertexbufferobject = bufferobject;
623 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, bufferobject);
628 static void GL_BindEBO(int bufferobject)
630 if (gl_state.elementbufferobject != bufferobject)
632 gl_state.elementbufferobject = bufferobject;
634 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, bufferobject);
639 static void GL_Backend_ResetState(void)
642 gl_state.active = true;
643 gl_state.depthtest = true;
644 gl_state.alphatest = false;
645 gl_state.blendfunc1 = GL_ONE;
646 gl_state.blendfunc2 = GL_ZERO;
647 gl_state.blend = false;
648 gl_state.depthmask = GL_TRUE;
649 gl_state.colormask = 15;
650 gl_state.color4f[0] = gl_state.color4f[1] = gl_state.color4f[2] = gl_state.color4f[3] = 1;
651 gl_state.lockrange_first = 0;
652 gl_state.lockrange_count = 0;
653 gl_state.cullface = v_flipped_state ? GL_BACK : GL_FRONT; // quake is backwards, this culls back faces
654 gl_state.cullfaceenable = true;
655 gl_state.polygonoffset[0] = 0;
656 gl_state.polygonoffset[1] = 0;
660 qglColorMask(1, 1, 1, 1);
661 qglAlphaFunc(GL_GEQUAL, 0.5);CHECKGLERROR
662 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
663 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
664 qglDisable(GL_BLEND);CHECKGLERROR
665 qglCullFace(gl_state.cullface);CHECKGLERROR
666 qglEnable(GL_CULL_FACE);CHECKGLERROR
667 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
668 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
669 qglDepthMask(gl_state.depthmask);CHECKGLERROR
670 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
672 if (vid.support.arb_vertex_buffer_object)
674 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
675 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
678 if (vid.support.ext_framebuffer_object)
680 qglBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
681 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
684 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), NULL);CHECKGLERROR
685 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
687 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL);CHECKGLERROR
688 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
690 GL_Color(0, 0, 0, 0);
691 GL_Color(1, 1, 1, 1);
693 gl_state.unit = MAX_TEXTUREUNITS;
694 gl_state.clientunit = MAX_TEXTUREUNITS;
695 for (i = 0;i < vid.teximageunits;i++)
698 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
699 if (vid.support.ext_texture_3d)
701 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
703 if (vid.support.arb_texture_cube_map)
705 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
707 if (vid.support.arb_texture_rectangle)
709 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);CHECKGLERROR
713 for (i = 0;i < vid.texarrayunits;i++)
715 GL_ClientActiveTexture(i);
717 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
718 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
721 for (i = 0;i < vid.texunits;i++)
724 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
725 if (vid.support.ext_texture_3d)
727 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
729 if (vid.support.arb_texture_cube_map)
731 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
733 if (vid.support.arb_texture_rectangle)
735 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
737 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
738 qglLoadIdentity();CHECKGLERROR
739 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
740 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
746 void GL_ActiveTexture(unsigned int num)
748 if (gl_state.unit != num)
751 if (qglActiveTexture)
754 qglActiveTexture(GL_TEXTURE0_ARB + gl_state.unit);
760 void GL_ClientActiveTexture(unsigned int num)
762 if (gl_state.clientunit != num)
764 gl_state.clientunit = num;
765 if (qglActiveTexture)
768 qglClientActiveTexture(GL_TEXTURE0_ARB + gl_state.clientunit);
774 void GL_BlendFunc(int blendfunc1, int blendfunc2)
776 if (gl_state.blendfunc1 != blendfunc1 || gl_state.blendfunc2 != blendfunc2)
779 qglBlendFunc(gl_state.blendfunc1 = blendfunc1, gl_state.blendfunc2 = blendfunc2);CHECKGLERROR
780 if (gl_state.blendfunc2 == GL_ZERO)
782 if (gl_state.blendfunc1 == GL_ONE)
787 qglDisable(GL_BLEND);CHECKGLERROR
795 qglEnable(GL_BLEND);CHECKGLERROR
804 qglEnable(GL_BLEND);CHECKGLERROR
810 void GL_DepthMask(int state)
812 if (gl_state.depthmask != state)
815 qglDepthMask(gl_state.depthmask = state);CHECKGLERROR
819 void GL_DepthTest(int state)
821 if (gl_state.depthtest != state)
823 gl_state.depthtest = state;
825 if (gl_state.depthtest)
827 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
831 qglDisable(GL_DEPTH_TEST);CHECKGLERROR
836 void GL_DepthRange(float nearfrac, float farfrac)
838 if (gl_state.depthrange[0] != nearfrac || gl_state.depthrange[1] != farfrac)
840 gl_state.depthrange[0] = nearfrac;
841 gl_state.depthrange[1] = farfrac;
842 qglDepthRange(nearfrac, farfrac);
846 void GL_PolygonOffset(float planeoffset, float depthoffset)
848 if (gl_state.polygonoffset[0] != planeoffset || gl_state.polygonoffset[1] != depthoffset)
850 gl_state.polygonoffset[0] = planeoffset;
851 gl_state.polygonoffset[1] = depthoffset;
852 qglPolygonOffset(planeoffset, depthoffset);
856 void GL_SetMirrorState(qboolean state)
858 if(!state != !v_flipped_state)
860 // change cull face mode!
861 if(gl_state.cullface == GL_BACK)
862 qglCullFace((gl_state.cullface = GL_FRONT));
863 else if(gl_state.cullface == GL_FRONT)
864 qglCullFace((gl_state.cullface = GL_BACK));
866 v_flipped_state = state;
869 void GL_CullFace(int state)
875 if(state == GL_FRONT)
877 else if(state == GL_BACK)
881 if (state != GL_NONE)
883 if (!gl_state.cullfaceenable)
885 gl_state.cullfaceenable = true;
886 qglEnable(GL_CULL_FACE);CHECKGLERROR
888 if (gl_state.cullface != state)
890 gl_state.cullface = state;
891 qglCullFace(gl_state.cullface);CHECKGLERROR
896 if (gl_state.cullfaceenable)
898 gl_state.cullfaceenable = false;
899 qglDisable(GL_CULL_FACE);CHECKGLERROR
904 void GL_AlphaTest(int state)
906 if (gl_state.alphatest != state)
908 gl_state.alphatest = state;
910 if (gl_state.alphatest)
912 qglEnable(GL_ALPHA_TEST);CHECKGLERROR
916 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
921 void GL_ColorMask(int r, int g, int b, int a)
923 int state = r*8 + g*4 + b*2 + a*1;
924 if (gl_state.colormask != state)
926 gl_state.colormask = state;
928 qglColorMask((GLboolean)r, (GLboolean)g, (GLboolean)b, (GLboolean)a);CHECKGLERROR
932 void GL_Color(float cr, float cg, float cb, float ca)
934 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)
936 gl_state.color4f[0] = cr;
937 gl_state.color4f[1] = cg;
938 gl_state.color4f[2] = cb;
939 gl_state.color4f[3] = ca;
941 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);
946 void GL_LockArrays(int first, int count)
948 if (count < gl_lockarrays_minimumvertices.integer)
953 if (gl_state.lockrange_count != count || gl_state.lockrange_first != first)
955 if (gl_state.lockrange_count)
957 gl_state.lockrange_count = 0;
959 qglUnlockArraysEXT();
962 if (count && vid.support.ext_compiled_vertex_array && gl_lockarrays.integer)
964 gl_state.lockrange_first = first;
965 gl_state.lockrange_count = count;
967 qglLockArraysEXT(first, count);
973 void GL_Scissor (int x, int y, int width, int height)
976 qglScissor(x, y,width,height);
980 void GL_ScissorTest(int state)
982 if(gl_state.scissortest == state)
986 if((gl_state.scissortest = state))
987 qglEnable(GL_SCISSOR_TEST);
989 qglDisable(GL_SCISSOR_TEST);
993 void GL_Clear(int mask)
996 qglClear(mask);CHECKGLERROR
999 // called at beginning of frame
1000 void R_Mesh_Start(void)
1004 if (gl_printcheckerror.integer && !gl_paranoid.integer)
1006 Con_Printf("WARNING: gl_printcheckerror is on but gl_paranoid is off, turning it on...\n");
1007 Cvar_SetValueQuick(&gl_paranoid, 1);
1011 qboolean GL_Backend_CompileShader(int programobject, GLenum shadertypeenum, const char *shadertype, int numstrings, const char **strings)
1015 char compilelog[MAX_INPUTLINE];
1016 shaderobject = qglCreateShaderObjectARB(shadertypeenum);CHECKGLERROR
1019 qglShaderSourceARB(shaderobject, numstrings, strings, NULL);CHECKGLERROR
1020 qglCompileShaderARB(shaderobject);CHECKGLERROR
1021 qglGetObjectParameterivARB(shaderobject, GL_OBJECT_COMPILE_STATUS_ARB, &shadercompiled);CHECKGLERROR
1022 qglGetInfoLogARB(shaderobject, sizeof(compilelog), NULL, compilelog);CHECKGLERROR
1023 if (compilelog[0] && developer.integer > 0)
1025 int i, j, pretextlines = 0;
1026 for (i = 0;i < numstrings - 1;i++)
1027 for (j = 0;strings[i][j];j++)
1028 if (strings[i][j] == '\n')
1030 Con_DPrintf("%s shader compile log:\n%s\n(line offset for any above warnings/errors: %i)\n", shadertype, compilelog, pretextlines);
1032 if (!shadercompiled)
1034 qglDeleteObjectARB(shaderobject);CHECKGLERROR
1037 qglAttachObjectARB(programobject, shaderobject);CHECKGLERROR
1038 qglDeleteObjectARB(shaderobject);CHECKGLERROR
1042 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)
1044 GLint programlinked;
1045 GLuint programobject = 0;
1046 char linklog[MAX_INPUTLINE];
1049 programobject = qglCreateProgramObjectARB();CHECKGLERROR
1053 if (vertexstrings_count && !GL_Backend_CompileShader(programobject, GL_VERTEX_SHADER_ARB, "vertex", vertexstrings_count, vertexstrings_list))
1056 #ifdef GL_GEOMETRY_SHADER_ARB
1057 if (geometrystrings_count && !GL_Backend_CompileShader(programobject, GL_GEOMETRY_SHADER_ARB, "geometry", geometrystrings_count, geometrystrings_list))
1061 if (fragmentstrings_count && !GL_Backend_CompileShader(programobject, GL_FRAGMENT_SHADER_ARB, "fragment", fragmentstrings_count, fragmentstrings_list))
1064 qglLinkProgramARB(programobject);CHECKGLERROR
1065 qglGetObjectParameterivARB(programobject, GL_OBJECT_LINK_STATUS_ARB, &programlinked);CHECKGLERROR
1066 qglGetInfoLogARB(programobject, sizeof(linklog), NULL, linklog);CHECKGLERROR
1069 Con_DPrintf("program link log:\n%s\n", linklog);
1070 // software vertex shader is ok but software fragment shader is WAY
1071 // too slow, fail program if so.
1072 // NOTE: this string might be ATI specific, but that's ok because the
1073 // ATI R300 chip (Radeon 9500-9800/X300) is the most likely to use a
1074 // software fragment shader due to low instruction and dependent
1076 if (strstr(linklog, "fragment shader will run in software"))
1077 programlinked = false;
1081 return programobject;
1083 qglDeleteObjectARB(programobject);CHECKGLERROR
1087 void GL_Backend_FreeProgram(unsigned int prog)
1090 qglDeleteObjectARB(prog);
1094 void GL_Backend_RenumberElements(int *out, int count, const int *in, int offset)
1099 for (i = 0;i < count;i++)
1100 *out++ = *in++ + offset;
1103 memcpy(out, in, sizeof(*out) * count);
1106 // renders triangles using vertices from the active arrays
1107 int paranoidblah = 0;
1108 void R_Mesh_Draw(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const unsigned short *element3s, int bufferobject3i, int bufferobject3s)
1110 unsigned int numelements = numtriangles * 3;
1111 if (numvertices < 3 || numtriangles < 1)
1113 if (numvertices < 0 || numtriangles < 0 || developer.integer >= 100)
1114 Con_Printf("R_Mesh_Draw(%d, %d, %d, %d, %8p, %8p, %i, %i);\n", firstvertex, numvertices, firsttriangle, numtriangles, (void *)element3i, (void *)element3s, bufferobject3i, bufferobject3s);
1117 if (!gl_mesh_prefer_short_elements.integer)
1125 element3i += firsttriangle * 3;
1127 element3s += firsttriangle * 3;
1128 switch (gl_vbo.integer)
1133 bufferobject3i = bufferobject3s = 0;
1139 bufferobject3i = bufferobject3s = 0;
1143 r_refdef.stats.meshes++;
1144 r_refdef.stats.meshes_elements += numelements;
1145 if (gl_paranoid.integer)
1147 unsigned int i, j, size;
1149 // note: there's no validation done here on buffer objects because it
1150 // is somewhat difficult to get at the data, and gl_paranoid can be
1151 // used without buffer objects if the need arises
1152 // (the data could be gotten using glMapBuffer but it would be very
1153 // slow due to uncachable video memory reads)
1154 if (!qglIsEnabled(GL_VERTEX_ARRAY))
1155 Con_Print("R_Mesh_Draw: vertex array not enabled\n");
1157 if (gl_state.pointer_vertex)
1158 for (j = 0, size = numvertices * 3, p = (int *)((float *)gl_state.pointer_vertex + firstvertex * 3);j < size;j++, p++)
1160 if (gl_state.pointer_color_enabled)
1162 if (!qglIsEnabled(GL_COLOR_ARRAY))
1163 Con_Print("R_Mesh_Draw: color array set but not enabled\n");
1165 if (gl_state.pointer_color && gl_state.pointer_color_enabled)
1166 for (j = 0, size = numvertices * 4, p = (int *)((float *)gl_state.pointer_color + firstvertex * 4);j < size;j++, p++)
1169 for (i = 0;i < vid.texarrayunits;i++)
1171 if (gl_state.units[i].arrayenabled)
1173 GL_ClientActiveTexture(i);
1174 if (!qglIsEnabled(GL_TEXTURE_COORD_ARRAY))
1175 Con_Print("R_Mesh_Draw: texcoord array set but not enabled\n");
1177 if (gl_state.units[i].pointer_texcoord && gl_state.units[i].arrayenabled)
1178 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++)
1184 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1186 if (element3i[i] < firstvertex || element3i[i] >= firstvertex + numvertices)
1188 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3i array\n", element3i[i], firstvertex, firstvertex + numvertices);
1195 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1197 if (element3s[i] < firstvertex || element3s[i] >= firstvertex + numvertices)
1199 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3s array\n", element3s[i], firstvertex, firstvertex + numvertices);
1206 if (r_render.integer || r_refdef.draw2dstage)
1209 if (gl_mesh_testmanualfeeding.integer)
1211 unsigned int i, j, element;
1213 qglBegin(GL_TRIANGLES);
1214 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1216 element = element3i ? element3i[i] : element3s[i];
1217 for (j = 0;j < vid.texarrayunits;j++)
1219 if (gl_state.units[j].pointer_texcoord && gl_state.units[j].arrayenabled)
1221 if (vid.texarrayunits > 1)
1223 if (gl_state.units[j].arraycomponents == 4)
1225 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 4;
1226 qglMultiTexCoord4f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2], p[3]);
1228 else if (gl_state.units[j].arraycomponents == 3)
1230 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 3;
1231 qglMultiTexCoord3f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2]);
1233 else if (gl_state.units[j].arraycomponents == 2)
1235 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 2;
1236 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, p[0], p[1]);
1240 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 1;
1241 qglMultiTexCoord1f(GL_TEXTURE0_ARB + j, p[0]);
1246 if (gl_state.units[j].arraycomponents == 4)
1248 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 4;
1249 qglTexCoord4f(p[0], p[1], p[2], p[3]);
1251 else if (gl_state.units[j].arraycomponents == 3)
1253 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 3;
1254 qglTexCoord3f(p[0], p[1], p[2]);
1256 else if (gl_state.units[j].arraycomponents == 2)
1258 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 2;
1259 qglTexCoord2f(p[0], p[1]);
1263 p = ((const GLfloat *)(gl_state.units[j].pointer_texcoord)) + element * 1;
1264 qglTexCoord1f(p[0]);
1269 if (gl_state.pointer_color && gl_state.pointer_color_enabled)
1271 p = ((const GLfloat *)(gl_state.pointer_color)) + element * 4;
1272 qglColor4f(p[0], p[1], p[2], p[3]);
1274 p = ((const GLfloat *)(gl_state.pointer_vertex)) + element * 3;
1275 qglVertex3f(p[0], p[1], p[2]);
1280 else if (gl_mesh_testarrayelement.integer)
1283 qglBegin(GL_TRIANGLES);
1286 for (i = 0;i < numtriangles * 3;i++)
1287 qglArrayElement(element3i[i]);
1291 for (i = 0;i < numtriangles * 3;i++)
1292 qglArrayElement(element3s[i]);
1297 else if (bufferobject3s)
1299 GL_BindEBO(bufferobject3s);
1300 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1302 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, (void *)(firsttriangle * sizeof(unsigned short[3])));
1307 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, (void *)(firsttriangle * sizeof(unsigned short[3])));
1311 else if (bufferobject3i)
1313 GL_BindEBO(bufferobject3i);
1314 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1316 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, (void *)(firsttriangle * sizeof(unsigned int[3])));
1321 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, (void *)(firsttriangle * sizeof(unsigned int[3])));
1328 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1330 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, element3s);
1335 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, element3s);
1342 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1344 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, element3i);
1349 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, element3i);
1356 // restores backend state, used when done with 3D rendering
1357 void R_Mesh_Finish(void)
1361 int R_Mesh_CreateStaticBufferObject(unsigned int target, void *data, size_t size, const char *name)
1363 gl_bufferobjectinfo_t *info;
1364 GLuint bufferobject;
1366 if (!gl_vbo.integer)
1369 qglGenBuffersARB(1, &bufferobject);
1372 case GL_ELEMENT_ARRAY_BUFFER_ARB: GL_BindEBO(bufferobject);break;
1373 case GL_ARRAY_BUFFER_ARB: GL_BindVBO(bufferobject);break;
1374 default: Sys_Error("R_Mesh_CreateStaticBufferObject: unknown target type %i\n", target);return 0;
1376 qglBufferDataARB(target, size, data, GL_STATIC_DRAW_ARB);
1378 info = (gl_bufferobjectinfo_t *) Mem_ExpandableArray_AllocRecord(&gl_state.bufferobjectinfoarray);
1379 memset(info, 0, sizeof(*info));
1380 info->target = target;
1381 info->object = bufferobject;
1383 strlcpy(info->name, name, sizeof(info->name));
1385 return (int)bufferobject;
1388 void R_Mesh_DestroyBufferObject(int bufferobject)
1391 gl_bufferobjectinfo_t *info;
1393 qglDeleteBuffersARB(1, (GLuint *)&bufferobject);
1395 endindex = Mem_ExpandableArray_IndexRange(&gl_state.bufferobjectinfoarray);
1396 for (i = 0;i < endindex;i++)
1398 info = (gl_bufferobjectinfo_t *) Mem_ExpandableArray_RecordAtIndex(&gl_state.bufferobjectinfoarray, i);
1401 if (info->object == bufferobject)
1403 Mem_ExpandableArray_FreeRecord(&gl_state.bufferobjectinfoarray, (void *)info);
1409 void GL_Mesh_ListVBOs(qboolean printeach)
1412 size_t ebocount = 0, ebomemory = 0;
1413 size_t vbocount = 0, vbomemory = 0;
1414 gl_bufferobjectinfo_t *info;
1415 endindex = Mem_ExpandableArray_IndexRange(&gl_state.bufferobjectinfoarray);
1416 for (i = 0;i < endindex;i++)
1418 info = (gl_bufferobjectinfo_t *) Mem_ExpandableArray_RecordAtIndex(&gl_state.bufferobjectinfoarray, i);
1421 switch(info->target)
1423 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;
1424 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;
1425 default: Con_Printf("gl_vbostats: unknown target type %i\n", info->target);break;
1428 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);
1431 void R_Mesh_Matrix(const matrix4x4_t *matrix)
1433 if (memcmp(matrix, &gl_state.modelmatrix, sizeof(matrix4x4_t)))
1436 gl_state.modelmatrix = *matrix;
1437 Matrix4x4_Concat(&gl_state.modelviewmatrix, &gl_state.viewport.viewmatrix, &gl_state.modelmatrix);
1438 Matrix4x4_ToArrayFloatGL(&gl_state.modelviewmatrix, glmatrix);
1440 qglLoadMatrixf(glmatrix);CHECKGLERROR
1444 void R_Mesh_VertexPointer(const float *vertex3f, int bufferobject, size_t bufferoffset)
1446 if (!gl_vbo.integer || gl_mesh_testarrayelement.integer)
1448 if (gl_state.pointer_vertex != vertex3f || gl_state.pointer_vertex_buffer != bufferobject || gl_state.pointer_vertex_offset != bufferoffset)
1450 gl_state.pointer_vertex = vertex3f;
1451 gl_state.pointer_vertex_buffer = bufferobject;
1452 gl_state.pointer_vertex_offset = bufferoffset;
1454 GL_BindVBO(bufferobject);
1455 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), bufferobject ? (void *)bufferoffset : vertex3f);CHECKGLERROR
1459 void R_Mesh_ColorPointer(const float *color4f, int bufferobject, size_t bufferoffset)
1461 // note: this can not rely on bufferobject to decide whether a color array
1462 // is supplied, because surfmesh_t shares one vbo for all arrays, which
1463 // means that a valid vbo may be supplied even if there is no color array.
1466 if (!gl_vbo.integer || gl_mesh_testarrayelement.integer)
1468 // caller wants color array enabled
1469 if (!gl_state.pointer_color_enabled)
1471 gl_state.pointer_color_enabled = true;
1473 qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1475 if (gl_state.pointer_color != color4f || gl_state.pointer_color_buffer != bufferobject || gl_state.pointer_color_offset != bufferoffset)
1477 gl_state.pointer_color = color4f;
1478 gl_state.pointer_color_buffer = bufferobject;
1479 gl_state.pointer_color_offset = bufferoffset;
1481 GL_BindVBO(bufferobject);
1482 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), bufferobject ? (void *)bufferoffset : color4f);CHECKGLERROR
1487 // caller wants color array disabled
1488 if (gl_state.pointer_color_enabled)
1490 gl_state.pointer_color_enabled = false;
1492 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1493 // when color array is on the glColor gets trashed, set it again
1494 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);CHECKGLERROR
1499 void R_Mesh_TexCoordPointer(unsigned int unitnum, unsigned int numcomponents, const float *texcoord, int bufferobject, size_t bufferoffset)
1501 gltextureunit_t *unit = gl_state.units + unitnum;
1502 // update array settings
1504 // note: there is no need to check bufferobject here because all cases
1505 // that involve a valid bufferobject also supply a texcoord array
1508 if (!gl_vbo.integer || gl_mesh_testarrayelement.integer)
1510 // texture array unit is enabled, enable the array
1511 if (!unit->arrayenabled)
1513 unit->arrayenabled = true;
1514 GL_ClientActiveTexture(unitnum);
1515 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1518 if (unit->pointer_texcoord != texcoord || unit->pointer_texcoord_buffer != bufferobject || unit->pointer_texcoord_offset != bufferoffset || unit->arraycomponents != numcomponents)
1520 unit->pointer_texcoord = texcoord;
1521 unit->pointer_texcoord_buffer = bufferobject;
1522 unit->pointer_texcoord_offset = bufferoffset;
1523 unit->arraycomponents = numcomponents;
1524 GL_ClientActiveTexture(unitnum);
1525 GL_BindVBO(bufferobject);
1526 qglTexCoordPointer(unit->arraycomponents, GL_FLOAT, sizeof(float) * unit->arraycomponents, bufferobject ? (void *)bufferoffset : texcoord);CHECKGLERROR
1531 // texture array unit is disabled, disable the array
1532 if (unit->arrayenabled)
1534 unit->arrayenabled = false;
1535 GL_ClientActiveTexture(unitnum);
1536 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1541 void R_Mesh_TexBindAll(unsigned int unitnum, int tex2d, int tex3d, int texcubemap, int texrectangle)
1543 gltextureunit_t *unit = gl_state.units + unitnum;
1544 if (unitnum >= vid.teximageunits)
1546 // update 2d texture binding
1547 if (unit->t2d != tex2d)
1549 GL_ActiveTexture(unitnum);
1550 if (unitnum < vid.texunits)
1556 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
1563 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1568 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1570 // update 3d texture binding
1571 if (unit->t3d != tex3d)
1573 GL_ActiveTexture(unitnum);
1574 if (unitnum < vid.texunits)
1580 qglEnable(GL_TEXTURE_3D);CHECKGLERROR
1587 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1592 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1594 // update cubemap texture binding
1595 if (unit->tcubemap != texcubemap)
1597 GL_ActiveTexture(unitnum);
1598 if (unitnum < vid.texunits)
1602 if (unit->tcubemap == 0)
1604 qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1611 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1615 unit->tcubemap = texcubemap;
1616 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1618 // update rectangle texture binding
1619 if (unit->trectangle != texrectangle)
1621 GL_ActiveTexture(unitnum);
1622 if (unitnum < vid.texunits)
1626 if (unit->trectangle == 0)
1628 qglEnable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
1633 if (unit->trectangle)
1635 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
1639 unit->trectangle = texrectangle;
1640 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
1644 void R_Mesh_TexBind(unsigned int unitnum, int texnum)
1646 gltextureunit_t *unit = gl_state.units + unitnum;
1647 if (unitnum >= vid.teximageunits)
1649 // update 2d texture binding
1650 if (unit->t2d != texnum)
1652 GL_ActiveTexture(unitnum);
1653 if (unitnum < vid.texunits)
1659 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
1666 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1671 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1673 // update 3d texture binding
1676 GL_ActiveTexture(unitnum);
1677 if (unitnum < vid.texunits)
1681 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1685 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1687 // update cubemap texture binding
1688 if (unit->tcubemap != 0)
1690 GL_ActiveTexture(unitnum);
1691 if (unitnum < vid.texunits)
1695 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1699 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1701 // update rectangle texture binding
1702 if (unit->trectangle != 0)
1704 GL_ActiveTexture(unitnum);
1705 if (unitnum < vid.texunits)
1707 if (unit->trectangle)
1709 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
1712 unit->trectangle = 0;
1713 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
1717 static const float gl_identitymatrix[16] = {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
1719 void R_Mesh_TexMatrix(unsigned int unitnum, const matrix4x4_t *matrix)
1721 gltextureunit_t *unit = gl_state.units + unitnum;
1722 if (matrix->m[3][3])
1724 // texmatrix specified, check if it is different
1725 if (!unit->texmatrixenabled || memcmp(&unit->matrix, matrix, sizeof(matrix4x4_t)))
1728 unit->texmatrixenabled = true;
1729 unit->matrix = *matrix;
1731 Matrix4x4_ToArrayFloatGL(&unit->matrix, glmatrix);
1732 GL_ActiveTexture(unitnum);
1733 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1734 qglLoadMatrixf(glmatrix);CHECKGLERROR
1735 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1740 // no texmatrix specified, revert to identity
1741 if (unit->texmatrixenabled)
1743 unit->texmatrixenabled = false;
1744 unit->matrix = identitymatrix;
1746 GL_ActiveTexture(unitnum);
1747 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1748 qglLoadIdentity();CHECKGLERROR
1749 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1754 void R_Mesh_TexCombine(unsigned int unitnum, int combinergb, int combinealpha, int rgbscale, int alphascale)
1756 gltextureunit_t *unit = gl_state.units + unitnum;
1759 combinergb = GL_MODULATE;
1761 combinealpha = GL_MODULATE;
1766 if (combinergb != combinealpha || rgbscale != 1 || alphascale != 1 || combinergb == GL_DOT3_RGBA_ARB || combinergb == GL_DOT3_RGB_ARB)
1768 if (combinergb == GL_DECAL)
1769 combinergb = GL_INTERPOLATE_ARB;
1770 if (unit->combine != GL_COMBINE_ARB)
1772 unit->combine = GL_COMBINE_ARB;
1773 GL_ActiveTexture(unitnum);
1774 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);CHECKGLERROR
1775 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_TEXTURE);CHECKGLERROR // for GL_INTERPOLATE_ARB mode
1777 if (unit->combinergb != combinergb)
1779 unit->combinergb = combinergb;
1780 GL_ActiveTexture(unitnum);
1781 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
1783 if (unit->combinealpha != combinealpha)
1785 unit->combinealpha = combinealpha;
1786 GL_ActiveTexture(unitnum);
1787 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
1789 if (unit->rgbscale != rgbscale)
1791 unit->rgbscale = rgbscale;
1792 GL_ActiveTexture(unitnum);
1793 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, unit->rgbscale);CHECKGLERROR
1795 if (unit->alphascale != alphascale)
1797 unit->alphascale = alphascale;
1798 GL_ActiveTexture(unitnum);
1799 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, unit->alphascale);CHECKGLERROR
1804 if (unit->combine != combinergb)
1806 unit->combine = combinergb;
1807 GL_ActiveTexture(unitnum);
1808 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combine);CHECKGLERROR
1813 void R_Mesh_TextureState(const rmeshstate_t *m)
1820 for (i = 0;i < vid.teximageunits;i++)
1821 R_Mesh_TexBindAll(i, m->tex[i], m->tex3d[i], m->texcubemap[i], m->texrectangle[i]);
1822 for (i = 0;i < vid.texarrayunits;i++)
1824 if (m->pointer_texcoord3f[i])
1825 R_Mesh_TexCoordPointer(i, 3, m->pointer_texcoord3f[i], m->pointer_texcoord_bufferobject[i], m->pointer_texcoord_bufferoffset[i]);
1827 R_Mesh_TexCoordPointer(i, 2, m->pointer_texcoord[i], m->pointer_texcoord_bufferobject[i], m->pointer_texcoord_bufferoffset[i]);
1829 for (i = 0;i < vid.texunits;i++)
1831 R_Mesh_TexMatrix(i, &m->texmatrix[i]);
1832 R_Mesh_TexCombine(i, m->texcombinergb[i], m->texcombinealpha[i], m->texrgbscale[i], m->texalphascale[i]);
1837 void R_Mesh_ResetTextureState(void)
1839 unsigned int unitnum;
1844 for (unitnum = 0;unitnum < vid.teximageunits;unitnum++)
1846 gltextureunit_t *unit = gl_state.units + unitnum;
1847 // update 2d texture binding
1850 GL_ActiveTexture(unitnum);
1851 if (unitnum < vid.texunits)
1853 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1856 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
1858 // update 3d texture binding
1861 GL_ActiveTexture(unitnum);
1862 if (unitnum < vid.texunits)
1864 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1867 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
1869 // update cubemap texture binding
1872 GL_ActiveTexture(unitnum);
1873 if (unitnum < vid.texunits)
1875 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1878 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
1880 // update rectangle texture binding
1881 if (unit->trectangle)
1883 GL_ActiveTexture(unitnum);
1884 if (unitnum < vid.texunits)
1886 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
1888 unit->trectangle = 0;
1889 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
1892 for (unitnum = 0;unitnum < vid.texarrayunits;unitnum++)
1894 gltextureunit_t *unit = gl_state.units + unitnum;
1895 // texture array unit is disabled, disable the array
1896 if (unit->arrayenabled)
1898 unit->arrayenabled = false;
1899 GL_ClientActiveTexture(unitnum);
1900 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1903 for (unitnum = 0;unitnum < vid.texunits;unitnum++)
1905 gltextureunit_t *unit = gl_state.units + unitnum;
1906 // no texmatrix specified, revert to identity
1907 if (unit->texmatrixenabled)
1909 unit->texmatrixenabled = false;
1910 unit->matrix = identitymatrix;
1912 GL_ActiveTexture(unitnum);
1913 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1914 qglLoadIdentity();CHECKGLERROR
1915 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1917 if (unit->combine != GL_MODULATE)
1919 unit->combine = GL_MODULATE;
1920 GL_ActiveTexture(unitnum);
1921 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combine);CHECKGLERROR