3 #include "cl_collision.h"
6 extern LPDIRECT3DDEVICE9 vid_d3d9dev;
7 extern D3DCAPS9 vid_d3d9caps;
10 #define MAX_RENDERTARGETS 4
12 cvar_t gl_mesh_drawrangeelements = {0, "gl_mesh_drawrangeelements", "1", "use glDrawRangeElements function if available instead of glDrawElements (for performance comparisons or bug testing)"};
13 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)"};
14 cvar_t gl_mesh_prefer_short_elements = {CVAR_SAVE, "gl_mesh_prefer_short_elements", "1", "use GL_UNSIGNED_SHORT element arrays instead of GL_UNSIGNED_INT"};
15 cvar_t gl_mesh_separatearrays = {0, "gl_mesh_separatearrays", "1", "use several separate vertex arrays rather than one combined stream"};
16 cvar_t gl_paranoid = {0, "gl_paranoid", "0", "enables OpenGL error checking and other tests"};
17 cvar_t gl_printcheckerror = {0, "gl_printcheckerror", "0", "prints all OpenGL error checks, useful to identify location of driver crashes"};
19 cvar_t r_render = {0, "r_render", "1", "enables rendering 3D views (you want this on!)"};
20 cvar_t r_renderview = {0, "r_renderview", "1", "enables rendering 3D views (you want this on!)"};
21 cvar_t r_waterwarp = {CVAR_SAVE, "r_waterwarp", "1", "warp view while underwater"};
22 cvar_t gl_polyblend = {CVAR_SAVE, "gl_polyblend", "1", "tints view while underwater, hurt, etc"};
23 cvar_t gl_dither = {CVAR_SAVE, "gl_dither", "1", "enables OpenGL dithering (16bit looks bad with this off)"};
24 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)"};
25 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"};
26 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"};
27 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"};
29 cvar_t v_flipped = {0, "v_flipped", "0", "mirror the screen (poor man's left handed mode)"};
30 qboolean v_flipped_state = false;
32 r_viewport_t gl_viewport;
33 matrix4x4_t gl_modelmatrix;
34 matrix4x4_t gl_viewmatrix;
35 matrix4x4_t gl_modelviewmatrix;
36 matrix4x4_t gl_projectionmatrix;
37 matrix4x4_t gl_modelviewprojectionmatrix;
38 float gl_modelview16f[16];
39 float gl_modelviewprojection16f[16];
40 qboolean gl_modelmatrixchanged;
42 int gl_maxdrawrangeelementsvertices;
43 int gl_maxdrawrangeelementsindices;
48 void GL_PrintError(int errornumber, char *filename, int linenumber)
52 #ifdef GL_INVALID_ENUM
54 Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
57 #ifdef GL_INVALID_VALUE
58 case GL_INVALID_VALUE:
59 Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
62 #ifdef GL_INVALID_OPERATION
63 case GL_INVALID_OPERATION:
64 Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
67 #ifdef GL_STACK_OVERFLOW
68 case GL_STACK_OVERFLOW:
69 Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
72 #ifdef GL_STACK_UNDERFLOW
73 case GL_STACK_UNDERFLOW:
74 Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
77 #ifdef GL_OUT_OF_MEMORY
78 case GL_OUT_OF_MEMORY:
79 Con_Printf("GL_OUT_OF_MEMORY at %s:%i\n", filename, linenumber);
82 #ifdef GL_TABLE_TOO_LARGE
83 case GL_TABLE_TOO_LARGE:
84 Con_Printf("GL_TABLE_TOO_LARGE at %s:%i\n", filename, linenumber);
87 #ifdef GL_INVALID_FRAMEBUFFER_OPERATION_EXT
88 case GL_INVALID_FRAMEBUFFER_OPERATION_EXT:
89 Con_Printf("GL_INVALID_FRAMEBUFFER_OPERATION at %s:%i\n", filename, linenumber);
93 Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
99 #define BACKENDACTIVECHECK if (!gl_state.active) Sys_Error("GL backend function called when backend is not active");
101 void SCR_ScreenShot_f (void);
103 typedef struct gltextureunit_s
105 int pointer_texcoord_components;
106 int pointer_texcoord_gltype;
107 size_t pointer_texcoord_stride;
108 const void *pointer_texcoord_pointer;
109 const r_meshbuffer_t *pointer_texcoord_vertexbuffer;
110 size_t pointer_texcoord_offset;
113 int t2d, t3d, tcubemap, trectangle;
115 int rgbscale, alphascale;
117 int combinergb, combinealpha;
118 // texmatrixenabled exists only to avoid unnecessary texmatrix compares
119 int texmatrixenabled;
124 typedef struct gl_state_s
132 int colormask; // stored as bottom 4 bits: r g b a (3 2 1 0 order)
136 float polygonoffset[2];
139 float alphafuncvalue;
142 unsigned int clientunit;
143 gltextureunit_t units[MAX_TEXTUREUNITS];
147 int vertexbufferobject;
148 int elementbufferobject;
149 int framebufferobject;
150 qboolean pointer_color_enabled;
152 int pointer_vertex_components;
153 int pointer_vertex_gltype;
154 size_t pointer_vertex_stride;
155 const void *pointer_vertex_pointer;
156 const r_meshbuffer_t *pointer_vertex_vertexbuffer;
157 size_t pointer_vertex_offset;
159 int pointer_color_components;
160 int pointer_color_gltype;
161 size_t pointer_color_stride;
162 const void *pointer_color_pointer;
163 const r_meshbuffer_t *pointer_color_vertexbuffer;
164 size_t pointer_color_offset;
166 void *preparevertices_tempdata;
167 size_t preparevertices_tempdatamaxsize;
168 r_meshbuffer_t *preparevertices_dynamicvertexbuffer;
169 r_vertexposition_t *preparevertices_vertexposition;
170 r_vertexgeneric_t *preparevertices_vertexgeneric;
171 r_vertexmesh_t *preparevertices_vertexmesh;
172 int preparevertices_numvertices;
174 r_meshbuffer_t *draw_dynamicindexbuffer;
176 qboolean usevbo_staticvertex;
177 qboolean usevbo_staticindex;
178 qboolean usevbo_dynamicvertex;
179 qboolean usevbo_dynamicindex;
181 memexpandablearray_t meshbufferarray;
186 rtexture_t *d3drt_depthtexture;
187 rtexture_t *d3drt_colortextures[MAX_RENDERTARGETS];
188 IDirect3DSurface9 *d3drt_depthsurface;
189 IDirect3DSurface9 *d3drt_colorsurfaces[MAX_RENDERTARGETS];
190 IDirect3DSurface9 *d3drt_backbufferdepthsurface;
191 IDirect3DSurface9 *d3drt_backbuffercolorsurface;
196 static gl_state_t gl_state;
200 note: here's strip order for a terrain row:
207 A0B, 01B, B1C, 12C, C2D, 23D, D3E, 34E
209 *elements++ = i + row;
211 *elements++ = i + row + 1;
214 *elements++ = i + row + 1;
217 for (y = 0;y < rows - 1;y++)
219 for (x = 0;x < columns - 1;x++)
222 *elements++ = i + columns;
224 *elements++ = i + columns + 1;
227 *elements++ = i + columns + 1;
238 for (y = 0;y < rows - 1;y++)
240 for (x = 0;x < columns - 1;x++)
244 *elements++ = i + columns;
245 *elements++ = i + columns + 1;
246 *elements++ = i + columns;
247 *elements++ = i + columns + 1;
253 int polygonelement3i[(POLYGONELEMENTS_MAXPOINTS-2)*3];
254 unsigned short polygonelement3s[(POLYGONELEMENTS_MAXPOINTS-2)*3];
255 int quadelement3i[QUADELEMENTS_MAXQUADS*6];
256 unsigned short quadelement3s[QUADELEMENTS_MAXQUADS*6];
258 void GL_VBOStats_f(void)
260 GL_Mesh_ListVBOs(true);
263 static void GL_Backend_ResetState(void);
265 static void R_Mesh_InitVertexDeclarations(void);
266 static void R_Mesh_DestroyVertexDeclarations(void);
268 static void gl_backend_start(void)
270 memset(&gl_state, 0, sizeof(gl_state));
272 R_Mesh_InitVertexDeclarations();
274 gl_state.usevbo_staticvertex = (vid.support.arb_vertex_buffer_object && gl_vbo.integer) || vid.forcevbo;
275 gl_state.usevbo_staticindex = (vid.support.arb_vertex_buffer_object && (gl_vbo.integer == 1 || gl_vbo.integer == 3)) || vid.forcevbo;
276 gl_state.usevbo_dynamicvertex = (vid.support.arb_vertex_buffer_object && gl_vbo_dynamicvertex.integer) || vid.forcevbo;
277 gl_state.usevbo_dynamicindex = (vid.support.arb_vertex_buffer_object && gl_vbo_dynamicindex.integer) || vid.forcevbo;
278 Mem_ExpandableArray_NewArray(&gl_state.meshbufferarray, r_main_mempool, sizeof(r_meshbuffer_t), 128);
280 Con_DPrintf("OpenGL backend started.\n");
284 GL_Backend_ResetState();
286 switch(vid.renderpath)
288 case RENDERPATH_GL11:
289 case RENDERPATH_GL13:
290 case RENDERPATH_GL20:
291 case RENDERPATH_CGGL:
293 case RENDERPATH_D3D9:
295 IDirect3DDevice9_GetDepthStencilSurface(vid_d3d9dev, &gl_state.d3drt_backbufferdepthsurface);
296 IDirect3DDevice9_GetRenderTarget(vid_d3d9dev, 0, &gl_state.d3drt_backbuffercolorsurface);
299 case RENDERPATH_D3D10:
300 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
302 case RENDERPATH_D3D11:
303 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
308 static void gl_backend_shutdown(void)
310 Con_DPrint("OpenGL Backend shutting down\n");
312 switch(vid.renderpath)
314 case RENDERPATH_GL11:
315 case RENDERPATH_GL13:
316 case RENDERPATH_GL20:
317 case RENDERPATH_CGGL:
319 case RENDERPATH_D3D9:
321 IDirect3DSurface9_Release(gl_state.d3drt_backbufferdepthsurface);
322 IDirect3DSurface9_Release(gl_state.d3drt_backbuffercolorsurface);
325 case RENDERPATH_D3D10:
326 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
328 case RENDERPATH_D3D11:
329 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
333 if (gl_state.preparevertices_tempdata)
334 Mem_Free(gl_state.preparevertices_tempdata);
335 if (gl_state.preparevertices_dynamicvertexbuffer)
336 R_Mesh_DestroyMeshBuffer(gl_state.preparevertices_dynamicvertexbuffer);
338 Mem_ExpandableArray_FreeArray(&gl_state.meshbufferarray);
340 R_Mesh_DestroyVertexDeclarations();
342 memset(&gl_state, 0, sizeof(gl_state));
345 static void gl_backend_newmap(void)
349 static void gl_backend_devicelost(void)
352 r_meshbuffer_t *buffer;
353 endindex = Mem_ExpandableArray_IndexRange(&gl_state.meshbufferarray);
354 for (i = 0;i < endindex;i++)
356 buffer = (r_meshbuffer_t *) Mem_ExpandableArray_RecordAtIndex(&gl_state.meshbufferarray, i);
357 if (!buffer || !buffer->isdynamic)
359 switch(vid.renderpath)
361 case RENDERPATH_GL11:
362 case RENDERPATH_GL13:
363 case RENDERPATH_GL20:
364 case RENDERPATH_CGGL:
366 case RENDERPATH_D3D9:
368 if (buffer->devicebuffer)
370 if (buffer->isindexbuffer)
371 IDirect3DIndexBuffer9_Release((IDirect3DIndexBuffer9*)buffer->devicebuffer);
373 IDirect3DVertexBuffer9_Release((IDirect3DVertexBuffer9*)buffer->devicebuffer);
374 buffer->devicebuffer = NULL;
378 case RENDERPATH_D3D10:
379 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
381 case RENDERPATH_D3D11:
382 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
388 static void gl_backend_devicerestored(void)
392 void gl_backend_init(void)
396 for (i = 0;i < POLYGONELEMENTS_MAXPOINTS - 2;i++)
398 polygonelement3s[i * 3 + 0] = 0;
399 polygonelement3s[i * 3 + 1] = i + 1;
400 polygonelement3s[i * 3 + 2] = i + 2;
402 // elements for rendering a series of quads as triangles
403 for (i = 0;i < QUADELEMENTS_MAXQUADS;i++)
405 quadelement3s[i * 6 + 0] = i * 4;
406 quadelement3s[i * 6 + 1] = i * 4 + 1;
407 quadelement3s[i * 6 + 2] = i * 4 + 2;
408 quadelement3s[i * 6 + 3] = i * 4;
409 quadelement3s[i * 6 + 4] = i * 4 + 2;
410 quadelement3s[i * 6 + 5] = i * 4 + 3;
413 for (i = 0;i < (POLYGONELEMENTS_MAXPOINTS - 2)*3;i++)
414 polygonelement3i[i] = polygonelement3s[i];
415 for (i = 0;i < QUADELEMENTS_MAXQUADS*3;i++)
416 quadelement3i[i] = quadelement3s[i];
418 Cvar_RegisterVariable(&r_render);
419 Cvar_RegisterVariable(&r_renderview);
420 Cvar_RegisterVariable(&r_waterwarp);
421 Cvar_RegisterVariable(&gl_polyblend);
422 Cvar_RegisterVariable(&v_flipped);
423 Cvar_RegisterVariable(&gl_dither);
424 Cvar_RegisterVariable(&gl_vbo);
425 Cvar_RegisterVariable(&gl_vbo_dynamicvertex);
426 Cvar_RegisterVariable(&gl_vbo_dynamicindex);
427 Cvar_RegisterVariable(&gl_paranoid);
428 Cvar_RegisterVariable(&gl_printcheckerror);
430 Cvar_RegisterVariable(&gl_mesh_drawrangeelements);
431 Cvar_RegisterVariable(&gl_mesh_testmanualfeeding);
432 Cvar_RegisterVariable(&gl_mesh_prefer_short_elements);
433 Cvar_RegisterVariable(&gl_mesh_separatearrays);
435 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");
437 R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap, gl_backend_devicelost, gl_backend_devicerestored);
440 void GL_SetMirrorState(qboolean state);
442 void R_Viewport_TransformToScreen(const r_viewport_t *v, const vec4_t in, vec4_t out)
446 Matrix4x4_Transform4 (&v->viewmatrix, in, temp);
447 Matrix4x4_Transform4 (&v->projectmatrix, temp, out);
449 out[0] = v->x + (out[0] * iw + 1.0f) * v->width * 0.5f;
450 out[1] = v->y + v->height - (out[1] * iw + 1.0f) * v->height * 0.5f;
451 out[2] = v->z + (out[2] * iw + 1.0f) * v->depth * 0.5f;
454 static void R_Viewport_ApplyNearClipPlaneFloatGL(const r_viewport_t *v, float *m, float normalx, float normaly, float normalz, float dist)
458 float clipPlane[4], v3[3], v4[3];
461 // This is inspired by Oblique Depth Projection from http://www.terathon.com/code/oblique.php
463 VectorSet(normal, normalx, normaly, normalz);
464 Matrix4x4_Transform3x3(&v->viewmatrix, normal, clipPlane);
465 VectorScale(normal, dist, v3);
466 Matrix4x4_Transform(&v->viewmatrix, v3, v4);
467 // FIXME: LordHavoc: I think this can be done more efficiently somehow but I can't remember the technique
468 clipPlane[3] = -DotProduct(v4, clipPlane);
472 // testing code for comparing results
474 VectorCopy4(clipPlane, clipPlane2);
475 R_EntityMatrix(&identitymatrix);
476 VectorSet(q, normal[0], normal[1], normal[2], -dist);
477 qglClipPlane(GL_CLIP_PLANE0, q);
478 qglGetClipPlane(GL_CLIP_PLANE0, q);
479 VectorCopy4(q, clipPlane);
483 // Calculate the clip-space corner point opposite the clipping plane
484 // as (sgn(clipPlane.x), sgn(clipPlane.y), 1, 1) and
485 // transform it into camera space by multiplying it
486 // by the inverse of the projection matrix
487 q[0] = ((clipPlane[0] < 0.0f ? -1.0f : clipPlane[0] > 0.0f ? 1.0f : 0.0f) + m[8]) / m[0];
488 q[1] = ((clipPlane[1] < 0.0f ? -1.0f : clipPlane[1] > 0.0f ? 1.0f : 0.0f) + m[9]) / m[5];
490 q[3] = (1.0f + m[10]) / m[14];
492 // Calculate the scaled plane vector
493 d = 2.0f / DotProduct4(clipPlane, q);
495 // Replace the third row of the projection matrix
496 m[2] = clipPlane[0] * d;
497 m[6] = clipPlane[1] * d;
498 m[10] = clipPlane[2] * d + 1.0f;
499 m[14] = clipPlane[3] * d;
502 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)
504 float left = x1, right = x2, bottom = y2, top = y1, zNear = nearclip, zFar = farclip;
506 memset(v, 0, sizeof(*v));
507 v->type = R_VIEWPORTTYPE_ORTHO;
508 v->cameramatrix = *cameramatrix;
515 memset(m, 0, sizeof(m));
516 m[0] = 2/(right - left);
517 m[5] = 2/(top - bottom);
518 m[10] = -2/(zFar - zNear);
519 m[12] = - (right + left)/(right - left);
520 m[13] = - (top + bottom)/(top - bottom);
521 m[14] = - (zFar + zNear)/(zFar - zNear);
523 switch(vid.renderpath)
525 case RENDERPATH_GL11:
526 case RENDERPATH_GL13:
527 case RENDERPATH_GL20:
528 case RENDERPATH_CGGL:
530 case RENDERPATH_D3D9:
531 case RENDERPATH_D3D10:
532 case RENDERPATH_D3D11:
533 m[10] = -1/(zFar - zNear);
534 m[14] = -zNear/(zFar-zNear);
537 v->screentodepth[0] = -farclip / (farclip - nearclip);
538 v->screentodepth[1] = farclip * nearclip / (farclip - nearclip);
540 Matrix4x4_Invert_Full(&v->viewmatrix, &v->cameramatrix);
543 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
545 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
551 Vector4Set(test1, (x1+x2)*0.5f, (y1+y2)*0.5f, 0.0f, 1.0f);
552 R_Viewport_TransformToScreen(v, test1, test2);
553 Con_Printf("%f %f %f -> %f %f %f\n", test1[0], test1[1], test1[2], test2[0], test2[1], test2[2]);
558 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)
560 matrix4x4_t tempmatrix, basematrix;
562 memset(v, 0, sizeof(*v));
564 v->type = R_VIEWPORTTYPE_PERSPECTIVE;
565 v->cameramatrix = *cameramatrix;
572 memset(m, 0, sizeof(m));
573 m[0] = 1.0 / frustumx;
574 m[5] = 1.0 / frustumy;
575 m[10] = -(farclip + nearclip) / (farclip - nearclip);
577 m[14] = -2 * nearclip * farclip / (farclip - nearclip);
578 v->screentodepth[0] = -farclip / (farclip - nearclip);
579 v->screentodepth[1] = farclip * nearclip / (farclip - nearclip);
581 Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
582 Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
583 Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
584 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
587 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
589 if(v_flipped.integer)
597 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
600 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)
602 matrix4x4_t tempmatrix, basematrix;
603 const float nudge = 1.0 - 1.0 / (1<<23);
605 memset(v, 0, sizeof(*v));
607 v->type = R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP;
608 v->cameramatrix = *cameramatrix;
615 memset(m, 0, sizeof(m));
616 m[ 0] = 1.0 / frustumx;
617 m[ 5] = 1.0 / frustumy;
620 m[14] = -2 * nearclip * nudge;
621 v->screentodepth[0] = (m[10] + 1) * 0.5 - 1;
622 v->screentodepth[1] = m[14] * -0.5;
624 Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
625 Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
626 Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
627 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
630 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
632 if(v_flipped.integer)
640 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
643 float cubeviewmatrix[6][16] =
645 // standard cubemap projections
683 float rectviewmatrix[6][16] =
685 // sign-preserving cubemap projections
724 void R_Viewport_InitCubeSideView(r_viewport_t *v, const matrix4x4_t *cameramatrix, int side, int size, float nearclip, float farclip, const float *nearplane)
726 matrix4x4_t tempmatrix, basematrix;
728 memset(v, 0, sizeof(*v));
729 v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
730 v->cameramatrix = *cameramatrix;
734 memset(m, 0, sizeof(m));
736 m[10] = -(farclip + nearclip) / (farclip - nearclip);
738 m[14] = -2 * nearclip * farclip / (farclip - nearclip);
740 Matrix4x4_FromArrayFloatGL(&basematrix, cubeviewmatrix[side]);
741 Matrix4x4_Invert_Simple(&tempmatrix, &v->cameramatrix);
742 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
745 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
747 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
750 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)
752 matrix4x4_t tempmatrix, basematrix;
754 memset(v, 0, sizeof(*v));
755 v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
756 v->cameramatrix = *cameramatrix;
757 v->x = (side & 1) * size;
758 v->y = (side >> 1) * size;
762 memset(m, 0, sizeof(m));
763 m[0] = m[5] = 1.0f * ((float)size - border) / size;
764 m[10] = -(farclip + nearclip) / (farclip - nearclip);
766 m[14] = -2 * nearclip * farclip / (farclip - nearclip);
768 Matrix4x4_FromArrayFloatGL(&basematrix, rectviewmatrix[side]);
769 Matrix4x4_Invert_Simple(&tempmatrix, &v->cameramatrix);
770 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
772 switch(vid.renderpath)
774 case RENDERPATH_GL20:
775 case RENDERPATH_CGGL:
776 case RENDERPATH_GL13:
777 case RENDERPATH_GL11:
779 case RENDERPATH_D3D9:
782 case RENDERPATH_D3D10:
783 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
785 case RENDERPATH_D3D11:
786 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
791 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
793 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
796 void R_SetViewport(const r_viewport_t *v)
801 // FIXME: v_flipped_state is evil, this probably breaks somewhere
802 GL_SetMirrorState(v_flipped.integer && (v->type == R_VIEWPORTTYPE_PERSPECTIVE || v->type == R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP));
804 // copy over the matrices to our state
805 gl_viewmatrix = v->viewmatrix;
806 gl_projectionmatrix = v->projectmatrix;
808 switch(vid.renderpath)
810 case RENDERPATH_GL20:
811 case RENDERPATH_CGGL:
813 // qglViewport(v->x, v->y, v->width, v->height);CHECKGLERROR
815 case RENDERPATH_GL13:
816 case RENDERPATH_GL11:
818 qglViewport(v->x, v->y, v->width, v->height);CHECKGLERROR
819 // Load the projection matrix into OpenGL
820 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
821 Matrix4x4_ToArrayFloatGL(&gl_projectionmatrix, m);
822 qglLoadMatrixf(m);CHECKGLERROR
823 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
825 case RENDERPATH_D3D9:
828 D3DVIEWPORT9 d3dviewport;
829 d3dviewport.X = gl_viewport.x;
830 d3dviewport.Y = gl_viewport.y;
831 d3dviewport.Width = gl_viewport.width;
832 d3dviewport.Height = gl_viewport.height;
833 d3dviewport.MinZ = gl_state.depthrange[0];
834 d3dviewport.MaxZ = gl_state.depthrange[1];
835 IDirect3DDevice9_SetViewport(vid_d3d9dev, &d3dviewport);
839 case RENDERPATH_D3D10:
840 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
842 case RENDERPATH_D3D11:
843 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
847 // force an update of the derived matrices
848 gl_modelmatrixchanged = true;
849 R_EntityMatrix(&gl_modelmatrix);
852 void R_GetViewport(r_viewport_t *v)
857 static void GL_BindVBO(int bufferobject)
859 if (gl_state.vertexbufferobject != bufferobject)
861 gl_state.vertexbufferobject = bufferobject;
863 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, bufferobject);CHECKGLERROR
867 static void GL_BindEBO(int bufferobject)
869 if (gl_state.elementbufferobject != bufferobject)
871 gl_state.elementbufferobject = bufferobject;
873 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, bufferobject);CHECKGLERROR
877 int R_Mesh_CreateFramebufferObject(rtexture_t *depthtexture, rtexture_t *colortexture, rtexture_t *colortexture2, rtexture_t *colortexture3, rtexture_t *colortexture4)
880 switch(vid.renderpath)
882 case RENDERPATH_GL11:
883 case RENDERPATH_GL13:
884 case RENDERPATH_GL20:
885 case RENDERPATH_CGGL:
886 if (!vid.support.ext_framebuffer_object)
888 qglGenFramebuffersEXT(1, (GLuint*)&temp);CHECKGLERROR
889 R_Mesh_SetRenderTargets(temp, NULL, NULL, NULL, NULL, NULL);
890 if (depthtexture) qglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, depthtexture->gltexturetypeenum, R_GetTexture(depthtexture), 0);CHECKGLERROR
891 if (colortexture) qglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, colortexture->gltexturetypeenum, R_GetTexture(colortexture), 0);CHECKGLERROR
892 if (colortexture2) qglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, colortexture2->gltexturetypeenum, R_GetTexture(colortexture2), 0);CHECKGLERROR
893 if (colortexture3) qglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT2_EXT, colortexture3->gltexturetypeenum, R_GetTexture(colortexture3), 0);CHECKGLERROR
894 if (colortexture4) qglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT3_EXT, colortexture4->gltexturetypeenum, R_GetTexture(colortexture4), 0);CHECKGLERROR
896 case RENDERPATH_D3D9:
897 case RENDERPATH_D3D10:
898 case RENDERPATH_D3D11:
904 void R_Mesh_DestroyFramebufferObject(int fbo)
906 switch(vid.renderpath)
908 case RENDERPATH_GL11:
909 case RENDERPATH_GL13:
910 case RENDERPATH_GL20:
911 case RENDERPATH_CGGL:
913 qglDeleteFramebuffersEXT(1, (GLuint*)&fbo);
915 case RENDERPATH_D3D9:
916 case RENDERPATH_D3D10:
917 case RENDERPATH_D3D11:
922 void R_Mesh_ResetRenderTargets(void)
924 switch(vid.renderpath)
926 case RENDERPATH_GL11:
927 case RENDERPATH_GL13:
928 case RENDERPATH_GL20:
929 case RENDERPATH_CGGL:
930 if (gl_state.framebufferobject)
932 gl_state.framebufferobject = 0;
933 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, gl_state.framebufferobject);
936 case RENDERPATH_D3D9:
938 if (gl_state.framebufferobject)
941 gl_state.framebufferobject = 0;
942 IDirect3DDevice9_SetDepthStencilSurface(vid_d3d9dev, gl_state.d3drt_backbufferdepthsurface);
943 IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, 0, gl_state.d3drt_backbuffercolorsurface);
944 gl_state.d3drt_depthsurface = NULL;
945 for (i = 1;i < vid.maxdrawbuffers;i++)
947 if (gl_state.d3drt_colorsurfaces[i])
949 gl_state.d3drt_colorsurfaces[i] = NULL;
950 IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, i, NULL);
956 case RENDERPATH_D3D10:
957 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
959 case RENDERPATH_D3D11:
960 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
965 void R_Mesh_SetRenderTargets(int fbo, rtexture_t *depthtexture, rtexture_t *colortexture, rtexture_t *colortexture2, rtexture_t *colortexture3, rtexture_t *colortexture4)
969 rtexture_t *textures[5];
970 Vector4Set(textures, colortexture, colortexture2, colortexture3, colortexture4);
971 textures[4] = depthtexture;
972 // unbind any matching textures immediately, otherwise D3D will complain about a bound texture being used as a render target
973 for (j = 0;j < 5;j++)
975 for (i = 0;i < vid.teximageunits;i++)
976 if (gl_state.units[i].texture == textures[j])
977 R_Mesh_TexBind(i, NULL);
978 // set up framebuffer object or render targets for the active rendering API
979 switch(vid.renderpath)
981 case RENDERPATH_GL11:
982 case RENDERPATH_GL13:
983 case RENDERPATH_GL20:
984 case RENDERPATH_CGGL:
985 if (gl_state.framebufferobject != fbo)
987 gl_state.framebufferobject = fbo;
988 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, gl_state.framebufferobject);
991 case RENDERPATH_D3D9:
993 // set up the new render targets, a NULL depthtexture intentionally binds nothing
994 // TODO: optimize: keep surface pointer around in rtexture_t until texture is freed or lost
997 gl_state.framebufferobject = 1;
998 gl_state.d3drt_depthtexture = depthtexture;
999 if (gl_state.d3drt_depthtexture)
1001 IDirect3DTexture9_GetSurfaceLevel((IDirect3DTexture9 *)gl_state.d3drt_depthtexture->d3dtexture, 0, &gl_state.d3drt_depthsurface);
1002 IDirect3DDevice9_SetDepthStencilSurface(vid_d3d9dev, gl_state.d3drt_depthsurface);
1003 IDirect3DSurface9_Release(gl_state.d3drt_depthsurface);
1006 IDirect3DDevice9_SetDepthStencilSurface(vid_d3d9dev, NULL);
1007 for (i = 0;i < vid.maxdrawbuffers;i++)
1009 gl_state.d3drt_colortextures[i] = textures[i];
1010 if (gl_state.d3drt_colortextures[i])
1012 IDirect3DTexture9_GetSurfaceLevel((IDirect3DTexture9 *)gl_state.d3drt_colortextures[i]->d3dtexture, 0, &gl_state.d3drt_colorsurfaces[i]);
1013 IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, i, gl_state.d3drt_colorsurfaces[i]);
1014 IDirect3DSurface9_Release(gl_state.d3drt_colorsurfaces[i]);
1017 IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, i, NULL);
1021 R_Mesh_ResetRenderTargets();
1024 case RENDERPATH_D3D10:
1025 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1027 case RENDERPATH_D3D11:
1028 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1034 static int d3dcmpforglfunc(int f)
1038 case GL_NEVER: return D3DCMP_NEVER;
1039 case GL_LESS: return D3DCMP_LESS;
1040 case GL_EQUAL: return D3DCMP_EQUAL;
1041 case GL_LEQUAL: return D3DCMP_LESSEQUAL;
1042 case GL_GREATER: return D3DCMP_GREATER;
1043 case GL_NOTEQUAL: return D3DCMP_NOTEQUAL;
1044 case GL_GEQUAL: return D3DCMP_GREATEREQUAL;
1045 case GL_ALWAYS: return D3DCMP_ALWAYS;
1046 default: Con_DPrintf("Unknown GL_DepthFunc\n");return D3DCMP_ALWAYS;
1050 static int d3dstencilopforglfunc(int f)
1054 case GL_KEEP: return D3DSTENCILOP_KEEP;
1055 case GL_INCR: return D3DSTENCILOP_INCR; // note: GL_INCR is clamped, D3DSTENCILOP_INCR wraps
1056 case GL_DECR: return D3DSTENCILOP_DECR; // note: GL_DECR is clamped, D3DSTENCILOP_DECR wraps
1057 default: Con_DPrintf("Unknown GL_StencilFunc\n");return D3DSTENCILOP_KEEP;
1063 static void GL_Backend_ResetState(void)
1066 gl_state.active = true;
1067 gl_state.depthtest = true;
1068 gl_state.alphatest = false;
1069 gl_state.alphafunc = GL_GEQUAL;
1070 gl_state.alphafuncvalue = 0.5f;
1071 gl_state.blendfunc1 = GL_ONE;
1072 gl_state.blendfunc2 = GL_ZERO;
1073 gl_state.blend = false;
1074 gl_state.depthmask = GL_TRUE;
1075 gl_state.colormask = 15;
1076 gl_state.color4f[0] = gl_state.color4f[1] = gl_state.color4f[2] = gl_state.color4f[3] = 1;
1077 gl_state.lockrange_first = 0;
1078 gl_state.lockrange_count = 0;
1079 gl_state.cullface = GL_NONE;
1080 gl_state.cullfaceenable = false;
1081 gl_state.polygonoffset[0] = 0;
1082 gl_state.polygonoffset[1] = 0;
1083 gl_state.framebufferobject = 0;
1084 gl_state.depthfunc = GL_LEQUAL;
1086 switch(vid.renderpath)
1088 case RENDERPATH_D3D9:
1091 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_COLORWRITEENABLE, gl_state.colormask);
1092 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ALPHATESTENABLE, gl_state.alphatest);
1093 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ALPHAFUNC, d3dcmpforglfunc(gl_state.alphafunc));
1094 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ALPHAREF, (int)bound(0, gl_state.alphafuncvalue * 256.0f, 255));
1095 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, D3DCULL_NONE);
1096 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZFUNC, d3dcmpforglfunc(gl_state.depthfunc));
1097 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZENABLE, gl_state.depthtest);
1098 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZWRITEENABLE, gl_state.depthmask);
1099 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_SLOPESCALEDEPTHBIAS, gl_state.polygonoffset[0]);
1100 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_DEPTHBIAS, gl_state.polygonoffset[1] * (1.0f / 16777216.0f));
1104 case RENDERPATH_D3D10:
1105 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1107 case RENDERPATH_D3D11:
1108 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1110 case RENDERPATH_GL20:
1111 case RENDERPATH_CGGL:
1114 qglColorMask(1, 1, 1, 1);CHECKGLERROR
1115 qglAlphaFunc(gl_state.alphafunc, gl_state.alphafuncvalue);CHECKGLERROR
1116 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
1117 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
1118 qglDisable(GL_BLEND);CHECKGLERROR
1119 qglCullFace(gl_state.cullface);CHECKGLERROR
1120 qglDisable(GL_CULL_FACE);CHECKGLERROR
1121 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
1122 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1123 qglDepthMask(gl_state.depthmask);CHECKGLERROR
1124 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
1126 if (vid.support.arb_vertex_buffer_object)
1128 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
1129 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
1132 if (vid.support.ext_framebuffer_object)
1134 qglBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
1135 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
1138 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), NULL);CHECKGLERROR
1139 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
1141 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL);CHECKGLERROR
1142 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1143 qglColor4f(1, 1, 1, 1);CHECKGLERROR
1145 if (vid.support.ext_framebuffer_object)
1146 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, gl_state.framebufferobject);
1148 gl_state.unit = MAX_TEXTUREUNITS;
1149 gl_state.clientunit = MAX_TEXTUREUNITS;
1150 for (i = 0;i < vid.teximageunits;i++)
1152 GL_ActiveTexture(i);
1153 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
1154 if (vid.support.ext_texture_3d)
1156 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
1158 if (vid.support.arb_texture_cube_map)
1160 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
1162 if (vid.support.arb_texture_rectangle)
1164 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);CHECKGLERROR
1168 for (i = 0;i < vid.texarrayunits;i++)
1170 GL_ClientActiveTexture(i);
1172 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
1173 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1177 case RENDERPATH_GL13:
1178 case RENDERPATH_GL11:
1181 qglColorMask(1, 1, 1, 1);CHECKGLERROR
1182 qglAlphaFunc(gl_state.alphafunc, gl_state.alphafuncvalue);CHECKGLERROR
1183 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
1184 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
1185 qglDisable(GL_BLEND);CHECKGLERROR
1186 qglCullFace(gl_state.cullface);CHECKGLERROR
1187 qglDisable(GL_CULL_FACE);CHECKGLERROR
1188 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
1189 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1190 qglDepthMask(gl_state.depthmask);CHECKGLERROR
1191 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
1193 if (vid.support.arb_vertex_buffer_object)
1195 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
1196 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
1199 if (vid.support.ext_framebuffer_object)
1201 qglBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
1202 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
1205 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), NULL);CHECKGLERROR
1206 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
1208 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL);CHECKGLERROR
1209 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1210 qglColor4f(1, 1, 1, 1);CHECKGLERROR
1212 if (vid.support.ext_framebuffer_object)
1213 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, gl_state.framebufferobject);
1215 gl_state.unit = MAX_TEXTUREUNITS;
1216 gl_state.clientunit = MAX_TEXTUREUNITS;
1217 for (i = 0;i < vid.texunits;i++)
1219 GL_ActiveTexture(i);
1220 GL_ClientActiveTexture(i);
1221 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1222 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
1223 if (vid.support.ext_texture_3d)
1225 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1226 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
1228 if (vid.support.arb_texture_cube_map)
1230 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1231 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
1233 if (vid.support.arb_texture_rectangle)
1235 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
1236 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);CHECKGLERROR
1239 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
1240 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1241 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1242 qglLoadIdentity();CHECKGLERROR
1243 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1244 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
1251 void GL_ActiveTexture(unsigned int num)
1253 if (gl_state.unit != num)
1255 gl_state.unit = num;
1256 switch(vid.renderpath)
1258 case RENDERPATH_GL11:
1259 case RENDERPATH_GL13:
1260 case RENDERPATH_GL20:
1261 case RENDERPATH_CGGL:
1262 if (qglActiveTexture)
1265 qglActiveTexture(GL_TEXTURE0_ARB + gl_state.unit);
1269 case RENDERPATH_D3D9:
1270 case RENDERPATH_D3D10:
1271 case RENDERPATH_D3D11:
1277 void GL_ClientActiveTexture(unsigned int num)
1279 if (gl_state.clientunit != num)
1281 gl_state.clientunit = num;
1282 switch(vid.renderpath)
1284 case RENDERPATH_GL11:
1285 case RENDERPATH_GL13:
1286 case RENDERPATH_GL20:
1287 case RENDERPATH_CGGL:
1288 if (qglActiveTexture)
1291 qglClientActiveTexture(GL_TEXTURE0_ARB + gl_state.clientunit);
1295 case RENDERPATH_D3D9:
1296 case RENDERPATH_D3D10:
1297 case RENDERPATH_D3D11:
1303 void GL_BlendFunc(int blendfunc1, int blendfunc2)
1305 if (gl_state.blendfunc1 != blendfunc1 || gl_state.blendfunc2 != blendfunc2)
1307 qboolean blendenable;
1308 gl_state.blendfunc1 = blendfunc1;
1309 gl_state.blendfunc2 = blendfunc2;
1310 blendenable = (gl_state.blendfunc1 != GL_ONE || gl_state.blendfunc2 != GL_ZERO);
1311 switch(vid.renderpath)
1313 case RENDERPATH_GL11:
1314 case RENDERPATH_GL13:
1315 case RENDERPATH_GL20:
1316 case RENDERPATH_CGGL:
1318 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
1319 if (gl_state.blend != blendenable)
1321 gl_state.blend = blendenable;
1322 if (!gl_state.blend)
1324 qglDisable(GL_BLEND);CHECKGLERROR
1328 qglEnable(GL_BLEND);CHECKGLERROR
1332 case RENDERPATH_D3D9:
1337 D3DBLEND d3dblendfunc[2];
1338 glblendfunc[0] = gl_state.blendfunc1;
1339 glblendfunc[1] = gl_state.blendfunc2;
1340 for (i = 0;i < 2;i++)
1342 switch(glblendfunc[i])
1344 case GL_ZERO: d3dblendfunc[i] = D3DBLEND_ZERO;break;
1345 case GL_ONE: d3dblendfunc[i] = D3DBLEND_ONE;break;
1346 case GL_SRC_COLOR: d3dblendfunc[i] = D3DBLEND_SRCCOLOR;break;
1347 case GL_ONE_MINUS_SRC_COLOR: d3dblendfunc[i] = D3DBLEND_INVSRCCOLOR;break;
1348 case GL_SRC_ALPHA: d3dblendfunc[i] = D3DBLEND_SRCALPHA;break;
1349 case GL_ONE_MINUS_SRC_ALPHA: d3dblendfunc[i] = D3DBLEND_INVSRCALPHA;break;
1350 case GL_DST_ALPHA: d3dblendfunc[i] = D3DBLEND_DESTALPHA;break;
1351 case GL_ONE_MINUS_DST_ALPHA: d3dblendfunc[i] = D3DBLEND_INVDESTALPHA;break;
1352 case GL_DST_COLOR: d3dblendfunc[i] = D3DBLEND_DESTCOLOR;break;
1353 case GL_ONE_MINUS_DST_COLOR: d3dblendfunc[i] = D3DBLEND_INVDESTCOLOR;break;
1356 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_SRCBLEND, d3dblendfunc[0]);
1357 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_DESTBLEND, d3dblendfunc[1]);
1358 if (gl_state.blend != blendenable)
1360 gl_state.blend = blendenable;
1361 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ALPHABLENDENABLE, gl_state.blend);
1366 case RENDERPATH_D3D10:
1367 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1369 case RENDERPATH_D3D11:
1370 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1376 void GL_DepthMask(int state)
1378 if (gl_state.depthmask != state)
1380 gl_state.depthmask = state;
1381 switch(vid.renderpath)
1383 case RENDERPATH_GL11:
1384 case RENDERPATH_GL13:
1385 case RENDERPATH_GL20:
1386 case RENDERPATH_CGGL:
1388 qglDepthMask(gl_state.depthmask);CHECKGLERROR
1390 case RENDERPATH_D3D9:
1392 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZWRITEENABLE, gl_state.depthmask);
1395 case RENDERPATH_D3D10:
1396 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1398 case RENDERPATH_D3D11:
1399 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1405 void GL_DepthTest(int state)
1407 if (gl_state.depthtest != state)
1409 gl_state.depthtest = state;
1410 switch(vid.renderpath)
1412 case RENDERPATH_GL11:
1413 case RENDERPATH_GL13:
1414 case RENDERPATH_GL20:
1415 case RENDERPATH_CGGL:
1417 if (gl_state.depthtest)
1419 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1423 qglDisable(GL_DEPTH_TEST);CHECKGLERROR
1426 case RENDERPATH_D3D9:
1428 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZENABLE, gl_state.depthtest);
1431 case RENDERPATH_D3D10:
1432 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1434 case RENDERPATH_D3D11:
1435 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1441 void GL_DepthFunc(int state)
1443 if (gl_state.depthfunc != state)
1445 gl_state.depthfunc = state;
1446 switch(vid.renderpath)
1448 case RENDERPATH_GL11:
1449 case RENDERPATH_GL13:
1450 case RENDERPATH_GL20:
1451 case RENDERPATH_CGGL:
1453 qglDepthFunc(gl_state.depthfunc);CHECKGLERROR
1455 case RENDERPATH_D3D9:
1457 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZFUNC, d3dcmpforglfunc(gl_state.depthfunc));
1460 case RENDERPATH_D3D10:
1461 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1463 case RENDERPATH_D3D11:
1464 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1470 void GL_DepthRange(float nearfrac, float farfrac)
1472 if (gl_state.depthrange[0] != nearfrac || gl_state.depthrange[1] != farfrac)
1474 gl_state.depthrange[0] = nearfrac;
1475 gl_state.depthrange[1] = farfrac;
1476 switch(vid.renderpath)
1478 case RENDERPATH_GL11:
1479 case RENDERPATH_GL13:
1480 case RENDERPATH_GL20:
1481 case RENDERPATH_CGGL:
1482 qglDepthRange(gl_state.depthrange[0], gl_state.depthrange[1]);
1484 case RENDERPATH_D3D9:
1487 D3DVIEWPORT9 d3dviewport;
1488 d3dviewport.X = gl_viewport.x;
1489 d3dviewport.Y = gl_viewport.y;
1490 d3dviewport.Width = gl_viewport.width;
1491 d3dviewport.Height = gl_viewport.height;
1492 d3dviewport.MinZ = gl_state.depthrange[0];
1493 d3dviewport.MaxZ = gl_state.depthrange[1];
1494 IDirect3DDevice9_SetViewport(vid_d3d9dev, &d3dviewport);
1498 case RENDERPATH_D3D10:
1499 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1501 case RENDERPATH_D3D11:
1502 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1508 void R_SetStencilSeparate(qboolean enable, int writemask, int frontfail, int frontzfail, int frontzpass, int backfail, int backzfail, int backzpass, int frontcompare, int backcompare, int comparereference, int comparemask)
1510 switch (vid.renderpath)
1512 case RENDERPATH_GL11:
1513 case RENDERPATH_GL13:
1514 case RENDERPATH_GL20:
1515 case RENDERPATH_CGGL:
1519 qglEnable(GL_STENCIL_TEST);CHECKGLERROR
1523 qglDisable(GL_STENCIL_TEST);CHECKGLERROR
1525 if (vid.support.ati_separate_stencil)
1527 qglStencilMask(writemask);CHECKGLERROR
1528 qglStencilOpSeparate(GL_FRONT, frontfail, frontzfail, frontzpass);CHECKGLERROR
1529 qglStencilOpSeparate(GL_BACK, backfail, backzfail, backzpass);CHECKGLERROR
1530 qglStencilFuncSeparate(frontcompare, backcompare, comparereference, comparereference);CHECKGLERROR
1532 else if (vid.support.ext_stencil_two_side)
1534 qglEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
1535 qglActiveStencilFaceEXT(GL_FRONT);CHECKGLERROR
1536 qglStencilMask(writemask);CHECKGLERROR
1537 qglStencilOp(frontfail, frontzfail, frontzpass);CHECKGLERROR
1538 qglStencilFunc(frontcompare, comparereference, comparemask);CHECKGLERROR
1539 qglActiveStencilFaceEXT(GL_BACK);CHECKGLERROR
1540 qglStencilMask(writemask);CHECKGLERROR
1541 qglStencilOp(backfail, backzfail, backzpass);CHECKGLERROR
1542 qglStencilFunc(backcompare, comparereference, comparemask);CHECKGLERROR
1545 case RENDERPATH_D3D9:
1547 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_TWOSIDEDSTENCILMODE, true);
1548 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILENABLE, enable);
1549 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILWRITEMASK, writemask);
1550 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILFAIL, d3dstencilopforglfunc(frontfail));
1551 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILZFAIL, d3dstencilopforglfunc(frontzfail));
1552 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILPASS, d3dstencilopforglfunc(frontzpass));
1553 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILFUNC, frontcompare);
1554 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CCW_STENCILFAIL, d3dstencilopforglfunc(backfail));
1555 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CCW_STENCILZFAIL, d3dstencilopforglfunc(backzfail));
1556 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CCW_STENCILPASS, d3dstencilopforglfunc(backzpass));
1557 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CCW_STENCILFUNC, backcompare);
1558 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILREF, comparereference);
1559 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILMASK, comparemask);
1562 case RENDERPATH_D3D10:
1563 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1565 case RENDERPATH_D3D11:
1566 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1571 void R_SetStencil(qboolean enable, int writemask, int fail, int zfail, int zpass, int compare, int comparereference, int comparemask)
1573 switch (vid.renderpath)
1575 case RENDERPATH_GL11:
1576 case RENDERPATH_GL13:
1577 case RENDERPATH_GL20:
1578 case RENDERPATH_CGGL:
1582 qglEnable(GL_STENCIL_TEST);CHECKGLERROR
1586 qglDisable(GL_STENCIL_TEST);CHECKGLERROR
1588 if (vid.support.ext_stencil_two_side)
1590 qglDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
1592 qglStencilMask(writemask);CHECKGLERROR
1593 qglStencilOp(fail, zfail, zpass);CHECKGLERROR
1594 qglStencilFunc(compare, comparereference, comparemask);CHECKGLERROR
1597 case RENDERPATH_D3D9:
1599 if (vid.support.ati_separate_stencil)
1600 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_TWOSIDEDSTENCILMODE, true);
1601 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILENABLE, enable);
1602 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILWRITEMASK, writemask);
1603 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILFAIL, d3dstencilopforglfunc(fail));
1604 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILZFAIL, d3dstencilopforglfunc(zfail));
1605 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILPASS, d3dstencilopforglfunc(zpass));
1606 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILFUNC, compare);
1607 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILREF, comparereference);
1608 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILMASK, comparemask);
1611 case RENDERPATH_D3D10:
1612 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1614 case RENDERPATH_D3D11:
1615 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1620 void GL_PolygonOffset(float planeoffset, float depthoffset)
1622 if (gl_state.polygonoffset[0] != planeoffset || gl_state.polygonoffset[1] != depthoffset)
1624 gl_state.polygonoffset[0] = planeoffset;
1625 gl_state.polygonoffset[1] = depthoffset;
1626 switch(vid.renderpath)
1628 case RENDERPATH_GL11:
1629 case RENDERPATH_GL13:
1630 case RENDERPATH_GL20:
1631 case RENDERPATH_CGGL:
1632 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
1634 case RENDERPATH_D3D9:
1636 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_SLOPESCALEDEPTHBIAS, gl_state.polygonoffset[0]);
1637 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_DEPTHBIAS, gl_state.polygonoffset[1] * (1.0f / 16777216.0f));
1640 case RENDERPATH_D3D10:
1641 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1643 case RENDERPATH_D3D11:
1644 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1650 void GL_SetMirrorState(qboolean state)
1652 if (v_flipped_state != state)
1654 v_flipped_state = state;
1655 if (gl_state.cullface == GL_BACK)
1656 gl_state.cullface = GL_FRONT;
1657 else if (gl_state.cullface == GL_FRONT)
1658 gl_state.cullface = GL_BACK;
1661 switch(vid.renderpath)
1663 case RENDERPATH_GL11:
1664 case RENDERPATH_GL13:
1665 case RENDERPATH_GL20:
1666 case RENDERPATH_CGGL:
1667 qglCullFace(gl_state.cullface);
1669 case RENDERPATH_D3D9:
1671 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, gl_state.cullface == GL_FRONT ? D3DCULL_CCW : D3DCULL_CW);
1674 case RENDERPATH_D3D10:
1675 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1677 case RENDERPATH_D3D11:
1678 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1684 void GL_CullFace(int state)
1688 if(state == GL_FRONT)
1690 else if(state == GL_BACK)
1694 switch(vid.renderpath)
1696 case RENDERPATH_GL11:
1697 case RENDERPATH_GL13:
1698 case RENDERPATH_GL20:
1699 case RENDERPATH_CGGL:
1702 if (state != GL_NONE)
1704 if (!gl_state.cullfaceenable)
1706 gl_state.cullfaceenable = true;
1707 qglEnable(GL_CULL_FACE);CHECKGLERROR
1709 if (gl_state.cullface != state)
1711 gl_state.cullface = state;
1712 qglCullFace(gl_state.cullface);CHECKGLERROR
1717 if (gl_state.cullfaceenable)
1719 gl_state.cullfaceenable = false;
1720 qglDisable(GL_CULL_FACE);CHECKGLERROR
1724 case RENDERPATH_D3D9:
1726 if (gl_state.cullface != state)
1728 gl_state.cullface = state;
1729 switch(gl_state.cullface)
1732 gl_state.cullfaceenable = false;
1733 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, D3DCULL_NONE);
1736 gl_state.cullfaceenable = true;
1737 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, D3DCULL_CCW);
1740 gl_state.cullfaceenable = true;
1741 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, D3DCULL_CW);
1747 case RENDERPATH_D3D10:
1748 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1750 case RENDERPATH_D3D11:
1751 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1756 void GL_AlphaTest(int state)
1758 if (gl_state.alphatest != state)
1760 gl_state.alphatest = state;
1761 switch(vid.renderpath)
1763 case RENDERPATH_GL11:
1764 case RENDERPATH_GL13:
1765 case RENDERPATH_GL20:
1766 case RENDERPATH_CGGL:
1768 if (gl_state.alphatest)
1770 qglEnable(GL_ALPHA_TEST);CHECKGLERROR
1774 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
1777 case RENDERPATH_D3D9:
1779 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ALPHATESTENABLE, gl_state.alphatest);
1782 case RENDERPATH_D3D10:
1783 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1785 case RENDERPATH_D3D11:
1786 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1792 void GL_AlphaFunc(int state, float value)
1794 if (gl_state.alphafunc != state || gl_state.alphafuncvalue != value)
1796 gl_state.alphafunc = state;
1797 gl_state.alphafuncvalue = value;
1798 switch(vid.renderpath)
1800 case RENDERPATH_GL11:
1801 case RENDERPATH_GL13:
1802 case RENDERPATH_GL20:
1803 case RENDERPATH_CGGL:
1805 qglAlphaFunc(gl_state.alphafunc, gl_state.alphafuncvalue);CHECKGLERROR
1807 case RENDERPATH_D3D9:
1809 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ALPHAFUNC, d3dcmpforglfunc(gl_state.alphafunc));
1810 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ALPHAREF, (int)bound(0, value * 256.0f, 255));
1813 case RENDERPATH_D3D10:
1814 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1816 case RENDERPATH_D3D11:
1817 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1823 void GL_ColorMask(int r, int g, int b, int a)
1825 // NOTE: this matches D3DCOLORWRITEENABLE_RED, GREEN, BLUE, ALPHA
1826 int state = (r ? 1 : 0) | (g ? 2 : 0) | (b ? 4 : 0) | (a ? 8 : 0);
1827 if (gl_state.colormask != state)
1829 gl_state.colormask = state;
1830 switch(vid.renderpath)
1832 case RENDERPATH_GL11:
1833 case RENDERPATH_GL13:
1834 case RENDERPATH_GL20:
1835 case RENDERPATH_CGGL:
1837 qglColorMask((GLboolean)r, (GLboolean)g, (GLboolean)b, (GLboolean)a);CHECKGLERROR
1839 case RENDERPATH_D3D9:
1841 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_COLORWRITEENABLE, state);
1844 case RENDERPATH_D3D10:
1845 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1847 case RENDERPATH_D3D11:
1848 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1854 void GL_Color(float cr, float cg, float cb, float ca)
1856 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)
1858 gl_state.color4f[0] = cr;
1859 gl_state.color4f[1] = cg;
1860 gl_state.color4f[2] = cb;
1861 gl_state.color4f[3] = ca;
1862 switch(vid.renderpath)
1864 case RENDERPATH_GL11:
1865 case RENDERPATH_GL13:
1866 case RENDERPATH_GL20:
1867 case RENDERPATH_CGGL:
1869 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);
1872 case RENDERPATH_D3D9:
1873 case RENDERPATH_D3D10:
1874 case RENDERPATH_D3D11:
1875 // no equivalent in D3D
1881 void GL_Scissor (int x, int y, int width, int height)
1883 switch(vid.renderpath)
1885 case RENDERPATH_GL11:
1886 case RENDERPATH_GL13:
1887 case RENDERPATH_GL20:
1888 case RENDERPATH_CGGL:
1890 qglScissor(x, y,width,height);
1893 case RENDERPATH_D3D9:
1899 d3drect.right = x + width;
1900 d3drect.bottom = y + height;
1901 IDirect3DDevice9_SetScissorRect(vid_d3d9dev, &d3drect);
1905 case RENDERPATH_D3D10:
1906 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1908 case RENDERPATH_D3D11:
1909 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1914 void GL_ScissorTest(int state)
1916 if (gl_state.scissortest != state)
1918 gl_state.scissortest = state;
1919 switch(vid.renderpath)
1921 case RENDERPATH_GL11:
1922 case RENDERPATH_GL13:
1923 case RENDERPATH_GL20:
1924 case RENDERPATH_CGGL:
1926 if(gl_state.scissortest)
1927 qglEnable(GL_SCISSOR_TEST);
1929 qglDisable(GL_SCISSOR_TEST);
1932 case RENDERPATH_D3D9:
1934 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_SCISSORTESTENABLE, gl_state.scissortest);
1937 case RENDERPATH_D3D10:
1938 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1940 case RENDERPATH_D3D11:
1941 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1947 void GL_Clear(int mask, const float *colorvalue, float depthvalue, int stencilvalue)
1949 static const float blackcolor[4] = {0, 0, 0, 0};
1950 // prevent warnings when trying to clear a buffer that does not exist
1952 colorvalue = blackcolor;
1955 mask &= ~GL_STENCIL_BUFFER_BIT;
1958 switch(vid.renderpath)
1960 case RENDERPATH_GL11:
1961 case RENDERPATH_GL13:
1962 case RENDERPATH_GL20:
1963 case RENDERPATH_CGGL:
1965 if (mask & GL_COLOR_BUFFER_BIT)
1967 qglClearColor(colorvalue[0], colorvalue[1], colorvalue[2], colorvalue[3]);CHECKGLERROR
1969 if (mask & GL_DEPTH_BUFFER_BIT)
1971 qglClearDepth(depthvalue);CHECKGLERROR
1973 if (mask & GL_STENCIL_BUFFER_BIT)
1975 qglClearStencil(stencilvalue);CHECKGLERROR
1977 qglClear(mask);CHECKGLERROR
1979 case RENDERPATH_D3D9:
1981 IDirect3DDevice9_Clear(vid_d3d9dev, 0, NULL, ((mask & GL_COLOR_BUFFER_BIT) ? D3DCLEAR_TARGET : 0) | ((mask & GL_STENCIL_BUFFER_BIT) ? D3DCLEAR_STENCIL : 0) | ((mask & GL_DEPTH_BUFFER_BIT) ? D3DCLEAR_ZBUFFER : 0), D3DCOLOR_COLORVALUE(colorvalue[0], colorvalue[1], colorvalue[2], colorvalue[3]), depthvalue, stencilvalue);
1984 case RENDERPATH_D3D10:
1985 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1987 case RENDERPATH_D3D11:
1988 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1993 void GL_ReadPixelsBGRA(int x, int y, int width, int height, unsigned char *outpixels)
1995 switch(vid.renderpath)
1997 case RENDERPATH_GL11:
1998 case RENDERPATH_GL13:
1999 case RENDERPATH_GL20:
2000 case RENDERPATH_CGGL:
2002 qglReadPixels(x, y, width, height, GL_BGRA, GL_UNSIGNED_BYTE, outpixels);CHECKGLERROR
2004 case RENDERPATH_D3D9:
2007 // LordHavoc: we can't directly download the backbuffer because it may be
2008 // multisampled, and it may not be lockable, so we blit it to a lockable
2009 // surface of the same dimensions (but without multisample) to resolve the
2010 // multisample buffer to a normal image, and then lock that...
2011 IDirect3DSurface9 *stretchsurface = NULL;
2012 if (!FAILED(IDirect3DDevice9_CreateRenderTarget(vid_d3d9dev, vid.width, vid.height, D3DFMT_A8R8G8B8, D3DMULTISAMPLE_NONE, 0, TRUE, &stretchsurface, NULL)))
2014 D3DLOCKED_RECT lockedrect;
2015 if (!FAILED(IDirect3DDevice9_StretchRect(vid_d3d9dev, gl_state.d3drt_backbuffercolorsurface, NULL, stretchsurface, NULL, D3DTEXF_POINT)))
2017 if (!FAILED(IDirect3DSurface9_LockRect(stretchsurface, &lockedrect, NULL, D3DLOCK_READONLY)))
2020 unsigned char *row = (unsigned char *)lockedrect.pBits + x * 4 + lockedrect.Pitch * (vid.height - 1 - y);
2021 for (line = 0;line < height;line++, row -= lockedrect.Pitch)
2022 memcpy(outpixels + line * width * 4, row, width * 4);
2023 IDirect3DSurface9_UnlockRect(stretchsurface);
2026 IDirect3DSurface9_Release(stretchsurface);
2029 //IDirect3DSurface9 *syssurface = NULL;
2030 //if (!FAILED(IDirect3DDevice9_CreateRenderTarget(vid_d3d9dev, vid.width, vid.height, D3DFMT_A8R8G8B8, D3DMULTISAMPLE_NONE, 0, FALSE, &stretchsurface, NULL)))
2031 //if (!FAILED(IDirect3DDevice9_CreateOffscreenPlainSurface(vid_d3d9dev, vid.width, vid.height, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &syssurface, NULL)))
2032 //IDirect3DDevice9_GetRenderTargetData(vid_d3d9dev, gl_state.d3drt_backbuffercolorsurface, syssurface);
2033 //if (!FAILED(IDirect3DDevice9_GetFrontBufferData(vid_d3d9dev, 0, syssurface)))
2034 //if (!FAILED(IDirect3DSurface9_LockRect(syssurface, &lockedrect, NULL, D3DLOCK_READONLY)))
2035 //IDirect3DSurface9_UnlockRect(syssurface);
2036 //IDirect3DSurface9_Release(syssurface);
2040 case RENDERPATH_D3D10:
2041 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2043 case RENDERPATH_D3D11:
2044 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2049 // called at beginning of frame
2050 void R_Mesh_Start(void)
2053 gl_state.usevbo_staticvertex = (vid.support.arb_vertex_buffer_object && gl_vbo.integer) || vid.forcevbo;
2054 gl_state.usevbo_staticindex = (vid.support.arb_vertex_buffer_object && (gl_vbo.integer == 1 || gl_vbo.integer == 3)) || vid.forcevbo;
2055 gl_state.usevbo_dynamicvertex = (vid.support.arb_vertex_buffer_object && gl_vbo_dynamicvertex.integer) || vid.forcevbo;
2056 gl_state.usevbo_dynamicindex = (vid.support.arb_vertex_buffer_object && gl_vbo_dynamicindex.integer) || vid.forcevbo;
2057 if (gl_printcheckerror.integer && !gl_paranoid.integer)
2059 Con_Printf("WARNING: gl_printcheckerror is on but gl_paranoid is off, turning it on...\n");
2060 Cvar_SetValueQuick(&gl_paranoid, 1);
2064 qboolean GL_Backend_CompileShader(int programobject, GLenum shadertypeenum, const char *shadertype, int numstrings, const char **strings)
2068 char compilelog[MAX_INPUTLINE];
2069 shaderobject = qglCreateShaderObjectARB(shadertypeenum);CHECKGLERROR
2072 qglShaderSourceARB(shaderobject, numstrings, strings, NULL);CHECKGLERROR
2073 qglCompileShaderARB(shaderobject);CHECKGLERROR
2074 qglGetObjectParameterivARB(shaderobject, GL_OBJECT_COMPILE_STATUS_ARB, &shadercompiled);CHECKGLERROR
2075 qglGetInfoLogARB(shaderobject, sizeof(compilelog), NULL, compilelog);CHECKGLERROR
2076 if (compilelog[0] && (strstr(compilelog, "error") || strstr(compilelog, "ERROR") || strstr(compilelog, "Error") || strstr(compilelog, "WARNING") || strstr(compilelog, "warning") || strstr(compilelog, "Warning")))
2078 int i, j, pretextlines = 0;
2079 for (i = 0;i < numstrings - 1;i++)
2080 for (j = 0;strings[i][j];j++)
2081 if (strings[i][j] == '\n')
2083 Con_Printf("%s shader compile log:\n%s\n(line offset for any above warnings/errors: %i)\n", shadertype, compilelog, pretextlines);
2085 if (!shadercompiled)
2087 qglDeleteObjectARB(shaderobject);CHECKGLERROR
2090 qglAttachObjectARB(programobject, shaderobject);CHECKGLERROR
2091 qglDeleteObjectARB(shaderobject);CHECKGLERROR
2095 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)
2097 GLint programlinked;
2098 GLuint programobject = 0;
2099 char linklog[MAX_INPUTLINE];
2102 programobject = qglCreateProgramObjectARB();CHECKGLERROR
2106 if (vertexstrings_count && !GL_Backend_CompileShader(programobject, GL_VERTEX_SHADER_ARB, "vertex", vertexstrings_count, vertexstrings_list))
2109 #ifdef GL_GEOMETRY_SHADER_ARB
2110 if (geometrystrings_count && !GL_Backend_CompileShader(programobject, GL_GEOMETRY_SHADER_ARB, "geometry", geometrystrings_count, geometrystrings_list))
2114 if (fragmentstrings_count && !GL_Backend_CompileShader(programobject, GL_FRAGMENT_SHADER_ARB, "fragment", fragmentstrings_count, fragmentstrings_list))
2117 qglLinkProgramARB(programobject);CHECKGLERROR
2118 qglGetObjectParameterivARB(programobject, GL_OBJECT_LINK_STATUS_ARB, &programlinked);CHECKGLERROR
2119 qglGetInfoLogARB(programobject, sizeof(linklog), NULL, linklog);CHECKGLERROR
2122 if (strstr(linklog, "error") || strstr(linklog, "ERROR") || strstr(linklog, "Error") || strstr(linklog, "WARNING") || strstr(linklog, "warning") || strstr(linklog, "Warning"))
2123 Con_DPrintf("program link log:\n%s\n", linklog);
2124 // software vertex shader is ok but software fragment shader is WAY
2125 // too slow, fail program if so.
2126 // NOTE: this string might be ATI specific, but that's ok because the
2127 // ATI R300 chip (Radeon 9500-9800/X300) is the most likely to use a
2128 // software fragment shader due to low instruction and dependent
2130 if (strstr(linklog, "fragment shader will run in software"))
2131 programlinked = false;
2135 return programobject;
2137 qglDeleteObjectARB(programobject);CHECKGLERROR
2141 void GL_Backend_FreeProgram(unsigned int prog)
2144 qglDeleteObjectARB(prog);
2148 void GL_Backend_RenumberElements(int *out, int count, const int *in, int offset)
2153 for (i = 0;i < count;i++)
2154 *out++ = *in++ + offset;
2157 memcpy(out, in, sizeof(*out) * count);
2160 // renders triangles using vertices from the active arrays
2161 int paranoidblah = 0;
2162 void R_Mesh_Draw(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const r_meshbuffer_t *element3i_indexbuffer, size_t element3i_bufferoffset, const unsigned short *element3s, const r_meshbuffer_t *element3s_indexbuffer, size_t element3s_bufferoffset)
2164 unsigned int numelements = numtriangles * 3;
2166 size_t bufferoffset3i;
2168 size_t bufferoffset3s;
2169 if (numvertices < 3 || numtriangles < 1)
2171 if (numvertices < 0 || numtriangles < 0 || developer_extra.integer)
2172 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);
2175 if (!gl_mesh_prefer_short_elements.integer)
2179 if (element3i_indexbuffer)
2180 element3i_indexbuffer = NULL;
2182 // adjust the pointers for firsttriangle
2184 element3i += firsttriangle * 3;
2185 if (element3i_indexbuffer)
2186 element3i_bufferoffset += firsttriangle * 3 * sizeof(*element3i);
2188 element3s += firsttriangle * 3;
2189 if (element3s_indexbuffer)
2190 element3s_bufferoffset += firsttriangle * 3 * sizeof(*element3s);
2191 // check if the user specified to ignore static index buffers
2192 if (!gl_state.usevbo_staticindex || (gl_vbo.integer == 3 && !vid.forcevbo && (element3i_bufferoffset || element3s_bufferoffset)))
2194 element3i_indexbuffer = NULL;
2195 element3s_indexbuffer = NULL;
2197 // upload a dynamic index buffer if needed
2200 if (!element3s_indexbuffer && gl_state.usevbo_dynamicindex)
2202 if (gl_state.draw_dynamicindexbuffer)
2203 R_Mesh_UpdateMeshBuffer(gl_state.draw_dynamicindexbuffer, (void *)element3s, numelements * sizeof(*element3s));
2205 gl_state.draw_dynamicindexbuffer = R_Mesh_CreateMeshBuffer((void *)element3s, numelements * sizeof(*element3s), "temporary", true, true, true);
2206 element3s_indexbuffer = gl_state.draw_dynamicindexbuffer;
2207 element3s_bufferoffset = 0;
2212 if (!element3i_indexbuffer && gl_state.usevbo_dynamicindex)
2214 if (gl_state.draw_dynamicindexbuffer)
2215 R_Mesh_UpdateMeshBuffer(gl_state.draw_dynamicindexbuffer, (void *)element3i, numelements * sizeof(*element3i));
2217 gl_state.draw_dynamicindexbuffer = R_Mesh_CreateMeshBuffer((void *)element3i, numelements * sizeof(*element3i), "temporary", true, true, false);
2218 element3i_indexbuffer = gl_state.draw_dynamicindexbuffer;
2219 element3i_bufferoffset = 0;
2222 bufferobject3i = element3i_indexbuffer ? element3i_indexbuffer->bufferobject : 0;
2223 bufferoffset3i = element3i_bufferoffset;
2224 bufferobject3s = element3s_indexbuffer ? element3s_indexbuffer->bufferobject : 0;
2225 bufferoffset3s = element3s_bufferoffset;
2226 r_refdef.stats.meshes++;
2227 r_refdef.stats.meshes_elements += numelements;
2228 if (gl_paranoid.integer)
2231 // LordHavoc: disabled this - it needs to be updated to handle components and gltype and stride in each array
2233 unsigned int j, size;
2235 // note: there's no validation done here on buffer objects because it
2236 // is somewhat difficult to get at the data, and gl_paranoid can be
2237 // used without buffer objects if the need arises
2238 // (the data could be gotten using glMapBuffer but it would be very
2239 // slow due to uncachable video memory reads)
2240 if (!qglIsEnabled(GL_VERTEX_ARRAY))
2241 Con_Print("R_Mesh_Draw: vertex array not enabled\n");
2243 if (gl_state.pointer_vertex_pointer)
2244 for (j = 0, size = numvertices * 3, p = (int *)((float *)gl_state.pointer_vertex + firstvertex * 3);j < size;j++, p++)
2246 if (gl_state.pointer_color_enabled)
2248 if (!qglIsEnabled(GL_COLOR_ARRAY))
2249 Con_Print("R_Mesh_Draw: color array set but not enabled\n");
2251 if (gl_state.pointer_color && gl_state.pointer_color_enabled)
2252 for (j = 0, size = numvertices * 4, p = (int *)((float *)gl_state.pointer_color + firstvertex * 4);j < size;j++, p++)
2255 for (i = 0;i < vid.texarrayunits;i++)
2257 if (gl_state.units[i].arrayenabled)
2259 GL_ClientActiveTexture(i);
2260 if (!qglIsEnabled(GL_TEXTURE_COORD_ARRAY))
2261 Con_Print("R_Mesh_Draw: texcoord array set but not enabled\n");
2263 if (gl_state.units[i].pointer_texcoord && gl_state.units[i].arrayenabled)
2264 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++)
2271 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
2273 if (element3i[i] < firstvertex || element3i[i] >= firstvertex + numvertices)
2275 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3i array\n", element3i[i], firstvertex, firstvertex + numvertices);
2282 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
2284 if (element3s[i] < firstvertex || element3s[i] >= firstvertex + numvertices)
2286 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3s array\n", element3s[i], firstvertex, firstvertex + numvertices);
2292 if (r_render.integer || r_refdef.draw2dstage)
2294 switch(vid.renderpath)
2296 case RENDERPATH_GL11:
2297 case RENDERPATH_GL13:
2298 case RENDERPATH_GL20:
2299 case RENDERPATH_CGGL:
2301 if (gl_mesh_testmanualfeeding.integer)
2303 unsigned int i, j, element;
2305 qglBegin(GL_TRIANGLES);
2306 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
2309 element = element3i[i];
2311 element = element3s[i];
2313 element = firstvertex + i;
2314 for (j = 0;j < vid.texarrayunits;j++)
2316 if (gl_state.units[j].pointer_texcoord_pointer && gl_state.units[j].arrayenabled)
2318 if (gl_state.units[j].pointer_texcoord_gltype == GL_FLOAT)
2320 p = (const GLfloat *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
2321 if (vid.texarrayunits > 1)
2323 if (gl_state.units[j].pointer_texcoord_components == 4)
2324 qglMultiTexCoord4f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2], p[3]);
2325 else if (gl_state.units[j].pointer_texcoord_components == 3)
2326 qglMultiTexCoord3f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2]);
2327 else if (gl_state.units[j].pointer_texcoord_components == 2)
2328 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, p[0], p[1]);
2330 qglMultiTexCoord1f(GL_TEXTURE0_ARB + j, p[0]);
2334 if (gl_state.units[j].pointer_texcoord_components == 4)
2335 qglTexCoord4f(p[0], p[1], p[2], p[3]);
2336 else if (gl_state.units[j].pointer_texcoord_components == 3)
2337 qglTexCoord3f(p[0], p[1], p[2]);
2338 else if (gl_state.units[j].pointer_texcoord_components == 2)
2339 qglTexCoord2f(p[0], p[1]);
2341 qglTexCoord1f(p[0]);
2344 else if (gl_state.units[j].pointer_texcoord_gltype == GL_SHORT)
2346 const GLshort *s = (const GLshort *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
2347 if (vid.texarrayunits > 1)
2349 if (gl_state.units[j].pointer_texcoord_components == 4)
2350 qglMultiTexCoord4f(GL_TEXTURE0_ARB + j, s[0], s[1], s[2], s[3]);
2351 else if (gl_state.units[j].pointer_texcoord_components == 3)
2352 qglMultiTexCoord3f(GL_TEXTURE0_ARB + j, s[0], s[1], s[2]);
2353 else if (gl_state.units[j].pointer_texcoord_components == 2)
2354 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, s[0], s[1]);
2355 else if (gl_state.units[j].pointer_texcoord_components == 1)
2356 qglMultiTexCoord1f(GL_TEXTURE0_ARB + j, s[0]);
2360 if (gl_state.units[j].pointer_texcoord_components == 4)
2361 qglTexCoord4f(s[0], s[1], s[2], s[3]);
2362 else if (gl_state.units[j].pointer_texcoord_components == 3)
2363 qglTexCoord3f(s[0], s[1], s[2]);
2364 else if (gl_state.units[j].pointer_texcoord_components == 2)
2365 qglTexCoord2f(s[0], s[1]);
2366 else if (gl_state.units[j].pointer_texcoord_components == 1)
2367 qglTexCoord1f(s[0]);
2370 else if (gl_state.units[j].pointer_texcoord_gltype == GL_BYTE)
2372 const GLbyte *sb = (const GLbyte *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
2373 if (vid.texarrayunits > 1)
2375 if (gl_state.units[j].pointer_texcoord_components == 4)
2376 qglMultiTexCoord4f(GL_TEXTURE0_ARB + j, sb[0], sb[1], sb[2], sb[3]);
2377 else if (gl_state.units[j].pointer_texcoord_components == 3)
2378 qglMultiTexCoord3f(GL_TEXTURE0_ARB + j, sb[0], sb[1], sb[2]);
2379 else if (gl_state.units[j].pointer_texcoord_components == 2)
2380 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, sb[0], sb[1]);
2381 else if (gl_state.units[j].pointer_texcoord_components == 1)
2382 qglMultiTexCoord1f(GL_TEXTURE0_ARB + j, sb[0]);
2386 if (gl_state.units[j].pointer_texcoord_components == 4)
2387 qglTexCoord4f(sb[0], sb[1], sb[2], sb[3]);
2388 else if (gl_state.units[j].pointer_texcoord_components == 3)
2389 qglTexCoord3f(sb[0], sb[1], sb[2]);
2390 else if (gl_state.units[j].pointer_texcoord_components == 2)
2391 qglTexCoord2f(sb[0], sb[1]);
2392 else if (gl_state.units[j].pointer_texcoord_components == 1)
2393 qglTexCoord1f(sb[0]);
2398 if (gl_state.pointer_color_pointer && gl_state.pointer_color_enabled && gl_state.pointer_color_components == 4)
2400 if (gl_state.pointer_color_gltype == GL_FLOAT)
2402 p = (const GLfloat *)((const unsigned char *)gl_state.pointer_color_pointer + element * gl_state.pointer_color_stride);
2403 qglColor4f(p[0], p[1], p[2], p[3]);
2405 else if (gl_state.pointer_color_gltype == GL_UNSIGNED_BYTE)
2407 const GLubyte *ub = (const GLubyte *)((const unsigned char *)gl_state.pointer_color_pointer + element * gl_state.pointer_color_stride);
2408 qglColor4ub(ub[0], ub[1], ub[2], ub[3]);
2411 if (gl_state.pointer_vertex_gltype == GL_FLOAT)
2413 p = (const GLfloat *)((const unsigned char *)gl_state.pointer_vertex_pointer + element * gl_state.pointer_vertex_stride);
2414 if (gl_state.pointer_vertex_components == 4)
2415 qglVertex4f(p[0], p[1], p[2], p[3]);
2416 else if (gl_state.pointer_vertex_components == 3)
2417 qglVertex3f(p[0], p[1], p[2]);
2419 qglVertex2f(p[0], p[1]);
2425 else if (bufferobject3s)
2427 GL_BindEBO(bufferobject3s);
2428 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
2430 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, (void *)bufferoffset3s);
2435 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, (void *)(firsttriangle * sizeof(unsigned short[3])));
2439 else if (bufferobject3i)
2441 GL_BindEBO(bufferobject3i);
2442 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
2444 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, (void *)bufferoffset3i);
2449 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, (void *)(firsttriangle * sizeof(unsigned int[3])));
2456 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
2458 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, element3s);
2463 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, element3s);
2470 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
2472 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, element3i);
2477 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, element3i);
2483 qglDrawArrays(GL_TRIANGLES, firstvertex, numvertices);
2487 case RENDERPATH_D3D9:
2490 if (element3s_indexbuffer)
2492 IDirect3DDevice9_SetIndices(vid_d3d9dev, (IDirect3DIndexBuffer9 *)element3s_indexbuffer->devicebuffer);
2493 IDirect3DDevice9_DrawIndexedPrimitive(vid_d3d9dev, D3DPT_TRIANGLELIST, 0, firstvertex, numvertices, element3s_bufferoffset>>1, numtriangles);
2495 else if (element3i_indexbuffer)
2497 IDirect3DDevice9_SetIndices(vid_d3d9dev, (IDirect3DIndexBuffer9 *)element3i_indexbuffer->devicebuffer);
2498 IDirect3DDevice9_DrawIndexedPrimitive(vid_d3d9dev, D3DPT_TRIANGLELIST, 0, firstvertex, numvertices, element3i_bufferoffset>>2, numtriangles);
2501 IDirect3DDevice9_DrawPrimitive(vid_d3d9dev, D3DPT_TRIANGLELIST, firstvertex, numvertices);
2505 case RENDERPATH_D3D10:
2506 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2508 case RENDERPATH_D3D11:
2509 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2515 // restores backend state, used when done with 3D rendering
2516 void R_Mesh_Finish(void)
2520 r_meshbuffer_t *R_Mesh_CreateMeshBuffer(const void *data, size_t size, const char *name, qboolean isindexbuffer, qboolean isdynamic, qboolean isindex16)
2522 r_meshbuffer_t *buffer;
2523 if (!(isdynamic ? (isindexbuffer ? gl_state.usevbo_dynamicindex : gl_state.usevbo_dynamicvertex) : (isindexbuffer ? gl_state.usevbo_staticindex : gl_state.usevbo_staticvertex)))
2525 buffer = (r_meshbuffer_t *)Mem_ExpandableArray_AllocRecord(&gl_state.meshbufferarray);
2526 memset(buffer, 0, sizeof(*buffer));
2527 buffer->bufferobject = 0;
2528 buffer->devicebuffer = NULL;
2530 buffer->isindexbuffer = isindexbuffer;
2531 buffer->isdynamic = isdynamic;
2532 buffer->isindex16 = isindex16;
2533 strlcpy(buffer->name, name, sizeof(buffer->name));
2534 R_Mesh_UpdateMeshBuffer(buffer, data, size);
2538 void R_Mesh_UpdateMeshBuffer(r_meshbuffer_t *buffer, const void *data, size_t size)
2542 if (buffer->isindexbuffer)
2544 r_refdef.stats.indexbufferuploadcount++;
2545 r_refdef.stats.indexbufferuploadsize += size;
2549 r_refdef.stats.vertexbufferuploadcount++;
2550 r_refdef.stats.vertexbufferuploadsize += size;
2552 switch(vid.renderpath)
2554 case RENDERPATH_GL11:
2555 case RENDERPATH_GL13:
2556 case RENDERPATH_GL20:
2557 case RENDERPATH_CGGL:
2558 if (!buffer->bufferobject)
2559 qglGenBuffersARB(1, (GLuint *)&buffer->bufferobject);
2560 if (buffer->isindexbuffer)
2561 GL_BindEBO(buffer->bufferobject);
2563 GL_BindVBO(buffer->bufferobject);
2564 qglBufferDataARB(buffer->isindexbuffer ? GL_ELEMENT_ARRAY_BUFFER_ARB : GL_ARRAY_BUFFER_ARB, size, data, buffer->isdynamic ? GL_STREAM_DRAW_ARB : GL_STATIC_DRAW_ARB);
2566 case RENDERPATH_D3D9:
2570 void *datapointer = NULL;
2571 if (buffer->isindexbuffer)
2573 IDirect3DIndexBuffer9 *d3d9indexbuffer = (IDirect3DIndexBuffer9 *)buffer->devicebuffer;
2574 if (size != buffer->size || !buffer->devicebuffer)
2576 if (buffer->devicebuffer)
2577 IDirect3DIndexBuffer9_Release((IDirect3DIndexBuffer9*)buffer->devicebuffer);
2578 buffer->devicebuffer = NULL;
2579 if (FAILED(result = IDirect3DDevice9_CreateIndexBuffer(vid_d3d9dev, size, buffer->isdynamic ? D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC : 0, buffer->isindex16 ? D3DFMT_INDEX16 : D3DFMT_INDEX32, buffer->isdynamic ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED, &d3d9indexbuffer, NULL)))
2580 Sys_Error("IDirect3DDevice9_CreateIndexBuffer(%p, %d, %x, %x, %x, %p, NULL) returned %x\n", vid_d3d9dev, (int)size, buffer->isdynamic ? D3DUSAGE_DYNAMIC : 0, buffer->isindex16 ? D3DFMT_INDEX16 : D3DFMT_INDEX32, buffer->isdynamic ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED, &d3d9indexbuffer, result);
2581 buffer->devicebuffer = (void *)d3d9indexbuffer;
2582 buffer->size = size;
2584 if (!FAILED(IDirect3DIndexBuffer9_Lock(d3d9indexbuffer, 0, 0, &datapointer, buffer->isdynamic ? D3DLOCK_DISCARD : 0)))
2587 memcpy(datapointer, data, size);
2589 memset(datapointer, 0, size);
2590 IDirect3DIndexBuffer9_Unlock(d3d9indexbuffer);
2595 IDirect3DVertexBuffer9 *d3d9vertexbuffer = (IDirect3DVertexBuffer9 *)buffer->devicebuffer;
2596 if (size != buffer->size || !buffer->devicebuffer)
2598 if (buffer->devicebuffer)
2599 IDirect3DVertexBuffer9_Release((IDirect3DVertexBuffer9*)buffer->devicebuffer);
2600 buffer->devicebuffer = NULL;
2601 if (FAILED(result = IDirect3DDevice9_CreateVertexBuffer(vid_d3d9dev, size, buffer->isdynamic ? D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC : 0, 0, buffer->isdynamic ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED, &d3d9vertexbuffer, NULL)))
2602 Sys_Error("IDirect3DDevice9_CreateVertexBuffer(%p, %d, %x, %x, %x, %p, NULL) returned %x\n", vid_d3d9dev, (int)size, buffer->isdynamic ? D3DUSAGE_DYNAMIC : 0, 0, buffer->isdynamic ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED, &d3d9vertexbuffer, result);
2603 buffer->devicebuffer = (void *)d3d9vertexbuffer;
2604 buffer->size = size;
2606 if (!FAILED(IDirect3DVertexBuffer9_Lock(d3d9vertexbuffer, 0, 0, &datapointer, buffer->isdynamic ? D3DLOCK_DISCARD : 0)))
2609 memcpy(datapointer, data, size);
2611 memset(datapointer, 0, size);
2612 IDirect3DVertexBuffer9_Unlock(d3d9vertexbuffer);
2618 case RENDERPATH_D3D10:
2619 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2621 case RENDERPATH_D3D11:
2622 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2627 void R_Mesh_DestroyMeshBuffer(r_meshbuffer_t *buffer)
2631 switch(vid.renderpath)
2633 case RENDERPATH_GL11:
2634 case RENDERPATH_GL13:
2635 case RENDERPATH_GL20:
2636 case RENDERPATH_CGGL:
2637 qglDeleteBuffersARB(1, (GLuint *)&buffer->bufferobject);
2639 case RENDERPATH_D3D9:
2641 if (buffer->devicebuffer)
2643 if (buffer->isindexbuffer)
2644 IDirect3DIndexBuffer9_Release((IDirect3DIndexBuffer9 *)buffer->devicebuffer);
2646 IDirect3DVertexBuffer9_Release((IDirect3DVertexBuffer9 *)buffer->devicebuffer);
2650 case RENDERPATH_D3D10:
2651 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2653 case RENDERPATH_D3D11:
2654 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2657 Mem_ExpandableArray_FreeRecord(&gl_state.meshbufferarray, (void *)buffer);
2660 void GL_Mesh_ListVBOs(qboolean printeach)
2663 size_t ebocount = 0, ebomemory = 0;
2664 size_t vbocount = 0, vbomemory = 0;
2665 r_meshbuffer_t *buffer;
2666 endindex = Mem_ExpandableArray_IndexRange(&gl_state.meshbufferarray);
2667 for (i = 0;i < endindex;i++)
2669 buffer = (r_meshbuffer_t *) Mem_ExpandableArray_RecordAtIndex(&gl_state.meshbufferarray, i);
2672 if (buffer->isindexbuffer) {ebocount++;ebomemory += buffer->size;if (printeach) Con_Printf("indexbuffer #%i %s = %i bytes%s\n", i, buffer->name, (int)buffer->size, buffer->isdynamic ? " (dynamic)" : " (static)");}
2673 else {vbocount++;vbomemory += buffer->size;if (printeach) Con_Printf("vertexbuffer #%i %s = %i bytes%s\n", i, buffer->name, (int)buffer->size, buffer->isdynamic ? " (dynamic)" : " (static)");}
2675 Con_Printf("vertex buffers: %i indexbuffers totalling %i bytes (%.3f MB), %i vertexbuffers 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);
2680 void R_Mesh_VertexPointer(int components, int gltype, size_t stride, const void *pointer, const r_meshbuffer_t *vertexbuffer, size_t bufferoffset)
2682 int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
2683 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)
2685 gl_state.pointer_vertex_components = components;
2686 gl_state.pointer_vertex_gltype = gltype;
2687 gl_state.pointer_vertex_stride = stride;
2688 gl_state.pointer_vertex_pointer = pointer;
2689 gl_state.pointer_vertex_vertexbuffer = vertexbuffer;
2690 gl_state.pointer_vertex_offset = bufferoffset;
2692 GL_BindVBO(bufferobject);
2693 qglVertexPointer(components, gltype, stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
2697 void R_Mesh_ColorPointer(int components, int gltype, size_t stride, const void *pointer, const r_meshbuffer_t *vertexbuffer, size_t bufferoffset)
2699 // note: vertexbuffer may be non-NULL even if pointer is NULL, so check
2700 // the pointer only.
2703 int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
2704 // caller wants color array enabled
2705 if (!gl_state.pointer_color_enabled)
2707 gl_state.pointer_color_enabled = true;
2709 qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
2711 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)
2713 gl_state.pointer_color_components = components;
2714 gl_state.pointer_color_gltype = gltype;
2715 gl_state.pointer_color_stride = stride;
2716 gl_state.pointer_color_pointer = pointer;
2717 gl_state.pointer_color_vertexbuffer = vertexbuffer;
2718 gl_state.pointer_color_offset = bufferoffset;
2720 GL_BindVBO(bufferobject);
2721 qglColorPointer(components, gltype, stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
2726 // caller wants color array disabled
2727 if (gl_state.pointer_color_enabled)
2729 gl_state.pointer_color_enabled = false;
2731 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
2732 // when color array is on the glColor gets trashed, set it again
2733 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);CHECKGLERROR
2738 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)
2740 gltextureunit_t *unit = gl_state.units + unitnum;
2741 // update array settings
2743 // note: there is no need to check bufferobject here because all cases
2744 // that involve a valid bufferobject also supply a texcoord array
2747 int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
2748 // texture array unit is enabled, enable the array
2749 if (!unit->arrayenabled)
2751 unit->arrayenabled = true;
2752 GL_ClientActiveTexture(unitnum);
2753 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
2756 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)
2758 unit->pointer_texcoord_components = components;
2759 unit->pointer_texcoord_gltype = gltype;
2760 unit->pointer_texcoord_stride = stride;
2761 unit->pointer_texcoord_pointer = pointer;
2762 unit->pointer_texcoord_vertexbuffer = vertexbuffer;
2763 unit->pointer_texcoord_offset = bufferoffset;
2764 GL_ClientActiveTexture(unitnum);
2765 GL_BindVBO(bufferobject);
2766 qglTexCoordPointer(components, gltype, stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
2771 // texture array unit is disabled, disable the array
2772 if (unit->arrayenabled)
2774 unit->arrayenabled = false;
2775 GL_ClientActiveTexture(unitnum);
2776 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
2781 int R_Mesh_TexBound(unsigned int unitnum, int id)
2783 gltextureunit_t *unit = gl_state.units + unitnum;
2784 if (unitnum >= vid.teximageunits)
2786 if (id == GL_TEXTURE_2D)
2788 if (id == GL_TEXTURE_3D)
2790 if (id == GL_TEXTURE_CUBE_MAP_ARB)
2791 return unit->tcubemap;
2792 if (id == GL_TEXTURE_RECTANGLE_ARB)
2793 return unit->trectangle;
2797 void R_Mesh_CopyToTexture(rtexture_t *tex, int tx, int ty, int sx, int sy, int width, int height)
2799 switch(vid.renderpath)
2801 case RENDERPATH_GL11:
2802 case RENDERPATH_GL13:
2803 case RENDERPATH_GL20:
2804 case RENDERPATH_CGGL:
2805 R_Mesh_TexBind(0, tex);
2806 GL_ActiveTexture(0);CHECKGLERROR
2807 qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, tx, ty, sx, sy, width, height);CHECKGLERROR
2809 case RENDERPATH_D3D9:
2812 IDirect3DSurface9 *currentsurface = NULL;
2813 IDirect3DSurface9 *texturesurface = NULL;
2816 sourcerect.left = sx;
2817 sourcerect.top = sy;
2818 sourcerect.right = sx + width;
2819 sourcerect.bottom = sy + height;
2822 destrect.right = tx + width;
2823 destrect.bottom = ty + height;
2824 if (!FAILED(IDirect3DTexture9_GetSurfaceLevel(((IDirect3DTexture9 *)tex->d3dtexture), 0, &texturesurface)))
2826 if (!FAILED(IDirect3DDevice9_GetRenderTarget(vid_d3d9dev, 0, ¤tsurface)))
2828 IDirect3DDevice9_StretchRect(vid_d3d9dev, currentsurface, &sourcerect, texturesurface, &destrect, D3DTEXF_NONE);
2829 IDirect3DSurface9_Release(currentsurface);
2831 IDirect3DSurface9_Release(texturesurface);
2836 case RENDERPATH_D3D10:
2837 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2839 case RENDERPATH_D3D11:
2840 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2846 int d3drswrap[16] = {D3DRS_WRAP0, D3DRS_WRAP1, D3DRS_WRAP2, D3DRS_WRAP3, D3DRS_WRAP4, D3DRS_WRAP5, D3DRS_WRAP6, D3DRS_WRAP7, D3DRS_WRAP8, D3DRS_WRAP9, D3DRS_WRAP10, D3DRS_WRAP11, D3DRS_WRAP12, D3DRS_WRAP13, D3DRS_WRAP14, D3DRS_WRAP15};
2849 void R_Mesh_TexBind(unsigned int unitnum, rtexture_t *tex)
2851 gltextureunit_t *unit = gl_state.units + unitnum;
2852 int tex2d, tex3d, texcubemap, texnum;
2853 if (unitnum >= vid.teximageunits)
2855 // if (unit->texture == tex)
2857 switch(vid.renderpath)
2859 case RENDERPATH_GL20:
2860 case RENDERPATH_CGGL:
2863 tex = r_texture_white;
2864 // not initialized enough yet...
2868 unit->texture = tex;
2869 texnum = R_GetTexture(tex);
2870 switch(tex->gltexturetypeenum)
2872 case GL_TEXTURE_2D: if (unit->t2d != texnum) {GL_ActiveTexture(unitnum);unit->t2d = texnum;qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR}break;
2873 case GL_TEXTURE_3D: if (unit->t3d != texnum) {GL_ActiveTexture(unitnum);unit->t3d = texnum;qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR}break;
2874 case GL_TEXTURE_CUBE_MAP_ARB: if (unit->tcubemap != texnum) {GL_ActiveTexture(unitnum);unit->tcubemap = texnum;qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR}break;
2875 case GL_TEXTURE_RECTANGLE_ARB: if (unit->trectangle != texnum) {GL_ActiveTexture(unitnum);unit->trectangle = texnum;qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR}break;
2878 case RENDERPATH_GL13:
2879 case RENDERPATH_GL11:
2880 unit->texture = tex;
2886 texnum = R_GetTexture(tex);
2887 switch(tex->gltexturetypeenum)
2895 case GL_TEXTURE_CUBE_MAP_ARB:
2896 texcubemap = texnum;
2900 // update 2d texture binding
2901 if (unit->t2d != tex2d)
2903 GL_ActiveTexture(unitnum);
2908 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
2915 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
2919 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
2921 // update 3d texture binding
2922 if (unit->t3d != tex3d)
2924 GL_ActiveTexture(unitnum);
2929 qglEnable(GL_TEXTURE_3D);CHECKGLERROR
2936 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
2940 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
2942 // update cubemap texture binding
2943 if (unit->tcubemap != texcubemap)
2945 GL_ActiveTexture(unitnum);
2948 if (unit->tcubemap == 0)
2950 qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
2957 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
2960 unit->tcubemap = texcubemap;
2961 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
2964 case RENDERPATH_D3D9:
2967 extern cvar_t gl_texture_anisotropy;
2970 tex = r_texture_white;
2971 // not initialized enough yet...
2975 if (unit->texture == tex)
2977 unit->texture = tex;
2978 // upload texture if needed
2980 R_RealGetTexture(tex);
2981 IDirect3DDevice9_SetTexture(vid_d3d9dev, unitnum, (IDirect3DBaseTexture9*)tex->d3dtexture);
2982 //IDirect3DDevice9_SetRenderState(vid_d3d9dev, d3drswrap[unitnum], (tex->flags & TEXF_CLAMP) ? (D3DWRAPCOORD_0 | D3DWRAPCOORD_1 | D3DWRAPCOORD_2) : 0);
2983 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_ADDRESSU, tex->d3daddressu);
2984 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_ADDRESSV, tex->d3daddressv);
2985 if (tex->d3daddressw)
2986 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_ADDRESSW, tex->d3daddressw);
2987 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MAGFILTER, tex->d3dmagfilter);
2988 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MINFILTER, tex->d3dminfilter);
2989 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MIPFILTER, tex->d3dmipfilter);
2990 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MIPMAPLODBIAS, tex->d3dmipmaplodbias);
2991 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MAXMIPLEVEL, tex->d3dmaxmiplevelfilter);
2992 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MAXANISOTROPY, gl_texture_anisotropy.integer);
2996 case RENDERPATH_D3D10:
2997 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2999 case RENDERPATH_D3D11:
3000 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3005 void R_Mesh_TexMatrix(unsigned int unitnum, const matrix4x4_t *matrix)
3007 gltextureunit_t *unit = gl_state.units + unitnum;
3008 switch(vid.renderpath)
3010 case RENDERPATH_GL11:
3011 case RENDERPATH_GL13:
3012 case RENDERPATH_GL20:
3013 case RENDERPATH_CGGL:
3014 if (matrix && matrix->m[3][3])
3016 // texmatrix specified, check if it is different
3017 if (!unit->texmatrixenabled || memcmp(&unit->matrix, matrix, sizeof(matrix4x4_t)))
3020 unit->texmatrixenabled = true;
3021 unit->matrix = *matrix;
3023 Matrix4x4_ToArrayFloatGL(&unit->matrix, glmatrix);
3024 GL_ActiveTexture(unitnum);
3025 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
3026 qglLoadMatrixf(glmatrix);CHECKGLERROR
3027 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
3032 // no texmatrix specified, revert to identity
3033 if (unit->texmatrixenabled)
3035 unit->texmatrixenabled = false;
3036 unit->matrix = identitymatrix;
3038 GL_ActiveTexture(unitnum);
3039 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
3040 qglLoadIdentity();CHECKGLERROR
3041 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
3045 case RENDERPATH_D3D9:
3046 case RENDERPATH_D3D10:
3047 case RENDERPATH_D3D11:
3052 void R_Mesh_TexCombine(unsigned int unitnum, int combinergb, int combinealpha, int rgbscale, int alphascale)
3054 gltextureunit_t *unit = gl_state.units + unitnum;
3056 switch(vid.renderpath)
3058 case RENDERPATH_GL20:
3059 case RENDERPATH_CGGL:
3062 case RENDERPATH_GL13:
3063 // GL_ARB_texture_env_combine
3065 combinergb = GL_MODULATE;
3067 combinealpha = GL_MODULATE;
3072 if (combinergb != combinealpha || rgbscale != 1 || alphascale != 1)
3074 if (combinergb == GL_DECAL)
3075 combinergb = GL_INTERPOLATE_ARB;
3076 if (unit->combine != GL_COMBINE_ARB)
3078 unit->combine = GL_COMBINE_ARB;
3079 GL_ActiveTexture(unitnum);
3080 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);CHECKGLERROR
3081 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_TEXTURE);CHECKGLERROR // for GL_INTERPOLATE_ARB mode
3083 if (unit->combinergb != combinergb)
3085 unit->combinergb = combinergb;
3086 GL_ActiveTexture(unitnum);
3087 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
3089 if (unit->combinealpha != combinealpha)
3091 unit->combinealpha = combinealpha;
3092 GL_ActiveTexture(unitnum);
3093 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
3095 if (unit->rgbscale != rgbscale)
3097 unit->rgbscale = rgbscale;
3098 GL_ActiveTexture(unitnum);
3099 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, unit->rgbscale);CHECKGLERROR
3101 if (unit->alphascale != alphascale)
3103 unit->alphascale = alphascale;
3104 GL_ActiveTexture(unitnum);
3105 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, unit->alphascale);CHECKGLERROR
3110 if (unit->combine != combinergb)
3112 unit->combine = combinergb;
3113 GL_ActiveTexture(unitnum);
3114 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combine);CHECKGLERROR
3118 case RENDERPATH_GL11:
3121 combinergb = GL_MODULATE;
3122 if (unit->combine != combinergb)
3124 unit->combine = combinergb;
3125 GL_ActiveTexture(unitnum);
3126 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combine);CHECKGLERROR
3129 case RENDERPATH_D3D9:
3130 case RENDERPATH_D3D10:
3131 case RENDERPATH_D3D11:
3136 void R_Mesh_ResetTextureState(void)
3138 unsigned int unitnum;
3143 switch(vid.renderpath)
3145 case RENDERPATH_GL20:
3146 case RENDERPATH_CGGL:
3147 for (unitnum = 0;unitnum < vid.teximageunits;unitnum++)
3149 gltextureunit_t *unit = gl_state.units + unitnum;
3153 GL_ActiveTexture(unitnum);
3154 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
3159 GL_ActiveTexture(unitnum);
3160 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
3165 GL_ActiveTexture(unitnum);
3166 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
3168 if (unit->trectangle)
3170 unit->trectangle = 0;
3171 GL_ActiveTexture(unitnum);
3172 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
3175 for (unitnum = 0;unitnum < vid.texarrayunits;unitnum++)
3177 gltextureunit_t *unit = gl_state.units + unitnum;
3178 if (unit->arrayenabled)
3180 unit->arrayenabled = false;
3181 GL_ClientActiveTexture(unitnum);
3182 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
3185 for (unitnum = 0;unitnum < vid.texunits;unitnum++)
3187 gltextureunit_t *unit = gl_state.units + unitnum;
3188 if (unit->texmatrixenabled)
3190 unit->texmatrixenabled = false;
3191 unit->matrix = identitymatrix;
3193 GL_ActiveTexture(unitnum);
3194 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
3195 qglLoadIdentity();CHECKGLERROR
3196 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
3200 case RENDERPATH_GL13:
3201 case RENDERPATH_GL11:
3202 for (unitnum = 0;unitnum < vid.texunits;unitnum++)
3204 gltextureunit_t *unit = gl_state.units + unitnum;
3208 GL_ActiveTexture(unitnum);
3209 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
3210 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
3215 GL_ActiveTexture(unitnum);
3216 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
3217 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
3222 GL_ActiveTexture(unitnum);
3223 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
3224 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
3226 if (unit->trectangle)
3228 unit->trectangle = 0;
3229 GL_ActiveTexture(unitnum);
3230 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
3231 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
3233 if (unit->arrayenabled)
3235 unit->arrayenabled = false;
3236 GL_ClientActiveTexture(unitnum);
3237 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
3239 if (unit->texmatrixenabled)
3241 unit->texmatrixenabled = false;
3242 unit->matrix = identitymatrix;
3244 GL_ActiveTexture(unitnum);
3245 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
3246 qglLoadIdentity();CHECKGLERROR
3247 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
3249 if (unit->combine != GL_MODULATE)
3251 unit->combine = GL_MODULATE;
3252 GL_ActiveTexture(unitnum);
3253 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combine);CHECKGLERROR
3257 case RENDERPATH_D3D9:
3258 case RENDERPATH_D3D10:
3259 case RENDERPATH_D3D11:
3267 //#define r_vertexposition_d3d9fvf (D3DFVF_XYZ)
3268 //#define r_vertexgeneric_d3d9fvf (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)
3269 //#define r_vertexmesh_d3d9fvf (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX5 | D3DFVF_TEXCOORDSIZE1(3) | D3DFVF_TEXCOORDSIZE2(3) | D3DFVF_TEXCOORDSIZE3(3))
3271 D3DVERTEXELEMENT9 r_vertexposition_d3d9elements[] =
3273 {0, (int)((size_t)&((r_vertexposition_t *)0)->vertex3f), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
3277 D3DVERTEXELEMENT9 r_vertexgeneric_d3d9elements[] =
3279 {0, (int)((size_t)&((r_vertexgeneric_t *)0)->vertex3f ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
3280 {0, (int)((size_t)&((r_vertexgeneric_t *)0)->color4ub ), D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
3281 {0, (int)((size_t)&((r_vertexgeneric_t *)0)->texcoord2f), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
3285 D3DVERTEXELEMENT9 r_vertexmesh_d3d9elements[] =
3287 {0, (int)((size_t)&((r_vertexmesh_t *)0)->vertex3f ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
3288 {0, (int)((size_t)&((r_vertexmesh_t *)0)->color4ub ), D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
3289 {0, (int)((size_t)&((r_vertexmesh_t *)0)->texcoordtexture2f ), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
3290 {0, (int)((size_t)&((r_vertexmesh_t *)0)->svector3f ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1},
3291 {0, (int)((size_t)&((r_vertexmesh_t *)0)->tvector3f ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 2},
3292 {0, (int)((size_t)&((r_vertexmesh_t *)0)->normal3f ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 3},
3293 {0, (int)((size_t)&((r_vertexmesh_t *)0)->texcoordlightmap2f), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 4},
3297 IDirect3DVertexDeclaration9 *r_vertexposition_d3d9decl;
3298 IDirect3DVertexDeclaration9 *r_vertexgeneric_d3d9decl;
3299 IDirect3DVertexDeclaration9 *r_vertexmesh_d3d9decl;
3302 static void R_Mesh_InitVertexDeclarations(void)
3305 r_vertexposition_d3d9decl = NULL;
3306 r_vertexgeneric_d3d9decl = NULL;
3307 r_vertexmesh_d3d9decl = NULL;
3308 switch(vid.renderpath)
3310 case RENDERPATH_D3D9:
3311 IDirect3DDevice9_CreateVertexDeclaration(vid_d3d9dev, r_vertexposition_d3d9elements, &r_vertexposition_d3d9decl);
3312 IDirect3DDevice9_CreateVertexDeclaration(vid_d3d9dev, r_vertexgeneric_d3d9elements, &r_vertexgeneric_d3d9decl);
3313 IDirect3DDevice9_CreateVertexDeclaration(vid_d3d9dev, r_vertexmesh_d3d9elements, &r_vertexmesh_d3d9decl);
3315 case RENDERPATH_D3D10:
3316 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3318 case RENDERPATH_D3D11:
3319 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3325 static void R_Mesh_DestroyVertexDeclarations(void)
3328 if (r_vertexposition_d3d9decl)
3329 IDirect3DVertexDeclaration9_Release(r_vertexposition_d3d9decl);
3330 r_vertexposition_d3d9decl = NULL;
3331 if (r_vertexgeneric_d3d9decl)
3332 IDirect3DVertexDeclaration9_Release(r_vertexgeneric_d3d9decl);
3333 r_vertexgeneric_d3d9decl = NULL;
3334 if (r_vertexmesh_d3d9decl)
3335 IDirect3DVertexDeclaration9_Release(r_vertexmesh_d3d9decl);
3336 r_vertexmesh_d3d9decl = NULL;
3340 r_vertexposition_t *R_Mesh_PrepareVertices_Position_Lock(int numvertices)
3343 size = sizeof(r_vertexposition_t) * numvertices;
3344 if (gl_state.preparevertices_tempdatamaxsize < size)
3346 gl_state.preparevertices_tempdatamaxsize = size;
3347 gl_state.preparevertices_tempdata = Mem_Realloc(r_main_mempool, gl_state.preparevertices_tempdata, gl_state.preparevertices_tempdatamaxsize);
3349 gl_state.preparevertices_vertexposition = (r_vertexposition_t *)gl_state.preparevertices_tempdata;
3350 gl_state.preparevertices_numvertices = numvertices;
3351 return gl_state.preparevertices_vertexposition;
3354 qboolean R_Mesh_PrepareVertices_Position_Unlock(void)
3356 R_Mesh_PrepareVertices_Position(gl_state.preparevertices_numvertices, gl_state.preparevertices_vertexposition, NULL);
3357 gl_state.preparevertices_vertexposition = NULL;
3358 gl_state.preparevertices_numvertices = 0;
3362 void R_Mesh_PrepareVertices_Position_Arrays(int numvertices, const float *vertex3f)
3365 r_vertexposition_t *vertex;
3366 switch(vid.renderpath)
3368 case RENDERPATH_GL20:
3369 case RENDERPATH_CGGL:
3370 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
3371 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
3372 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3373 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3374 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3375 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3376 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3378 case RENDERPATH_GL13:
3379 case RENDERPATH_GL11:
3380 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
3381 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
3382 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3383 if (vid.texunits >= 2)
3384 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3385 if (vid.texunits >= 3)
3386 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3388 case RENDERPATH_D3D9:
3389 case RENDERPATH_D3D10:
3390 case RENDERPATH_D3D11:
3394 // no quick path for this case, convert to vertex structs
3395 vertex = R_Mesh_PrepareVertices_Position_Lock(numvertices);
3396 for (i = 0;i < numvertices;i++)
3397 VectorCopy(vertex3f + 3*i, vertex[i].vertex3f);
3398 R_Mesh_PrepareVertices_Position_Unlock();
3399 R_Mesh_PrepareVertices_Position(numvertices, vertex, NULL);
3402 void R_Mesh_PrepareVertices_Position(int numvertices, const r_vertexposition_t *vertex, const r_meshbuffer_t *vertexbuffer)
3404 // upload temporary vertexbuffer for this rendering
3405 if (!gl_state.usevbo_staticvertex)
3406 vertexbuffer = NULL;
3407 if (!vertexbuffer && gl_state.usevbo_dynamicvertex)
3409 if (gl_state.preparevertices_dynamicvertexbuffer)
3410 R_Mesh_UpdateMeshBuffer(gl_state.preparevertices_dynamicvertexbuffer, vertex, numvertices * sizeof(*vertex));
3412 gl_state.preparevertices_dynamicvertexbuffer = R_Mesh_CreateMeshBuffer(vertex, numvertices * sizeof(*vertex), "temporary", false, true, false);
3413 vertexbuffer = gl_state.preparevertices_dynamicvertexbuffer;
3417 switch(vid.renderpath)
3419 case RENDERPATH_GL20:
3420 case RENDERPATH_CGGL:
3421 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3422 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3423 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3424 case RENDERPATH_GL13:
3425 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3426 case RENDERPATH_GL11:
3427 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , vertexbuffer, (int)((unsigned char *)vertex->vertex3f - (unsigned char *)vertex));
3428 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
3429 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3431 case RENDERPATH_D3D9:
3433 IDirect3DDevice9_SetVertexDeclaration(vid_d3d9dev, r_vertexposition_d3d9decl);
3434 IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, (IDirect3DVertexBuffer9*)vertexbuffer->devicebuffer, 0, sizeof(*vertex));
3437 case RENDERPATH_D3D10:
3438 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3440 case RENDERPATH_D3D11:
3441 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3446 switch(vid.renderpath)
3448 case RENDERPATH_GL20:
3449 case RENDERPATH_CGGL:
3450 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3451 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3452 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3453 case RENDERPATH_GL13:
3454 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3455 case RENDERPATH_GL11:
3456 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , NULL, 0);
3457 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
3458 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3460 case RENDERPATH_D3D9:
3461 case RENDERPATH_D3D10:
3462 case RENDERPATH_D3D11:
3469 r_vertexgeneric_t *R_Mesh_PrepareVertices_Generic_Lock(int numvertices)
3472 size = sizeof(r_vertexgeneric_t) * numvertices;
3473 if (gl_state.preparevertices_tempdatamaxsize < size)
3475 gl_state.preparevertices_tempdatamaxsize = size;
3476 gl_state.preparevertices_tempdata = Mem_Realloc(r_main_mempool, gl_state.preparevertices_tempdata, gl_state.preparevertices_tempdatamaxsize);
3478 gl_state.preparevertices_vertexgeneric = (r_vertexgeneric_t *)gl_state.preparevertices_tempdata;
3479 gl_state.preparevertices_numvertices = numvertices;
3480 return gl_state.preparevertices_vertexgeneric;
3483 qboolean R_Mesh_PrepareVertices_Generic_Unlock(void)
3485 R_Mesh_PrepareVertices_Generic(gl_state.preparevertices_numvertices, gl_state.preparevertices_vertexgeneric, NULL);
3486 gl_state.preparevertices_vertexgeneric = NULL;
3487 gl_state.preparevertices_numvertices = 0;
3491 void R_Mesh_PrepareVertices_Generic_Arrays(int numvertices, const float *vertex3f, const float *color4f, const float *texcoord2f)
3494 r_vertexgeneric_t *vertex;
3495 switch(vid.renderpath)
3497 case RENDERPATH_GL20:
3498 case RENDERPATH_CGGL:
3499 if (gl_mesh_separatearrays.integer)
3501 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
3502 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
3503 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoord2f, NULL, 0);
3504 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3505 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3506 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3507 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3511 case RENDERPATH_GL13:
3512 case RENDERPATH_GL11:
3513 if (gl_mesh_separatearrays.integer)
3515 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
3516 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
3517 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoord2f, NULL, 0);
3518 if (vid.texunits >= 2)
3519 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3520 if (vid.texunits >= 3)
3521 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3525 case RENDERPATH_D3D9:
3526 case RENDERPATH_D3D10:
3527 case RENDERPATH_D3D11:
3531 // no quick path for this case, convert to vertex structs
3532 vertex = R_Mesh_PrepareVertices_Generic_Lock(numvertices);
3533 for (i = 0;i < numvertices;i++)
3534 VectorCopy(vertex3f + 3*i, vertex[i].vertex3f);
3537 for (i = 0;i < numvertices;i++)
3538 Vector4Scale(color4f + 4*i, 255.0f, vertex[i].color4ub);
3542 float tempcolor4f[4];
3543 unsigned char tempcolor4ub[4];
3544 Vector4Scale(gl_state.color4f, 255.0f, tempcolor4f);
3545 tempcolor4ub[0] = (unsigned char)bound(0.0f, tempcolor4f[0], 255.0f);
3546 tempcolor4ub[1] = (unsigned char)bound(0.0f, tempcolor4f[1], 255.0f);
3547 tempcolor4ub[2] = (unsigned char)bound(0.0f, tempcolor4f[2], 255.0f);
3548 tempcolor4ub[3] = (unsigned char)bound(0.0f, tempcolor4f[3], 255.0f);
3549 for (i = 0;i < numvertices;i++)
3550 Vector4Copy(tempcolor4ub, vertex[i].color4ub);
3553 for (i = 0;i < numvertices;i++)
3554 Vector2Copy(texcoord2f + 2*i, vertex[i].texcoord2f);
3555 R_Mesh_PrepareVertices_Generic_Unlock();
3556 R_Mesh_PrepareVertices_Generic(numvertices, vertex, NULL);
3559 void R_Mesh_PrepareVertices_Generic(int numvertices, const r_vertexgeneric_t *vertex, const r_meshbuffer_t *vertexbuffer)
3561 // upload temporary vertexbuffer for this rendering
3562 if (!gl_state.usevbo_staticvertex)
3563 vertexbuffer = NULL;
3564 if (!vertexbuffer && gl_state.usevbo_dynamicvertex)
3566 if (gl_state.preparevertices_dynamicvertexbuffer)
3567 R_Mesh_UpdateMeshBuffer(gl_state.preparevertices_dynamicvertexbuffer, vertex, numvertices * sizeof(*vertex));
3569 gl_state.preparevertices_dynamicvertexbuffer = R_Mesh_CreateMeshBuffer(vertex, numvertices * sizeof(*vertex), "temporary", false, true, false);
3570 vertexbuffer = gl_state.preparevertices_dynamicvertexbuffer;
3574 switch(vid.renderpath)
3576 case RENDERPATH_GL20:
3577 case RENDERPATH_CGGL:
3578 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3579 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3580 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3581 case RENDERPATH_GL13:
3582 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3583 case RENDERPATH_GL11:
3584 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , vertexbuffer, (int)((unsigned char *)vertex->vertex3f - (unsigned char *)vertex));
3585 R_Mesh_ColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(*vertex), vertex->color4ub , vertexbuffer, (int)((unsigned char *)vertex->color4ub - (unsigned char *)vertex));
3586 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoord2f , vertexbuffer, (int)((unsigned char *)vertex->texcoord2f - (unsigned char *)vertex));
3588 case RENDERPATH_D3D9:
3590 IDirect3DDevice9_SetVertexDeclaration(vid_d3d9dev, r_vertexgeneric_d3d9decl);
3591 IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, (IDirect3DVertexBuffer9*)vertexbuffer->devicebuffer, 0, sizeof(*vertex));
3594 case RENDERPATH_D3D10:
3595 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3597 case RENDERPATH_D3D11:
3598 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3603 switch(vid.renderpath)
3605 case RENDERPATH_GL20:
3606 case RENDERPATH_CGGL:
3607 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3608 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3609 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3610 case RENDERPATH_GL13:
3611 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3612 case RENDERPATH_GL11:
3613 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , NULL, 0);
3614 R_Mesh_ColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(*vertex), vertex->color4ub , NULL, 0);
3615 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoord2f , NULL, 0);
3617 case RENDERPATH_D3D9:
3618 case RENDERPATH_D3D10:
3619 case RENDERPATH_D3D11:
3626 r_vertexmesh_t *R_Mesh_PrepareVertices_Mesh_Lock(int numvertices)
3629 size = sizeof(r_vertexmesh_t) * numvertices;
3630 if (gl_state.preparevertices_tempdatamaxsize < size)
3632 gl_state.preparevertices_tempdatamaxsize = size;
3633 gl_state.preparevertices_tempdata = Mem_Realloc(r_main_mempool, gl_state.preparevertices_tempdata, gl_state.preparevertices_tempdatamaxsize);
3635 gl_state.preparevertices_vertexmesh = (r_vertexmesh_t *)gl_state.preparevertices_tempdata;
3636 gl_state.preparevertices_numvertices = numvertices;
3637 return gl_state.preparevertices_vertexmesh;
3640 qboolean R_Mesh_PrepareVertices_Mesh_Unlock(void)
3642 R_Mesh_PrepareVertices_Mesh(gl_state.preparevertices_numvertices, gl_state.preparevertices_vertexmesh, NULL);
3643 gl_state.preparevertices_vertexmesh = NULL;
3644 gl_state.preparevertices_numvertices = 0;
3648 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)
3651 r_vertexmesh_t *vertex;
3652 switch(vid.renderpath)
3654 case RENDERPATH_GL20:
3655 case RENDERPATH_CGGL:
3656 if (gl_mesh_separatearrays.integer)
3658 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
3659 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
3660 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoordtexture2f, NULL, 0);
3661 R_Mesh_TexCoordPointer(1, 3, GL_FLOAT, sizeof(float[3]), svector3f, NULL, 0);
3662 R_Mesh_TexCoordPointer(2, 3, GL_FLOAT, sizeof(float[3]), tvector3f, NULL, 0);
3663 R_Mesh_TexCoordPointer(3, 3, GL_FLOAT, sizeof(float[3]), normal3f, NULL, 0);
3664 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), texcoordlightmap2f, NULL, 0);
3668 case RENDERPATH_GL13:
3669 case RENDERPATH_GL11:
3670 if (gl_mesh_separatearrays.integer)
3672 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
3673 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
3674 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoordtexture2f, NULL, 0);
3675 if (vid.texunits >= 2)
3676 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), texcoordlightmap2f, NULL, 0);
3677 if (vid.texunits >= 3)
3678 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3682 case RENDERPATH_D3D9:
3683 case RENDERPATH_D3D10:
3684 case RENDERPATH_D3D11:
3688 vertex = R_Mesh_PrepareVertices_Mesh_Lock(numvertices);
3689 for (i = 0;i < numvertices;i++)
3690 VectorCopy(vertex3f + 3*i, vertex[i].vertex3f);
3692 for (i = 0;i < numvertices;i++)
3693 VectorCopy(svector3f + 3*i, vertex[i].svector3f);
3695 for (i = 0;i < numvertices;i++)
3696 VectorCopy(tvector3f + 3*i, vertex[i].tvector3f);
3698 for (i = 0;i < numvertices;i++)
3699 VectorCopy(normal3f + 3*i, vertex[i].normal3f);
3702 for (i = 0;i < numvertices;i++)
3703 Vector4Scale(color4f + 4*i, 255.0f, vertex[i].color4ub);
3707 float tempcolor4f[4];
3708 unsigned char tempcolor4ub[4];
3709 Vector4Scale(gl_state.color4f, 255.0f, tempcolor4f);
3710 tempcolor4ub[0] = (unsigned char)bound(0.0f, tempcolor4f[0], 255.0f);
3711 tempcolor4ub[1] = (unsigned char)bound(0.0f, tempcolor4f[1], 255.0f);
3712 tempcolor4ub[2] = (unsigned char)bound(0.0f, tempcolor4f[2], 255.0f);
3713 tempcolor4ub[3] = (unsigned char)bound(0.0f, tempcolor4f[3], 255.0f);
3714 for (i = 0;i < numvertices;i++)
3715 Vector4Copy(tempcolor4ub, vertex[i].color4ub);
3717 if (texcoordtexture2f)
3718 for (i = 0;i < numvertices;i++)
3719 Vector2Copy(texcoordtexture2f + 2*i, vertex[i].texcoordtexture2f);
3720 if (texcoordlightmap2f)
3721 for (i = 0;i < numvertices;i++)
3722 Vector2Copy(texcoordlightmap2f + 2*i, vertex[i].texcoordlightmap2f);
3723 R_Mesh_PrepareVertices_Mesh_Unlock();
3724 R_Mesh_PrepareVertices_Mesh(numvertices, vertex, NULL);
3727 void R_Mesh_PrepareVertices_Mesh(int numvertices, const r_vertexmesh_t *vertex, const r_meshbuffer_t *vertexbuffer)
3729 // upload temporary vertexbuffer for this rendering
3730 if (!gl_state.usevbo_staticvertex)
3731 vertexbuffer = NULL;
3732 if (!vertexbuffer && gl_state.usevbo_dynamicvertex)
3734 if (gl_state.preparevertices_dynamicvertexbuffer)
3735 R_Mesh_UpdateMeshBuffer(gl_state.preparevertices_dynamicvertexbuffer, vertex, numvertices * sizeof(*vertex));
3737 gl_state.preparevertices_dynamicvertexbuffer = R_Mesh_CreateMeshBuffer(vertex, numvertices * sizeof(*vertex), "temporary", false, true, false);
3738 vertexbuffer = gl_state.preparevertices_dynamicvertexbuffer;
3742 switch(vid.renderpath)
3744 case RENDERPATH_GL20:
3745 case RENDERPATH_CGGL:
3746 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , vertexbuffer, (int)((unsigned char *)vertex->vertex3f - (unsigned char *)vertex));
3747 R_Mesh_ColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(*vertex), vertex->color4ub , vertexbuffer, (int)((unsigned char *)vertex->color4ub - (unsigned char *)vertex));
3748 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordtexture2f , vertexbuffer, (int)((unsigned char *)vertex->texcoordtexture2f - (unsigned char *)vertex));
3749 R_Mesh_TexCoordPointer(1, 3, GL_FLOAT , sizeof(*vertex), vertex->svector3f , vertexbuffer, (int)((unsigned char *)vertex->svector3f - (unsigned char *)vertex));
3750 R_Mesh_TexCoordPointer(2, 3, GL_FLOAT , sizeof(*vertex), vertex->tvector3f , vertexbuffer, (int)((unsigned char *)vertex->tvector3f - (unsigned char *)vertex));
3751 R_Mesh_TexCoordPointer(3, 4, GL_FLOAT , sizeof(*vertex), vertex->normal3f , vertexbuffer, (int)((unsigned char *)vertex->normal3f - (unsigned char *)vertex));
3752 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordlightmap2f, vertexbuffer, (int)((unsigned char *)vertex->texcoordlightmap2f - (unsigned char *)vertex));
3754 case RENDERPATH_GL13:
3755 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordlightmap2f, vertexbuffer, (int)((unsigned char *)vertex->texcoordlightmap2f - (unsigned char *)vertex));
3756 case RENDERPATH_GL11:
3757 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , vertexbuffer, (int)((unsigned char *)vertex->vertex3f - (unsigned char *)vertex));
3758 R_Mesh_ColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(*vertex), vertex->color4ub , vertexbuffer, (int)((unsigned char *)vertex->color4ub - (unsigned char *)vertex));
3759 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordtexture2f , vertexbuffer, (int)((unsigned char *)vertex->texcoordtexture2f - (unsigned char *)vertex));
3761 case RENDERPATH_D3D9:
3763 IDirect3DDevice9_SetVertexDeclaration(vid_d3d9dev, r_vertexmesh_d3d9decl);
3764 IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, (IDirect3DVertexBuffer9*)vertexbuffer->devicebuffer, 0, sizeof(*vertex));
3767 case RENDERPATH_D3D10:
3768 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3770 case RENDERPATH_D3D11:
3771 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3776 switch(vid.renderpath)
3778 case RENDERPATH_GL20:
3779 case RENDERPATH_CGGL:
3780 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , NULL, 0);
3781 R_Mesh_ColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(*vertex), vertex->color4ub , NULL, 0);
3782 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordtexture2f , NULL, 0);
3783 R_Mesh_TexCoordPointer(1, 3, GL_FLOAT , sizeof(*vertex), vertex->svector3f , NULL, 0);
3784 R_Mesh_TexCoordPointer(2, 3, GL_FLOAT , sizeof(*vertex), vertex->tvector3f , NULL, 0);
3785 R_Mesh_TexCoordPointer(3, 4, GL_FLOAT , sizeof(*vertex), vertex->normal3f , NULL, 0);
3786 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordlightmap2f, NULL, 0);
3788 case RENDERPATH_GL13:
3789 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordlightmap2f, NULL, 0);
3790 case RENDERPATH_GL11:
3791 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , NULL, 0);
3792 R_Mesh_ColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(*vertex), vertex->color4ub , NULL, 0);
3793 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordtexture2f , NULL, 0);
3795 case RENDERPATH_D3D9:
3796 case RENDERPATH_D3D10:
3797 case RENDERPATH_D3D11: