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 if(v_flipped.integer)
565 frustumx = -frustumx;
567 v->type = R_VIEWPORTTYPE_PERSPECTIVE;
568 v->cameramatrix = *cameramatrix;
575 memset(m, 0, sizeof(m));
576 m[0] = 1.0 / frustumx;
577 m[5] = 1.0 / frustumy;
578 m[10] = -(farclip + nearclip) / (farclip - nearclip);
580 m[14] = -2 * nearclip * farclip / (farclip - nearclip);
581 v->screentodepth[0] = -farclip / (farclip - nearclip);
582 v->screentodepth[1] = farclip * nearclip / (farclip - nearclip);
584 Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
585 Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
586 Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
587 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
590 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
592 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
595 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)
597 matrix4x4_t tempmatrix, basematrix;
598 const float nudge = 1.0 - 1.0 / (1<<23);
600 memset(v, 0, sizeof(*v));
602 if(v_flipped.integer)
603 frustumx = -frustumx;
605 v->type = R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP;
606 v->cameramatrix = *cameramatrix;
613 memset(m, 0, sizeof(m));
614 m[ 0] = 1.0 / frustumx;
615 m[ 5] = 1.0 / frustumy;
618 m[14] = -2 * nearclip * nudge;
619 v->screentodepth[0] = (m[10] + 1) * 0.5 - 1;
620 v->screentodepth[1] = m[14] * -0.5;
622 Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
623 Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
624 Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
625 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
628 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
630 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
633 float cubeviewmatrix[6][16] =
635 // standard cubemap projections
673 float rectviewmatrix[6][16] =
675 // sign-preserving cubemap projections
714 void R_Viewport_InitCubeSideView(r_viewport_t *v, const matrix4x4_t *cameramatrix, int side, int size, float nearclip, float farclip, const float *nearplane)
716 matrix4x4_t tempmatrix, basematrix;
718 memset(v, 0, sizeof(*v));
719 v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
720 v->cameramatrix = *cameramatrix;
724 memset(m, 0, sizeof(m));
726 m[10] = -(farclip + nearclip) / (farclip - nearclip);
728 m[14] = -2 * nearclip * farclip / (farclip - nearclip);
730 Matrix4x4_FromArrayFloatGL(&basematrix, cubeviewmatrix[side]);
731 Matrix4x4_Invert_Simple(&tempmatrix, &v->cameramatrix);
732 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
735 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
737 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
740 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)
742 matrix4x4_t tempmatrix, basematrix;
744 memset(v, 0, sizeof(*v));
745 v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
746 v->cameramatrix = *cameramatrix;
747 v->x = (side & 1) * size;
748 v->y = (side >> 1) * size;
752 memset(m, 0, sizeof(m));
753 m[0] = m[5] = 1.0f * ((float)size - border) / size;
754 m[10] = -(farclip + nearclip) / (farclip - nearclip);
756 m[14] = -2 * nearclip * farclip / (farclip - nearclip);
758 Matrix4x4_FromArrayFloatGL(&basematrix, rectviewmatrix[side]);
759 Matrix4x4_Invert_Simple(&tempmatrix, &v->cameramatrix);
760 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
762 switch(vid.renderpath)
764 case RENDERPATH_GL20:
765 case RENDERPATH_CGGL:
766 case RENDERPATH_GL13:
767 case RENDERPATH_GL11:
769 case RENDERPATH_D3D9:
772 case RENDERPATH_D3D10:
773 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
775 case RENDERPATH_D3D11:
776 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
781 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
783 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
786 void R_SetViewport(const r_viewport_t *v)
791 // FIXME: v_flipped_state is evil, this probably breaks somewhere
792 GL_SetMirrorState(v_flipped.integer && (v->type == R_VIEWPORTTYPE_PERSPECTIVE || v->type == R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP));
794 // copy over the matrices to our state
795 gl_viewmatrix = v->viewmatrix;
796 gl_projectionmatrix = v->projectmatrix;
798 switch(vid.renderpath)
800 case RENDERPATH_GL20:
801 case RENDERPATH_CGGL:
803 // qglViewport(v->x, v->y, v->width, v->height);CHECKGLERROR
805 case RENDERPATH_GL13:
806 case RENDERPATH_GL11:
808 qglViewport(v->x, v->y, v->width, v->height);CHECKGLERROR
809 // Load the projection matrix into OpenGL
810 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
811 Matrix4x4_ToArrayFloatGL(&gl_projectionmatrix, m);
812 qglLoadMatrixf(m);CHECKGLERROR
813 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
815 case RENDERPATH_D3D9:
818 D3DVIEWPORT9 d3dviewport;
819 d3dviewport.X = gl_viewport.x;
820 d3dviewport.Y = gl_viewport.y;
821 d3dviewport.Width = gl_viewport.width;
822 d3dviewport.Height = gl_viewport.height;
823 d3dviewport.MinZ = gl_state.depthrange[0];
824 d3dviewport.MaxZ = gl_state.depthrange[1];
825 IDirect3DDevice9_SetViewport(vid_d3d9dev, &d3dviewport);
829 case RENDERPATH_D3D10:
830 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
832 case RENDERPATH_D3D11:
833 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
837 // force an update of the derived matrices
838 gl_modelmatrixchanged = true;
839 R_EntityMatrix(&gl_modelmatrix);
842 void R_GetViewport(r_viewport_t *v)
847 static void GL_BindVBO(int bufferobject)
849 if (gl_state.vertexbufferobject != bufferobject)
851 gl_state.vertexbufferobject = bufferobject;
853 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, bufferobject);CHECKGLERROR
857 static void GL_BindEBO(int bufferobject)
859 if (gl_state.elementbufferobject != bufferobject)
861 gl_state.elementbufferobject = bufferobject;
863 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, bufferobject);CHECKGLERROR
867 int R_Mesh_CreateFramebufferObject(rtexture_t *depthtexture, rtexture_t *colortexture, rtexture_t *colortexture2, rtexture_t *colortexture3, rtexture_t *colortexture4)
870 switch(vid.renderpath)
872 case RENDERPATH_GL11:
873 case RENDERPATH_GL13:
874 case RENDERPATH_GL20:
875 case RENDERPATH_CGGL:
876 if (!vid.support.ext_framebuffer_object)
878 qglGenFramebuffersEXT(1, (GLuint*)&temp);CHECKGLERROR
879 R_Mesh_SetRenderTargets(temp, NULL, NULL, NULL, NULL, NULL);
880 if (depthtexture) qglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, depthtexture->gltexturetypeenum, R_GetTexture(depthtexture), 0);CHECKGLERROR
881 if (colortexture) qglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, colortexture->gltexturetypeenum, R_GetTexture(colortexture), 0);CHECKGLERROR
882 if (colortexture2) qglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, colortexture2->gltexturetypeenum, R_GetTexture(colortexture2), 0);CHECKGLERROR
883 if (colortexture3) qglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT2_EXT, colortexture3->gltexturetypeenum, R_GetTexture(colortexture3), 0);CHECKGLERROR
884 if (colortexture4) qglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT3_EXT, colortexture4->gltexturetypeenum, R_GetTexture(colortexture4), 0);CHECKGLERROR
886 case RENDERPATH_D3D9:
887 case RENDERPATH_D3D10:
888 case RENDERPATH_D3D11:
894 void R_Mesh_DestroyFramebufferObject(int fbo)
896 switch(vid.renderpath)
898 case RENDERPATH_GL11:
899 case RENDERPATH_GL13:
900 case RENDERPATH_GL20:
901 case RENDERPATH_CGGL:
903 qglDeleteFramebuffersEXT(1, (GLuint*)&fbo);
905 case RENDERPATH_D3D9:
906 case RENDERPATH_D3D10:
907 case RENDERPATH_D3D11:
912 void R_Mesh_ResetRenderTargets(void)
914 switch(vid.renderpath)
916 case RENDERPATH_GL11:
917 case RENDERPATH_GL13:
918 case RENDERPATH_GL20:
919 case RENDERPATH_CGGL:
920 if (gl_state.framebufferobject)
922 gl_state.framebufferobject = 0;
923 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, gl_state.framebufferobject);
926 case RENDERPATH_D3D9:
928 if (gl_state.framebufferobject)
931 gl_state.framebufferobject = 0;
932 IDirect3DDevice9_SetDepthStencilSurface(vid_d3d9dev, gl_state.d3drt_backbufferdepthsurface);
933 IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, 0, gl_state.d3drt_backbuffercolorsurface);
934 gl_state.d3drt_depthsurface = NULL;
935 for (i = 1;i < vid.maxdrawbuffers;i++)
937 if (gl_state.d3drt_colorsurfaces[i])
939 gl_state.d3drt_colorsurfaces[i] = NULL;
940 IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, i, NULL);
946 case RENDERPATH_D3D10:
947 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
949 case RENDERPATH_D3D11:
950 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
955 void R_Mesh_SetRenderTargets(int fbo, rtexture_t *depthtexture, rtexture_t *colortexture, rtexture_t *colortexture2, rtexture_t *colortexture3, rtexture_t *colortexture4)
959 rtexture_t *textures[5];
960 Vector4Set(textures, colortexture, colortexture2, colortexture3, colortexture4);
961 textures[4] = depthtexture;
962 // unbind any matching textures immediately, otherwise D3D will complain about a bound texture being used as a render target
963 for (j = 0;j < 5;j++)
965 for (i = 0;i < vid.teximageunits;i++)
966 if (gl_state.units[i].texture == textures[j])
967 R_Mesh_TexBind(i, NULL);
968 // set up framebuffer object or render targets for the active rendering API
969 switch(vid.renderpath)
971 case RENDERPATH_GL11:
972 case RENDERPATH_GL13:
973 case RENDERPATH_GL20:
974 case RENDERPATH_CGGL:
975 if (gl_state.framebufferobject != fbo)
977 gl_state.framebufferobject = fbo;
978 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, gl_state.framebufferobject);
981 case RENDERPATH_D3D9:
983 // set up the new render targets, a NULL depthtexture intentionally binds nothing
984 // TODO: optimize: keep surface pointer around in rtexture_t until texture is freed or lost
987 gl_state.framebufferobject = 1;
988 gl_state.d3drt_depthtexture = depthtexture;
989 if (gl_state.d3drt_depthtexture)
991 IDirect3DTexture9_GetSurfaceLevel((IDirect3DTexture9 *)gl_state.d3drt_depthtexture->d3dtexture, 0, &gl_state.d3drt_depthsurface);
992 IDirect3DDevice9_SetDepthStencilSurface(vid_d3d9dev, gl_state.d3drt_depthsurface);
993 IDirect3DSurface9_Release(gl_state.d3drt_depthsurface);
996 IDirect3DDevice9_SetDepthStencilSurface(vid_d3d9dev, NULL);
997 for (i = 0;i < vid.maxdrawbuffers;i++)
999 gl_state.d3drt_colortextures[i] = textures[i];
1000 if (gl_state.d3drt_colortextures[i])
1002 IDirect3DTexture9_GetSurfaceLevel((IDirect3DTexture9 *)gl_state.d3drt_colortextures[i]->d3dtexture, 0, &gl_state.d3drt_colorsurfaces[i]);
1003 IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, i, gl_state.d3drt_colorsurfaces[i]);
1004 IDirect3DSurface9_Release(gl_state.d3drt_colorsurfaces[i]);
1007 IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, i, NULL);
1011 R_Mesh_ResetRenderTargets();
1014 case RENDERPATH_D3D10:
1015 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1017 case RENDERPATH_D3D11:
1018 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1024 static int d3dcmpforglfunc(int f)
1028 case GL_NEVER: return D3DCMP_NEVER;
1029 case GL_LESS: return D3DCMP_LESS;
1030 case GL_EQUAL: return D3DCMP_EQUAL;
1031 case GL_LEQUAL: return D3DCMP_LESSEQUAL;
1032 case GL_GREATER: return D3DCMP_GREATER;
1033 case GL_NOTEQUAL: return D3DCMP_NOTEQUAL;
1034 case GL_GEQUAL: return D3DCMP_GREATEREQUAL;
1035 case GL_ALWAYS: return D3DCMP_ALWAYS;
1036 default: Con_DPrintf("Unknown GL_DepthFunc\n");return D3DCMP_ALWAYS;
1040 static int d3dstencilopforglfunc(int f)
1044 case GL_KEEP: return D3DSTENCILOP_KEEP;
1045 case GL_INCR: return D3DSTENCILOP_INCR; // note: GL_INCR is clamped, D3DSTENCILOP_INCR wraps
1046 case GL_DECR: return D3DSTENCILOP_DECR; // note: GL_DECR is clamped, D3DSTENCILOP_DECR wraps
1047 default: Con_DPrintf("Unknown GL_StencilFunc\n");return D3DSTENCILOP_KEEP;
1053 static void GL_Backend_ResetState(void)
1056 gl_state.active = true;
1057 gl_state.depthtest = true;
1058 gl_state.alphatest = false;
1059 gl_state.alphafunc = GL_GEQUAL;
1060 gl_state.alphafuncvalue = 0.5f;
1061 gl_state.blendfunc1 = GL_ONE;
1062 gl_state.blendfunc2 = GL_ZERO;
1063 gl_state.blend = false;
1064 gl_state.depthmask = GL_TRUE;
1065 gl_state.colormask = 15;
1066 gl_state.color4f[0] = gl_state.color4f[1] = gl_state.color4f[2] = gl_state.color4f[3] = 1;
1067 gl_state.lockrange_first = 0;
1068 gl_state.lockrange_count = 0;
1069 gl_state.cullface = GL_NONE;
1070 gl_state.cullfaceenable = false;
1071 gl_state.polygonoffset[0] = 0;
1072 gl_state.polygonoffset[1] = 0;
1073 gl_state.framebufferobject = 0;
1074 gl_state.depthfunc = GL_LEQUAL;
1076 switch(vid.renderpath)
1078 case RENDERPATH_D3D9:
1081 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_COLORWRITEENABLE, gl_state.colormask);
1082 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ALPHATESTENABLE, gl_state.alphatest);
1083 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ALPHAFUNC, d3dcmpforglfunc(gl_state.alphafunc));
1084 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ALPHAREF, (int)bound(0, gl_state.alphafuncvalue * 256.0f, 255));
1085 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, D3DCULL_NONE);
1086 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZFUNC, d3dcmpforglfunc(gl_state.depthfunc));
1087 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZENABLE, gl_state.depthtest);
1088 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZWRITEENABLE, gl_state.depthmask);
1089 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_SLOPESCALEDEPTHBIAS, gl_state.polygonoffset[0]);
1090 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_DEPTHBIAS, gl_state.polygonoffset[1] * (1.0f / 16777216.0f));
1094 case RENDERPATH_D3D10:
1095 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1097 case RENDERPATH_D3D11:
1098 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1100 case RENDERPATH_GL20:
1101 case RENDERPATH_CGGL:
1104 qglColorMask(1, 1, 1, 1);CHECKGLERROR
1105 qglAlphaFunc(gl_state.alphafunc, gl_state.alphafuncvalue);CHECKGLERROR
1106 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
1107 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
1108 qglDisable(GL_BLEND);CHECKGLERROR
1109 qglCullFace(gl_state.cullface);CHECKGLERROR
1110 qglDisable(GL_CULL_FACE);CHECKGLERROR
1111 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
1112 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1113 qglDepthMask(gl_state.depthmask);CHECKGLERROR
1114 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
1116 if (vid.support.arb_vertex_buffer_object)
1118 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
1119 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
1122 if (vid.support.ext_framebuffer_object)
1124 qglBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
1125 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
1128 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), NULL);CHECKGLERROR
1129 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
1131 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL);CHECKGLERROR
1132 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1133 qglColor4f(1, 1, 1, 1);CHECKGLERROR
1135 if (vid.support.ext_framebuffer_object)
1136 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, gl_state.framebufferobject);
1138 gl_state.unit = MAX_TEXTUREUNITS;
1139 gl_state.clientunit = MAX_TEXTUREUNITS;
1140 for (i = 0;i < vid.teximageunits;i++)
1142 GL_ActiveTexture(i);
1143 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
1144 if (vid.support.ext_texture_3d)
1146 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
1148 if (vid.support.arb_texture_cube_map)
1150 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
1152 if (vid.support.arb_texture_rectangle)
1154 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);CHECKGLERROR
1158 for (i = 0;i < vid.texarrayunits;i++)
1160 GL_ClientActiveTexture(i);
1162 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
1163 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1167 case RENDERPATH_GL13:
1168 case RENDERPATH_GL11:
1171 qglColorMask(1, 1, 1, 1);CHECKGLERROR
1172 qglAlphaFunc(gl_state.alphafunc, gl_state.alphafuncvalue);CHECKGLERROR
1173 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
1174 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
1175 qglDisable(GL_BLEND);CHECKGLERROR
1176 qglCullFace(gl_state.cullface);CHECKGLERROR
1177 qglDisable(GL_CULL_FACE);CHECKGLERROR
1178 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
1179 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1180 qglDepthMask(gl_state.depthmask);CHECKGLERROR
1181 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
1183 if (vid.support.arb_vertex_buffer_object)
1185 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
1186 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
1189 if (vid.support.ext_framebuffer_object)
1191 qglBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
1192 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
1195 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), NULL);CHECKGLERROR
1196 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
1198 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL);CHECKGLERROR
1199 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1200 qglColor4f(1, 1, 1, 1);CHECKGLERROR
1202 if (vid.support.ext_framebuffer_object)
1203 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, gl_state.framebufferobject);
1205 gl_state.unit = MAX_TEXTUREUNITS;
1206 gl_state.clientunit = MAX_TEXTUREUNITS;
1207 for (i = 0;i < vid.texunits;i++)
1209 GL_ActiveTexture(i);
1210 GL_ClientActiveTexture(i);
1211 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1212 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
1213 if (vid.support.ext_texture_3d)
1215 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1216 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
1218 if (vid.support.arb_texture_cube_map)
1220 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1221 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
1223 if (vid.support.arb_texture_rectangle)
1225 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
1226 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);CHECKGLERROR
1229 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
1230 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1231 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1232 qglLoadIdentity();CHECKGLERROR
1233 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1234 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
1241 void GL_ActiveTexture(unsigned int num)
1243 if (gl_state.unit != num)
1245 gl_state.unit = num;
1246 switch(vid.renderpath)
1248 case RENDERPATH_GL11:
1249 case RENDERPATH_GL13:
1250 case RENDERPATH_GL20:
1251 case RENDERPATH_CGGL:
1252 if (qglActiveTexture)
1255 qglActiveTexture(GL_TEXTURE0_ARB + gl_state.unit);
1259 case RENDERPATH_D3D9:
1260 case RENDERPATH_D3D10:
1261 case RENDERPATH_D3D11:
1267 void GL_ClientActiveTexture(unsigned int num)
1269 if (gl_state.clientunit != num)
1271 gl_state.clientunit = num;
1272 switch(vid.renderpath)
1274 case RENDERPATH_GL11:
1275 case RENDERPATH_GL13:
1276 case RENDERPATH_GL20:
1277 case RENDERPATH_CGGL:
1278 if (qglActiveTexture)
1281 qglClientActiveTexture(GL_TEXTURE0_ARB + gl_state.clientunit);
1285 case RENDERPATH_D3D9:
1286 case RENDERPATH_D3D10:
1287 case RENDERPATH_D3D11:
1293 void GL_BlendFunc(int blendfunc1, int blendfunc2)
1295 if (gl_state.blendfunc1 != blendfunc1 || gl_state.blendfunc2 != blendfunc2)
1297 qboolean blendenable;
1298 gl_state.blendfunc1 = blendfunc1;
1299 gl_state.blendfunc2 = blendfunc2;
1300 blendenable = (gl_state.blendfunc1 != GL_ONE || gl_state.blendfunc2 != GL_ZERO);
1301 switch(vid.renderpath)
1303 case RENDERPATH_GL11:
1304 case RENDERPATH_GL13:
1305 case RENDERPATH_GL20:
1306 case RENDERPATH_CGGL:
1308 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
1309 if (gl_state.blend != blendenable)
1311 gl_state.blend = blendenable;
1312 if (!gl_state.blend)
1314 qglDisable(GL_BLEND);CHECKGLERROR
1318 qglEnable(GL_BLEND);CHECKGLERROR
1322 case RENDERPATH_D3D9:
1327 D3DBLEND d3dblendfunc[2];
1328 glblendfunc[0] = gl_state.blendfunc1;
1329 glblendfunc[1] = gl_state.blendfunc2;
1330 for (i = 0;i < 2;i++)
1332 switch(glblendfunc[i])
1334 case GL_ZERO: d3dblendfunc[i] = D3DBLEND_ZERO;break;
1335 case GL_ONE: d3dblendfunc[i] = D3DBLEND_ONE;break;
1336 case GL_SRC_COLOR: d3dblendfunc[i] = D3DBLEND_SRCCOLOR;break;
1337 case GL_ONE_MINUS_SRC_COLOR: d3dblendfunc[i] = D3DBLEND_INVSRCCOLOR;break;
1338 case GL_SRC_ALPHA: d3dblendfunc[i] = D3DBLEND_SRCALPHA;break;
1339 case GL_ONE_MINUS_SRC_ALPHA: d3dblendfunc[i] = D3DBLEND_INVSRCALPHA;break;
1340 case GL_DST_ALPHA: d3dblendfunc[i] = D3DBLEND_DESTALPHA;break;
1341 case GL_ONE_MINUS_DST_ALPHA: d3dblendfunc[i] = D3DBLEND_INVDESTALPHA;break;
1342 case GL_DST_COLOR: d3dblendfunc[i] = D3DBLEND_DESTCOLOR;break;
1343 case GL_ONE_MINUS_DST_COLOR: d3dblendfunc[i] = D3DBLEND_INVDESTCOLOR;break;
1346 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_SRCBLEND, d3dblendfunc[0]);
1347 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_DESTBLEND, d3dblendfunc[1]);
1348 if (gl_state.blend != blendenable)
1350 gl_state.blend = blendenable;
1351 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ALPHABLENDENABLE, gl_state.blend);
1356 case RENDERPATH_D3D10:
1357 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1359 case RENDERPATH_D3D11:
1360 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1366 void GL_DepthMask(int state)
1368 if (gl_state.depthmask != state)
1370 gl_state.depthmask = state;
1371 switch(vid.renderpath)
1373 case RENDERPATH_GL11:
1374 case RENDERPATH_GL13:
1375 case RENDERPATH_GL20:
1376 case RENDERPATH_CGGL:
1378 qglDepthMask(gl_state.depthmask);CHECKGLERROR
1380 case RENDERPATH_D3D9:
1382 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZWRITEENABLE, gl_state.depthmask);
1385 case RENDERPATH_D3D10:
1386 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1388 case RENDERPATH_D3D11:
1389 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1395 void GL_DepthTest(int state)
1397 if (gl_state.depthtest != state)
1399 gl_state.depthtest = state;
1400 switch(vid.renderpath)
1402 case RENDERPATH_GL11:
1403 case RENDERPATH_GL13:
1404 case RENDERPATH_GL20:
1405 case RENDERPATH_CGGL:
1407 if (gl_state.depthtest)
1409 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1413 qglDisable(GL_DEPTH_TEST);CHECKGLERROR
1416 case RENDERPATH_D3D9:
1418 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZENABLE, gl_state.depthtest);
1421 case RENDERPATH_D3D10:
1422 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1424 case RENDERPATH_D3D11:
1425 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1431 void GL_DepthFunc(int state)
1433 if (gl_state.depthfunc != state)
1435 gl_state.depthfunc = state;
1436 switch(vid.renderpath)
1438 case RENDERPATH_GL11:
1439 case RENDERPATH_GL13:
1440 case RENDERPATH_GL20:
1441 case RENDERPATH_CGGL:
1443 qglDepthFunc(gl_state.depthfunc);CHECKGLERROR
1445 case RENDERPATH_D3D9:
1447 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZFUNC, d3dcmpforglfunc(gl_state.depthfunc));
1450 case RENDERPATH_D3D10:
1451 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1453 case RENDERPATH_D3D11:
1454 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1460 void GL_DepthRange(float nearfrac, float farfrac)
1462 if (gl_state.depthrange[0] != nearfrac || gl_state.depthrange[1] != farfrac)
1464 gl_state.depthrange[0] = nearfrac;
1465 gl_state.depthrange[1] = farfrac;
1466 switch(vid.renderpath)
1468 case RENDERPATH_GL11:
1469 case RENDERPATH_GL13:
1470 case RENDERPATH_GL20:
1471 case RENDERPATH_CGGL:
1472 qglDepthRange(gl_state.depthrange[0], gl_state.depthrange[1]);
1474 case RENDERPATH_D3D9:
1477 D3DVIEWPORT9 d3dviewport;
1478 d3dviewport.X = gl_viewport.x;
1479 d3dviewport.Y = gl_viewport.y;
1480 d3dviewport.Width = gl_viewport.width;
1481 d3dviewport.Height = gl_viewport.height;
1482 d3dviewport.MinZ = gl_state.depthrange[0];
1483 d3dviewport.MaxZ = gl_state.depthrange[1];
1484 IDirect3DDevice9_SetViewport(vid_d3d9dev, &d3dviewport);
1488 case RENDERPATH_D3D10:
1489 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1491 case RENDERPATH_D3D11:
1492 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1498 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)
1500 switch (vid.renderpath)
1502 case RENDERPATH_GL11:
1503 case RENDERPATH_GL13:
1504 case RENDERPATH_GL20:
1505 case RENDERPATH_CGGL:
1509 qglEnable(GL_STENCIL_TEST);CHECKGLERROR
1513 qglDisable(GL_STENCIL_TEST);CHECKGLERROR
1515 if (vid.support.ati_separate_stencil)
1517 qglStencilMask(writemask);CHECKGLERROR
1518 qglStencilOpSeparate(GL_FRONT, frontfail, frontzfail, frontzpass);CHECKGLERROR
1519 qglStencilOpSeparate(GL_BACK, backfail, backzfail, backzpass);CHECKGLERROR
1520 qglStencilFuncSeparate(frontcompare, backcompare, comparereference, comparereference);CHECKGLERROR
1522 else if (vid.support.ext_stencil_two_side)
1524 qglEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
1525 qglActiveStencilFaceEXT(GL_FRONT);CHECKGLERROR
1526 qglStencilMask(writemask);CHECKGLERROR
1527 qglStencilOp(frontfail, frontzfail, frontzpass);CHECKGLERROR
1528 qglStencilFunc(frontcompare, comparereference, comparemask);CHECKGLERROR
1529 qglActiveStencilFaceEXT(GL_BACK);CHECKGLERROR
1530 qglStencilMask(writemask);CHECKGLERROR
1531 qglStencilOp(backfail, backzfail, backzpass);CHECKGLERROR
1532 qglStencilFunc(backcompare, comparereference, comparemask);CHECKGLERROR
1535 case RENDERPATH_D3D9:
1537 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_TWOSIDEDSTENCILMODE, true);
1538 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILENABLE, enable);
1539 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILWRITEMASK, writemask);
1540 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILFAIL, d3dstencilopforglfunc(frontfail));
1541 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILZFAIL, d3dstencilopforglfunc(frontzfail));
1542 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILPASS, d3dstencilopforglfunc(frontzpass));
1543 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILFUNC, frontcompare);
1544 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CCW_STENCILFAIL, d3dstencilopforglfunc(backfail));
1545 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CCW_STENCILZFAIL, d3dstencilopforglfunc(backzfail));
1546 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CCW_STENCILPASS, d3dstencilopforglfunc(backzpass));
1547 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CCW_STENCILFUNC, backcompare);
1548 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILREF, comparereference);
1549 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILMASK, comparemask);
1552 case RENDERPATH_D3D10:
1553 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1555 case RENDERPATH_D3D11:
1556 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1561 void R_SetStencil(qboolean enable, int writemask, int fail, int zfail, int zpass, int compare, int comparereference, int comparemask)
1563 switch (vid.renderpath)
1565 case RENDERPATH_GL11:
1566 case RENDERPATH_GL13:
1567 case RENDERPATH_GL20:
1568 case RENDERPATH_CGGL:
1572 qglEnable(GL_STENCIL_TEST);CHECKGLERROR
1576 qglDisable(GL_STENCIL_TEST);CHECKGLERROR
1578 if (vid.support.ext_stencil_two_side)
1580 qglDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
1582 qglStencilMask(writemask);CHECKGLERROR
1583 qglStencilOp(fail, zfail, zpass);CHECKGLERROR
1584 qglStencilFunc(compare, comparereference, comparemask);CHECKGLERROR
1587 case RENDERPATH_D3D9:
1589 if (vid.support.ati_separate_stencil)
1590 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_TWOSIDEDSTENCILMODE, true);
1591 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILENABLE, enable);
1592 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILWRITEMASK, writemask);
1593 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILFAIL, d3dstencilopforglfunc(fail));
1594 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILZFAIL, d3dstencilopforglfunc(zfail));
1595 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILPASS, d3dstencilopforglfunc(zpass));
1596 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILFUNC, compare);
1597 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILREF, comparereference);
1598 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILMASK, comparemask);
1601 case RENDERPATH_D3D10:
1602 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1604 case RENDERPATH_D3D11:
1605 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1610 void GL_PolygonOffset(float planeoffset, float depthoffset)
1612 if (gl_state.polygonoffset[0] != planeoffset || gl_state.polygonoffset[1] != depthoffset)
1614 gl_state.polygonoffset[0] = planeoffset;
1615 gl_state.polygonoffset[1] = depthoffset;
1616 switch(vid.renderpath)
1618 case RENDERPATH_GL11:
1619 case RENDERPATH_GL13:
1620 case RENDERPATH_GL20:
1621 case RENDERPATH_CGGL:
1622 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
1624 case RENDERPATH_D3D9:
1626 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_SLOPESCALEDEPTHBIAS, gl_state.polygonoffset[0]);
1627 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_DEPTHBIAS, gl_state.polygonoffset[1] * (1.0f / 16777216.0f));
1630 case RENDERPATH_D3D10:
1631 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1633 case RENDERPATH_D3D11:
1634 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1640 void GL_SetMirrorState(qboolean state)
1642 if (v_flipped_state != state)
1644 v_flipped_state = state;
1645 if (gl_state.cullface == GL_BACK)
1646 gl_state.cullface = GL_FRONT;
1647 else if (gl_state.cullface == GL_FRONT)
1648 gl_state.cullface = GL_BACK;
1651 switch(vid.renderpath)
1653 case RENDERPATH_GL11:
1654 case RENDERPATH_GL13:
1655 case RENDERPATH_GL20:
1656 case RENDERPATH_CGGL:
1657 qglCullFace(gl_state.cullface);
1659 case RENDERPATH_D3D9:
1661 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, gl_state.cullface == GL_FRONT ? D3DCULL_CCW : D3DCULL_CW);
1664 case RENDERPATH_D3D10:
1665 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1667 case RENDERPATH_D3D11:
1668 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1674 void GL_CullFace(int state)
1678 if(state == GL_FRONT)
1680 else if(state == GL_BACK)
1684 switch(vid.renderpath)
1686 case RENDERPATH_GL11:
1687 case RENDERPATH_GL13:
1688 case RENDERPATH_GL20:
1689 case RENDERPATH_CGGL:
1692 if (state != GL_NONE)
1694 if (!gl_state.cullfaceenable)
1696 gl_state.cullfaceenable = true;
1697 qglEnable(GL_CULL_FACE);CHECKGLERROR
1699 if (gl_state.cullface != state)
1701 gl_state.cullface = state;
1702 qglCullFace(gl_state.cullface);CHECKGLERROR
1707 if (gl_state.cullfaceenable)
1709 gl_state.cullfaceenable = false;
1710 qglDisable(GL_CULL_FACE);CHECKGLERROR
1714 case RENDERPATH_D3D9:
1716 if (gl_state.cullface != state)
1718 gl_state.cullface = state;
1719 switch(gl_state.cullface)
1722 gl_state.cullfaceenable = false;
1723 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, D3DCULL_NONE);
1726 gl_state.cullfaceenable = true;
1727 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, D3DCULL_CCW);
1730 gl_state.cullfaceenable = true;
1731 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, D3DCULL_CW);
1737 case RENDERPATH_D3D10:
1738 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1740 case RENDERPATH_D3D11:
1741 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1746 void GL_AlphaTest(int state)
1748 if (gl_state.alphatest != state)
1750 gl_state.alphatest = state;
1751 switch(vid.renderpath)
1753 case RENDERPATH_GL11:
1754 case RENDERPATH_GL13:
1755 case RENDERPATH_GL20:
1756 case RENDERPATH_CGGL:
1758 if (gl_state.alphatest)
1760 qglEnable(GL_ALPHA_TEST);CHECKGLERROR
1764 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
1767 case RENDERPATH_D3D9:
1769 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ALPHATESTENABLE, gl_state.alphatest);
1772 case RENDERPATH_D3D10:
1773 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1775 case RENDERPATH_D3D11:
1776 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1782 void GL_AlphaFunc(int state, float value)
1784 if (gl_state.alphafunc != state || gl_state.alphafuncvalue != value)
1786 gl_state.alphafunc = state;
1787 gl_state.alphafuncvalue = value;
1788 switch(vid.renderpath)
1790 case RENDERPATH_GL11:
1791 case RENDERPATH_GL13:
1792 case RENDERPATH_GL20:
1793 case RENDERPATH_CGGL:
1795 qglAlphaFunc(gl_state.alphafunc, gl_state.alphafuncvalue);CHECKGLERROR
1797 case RENDERPATH_D3D9:
1799 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ALPHAFUNC, d3dcmpforglfunc(gl_state.alphafunc));
1800 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ALPHAREF, (int)bound(0, value * 256.0f, 255));
1803 case RENDERPATH_D3D10:
1804 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1806 case RENDERPATH_D3D11:
1807 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1813 void GL_ColorMask(int r, int g, int b, int a)
1815 // NOTE: this matches D3DCOLORWRITEENABLE_RED, GREEN, BLUE, ALPHA
1816 int state = (r ? 1 : 0) | (g ? 2 : 0) | (b ? 4 : 0) | (a ? 8 : 0);
1817 if (gl_state.colormask != state)
1819 gl_state.colormask = state;
1820 switch(vid.renderpath)
1822 case RENDERPATH_GL11:
1823 case RENDERPATH_GL13:
1824 case RENDERPATH_GL20:
1825 case RENDERPATH_CGGL:
1827 qglColorMask((GLboolean)r, (GLboolean)g, (GLboolean)b, (GLboolean)a);CHECKGLERROR
1829 case RENDERPATH_D3D9:
1831 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_COLORWRITEENABLE, state);
1834 case RENDERPATH_D3D10:
1835 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1837 case RENDERPATH_D3D11:
1838 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1844 void GL_Color(float cr, float cg, float cb, float ca)
1846 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)
1848 gl_state.color4f[0] = cr;
1849 gl_state.color4f[1] = cg;
1850 gl_state.color4f[2] = cb;
1851 gl_state.color4f[3] = ca;
1852 switch(vid.renderpath)
1854 case RENDERPATH_GL11:
1855 case RENDERPATH_GL13:
1856 case RENDERPATH_GL20:
1857 case RENDERPATH_CGGL:
1859 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);
1862 case RENDERPATH_D3D9:
1863 case RENDERPATH_D3D10:
1864 case RENDERPATH_D3D11:
1865 // no equivalent in D3D
1871 void GL_Scissor (int x, int y, int width, int height)
1873 switch(vid.renderpath)
1875 case RENDERPATH_GL11:
1876 case RENDERPATH_GL13:
1877 case RENDERPATH_GL20:
1878 case RENDERPATH_CGGL:
1880 qglScissor(x, y,width,height);
1883 case RENDERPATH_D3D9:
1889 d3drect.right = x + width;
1890 d3drect.bottom = y + height;
1891 IDirect3DDevice9_SetScissorRect(vid_d3d9dev, &d3drect);
1895 case RENDERPATH_D3D10:
1896 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1898 case RENDERPATH_D3D11:
1899 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1904 void GL_ScissorTest(int state)
1906 if (gl_state.scissortest != state)
1908 gl_state.scissortest = state;
1909 switch(vid.renderpath)
1911 case RENDERPATH_GL11:
1912 case RENDERPATH_GL13:
1913 case RENDERPATH_GL20:
1914 case RENDERPATH_CGGL:
1916 if(gl_state.scissortest)
1917 qglEnable(GL_SCISSOR_TEST);
1919 qglDisable(GL_SCISSOR_TEST);
1922 case RENDERPATH_D3D9:
1924 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_SCISSORTESTENABLE, gl_state.scissortest);
1927 case RENDERPATH_D3D10:
1928 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1930 case RENDERPATH_D3D11:
1931 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1937 void GL_Clear(int mask, const float *colorvalue, float depthvalue, int stencilvalue)
1939 static const float blackcolor[4] = {0, 0, 0, 0};
1940 // prevent warnings when trying to clear a buffer that does not exist
1942 colorvalue = blackcolor;
1945 mask &= ~GL_STENCIL_BUFFER_BIT;
1948 switch(vid.renderpath)
1950 case RENDERPATH_GL11:
1951 case RENDERPATH_GL13:
1952 case RENDERPATH_GL20:
1953 case RENDERPATH_CGGL:
1955 if (mask & GL_COLOR_BUFFER_BIT)
1957 qglClearColor(colorvalue[0], colorvalue[1], colorvalue[2], colorvalue[3]);CHECKGLERROR
1959 if (mask & GL_DEPTH_BUFFER_BIT)
1961 qglClearDepth(depthvalue);CHECKGLERROR
1963 if (mask & GL_STENCIL_BUFFER_BIT)
1965 qglClearStencil(stencilvalue);CHECKGLERROR
1967 qglClear(mask);CHECKGLERROR
1969 case RENDERPATH_D3D9:
1971 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);
1974 case RENDERPATH_D3D10:
1975 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1977 case RENDERPATH_D3D11:
1978 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1983 void GL_ReadPixelsBGRA(int x, int y, int width, int height, unsigned char *outpixels)
1985 switch(vid.renderpath)
1987 case RENDERPATH_GL11:
1988 case RENDERPATH_GL13:
1989 case RENDERPATH_GL20:
1990 case RENDERPATH_CGGL:
1992 qglReadPixels(x, y, width, height, GL_BGRA, GL_UNSIGNED_BYTE, outpixels);CHECKGLERROR
1994 case RENDERPATH_D3D9:
1997 // LordHavoc: we can't directly download the backbuffer because it may be
1998 // multisampled, and it may not be lockable, so we blit it to a lockable
1999 // surface of the same dimensions (but without multisample) to resolve the
2000 // multisample buffer to a normal image, and then lock that...
2001 IDirect3DSurface9 *stretchsurface = NULL;
2002 if (!FAILED(IDirect3DDevice9_CreateRenderTarget(vid_d3d9dev, vid.width, vid.height, D3DFMT_A8R8G8B8, D3DMULTISAMPLE_NONE, 0, TRUE, &stretchsurface, NULL)))
2004 D3DLOCKED_RECT lockedrect;
2005 if (!FAILED(IDirect3DDevice9_StretchRect(vid_d3d9dev, gl_state.d3drt_backbuffercolorsurface, NULL, stretchsurface, NULL, D3DTEXF_POINT)))
2007 if (!FAILED(IDirect3DSurface9_LockRect(stretchsurface, &lockedrect, NULL, D3DLOCK_READONLY)))
2010 unsigned char *row = (unsigned char *)lockedrect.pBits + x * 4 + lockedrect.Pitch * (vid.height - 1 - y);
2011 for (line = 0;line < height;line++, row -= lockedrect.Pitch)
2012 memcpy(outpixels + line * width * 4, row, width * 4);
2013 IDirect3DSurface9_UnlockRect(stretchsurface);
2016 IDirect3DSurface9_Release(stretchsurface);
2019 //IDirect3DSurface9 *syssurface = NULL;
2020 //if (!FAILED(IDirect3DDevice9_CreateRenderTarget(vid_d3d9dev, vid.width, vid.height, D3DFMT_A8R8G8B8, D3DMULTISAMPLE_NONE, 0, FALSE, &stretchsurface, NULL)))
2021 //if (!FAILED(IDirect3DDevice9_CreateOffscreenPlainSurface(vid_d3d9dev, vid.width, vid.height, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &syssurface, NULL)))
2022 //IDirect3DDevice9_GetRenderTargetData(vid_d3d9dev, gl_state.d3drt_backbuffercolorsurface, syssurface);
2023 //if (!FAILED(IDirect3DDevice9_GetFrontBufferData(vid_d3d9dev, 0, syssurface)))
2024 //if (!FAILED(IDirect3DSurface9_LockRect(syssurface, &lockedrect, NULL, D3DLOCK_READONLY)))
2025 //IDirect3DSurface9_UnlockRect(syssurface);
2026 //IDirect3DSurface9_Release(syssurface);
2030 case RENDERPATH_D3D10:
2031 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2033 case RENDERPATH_D3D11:
2034 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2039 // called at beginning of frame
2040 void R_Mesh_Start(void)
2043 gl_state.usevbo_staticvertex = (vid.support.arb_vertex_buffer_object && gl_vbo.integer) || vid.forcevbo;
2044 gl_state.usevbo_staticindex = (vid.support.arb_vertex_buffer_object && (gl_vbo.integer == 1 || gl_vbo.integer == 3)) || vid.forcevbo;
2045 gl_state.usevbo_dynamicvertex = (vid.support.arb_vertex_buffer_object && gl_vbo_dynamicvertex.integer) || vid.forcevbo;
2046 gl_state.usevbo_dynamicindex = (vid.support.arb_vertex_buffer_object && gl_vbo_dynamicindex.integer) || vid.forcevbo;
2047 if (gl_printcheckerror.integer && !gl_paranoid.integer)
2049 Con_Printf("WARNING: gl_printcheckerror is on but gl_paranoid is off, turning it on...\n");
2050 Cvar_SetValueQuick(&gl_paranoid, 1);
2054 qboolean GL_Backend_CompileShader(int programobject, GLenum shadertypeenum, const char *shadertype, int numstrings, const char **strings)
2058 char compilelog[MAX_INPUTLINE];
2059 shaderobject = qglCreateShaderObjectARB(shadertypeenum);CHECKGLERROR
2062 qglShaderSourceARB(shaderobject, numstrings, strings, NULL);CHECKGLERROR
2063 qglCompileShaderARB(shaderobject);CHECKGLERROR
2064 qglGetObjectParameterivARB(shaderobject, GL_OBJECT_COMPILE_STATUS_ARB, &shadercompiled);CHECKGLERROR
2065 qglGetInfoLogARB(shaderobject, sizeof(compilelog), NULL, compilelog);CHECKGLERROR
2066 if (compilelog[0] && (strstr(compilelog, "error") || strstr(compilelog, "ERROR") || strstr(compilelog, "Error") || strstr(compilelog, "WARNING") || strstr(compilelog, "warning") || strstr(compilelog, "Warning")))
2068 int i, j, pretextlines = 0;
2069 for (i = 0;i < numstrings - 1;i++)
2070 for (j = 0;strings[i][j];j++)
2071 if (strings[i][j] == '\n')
2073 Con_Printf("%s shader compile log:\n%s\n(line offset for any above warnings/errors: %i)\n", shadertype, compilelog, pretextlines);
2075 if (!shadercompiled)
2077 qglDeleteObjectARB(shaderobject);CHECKGLERROR
2080 qglAttachObjectARB(programobject, shaderobject);CHECKGLERROR
2081 qglDeleteObjectARB(shaderobject);CHECKGLERROR
2085 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)
2087 GLint programlinked;
2088 GLuint programobject = 0;
2089 char linklog[MAX_INPUTLINE];
2092 programobject = qglCreateProgramObjectARB();CHECKGLERROR
2096 if (vertexstrings_count && !GL_Backend_CompileShader(programobject, GL_VERTEX_SHADER_ARB, "vertex", vertexstrings_count, vertexstrings_list))
2099 #ifdef GL_GEOMETRY_SHADER_ARB
2100 if (geometrystrings_count && !GL_Backend_CompileShader(programobject, GL_GEOMETRY_SHADER_ARB, "geometry", geometrystrings_count, geometrystrings_list))
2104 if (fragmentstrings_count && !GL_Backend_CompileShader(programobject, GL_FRAGMENT_SHADER_ARB, "fragment", fragmentstrings_count, fragmentstrings_list))
2107 qglLinkProgramARB(programobject);CHECKGLERROR
2108 qglGetObjectParameterivARB(programobject, GL_OBJECT_LINK_STATUS_ARB, &programlinked);CHECKGLERROR
2109 qglGetInfoLogARB(programobject, sizeof(linklog), NULL, linklog);CHECKGLERROR
2112 if (strstr(linklog, "error") || strstr(linklog, "ERROR") || strstr(linklog, "Error") || strstr(linklog, "WARNING") || strstr(linklog, "warning") || strstr(linklog, "Warning"))
2113 Con_DPrintf("program link log:\n%s\n", linklog);
2114 // software vertex shader is ok but software fragment shader is WAY
2115 // too slow, fail program if so.
2116 // NOTE: this string might be ATI specific, but that's ok because the
2117 // ATI R300 chip (Radeon 9500-9800/X300) is the most likely to use a
2118 // software fragment shader due to low instruction and dependent
2120 if (strstr(linklog, "fragment shader will run in software"))
2121 programlinked = false;
2125 return programobject;
2127 qglDeleteObjectARB(programobject);CHECKGLERROR
2131 void GL_Backend_FreeProgram(unsigned int prog)
2134 qglDeleteObjectARB(prog);
2138 void GL_Backend_RenumberElements(int *out, int count, const int *in, int offset)
2143 for (i = 0;i < count;i++)
2144 *out++ = *in++ + offset;
2147 memcpy(out, in, sizeof(*out) * count);
2150 // renders triangles using vertices from the active arrays
2151 int paranoidblah = 0;
2152 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)
2154 unsigned int numelements = numtriangles * 3;
2156 size_t bufferoffset3i;
2158 size_t bufferoffset3s;
2159 if (numvertices < 3 || numtriangles < 1)
2161 if (numvertices < 0 || numtriangles < 0 || developer_extra.integer)
2162 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);
2165 if (!gl_mesh_prefer_short_elements.integer)
2169 if (element3i_indexbuffer)
2170 element3i_indexbuffer = NULL;
2172 // adjust the pointers for firsttriangle
2174 element3i += firsttriangle * 3;
2175 if (element3i_indexbuffer)
2176 element3i_bufferoffset += firsttriangle * 3 * sizeof(*element3i);
2178 element3s += firsttriangle * 3;
2179 if (element3s_indexbuffer)
2180 element3s_bufferoffset += firsttriangle * 3 * sizeof(*element3s);
2181 // check if the user specified to ignore static index buffers
2182 if (!gl_state.usevbo_staticindex || (gl_vbo.integer == 3 && !vid.forcevbo && (element3i_bufferoffset || element3s_bufferoffset)))
2184 element3i_indexbuffer = NULL;
2185 element3s_indexbuffer = NULL;
2187 // upload a dynamic index buffer if needed
2190 if (!element3s_indexbuffer && gl_state.usevbo_dynamicindex)
2192 if (gl_state.draw_dynamicindexbuffer)
2193 R_Mesh_UpdateMeshBuffer(gl_state.draw_dynamicindexbuffer, (void *)element3s, numelements * sizeof(*element3s));
2195 gl_state.draw_dynamicindexbuffer = R_Mesh_CreateMeshBuffer((void *)element3s, numelements * sizeof(*element3s), "temporary", true, true, true);
2196 element3s_indexbuffer = gl_state.draw_dynamicindexbuffer;
2197 element3s_bufferoffset = 0;
2202 if (!element3i_indexbuffer && gl_state.usevbo_dynamicindex)
2204 if (gl_state.draw_dynamicindexbuffer)
2205 R_Mesh_UpdateMeshBuffer(gl_state.draw_dynamicindexbuffer, (void *)element3i, numelements * sizeof(*element3i));
2207 gl_state.draw_dynamicindexbuffer = R_Mesh_CreateMeshBuffer((void *)element3i, numelements * sizeof(*element3i), "temporary", true, true, false);
2208 element3i_indexbuffer = gl_state.draw_dynamicindexbuffer;
2209 element3i_bufferoffset = 0;
2212 bufferobject3i = element3i_indexbuffer ? element3i_indexbuffer->bufferobject : 0;
2213 bufferoffset3i = element3i_bufferoffset;
2214 bufferobject3s = element3s_indexbuffer ? element3s_indexbuffer->bufferobject : 0;
2215 bufferoffset3s = element3s_bufferoffset;
2216 r_refdef.stats.meshes++;
2217 r_refdef.stats.meshes_elements += numelements;
2218 if (gl_paranoid.integer)
2221 // LordHavoc: disabled this - it needs to be updated to handle components and gltype and stride in each array
2223 unsigned int j, size;
2225 // note: there's no validation done here on buffer objects because it
2226 // is somewhat difficult to get at the data, and gl_paranoid can be
2227 // used without buffer objects if the need arises
2228 // (the data could be gotten using glMapBuffer but it would be very
2229 // slow due to uncachable video memory reads)
2230 if (!qglIsEnabled(GL_VERTEX_ARRAY))
2231 Con_Print("R_Mesh_Draw: vertex array not enabled\n");
2233 if (gl_state.pointer_vertex_pointer)
2234 for (j = 0, size = numvertices * 3, p = (int *)((float *)gl_state.pointer_vertex + firstvertex * 3);j < size;j++, p++)
2236 if (gl_state.pointer_color_enabled)
2238 if (!qglIsEnabled(GL_COLOR_ARRAY))
2239 Con_Print("R_Mesh_Draw: color array set but not enabled\n");
2241 if (gl_state.pointer_color && gl_state.pointer_color_enabled)
2242 for (j = 0, size = numvertices * 4, p = (int *)((float *)gl_state.pointer_color + firstvertex * 4);j < size;j++, p++)
2245 for (i = 0;i < vid.texarrayunits;i++)
2247 if (gl_state.units[i].arrayenabled)
2249 GL_ClientActiveTexture(i);
2250 if (!qglIsEnabled(GL_TEXTURE_COORD_ARRAY))
2251 Con_Print("R_Mesh_Draw: texcoord array set but not enabled\n");
2253 if (gl_state.units[i].pointer_texcoord && gl_state.units[i].arrayenabled)
2254 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++)
2261 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
2263 if (element3i[i] < firstvertex || element3i[i] >= firstvertex + numvertices)
2265 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3i array\n", element3i[i], firstvertex, firstvertex + numvertices);
2272 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
2274 if (element3s[i] < firstvertex || element3s[i] >= firstvertex + numvertices)
2276 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3s array\n", element3s[i], firstvertex, firstvertex + numvertices);
2282 if (r_render.integer || r_refdef.draw2dstage)
2284 switch(vid.renderpath)
2286 case RENDERPATH_GL11:
2287 case RENDERPATH_GL13:
2288 case RENDERPATH_GL20:
2289 case RENDERPATH_CGGL:
2291 if (gl_mesh_testmanualfeeding.integer)
2293 unsigned int i, j, element;
2295 qglBegin(GL_TRIANGLES);
2296 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
2299 element = element3i[i];
2301 element = element3s[i];
2303 element = firstvertex + i;
2304 for (j = 0;j < vid.texarrayunits;j++)
2306 if (gl_state.units[j].pointer_texcoord_pointer && gl_state.units[j].arrayenabled)
2308 if (gl_state.units[j].pointer_texcoord_gltype == GL_FLOAT)
2310 p = (const GLfloat *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
2311 if (vid.texarrayunits > 1)
2313 if (gl_state.units[j].pointer_texcoord_components == 4)
2314 qglMultiTexCoord4f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2], p[3]);
2315 else if (gl_state.units[j].pointer_texcoord_components == 3)
2316 qglMultiTexCoord3f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2]);
2317 else if (gl_state.units[j].pointer_texcoord_components == 2)
2318 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, p[0], p[1]);
2320 qglMultiTexCoord1f(GL_TEXTURE0_ARB + j, p[0]);
2324 if (gl_state.units[j].pointer_texcoord_components == 4)
2325 qglTexCoord4f(p[0], p[1], p[2], p[3]);
2326 else if (gl_state.units[j].pointer_texcoord_components == 3)
2327 qglTexCoord3f(p[0], p[1], p[2]);
2328 else if (gl_state.units[j].pointer_texcoord_components == 2)
2329 qglTexCoord2f(p[0], p[1]);
2331 qglTexCoord1f(p[0]);
2334 else if (gl_state.units[j].pointer_texcoord_gltype == GL_SHORT)
2336 const GLshort *s = (const GLshort *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
2337 if (vid.texarrayunits > 1)
2339 if (gl_state.units[j].pointer_texcoord_components == 4)
2340 qglMultiTexCoord4f(GL_TEXTURE0_ARB + j, s[0], s[1], s[2], s[3]);
2341 else if (gl_state.units[j].pointer_texcoord_components == 3)
2342 qglMultiTexCoord3f(GL_TEXTURE0_ARB + j, s[0], s[1], s[2]);
2343 else if (gl_state.units[j].pointer_texcoord_components == 2)
2344 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, s[0], s[1]);
2345 else if (gl_state.units[j].pointer_texcoord_components == 1)
2346 qglMultiTexCoord1f(GL_TEXTURE0_ARB + j, s[0]);
2350 if (gl_state.units[j].pointer_texcoord_components == 4)
2351 qglTexCoord4f(s[0], s[1], s[2], s[3]);
2352 else if (gl_state.units[j].pointer_texcoord_components == 3)
2353 qglTexCoord3f(s[0], s[1], s[2]);
2354 else if (gl_state.units[j].pointer_texcoord_components == 2)
2355 qglTexCoord2f(s[0], s[1]);
2356 else if (gl_state.units[j].pointer_texcoord_components == 1)
2357 qglTexCoord1f(s[0]);
2360 else if (gl_state.units[j].pointer_texcoord_gltype == GL_BYTE)
2362 const GLbyte *sb = (const GLbyte *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
2363 if (vid.texarrayunits > 1)
2365 if (gl_state.units[j].pointer_texcoord_components == 4)
2366 qglMultiTexCoord4f(GL_TEXTURE0_ARB + j, sb[0], sb[1], sb[2], sb[3]);
2367 else if (gl_state.units[j].pointer_texcoord_components == 3)
2368 qglMultiTexCoord3f(GL_TEXTURE0_ARB + j, sb[0], sb[1], sb[2]);
2369 else if (gl_state.units[j].pointer_texcoord_components == 2)
2370 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, sb[0], sb[1]);
2371 else if (gl_state.units[j].pointer_texcoord_components == 1)
2372 qglMultiTexCoord1f(GL_TEXTURE0_ARB + j, sb[0]);
2376 if (gl_state.units[j].pointer_texcoord_components == 4)
2377 qglTexCoord4f(sb[0], sb[1], sb[2], sb[3]);
2378 else if (gl_state.units[j].pointer_texcoord_components == 3)
2379 qglTexCoord3f(sb[0], sb[1], sb[2]);
2380 else if (gl_state.units[j].pointer_texcoord_components == 2)
2381 qglTexCoord2f(sb[0], sb[1]);
2382 else if (gl_state.units[j].pointer_texcoord_components == 1)
2383 qglTexCoord1f(sb[0]);
2388 if (gl_state.pointer_color_pointer && gl_state.pointer_color_enabled && gl_state.pointer_color_components == 4)
2390 if (gl_state.pointer_color_gltype == GL_FLOAT)
2392 p = (const GLfloat *)((const unsigned char *)gl_state.pointer_color_pointer + element * gl_state.pointer_color_stride);
2393 qglColor4f(p[0], p[1], p[2], p[3]);
2395 else if (gl_state.pointer_color_gltype == GL_UNSIGNED_BYTE)
2397 const GLubyte *ub = (const GLubyte *)((const unsigned char *)gl_state.pointer_color_pointer + element * gl_state.pointer_color_stride);
2398 qglColor4ub(ub[0], ub[1], ub[2], ub[3]);
2401 if (gl_state.pointer_vertex_gltype == GL_FLOAT)
2403 p = (const GLfloat *)((const unsigned char *)gl_state.pointer_vertex_pointer + element * gl_state.pointer_vertex_stride);
2404 if (gl_state.pointer_vertex_components == 4)
2405 qglVertex4f(p[0], p[1], p[2], p[3]);
2406 else if (gl_state.pointer_vertex_components == 3)
2407 qglVertex3f(p[0], p[1], p[2]);
2409 qglVertex2f(p[0], p[1]);
2415 else if (bufferobject3s)
2417 GL_BindEBO(bufferobject3s);
2418 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
2420 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, (void *)bufferoffset3s);
2425 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, (void *)(firsttriangle * sizeof(unsigned short[3])));
2429 else if (bufferobject3i)
2431 GL_BindEBO(bufferobject3i);
2432 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
2434 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, (void *)bufferoffset3i);
2439 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, (void *)(firsttriangle * sizeof(unsigned int[3])));
2446 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
2448 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, element3s);
2453 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, element3s);
2460 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
2462 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, element3i);
2467 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, element3i);
2473 qglDrawArrays(GL_TRIANGLES, firstvertex, numvertices);
2477 case RENDERPATH_D3D9:
2480 if (element3s_indexbuffer)
2482 IDirect3DDevice9_SetIndices(vid_d3d9dev, (IDirect3DIndexBuffer9 *)element3s_indexbuffer->devicebuffer);
2483 IDirect3DDevice9_DrawIndexedPrimitive(vid_d3d9dev, D3DPT_TRIANGLELIST, 0, firstvertex, numvertices, element3s_bufferoffset>>1, numtriangles);
2485 else if (element3i_indexbuffer)
2487 IDirect3DDevice9_SetIndices(vid_d3d9dev, (IDirect3DIndexBuffer9 *)element3i_indexbuffer->devicebuffer);
2488 IDirect3DDevice9_DrawIndexedPrimitive(vid_d3d9dev, D3DPT_TRIANGLELIST, 0, firstvertex, numvertices, element3i_bufferoffset>>2, numtriangles);
2491 IDirect3DDevice9_DrawPrimitive(vid_d3d9dev, D3DPT_TRIANGLELIST, firstvertex, numvertices);
2495 case RENDERPATH_D3D10:
2496 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2498 case RENDERPATH_D3D11:
2499 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2505 // restores backend state, used when done with 3D rendering
2506 void R_Mesh_Finish(void)
2510 r_meshbuffer_t *R_Mesh_CreateMeshBuffer(const void *data, size_t size, const char *name, qboolean isindexbuffer, qboolean isdynamic, qboolean isindex16)
2512 r_meshbuffer_t *buffer;
2513 if (!(isdynamic ? (isindexbuffer ? gl_state.usevbo_dynamicindex : gl_state.usevbo_dynamicvertex) : (isindexbuffer ? gl_state.usevbo_staticindex : gl_state.usevbo_staticvertex)))
2515 buffer = (r_meshbuffer_t *)Mem_ExpandableArray_AllocRecord(&gl_state.meshbufferarray);
2516 memset(buffer, 0, sizeof(*buffer));
2517 buffer->bufferobject = 0;
2518 buffer->devicebuffer = NULL;
2520 buffer->isindexbuffer = isindexbuffer;
2521 buffer->isdynamic = isdynamic;
2522 buffer->isindex16 = isindex16;
2523 strlcpy(buffer->name, name, sizeof(buffer->name));
2524 R_Mesh_UpdateMeshBuffer(buffer, data, size);
2528 void R_Mesh_UpdateMeshBuffer(r_meshbuffer_t *buffer, const void *data, size_t size)
2532 if (buffer->isindexbuffer)
2534 r_refdef.stats.indexbufferuploadcount++;
2535 r_refdef.stats.indexbufferuploadsize += size;
2539 r_refdef.stats.vertexbufferuploadcount++;
2540 r_refdef.stats.vertexbufferuploadsize += size;
2542 switch(vid.renderpath)
2544 case RENDERPATH_GL11:
2545 case RENDERPATH_GL13:
2546 case RENDERPATH_GL20:
2547 case RENDERPATH_CGGL:
2548 if (!buffer->bufferobject)
2549 qglGenBuffersARB(1, (GLuint *)&buffer->bufferobject);
2550 if (buffer->isindexbuffer)
2551 GL_BindEBO(buffer->bufferobject);
2553 GL_BindVBO(buffer->bufferobject);
2554 qglBufferDataARB(buffer->isindexbuffer ? GL_ELEMENT_ARRAY_BUFFER_ARB : GL_ARRAY_BUFFER_ARB, size, data, buffer->isdynamic ? GL_STREAM_DRAW_ARB : GL_STATIC_DRAW_ARB);
2556 case RENDERPATH_D3D9:
2560 void *datapointer = NULL;
2561 if (buffer->isindexbuffer)
2563 IDirect3DIndexBuffer9 *d3d9indexbuffer = (IDirect3DIndexBuffer9 *)buffer->devicebuffer;
2564 if (size != buffer->size || !buffer->devicebuffer)
2566 if (buffer->devicebuffer)
2567 IDirect3DIndexBuffer9_Release((IDirect3DIndexBuffer9*)buffer->devicebuffer);
2568 buffer->devicebuffer = NULL;
2569 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)))
2570 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);
2571 buffer->devicebuffer = (void *)d3d9indexbuffer;
2572 buffer->size = size;
2574 if (!FAILED(IDirect3DIndexBuffer9_Lock(d3d9indexbuffer, 0, 0, &datapointer, buffer->isdynamic ? D3DLOCK_DISCARD : 0)))
2577 memcpy(datapointer, data, size);
2579 memset(datapointer, 0, size);
2580 IDirect3DIndexBuffer9_Unlock(d3d9indexbuffer);
2585 IDirect3DVertexBuffer9 *d3d9vertexbuffer = (IDirect3DVertexBuffer9 *)buffer->devicebuffer;
2586 if (size != buffer->size || !buffer->devicebuffer)
2588 if (buffer->devicebuffer)
2589 IDirect3DVertexBuffer9_Release((IDirect3DVertexBuffer9*)buffer->devicebuffer);
2590 buffer->devicebuffer = NULL;
2591 if (FAILED(result = IDirect3DDevice9_CreateVertexBuffer(vid_d3d9dev, size, buffer->isdynamic ? D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC : 0, 0, buffer->isdynamic ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED, &d3d9vertexbuffer, NULL)))
2592 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);
2593 buffer->devicebuffer = (void *)d3d9vertexbuffer;
2594 buffer->size = size;
2596 if (!FAILED(IDirect3DVertexBuffer9_Lock(d3d9vertexbuffer, 0, 0, &datapointer, buffer->isdynamic ? D3DLOCK_DISCARD : 0)))
2599 memcpy(datapointer, data, size);
2601 memset(datapointer, 0, size);
2602 IDirect3DVertexBuffer9_Unlock(d3d9vertexbuffer);
2608 case RENDERPATH_D3D10:
2609 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2611 case RENDERPATH_D3D11:
2612 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2617 void R_Mesh_DestroyMeshBuffer(r_meshbuffer_t *buffer)
2621 switch(vid.renderpath)
2623 case RENDERPATH_GL11:
2624 case RENDERPATH_GL13:
2625 case RENDERPATH_GL20:
2626 case RENDERPATH_CGGL:
2627 qglDeleteBuffersARB(1, (GLuint *)&buffer->bufferobject);
2629 case RENDERPATH_D3D9:
2631 if (buffer->devicebuffer)
2633 if (buffer->isindexbuffer)
2634 IDirect3DIndexBuffer9_Release((IDirect3DIndexBuffer9 *)buffer->devicebuffer);
2636 IDirect3DVertexBuffer9_Release((IDirect3DVertexBuffer9 *)buffer->devicebuffer);
2640 case RENDERPATH_D3D10:
2641 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2643 case RENDERPATH_D3D11:
2644 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2647 Mem_ExpandableArray_FreeRecord(&gl_state.meshbufferarray, (void *)buffer);
2650 void GL_Mesh_ListVBOs(qboolean printeach)
2653 size_t ebocount = 0, ebomemory = 0;
2654 size_t vbocount = 0, vbomemory = 0;
2655 r_meshbuffer_t *buffer;
2656 endindex = Mem_ExpandableArray_IndexRange(&gl_state.meshbufferarray);
2657 for (i = 0;i < endindex;i++)
2659 buffer = (r_meshbuffer_t *) Mem_ExpandableArray_RecordAtIndex(&gl_state.meshbufferarray, i);
2662 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)");}
2663 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)");}
2665 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);
2670 void R_Mesh_VertexPointer(int components, int gltype, size_t stride, const void *pointer, const r_meshbuffer_t *vertexbuffer, size_t bufferoffset)
2672 int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
2673 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)
2675 gl_state.pointer_vertex_components = components;
2676 gl_state.pointer_vertex_gltype = gltype;
2677 gl_state.pointer_vertex_stride = stride;
2678 gl_state.pointer_vertex_pointer = pointer;
2679 gl_state.pointer_vertex_vertexbuffer = vertexbuffer;
2680 gl_state.pointer_vertex_offset = bufferoffset;
2682 GL_BindVBO(bufferobject);
2683 qglVertexPointer(components, gltype, stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
2687 void R_Mesh_ColorPointer(int components, int gltype, size_t stride, const void *pointer, const r_meshbuffer_t *vertexbuffer, size_t bufferoffset)
2689 // note: vertexbuffer may be non-NULL even if pointer is NULL, so check
2690 // the pointer only.
2693 int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
2694 // caller wants color array enabled
2695 if (!gl_state.pointer_color_enabled)
2697 gl_state.pointer_color_enabled = true;
2699 qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
2701 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)
2703 gl_state.pointer_color_components = components;
2704 gl_state.pointer_color_gltype = gltype;
2705 gl_state.pointer_color_stride = stride;
2706 gl_state.pointer_color_pointer = pointer;
2707 gl_state.pointer_color_vertexbuffer = vertexbuffer;
2708 gl_state.pointer_color_offset = bufferoffset;
2710 GL_BindVBO(bufferobject);
2711 qglColorPointer(components, gltype, stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
2716 // caller wants color array disabled
2717 if (gl_state.pointer_color_enabled)
2719 gl_state.pointer_color_enabled = false;
2721 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
2722 // when color array is on the glColor gets trashed, set it again
2723 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);CHECKGLERROR
2728 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)
2730 gltextureunit_t *unit = gl_state.units + unitnum;
2731 // update array settings
2733 // note: there is no need to check bufferobject here because all cases
2734 // that involve a valid bufferobject also supply a texcoord array
2737 int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
2738 // texture array unit is enabled, enable the array
2739 if (!unit->arrayenabled)
2741 unit->arrayenabled = true;
2742 GL_ClientActiveTexture(unitnum);
2743 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
2746 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)
2748 unit->pointer_texcoord_components = components;
2749 unit->pointer_texcoord_gltype = gltype;
2750 unit->pointer_texcoord_stride = stride;
2751 unit->pointer_texcoord_pointer = pointer;
2752 unit->pointer_texcoord_vertexbuffer = vertexbuffer;
2753 unit->pointer_texcoord_offset = bufferoffset;
2754 GL_ClientActiveTexture(unitnum);
2755 GL_BindVBO(bufferobject);
2756 qglTexCoordPointer(components, gltype, stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
2761 // texture array unit is disabled, disable the array
2762 if (unit->arrayenabled)
2764 unit->arrayenabled = false;
2765 GL_ClientActiveTexture(unitnum);
2766 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
2771 int R_Mesh_TexBound(unsigned int unitnum, int id)
2773 gltextureunit_t *unit = gl_state.units + unitnum;
2774 if (unitnum >= vid.teximageunits)
2776 if (id == GL_TEXTURE_2D)
2778 if (id == GL_TEXTURE_3D)
2780 if (id == GL_TEXTURE_CUBE_MAP_ARB)
2781 return unit->tcubemap;
2782 if (id == GL_TEXTURE_RECTANGLE_ARB)
2783 return unit->trectangle;
2787 void R_Mesh_CopyToTexture(rtexture_t *tex, int tx, int ty, int sx, int sy, int width, int height)
2789 switch(vid.renderpath)
2791 case RENDERPATH_GL11:
2792 case RENDERPATH_GL13:
2793 case RENDERPATH_GL20:
2794 case RENDERPATH_CGGL:
2795 R_Mesh_TexBind(0, tex);
2796 GL_ActiveTexture(0);CHECKGLERROR
2797 qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, tx, ty, sx, sy, width, height);CHECKGLERROR
2799 case RENDERPATH_D3D9:
2802 IDirect3DSurface9 *currentsurface = NULL;
2803 IDirect3DSurface9 *texturesurface = NULL;
2806 sourcerect.left = sx;
2807 sourcerect.top = sy;
2808 sourcerect.right = sx + width;
2809 sourcerect.bottom = sy + height;
2812 destrect.right = tx + width;
2813 destrect.bottom = ty + height;
2814 if (!FAILED(IDirect3DTexture9_GetSurfaceLevel(((IDirect3DTexture9 *)tex->d3dtexture), 0, &texturesurface)))
2816 if (!FAILED(IDirect3DDevice9_GetRenderTarget(vid_d3d9dev, 0, ¤tsurface)))
2818 IDirect3DDevice9_StretchRect(vid_d3d9dev, currentsurface, &sourcerect, texturesurface, &destrect, D3DTEXF_NONE);
2819 IDirect3DSurface9_Release(currentsurface);
2821 IDirect3DSurface9_Release(texturesurface);
2826 case RENDERPATH_D3D10:
2827 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2829 case RENDERPATH_D3D11:
2830 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2836 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};
2839 void R_Mesh_TexBind(unsigned int unitnum, rtexture_t *tex)
2841 gltextureunit_t *unit = gl_state.units + unitnum;
2842 int tex2d, tex3d, texcubemap, texnum;
2843 if (unitnum >= vid.teximageunits)
2845 // if (unit->texture == tex)
2847 switch(vid.renderpath)
2849 case RENDERPATH_GL20:
2850 case RENDERPATH_CGGL:
2853 tex = r_texture_white;
2854 // not initialized enough yet...
2858 unit->texture = tex;
2859 texnum = R_GetTexture(tex);
2860 switch(tex->gltexturetypeenum)
2862 case GL_TEXTURE_2D: if (unit->t2d != texnum) {GL_ActiveTexture(unitnum);unit->t2d = texnum;qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR}break;
2863 case GL_TEXTURE_3D: if (unit->t3d != texnum) {GL_ActiveTexture(unitnum);unit->t3d = texnum;qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR}break;
2864 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;
2865 case GL_TEXTURE_RECTANGLE_ARB: if (unit->trectangle != texnum) {GL_ActiveTexture(unitnum);unit->trectangle = texnum;qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR}break;
2868 case RENDERPATH_GL13:
2869 case RENDERPATH_GL11:
2870 unit->texture = tex;
2876 texnum = R_GetTexture(tex);
2877 switch(tex->gltexturetypeenum)
2885 case GL_TEXTURE_CUBE_MAP_ARB:
2886 texcubemap = texnum;
2890 // update 2d texture binding
2891 if (unit->t2d != tex2d)
2893 GL_ActiveTexture(unitnum);
2898 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
2905 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
2909 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
2911 // update 3d texture binding
2912 if (unit->t3d != tex3d)
2914 GL_ActiveTexture(unitnum);
2919 qglEnable(GL_TEXTURE_3D);CHECKGLERROR
2926 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
2930 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
2932 // update cubemap texture binding
2933 if (unit->tcubemap != texcubemap)
2935 GL_ActiveTexture(unitnum);
2938 if (unit->tcubemap == 0)
2940 qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
2947 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
2950 unit->tcubemap = texcubemap;
2951 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
2954 case RENDERPATH_D3D9:
2957 extern cvar_t gl_texture_anisotropy;
2960 tex = r_texture_white;
2961 // not initialized enough yet...
2965 if (unit->texture == tex)
2967 unit->texture = tex;
2968 // upload texture if needed
2970 R_RealGetTexture(tex);
2971 IDirect3DDevice9_SetTexture(vid_d3d9dev, unitnum, (IDirect3DBaseTexture9*)tex->d3dtexture);
2972 //IDirect3DDevice9_SetRenderState(vid_d3d9dev, d3drswrap[unitnum], (tex->flags & TEXF_CLAMP) ? (D3DWRAPCOORD_0 | D3DWRAPCOORD_1 | D3DWRAPCOORD_2) : 0);
2973 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_ADDRESSU, tex->d3daddressu);
2974 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_ADDRESSV, tex->d3daddressv);
2975 if (tex->d3daddressw)
2976 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_ADDRESSW, tex->d3daddressw);
2977 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MAGFILTER, tex->d3dmagfilter);
2978 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MINFILTER, tex->d3dminfilter);
2979 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MIPFILTER, tex->d3dmipfilter);
2980 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MIPMAPLODBIAS, tex->d3dmipmaplodbias);
2981 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MAXMIPLEVEL, tex->d3dmaxmiplevelfilter);
2982 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MAXANISOTROPY, gl_texture_anisotropy.integer);
2986 case RENDERPATH_D3D10:
2987 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2989 case RENDERPATH_D3D11:
2990 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2995 void R_Mesh_TexMatrix(unsigned int unitnum, const matrix4x4_t *matrix)
2997 gltextureunit_t *unit = gl_state.units + unitnum;
2998 switch(vid.renderpath)
3000 case RENDERPATH_GL11:
3001 case RENDERPATH_GL13:
3002 case RENDERPATH_GL20:
3003 case RENDERPATH_CGGL:
3004 if (matrix && matrix->m[3][3])
3006 // texmatrix specified, check if it is different
3007 if (!unit->texmatrixenabled || memcmp(&unit->matrix, matrix, sizeof(matrix4x4_t)))
3010 unit->texmatrixenabled = true;
3011 unit->matrix = *matrix;
3013 Matrix4x4_ToArrayFloatGL(&unit->matrix, glmatrix);
3014 GL_ActiveTexture(unitnum);
3015 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
3016 qglLoadMatrixf(glmatrix);CHECKGLERROR
3017 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
3022 // no texmatrix specified, revert to identity
3023 if (unit->texmatrixenabled)
3025 unit->texmatrixenabled = false;
3026 unit->matrix = identitymatrix;
3028 GL_ActiveTexture(unitnum);
3029 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
3030 qglLoadIdentity();CHECKGLERROR
3031 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
3035 case RENDERPATH_D3D9:
3036 case RENDERPATH_D3D10:
3037 case RENDERPATH_D3D11:
3042 void R_Mesh_TexCombine(unsigned int unitnum, int combinergb, int combinealpha, int rgbscale, int alphascale)
3044 gltextureunit_t *unit = gl_state.units + unitnum;
3046 switch(vid.renderpath)
3048 case RENDERPATH_GL20:
3049 case RENDERPATH_CGGL:
3052 case RENDERPATH_GL13:
3053 // GL_ARB_texture_env_combine
3055 combinergb = GL_MODULATE;
3057 combinealpha = GL_MODULATE;
3062 if (combinergb != combinealpha || rgbscale != 1 || alphascale != 1)
3064 if (combinergb == GL_DECAL)
3065 combinergb = GL_INTERPOLATE_ARB;
3066 if (unit->combine != GL_COMBINE_ARB)
3068 unit->combine = GL_COMBINE_ARB;
3069 GL_ActiveTexture(unitnum);
3070 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);CHECKGLERROR
3071 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_TEXTURE);CHECKGLERROR // for GL_INTERPOLATE_ARB mode
3073 if (unit->combinergb != combinergb)
3075 unit->combinergb = combinergb;
3076 GL_ActiveTexture(unitnum);
3077 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
3079 if (unit->combinealpha != combinealpha)
3081 unit->combinealpha = combinealpha;
3082 GL_ActiveTexture(unitnum);
3083 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
3085 if (unit->rgbscale != rgbscale)
3087 unit->rgbscale = rgbscale;
3088 GL_ActiveTexture(unitnum);
3089 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, unit->rgbscale);CHECKGLERROR
3091 if (unit->alphascale != alphascale)
3093 unit->alphascale = alphascale;
3094 GL_ActiveTexture(unitnum);
3095 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, unit->alphascale);CHECKGLERROR
3100 if (unit->combine != combinergb)
3102 unit->combine = combinergb;
3103 GL_ActiveTexture(unitnum);
3104 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combine);CHECKGLERROR
3108 case RENDERPATH_GL11:
3111 combinergb = GL_MODULATE;
3112 if (unit->combine != combinergb)
3114 unit->combine = combinergb;
3115 GL_ActiveTexture(unitnum);
3116 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combine);CHECKGLERROR
3119 case RENDERPATH_D3D9:
3120 case RENDERPATH_D3D10:
3121 case RENDERPATH_D3D11:
3126 void R_Mesh_ResetTextureState(void)
3128 unsigned int unitnum;
3133 switch(vid.renderpath)
3135 case RENDERPATH_GL20:
3136 case RENDERPATH_CGGL:
3137 for (unitnum = 0;unitnum < vid.teximageunits;unitnum++)
3139 gltextureunit_t *unit = gl_state.units + unitnum;
3143 GL_ActiveTexture(unitnum);
3144 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
3149 GL_ActiveTexture(unitnum);
3150 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
3155 GL_ActiveTexture(unitnum);
3156 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
3158 if (unit->trectangle)
3160 unit->trectangle = 0;
3161 GL_ActiveTexture(unitnum);
3162 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
3165 for (unitnum = 0;unitnum < vid.texarrayunits;unitnum++)
3167 gltextureunit_t *unit = gl_state.units + unitnum;
3168 if (unit->arrayenabled)
3170 unit->arrayenabled = false;
3171 GL_ClientActiveTexture(unitnum);
3172 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
3175 for (unitnum = 0;unitnum < vid.texunits;unitnum++)
3177 gltextureunit_t *unit = gl_state.units + unitnum;
3178 if (unit->texmatrixenabled)
3180 unit->texmatrixenabled = false;
3181 unit->matrix = identitymatrix;
3183 GL_ActiveTexture(unitnum);
3184 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
3185 qglLoadIdentity();CHECKGLERROR
3186 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
3190 case RENDERPATH_GL13:
3191 case RENDERPATH_GL11:
3192 for (unitnum = 0;unitnum < vid.texunits;unitnum++)
3194 gltextureunit_t *unit = gl_state.units + unitnum;
3198 GL_ActiveTexture(unitnum);
3199 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
3200 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
3205 GL_ActiveTexture(unitnum);
3206 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
3207 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
3212 GL_ActiveTexture(unitnum);
3213 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
3214 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
3216 if (unit->trectangle)
3218 unit->trectangle = 0;
3219 GL_ActiveTexture(unitnum);
3220 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
3221 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
3223 if (unit->arrayenabled)
3225 unit->arrayenabled = false;
3226 GL_ClientActiveTexture(unitnum);
3227 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
3229 if (unit->texmatrixenabled)
3231 unit->texmatrixenabled = false;
3232 unit->matrix = identitymatrix;
3234 GL_ActiveTexture(unitnum);
3235 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
3236 qglLoadIdentity();CHECKGLERROR
3237 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
3239 if (unit->combine != GL_MODULATE)
3241 unit->combine = GL_MODULATE;
3242 GL_ActiveTexture(unitnum);
3243 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combine);CHECKGLERROR
3247 case RENDERPATH_D3D9:
3248 case RENDERPATH_D3D10:
3249 case RENDERPATH_D3D11:
3257 //#define r_vertexposition_d3d9fvf (D3DFVF_XYZ)
3258 //#define r_vertexgeneric_d3d9fvf (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)
3259 //#define r_vertexmesh_d3d9fvf (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX5 | D3DFVF_TEXCOORDSIZE1(3) | D3DFVF_TEXCOORDSIZE2(3) | D3DFVF_TEXCOORDSIZE3(3))
3261 D3DVERTEXELEMENT9 r_vertexposition_d3d9elements[] =
3263 {0, (int)((size_t)&((r_vertexposition_t *)0)->vertex3f), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
3267 D3DVERTEXELEMENT9 r_vertexgeneric_d3d9elements[] =
3269 {0, (int)((size_t)&((r_vertexgeneric_t *)0)->vertex3f ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
3270 {0, (int)((size_t)&((r_vertexgeneric_t *)0)->color4ub ), D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
3271 {0, (int)((size_t)&((r_vertexgeneric_t *)0)->texcoord2f), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
3275 D3DVERTEXELEMENT9 r_vertexmesh_d3d9elements[] =
3277 {0, (int)((size_t)&((r_vertexmesh_t *)0)->vertex3f ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
3278 {0, (int)((size_t)&((r_vertexmesh_t *)0)->color4ub ), D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
3279 {0, (int)((size_t)&((r_vertexmesh_t *)0)->texcoordtexture2f ), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
3280 {0, (int)((size_t)&((r_vertexmesh_t *)0)->svector3f ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1},
3281 {0, (int)((size_t)&((r_vertexmesh_t *)0)->tvector3f ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 2},
3282 {0, (int)((size_t)&((r_vertexmesh_t *)0)->normal3f ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 3},
3283 {0, (int)((size_t)&((r_vertexmesh_t *)0)->texcoordlightmap2f), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 4},
3287 IDirect3DVertexDeclaration9 *r_vertexposition_d3d9decl;
3288 IDirect3DVertexDeclaration9 *r_vertexgeneric_d3d9decl;
3289 IDirect3DVertexDeclaration9 *r_vertexmesh_d3d9decl;
3292 static void R_Mesh_InitVertexDeclarations(void)
3295 r_vertexposition_d3d9decl = NULL;
3296 r_vertexgeneric_d3d9decl = NULL;
3297 r_vertexmesh_d3d9decl = NULL;
3298 switch(vid.renderpath)
3300 case RENDERPATH_D3D9:
3301 IDirect3DDevice9_CreateVertexDeclaration(vid_d3d9dev, r_vertexposition_d3d9elements, &r_vertexposition_d3d9decl);
3302 IDirect3DDevice9_CreateVertexDeclaration(vid_d3d9dev, r_vertexgeneric_d3d9elements, &r_vertexgeneric_d3d9decl);
3303 IDirect3DDevice9_CreateVertexDeclaration(vid_d3d9dev, r_vertexmesh_d3d9elements, &r_vertexmesh_d3d9decl);
3305 case RENDERPATH_D3D10:
3306 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3308 case RENDERPATH_D3D11:
3309 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3315 static void R_Mesh_DestroyVertexDeclarations(void)
3318 if (r_vertexposition_d3d9decl)
3319 IDirect3DVertexDeclaration9_Release(r_vertexposition_d3d9decl);
3320 r_vertexposition_d3d9decl = NULL;
3321 if (r_vertexgeneric_d3d9decl)
3322 IDirect3DVertexDeclaration9_Release(r_vertexgeneric_d3d9decl);
3323 r_vertexgeneric_d3d9decl = NULL;
3324 if (r_vertexmesh_d3d9decl)
3325 IDirect3DVertexDeclaration9_Release(r_vertexmesh_d3d9decl);
3326 r_vertexmesh_d3d9decl = NULL;
3330 r_vertexposition_t *R_Mesh_PrepareVertices_Position_Lock(int numvertices)
3333 size = sizeof(r_vertexposition_t) * numvertices;
3334 if (gl_state.preparevertices_tempdatamaxsize < size)
3336 gl_state.preparevertices_tempdatamaxsize = size;
3337 gl_state.preparevertices_tempdata = Mem_Realloc(r_main_mempool, gl_state.preparevertices_tempdata, gl_state.preparevertices_tempdatamaxsize);
3339 gl_state.preparevertices_vertexposition = (r_vertexposition_t *)gl_state.preparevertices_tempdata;
3340 gl_state.preparevertices_numvertices = numvertices;
3341 return gl_state.preparevertices_vertexposition;
3344 qboolean R_Mesh_PrepareVertices_Position_Unlock(void)
3346 R_Mesh_PrepareVertices_Position(gl_state.preparevertices_numvertices, gl_state.preparevertices_vertexposition, NULL);
3347 gl_state.preparevertices_vertexposition = NULL;
3348 gl_state.preparevertices_numvertices = 0;
3352 void R_Mesh_PrepareVertices_Position_Arrays(int numvertices, const float *vertex3f)
3355 r_vertexposition_t *vertex;
3356 switch(vid.renderpath)
3358 case RENDERPATH_GL20:
3359 case RENDERPATH_CGGL:
3360 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
3361 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
3362 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3363 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3364 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3365 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3366 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3368 case RENDERPATH_GL13:
3369 case RENDERPATH_GL11:
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 if (vid.texunits >= 2)
3374 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3375 if (vid.texunits >= 3)
3376 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3378 case RENDERPATH_D3D9:
3379 case RENDERPATH_D3D10:
3380 case RENDERPATH_D3D11:
3384 // no quick path for this case, convert to vertex structs
3385 vertex = R_Mesh_PrepareVertices_Position_Lock(numvertices);
3386 for (i = 0;i < numvertices;i++)
3387 VectorCopy(vertex3f + 3*i, vertex[i].vertex3f);
3388 R_Mesh_PrepareVertices_Position_Unlock();
3389 R_Mesh_PrepareVertices_Position(numvertices, vertex, NULL);
3392 void R_Mesh_PrepareVertices_Position(int numvertices, const r_vertexposition_t *vertex, const r_meshbuffer_t *vertexbuffer)
3394 // upload temporary vertexbuffer for this rendering
3395 if (!gl_state.usevbo_staticvertex)
3396 vertexbuffer = NULL;
3397 if (!vertexbuffer && gl_state.usevbo_dynamicvertex)
3399 if (gl_state.preparevertices_dynamicvertexbuffer)
3400 R_Mesh_UpdateMeshBuffer(gl_state.preparevertices_dynamicvertexbuffer, vertex, numvertices * sizeof(*vertex));
3402 gl_state.preparevertices_dynamicvertexbuffer = R_Mesh_CreateMeshBuffer(vertex, numvertices * sizeof(*vertex), "temporary", false, true, false);
3403 vertexbuffer = gl_state.preparevertices_dynamicvertexbuffer;
3407 switch(vid.renderpath)
3409 case RENDERPATH_GL20:
3410 case RENDERPATH_CGGL:
3411 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3412 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3413 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3414 case RENDERPATH_GL13:
3415 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3416 case RENDERPATH_GL11:
3417 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , vertexbuffer, (int)((unsigned char *)vertex->vertex3f - (unsigned char *)vertex));
3418 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
3419 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3421 case RENDERPATH_D3D9:
3423 IDirect3DDevice9_SetVertexDeclaration(vid_d3d9dev, r_vertexposition_d3d9decl);
3424 IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, (IDirect3DVertexBuffer9*)vertexbuffer->devicebuffer, 0, sizeof(*vertex));
3427 case RENDERPATH_D3D10:
3428 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3430 case RENDERPATH_D3D11:
3431 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3436 switch(vid.renderpath)
3438 case RENDERPATH_GL20:
3439 case RENDERPATH_CGGL:
3440 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3441 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3442 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3443 case RENDERPATH_GL13:
3444 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3445 case RENDERPATH_GL11:
3446 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , NULL, 0);
3447 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
3448 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3450 case RENDERPATH_D3D9:
3451 case RENDERPATH_D3D10:
3452 case RENDERPATH_D3D11:
3459 r_vertexgeneric_t *R_Mesh_PrepareVertices_Generic_Lock(int numvertices)
3462 size = sizeof(r_vertexgeneric_t) * numvertices;
3463 if (gl_state.preparevertices_tempdatamaxsize < size)
3465 gl_state.preparevertices_tempdatamaxsize = size;
3466 gl_state.preparevertices_tempdata = Mem_Realloc(r_main_mempool, gl_state.preparevertices_tempdata, gl_state.preparevertices_tempdatamaxsize);
3468 gl_state.preparevertices_vertexgeneric = (r_vertexgeneric_t *)gl_state.preparevertices_tempdata;
3469 gl_state.preparevertices_numvertices = numvertices;
3470 return gl_state.preparevertices_vertexgeneric;
3473 qboolean R_Mesh_PrepareVertices_Generic_Unlock(void)
3475 R_Mesh_PrepareVertices_Generic(gl_state.preparevertices_numvertices, gl_state.preparevertices_vertexgeneric, NULL);
3476 gl_state.preparevertices_vertexgeneric = NULL;
3477 gl_state.preparevertices_numvertices = 0;
3481 void R_Mesh_PrepareVertices_Generic_Arrays(int numvertices, const float *vertex3f, const float *color4f, const float *texcoord2f)
3484 r_vertexgeneric_t *vertex;
3485 switch(vid.renderpath)
3487 case RENDERPATH_GL20:
3488 case RENDERPATH_CGGL:
3489 if (gl_mesh_separatearrays.integer)
3491 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
3492 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
3493 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoord2f, NULL, 0);
3494 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3495 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3496 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3497 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3501 case RENDERPATH_GL13:
3502 case RENDERPATH_GL11:
3503 if (gl_mesh_separatearrays.integer)
3505 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
3506 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
3507 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoord2f, NULL, 0);
3508 if (vid.texunits >= 2)
3509 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3510 if (vid.texunits >= 3)
3511 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3515 case RENDERPATH_D3D9:
3516 case RENDERPATH_D3D10:
3517 case RENDERPATH_D3D11:
3521 // no quick path for this case, convert to vertex structs
3522 vertex = R_Mesh_PrepareVertices_Generic_Lock(numvertices);
3523 for (i = 0;i < numvertices;i++)
3524 VectorCopy(vertex3f + 3*i, vertex[i].vertex3f);
3527 for (i = 0;i < numvertices;i++)
3528 Vector4Scale(color4f + 4*i, 255.0f, vertex[i].color4ub);
3532 float tempcolor4f[4];
3533 unsigned char tempcolor4ub[4];
3534 Vector4Scale(gl_state.color4f, 255.0f, tempcolor4f);
3535 tempcolor4ub[0] = (unsigned char)bound(0.0f, tempcolor4f[0], 255.0f);
3536 tempcolor4ub[1] = (unsigned char)bound(0.0f, tempcolor4f[1], 255.0f);
3537 tempcolor4ub[2] = (unsigned char)bound(0.0f, tempcolor4f[2], 255.0f);
3538 tempcolor4ub[3] = (unsigned char)bound(0.0f, tempcolor4f[3], 255.0f);
3539 for (i = 0;i < numvertices;i++)
3540 Vector4Copy(tempcolor4ub, vertex[i].color4ub);
3543 for (i = 0;i < numvertices;i++)
3544 Vector2Copy(texcoord2f + 2*i, vertex[i].texcoord2f);
3545 R_Mesh_PrepareVertices_Generic_Unlock();
3546 R_Mesh_PrepareVertices_Generic(numvertices, vertex, NULL);
3549 void R_Mesh_PrepareVertices_Generic(int numvertices, const r_vertexgeneric_t *vertex, const r_meshbuffer_t *vertexbuffer)
3551 // upload temporary vertexbuffer for this rendering
3552 if (!gl_state.usevbo_staticvertex)
3553 vertexbuffer = NULL;
3554 if (!vertexbuffer && gl_state.usevbo_dynamicvertex)
3556 if (gl_state.preparevertices_dynamicvertexbuffer)
3557 R_Mesh_UpdateMeshBuffer(gl_state.preparevertices_dynamicvertexbuffer, vertex, numvertices * sizeof(*vertex));
3559 gl_state.preparevertices_dynamicvertexbuffer = R_Mesh_CreateMeshBuffer(vertex, numvertices * sizeof(*vertex), "temporary", false, true, false);
3560 vertexbuffer = gl_state.preparevertices_dynamicvertexbuffer;
3564 switch(vid.renderpath)
3566 case RENDERPATH_GL20:
3567 case RENDERPATH_CGGL:
3568 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3569 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3570 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3571 case RENDERPATH_GL13:
3572 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3573 case RENDERPATH_GL11:
3574 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , vertexbuffer, (int)((unsigned char *)vertex->vertex3f - (unsigned char *)vertex));
3575 R_Mesh_ColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(*vertex), vertex->color4ub , vertexbuffer, (int)((unsigned char *)vertex->color4ub - (unsigned char *)vertex));
3576 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoord2f , vertexbuffer, (int)((unsigned char *)vertex->texcoord2f - (unsigned char *)vertex));
3578 case RENDERPATH_D3D9:
3580 IDirect3DDevice9_SetVertexDeclaration(vid_d3d9dev, r_vertexgeneric_d3d9decl);
3581 IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, (IDirect3DVertexBuffer9*)vertexbuffer->devicebuffer, 0, sizeof(*vertex));
3584 case RENDERPATH_D3D10:
3585 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3587 case RENDERPATH_D3D11:
3588 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3593 switch(vid.renderpath)
3595 case RENDERPATH_GL20:
3596 case RENDERPATH_CGGL:
3597 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3598 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3599 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3600 case RENDERPATH_GL13:
3601 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3602 case RENDERPATH_GL11:
3603 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , NULL, 0);
3604 R_Mesh_ColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(*vertex), vertex->color4ub , NULL, 0);
3605 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoord2f , NULL, 0);
3607 case RENDERPATH_D3D9:
3608 case RENDERPATH_D3D10:
3609 case RENDERPATH_D3D11:
3616 r_vertexmesh_t *R_Mesh_PrepareVertices_Mesh_Lock(int numvertices)
3619 size = sizeof(r_vertexmesh_t) * numvertices;
3620 if (gl_state.preparevertices_tempdatamaxsize < size)
3622 gl_state.preparevertices_tempdatamaxsize = size;
3623 gl_state.preparevertices_tempdata = Mem_Realloc(r_main_mempool, gl_state.preparevertices_tempdata, gl_state.preparevertices_tempdatamaxsize);
3625 gl_state.preparevertices_vertexmesh = (r_vertexmesh_t *)gl_state.preparevertices_tempdata;
3626 gl_state.preparevertices_numvertices = numvertices;
3627 return gl_state.preparevertices_vertexmesh;
3630 qboolean R_Mesh_PrepareVertices_Mesh_Unlock(void)
3632 R_Mesh_PrepareVertices_Mesh(gl_state.preparevertices_numvertices, gl_state.preparevertices_vertexmesh, NULL);
3633 gl_state.preparevertices_vertexmesh = NULL;
3634 gl_state.preparevertices_numvertices = 0;
3638 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)
3641 r_vertexmesh_t *vertex;
3642 switch(vid.renderpath)
3644 case RENDERPATH_GL20:
3645 case RENDERPATH_CGGL:
3646 if (gl_mesh_separatearrays.integer)
3648 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
3649 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
3650 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoordtexture2f, NULL, 0);
3651 R_Mesh_TexCoordPointer(1, 3, GL_FLOAT, sizeof(float[3]), svector3f, NULL, 0);
3652 R_Mesh_TexCoordPointer(2, 3, GL_FLOAT, sizeof(float[3]), tvector3f, NULL, 0);
3653 R_Mesh_TexCoordPointer(3, 3, GL_FLOAT, sizeof(float[3]), normal3f, NULL, 0);
3654 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), texcoordlightmap2f, NULL, 0);
3658 case RENDERPATH_GL13:
3659 case RENDERPATH_GL11:
3660 if (gl_mesh_separatearrays.integer)
3662 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
3663 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
3664 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoordtexture2f, NULL, 0);
3665 if (vid.texunits >= 2)
3666 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), texcoordlightmap2f, NULL, 0);
3667 if (vid.texunits >= 3)
3668 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3672 case RENDERPATH_D3D9:
3673 case RENDERPATH_D3D10:
3674 case RENDERPATH_D3D11:
3678 vertex = R_Mesh_PrepareVertices_Mesh_Lock(numvertices);
3679 for (i = 0;i < numvertices;i++)
3680 VectorCopy(vertex3f + 3*i, vertex[i].vertex3f);
3682 for (i = 0;i < numvertices;i++)
3683 VectorCopy(svector3f + 3*i, vertex[i].svector3f);
3685 for (i = 0;i < numvertices;i++)
3686 VectorCopy(tvector3f + 3*i, vertex[i].tvector3f);
3688 for (i = 0;i < numvertices;i++)
3689 VectorCopy(normal3f + 3*i, vertex[i].normal3f);
3692 for (i = 0;i < numvertices;i++)
3693 Vector4Scale(color4f + 4*i, 255.0f, vertex[i].color4ub);
3697 float tempcolor4f[4];
3698 unsigned char tempcolor4ub[4];
3699 Vector4Scale(gl_state.color4f, 255.0f, tempcolor4f);
3700 tempcolor4ub[0] = (unsigned char)bound(0.0f, tempcolor4f[0], 255.0f);
3701 tempcolor4ub[1] = (unsigned char)bound(0.0f, tempcolor4f[1], 255.0f);
3702 tempcolor4ub[2] = (unsigned char)bound(0.0f, tempcolor4f[2], 255.0f);
3703 tempcolor4ub[3] = (unsigned char)bound(0.0f, tempcolor4f[3], 255.0f);
3704 for (i = 0;i < numvertices;i++)
3705 Vector4Copy(tempcolor4ub, vertex[i].color4ub);
3707 if (texcoordtexture2f)
3708 for (i = 0;i < numvertices;i++)
3709 Vector2Copy(texcoordtexture2f + 2*i, vertex[i].texcoordtexture2f);
3710 if (texcoordlightmap2f)
3711 for (i = 0;i < numvertices;i++)
3712 Vector2Copy(texcoordlightmap2f + 2*i, vertex[i].texcoordlightmap2f);
3713 R_Mesh_PrepareVertices_Mesh_Unlock();
3714 R_Mesh_PrepareVertices_Mesh(numvertices, vertex, NULL);
3717 void R_Mesh_PrepareVertices_Mesh(int numvertices, const r_vertexmesh_t *vertex, const r_meshbuffer_t *vertexbuffer)
3719 // upload temporary vertexbuffer for this rendering
3720 if (!gl_state.usevbo_staticvertex)
3721 vertexbuffer = NULL;
3722 if (!vertexbuffer && gl_state.usevbo_dynamicvertex)
3724 if (gl_state.preparevertices_dynamicvertexbuffer)
3725 R_Mesh_UpdateMeshBuffer(gl_state.preparevertices_dynamicvertexbuffer, vertex, numvertices * sizeof(*vertex));
3727 gl_state.preparevertices_dynamicvertexbuffer = R_Mesh_CreateMeshBuffer(vertex, numvertices * sizeof(*vertex), "temporary", false, true, false);
3728 vertexbuffer = gl_state.preparevertices_dynamicvertexbuffer;
3732 switch(vid.renderpath)
3734 case RENDERPATH_GL20:
3735 case RENDERPATH_CGGL:
3736 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , vertexbuffer, (int)((unsigned char *)vertex->vertex3f - (unsigned char *)vertex));
3737 R_Mesh_ColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(*vertex), vertex->color4ub , vertexbuffer, (int)((unsigned char *)vertex->color4ub - (unsigned char *)vertex));
3738 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordtexture2f , vertexbuffer, (int)((unsigned char *)vertex->texcoordtexture2f - (unsigned char *)vertex));
3739 R_Mesh_TexCoordPointer(1, 3, GL_FLOAT , sizeof(*vertex), vertex->svector3f , vertexbuffer, (int)((unsigned char *)vertex->svector3f - (unsigned char *)vertex));
3740 R_Mesh_TexCoordPointer(2, 3, GL_FLOAT , sizeof(*vertex), vertex->tvector3f , vertexbuffer, (int)((unsigned char *)vertex->tvector3f - (unsigned char *)vertex));
3741 R_Mesh_TexCoordPointer(3, 4, GL_FLOAT , sizeof(*vertex), vertex->normal3f , vertexbuffer, (int)((unsigned char *)vertex->normal3f - (unsigned char *)vertex));
3742 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordlightmap2f, vertexbuffer, (int)((unsigned char *)vertex->texcoordlightmap2f - (unsigned char *)vertex));
3744 case RENDERPATH_GL13:
3745 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordlightmap2f, vertexbuffer, (int)((unsigned char *)vertex->texcoordlightmap2f - (unsigned char *)vertex));
3746 case RENDERPATH_GL11:
3747 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , vertexbuffer, (int)((unsigned char *)vertex->vertex3f - (unsigned char *)vertex));
3748 R_Mesh_ColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(*vertex), vertex->color4ub , vertexbuffer, (int)((unsigned char *)vertex->color4ub - (unsigned char *)vertex));
3749 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordtexture2f , vertexbuffer, (int)((unsigned char *)vertex->texcoordtexture2f - (unsigned char *)vertex));
3751 case RENDERPATH_D3D9:
3753 IDirect3DDevice9_SetVertexDeclaration(vid_d3d9dev, r_vertexmesh_d3d9decl);
3754 IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, (IDirect3DVertexBuffer9*)vertexbuffer->devicebuffer, 0, sizeof(*vertex));
3757 case RENDERPATH_D3D10:
3758 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3760 case RENDERPATH_D3D11:
3761 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3766 switch(vid.renderpath)
3768 case RENDERPATH_GL20:
3769 case RENDERPATH_CGGL:
3770 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , NULL, 0);
3771 R_Mesh_ColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(*vertex), vertex->color4ub , NULL, 0);
3772 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordtexture2f , NULL, 0);
3773 R_Mesh_TexCoordPointer(1, 3, GL_FLOAT , sizeof(*vertex), vertex->svector3f , NULL, 0);
3774 R_Mesh_TexCoordPointer(2, 3, GL_FLOAT , sizeof(*vertex), vertex->tvector3f , NULL, 0);
3775 R_Mesh_TexCoordPointer(3, 4, GL_FLOAT , sizeof(*vertex), vertex->normal3f , NULL, 0);
3776 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordlightmap2f, NULL, 0);
3778 case RENDERPATH_GL13:
3779 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordlightmap2f, NULL, 0);
3780 case RENDERPATH_GL11:
3781 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , NULL, 0);
3782 R_Mesh_ColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(*vertex), vertex->color4ub , NULL, 0);
3783 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordtexture2f , NULL, 0);
3785 case RENDERPATH_D3D9:
3786 case RENDERPATH_D3D10:
3787 case RENDERPATH_D3D11: