3 #include "cl_collision.h"
5 // on GLES we have to use some proper #define's
7 #define GL_FRAMEBUFFER 0x8D40
8 #define GL_DEPTH_ATTACHMENT 0x8D00
9 #define GL_COLOR_ATTACHMENT0 0x8CE0
10 #define GL_INVALID_FRAMEBUFFER_OPERATION 0x0506
12 #ifndef GL_COLOR_ATTACHMENT1
13 #define GL_COLOR_ATTACHMENT1 0x8CE1
14 #define GL_COLOR_ATTACHMENT2 0x8CE2
15 #define GL_COLOR_ATTACHMENT3 0x8CE3
16 #define GL_COLOR_ATTACHMENT4 0x8CE4
17 #define GL_COLOR_ATTACHMENT5 0x8CE5
18 #define GL_COLOR_ATTACHMENT6 0x8CE6
19 #define GL_COLOR_ATTACHMENT7 0x8CE7
20 #define GL_COLOR_ATTACHMENT8 0x8CE8
21 #define GL_COLOR_ATTACHMENT9 0x8CE9
22 #define GL_COLOR_ATTACHMENT10 0x8CEA
23 #define GL_COLOR_ATTACHMENT11 0x8CEB
24 #define GL_COLOR_ATTACHMENT12 0x8CEC
25 #define GL_COLOR_ATTACHMENT13 0x8CED
26 #define GL_COLOR_ATTACHMENT14 0x8CEE
27 #define GL_COLOR_ATTACHMENT15 0x8CEF
29 #ifndef GL_ARRAY_BUFFER
30 #define GL_ARRAY_BUFFER 0x8892
31 #define GL_ELEMENT_ARRAY_BUFFER 0x8893
34 #define GL_TEXTURE0 0x84C0
35 #define GL_TEXTURE1 0x84C1
36 #define GL_TEXTURE2 0x84C2
37 #define GL_TEXTURE3 0x84C3
38 #define GL_TEXTURE4 0x84C4
39 #define GL_TEXTURE5 0x84C5
40 #define GL_TEXTURE6 0x84C6
41 #define GL_TEXTURE7 0x84C7
42 #define GL_TEXTURE8 0x84C8
43 #define GL_TEXTURE9 0x84C9
44 #define GL_TEXTURE10 0x84CA
45 #define GL_TEXTURE11 0x84CB
46 #define GL_TEXTURE12 0x84CC
47 #define GL_TEXTURE13 0x84CD
48 #define GL_TEXTURE14 0x84CE
49 #define GL_TEXTURE15 0x84CF
50 #define GL_TEXTURE16 0x84D0
51 #define GL_TEXTURE17 0x84D1
52 #define GL_TEXTURE18 0x84D2
53 #define GL_TEXTURE19 0x84D3
54 #define GL_TEXTURE20 0x84D4
55 #define GL_TEXTURE21 0x84D5
56 #define GL_TEXTURE22 0x84D6
57 #define GL_TEXTURE23 0x84D7
58 #define GL_TEXTURE24 0x84D8
59 #define GL_TEXTURE25 0x84D9
60 #define GL_TEXTURE26 0x84DA
61 #define GL_TEXTURE27 0x84DB
62 #define GL_TEXTURE28 0x84DC
63 #define GL_TEXTURE29 0x84DD
64 #define GL_TEXTURE30 0x84DE
65 #define GL_TEXTURE31 0x84DF
69 #define GL_TEXTURE_3D 0x806F
71 #ifndef GL_TEXTURE_CUBE_MAP
72 #define GL_TEXTURE_CUBE_MAP 0x8513
76 #define MAX_RENDERTARGETS 4
78 cvar_t gl_mesh_drawrangeelements = {0, "gl_mesh_drawrangeelements", "1", "use glDrawRangeElements function if available instead of glDrawElements (for performance comparisons or bug testing)"};
79 cvar_t gl_paranoid = {0, "gl_paranoid", "0", "enables OpenGL error checking and other tests"};
80 cvar_t gl_printcheckerror = {0, "gl_printcheckerror", "0", "prints all OpenGL error checks, useful to identify location of driver crashes"};
82 cvar_t r_render = {0, "r_render", "1", "enables rendering 3D views (you want this on!)"};
83 cvar_t r_renderview = {0, "r_renderview", "1", "enables rendering 3D views (you want this on!)"};
84 cvar_t r_waterwarp = {CVAR_SAVE, "r_waterwarp", "1", "warp view while underwater"};
85 cvar_t gl_polyblend = {CVAR_SAVE, "gl_polyblend", "1", "tints view while underwater, hurt, etc"};
86 cvar_t gl_dither = {CVAR_SAVE, "gl_dither", "1", "enables OpenGL dithering (16bit looks bad with this off)"};
87 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)"};
88 cvar_t gl_vbo_dynamicvertex = {CVAR_SAVE, "gl_vbo_dynamicvertex", "0", "make use of GL_ARB_vertex_buffer_object extension when rendering dynamic (animated/procedural) geometry such as text and particles"};
89 cvar_t gl_vbo_dynamicindex = {CVAR_SAVE, "gl_vbo_dynamicindex", "0", "make use of GL_ARB_vertex_buffer_object extension when rendering dynamic (animated/procedural) geometry such as text and particles"};
90 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"};
92 cvar_t v_flipped = {0, "v_flipped", "0", "mirror the screen (poor man's left handed mode)"};
93 qboolean v_flipped_state = false;
95 r_viewport_t gl_viewport;
96 matrix4x4_t gl_modelmatrix;
97 matrix4x4_t gl_viewmatrix;
98 matrix4x4_t gl_modelviewmatrix;
99 matrix4x4_t gl_projectionmatrix;
100 matrix4x4_t gl_modelviewprojectionmatrix;
101 float gl_modelview16f[16];
102 float gl_modelviewprojection16f[16];
103 qboolean gl_modelmatrixchanged;
105 int gl_maxdrawrangeelementsvertices;
106 int gl_maxdrawrangeelementsindices;
109 int gl_errornumber = 0;
111 void GL_PrintError(int errornumber, const char *filename, int linenumber)
115 #ifdef GL_INVALID_ENUM
116 case GL_INVALID_ENUM:
117 Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
120 #ifdef GL_INVALID_VALUE
121 case GL_INVALID_VALUE:
122 Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
125 #ifdef GL_INVALID_OPERATION
126 case GL_INVALID_OPERATION:
127 Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
130 #ifdef GL_STACK_OVERFLOW
131 case GL_STACK_OVERFLOW:
132 Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
135 #ifdef GL_STACK_UNDERFLOW
136 case GL_STACK_UNDERFLOW:
137 Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
140 #ifdef GL_OUT_OF_MEMORY
141 case GL_OUT_OF_MEMORY:
142 Con_Printf("GL_OUT_OF_MEMORY at %s:%i\n", filename, linenumber);
145 #ifdef GL_TABLE_TOO_LARGE
146 case GL_TABLE_TOO_LARGE:
147 Con_Printf("GL_TABLE_TOO_LARGE at %s:%i\n", filename, linenumber);
150 #ifdef GL_INVALID_FRAMEBUFFER_OPERATION
151 case GL_INVALID_FRAMEBUFFER_OPERATION:
152 Con_Printf("GL_INVALID_FRAMEBUFFER_OPERATION at %s:%i\n", filename, linenumber);
156 Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
162 #define BACKENDACTIVECHECK if (!gl_state.active) Sys_Error("GL backend function called when backend is not active");
164 void SCR_ScreenShot_f (void);
166 typedef struct gltextureunit_s
168 int pointer_texcoord_components;
169 int pointer_texcoord_gltype;
170 size_t pointer_texcoord_stride;
171 const void *pointer_texcoord_pointer;
172 const r_meshbuffer_t *pointer_texcoord_vertexbuffer;
173 size_t pointer_texcoord_offset;
176 int t2d, t3d, tcubemap;
178 int rgbscale, alphascale;
180 int combinergb, combinealpha;
181 // texmatrixenabled exists only to avoid unnecessary texmatrix compares
182 int texmatrixenabled;
187 typedef struct gl_state_s
195 int colormask; // stored as bottom 4 bits: r g b a (3 2 1 0 order)
199 float polygonoffset[2];
202 float alphafuncvalue;
203 qboolean alphatocoverage;
206 unsigned int clientunit;
207 gltextureunit_t units[MAX_TEXTUREUNITS];
211 int vertexbufferobject;
212 int elementbufferobject;
213 int uniformbufferobject;
214 int framebufferobject;
215 int defaultframebufferobject; // deal with platforms that use a non-zero default fbo
216 qboolean pointer_color_enabled;
218 int pointer_vertex_components;
219 int pointer_vertex_gltype;
220 size_t pointer_vertex_stride;
221 const void *pointer_vertex_pointer;
222 const r_meshbuffer_t *pointer_vertex_vertexbuffer;
223 size_t pointer_vertex_offset;
225 int pointer_color_components;
226 int pointer_color_gltype;
227 size_t pointer_color_stride;
228 const void *pointer_color_pointer;
229 const r_meshbuffer_t *pointer_color_vertexbuffer;
230 size_t pointer_color_offset;
232 void *preparevertices_tempdata;
233 size_t preparevertices_tempdatamaxsize;
234 int preparevertices_numvertices;
236 qboolean usevbo_staticvertex;
237 qboolean usevbo_staticindex;
238 qboolean usevbo_dynamicvertex;
239 qboolean usevbo_dynamicindex;
241 memexpandablearray_t meshbufferarray;
247 static gl_state_t gl_state;
251 note: here's strip order for a terrain row:
258 A0B, 01B, B1C, 12C, C2D, 23D, D3E, 34E
260 *elements++ = i + row;
262 *elements++ = i + row + 1;
265 *elements++ = i + row + 1;
268 for (y = 0;y < rows - 1;y++)
270 for (x = 0;x < columns - 1;x++)
273 *elements++ = i + columns;
275 *elements++ = i + columns + 1;
278 *elements++ = i + columns + 1;
289 for (y = 0;y < rows - 1;y++)
291 for (x = 0;x < columns - 1;x++)
295 *elements++ = i + columns;
296 *elements++ = i + columns + 1;
297 *elements++ = i + columns;
298 *elements++ = i + columns + 1;
304 int polygonelement3i[(POLYGONELEMENTS_MAXPOINTS-2)*3];
305 unsigned short polygonelement3s[(POLYGONELEMENTS_MAXPOINTS-2)*3];
306 int quadelement3i[QUADELEMENTS_MAXQUADS*6];
307 unsigned short quadelement3s[QUADELEMENTS_MAXQUADS*6];
309 static void GL_VBOStats_f(void)
311 GL_Mesh_ListVBOs(true);
314 static void GL_Backend_ResetState(void);
316 static void R_Mesh_SetUseVBO(void)
318 switch(vid.renderpath)
320 case RENDERPATH_GL20:
321 case RENDERPATH_GLES2:
322 gl_state.usevbo_staticvertex = (vid.support.arb_vertex_buffer_object && gl_vbo.integer) || vid.forcevbo;
323 gl_state.usevbo_staticindex = (vid.support.arb_vertex_buffer_object && (gl_vbo.integer == 1 || gl_vbo.integer == 3)) || vid.forcevbo;
324 gl_state.usevbo_dynamicvertex = (vid.support.arb_vertex_buffer_object && gl_vbo_dynamicvertex.integer && gl_vbo.integer) || vid.forcevbo;
325 gl_state.usevbo_dynamicindex = (vid.support.arb_vertex_buffer_object && gl_vbo_dynamicindex.integer && gl_vbo.integer) || vid.forcevbo;
330 static void gl_backend_start(void)
332 memset(&gl_state, 0, sizeof(gl_state));
335 Mem_ExpandableArray_NewArray(&gl_state.meshbufferarray, r_main_mempool, sizeof(r_meshbuffer_t), 128);
337 Con_DPrintf("OpenGL backend started.\n");
341 GL_Backend_ResetState();
343 switch(vid.renderpath)
345 case RENDERPATH_GL20:
346 case RENDERPATH_GLES2:
347 // fetch current fbo here (default fbo is not 0 on some GLES devices)
348 if (vid.support.ext_framebuffer_object)
349 qglGetIntegerv(GL_FRAMEBUFFER_BINDING, &gl_state.defaultframebufferobject);
354 static void gl_backend_shutdown(void)
356 Con_DPrint("OpenGL Backend shutting down\n");
358 switch(vid.renderpath)
360 case RENDERPATH_GL20:
361 case RENDERPATH_GLES2:
365 if (gl_state.preparevertices_tempdata)
366 Mem_Free(gl_state.preparevertices_tempdata);
368 Mem_ExpandableArray_FreeArray(&gl_state.meshbufferarray);
370 memset(&gl_state, 0, sizeof(gl_state));
373 static void gl_backend_newmap(void)
377 static void gl_backend_devicelost(void)
380 r_meshbuffer_t *buffer;
381 switch(vid.renderpath)
383 case RENDERPATH_GL20:
384 case RENDERPATH_GLES2:
387 endindex = (int)Mem_ExpandableArray_IndexRange(&gl_state.meshbufferarray);
388 for (i = 0;i < endindex;i++)
390 buffer = (r_meshbuffer_t *) Mem_ExpandableArray_RecordAtIndex(&gl_state.meshbufferarray, i);
391 if (!buffer || !buffer->isdynamic)
393 switch(vid.renderpath)
395 case RENDERPATH_GL20:
396 case RENDERPATH_GLES2:
402 static void gl_backend_devicerestored(void)
404 switch(vid.renderpath)
406 case RENDERPATH_GL20:
407 case RENDERPATH_GLES2:
412 void gl_backend_init(void)
416 for (i = 0;i < POLYGONELEMENTS_MAXPOINTS - 2;i++)
418 polygonelement3s[i * 3 + 0] = 0;
419 polygonelement3s[i * 3 + 1] = i + 1;
420 polygonelement3s[i * 3 + 2] = i + 2;
422 // elements for rendering a series of quads as triangles
423 for (i = 0;i < QUADELEMENTS_MAXQUADS;i++)
425 quadelement3s[i * 6 + 0] = i * 4;
426 quadelement3s[i * 6 + 1] = i * 4 + 1;
427 quadelement3s[i * 6 + 2] = i * 4 + 2;
428 quadelement3s[i * 6 + 3] = i * 4;
429 quadelement3s[i * 6 + 4] = i * 4 + 2;
430 quadelement3s[i * 6 + 5] = i * 4 + 3;
433 for (i = 0;i < (POLYGONELEMENTS_MAXPOINTS - 2)*3;i++)
434 polygonelement3i[i] = polygonelement3s[i];
435 for (i = 0;i < QUADELEMENTS_MAXQUADS*6;i++)
436 quadelement3i[i] = quadelement3s[i];
438 Cvar_RegisterVariable(&r_render);
439 Cvar_RegisterVariable(&r_renderview);
440 Cvar_RegisterVariable(&r_waterwarp);
441 Cvar_RegisterVariable(&gl_polyblend);
442 Cvar_RegisterVariable(&v_flipped);
443 Cvar_RegisterVariable(&gl_dither);
444 Cvar_RegisterVariable(&gl_vbo);
445 Cvar_RegisterVariable(&gl_vbo_dynamicvertex);
446 Cvar_RegisterVariable(&gl_vbo_dynamicindex);
447 Cvar_RegisterVariable(&gl_paranoid);
448 Cvar_RegisterVariable(&gl_printcheckerror);
450 Cvar_RegisterVariable(&gl_mesh_drawrangeelements);
452 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");
454 R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap, gl_backend_devicelost, gl_backend_devicerestored);
457 void GL_SetMirrorState(qboolean state);
459 void R_Viewport_TransformToScreen(const r_viewport_t *v, const vec4_t in, vec4_t out)
463 Matrix4x4_Transform4 (&v->viewmatrix, in, temp);
464 Matrix4x4_Transform4 (&v->projectmatrix, temp, out);
466 out[0] = v->x + (out[0] * iw + 1.0f) * v->width * 0.5f;
468 // for an odd reason, inverting this is wrong for R_Shadow_ScissorForBBox (we then get badly scissored lights)
469 //out[1] = v->y + v->height - (out[1] * iw + 1.0f) * v->height * 0.5f;
470 out[1] = v->y + (out[1] * iw + 1.0f) * v->height * 0.5f;
472 out[2] = v->z + (out[2] * iw + 1.0f) * v->depth * 0.5f;
477 switch(vid.renderpath)
479 case RENDERPATH_GL20:
480 case RENDERPATH_GLES2:
486 static int bboxedges[12][2] =
505 qboolean R_ScissorForBBox(const float *mins, const float *maxs, int *scissor)
507 int i, ix1, iy1, ix2, iy2;
508 float x1, y1, x2, y2;
519 scissor[0] = r_refdef.view.viewport.x;
520 scissor[1] = r_refdef.view.viewport.y;
521 scissor[2] = r_refdef.view.viewport.width;
522 scissor[3] = r_refdef.view.viewport.height;
524 // if view is inside the box, just say yes it's visible
525 if (BoxesOverlap(r_refdef.view.origin, r_refdef.view.origin, mins, maxs))
528 // transform all corners that are infront of the nearclip plane
529 VectorNegate(r_refdef.view.frustum[4].normal, plane4f);
530 plane4f[3] = r_refdef.view.frustum[4].dist;
532 for (i = 0;i < 8;i++)
534 Vector4Set(corner[i], (i & 1) ? maxs[0] : mins[0], (i & 2) ? maxs[1] : mins[1], (i & 4) ? maxs[2] : mins[2], 1);
535 dist[i] = DotProduct4(corner[i], plane4f);
536 sign[i] = dist[i] > 0;
539 VectorCopy(corner[i], vertex[numvertices]);
543 // if some points are behind the nearclip, add clipped edge points to make
544 // sure that the scissor boundary is complete
545 if (numvertices > 0 && numvertices < 8)
547 // add clipped edge points
548 for (i = 0;i < 12;i++)
552 if (sign[j] != sign[k])
554 f = dist[j] / (dist[j] - dist[k]);
555 VectorLerp(corner[j], f, corner[k], vertex[numvertices]);
561 // if we have no points to check, it is behind the view plane
565 // if we have some points to transform, check what screen area is covered
566 x1 = y1 = x2 = y2 = 0;
568 //Con_Printf("%i vertices to transform...\n", numvertices);
569 for (i = 0;i < numvertices;i++)
571 VectorCopy(vertex[i], v);
572 R_Viewport_TransformToScreen(&r_refdef.view.viewport, v, v2);
573 //Con_Printf("%.3f %.3f %.3f %.3f transformed to %.3f %.3f %.3f %.3f\n", v[0], v[1], v[2], v[3], v2[0], v2[1], v2[2], v2[3]);
576 if (x1 > v2[0]) x1 = v2[0];
577 if (x2 < v2[0]) x2 = v2[0];
578 if (y1 > v2[1]) y1 = v2[1];
579 if (y2 < v2[1]) y2 = v2[1];
588 // now convert the scissor rectangle to integer screen coordinates
589 ix1 = (int)(x1 - 1.0f);
590 //iy1 = vid.height - (int)(y2 - 1.0f);
591 //iy1 = r_refdef.view.viewport.width + 2 * r_refdef.view.viewport.x - (int)(y2 - 1.0f);
592 iy1 = (int)(y1 - 1.0f);
593 ix2 = (int)(x2 + 1.0f);
594 //iy2 = vid.height - (int)(y1 + 1.0f);
595 //iy2 = r_refdef.view.viewport.height + 2 * r_refdef.view.viewport.y - (int)(y1 + 1.0f);
596 iy2 = (int)(y2 + 1.0f);
597 //Con_Printf("%f %f %f %f\n", x1, y1, x2, y2);
599 // clamp it to the screen
600 if (ix1 < r_refdef.view.viewport.x) ix1 = r_refdef.view.viewport.x;
601 if (iy1 < r_refdef.view.viewport.y) iy1 = r_refdef.view.viewport.y;
602 if (ix2 > r_refdef.view.viewport.x + r_refdef.view.viewport.width) ix2 = r_refdef.view.viewport.x + r_refdef.view.viewport.width;
603 if (iy2 > r_refdef.view.viewport.y + r_refdef.view.viewport.height) iy2 = r_refdef.view.viewport.y + r_refdef.view.viewport.height;
605 // if it is inside out, it's not visible
606 if (ix2 <= ix1 || iy2 <= iy1)
609 // the light area is visible, set up the scissor rectangle
612 scissor[2] = ix2 - ix1;
613 scissor[3] = iy2 - iy1;
615 // D3D Y coordinate is top to bottom, OpenGL is bottom to top, fix the D3D one
616 switch(vid.renderpath)
618 case RENDERPATH_GL20:
619 case RENDERPATH_GLES2:
627 static void R_Viewport_ApplyNearClipPlaneFloatGL(const r_viewport_t *v, float *m, float normalx, float normaly, float normalz, float dist)
631 float clipPlane[4], v3[3], v4[3];
634 // This is inspired by Oblique Depth Projection from http://www.terathon.com/code/oblique.php
636 VectorSet(normal, normalx, normaly, normalz);
637 Matrix4x4_Transform3x3(&v->viewmatrix, normal, clipPlane);
638 VectorScale(normal, -dist, v3);
639 Matrix4x4_Transform(&v->viewmatrix, v3, v4);
640 // FIXME: LordHavoc: I think this can be done more efficiently somehow but I can't remember the technique
641 clipPlane[3] = -DotProduct(v4, clipPlane);
645 // testing code for comparing results
647 VectorCopy4(clipPlane, clipPlane2);
648 R_EntityMatrix(&identitymatrix);
649 VectorSet(q, normal[0], normal[1], normal[2], -dist);
650 qglClipPlane(GL_CLIP_PLANE0, q);
651 qglGetClipPlane(GL_CLIP_PLANE0, q);
652 VectorCopy4(q, clipPlane);
656 // Calculate the clip-space corner point opposite the clipping plane
657 // as (sgn(clipPlane.x), sgn(clipPlane.y), 1, 1) and
658 // transform it into camera space by multiplying it
659 // by the inverse of the projection matrix
660 q[0] = ((clipPlane[0] < 0.0f ? -1.0f : clipPlane[0] > 0.0f ? 1.0f : 0.0f) + m[8]) / m[0];
661 q[1] = ((clipPlane[1] < 0.0f ? -1.0f : clipPlane[1] > 0.0f ? 1.0f : 0.0f) + m[9]) / m[5];
663 q[3] = (1.0f + m[10]) / m[14];
665 // Calculate the scaled plane vector
666 d = 2.0f / DotProduct4(clipPlane, q);
668 // Replace the third row of the projection matrix
669 m[2] = clipPlane[0] * d;
670 m[6] = clipPlane[1] * d;
671 m[10] = clipPlane[2] * d + 1.0f;
672 m[14] = clipPlane[3] * d;
675 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)
677 float left = x1, right = x2, bottom = y2, top = y1, zNear = nearclip, zFar = farclip;
679 memset(v, 0, sizeof(*v));
680 v->type = R_VIEWPORTTYPE_ORTHO;
681 v->cameramatrix = *cameramatrix;
688 memset(m, 0, sizeof(m));
689 m[0] = 2/(right - left);
690 m[5] = 2/(top - bottom);
691 m[10] = -2/(zFar - zNear);
692 m[12] = - (right + left)/(right - left);
693 m[13] = - (top + bottom)/(top - bottom);
694 m[14] = - (zFar + zNear)/(zFar - zNear);
696 switch(vid.renderpath)
698 case RENDERPATH_GL20:
699 case RENDERPATH_GLES2:
702 v->screentodepth[0] = -farclip / (farclip - nearclip);
703 v->screentodepth[1] = farclip * nearclip / (farclip - nearclip);
705 Matrix4x4_Invert_Full(&v->viewmatrix, &v->cameramatrix);
708 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
710 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
716 Vector4Set(test1, (x1+x2)*0.5f, (y1+y2)*0.5f, 0.0f, 1.0f);
717 R_Viewport_TransformToScreen(v, test1, test2);
718 Con_Printf("%f %f %f -> %f %f %f\n", test1[0], test1[1], test1[2], test2[0], test2[1], test2[2]);
723 void R_Viewport_InitOrtho3D(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)
725 matrix4x4_t tempmatrix, basematrix;
727 memset(v, 0, sizeof(*v));
729 v->type = R_VIEWPORTTYPE_PERSPECTIVE;
730 v->cameramatrix = *cameramatrix;
737 memset(m, 0, sizeof(m));
738 m[0] = 1.0 / frustumx;
739 m[5] = 1.0 / frustumy;
740 m[10] = -2 / (farclip - nearclip);
741 m[14] = -(farclip + nearclip) / (farclip - nearclip);
743 v->screentodepth[0] = -farclip / (farclip - nearclip);
744 v->screentodepth[1] = farclip * nearclip / (farclip - nearclip);
746 Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
747 Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
748 Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
749 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
752 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
754 if(v_flipped.integer)
762 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
765 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)
767 matrix4x4_t tempmatrix, basematrix;
769 memset(v, 0, sizeof(*v));
771 v->type = R_VIEWPORTTYPE_PERSPECTIVE;
772 v->cameramatrix = *cameramatrix;
779 memset(m, 0, sizeof(m));
780 m[0] = 1.0 / frustumx;
781 m[5] = 1.0 / frustumy;
782 m[10] = -(farclip + nearclip) / (farclip - nearclip);
784 m[14] = -2 * nearclip * farclip / (farclip - nearclip);
785 v->screentodepth[0] = -farclip / (farclip - nearclip);
786 v->screentodepth[1] = farclip * nearclip / (farclip - nearclip);
788 Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
789 Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
790 Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
791 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
794 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
796 if(v_flipped.integer)
804 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
807 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)
809 matrix4x4_t tempmatrix, basematrix;
810 const float nudge = 1.0 - 1.0 / (1<<23);
812 memset(v, 0, sizeof(*v));
814 v->type = R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP;
815 v->cameramatrix = *cameramatrix;
822 memset(m, 0, sizeof(m));
823 m[ 0] = 1.0 / frustumx;
824 m[ 5] = 1.0 / frustumy;
827 m[14] = -2 * nearclip * nudge;
828 v->screentodepth[0] = (m[10] + 1) * 0.5 - 1;
829 v->screentodepth[1] = m[14] * -0.5;
831 Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
832 Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
833 Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
834 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
837 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
839 if(v_flipped.integer)
847 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
850 float cubeviewmatrix[6][16] =
852 // standard cubemap projections
890 float rectviewmatrix[6][16] =
892 // sign-preserving cubemap projections
931 void R_Viewport_InitCubeSideView(r_viewport_t *v, const matrix4x4_t *cameramatrix, int side, int size, float nearclip, float farclip, const float *nearplane)
933 matrix4x4_t tempmatrix, basematrix;
935 memset(v, 0, sizeof(*v));
936 v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
937 v->cameramatrix = *cameramatrix;
941 memset(m, 0, sizeof(m));
943 m[10] = -(farclip + nearclip) / (farclip - nearclip);
945 m[14] = -2 * nearclip * farclip / (farclip - nearclip);
947 Matrix4x4_FromArrayFloatGL(&basematrix, cubeviewmatrix[side]);
948 Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
949 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
952 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
954 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
957 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, int offsetx, int offsety)
959 matrix4x4_t tempmatrix, basematrix;
961 memset(v, 0, sizeof(*v));
962 v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
963 v->cameramatrix = *cameramatrix;
964 v->x = offsetx + (side & 1) * size;
965 v->y = offsety + (side >> 1) * size;
969 memset(m, 0, sizeof(m));
970 m[0] = m[5] = 1.0f * ((float)size - border) / size;
971 m[10] = -(farclip + nearclip) / (farclip - nearclip);
973 m[14] = -2 * nearclip * farclip / (farclip - nearclip);
975 Matrix4x4_FromArrayFloatGL(&basematrix, rectviewmatrix[side]);
976 Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
977 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
980 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
982 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
985 void R_SetViewport(const r_viewport_t *v)
989 // FIXME: v_flipped_state is evil, this probably breaks somewhere
990 GL_SetMirrorState(v_flipped.integer && (v->type == R_VIEWPORTTYPE_PERSPECTIVE || v->type == R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP));
992 // copy over the matrices to our state
993 gl_viewmatrix = v->viewmatrix;
994 gl_projectionmatrix = v->projectmatrix;
996 switch(vid.renderpath)
998 case RENDERPATH_GL20:
999 case RENDERPATH_GLES2:
1001 qglViewport(v->x, v->y, v->width, v->height);CHECKGLERROR
1005 // force an update of the derived matrices
1006 gl_modelmatrixchanged = true;
1007 R_EntityMatrix(&gl_modelmatrix);
1010 void R_GetViewport(r_viewport_t *v)
1015 static void GL_BindVBO(int bufferobject)
1017 if (gl_state.vertexbufferobject != bufferobject)
1019 gl_state.vertexbufferobject = bufferobject;
1021 qglBindBufferARB(GL_ARRAY_BUFFER, bufferobject);CHECKGLERROR
1025 static void GL_BindEBO(int bufferobject)
1027 if (gl_state.elementbufferobject != bufferobject)
1029 gl_state.elementbufferobject = bufferobject;
1031 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, bufferobject);CHECKGLERROR
1035 static void GL_BindUBO(int bufferobject)
1037 if (gl_state.uniformbufferobject != bufferobject)
1039 gl_state.uniformbufferobject = bufferobject;
1040 #ifdef GL_UNIFORM_BUFFER
1042 qglBindBufferARB(GL_UNIFORM_BUFFER, bufferobject);CHECKGLERROR
1047 static const GLuint drawbuffers[4] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3};
1048 int R_Mesh_CreateFramebufferObject(rtexture_t *depthtexture, rtexture_t *colortexture, rtexture_t *colortexture2, rtexture_t *colortexture3, rtexture_t *colortexture4)
1050 switch(vid.renderpath)
1052 case RENDERPATH_GL20:
1053 case RENDERPATH_GLES2:
1054 if (vid.support.arb_framebuffer_object)
1058 qglGenFramebuffers(1, (GLuint*)&temp);CHECKGLERROR
1059 R_Mesh_SetRenderTargets(temp, NULL, NULL, NULL, NULL, NULL);
1060 // GL_ARB_framebuffer_object (GL3-class hardware) - depth stencil attachment
1062 // FIXME: separate stencil attachment on GLES
1063 if (depthtexture && depthtexture->texnum ) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT , depthtexture->gltexturetypeenum , depthtexture->texnum , 0);CHECKGLERROR
1064 if (depthtexture && depthtexture->renderbuffernum ) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT , GL_RENDERBUFFER, depthtexture->renderbuffernum );CHECKGLERROR
1066 if (depthtexture && depthtexture->texnum )
1068 qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT , depthtexture->gltexturetypeenum , depthtexture->texnum , 0);CHECKGLERROR
1069 if (depthtexture->glisdepthstencil) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT , depthtexture->gltexturetypeenum , depthtexture->texnum , 0);CHECKGLERROR
1071 if (depthtexture && depthtexture->renderbuffernum )
1073 qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT , GL_RENDERBUFFER, depthtexture->renderbuffernum );CHECKGLERROR
1074 if (depthtexture->glisdepthstencil) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT , GL_RENDERBUFFER, depthtexture->renderbuffernum );CHECKGLERROR
1077 if (colortexture && colortexture->texnum ) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 , colortexture->gltexturetypeenum , colortexture->texnum , 0);CHECKGLERROR
1078 if (colortexture2 && colortexture2->texnum) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1 , colortexture2->gltexturetypeenum, colortexture2->texnum, 0);CHECKGLERROR
1079 if (colortexture3 && colortexture3->texnum) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2 , colortexture3->gltexturetypeenum, colortexture3->texnum, 0);CHECKGLERROR
1080 if (colortexture4 && colortexture4->texnum) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3 , colortexture4->gltexturetypeenum, colortexture4->texnum, 0);CHECKGLERROR
1081 if (colortexture && colortexture->renderbuffernum ) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 , GL_RENDERBUFFER, colortexture->renderbuffernum );CHECKGLERROR
1082 if (colortexture2 && colortexture2->renderbuffernum) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1 , GL_RENDERBUFFER, colortexture2->renderbuffernum);CHECKGLERROR
1083 if (colortexture3 && colortexture3->renderbuffernum) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2 , GL_RENDERBUFFER, colortexture3->renderbuffernum);CHECKGLERROR
1084 if (colortexture4 && colortexture4->renderbuffernum) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3 , GL_RENDERBUFFER, colortexture4->renderbuffernum);CHECKGLERROR
1087 if (colortexture4 && qglDrawBuffersARB)
1089 qglDrawBuffersARB(4, drawbuffers);CHECKGLERROR
1090 qglReadBuffer(GL_NONE);CHECKGLERROR
1092 else if (colortexture3 && qglDrawBuffersARB)
1094 qglDrawBuffersARB(3, drawbuffers);CHECKGLERROR
1095 qglReadBuffer(GL_NONE);CHECKGLERROR
1097 else if (colortexture2 && qglDrawBuffersARB)
1099 qglDrawBuffersARB(2, drawbuffers);CHECKGLERROR
1100 qglReadBuffer(GL_NONE);CHECKGLERROR
1102 else if (colortexture && qglDrawBuffer)
1104 qglDrawBuffer(GL_COLOR_ATTACHMENT0);CHECKGLERROR
1105 qglReadBuffer(GL_COLOR_ATTACHMENT0);CHECKGLERROR
1107 else if (qglDrawBuffer)
1109 qglDrawBuffer(GL_NONE);CHECKGLERROR
1110 qglReadBuffer(GL_NONE);CHECKGLERROR
1113 status = qglCheckFramebufferStatus(GL_FRAMEBUFFER);CHECKGLERROR
1114 if (status != GL_FRAMEBUFFER_COMPLETE)
1116 Con_Printf("R_Mesh_CreateFramebufferObject: glCheckFramebufferStatus returned %i\n", status);
1117 gl_state.framebufferobject = 0; // GL unbinds it for us
1118 qglDeleteFramebuffers(1, (GLuint*)&temp);
1123 else if (vid.support.ext_framebuffer_object)
1127 qglGenFramebuffers(1, (GLuint*)&temp);CHECKGLERROR
1128 R_Mesh_SetRenderTargets(temp, NULL, NULL, NULL, NULL, NULL);
1129 // GL_EXT_framebuffer_object (GL2-class hardware) - no depth stencil attachment, let it break stencil
1130 if (depthtexture && depthtexture->texnum ) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT , depthtexture->gltexturetypeenum , depthtexture->texnum , 0);CHECKGLERROR
1131 if (depthtexture && depthtexture->renderbuffernum ) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT , GL_RENDERBUFFER, depthtexture->renderbuffernum );CHECKGLERROR
1132 if (colortexture && colortexture->texnum ) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 , colortexture->gltexturetypeenum , colortexture->texnum , 0);CHECKGLERROR
1133 if (colortexture2 && colortexture2->texnum) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1 , colortexture2->gltexturetypeenum, colortexture2->texnum, 0);CHECKGLERROR
1134 if (colortexture3 && colortexture3->texnum) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2 , colortexture3->gltexturetypeenum, colortexture3->texnum, 0);CHECKGLERROR
1135 if (colortexture4 && colortexture4->texnum) qglFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3 , colortexture4->gltexturetypeenum, colortexture4->texnum, 0);CHECKGLERROR
1136 if (colortexture && colortexture->renderbuffernum ) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 , GL_RENDERBUFFER, colortexture->renderbuffernum );CHECKGLERROR
1137 if (colortexture2 && colortexture2->renderbuffernum) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1 , GL_RENDERBUFFER, colortexture2->renderbuffernum);CHECKGLERROR
1138 if (colortexture3 && colortexture3->renderbuffernum) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2 , GL_RENDERBUFFER, colortexture3->renderbuffernum);CHECKGLERROR
1139 if (colortexture4 && colortexture4->renderbuffernum) qglFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3 , GL_RENDERBUFFER, colortexture4->renderbuffernum);CHECKGLERROR
1142 if (colortexture4 && qglDrawBuffersARB)
1144 qglDrawBuffersARB(4, drawbuffers);CHECKGLERROR
1145 qglReadBuffer(GL_NONE);CHECKGLERROR
1147 else if (colortexture3 && qglDrawBuffersARB)
1149 qglDrawBuffersARB(3, drawbuffers);CHECKGLERROR
1150 qglReadBuffer(GL_NONE);CHECKGLERROR
1152 else if (colortexture2 && qglDrawBuffersARB)
1154 qglDrawBuffersARB(2, drawbuffers);CHECKGLERROR
1155 qglReadBuffer(GL_NONE);CHECKGLERROR
1157 else if (colortexture && qglDrawBuffer)
1159 qglDrawBuffer(GL_COLOR_ATTACHMENT0);CHECKGLERROR
1160 qglReadBuffer(GL_COLOR_ATTACHMENT0);CHECKGLERROR
1162 else if (qglDrawBuffer)
1164 qglDrawBuffer(GL_NONE);CHECKGLERROR
1165 qglReadBuffer(GL_NONE);CHECKGLERROR
1168 status = qglCheckFramebufferStatus(GL_FRAMEBUFFER);CHECKGLERROR
1169 if (status != GL_FRAMEBUFFER_COMPLETE)
1171 Con_Printf("R_Mesh_CreateFramebufferObject: glCheckFramebufferStatus returned %i\n", status);
1172 gl_state.framebufferobject = 0; // GL unbinds it for us
1173 qglDeleteFramebuffers(1, (GLuint*)&temp);
1183 void R_Mesh_DestroyFramebufferObject(int fbo)
1185 switch(vid.renderpath)
1187 case RENDERPATH_GL20:
1188 case RENDERPATH_GLES2:
1191 // GL clears the binding if we delete something bound
1192 if (gl_state.framebufferobject == fbo)
1193 gl_state.framebufferobject = 0;
1194 qglDeleteFramebuffers(1, (GLuint*)&fbo);
1200 void R_Mesh_SetRenderTargets(int fbo, rtexture_t *depthtexture, rtexture_t *colortexture, rtexture_t *colortexture2, rtexture_t *colortexture3, rtexture_t *colortexture4)
1204 rtexture_t *textures[5];
1205 Vector4Set(textures, colortexture, colortexture2, colortexture3, colortexture4);
1206 textures[4] = depthtexture;
1207 // unbind any matching textures immediately, otherwise D3D will complain about a bound texture being used as a render target
1208 for (j = 0;j < 5;j++)
1210 for (i = 0;i < vid.teximageunits;i++)
1211 if (gl_state.units[i].texture == textures[j])
1212 R_Mesh_TexBind(i, NULL);
1213 // set up framebuffer object or render targets for the active rendering API
1214 switch (vid.renderpath)
1216 case RENDERPATH_GL20:
1217 case RENDERPATH_GLES2:
1218 if (gl_state.framebufferobject != fbo)
1220 gl_state.framebufferobject = fbo;
1221 qglBindFramebuffer(GL_FRAMEBUFFER, gl_state.framebufferobject ? gl_state.framebufferobject : gl_state.defaultframebufferobject);
1227 static void GL_Backend_ResetState(void)
1230 gl_state.active = true;
1231 gl_state.depthtest = true;
1232 gl_state.alphatest = false;
1233 gl_state.alphafunc = GL_GEQUAL;
1234 gl_state.alphafuncvalue = 0.5f;
1235 gl_state.alphatocoverage = false;
1236 gl_state.blendfunc1 = GL_ONE;
1237 gl_state.blendfunc2 = GL_ZERO;
1238 gl_state.blend = false;
1239 gl_state.depthmask = GL_TRUE;
1240 gl_state.colormask = 15;
1241 gl_state.color4f[0] = gl_state.color4f[1] = gl_state.color4f[2] = gl_state.color4f[3] = 1;
1242 gl_state.lockrange_first = 0;
1243 gl_state.lockrange_count = 0;
1244 gl_state.cullface = GL_FRONT;
1245 gl_state.cullfaceenable = false;
1246 gl_state.polygonoffset[0] = 0;
1247 gl_state.polygonoffset[1] = 0;
1248 gl_state.framebufferobject = 0;
1249 gl_state.depthfunc = GL_LEQUAL;
1251 switch(vid.renderpath)
1253 case RENDERPATH_GL20:
1254 case RENDERPATH_GLES2:
1256 qglColorMask(1, 1, 1, 1);CHECKGLERROR
1257 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
1258 qglDisable(GL_BLEND);CHECKGLERROR
1259 qglCullFace(gl_state.cullface);CHECKGLERROR
1260 qglDisable(GL_CULL_FACE);CHECKGLERROR
1261 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
1262 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1263 qglDepthMask(gl_state.depthmask);CHECKGLERROR
1264 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
1265 if (vid.support.arb_vertex_buffer_object)
1267 qglBindBufferARB(GL_ARRAY_BUFFER, 0);
1268 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER, 0);
1270 if (vid.support.ext_framebuffer_object)
1271 qglBindFramebuffer(GL_FRAMEBUFFER, gl_state.defaultframebufferobject);
1272 qglEnableVertexAttribArray(GLSLATTRIB_POSITION);
1273 qglVertexAttribPointer(GLSLATTRIB_POSITION, 3, GL_FLOAT, false, sizeof(float[3]), NULL);CHECKGLERROR
1274 qglDisableVertexAttribArray(GLSLATTRIB_COLOR);
1275 qglVertexAttribPointer(GLSLATTRIB_COLOR, 4, GL_FLOAT, false, sizeof(float[4]), NULL);CHECKGLERROR
1276 qglVertexAttrib4f(GLSLATTRIB_COLOR, 1, 1, 1, 1);
1277 gl_state.unit = MAX_TEXTUREUNITS;
1278 gl_state.clientunit = MAX_TEXTUREUNITS;
1279 for (i = 0;i < vid.teximageunits;i++)
1281 GL_ActiveTexture(i);
1282 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
1283 if (vid.support.ext_texture_3d)
1285 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
1287 if (vid.support.arb_texture_cube_map)
1289 qglBindTexture(GL_TEXTURE_CUBE_MAP, 0);CHECKGLERROR
1292 for (i = 0;i < vid.texarrayunits;i++)
1295 qglVertexAttribPointer(i+GLSLATTRIB_TEXCOORD0, 2, GL_FLOAT, false, sizeof(float[2]), NULL);CHECKGLERROR
1296 qglDisableVertexAttribArray(i+GLSLATTRIB_TEXCOORD0);CHECKGLERROR
1303 void GL_ActiveTexture(unsigned int num)
1305 if (gl_state.unit != num)
1307 gl_state.unit = num;
1308 switch(vid.renderpath)
1310 case RENDERPATH_GL20:
1311 case RENDERPATH_GLES2:
1313 qglActiveTexture(GL_TEXTURE0 + gl_state.unit);
1320 void GL_BlendFunc(int blendfunc1, int blendfunc2)
1322 if (gl_state.blendfunc1 != blendfunc1 || gl_state.blendfunc2 != blendfunc2)
1324 qboolean blendenable;
1325 gl_state.blendfunc1 = blendfunc1;
1326 gl_state.blendfunc2 = blendfunc2;
1327 blendenable = (gl_state.blendfunc1 != GL_ONE || gl_state.blendfunc2 != GL_ZERO);
1328 switch(vid.renderpath)
1330 case RENDERPATH_GL20:
1331 case RENDERPATH_GLES2:
1333 if (qglBlendFuncSeparate)
1335 qglBlendFuncSeparate(gl_state.blendfunc1, gl_state.blendfunc2, GL_ZERO, GL_ONE);CHECKGLERROR // ELUAN: Adreno 225 (and others) compositing workaround
1339 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
1341 if (gl_state.blend != blendenable)
1343 gl_state.blend = blendenable;
1344 if (!gl_state.blend)
1346 qglDisable(GL_BLEND);CHECKGLERROR
1350 qglEnable(GL_BLEND);CHECKGLERROR
1358 void GL_DepthMask(int state)
1360 if (gl_state.depthmask != state)
1362 gl_state.depthmask = state;
1363 switch(vid.renderpath)
1365 case RENDERPATH_GL20:
1366 case RENDERPATH_GLES2:
1368 qglDepthMask(gl_state.depthmask);CHECKGLERROR
1374 void GL_DepthTest(int state)
1376 if (gl_state.depthtest != state)
1378 gl_state.depthtest = state;
1379 switch(vid.renderpath)
1381 case RENDERPATH_GL20:
1382 case RENDERPATH_GLES2:
1384 if (gl_state.depthtest)
1386 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1390 qglDisable(GL_DEPTH_TEST);CHECKGLERROR
1397 void GL_DepthFunc(int state)
1399 if (gl_state.depthfunc != state)
1401 gl_state.depthfunc = state;
1402 switch(vid.renderpath)
1404 case RENDERPATH_GL20:
1405 case RENDERPATH_GLES2:
1407 qglDepthFunc(gl_state.depthfunc);CHECKGLERROR
1413 void GL_DepthRange(float nearfrac, float farfrac)
1415 if (gl_state.depthrange[0] != nearfrac || gl_state.depthrange[1] != farfrac)
1417 gl_state.depthrange[0] = nearfrac;
1418 gl_state.depthrange[1] = farfrac;
1419 switch(vid.renderpath)
1421 case RENDERPATH_GL20:
1422 case RENDERPATH_GLES2:
1424 qglDepthRangef(gl_state.depthrange[0], gl_state.depthrange[1]);
1426 qglDepthRange(gl_state.depthrange[0], gl_state.depthrange[1]);
1433 void R_SetStencil(qboolean enable, int writemask, int fail, int zfail, int zpass, int compare, int comparereference, int comparemask)
1435 switch (vid.renderpath)
1437 case RENDERPATH_GL20:
1438 case RENDERPATH_GLES2:
1442 qglEnable(GL_STENCIL_TEST);CHECKGLERROR
1446 qglDisable(GL_STENCIL_TEST);CHECKGLERROR
1448 qglStencilMask(writemask);CHECKGLERROR
1449 qglStencilOp(fail, zfail, zpass);CHECKGLERROR
1450 qglStencilFunc(compare, comparereference, comparemask);CHECKGLERROR
1456 void GL_PolygonOffset(float planeoffset, float depthoffset)
1458 if (gl_state.polygonoffset[0] != planeoffset || gl_state.polygonoffset[1] != depthoffset)
1460 gl_state.polygonoffset[0] = planeoffset;
1461 gl_state.polygonoffset[1] = depthoffset;
1462 switch(vid.renderpath)
1464 case RENDERPATH_GL20:
1465 case RENDERPATH_GLES2:
1466 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
1472 void GL_SetMirrorState(qboolean state)
1474 if (v_flipped_state != state)
1476 v_flipped_state = state;
1477 if (gl_state.cullface == GL_BACK)
1478 gl_state.cullface = GL_FRONT;
1479 else if (gl_state.cullface == GL_FRONT)
1480 gl_state.cullface = GL_BACK;
1483 switch(vid.renderpath)
1485 case RENDERPATH_GL20:
1486 case RENDERPATH_GLES2:
1487 qglCullFace(gl_state.cullface);CHECKGLERROR
1493 void GL_CullFace(int state)
1497 if(state == GL_FRONT)
1499 else if(state == GL_BACK)
1503 switch(vid.renderpath)
1505 case RENDERPATH_GL20:
1506 case RENDERPATH_GLES2:
1509 if (state != GL_NONE)
1511 if (!gl_state.cullfaceenable)
1513 gl_state.cullfaceenable = true;
1514 qglEnable(GL_CULL_FACE);CHECKGLERROR
1516 if (gl_state.cullface != state)
1518 gl_state.cullface = state;
1519 qglCullFace(gl_state.cullface);CHECKGLERROR
1524 if (gl_state.cullfaceenable)
1526 gl_state.cullfaceenable = false;
1527 qglDisable(GL_CULL_FACE);CHECKGLERROR
1534 void GL_AlphaToCoverage(qboolean state)
1536 if (gl_state.alphatocoverage != state)
1538 gl_state.alphatocoverage = state;
1539 switch(vid.renderpath)
1541 case RENDERPATH_GLES2:
1543 case RENDERPATH_GL20:
1544 #ifdef GL_SAMPLE_ALPHA_TO_COVERAGE_ARB
1545 // alpha to coverage turns the alpha value of the pixel into 0%, 25%, 50%, 75% or 100% by masking the multisample fragments accordingly
1547 if (gl_state.alphatocoverage)
1549 qglEnable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);CHECKGLERROR
1550 // qglEnable(GL_MULTISAMPLE_ARB);CHECKGLERROR
1554 qglDisable(GL_SAMPLE_ALPHA_TO_COVERAGE_ARB);CHECKGLERROR
1555 // qglDisable(GL_MULTISAMPLE_ARB);CHECKGLERROR
1563 void GL_ColorMask(int r, int g, int b, int a)
1565 // NOTE: this matches D3DCOLORWRITEENABLE_RED, GREEN, BLUE, ALPHA
1566 int state = (r ? 1 : 0) | (g ? 2 : 0) | (b ? 4 : 0) | (a ? 8 : 0);
1567 if (gl_state.colormask != state)
1569 gl_state.colormask = state;
1570 switch(vid.renderpath)
1572 case RENDERPATH_GL20:
1573 case RENDERPATH_GLES2:
1575 qglColorMask((GLboolean)r, (GLboolean)g, (GLboolean)b, (GLboolean)a);CHECKGLERROR
1581 void GL_Color(float cr, float cg, float cb, float ca)
1583 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)
1585 gl_state.color4f[0] = cr;
1586 gl_state.color4f[1] = cg;
1587 gl_state.color4f[2] = cb;
1588 gl_state.color4f[3] = ca;
1589 switch(vid.renderpath)
1591 case RENDERPATH_GL20:
1592 case RENDERPATH_GLES2:
1593 qglVertexAttrib4f(GLSLATTRIB_COLOR, cr, cg, cb, ca);
1599 void GL_Scissor (int x, int y, int width, int height)
1601 switch(vid.renderpath)
1603 case RENDERPATH_GL20:
1604 case RENDERPATH_GLES2:
1606 qglScissor(x, y,width,height);
1612 void GL_ScissorTest(int state)
1614 if (gl_state.scissortest != state)
1616 gl_state.scissortest = state;
1617 switch(vid.renderpath)
1619 case RENDERPATH_GL20:
1620 case RENDERPATH_GLES2:
1622 if(gl_state.scissortest)
1623 qglEnable(GL_SCISSOR_TEST);
1625 qglDisable(GL_SCISSOR_TEST);
1632 void GL_Clear(int mask, const float *colorvalue, float depthvalue, int stencilvalue)
1634 // opaque black - if you want transparent black, you'll need to pass in a colorvalue
1635 static const float blackcolor[4] = {0.0f, 0.0f, 0.0f, 1.0f};
1636 // prevent warnings when trying to clear a buffer that does not exist
1638 colorvalue = blackcolor;
1641 mask &= ~GL_STENCIL_BUFFER_BIT;
1644 switch(vid.renderpath)
1646 case RENDERPATH_GL20:
1647 case RENDERPATH_GLES2:
1649 if (mask & GL_COLOR_BUFFER_BIT)
1651 qglClearColor(colorvalue[0], colorvalue[1], colorvalue[2], colorvalue[3]);CHECKGLERROR
1653 if (mask & GL_DEPTH_BUFFER_BIT)
1656 qglClearDepthf(depthvalue);CHECKGLERROR
1658 qglClearDepth(depthvalue);CHECKGLERROR
1661 if (mask & GL_STENCIL_BUFFER_BIT)
1663 qglClearStencil(stencilvalue);CHECKGLERROR
1665 qglClear(mask);CHECKGLERROR
1670 void GL_ReadPixelsBGRA(int x, int y, int width, int height, unsigned char *outpixels)
1672 switch(vid.renderpath)
1674 case RENDERPATH_GL20:
1675 case RENDERPATH_GLES2:
1684 qglReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, outpixels);CHECKGLERROR
1685 for (i = 0;i < width * height * 4;i += 4)
1688 // g = outpixels[i+1];
1690 // a = outpixels[i+3];
1692 // outpixels[i+1] = g;
1694 // outpixels[i+3] = a;
1698 qglReadPixels(x, y, width, height, GL_BGRA, GL_UNSIGNED_BYTE, outpixels);CHECKGLERROR
1704 // called at beginning of frame
1705 void R_Mesh_Start(void)
1708 R_Mesh_SetRenderTargets(0, NULL, NULL, NULL, NULL, NULL);
1710 if (gl_printcheckerror.integer && !gl_paranoid.integer)
1712 Con_Printf("WARNING: gl_printcheckerror is on but gl_paranoid is off, turning it on...\n");
1713 Cvar_SetValueQuick(&gl_paranoid, 1);
1717 static qboolean GL_Backend_CompileShader(int programobject, GLenum shadertypeenum, const char *shadertype, int numstrings, const char **strings)
1721 char compilelog[MAX_INPUTLINE];
1722 shaderobject = qglCreateShader(shadertypeenum);CHECKGLERROR
1725 qglShaderSource(shaderobject, numstrings, strings, NULL);CHECKGLERROR
1726 qglCompileShader(shaderobject);CHECKGLERROR
1727 qglGetShaderiv(shaderobject, GL_COMPILE_STATUS, &shadercompiled);CHECKGLERROR
1728 qglGetShaderInfoLog(shaderobject, sizeof(compilelog), NULL, compilelog);CHECKGLERROR
1729 if (compilelog[0] && ((strstr(compilelog, "error") || strstr(compilelog, "ERROR") || strstr(compilelog, "Error")) || ((strstr(compilelog, "WARNING") || strstr(compilelog, "warning") || strstr(compilelog, "Warning")) && developer.integer) || developer_extra.integer))
1731 int i, j, pretextlines = 0;
1732 for (i = 0;i < numstrings - 1;i++)
1733 for (j = 0;strings[i][j];j++)
1734 if (strings[i][j] == '\n')
1736 Con_Printf("%s shader compile log:\n%s\n(line offset for any above warnings/errors: %i)\n", shadertype, compilelog, pretextlines);
1738 if (!shadercompiled)
1740 qglDeleteShader(shaderobject);CHECKGLERROR
1743 qglAttachShader(programobject, shaderobject);CHECKGLERROR
1744 qglDeleteShader(shaderobject);CHECKGLERROR
1748 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)
1750 GLint programlinked;
1751 GLuint programobject = 0;
1752 char linklog[MAX_INPUTLINE];
1755 programobject = qglCreateProgram();CHECKGLERROR
1759 qglBindAttribLocation(programobject, GLSLATTRIB_POSITION , "Attrib_Position" );
1760 qglBindAttribLocation(programobject, GLSLATTRIB_COLOR , "Attrib_Color" );
1761 qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD0, "Attrib_TexCoord0");
1762 qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD1, "Attrib_TexCoord1");
1763 qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD2, "Attrib_TexCoord2");
1764 qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD3, "Attrib_TexCoord3");
1765 qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD4, "Attrib_TexCoord4");
1766 qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD5, "Attrib_TexCoord5");
1767 qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD6, "Attrib_SkeletalIndex");
1768 qglBindAttribLocation(programobject, GLSLATTRIB_TEXCOORD7, "Attrib_SkeletalWeight");
1770 if(vid.support.gl20shaders130)
1771 qglBindFragDataLocation(programobject, 0, "dp_FragColor");
1774 if (vertexstrings_count && !GL_Backend_CompileShader(programobject, GL_VERTEX_SHADER, "vertex", vertexstrings_count, vertexstrings_list))
1777 #if defined(GL_GEOMETRY_SHADER) && !defined(USE_GLES2)
1778 if (geometrystrings_count && !GL_Backend_CompileShader(programobject, GL_GEOMETRY_SHADER, "geometry", geometrystrings_count, geometrystrings_list))
1782 if (fragmentstrings_count && !GL_Backend_CompileShader(programobject, GL_FRAGMENT_SHADER, "fragment", fragmentstrings_count, fragmentstrings_list))
1785 qglLinkProgram(programobject);CHECKGLERROR
1786 qglGetProgramiv(programobject, GL_LINK_STATUS, &programlinked);CHECKGLERROR
1787 qglGetProgramInfoLog(programobject, sizeof(linklog), NULL, linklog);CHECKGLERROR
1792 if (strstr(linklog, "error") || strstr(linklog, "ERROR") || strstr(linklog, "Error") || strstr(linklog, "WARNING") || strstr(linklog, "warning") || strstr(linklog, "Warning") || developer_extra.integer)
1793 Con_DPrintf("program link log:\n%s\n", linklog);
1795 // software vertex shader is ok but software fragment shader is WAY
1796 // too slow, fail program if so.
1797 // NOTE: this string might be ATI specific, but that's ok because the
1798 // ATI R300 chip (Radeon 9500-9800/X300) is the most likely to use a
1799 // software fragment shader due to low instruction and dependent
1801 if (strstr(linklog, "fragment shader will run in software"))
1802 programlinked = false;
1808 return programobject;
1810 qglDeleteProgram(programobject);CHECKGLERROR
1814 void GL_Backend_FreeProgram(unsigned int prog)
1817 qglDeleteProgram(prog);
1821 // renders triangles using vertices from the active arrays
1822 int paranoidblah = 0;
1823 void R_Mesh_Draw(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const r_meshbuffer_t *element3i_indexbuffer, int element3i_bufferoffset, const unsigned short *element3s, const r_meshbuffer_t *element3s_indexbuffer, int element3s_bufferoffset)
1825 unsigned int numelements = numtriangles * 3;
1827 size_t bufferoffset3i;
1829 size_t bufferoffset3s;
1830 if (numvertices < 3 || numtriangles < 1)
1832 if (numvertices < 0 || numtriangles < 0 || developer_extra.integer)
1833 Con_DPrintf("R_Mesh_Draw(%d, %d, %d, %d, %8p, %8p, %8x, %8p, %8p, %8x);\n", firstvertex, numvertices, firsttriangle, numtriangles, (void *)element3i, (void *)element3i_indexbuffer, (int)element3i_bufferoffset, (void *)element3s, (void *)element3s_indexbuffer, (int)element3s_bufferoffset);
1836 // adjust the pointers for firsttriangle
1838 element3i += firsttriangle * 3;
1839 if (element3i_indexbuffer)
1840 element3i_bufferoffset += firsttriangle * 3 * sizeof(*element3i);
1842 element3s += firsttriangle * 3;
1843 if (element3s_indexbuffer)
1844 element3s_bufferoffset += firsttriangle * 3 * sizeof(*element3s);
1845 switch(vid.renderpath)
1847 case RENDERPATH_GL20:
1848 case RENDERPATH_GLES2:
1849 // check if the user specified to ignore static index buffers
1850 if (!gl_state.usevbo_staticindex || (gl_vbo.integer == 3 && !vid.forcevbo && (element3i_bufferoffset || element3s_bufferoffset)))
1852 element3i_indexbuffer = NULL;
1853 element3s_indexbuffer = NULL;
1857 // upload a dynamic index buffer if needed
1860 if (!element3s_indexbuffer && gl_state.usevbo_dynamicindex)
1861 element3s_indexbuffer = R_BufferData_Store(numelements * sizeof(*element3s), (void *)element3s, R_BUFFERDATA_INDEX16, &element3s_bufferoffset);
1865 if (!element3i_indexbuffer && gl_state.usevbo_dynamicindex)
1866 element3i_indexbuffer = R_BufferData_Store(numelements * sizeof(*element3i), (void *)element3i, R_BUFFERDATA_INDEX32, &element3i_bufferoffset);
1868 bufferobject3i = element3i_indexbuffer ? element3i_indexbuffer->bufferobject : 0;
1869 bufferoffset3i = element3i_bufferoffset;
1870 bufferobject3s = element3s_indexbuffer ? element3s_indexbuffer->bufferobject : 0;
1871 bufferoffset3s = element3s_bufferoffset;
1872 r_refdef.stats[r_stat_draws]++;
1873 r_refdef.stats[r_stat_draws_vertices] += numvertices;
1874 r_refdef.stats[r_stat_draws_elements] += numelements;
1875 if (gl_paranoid.integer)
1878 // LordHavoc: disabled this - it needs to be updated to handle components and gltype and stride in each array
1880 unsigned int j, size;
1882 // note: there's no validation done here on buffer objects because it
1883 // is somewhat difficult to get at the data, and gl_paranoid can be
1884 // used without buffer objects if the need arises
1885 // (the data could be gotten using glMapBuffer but it would be very
1886 // slow due to uncachable video memory reads)
1887 if (!qglIsEnabled(GL_VERTEX_ARRAY))
1888 Con_Print("R_Mesh_Draw: vertex array not enabled\n");
1890 if (gl_state.pointer_vertex_pointer)
1891 for (j = 0, size = numvertices * 3, p = (int *)((float *)gl_state.pointer_vertex + firstvertex * 3);j < size;j++, p++)
1893 if (gl_state.pointer_color_enabled)
1895 if (!qglIsEnabled(GL_COLOR_ARRAY))
1896 Con_Print("R_Mesh_Draw: color array set but not enabled\n");
1898 if (gl_state.pointer_color && gl_state.pointer_color_enabled)
1899 for (j = 0, size = numvertices * 4, p = (int *)((float *)gl_state.pointer_color + firstvertex * 4);j < size;j++, p++)
1902 for (i = 0;i < vid.texarrayunits;i++)
1904 if (gl_state.units[i].arrayenabled)
1906 GL_ClientActiveTexture(i);
1907 if (!qglIsEnabled(GL_TEXTURE_COORD_ARRAY))
1908 Con_Print("R_Mesh_Draw: texcoord array set but not enabled\n");
1910 if (gl_state.units[i].pointer_texcoord && gl_state.units[i].arrayenabled)
1911 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++)
1918 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1920 if (element3i[i] < firstvertex || element3i[i] >= firstvertex + numvertices)
1922 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3i array\n", element3i[i], firstvertex, firstvertex + numvertices);
1929 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
1931 if (element3s[i] < firstvertex || element3s[i] >= firstvertex + numvertices)
1933 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3s array\n", element3s[i], firstvertex, firstvertex + numvertices);
1939 if (r_render.integer || r_refdef.draw2dstage)
1941 switch(vid.renderpath)
1943 case RENDERPATH_GL20:
1947 GL_BindEBO(bufferobject3s);
1949 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1951 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, (void *)bufferoffset3s);
1957 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, (void *)bufferoffset3s);
1961 else if (bufferobject3i)
1963 GL_BindEBO(bufferobject3i);
1965 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1967 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, (void *)bufferoffset3i);
1973 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, (void *)bufferoffset3i);
1981 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1983 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, element3s);
1989 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, element3s);
1997 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
1999 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, element3i);
2005 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, element3i);
2011 qglDrawArrays(GL_TRIANGLES, firstvertex, numvertices);
2015 case RENDERPATH_GLES2:
2016 // GLES does not have glDrawRangeElements so this is a bit shorter than the GL20 path
2019 GL_BindEBO(bufferobject3s);
2020 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, (void *)bufferoffset3s);
2023 else if (bufferobject3i)
2025 GL_BindEBO(bufferobject3i);
2026 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, (void *)bufferoffset3i);
2032 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, element3s);
2038 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, element3i);
2043 qglDrawArrays(GL_TRIANGLES, firstvertex, numvertices);
2051 // restores backend state, used when done with 3D rendering
2052 void R_Mesh_Finish(void)
2054 R_Mesh_SetRenderTargets(0, NULL, NULL, NULL, NULL, NULL);
2057 r_meshbuffer_t *R_Mesh_CreateMeshBuffer(const void *data, size_t size, const char *name, qboolean isindexbuffer, qboolean isuniformbuffer, qboolean isdynamic, qboolean isindex16)
2059 r_meshbuffer_t *buffer;
2060 if (isuniformbuffer)
2062 if (!vid.support.arb_uniform_buffer_object)
2067 if (!vid.support.arb_vertex_buffer_object)
2069 if (!isdynamic && !(isindexbuffer ? gl_state.usevbo_staticindex : gl_state.usevbo_staticvertex))
2072 buffer = (r_meshbuffer_t *)Mem_ExpandableArray_AllocRecord(&gl_state.meshbufferarray);
2073 memset(buffer, 0, sizeof(*buffer));
2074 buffer->bufferobject = 0;
2075 buffer->devicebuffer = NULL;
2076 buffer->size = size;
2077 buffer->isindexbuffer = isindexbuffer;
2078 buffer->isuniformbuffer = isuniformbuffer;
2079 buffer->isdynamic = isdynamic;
2080 buffer->isindex16 = isindex16;
2081 strlcpy(buffer->name, name, sizeof(buffer->name));
2082 R_Mesh_UpdateMeshBuffer(buffer, data, size, false, 0);
2086 void R_Mesh_UpdateMeshBuffer(r_meshbuffer_t *buffer, const void *data, size_t size, qboolean subdata, size_t offset)
2090 if (buffer->isindexbuffer)
2092 r_refdef.stats[r_stat_indexbufferuploadcount]++;
2093 r_refdef.stats[r_stat_indexbufferuploadsize] += (int)size;
2097 r_refdef.stats[r_stat_vertexbufferuploadcount]++;
2098 r_refdef.stats[r_stat_vertexbufferuploadsize] += (int)size;
2101 buffer->size = size;
2102 switch(vid.renderpath)
2104 case RENDERPATH_GL20:
2105 case RENDERPATH_GLES2:
2106 if (!buffer->bufferobject)
2107 qglGenBuffersARB(1, (GLuint *)&buffer->bufferobject);
2108 if (buffer->isuniformbuffer)
2109 GL_BindUBO(buffer->bufferobject);
2110 else if (buffer->isindexbuffer)
2111 GL_BindEBO(buffer->bufferobject);
2113 GL_BindVBO(buffer->bufferobject);
2117 buffertype = buffer->isindexbuffer ? GL_ELEMENT_ARRAY_BUFFER : GL_ARRAY_BUFFER;
2118 #ifdef GL_UNIFORM_BUFFER
2119 if (buffer->isuniformbuffer)
2120 buffertype = GL_UNIFORM_BUFFER;
2123 qglBufferSubDataARB(buffertype, offset, size, data);
2125 qglBufferDataARB(buffertype, size, data, buffer->isdynamic ? GL_STREAM_DRAW : GL_STATIC_DRAW);
2127 if (buffer->isuniformbuffer)
2133 void R_Mesh_DestroyMeshBuffer(r_meshbuffer_t *buffer)
2137 switch(vid.renderpath)
2139 case RENDERPATH_GL20:
2140 case RENDERPATH_GLES2:
2141 // GL clears the binding if we delete something bound
2142 if (gl_state.uniformbufferobject == buffer->bufferobject)
2143 gl_state.uniformbufferobject = 0;
2144 if (gl_state.vertexbufferobject == buffer->bufferobject)
2145 gl_state.vertexbufferobject = 0;
2146 if (gl_state.elementbufferobject == buffer->bufferobject)
2147 gl_state.elementbufferobject = 0;
2148 qglDeleteBuffersARB(1, (GLuint *)&buffer->bufferobject);
2151 Mem_ExpandableArray_FreeRecord(&gl_state.meshbufferarray, (void *)buffer);
2154 static const char *buffertypename[R_BUFFERDATA_COUNT] = {"vertex", "index16", "index32", "uniform"};
2155 void GL_Mesh_ListVBOs(qboolean printeach)
2160 int index16count, index16mem;
2161 int index32count, index32mem;
2162 int vertexcount, vertexmem;
2163 int uniformcount, uniformmem;
2164 int totalcount, totalmem;
2165 size_t bufferstat[R_BUFFERDATA_COUNT][2][2];
2166 r_meshbuffer_t *buffer;
2167 memset(bufferstat, 0, sizeof(bufferstat));
2168 endindex = (int)Mem_ExpandableArray_IndexRange(&gl_state.meshbufferarray);
2169 for (i = 0;i < endindex;i++)
2171 buffer = (r_meshbuffer_t *) Mem_ExpandableArray_RecordAtIndex(&gl_state.meshbufferarray, i);
2174 if (buffer->isuniformbuffer)
2175 type = R_BUFFERDATA_UNIFORM;
2176 else if (buffer->isindexbuffer && buffer->isindex16)
2177 type = R_BUFFERDATA_INDEX16;
2178 else if (buffer->isindexbuffer)
2179 type = R_BUFFERDATA_INDEX32;
2181 type = R_BUFFERDATA_VERTEX;
2182 isdynamic = buffer->isdynamic;
2183 bufferstat[type][isdynamic][0]++;
2184 bufferstat[type][isdynamic][1] += buffer->size;
2186 Con_Printf("buffer #%i %s = %i bytes (%s %s)\n", i, buffer->name, (int)buffer->size, isdynamic ? "dynamic" : "static", buffertypename[type]);
2188 index16count = (int)(bufferstat[R_BUFFERDATA_INDEX16][0][0] + bufferstat[R_BUFFERDATA_INDEX16][1][0]);
2189 index16mem = (int)(bufferstat[R_BUFFERDATA_INDEX16][0][1] + bufferstat[R_BUFFERDATA_INDEX16][1][1]);
2190 index32count = (int)(bufferstat[R_BUFFERDATA_INDEX32][0][0] + bufferstat[R_BUFFERDATA_INDEX32][1][0]);
2191 index32mem = (int)(bufferstat[R_BUFFERDATA_INDEX32][0][1] + bufferstat[R_BUFFERDATA_INDEX32][1][1]);
2192 vertexcount = (int)(bufferstat[R_BUFFERDATA_VERTEX ][0][0] + bufferstat[R_BUFFERDATA_VERTEX ][1][0]);
2193 vertexmem = (int)(bufferstat[R_BUFFERDATA_VERTEX ][0][1] + bufferstat[R_BUFFERDATA_VERTEX ][1][1]);
2194 uniformcount = (int)(bufferstat[R_BUFFERDATA_UNIFORM][0][0] + bufferstat[R_BUFFERDATA_UNIFORM][1][0]);
2195 uniformmem = (int)(bufferstat[R_BUFFERDATA_UNIFORM][0][1] + bufferstat[R_BUFFERDATA_UNIFORM][1][1]);
2196 totalcount = index16count + index32count + vertexcount + uniformcount;
2197 totalmem = index16mem + index32mem + vertexmem + uniformmem;
2198 Con_Printf("%i 16bit indexbuffers totalling %i bytes (%.3f MB)\n%i 32bit indexbuffers totalling %i bytes (%.3f MB)\n%i vertexbuffers totalling %i bytes (%.3f MB)\n%i uniformbuffers totalling %i bytes (%.3f MB)\ncombined %i buffers totalling %i bytes (%.3fMB)\n", index16count, index16mem, index16mem / 10248576.0, index32count, index32mem, index32mem / 10248576.0, vertexcount, vertexmem, vertexmem / 10248576.0, uniformcount, uniformmem, uniformmem / 10248576.0, totalcount, totalmem, totalmem / 10248576.0);
2203 void R_Mesh_VertexPointer(int components, int gltype, size_t stride, const void *pointer, const r_meshbuffer_t *vertexbuffer, size_t bufferoffset)
2205 switch(vid.renderpath)
2207 case RENDERPATH_GL20:
2208 case RENDERPATH_GLES2:
2209 if (gl_state.pointer_vertex_components != components || gl_state.pointer_vertex_gltype != gltype || gl_state.pointer_vertex_stride != stride || gl_state.pointer_vertex_pointer != pointer || gl_state.pointer_vertex_vertexbuffer != vertexbuffer || gl_state.pointer_vertex_offset != bufferoffset)
2211 int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
2212 gl_state.pointer_vertex_components = components;
2213 gl_state.pointer_vertex_gltype = gltype;
2214 gl_state.pointer_vertex_stride = stride;
2215 gl_state.pointer_vertex_pointer = pointer;
2216 gl_state.pointer_vertex_vertexbuffer = vertexbuffer;
2217 gl_state.pointer_vertex_offset = bufferoffset;
2219 GL_BindVBO(bufferobject);
2220 // LordHavoc: special flag added to gltype for unnormalized types
2221 qglVertexAttribPointer(GLSLATTRIB_POSITION, components, gltype & ~0x80000000, (gltype & 0x80000000) == 0, (GLsizei)stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
2227 void R_Mesh_ColorPointer(int components, int gltype, size_t stride, const void *pointer, const r_meshbuffer_t *vertexbuffer, size_t bufferoffset)
2229 // note: vertexbuffer may be non-NULL even if pointer is NULL, so check
2230 // the pointer only.
2231 switch(vid.renderpath)
2233 case RENDERPATH_GL20:
2234 case RENDERPATH_GLES2:
2238 // caller wants color array enabled
2239 int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
2240 if (!gl_state.pointer_color_enabled)
2242 gl_state.pointer_color_enabled = true;
2244 qglEnableVertexAttribArray(GLSLATTRIB_COLOR);CHECKGLERROR
2246 if (gl_state.pointer_color_components != components || gl_state.pointer_color_gltype != gltype || gl_state.pointer_color_stride != stride || gl_state.pointer_color_pointer != pointer || gl_state.pointer_color_vertexbuffer != vertexbuffer || gl_state.pointer_color_offset != bufferoffset)
2248 gl_state.pointer_color_components = components;
2249 gl_state.pointer_color_gltype = gltype;
2250 gl_state.pointer_color_stride = stride;
2251 gl_state.pointer_color_pointer = pointer;
2252 gl_state.pointer_color_vertexbuffer = vertexbuffer;
2253 gl_state.pointer_color_offset = bufferoffset;
2255 GL_BindVBO(bufferobject);
2256 // LordHavoc: special flag added to gltype for unnormalized types
2257 qglVertexAttribPointer(GLSLATTRIB_COLOR, components, gltype & ~0x80000000, (gltype & 0x80000000) == 0, (GLsizei)stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
2262 // caller wants color array disabled
2263 if (gl_state.pointer_color_enabled)
2265 gl_state.pointer_color_enabled = false;
2267 qglDisableVertexAttribArray(GLSLATTRIB_COLOR);CHECKGLERROR
2268 // when color array is on the current color gets trashed, set it again
2269 qglVertexAttrib4f(GLSLATTRIB_COLOR, gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);CHECKGLERROR
2276 void R_Mesh_TexCoordPointer(unsigned int unitnum, int components, int gltype, size_t stride, const void *pointer, const r_meshbuffer_t *vertexbuffer, size_t bufferoffset)
2278 gltextureunit_t *unit = gl_state.units + unitnum;
2279 // update array settings
2280 // note: there is no need to check bufferobject here because all cases
2281 // that involve a valid bufferobject also supply a texcoord array
2282 switch(vid.renderpath)
2284 case RENDERPATH_GL20:
2285 case RENDERPATH_GLES2:
2289 int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
2290 // texture array unit is enabled, enable the array
2291 if (!unit->arrayenabled)
2293 unit->arrayenabled = true;
2294 qglEnableVertexAttribArray(unitnum+GLSLATTRIB_TEXCOORD0);CHECKGLERROR
2297 if (unit->pointer_texcoord_components != components || unit->pointer_texcoord_gltype != gltype || unit->pointer_texcoord_stride != stride || unit->pointer_texcoord_pointer != pointer || unit->pointer_texcoord_vertexbuffer != vertexbuffer || unit->pointer_texcoord_offset != bufferoffset)
2299 unit->pointer_texcoord_components = components;
2300 unit->pointer_texcoord_gltype = gltype;
2301 unit->pointer_texcoord_stride = stride;
2302 unit->pointer_texcoord_pointer = pointer;
2303 unit->pointer_texcoord_vertexbuffer = vertexbuffer;
2304 unit->pointer_texcoord_offset = bufferoffset;
2305 GL_BindVBO(bufferobject);
2306 // LordHavoc: special flag added to gltype for unnormalized types
2307 qglVertexAttribPointer(unitnum+GLSLATTRIB_TEXCOORD0, components, gltype & ~0x80000000, (gltype & 0x80000000) == 0, (GLsizei)stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
2312 // texture array unit is disabled, disable the array
2313 if (unit->arrayenabled)
2315 unit->arrayenabled = false;
2316 qglDisableVertexAttribArray(unitnum+GLSLATTRIB_TEXCOORD0);CHECKGLERROR
2323 int R_Mesh_TexBound(unsigned int unitnum, int id)
2325 gltextureunit_t *unit = gl_state.units + unitnum;
2326 if (unitnum >= vid.teximageunits)
2328 if (id == GL_TEXTURE_2D)
2330 if (id == GL_TEXTURE_3D)
2332 if (id == GL_TEXTURE_CUBE_MAP)
2333 return unit->tcubemap;
2337 void R_Mesh_CopyToTexture(rtexture_t *tex, int tx, int ty, int sx, int sy, int width, int height)
2339 switch(vid.renderpath)
2341 case RENDERPATH_GL20:
2342 case RENDERPATH_GLES2:
2343 R_Mesh_TexBind(0, tex);
2344 GL_ActiveTexture(0);CHECKGLERROR
2345 qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, tx, ty, sx, sy, width, height);CHECKGLERROR
2350 void R_Mesh_ClearBindingsForTexture(int texnum)
2352 gltextureunit_t *unit;
2353 unsigned int unitnum;
2354 // this doesn't really unbind the texture, but it does prevent a mistaken "do nothing" behavior on the next time this same texnum is bound on the same unit as the same type (this mainly affects r_shadow_bouncegrid because 3D textures are so rarely used)
2355 for (unitnum = 0;unitnum < vid.teximageunits;unitnum++)
2357 unit = gl_state.units + unitnum;
2358 if (unit->t2d == texnum)
2360 if (unit->t3d == texnum)
2362 if (unit->tcubemap == texnum)
2363 unit->tcubemap = -1;
2367 void R_Mesh_TexBind(unsigned int unitnum, rtexture_t *tex)
2369 gltextureunit_t *unit = gl_state.units + unitnum;
2371 if (unitnum >= vid.teximageunits)
2373 if (unit->texture == tex)
2375 switch(vid.renderpath)
2377 case RENDERPATH_GL20:
2378 case RENDERPATH_GLES2:
2381 tex = r_texture_white;
2382 // not initialized enough yet...
2386 unit->texture = tex;
2387 texnum = R_GetTexture(tex);
2388 switch(tex->gltexturetypeenum)
2390 case GL_TEXTURE_2D: if (unit->t2d != texnum) {GL_ActiveTexture(unitnum);unit->t2d = texnum;qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR}break;
2391 case GL_TEXTURE_3D: if (unit->t3d != texnum) {GL_ActiveTexture(unitnum);unit->t3d = texnum;qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR}break;
2392 case GL_TEXTURE_CUBE_MAP: if (unit->tcubemap != texnum) {GL_ActiveTexture(unitnum);unit->tcubemap = texnum;qglBindTexture(GL_TEXTURE_CUBE_MAP, unit->tcubemap);CHECKGLERROR}break;
2398 void R_Mesh_ResetTextureState(void)
2400 unsigned int unitnum;
2404 for (unitnum = 0;unitnum < vid.teximageunits;unitnum++)
2405 R_Mesh_TexBind(unitnum, NULL);
2406 for (unitnum = 0;unitnum < vid.texarrayunits;unitnum++)
2407 R_Mesh_TexCoordPointer(unitnum, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
2410 void R_Mesh_PrepareVertices_Vertex3f(int numvertices, const float *vertex3f, const r_meshbuffer_t *vertexbuffer, int bufferoffset)
2412 // upload temporary vertexbuffer for this rendering
2413 if (!gl_state.usevbo_staticvertex)
2414 vertexbuffer = NULL;
2415 if (!vertexbuffer && gl_state.usevbo_dynamicvertex)
2416 vertexbuffer = R_BufferData_Store(numvertices * sizeof(float[3]), (void *)vertex3f, R_BUFFERDATA_VERTEX, &bufferoffset);
2419 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, vertexbuffer, bufferoffset);
2420 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
2421 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
2422 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
2423 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
2424 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
2425 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
2426 R_Mesh_TexCoordPointer(5, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
2427 R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
2428 R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
2432 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, vertexbuffer, 0);
2433 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
2434 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
2435 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
2436 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
2437 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
2438 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
2439 R_Mesh_TexCoordPointer(5, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
2440 R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
2441 R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
2445 void R_Mesh_PrepareVertices_Generic_Arrays(int numvertices, const float *vertex3f, const float *color4f, const float *texcoord2f)
2447 if (gl_state.usevbo_dynamicvertex)
2449 r_meshbuffer_t *buffer_vertex3f = NULL;
2450 r_meshbuffer_t *buffer_color4f = NULL;
2451 r_meshbuffer_t *buffer_texcoord2f = NULL;
2452 int bufferoffset_vertex3f = 0;
2453 int bufferoffset_color4f = 0;
2454 int bufferoffset_texcoord2f = 0;
2455 buffer_color4f = R_BufferData_Store(numvertices * sizeof(float[4]), color4f , R_BUFFERDATA_VERTEX, &bufferoffset_color4f );
2456 buffer_vertex3f = R_BufferData_Store(numvertices * sizeof(float[3]), vertex3f , R_BUFFERDATA_VERTEX, &bufferoffset_vertex3f );
2457 buffer_texcoord2f = R_BufferData_Store(numvertices * sizeof(float[2]), texcoord2f, R_BUFFERDATA_VERTEX, &bufferoffset_texcoord2f);
2458 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(float[3]) , vertex3f , buffer_vertex3f , bufferoffset_vertex3f );
2459 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(float[4]) , color4f , buffer_color4f , bufferoffset_color4f );
2460 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(float[2]) , texcoord2f , buffer_texcoord2f , bufferoffset_texcoord2f );
2461 R_Mesh_TexCoordPointer(1, 3, GL_FLOAT , sizeof(float[3]) , NULL , NULL , 0 );
2462 R_Mesh_TexCoordPointer(2, 3, GL_FLOAT , sizeof(float[3]) , NULL , NULL , 0 );
2463 R_Mesh_TexCoordPointer(3, 3, GL_FLOAT , sizeof(float[3]) , NULL , NULL , 0 );
2464 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT , sizeof(float[2]) , NULL , NULL , 0 );
2465 R_Mesh_TexCoordPointer(5, 2, GL_FLOAT , sizeof(float[2]) , NULL , NULL , 0 );
2466 R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL , NULL , 0 );
2467 R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL , NULL , 0 );
2471 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
2472 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
2473 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoord2f, NULL, 0);
2474 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
2475 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
2476 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
2477 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
2478 R_Mesh_TexCoordPointer(5, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
2479 R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
2480 R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
2484 void R_Mesh_PrepareVertices_Mesh_Arrays(int numvertices, const float *vertex3f, const float *svector3f, const float *tvector3f, const float *normal3f, const float *color4f, const float *texcoordtexture2f, const float *texcoordlightmap2f)
2486 if (gl_state.usevbo_dynamicvertex)
2488 r_meshbuffer_t *buffer_vertex3f = NULL;
2489 r_meshbuffer_t *buffer_color4f = NULL;
2490 r_meshbuffer_t *buffer_texcoordtexture2f = NULL;
2491 r_meshbuffer_t *buffer_svector3f = NULL;
2492 r_meshbuffer_t *buffer_tvector3f = NULL;
2493 r_meshbuffer_t *buffer_normal3f = NULL;
2494 r_meshbuffer_t *buffer_texcoordlightmap2f = NULL;
2495 int bufferoffset_vertex3f = 0;
2496 int bufferoffset_color4f = 0;
2497 int bufferoffset_texcoordtexture2f = 0;
2498 int bufferoffset_svector3f = 0;
2499 int bufferoffset_tvector3f = 0;
2500 int bufferoffset_normal3f = 0;
2501 int bufferoffset_texcoordlightmap2f = 0;
2502 buffer_color4f = R_BufferData_Store(numvertices * sizeof(float[4]), color4f , R_BUFFERDATA_VERTEX, &bufferoffset_color4f );
2503 buffer_vertex3f = R_BufferData_Store(numvertices * sizeof(float[3]), vertex3f , R_BUFFERDATA_VERTEX, &bufferoffset_vertex3f );
2504 buffer_svector3f = R_BufferData_Store(numvertices * sizeof(float[3]), svector3f , R_BUFFERDATA_VERTEX, &bufferoffset_svector3f );
2505 buffer_tvector3f = R_BufferData_Store(numvertices * sizeof(float[3]), tvector3f , R_BUFFERDATA_VERTEX, &bufferoffset_tvector3f );
2506 buffer_normal3f = R_BufferData_Store(numvertices * sizeof(float[3]), normal3f , R_BUFFERDATA_VERTEX, &bufferoffset_normal3f );
2507 buffer_texcoordtexture2f = R_BufferData_Store(numvertices * sizeof(float[2]), texcoordtexture2f , R_BUFFERDATA_VERTEX, &bufferoffset_texcoordtexture2f );
2508 buffer_texcoordlightmap2f = R_BufferData_Store(numvertices * sizeof(float[2]), texcoordlightmap2f, R_BUFFERDATA_VERTEX, &bufferoffset_texcoordlightmap2f);
2509 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(float[3]) , vertex3f , buffer_vertex3f , bufferoffset_vertex3f );
2510 R_Mesh_ColorPointer( 4, GL_FLOAT , sizeof(float[4]) , color4f , buffer_color4f , bufferoffset_color4f );
2511 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(float[2]) , texcoordtexture2f , buffer_texcoordtexture2f , bufferoffset_texcoordtexture2f );
2512 R_Mesh_TexCoordPointer(1, 3, GL_FLOAT , sizeof(float[3]) , svector3f , buffer_svector3f , bufferoffset_svector3f );
2513 R_Mesh_TexCoordPointer(2, 3, GL_FLOAT , sizeof(float[3]) , tvector3f , buffer_tvector3f , bufferoffset_tvector3f );
2514 R_Mesh_TexCoordPointer(3, 3, GL_FLOAT , sizeof(float[3]) , normal3f , buffer_normal3f , bufferoffset_normal3f );
2515 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT , sizeof(float[2]) , texcoordlightmap2f, buffer_texcoordlightmap2f, bufferoffset_texcoordlightmap2f);
2516 R_Mesh_TexCoordPointer(5, 2, GL_FLOAT , sizeof(float[2]) , NULL , NULL , 0 );
2517 R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL , NULL , 0 );
2518 R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL , NULL , 0 );
2522 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
2523 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
2524 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoordtexture2f, NULL, 0);
2525 R_Mesh_TexCoordPointer(1, 3, GL_FLOAT, sizeof(float[3]), svector3f, NULL, 0);
2526 R_Mesh_TexCoordPointer(2, 3, GL_FLOAT, sizeof(float[3]), tvector3f, NULL, 0);
2527 R_Mesh_TexCoordPointer(3, 3, GL_FLOAT, sizeof(float[3]), normal3f, NULL, 0);
2528 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), texcoordlightmap2f, NULL, 0);
2529 R_Mesh_TexCoordPointer(5, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
2530 R_Mesh_TexCoordPointer(6, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
2531 R_Mesh_TexCoordPointer(7, 4, GL_UNSIGNED_BYTE, sizeof(unsigned char[4]), NULL, NULL, 0);
2535 void GL_BlendEquationSubtract(qboolean negated)
2539 switch(vid.renderpath)
2541 case RENDERPATH_GL20:
2542 case RENDERPATH_GLES2:
2543 qglBlendEquationEXT(GL_FUNC_REVERSE_SUBTRACT);
2549 switch(vid.renderpath)
2551 case RENDERPATH_GL20:
2552 case RENDERPATH_GLES2:
2553 qglBlendEquationEXT(GL_FUNC_ADD);