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[10] = -1/(zFar - zNear);
520 m[12] = - (right + left)/(right - left);
521 m[13] = - (top + bottom)/(top - bottom);
522 // m[14] = - (zFar + zNear)/(zFar - zNear);
523 m[14] = -zNear/(zFar-zNear);
525 v->screentodepth[0] = -farclip / (farclip - nearclip);
526 v->screentodepth[1] = farclip * nearclip / (farclip - nearclip);
528 Matrix4x4_Invert_Full(&v->viewmatrix, &v->cameramatrix);
531 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
533 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
539 Vector4Set(test1, (x1+x2)*0.5f, (y1+y2)*0.5f, 0.0f, 1.0f);
540 R_Viewport_TransformToScreen(v, test1, test2);
541 Con_Printf("%f %f %f -> %f %f %f\n", test1[0], test1[1], test1[2], test2[0], test2[1], test2[2]);
546 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)
548 matrix4x4_t tempmatrix, basematrix;
550 memset(v, 0, sizeof(*v));
552 if(v_flipped.integer)
553 frustumx = -frustumx;
555 v->type = R_VIEWPORTTYPE_PERSPECTIVE;
556 v->cameramatrix = *cameramatrix;
563 memset(m, 0, sizeof(m));
564 m[0] = 1.0 / frustumx;
565 m[5] = 1.0 / frustumy;
566 m[10] = -(farclip + nearclip) / (farclip - nearclip);
568 m[14] = -2 * nearclip * farclip / (farclip - nearclip);
569 v->screentodepth[0] = -farclip / (farclip - nearclip);
570 v->screentodepth[1] = farclip * nearclip / (farclip - nearclip);
572 Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
573 Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
574 Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
575 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
578 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
580 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
583 void R_Viewport_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)
585 matrix4x4_t tempmatrix, basematrix;
586 const float nudge = 1.0 - 1.0 / (1<<23);
588 memset(v, 0, sizeof(*v));
590 if(v_flipped.integer)
591 frustumx = -frustumx;
593 v->type = R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP;
594 v->cameramatrix = *cameramatrix;
601 memset(m, 0, sizeof(m));
602 m[ 0] = 1.0 / frustumx;
603 m[ 5] = 1.0 / frustumy;
606 m[14] = -2 * nearclip * nudge;
607 v->screentodepth[0] = (m[10] + 1) * 0.5 - 1;
608 v->screentodepth[1] = m[14] * -0.5;
610 Matrix4x4_Invert_Full(&tempmatrix, &v->cameramatrix);
611 Matrix4x4_CreateRotate(&basematrix, -90, 1, 0, 0);
612 Matrix4x4_ConcatRotate(&basematrix, 90, 0, 0, 1);
613 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
616 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
618 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
621 float cubeviewmatrix[6][16] =
623 // standard cubemap projections
661 float rectviewmatrix[6][16] =
663 // sign-preserving cubemap projections
702 void R_Viewport_InitCubeSideView(r_viewport_t *v, const matrix4x4_t *cameramatrix, int side, int size, float nearclip, float farclip, const float *nearplane)
704 matrix4x4_t tempmatrix, basematrix;
706 memset(v, 0, sizeof(*v));
707 v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
708 v->cameramatrix = *cameramatrix;
712 memset(m, 0, sizeof(m));
714 m[10] = -(farclip + nearclip) / (farclip - nearclip);
716 m[14] = -2 * nearclip * farclip / (farclip - nearclip);
718 Matrix4x4_FromArrayFloatGL(&basematrix, cubeviewmatrix[side]);
719 Matrix4x4_Invert_Simple(&tempmatrix, &v->cameramatrix);
720 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
723 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
725 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
728 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)
730 matrix4x4_t tempmatrix, basematrix;
732 memset(v, 0, sizeof(*v));
733 v->type = R_VIEWPORTTYPE_PERSPECTIVECUBESIDE;
734 v->cameramatrix = *cameramatrix;
735 v->x = (side & 1) * size;
736 v->y = (side >> 1) * size;
740 memset(m, 0, sizeof(m));
741 m[0] = m[5] = 1.0f * ((float)size - border) / size;
742 m[10] = -(farclip + nearclip) / (farclip - nearclip);
744 m[14] = -2 * nearclip * farclip / (farclip - nearclip);
746 Matrix4x4_FromArrayFloatGL(&basematrix, rectviewmatrix[side]);
747 Matrix4x4_Invert_Simple(&tempmatrix, &v->cameramatrix);
748 Matrix4x4_Concat(&v->viewmatrix, &basematrix, &tempmatrix);
750 switch(vid.renderpath)
752 case RENDERPATH_GL20:
753 case RENDERPATH_CGGL:
754 case RENDERPATH_GL13:
755 case RENDERPATH_GL11:
757 case RENDERPATH_D3D9:
760 case RENDERPATH_D3D10:
761 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
763 case RENDERPATH_D3D11:
764 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
769 R_Viewport_ApplyNearClipPlaneFloatGL(v, m, nearplane[0], nearplane[1], nearplane[2], nearplane[3]);
771 Matrix4x4_FromArrayFloatGL(&v->projectmatrix, m);
774 void R_SetViewport(const r_viewport_t *v)
779 // FIXME: v_flipped_state is evil, this probably breaks somewhere
780 GL_SetMirrorState(v_flipped.integer && (v->type == R_VIEWPORTTYPE_PERSPECTIVE || v->type == R_VIEWPORTTYPE_PERSPECTIVE_INFINITEFARCLIP));
782 // copy over the matrices to our state
783 gl_viewmatrix = v->viewmatrix;
784 gl_projectionmatrix = v->projectmatrix;
786 switch(vid.renderpath)
788 case RENDERPATH_GL20:
789 case RENDERPATH_CGGL:
791 // qglViewport(v->x, v->y, v->width, v->height);CHECKGLERROR
793 case RENDERPATH_GL13:
794 case RENDERPATH_GL11:
796 qglViewport(v->x, v->y, v->width, v->height);CHECKGLERROR
797 // Load the projection matrix into OpenGL
798 qglMatrixMode(GL_PROJECTION);CHECKGLERROR
799 Matrix4x4_ToArrayFloatGL(&gl_projectionmatrix, m);
800 qglLoadMatrixf(m);CHECKGLERROR
801 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
803 case RENDERPATH_D3D9:
806 D3DVIEWPORT9 d3dviewport;
807 d3dviewport.X = gl_viewport.x;
808 d3dviewport.Y = gl_viewport.y;
809 d3dviewport.Width = gl_viewport.width;
810 d3dviewport.Height = gl_viewport.height;
811 d3dviewport.MinZ = gl_state.depthrange[0];
812 d3dviewport.MaxZ = gl_state.depthrange[1];
813 IDirect3DDevice9_SetViewport(vid_d3d9dev, &d3dviewport);
817 case RENDERPATH_D3D10:
818 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
820 case RENDERPATH_D3D11:
821 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
825 // force an update of the derived matrices
826 gl_modelmatrixchanged = true;
827 R_EntityMatrix(&gl_modelmatrix);
830 void R_GetViewport(r_viewport_t *v)
835 static void GL_BindVBO(int bufferobject)
837 if (gl_state.vertexbufferobject != bufferobject)
839 gl_state.vertexbufferobject = bufferobject;
841 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, bufferobject);CHECKGLERROR
845 static void GL_BindEBO(int bufferobject)
847 if (gl_state.elementbufferobject != bufferobject)
849 gl_state.elementbufferobject = bufferobject;
851 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, bufferobject);CHECKGLERROR
855 int R_Mesh_CreateFramebufferObject(rtexture_t *depthtexture, rtexture_t *colortexture, rtexture_t *colortexture2, rtexture_t *colortexture3, rtexture_t *colortexture4)
858 switch(vid.renderpath)
860 case RENDERPATH_GL11:
861 case RENDERPATH_GL13:
862 case RENDERPATH_GL20:
863 case RENDERPATH_CGGL:
864 if (!vid.support.ext_framebuffer_object)
866 qglGenFramebuffersEXT(1, (GLuint*)&temp);CHECKGLERROR
867 R_Mesh_SetRenderTargets(temp, NULL, NULL, NULL, NULL, NULL);
868 if (depthtexture) qglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, depthtexture->gltexturetypeenum, R_GetTexture(depthtexture), 0);CHECKGLERROR
869 if (colortexture) qglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, colortexture->gltexturetypeenum, R_GetTexture(colortexture), 0);CHECKGLERROR
870 if (colortexture2) qglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, colortexture2->gltexturetypeenum, R_GetTexture(colortexture2), 0);CHECKGLERROR
871 if (colortexture3) qglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT2_EXT, colortexture3->gltexturetypeenum, R_GetTexture(colortexture3), 0);CHECKGLERROR
872 if (colortexture4) qglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT3_EXT, colortexture4->gltexturetypeenum, R_GetTexture(colortexture4), 0);CHECKGLERROR
874 case RENDERPATH_D3D9:
875 case RENDERPATH_D3D10:
876 case RENDERPATH_D3D11:
882 void R_Mesh_DestroyFramebufferObject(int fbo)
884 switch(vid.renderpath)
886 case RENDERPATH_GL11:
887 case RENDERPATH_GL13:
888 case RENDERPATH_GL20:
889 case RENDERPATH_CGGL:
891 qglDeleteFramebuffersEXT(1, (GLuint*)&fbo);
893 case RENDERPATH_D3D9:
894 case RENDERPATH_D3D10:
895 case RENDERPATH_D3D11:
900 void R_Mesh_ResetRenderTargets(void)
902 switch(vid.renderpath)
904 case RENDERPATH_GL11:
905 case RENDERPATH_GL13:
906 case RENDERPATH_GL20:
907 case RENDERPATH_CGGL:
908 if (gl_state.framebufferobject)
910 gl_state.framebufferobject = 0;
911 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, gl_state.framebufferobject);
914 case RENDERPATH_D3D9:
916 if (gl_state.framebufferobject)
919 gl_state.framebufferobject = 0;
920 IDirect3DDevice9_SetDepthStencilSurface(vid_d3d9dev, gl_state.d3drt_backbufferdepthsurface);
921 IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, 0, gl_state.d3drt_backbuffercolorsurface);
922 gl_state.d3drt_depthsurface = NULL;
923 for (i = 1;i < vid.maxdrawbuffers;i++)
925 if (gl_state.d3drt_colorsurfaces[i])
927 gl_state.d3drt_colorsurfaces[i] = NULL;
928 IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, i, NULL);
934 case RENDERPATH_D3D10:
935 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
937 case RENDERPATH_D3D11:
938 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
943 void R_Mesh_SetRenderTargets(int fbo, rtexture_t *depthtexture, rtexture_t *colortexture, rtexture_t *colortexture2, rtexture_t *colortexture3, rtexture_t *colortexture4)
947 rtexture_t *textures[5];
948 Vector4Set(textures, colortexture, colortexture2, colortexture3, colortexture4);
949 textures[4] = depthtexture;
950 // unbind any matching textures immediately, otherwise D3D will complain about a bound texture being used as a render target
951 for (j = 0;j < 5;j++)
953 for (i = 0;i < vid.teximageunits;i++)
954 if (gl_state.units[i].texture == textures[j])
955 R_Mesh_TexBind(i, NULL);
956 // set up framebuffer object or render targets for the active rendering API
957 switch(vid.renderpath)
959 case RENDERPATH_GL11:
960 case RENDERPATH_GL13:
961 case RENDERPATH_GL20:
962 case RENDERPATH_CGGL:
963 if (gl_state.framebufferobject != fbo)
965 gl_state.framebufferobject = fbo;
966 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, gl_state.framebufferobject);
969 case RENDERPATH_D3D9:
971 // set up the new render targets, a NULL depthtexture intentionally binds nothing
972 // TODO: optimize: keep surface pointer around in rtexture_t until texture is freed or lost
975 gl_state.framebufferobject = 1;
976 gl_state.d3drt_depthtexture = depthtexture;
977 if (gl_state.d3drt_depthtexture)
979 IDirect3DTexture9_GetSurfaceLevel((IDirect3DTexture9 *)gl_state.d3drt_depthtexture->d3dtexture, 0, &gl_state.d3drt_depthsurface);
980 IDirect3DDevice9_SetDepthStencilSurface(vid_d3d9dev, gl_state.d3drt_depthsurface);
981 IDirect3DSurface9_Release(gl_state.d3drt_depthsurface);
984 IDirect3DDevice9_SetDepthStencilSurface(vid_d3d9dev, NULL);
985 for (i = 0;i < vid.maxdrawbuffers;i++)
987 gl_state.d3drt_colortextures[i] = textures[i];
988 if (gl_state.d3drt_colortextures[i])
990 IDirect3DTexture9_GetSurfaceLevel((IDirect3DTexture9 *)gl_state.d3drt_colortextures[i]->d3dtexture, 0, &gl_state.d3drt_colorsurfaces[i]);
991 IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, i, gl_state.d3drt_colorsurfaces[i]);
992 IDirect3DSurface9_Release(gl_state.d3drt_colorsurfaces[i]);
995 IDirect3DDevice9_SetRenderTarget(vid_d3d9dev, i, NULL);
999 R_Mesh_ResetRenderTargets();
1002 case RENDERPATH_D3D10:
1003 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1005 case RENDERPATH_D3D11:
1006 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1012 static int d3dcmpforglfunc(int f)
1016 case GL_NEVER: return D3DCMP_NEVER;
1017 case GL_LESS: return D3DCMP_LESS;
1018 case GL_EQUAL: return D3DCMP_EQUAL;
1019 case GL_LEQUAL: return D3DCMP_LESSEQUAL;
1020 case GL_GREATER: return D3DCMP_GREATER;
1021 case GL_NOTEQUAL: return D3DCMP_NOTEQUAL;
1022 case GL_GEQUAL: return D3DCMP_GREATEREQUAL;
1023 case GL_ALWAYS: return D3DCMP_ALWAYS;
1024 default: Con_DPrintf("Unknown GL_DepthFunc\n");return D3DCMP_ALWAYS;
1028 static int d3dstencilopforglfunc(int f)
1032 case GL_KEEP: return D3DSTENCILOP_KEEP;
1033 case GL_INCR: return D3DSTENCILOP_INCR; // note: GL_INCR is clamped, D3DSTENCILOP_INCR wraps
1034 case GL_DECR: return D3DSTENCILOP_DECR; // note: GL_DECR is clamped, D3DSTENCILOP_DECR wraps
1035 default: Con_DPrintf("Unknown GL_StencilFunc\n");return D3DSTENCILOP_KEEP;
1041 static void GL_Backend_ResetState(void)
1044 gl_state.active = true;
1045 gl_state.depthtest = true;
1046 gl_state.alphatest = false;
1047 gl_state.alphafunc = GL_GEQUAL;
1048 gl_state.alphafuncvalue = 0.5f;
1049 gl_state.blendfunc1 = GL_ONE;
1050 gl_state.blendfunc2 = GL_ZERO;
1051 gl_state.blend = false;
1052 gl_state.depthmask = GL_TRUE;
1053 gl_state.colormask = 15;
1054 gl_state.color4f[0] = gl_state.color4f[1] = gl_state.color4f[2] = gl_state.color4f[3] = 1;
1055 gl_state.lockrange_first = 0;
1056 gl_state.lockrange_count = 0;
1057 gl_state.cullface = GL_NONE;
1058 gl_state.cullfaceenable = false;
1059 gl_state.polygonoffset[0] = 0;
1060 gl_state.polygonoffset[1] = 0;
1061 gl_state.framebufferobject = 0;
1062 gl_state.depthfunc = GL_LEQUAL;
1064 switch(vid.renderpath)
1066 case RENDERPATH_D3D9:
1069 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_COLORWRITEENABLE, gl_state.colormask);
1070 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ALPHATESTENABLE, gl_state.alphatest);
1071 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ALPHAFUNC, d3dcmpforglfunc(gl_state.alphafunc));
1072 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ALPHAREF, (int)bound(0, gl_state.alphafuncvalue * 256.0f, 255));
1073 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, D3DCULL_NONE);
1074 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZFUNC, d3dcmpforglfunc(gl_state.depthfunc));
1075 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZENABLE, gl_state.depthtest);
1076 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZWRITEENABLE, gl_state.depthmask);
1077 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_SLOPESCALEDEPTHBIAS, gl_state.polygonoffset[0]);
1078 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_DEPTHBIAS, gl_state.polygonoffset[1] * (1.0f / 16777216.0f));
1082 case RENDERPATH_D3D10:
1083 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1085 case RENDERPATH_D3D11:
1086 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1088 case RENDERPATH_GL20:
1089 case RENDERPATH_CGGL:
1092 qglColorMask(1, 1, 1, 1);CHECKGLERROR
1093 qglAlphaFunc(gl_state.alphafunc, gl_state.alphafuncvalue);CHECKGLERROR
1094 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
1095 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
1096 qglDisable(GL_BLEND);CHECKGLERROR
1097 qglCullFace(gl_state.cullface);CHECKGLERROR
1098 qglDisable(GL_CULL_FACE);CHECKGLERROR
1099 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
1100 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1101 qglDepthMask(gl_state.depthmask);CHECKGLERROR
1102 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
1104 if (vid.support.arb_vertex_buffer_object)
1106 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
1107 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
1110 if (vid.support.ext_framebuffer_object)
1112 qglBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
1113 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
1116 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), NULL);CHECKGLERROR
1117 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
1119 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL);CHECKGLERROR
1120 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1121 qglColor4f(1, 1, 1, 1);CHECKGLERROR
1123 if (vid.support.ext_framebuffer_object)
1124 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, gl_state.framebufferobject);
1126 gl_state.unit = MAX_TEXTUREUNITS;
1127 gl_state.clientunit = MAX_TEXTUREUNITS;
1128 for (i = 0;i < vid.teximageunits;i++)
1130 GL_ActiveTexture(i);
1131 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
1132 if (vid.support.ext_texture_3d)
1134 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
1136 if (vid.support.arb_texture_cube_map)
1138 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
1140 if (vid.support.arb_texture_rectangle)
1142 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);CHECKGLERROR
1146 for (i = 0;i < vid.texarrayunits;i++)
1148 GL_ClientActiveTexture(i);
1150 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
1151 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1155 case RENDERPATH_GL13:
1156 case RENDERPATH_GL11:
1159 qglColorMask(1, 1, 1, 1);CHECKGLERROR
1160 qglAlphaFunc(gl_state.alphafunc, gl_state.alphafuncvalue);CHECKGLERROR
1161 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
1162 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
1163 qglDisable(GL_BLEND);CHECKGLERROR
1164 qglCullFace(gl_state.cullface);CHECKGLERROR
1165 qglDisable(GL_CULL_FACE);CHECKGLERROR
1166 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
1167 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1168 qglDepthMask(gl_state.depthmask);CHECKGLERROR
1169 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
1171 if (vid.support.arb_vertex_buffer_object)
1173 qglBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
1174 qglBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
1177 if (vid.support.ext_framebuffer_object)
1179 qglBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
1180 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
1183 qglVertexPointer(3, GL_FLOAT, sizeof(float[3]), NULL);CHECKGLERROR
1184 qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
1186 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL);CHECKGLERROR
1187 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
1188 qglColor4f(1, 1, 1, 1);CHECKGLERROR
1190 if (vid.support.ext_framebuffer_object)
1191 qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, gl_state.framebufferobject);
1193 gl_state.unit = MAX_TEXTUREUNITS;
1194 gl_state.clientunit = MAX_TEXTUREUNITS;
1195 for (i = 0;i < vid.texunits;i++)
1197 GL_ActiveTexture(i);
1198 GL_ClientActiveTexture(i);
1199 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
1200 qglBindTexture(GL_TEXTURE_2D, 0);CHECKGLERROR
1201 if (vid.support.ext_texture_3d)
1203 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
1204 qglBindTexture(GL_TEXTURE_3D, 0);CHECKGLERROR
1206 if (vid.support.arb_texture_cube_map)
1208 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
1209 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, 0);CHECKGLERROR
1211 if (vid.support.arb_texture_rectangle)
1213 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
1214 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);CHECKGLERROR
1217 qglTexCoordPointer(2, GL_FLOAT, sizeof(float[2]), NULL);CHECKGLERROR
1218 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
1219 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
1220 qglLoadIdentity();CHECKGLERROR
1221 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
1222 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
1229 void GL_ActiveTexture(unsigned int num)
1231 if (gl_state.unit != num)
1233 gl_state.unit = num;
1234 switch(vid.renderpath)
1236 case RENDERPATH_GL11:
1237 case RENDERPATH_GL13:
1238 case RENDERPATH_GL20:
1239 case RENDERPATH_CGGL:
1240 if (qglActiveTexture)
1243 qglActiveTexture(GL_TEXTURE0_ARB + gl_state.unit);
1247 case RENDERPATH_D3D9:
1248 case RENDERPATH_D3D10:
1249 case RENDERPATH_D3D11:
1255 void GL_ClientActiveTexture(unsigned int num)
1257 if (gl_state.clientunit != num)
1259 gl_state.clientunit = num;
1260 switch(vid.renderpath)
1262 case RENDERPATH_GL11:
1263 case RENDERPATH_GL13:
1264 case RENDERPATH_GL20:
1265 case RENDERPATH_CGGL:
1266 if (qglActiveTexture)
1269 qglClientActiveTexture(GL_TEXTURE0_ARB + gl_state.clientunit);
1273 case RENDERPATH_D3D9:
1274 case RENDERPATH_D3D10:
1275 case RENDERPATH_D3D11:
1281 void GL_BlendFunc(int blendfunc1, int blendfunc2)
1283 if (gl_state.blendfunc1 != blendfunc1 || gl_state.blendfunc2 != blendfunc2)
1285 qboolean blendenable;
1286 gl_state.blendfunc1 = blendfunc1;
1287 gl_state.blendfunc2 = blendfunc2;
1288 blendenable = (gl_state.blendfunc1 != GL_ONE || gl_state.blendfunc2 != GL_ZERO);
1289 switch(vid.renderpath)
1291 case RENDERPATH_GL11:
1292 case RENDERPATH_GL13:
1293 case RENDERPATH_GL20:
1294 case RENDERPATH_CGGL:
1296 qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
1297 if (gl_state.blend != blendenable)
1299 gl_state.blend = blendenable;
1300 if (!gl_state.blend)
1302 qglDisable(GL_BLEND);CHECKGLERROR
1306 qglEnable(GL_BLEND);CHECKGLERROR
1310 case RENDERPATH_D3D9:
1315 D3DBLEND d3dblendfunc[2];
1316 glblendfunc[0] = gl_state.blendfunc1;
1317 glblendfunc[1] = gl_state.blendfunc2;
1318 for (i = 0;i < 2;i++)
1320 switch(glblendfunc[i])
1322 case GL_ZERO: d3dblendfunc[i] = D3DBLEND_ZERO;break;
1323 case GL_ONE: d3dblendfunc[i] = D3DBLEND_ONE;break;
1324 case GL_SRC_COLOR: d3dblendfunc[i] = D3DBLEND_SRCCOLOR;break;
1325 case GL_ONE_MINUS_SRC_COLOR: d3dblendfunc[i] = D3DBLEND_INVSRCCOLOR;break;
1326 case GL_SRC_ALPHA: d3dblendfunc[i] = D3DBLEND_SRCALPHA;break;
1327 case GL_ONE_MINUS_SRC_ALPHA: d3dblendfunc[i] = D3DBLEND_INVSRCALPHA;break;
1328 case GL_DST_ALPHA: d3dblendfunc[i] = D3DBLEND_DESTALPHA;break;
1329 case GL_ONE_MINUS_DST_ALPHA: d3dblendfunc[i] = D3DBLEND_INVDESTALPHA;break;
1330 case GL_DST_COLOR: d3dblendfunc[i] = D3DBLEND_DESTCOLOR;break;
1331 case GL_ONE_MINUS_DST_COLOR: d3dblendfunc[i] = D3DBLEND_INVDESTCOLOR;break;
1334 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_SRCBLEND, d3dblendfunc[0]);
1335 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_DESTBLEND, d3dblendfunc[1]);
1336 if (gl_state.blend != blendenable)
1338 gl_state.blend = blendenable;
1339 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ALPHABLENDENABLE, gl_state.blend);
1344 case RENDERPATH_D3D10:
1345 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1347 case RENDERPATH_D3D11:
1348 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1354 void GL_DepthMask(int state)
1356 if (gl_state.depthmask != state)
1358 gl_state.depthmask = state;
1359 switch(vid.renderpath)
1361 case RENDERPATH_GL11:
1362 case RENDERPATH_GL13:
1363 case RENDERPATH_GL20:
1364 case RENDERPATH_CGGL:
1366 qglDepthMask(gl_state.depthmask);CHECKGLERROR
1368 case RENDERPATH_D3D9:
1370 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZWRITEENABLE, gl_state.depthmask);
1373 case RENDERPATH_D3D10:
1374 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1376 case RENDERPATH_D3D11:
1377 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1383 void GL_DepthTest(int state)
1385 if (gl_state.depthtest != state)
1387 gl_state.depthtest = state;
1388 switch(vid.renderpath)
1390 case RENDERPATH_GL11:
1391 case RENDERPATH_GL13:
1392 case RENDERPATH_GL20:
1393 case RENDERPATH_CGGL:
1395 if (gl_state.depthtest)
1397 qglEnable(GL_DEPTH_TEST);CHECKGLERROR
1401 qglDisable(GL_DEPTH_TEST);CHECKGLERROR
1404 case RENDERPATH_D3D9:
1406 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZENABLE, gl_state.depthtest);
1409 case RENDERPATH_D3D10:
1410 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1412 case RENDERPATH_D3D11:
1413 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1419 void GL_DepthFunc(int state)
1421 if (gl_state.depthfunc != state)
1423 gl_state.depthfunc = state;
1424 switch(vid.renderpath)
1426 case RENDERPATH_GL11:
1427 case RENDERPATH_GL13:
1428 case RENDERPATH_GL20:
1429 case RENDERPATH_CGGL:
1431 qglDepthFunc(gl_state.depthfunc);CHECKGLERROR
1433 case RENDERPATH_D3D9:
1435 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ZFUNC, d3dcmpforglfunc(gl_state.depthfunc));
1438 case RENDERPATH_D3D10:
1439 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1441 case RENDERPATH_D3D11:
1442 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1448 void GL_DepthRange(float nearfrac, float farfrac)
1450 if (gl_state.depthrange[0] != nearfrac || gl_state.depthrange[1] != farfrac)
1452 gl_state.depthrange[0] = nearfrac;
1453 gl_state.depthrange[1] = farfrac;
1454 switch(vid.renderpath)
1456 case RENDERPATH_GL11:
1457 case RENDERPATH_GL13:
1458 case RENDERPATH_GL20:
1459 case RENDERPATH_CGGL:
1460 qglDepthRange(gl_state.depthrange[0], gl_state.depthrange[1]);
1462 case RENDERPATH_D3D9:
1465 D3DVIEWPORT9 d3dviewport;
1466 d3dviewport.X = gl_viewport.x;
1467 d3dviewport.Y = gl_viewport.y;
1468 d3dviewport.Width = gl_viewport.width;
1469 d3dviewport.Height = gl_viewport.height;
1470 d3dviewport.MinZ = gl_state.depthrange[0];
1471 d3dviewport.MaxZ = gl_state.depthrange[1];
1472 IDirect3DDevice9_SetViewport(vid_d3d9dev, &d3dviewport);
1476 case RENDERPATH_D3D10:
1477 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1479 case RENDERPATH_D3D11:
1480 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1486 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)
1488 switch (vid.renderpath)
1490 case RENDERPATH_GL11:
1491 case RENDERPATH_GL13:
1492 case RENDERPATH_GL20:
1493 case RENDERPATH_CGGL:
1497 qglEnable(GL_STENCIL_TEST);CHECKGLERROR
1501 qglDisable(GL_STENCIL_TEST);CHECKGLERROR
1503 if (vid.support.ati_separate_stencil)
1505 qglStencilMask(writemask);CHECKGLERROR
1506 qglStencilOpSeparate(GL_FRONT, frontfail, frontzfail, frontzpass);CHECKGLERROR
1507 qglStencilOpSeparate(GL_BACK, backfail, backzfail, backzpass);CHECKGLERROR
1508 qglStencilFuncSeparate(frontcompare, backcompare, comparereference, comparereference);CHECKGLERROR
1510 else if (vid.support.ext_stencil_two_side)
1512 qglEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
1513 qglActiveStencilFaceEXT(GL_FRONT);CHECKGLERROR
1514 qglStencilMask(writemask);CHECKGLERROR
1515 qglStencilOp(frontfail, frontzfail, frontzpass);CHECKGLERROR
1516 qglStencilFunc(frontcompare, comparereference, comparemask);CHECKGLERROR
1517 qglActiveStencilFaceEXT(GL_BACK);CHECKGLERROR
1518 qglStencilMask(writemask);CHECKGLERROR
1519 qglStencilOp(backfail, backzfail, backzpass);CHECKGLERROR
1520 qglStencilFunc(backcompare, comparereference, comparemask);CHECKGLERROR
1523 case RENDERPATH_D3D9:
1525 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_TWOSIDEDSTENCILMODE, true);
1526 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILENABLE, enable);
1527 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILWRITEMASK, writemask);
1528 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILFAIL, d3dstencilopforglfunc(frontfail));
1529 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILZFAIL, d3dstencilopforglfunc(frontzfail));
1530 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILPASS, d3dstencilopforglfunc(frontzpass));
1531 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILFUNC, frontcompare);
1532 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CCW_STENCILFAIL, d3dstencilopforglfunc(backfail));
1533 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CCW_STENCILZFAIL, d3dstencilopforglfunc(backzfail));
1534 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CCW_STENCILPASS, d3dstencilopforglfunc(backzpass));
1535 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CCW_STENCILFUNC, backcompare);
1536 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILREF, comparereference);
1537 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILMASK, comparemask);
1540 case RENDERPATH_D3D10:
1541 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1543 case RENDERPATH_D3D11:
1544 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1549 void R_SetStencil(qboolean enable, int writemask, int fail, int zfail, int zpass, int compare, int comparereference, int comparemask)
1551 switch (vid.renderpath)
1553 case RENDERPATH_GL11:
1554 case RENDERPATH_GL13:
1555 case RENDERPATH_GL20:
1556 case RENDERPATH_CGGL:
1560 qglEnable(GL_STENCIL_TEST);CHECKGLERROR
1564 qglDisable(GL_STENCIL_TEST);CHECKGLERROR
1566 if (vid.support.ext_stencil_two_side)
1568 qglDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
1570 qglStencilMask(writemask);CHECKGLERROR
1571 qglStencilOp(fail, zfail, zpass);CHECKGLERROR
1572 qglStencilFunc(compare, comparereference, comparemask);CHECKGLERROR
1575 case RENDERPATH_D3D9:
1577 if (vid.support.ati_separate_stencil)
1578 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_TWOSIDEDSTENCILMODE, true);
1579 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILENABLE, enable);
1580 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILWRITEMASK, writemask);
1581 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILFAIL, d3dstencilopforglfunc(fail));
1582 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILZFAIL, d3dstencilopforglfunc(zfail));
1583 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILPASS, d3dstencilopforglfunc(zpass));
1584 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILFUNC, compare);
1585 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILREF, comparereference);
1586 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_STENCILMASK, comparemask);
1589 case RENDERPATH_D3D10:
1590 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1592 case RENDERPATH_D3D11:
1593 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1598 void GL_PolygonOffset(float planeoffset, float depthoffset)
1600 if (gl_state.polygonoffset[0] != planeoffset || gl_state.polygonoffset[1] != depthoffset)
1602 gl_state.polygonoffset[0] = planeoffset;
1603 gl_state.polygonoffset[1] = depthoffset;
1604 switch(vid.renderpath)
1606 case RENDERPATH_GL11:
1607 case RENDERPATH_GL13:
1608 case RENDERPATH_GL20:
1609 case RENDERPATH_CGGL:
1610 qglPolygonOffset(gl_state.polygonoffset[0], gl_state.polygonoffset[1]);
1612 case RENDERPATH_D3D9:
1614 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_SLOPESCALEDEPTHBIAS, gl_state.polygonoffset[0]);
1615 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_DEPTHBIAS, gl_state.polygonoffset[1] * (1.0f / 16777216.0f));
1618 case RENDERPATH_D3D10:
1619 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1621 case RENDERPATH_D3D11:
1622 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1628 void GL_SetMirrorState(qboolean state)
1630 if (v_flipped_state != state)
1632 v_flipped_state = state;
1633 if (gl_state.cullface == GL_BACK)
1634 gl_state.cullface = GL_FRONT;
1635 else if (gl_state.cullface == GL_FRONT)
1636 gl_state.cullface = GL_BACK;
1639 switch(vid.renderpath)
1641 case RENDERPATH_GL11:
1642 case RENDERPATH_GL13:
1643 case RENDERPATH_GL20:
1644 case RENDERPATH_CGGL:
1645 qglCullFace(gl_state.cullface);
1647 case RENDERPATH_D3D9:
1649 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, gl_state.cullface == GL_FRONT ? D3DCULL_CCW : D3DCULL_CW);
1652 case RENDERPATH_D3D10:
1653 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1655 case RENDERPATH_D3D11:
1656 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1662 void GL_CullFace(int state)
1666 if(state == GL_FRONT)
1668 else if(state == GL_BACK)
1672 switch(vid.renderpath)
1674 case RENDERPATH_GL11:
1675 case RENDERPATH_GL13:
1676 case RENDERPATH_GL20:
1677 case RENDERPATH_CGGL:
1680 if (state != GL_NONE)
1682 if (!gl_state.cullfaceenable)
1684 gl_state.cullfaceenable = true;
1685 qglEnable(GL_CULL_FACE);CHECKGLERROR
1687 if (gl_state.cullface != state)
1689 gl_state.cullface = state;
1690 qglCullFace(gl_state.cullface);CHECKGLERROR
1695 if (gl_state.cullfaceenable)
1697 gl_state.cullfaceenable = false;
1698 qglDisable(GL_CULL_FACE);CHECKGLERROR
1702 case RENDERPATH_D3D9:
1704 if (gl_state.cullface != state)
1706 gl_state.cullface = state;
1707 switch(gl_state.cullface)
1710 gl_state.cullfaceenable = false;
1711 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, D3DCULL_NONE);
1714 gl_state.cullfaceenable = true;
1715 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, D3DCULL_CCW);
1718 gl_state.cullfaceenable = true;
1719 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_CULLMODE, D3DCULL_CW);
1725 case RENDERPATH_D3D10:
1726 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1728 case RENDERPATH_D3D11:
1729 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1734 void GL_AlphaTest(int state)
1736 if (gl_state.alphatest != state)
1738 gl_state.alphatest = state;
1739 switch(vid.renderpath)
1741 case RENDERPATH_GL11:
1742 case RENDERPATH_GL13:
1743 case RENDERPATH_GL20:
1744 case RENDERPATH_CGGL:
1746 if (gl_state.alphatest)
1748 qglEnable(GL_ALPHA_TEST);CHECKGLERROR
1752 qglDisable(GL_ALPHA_TEST);CHECKGLERROR
1755 case RENDERPATH_D3D9:
1757 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ALPHATESTENABLE, gl_state.alphatest);
1760 case RENDERPATH_D3D10:
1761 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1763 case RENDERPATH_D3D11:
1764 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1770 void GL_AlphaFunc(int state, float value)
1772 if (gl_state.alphafunc != state || gl_state.alphafuncvalue != value)
1774 gl_state.alphafunc = state;
1775 gl_state.alphafuncvalue = value;
1776 switch(vid.renderpath)
1778 case RENDERPATH_GL11:
1779 case RENDERPATH_GL13:
1780 case RENDERPATH_GL20:
1781 case RENDERPATH_CGGL:
1783 qglAlphaFunc(gl_state.alphafunc, gl_state.alphafuncvalue);CHECKGLERROR
1785 case RENDERPATH_D3D9:
1787 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ALPHAFUNC, d3dcmpforglfunc(gl_state.alphafunc));
1788 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_ALPHAREF, (int)bound(0, value * 256.0f, 255));
1791 case RENDERPATH_D3D10:
1792 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1794 case RENDERPATH_D3D11:
1795 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1801 void GL_ColorMask(int r, int g, int b, int a)
1803 // NOTE: this matches D3DCOLORWRITEENABLE_RED, GREEN, BLUE, ALPHA
1804 int state = (r ? 1 : 0) | (g ? 2 : 0) | (b ? 4 : 0) | (a ? 8 : 0);
1805 if (gl_state.colormask != state)
1807 gl_state.colormask = state;
1808 switch(vid.renderpath)
1810 case RENDERPATH_GL11:
1811 case RENDERPATH_GL13:
1812 case RENDERPATH_GL20:
1813 case RENDERPATH_CGGL:
1815 qglColorMask((GLboolean)r, (GLboolean)g, (GLboolean)b, (GLboolean)a);CHECKGLERROR
1817 case RENDERPATH_D3D9:
1819 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_COLORWRITEENABLE, state);
1822 case RENDERPATH_D3D10:
1823 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1825 case RENDERPATH_D3D11:
1826 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1832 void GL_Color(float cr, float cg, float cb, float ca)
1834 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)
1836 gl_state.color4f[0] = cr;
1837 gl_state.color4f[1] = cg;
1838 gl_state.color4f[2] = cb;
1839 gl_state.color4f[3] = ca;
1840 switch(vid.renderpath)
1842 case RENDERPATH_GL11:
1843 case RENDERPATH_GL13:
1844 case RENDERPATH_GL20:
1845 case RENDERPATH_CGGL:
1847 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);
1850 case RENDERPATH_D3D9:
1851 case RENDERPATH_D3D10:
1852 case RENDERPATH_D3D11:
1853 // no equivalent in D3D
1859 void GL_Scissor (int x, int y, int width, int height)
1861 switch(vid.renderpath)
1863 case RENDERPATH_GL11:
1864 case RENDERPATH_GL13:
1865 case RENDERPATH_GL20:
1866 case RENDERPATH_CGGL:
1868 qglScissor(x, y,width,height);
1871 case RENDERPATH_D3D9:
1877 d3drect.right = x + width;
1878 d3drect.bottom = y + height;
1879 IDirect3DDevice9_SetScissorRect(vid_d3d9dev, &d3drect);
1883 case RENDERPATH_D3D10:
1884 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1886 case RENDERPATH_D3D11:
1887 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1892 void GL_ScissorTest(int state)
1894 if (gl_state.scissortest != state)
1896 gl_state.scissortest = state;
1897 switch(vid.renderpath)
1899 case RENDERPATH_GL11:
1900 case RENDERPATH_GL13:
1901 case RENDERPATH_GL20:
1902 case RENDERPATH_CGGL:
1904 if(gl_state.scissortest)
1905 qglEnable(GL_SCISSOR_TEST);
1907 qglDisable(GL_SCISSOR_TEST);
1910 case RENDERPATH_D3D9:
1912 IDirect3DDevice9_SetRenderState(vid_d3d9dev, D3DRS_SCISSORTESTENABLE, gl_state.scissortest);
1915 case RENDERPATH_D3D10:
1916 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1918 case RENDERPATH_D3D11:
1919 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1925 void GL_Clear(int mask, const float *colorvalue, float depthvalue, int stencilvalue)
1927 static const float blackcolor[4] = {0, 0, 0, 0};
1928 // prevent warnings when trying to clear a buffer that does not exist
1930 colorvalue = blackcolor;
1933 mask &= ~GL_STENCIL_BUFFER_BIT;
1936 switch(vid.renderpath)
1938 case RENDERPATH_GL11:
1939 case RENDERPATH_GL13:
1940 case RENDERPATH_GL20:
1941 case RENDERPATH_CGGL:
1943 if (mask & GL_COLOR_BUFFER_BIT)
1945 qglClearColor(colorvalue[0], colorvalue[1], colorvalue[2], colorvalue[3]);CHECKGLERROR
1947 if (mask & GL_DEPTH_BUFFER_BIT)
1949 qglClearDepth(depthvalue);CHECKGLERROR
1951 if (mask & GL_STENCIL_BUFFER_BIT)
1953 qglClearStencil(stencilvalue);CHECKGLERROR
1955 qglClear(mask);CHECKGLERROR
1957 case RENDERPATH_D3D9:
1959 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);
1962 case RENDERPATH_D3D10:
1963 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1965 case RENDERPATH_D3D11:
1966 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
1971 void GL_ReadPixelsBGRA(int x, int y, int width, int height, unsigned char *outpixels)
1973 switch(vid.renderpath)
1975 case RENDERPATH_GL11:
1976 case RENDERPATH_GL13:
1977 case RENDERPATH_GL20:
1978 case RENDERPATH_CGGL:
1980 qglReadPixels(x, y, width, height, GL_BGRA, GL_UNSIGNED_BYTE, outpixels);CHECKGLERROR
1982 case RENDERPATH_D3D9:
1985 // LordHavoc: we can't directly download the backbuffer because it may be
1986 // multisampled, and it may not be lockable, so we blit it to a lockable
1987 // surface of the same dimensions (but without multisample) to resolve the
1988 // multisample buffer to a normal image, and then lock that...
1989 IDirect3DSurface9 *stretchsurface = NULL;
1990 if (!FAILED(IDirect3DDevice9_CreateRenderTarget(vid_d3d9dev, vid.width, vid.height, D3DFMT_A8R8G8B8, D3DMULTISAMPLE_NONE, 0, TRUE, &stretchsurface, NULL)))
1992 D3DLOCKED_RECT lockedrect;
1993 if (!FAILED(IDirect3DDevice9_StretchRect(vid_d3d9dev, gl_state.d3drt_backbuffercolorsurface, NULL, stretchsurface, NULL, D3DTEXF_POINT)))
1995 if (!FAILED(IDirect3DSurface9_LockRect(stretchsurface, &lockedrect, NULL, D3DLOCK_READONLY)))
1998 unsigned char *row = (unsigned char *)lockedrect.pBits + x * 4 + lockedrect.Pitch * (vid.height - 1 - y);
1999 for (line = 0;line < height;line++, row -= lockedrect.Pitch)
2000 memcpy(outpixels + line * width * 4, row, width * 4);
2001 IDirect3DSurface9_UnlockRect(stretchsurface);
2004 IDirect3DSurface9_Release(stretchsurface);
2007 //IDirect3DSurface9 *syssurface = NULL;
2008 //if (!FAILED(IDirect3DDevice9_CreateRenderTarget(vid_d3d9dev, vid.width, vid.height, D3DFMT_A8R8G8B8, D3DMULTISAMPLE_NONE, 0, FALSE, &stretchsurface, NULL)))
2009 //if (!FAILED(IDirect3DDevice9_CreateOffscreenPlainSurface(vid_d3d9dev, vid.width, vid.height, D3DFMT_A8R8G8B8, D3DPOOL_SCRATCH, &syssurface, NULL)))
2010 //IDirect3DDevice9_GetRenderTargetData(vid_d3d9dev, gl_state.d3drt_backbuffercolorsurface, syssurface);
2011 //if (!FAILED(IDirect3DDevice9_GetFrontBufferData(vid_d3d9dev, 0, syssurface)))
2012 //if (!FAILED(IDirect3DSurface9_LockRect(syssurface, &lockedrect, NULL, D3DLOCK_READONLY)))
2013 //IDirect3DSurface9_UnlockRect(syssurface);
2014 //IDirect3DSurface9_Release(syssurface);
2018 case RENDERPATH_D3D10:
2019 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2021 case RENDERPATH_D3D11:
2022 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2027 // called at beginning of frame
2028 void R_Mesh_Start(void)
2031 gl_state.usevbo_staticvertex = (vid.support.arb_vertex_buffer_object && gl_vbo.integer) || vid.forcevbo;
2032 gl_state.usevbo_staticindex = (vid.support.arb_vertex_buffer_object && (gl_vbo.integer == 1 || gl_vbo.integer == 3)) || vid.forcevbo;
2033 gl_state.usevbo_dynamicvertex = (vid.support.arb_vertex_buffer_object && gl_vbo_dynamicvertex.integer) || vid.forcevbo;
2034 gl_state.usevbo_dynamicindex = (vid.support.arb_vertex_buffer_object && gl_vbo_dynamicindex.integer) || vid.forcevbo;
2035 if (gl_printcheckerror.integer && !gl_paranoid.integer)
2037 Con_Printf("WARNING: gl_printcheckerror is on but gl_paranoid is off, turning it on...\n");
2038 Cvar_SetValueQuick(&gl_paranoid, 1);
2042 qboolean GL_Backend_CompileShader(int programobject, GLenum shadertypeenum, const char *shadertype, int numstrings, const char **strings)
2046 char compilelog[MAX_INPUTLINE];
2047 shaderobject = qglCreateShaderObjectARB(shadertypeenum);CHECKGLERROR
2050 qglShaderSourceARB(shaderobject, numstrings, strings, NULL);CHECKGLERROR
2051 qglCompileShaderARB(shaderobject);CHECKGLERROR
2052 qglGetObjectParameterivARB(shaderobject, GL_OBJECT_COMPILE_STATUS_ARB, &shadercompiled);CHECKGLERROR
2053 qglGetInfoLogARB(shaderobject, sizeof(compilelog), NULL, compilelog);CHECKGLERROR
2054 if (compilelog[0] && (strstr(compilelog, "error") || strstr(compilelog, "ERROR") || strstr(compilelog, "Error") || strstr(compilelog, "WARNING") || strstr(compilelog, "warning") || strstr(compilelog, "Warning")))
2056 int i, j, pretextlines = 0;
2057 for (i = 0;i < numstrings - 1;i++)
2058 for (j = 0;strings[i][j];j++)
2059 if (strings[i][j] == '\n')
2061 Con_Printf("%s shader compile log:\n%s\n(line offset for any above warnings/errors: %i)\n", shadertype, compilelog, pretextlines);
2063 if (!shadercompiled)
2065 qglDeleteObjectARB(shaderobject);CHECKGLERROR
2068 qglAttachObjectARB(programobject, shaderobject);CHECKGLERROR
2069 qglDeleteObjectARB(shaderobject);CHECKGLERROR
2073 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)
2075 GLint programlinked;
2076 GLuint programobject = 0;
2077 char linklog[MAX_INPUTLINE];
2080 programobject = qglCreateProgramObjectARB();CHECKGLERROR
2084 if (vertexstrings_count && !GL_Backend_CompileShader(programobject, GL_VERTEX_SHADER_ARB, "vertex", vertexstrings_count, vertexstrings_list))
2087 #ifdef GL_GEOMETRY_SHADER_ARB
2088 if (geometrystrings_count && !GL_Backend_CompileShader(programobject, GL_GEOMETRY_SHADER_ARB, "geometry", geometrystrings_count, geometrystrings_list))
2092 if (fragmentstrings_count && !GL_Backend_CompileShader(programobject, GL_FRAGMENT_SHADER_ARB, "fragment", fragmentstrings_count, fragmentstrings_list))
2095 qglLinkProgramARB(programobject);CHECKGLERROR
2096 qglGetObjectParameterivARB(programobject, GL_OBJECT_LINK_STATUS_ARB, &programlinked);CHECKGLERROR
2097 qglGetInfoLogARB(programobject, sizeof(linklog), NULL, linklog);CHECKGLERROR
2100 if (strstr(linklog, "error") || strstr(linklog, "ERROR") || strstr(linklog, "Error") || strstr(linklog, "WARNING") || strstr(linklog, "warning") || strstr(linklog, "Warning"))
2101 Con_DPrintf("program link log:\n%s\n", linklog);
2102 // software vertex shader is ok but software fragment shader is WAY
2103 // too slow, fail program if so.
2104 // NOTE: this string might be ATI specific, but that's ok because the
2105 // ATI R300 chip (Radeon 9500-9800/X300) is the most likely to use a
2106 // software fragment shader due to low instruction and dependent
2108 if (strstr(linklog, "fragment shader will run in software"))
2109 programlinked = false;
2113 return programobject;
2115 qglDeleteObjectARB(programobject);CHECKGLERROR
2119 void GL_Backend_FreeProgram(unsigned int prog)
2122 qglDeleteObjectARB(prog);
2126 void GL_Backend_RenumberElements(int *out, int count, const int *in, int offset)
2131 for (i = 0;i < count;i++)
2132 *out++ = *in++ + offset;
2135 memcpy(out, in, sizeof(*out) * count);
2138 // renders triangles using vertices from the active arrays
2139 int paranoidblah = 0;
2140 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)
2142 unsigned int numelements = numtriangles * 3;
2144 size_t bufferoffset3i;
2146 size_t bufferoffset3s;
2147 if (numvertices < 3 || numtriangles < 1)
2149 if (numvertices < 0 || numtriangles < 0 || developer_extra.integer)
2150 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);
2153 if (!gl_mesh_prefer_short_elements.integer)
2157 if (element3i_indexbuffer)
2158 element3i_indexbuffer = NULL;
2160 // adjust the pointers for firsttriangle
2162 element3i += firsttriangle * 3;
2163 if (element3i_indexbuffer)
2164 element3i_bufferoffset += firsttriangle * 3 * sizeof(*element3i);
2166 element3s += firsttriangle * 3;
2167 if (element3s_indexbuffer)
2168 element3s_bufferoffset += firsttriangle * 3 * sizeof(*element3s);
2169 // check if the user specified to ignore static index buffers
2170 if (!gl_state.usevbo_staticindex || (gl_vbo.integer == 3 && !vid.forcevbo && (element3i_bufferoffset || element3s_bufferoffset)))
2172 element3i_indexbuffer = NULL;
2173 element3s_indexbuffer = NULL;
2175 // upload a dynamic index buffer if needed
2178 if (!element3s_indexbuffer && gl_state.usevbo_dynamicindex)
2180 if (gl_state.draw_dynamicindexbuffer)
2181 R_Mesh_UpdateMeshBuffer(gl_state.draw_dynamicindexbuffer, (void *)element3s, numelements * sizeof(*element3s));
2183 gl_state.draw_dynamicindexbuffer = R_Mesh_CreateMeshBuffer((void *)element3s, numelements * sizeof(*element3s), "temporary", true, true, true);
2184 element3s_indexbuffer = gl_state.draw_dynamicindexbuffer;
2185 element3s_bufferoffset = 0;
2190 if (!element3i_indexbuffer && gl_state.usevbo_dynamicindex)
2192 if (gl_state.draw_dynamicindexbuffer)
2193 R_Mesh_UpdateMeshBuffer(gl_state.draw_dynamicindexbuffer, (void *)element3i, numelements * sizeof(*element3i));
2195 gl_state.draw_dynamicindexbuffer = R_Mesh_CreateMeshBuffer((void *)element3i, numelements * sizeof(*element3i), "temporary", true, true, false);
2196 element3i_indexbuffer = gl_state.draw_dynamicindexbuffer;
2197 element3i_bufferoffset = 0;
2200 bufferobject3i = element3i_indexbuffer ? element3i_indexbuffer->bufferobject : 0;
2201 bufferoffset3i = element3i_bufferoffset;
2202 bufferobject3s = element3s_indexbuffer ? element3s_indexbuffer->bufferobject : 0;
2203 bufferoffset3s = element3s_bufferoffset;
2204 r_refdef.stats.meshes++;
2205 r_refdef.stats.meshes_elements += numelements;
2206 if (gl_paranoid.integer)
2209 // LordHavoc: disabled this - it needs to be updated to handle components and gltype and stride in each array
2211 unsigned int j, size;
2213 // note: there's no validation done here on buffer objects because it
2214 // is somewhat difficult to get at the data, and gl_paranoid can be
2215 // used without buffer objects if the need arises
2216 // (the data could be gotten using glMapBuffer but it would be very
2217 // slow due to uncachable video memory reads)
2218 if (!qglIsEnabled(GL_VERTEX_ARRAY))
2219 Con_Print("R_Mesh_Draw: vertex array not enabled\n");
2221 if (gl_state.pointer_vertex_pointer)
2222 for (j = 0, size = numvertices * 3, p = (int *)((float *)gl_state.pointer_vertex + firstvertex * 3);j < size;j++, p++)
2224 if (gl_state.pointer_color_enabled)
2226 if (!qglIsEnabled(GL_COLOR_ARRAY))
2227 Con_Print("R_Mesh_Draw: color array set but not enabled\n");
2229 if (gl_state.pointer_color && gl_state.pointer_color_enabled)
2230 for (j = 0, size = numvertices * 4, p = (int *)((float *)gl_state.pointer_color + firstvertex * 4);j < size;j++, p++)
2233 for (i = 0;i < vid.texarrayunits;i++)
2235 if (gl_state.units[i].arrayenabled)
2237 GL_ClientActiveTexture(i);
2238 if (!qglIsEnabled(GL_TEXTURE_COORD_ARRAY))
2239 Con_Print("R_Mesh_Draw: texcoord array set but not enabled\n");
2241 if (gl_state.units[i].pointer_texcoord && gl_state.units[i].arrayenabled)
2242 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++)
2249 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
2251 if (element3i[i] < firstvertex || element3i[i] >= firstvertex + numvertices)
2253 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3i array\n", element3i[i], firstvertex, firstvertex + numvertices);
2260 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
2262 if (element3s[i] < firstvertex || element3s[i] >= firstvertex + numvertices)
2264 Con_Printf("R_Mesh_Draw: invalid vertex index %i (outside range %i - %i) in element3s array\n", element3s[i], firstvertex, firstvertex + numvertices);
2270 if (r_render.integer || r_refdef.draw2dstage)
2272 switch(vid.renderpath)
2274 case RENDERPATH_GL11:
2275 case RENDERPATH_GL13:
2276 case RENDERPATH_GL20:
2277 case RENDERPATH_CGGL:
2279 if (gl_mesh_testmanualfeeding.integer)
2281 unsigned int i, j, element;
2283 qglBegin(GL_TRIANGLES);
2284 for (i = 0;i < (unsigned int) numtriangles * 3;i++)
2287 element = element3i[i];
2289 element = element3s[i];
2291 element = firstvertex + i;
2292 for (j = 0;j < vid.texarrayunits;j++)
2294 if (gl_state.units[j].pointer_texcoord_pointer && gl_state.units[j].arrayenabled)
2296 if (gl_state.units[j].pointer_texcoord_gltype == GL_FLOAT)
2298 p = (const GLfloat *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
2299 if (vid.texarrayunits > 1)
2301 if (gl_state.units[j].pointer_texcoord_components == 4)
2302 qglMultiTexCoord4f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2], p[3]);
2303 else if (gl_state.units[j].pointer_texcoord_components == 3)
2304 qglMultiTexCoord3f(GL_TEXTURE0_ARB + j, p[0], p[1], p[2]);
2305 else if (gl_state.units[j].pointer_texcoord_components == 2)
2306 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, p[0], p[1]);
2308 qglMultiTexCoord1f(GL_TEXTURE0_ARB + j, p[0]);
2312 if (gl_state.units[j].pointer_texcoord_components == 4)
2313 qglTexCoord4f(p[0], p[1], p[2], p[3]);
2314 else if (gl_state.units[j].pointer_texcoord_components == 3)
2315 qglTexCoord3f(p[0], p[1], p[2]);
2316 else if (gl_state.units[j].pointer_texcoord_components == 2)
2317 qglTexCoord2f(p[0], p[1]);
2319 qglTexCoord1f(p[0]);
2322 else if (gl_state.units[j].pointer_texcoord_gltype == GL_SHORT)
2324 const GLshort *s = (const GLshort *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
2325 if (vid.texarrayunits > 1)
2327 if (gl_state.units[j].pointer_texcoord_components == 4)
2328 qglMultiTexCoord4f(GL_TEXTURE0_ARB + j, s[0], s[1], s[2], s[3]);
2329 else if (gl_state.units[j].pointer_texcoord_components == 3)
2330 qglMultiTexCoord3f(GL_TEXTURE0_ARB + j, s[0], s[1], s[2]);
2331 else if (gl_state.units[j].pointer_texcoord_components == 2)
2332 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, s[0], s[1]);
2333 else if (gl_state.units[j].pointer_texcoord_components == 1)
2334 qglMultiTexCoord1f(GL_TEXTURE0_ARB + j, s[0]);
2338 if (gl_state.units[j].pointer_texcoord_components == 4)
2339 qglTexCoord4f(s[0], s[1], s[2], s[3]);
2340 else if (gl_state.units[j].pointer_texcoord_components == 3)
2341 qglTexCoord3f(s[0], s[1], s[2]);
2342 else if (gl_state.units[j].pointer_texcoord_components == 2)
2343 qglTexCoord2f(s[0], s[1]);
2344 else if (gl_state.units[j].pointer_texcoord_components == 1)
2345 qglTexCoord1f(s[0]);
2348 else if (gl_state.units[j].pointer_texcoord_gltype == GL_BYTE)
2350 const GLbyte *sb = (const GLbyte *)((const unsigned char *)gl_state.units[j].pointer_texcoord_pointer + element * gl_state.units[j].pointer_texcoord_stride);
2351 if (vid.texarrayunits > 1)
2353 if (gl_state.units[j].pointer_texcoord_components == 4)
2354 qglMultiTexCoord4f(GL_TEXTURE0_ARB + j, sb[0], sb[1], sb[2], sb[3]);
2355 else if (gl_state.units[j].pointer_texcoord_components == 3)
2356 qglMultiTexCoord3f(GL_TEXTURE0_ARB + j, sb[0], sb[1], sb[2]);
2357 else if (gl_state.units[j].pointer_texcoord_components == 2)
2358 qglMultiTexCoord2f(GL_TEXTURE0_ARB + j, sb[0], sb[1]);
2359 else if (gl_state.units[j].pointer_texcoord_components == 1)
2360 qglMultiTexCoord1f(GL_TEXTURE0_ARB + j, sb[0]);
2364 if (gl_state.units[j].pointer_texcoord_components == 4)
2365 qglTexCoord4f(sb[0], sb[1], sb[2], sb[3]);
2366 else if (gl_state.units[j].pointer_texcoord_components == 3)
2367 qglTexCoord3f(sb[0], sb[1], sb[2]);
2368 else if (gl_state.units[j].pointer_texcoord_components == 2)
2369 qglTexCoord2f(sb[0], sb[1]);
2370 else if (gl_state.units[j].pointer_texcoord_components == 1)
2371 qglTexCoord1f(sb[0]);
2376 if (gl_state.pointer_color_pointer && gl_state.pointer_color_enabled && gl_state.pointer_color_components == 4)
2378 if (gl_state.pointer_color_gltype == GL_FLOAT)
2380 p = (const GLfloat *)((const unsigned char *)gl_state.pointer_color_pointer + element * gl_state.pointer_color_stride);
2381 qglColor4f(p[0], p[1], p[2], p[3]);
2383 else if (gl_state.pointer_color_gltype == GL_UNSIGNED_BYTE)
2385 const GLubyte *ub = (const GLubyte *)((const unsigned char *)gl_state.pointer_color_pointer + element * gl_state.pointer_color_stride);
2386 qglColor4ub(ub[0], ub[1], ub[2], ub[3]);
2389 if (gl_state.pointer_vertex_gltype == GL_FLOAT)
2391 p = (const GLfloat *)((const unsigned char *)gl_state.pointer_vertex_pointer + element * gl_state.pointer_vertex_stride);
2392 if (gl_state.pointer_vertex_components == 4)
2393 qglVertex4f(p[0], p[1], p[2], p[3]);
2394 else if (gl_state.pointer_vertex_components == 3)
2395 qglVertex3f(p[0], p[1], p[2]);
2397 qglVertex2f(p[0], p[1]);
2403 else if (bufferobject3s)
2405 GL_BindEBO(bufferobject3s);
2406 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
2408 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, (void *)bufferoffset3s);
2413 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, (void *)(firsttriangle * sizeof(unsigned short[3])));
2417 else if (bufferobject3i)
2419 GL_BindEBO(bufferobject3i);
2420 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
2422 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, (void *)bufferoffset3i);
2427 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, (void *)(firsttriangle * sizeof(unsigned int[3])));
2434 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
2436 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_SHORT, element3s);
2441 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_SHORT, element3s);
2448 if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
2450 qglDrawRangeElements(GL_TRIANGLES, firstvertex, firstvertex + numvertices - 1, numelements, GL_UNSIGNED_INT, element3i);
2455 qglDrawElements(GL_TRIANGLES, numelements, GL_UNSIGNED_INT, element3i);
2461 qglDrawArrays(GL_TRIANGLES, firstvertex, numvertices);
2465 case RENDERPATH_D3D9:
2468 if (element3s_indexbuffer)
2470 IDirect3DDevice9_SetIndices(vid_d3d9dev, (IDirect3DIndexBuffer9 *)element3s_indexbuffer->devicebuffer);
2471 IDirect3DDevice9_DrawIndexedPrimitive(vid_d3d9dev, D3DPT_TRIANGLELIST, 0, firstvertex, numvertices, element3s_bufferoffset>>1, numtriangles);
2473 else if (element3i_indexbuffer)
2475 IDirect3DDevice9_SetIndices(vid_d3d9dev, (IDirect3DIndexBuffer9 *)element3i_indexbuffer->devicebuffer);
2476 IDirect3DDevice9_DrawIndexedPrimitive(vid_d3d9dev, D3DPT_TRIANGLELIST, 0, firstvertex, numvertices, element3i_bufferoffset>>2, numtriangles);
2479 IDirect3DDevice9_DrawPrimitive(vid_d3d9dev, D3DPT_TRIANGLELIST, firstvertex, numvertices);
2483 case RENDERPATH_D3D10:
2484 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2486 case RENDERPATH_D3D11:
2487 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2493 // restores backend state, used when done with 3D rendering
2494 void R_Mesh_Finish(void)
2498 r_meshbuffer_t *R_Mesh_CreateMeshBuffer(const void *data, size_t size, const char *name, qboolean isindexbuffer, qboolean isdynamic, qboolean isindex16)
2500 r_meshbuffer_t *buffer;
2501 if (!(isdynamic ? (isindexbuffer ? gl_state.usevbo_dynamicindex : gl_state.usevbo_dynamicvertex) : (isindexbuffer ? gl_state.usevbo_staticindex : gl_state.usevbo_staticvertex)))
2503 buffer = (r_meshbuffer_t *)Mem_ExpandableArray_AllocRecord(&gl_state.meshbufferarray);
2504 memset(buffer, 0, sizeof(*buffer));
2505 buffer->bufferobject = 0;
2506 buffer->devicebuffer = NULL;
2508 buffer->isindexbuffer = isindexbuffer;
2509 buffer->isdynamic = isdynamic;
2510 buffer->isindex16 = isindex16;
2511 strlcpy(buffer->name, name, sizeof(buffer->name));
2512 R_Mesh_UpdateMeshBuffer(buffer, data, size);
2516 void R_Mesh_UpdateMeshBuffer(r_meshbuffer_t *buffer, const void *data, size_t size)
2520 if (buffer->isindexbuffer)
2522 r_refdef.stats.indexbufferuploadcount++;
2523 r_refdef.stats.indexbufferuploadsize += size;
2527 r_refdef.stats.vertexbufferuploadcount++;
2528 r_refdef.stats.vertexbufferuploadsize += size;
2530 switch(vid.renderpath)
2532 case RENDERPATH_GL11:
2533 case RENDERPATH_GL13:
2534 case RENDERPATH_GL20:
2535 case RENDERPATH_CGGL:
2536 if (!buffer->bufferobject)
2537 qglGenBuffersARB(1, (GLuint *)&buffer->bufferobject);
2538 if (buffer->isindexbuffer)
2539 GL_BindEBO(buffer->bufferobject);
2541 GL_BindVBO(buffer->bufferobject);
2542 qglBufferDataARB(buffer->isindexbuffer ? GL_ELEMENT_ARRAY_BUFFER_ARB : GL_ARRAY_BUFFER_ARB, size, data, buffer->isdynamic ? GL_STREAM_DRAW_ARB : GL_STATIC_DRAW_ARB);
2544 case RENDERPATH_D3D9:
2548 void *datapointer = NULL;
2549 if (buffer->isindexbuffer)
2551 IDirect3DIndexBuffer9 *d3d9indexbuffer = (IDirect3DIndexBuffer9 *)buffer->devicebuffer;
2552 if (size != buffer->size || !buffer->devicebuffer)
2554 if (buffer->devicebuffer)
2555 IDirect3DIndexBuffer9_Release((IDirect3DIndexBuffer9*)buffer->devicebuffer);
2556 buffer->devicebuffer = NULL;
2557 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)))
2558 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);
2559 buffer->devicebuffer = (void *)d3d9indexbuffer;
2560 buffer->size = size;
2562 if (!FAILED(IDirect3DIndexBuffer9_Lock(d3d9indexbuffer, 0, 0, &datapointer, buffer->isdynamic ? D3DLOCK_DISCARD : 0)))
2565 memcpy(datapointer, data, size);
2567 memset(datapointer, 0, size);
2568 IDirect3DIndexBuffer9_Unlock(d3d9indexbuffer);
2573 IDirect3DVertexBuffer9 *d3d9vertexbuffer = (IDirect3DVertexBuffer9 *)buffer->devicebuffer;
2574 if (size != buffer->size || !buffer->devicebuffer)
2576 if (buffer->devicebuffer)
2577 IDirect3DVertexBuffer9_Release((IDirect3DVertexBuffer9*)buffer->devicebuffer);
2578 buffer->devicebuffer = NULL;
2579 if (FAILED(result = IDirect3DDevice9_CreateVertexBuffer(vid_d3d9dev, size, buffer->isdynamic ? D3DUSAGE_WRITEONLY | D3DUSAGE_DYNAMIC : 0, 0, buffer->isdynamic ? D3DPOOL_DEFAULT : D3DPOOL_MANAGED, &d3d9vertexbuffer, NULL)))
2580 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);
2581 buffer->devicebuffer = (void *)d3d9vertexbuffer;
2582 buffer->size = size;
2584 if (!FAILED(IDirect3DVertexBuffer9_Lock(d3d9vertexbuffer, 0, 0, &datapointer, buffer->isdynamic ? D3DLOCK_DISCARD : 0)))
2587 memcpy(datapointer, data, size);
2589 memset(datapointer, 0, size);
2590 IDirect3DVertexBuffer9_Unlock(d3d9vertexbuffer);
2596 case RENDERPATH_D3D10:
2597 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2599 case RENDERPATH_D3D11:
2600 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2605 void R_Mesh_DestroyMeshBuffer(r_meshbuffer_t *buffer)
2609 switch(vid.renderpath)
2611 case RENDERPATH_GL11:
2612 case RENDERPATH_GL13:
2613 case RENDERPATH_GL20:
2614 case RENDERPATH_CGGL:
2615 qglDeleteBuffersARB(1, (GLuint *)&buffer->bufferobject);
2617 case RENDERPATH_D3D9:
2619 if (buffer->devicebuffer)
2621 if (buffer->isindexbuffer)
2622 IDirect3DIndexBuffer9_Release((IDirect3DIndexBuffer9 *)buffer->devicebuffer);
2624 IDirect3DVertexBuffer9_Release((IDirect3DVertexBuffer9 *)buffer->devicebuffer);
2628 case RENDERPATH_D3D10:
2629 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2631 case RENDERPATH_D3D11:
2632 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2635 Mem_ExpandableArray_FreeRecord(&gl_state.meshbufferarray, (void *)buffer);
2638 void GL_Mesh_ListVBOs(qboolean printeach)
2641 size_t ebocount = 0, ebomemory = 0;
2642 size_t vbocount = 0, vbomemory = 0;
2643 r_meshbuffer_t *buffer;
2644 endindex = Mem_ExpandableArray_IndexRange(&gl_state.meshbufferarray);
2645 for (i = 0;i < endindex;i++)
2647 buffer = (r_meshbuffer_t *) Mem_ExpandableArray_RecordAtIndex(&gl_state.meshbufferarray, i);
2650 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)");}
2651 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)");}
2653 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);
2658 void R_Mesh_VertexPointer(int components, int gltype, size_t stride, const void *pointer, const r_meshbuffer_t *vertexbuffer, size_t bufferoffset)
2660 int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
2661 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)
2663 gl_state.pointer_vertex_components = components;
2664 gl_state.pointer_vertex_gltype = gltype;
2665 gl_state.pointer_vertex_stride = stride;
2666 gl_state.pointer_vertex_pointer = pointer;
2667 gl_state.pointer_vertex_vertexbuffer = vertexbuffer;
2668 gl_state.pointer_vertex_offset = bufferoffset;
2670 GL_BindVBO(bufferobject);
2671 qglVertexPointer(components, gltype, stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
2675 void R_Mesh_ColorPointer(int components, int gltype, size_t stride, const void *pointer, const r_meshbuffer_t *vertexbuffer, size_t bufferoffset)
2677 // note: vertexbuffer may be non-NULL even if pointer is NULL, so check
2678 // the pointer only.
2681 int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
2682 // caller wants color array enabled
2683 if (!gl_state.pointer_color_enabled)
2685 gl_state.pointer_color_enabled = true;
2687 qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
2689 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)
2691 gl_state.pointer_color_components = components;
2692 gl_state.pointer_color_gltype = gltype;
2693 gl_state.pointer_color_stride = stride;
2694 gl_state.pointer_color_pointer = pointer;
2695 gl_state.pointer_color_vertexbuffer = vertexbuffer;
2696 gl_state.pointer_color_offset = bufferoffset;
2698 GL_BindVBO(bufferobject);
2699 qglColorPointer(components, gltype, stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
2704 // caller wants color array disabled
2705 if (gl_state.pointer_color_enabled)
2707 gl_state.pointer_color_enabled = false;
2709 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
2710 // when color array is on the glColor gets trashed, set it again
2711 qglColor4f(gl_state.color4f[0], gl_state.color4f[1], gl_state.color4f[2], gl_state.color4f[3]);CHECKGLERROR
2716 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)
2718 gltextureunit_t *unit = gl_state.units + unitnum;
2719 // update array settings
2721 // note: there is no need to check bufferobject here because all cases
2722 // that involve a valid bufferobject also supply a texcoord array
2725 int bufferobject = vertexbuffer ? vertexbuffer->bufferobject : 0;
2726 // texture array unit is enabled, enable the array
2727 if (!unit->arrayenabled)
2729 unit->arrayenabled = true;
2730 GL_ClientActiveTexture(unitnum);
2731 qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
2734 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)
2736 unit->pointer_texcoord_components = components;
2737 unit->pointer_texcoord_gltype = gltype;
2738 unit->pointer_texcoord_stride = stride;
2739 unit->pointer_texcoord_pointer = pointer;
2740 unit->pointer_texcoord_vertexbuffer = vertexbuffer;
2741 unit->pointer_texcoord_offset = bufferoffset;
2742 GL_ClientActiveTexture(unitnum);
2743 GL_BindVBO(bufferobject);
2744 qglTexCoordPointer(components, gltype, stride, bufferobject ? (void *)bufferoffset : pointer);CHECKGLERROR
2749 // texture array unit is disabled, disable the array
2750 if (unit->arrayenabled)
2752 unit->arrayenabled = false;
2753 GL_ClientActiveTexture(unitnum);
2754 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
2759 int R_Mesh_TexBound(unsigned int unitnum, int id)
2761 gltextureunit_t *unit = gl_state.units + unitnum;
2762 if (unitnum >= vid.teximageunits)
2764 if (id == GL_TEXTURE_2D)
2766 if (id == GL_TEXTURE_3D)
2768 if (id == GL_TEXTURE_CUBE_MAP_ARB)
2769 return unit->tcubemap;
2770 if (id == GL_TEXTURE_RECTANGLE_ARB)
2771 return unit->trectangle;
2775 void R_Mesh_CopyToTexture(rtexture_t *tex, int tx, int ty, int sx, int sy, int width, int height)
2777 switch(vid.renderpath)
2779 case RENDERPATH_GL11:
2780 case RENDERPATH_GL13:
2781 case RENDERPATH_GL20:
2782 case RENDERPATH_CGGL:
2783 R_Mesh_TexBind(0, tex);
2784 GL_ActiveTexture(0);CHECKGLERROR
2785 qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, tx, ty, sx, sy, width, height);CHECKGLERROR
2787 case RENDERPATH_D3D9:
2790 IDirect3DSurface9 *currentsurface = NULL;
2791 IDirect3DSurface9 *texturesurface = NULL;
2794 sourcerect.left = sx;
2795 sourcerect.top = sy;
2796 sourcerect.right = sx + width;
2797 sourcerect.bottom = sy + height;
2800 destrect.right = tx + width;
2801 destrect.bottom = ty + height;
2802 if (!FAILED(IDirect3DTexture9_GetSurfaceLevel(((IDirect3DTexture9 *)tex->d3dtexture), 0, &texturesurface)))
2804 if (!FAILED(IDirect3DDevice9_GetRenderTarget(vid_d3d9dev, 0, ¤tsurface)))
2806 IDirect3DDevice9_StretchRect(vid_d3d9dev, currentsurface, &sourcerect, texturesurface, &destrect, D3DTEXF_NONE);
2807 IDirect3DSurface9_Release(currentsurface);
2809 IDirect3DSurface9_Release(texturesurface);
2814 case RENDERPATH_D3D10:
2815 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2817 case RENDERPATH_D3D11:
2818 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2824 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};
2827 void R_Mesh_TexBind(unsigned int unitnum, rtexture_t *tex)
2829 gltextureunit_t *unit = gl_state.units + unitnum;
2830 int tex2d, tex3d, texcubemap, texnum;
2831 if (unitnum >= vid.teximageunits)
2833 // if (unit->texture == tex)
2835 switch(vid.renderpath)
2837 case RENDERPATH_GL20:
2838 case RENDERPATH_CGGL:
2841 tex = r_texture_white;
2842 // not initialized enough yet...
2846 unit->texture = tex;
2847 texnum = R_GetTexture(tex);
2848 switch(tex->gltexturetypeenum)
2850 case GL_TEXTURE_2D: if (unit->t2d != texnum) {GL_ActiveTexture(unitnum);unit->t2d = texnum;qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR}break;
2851 case GL_TEXTURE_3D: if (unit->t3d != texnum) {GL_ActiveTexture(unitnum);unit->t3d = texnum;qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR}break;
2852 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;
2853 case GL_TEXTURE_RECTANGLE_ARB: if (unit->trectangle != texnum) {GL_ActiveTexture(unitnum);unit->trectangle = texnum;qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR}break;
2856 case RENDERPATH_GL13:
2857 case RENDERPATH_GL11:
2858 unit->texture = tex;
2864 texnum = R_GetTexture(tex);
2865 switch(tex->gltexturetypeenum)
2873 case GL_TEXTURE_CUBE_MAP_ARB:
2874 texcubemap = texnum;
2878 // update 2d texture binding
2879 if (unit->t2d != tex2d)
2881 GL_ActiveTexture(unitnum);
2886 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
2893 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
2897 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
2899 // update 3d texture binding
2900 if (unit->t3d != tex3d)
2902 GL_ActiveTexture(unitnum);
2907 qglEnable(GL_TEXTURE_3D);CHECKGLERROR
2914 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
2918 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
2920 // update cubemap texture binding
2921 if (unit->tcubemap != texcubemap)
2923 GL_ActiveTexture(unitnum);
2926 if (unit->tcubemap == 0)
2928 qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
2935 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
2938 unit->tcubemap = texcubemap;
2939 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
2942 case RENDERPATH_D3D9:
2945 extern cvar_t gl_texture_anisotropy;
2948 tex = r_texture_white;
2949 // not initialized enough yet...
2953 if (unit->texture == tex)
2955 unit->texture = tex;
2956 // upload texture if needed
2958 R_RealGetTexture(tex);
2959 IDirect3DDevice9_SetTexture(vid_d3d9dev, unitnum, (IDirect3DBaseTexture9*)tex->d3dtexture);
2960 //IDirect3DDevice9_SetRenderState(vid_d3d9dev, d3drswrap[unitnum], (tex->flags & TEXF_CLAMP) ? (D3DWRAPCOORD_0 | D3DWRAPCOORD_1 | D3DWRAPCOORD_2) : 0);
2961 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_ADDRESSU, tex->d3daddressu);
2962 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_ADDRESSV, tex->d3daddressv);
2963 if (tex->d3daddressw)
2964 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_ADDRESSW, tex->d3daddressw);
2965 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MAGFILTER, tex->d3dmagfilter);
2966 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MINFILTER, tex->d3dminfilter);
2967 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MIPFILTER, tex->d3dmipfilter);
2968 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MIPMAPLODBIAS, tex->d3dmipmaplodbias);
2969 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MAXMIPLEVEL, tex->d3dmaxmiplevelfilter);
2970 IDirect3DDevice9_SetSamplerState(vid_d3d9dev, unitnum, D3DSAMP_MAXANISOTROPY, gl_texture_anisotropy.integer);
2974 case RENDERPATH_D3D10:
2975 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2977 case RENDERPATH_D3D11:
2978 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
2983 void R_Mesh_TexMatrix(unsigned int unitnum, const matrix4x4_t *matrix)
2985 gltextureunit_t *unit = gl_state.units + unitnum;
2986 switch(vid.renderpath)
2988 case RENDERPATH_GL11:
2989 case RENDERPATH_GL13:
2990 case RENDERPATH_GL20:
2991 case RENDERPATH_CGGL:
2992 if (matrix && matrix->m[3][3])
2994 // texmatrix specified, check if it is different
2995 if (!unit->texmatrixenabled || memcmp(&unit->matrix, matrix, sizeof(matrix4x4_t)))
2998 unit->texmatrixenabled = true;
2999 unit->matrix = *matrix;
3001 Matrix4x4_ToArrayFloatGL(&unit->matrix, glmatrix);
3002 GL_ActiveTexture(unitnum);
3003 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
3004 qglLoadMatrixf(glmatrix);CHECKGLERROR
3005 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
3010 // no texmatrix specified, revert to identity
3011 if (unit->texmatrixenabled)
3013 unit->texmatrixenabled = false;
3014 unit->matrix = identitymatrix;
3016 GL_ActiveTexture(unitnum);
3017 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
3018 qglLoadIdentity();CHECKGLERROR
3019 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
3023 case RENDERPATH_D3D9:
3024 case RENDERPATH_D3D10:
3025 case RENDERPATH_D3D11:
3030 void R_Mesh_TexCombine(unsigned int unitnum, int combinergb, int combinealpha, int rgbscale, int alphascale)
3032 gltextureunit_t *unit = gl_state.units + unitnum;
3034 switch(vid.renderpath)
3036 case RENDERPATH_GL20:
3037 case RENDERPATH_CGGL:
3040 case RENDERPATH_GL13:
3041 // GL_ARB_texture_env_combine
3043 combinergb = GL_MODULATE;
3045 combinealpha = GL_MODULATE;
3050 if (combinergb != combinealpha || rgbscale != 1 || alphascale != 1)
3052 if (combinergb == GL_DECAL)
3053 combinergb = GL_INTERPOLATE_ARB;
3054 if (unit->combine != GL_COMBINE_ARB)
3056 unit->combine = GL_COMBINE_ARB;
3057 GL_ActiveTexture(unitnum);
3058 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);CHECKGLERROR
3059 qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_TEXTURE);CHECKGLERROR // for GL_INTERPOLATE_ARB mode
3061 if (unit->combinergb != combinergb)
3063 unit->combinergb = combinergb;
3064 GL_ActiveTexture(unitnum);
3065 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, unit->combinergb);CHECKGLERROR
3067 if (unit->combinealpha != combinealpha)
3069 unit->combinealpha = combinealpha;
3070 GL_ActiveTexture(unitnum);
3071 qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, unit->combinealpha);CHECKGLERROR
3073 if (unit->rgbscale != rgbscale)
3075 unit->rgbscale = rgbscale;
3076 GL_ActiveTexture(unitnum);
3077 qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, unit->rgbscale);CHECKGLERROR
3079 if (unit->alphascale != alphascale)
3081 unit->alphascale = alphascale;
3082 GL_ActiveTexture(unitnum);
3083 qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, unit->alphascale);CHECKGLERROR
3088 if (unit->combine != combinergb)
3090 unit->combine = combinergb;
3091 GL_ActiveTexture(unitnum);
3092 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combine);CHECKGLERROR
3096 case RENDERPATH_GL11:
3099 combinergb = GL_MODULATE;
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
3107 case RENDERPATH_D3D9:
3108 case RENDERPATH_D3D10:
3109 case RENDERPATH_D3D11:
3114 void R_Mesh_ResetTextureState(void)
3116 unsigned int unitnum;
3121 switch(vid.renderpath)
3123 case RENDERPATH_GL20:
3124 case RENDERPATH_CGGL:
3125 for (unitnum = 0;unitnum < vid.teximageunits;unitnum++)
3127 gltextureunit_t *unit = gl_state.units + unitnum;
3131 GL_ActiveTexture(unitnum);
3132 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
3137 GL_ActiveTexture(unitnum);
3138 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
3143 GL_ActiveTexture(unitnum);
3144 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
3146 if (unit->trectangle)
3148 unit->trectangle = 0;
3149 GL_ActiveTexture(unitnum);
3150 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
3153 for (unitnum = 0;unitnum < vid.texarrayunits;unitnum++)
3155 gltextureunit_t *unit = gl_state.units + unitnum;
3156 if (unit->arrayenabled)
3158 unit->arrayenabled = false;
3159 GL_ClientActiveTexture(unitnum);
3160 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
3163 for (unitnum = 0;unitnum < vid.texunits;unitnum++)
3165 gltextureunit_t *unit = gl_state.units + unitnum;
3166 if (unit->texmatrixenabled)
3168 unit->texmatrixenabled = false;
3169 unit->matrix = identitymatrix;
3171 GL_ActiveTexture(unitnum);
3172 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
3173 qglLoadIdentity();CHECKGLERROR
3174 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
3178 case RENDERPATH_GL13:
3179 case RENDERPATH_GL11:
3180 for (unitnum = 0;unitnum < vid.texunits;unitnum++)
3182 gltextureunit_t *unit = gl_state.units + unitnum;
3186 GL_ActiveTexture(unitnum);
3187 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
3188 qglBindTexture(GL_TEXTURE_2D, unit->t2d);CHECKGLERROR
3193 GL_ActiveTexture(unitnum);
3194 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
3195 qglBindTexture(GL_TEXTURE_3D, unit->t3d);CHECKGLERROR
3200 GL_ActiveTexture(unitnum);
3201 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
3202 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, unit->tcubemap);CHECKGLERROR
3204 if (unit->trectangle)
3206 unit->trectangle = 0;
3207 GL_ActiveTexture(unitnum);
3208 qglDisable(GL_TEXTURE_RECTANGLE_ARB);CHECKGLERROR
3209 qglBindTexture(GL_TEXTURE_RECTANGLE_ARB, unit->trectangle);CHECKGLERROR
3211 if (unit->arrayenabled)
3213 unit->arrayenabled = false;
3214 GL_ClientActiveTexture(unitnum);
3215 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
3217 if (unit->texmatrixenabled)
3219 unit->texmatrixenabled = false;
3220 unit->matrix = identitymatrix;
3222 GL_ActiveTexture(unitnum);
3223 qglMatrixMode(GL_TEXTURE);CHECKGLERROR
3224 qglLoadIdentity();CHECKGLERROR
3225 qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
3227 if (unit->combine != GL_MODULATE)
3229 unit->combine = GL_MODULATE;
3230 GL_ActiveTexture(unitnum);
3231 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, unit->combine);CHECKGLERROR
3235 case RENDERPATH_D3D9:
3236 case RENDERPATH_D3D10:
3237 case RENDERPATH_D3D11:
3245 //#define r_vertexposition_d3d9fvf (D3DFVF_XYZ)
3246 //#define r_vertexgeneric_d3d9fvf (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)
3247 //#define r_vertexmesh_d3d9fvf (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX5 | D3DFVF_TEXCOORDSIZE1(3) | D3DFVF_TEXCOORDSIZE2(3) | D3DFVF_TEXCOORDSIZE3(3))
3249 D3DVERTEXELEMENT9 r_vertexposition_d3d9elements[] =
3251 {0, (int)((size_t)&((r_vertexposition_t *)0)->vertex3f), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
3255 D3DVERTEXELEMENT9 r_vertexgeneric_d3d9elements[] =
3257 {0, (int)((size_t)&((r_vertexgeneric_t *)0)->vertex3f ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
3258 {0, (int)((size_t)&((r_vertexgeneric_t *)0)->color4ub ), D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
3259 {0, (int)((size_t)&((r_vertexgeneric_t *)0)->texcoord2f), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
3263 D3DVERTEXELEMENT9 r_vertexmesh_d3d9elements[] =
3265 {0, (int)((size_t)&((r_vertexmesh_t *)0)->vertex3f ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},
3266 {0, (int)((size_t)&((r_vertexmesh_t *)0)->color4ub ), D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0},
3267 {0, (int)((size_t)&((r_vertexmesh_t *)0)->texcoordtexture2f ), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
3268 {0, (int)((size_t)&((r_vertexmesh_t *)0)->svector3f ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1},
3269 {0, (int)((size_t)&((r_vertexmesh_t *)0)->tvector3f ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 2},
3270 {0, (int)((size_t)&((r_vertexmesh_t *)0)->normal3f ), D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 3},
3271 {0, (int)((size_t)&((r_vertexmesh_t *)0)->texcoordlightmap2f), D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 4},
3275 IDirect3DVertexDeclaration9 *r_vertexposition_d3d9decl;
3276 IDirect3DVertexDeclaration9 *r_vertexgeneric_d3d9decl;
3277 IDirect3DVertexDeclaration9 *r_vertexmesh_d3d9decl;
3280 static void R_Mesh_InitVertexDeclarations(void)
3283 r_vertexposition_d3d9decl = NULL;
3284 r_vertexgeneric_d3d9decl = NULL;
3285 r_vertexmesh_d3d9decl = NULL;
3286 switch(vid.renderpath)
3288 case RENDERPATH_D3D9:
3289 IDirect3DDevice9_CreateVertexDeclaration(vid_d3d9dev, r_vertexposition_d3d9elements, &r_vertexposition_d3d9decl);
3290 IDirect3DDevice9_CreateVertexDeclaration(vid_d3d9dev, r_vertexgeneric_d3d9elements, &r_vertexgeneric_d3d9decl);
3291 IDirect3DDevice9_CreateVertexDeclaration(vid_d3d9dev, r_vertexmesh_d3d9elements, &r_vertexmesh_d3d9decl);
3293 case RENDERPATH_D3D10:
3294 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3296 case RENDERPATH_D3D11:
3297 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3303 static void R_Mesh_DestroyVertexDeclarations(void)
3306 if (r_vertexposition_d3d9decl)
3307 IDirect3DVertexDeclaration9_Release(r_vertexposition_d3d9decl);
3308 r_vertexposition_d3d9decl = NULL;
3309 if (r_vertexgeneric_d3d9decl)
3310 IDirect3DVertexDeclaration9_Release(r_vertexgeneric_d3d9decl);
3311 r_vertexgeneric_d3d9decl = NULL;
3312 if (r_vertexmesh_d3d9decl)
3313 IDirect3DVertexDeclaration9_Release(r_vertexmesh_d3d9decl);
3314 r_vertexmesh_d3d9decl = NULL;
3318 r_vertexposition_t *R_Mesh_PrepareVertices_Position_Lock(int numvertices)
3321 size = sizeof(r_vertexposition_t) * numvertices;
3322 if (gl_state.preparevertices_tempdatamaxsize < size)
3324 gl_state.preparevertices_tempdatamaxsize = size;
3325 gl_state.preparevertices_tempdata = Mem_Realloc(r_main_mempool, gl_state.preparevertices_tempdata, gl_state.preparevertices_tempdatamaxsize);
3327 gl_state.preparevertices_vertexposition = (r_vertexposition_t *)gl_state.preparevertices_tempdata;
3328 gl_state.preparevertices_numvertices = numvertices;
3329 return gl_state.preparevertices_vertexposition;
3332 qboolean R_Mesh_PrepareVertices_Position_Unlock(void)
3334 R_Mesh_PrepareVertices_Position(gl_state.preparevertices_numvertices, gl_state.preparevertices_vertexposition, NULL);
3335 gl_state.preparevertices_vertexposition = NULL;
3336 gl_state.preparevertices_numvertices = 0;
3340 void R_Mesh_PrepareVertices_Position_Arrays(int numvertices, const float *vertex3f)
3343 r_vertexposition_t *vertex;
3344 switch(vid.renderpath)
3346 case RENDERPATH_GL20:
3347 case RENDERPATH_CGGL:
3348 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
3349 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
3350 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3351 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3352 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3353 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3354 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3356 case RENDERPATH_GL13:
3357 case RENDERPATH_GL11:
3358 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
3359 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
3360 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3361 if (vid.texunits >= 2)
3362 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3363 if (vid.texunits >= 3)
3364 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3366 case RENDERPATH_D3D9:
3367 case RENDERPATH_D3D10:
3368 case RENDERPATH_D3D11:
3372 // no quick path for this case, convert to vertex structs
3373 vertex = R_Mesh_PrepareVertices_Position_Lock(numvertices);
3374 for (i = 0;i < numvertices;i++)
3375 VectorCopy(vertex3f + 3*i, vertex[i].vertex3f);
3376 R_Mesh_PrepareVertices_Position_Unlock();
3377 R_Mesh_PrepareVertices_Position(numvertices, vertex, NULL);
3380 void R_Mesh_PrepareVertices_Position(int numvertices, const r_vertexposition_t *vertex, const r_meshbuffer_t *vertexbuffer)
3382 // upload temporary vertexbuffer for this rendering
3383 if (!gl_state.usevbo_staticvertex)
3384 vertexbuffer = NULL;
3385 if (!vertexbuffer && gl_state.usevbo_dynamicvertex)
3387 if (gl_state.preparevertices_dynamicvertexbuffer)
3388 R_Mesh_UpdateMeshBuffer(gl_state.preparevertices_dynamicvertexbuffer, vertex, numvertices * sizeof(*vertex));
3390 gl_state.preparevertices_dynamicvertexbuffer = R_Mesh_CreateMeshBuffer(vertex, numvertices * sizeof(*vertex), "temporary", false, true, false);
3391 vertexbuffer = gl_state.preparevertices_dynamicvertexbuffer;
3395 switch(vid.renderpath)
3397 case RENDERPATH_GL20:
3398 case RENDERPATH_CGGL:
3399 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3400 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3401 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3402 case RENDERPATH_GL13:
3403 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3404 case RENDERPATH_GL11:
3405 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , vertexbuffer, (int)((unsigned char *)vertex->vertex3f - (unsigned char *)vertex));
3406 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
3407 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3409 case RENDERPATH_D3D9:
3411 IDirect3DDevice9_SetVertexDeclaration(vid_d3d9dev, r_vertexposition_d3d9decl);
3412 IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, (IDirect3DVertexBuffer9*)vertexbuffer->devicebuffer, 0, sizeof(*vertex));
3415 case RENDERPATH_D3D10:
3416 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3418 case RENDERPATH_D3D11:
3419 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3424 switch(vid.renderpath)
3426 case RENDERPATH_GL20:
3427 case RENDERPATH_CGGL:
3428 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3429 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3430 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3431 case RENDERPATH_GL13:
3432 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3433 case RENDERPATH_GL11:
3434 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , NULL, 0);
3435 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), NULL, NULL, 0);
3436 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3438 case RENDERPATH_D3D9:
3439 case RENDERPATH_D3D10:
3440 case RENDERPATH_D3D11:
3447 r_vertexgeneric_t *R_Mesh_PrepareVertices_Generic_Lock(int numvertices)
3450 size = sizeof(r_vertexgeneric_t) * numvertices;
3451 if (gl_state.preparevertices_tempdatamaxsize < size)
3453 gl_state.preparevertices_tempdatamaxsize = size;
3454 gl_state.preparevertices_tempdata = Mem_Realloc(r_main_mempool, gl_state.preparevertices_tempdata, gl_state.preparevertices_tempdatamaxsize);
3456 gl_state.preparevertices_vertexgeneric = (r_vertexgeneric_t *)gl_state.preparevertices_tempdata;
3457 gl_state.preparevertices_numvertices = numvertices;
3458 return gl_state.preparevertices_vertexgeneric;
3461 qboolean R_Mesh_PrepareVertices_Generic_Unlock(void)
3463 R_Mesh_PrepareVertices_Generic(gl_state.preparevertices_numvertices, gl_state.preparevertices_vertexgeneric, NULL);
3464 gl_state.preparevertices_vertexgeneric = NULL;
3465 gl_state.preparevertices_numvertices = 0;
3469 void R_Mesh_PrepareVertices_Generic_Arrays(int numvertices, const float *vertex3f, const float *color4f, const float *texcoord2f)
3472 r_vertexgeneric_t *vertex;
3473 switch(vid.renderpath)
3475 case RENDERPATH_GL20:
3476 case RENDERPATH_CGGL:
3477 if (gl_mesh_separatearrays.integer)
3479 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
3480 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
3481 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoord2f, NULL, 0);
3482 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3483 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3484 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3485 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3489 case RENDERPATH_GL13:
3490 case RENDERPATH_GL11:
3491 if (gl_mesh_separatearrays.integer)
3493 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
3494 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
3495 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoord2f, NULL, 0);
3496 if (vid.texunits >= 2)
3497 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3498 if (vid.texunits >= 3)
3499 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3503 case RENDERPATH_D3D9:
3504 case RENDERPATH_D3D10:
3505 case RENDERPATH_D3D11:
3509 // no quick path for this case, convert to vertex structs
3510 vertex = R_Mesh_PrepareVertices_Generic_Lock(numvertices);
3511 for (i = 0;i < numvertices;i++)
3512 VectorCopy(vertex3f + 3*i, vertex[i].vertex3f);
3515 for (i = 0;i < numvertices;i++)
3516 Vector4Scale(color4f + 4*i, 255.0f, vertex[i].color4ub);
3520 float tempcolor4f[4];
3521 unsigned char tempcolor4ub[4];
3522 Vector4Scale(gl_state.color4f, 255.0f, tempcolor4f);
3523 tempcolor4ub[0] = (unsigned char)bound(0.0f, tempcolor4f[0], 255.0f);
3524 tempcolor4ub[1] = (unsigned char)bound(0.0f, tempcolor4f[1], 255.0f);
3525 tempcolor4ub[2] = (unsigned char)bound(0.0f, tempcolor4f[2], 255.0f);
3526 tempcolor4ub[3] = (unsigned char)bound(0.0f, tempcolor4f[3], 255.0f);
3527 for (i = 0;i < numvertices;i++)
3528 Vector4Copy(tempcolor4ub, vertex[i].color4ub);
3531 for (i = 0;i < numvertices;i++)
3532 Vector2Copy(texcoord2f + 2*i, vertex[i].texcoord2f);
3533 R_Mesh_PrepareVertices_Generic_Unlock();
3534 R_Mesh_PrepareVertices_Generic(numvertices, vertex, NULL);
3537 void R_Mesh_PrepareVertices_Generic(int numvertices, const r_vertexgeneric_t *vertex, const r_meshbuffer_t *vertexbuffer)
3539 // upload temporary vertexbuffer for this rendering
3540 if (!gl_state.usevbo_staticvertex)
3541 vertexbuffer = NULL;
3542 if (!vertexbuffer && gl_state.usevbo_dynamicvertex)
3544 if (gl_state.preparevertices_dynamicvertexbuffer)
3545 R_Mesh_UpdateMeshBuffer(gl_state.preparevertices_dynamicvertexbuffer, vertex, numvertices * sizeof(*vertex));
3547 gl_state.preparevertices_dynamicvertexbuffer = R_Mesh_CreateMeshBuffer(vertex, numvertices * sizeof(*vertex), "temporary", false, true, false);
3548 vertexbuffer = gl_state.preparevertices_dynamicvertexbuffer;
3552 switch(vid.renderpath)
3554 case RENDERPATH_GL20:
3555 case RENDERPATH_CGGL:
3556 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3557 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3558 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3559 case RENDERPATH_GL13:
3560 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3561 case RENDERPATH_GL11:
3562 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , vertexbuffer, (int)((unsigned char *)vertex->vertex3f - (unsigned char *)vertex));
3563 R_Mesh_ColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(*vertex), vertex->color4ub , vertexbuffer, (int)((unsigned char *)vertex->color4ub - (unsigned char *)vertex));
3564 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoord2f , vertexbuffer, (int)((unsigned char *)vertex->texcoord2f - (unsigned char *)vertex));
3566 case RENDERPATH_D3D9:
3568 IDirect3DDevice9_SetVertexDeclaration(vid_d3d9dev, r_vertexgeneric_d3d9decl);
3569 IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, (IDirect3DVertexBuffer9*)vertexbuffer->devicebuffer, 0, sizeof(*vertex));
3572 case RENDERPATH_D3D10:
3573 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3575 case RENDERPATH_D3D11:
3576 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3581 switch(vid.renderpath)
3583 case RENDERPATH_GL20:
3584 case RENDERPATH_CGGL:
3585 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3586 R_Mesh_TexCoordPointer(3, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3587 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3588 case RENDERPATH_GL13:
3589 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3590 case RENDERPATH_GL11:
3591 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , NULL, 0);
3592 R_Mesh_ColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(*vertex), vertex->color4ub , NULL, 0);
3593 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoord2f , NULL, 0);
3595 case RENDERPATH_D3D9:
3596 case RENDERPATH_D3D10:
3597 case RENDERPATH_D3D11:
3604 r_vertexmesh_t *R_Mesh_PrepareVertices_Mesh_Lock(int numvertices)
3607 size = sizeof(r_vertexmesh_t) * numvertices;
3608 if (gl_state.preparevertices_tempdatamaxsize < size)
3610 gl_state.preparevertices_tempdatamaxsize = size;
3611 gl_state.preparevertices_tempdata = Mem_Realloc(r_main_mempool, gl_state.preparevertices_tempdata, gl_state.preparevertices_tempdatamaxsize);
3613 gl_state.preparevertices_vertexmesh = (r_vertexmesh_t *)gl_state.preparevertices_tempdata;
3614 gl_state.preparevertices_numvertices = numvertices;
3615 return gl_state.preparevertices_vertexmesh;
3618 qboolean R_Mesh_PrepareVertices_Mesh_Unlock(void)
3620 R_Mesh_PrepareVertices_Mesh(gl_state.preparevertices_numvertices, gl_state.preparevertices_vertexmesh, NULL);
3621 gl_state.preparevertices_vertexmesh = NULL;
3622 gl_state.preparevertices_numvertices = 0;
3626 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)
3629 r_vertexmesh_t *vertex;
3630 switch(vid.renderpath)
3632 case RENDERPATH_GL20:
3633 case RENDERPATH_CGGL:
3634 if (gl_mesh_separatearrays.integer)
3636 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
3637 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
3638 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoordtexture2f, NULL, 0);
3639 R_Mesh_TexCoordPointer(1, 3, GL_FLOAT, sizeof(float[3]), svector3f, NULL, 0);
3640 R_Mesh_TexCoordPointer(2, 3, GL_FLOAT, sizeof(float[3]), tvector3f, NULL, 0);
3641 R_Mesh_TexCoordPointer(3, 3, GL_FLOAT, sizeof(float[3]), normal3f, NULL, 0);
3642 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT, sizeof(float[2]), texcoordlightmap2f, NULL, 0);
3646 case RENDERPATH_GL13:
3647 case RENDERPATH_GL11:
3648 if (gl_mesh_separatearrays.integer)
3650 R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), vertex3f, NULL, 0);
3651 R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), color4f, NULL, 0);
3652 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT, sizeof(float[2]), texcoordtexture2f, NULL, 0);
3653 if (vid.texunits >= 2)
3654 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT, sizeof(float[2]), texcoordlightmap2f, NULL, 0);
3655 if (vid.texunits >= 3)
3656 R_Mesh_TexCoordPointer(2, 2, GL_FLOAT, sizeof(float[2]), NULL, NULL, 0);
3660 case RENDERPATH_D3D9:
3661 case RENDERPATH_D3D10:
3662 case RENDERPATH_D3D11:
3666 vertex = R_Mesh_PrepareVertices_Mesh_Lock(numvertices);
3667 for (i = 0;i < numvertices;i++)
3668 VectorCopy(vertex3f + 3*i, vertex[i].vertex3f);
3670 for (i = 0;i < numvertices;i++)
3671 VectorCopy(svector3f + 3*i, vertex[i].svector3f);
3673 for (i = 0;i < numvertices;i++)
3674 VectorCopy(tvector3f + 3*i, vertex[i].tvector3f);
3676 for (i = 0;i < numvertices;i++)
3677 VectorCopy(normal3f + 3*i, vertex[i].normal3f);
3680 for (i = 0;i < numvertices;i++)
3681 Vector4Scale(color4f + 4*i, 255.0f, vertex[i].color4ub);
3685 float tempcolor4f[4];
3686 unsigned char tempcolor4ub[4];
3687 Vector4Scale(gl_state.color4f, 255.0f, tempcolor4f);
3688 tempcolor4ub[0] = (unsigned char)bound(0.0f, tempcolor4f[0], 255.0f);
3689 tempcolor4ub[1] = (unsigned char)bound(0.0f, tempcolor4f[1], 255.0f);
3690 tempcolor4ub[2] = (unsigned char)bound(0.0f, tempcolor4f[2], 255.0f);
3691 tempcolor4ub[3] = (unsigned char)bound(0.0f, tempcolor4f[3], 255.0f);
3692 for (i = 0;i < numvertices;i++)
3693 Vector4Copy(tempcolor4ub, vertex[i].color4ub);
3695 if (texcoordtexture2f)
3696 for (i = 0;i < numvertices;i++)
3697 Vector2Copy(texcoordtexture2f + 2*i, vertex[i].texcoordtexture2f);
3698 if (texcoordlightmap2f)
3699 for (i = 0;i < numvertices;i++)
3700 Vector2Copy(texcoordlightmap2f + 2*i, vertex[i].texcoordlightmap2f);
3701 R_Mesh_PrepareVertices_Mesh_Unlock();
3702 R_Mesh_PrepareVertices_Mesh(numvertices, vertex, NULL);
3705 void R_Mesh_PrepareVertices_Mesh(int numvertices, const r_vertexmesh_t *vertex, const r_meshbuffer_t *vertexbuffer)
3707 // upload temporary vertexbuffer for this rendering
3708 if (!gl_state.usevbo_staticvertex)
3709 vertexbuffer = NULL;
3710 if (!vertexbuffer && gl_state.usevbo_dynamicvertex)
3712 if (gl_state.preparevertices_dynamicvertexbuffer)
3713 R_Mesh_UpdateMeshBuffer(gl_state.preparevertices_dynamicvertexbuffer, vertex, numvertices * sizeof(*vertex));
3715 gl_state.preparevertices_dynamicvertexbuffer = R_Mesh_CreateMeshBuffer(vertex, numvertices * sizeof(*vertex), "temporary", false, true, false);
3716 vertexbuffer = gl_state.preparevertices_dynamicvertexbuffer;
3720 switch(vid.renderpath)
3722 case RENDERPATH_GL20:
3723 case RENDERPATH_CGGL:
3724 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , vertexbuffer, (int)((unsigned char *)vertex->vertex3f - (unsigned char *)vertex));
3725 R_Mesh_ColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(*vertex), vertex->color4ub , vertexbuffer, (int)((unsigned char *)vertex->color4ub - (unsigned char *)vertex));
3726 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordtexture2f , vertexbuffer, (int)((unsigned char *)vertex->texcoordtexture2f - (unsigned char *)vertex));
3727 R_Mesh_TexCoordPointer(1, 3, GL_FLOAT , sizeof(*vertex), vertex->svector3f , vertexbuffer, (int)((unsigned char *)vertex->svector3f - (unsigned char *)vertex));
3728 R_Mesh_TexCoordPointer(2, 3, GL_FLOAT , sizeof(*vertex), vertex->tvector3f , vertexbuffer, (int)((unsigned char *)vertex->tvector3f - (unsigned char *)vertex));
3729 R_Mesh_TexCoordPointer(3, 4, GL_FLOAT , sizeof(*vertex), vertex->normal3f , vertexbuffer, (int)((unsigned char *)vertex->normal3f - (unsigned char *)vertex));
3730 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordlightmap2f, vertexbuffer, (int)((unsigned char *)vertex->texcoordlightmap2f - (unsigned char *)vertex));
3732 case RENDERPATH_GL13:
3733 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordlightmap2f, vertexbuffer, (int)((unsigned char *)vertex->texcoordlightmap2f - (unsigned char *)vertex));
3734 case RENDERPATH_GL11:
3735 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , vertexbuffer, (int)((unsigned char *)vertex->vertex3f - (unsigned char *)vertex));
3736 R_Mesh_ColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(*vertex), vertex->color4ub , vertexbuffer, (int)((unsigned char *)vertex->color4ub - (unsigned char *)vertex));
3737 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordtexture2f , vertexbuffer, (int)((unsigned char *)vertex->texcoordtexture2f - (unsigned char *)vertex));
3739 case RENDERPATH_D3D9:
3741 IDirect3DDevice9_SetVertexDeclaration(vid_d3d9dev, r_vertexmesh_d3d9decl);
3742 IDirect3DDevice9_SetStreamSource(vid_d3d9dev, 0, (IDirect3DVertexBuffer9*)vertexbuffer->devicebuffer, 0, sizeof(*vertex));
3745 case RENDERPATH_D3D10:
3746 Con_DPrintf("FIXME D3D10 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3748 case RENDERPATH_D3D11:
3749 Con_DPrintf("FIXME D3D11 %s:%i %s\n", __FILE__, __LINE__, __FUNCTION__);
3754 switch(vid.renderpath)
3756 case RENDERPATH_GL20:
3757 case RENDERPATH_CGGL:
3758 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , NULL, 0);
3759 R_Mesh_ColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(*vertex), vertex->color4ub , NULL, 0);
3760 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordtexture2f , NULL, 0);
3761 R_Mesh_TexCoordPointer(1, 3, GL_FLOAT , sizeof(*vertex), vertex->svector3f , NULL, 0);
3762 R_Mesh_TexCoordPointer(2, 3, GL_FLOAT , sizeof(*vertex), vertex->tvector3f , NULL, 0);
3763 R_Mesh_TexCoordPointer(3, 4, GL_FLOAT , sizeof(*vertex), vertex->normal3f , NULL, 0);
3764 R_Mesh_TexCoordPointer(4, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordlightmap2f, NULL, 0);
3766 case RENDERPATH_GL13:
3767 R_Mesh_TexCoordPointer(1, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordlightmap2f, NULL, 0);
3768 case RENDERPATH_GL11:
3769 R_Mesh_VertexPointer( 3, GL_FLOAT , sizeof(*vertex), vertex->vertex3f , NULL, 0);
3770 R_Mesh_ColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(*vertex), vertex->color4ub , NULL, 0);
3771 R_Mesh_TexCoordPointer(0, 2, GL_FLOAT , sizeof(*vertex), vertex->texcoordtexture2f , NULL, 0);
3773 case RENDERPATH_D3D9:
3774 case RENDERPATH_D3D10:
3775 case RENDERPATH_D3D11: