3D attenuation texture works now
[divverent/darkplaces.git] / gl_backend.c
1
2 #include "quakedef.h"
3
4 cvar_t gl_mesh_maxverts = {0, "gl_mesh_maxverts", "1024"};
5 cvar_t gl_mesh_floatcolors = {0, "gl_mesh_floatcolors", "1"};
6 cvar_t gl_mesh_drawrangeelements = {0, "gl_mesh_drawrangeelements", "1"};
7
8 cvar_t r_render = {0, "r_render", "1"};
9 cvar_t gl_dither = {CVAR_SAVE, "gl_dither", "1"}; // whether or not to use dithering
10 cvar_t gl_lockarrays = {0, "gl_lockarrays", "1"};
11
12 int gl_maxdrawrangeelementsvertices;
13 int gl_maxdrawrangeelementsindices;
14
15 #ifdef DEBUGGL
16 int errornumber = 0;
17
18 void GL_PrintError(int errornumber, char *filename, int linenumber)
19 {
20         switch(errornumber)
21         {
22 #ifdef GL_INVALID_ENUM
23         case GL_INVALID_ENUM:
24                 Con_Printf("GL_INVALID_ENUM at %s:%i\n", filename, linenumber);
25                 break;
26 #endif
27 #ifdef GL_INVALID_VALUE
28         case GL_INVALID_VALUE:
29                 Con_Printf("GL_INVALID_VALUE at %s:%i\n", filename, linenumber);
30                 break;
31 #endif
32 #ifdef GL_INVALID_OPERATION
33         case GL_INVALID_OPERATION:
34                 Con_Printf("GL_INVALID_OPERATION at %s:%i\n", filename, linenumber);
35                 break;
36 #endif
37 #ifdef GL_STACK_OVERFLOW
38         case GL_STACK_OVERFLOW:
39                 Con_Printf("GL_STACK_OVERFLOW at %s:%i\n", filename, linenumber);
40                 break;
41 #endif
42 #ifdef GL_STACK_UNDERFLOW
43         case GL_STACK_UNDERFLOW:
44                 Con_Printf("GL_STACK_UNDERFLOW at %s:%i\n", filename, linenumber);
45                 break;
46 #endif
47 #ifdef GL_OUT_OF_MEMORY
48         case GL_OUT_OF_MEMORY:
49                 Con_Printf("GL_OUT_OF_MEMORY at %s:%i\n", filename, linenumber);
50                 break;
51 #endif
52 #ifdef GL_TABLE_TOO_LARGE
53     case GL_TABLE_TOO_LARGE:
54                 Con_Printf("GL_TABLE_TOO_LARGE at %s:%i\n", filename, linenumber);
55                 break;
56 #endif
57         default:
58                 Con_Printf("GL UNKNOWN (%i) at %s:%i\n", errornumber, filename, linenumber);
59                 break;
60         }
61 }
62 #endif
63
64 #define BACKENDACTIVECHECK if (!backendactive) Sys_Error("GL backend function called when backend is not active\n");
65
66 int c_meshs, c_meshelements;
67
68 void SCR_ScreenShot_f (void);
69
70 // these are externally accessible
71 int r_lightmapscalebit;
72 float r_colorscale;
73 float *varray_vertex;
74 float *varray_color;
75 float *varray_texcoord[MAX_TEXTUREUNITS];
76 int mesh_maxverts;
77
78 static matrix4x4_t backend_viewmatrix;
79 static matrix4x4_t backend_modelmatrix;
80 static matrix4x4_t backend_modelviewmatrix;
81 static matrix4x4_t backend_glmodelviewmatrix;
82
83 static int backendunits, backendactive;
84 static qbyte *varray_bcolor;
85 static mempool_t *gl_backend_mempool;
86
87 int polygonelements[768];
88
89 void GL_Backend_AllocArrays(void)
90 {
91         int i;
92
93         for (i = 0;i < POLYGONELEMENTS_MAXPOINTS - 2;i++)
94         {
95                 polygonelements[i * 3 + 0] = 0;
96                 polygonelements[i * 3 + 1] = i + 1;
97                 polygonelements[i * 3 + 2] = i + 2;
98         }
99
100         if (!gl_backend_mempool)
101                 gl_backend_mempool = Mem_AllocPool("GL_Backend");
102
103         varray_vertex = Mem_Alloc(gl_backend_mempool, mesh_maxverts * sizeof(float[4]));
104         varray_color = Mem_Alloc(gl_backend_mempool, mesh_maxverts * sizeof(float[4]));
105         varray_bcolor = Mem_Alloc(gl_backend_mempool, mesh_maxverts * sizeof(qbyte[4]));
106         for (i = 0;i < backendunits;i++)
107                 varray_texcoord[i] = Mem_Alloc(gl_backend_mempool, mesh_maxverts * sizeof(float[4]));
108         for (;i < MAX_TEXTUREUNITS;i++)
109                 varray_texcoord[i] = NULL;
110 }
111
112 void GL_Backend_FreeArrays(int resizingbuffers)
113 {
114         int i;
115         if (resizingbuffers)
116                 Mem_EmptyPool(gl_backend_mempool);
117         else
118                 Mem_FreePool(&gl_backend_mempool);
119         varray_vertex = NULL;
120         varray_color = NULL;
121         varray_bcolor = NULL;
122         for (i = 0;i < MAX_TEXTUREUNITS;i++)
123                 varray_texcoord[i] = NULL;
124 }
125
126 static void gl_backend_start(void)
127 {
128         Con_Printf("OpenGL Backend started with gl_mesh_maxverts %i\n", gl_mesh_maxverts.integer);
129         if (qglDrawRangeElements != NULL)
130         {
131                 qglGetIntegerv(GL_MAX_ELEMENTS_VERTICES, &gl_maxdrawrangeelementsvertices);
132                 qglGetIntegerv(GL_MAX_ELEMENTS_INDICES, &gl_maxdrawrangeelementsindices);
133                 CHECKGLERROR
134                 Con_Printf("glDrawRangeElements detected (max vertices %i, max indices %i)\n", gl_maxdrawrangeelementsvertices, gl_maxdrawrangeelementsindices);
135         }
136         if (strstr(gl_renderer, "3Dfx"))
137         {
138                 Con_Printf("3Dfx driver detected, forcing gl_mesh_floatcolors to 0 to prevent crashs\n");
139                 Cvar_SetValueQuick(&gl_mesh_floatcolors, 0);
140         }
141
142         backendunits = min(MAX_TEXTUREUNITS, gl_textureunits);
143
144         GL_Backend_AllocArrays();
145
146         backendactive = true;
147 }
148
149 static void gl_backend_shutdown(void)
150 {
151         backendunits = 0;
152         backendactive = false;
153
154         Con_Printf("OpenGL Backend shutting down\n");
155
156         GL_Backend_FreeArrays(false);
157 }
158
159 void GL_Backend_CheckCvars(void)
160 {
161         // 21760 is (65536 / 3) rounded off to a multiple of 128
162         if (gl_mesh_maxverts.integer < 1024)
163                 Cvar_SetValueQuick(&gl_mesh_maxverts, 1024);
164         if (gl_mesh_maxverts.integer > 21760)
165                 Cvar_SetValueQuick(&gl_mesh_maxverts, 21760);
166 }
167
168 void GL_Backend_ResizeArrays(int numtriangles)
169 {
170         Cvar_SetValueQuick(&gl_mesh_maxverts, numtriangles);
171         GL_Backend_CheckCvars();
172         mesh_maxverts = gl_mesh_maxverts.integer;
173         GL_Backend_FreeArrays(true);
174         GL_Backend_AllocArrays();
175 }
176
177 static void gl_backend_newmap(void)
178 {
179 }
180
181 void gl_backend_init(void)
182 {
183         Cvar_RegisterVariable(&r_render);
184         Cvar_RegisterVariable(&gl_dither);
185         Cvar_RegisterVariable(&gl_lockarrays);
186 #ifdef NORENDER
187         Cvar_SetValue("r_render", 0);
188 #endif
189
190         Cvar_RegisterVariable(&gl_mesh_maxverts);
191         Cvar_RegisterVariable(&gl_mesh_floatcolors);
192         Cvar_RegisterVariable(&gl_mesh_drawrangeelements);
193         GL_Backend_CheckCvars();
194         R_RegisterModule("GL_Backend", gl_backend_start, gl_backend_shutdown, gl_backend_newmap);
195 }
196
197 void GL_SetupView_ViewPort (int x, int y, int width, int height)
198 {
199         if (!r_render.integer)
200                 return;
201
202         // y is weird beause OpenGL is bottom to top, we use top to bottom
203         qglViewport(x, vid.realheight - (y + height), width, height);
204         CHECKGLERROR
205 }
206
207 void GL_SetupView_Orientation_Identity (void)
208 {
209         Matrix4x4_CreateIdentity(&backend_viewmatrix);
210         memset(&backend_modelmatrix, 0, sizeof(backend_modelmatrix));
211 }
212
213 void GL_SetupView_Orientation_FromEntity (vec3_t origin, vec3_t angles)
214 {
215         Matrix4x4_CreateRotate(&backend_viewmatrix, -90, 1, 0, 0);
216         Matrix4x4_ConcatRotate(&backend_viewmatrix, 90, 0, 0, 1);
217         Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[2], 1, 0, 0);
218         Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[0], 0, 1, 0);
219         Matrix4x4_ConcatRotate(&backend_viewmatrix, -angles[1], 0, 0, 1);
220         Matrix4x4_ConcatTranslate(&backend_viewmatrix, -origin[0], -origin[1], -origin[2]);
221         memset(&backend_modelmatrix, 0, sizeof(backend_modelmatrix));
222 }
223
224 void GL_SetupView_Mode_Perspective (double aspect, double fovx, double fovy, double zNear, double zFar)
225 {
226         double xmax, ymax;
227
228         if (!r_render.integer)
229                 return;
230
231         // set up viewpoint
232         qglMatrixMode(GL_PROJECTION);CHECKGLERROR
233         qglLoadIdentity ();CHECKGLERROR
234         // pyramid slopes
235         xmax = zNear * tan(fovx * M_PI / 360.0);
236         ymax = zNear * tan(fovy * M_PI / 360.0);
237         // set view pyramid
238         qglFrustum(-xmax, xmax, -ymax, ymax, zNear, zFar);CHECKGLERROR
239         qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
240         GL_SetupView_Orientation_Identity();
241 }
242
243 void GL_SetupView_Mode_Ortho (double x1, double y1, double x2, double y2, double zNear, double zFar)
244 {
245         if (!r_render.integer)
246                 return;
247
248         // set up viewpoint
249         qglMatrixMode(GL_PROJECTION);CHECKGLERROR
250         qglLoadIdentity ();CHECKGLERROR
251         qglOrtho(x1, x2, y2, y1, zNear, zFar);
252         qglMatrixMode(GL_MODELVIEW);CHECKGLERROR
253         GL_SetupView_Orientation_Identity();
254 }
255
256 typedef struct gltextureunit_s
257 {
258         unsigned int t1d, t2d, t3d, tcubemap;
259         unsigned int arrayenabled;
260         float rgbscale;
261         // FIXME: add more combine stuff
262 }
263 gltextureunit_t;
264
265 static struct
266 {
267         int blendfunc1;
268         int blendfunc2;
269         int blend;
270         GLboolean depthmask;
271         int depthdisable;
272         int unit;
273         int clientunit;
274         gltextureunit_t units[MAX_TEXTUREUNITS];
275         int colorarray;
276 }
277 gl_state;
278
279 void GL_SetupTextureState(void)
280 {
281         int i;
282         gltextureunit_t *unit;
283         for (i = 0;i < backendunits;i++)
284         {
285                 if (qglActiveTexture)
286                         qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
287                 if (qglClientActiveTexture)
288                         qglClientActiveTexture(GL_TEXTURE0_ARB + (gl_state.clientunit = i));CHECKGLERROR
289                 unit = gl_state.units + i;
290                 unit->t1d = 0;
291                 unit->t2d = 0;
292                 unit->t3d = 0;
293                 unit->tcubemap = 0;
294                 unit->rgbscale = 1;
295                 unit->arrayenabled = false;
296                 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
297                 if (gl_texture3d)
298                 {
299                         qglTexCoordPointer(3, GL_FLOAT, sizeof(float[4]), varray_texcoord[i]);CHECKGLERROR
300                 }
301                 else
302                 {
303                         qglTexCoordPointer(2, GL_FLOAT, sizeof(float[4]), varray_texcoord[i]);CHECKGLERROR
304                 }
305                 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
306                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
307                 if (gl_texture3d)
308                 {
309                         qglDisable(GL_TEXTURE_3D);CHECKGLERROR
310                 }
311                 if (gl_texturecubemap)
312                 {
313                         qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
314                 }
315                 if (gl_combine.integer)
316                 {
317                         qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_ARB);CHECKGLERROR
318                         qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);CHECKGLERROR
319                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);CHECKGLERROR
320                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
321                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB_ARB, GL_CONSTANT_ARB);CHECKGLERROR
322                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
323                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);CHECKGLERROR
324                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB_ARB, GL_SRC_ALPHA);CHECKGLERROR
325                         qglTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_MODULATE);CHECKGLERROR
326                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_TEXTURE);CHECKGLERROR
327                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA_ARB, GL_PREVIOUS_ARB);CHECKGLERROR
328                         qglTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA_ARB, GL_CONSTANT_ARB);CHECKGLERROR
329                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
330                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
331                         qglTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA_ARB, GL_SRC_ALPHA);CHECKGLERROR
332                         qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
333                         qglTexEnvi(GL_TEXTURE_ENV, GL_ALPHA_SCALE, 1);CHECKGLERROR
334                 }
335                 else
336                 {
337                         qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
338                 }
339         }
340 }
341
342 void GL_Backend_ResetState(void)
343 {
344         memset(&gl_state, 0, sizeof(gl_state));
345         gl_state.depthdisable = false;
346         gl_state.blendfunc1 = GL_ONE;
347         gl_state.blendfunc2 = GL_ZERO;
348         gl_state.blend = false;
349         gl_state.depthmask = GL_TRUE;
350         gl_state.colorarray = false;
351
352         qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
353         qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
354
355         qglEnable(GL_CULL_FACE);CHECKGLERROR
356         qglCullFace(GL_FRONT);CHECKGLERROR
357         qglEnable(GL_DEPTH_TEST);CHECKGLERROR
358         qglBlendFunc(gl_state.blendfunc1, gl_state.blendfunc2);CHECKGLERROR
359         qglDisable(GL_BLEND);CHECKGLERROR
360         qglDepthMask(gl_state.depthmask);CHECKGLERROR
361         qglVertexPointer(3, GL_FLOAT, sizeof(float[4]), varray_vertex);CHECKGLERROR
362         qglEnableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
363         if (gl_mesh_floatcolors.integer)
364         {
365                 qglColorPointer(4, GL_FLOAT, sizeof(float[4]), varray_color);CHECKGLERROR
366         }
367         else
368         {
369                 qglColorPointer(4, GL_UNSIGNED_BYTE, sizeof(qbyte[4]), varray_bcolor);CHECKGLERROR
370         }
371         GL_Color(1, 1, 1, 1);
372
373         GL_SetupTextureState();
374 }
375
376 void GL_UseColorArray(void)
377 {
378         if (!gl_state.colorarray)
379         {
380                 gl_state.colorarray = true;
381                 qglEnableClientState(GL_COLOR_ARRAY);CHECKGLERROR
382         }
383 }
384
385 void GL_Color(float cr, float cg, float cb, float ca)
386 {
387         if (gl_state.colorarray)
388         {
389                 gl_state.colorarray = false;
390                 qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
391         }
392         qglColor4f(cr, cg, cb, ca);
393 }
394
395 // called at beginning of frame
396 void R_Mesh_Start(void)
397 {
398         BACKENDACTIVECHECK
399
400         CHECKGLERROR
401
402         GL_Backend_CheckCvars();
403         if (mesh_maxverts != gl_mesh_maxverts.integer)
404                 GL_Backend_ResizeArrays(gl_mesh_maxverts.integer);
405
406         GL_Backend_ResetState();
407 }
408
409 int gl_backend_rebindtextures;
410
411 void GL_ConvertColorsFloatToByte(int numverts)
412 {
413         int i, k, total;
414         // LordHavoc: to avoid problems with aliasing (treating memory as two
415         // different types - exactly what this is doing), these must be volatile
416         // (or a union)
417         volatile int *icolor;
418         volatile float *fcolor;
419         qbyte *bcolor;
420
421         total = numverts * 4;
422
423         // shift float to have 8bit fraction at base of number
424         fcolor = varray_color;
425         for (i = 0;i < total;)
426         {
427                 fcolor[i    ] += 32768.0f;
428                 fcolor[i + 1] += 32768.0f;
429                 fcolor[i + 2] += 32768.0f;
430                 fcolor[i + 3] += 32768.0f;
431                 i += 4;
432         }
433
434         // then read as integer and kill float bits...
435         icolor = (int *)varray_color;
436         bcolor = varray_bcolor;
437         for (i = 0;i < total;)
438         {
439                 k = icolor[i    ] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i    ] = (qbyte) k;
440                 k = icolor[i + 1] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i + 1] = (qbyte) k;
441                 k = icolor[i + 2] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i + 2] = (qbyte) k;
442                 k = icolor[i + 3] & 0x7FFFFF;if (k > 255) k = 255;bcolor[i + 3] = (qbyte) k;
443                 i += 4;
444         }
445 }
446
447 void GL_DrawRangeElements(int firstvert, int endvert, int indexcount, int *index)
448 {
449         int arraylocked = false;
450         c_meshs++;
451         c_meshelements += indexcount;
452         if (gl_supportslockarrays && gl_lockarrays.integer)
453         {
454                 qglLockArraysEXT(firstvert, endvert - firstvert);
455                 CHECKGLERROR
456                 arraylocked = true;
457         }
458         if (gl_mesh_drawrangeelements.integer && qglDrawRangeElements != NULL)
459                 qglDrawRangeElements(GL_TRIANGLES, firstvert, endvert, indexcount, GL_UNSIGNED_INT, (GLuint *) index);
460         else
461                 qglDrawElements(GL_TRIANGLES, indexcount, GL_UNSIGNED_INT, (GLuint *) index);
462         CHECKGLERROR
463         if (arraylocked)
464         {
465                 qglUnlockArraysEXT();
466                 CHECKGLERROR
467                 arraylocked = false;
468         }
469 }
470
471 // enlarges geometry buffers if they are too small
472 void _R_Mesh_ResizeCheck(int numverts)
473 {
474         if (numverts > mesh_maxverts)
475         {
476                 BACKENDACTIVECHECK
477                 GL_Backend_ResizeArrays(numverts + 100);
478                 GL_Backend_ResetState();
479         }
480 }
481
482 // renders the mesh
483 void R_Mesh_Draw(int numverts, int numtriangles, int *elements)
484 {
485         BACKENDACTIVECHECK
486
487         CHECKGLERROR
488
489         if (gl_state.colorarray && !gl_mesh_floatcolors.integer)
490                 GL_ConvertColorsFloatToByte(numverts);
491         if (!r_render.integer)
492                 return;
493         GL_DrawRangeElements(0, numverts, numtriangles * 3, elements);
494 }
495
496 // restores backend state, used when done with 3D rendering
497 void R_Mesh_Finish(void)
498 {
499         int i;
500         BACKENDACTIVECHECK
501
502         for (i = backendunits - 1;i >= 0;i--)
503         {
504                 if (qglActiveTexture)
505                         qglActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
506                 if (qglClientActiveTexture)
507                         qglClientActiveTexture(GL_TEXTURE0_ARB + i);CHECKGLERROR
508                 qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
509                 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
510                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
511                 if (gl_texture3d)
512                 {
513                         qglDisable(GL_TEXTURE_3D);CHECKGLERROR
514                 }
515                 if (gl_texturecubemap)
516                 {
517                         qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
518                 }
519                 qglTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);CHECKGLERROR
520                 if (gl_combine.integer)
521                 {
522                         qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, 1);CHECKGLERROR
523                 }
524         }
525         qglDisableClientState(GL_COLOR_ARRAY);CHECKGLERROR
526         qglDisableClientState(GL_VERTEX_ARRAY);CHECKGLERROR
527
528         qglDisable(GL_BLEND);CHECKGLERROR
529         qglEnable(GL_DEPTH_TEST);CHECKGLERROR
530         qglDepthMask(GL_TRUE);CHECKGLERROR
531         qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);CHECKGLERROR
532 }
533
534 void R_Mesh_Matrix(const matrix4x4_t *matrix)
535 {
536         if (memcmp(matrix, &backend_modelmatrix, sizeof(matrix4x4_t)))
537         {
538                 backend_modelmatrix = *matrix;
539                 Matrix4x4_Concat(&backend_modelviewmatrix, &backend_viewmatrix, matrix);
540                 Matrix4x4_Transpose(&backend_glmodelviewmatrix, &backend_modelviewmatrix);
541                 qglLoadMatrixf(&backend_glmodelviewmatrix.m[0][0]);
542         }
543 }
544
545 // sets up the requested state
546 void R_Mesh_MainState(const rmeshstate_t *m)
547 {
548         BACKENDACTIVECHECK
549
550         if (gl_state.blendfunc1 != m->blendfunc1 || gl_state.blendfunc2 != m->blendfunc2)
551         {
552                 qglBlendFunc(gl_state.blendfunc1 = m->blendfunc1, gl_state.blendfunc2 = m->blendfunc2);CHECKGLERROR
553                 if (gl_state.blendfunc2 == GL_ZERO)
554                 {
555                         if (gl_state.blendfunc1 == GL_ONE)
556                         {
557                                 if (gl_state.blend)
558                                 {
559                                         gl_state.blend = 0;
560                                         qglDisable(GL_BLEND);CHECKGLERROR
561                                 }
562                         }
563                         else
564                         {
565                                 if (!gl_state.blend)
566                                 {
567                                         gl_state.blend = 1;
568                                         qglEnable(GL_BLEND);CHECKGLERROR
569                                 }
570                         }
571                 }
572                 else
573                 {
574                         if (!gl_state.blend)
575                         {
576                                 gl_state.blend = 1;
577                                 qglEnable(GL_BLEND);CHECKGLERROR
578                         }
579                 }
580         }
581         if (gl_state.depthdisable != m->depthdisable)
582         {
583                 gl_state.depthdisable = m->depthdisable;
584                 if (gl_state.depthdisable)
585                         qglDisable(GL_DEPTH_TEST);
586                 else
587                         qglEnable(GL_DEPTH_TEST);
588         }
589         if (gl_state.depthmask != (m->blendfunc2 == GL_ZERO || m->depthwrite))
590         {
591                 qglDepthMask(gl_state.depthmask = (m->blendfunc2 == GL_ZERO || m->depthwrite));CHECKGLERROR
592         }
593 }
594
595 void R_Mesh_TextureState(const rmeshstate_t *m)
596 {
597         int i;
598         float rgbscale;
599         gltextureunit_t *unit;
600
601         BACKENDACTIVECHECK
602
603         if (gl_backend_rebindtextures)
604         {
605                 gl_backend_rebindtextures = false;
606                 GL_SetupTextureState();
607         }
608
609         for (i = 0;i < backendunits;i++)
610         {
611                 unit = gl_state.units + i;
612                 if (unit->t1d != m->tex1d[i] || unit->t2d != m->tex[i] || unit->t3d != m->tex3d[i] || unit->tcubemap != m->texcubemap[i])
613                 {
614                         if (gl_state.unit != i)
615                         {
616                                 qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
617                         }
618                         if (m->tex1d[i] || m->tex[i] || m->tex3d[i] || m->texcubemap[i])
619                         {
620                                 if (!unit->arrayenabled)
621                                 {
622                                         unit->arrayenabled = true;
623                                         if (gl_state.clientunit != i)
624                                         {
625                                                 qglClientActiveTexture(GL_TEXTURE0_ARB + (gl_state.clientunit = i));CHECKGLERROR
626                                         }
627                                         qglEnableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
628                                 }
629                         }
630                         else
631                         {
632                                 if (unit->arrayenabled)
633                                 {
634                                         unit->arrayenabled = false;
635                                         if (gl_state.clientunit != i)
636                                         {
637                                                 qglClientActiveTexture(GL_TEXTURE0_ARB + (gl_state.clientunit = i));CHECKGLERROR
638                                         }
639                                         qglDisableClientState(GL_TEXTURE_COORD_ARRAY);CHECKGLERROR
640                                 }
641                         }
642                         if (unit->t1d != m->tex1d[i])
643                         {
644                                 if (m->tex1d[i])
645                                 {
646                                         if (unit->t1d == 0)
647                                                 qglEnable(GL_TEXTURE_1D);CHECKGLERROR
648                                 }
649                                 else
650                                 {
651                                         if (unit->t1d)
652                                                 qglDisable(GL_TEXTURE_1D);CHECKGLERROR
653                                 }
654                                 qglBindTexture(GL_TEXTURE_1D, (unit->t1d = m->tex1d[i]));CHECKGLERROR
655                         }
656                         if (unit->t2d != m->tex[i])
657                         {
658                                 if (m->tex[i])
659                                 {
660                                         if (unit->t2d == 0)
661                                                 qglEnable(GL_TEXTURE_2D);CHECKGLERROR
662                                 }
663                                 else
664                                 {
665                                         if (unit->t2d)
666                                                 qglDisable(GL_TEXTURE_2D);CHECKGLERROR
667                                 }
668                                 qglBindTexture(GL_TEXTURE_2D, (unit->t2d = m->tex[i]));CHECKGLERROR
669                         }
670                         if (unit->t3d != m->tex3d[i])
671                         {
672                                 if (m->tex3d[i])
673                                 {
674                                         if (unit->t3d == 0)
675                                                 qglEnable(GL_TEXTURE_3D);CHECKGLERROR
676                                 }
677                                 else
678                                 {
679                                         if (unit->t3d)
680                                                 qglDisable(GL_TEXTURE_3D);CHECKGLERROR
681                                 }
682                                 qglBindTexture(GL_TEXTURE_3D, (unit->t3d = m->tex3d[i]));CHECKGLERROR
683                         }
684                         if (unit->tcubemap != m->texcubemap[i])
685                         {
686                                 if (m->texcubemap[i])
687                                 {
688                                         if (unit->tcubemap == 0)
689                                                 qglEnable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
690                                 }
691                                 else
692                                 {
693                                         if (unit->tcubemap)
694                                                 qglDisable(GL_TEXTURE_CUBE_MAP_ARB);CHECKGLERROR
695                                 }
696                                 qglBindTexture(GL_TEXTURE_CUBE_MAP_ARB, (unit->tcubemap = m->texcubemap[i]));CHECKGLERROR
697                         }
698                 }
699                 rgbscale = max(m->texrgbscale[i], 1);
700                 if (gl_state.units[i].rgbscale != rgbscale)
701                 {
702                         if (gl_state.unit != i)
703                         {
704                                 qglActiveTexture(GL_TEXTURE0_ARB + (gl_state.unit = i));CHECKGLERROR
705                         }
706                         qglTexEnvi(GL_TEXTURE_ENV, GL_RGB_SCALE_ARB, (gl_state.units[i].rgbscale = rgbscale));CHECKGLERROR
707                 }
708         }
709 }
710
711 void R_Mesh_State(const rmeshstate_t *m)
712 {
713         R_Mesh_MainState(m);
714         R_Mesh_TextureState(m);
715 }
716
717 /*
718 ==============================================================================
719
720                                                 SCREEN SHOTS
721
722 ==============================================================================
723 */
724
725 qboolean SCR_ScreenShot(char *filename, int x, int y, int width, int height)
726 {
727         qboolean ret;
728         int i;
729         qbyte *buffer;
730
731         if (!r_render.integer)
732                 return false;
733
734         buffer = Mem_Alloc(tempmempool, width*height*3);
735         qglReadPixels (x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, buffer);
736         CHECKGLERROR
737
738         // LordHavoc: compensate for v_overbrightbits when using hardware gamma
739         if (v_hwgamma.integer)
740                 for (i = 0;i < width * height * 3;i++)
741                         buffer[i] <<= v_overbrightbits.integer;
742
743         ret = Image_WriteTGARGB_preflipped(filename, width, height, buffer);
744
745         Mem_Free(buffer);
746         return ret;
747 }
748
749 //=============================================================================
750
751 void R_ClearScreen(void)
752 {
753         if (r_render.integer)
754         {
755                 // clear to black
756                 qglClearColor(0,0,0,0);CHECKGLERROR
757                 // clear the screen
758                 qglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);CHECKGLERROR
759                 // set dithering mode
760                 if (gl_dither.integer)
761                 {
762                         qglEnable(GL_DITHER);CHECKGLERROR
763                 }
764                 else
765                 {
766                         qglDisable(GL_DITHER);CHECKGLERROR
767                 }
768         }
769 }
770
771 /*
772 ==================
773 SCR_UpdateScreen
774
775 This is called every frame, and can also be called explicitly to flush
776 text to the screen.
777 ==================
778 */
779 void SCR_UpdateScreen (void)
780 {
781         VID_Finish ();
782
783         R_TimeReport("finish");
784
785         if (r_textureunits.integer > gl_textureunits)
786                 Cvar_SetValueQuick(&r_textureunits, gl_textureunits);
787         if (r_textureunits.integer < 1)
788                 Cvar_SetValueQuick(&r_textureunits, 1);
789
790         if (gl_combine.integer && (!gl_combine_extension || r_textureunits.integer < 2))
791                 Cvar_SetValueQuick(&gl_combine, 0);
792
793         // lighting scale
794         r_colorscale = 1.0f / (float) (1 << v_overbrightbits.integer);
795
796         // lightmaps only
797         r_lightmapscalebit = v_overbrightbits.integer;
798         if (gl_combine.integer && r_textureunits.integer > 1)
799                 r_lightmapscalebit += 2;
800
801         R_TimeReport("setup");
802
803         R_ClearScreen();
804
805         R_TimeReport("clear");
806
807         if (scr_conlines < vid.conheight && cls.signon == SIGNONS)
808                 R_RenderView();
809
810         // draw 2D stuff
811         R_DrawQueue();
812
813         // tell driver to commit it's partially full geometry queue to the rendering queue
814         // (this doesn't wait for the commands themselves to complete)
815         qglFlush();
816 }
817