]> icculus.org git repositories - divverent/darkplaces.git/blob - gl_rmain.c
changed cl_ignoremousemove from a qboolean to an int named
[divverent/darkplaces.git] / gl_rmain.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // r_main.c
21
22 #include "quakedef.h"
23 #include "cl_dyntexture.h"
24 #include "r_shadow.h"
25 #include "polygon.h"
26 #include "image.h"
27
28 mempool_t *r_main_mempool;
29 rtexturepool_t *r_main_texturepool;
30
31 //
32 // screen size info
33 //
34 r_refdef_t r_refdef;
35 r_view_t r_view;
36 r_viewcache_t r_viewcache;
37
38 cvar_t r_depthfirst = {CVAR_SAVE, "r_depthfirst", "1", "renders a depth-only version of the scene before normal rendering begins to eliminate overdraw, values: 0 = off, 1 = world depth, 2 = world and model depth"};
39 cvar_t r_nearclip = {0, "r_nearclip", "1", "distance from camera of nearclip plane" };
40 cvar_t r_showbboxes = {0, "r_showbboxes", "0", "shows bounding boxes of server entities, value controls opacity scaling (1 = 10%,  10 = 100%)"};
41 cvar_t r_showsurfaces = {0, "r_showsurfaces", "0", "1 shows surfaces as different colors, or a value of 2 shows triangle draw order (for analyzing whether meshes are optimized for vertex cache)"};
42 cvar_t r_showtris = {0, "r_showtris", "0", "shows triangle outlines, value controls brightness (can be above 1)"};
43 cvar_t r_shownormals = {0, "r_shownormals", "0", "shows per-vertex surface normals and tangent vectors for bumpmapped lighting"};
44 cvar_t r_showlighting = {0, "r_showlighting", "0", "shows areas lit by lights, useful for finding out why some areas of a map render slowly (bright orange = lots of passes = slow), a value of 2 disables depth testing which can be interesting but not very useful"};
45 cvar_t r_showshadowvolumes = {0, "r_showshadowvolumes", "0", "shows areas shadowed by lights, useful for finding out why some areas of a map render slowly (bright blue = lots of passes = slow), a value of 2 disables depth testing which can be interesting but not very useful"};
46 cvar_t r_showcollisionbrushes = {0, "r_showcollisionbrushes", "0", "draws collision brushes in quake3 maps (mode 1), mode 2 disables rendering of world (trippy!)"};
47 cvar_t r_showcollisionbrushes_polygonfactor = {0, "r_showcollisionbrushes_polygonfactor", "-1", "expands outward the brush polygons a little bit, used to make collision brushes appear infront of walls"};
48 cvar_t r_showcollisionbrushes_polygonoffset = {0, "r_showcollisionbrushes_polygonoffset", "0", "nudges brush polygon depth in hardware depth units, used to make collision brushes appear infront of walls"};
49 cvar_t r_showdisabledepthtest = {0, "r_showdisabledepthtest", "0", "disables depth testing on r_show* cvars, allowing you to see what hidden geometry the graphics card is processing"};
50 cvar_t r_drawportals = {0, "r_drawportals", "0", "shows portals (separating polygons) in world interior in quake1 maps"};
51 cvar_t r_drawentities = {0, "r_drawentities","1", "draw entities (doors, players, projectiles, etc)"};
52 cvar_t r_drawviewmodel = {0, "r_drawviewmodel","1", "draw your weapon model"};
53 cvar_t r_cullentities_trace = {0, "r_cullentities_trace", "1", "probabistically cull invisible entities"};
54 cvar_t r_cullentities_trace_samples = {0, "r_cullentities_trace_samples", "2", "number of samples to test for entity culling"};
55 cvar_t r_cullentities_trace_enlarge = {0, "r_cullentities_trace_enlarge", "0", "box enlargement for entity culling"};
56 cvar_t r_cullentities_trace_delay = {0, "r_cullentities_trace_delay", "1", "number of seconds until the entity gets actually culled"};
57 cvar_t r_speeds = {0, "r_speeds","0", "displays rendering statistics and per-subsystem timings"};
58 cvar_t r_fullbright = {0, "r_fullbright","0", "makes map very bright and renders faster"};
59 cvar_t r_wateralpha = {CVAR_SAVE, "r_wateralpha","1", "opacity of water polygons"};
60 cvar_t r_dynamic = {CVAR_SAVE, "r_dynamic","1", "enables dynamic lights (rocket glow and such)"};
61 cvar_t r_fullbrights = {CVAR_SAVE, "r_fullbrights", "1", "enables glowing pixels in quake textures (changes need r_restart to take effect)"};
62 cvar_t r_shadows = {CVAR_SAVE, "r_shadows", "0", "casts fake stencil shadows from models onto the world (rtlights are unaffected by this)"};
63 cvar_t r_shadows_throwdistance = {CVAR_SAVE, "r_shadows_throwdistance", "500", "how far to cast shadows from models"};
64 cvar_t r_q1bsp_skymasking = {0, "r_q1bsp_skymasking", "1", "allows sky polygons in quake1 maps to obscure other geometry"};
65 cvar_t r_polygonoffset_submodel_factor = {0, "r_polygonoffset_submodel_factor", "0", "biases depth values of world submodels such as doors, to prevent z-fighting artifacts in Quake maps"};
66 cvar_t r_polygonoffset_submodel_offset = {0, "r_polygonoffset_submodel_offset", "2", "biases depth values of world submodels such as doors, to prevent z-fighting artifacts in Quake maps"};
67 cvar_t r_fog_exp2 = {0, "r_fog_exp2", "0", "uses GL_EXP2 fog (as in Nehahra) rather than realistic GL_EXP fog"};
68
69 cvar_t gl_fogenable = {0, "gl_fogenable", "0", "nehahra fog enable (for Nehahra compatibility only)"};
70 cvar_t gl_fogdensity = {0, "gl_fogdensity", "0.25", "nehahra fog density (recommend values below 0.1) (for Nehahra compatibility only)"};
71 cvar_t gl_fogred = {0, "gl_fogred","0.3", "nehahra fog color red value (for Nehahra compatibility only)"};
72 cvar_t gl_foggreen = {0, "gl_foggreen","0.3", "nehahra fog color green value (for Nehahra compatibility only)"};
73 cvar_t gl_fogblue = {0, "gl_fogblue","0.3", "nehahra fog color blue value (for Nehahra compatibility only)"};
74 cvar_t gl_fogstart = {0, "gl_fogstart", "0", "nehahra fog start distance (for Nehahra compatibility only)"};
75 cvar_t gl_fogend = {0, "gl_fogend","0", "nehahra fog end distance (for Nehahra compatibility only)"};
76 cvar_t gl_skyclip = {0, "gl_skyclip", "4608", "nehahra farclip distance - the real fog end (for Nehahra compatibility only)"};
77
78 cvar_t r_textureunits = {0, "r_textureunits", "32", "number of hardware texture units reported by driver (note: setting this to 1 turns off gl_combine)"};
79
80 cvar_t r_glsl = {CVAR_SAVE, "r_glsl", "1", "enables use of OpenGL 2.0 pixel shaders for lighting"};
81 cvar_t r_glsl_offsetmapping = {CVAR_SAVE, "r_glsl_offsetmapping", "0", "offset mapping effect (also known as parallax mapping or virtual displacement mapping)"};
82 cvar_t r_glsl_offsetmapping_reliefmapping = {CVAR_SAVE, "r_glsl_offsetmapping_reliefmapping", "0", "relief mapping effect (higher quality)"};
83 cvar_t r_glsl_offsetmapping_scale = {CVAR_SAVE, "r_glsl_offsetmapping_scale", "0.04", "how deep the offset mapping effect is"};
84 cvar_t r_glsl_deluxemapping = {CVAR_SAVE, "r_glsl_deluxemapping", "1", "use per pixel lighting on deluxemap-compiled q3bsp maps (or a value of 2 forces deluxemap shading even without deluxemaps)"};
85 cvar_t r_glsl_contrastboost = {CVAR_SAVE, "r_glsl_contrastboost", "1", "by how much to multiply the contrast in dark areas (1 is no change)"};
86
87 cvar_t r_water = {CVAR_SAVE, "r_water", "0", "whether to use reflections and refraction on water surfaces (note: r_wateralpha must be set below 1)"};
88 cvar_t r_water_clippingplanebias = {CVAR_SAVE, "r_water_clippingplanebias", "1", "a rather technical setting which avoids black pixels around water edges"};
89 cvar_t r_water_resolutionmultiplier = {CVAR_SAVE, "r_water_resolutionmultiplier", "0.5", "multiplier for screen resolution when rendering refracted/reflected scenes, 1 is full quality, lower values are faster"};
90 cvar_t r_water_refractdistort = {CVAR_SAVE, "r_water_refractdistort", "0.01", "how much water refractions shimmer"};
91 cvar_t r_water_reflectdistort = {CVAR_SAVE, "r_water_reflectdistort", "0.01", "how much water reflections shimmer"};
92
93 cvar_t r_lerpsprites = {CVAR_SAVE, "r_lerpsprites", "1", "enables animation smoothing on sprites (requires r_lerpmodels 1)"};
94 cvar_t r_lerpmodels = {CVAR_SAVE, "r_lerpmodels", "1", "enables animation smoothing on models"};
95 cvar_t r_lerplightstyles = {CVAR_SAVE, "r_lerplightstyles", "0", "enable animation smoothing on flickering lights"};
96 cvar_t r_waterscroll = {CVAR_SAVE, "r_waterscroll", "1", "makes water scroll around, value controls how much"};
97
98 cvar_t r_bloom = {CVAR_SAVE, "r_bloom", "0", "enables bloom effect (makes bright pixels affect neighboring pixels)"};
99 cvar_t r_bloom_colorscale = {CVAR_SAVE, "r_bloom_colorscale", "1", "how bright the glow is"};
100 cvar_t r_bloom_brighten = {CVAR_SAVE, "r_bloom_brighten", "2", "how bright the glow is, after subtract/power"};
101 cvar_t r_bloom_blur = {CVAR_SAVE, "r_bloom_blur", "4", "how large the glow is"};
102 cvar_t r_bloom_resolution = {CVAR_SAVE, "r_bloom_resolution", "320", "what resolution to perform the bloom effect at (independent of screen resolution)"};
103 cvar_t r_bloom_colorexponent = {CVAR_SAVE, "r_bloom_colorexponent", "1", "how exagerated the glow is"};
104 cvar_t r_bloom_colorsubtract = {CVAR_SAVE, "r_bloom_colorsubtract", "0.125", "reduces bloom colors by a certain amount"};
105
106 cvar_t r_hdr = {CVAR_SAVE, "r_hdr", "0", "enables High Dynamic Range bloom effect (higher quality version of r_bloom)"};
107 cvar_t r_hdr_scenebrightness = {CVAR_SAVE, "r_hdr_scenebrightness", "1", "global rendering brightness"};
108 cvar_t r_hdr_glowintensity = {CVAR_SAVE, "r_hdr_glowintensity", "1", "how bright light emitting textures should appear"};
109 cvar_t r_hdr_range = {CVAR_SAVE, "r_hdr_range", "4", "how much dynamic range to render bloom with (equivilant to multiplying r_bloom_brighten by this value and dividing r_bloom_colorscale by this value)"};
110
111 cvar_t r_smoothnormals_areaweighting = {0, "r_smoothnormals_areaweighting", "1", "uses significantly faster (and supposedly higher quality) area-weighted vertex normals and tangent vectors rather than summing normalized triangle normals and tangents"};
112
113 cvar_t developer_texturelogging = {0, "developer_texturelogging", "0", "produces a textures.log file containing names of skins and map textures the engine tried to load"};
114
115 cvar_t gl_lightmaps = {0, "gl_lightmaps", "0", "draws only lightmaps, no texture (for level designers)"};
116
117 cvar_t r_test = {0, "r_test", "0", "internal development use only, leave it alone (usually does nothing anyway)"};
118 cvar_t r_batchmode = {0, "r_batchmode", "1", "selects method of rendering multiple surfaces with one driver call (values are 0, 1, 2, etc...)"};
119 cvar_t r_track_sprites = {CVAR_SAVE, "r_track_sprites", "1", "track SPR_LABEL* sprites by putting them as indicator at the screen border to rotate to"};
120 cvar_t r_track_sprites_flags = {CVAR_SAVE, "r_track_sprites_flags", "1", "1: Rotate sprites accodringly, 2: Make it a continuous rotation"};
121 cvar_t r_track_sprites_scalew = {CVAR_SAVE, "r_track_sprites_scalew", "1", "width scaling of tracked sprites"};
122 cvar_t r_track_sprites_scaleh = {CVAR_SAVE, "r_track_sprites_scaleh", "1", "height scaling of tracked sprites"};
123
124 extern qboolean v_flipped_state;
125
126 typedef struct r_glsl_bloomshader_s
127 {
128         int program;
129         int loc_Texture_Bloom;
130 }
131 r_glsl_bloomshader_t;
132
133 static struct r_bloomstate_s
134 {
135         qboolean enabled;
136         qboolean hdr;
137
138         int bloomwidth, bloomheight;
139
140         int screentexturewidth, screentextureheight;
141         rtexture_t *texture_screen;
142
143         int bloomtexturewidth, bloomtextureheight;
144         rtexture_t *texture_bloom;
145
146         r_glsl_bloomshader_t *shader;
147
148         // arrays for rendering the screen passes
149         float screentexcoord2f[8];
150         float bloomtexcoord2f[8];
151         float offsettexcoord2f[8];
152 }
153 r_bloomstate;
154
155 typedef struct r_waterstate_waterplane_s
156 {
157         rtexture_t *texture_refraction;
158         rtexture_t *texture_reflection;
159         mplane_t plane;
160         int materialflags; // combined flags of all water surfaces on this plane
161         unsigned char pvsbits[(32768+7)>>3]; // FIXME: buffer overflow on huge maps
162         qboolean pvsvalid;
163 }
164 r_waterstate_waterplane_t;
165
166 #define MAX_WATERPLANES 16
167
168 static struct r_waterstate_s
169 {
170         qboolean enabled;
171
172         qboolean renderingscene; // true while rendering a refraction or reflection texture, disables water surfaces
173
174         int waterwidth, waterheight;
175         int texturewidth, textureheight;
176
177         int maxwaterplanes; // same as MAX_WATERPLANES
178         int numwaterplanes;
179         r_waterstate_waterplane_t waterplanes[MAX_WATERPLANES];
180
181         float screenscale[2];
182         float screencenter[2];
183 }
184 r_waterstate;
185
186 // shadow volume bsp struct with automatically growing nodes buffer
187 svbsp_t r_svbsp;
188
189 rtexture_t *r_texture_blanknormalmap;
190 rtexture_t *r_texture_white;
191 rtexture_t *r_texture_grey128;
192 rtexture_t *r_texture_black;
193 rtexture_t *r_texture_notexture;
194 rtexture_t *r_texture_whitecube;
195 rtexture_t *r_texture_normalizationcube;
196 rtexture_t *r_texture_fogattenuation;
197 //rtexture_t *r_texture_fogintensity;
198
199 char r_qwskincache[MAX_SCOREBOARD][MAX_QPATH];
200 skinframe_t *r_qwskincache_skinframe[MAX_SCOREBOARD];
201
202 // vertex coordinates for a quad that covers the screen exactly
203 const static float r_screenvertex3f[12] =
204 {
205         0, 0, 0,
206         1, 0, 0,
207         1, 1, 0,
208         0, 1, 0
209 };
210
211 extern void R_DrawModelShadows(void);
212
213 void R_ModulateColors(float *in, float *out, int verts, float r, float g, float b)
214 {
215         int i;
216         for (i = 0;i < verts;i++)
217         {
218                 out[0] = in[0] * r;
219                 out[1] = in[1] * g;
220                 out[2] = in[2] * b;
221                 out[3] = in[3];
222                 in += 4;
223                 out += 4;
224         }
225 }
226
227 void R_FillColors(float *out, int verts, float r, float g, float b, float a)
228 {
229         int i;
230         for (i = 0;i < verts;i++)
231         {
232                 out[0] = r;
233                 out[1] = g;
234                 out[2] = b;
235                 out[3] = a;
236                 out += 4;
237         }
238 }
239
240 // FIXME: move this to client?
241 void FOG_clear(void)
242 {
243         if (gamemode == GAME_NEHAHRA)
244         {
245                 Cvar_Set("gl_fogenable", "0");
246                 Cvar_Set("gl_fogdensity", "0.2");
247                 Cvar_Set("gl_fogred", "0.3");
248                 Cvar_Set("gl_foggreen", "0.3");
249                 Cvar_Set("gl_fogblue", "0.3");
250         }
251         r_refdef.fog_density = 0;
252         r_refdef.fog_red = 0;
253         r_refdef.fog_green = 0;
254         r_refdef.fog_blue = 0;
255         r_refdef.fog_alpha = 1;
256         r_refdef.fog_start = 0;
257         r_refdef.fog_end = 0;
258 }
259
260 float FogForDistance(vec_t dist)
261 {
262         unsigned int fogmasktableindex = (unsigned int)(dist * r_refdef.fogmasktabledistmultiplier);
263         return r_refdef.fogmasktable[min(fogmasktableindex, FOGMASKTABLEWIDTH - 1)];
264 }
265
266 float FogPoint_World(const vec3_t p)
267 {
268         return FogForDistance(VectorDistance((p), r_view.origin));
269 }
270
271 float FogPoint_Model(const vec3_t p)
272 {
273         return FogForDistance(VectorDistance((p), rsurface.modelorg));
274 }
275
276 static void R_BuildBlankTextures(void)
277 {
278         unsigned char data[4];
279         data[2] = 128; // normal X
280         data[1] = 128; // normal Y
281         data[0] = 255; // normal Z
282         data[3] = 128; // height
283         r_texture_blanknormalmap = R_LoadTexture2D(r_main_texturepool, "blankbump", 1, 1, data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_PERSISTENT, NULL);
284         data[0] = 255;
285         data[1] = 255;
286         data[2] = 255;
287         data[3] = 255;
288         r_texture_white = R_LoadTexture2D(r_main_texturepool, "blankwhite", 1, 1, data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_PERSISTENT, NULL);
289         data[0] = 128;
290         data[1] = 128;
291         data[2] = 128;
292         data[3] = 255;
293         r_texture_grey128 = R_LoadTexture2D(r_main_texturepool, "blankgrey128", 1, 1, data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_PERSISTENT, NULL);
294         data[0] = 0;
295         data[1] = 0;
296         data[2] = 0;
297         data[3] = 255;
298         r_texture_black = R_LoadTexture2D(r_main_texturepool, "blankblack", 1, 1, data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_PERSISTENT, NULL);
299 }
300
301 static void R_BuildNoTexture(void)
302 {
303         int x, y;
304         unsigned char pix[16][16][4];
305         // this makes a light grey/dark grey checkerboard texture
306         for (y = 0;y < 16;y++)
307         {
308                 for (x = 0;x < 16;x++)
309                 {
310                         if ((y < 8) ^ (x < 8))
311                         {
312                                 pix[y][x][0] = 128;
313                                 pix[y][x][1] = 128;
314                                 pix[y][x][2] = 128;
315                                 pix[y][x][3] = 255;
316                         }
317                         else
318                         {
319                                 pix[y][x][0] = 64;
320                                 pix[y][x][1] = 64;
321                                 pix[y][x][2] = 64;
322                                 pix[y][x][3] = 255;
323                         }
324                 }
325         }
326         r_texture_notexture = R_LoadTexture2D(r_main_texturepool, "notexture", 16, 16, &pix[0][0][0], TEXTYPE_BGRA, TEXF_MIPMAP | TEXF_PERSISTENT, NULL);
327 }
328
329 static void R_BuildWhiteCube(void)
330 {
331         unsigned char data[6*1*1*4];
332         memset(data, 255, sizeof(data));
333         r_texture_whitecube = R_LoadTextureCubeMap(r_main_texturepool, "whitecube", 1, data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_PERSISTENT, NULL);
334 }
335
336 static void R_BuildNormalizationCube(void)
337 {
338         int x, y, side;
339         vec3_t v;
340         vec_t s, t, intensity;
341 #define NORMSIZE 64
342         unsigned char data[6][NORMSIZE][NORMSIZE][4];
343         for (side = 0;side < 6;side++)
344         {
345                 for (y = 0;y < NORMSIZE;y++)
346                 {
347                         for (x = 0;x < NORMSIZE;x++)
348                         {
349                                 s = (x + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
350                                 t = (y + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
351                                 switch(side)
352                                 {
353                                 default:
354                                 case 0:
355                                         v[0] = 1;
356                                         v[1] = -t;
357                                         v[2] = -s;
358                                         break;
359                                 case 1:
360                                         v[0] = -1;
361                                         v[1] = -t;
362                                         v[2] = s;
363                                         break;
364                                 case 2:
365                                         v[0] = s;
366                                         v[1] = 1;
367                                         v[2] = t;
368                                         break;
369                                 case 3:
370                                         v[0] = s;
371                                         v[1] = -1;
372                                         v[2] = -t;
373                                         break;
374                                 case 4:
375                                         v[0] = s;
376                                         v[1] = -t;
377                                         v[2] = 1;
378                                         break;
379                                 case 5:
380                                         v[0] = -s;
381                                         v[1] = -t;
382                                         v[2] = -1;
383                                         break;
384                                 }
385                                 intensity = 127.0f / sqrt(DotProduct(v, v));
386                                 data[side][y][x][2] = (unsigned char)(128.0f + intensity * v[0]);
387                                 data[side][y][x][1] = (unsigned char)(128.0f + intensity * v[1]);
388                                 data[side][y][x][0] = (unsigned char)(128.0f + intensity * v[2]);
389                                 data[side][y][x][3] = 255;
390                         }
391                 }
392         }
393         r_texture_normalizationcube = R_LoadTextureCubeMap(r_main_texturepool, "normalcube", NORMSIZE, &data[0][0][0][0], TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_PERSISTENT, NULL);
394 }
395
396 static void R_BuildFogTexture(void)
397 {
398         int x, b;
399 #define FOGWIDTH 256
400         unsigned char data1[FOGWIDTH][4];
401         //unsigned char data2[FOGWIDTH][4];
402         double d, r, alpha;
403
404         r_refdef.fogmasktable_start = r_refdef.fog_start;
405         r_refdef.fogmasktable_alpha = r_refdef.fog_alpha;
406         r_refdef.fogmasktable_range = r_refdef.fogrange;
407         r_refdef.fogmasktable_density = r_refdef.fog_density;
408
409         r = r_refdef.fogmasktable_range / FOGMASKTABLEWIDTH;
410         for (x = 0;x < FOGMASKTABLEWIDTH;x++)
411         {
412                 d = (x * r - r_refdef.fogmasktable_start);
413                 Con_Printf("%f ", d);
414                 d = max(0, d);
415                 if (r_fog_exp2.integer)
416                         alpha = exp(-r_refdef.fogmasktable_density * r_refdef.fogmasktable_density * 0.0001 * d * d);
417                 else
418                         alpha = exp(-r_refdef.fogmasktable_density * 0.004 * d);
419                 Con_Printf(" : %f ", alpha);
420                 alpha = 1 - (1 - alpha) * r_refdef.fogmasktable_alpha;
421                 Con_Printf(" = %f\n", alpha);
422                 r_refdef.fogmasktable[x] = bound(0, alpha, 1);
423         }
424
425         for (x = 0;x < FOGWIDTH;x++)
426         {
427                 b = (int)(r_refdef.fogmasktable[x * (FOGMASKTABLEWIDTH - 1) / (FOGWIDTH - 1)] * 255);
428                 data1[x][0] = b;
429                 data1[x][1] = b;
430                 data1[x][2] = b;
431                 data1[x][3] = 255;
432                 //data2[x][0] = 255 - b;
433                 //data2[x][1] = 255 - b;
434                 //data2[x][2] = 255 - b;
435                 //data2[x][3] = 255;
436         }
437         if (r_texture_fogattenuation)
438         {
439                 R_UpdateTexture(r_texture_fogattenuation, &data1[0][0], 0, 0, FOGWIDTH, 1);
440                 //R_UpdateTexture(r_texture_fogattenuation, &data2[0][0], 0, 0, FOGWIDTH, 1);
441         }
442         else
443         {
444                 r_texture_fogattenuation = R_LoadTexture2D(r_main_texturepool, "fogattenuation", FOGWIDTH, 1, &data1[0][0], TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_FORCELINEAR | TEXF_CLAMP | TEXF_PERSISTENT, NULL);
445                 //r_texture_fogintensity = R_LoadTexture2D(r_main_texturepool, "fogintensity", FOGWIDTH, 1, &data2[0][0], TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_FORCELINEAR | TEXF_CLAMP, NULL);
446         }
447 }
448
449 static const char *builtinshaderstring =
450 "// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader\n"
451 "// written by Forest 'LordHavoc' Hale\n"
452 "\n"
453 "// common definitions between vertex shader and fragment shader:\n"
454 "\n"
455 "#ifdef __GLSL_CG_DATA_TYPES\n"
456 "# define myhalf half\n"
457 "# define myhvec2 hvec2\n"
458 "# define myhvec3 hvec3\n"
459 "# define myhvec4 hvec4\n"
460 "#else\n"
461 "# define myhalf float\n"
462 "# define myhvec2 vec2\n"
463 "# define myhvec3 vec3\n"
464 "# define myhvec4 vec4\n"
465 "#endif\n"
466 "\n"
467 "varying vec2 TexCoord;\n"
468 "varying vec2 TexCoordLightmap;\n"
469 "\n"
470 "//#ifdef MODE_LIGHTSOURCE\n"
471 "varying vec3 CubeVector;\n"
472 "//#endif\n"
473 "\n"
474 "//#ifdef MODE_LIGHTSOURCE\n"
475 "varying vec3 LightVector;\n"
476 "//#else\n"
477 "//# ifdef MODE_LIGHTDIRECTION\n"
478 "//varying vec3 LightVector;\n"
479 "//# endif\n"
480 "//#endif\n"
481 "\n"
482 "varying vec3 EyeVector;\n"
483 "//#ifdef USEFOG\n"
484 "varying vec3 EyeVectorModelSpace;\n"
485 "//#endif\n"
486 "\n"
487 "varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent)\n"
488 "varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal)\n"
489 "varying vec3 VectorR; // direction of R texcoord (surface normal)\n"
490 "\n"
491 "//#ifdef MODE_WATER\n"
492 "varying vec4 ModelViewProjectionPosition;\n"
493 "//#else\n"
494 "//# ifdef MODE_REFRACTION\n"
495 "//varying vec4 ModelViewProjectionPosition;\n"
496 "//# else\n"
497 "//#  ifdef USEREFLECTION\n"
498 "//varying vec4 ModelViewProjectionPosition;\n"
499 "//#  endif\n"
500 "//# endif\n"
501 "//#endif\n"
502 "\n"
503 "\n"
504 "\n"
505 "\n"
506 "\n"
507 "// vertex shader specific:\n"
508 "#ifdef VERTEX_SHADER\n"
509 "\n"
510 "uniform vec3 LightPosition;\n"
511 "uniform vec3 EyePosition;\n"
512 "uniform vec3 LightDir;\n"
513 "\n"
514 "// TODO: get rid of tangentt (texcoord2) and use a crossproduct to regenerate it from tangents (texcoord1) and normal (texcoord3)\n"
515 "\n"
516 "void main(void)\n"
517 "{\n"
518 "       gl_FrontColor = gl_Color;\n"
519 "       // copy the surface texcoord\n"
520 "       TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0);\n"
521 "#ifndef MODE_LIGHTSOURCE\n"
522 "# ifndef MODE_LIGHTDIRECTION\n"
523 "       TexCoordLightmap = vec2(gl_MultiTexCoord4);\n"
524 "# endif\n"
525 "#endif\n"
526 "\n"
527 "#ifdef MODE_LIGHTSOURCE\n"
528 "       // transform vertex position into light attenuation/cubemap space\n"
529 "       // (-1 to +1 across the light box)\n"
530 "       CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex);\n"
531 "\n"
532 "       // transform unnormalized light direction into tangent space\n"
533 "       // (we use unnormalized to ensure that it interpolates correctly and then\n"
534 "       //  normalize it per pixel)\n"
535 "       vec3 lightminusvertex = LightPosition - gl_Vertex.xyz;\n"
536 "       LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz);\n"
537 "       LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz);\n"
538 "       LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz);\n"
539 "#endif\n"
540 "\n"
541 "#ifdef MODE_LIGHTDIRECTION\n"
542 "       LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz);\n"
543 "       LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz);\n"
544 "       LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz);\n"
545 "#endif\n"
546 "\n"
547 "       // transform unnormalized eye direction into tangent space\n"
548 "#ifndef USEFOG\n"
549 "       vec3 EyeVectorModelSpace;\n"
550 "#endif\n"
551 "       EyeVectorModelSpace = EyePosition - gl_Vertex.xyz;\n"
552 "       EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz);\n"
553 "       EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz);\n"
554 "       EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz);\n"
555 "\n"
556 "#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
557 "       VectorS = gl_MultiTexCoord1.xyz;\n"
558 "       VectorT = gl_MultiTexCoord2.xyz;\n"
559 "       VectorR = gl_MultiTexCoord3.xyz;\n"
560 "#endif\n"
561 "\n"
562 "//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION)\n"
563 "//     ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix;\n"
564 "//     //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition;\n"
565 "//     //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition;\n"
566 "//#endif\n"
567 "\n"
568 "// transform vertex to camera space, using ftransform to match non-VS\n"
569 "       // rendering\n"
570 "       gl_Position = ftransform();\n"
571 "\n"
572 "#ifdef MODE_WATER\n"
573 "       ModelViewProjectionPosition = gl_Position;\n"
574 "#endif\n"
575 "#ifdef MODE_REFRACTION\n"
576 "       ModelViewProjectionPosition = gl_Position;\n"
577 "#endif\n"
578 "#ifdef USEREFLECTION\n"
579 "       ModelViewProjectionPosition = gl_Position;\n"
580 "#endif\n"
581 "}\n"
582 "\n"
583 "#endif // VERTEX_SHADER\n"
584 "\n"
585 "\n"
586 "\n"
587 "\n"
588 "// fragment shader specific:\n"
589 "#ifdef FRAGMENT_SHADER\n"
590 "\n"
591 "// 13 textures, we can only use up to 16 on DX9-class hardware\n"
592 "uniform sampler2D Texture_Normal;\n"
593 "uniform sampler2D Texture_Color;\n"
594 "uniform sampler2D Texture_Gloss;\n"
595 "uniform samplerCube Texture_Cube;\n"
596 "uniform sampler2D Texture_Attenuation;\n"
597 "uniform sampler2D Texture_FogMask;\n"
598 "uniform sampler2D Texture_Pants;\n"
599 "uniform sampler2D Texture_Shirt;\n"
600 "uniform sampler2D Texture_Lightmap;\n"
601 "uniform sampler2D Texture_Deluxemap;\n"
602 "uniform sampler2D Texture_Glow;\n"
603 "uniform sampler2D Texture_Reflection;\n"
604 "uniform sampler2D Texture_Refraction;\n"
605 "\n"
606 "uniform myhvec3 LightColor;\n"
607 "uniform myhvec3 AmbientColor;\n"
608 "uniform myhvec3 DiffuseColor;\n"
609 "uniform myhvec3 SpecularColor;\n"
610 "uniform myhvec3 Color_Pants;\n"
611 "uniform myhvec3 Color_Shirt;\n"
612 "uniform myhvec3 FogColor;\n"
613 "\n"
614 "uniform myhvec4 TintColor;\n"
615 "\n"
616 "\n"
617 "//#ifdef MODE_WATER\n"
618 "uniform vec4 DistortScaleRefractReflect;\n"
619 "uniform vec4 ScreenScaleRefractReflect;\n"
620 "uniform vec4 ScreenCenterRefractReflect;\n"
621 "uniform myhvec4 RefractColor;\n"
622 "uniform myhvec4 ReflectColor;\n"
623 "uniform myhalf ReflectFactor;\n"
624 "uniform myhalf ReflectOffset;\n"
625 "//#else\n"
626 "//# ifdef MODE_REFRACTION\n"
627 "//uniform vec4 DistortScaleRefractReflect;\n"
628 "//uniform vec4 ScreenScaleRefractReflect;\n"
629 "//uniform vec4 ScreenCenterRefractReflect;\n"
630 "//uniform myhvec4 RefractColor;\n"
631 "//#  ifdef USEREFLECTION\n"
632 "//uniform myhvec4 ReflectColor;\n"
633 "//#  endif\n"
634 "//# else\n"
635 "//#  ifdef USEREFLECTION\n"
636 "//uniform vec4 DistortScaleRefractReflect;\n"
637 "//uniform vec4 ScreenScaleRefractReflect;\n"
638 "//uniform vec4 ScreenCenterRefractReflect;\n"
639 "//uniform myhvec4 ReflectColor;\n"
640 "//#  endif\n"
641 "//# endif\n"
642 "//#endif\n"
643 "\n"
644 "uniform myhalf GlowScale;\n"
645 "uniform myhalf SceneBrightness;\n"
646 "#ifdef USECONTRASTBOOST\n"
647 "uniform myhalf ContrastBoostCoeff;\n"
648 "#endif\n"
649 "\n"
650 "uniform float OffsetMapping_Scale;\n"
651 "uniform float OffsetMapping_Bias;\n"
652 "uniform float FogRangeRecip;\n"
653 "\n"
654 "uniform myhalf AmbientScale;\n"
655 "uniform myhalf DiffuseScale;\n"
656 "uniform myhalf SpecularScale;\n"
657 "uniform myhalf SpecularPower;\n"
658 "\n"
659 "#ifdef USEOFFSETMAPPING\n"
660 "vec2 OffsetMapping(vec2 TexCoord)\n"
661 "{\n"
662 "#ifdef USEOFFSETMAPPING_RELIEFMAPPING\n"
663 "       // 14 sample relief mapping: linear search and then binary search\n"
664 "       // this basically steps forward a small amount repeatedly until it finds\n"
665 "       // itself inside solid, then jitters forward and back using decreasing\n"
666 "       // amounts to find the impact\n"
667 "       //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1);\n"
668 "       //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1);\n"
669 "       vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1);\n"
670 "       vec3 RT = vec3(TexCoord, 1);\n"
671 "       OffsetVector *= 0.1;\n"
672 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
673 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
674 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
675 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
676 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
677 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
678 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
679 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
680 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
681 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z)          - 0.5);\n"
682 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5    - 0.25);\n"
683 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25   - 0.125);\n"
684 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125  - 0.0625);\n"
685 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125);\n"
686 "       return RT.xy;\n"
687 "#else\n"
688 "       // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits)\n"
689 "       // this basically moves forward the full distance, and then backs up based\n"
690 "       // on height of samples\n"
691 "       //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1));\n"
692 "       //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1));\n"
693 "       vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1));\n"
694 "       TexCoord += OffsetVector;\n"
695 "       OffsetVector *= 0.333;\n"
696 "       TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
697 "       TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
698 "       TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
699 "       return TexCoord;\n"
700 "#endif\n"
701 "}\n"
702 "#endif // USEOFFSETMAPPING\n"
703 "\n"
704 "#ifdef MODE_WATER\n"
705 "\n"
706 "// water pass\n"
707 "void main(void)\n"
708 "{\n"
709 "#ifdef USEOFFSETMAPPING\n"
710 "       // apply offsetmapping\n"
711 "       vec2 TexCoordOffset = OffsetMapping(TexCoord);\n"
712 "#define TexCoord TexCoordOffset\n"
713 "#endif\n"
714 "\n"
715 "       vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);\n"
716 "       //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
717 "       vec4 ScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect + vec2(normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5))).xyxy * DistortScaleRefractReflect;\n"
718 "       float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 5.0) * ReflectFactor + ReflectOffset;\n"
719 "       gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel);\n"
720 "}\n"
721 "\n"
722 "#else // MODE_WATER\n"
723 "#ifdef MODE_REFRACTION\n"
724 "\n"
725 "// refraction pass\n"
726 "void main(void)\n"
727 "{\n"
728 "#ifdef USEOFFSETMAPPING\n"
729 "       // apply offsetmapping\n"
730 "       vec2 TexCoordOffset = OffsetMapping(TexCoord);\n"
731 "#define TexCoord TexCoordOffset\n"
732 "#endif\n"
733 "\n"
734 "       vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w);\n"
735 "       //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy;\n"
736 "       vec2 ScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy + vec2(normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5))).xy * DistortScaleRefractReflect.xy;\n"
737 "       gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor;\n"
738 "}\n"
739 "\n"
740 "#else // MODE_REFRACTION\n"
741 "void main(void)\n"
742 "{\n"
743 "#ifdef USEOFFSETMAPPING\n"
744 "       // apply offsetmapping\n"
745 "       vec2 TexCoordOffset = OffsetMapping(TexCoord);\n"
746 "#define TexCoord TexCoordOffset\n"
747 "#endif\n"
748 "\n"
749 "       // combine the diffuse textures (base, pants, shirt)\n"
750 "       myhvec4 color = myhvec4(texture2D(Texture_Color, TexCoord));\n"
751 "#ifdef USECOLORMAPPING\n"
752 "       color.rgb += myhvec3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhvec3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt;\n"
753 "#endif\n"
754 "\n"
755 "\n"
756 "\n"
757 "\n"
758 "#ifdef MODE_LIGHTSOURCE\n"
759 "       // light source\n"
760 "\n"
761 "       // calculate surface normal, light normal, and specular normal\n"
762 "       // compute color intensity for the two textures (colormap and glossmap)\n"
763 "       // scale by light color and attenuation as efficiently as possible\n"
764 "       // (do as much scalar math as possible rather than vector math)\n"
765 "# ifdef USESPECULAR\n"
766 "       myhvec3 surfacenormal = normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5));\n"
767 "       myhvec3 diffusenormal = myhvec3(normalize(LightVector));\n"
768 "       myhvec3 specularnormal = normalize(diffusenormal + myhvec3(normalize(EyeVector)));\n"
769 "\n"
770 "       // calculate directional shading\n"
771 "       color.rgb = LightColor * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (color.rgb * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))) + (SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower)) * myhvec3(texture2D(Texture_Gloss, TexCoord)));\n"
772 "# else\n"
773 "#  ifdef USEDIFFUSE\n"
774 "       myhvec3 surfacenormal = normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5));\n"
775 "       myhvec3 diffusenormal = myhvec3(normalize(LightVector));\n"
776 "\n"
777 "       // calculate directional shading\n"
778 "       color.rgb = color.rgb * LightColor * (myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))));\n"
779 "#  else\n"
780 "       // calculate directionless shading\n"
781 "       color.rgb = color.rgb * LightColor * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0)));\n"
782 "#  endif\n"
783 "# endif\n"
784 "\n"
785 "# ifdef USECUBEFILTER\n"
786 "       // apply light cubemap filter\n"
787 "       //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector));\n"
788 "       color.rgb *= myhvec3(textureCube(Texture_Cube, CubeVector));\n"
789 "# endif\n"
790 "#endif // MODE_LIGHTSOURCE\n"
791 "\n"
792 "\n"
793 "\n"
794 "\n"
795 "#ifdef MODE_LIGHTDIRECTION\n"
796 "       // directional model lighting\n"
797 "# ifdef USESPECULAR\n"
798 "       // get the surface normal and light normal\n"
799 "       myhvec3 surfacenormal = normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5));\n"
800 "       myhvec3 diffusenormal = myhvec3(LightVector);\n"
801 "\n"
802 "       // calculate directional shading\n"
803 "       color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0));\n"
804 "       myhvec3 specularnormal = normalize(diffusenormal + myhvec3(normalize(EyeVector)));\n"
805 "       color.rgb += myhvec3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower);\n"
806 "# else\n"
807 "#  ifdef USEDIFFUSE\n"
808 "       // get the surface normal and light normal\n"
809 "       myhvec3 surfacenormal = normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5));\n"
810 "       myhvec3 diffusenormal = myhvec3(LightVector);\n"
811 "\n"
812 "       // calculate directional shading\n"
813 "       color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0));\n"
814 "#  else\n"
815 "       color.rgb *= AmbientColor;\n"
816 "#  endif\n"
817 "# endif\n"
818 "\n"
819 "       color.a *= TintColor.a;\n"
820 "#endif // MODE_LIGHTDIRECTION\n"
821 "\n"
822 "\n"
823 "\n"
824 "\n"
825 "#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
826 "       // deluxemap lightmapping using light vectors in modelspace (evil q3map2)\n"
827 "\n"
828 "       // get the surface normal and light normal\n"
829 "       myhvec3 surfacenormal = normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5));\n"
830 "\n"
831 "       myhvec3 diffusenormal_modelspace = myhvec3(texture2D(Texture_Deluxemap, TexCoordLightmap)) - myhvec3(0.5);\n"
832 "       myhvec3 diffusenormal = normalize(myhvec3(dot(diffusenormal_modelspace, myhvec3(VectorS)), dot(diffusenormal_modelspace, myhvec3(VectorT)), dot(diffusenormal_modelspace, myhvec3(VectorR))));\n"
833 "       // calculate directional shading\n"
834 "       myhvec3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)));\n"
835 "# ifdef USESPECULAR\n"
836 "       myhvec3 specularnormal = myhvec3(normalize(diffusenormal + myhvec3(normalize(EyeVector))));\n"
837 "       tempcolor += myhvec3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower);\n"
838 "# endif\n"
839 "\n"
840 "       // apply lightmap color\n"
841 "       color.rgb = color.rgb * AmbientScale + tempcolor * myhvec3(texture2D(Texture_Lightmap, TexCoordLightmap));\n"
842 "\n"
843 "       color *= TintColor;\n"
844 "#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
845 "\n"
846 "\n"
847 "\n"
848 "\n"
849 "#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n"
850 "       // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light)\n"
851 "\n"
852 "       // get the surface normal and light normal\n"
853 "       myhvec3 surfacenormal = normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5));\n"
854 "\n"
855 "       myhvec3 diffusenormal = normalize(myhvec3(texture2D(Texture_Deluxemap, TexCoordLightmap)) - myhvec3(0.5));\n"
856 "       // calculate directional shading\n"
857 "       myhvec3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0)));\n"
858 "# ifdef USESPECULAR\n"
859 "       myhvec3 specularnormal = myhvec3(normalize(diffusenormal + myhvec3(normalize(EyeVector))));\n"
860 "       tempcolor += myhvec3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower);\n"
861 "# endif\n"
862 "\n"
863 "       // apply lightmap color\n"
864 "       color.rgb = color.rgb * AmbientScale + tempcolor * myhvec3(texture2D(Texture_Lightmap, TexCoordLightmap));\n"
865 "\n"
866 "       color *= TintColor;\n"
867 "#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n"
868 "\n"
869 "\n"
870 "\n"
871 "\n"
872 "#ifdef MODE_LIGHTMAP\n"
873 "       // apply lightmap color\n"
874 "       color.rgb = color.rgb * myhvec3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale + color.rgb * AmbientScale;\n"
875 "\n"
876 "       color *= TintColor;\n"
877 "#endif // MODE_LIGHTMAP\n"
878 "\n"
879 "\n"
880 "\n"
881 "\n"
882 "#ifdef MODE_VERTEXCOLOR\n"
883 "       // apply lightmap color\n"
884 "       color.rgb = color.rgb * myhvec3(gl_Color.rgb) * DiffuseScale + color.rgb * AmbientScale;\n"
885 "\n"
886 "       color *= TintColor;\n"
887 "#endif // MODE_VERTEXCOLOR\n"
888 "\n"
889 "\n"
890 "\n"
891 "\n"
892 "#ifdef MODE_FLATCOLOR\n"
893 "       color *= TintColor;\n"
894 "#endif // MODE_FLATCOLOR\n"
895 "\n"
896 "\n"
897 "\n"
898 "\n"
899 "\n"
900 "\n"
901 "\n"
902 "\n"
903 "#ifdef USEGLOW\n"
904 "       color.rgb += myhvec3(texture2D(Texture_Glow, TexCoord)) * GlowScale;\n"
905 "#endif\n"
906 "\n"
907 "#ifdef USECONTRASTBOOST\n"
908 "       color.rgb = color.rgb / (ContrastBoostCoeff * color.rgb + myhvec3(1, 1, 1));\n"
909 "#endif\n"
910 "\n"
911 "       color.rgb *= SceneBrightness;\n"
912 "\n"
913 "       // apply fog after Contrastboost/SceneBrightness because its color is already modified appropriately\n"
914 "#ifdef USEFOG\n"
915 "       color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhvec2(length(EyeVectorModelSpace)*FogRangeRecip, 0.0))));\n"
916 "#endif\n"
917 "\n"
918 "       // reflection must come last because it already contains exactly the correct fog (the reflection render preserves camera distance from the plane, it only flips the side) and ContrastBoost/SceneBrightness\n"
919 "#ifdef USEREFLECTION\n"
920 "       vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);\n"
921 "       //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
922 "       vec4 ScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect + vec3(normalize(myhvec3(texture2D(Texture_Normal, TexCoord)) - myhvec3(0.5))).xyxy * DistortScaleRefractReflect;\n"
923 "       color.rgb = mix(color.rgb, myhvec3(texture2D(Texture_Reflection, ScreenTexCoord.zw)) * ReflectColor.rgb, ReflectColor.a);\n"
924 "#endif\n"
925 "\n"
926 "       gl_FragColor = vec4(color);\n"
927 "}\n"
928 "#endif // MODE_REFRACTION\n"
929 "#endif // MODE_WATER\n"
930 "\n"
931 "#endif // FRAGMENT_SHADER\n"
932 ;
933
934 #define SHADERPERMUTATION_COLORMAPPING (1<<0) // indicates this is a colormapped skin
935 #define SHADERPERMUTATION_CONTRASTBOOST (1<<1) // r_glsl_contrastboost boosts the contrast at low color levels (similar to gamma)
936 #define SHADERPERMUTATION_FOG (1<<2) // tint the color by fog color or black if using additive blend mode
937 #define SHADERPERMUTATION_CUBEFILTER (1<<3) // (lightsource) use cubemap light filter
938 #define SHADERPERMUTATION_GLOW (1<<4) // (lightmap) blend in an additive glow texture
939 #define SHADERPERMUTATION_DIFFUSE (1<<5) // (lightsource) whether to use directional shading
940 #define SHADERPERMUTATION_SPECULAR (1<<6) // (lightsource or deluxemapping) render specular effects
941 #define SHADERPERMUTATION_REFLECTION (1<<7) // normalmap-perturbed reflection of the scene infront of the surface, preformed as an overlay on the surface
942 #define SHADERPERMUTATION_OFFSETMAPPING (1<<8) // adjust texcoords to roughly simulate a displacement mapped surface
943 #define SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING (1<<9) // adjust texcoords to accurately simulate a displacement mapped surface (requires OFFSETMAPPING to also be set!)
944 #define SHADERPERMUTATION_MODEBASE (1<<10) // multiplier for the SHADERMODE_ values to get a valid index
945
946 // NOTE: MUST MATCH ORDER OF SHADERPERMUTATION_* DEFINES!
947 const char *shaderpermutationinfo[][2] =
948 {
949         {"#define USECOLORMAPPING\n", " colormapping"},
950         {"#define USECONTRASTBOOST\n", " contrastboost"},
951         {"#define USEFOG\n", " fog"},
952         {"#define USECUBEFILTER\n", " cubefilter"},
953         {"#define USEGLOW\n", " glow"},
954         {"#define USEDIFFUSE\n", " diffuse"},
955         {"#define USESPECULAR\n", " specular"},
956         {"#define USEREFLECTION\n", " reflection"},
957         {"#define USEOFFSETMAPPING\n", " offsetmapping"},
958         {"#define USEOFFSETMAPPING_RELIEFMAPPING\n", " reliefmapping"},
959         {NULL, NULL}
960 };
961
962 // this enum is multiplied by SHADERPERMUTATION_MODEBASE
963 typedef enum shadermode_e
964 {
965         SHADERMODE_FLATCOLOR, // (lightmap) modulate texture by uniform color (q1bsp, q3bsp)
966         SHADERMODE_VERTEXCOLOR, // (lightmap) modulate texture by vertex colors (q3bsp)
967         SHADERMODE_LIGHTMAP, // (lightmap) modulate texture by lightmap texture (q1bsp, q3bsp)
968         SHADERMODE_LIGHTDIRECTIONMAP_MODELSPACE, // (lightmap) use directional pixel shading from texture containing modelspace light directions (q3bsp deluxemap)
969         SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE, // (lightmap) use directional pixel shading from texture containing tangentspace light directions (q1bsp deluxemap)
970         SHADERMODE_LIGHTDIRECTION, // (lightmap) use directional pixel shading from fixed light direction (q3bsp)
971         SHADERMODE_LIGHTSOURCE, // (lightsource) use directional pixel shading from light source (rtlight)
972         SHADERMODE_REFRACTION, // refract background (the material is rendered normally after this pass)
973         SHADERMODE_WATER, // refract background and reflection (the material is rendered normally after this pass)
974         SHADERMODE_COUNT
975 }
976 shadermode_t;
977
978 // NOTE: MUST MATCH ORDER OF SHADERMODE_* ENUMS!
979 const char *shadermodeinfo[][2] =
980 {
981         {"#define MODE_FLATCOLOR\n", " flatcolor"},
982         {"#define MODE_VERTEXCOLOR\n", " vertexcolor"},
983         {"#define MODE_LIGHTMAP\n", " lightmap"},
984         {"#define MODE_LIGHTDIRECTIONMAP_MODELSPACE\n", " lightdirectionmap_modelspace"},
985         {"#define MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n", " lightdirectionmap_tangentspace"},
986         {"#define MODE_LIGHTDIRECTION\n", " lightdirection"},
987         {"#define MODE_LIGHTSOURCE\n", " lightsource"},
988         {"#define MODE_REFRACTION\n", " refraction"},
989         {"#define MODE_WATER\n", " water"},
990         {NULL, NULL}
991 };
992
993 #define SHADERPERMUTATION_INDICES (SHADERPERMUTATION_MODEBASE * SHADERMODE_COUNT)
994
995 typedef struct r_glsl_permutation_s
996 {
997         // indicates if we have tried compiling this permutation already
998         qboolean compiled;
999         // 0 if compilation failed
1000         int program;
1001         // locations of detected uniforms in program object, or -1 if not found
1002         int loc_Texture_Normal;
1003         int loc_Texture_Color;
1004         int loc_Texture_Gloss;
1005         int loc_Texture_Cube;
1006         int loc_Texture_Attenuation;
1007         int loc_Texture_FogMask;
1008         int loc_Texture_Pants;
1009         int loc_Texture_Shirt;
1010         int loc_Texture_Lightmap;
1011         int loc_Texture_Deluxemap;
1012         int loc_Texture_Glow;
1013         int loc_Texture_Refraction;
1014         int loc_Texture_Reflection;
1015         int loc_FogColor;
1016         int loc_LightPosition;
1017         int loc_EyePosition;
1018         int loc_LightColor;
1019         int loc_Color_Pants;
1020         int loc_Color_Shirt;
1021         int loc_FogRangeRecip;
1022         int loc_AmbientScale;
1023         int loc_DiffuseScale;
1024         int loc_SpecularScale;
1025         int loc_SpecularPower;
1026         int loc_GlowScale;
1027         int loc_SceneBrightness; // or: Scenebrightness * ContrastBoost
1028         int loc_OffsetMapping_Scale;
1029         int loc_TintColor;
1030         int loc_AmbientColor;
1031         int loc_DiffuseColor;
1032         int loc_SpecularColor;
1033         int loc_LightDir;
1034         int loc_ContrastBoostCoeff; // 1 - 1/ContrastBoost
1035         int loc_DistortScaleRefractReflect;
1036         int loc_ScreenScaleRefractReflect;
1037         int loc_ScreenCenterRefractReflect;
1038         int loc_RefractColor;
1039         int loc_ReflectColor;
1040         int loc_ReflectFactor;
1041         int loc_ReflectOffset;
1042 }
1043 r_glsl_permutation_t;
1044
1045 // information about each possible shader permutation
1046 r_glsl_permutation_t r_glsl_permutations[SHADERPERMUTATION_INDICES];
1047 // currently selected permutation
1048 r_glsl_permutation_t *r_glsl_permutation;
1049
1050 // these are additional flags used only by R_GLSL_CompilePermutation
1051 #define SHADERTYPE_USES_VERTEXSHADER (1<<0)
1052 #define SHADERTYPE_USES_GEOMETRYSHADER (1<<1)
1053 #define SHADERTYPE_USES_FRAGMENTSHADER (1<<2)
1054
1055 static void R_GLSL_CompilePermutation(const char *filename, int permutation, int shadertype)
1056 {
1057         int i;
1058         qboolean shaderfound;
1059         r_glsl_permutation_t *p = r_glsl_permutations + permutation;
1060         int vertstrings_count;
1061         int geomstrings_count;
1062         int fragstrings_count;
1063         char *shaderstring;
1064         const char *vertstrings_list[32+1];
1065         const char *geomstrings_list[32+1];
1066         const char *fragstrings_list[32+1];
1067         char permutationname[256];
1068         if (p->compiled)
1069                 return;
1070         p->compiled = true;
1071         p->program = 0;
1072         vertstrings_list[0] = "#define VERTEX_SHADER\n";
1073         geomstrings_list[0] = "#define GEOMETRY_SHADER\n";
1074         fragstrings_list[0] = "#define FRAGMENT_SHADER\n";
1075         vertstrings_count = 1;
1076         geomstrings_count = 1;
1077         fragstrings_count = 1;
1078         permutationname[0] = 0;
1079         i = permutation / SHADERPERMUTATION_MODEBASE;
1080         vertstrings_list[vertstrings_count++] = shadermodeinfo[i][0];
1081         geomstrings_list[geomstrings_count++] = shadermodeinfo[i][0];
1082         fragstrings_list[fragstrings_count++] = shadermodeinfo[i][0];
1083         strlcat(permutationname, shadermodeinfo[i][1], sizeof(permutationname));
1084         for (i = 0;shaderpermutationinfo[i][0];i++)
1085         {
1086                 if (permutation & (1<<i))
1087                 {
1088                         vertstrings_list[vertstrings_count++] = shaderpermutationinfo[i][0];
1089                         geomstrings_list[geomstrings_count++] = shaderpermutationinfo[i][0];
1090                         fragstrings_list[fragstrings_count++] = shaderpermutationinfo[i][0];
1091                         strlcat(permutationname, shaderpermutationinfo[i][1], sizeof(permutationname));
1092                 }
1093                 else
1094                 {
1095                         // keep line numbers correct
1096                         vertstrings_list[vertstrings_count++] = "\n";
1097                         geomstrings_list[geomstrings_count++] = "\n";
1098                         fragstrings_list[fragstrings_count++] = "\n";
1099                 }
1100         }
1101         shaderstring = (char *)FS_LoadFile(filename, r_main_mempool, false, NULL);
1102         shaderfound = false;
1103         if (shaderstring)
1104         {
1105                 Con_DPrint("from disk... ");
1106                 vertstrings_list[vertstrings_count++] = shaderstring;
1107                 geomstrings_list[geomstrings_count++] = shaderstring;
1108                 fragstrings_list[fragstrings_count++] = shaderstring;
1109                 shaderfound = true;
1110         }
1111         else if (!strcmp(filename, "glsl/default.glsl"))
1112         {
1113                 vertstrings_list[vertstrings_count++] = builtinshaderstring;
1114                 geomstrings_list[geomstrings_count++] = builtinshaderstring;
1115                 fragstrings_list[fragstrings_count++] = builtinshaderstring;
1116                 shaderfound = true;
1117         }
1118         // clear any lists that are not needed by this shader
1119         if (!(shadertype & SHADERTYPE_USES_VERTEXSHADER))
1120                 vertstrings_count = 0;
1121         if (!(shadertype & SHADERTYPE_USES_GEOMETRYSHADER))
1122                 geomstrings_count = 0;
1123         if (!(shadertype & SHADERTYPE_USES_FRAGMENTSHADER))
1124                 fragstrings_count = 0;
1125         // compile the shader program
1126         if (shaderfound && vertstrings_count + geomstrings_count + fragstrings_count)
1127                 p->program = GL_Backend_CompileProgram(vertstrings_count, vertstrings_list, geomstrings_count, geomstrings_list, fragstrings_count, fragstrings_list);
1128         if (p->program)
1129         {
1130                 CHECKGLERROR
1131                 qglUseProgramObjectARB(p->program);CHECKGLERROR
1132                 // look up all the uniform variable names we care about, so we don't
1133                 // have to look them up every time we set them
1134                 p->loc_Texture_Normal      = qglGetUniformLocationARB(p->program, "Texture_Normal");
1135                 p->loc_Texture_Color       = qglGetUniformLocationARB(p->program, "Texture_Color");
1136                 p->loc_Texture_Gloss       = qglGetUniformLocationARB(p->program, "Texture_Gloss");
1137                 p->loc_Texture_Cube        = qglGetUniformLocationARB(p->program, "Texture_Cube");
1138                 p->loc_Texture_Attenuation = qglGetUniformLocationARB(p->program, "Texture_Attenuation");
1139                 p->loc_Texture_FogMask     = qglGetUniformLocationARB(p->program, "Texture_FogMask");
1140                 p->loc_Texture_Pants       = qglGetUniformLocationARB(p->program, "Texture_Pants");
1141                 p->loc_Texture_Shirt       = qglGetUniformLocationARB(p->program, "Texture_Shirt");
1142                 p->loc_Texture_Lightmap    = qglGetUniformLocationARB(p->program, "Texture_Lightmap");
1143                 p->loc_Texture_Deluxemap   = qglGetUniformLocationARB(p->program, "Texture_Deluxemap");
1144                 p->loc_Texture_Glow        = qglGetUniformLocationARB(p->program, "Texture_Glow");
1145                 p->loc_Texture_Refraction  = qglGetUniformLocationARB(p->program, "Texture_Refraction");
1146                 p->loc_Texture_Reflection  = qglGetUniformLocationARB(p->program, "Texture_Reflection");
1147                 p->loc_FogColor            = qglGetUniformLocationARB(p->program, "FogColor");
1148                 p->loc_LightPosition       = qglGetUniformLocationARB(p->program, "LightPosition");
1149                 p->loc_EyePosition         = qglGetUniformLocationARB(p->program, "EyePosition");
1150                 p->loc_LightColor          = qglGetUniformLocationARB(p->program, "LightColor");
1151                 p->loc_Color_Pants         = qglGetUniformLocationARB(p->program, "Color_Pants");
1152                 p->loc_Color_Shirt         = qglGetUniformLocationARB(p->program, "Color_Shirt");
1153                 p->loc_FogRangeRecip       = qglGetUniformLocationARB(p->program, "FogRangeRecip");
1154                 p->loc_AmbientScale        = qglGetUniformLocationARB(p->program, "AmbientScale");
1155                 p->loc_DiffuseScale        = qglGetUniformLocationARB(p->program, "DiffuseScale");
1156                 p->loc_SpecularPower       = qglGetUniformLocationARB(p->program, "SpecularPower");
1157                 p->loc_SpecularScale       = qglGetUniformLocationARB(p->program, "SpecularScale");
1158                 p->loc_GlowScale           = qglGetUniformLocationARB(p->program, "GlowScale");
1159                 p->loc_SceneBrightness     = qglGetUniformLocationARB(p->program, "SceneBrightness");
1160                 p->loc_OffsetMapping_Scale = qglGetUniformLocationARB(p->program, "OffsetMapping_Scale");
1161                 p->loc_TintColor       = qglGetUniformLocationARB(p->program, "TintColor");
1162                 p->loc_AmbientColor        = qglGetUniformLocationARB(p->program, "AmbientColor");
1163                 p->loc_DiffuseColor        = qglGetUniformLocationARB(p->program, "DiffuseColor");
1164                 p->loc_SpecularColor       = qglGetUniformLocationARB(p->program, "SpecularColor");
1165                 p->loc_LightDir            = qglGetUniformLocationARB(p->program, "LightDir");
1166                 p->loc_ContrastBoostCoeff  = qglGetUniformLocationARB(p->program, "ContrastBoostCoeff");
1167                 p->loc_DistortScaleRefractReflect = qglGetUniformLocationARB(p->program, "DistortScaleRefractReflect");
1168                 p->loc_ScreenScaleRefractReflect = qglGetUniformLocationARB(p->program, "ScreenScaleRefractReflect");
1169                 p->loc_ScreenCenterRefractReflect = qglGetUniformLocationARB(p->program, "ScreenCenterRefractReflect");
1170                 p->loc_RefractColor        = qglGetUniformLocationARB(p->program, "RefractColor");
1171                 p->loc_ReflectColor        = qglGetUniformLocationARB(p->program, "ReflectColor");
1172                 p->loc_ReflectFactor       = qglGetUniformLocationARB(p->program, "ReflectFactor");
1173                 p->loc_ReflectOffset       = qglGetUniformLocationARB(p->program, "ReflectOffset");
1174                 // initialize the samplers to refer to the texture units we use
1175                 if (p->loc_Texture_Normal >= 0)    qglUniform1iARB(p->loc_Texture_Normal, 0);
1176                 if (p->loc_Texture_Color >= 0)     qglUniform1iARB(p->loc_Texture_Color, 1);
1177                 if (p->loc_Texture_Gloss >= 0)     qglUniform1iARB(p->loc_Texture_Gloss, 2);
1178                 if (p->loc_Texture_Cube >= 0)      qglUniform1iARB(p->loc_Texture_Cube, 3);
1179                 if (p->loc_Texture_FogMask >= 0)   qglUniform1iARB(p->loc_Texture_FogMask, 4);
1180                 if (p->loc_Texture_Pants >= 0)     qglUniform1iARB(p->loc_Texture_Pants, 5);
1181                 if (p->loc_Texture_Shirt >= 0)     qglUniform1iARB(p->loc_Texture_Shirt, 6);
1182                 if (p->loc_Texture_Lightmap >= 0)  qglUniform1iARB(p->loc_Texture_Lightmap, 7);
1183                 if (p->loc_Texture_Deluxemap >= 0) qglUniform1iARB(p->loc_Texture_Deluxemap, 8);
1184                 if (p->loc_Texture_Glow >= 0)      qglUniform1iARB(p->loc_Texture_Glow, 9);
1185                 if (p->loc_Texture_Attenuation >= 0) qglUniform1iARB(p->loc_Texture_Attenuation, 10);
1186                 if (p->loc_Texture_Refraction >= 0) qglUniform1iARB(p->loc_Texture_Refraction, 11);
1187                 if (p->loc_Texture_Reflection >= 0) qglUniform1iARB(p->loc_Texture_Reflection, 12);
1188                 CHECKGLERROR
1189                 qglUseProgramObjectARB(0);CHECKGLERROR
1190                 if (developer.integer)
1191                         Con_Printf("GLSL shader %s :%s compiled.\n", filename, permutationname);
1192         }
1193         else
1194         {
1195                 if (developer.integer)
1196                         Con_Printf("GLSL shader %s :%s failed!  source code line offset for above errors is %i.\n", permutationname, filename, -(vertstrings_count - 1));
1197                 else
1198                         Con_Printf("GLSL shader %s :%s failed!  some features may not work properly.\n", permutationname, filename);
1199         }
1200         if (shaderstring)
1201                 Mem_Free(shaderstring);
1202 }
1203
1204 void R_GLSL_Restart_f(void)
1205 {
1206         int i;
1207         for (i = 0;i < SHADERPERMUTATION_INDICES;i++)
1208                 if (r_glsl_permutations[i].program)
1209                         GL_Backend_FreeProgram(r_glsl_permutations[i].program);
1210         memset(r_glsl_permutations, 0, sizeof(r_glsl_permutations));
1211 }
1212
1213 void R_GLSL_DumpShader_f(void)
1214 {
1215         int i;
1216
1217         qfile_t *file = FS_Open("glsl/default.glsl", "w", false, false);
1218         if(!file)
1219         {
1220                 Con_Printf("failed to write to glsl/default.glsl\n");
1221                 return;
1222         }
1223
1224         FS_Print(file, "// The engine may define the following macros:\n");
1225         FS_Print(file, "// #define VERTEX_SHADER\n// #define GEOMETRY_SHADER\n// #define FRAGMENT_SHADER\n");
1226         for (i = 0;shadermodeinfo[i][0];i++)
1227                 FS_Printf(file, "// %s", shadermodeinfo[i][0]);
1228         for (i = 0;shaderpermutationinfo[i][0];i++)
1229                 FS_Printf(file, "// %s", shaderpermutationinfo[i][0]);
1230         FS_Print(file, "\n");
1231         FS_Print(file, builtinshaderstring);
1232         FS_Close(file);
1233
1234         Con_Printf("glsl/default.glsl written\n");
1235 }
1236
1237 extern rtexture_t *r_shadow_attenuationgradienttexture;
1238 extern rtexture_t *r_shadow_attenuation2dtexture;
1239 extern rtexture_t *r_shadow_attenuation3dtexture;
1240 int R_SetupSurfaceShader(const vec3_t lightcolorbase, qboolean modellighting, float ambientscale, float diffusescale, float specularscale, rsurfacepass_t rsurfacepass)
1241 {
1242         // select a permutation of the lighting shader appropriate to this
1243         // combination of texture, entity, light source, and fogging, only use the
1244         // minimum features necessary to avoid wasting rendering time in the
1245         // fragment shader on features that are not being used
1246         const char *shaderfilename = NULL;
1247         unsigned int permutation = 0;
1248         unsigned int shadertype = 0;
1249         shadermode_t mode = 0;
1250         r_glsl_permutation = NULL;
1251         shaderfilename = "glsl/default.glsl";
1252         shadertype = SHADERTYPE_USES_VERTEXSHADER | SHADERTYPE_USES_FRAGMENTSHADER;
1253         // TODO: implement geometry-shader based shadow volumes someday
1254         if (r_glsl_offsetmapping.integer)
1255         {
1256                 permutation |= SHADERPERMUTATION_OFFSETMAPPING;
1257                 if (r_glsl_offsetmapping_reliefmapping.integer)
1258                         permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;
1259         }
1260         if (rsurfacepass == RSURFPASS_BACKGROUND)
1261         {
1262                 // distorted background
1263                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_WATERSHADER)
1264                         mode = SHADERMODE_WATER;
1265                 else
1266                         mode = SHADERMODE_REFRACTION;
1267         }
1268         else if (rsurfacepass == RSURFPASS_RTLIGHT)
1269         {
1270                 // light source
1271                 mode = SHADERMODE_LIGHTSOURCE;
1272                 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
1273                         permutation |= SHADERPERMUTATION_CUBEFILTER;
1274                 if (diffusescale > 0)
1275                         permutation |= SHADERPERMUTATION_DIFFUSE;
1276                 if (specularscale > 0)
1277                         permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
1278                 if (r_refdef.fogenabled)
1279                         permutation |= SHADERPERMUTATION_FOG;
1280                 if (rsurface.texture->colormapping)
1281                         permutation |= SHADERPERMUTATION_COLORMAPPING;
1282                 if(r_glsl_contrastboost.value > 1 || r_glsl_contrastboost.value < 0)
1283                         permutation |= SHADERPERMUTATION_CONTRASTBOOST;
1284         }
1285         else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)
1286         {
1287                 // unshaded geometry (fullbright or ambient model lighting)
1288                 mode = SHADERMODE_FLATCOLOR;
1289                 if (rsurface.texture->currentskinframe->glow)
1290                         permutation |= SHADERPERMUTATION_GLOW;
1291                 if (r_refdef.fogenabled)
1292                         permutation |= SHADERPERMUTATION_FOG;
1293                 if (rsurface.texture->colormapping)
1294                         permutation |= SHADERPERMUTATION_COLORMAPPING;
1295                 if (r_glsl_offsetmapping.integer)
1296                 {
1297                         permutation |= SHADERPERMUTATION_OFFSETMAPPING;
1298                         if (r_glsl_offsetmapping_reliefmapping.integer)
1299                                 permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;
1300                 }
1301                 if(r_glsl_contrastboost.value > 1 || r_glsl_contrastboost.value < 0)
1302                         permutation |= SHADERPERMUTATION_CONTRASTBOOST;
1303                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
1304                         permutation |= SHADERPERMUTATION_REFLECTION;
1305         }
1306         else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT_DIRECTIONAL)
1307         {
1308                 // directional model lighting
1309                 mode = SHADERMODE_LIGHTDIRECTION;
1310                 if (rsurface.texture->currentskinframe->glow)
1311                         permutation |= SHADERPERMUTATION_GLOW;
1312                 permutation |= SHADERPERMUTATION_DIFFUSE;
1313                 if (specularscale > 0)
1314                         permutation |= SHADERPERMUTATION_SPECULAR;
1315                 if (r_refdef.fogenabled)
1316                         permutation |= SHADERPERMUTATION_FOG;
1317                 if (rsurface.texture->colormapping)
1318                         permutation |= SHADERPERMUTATION_COLORMAPPING;
1319                 if(r_glsl_contrastboost.value > 1 || r_glsl_contrastboost.value < 0)
1320                         permutation |= SHADERPERMUTATION_CONTRASTBOOST;
1321                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
1322                         permutation |= SHADERPERMUTATION_REFLECTION;
1323         }
1324         else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
1325         {
1326                 // ambient model lighting
1327                 mode = SHADERMODE_LIGHTDIRECTION;
1328                 if (rsurface.texture->currentskinframe->glow)
1329                         permutation |= SHADERPERMUTATION_GLOW;
1330                 if (r_refdef.fogenabled)
1331                         permutation |= SHADERPERMUTATION_FOG;
1332                 if (rsurface.texture->colormapping)
1333                         permutation |= SHADERPERMUTATION_COLORMAPPING;
1334                 if(r_glsl_contrastboost.value > 1 || r_glsl_contrastboost.value < 0)
1335                         permutation |= SHADERPERMUTATION_CONTRASTBOOST;
1336                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
1337                         permutation |= SHADERPERMUTATION_REFLECTION;
1338         }
1339         else
1340         {
1341                 // lightmapped wall
1342                 if (r_glsl_deluxemapping.integer >= 1 && rsurface.uselightmaptexture && r_refdef.worldmodel && r_refdef.worldmodel->brushq3.deluxemapping)
1343                 {
1344                         // deluxemapping (light direction texture)
1345                         if (rsurface.uselightmaptexture && r_refdef.worldmodel && r_refdef.worldmodel->brushq3.deluxemapping && r_refdef.worldmodel->brushq3.deluxemapping_modelspace)
1346                                 mode = SHADERMODE_LIGHTDIRECTIONMAP_MODELSPACE;
1347                         else
1348                                 mode = SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE;
1349                         if (specularscale > 0)
1350                                 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
1351                 }
1352                 else if (r_glsl_deluxemapping.integer >= 2)
1353                 {
1354                         // fake deluxemapping (uniform light direction in tangentspace)
1355                         mode = SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE;
1356                         if (specularscale > 0)
1357                                 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
1358                 }
1359                 else if (rsurface.uselightmaptexture)
1360                 {
1361                         // ordinary lightmapping (q1bsp, q3bsp)
1362                         mode = SHADERMODE_LIGHTMAP;
1363                 }
1364                 else
1365                 {
1366                         // ordinary vertex coloring (q3bsp)
1367                         mode = SHADERMODE_VERTEXCOLOR;
1368                 }
1369                 if (rsurface.texture->currentskinframe->glow)
1370                         permutation |= SHADERPERMUTATION_GLOW;
1371                 if (r_refdef.fogenabled)
1372                         permutation |= SHADERPERMUTATION_FOG;
1373                 if (rsurface.texture->colormapping)
1374                         permutation |= SHADERPERMUTATION_COLORMAPPING;
1375                 if(r_glsl_contrastboost.value > 1 || r_glsl_contrastboost.value < 0)
1376                         permutation |= SHADERPERMUTATION_CONTRASTBOOST;
1377                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
1378                         permutation |= SHADERPERMUTATION_REFLECTION;
1379         }
1380         permutation |= mode * SHADERPERMUTATION_MODEBASE;
1381         if (!r_glsl_permutations[permutation].program)
1382         {
1383                 if (!r_glsl_permutations[permutation].compiled)
1384                         R_GLSL_CompilePermutation(shaderfilename, permutation, shadertype);
1385                 if (!r_glsl_permutations[permutation].program)
1386                 {
1387                         // remove features until we find a valid permutation
1388                         unsigned int i;
1389                         for (i = (SHADERPERMUTATION_MODEBASE >> 1);;i>>=1)
1390                         {
1391                                 if (!i)
1392                                 {
1393                                         Con_Printf("OpenGL 2.0 shaders disabled - unable to find a working shader permutation fallback on this driver (set r_glsl 1 if you want to try again)\n");
1394                                         Cvar_SetValueQuick(&r_glsl, 0);
1395                                         return 0; // no bit left to clear
1396                                 }
1397                                 // reduce i more quickly whenever it would not remove any bits
1398                                 if (!(permutation & i))
1399                                         continue;
1400                                 permutation &= ~i;
1401                                 if (!r_glsl_permutations[permutation].compiled)
1402                                         R_GLSL_CompilePermutation(shaderfilename, permutation, shadertype);
1403                                 if (r_glsl_permutations[permutation].program)
1404                                         break;
1405                         }
1406                 }
1407         }
1408         r_glsl_permutation = r_glsl_permutations + permutation;
1409         CHECKGLERROR
1410         qglUseProgramObjectARB(r_glsl_permutation->program);CHECKGLERROR
1411         if (mode == SHADERMODE_LIGHTSOURCE)
1412         {
1413                 if (r_glsl_permutation->loc_LightPosition >= 0) qglUniform3fARB(r_glsl_permutation->loc_LightPosition, rsurface.entitylightorigin[0], rsurface.entitylightorigin[1], rsurface.entitylightorigin[2]);
1414                 if (permutation & SHADERPERMUTATION_DIFFUSE)
1415                 {
1416                         if (r_glsl_permutation->loc_LightColor >= 0) qglUniform3fARB(r_glsl_permutation->loc_LightColor, lightcolorbase[0], lightcolorbase[1], lightcolorbase[2]);
1417                         if (r_glsl_permutation->loc_AmbientScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_AmbientScale, ambientscale);
1418                         if (r_glsl_permutation->loc_DiffuseScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_DiffuseScale, diffusescale);
1419                         if (r_glsl_permutation->loc_SpecularScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularScale, specularscale);
1420                 }
1421                 else
1422                 {
1423                         // ambient only is simpler
1424                         if (r_glsl_permutation->loc_LightColor >= 0) qglUniform3fARB(r_glsl_permutation->loc_LightColor, lightcolorbase[0] * ambientscale, lightcolorbase[1] * ambientscale, lightcolorbase[2] * ambientscale);
1425                         if (r_glsl_permutation->loc_AmbientScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_AmbientScale, 1);
1426                         if (r_glsl_permutation->loc_DiffuseScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_DiffuseScale, 0);
1427                         if (r_glsl_permutation->loc_SpecularScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularScale, 0);
1428                 }
1429         }
1430         else if (mode == SHADERMODE_LIGHTDIRECTION)
1431         {
1432                 if (r_glsl_permutation->loc_AmbientColor >= 0)
1433                         qglUniform3fARB(r_glsl_permutation->loc_AmbientColor , rsurface.modellight_ambient[0] * ambientscale  * rsurface.texture->lightmapcolor[0] * 0.5f, rsurface.modellight_ambient[1] * ambientscale  * rsurface.texture->lightmapcolor[1] * 0.5f, rsurface.modellight_ambient[2] * ambientscale  * rsurface.texture->lightmapcolor[2] * 0.5f);
1434                 if (r_glsl_permutation->loc_DiffuseColor >= 0)
1435                         qglUniform3fARB(r_glsl_permutation->loc_DiffuseColor , rsurface.modellight_diffuse[0] * diffusescale  * rsurface.texture->lightmapcolor[0] * 0.5f, rsurface.modellight_diffuse[1] * diffusescale  * rsurface.texture->lightmapcolor[1] * 0.5f, rsurface.modellight_diffuse[2] * diffusescale  * rsurface.texture->lightmapcolor[2] * 0.5f);
1436                 if (r_glsl_permutation->loc_SpecularColor >= 0)
1437                         qglUniform3fARB(r_glsl_permutation->loc_SpecularColor, rsurface.modellight_diffuse[0] * specularscale * rsurface.texture->lightmapcolor[0] * 0.5f, rsurface.modellight_diffuse[1] * specularscale * rsurface.texture->lightmapcolor[1] * 0.5f, rsurface.modellight_diffuse[2] * specularscale * rsurface.texture->lightmapcolor[2] * 0.5f);
1438                 if (r_glsl_permutation->loc_LightDir >= 0)
1439                         qglUniform3fARB(r_glsl_permutation->loc_LightDir, rsurface.modellight_lightdir[0], rsurface.modellight_lightdir[1], rsurface.modellight_lightdir[2]);
1440         }
1441         else
1442         {
1443                 if (r_glsl_permutation->loc_AmbientScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_AmbientScale, r_ambient.value * 1.0f / 128.0f);
1444                 if (r_glsl_permutation->loc_DiffuseScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_DiffuseScale, r_refdef.lightmapintensity);
1445                 if (r_glsl_permutation->loc_SpecularScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularScale, r_refdef.lightmapintensity * specularscale);
1446         }
1447         if (r_glsl_permutation->loc_TintColor >= 0) qglUniform4fARB(r_glsl_permutation->loc_TintColor, rsurface.texture->lightmapcolor[0], rsurface.texture->lightmapcolor[1], rsurface.texture->lightmapcolor[2], rsurface.texture->lightmapcolor[3]);
1448         if (r_glsl_permutation->loc_GlowScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_GlowScale, r_hdr_glowintensity.value);
1449         if (r_glsl_permutation->loc_ContrastBoostCoeff >= 0)
1450         {
1451                 // The formula used is actually:
1452                 //   color.rgb *= ContrastBoost / ((ContrastBoost - 1) * color.rgb + 1);
1453                 //   color.rgb *= SceneBrightness;
1454                 // simplified:
1455                 //   color.rgb = [[SceneBrightness * ContrastBoost]] * color.rgb / ([[ContrastBoost - 1]] * color.rgb + 1);
1456                 // and do [[calculations]] here in the engine
1457                 qglUniform1fARB(r_glsl_permutation->loc_ContrastBoostCoeff, r_glsl_contrastboost.value - 1);
1458                 if (r_glsl_permutation->loc_SceneBrightness >= 0) qglUniform1fARB(r_glsl_permutation->loc_SceneBrightness, r_view.colorscale * r_glsl_contrastboost.value);
1459         }
1460         else
1461                 if (r_glsl_permutation->loc_SceneBrightness >= 0) qglUniform1fARB(r_glsl_permutation->loc_SceneBrightness, r_view.colorscale);
1462         if (r_glsl_permutation->loc_FogColor >= 0)
1463         {
1464                 // additive passes are only darkened by fog, not tinted
1465                 if (rsurface.rtlight || (rsurface.texture->currentmaterialflags & MATERIALFLAG_ADD))
1466                         qglUniform3fARB(r_glsl_permutation->loc_FogColor, 0, 0, 0);
1467                 else
1468                         qglUniform3fARB(r_glsl_permutation->loc_FogColor, r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2]);
1469         }
1470         if (r_glsl_permutation->loc_EyePosition >= 0) qglUniform3fARB(r_glsl_permutation->loc_EyePosition, rsurface.modelorg[0], rsurface.modelorg[1], rsurface.modelorg[2]);
1471         if (r_glsl_permutation->loc_Color_Pants >= 0)
1472         {
1473                 if (rsurface.texture->currentskinframe->pants)
1474                         qglUniform3fARB(r_glsl_permutation->loc_Color_Pants, rsurface.colormap_pantscolor[0], rsurface.colormap_pantscolor[1], rsurface.colormap_pantscolor[2]);
1475                 else
1476                         qglUniform3fARB(r_glsl_permutation->loc_Color_Pants, 0, 0, 0);
1477         }
1478         if (r_glsl_permutation->loc_Color_Shirt >= 0)
1479         {
1480                 if (rsurface.texture->currentskinframe->shirt)
1481                         qglUniform3fARB(r_glsl_permutation->loc_Color_Shirt, rsurface.colormap_shirtcolor[0], rsurface.colormap_shirtcolor[1], rsurface.colormap_shirtcolor[2]);
1482                 else
1483                         qglUniform3fARB(r_glsl_permutation->loc_Color_Shirt, 0, 0, 0);
1484         }
1485         if (r_glsl_permutation->loc_FogRangeRecip >= 0) qglUniform1fARB(r_glsl_permutation->loc_FogRangeRecip, r_refdef.fograngerecip);
1486         if (r_glsl_permutation->loc_SpecularPower >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularPower, rsurface.texture->specularpower);
1487         if (r_glsl_permutation->loc_OffsetMapping_Scale >= 0) qglUniform1fARB(r_glsl_permutation->loc_OffsetMapping_Scale, r_glsl_offsetmapping_scale.value);
1488         if (r_glsl_permutation->loc_DistortScaleRefractReflect >= 0) qglUniform4fARB(r_glsl_permutation->loc_DistortScaleRefractReflect, r_water_refractdistort.value * rsurface.texture->refractfactor, r_water_refractdistort.value * rsurface.texture->refractfactor, r_water_reflectdistort.value * rsurface.texture->reflectfactor, r_water_reflectdistort.value * rsurface.texture->reflectfactor);
1489         if (r_glsl_permutation->loc_ScreenScaleRefractReflect >= 0) qglUniform4fARB(r_glsl_permutation->loc_ScreenScaleRefractReflect, r_waterstate.screenscale[0], r_waterstate.screenscale[1], r_waterstate.screenscale[0], r_waterstate.screenscale[1]);
1490         if (r_glsl_permutation->loc_ScreenCenterRefractReflect >= 0) qglUniform4fARB(r_glsl_permutation->loc_ScreenCenterRefractReflect, r_waterstate.screencenter[0], r_waterstate.screencenter[1], r_waterstate.screencenter[0], r_waterstate.screencenter[1]);
1491         if (r_glsl_permutation->loc_RefractColor >= 0) qglUniform4fvARB(r_glsl_permutation->loc_RefractColor, 1, rsurface.texture->refractcolor4f);
1492         if (r_glsl_permutation->loc_ReflectColor >= 0) qglUniform4fvARB(r_glsl_permutation->loc_ReflectColor, 1, rsurface.texture->reflectcolor4f);
1493         if (r_glsl_permutation->loc_ReflectFactor >= 0) qglUniform1fARB(r_glsl_permutation->loc_ReflectFactor, rsurface.texture->reflectmax - rsurface.texture->reflectmin);
1494         if (r_glsl_permutation->loc_ReflectOffset >= 0) qglUniform1fARB(r_glsl_permutation->loc_ReflectOffset, rsurface.texture->reflectmin);
1495         CHECKGLERROR
1496         return permutation;
1497 }
1498
1499 #define SKINFRAME_HASH 1024
1500
1501 struct
1502 {
1503         int loadsequence; // incremented each level change
1504         memexpandablearray_t array;
1505         skinframe_t *hash[SKINFRAME_HASH];
1506 }
1507 r_skinframe;
1508
1509 void R_SkinFrame_PrepareForPurge(void)
1510 {
1511         r_skinframe.loadsequence++;
1512         // wrap it without hitting zero
1513         if (r_skinframe.loadsequence >= 200)
1514                 r_skinframe.loadsequence = 1;
1515 }
1516
1517 void R_SkinFrame_MarkUsed(skinframe_t *skinframe)
1518 {
1519         if (!skinframe)
1520                 return;
1521         // mark the skinframe as used for the purging code
1522         skinframe->loadsequence = r_skinframe.loadsequence;
1523 }
1524
1525 void R_SkinFrame_Purge(void)
1526 {
1527         int i;
1528         skinframe_t *s;
1529         for (i = 0;i < SKINFRAME_HASH;i++)
1530         {
1531                 for (s = r_skinframe.hash[i];s;s = s->next)
1532                 {
1533                         if (s->loadsequence && s->loadsequence != r_skinframe.loadsequence)
1534                         {
1535                                 if (s->merged == s->base)
1536                                         s->merged = NULL;
1537                                 // FIXME: maybe pass a pointer to the pointer to R_PurgeTexture and reset it to NULL inside? [11/29/2007 Black]
1538                                 R_PurgeTexture(s->stain );s->stain  = NULL;
1539                                 R_PurgeTexture(s->merged);s->merged = NULL;
1540                                 R_PurgeTexture(s->base  );s->base   = NULL;
1541                                 R_PurgeTexture(s->pants );s->pants  = NULL;
1542                                 R_PurgeTexture(s->shirt );s->shirt  = NULL;
1543                                 R_PurgeTexture(s->nmap  );s->nmap   = NULL;
1544                                 R_PurgeTexture(s->gloss );s->gloss  = NULL;
1545                                 R_PurgeTexture(s->glow  );s->glow   = NULL;
1546                                 R_PurgeTexture(s->fog   );s->fog    = NULL;
1547                                 s->loadsequence = 0;
1548                         }
1549                 }
1550         }
1551 }
1552
1553 skinframe_t *R_SkinFrame_FindNextByName( skinframe_t *last, const char *name ) {
1554         skinframe_t *item;
1555         char basename[MAX_QPATH];
1556
1557         Image_StripImageExtension(name, basename, sizeof(basename));
1558
1559         if( last == NULL ) {
1560                 int hashindex;
1561                 hashindex = CRC_Block((unsigned char *)basename, strlen(basename)) & (SKINFRAME_HASH - 1);
1562                 item = r_skinframe.hash[hashindex];
1563         } else {
1564                 item = last->next;
1565         }
1566
1567         // linearly search through the hash bucket
1568         for( ; item ; item = item->next ) {
1569                 if( !strcmp( item->basename, basename ) ) {
1570                         return item;
1571                 }
1572         }
1573         return NULL;
1574 }
1575
1576 skinframe_t *R_SkinFrame_Find(const char *name, int textureflags, int comparewidth, int compareheight, int comparecrc, qboolean add)
1577 {
1578         skinframe_t *item;
1579         int hashindex;
1580         char basename[MAX_QPATH];
1581
1582         Image_StripImageExtension(name, basename, sizeof(basename));
1583
1584         hashindex = CRC_Block((unsigned char *)basename, strlen(basename)) & (SKINFRAME_HASH - 1);
1585         for (item = r_skinframe.hash[hashindex];item;item = item->next)
1586                 if (!strcmp(item->basename, basename) && item->textureflags == textureflags && item->comparewidth == comparewidth && item->compareheight == compareheight && item->comparecrc == comparecrc)
1587                         break;
1588
1589         if (!item) {
1590                 rtexture_t *dyntexture;
1591                 // check whether its a dynamic texture
1592                 dyntexture = CL_GetDynTexture( basename );
1593                 if (!add && !dyntexture)
1594                         return NULL;
1595                 item = (skinframe_t *)Mem_ExpandableArray_AllocRecord(&r_skinframe.array);
1596                 memset(item, 0, sizeof(*item));
1597                 strlcpy(item->basename, basename, sizeof(item->basename));
1598                 item->base = dyntexture; // either NULL or dyntexture handle
1599                 item->textureflags = textureflags;
1600                 item->comparewidth = comparewidth;
1601                 item->compareheight = compareheight;
1602                 item->comparecrc = comparecrc;
1603                 item->next = r_skinframe.hash[hashindex];
1604                 r_skinframe.hash[hashindex] = item;
1605         }
1606         else if( item->base == NULL )
1607         {
1608                 rtexture_t *dyntexture;
1609                 // check whether its a dynamic texture
1610                 // this only needs to be done because Purge doesnt delete skinframes - only sets the texture pointers to NULL and we need to restore it before returing.. [11/29/2007 Black]
1611                 dyntexture = CL_GetDynTexture( basename );
1612                 item->base = dyntexture; // either NULL or dyntexture handle
1613         }
1614
1615         R_SkinFrame_MarkUsed(item);
1616         return item;
1617 }
1618
1619 skinframe_t *R_SkinFrame_LoadExternal(const char *name, int textureflags, qboolean complain)
1620 {
1621         // FIXME: it should be possible to disable loading various layers using
1622         // cvars, to prevent wasted loading time and memory usage if the user does
1623         // not want them
1624         qboolean loadnormalmap = true;
1625         qboolean loadgloss = true;
1626         qboolean loadpantsandshirt = true;
1627         qboolean loadglow = true;
1628         int j;
1629         unsigned char *pixels;
1630         unsigned char *bumppixels;
1631         unsigned char *basepixels = NULL;
1632         int basepixels_width;
1633         int basepixels_height;
1634         skinframe_t *skinframe;
1635
1636         if (cls.state == ca_dedicated)
1637                 return NULL;
1638
1639         // return an existing skinframe if already loaded
1640         // if loading of the first image fails, don't make a new skinframe as it
1641         // would cause all future lookups of this to be missing
1642         skinframe = R_SkinFrame_Find(name, textureflags, 0, 0, 0, false);
1643         if (skinframe && skinframe->base)
1644                 return skinframe;
1645
1646         basepixels = loadimagepixelsbgra(name, complain, true);
1647         if (basepixels == NULL)
1648                 return NULL;
1649
1650         // we've got some pixels to store, so really allocate this new texture now
1651         if (!skinframe)
1652                 skinframe = R_SkinFrame_Find(name, textureflags, 0, 0, 0, true);
1653         skinframe->stain = NULL;
1654         skinframe->merged = NULL;
1655         skinframe->base = r_texture_notexture;
1656         skinframe->pants = NULL;
1657         skinframe->shirt = NULL;
1658         skinframe->nmap = r_texture_blanknormalmap;
1659         skinframe->gloss = NULL;
1660         skinframe->glow = NULL;
1661         skinframe->fog = NULL;
1662
1663         basepixels_width = image_width;
1664         basepixels_height = image_height;
1665         skinframe->base = R_LoadTexture2D (r_main_texturepool, skinframe->basename, basepixels_width, basepixels_height, basepixels, TEXTYPE_BGRA, skinframe->textureflags & (gl_texturecompression_color.integer ? ~0 : ~TEXF_COMPRESS), NULL);
1666
1667         if (textureflags & TEXF_ALPHA)
1668         {
1669                 for (j = 3;j < basepixels_width * basepixels_height * 4;j += 4)
1670                         if (basepixels[j] < 255)
1671                                 break;
1672                 if (j < basepixels_width * basepixels_height * 4)
1673                 {
1674                         // has transparent pixels
1675                         pixels = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
1676                         for (j = 0;j < image_width * image_height * 4;j += 4)
1677                         {
1678                                 pixels[j+0] = 255;
1679                                 pixels[j+1] = 255;
1680                                 pixels[j+2] = 255;
1681                                 pixels[j+3] = basepixels[j+3];
1682                         }
1683                         skinframe->fog = R_LoadTexture2D (r_main_texturepool, va("%s_mask", skinframe->basename), image_width, image_height, pixels, TEXTYPE_BGRA, skinframe->textureflags & (gl_texturecompression_color.integer ? ~0 : ~TEXF_COMPRESS), NULL);
1684                         Mem_Free(pixels);
1685                 }
1686         }
1687
1688         // _norm is the name used by tenebrae and has been adopted as standard
1689         if (loadnormalmap)
1690         {
1691                 if ((pixels = loadimagepixelsbgra(va("%s_norm", skinframe->basename), false, false)) != NULL)
1692                 {
1693                         skinframe->nmap = R_LoadTexture2D (r_main_texturepool, va("%s_nmap", skinframe->basename), image_width, image_height, pixels, TEXTYPE_BGRA, skinframe->textureflags & (gl_texturecompression_normal.integer ? ~0 : ~TEXF_COMPRESS), NULL);
1694                         Mem_Free(pixels);
1695                         pixels = NULL;
1696                 }
1697                 else if (r_shadow_bumpscale_bumpmap.value > 0 && (bumppixels = loadimagepixelsbgra(va("%s_bump", skinframe->basename), false, false)) != NULL)
1698                 {
1699                         pixels = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
1700                         Image_HeightmapToNormalmap_BGRA(bumppixels, pixels, image_width, image_height, false, r_shadow_bumpscale_bumpmap.value);
1701                         skinframe->nmap = R_LoadTexture2D (r_main_texturepool, va("%s_nmap", skinframe->basename), image_width, image_height, pixels, TEXTYPE_BGRA, skinframe->textureflags & (gl_texturecompression_normal.integer ? ~0 : ~TEXF_COMPRESS), NULL);
1702                         Mem_Free(pixels);
1703                         Mem_Free(bumppixels);
1704                 }
1705                 else if (r_shadow_bumpscale_basetexture.value > 0)
1706                 {
1707                         pixels = (unsigned char *)Mem_Alloc(tempmempool, basepixels_width * basepixels_height * 4);
1708                         Image_HeightmapToNormalmap_BGRA(basepixels, pixels, basepixels_width, basepixels_height, false, r_shadow_bumpscale_basetexture.value);
1709                         skinframe->nmap = R_LoadTexture2D (r_main_texturepool, va("%s_nmap", skinframe->basename), basepixels_width, basepixels_height, pixels, TEXTYPE_BGRA, skinframe->textureflags & (gl_texturecompression_normal.integer ? ~0 : ~TEXF_COMPRESS), NULL);
1710                         Mem_Free(pixels);
1711                 }
1712         }
1713         // _luma is supported for tenebrae compatibility
1714         // (I think it's a very stupid name, but oh well)
1715         // _glow is the preferred name
1716         if (loadglow          && ((pixels = loadimagepixelsbgra(va("%s_glow", skinframe->basename), false, false)) != NULL || (pixels = loadimagepixelsbgra(va("%s_luma", skinframe->basename), false, false)) != NULL)) {skinframe->glow = R_LoadTexture2D (r_main_texturepool, va("%s_glow", skinframe->basename), image_width, image_height, pixels, TEXTYPE_BGRA, skinframe->textureflags & (gl_texturecompression_glow.integer ? ~0 : ~TEXF_COMPRESS), NULL);Mem_Free(pixels);pixels = NULL;}
1717         if (loadgloss         && (pixels = loadimagepixelsbgra(va("%s_gloss", skinframe->basename), false, false)) != NULL) {skinframe->gloss = R_LoadTexture2D (r_main_texturepool, va("%s_gloss", skinframe->basename), image_width, image_height, pixels, TEXTYPE_BGRA, skinframe->textureflags & (gl_texturecompression_gloss.integer ? ~0 : ~TEXF_COMPRESS), NULL);Mem_Free(pixels);pixels = NULL;}
1718         if (loadpantsandshirt && (pixels = loadimagepixelsbgra(va("%s_pants", skinframe->basename), false, false)) != NULL) {skinframe->pants = R_LoadTexture2D (r_main_texturepool, va("%s_pants", skinframe->basename), image_width, image_height, pixels, TEXTYPE_BGRA, skinframe->textureflags & (gl_texturecompression_color.integer ? ~0 : ~TEXF_COMPRESS), NULL);Mem_Free(pixels);pixels = NULL;}
1719         if (loadpantsandshirt && (pixels = loadimagepixelsbgra(va("%s_shirt", skinframe->basename), false, false)) != NULL) {skinframe->shirt = R_LoadTexture2D (r_main_texturepool, va("%s_shirt", skinframe->basename), image_width, image_height, pixels, TEXTYPE_BGRA, skinframe->textureflags & (gl_texturecompression_color.integer ? ~0 : ~TEXF_COMPRESS), NULL);Mem_Free(pixels);pixels = NULL;}
1720
1721         if (basepixels)
1722                 Mem_Free(basepixels);
1723
1724         return skinframe;
1725 }
1726
1727 static rtexture_t *R_SkinFrame_TextureForSkinLayer(const unsigned char *in, int width, int height, const char *name, const unsigned int *palette, int textureflags, qboolean force)
1728 {
1729         int i;
1730         if (!force)
1731         {
1732                 for (i = 0;i < width*height;i++)
1733                         if (((unsigned char *)&palette[in[i]])[3] > 0)
1734                                 break;
1735                 if (i == width*height)
1736                         return NULL;
1737         }
1738         return R_LoadTexture2D (r_main_texturepool, name, width, height, in, TEXTYPE_PALETTE, textureflags, palette);
1739 }
1740
1741 // this is only used by .spr32 sprites, HL .spr files, HL .bsp files
1742 skinframe_t *R_SkinFrame_LoadInternalBGRA(const char *name, int textureflags, const unsigned char *skindata, int width, int height)
1743 {
1744         int i;
1745         unsigned char *temp1, *temp2;
1746         skinframe_t *skinframe;
1747
1748         if (cls.state == ca_dedicated)
1749                 return NULL;
1750
1751         // if already loaded just return it, otherwise make a new skinframe
1752         skinframe = R_SkinFrame_Find(name, textureflags, width, height, skindata ? CRC_Block(skindata, width*height*4) : 0, true);
1753         if (skinframe && skinframe->base)
1754                 return skinframe;
1755
1756         skinframe->stain = NULL;
1757         skinframe->merged = NULL;
1758         skinframe->base = r_texture_notexture;
1759         skinframe->pants = NULL;
1760         skinframe->shirt = NULL;
1761         skinframe->nmap = r_texture_blanknormalmap;
1762         skinframe->gloss = NULL;
1763         skinframe->glow = NULL;
1764         skinframe->fog = NULL;
1765
1766         // if no data was provided, then clearly the caller wanted to get a blank skinframe
1767         if (!skindata)
1768                 return NULL;
1769
1770         if (r_shadow_bumpscale_basetexture.value > 0)
1771         {
1772                 temp1 = (unsigned char *)Mem_Alloc(tempmempool, width * height * 8);
1773                 temp2 = temp1 + width * height * 4;
1774                 Image_HeightmapToNormalmap_BGRA(skindata, temp2, width, height, false, r_shadow_bumpscale_basetexture.value);
1775                 skinframe->nmap = R_LoadTexture2D(r_main_texturepool, va("%s_nmap", skinframe->basename), width, height, temp2, TEXTYPE_BGRA, skinframe->textureflags | TEXF_ALPHA, NULL);
1776                 Mem_Free(temp1);
1777         }
1778         skinframe->base = skinframe->merged = R_LoadTexture2D(r_main_texturepool, skinframe->basename, width, height, skindata, TEXTYPE_BGRA, skinframe->textureflags, NULL);
1779         if (textureflags & TEXF_ALPHA)
1780         {
1781                 for (i = 3;i < width * height * 4;i += 4)
1782                         if (skindata[i] < 255)
1783                                 break;
1784                 if (i < width * height * 4)
1785                 {
1786                         unsigned char *fogpixels = (unsigned char *)Mem_Alloc(tempmempool, width * height * 4);
1787                         memcpy(fogpixels, skindata, width * height * 4);
1788                         for (i = 0;i < width * height * 4;i += 4)
1789                                 fogpixels[i] = fogpixels[i+1] = fogpixels[i+2] = 255;
1790                         skinframe->fog = R_LoadTexture2D(r_main_texturepool, va("%s_fog", skinframe->basename), width, height, fogpixels, TEXTYPE_BGRA, skinframe->textureflags, NULL);
1791                         Mem_Free(fogpixels);
1792                 }
1793         }
1794
1795         return skinframe;
1796 }
1797
1798 skinframe_t *R_SkinFrame_LoadInternalQuake(const char *name, int textureflags, int loadpantsandshirt, int loadglowtexture, const unsigned char *skindata, int width, int height)
1799 {
1800         int i;
1801         unsigned char *temp1, *temp2;
1802         skinframe_t *skinframe;
1803
1804         if (cls.state == ca_dedicated)
1805                 return NULL;
1806
1807         // if already loaded just return it, otherwise make a new skinframe
1808         skinframe = R_SkinFrame_Find(name, textureflags, width, height, skindata ? CRC_Block(skindata, width*height) : 0, true);
1809         if (skinframe && skinframe->base)
1810                 return skinframe;
1811
1812         skinframe->stain = NULL;
1813         skinframe->merged = NULL;
1814         skinframe->base = r_texture_notexture;
1815         skinframe->pants = NULL;
1816         skinframe->shirt = NULL;
1817         skinframe->nmap = r_texture_blanknormalmap;
1818         skinframe->gloss = NULL;
1819         skinframe->glow = NULL;
1820         skinframe->fog = NULL;
1821
1822         // if no data was provided, then clearly the caller wanted to get a blank skinframe
1823         if (!skindata)
1824                 return NULL;
1825
1826         if (r_shadow_bumpscale_basetexture.value > 0)
1827         {
1828                 temp1 = (unsigned char *)Mem_Alloc(tempmempool, width * height * 8);
1829                 temp2 = temp1 + width * height * 4;
1830                 // use either a custom palette or the quake palette
1831                 Image_Copy8bitBGRA(skindata, temp1, width * height, palette_bgra_complete);
1832                 Image_HeightmapToNormalmap_BGRA(temp1, temp2, width, height, false, r_shadow_bumpscale_basetexture.value);
1833                 skinframe->nmap = R_LoadTexture2D(r_main_texturepool, va("%s_nmap", skinframe->basename), width, height, temp2, TEXTYPE_BGRA, skinframe->textureflags | TEXF_ALPHA, NULL);
1834                 Mem_Free(temp1);
1835         }
1836         // use either a custom palette, or the quake palette
1837         skinframe->base = skinframe->merged = R_SkinFrame_TextureForSkinLayer(skindata, width, height, va("%s_merged", skinframe->basename), (loadglowtexture ? palette_bgra_nofullbrights : ((skinframe->textureflags & TEXF_ALPHA) ? palette_bgra_transparent : palette_bgra_complete)), skinframe->textureflags, true); // all
1838         if (loadglowtexture)
1839                 skinframe->glow = R_SkinFrame_TextureForSkinLayer(skindata, width, height, va("%s_glow", skinframe->basename), palette_bgra_onlyfullbrights, skinframe->textureflags, false); // glow
1840         if (loadpantsandshirt)
1841         {
1842                 skinframe->pants = R_SkinFrame_TextureForSkinLayer(skindata, width, height, va("%s_pants", skinframe->basename), palette_bgra_pantsaswhite, skinframe->textureflags, false); // pants
1843                 skinframe->shirt = R_SkinFrame_TextureForSkinLayer(skindata, width, height, va("%s_shirt", skinframe->basename), palette_bgra_shirtaswhite, skinframe->textureflags, false); // shirt
1844         }
1845         if (skinframe->pants || skinframe->shirt)
1846                 skinframe->base = R_SkinFrame_TextureForSkinLayer(skindata, width, height, va("%s_nospecial", skinframe->basename), loadglowtexture ? palette_bgra_nocolormapnofullbrights : palette_bgra_nocolormap, skinframe->textureflags, false); // no special colors
1847         if (textureflags & TEXF_ALPHA)
1848         {
1849                 for (i = 0;i < width * height;i++)
1850                         if (((unsigned char *)palette_bgra_alpha)[skindata[i]*4+3] < 255)
1851                                 break;
1852                 if (i < width * height)
1853                         skinframe->fog = R_SkinFrame_TextureForSkinLayer(skindata, width, height, va("%s_fog", skinframe->basename), palette_bgra_alpha, skinframe->textureflags, true); // fog mask
1854         }
1855
1856         return skinframe;
1857 }
1858
1859 skinframe_t *R_SkinFrame_LoadMissing(void)
1860 {
1861         skinframe_t *skinframe;
1862
1863         if (cls.state == ca_dedicated)
1864                 return NULL;
1865
1866         skinframe = R_SkinFrame_Find("missing", TEXF_PRECACHE, 0, 0, 0, true);
1867         skinframe->stain = NULL;
1868         skinframe->merged = NULL;
1869         skinframe->base = r_texture_notexture;
1870         skinframe->pants = NULL;
1871         skinframe->shirt = NULL;
1872         skinframe->nmap = r_texture_blanknormalmap;
1873         skinframe->gloss = NULL;
1874         skinframe->glow = NULL;
1875         skinframe->fog = NULL;
1876
1877         return skinframe;
1878 }
1879
1880 void gl_main_start(void)
1881 {
1882         memset(r_qwskincache, 0, sizeof(r_qwskincache));
1883         memset(r_qwskincache_skinframe, 0, sizeof(r_qwskincache_skinframe));
1884
1885         // set up r_skinframe loading system for textures
1886         memset(&r_skinframe, 0, sizeof(r_skinframe));
1887         r_skinframe.loadsequence = 1;
1888         Mem_ExpandableArray_NewArray(&r_skinframe.array, r_main_mempool, sizeof(skinframe_t), 256);
1889
1890         r_main_texturepool = R_AllocTexturePool();
1891         R_BuildBlankTextures();
1892         R_BuildNoTexture();
1893         if (gl_texturecubemap)
1894         {
1895                 R_BuildWhiteCube();
1896                 R_BuildNormalizationCube();
1897         }
1898         r_texture_fogattenuation = NULL;
1899         //r_texture_fogintensity = NULL;
1900         memset(&r_bloomstate, 0, sizeof(r_bloomstate));
1901         memset(&r_waterstate, 0, sizeof(r_waterstate));
1902         memset(r_glsl_permutations, 0, sizeof(r_glsl_permutations));
1903         memset(&r_svbsp, 0, sizeof (r_svbsp));
1904
1905         r_refdef.fogmasktable_density = 0;
1906 }
1907
1908 void gl_main_shutdown(void)
1909 {
1910         memset(r_qwskincache, 0, sizeof(r_qwskincache));
1911         memset(r_qwskincache_skinframe, 0, sizeof(r_qwskincache_skinframe));
1912
1913         // clear out the r_skinframe state
1914         Mem_ExpandableArray_FreeArray(&r_skinframe.array);
1915         memset(&r_skinframe, 0, sizeof(r_skinframe));
1916
1917         if (r_svbsp.nodes)
1918                 Mem_Free(r_svbsp.nodes);
1919         memset(&r_svbsp, 0, sizeof (r_svbsp));
1920         R_FreeTexturePool(&r_main_texturepool);
1921         r_texture_blanknormalmap = NULL;
1922         r_texture_white = NULL;
1923         r_texture_grey128 = NULL;
1924         r_texture_black = NULL;
1925         r_texture_whitecube = NULL;
1926         r_texture_normalizationcube = NULL;
1927         r_texture_fogattenuation = NULL;
1928         //r_texture_fogintensity = NULL;
1929         memset(&r_bloomstate, 0, sizeof(r_bloomstate));
1930         memset(&r_waterstate, 0, sizeof(r_waterstate));
1931         R_GLSL_Restart_f();
1932 }
1933
1934 extern void CL_ParseEntityLump(char *entitystring);
1935 void gl_main_newmap(void)
1936 {
1937         // FIXME: move this code to client
1938         int l;
1939         char *entities, entname[MAX_QPATH];
1940         if (cl.worldmodel)
1941         {
1942                 strlcpy(entname, cl.worldmodel->name, sizeof(entname));
1943                 l = (int)strlen(entname) - 4;
1944                 if (l >= 0 && !strcmp(entname + l, ".bsp"))
1945                 {
1946                         memcpy(entname + l, ".ent", 5);
1947                         if ((entities = (char *)FS_LoadFile(entname, tempmempool, true, NULL)))
1948                         {
1949                                 CL_ParseEntityLump(entities);
1950                                 Mem_Free(entities);
1951                                 return;
1952                         }
1953                 }
1954                 if (cl.worldmodel->brush.entities)
1955                         CL_ParseEntityLump(cl.worldmodel->brush.entities);
1956         }
1957 }
1958
1959 void GL_Main_Init(void)
1960 {
1961         r_main_mempool = Mem_AllocPool("Renderer", 0, NULL);
1962
1963         Cmd_AddCommand("r_glsl_restart", R_GLSL_Restart_f, "unloads GLSL shaders, they will then be reloaded as needed");
1964         Cmd_AddCommand("r_glsl_dumpshader", R_GLSL_DumpShader_f, "dumps the engine internal default.glsl shader into glsl/default.glsl");
1965         // FIXME: the client should set up r_refdef.fog stuff including the fogmasktable
1966         if (gamemode == GAME_NEHAHRA)
1967         {
1968                 Cvar_RegisterVariable (&gl_fogenable);
1969                 Cvar_RegisterVariable (&gl_fogdensity);
1970                 Cvar_RegisterVariable (&gl_fogred);
1971                 Cvar_RegisterVariable (&gl_foggreen);
1972                 Cvar_RegisterVariable (&gl_fogblue);
1973                 Cvar_RegisterVariable (&gl_fogstart);
1974                 Cvar_RegisterVariable (&gl_fogend);
1975                 Cvar_RegisterVariable (&gl_skyclip);
1976         }
1977         Cvar_RegisterVariable(&r_depthfirst);
1978         Cvar_RegisterVariable(&r_nearclip);
1979         Cvar_RegisterVariable(&r_showbboxes);
1980         Cvar_RegisterVariable(&r_showsurfaces);
1981         Cvar_RegisterVariable(&r_showtris);
1982         Cvar_RegisterVariable(&r_shownormals);
1983         Cvar_RegisterVariable(&r_showlighting);
1984         Cvar_RegisterVariable(&r_showshadowvolumes);
1985         Cvar_RegisterVariable(&r_showcollisionbrushes);
1986         Cvar_RegisterVariable(&r_showcollisionbrushes_polygonfactor);
1987         Cvar_RegisterVariable(&r_showcollisionbrushes_polygonoffset);
1988         Cvar_RegisterVariable(&r_showdisabledepthtest);
1989         Cvar_RegisterVariable(&r_drawportals);
1990         Cvar_RegisterVariable(&r_drawentities);
1991         Cvar_RegisterVariable(&r_cullentities_trace);
1992         Cvar_RegisterVariable(&r_cullentities_trace_samples);
1993         Cvar_RegisterVariable(&r_cullentities_trace_enlarge);
1994         Cvar_RegisterVariable(&r_cullentities_trace_delay);
1995         Cvar_RegisterVariable(&r_drawviewmodel);
1996         Cvar_RegisterVariable(&r_speeds);
1997         Cvar_RegisterVariable(&r_fullbrights);
1998         Cvar_RegisterVariable(&r_wateralpha);
1999         Cvar_RegisterVariable(&r_dynamic);
2000         Cvar_RegisterVariable(&r_fullbright);
2001         Cvar_RegisterVariable(&r_shadows);
2002         Cvar_RegisterVariable(&r_shadows_throwdistance);
2003         Cvar_RegisterVariable(&r_q1bsp_skymasking);
2004         Cvar_RegisterVariable(&r_polygonoffset_submodel_factor);
2005         Cvar_RegisterVariable(&r_polygonoffset_submodel_offset);
2006         Cvar_RegisterVariable(&r_fog_exp2);
2007         Cvar_RegisterVariable(&r_textureunits);
2008         Cvar_RegisterVariable(&r_glsl);
2009         Cvar_RegisterVariable(&r_glsl_offsetmapping);
2010         Cvar_RegisterVariable(&r_glsl_offsetmapping_reliefmapping);
2011         Cvar_RegisterVariable(&r_glsl_offsetmapping_scale);
2012         Cvar_RegisterVariable(&r_glsl_deluxemapping);
2013         Cvar_RegisterVariable(&r_water);
2014         Cvar_RegisterVariable(&r_water_resolutionmultiplier);
2015         Cvar_RegisterVariable(&r_water_clippingplanebias);
2016         Cvar_RegisterVariable(&r_water_refractdistort);
2017         Cvar_RegisterVariable(&r_water_reflectdistort);
2018         Cvar_RegisterVariable(&r_lerpsprites);
2019         Cvar_RegisterVariable(&r_lerpmodels);
2020         Cvar_RegisterVariable(&r_lerplightstyles);
2021         Cvar_RegisterVariable(&r_waterscroll);
2022         Cvar_RegisterVariable(&r_bloom);
2023         Cvar_RegisterVariable(&r_bloom_colorscale);
2024         Cvar_RegisterVariable(&r_bloom_brighten);
2025         Cvar_RegisterVariable(&r_bloom_blur);
2026         Cvar_RegisterVariable(&r_bloom_resolution);
2027         Cvar_RegisterVariable(&r_bloom_colorexponent);
2028         Cvar_RegisterVariable(&r_bloom_colorsubtract);
2029         Cvar_RegisterVariable(&r_hdr);
2030         Cvar_RegisterVariable(&r_hdr_scenebrightness);
2031         Cvar_RegisterVariable(&r_glsl_contrastboost);
2032         Cvar_RegisterVariable(&r_hdr_glowintensity);
2033         Cvar_RegisterVariable(&r_hdr_range);
2034         Cvar_RegisterVariable(&r_smoothnormals_areaweighting);
2035         Cvar_RegisterVariable(&developer_texturelogging);
2036         Cvar_RegisterVariable(&gl_lightmaps);
2037         Cvar_RegisterVariable(&r_test);
2038         Cvar_RegisterVariable(&r_batchmode);
2039         if (gamemode == GAME_NEHAHRA || gamemode == GAME_TENEBRAE)
2040                 Cvar_SetValue("r_fullbrights", 0);
2041         R_RegisterModule("GL_Main", gl_main_start, gl_main_shutdown, gl_main_newmap);
2042
2043         Cvar_RegisterVariable(&r_track_sprites);
2044         Cvar_RegisterVariable(&r_track_sprites_flags);
2045         Cvar_RegisterVariable(&r_track_sprites_scalew);
2046         Cvar_RegisterVariable(&r_track_sprites_scaleh);
2047 }
2048
2049 extern void R_Textures_Init(void);
2050 extern void GL_Draw_Init(void);
2051 extern void GL_Main_Init(void);
2052 extern void R_Shadow_Init(void);
2053 extern void R_Sky_Init(void);
2054 extern void GL_Surf_Init(void);
2055 extern void R_Particles_Init(void);
2056 extern void R_Explosion_Init(void);
2057 extern void gl_backend_init(void);
2058 extern void Sbar_Init(void);
2059 extern void R_LightningBeams_Init(void);
2060 extern void Mod_RenderInit(void);
2061
2062 void Render_Init(void)
2063 {
2064         gl_backend_init();
2065         R_Textures_Init();
2066         GL_Main_Init();
2067         GL_Draw_Init();
2068         R_Shadow_Init();
2069         R_Sky_Init();
2070         GL_Surf_Init();
2071         Sbar_Init();
2072         R_Particles_Init();
2073         R_Explosion_Init();
2074         R_LightningBeams_Init();
2075         Mod_RenderInit();
2076 }
2077
2078 /*
2079 ===============
2080 GL_Init
2081 ===============
2082 */
2083 extern char *ENGINE_EXTENSIONS;
2084 void GL_Init (void)
2085 {
2086         VID_CheckExtensions();
2087
2088         // LordHavoc: report supported extensions
2089         Con_DPrintf("\nQuakeC extensions for server and client: %s\nQuakeC extensions for menu: %s\n", vm_sv_extensions, vm_m_extensions );
2090
2091         // clear to black (loading plaque will be seen over this)
2092         CHECKGLERROR
2093         qglClearColor(0,0,0,1);CHECKGLERROR
2094         qglClear(GL_COLOR_BUFFER_BIT);CHECKGLERROR
2095 }
2096
2097 int R_CullBox(const vec3_t mins, const vec3_t maxs)
2098 {
2099         int i;
2100         mplane_t *p;
2101         for (i = 0;i < r_view.numfrustumplanes;i++)
2102         {
2103                 // skip nearclip plane, it often culls portals when you are very close, and is almost never useful
2104                 if (i == 4)
2105                         continue;
2106                 p = r_view.frustum + i;
2107                 switch(p->signbits)
2108                 {
2109                 default:
2110                 case 0:
2111                         if (p->normal[0]*maxs[0] + p->normal[1]*maxs[1] + p->normal[2]*maxs[2] < p->dist)
2112                                 return true;
2113                         break;
2114                 case 1:
2115                         if (p->normal[0]*mins[0] + p->normal[1]*maxs[1] + p->normal[2]*maxs[2] < p->dist)
2116                                 return true;
2117                         break;
2118                 case 2:
2119                         if (p->normal[0]*maxs[0] + p->normal[1]*mins[1] + p->normal[2]*maxs[2] < p->dist)
2120                                 return true;
2121                         break;
2122                 case 3:
2123                         if (p->normal[0]*mins[0] + p->normal[1]*mins[1] + p->normal[2]*maxs[2] < p->dist)
2124                                 return true;
2125                         break;
2126                 case 4:
2127                         if (p->normal[0]*maxs[0] + p->normal[1]*maxs[1] + p->normal[2]*mins[2] < p->dist)
2128                                 return true;
2129                         break;
2130                 case 5:
2131                         if (p->normal[0]*mins[0] + p->normal[1]*maxs[1] + p->normal[2]*mins[2] < p->dist)
2132                                 return true;
2133                         break;
2134                 case 6:
2135                         if (p->normal[0]*maxs[0] + p->normal[1]*mins[1] + p->normal[2]*mins[2] < p->dist)
2136                                 return true;
2137                         break;
2138                 case 7:
2139                         if (p->normal[0]*mins[0] + p->normal[1]*mins[1] + p->normal[2]*mins[2] < p->dist)
2140                                 return true;
2141                         break;
2142                 }
2143         }
2144         return false;
2145 }
2146
2147 int R_CullBoxCustomPlanes(const vec3_t mins, const vec3_t maxs, int numplanes, const mplane_t *planes)
2148 {
2149         int i;
2150         const mplane_t *p;
2151         for (i = 0;i < numplanes;i++)
2152         {
2153                 p = planes + i;
2154                 switch(p->signbits)
2155                 {
2156                 default:
2157                 case 0:
2158                         if (p->normal[0]*maxs[0] + p->normal[1]*maxs[1] + p->normal[2]*maxs[2] < p->dist)
2159                                 return true;
2160                         break;
2161                 case 1:
2162                         if (p->normal[0]*mins[0] + p->normal[1]*maxs[1] + p->normal[2]*maxs[2] < p->dist)
2163                                 return true;
2164                         break;
2165                 case 2:
2166                         if (p->normal[0]*maxs[0] + p->normal[1]*mins[1] + p->normal[2]*maxs[2] < p->dist)
2167                                 return true;
2168                         break;
2169                 case 3:
2170                         if (p->normal[0]*mins[0] + p->normal[1]*mins[1] + p->normal[2]*maxs[2] < p->dist)
2171                                 return true;
2172                         break;
2173                 case 4:
2174                         if (p->normal[0]*maxs[0] + p->normal[1]*maxs[1] + p->normal[2]*mins[2] < p->dist)
2175                                 return true;
2176                         break;
2177                 case 5:
2178                         if (p->normal[0]*mins[0] + p->normal[1]*maxs[1] + p->normal[2]*mins[2] < p->dist)
2179                                 return true;
2180                         break;
2181                 case 6:
2182                         if (p->normal[0]*maxs[0] + p->normal[1]*mins[1] + p->normal[2]*mins[2] < p->dist)
2183                                 return true;
2184                         break;
2185                 case 7:
2186                         if (p->normal[0]*mins[0] + p->normal[1]*mins[1] + p->normal[2]*mins[2] < p->dist)
2187                                 return true;
2188                         break;
2189                 }
2190         }
2191         return false;
2192 }
2193
2194 //==================================================================================
2195
2196 static void R_View_UpdateEntityVisible (void)
2197 {
2198         int i, renderimask;
2199         entity_render_t *ent;
2200
2201         if (!r_drawentities.integer)
2202                 return;
2203
2204         renderimask = r_refdef.envmap ? (RENDER_EXTERIORMODEL | RENDER_VIEWMODEL) : ((chase_active.integer || r_waterstate.renderingscene) ? RENDER_VIEWMODEL : RENDER_EXTERIORMODEL);
2205         if (r_refdef.worldmodel && r_refdef.worldmodel->brush.BoxTouchingVisibleLeafs)
2206         {
2207                 // worldmodel can check visibility
2208                 for (i = 0;i < r_refdef.numentities;i++)
2209                 {
2210                         ent = r_refdef.entities[i];
2211                         r_viewcache.entityvisible[i] = !(ent->flags & renderimask) && ((ent->model && ent->model->type == mod_sprite && (ent->model->sprite.sprnum_type == SPR_LABEL || ent->model->sprite.sprnum_type == SPR_LABEL_SCALE)) || !R_CullBox(ent->mins, ent->maxs)) && ((ent->effects & EF_NODEPTHTEST) || (ent->flags & RENDER_VIEWMODEL) || r_refdef.worldmodel->brush.BoxTouchingVisibleLeafs(r_refdef.worldmodel, r_viewcache.world_leafvisible, ent->mins, ent->maxs));
2212
2213                 }
2214                 if(r_cullentities_trace.integer)
2215                 {
2216                         for (i = 0;i < r_refdef.numentities;i++)
2217                         {
2218                                 ent = r_refdef.entities[i];
2219                                 if(r_viewcache.entityvisible[i] && !(ent->effects & EF_NODEPTHTEST) && !(ent->flags & RENDER_VIEWMODEL) && !(ent->model && (ent->model->name[0] == '*')))
2220                                 {
2221                                         if(Mod_CanSeeBox_Trace(r_cullentities_trace_samples.integer, r_cullentities_trace_enlarge.value, r_refdef.worldmodel, r_view.origin, ent->mins, ent->maxs))
2222                                                 ent->last_trace_visibility = realtime;
2223                                         if(ent->last_trace_visibility < realtime - r_cullentities_trace_delay.value)
2224                                                 r_viewcache.entityvisible[i] = 0;
2225                                 }
2226                         }
2227                 }
2228         }
2229         else
2230         {
2231                 // no worldmodel or it can't check visibility
2232                 for (i = 0;i < r_refdef.numentities;i++)
2233                 {
2234                         ent = r_refdef.entities[i];
2235                         r_viewcache.entityvisible[i] = !(ent->flags & renderimask) && ((ent->model && ent->model->type == mod_sprite && (ent->model->sprite.sprnum_type == SPR_LABEL || ent->model->sprite.sprnum_type == SPR_LABEL_SCALE)) || !R_CullBox(ent->mins, ent->maxs));
2236                 }
2237         }
2238 }
2239
2240 // only used if skyrendermasked, and normally returns false
2241 int R_DrawBrushModelsSky (void)
2242 {
2243         int i, sky;
2244         entity_render_t *ent;
2245
2246         if (!r_drawentities.integer)
2247                 return false;
2248
2249         sky = false;
2250         for (i = 0;i < r_refdef.numentities;i++)
2251         {
2252                 if (!r_viewcache.entityvisible[i])
2253                         continue;
2254                 ent = r_refdef.entities[i];
2255                 if (!ent->model || !ent->model->DrawSky)
2256                         continue;
2257                 ent->model->DrawSky(ent);
2258                 sky = true;
2259         }
2260         return sky;
2261 }
2262
2263 static void R_DrawNoModel(entity_render_t *ent);
2264 static void R_DrawModels(void)
2265 {
2266         int i;
2267         entity_render_t *ent;
2268
2269         if (!r_drawentities.integer)
2270                 return;
2271
2272         for (i = 0;i < r_refdef.numentities;i++)
2273         {
2274                 if (!r_viewcache.entityvisible[i])
2275                         continue;
2276                 ent = r_refdef.entities[i];
2277                 r_refdef.stats.entities++;
2278                 if (ent->model && ent->model->Draw != NULL)
2279                         ent->model->Draw(ent);
2280                 else
2281                         R_DrawNoModel(ent);
2282         }
2283 }
2284
2285 static void R_DrawModelsDepth(void)
2286 {
2287         int i;
2288         entity_render_t *ent;
2289
2290         if (!r_drawentities.integer)
2291                 return;
2292
2293         for (i = 0;i < r_refdef.numentities;i++)
2294         {
2295                 if (!r_viewcache.entityvisible[i])
2296                         continue;
2297                 ent = r_refdef.entities[i];
2298                 if (ent->model && ent->model->DrawDepth != NULL)
2299                         ent->model->DrawDepth(ent);
2300         }
2301 }
2302
2303 static void R_DrawModelsDebug(void)
2304 {
2305         int i;
2306         entity_render_t *ent;
2307
2308         if (!r_drawentities.integer)
2309                 return;
2310
2311         for (i = 0;i < r_refdef.numentities;i++)
2312         {
2313                 if (!r_viewcache.entityvisible[i])
2314                         continue;
2315                 ent = r_refdef.entities[i];
2316                 if (ent->model && ent->model->DrawDebug != NULL)
2317                         ent->model->DrawDebug(ent);
2318         }
2319 }
2320
2321 static void R_DrawModelsAddWaterPlanes(void)
2322 {
2323         int i;
2324         entity_render_t *ent;
2325
2326         if (!r_drawentities.integer)
2327                 return;
2328
2329         for (i = 0;i < r_refdef.numentities;i++)
2330         {
2331                 if (!r_viewcache.entityvisible[i])
2332                         continue;
2333                 ent = r_refdef.entities[i];
2334                 if (ent->model && ent->model->DrawAddWaterPlanes != NULL)
2335                         ent->model->DrawAddWaterPlanes(ent);
2336         }
2337 }
2338
2339 static void R_View_SetFrustum(void)
2340 {
2341         int i;
2342         double slopex, slopey;
2343
2344         // break apart the view matrix into vectors for various purposes
2345         Matrix4x4_ToVectors(&r_view.matrix, r_view.forward, r_view.left, r_view.up, r_view.origin);
2346         VectorNegate(r_view.left, r_view.right);
2347
2348 #if 0
2349         r_view.frustum[0].normal[0] = 0 - 1.0 / r_view.frustum_x;
2350         r_view.frustum[0].normal[1] = 0 - 0;
2351         r_view.frustum[0].normal[2] = -1 - 0;
2352         r_view.frustum[1].normal[0] = 0 + 1.0 / r_view.frustum_x;
2353         r_view.frustum[1].normal[1] = 0 + 0;
2354         r_view.frustum[1].normal[2] = -1 + 0;
2355         r_view.frustum[2].normal[0] = 0 - 0;
2356         r_view.frustum[2].normal[1] = 0 - 1.0 / r_view.frustum_y;
2357         r_view.frustum[2].normal[2] = -1 - 0;
2358         r_view.frustum[3].normal[0] = 0 + 0;
2359         r_view.frustum[3].normal[1] = 0 + 1.0 / r_view.frustum_y;
2360         r_view.frustum[3].normal[2] = -1 + 0;
2361 #endif
2362
2363 #if 0
2364         zNear = r_refdef.nearclip;
2365         nudge = 1.0 - 1.0 / (1<<23);
2366         r_view.frustum[4].normal[0] = 0 - 0;
2367         r_view.frustum[4].normal[1] = 0 - 0;
2368         r_view.frustum[4].normal[2] = -1 - -nudge;
2369         r_view.frustum[4].dist = 0 - -2 * zNear * nudge;
2370         r_view.frustum[5].normal[0] = 0 + 0;
2371         r_view.frustum[5].normal[1] = 0 + 0;
2372         r_view.frustum[5].normal[2] = -1 + -nudge;
2373         r_view.frustum[5].dist = 0 + -2 * zNear * nudge;
2374 #endif
2375
2376
2377
2378 #if 0
2379         r_view.frustum[0].normal[0] = m[3] - m[0];
2380         r_view.frustum[0].normal[1] = m[7] - m[4];
2381         r_view.frustum[0].normal[2] = m[11] - m[8];
2382         r_view.frustum[0].dist = m[15] - m[12];
2383
2384         r_view.frustum[1].normal[0] = m[3] + m[0];
2385         r_view.frustum[1].normal[1] = m[7] + m[4];
2386         r_view.frustum[1].normal[2] = m[11] + m[8];
2387         r_view.frustum[1].dist = m[15] + m[12];
2388
2389         r_view.frustum[2].normal[0] = m[3] - m[1];
2390         r_view.frustum[2].normal[1] = m[7] - m[5];
2391         r_view.frustum[2].normal[2] = m[11] - m[9];
2392         r_view.frustum[2].dist = m[15] - m[13];
2393
2394         r_view.frustum[3].normal[0] = m[3] + m[1];
2395         r_view.frustum[3].normal[1] = m[7] + m[5];
2396         r_view.frustum[3].normal[2] = m[11] + m[9];
2397         r_view.frustum[3].dist = m[15] + m[13];
2398
2399         r_view.frustum[4].normal[0] = m[3] - m[2];
2400         r_view.frustum[4].normal[1] = m[7] - m[6];
2401         r_view.frustum[4].normal[2] = m[11] - m[10];
2402         r_view.frustum[4].dist = m[15] - m[14];
2403
2404         r_view.frustum[5].normal[0] = m[3] + m[2];
2405         r_view.frustum[5].normal[1] = m[7] + m[6];
2406         r_view.frustum[5].normal[2] = m[11] + m[10];
2407         r_view.frustum[5].dist = m[15] + m[14];
2408 #endif
2409
2410         if (r_view.useperspective)
2411         {
2412                 slopex = 1.0 / r_view.frustum_x;
2413                 slopey = 1.0 / r_view.frustum_y;
2414                 VectorMA(r_view.forward, -slopex, r_view.left, r_view.frustum[0].normal);
2415                 VectorMA(r_view.forward,  slopex, r_view.left, r_view.frustum[1].normal);
2416                 VectorMA(r_view.forward, -slopey, r_view.up  , r_view.frustum[2].normal);
2417                 VectorMA(r_view.forward,  slopey, r_view.up  , r_view.frustum[3].normal);
2418                 VectorCopy(r_view.forward, r_view.frustum[4].normal);
2419
2420                 // Leaving those out was a mistake, those were in the old code, and they
2421                 // fix a reproducable bug in this one: frustum culling got fucked up when viewmatrix was an identity matrix
2422                 // I couldn't reproduce it after adding those normalizations. --blub
2423                 VectorNormalize(r_view.frustum[0].normal);
2424                 VectorNormalize(r_view.frustum[1].normal);
2425                 VectorNormalize(r_view.frustum[2].normal);
2426                 VectorNormalize(r_view.frustum[3].normal);
2427
2428                 // calculate frustum corners, which are used to calculate deformed frustum planes for shadow caster culling
2429                 VectorMAMAMAM(1, r_view.origin, 1024, r_view.forward, -1024 * slopex, r_view.left, -1024 * slopey, r_view.up, r_view.frustumcorner[0]);
2430                 VectorMAMAMAM(1, r_view.origin, 1024, r_view.forward,  1024 * slopex, r_view.left, -1024 * slopey, r_view.up, r_view.frustumcorner[1]);
2431                 VectorMAMAMAM(1, r_view.origin, 1024, r_view.forward, -1024 * slopex, r_view.left,  1024 * slopey, r_view.up, r_view.frustumcorner[2]);
2432                 VectorMAMAMAM(1, r_view.origin, 1024, r_view.forward,  1024 * slopex, r_view.left,  1024 * slopey, r_view.up, r_view.frustumcorner[3]);
2433
2434                 r_view.frustum[0].dist = DotProduct (r_view.origin, r_view.frustum[0].normal);
2435                 r_view.frustum[1].dist = DotProduct (r_view.origin, r_view.frustum[1].normal);
2436                 r_view.frustum[2].dist = DotProduct (r_view.origin, r_view.frustum[2].normal);
2437                 r_view.frustum[3].dist = DotProduct (r_view.origin, r_view.frustum[3].normal);
2438                 r_view.frustum[4].dist = DotProduct (r_view.origin, r_view.frustum[4].normal) + r_refdef.nearclip;
2439         }
2440         else
2441         {
2442                 VectorScale(r_view.left, -r_view.ortho_x, r_view.frustum[0].normal);
2443                 VectorScale(r_view.left,  r_view.ortho_x, r_view.frustum[1].normal);
2444                 VectorScale(r_view.up, -r_view.ortho_y, r_view.frustum[2].normal);
2445                 VectorScale(r_view.up,  r_view.ortho_y, r_view.frustum[3].normal);
2446                 VectorCopy(r_view.forward, r_view.frustum[4].normal);
2447                 r_view.frustum[0].dist = DotProduct (r_view.origin, r_view.frustum[0].normal) + r_view.ortho_x;
2448                 r_view.frustum[1].dist = DotProduct (r_view.origin, r_view.frustum[1].normal) + r_view.ortho_x;
2449                 r_view.frustum[2].dist = DotProduct (r_view.origin, r_view.frustum[2].normal) + r_view.ortho_y;
2450                 r_view.frustum[3].dist = DotProduct (r_view.origin, r_view.frustum[3].normal) + r_view.ortho_y;
2451                 r_view.frustum[4].dist = DotProduct (r_view.origin, r_view.frustum[4].normal) + r_refdef.nearclip;
2452         }
2453         r_view.numfrustumplanes = 5;
2454
2455         if (r_view.useclipplane)
2456         {
2457                 r_view.numfrustumplanes = 6;
2458                 r_view.frustum[5] = r_view.clipplane;
2459         }
2460
2461         for (i = 0;i < r_view.numfrustumplanes;i++)
2462                 PlaneClassify(r_view.frustum + i);
2463
2464         // LordHavoc: note to all quake engine coders, Quake had a special case
2465         // for 90 degrees which assumed a square view (wrong), so I removed it,
2466         // Quake2 has it disabled as well.
2467
2468         // rotate R_VIEWFORWARD right by FOV_X/2 degrees
2469         //RotatePointAroundVector( r_view.frustum[0].normal, r_view.up, r_view.forward, -(90 - r_refdef.fov_x / 2));
2470         //r_view.frustum[0].dist = DotProduct (r_view.origin, frustum[0].normal);
2471         //PlaneClassify(&frustum[0]);
2472
2473         // rotate R_VIEWFORWARD left by FOV_X/2 degrees
2474         //RotatePointAroundVector( r_view.frustum[1].normal, r_view.up, r_view.forward, (90 - r_refdef.fov_x / 2));
2475         //r_view.frustum[1].dist = DotProduct (r_view.origin, frustum[1].normal);
2476         //PlaneClassify(&frustum[1]);
2477
2478         // rotate R_VIEWFORWARD up by FOV_X/2 degrees
2479         //RotatePointAroundVector( r_view.frustum[2].normal, r_view.left, r_view.forward, -(90 - r_refdef.fov_y / 2));
2480         //r_view.frustum[2].dist = DotProduct (r_view.origin, frustum[2].normal);
2481         //PlaneClassify(&frustum[2]);
2482
2483         // rotate R_VIEWFORWARD down by FOV_X/2 degrees
2484         //RotatePointAroundVector( r_view.frustum[3].normal, r_view.left, r_view.forward, (90 - r_refdef.fov_y / 2));
2485         //r_view.frustum[3].dist = DotProduct (r_view.origin, frustum[3].normal);
2486         //PlaneClassify(&frustum[3]);
2487
2488         // nearclip plane
2489         //VectorCopy(r_view.forward, r_view.frustum[4].normal);
2490         //r_view.frustum[4].dist = DotProduct (r_view.origin, frustum[4].normal) + r_nearclip.value;
2491         //PlaneClassify(&frustum[4]);
2492 }
2493
2494 void R_View_Update(void)
2495 {
2496         R_View_SetFrustum();
2497         R_View_WorldVisibility(r_view.useclipplane);
2498         R_View_UpdateEntityVisible();
2499 }
2500
2501 void R_SetupView(void)
2502 {
2503         if (!r_view.useperspective)
2504                 GL_SetupView_Mode_Ortho(-r_view.ortho_x, -r_view.ortho_y, r_view.ortho_x, r_view.ortho_y, -r_refdef.farclip, r_refdef.farclip);
2505         else if (r_refdef.rtworldshadows || r_refdef.rtdlightshadows)
2506                 GL_SetupView_Mode_PerspectiveInfiniteFarClip(r_view.frustum_x, r_view.frustum_y, r_refdef.nearclip);
2507         else
2508                 GL_SetupView_Mode_Perspective(r_view.frustum_x, r_view.frustum_y, r_refdef.nearclip, r_refdef.farclip);
2509
2510         GL_SetupView_Orientation_FromEntity(&r_view.matrix);
2511
2512         if (r_view.useclipplane)
2513         {
2514                 // LordHavoc: couldn't figure out how to make this approach the
2515                 vec_t dist = r_view.clipplane.dist - r_water_clippingplanebias.value;
2516                 vec_t viewdist = DotProduct(r_view.origin, r_view.clipplane.normal);
2517                 if (viewdist < r_view.clipplane.dist + r_water_clippingplanebias.value)
2518                         dist = r_view.clipplane.dist;
2519                 GL_SetupView_ApplyCustomNearClipPlane(r_view.clipplane.normal[0], r_view.clipplane.normal[1], r_view.clipplane.normal[2], dist);
2520         }
2521 }
2522
2523 void R_ResetViewRendering2D(void)
2524 {
2525         if (gl_support_fragment_shader)
2526         {
2527                 qglUseProgramObjectARB(0);CHECKGLERROR
2528         }
2529
2530         DrawQ_Finish();
2531
2532         // GL is weird because it's bottom to top, r_view.y is top to bottom
2533         qglViewport(r_view.x, vid.height - (r_view.y + r_view.height), r_view.width, r_view.height);CHECKGLERROR
2534         GL_SetupView_Mode_Ortho(0, 0, 1, 1, -10, 100);
2535         GL_Scissor(r_view.x, r_view.y, r_view.width, r_view.height);
2536         GL_Color(1, 1, 1, 1);
2537         GL_ColorMask(r_view.colormask[0], r_view.colormask[1], r_view.colormask[2], 1);
2538         GL_BlendFunc(GL_ONE, GL_ZERO);
2539         GL_AlphaTest(false);
2540         GL_ScissorTest(false);
2541         GL_DepthMask(false);
2542         GL_DepthRange(0, 1);
2543         GL_DepthTest(false);
2544         R_Mesh_Matrix(&identitymatrix);
2545         R_Mesh_ResetTextureState();
2546         GL_PolygonOffset(0, 0);
2547         qglEnable(GL_POLYGON_OFFSET_FILL);CHECKGLERROR
2548         qglDepthFunc(GL_LEQUAL);CHECKGLERROR
2549         qglDisable(GL_STENCIL_TEST);CHECKGLERROR
2550         qglStencilMask(~0);CHECKGLERROR
2551         qglStencilFunc(GL_ALWAYS, 128, ~0);CHECKGLERROR
2552         qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);CHECKGLERROR
2553         GL_CullFace(GL_FRONT); // quake is backwards, this culls back faces
2554 }
2555
2556 void R_ResetViewRendering3D(void)
2557 {
2558         if (gl_support_fragment_shader)
2559         {
2560                 qglUseProgramObjectARB(0);CHECKGLERROR
2561         }
2562
2563         DrawQ_Finish();
2564
2565         // GL is weird because it's bottom to top, r_view.y is top to bottom
2566         qglViewport(r_view.x, vid.height - (r_view.y + r_view.height), r_view.width, r_view.height);CHECKGLERROR
2567         R_SetupView();
2568         GL_Scissor(r_view.x, r_view.y, r_view.width, r_view.height);
2569         GL_Color(1, 1, 1, 1);
2570         GL_ColorMask(r_view.colormask[0], r_view.colormask[1], r_view.colormask[2], 1);
2571         GL_BlendFunc(GL_ONE, GL_ZERO);
2572         GL_AlphaTest(false);
2573         GL_ScissorTest(true);
2574         GL_DepthMask(true);
2575         GL_DepthRange(0, 1);
2576         GL_DepthTest(true);
2577         R_Mesh_Matrix(&identitymatrix);
2578         R_Mesh_ResetTextureState();
2579         GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
2580         qglEnable(GL_POLYGON_OFFSET_FILL);CHECKGLERROR
2581         qglDepthFunc(GL_LEQUAL);CHECKGLERROR
2582         qglDisable(GL_STENCIL_TEST);CHECKGLERROR
2583         qglStencilMask(~0);CHECKGLERROR
2584         qglStencilFunc(GL_ALWAYS, 128, ~0);CHECKGLERROR
2585         qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);CHECKGLERROR
2586         GL_CullFace(r_view.cullface_back);
2587 }
2588
2589 /*
2590         R_Bloom_SetupShader(
2591 "// bloom shader\n"
2592 "// written by Forest 'LordHavoc' Hale\n"
2593 "\n"
2594 "// common definitions between vertex shader and fragment shader:\n"
2595 "\n"
2596 "#ifdef __GLSL_CG_DATA_TYPES\n"
2597 "#define myhalf half\n"
2598 "#define myhvec2 hvec2\n"
2599 "#define myhvec3 hvec3\n"
2600 "#define myhvec4 hvec4\n"
2601 "#else\n"
2602 "#define myhalf float\n"
2603 "#define myhvec2 vec2\n"
2604 "#define myhvec3 vec3\n"
2605 "#define myhvec4 vec4\n"
2606 "#endif\n"
2607 "\n"
2608 "varying vec2 ScreenTexCoord;\n"
2609 "varying vec2 BloomTexCoord;\n"
2610 "\n"
2611 "\n"
2612 "\n"
2613 "\n"
2614 "// vertex shader specific:\n"
2615 "#ifdef VERTEX_SHADER\n"
2616 "\n"
2617 "void main(void)\n"
2618 "{\n"
2619 "       ScreenTexCoord = vec2(gl_MultiTexCoord0);\n"
2620 "       BloomTexCoord = vec2(gl_MultiTexCoord1);\n"
2621 "       // transform vertex to camera space, using ftransform to match non-VS\n"
2622 "       // rendering\n"
2623 "       gl_Position = ftransform();\n"
2624 "}\n"
2625 "\n"
2626 "#endif // VERTEX_SHADER\n"
2627 "\n"
2628 "\n"
2629 "\n"
2630 "\n"
2631 "// fragment shader specific:\n"
2632 "#ifdef FRAGMENT_SHADER\n"
2633 "\n"
2634 "void main(void)\n"
2635 "{\n"
2636 "       int x, y;
2637 "       myhvec3 color = myhvec3(texture2D(Texture_Screen, ScreenTexCoord));\n"
2638 "       for (x = -BLUR_X;x <= BLUR_X;x++)
2639 "       color.rgb += myhvec3(texture2D(Texture_Bloom, BloomTexCoord));\n"
2640 "       color.rgb += myhvec3(texture2D(Texture_Bloom, BloomTexCoord));\n"
2641 "       color.rgb += myhvec3(texture2D(Texture_Bloom, BloomTexCoord));\n"
2642 "       color.rgb += myhvec3(texture2D(Texture_Bloom, BloomTexCoord));\n"
2643
2644 "       gl_FragColor = vec4(color);\n"
2645 "}\n"
2646 "\n"
2647 "#endif // FRAGMENT_SHADER\n"
2648 */
2649
2650 void R_RenderScene(qboolean addwaterplanes);
2651
2652 static void R_Water_StartFrame(void)
2653 {
2654         int i;
2655         int waterwidth, waterheight, texturewidth, textureheight;
2656         r_waterstate_waterplane_t *p;
2657
2658         // set waterwidth and waterheight to the water resolution that will be
2659         // used (often less than the screen resolution for faster rendering)
2660         waterwidth = (int)bound(1, r_view.width * r_water_resolutionmultiplier.value, r_view.width);
2661         waterheight = (int)bound(1, r_view.height * r_water_resolutionmultiplier.value, r_view.height);
2662
2663         // calculate desired texture sizes
2664         // can't use water if the card does not support the texture size
2665         if (!r_water.integer || !r_glsl.integer || !gl_support_fragment_shader || waterwidth > gl_max_texture_size || waterheight > gl_max_texture_size)
2666                 texturewidth = textureheight = waterwidth = waterheight = 0;
2667         else if (gl_support_arb_texture_non_power_of_two)
2668         {
2669                 texturewidth = waterwidth;
2670                 textureheight = waterheight;
2671         }
2672         else
2673         {
2674                 for (texturewidth   = 1;texturewidth   < waterwidth ;texturewidth   *= 2);
2675                 for (textureheight  = 1;textureheight  < waterheight;textureheight  *= 2);
2676         }
2677
2678         // allocate textures as needed
2679         if (r_waterstate.waterwidth != waterwidth || r_waterstate.waterheight != waterheight || r_waterstate.texturewidth != texturewidth || r_waterstate.textureheight != textureheight)
2680         {
2681                 r_waterstate.maxwaterplanes = MAX_WATERPLANES;
2682                 for (i = 0, p = r_waterstate.waterplanes;i < r_waterstate.maxwaterplanes;i++, p++)
2683                 {
2684                         if (p->texture_refraction)
2685                                 R_FreeTexture(p->texture_refraction);
2686                         p->texture_refraction = NULL;
2687                         if (p->texture_reflection)
2688                                 R_FreeTexture(p->texture_reflection);
2689                         p->texture_reflection = NULL;
2690                 }
2691                 memset(&r_waterstate, 0, sizeof(r_waterstate));
2692                 r_waterstate.waterwidth = waterwidth;
2693                 r_waterstate.waterheight = waterheight;
2694                 r_waterstate.texturewidth = texturewidth;
2695                 r_waterstate.textureheight = textureheight;
2696         }
2697
2698         if (r_waterstate.waterwidth)
2699         {
2700                 r_waterstate.enabled = true;
2701
2702                 // set up variables that will be used in shader setup
2703                 r_waterstate.screenscale[0] = 0.5f * (float)waterwidth / (float)texturewidth;
2704                 r_waterstate.screenscale[1] = 0.5f * (float)waterheight / (float)textureheight;
2705                 r_waterstate.screencenter[0] = 0.5f * (float)waterwidth / (float)texturewidth;
2706                 r_waterstate.screencenter[1] = 0.5f * (float)waterheight / (float)textureheight;
2707         }
2708
2709         r_waterstate.maxwaterplanes = MAX_WATERPLANES;
2710         r_waterstate.numwaterplanes = 0;
2711 }
2712
2713 static void R_Water_AddWaterPlane(msurface_t *surface)
2714 {
2715         int triangleindex, planeindex;
2716         const int *e;
2717         vec3_t vert[3];
2718         vec3_t normal;
2719         vec3_t center;
2720         r_waterstate_waterplane_t *p;
2721         // just use the first triangle with a valid normal for any decisions
2722         VectorClear(normal);
2723         for (triangleindex = 0, e = rsurface.modelelement3i + surface->num_firsttriangle * 3;triangleindex < surface->num_triangles;triangleindex++, e += 3)
2724         {
2725                 Matrix4x4_Transform(&rsurface.matrix, rsurface.modelvertex3f + e[0]*3, vert[0]);
2726                 Matrix4x4_Transform(&rsurface.matrix, rsurface.modelvertex3f + e[1]*3, vert[1]);
2727                 Matrix4x4_Transform(&rsurface.matrix, rsurface.modelvertex3f + e[2]*3, vert[2]);
2728                 TriangleNormal(vert[0], vert[1], vert[2], normal);
2729                 if (VectorLength2(normal) >= 0.001)
2730                         break;
2731         }
2732
2733         // find a matching plane if there is one
2734         for (planeindex = 0, p = r_waterstate.waterplanes;planeindex < r_waterstate.numwaterplanes;planeindex++, p++)
2735                 if (fabs(PlaneDiff(vert[0], &p->plane)) < 1 && fabs(PlaneDiff(vert[1], &p->plane)) < 1 && fabs(PlaneDiff(vert[2], &p->plane)) < 1)
2736                         break;
2737         if (planeindex >= r_waterstate.maxwaterplanes)
2738                 return; // nothing we can do, out of planes
2739
2740         // if this triangle does not fit any known plane rendered this frame, add one
2741         if (planeindex >= r_waterstate.numwaterplanes)
2742         {
2743                 // store the new plane
2744                 r_waterstate.numwaterplanes++;
2745                 VectorCopy(normal, p->plane.normal);
2746                 VectorNormalize(p->plane.normal);
2747                 p->plane.dist = DotProduct(vert[0], p->plane.normal);
2748                 PlaneClassify(&p->plane);
2749                 // flip the plane if it does not face the viewer
2750                 if (PlaneDiff(r_view.origin, &p->plane) < 0)
2751                 {
2752                         VectorNegate(p->plane.normal, p->plane.normal);
2753                         p->plane.dist *= -1;
2754                         PlaneClassify(&p->plane);
2755                 }
2756                 // clear materialflags and pvs
2757                 p->materialflags = 0;
2758                 p->pvsvalid = false;
2759         }
2760         // merge this surface's materialflags into the waterplane
2761         p->materialflags |= surface->texture->currentframe->currentmaterialflags;
2762         // merge this surface's PVS into the waterplane
2763         VectorMAM(0.5f, surface->mins, 0.5f, surface->maxs, center);
2764         if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION) && r_refdef.worldmodel && r_refdef.worldmodel->brush.FatPVS
2765          && r_refdef.worldmodel->brush.PointInLeaf && r_refdef.worldmodel->brush.PointInLeaf(r_refdef.worldmodel, center)->clusterindex >= 0)
2766         {
2767                 r_refdef.worldmodel->brush.FatPVS(r_refdef.worldmodel, center, 2, p->pvsbits, sizeof(p->pvsbits), p->pvsvalid);
2768                 p->pvsvalid = true;
2769         }
2770 }
2771
2772 static void R_Water_ProcessPlanes(void)
2773 {
2774         r_view_t originalview;
2775         int planeindex;
2776         r_waterstate_waterplane_t *p;
2777
2778         originalview = r_view;
2779
2780         // make sure enough textures are allocated
2781         for (planeindex = 0, p = r_waterstate.waterplanes;planeindex < r_waterstate.numwaterplanes;planeindex++, p++)
2782         {
2783                 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION))
2784                 {
2785                         if (!p->texture_refraction)
2786                                 p->texture_refraction = R_LoadTexture2D(r_main_texturepool, va("waterplane%i_refraction", planeindex), r_waterstate.texturewidth, r_waterstate.textureheight, NULL, TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_CLAMP | TEXF_ALWAYSPRECACHE, NULL);
2787                         if (!p->texture_refraction)
2788                                 goto error;
2789                 }
2790
2791                 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFLECTION))
2792                 {
2793                         if (!p->texture_reflection)
2794                                 p->texture_reflection = R_LoadTexture2D(r_main_texturepool, va("waterplane%i_reflection", planeindex), r_waterstate.texturewidth, r_waterstate.textureheight, NULL, TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_CLAMP | TEXF_ALWAYSPRECACHE, NULL);
2795                         if (!p->texture_reflection)
2796                                 goto error;
2797                 }
2798         }
2799
2800         // render views
2801         for (planeindex = 0, p = r_waterstate.waterplanes;planeindex < r_waterstate.numwaterplanes;planeindex++, p++)
2802         {
2803                 r_view.showdebug = false;
2804                 r_view.width = r_waterstate.waterwidth;
2805                 r_view.height = r_waterstate.waterheight;
2806                 r_view.useclipplane = true;
2807                 r_waterstate.renderingscene = true;
2808
2809                 // render the normal view scene and copy into texture
2810                 // (except that a clipping plane should be used to hide everything on one side of the water, and the viewer's weapon model should be omitted)
2811                 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION))
2812                 {
2813                         r_view.clipplane = p->plane;
2814                         VectorNegate(r_view.clipplane.normal, r_view.clipplane.normal);
2815                         r_view.clipplane.dist = -r_view.clipplane.dist;
2816                         PlaneClassify(&r_view.clipplane);
2817
2818                         R_RenderScene(false);
2819
2820                         // copy view into the screen texture
2821                         R_Mesh_TexBind(0, R_GetTexture(p->texture_refraction));
2822                         GL_ActiveTexture(0);
2823                         CHECKGLERROR
2824                         qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_view.x, vid.height - (r_view.y + r_view.height), r_view.width, r_view.height);CHECKGLERROR
2825                 }
2826
2827                 if (p->materialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFLECTION))
2828                 {
2829                         // render reflected scene and copy into texture
2830                         Matrix4x4_Reflect(&r_view.matrix, p->plane.normal[0], p->plane.normal[1], p->plane.normal[2], p->plane.dist, -2);
2831                         r_view.clipplane = p->plane;
2832                         // reverse the cullface settings for this render
2833                         r_view.cullface_front = GL_FRONT;
2834                         r_view.cullface_back = GL_BACK;
2835                         if (r_refdef.worldmodel && r_refdef.worldmodel->brush.num_pvsclusterbytes)
2836                         {
2837                                 r_view.usecustompvs = true;
2838                                 if (p->pvsvalid)
2839                                         memcpy(r_viewcache.world_pvsbits, p->pvsbits, r_refdef.worldmodel->brush.num_pvsclusterbytes);
2840                                 else
2841                                         memset(r_viewcache.world_pvsbits, 0xFF, r_refdef.worldmodel->brush.num_pvsclusterbytes);
2842                         }
2843
2844                         R_ResetViewRendering3D();
2845                         R_ClearScreen(r_refdef.fogenabled);
2846                         if (r_timereport_active)
2847                                 R_TimeReport("viewclear");
2848
2849                         R_RenderScene(false);
2850
2851                         R_Mesh_TexBind(0, R_GetTexture(p->texture_reflection));
2852                         GL_ActiveTexture(0);
2853                         CHECKGLERROR
2854                         qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_view.x, vid.height - (r_view.y + r_view.height), r_view.width, r_view.height);CHECKGLERROR
2855
2856                         R_ResetViewRendering3D();
2857                         R_ClearScreen(r_refdef.fogenabled);
2858                         if (r_timereport_active)
2859                                 R_TimeReport("viewclear");
2860                 }
2861
2862                 r_view = originalview;
2863                 r_view.clear = true;
2864                 r_waterstate.renderingscene = false;
2865         }
2866         return;
2867 error:
2868         r_view = originalview;
2869         r_waterstate.renderingscene = false;
2870         Cvar_SetValueQuick(&r_water, 0);
2871         Con_Printf("R_Water_ProcessPlanes: Error: texture creation failed!  Turned off r_water.\n");
2872         return;
2873 }
2874
2875 void R_Bloom_StartFrame(void)
2876 {
2877         int bloomtexturewidth, bloomtextureheight, screentexturewidth, screentextureheight;
2878
2879         // set bloomwidth and bloomheight to the bloom resolution that will be
2880         // used (often less than the screen resolution for faster rendering)
2881         r_bloomstate.bloomwidth = bound(1, r_bloom_resolution.integer, r_view.width);
2882         r_bloomstate.bloomheight = r_bloomstate.bloomwidth * r_view.height / r_view.width;
2883         r_bloomstate.bloomheight = bound(1, r_bloomstate.bloomheight, r_view.height);
2884
2885         // calculate desired texture sizes
2886         if (gl_support_arb_texture_non_power_of_two)
2887         {
2888                 screentexturewidth = r_view.width;
2889                 screentextureheight = r_view.height;
2890                 bloomtexturewidth = r_bloomstate.bloomwidth;
2891                 bloomtextureheight = r_bloomstate.bloomheight;
2892         }
2893         else
2894         {
2895                 for (screentexturewidth  = 1;screentexturewidth  < vid.width               ;screentexturewidth  *= 2);
2896                 for (screentextureheight = 1;screentextureheight < vid.height              ;screentextureheight *= 2);
2897                 for (bloomtexturewidth   = 1;bloomtexturewidth   < r_bloomstate.bloomwidth ;bloomtexturewidth   *= 2);
2898                 for (bloomtextureheight  = 1;bloomtextureheight  < r_bloomstate.bloomheight;bloomtextureheight  *= 2);
2899         }
2900
2901         if (r_hdr.integer)
2902         {
2903                 screentexturewidth = screentextureheight = 0;
2904         }
2905         else if (r_bloom.integer)
2906         {
2907         }
2908         else
2909         {
2910                 screentexturewidth = screentextureheight = 0;
2911                 bloomtexturewidth = bloomtextureheight = 0;
2912         }
2913
2914         if ((!bloomtexturewidth && !bloomtextureheight) || r_bloom_resolution.integer < 4 || r_bloom_blur.value < 1 || r_bloom_blur.value >= 512 || screentexturewidth > gl_max_texture_size || screentextureheight > gl_max_texture_size || bloomtexturewidth > gl_max_texture_size || bloomtextureheight > gl_max_texture_size)
2915         {
2916                 // can't use bloom if the parameters are too weird
2917                 // can't use bloom if the card does not support the texture size
2918                 if (r_bloomstate.texture_screen)
2919                         R_FreeTexture(r_bloomstate.texture_screen);
2920                 if (r_bloomstate.texture_bloom)
2921                         R_FreeTexture(r_bloomstate.texture_bloom);
2922                 memset(&r_bloomstate, 0, sizeof(r_bloomstate));
2923                 return;
2924         }
2925
2926         r_bloomstate.enabled = true;
2927         r_bloomstate.hdr = r_hdr.integer != 0;
2928
2929         // allocate textures as needed
2930         if (r_bloomstate.screentexturewidth != screentexturewidth || r_bloomstate.screentextureheight != screentextureheight)
2931         {
2932                 if (r_bloomstate.texture_screen)
2933                         R_FreeTexture(r_bloomstate.texture_screen);
2934                 r_bloomstate.texture_screen = NULL;
2935                 r_bloomstate.screentexturewidth = screentexturewidth;
2936                 r_bloomstate.screentextureheight = screentextureheight;
2937                 if (r_bloomstate.screentexturewidth && r_bloomstate.screentextureheight)
2938                         r_bloomstate.texture_screen = R_LoadTexture2D(r_main_texturepool, "screen", r_bloomstate.screentexturewidth, r_bloomstate.screentextureheight, NULL, TEXTYPE_BGRA, TEXF_FORCENEAREST | TEXF_CLAMP | TEXF_ALWAYSPRECACHE, NULL);
2939         }
2940         if (r_bloomstate.bloomtexturewidth != bloomtexturewidth || r_bloomstate.bloomtextureheight != bloomtextureheight)
2941         {
2942                 if (r_bloomstate.texture_bloom)
2943                         R_FreeTexture(r_bloomstate.texture_bloom);
2944                 r_bloomstate.texture_bloom = NULL;
2945                 r_bloomstate.bloomtexturewidth = bloomtexturewidth;
2946                 r_bloomstate.bloomtextureheight = bloomtextureheight;
2947                 if (r_bloomstate.bloomtexturewidth && r_bloomstate.bloomtextureheight)
2948                         r_bloomstate.texture_bloom = R_LoadTexture2D(r_main_texturepool, "bloom", r_bloomstate.bloomtexturewidth, r_bloomstate.bloomtextureheight, NULL, TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_CLAMP | TEXF_ALWAYSPRECACHE, NULL);
2949         }
2950
2951         // set up a texcoord array for the full resolution screen image
2952         // (we have to keep this around to copy back during final render)
2953         r_bloomstate.screentexcoord2f[0] = 0;
2954         r_bloomstate.screentexcoord2f[1] = (float)r_view.height / (float)r_bloomstate.screentextureheight;
2955         r_bloomstate.screentexcoord2f[2] = (float)r_view.width / (float)r_bloomstate.screentexturewidth;
2956         r_bloomstate.screentexcoord2f[3] = (float)r_view.height / (float)r_bloomstate.screentextureheight;
2957         r_bloomstate.screentexcoord2f[4] = (float)r_view.width / (float)r_bloomstate.screentexturewidth;
2958         r_bloomstate.screentexcoord2f[5] = 0;
2959         r_bloomstate.screentexcoord2f[6] = 0;
2960         r_bloomstate.screentexcoord2f[7] = 0;
2961
2962         // set up a texcoord array for the reduced resolution bloom image
2963         // (which will be additive blended over the screen image)
2964         r_bloomstate.bloomtexcoord2f[0] = 0;
2965         r_bloomstate.bloomtexcoord2f[1] = (float)r_bloomstate.bloomheight / (float)r_bloomstate.bloomtextureheight;
2966         r_bloomstate.bloomtexcoord2f[2] = (float)r_bloomstate.bloomwidth / (float)r_bloomstate.bloomtexturewidth;
2967         r_bloomstate.bloomtexcoord2f[3] = (float)r_bloomstate.bloomheight / (float)r_bloomstate.bloomtextureheight;
2968         r_bloomstate.bloomtexcoord2f[4] = (float)r_bloomstate.bloomwidth / (float)r_bloomstate.bloomtexturewidth;
2969         r_bloomstate.bloomtexcoord2f[5] = 0;
2970         r_bloomstate.bloomtexcoord2f[6] = 0;
2971         r_bloomstate.bloomtexcoord2f[7] = 0;
2972 }
2973
2974 void R_Bloom_CopyScreenTexture(float colorscale)
2975 {
2976         r_refdef.stats.bloom++;
2977
2978         R_ResetViewRendering2D();
2979         R_Mesh_VertexPointer(r_screenvertex3f, 0, 0);
2980         R_Mesh_ColorPointer(NULL, 0, 0);
2981         R_Mesh_TexCoordPointer(0, 2, r_bloomstate.screentexcoord2f, 0, 0);
2982         R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_screen));
2983
2984         // copy view into the screen texture
2985         GL_ActiveTexture(0);
2986         CHECKGLERROR
2987         qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_view.x, vid.height - (r_view.y + r_view.height), r_view.width, r_view.height);CHECKGLERROR
2988         r_refdef.stats.bloom_copypixels += r_view.width * r_view.height;
2989
2990         // now scale it down to the bloom texture size
2991         CHECKGLERROR
2992         qglViewport(r_view.x, vid.height - (r_view.y + r_bloomstate.bloomheight), r_bloomstate.bloomwidth, r_bloomstate.bloomheight);CHECKGLERROR
2993         GL_BlendFunc(GL_ONE, GL_ZERO);
2994         GL_Color(colorscale, colorscale, colorscale, 1);
2995         // TODO: optimize with multitexture or GLSL
2996         R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
2997         r_refdef.stats.bloom_drawpixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
2998
2999         // we now have a bloom image in the framebuffer
3000         // copy it into the bloom image texture for later processing
3001         R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
3002         GL_ActiveTexture(0);
3003         CHECKGLERROR
3004         qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_view.x, vid.height - (r_view.y + r_bloomstate.bloomheight), r_bloomstate.bloomwidth, r_bloomstate.bloomheight);CHECKGLERROR
3005         r_refdef.stats.bloom_copypixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
3006 }
3007
3008 void R_Bloom_CopyHDRTexture(void)
3009 {
3010         R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
3011         GL_ActiveTexture(0);
3012         CHECKGLERROR
3013         qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_view.x, vid.height - (r_view.y + r_view.height), r_view.width, r_view.height);CHECKGLERROR
3014         r_refdef.stats.bloom_copypixels += r_view.width * r_view.height;
3015 }
3016
3017 void R_Bloom_MakeTexture(void)
3018 {
3019         int x, range, dir;
3020         float xoffset, yoffset, r, brighten;
3021
3022         r_refdef.stats.bloom++;
3023
3024         R_ResetViewRendering2D();
3025         R_Mesh_VertexPointer(r_screenvertex3f, 0, 0);
3026         R_Mesh_ColorPointer(NULL, 0, 0);
3027
3028         // we have a bloom image in the framebuffer
3029         CHECKGLERROR
3030         qglViewport(r_view.x, vid.height - (r_view.y + r_bloomstate.bloomheight), r_bloomstate.bloomwidth, r_bloomstate.bloomheight);CHECKGLERROR
3031
3032         for (x = 1;x < min(r_bloom_colorexponent.value, 32);)
3033         {
3034                 x *= 2;
3035                 r = bound(0, r_bloom_colorexponent.value / x, 1);
3036                 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
3037                 GL_Color(r, r, r, 1);
3038                 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
3039                 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.bloomtexcoord2f, 0, 0);
3040                 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3041                 r_refdef.stats.bloom_drawpixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
3042
3043                 // copy the vertically blurred bloom view to a texture
3044                 GL_ActiveTexture(0);
3045                 CHECKGLERROR
3046                 qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_view.x, vid.height - (r_view.y + r_bloomstate.bloomheight), r_bloomstate.bloomwidth, r_bloomstate.bloomheight);CHECKGLERROR
3047                 r_refdef.stats.bloom_copypixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
3048         }
3049
3050         range = r_bloom_blur.integer * r_bloomstate.bloomwidth / 320;
3051         brighten = r_bloom_brighten.value;
3052         if (r_hdr.integer)
3053                 brighten *= r_hdr_range.value;
3054         R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
3055         R_Mesh_TexCoordPointer(0, 2, r_bloomstate.offsettexcoord2f, 0, 0);
3056
3057         for (dir = 0;dir < 2;dir++)
3058         {
3059                 // blend on at multiple vertical offsets to achieve a vertical blur
3060                 // TODO: do offset blends using GLSL
3061                 GL_BlendFunc(GL_ONE, GL_ZERO);
3062                 for (x = -range;x <= range;x++)
3063                 {
3064                         if (!dir){xoffset = 0;yoffset = x;}
3065                         else {xoffset = x;yoffset = 0;}
3066                         xoffset /= (float)r_bloomstate.bloomtexturewidth;
3067                         yoffset /= (float)r_bloomstate.bloomtextureheight;
3068                         // compute a texcoord array with the specified x and y offset
3069                         r_bloomstate.offsettexcoord2f[0] = xoffset+0;
3070                         r_bloomstate.offsettexcoord2f[1] = yoffset+(float)r_bloomstate.bloomheight / (float)r_bloomstate.bloomtextureheight;
3071                         r_bloomstate.offsettexcoord2f[2] = xoffset+(float)r_bloomstate.bloomwidth / (float)r_bloomstate.bloomtexturewidth;
3072                         r_bloomstate.offsettexcoord2f[3] = yoffset+(float)r_bloomstate.bloomheight / (float)r_bloomstate.bloomtextureheight;
3073                         r_bloomstate.offsettexcoord2f[4] = xoffset+(float)r_bloomstate.bloomwidth / (float)r_bloomstate.bloomtexturewidth;
3074                         r_bloomstate.offsettexcoord2f[5] = yoffset+0;
3075                         r_bloomstate.offsettexcoord2f[6] = xoffset+0;
3076                         r_bloomstate.offsettexcoord2f[7] = yoffset+0;
3077                         // this r value looks like a 'dot' particle, fading sharply to
3078                         // black at the edges
3079                         // (probably not realistic but looks good enough)
3080                         //r = ((range*range+1)/((float)(x*x+1)))/(range*2+1);
3081                         //r = (dir ? 1.0f : brighten)/(range*2+1);
3082                         r = (dir ? 1.0f : brighten)/(range*2+1)*(1 - x*x/(float)(range*range));
3083                         GL_Color(r, r, r, 1);
3084                         R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3085                         r_refdef.stats.bloom_drawpixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
3086                         GL_BlendFunc(GL_ONE, GL_ONE);
3087                 }
3088
3089                 // copy the vertically blurred bloom view to a texture
3090                 GL_ActiveTexture(0);
3091                 CHECKGLERROR
3092                 qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_view.x, vid.height - (r_view.y + r_bloomstate.bloomheight), r_bloomstate.bloomwidth, r_bloomstate.bloomheight);CHECKGLERROR
3093                 r_refdef.stats.bloom_copypixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
3094         }
3095
3096         // apply subtract last
3097         // (just like it would be in a GLSL shader)
3098         if (r_bloom_colorsubtract.value > 0 && gl_support_ext_blend_subtract)
3099         {
3100                 GL_BlendFunc(GL_ONE, GL_ZERO);
3101                 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
3102                 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.bloomtexcoord2f, 0, 0);
3103                 GL_Color(1, 1, 1, 1);
3104                 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3105                 r_refdef.stats.bloom_drawpixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
3106
3107                 GL_BlendFunc(GL_ONE, GL_ONE);
3108                 qglBlendEquationEXT(GL_FUNC_REVERSE_SUBTRACT_EXT);
3109                 R_Mesh_TexBind(0, R_GetTexture(r_texture_white));
3110                 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.bloomtexcoord2f, 0, 0);
3111                 GL_Color(r_bloom_colorsubtract.value, r_bloom_colorsubtract.value, r_bloom_colorsubtract.value, 1);
3112                 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3113                 r_refdef.stats.bloom_drawpixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
3114                 qglBlendEquationEXT(GL_FUNC_ADD_EXT);
3115
3116                 // copy the darkened bloom view to a texture
3117                 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
3118                 GL_ActiveTexture(0);
3119                 CHECKGLERROR
3120                 qglCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, r_view.x, vid.height - (r_view.y + r_bloomstate.bloomheight), r_bloomstate.bloomwidth, r_bloomstate.bloomheight);CHECKGLERROR
3121                 r_refdef.stats.bloom_copypixels += r_bloomstate.bloomwidth * r_bloomstate.bloomheight;
3122         }
3123 }
3124
3125 void R_HDR_RenderBloomTexture(void)
3126 {
3127         int oldwidth, oldheight;
3128         float oldcolorscale;
3129
3130         oldcolorscale = r_view.colorscale;
3131         oldwidth = r_view.width;
3132         oldheight = r_view.height;
3133         r_view.width = r_bloomstate.bloomwidth;
3134         r_view.height = r_bloomstate.bloomheight;
3135
3136         // TODO: support GL_EXT_framebuffer_object rather than reusing the framebuffer?  it might improve SLI performance.
3137         // TODO: add exposure compensation features
3138         // TODO: add fp16 framebuffer support
3139
3140         r_view.showdebug = false;
3141         r_view.colorscale *= r_bloom_colorscale.value / bound(1, r_hdr_range.value, 16);
3142
3143         R_ClearScreen(r_refdef.fogenabled);
3144         if (r_timereport_active)
3145                 R_TimeReport("HDRclear");
3146
3147         r_waterstate.numwaterplanes = 0;
3148         R_RenderScene(r_waterstate.enabled);
3149         r_view.showdebug = true;
3150
3151         R_ResetViewRendering2D();
3152
3153         R_Bloom_CopyHDRTexture();
3154         R_Bloom_MakeTexture();
3155
3156         // restore the view settings
3157         r_view.width = oldwidth;
3158         r_view.height = oldheight;
3159         r_view.colorscale = oldcolorscale;
3160
3161         R_ResetViewRendering3D();
3162
3163         R_ClearScreen(r_refdef.fogenabled);
3164         if (r_timereport_active)
3165                 R_TimeReport("viewclear");
3166 }
3167
3168 static void R_BlendView(void)
3169 {
3170         if (r_bloomstate.enabled && r_bloomstate.hdr)
3171         {
3172                 // render high dynamic range bloom effect
3173                 // the bloom texture was made earlier this render, so we just need to
3174                 // blend it onto the screen...
3175                 R_ResetViewRendering2D();
3176                 R_Mesh_VertexPointer(r_screenvertex3f, 0, 0);
3177                 R_Mesh_ColorPointer(NULL, 0, 0);
3178                 GL_Color(1, 1, 1, 1);
3179                 GL_BlendFunc(GL_ONE, GL_ONE);
3180                 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
3181                 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.bloomtexcoord2f, 0, 0);
3182                 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3183                 r_refdef.stats.bloom_drawpixels += r_view.width * r_view.height;
3184         }
3185         else if (r_bloomstate.enabled)
3186         {
3187                 // render simple bloom effect
3188                 // copy the screen and shrink it and darken it for the bloom process
3189                 R_Bloom_CopyScreenTexture(r_bloom_colorscale.value);
3190                 // make the bloom texture
3191                 R_Bloom_MakeTexture();
3192                 // put the original screen image back in place and blend the bloom
3193                 // texture on it
3194                 R_ResetViewRendering2D();
3195                 R_Mesh_VertexPointer(r_screenvertex3f, 0, 0);
3196                 R_Mesh_ColorPointer(NULL, 0, 0);
3197                 GL_Color(1, 1, 1, 1);
3198                 GL_BlendFunc(GL_ONE, GL_ZERO);
3199                 // do both in one pass if possible
3200                 R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_bloom));
3201                 R_Mesh_TexCoordPointer(0, 2, r_bloomstate.bloomtexcoord2f, 0, 0);
3202                 if (r_textureunits.integer >= 2 && gl_combine.integer)
3203                 {
3204                         R_Mesh_TexCombine(1, GL_ADD, GL_ADD, 1, 1);
3205                         R_Mesh_TexBind(1, R_GetTexture(r_bloomstate.texture_screen));
3206                         R_Mesh_TexCoordPointer(1, 2, r_bloomstate.screentexcoord2f, 0, 0);
3207                 }
3208                 else
3209                 {
3210                         R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3211                         r_refdef.stats.bloom_drawpixels += r_view.width * r_view.height;
3212                         // now blend on the bloom texture
3213                         GL_BlendFunc(GL_ONE, GL_ONE);
3214                         R_Mesh_TexBind(0, R_GetTexture(r_bloomstate.texture_screen));
3215                         R_Mesh_TexCoordPointer(0, 2, r_bloomstate.screentexcoord2f, 0, 0);
3216                 }
3217                 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3218                 r_refdef.stats.bloom_drawpixels += r_view.width * r_view.height;
3219         }
3220         if (r_refdef.viewblend[3] >= (1.0f / 256.0f))
3221         {
3222                 // apply a color tint to the whole view
3223                 R_ResetViewRendering2D();
3224                 R_Mesh_VertexPointer(r_screenvertex3f, 0, 0);
3225                 R_Mesh_ColorPointer(NULL, 0, 0);
3226                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
3227                 GL_Color(r_refdef.viewblend[0], r_refdef.viewblend[1], r_refdef.viewblend[2], r_refdef.viewblend[3]);
3228                 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3229         }
3230 }
3231
3232 void R_RenderScene(qboolean addwaterplanes);
3233
3234 matrix4x4_t r_waterscrollmatrix;
3235
3236 void R_UpdateFogColor(void) // needs to be called before HDR subrender too, as that changes colorscale!
3237 {
3238         if (r_refdef.fog_density)
3239         {
3240                 r_refdef.fogcolor[0] = r_refdef.fog_red;
3241                 r_refdef.fogcolor[1] = r_refdef.fog_green;
3242                 r_refdef.fogcolor[2] = r_refdef.fog_blue;
3243
3244                 {
3245                         vec3_t fogvec;
3246                         VectorCopy(r_refdef.fogcolor, fogvec);
3247                         if(r_glsl.integer && (r_glsl_contrastboost.value > 1 || r_glsl_contrastboost.value < 0)) // need to support contrast boost
3248                         {
3249                                 //   color.rgb /= ((ContrastBoost - 1) * color.rgb + 1);
3250                                 fogvec[0] /= ((r_glsl_contrastboost.value - 1) * fogvec[0] + 1);
3251                                 fogvec[1] /= ((r_glsl_contrastboost.value - 1) * fogvec[1] + 1);
3252                                 fogvec[2] /= ((r_glsl_contrastboost.value - 1) * fogvec[2] + 1);
3253                         }
3254                         //   color.rgb *= ContrastBoost * SceneBrightness;
3255                         VectorScale(fogvec, r_glsl_contrastboost.value * r_view.colorscale, fogvec);
3256                         r_refdef.fogcolor[0] = bound(0.0f, fogvec[0], 1.0f);
3257                         r_refdef.fogcolor[1] = bound(0.0f, fogvec[1], 1.0f);
3258                         r_refdef.fogcolor[2] = bound(0.0f, fogvec[2], 1.0f);
3259                 }
3260         }
3261 }
3262
3263 void R_UpdateVariables(void)
3264 {
3265         R_Textures_Frame();
3266
3267         r_refdef.farclip = 4096;
3268         if (r_refdef.worldmodel)
3269                 r_refdef.farclip += VectorDistance(r_refdef.worldmodel->normalmins, r_refdef.worldmodel->normalmaxs);
3270         r_refdef.nearclip = bound (0.001f, r_nearclip.value, r_refdef.farclip - 1.0f);
3271
3272         if (r_shadow_frontsidecasting.integer < 0 || r_shadow_frontsidecasting.integer > 1)
3273                 Cvar_SetValueQuick(&r_shadow_frontsidecasting, 1);
3274         r_refdef.polygonfactor = 0;
3275         r_refdef.polygonoffset = 0;
3276         r_refdef.shadowpolygonfactor = r_refdef.polygonfactor + r_shadow_polygonfactor.value * (r_shadow_frontsidecasting.integer ? 1 : -1);
3277         r_refdef.shadowpolygonoffset = r_refdef.polygonoffset + r_shadow_polygonoffset.value * (r_shadow_frontsidecasting.integer ? 1 : -1);
3278
3279         r_refdef.rtworld = r_shadow_realtime_world.integer;
3280         r_refdef.rtworldshadows = r_shadow_realtime_world_shadows.integer && gl_stencil;
3281         r_refdef.rtdlight = (r_shadow_realtime_world.integer || r_shadow_realtime_dlight.integer) && !gl_flashblend.integer && r_dynamic.integer;
3282         r_refdef.rtdlightshadows = r_refdef.rtdlight && r_shadow_realtime_dlight_shadows.integer && gl_stencil;
3283         r_refdef.lightmapintensity = r_refdef.rtworld ? r_shadow_realtime_world_lightmaps.value : 1;
3284         if (r_showsurfaces.integer)
3285         {
3286                 r_refdef.rtworld = false;
3287                 r_refdef.rtworldshadows = false;
3288                 r_refdef.rtdlight = false;
3289                 r_refdef.rtdlightshadows = false;
3290                 r_refdef.lightmapintensity = 0;
3291         }
3292
3293         if (gamemode == GAME_NEHAHRA)
3294         {
3295                 if (gl_fogenable.integer)
3296                 {
3297                         r_refdef.oldgl_fogenable = true;
3298                         r_refdef.fog_density = gl_fogdensity.value;
3299                         r_refdef.fog_red = gl_fogred.value;
3300                         r_refdef.fog_green = gl_foggreen.value;
3301                         r_refdef.fog_blue = gl_fogblue.value;
3302                         r_refdef.fog_alpha = 1;
3303                         r_refdef.fog_start = 0;
3304                         r_refdef.fog_end = gl_skyclip.value;
3305                 }
3306                 else if (r_refdef.oldgl_fogenable)
3307                 {
3308                         r_refdef.oldgl_fogenable = false;
3309                         r_refdef.fog_density = 0;
3310                         r_refdef.fog_red = 0;
3311                         r_refdef.fog_green = 0;
3312                         r_refdef.fog_blue = 0;
3313                         r_refdef.fog_alpha = 0;
3314                         r_refdef.fog_start = 0;
3315                         r_refdef.fog_end = 0;
3316                 }
3317         }
3318
3319         r_refdef.fog_alpha = bound(0, r_refdef.fog_alpha, 1);
3320         r_refdef.fog_start = max(0, r_refdef.fog_start);
3321         r_refdef.fog_end = max(r_refdef.fog_start + 0.01, r_refdef.fog_end);
3322
3323         // R_UpdateFogColor(); // why? R_RenderScene does it anyway
3324
3325         if (r_refdef.fog_density)
3326         {
3327                 r_refdef.fogenabled = true;
3328                 // this is the point where the fog reaches 0.9986 alpha, which we
3329                 // consider a good enough cutoff point for the texture
3330                 // (0.9986 * 256 == 255.6)
3331                 if (r_fog_exp2.integer)
3332                         r_refdef.fogrange = 32 / (r_refdef.fog_density * r_refdef.fog_density) + r_refdef.fog_start;
3333                 else
3334                         r_refdef.fogrange = 2048 / r_refdef.fog_density + r_refdef.fog_start;
3335                 r_refdef.fogrange = bound(r_refdef.fog_start, r_refdef.fogrange, r_refdef.fog_end);
3336                 r_refdef.fograngerecip = 1.0f / r_refdef.fogrange;
3337                 r_refdef.fogmasktabledistmultiplier = FOGMASKTABLEWIDTH * r_refdef.fograngerecip;
3338                 // fog color was already set
3339                 // update the fog texture
3340                 if (r_refdef.fogmasktable_start != r_refdef.fog_start || r_refdef.fogmasktable_alpha != r_refdef.fog_alpha || r_refdef.fogmasktable_density != r_refdef.fog_density || r_refdef.fogmasktable_range != r_refdef.fogrange)
3341                         R_BuildFogTexture();
3342         }
3343         else
3344                 r_refdef.fogenabled = false;
3345 }
3346
3347 /*
3348 ================
3349 R_RenderView
3350 ================
3351 */
3352 void R_RenderView(void)
3353 {
3354         if (!r_refdef.entities/* || !r_refdef.worldmodel*/)
3355                 return; //Host_Error ("R_RenderView: NULL worldmodel");
3356
3357         r_view.colorscale = r_hdr_scenebrightness.value;
3358
3359         R_Shadow_UpdateWorldLightSelection();
3360
3361         R_Bloom_StartFrame();
3362         R_Water_StartFrame();
3363
3364         CHECKGLERROR
3365         if (r_timereport_active)
3366                 R_TimeReport("viewsetup");
3367
3368         R_ResetViewRendering3D();
3369
3370         if (r_view.clear || r_refdef.fogenabled)
3371         {
3372                 R_ClearScreen(r_refdef.fogenabled);
3373                 if (r_timereport_active)
3374                         R_TimeReport("viewclear");
3375         }
3376         r_view.clear = true;
3377
3378         r_view.showdebug = true;
3379
3380         // this produces a bloom texture to be used in R_BlendView() later
3381         if (r_hdr.integer)
3382                 R_HDR_RenderBloomTexture();
3383
3384         r_waterstate.numwaterplanes = 0;
3385         R_RenderScene(r_waterstate.enabled);
3386
3387         R_BlendView();
3388         if (r_timereport_active)
3389                 R_TimeReport("blendview");
3390
3391         GL_Scissor(0, 0, vid.width, vid.height);
3392         GL_ScissorTest(false);
3393         CHECKGLERROR
3394 }
3395
3396 extern void R_DrawLightningBeams (void);
3397 extern void VM_CL_AddPolygonsToMeshQueue (void);
3398 extern void R_DrawPortals (void);
3399 extern cvar_t cl_locs_show;
3400 static void R_DrawLocs(void);
3401 static void R_DrawEntityBBoxes(void);
3402 void R_RenderScene(qboolean addwaterplanes)
3403 {
3404         R_UpdateFogColor();
3405
3406         if (addwaterplanes)
3407         {
3408                 R_ResetViewRendering3D();
3409
3410                 R_View_Update();
3411                 if (r_timereport_active)
3412                         R_TimeReport("watervis");
3413
3414                 if (cl.csqc_vidvars.drawworld && r_refdef.worldmodel && r_refdef.worldmodel->DrawAddWaterPlanes)
3415                 {
3416                         r_refdef.worldmodel->DrawAddWaterPlanes(r_refdef.worldentity);
3417                         if (r_timereport_active)
3418                                 R_TimeReport("waterworld");
3419                 }
3420
3421                 // don't let sound skip if going slow
3422                 if (r_refdef.extraupdate)
3423                         S_ExtraUpdate ();
3424
3425                 R_DrawModelsAddWaterPlanes();
3426                 if (r_timereport_active)
3427                         R_TimeReport("watermodels");
3428
3429                 R_Water_ProcessPlanes();
3430                 if (r_timereport_active)
3431                         R_TimeReport("waterscenes");
3432         }
3433
3434         R_ResetViewRendering3D();
3435
3436         // don't let sound skip if going slow
3437         if (r_refdef.extraupdate)
3438                 S_ExtraUpdate ();
3439
3440         R_MeshQueue_BeginScene();
3441
3442         R_SkyStartFrame();
3443
3444         R_View_Update();
3445         if (r_timereport_active)
3446                 R_TimeReport("visibility");
3447
3448         Matrix4x4_CreateTranslate(&r_waterscrollmatrix, sin(r_refdef.time) * 0.025 * r_waterscroll.value, sin(r_refdef.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
3449
3450         if (cl.csqc_vidvars.drawworld)
3451         {
3452                 // don't let sound skip if going slow
3453                 if (r_refdef.extraupdate)
3454                         S_ExtraUpdate ();
3455
3456                 if (r_refdef.worldmodel && r_refdef.worldmodel->DrawSky)
3457                 {
3458                         r_refdef.worldmodel->DrawSky(r_refdef.worldentity);
3459                         if (r_timereport_active)
3460                                 R_TimeReport("worldsky");
3461                 }
3462
3463                 if (R_DrawBrushModelsSky() && r_timereport_active)
3464                         R_TimeReport("bmodelsky");
3465         }
3466
3467         if (r_depthfirst.integer >= 1 && cl.csqc_vidvars.drawworld && r_refdef.worldmodel && r_refdef.worldmodel->DrawDepth)
3468         {
3469                 r_refdef.worldmodel->DrawDepth(r_refdef.worldentity);
3470                 if (r_timereport_active)
3471                         R_TimeReport("worlddepth");
3472         }
3473         if (r_depthfirst.integer >= 2)
3474         {
3475                 R_DrawModelsDepth();
3476                 if (r_timereport_active)
3477                         R_TimeReport("modeldepth");
3478         }
3479
3480         if (cl.csqc_vidvars.drawworld && r_refdef.worldmodel && r_refdef.worldmodel->Draw)
3481         {
3482                 r_refdef.worldmodel->Draw(r_refdef.worldentity);
3483                 if (r_timereport_active)
3484                         R_TimeReport("world");
3485         }
3486
3487         // don't let sound skip if going slow
3488         if (r_refdef.extraupdate)
3489                 S_ExtraUpdate ();
3490
3491         R_DrawModels();
3492         if (r_timereport_active)
3493                 R_TimeReport("models");
3494
3495         // don't let sound skip if going slow
3496         if (r_refdef.extraupdate)
3497                 S_ExtraUpdate ();
3498
3499         if (r_shadows.integer > 0 && r_refdef.lightmapintensity > 0)
3500         {
3501                 R_DrawModelShadows();
3502
3503                 R_ResetViewRendering3D();
3504
3505                 // don't let sound skip if going slow
3506                 if (r_refdef.extraupdate)
3507                         S_ExtraUpdate ();
3508         }
3509
3510         R_ShadowVolumeLighting(false);
3511         if (r_timereport_active)
3512                 R_TimeReport("rtlights");
3513
3514         // don't let sound skip if going slow
3515         if (r_refdef.extraupdate)
3516                 S_ExtraUpdate ();
3517
3518         if (cl.csqc_vidvars.drawworld)
3519         {
3520                 R_DrawLightningBeams();
3521                 if (r_timereport_active)
3522                         R_TimeReport("lightning");
3523
3524                 R_DrawDecals();
3525                 if (r_timereport_active)
3526                         R_TimeReport("decals");
3527
3528                 R_DrawParticles();
3529                 if (r_timereport_active)
3530                         R_TimeReport("particles");
3531
3532                 R_DrawExplosions();
3533                 if (r_timereport_active)
3534                         R_TimeReport("explosions");
3535         }
3536
3537         if (gl_support_fragment_shader)
3538         {
3539                 qglUseProgramObjectARB(0);CHECKGLERROR
3540         }
3541         VM_CL_AddPolygonsToMeshQueue();
3542
3543         if (r_view.showdebug)
3544         {
3545                 if (cl_locs_show.integer)
3546                 {
3547                         R_DrawLocs();
3548                         if (r_timereport_active)
3549                                 R_TimeReport("showlocs");
3550                 }
3551
3552                 if (r_drawportals.integer)
3553                 {
3554                         R_DrawPortals();
3555                         if (r_timereport_active)
3556                                 R_TimeReport("portals");
3557                 }
3558
3559                 if (r_showbboxes.value > 0)
3560                 {
3561                         R_DrawEntityBBoxes();
3562                         if (r_timereport_active)
3563                                 R_TimeReport("bboxes");
3564                 }
3565         }
3566
3567         if (gl_support_fragment_shader)
3568         {
3569                 qglUseProgramObjectARB(0);CHECKGLERROR
3570         }
3571         R_MeshQueue_RenderTransparent();
3572         if (r_timereport_active)
3573                 R_TimeReport("drawtrans");
3574
3575         if (gl_support_fragment_shader)
3576         {
3577                 qglUseProgramObjectARB(0);CHECKGLERROR
3578         }
3579
3580         if (r_view.showdebug && r_refdef.worldmodel && r_refdef.worldmodel->DrawDebug && (r_showtris.value > 0 || r_shownormals.value > 0 || r_showcollisionbrushes.value > 0))
3581         {
3582                 r_refdef.worldmodel->DrawDebug(r_refdef.worldentity);
3583                 if (r_timereport_active)
3584                         R_TimeReport("worlddebug");
3585                 R_DrawModelsDebug();
3586                 if (r_timereport_active)
3587                         R_TimeReport("modeldebug");
3588         }
3589
3590         if (gl_support_fragment_shader)
3591         {
3592                 qglUseProgramObjectARB(0);CHECKGLERROR
3593         }
3594
3595         if (cl.csqc_vidvars.drawworld)
3596         {
3597                 R_DrawCoronas();
3598                 if (r_timereport_active)
3599                         R_TimeReport("coronas");
3600         }
3601
3602         // don't let sound skip if going slow
3603         if (r_refdef.extraupdate)
3604                 S_ExtraUpdate ();
3605
3606         R_ResetViewRendering2D();
3607 }
3608
3609 static const int bboxelements[36] =
3610 {
3611         5, 1, 3, 5, 3, 7,
3612         6, 2, 0, 6, 0, 4,
3613         7, 3, 2, 7, 2, 6,
3614         4, 0, 1, 4, 1, 5,
3615         4, 5, 7, 4, 7, 6,
3616         1, 0, 2, 1, 2, 3,
3617 };
3618
3619 void R_DrawBBoxMesh(vec3_t mins, vec3_t maxs, float cr, float cg, float cb, float ca)
3620 {
3621         int i;
3622         float *v, *c, f1, f2, vertex3f[8*3], color4f[8*4];
3623         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
3624         GL_DepthMask(false);
3625         GL_DepthRange(0, 1);
3626         GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
3627         R_Mesh_Matrix(&identitymatrix);
3628         R_Mesh_ResetTextureState();
3629
3630         vertex3f[ 0] = mins[0];vertex3f[ 1] = mins[1];vertex3f[ 2] = mins[2]; //
3631         vertex3f[ 3] = maxs[0];vertex3f[ 4] = mins[1];vertex3f[ 5] = mins[2];
3632         vertex3f[ 6] = mins[0];vertex3f[ 7] = maxs[1];vertex3f[ 8] = mins[2];
3633         vertex3f[ 9] = maxs[0];vertex3f[10] = maxs[1];vertex3f[11] = mins[2];
3634         vertex3f[12] = mins[0];vertex3f[13] = mins[1];vertex3f[14] = maxs[2];
3635         vertex3f[15] = maxs[0];vertex3f[16] = mins[1];vertex3f[17] = maxs[2];
3636         vertex3f[18] = mins[0];vertex3f[19] = maxs[1];vertex3f[20] = maxs[2];
3637         vertex3f[21] = maxs[0];vertex3f[22] = maxs[1];vertex3f[23] = maxs[2];
3638         R_FillColors(color4f, 8, cr, cg, cb, ca);
3639         if (r_refdef.fogenabled)
3640         {
3641                 for (i = 0, v = vertex3f, c = color4f;i < 8;i++, v += 3, c += 4)
3642                 {
3643                         f1 = FogPoint_World(v);
3644                         f2 = 1 - f1;
3645                         c[0] = c[0] * f1 + r_refdef.fogcolor[0] * f2;
3646                         c[1] = c[1] * f1 + r_refdef.fogcolor[1] * f2;
3647                         c[2] = c[2] * f1 + r_refdef.fogcolor[2] * f2;
3648                 }
3649         }
3650         R_Mesh_VertexPointer(vertex3f, 0, 0);
3651         R_Mesh_ColorPointer(color4f, 0, 0);
3652         R_Mesh_ResetTextureState();
3653         R_Mesh_Draw(0, 8, 12, bboxelements, 0, 0);
3654 }
3655
3656 static void R_DrawEntityBBoxes_Callback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
3657 {
3658         int i;
3659         float color[4];
3660         prvm_edict_t *edict;
3661         // this function draws bounding boxes of server entities
3662         if (!sv.active)
3663                 return;
3664         SV_VM_Begin();
3665         for (i = 0;i < numsurfaces;i++)
3666         {
3667                 edict = PRVM_EDICT_NUM(surfacelist[i]);
3668                 switch ((int)edict->fields.server->solid)
3669                 {
3670                         case SOLID_NOT:      Vector4Set(color, 1, 1, 1, 0.05);break;
3671                         case SOLID_TRIGGER:  Vector4Set(color, 1, 0, 1, 0.10);break;
3672                         case SOLID_BBOX:     Vector4Set(color, 0, 1, 0, 0.10);break;
3673                         case SOLID_SLIDEBOX: Vector4Set(color, 1, 0, 0, 0.10);break;
3674                         case SOLID_BSP:      Vector4Set(color, 0, 0, 1, 0.05);break;
3675                         default:             Vector4Set(color, 0, 0, 0, 0.50);break;
3676                 }
3677                 color[3] *= r_showbboxes.value;
3678                 color[3] = bound(0, color[3], 1);
3679                 GL_DepthTest(!r_showdisabledepthtest.integer);
3680                 GL_CullFace(r_view.cullface_front);
3681                 R_DrawBBoxMesh(edict->priv.server->areamins, edict->priv.server->areamaxs, color[0], color[1], color[2], color[3]);
3682         }
3683         SV_VM_End();
3684 }
3685
3686 static void R_DrawEntityBBoxes(void)
3687 {
3688         int i;
3689         prvm_edict_t *edict;
3690         vec3_t center;
3691         // this function draws bounding boxes of server entities
3692         if (!sv.active)
3693                 return;
3694         SV_VM_Begin();
3695         for (i = 0;i < prog->num_edicts;i++)
3696         {
3697                 edict = PRVM_EDICT_NUM(i);
3698                 if (edict->priv.server->free)
3699                         continue;
3700                 VectorLerp(edict->priv.server->areamins, 0.5f, edict->priv.server->areamaxs, center);
3701                 R_MeshQueue_AddTransparent(center, R_DrawEntityBBoxes_Callback, (entity_render_t *)NULL, i, (rtlight_t *)NULL);
3702         }
3703         SV_VM_End();
3704 }
3705
3706 int nomodelelements[24] =
3707 {
3708         5, 2, 0,
3709         5, 1, 2,
3710         5, 0, 3,
3711         5, 3, 1,
3712         0, 2, 4,
3713         2, 1, 4,
3714         3, 0, 4,
3715         1, 3, 4
3716 };
3717
3718 float nomodelvertex3f[6*3] =
3719 {
3720         -16,   0,   0,
3721          16,   0,   0,
3722           0, -16,   0,
3723           0,  16,   0,
3724           0,   0, -16,
3725           0,   0,  16
3726 };
3727
3728 float nomodelcolor4f[6*4] =
3729 {
3730         0.0f, 0.0f, 0.5f, 1.0f,
3731         0.0f, 0.0f, 0.5f, 1.0f,
3732         0.0f, 0.5f, 0.0f, 1.0f,
3733         0.0f, 0.5f, 0.0f, 1.0f,
3734         0.5f, 0.0f, 0.0f, 1.0f,
3735         0.5f, 0.0f, 0.0f, 1.0f
3736 };
3737
3738 void R_DrawNoModel_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
3739 {
3740         int i;
3741         float f1, f2, *c;
3742         float color4f[6*4];
3743         // this is only called once per entity so numsurfaces is always 1, and
3744         // surfacelist is always {0}, so this code does not handle batches
3745         R_Mesh_Matrix(&ent->matrix);
3746
3747         if (ent->flags & EF_ADDITIVE)
3748         {
3749                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
3750                 GL_DepthMask(false);
3751         }
3752         else if (ent->alpha < 1)
3753         {
3754                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
3755                 GL_DepthMask(false);
3756         }
3757         else
3758         {
3759                 GL_BlendFunc(GL_ONE, GL_ZERO);
3760                 GL_DepthMask(true);
3761         }
3762         GL_DepthRange(0, (ent->flags & RENDER_VIEWMODEL) ? 0.0625 : 1);
3763         GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
3764         GL_DepthTest(!(ent->effects & EF_NODEPTHTEST));
3765         GL_CullFace((ent->effects & EF_DOUBLESIDED) ? GL_NONE : r_view.cullface_back);
3766         R_Mesh_VertexPointer(nomodelvertex3f, 0, 0);
3767         if (r_refdef.fogenabled)
3768         {
3769                 vec3_t org;
3770                 memcpy(color4f, nomodelcolor4f, sizeof(float[6*4]));
3771                 R_Mesh_ColorPointer(color4f, 0, 0);
3772                 Matrix4x4_OriginFromMatrix(&ent->matrix, org);
3773                 f1 = FogPoint_World(org);
3774                 f2 = 1 - f1;
3775                 for (i = 0, c = color4f;i < 6;i++, c += 4)
3776                 {
3777                         c[0] = (c[0] * f1 + r_refdef.fogcolor[0] * f2);
3778                         c[1] = (c[1] * f1 + r_refdef.fogcolor[1] * f2);
3779                         c[2] = (c[2] * f1 + r_refdef.fogcolor[2] * f2);
3780                         c[3] *= ent->alpha;
3781                 }
3782         }
3783         else if (ent->alpha != 1)
3784         {
3785                 memcpy(color4f, nomodelcolor4f, sizeof(float[6*4]));
3786                 R_Mesh_ColorPointer(color4f, 0, 0);
3787                 for (i = 0, c = color4f;i < 6;i++, c += 4)
3788                         c[3] *= ent->alpha;
3789         }
3790         else
3791                 R_Mesh_ColorPointer(nomodelcolor4f, 0, 0);
3792         R_Mesh_ResetTextureState();
3793         R_Mesh_Draw(0, 6, 8, nomodelelements, 0, 0);
3794 }
3795
3796 void R_DrawNoModel(entity_render_t *ent)
3797 {
3798         vec3_t org;
3799         Matrix4x4_OriginFromMatrix(&ent->matrix, org);
3800         //if ((ent->effects & EF_ADDITIVE) || (ent->alpha < 1))
3801                 R_MeshQueue_AddTransparent(ent->effects & EF_NODEPTHTEST ? r_view.origin : org, R_DrawNoModel_TransparentCallback, ent, 0, rsurface.rtlight);
3802         //else
3803         //      R_DrawNoModelCallback(ent, 0);
3804 }
3805
3806 void R_CalcBeam_Vertex3f (float *vert, const vec3_t org1, const vec3_t org2, float width)
3807 {
3808         vec3_t right1, right2, diff, normal;
3809
3810         VectorSubtract (org2, org1, normal);
3811
3812         // calculate 'right' vector for start
3813         VectorSubtract (r_view.origin, org1, diff);
3814         CrossProduct (normal, diff, right1);
3815         VectorNormalize (right1);
3816
3817         // calculate 'right' vector for end
3818         VectorSubtract (r_view.origin, org2, diff);
3819         CrossProduct (normal, diff, right2);
3820         VectorNormalize (right2);
3821
3822         vert[ 0] = org1[0] + width * right1[0];
3823         vert[ 1] = org1[1] + width * right1[1];
3824         vert[ 2] = org1[2] + width * right1[2];
3825         vert[ 3] = org1[0] - width * right1[0];
3826         vert[ 4] = org1[1] - width * right1[1];
3827         vert[ 5] = org1[2] - width * right1[2];
3828         vert[ 6] = org2[0] - width * right2[0];
3829         vert[ 7] = org2[1] - width * right2[1];
3830         vert[ 8] = org2[2] - width * right2[2];
3831         vert[ 9] = org2[0] + width * right2[0];
3832         vert[10] = org2[1] + width * right2[1];
3833         vert[11] = org2[2] + width * right2[2];
3834 }
3835
3836 float spritetexcoord2f[4*2] = {0, 1, 0, 0, 1, 0, 1, 1};
3837
3838 void R_DrawSprite(int blendfunc1, int blendfunc2, rtexture_t *texture, rtexture_t *fogtexture, qboolean depthdisable, qboolean depthshort, const vec3_t origin, const vec3_t left, const vec3_t up, float scalex1, float scalex2, float scaley1, float scaley2, float cr, float cg, float cb, float ca)
3839 {
3840         float fog = 1.0f;
3841         float vertex3f[12];
3842
3843         if (r_refdef.fogenabled)
3844                 fog = FogPoint_World(origin);
3845
3846         R_Mesh_Matrix(&identitymatrix);
3847         GL_BlendFunc(blendfunc1, blendfunc2);
3848
3849         if(v_flipped_state)
3850         {
3851                 scalex1 = -scalex1;
3852                 scalex2 = -scalex2;
3853                 GL_CullFace(r_view.cullface_front);
3854         }
3855         else
3856                 GL_CullFace(r_view.cullface_back);
3857
3858         GL_DepthMask(false);
3859         GL_DepthRange(0, depthshort ? 0.0625 : 1);
3860         GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
3861         GL_DepthTest(!depthdisable);
3862
3863         vertex3f[ 0] = origin[0] + left[0] * scalex2 + up[0] * scaley1;
3864         vertex3f[ 1] = origin[1] + left[1] * scalex2 + up[1] * scaley1;
3865         vertex3f[ 2] = origin[2] + left[2] * scalex2 + up[2] * scaley1;
3866         vertex3f[ 3] = origin[0] + left[0] * scalex2 + up[0] * scaley2;
3867         vertex3f[ 4] = origin[1] + left[1] * scalex2 + up[1] * scaley2;
3868         vertex3f[ 5] = origin[2] + left[2] * scalex2 + up[2] * scaley2;
3869         vertex3f[ 6] = origin[0] + left[0] * scalex1 + up[0] * scaley2;
3870         vertex3f[ 7] = origin[1] + left[1] * scalex1 + up[1] * scaley2;
3871         vertex3f[ 8] = origin[2] + left[2] * scalex1 + up[2] * scaley2;
3872         vertex3f[ 9] = origin[0] + left[0] * scalex1 + up[0] * scaley1;
3873         vertex3f[10] = origin[1] + left[1] * scalex1 + up[1] * scaley1;
3874         vertex3f[11] = origin[2] + left[2] * scalex1 + up[2] * scaley1;
3875
3876         R_Mesh_VertexPointer(vertex3f, 0, 0);
3877         R_Mesh_ColorPointer(NULL, 0, 0);
3878         R_Mesh_ResetTextureState();
3879         R_Mesh_TexBind(0, R_GetTexture(texture));
3880         R_Mesh_TexCoordPointer(0, 2, spritetexcoord2f, 0, 0);
3881         // FIXME: fixed function path can't properly handle r_view.colorscale > 1
3882         GL_Color(cr * fog * r_view.colorscale, cg * fog * r_view.colorscale, cb * fog * r_view.colorscale, ca);
3883         R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3884
3885         if (blendfunc2 == GL_ONE_MINUS_SRC_ALPHA)
3886         {
3887                 R_Mesh_TexBind(0, R_GetTexture(fogtexture));
3888                 GL_BlendFunc(blendfunc1, GL_ONE);
3889                 fog = 1 - fog;
3890                 GL_Color(r_refdef.fogcolor[0] * fog, r_refdef.fogcolor[1] * fog, r_refdef.fogcolor[2] * fog, ca);
3891                 R_Mesh_Draw(0, 4, 2, polygonelements, 0, 0);
3892         }
3893 }
3894
3895 int R_Mesh_AddVertex(rmesh_t *mesh, float x, float y, float z)
3896 {
3897         int i;
3898         float *vertex3f;
3899         float v[3];
3900         VectorSet(v, x, y, z);
3901         for (i = 0, vertex3f = mesh->vertex3f;i < mesh->numvertices;i++, vertex3f += 3)
3902                 if (VectorDistance2(v, vertex3f) < mesh->epsilon2)
3903                         break;
3904         if (i == mesh->numvertices)
3905         {
3906                 if (mesh->numvertices < mesh->maxvertices)
3907                 {
3908                         VectorCopy(v, vertex3f);
3909                         mesh->numvertices++;
3910                 }
3911                 return mesh->numvertices;
3912         }
3913         else
3914                 return i;
3915 }
3916
3917 void R_Mesh_AddPolygon3f(rmesh_t *mesh, int numvertices, float *vertex3f)
3918 {
3919         int i;
3920         int *e, element[3];
3921         element[0] = R_Mesh_AddVertex(mesh, vertex3f[0], vertex3f[1], vertex3f[2]);vertex3f += 3;
3922         element[1] = R_Mesh_AddVertex(mesh, vertex3f[0], vertex3f[1], vertex3f[2]);vertex3f += 3;
3923         e = mesh->element3i + mesh->numtriangles * 3;
3924         for (i = 0;i < numvertices - 2;i++, vertex3f += 3)
3925         {
3926                 element[2] = R_Mesh_AddVertex(mesh, vertex3f[0], vertex3f[1], vertex3f[2]);
3927                 if (mesh->numtriangles < mesh->maxtriangles)
3928                 {
3929                         *e++ = element[0];
3930                         *e++ = element[1];
3931                         *e++ = element[2];
3932                         mesh->numtriangles++;
3933                 }
3934                 element[1] = element[2];
3935         }
3936 }
3937
3938 void R_Mesh_AddPolygon3d(rmesh_t *mesh, int numvertices, double *vertex3d)
3939 {
3940         int i;
3941         int *e, element[3];
3942         element[0] = R_Mesh_AddVertex(mesh, vertex3d[0], vertex3d[1], vertex3d[2]);vertex3d += 3;
3943         element[1] = R_Mesh_AddVertex(mesh, vertex3d[0], vertex3d[1], vertex3d[2]);vertex3d += 3;
3944         e = mesh->element3i + mesh->numtriangles * 3;
3945         for (i = 0;i < numvertices - 2;i++, vertex3d += 3)
3946         {
3947                 element[2] = R_Mesh_AddVertex(mesh, vertex3d[0], vertex3d[1], vertex3d[2]);
3948                 if (mesh->numtriangles < mesh->maxtriangles)
3949                 {
3950                         *e++ = element[0];
3951                         *e++ = element[1];
3952                         *e++ = element[2];
3953                         mesh->numtriangles++;
3954                 }
3955                 element[1] = element[2];
3956         }
3957 }
3958
3959 #define R_MESH_PLANE_DIST_EPSILON (1.0 / 32.0)
3960 void R_Mesh_AddBrushMeshFromPlanes(rmesh_t *mesh, int numplanes, mplane_t *planes)
3961 {
3962         int planenum, planenum2;
3963         int w;
3964         int tempnumpoints;
3965         mplane_t *plane, *plane2;
3966         double maxdist;
3967         double temppoints[2][256*3];
3968         // figure out how large a bounding box we need to properly compute this brush
3969         maxdist = 0;
3970         for (w = 0;w < numplanes;w++)
3971                 maxdist = max(maxdist, planes[w].dist);
3972         // now make it large enough to enclose the entire brush, and round it off to a reasonable multiple of 1024
3973         maxdist = floor(maxdist * (4.0 / 1024.0) + 1) * 1024.0;
3974         for (planenum = 0, plane = planes;planenum < numplanes;planenum++, plane++)
3975         {
3976                 w = 0;
3977                 tempnumpoints = 4;
3978                 PolygonD_QuadForPlane(temppoints[w], plane->normal[0], plane->normal[1], plane->normal[2], plane->dist, maxdist);
3979                 for (planenum2 = 0, plane2 = planes;planenum2 < numplanes && tempnumpoints >= 3;planenum2++, plane2++)
3980                 {
3981                         if (planenum2 == planenum)
3982                                 continue;
3983                         PolygonD_Divide(tempnumpoints, temppoints[w], plane2->normal[0], plane2->normal[1], plane2->normal[2], plane2->dist, R_MESH_PLANE_DIST_EPSILON, 0, NULL, NULL, 256, temppoints[!w], &tempnumpoints, NULL);
3984                         w = !w;
3985                 }
3986                 if (tempnumpoints < 3)
3987                         continue;
3988                 // generate elements forming a triangle fan for this polygon
3989                 R_Mesh_AddPolygon3d(mesh, tempnumpoints, temppoints[w]);
3990         }
3991 }
3992
3993 static void R_Texture_AddLayer(texture_t *t, qboolean depthmask, int blendfunc1, int blendfunc2, texturelayertype_t type, rtexture_t *texture, const matrix4x4_t *matrix, float r, float g, float b, float a)
3994 {
3995         texturelayer_t *layer;
3996         layer = t->currentlayers + t->currentnumlayers++;
3997         layer->type = type;
3998         layer->depthmask = depthmask;
3999         layer->blendfunc1 = blendfunc1;
4000         layer->blendfunc2 = blendfunc2;
4001         layer->texture = texture;
4002         layer->texmatrix = *matrix;
4003         layer->color[0] = r * r_view.colorscale;
4004         layer->color[1] = g * r_view.colorscale;
4005         layer->color[2] = b * r_view.colorscale;
4006         layer->color[3] = a;
4007 }
4008
4009 static float R_EvaluateQ3WaveFunc(q3wavefunc_t func, const float *parms)
4010 {
4011         double index, f;
4012         index = parms[2] + r_refdef.time * parms[3];
4013         index -= floor(index);
4014         switch (func)
4015         {
4016         default:
4017         case Q3WAVEFUNC_NONE:
4018         case Q3WAVEFUNC_NOISE:
4019         case Q3WAVEFUNC_COUNT:
4020                 f = 0;
4021                 break;
4022         case Q3WAVEFUNC_SIN: f = sin(index * M_PI * 2);break;
4023         case Q3WAVEFUNC_SQUARE: f = index < 0.5 ? 1 : -1;break;
4024         case Q3WAVEFUNC_SAWTOOTH: f = index;break;
4025         case Q3WAVEFUNC_INVERSESAWTOOTH: f = 1 - index;break;
4026         case Q3WAVEFUNC_TRIANGLE:
4027                 index *= 4;
4028                 f = index - floor(index);
4029                 if (index < 1)
4030                         f = f;
4031                 else if (index < 2)
4032                         f = 1 - f;
4033                 else if (index < 3)
4034                         f = -f;
4035                 else
4036                         f = -(1 - f);
4037                 break;
4038         }
4039         return (float)(parms[0] + parms[1] * f);
4040 }
4041
4042 void R_UpdateTextureInfo(const entity_render_t *ent, texture_t *t)
4043 {
4044         int i;
4045         model_t *model = ent->model;
4046         float f;
4047         float tcmat[12];
4048         q3shaderinfo_layer_tcmod_t *tcmod;
4049
4050         // switch to an alternate material if this is a q1bsp animated material
4051         {
4052                 texture_t *texture = t;
4053                 int s = ent->skinnum;
4054                 if ((unsigned int)s >= (unsigned int)model->numskins)
4055                         s = 0;
4056                 if (model->skinscenes)
4057                 {
4058                         if (model->skinscenes[s].framecount > 1)
4059                                 s = model->skinscenes[s].firstframe + (unsigned int) (r_refdef.time * model->skinscenes[s].framerate) % model->skinscenes[s].framecount;
4060                         else
4061                                 s = model->skinscenes[s].firstframe;
4062                 }
4063                 if (s > 0)
4064                         t = t + s * model->num_surfaces;
4065                 if (t->animated)
4066                 {
4067                         // use an alternate animation if the entity's frame is not 0,
4068                         // and only if the texture has an alternate animation
4069                         if (ent->frame2 != 0 && t->anim_total[1])
4070                                 t = t->anim_frames[1][(t->anim_total[1] >= 2) ? ((int)(r_refdef.time * 5.0f) % t->anim_total[1]) : 0];
4071                         else
4072                                 t = t->anim_frames[0][(t->anim_total[0] >= 2) ? ((int)(r_refdef.time * 5.0f) % t->anim_total[0]) : 0];
4073                 }
4074                 texture->currentframe = t;
4075         }
4076
4077         // update currentskinframe to be a qw skin or animation frame
4078         if ((i = ent->entitynumber - 1) >= 0 && i < cl.maxclients)
4079         {
4080                 if (strcmp(r_qwskincache[i], cl.scores[i].qw_skin))
4081                 {
4082                         strlcpy(r_qwskincache[i], cl.scores[i].qw_skin, sizeof(r_qwskincache[i]));
4083                         Con_DPrintf("loading skins/%s\n", r_qwskincache[i]);
4084                         r_qwskincache_skinframe[i] = R_SkinFrame_LoadExternal(va("skins/%s", r_qwskincache[i]), TEXF_PRECACHE | (r_mipskins.integer ? TEXF_MIPMAP : 0) | TEXF_PICMIP | TEXF_COMPRESS, developer.integer > 0);
4085                 }
4086                 t->currentskinframe = r_qwskincache_skinframe[i];
4087                 if (t->currentskinframe == NULL)
4088                         t->currentskinframe = t->skinframes[(int)(t->skinframerate * (cl.time - ent->frame2time)) % t->numskinframes];
4089         }
4090         else if (t->numskinframes >= 2)
4091                 t->currentskinframe = t->skinframes[(int)(t->skinframerate * (cl.time - ent->frame2time)) % t->numskinframes];
4092         if (t->backgroundnumskinframes >= 2)
4093                 t->backgroundcurrentskinframe = t->backgroundskinframes[(int)(t->backgroundskinframerate * (cl.time - ent->frame2time)) % t->backgroundnumskinframes];
4094
4095         t->currentmaterialflags = t->basematerialflags;
4096         t->currentalpha = ent->alpha;
4097         if (t->basematerialflags & MATERIALFLAG_WATERALPHA && (model->brush.supportwateralpha || r_novis.integer))
4098         {
4099                 t->currentalpha *= r_wateralpha.value;
4100                 /*
4101                  * FIXME what is this supposed to do?
4102                 // if rendering refraction/reflection, disable transparency
4103                 if (r_waterstate.enabled && (t->currentalpha < 1 || (t->currentmaterialflags & MATERIALFLAG_ALPHA)))
4104                         t->currentmaterialflags |= MATERIALFLAG_WATERSHADER;
4105                 */
4106         }
4107         if(!r_waterstate.enabled)
4108                 t->currentmaterialflags &= ~(MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION);
4109         if (!(ent->flags & RENDER_LIGHT))
4110                 t->currentmaterialflags |= MATERIALFLAG_FULLBRIGHT;
4111         else if (rsurface.modeltexcoordlightmap2f == NULL)
4112         {
4113                 // pick a model lighting mode
4114                 if (VectorLength2(ent->modellight_diffuse) >= (1.0f / 256.0f))
4115                         t->currentmaterialflags |= MATERIALFLAG_MODELLIGHT | MATERIALFLAG_MODELLIGHT_DIRECTIONAL;
4116                 else
4117                         t->currentmaterialflags |= MATERIALFLAG_MODELLIGHT;
4118         }
4119         if (ent->effects & EF_ADDITIVE)
4120                 t->currentmaterialflags |= MATERIALFLAG_ADD | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
4121         else if (t->currentalpha < 1)
4122                 t->currentmaterialflags |= MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED | MATERIALFLAG_NOSHADOW;
4123         if (ent->effects & EF_DOUBLESIDED)
4124                 t->currentmaterialflags |= MATERIALFLAG_NOSHADOW | MATERIALFLAG_NOCULLFACE;
4125         if (ent->effects & EF_NODEPTHTEST)
4126                 t->currentmaterialflags |= MATERIALFLAG_SHORTDEPTHRANGE;
4127         if (ent->flags & RENDER_VIEWMODEL)
4128                 t->currentmaterialflags |= MATERIALFLAG_SHORTDEPTHRANGE;
4129         if (t->backgroundnumskinframes && !(t->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED))
4130                 t->currentmaterialflags |= MATERIALFLAG_VERTEXTEXTUREBLEND;
4131
4132         // make sure that the waterscroll matrix is used on water surfaces when
4133         // there is no tcmod
4134         if (t->currentmaterialflags & MATERIALFLAG_WATER && r_waterscroll.value != 0)
4135                 t->currenttexmatrix = r_waterscrollmatrix;
4136
4137         for (i = 0, tcmod = t->tcmods;i < Q3MAXTCMODS && tcmod->tcmod;i++, tcmod++)
4138         {
4139                 matrix4x4_t matrix;
4140                 switch(tcmod->tcmod)
4141                 {
4142                 case Q3TCMOD_COUNT:
4143                 case Q3TCMOD_NONE:
4144                         if (t->currentmaterialflags & MATERIALFLAG_WATER && r_waterscroll.value != 0)
4145                                 matrix = r_waterscrollmatrix;
4146                         else
4147                                 matrix = identitymatrix;
4148                         break;
4149                 case Q3TCMOD_ENTITYTRANSLATE:
4150                         // this is used in Q3 to allow the gamecode to control texcoord
4151                         // scrolling on the entity, which is not supported in darkplaces yet.
4152                         Matrix4x4_CreateTranslate(&matrix, 0, 0, 0);
4153                         break;
4154                 case Q3TCMOD_ROTATE:
4155                         Matrix4x4_CreateTranslate(&matrix, 0.5, 0.5, 0);
4156                         Matrix4x4_ConcatRotate(&matrix, tcmod->parms[0] * r_refdef.time, 0, 0, 1);
4157                         Matrix4x4_ConcatTranslate(&matrix, -0.5, -0.5, 0);
4158                         break;
4159                 case Q3TCMOD_SCALE:
4160                         Matrix4x4_CreateScale3(&matrix, tcmod->parms[0], tcmod->parms[1], 1);
4161                         break;
4162                 case Q3TCMOD_SCROLL:
4163                         Matrix4x4_CreateTranslate(&matrix, tcmod->parms[0] * r_refdef.time, tcmod->parms[1] * r_refdef.time, 0);
4164                         break;
4165                 case Q3TCMOD_STRETCH:
4166                         f = 1.0f / R_EvaluateQ3WaveFunc(tcmod->wavefunc, tcmod->waveparms);
4167                         Matrix4x4_CreateFromQuakeEntity(&matrix, 0.5f * (1 - f), 0.5 * (1 - f), 0, 0, 0, 0, f);
4168                         break;
4169                 case Q3TCMOD_TRANSFORM:
4170                         VectorSet(tcmat +  0, tcmod->parms[0], tcmod->parms[1], 0);
4171                         VectorSet(tcmat +  3, tcmod->parms[2], tcmod->parms[3], 0);
4172                         VectorSet(tcmat +  6, 0                   , 0                , 1);
4173                         VectorSet(tcmat +  9, tcmod->parms[4], tcmod->parms[5], 0);
4174                         Matrix4x4_FromArray12FloatGL(&matrix, tcmat);
4175                         break;
4176                 case Q3TCMOD_TURBULENT:
4177                         // this is handled in the RSurf_PrepareVertices function
4178                         matrix = identitymatrix;
4179                         break;
4180                 }
4181                 // either replace or concatenate the transformation
4182                 if (i < 1)
4183                         t->currenttexmatrix = matrix;
4184                 else
4185                 {
4186                         matrix4x4_t temp = t->currenttexmatrix;
4187                         Matrix4x4_Concat(&t->currenttexmatrix, &matrix, &temp);
4188                 }
4189         }
4190
4191         t->colormapping = VectorLength2(ent->colormap_pantscolor) + VectorLength2(ent->colormap_shirtcolor) >= (1.0f / 1048576.0f);
4192         t->basetexture = (!t->colormapping && t->currentskinframe->merged) ? t->currentskinframe->merged : t->currentskinframe->base;
4193         t->glosstexture = r_texture_black;
4194         t->backgroundbasetexture = t->backgroundnumskinframes ? ((!t->colormapping && t->backgroundcurrentskinframe->merged) ? t->backgroundcurrentskinframe->merged : t->backgroundcurrentskinframe->base) : r_texture_white;
4195         t->backgroundglosstexture = r_texture_black;
4196         t->specularpower = r_shadow_glossexponent.value;
4197         // TODO: store reference values for these in the texture?
4198         t->specularscale = 0;
4199         if (r_shadow_gloss.integer > 0)
4200         {
4201                 if (t->currentskinframe->gloss || (t->backgroundcurrentskinframe && t->backgroundcurrentskinframe->gloss))
4202                 {
4203                         if (r_shadow_glossintensity.value > 0)
4204                         {
4205                                 t->glosstexture = t->currentskinframe->gloss ? t->currentskinframe->gloss : r_texture_white;
4206                                 t->backgroundglosstexture = (t->backgroundcurrentskinframe && t->backgroundcurrentskinframe->gloss) ? t->backgroundcurrentskinframe->gloss : r_texture_white;
4207                                 t->specularscale = r_shadow_glossintensity.value;
4208                         }
4209                 }
4210                 else if (r_shadow_gloss.integer >= 2 && r_shadow_gloss2intensity.value > 0)
4211                 {
4212                         t->glosstexture = r_texture_white;
4213                         t->backgroundglosstexture = r_texture_white;
4214                         t->specularscale = r_shadow_gloss2intensity.value;
4215                 }
4216         }
4217
4218         // lightmaps mode looks bad with dlights using actual texturing, so turn
4219         // off the colormap and glossmap, but leave the normalmap on as it still
4220         // accurately represents the shading involved
4221         if (gl_lightmaps.integer && !(t->currentmaterialflags & MATERIALFLAG_BLENDED))
4222         {
4223                 t->basetexture = r_texture_white;
4224                 t->specularscale = 0;
4225         }
4226
4227         VectorClear(t->dlightcolor);
4228         t->currentnumlayers = 0;
4229         if (!(t->currentmaterialflags & MATERIALFLAG_NODRAW))
4230         {
4231                 if (!(t->currentmaterialflags & MATERIALFLAG_SKY))
4232                 {
4233                         int blendfunc1, blendfunc2, depthmask;
4234                         if (t->currentmaterialflags & MATERIALFLAG_ADD)
4235                         {
4236                                 blendfunc1 = GL_SRC_ALPHA;
4237                                 blendfunc2 = GL_ONE;
4238                         }
4239                         else if (t->currentmaterialflags & MATERIALFLAG_ALPHA)
4240                         {
4241                                 blendfunc1 = GL_SRC_ALPHA;
4242                                 blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
4243                         }
4244                         else if (t->currentmaterialflags & MATERIALFLAG_CUSTOMBLEND)
4245                         {
4246                                 blendfunc1 = t->customblendfunc[0];
4247                                 blendfunc2 = t->customblendfunc[1];
4248                         }
4249                         else
4250                         {
4251                                 blendfunc1 = GL_ONE;
4252                                 blendfunc2 = GL_ZERO;
4253                         }
4254                         depthmask = !(t->currentmaterialflags & MATERIALFLAG_BLENDED);
4255                         if (t->currentmaterialflags & (MATERIALFLAG_WATER | MATERIALFLAG_WALL))
4256                         {
4257                                 rtexture_t *currentbasetexture;
4258                                 int layerflags = 0;
4259                                 if (r_refdef.fogenabled && (t->currentmaterialflags & MATERIALFLAG_BLENDED))
4260                                         layerflags |= TEXTURELAYERFLAG_FOGDARKEN;
4261                                 currentbasetexture = (VectorLength2(ent->colormap_pantscolor) + VectorLength2(ent->colormap_shirtcolor) < (1.0f / 1048576.0f) && t->currentskinframe->merged) ? t->currentskinframe->merged : t->currentskinframe->base;
4262                                 if (t->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)
4263                                 {
4264                                         // fullbright is not affected by r_refdef.lightmapintensity
4265                                         R_Texture_AddLayer(t, depthmask, blendfunc1, blendfunc2, TEXTURELAYERTYPE_TEXTURE, currentbasetexture, &t->currenttexmatrix, ent->colormod[0], ent->colormod[1], ent->colormod[2], t->currentalpha);
4266                                         if (VectorLength2(ent->colormap_pantscolor) >= (1.0f / 1048576.0f) && t->currentskinframe->pants)
4267                                                 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->currentskinframe->pants, &t->currenttexmatrix, ent->colormap_pantscolor[0] * ent->colormod[0], ent->colormap_pantscolor[1] * ent->colormod[1], ent->colormap_pantscolor[2] * ent->colormod[2], t->currentalpha);
4268                                         if (VectorLength2(ent->colormap_shirtcolor) >= (1.0f / 1048576.0f) && t->currentskinframe->shirt)
4269                                                 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->currentskinframe->shirt, &t->currenttexmatrix, ent->colormap_shirtcolor[0] * ent->colormod[0], ent->colormap_shirtcolor[1] * ent->colormod[1], ent->colormap_shirtcolor[2] * ent->colormod[2], t->currentalpha);
4270                                 }
4271                                 else
4272                                 {
4273                                         float colorscale;
4274                                         // set the color tint used for lights affecting this surface
4275                                         VectorSet(t->dlightcolor, ent->colormod[0] * t->currentalpha, ent->colormod[1] * t->currentalpha, ent->colormod[2] * t->currentalpha);
4276                                         colorscale = 2;
4277                                         // q3bsp has no lightmap updates, so the lightstylevalue that
4278                                         // would normally be baked into the lightmap must be
4279                                         // applied to the color
4280                                         // FIXME: r_glsl 1 rendering doesn't support overbright lightstyles with this (the default light style is not overbright)
4281                                         if (ent->model->type == mod_brushq3)
4282                                                 colorscale *= r_refdef.rtlightstylevalue[0];
4283                                         colorscale *= r_refdef.lightmapintensity;
4284                                         Vector4Set(t->lightmapcolor, ent->colormod[0] * colorscale, ent->colormod[1] * colorscale, ent->colormod[2] * colorscale, t->currentalpha);
4285                                         R_Texture_AddLayer(t, depthmask, blendfunc1, blendfunc2, TEXTURELAYERTYPE_LITTEXTURE, currentbasetexture, &t->currenttexmatrix, ent->colormod[0] * colorscale, ent->colormod[1] * colorscale, ent->colormod[2] * colorscale, t->currentalpha);
4286                                         if (r_ambient.value >= (1.0f/64.0f))
4287                                                 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, currentbasetexture, &t->currenttexmatrix, ent->colormod[0] * r_ambient.value * (1.0f / 64.0f), ent->colormod[1] * r_ambient.value * (1.0f / 64.0f), ent->colormod[2] * r_ambient.value * (1.0f / 64.0f), t->currentalpha);
4288                                         if (VectorLength2(ent->colormap_pantscolor) >= (1.0f / 1048576.0f) && t->currentskinframe->pants)
4289                                         {
4290                                                 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_LITTEXTURE, t->currentskinframe->pants, &t->currenttexmatrix, ent->colormap_pantscolor[0] * ent->colormod[0] * colorscale, ent->colormap_pantscolor[1] * ent->colormod[1] * colorscale, ent->colormap_pantscolor[2]  * ent->colormod[2] * colorscale, t->currentalpha);
4291                                                 if (r_ambient.value >= (1.0f/64.0f))
4292                                                         R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->currentskinframe->pants, &t->currenttexmatrix, ent->colormap_pantscolor[0] * ent->colormod[0] * r_ambient.value * (1.0f / 64.0f), ent->colormap_pantscolor[1] * ent->colormod[1] * r_ambient.value * (1.0f / 64.0f), ent->colormap_pantscolor[2] * ent->colormod[2] * r_ambient.value * (1.0f / 64.0f), t->currentalpha);
4293                                         }
4294                                         if (VectorLength2(ent->colormap_shirtcolor) >= (1.0f / 1048576.0f) && t->currentskinframe->shirt)
4295                                         {
4296                                                 R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_LITTEXTURE, t->currentskinframe->shirt, &t->currenttexmatrix, ent->colormap_shirtcolor[0] * ent->colormod[0] * colorscale, ent->colormap_shirtcolor[1] * ent->colormod[1] * colorscale, ent->colormap_shirtcolor[2] * ent->colormod[2] * colorscale, t->currentalpha);
4297                                                 if (r_ambient.value >= (1.0f/64.0f))
4298                                                         R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->currentskinframe->shirt, &t->currenttexmatrix, ent->colormap_shirtcolor[0] * ent->colormod[0] * r_ambient.value * (1.0f / 64.0f), ent->colormap_shirtcolor[1] * ent->colormod[1] * r_ambient.value * (1.0f / 64.0f), ent->colormap_shirtcolor[2] * ent->colormod[2] * r_ambient.value * (1.0f / 64.0f), t->currentalpha);
4299                                         }
4300                                 }
4301                                 if (t->currentskinframe->glow != NULL)
4302                                         R_Texture_AddLayer(t, false, GL_SRC_ALPHA, GL_ONE, TEXTURELAYERTYPE_TEXTURE, t->currentskinframe->glow, &t->currenttexmatrix, r_hdr_glowintensity.value, r_hdr_glowintensity.value, r_hdr_glowintensity.value, t->currentalpha);
4303                                 if (r_refdef.fogenabled && !(t->currentmaterialflags & MATERIALFLAG_ADD))
4304                                 {
4305                                         // if this is opaque use alpha blend which will darken the earlier
4306                                         // passes cheaply.
4307                                         //
4308                                         // if this is an alpha blended material, all the earlier passes
4309                                         // were darkened by fog already, so we only need to add the fog
4310                                         // color ontop through the fog mask texture
4311                                         //
4312                                         // if this is an additive blended material, all the earlier passes
4313                                         // were darkened by fog already, and we should not add fog color
4314                                         // (because the background was not darkened, there is no fog color
4315                                         // that was lost behind it).
4316                                         R_Texture_AddLayer(t, false, GL_SRC_ALPHA, (t->currentmaterialflags & MATERIALFLAG_BLENDED) ? GL_ONE : GL_ONE_MINUS_SRC_ALPHA, TEXTURELAYERTYPE_FOG, t->currentskinframe->fog, &identitymatrix, r_refdef.fogcolor[0] / r_view.colorscale, r_refdef.fogcolor[1] / r_view.colorscale, r_refdef.fogcolor[2] / r_view.colorscale, t->currentalpha);
4317                                 }
4318                         }
4319                 }
4320         }
4321 }
4322
4323 void R_UpdateAllTextureInfo(entity_render_t *ent)
4324 {
4325         int i;
4326         if (ent->model)
4327                 for (i = 0;i < ent->model->num_texturesperskin;i++)
4328                         R_UpdateTextureInfo(ent, ent->model->data_textures + i);
4329 }
4330
4331 rsurfacestate_t rsurface;
4332
4333 void R_Mesh_ResizeArrays(int newvertices)
4334 {
4335         float *base;
4336         if (rsurface.array_size >= newvertices)
4337                 return;
4338         if (rsurface.array_modelvertex3f)
4339                 Mem_Free(rsurface.array_modelvertex3f);
4340         rsurface.array_size = (newvertices + 1023) & ~1023;
4341         base = (float *)Mem_Alloc(r_main_mempool, rsurface.array_size * sizeof(float[33]));
4342         rsurface.array_modelvertex3f     = base + rsurface.array_size * 0;
4343         rsurface.array_modelsvector3f    = base + rsurface.array_size * 3;
4344         rsurface.array_modeltvector3f    = base + rsurface.array_size * 6;
4345         rsurface.array_modelnormal3f     = base + rsurface.array_size * 9;
4346         rsurface.array_deformedvertex3f  = base + rsurface.array_size * 12;
4347         rsurface.array_deformedsvector3f = base + rsurface.array_size * 15;
4348         rsurface.array_deformedtvector3f = base + rsurface.array_size * 18;
4349         rsurface.array_deformednormal3f  = base + rsurface.array_size * 21;
4350         rsurface.array_texcoord3f        = base + rsurface.array_size * 24;
4351         rsurface.array_color4f           = base + rsurface.array_size * 27;
4352         rsurface.array_generatedtexcoordtexture2f = base + rsurface.array_size * 31;
4353 }
4354
4355 void RSurf_CleanUp(void)
4356 {
4357         CHECKGLERROR
4358         if (rsurface.mode == RSURFMODE_GLSL)
4359         {
4360                 qglUseProgramObjectARB(0);CHECKGLERROR
4361         }
4362         GL_AlphaTest(false);
4363         rsurface.mode = RSURFMODE_NONE;
4364         rsurface.uselightmaptexture = false;
4365         rsurface.texture = NULL;
4366 }
4367
4368 void RSurf_ActiveWorldEntity(void)
4369 {
4370         model_t *model = r_refdef.worldmodel;
4371         RSurf_CleanUp();
4372         if (rsurface.array_size < model->surfmesh.num_vertices)
4373                 R_Mesh_ResizeArrays(model->surfmesh.num_vertices);
4374         rsurface.matrix = identitymatrix;
4375         rsurface.inversematrix = identitymatrix;
4376         R_Mesh_Matrix(&identitymatrix);
4377         VectorCopy(r_view.origin, rsurface.modelorg);
4378         VectorSet(rsurface.modellight_ambient, 0, 0, 0);
4379         VectorSet(rsurface.modellight_diffuse, 0, 0, 0);
4380         VectorSet(rsurface.modellight_lightdir, 0, 0, 1);
4381         VectorSet(rsurface.colormap_pantscolor, 0, 0, 0);
4382         VectorSet(rsurface.colormap_shirtcolor, 0, 0, 0);
4383         rsurface.frameblend[0].frame = 0;
4384         rsurface.frameblend[0].lerp = 1;
4385         rsurface.frameblend[1].frame = 0;
4386         rsurface.frameblend[1].lerp = 0;
4387         rsurface.frameblend[2].frame = 0;
4388         rsurface.frameblend[2].lerp = 0;
4389         rsurface.frameblend[3].frame = 0;
4390         rsurface.frameblend[3].lerp = 0;
4391         rsurface.basepolygonfactor = r_refdef.polygonfactor;
4392         rsurface.basepolygonoffset = r_refdef.polygonoffset;
4393         rsurface.modelvertex3f  = model->surfmesh.data_vertex3f;
4394         rsurface.modelvertex3f_bufferobject = model->surfmesh.vbo;
4395         rsurface.modelvertex3f_bufferoffset = model->surfmesh.vbooffset_vertex3f;
4396         rsurface.modelsvector3f = model->surfmesh.data_svector3f;
4397         rsurface.modelsvector3f_bufferobject = model->surfmesh.vbo;
4398         rsurface.modelsvector3f_bufferoffset = model->surfmesh.vbooffset_svector3f;
4399         rsurface.modeltvector3f = model->surfmesh.data_tvector3f;
4400         rsurface.modeltvector3f_bufferobject = model->surfmesh.vbo;
4401         rsurface.modeltvector3f_bufferoffset = model->surfmesh.vbooffset_tvector3f;
4402         rsurface.modelnormal3f  = model->surfmesh.data_normal3f;
4403         rsurface.modelnormal3f_bufferobject = model->surfmesh.vbo;
4404         rsurface.modelnormal3f_bufferoffset = model->surfmesh.vbooffset_normal3f;
4405         rsurface.modellightmapcolor4f  = model->surfmesh.data_lightmapcolor4f;
4406         rsurface.modellightmapcolor4f_bufferobject = model->surfmesh.vbo;
4407         rsurface.modellightmapcolor4f_bufferoffset = model->surfmesh.vbooffset_lightmapcolor4f;
4408         rsurface.modeltexcoordtexture2f  = model->surfmesh.data_texcoordtexture2f;
4409         rsurface.modeltexcoordtexture2f_bufferobject = model->surfmesh.vbo;
4410         rsurface.modeltexcoordtexture2f_bufferoffset = model->surfmesh.vbooffset_texcoordtexture2f;
4411         rsurface.modeltexcoordlightmap2f  = model->surfmesh.data_texcoordlightmap2f;
4412         rsurface.modeltexcoordlightmap2f_bufferobject = model->surfmesh.vbo;
4413         rsurface.modeltexcoordlightmap2f_bufferoffset = model->surfmesh.vbooffset_texcoordlightmap2f;
4414         rsurface.modelelement3i = model->surfmesh.data_element3i;
4415         rsurface.modelelement3i_bufferobject = model->surfmesh.ebo;
4416         rsurface.modellightmapoffsets = model->surfmesh.data_lightmapoffsets;
4417         rsurface.modelnum_vertices = model->surfmesh.num_vertices;
4418         rsurface.modelnum_triangles = model->surfmesh.num_triangles;
4419         rsurface.modelsurfaces = model->data_surfaces;
4420         rsurface.generatedvertex = false;
4421         rsurface.vertex3f  = rsurface.modelvertex3f;
4422         rsurface.vertex3f_bufferobject = rsurface.modelvertex3f_bufferobject;
4423         rsurface.vertex3f_bufferoffset = rsurface.modelvertex3f_bufferoffset;
4424         rsurface.svector3f = rsurface.modelsvector3f;
4425         rsurface.svector3f_bufferobject = rsurface.modelsvector3f_bufferobject;
4426         rsurface.svector3f_bufferoffset = rsurface.modelsvector3f_bufferoffset;
4427         rsurface.tvector3f = rsurface.modeltvector3f;
4428         rsurface.tvector3f_bufferobject = rsurface.modeltvector3f_bufferobject;
4429         rsurface.tvector3f_bufferoffset = rsurface.modeltvector3f_bufferoffset;
4430         rsurface.normal3f  = rsurface.modelnormal3f;
4431         rsurface.normal3f_bufferobject = rsurface.modelnormal3f_bufferobject;
4432         rsurface.normal3f_bufferoffset = rsurface.modelnormal3f_bufferoffset;
4433         rsurface.texcoordtexture2f = rsurface.modeltexcoordtexture2f;
4434 }
4435
4436 void RSurf_ActiveModelEntity(const entity_render_t *ent, qboolean wantnormals, qboolean wanttangents)
4437 {
4438         model_t *model = ent->model;
4439         RSurf_CleanUp();
4440         if (rsurface.array_size < model->surfmesh.num_vertices)
4441                 R_Mesh_ResizeArrays(model->surfmesh.num_vertices);
4442         rsurface.matrix = ent->matrix;
4443         rsurface.inversematrix = ent->inversematrix;
4444         R_Mesh_Matrix(&rsurface.matrix);
4445         Matrix4x4_Transform(&rsurface.inversematrix, r_view.origin, rsurface.modelorg);
4446         rsurface.modellight_ambient[0] = ent->modellight_ambient[0] * ent->colormod[0];
4447         rsurface.modellight_ambient[1] = ent->modellight_ambient[1] * ent->colormod[1];
4448         rsurface.modellight_ambient[2] = ent->modellight_ambient[2] * ent->colormod[2];
4449         rsurface.modellight_diffuse[0] = ent->modellight_diffuse[0] * ent->colormod[0];
4450         rsurface.modellight_diffuse[1] = ent->modellight_diffuse[1] * ent->colormod[1];
4451         rsurface.modellight_diffuse[2] = ent->modellight_diffuse[2] * ent->colormod[2];
4452         VectorCopy(ent->modellight_diffuse, rsurface.modellight_diffuse);
4453         VectorCopy(ent->modellight_lightdir, rsurface.modellight_lightdir);
4454         VectorCopy(ent->colormap_pantscolor, rsurface.colormap_pantscolor);
4455         VectorCopy(ent->colormap_shirtcolor, rsurface.colormap_shirtcolor);
4456         rsurface.frameblend[0] = ent->frameblend[0];
4457         rsurface.frameblend[1] = ent->frameblend[1];
4458         rsurface.frameblend[2] = ent->frameblend[2];
4459         rsurface.frameblend[3] = ent->frameblend[3];
4460         rsurface.basepolygonfactor = r_refdef.polygonfactor;
4461         rsurface.basepolygonoffset = r_refdef.polygonoffset;
4462         if (ent->model->brush.submodel)
4463         {
4464                 rsurface.basepolygonfactor += r_polygonoffset_submodel_factor.value;
4465                 rsurface.basepolygonoffset += r_polygonoffset_submodel_offset.value;
4466         }
4467         if (model->surfmesh.isanimated && (rsurface.frameblend[0].lerp != 1 || rsurface.frameblend[0].frame != 0))
4468         {
4469                 if (wanttangents)
4470                 {
4471                         rsurface.modelvertex3f = rsurface.array_modelvertex3f;
4472                         rsurface.modelsvector3f = rsurface.array_modelsvector3f;
4473                         rsurface.modeltvector3f = rsurface.array_modeltvector3f;
4474                         rsurface.modelnormal3f = rsurface.array_modelnormal3f;
4475                         Mod_Alias_GetMesh_Vertices(model, rsurface.frameblend, rsurface.array_modelvertex3f, rsurface.array_modelnormal3f, rsurface.array_modelsvector3f, rsurface.array_modeltvector3f);
4476                 }
4477                 else if (wantnormals)
4478                 {
4479                         rsurface.modelvertex3f = rsurface.array_modelvertex3f;
4480                         rsurface.modelsvector3f = NULL;
4481                         rsurface.modeltvector3f = NULL;
4482                         rsurface.modelnormal3f = rsurface.array_modelnormal3f;
4483                         Mod_Alias_GetMesh_Vertices(model, rsurface.frameblend, rsurface.array_modelvertex3f, rsurface.array_modelnormal3f, NULL, NULL);
4484                 }
4485                 else
4486                 {
4487                         rsurface.modelvertex3f = rsurface.array_modelvertex3f;
4488                         rsurface.modelsvector3f = NULL;
4489                         rsurface.modeltvector3f = NULL;
4490                         rsurface.modelnormal3f = NULL;
4491                         Mod_Alias_GetMesh_Vertices(model, rsurface.frameblend, rsurface.array_modelvertex3f, NULL, NULL, NULL);
4492                 }
4493                 rsurface.modelvertex3f_bufferobject = 0;
4494                 rsurface.modelvertex3f_bufferoffset = 0;
4495                 rsurface.modelsvector3f_bufferobject = 0;
4496                 rsurface.modelsvector3f_bufferoffset = 0;
4497                 rsurface.modeltvector3f_bufferobject = 0;
4498                 rsurface.modeltvector3f_bufferoffset = 0;
4499                 rsurface.modelnormal3f_bufferobject = 0;
4500                 rsurface.modelnormal3f_bufferoffset = 0;
4501                 rsurface.generatedvertex = true;
4502         }
4503         else
4504         {
4505                 rsurface.modelvertex3f  = model->surfmesh.data_vertex3f;
4506                 rsurface.modelvertex3f_bufferobject = model->surfmesh.vbo;
4507                 rsurface.modelvertex3f_bufferoffset = model->surfmesh.vbooffset_vertex3f;
4508                 rsurface.modelsvector3f = model->surfmesh.data_svector3f;
4509                 rsurface.modelsvector3f_bufferobject = model->surfmesh.vbo;
4510                 rsurface.modelsvector3f_bufferoffset = model->surfmesh.vbooffset_svector3f;
4511                 rsurface.modeltvector3f = model->surfmesh.data_tvector3f;
4512                 rsurface.modeltvector3f_bufferobject = model->surfmesh.vbo;
4513                 rsurface.modeltvector3f_bufferoffset = model->surfmesh.vbooffset_tvector3f;
4514                 rsurface.modelnormal3f  = model->surfmesh.data_normal3f;
4515                 rsurface.modelnormal3f_bufferobject = model->surfmesh.vbo;
4516                 rsurface.modelnormal3f_bufferoffset = model->surfmesh.vbooffset_normal3f;
4517                 rsurface.generatedvertex = false;
4518         }
4519         rsurface.modellightmapcolor4f  = model->surfmesh.data_lightmapcolor4f;
4520         rsurface.modellightmapcolor4f_bufferobject = model->surfmesh.vbo;
4521         rsurface.modellightmapcolor4f_bufferoffset = model->surfmesh.vbooffset_lightmapcolor4f;
4522         rsurface.modeltexcoordtexture2f  = model->surfmesh.data_texcoordtexture2f;
4523         rsurface.modeltexcoordtexture2f_bufferobject = model->surfmesh.vbo;
4524         rsurface.modeltexcoordtexture2f_bufferoffset = model->surfmesh.vbooffset_texcoordtexture2f;
4525         rsurface.modeltexcoordlightmap2f  = model->surfmesh.data_texcoordlightmap2f;
4526         rsurface.modeltexcoordlightmap2f_bufferobject = model->surfmesh.vbo;
4527         rsurface.modeltexcoordlightmap2f_bufferoffset = model->surfmesh.vbooffset_texcoordlightmap2f;
4528         rsurface.modelelement3i = model->surfmesh.data_element3i;
4529         rsurface.modelelement3i_bufferobject = model->surfmesh.ebo;
4530         rsurface.modellightmapoffsets = model->surfmesh.data_lightmapoffsets;
4531         rsurface.modelnum_vertices = model->surfmesh.num_vertices;
4532         rsurface.modelnum_triangles = model->surfmesh.num_triangles;
4533         rsurface.modelsurfaces = model->data_surfaces;
4534         rsurface.vertex3f  = rsurface.modelvertex3f;
4535         rsurface.vertex3f_bufferobject = rsurface.modelvertex3f_bufferobject;
4536         rsurface.vertex3f_bufferoffset = rsurface.modelvertex3f_bufferoffset;
4537         rsurface.svector3f = rsurface.modelsvector3f;
4538         rsurface.svector3f_bufferobject = rsurface.modelsvector3f_bufferobject;
4539         rsurface.svector3f_bufferoffset = rsurface.modelsvector3f_bufferoffset;
4540         rsurface.tvector3f = rsurface.modeltvector3f;
4541         rsurface.tvector3f_bufferobject = rsurface.modeltvector3f_bufferobject;
4542         rsurface.tvector3f_bufferoffset = rsurface.modeltvector3f_bufferoffset;
4543         rsurface.normal3f  = rsurface.modelnormal3f;
4544         rsurface.normal3f_bufferobject = rsurface.modelnormal3f_bufferobject;
4545         rsurface.normal3f_bufferoffset = rsurface.modelnormal3f_bufferoffset;
4546         rsurface.texcoordtexture2f = rsurface.modeltexcoordtexture2f;
4547 }
4548
4549 static const int quadedges[6][2] = {{0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 3}, {2, 3}};
4550 void RSurf_PrepareVerticesForBatch(qboolean generatenormals, qboolean generatetangents, int texturenumsurfaces, msurface_t **texturesurfacelist)
4551 {
4552         int deformindex;
4553         int texturesurfaceindex;
4554         int i, j;
4555         float amplitude;
4556         float animpos;
4557         float scale;
4558         const float *v1, *in_tc;
4559         float *out_tc;
4560         float center[3], forward[3], right[3], up[3], v[3], newforward[3], newright[3], newup[3];
4561         float waveparms[4];
4562         q3shaderinfo_deform_t *deform;
4563         // if vertices are dynamic (animated models), generate them into the temporary rsurface.array_model* arrays and point rsurface.model* at them instead of the static data from the model itself
4564         if (rsurface.generatedvertex)
4565         {
4566                 if (rsurface.texture->tcgen.tcgen == Q3TCGEN_ENVIRONMENT)
4567                         generatenormals = true;
4568                 for (i = 0;i < Q3MAXDEFORMS;i++)
4569                 {
4570                         if (rsurface.texture->deforms[i].deform == Q3DEFORM_AUTOSPRITE)
4571                         {
4572                                 generatetangents = true;
4573                                 generatenormals = true;
4574                         }
4575                         if (rsurface.texture->deforms[i].deform != Q3DEFORM_NONE)
4576                                 generatenormals = true;
4577                 }
4578                 if (generatenormals && !rsurface.modelnormal3f)
4579                 {
4580                         rsurface.normal3f = rsurface.modelnormal3f = rsurface.array_modelnormal3f;
4581                         rsurface.normal3f_bufferobject = rsurface.modelnormal3f_bufferobject = 0;
4582                         rsurface.normal3f_bufferoffset = rsurface.modelnormal3f_bufferoffset = 0;
4583                         Mod_BuildNormals(0, rsurface.modelnum_vertices, rsurface.modelnum_triangles, rsurface.modelvertex3f, rsurface.modelelement3i, rsurface.array_modelnormal3f, r_smoothnormals_areaweighting.integer);
4584                 }
4585                 if (generatetangents && !rsurface.modelsvector3f)
4586                 {
4587                         rsurface.svector3f = rsurface.modelsvector3f = rsurface.array_modelsvector3f;
4588                         rsurface.svector3f_bufferobject = rsurface.modelsvector3f_bufferobject = 0;
4589                         rsurface.svector3f_bufferoffset = rsurface.modelsvector3f_bufferoffset = 0;
4590                         rsurface.tvector3f = rsurface.modeltvector3f = rsurface.array_modeltvector3f;
4591                         rsurface.tvector3f_bufferobject = rsurface.modeltvector3f_bufferobject = 0;
4592                         rsurface.tvector3f_bufferoffset = rsurface.modeltvector3f_bufferoffset = 0;
4593                         Mod_BuildTextureVectorsFromNormals(0, rsurface.modelnum_vertices, rsurface.modelnum_triangles, rsurface.modelvertex3f, rsurface.modeltexcoordtexture2f, rsurface.modelnormal3f, rsurface.modelelement3i, rsurface.array_modelsvector3f, rsurface.array_modeltvector3f, r_smoothnormals_areaweighting.integer);
4594                 }
4595         }
4596         rsurface.vertex3f  = rsurface.modelvertex3f;
4597         rsurface.vertex3f_bufferobject = rsurface.modelvertex3f_bufferobject;
4598         rsurface.vertex3f_bufferoffset = rsurface.modelvertex3f_bufferoffset;
4599         rsurface.svector3f = rsurface.modelsvector3f;
4600         rsurface.svector3f_bufferobject = rsurface.modelsvector3f_bufferobject;
4601         rsurface.svector3f_bufferoffset = rsurface.modelsvector3f_bufferoffset;
4602         rsurface.tvector3f = rsurface.modeltvector3f;
4603         rsurface.tvector3f_bufferobject = rsurface.modeltvector3f_bufferobject;
4604         rsurface.tvector3f_bufferoffset = rsurface.modeltvector3f_bufferoffset;
4605         rsurface.normal3f  = rsurface.modelnormal3f;
4606         rsurface.normal3f_bufferobject = rsurface.modelnormal3f_bufferobject;
4607         rsurface.normal3f_bufferoffset = rsurface.modelnormal3f_bufferoffset;
4608         // if vertices are deformed (sprite flares and things in maps, possibly
4609         // water waves, bulges and other deformations), generate them into
4610         // rsurface.deform* arrays from whatever the rsurface.* arrays point to
4611         // (may be static model data or generated data for an animated model, or
4612         //  the previous deform pass)
4613         for (deformindex = 0, deform = rsurface.texture->deforms;deformindex < Q3MAXDEFORMS && deform->deform;deformindex++, deform++)
4614         {
4615                 switch (deform->deform)
4616                 {
4617                 default:
4618                 case Q3DEFORM_PROJECTIONSHADOW:
4619                 case Q3DEFORM_TEXT0:
4620                 case Q3DEFORM_TEXT1:
4621                 case Q3DEFORM_TEXT2:
4622                 case Q3DEFORM_TEXT3:
4623                 case Q3DEFORM_TEXT4:
4624                 case Q3DEFORM_TEXT5:
4625                 case Q3DEFORM_TEXT6:
4626                 case Q3DEFORM_TEXT7:
4627                 case Q3DEFORM_NONE:
4628                         break;
4629                 case Q3DEFORM_AUTOSPRITE:
4630                         Matrix4x4_Transform3x3(&rsurface.inversematrix, r_view.forward, newforward);
4631                         Matrix4x4_Transform3x3(&rsurface.inversematrix, r_view.right, newright);
4632                         Matrix4x4_Transform3x3(&rsurface.inversematrix, r_view.up, newup);
4633                         VectorNormalize(newforward);
4634                         VectorNormalize(newright);
4635                         VectorNormalize(newup);
4636                         // make deformed versions of only the model vertices used by the specified surfaces
4637                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4638                         {
4639                                 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4640                                 // a single autosprite surface can contain multiple sprites...
4641                                 for (j = 0;j < surface->num_vertices - 3;j += 4)
4642                                 {
4643                                         VectorClear(center);
4644                                         for (i = 0;i < 4;i++)
4645                                                 VectorAdd(center, (rsurface.vertex3f + 3 * surface->num_firstvertex) + (j+i) * 3, center);
4646                                         VectorScale(center, 0.25f, center);
4647                                         VectorCopy((rsurface.normal3f  + 3 * surface->num_firstvertex) + j*3, forward);
4648                                         VectorCopy((rsurface.svector3f + 3 * surface->num_firstvertex) + j*3, right);
4649                                         VectorCopy((rsurface.tvector3f + 3 * surface->num_firstvertex) + j*3, up);
4650                                         for (i = 0;i < 4;i++)
4651                                         {
4652                                                 VectorSubtract((rsurface.vertex3f + 3 * surface->num_firstvertex) + (j+i)*3, center, v);
4653                                                 VectorMAMAMAM(1, center, DotProduct(forward, v), newforward, DotProduct(right, v), newright, DotProduct(up, v), newup, rsurface.array_deformedvertex3f + (surface->num_firstvertex+i+j) * 3);
4654                                         }
4655                                 }
4656                                 Mod_BuildNormals(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, rsurface.vertex3f, rsurface.modelelement3i + surface->num_firsttriangle * 3, rsurface.array_deformednormal3f, r_smoothnormals_areaweighting.integer);
4657                                 Mod_BuildTextureVectorsFromNormals(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, rsurface.vertex3f, rsurface.modeltexcoordtexture2f, rsurface.array_deformednormal3f, rsurface.modelelement3i + surface->num_firsttriangle * 3, rsurface.array_deformedsvector3f, rsurface.array_deformedtvector3f, r_smoothnormals_areaweighting.integer);
4658                         }
4659                         rsurface.vertex3f = rsurface.array_deformedvertex3f;
4660                         rsurface.vertex3f_bufferobject = 0;
4661                         rsurface.vertex3f_bufferoffset = 0;
4662                         rsurface.svector3f = rsurface.array_deformedsvector3f;
4663                         rsurface.svector3f_bufferobject = 0;
4664                         rsurface.svector3f_bufferoffset = 0;
4665                         rsurface.tvector3f = rsurface.array_deformedtvector3f;
4666                         rsurface.tvector3f_bufferobject = 0;
4667                         rsurface.tvector3f_bufferoffset = 0;
4668                         rsurface.normal3f = rsurface.array_deformednormal3f;
4669                         rsurface.normal3f_bufferobject = 0;
4670                         rsurface.normal3f_bufferoffset = 0;
4671                         break;
4672                 case Q3DEFORM_AUTOSPRITE2:
4673                         Matrix4x4_Transform3x3(&rsurface.inversematrix, r_view.forward, newforward);
4674                         Matrix4x4_Transform3x3(&rsurface.inversematrix, r_view.right, newright);
4675                         Matrix4x4_Transform3x3(&rsurface.inversematrix, r_view.up, newup);
4676                         VectorNormalize(newforward);
4677                         VectorNormalize(newright);
4678                         VectorNormalize(newup);
4679                         // make deformed versions of only the model vertices used by the specified surfaces
4680                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4681                         {
4682                                 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4683                                 const float *v1, *v2;
4684                                 vec3_t start, end;
4685                                 float f, l;
4686                                 struct
4687                                 {
4688                                         float length2;
4689                                         const float *v1;
4690                                         const float *v2;
4691                                 }
4692                                 shortest[2];
4693                                 memset(shortest, 0, sizeof(shortest));
4694                                 // a single autosprite surface can contain multiple sprites...
4695                                 for (j = 0;j < surface->num_vertices - 3;j += 4)
4696                                 {
4697                                         VectorClear(center);
4698                                         for (i = 0;i < 4;i++)
4699                                                 VectorAdd(center, (rsurface.vertex3f + 3 * surface->num_firstvertex) + (j+i) * 3, center);
4700                                         VectorScale(center, 0.25f, center);
4701                                         // find the two shortest edges, then use them to define the
4702                                         // axis vectors for rotating around the central axis
4703                                         for (i = 0;i < 6;i++)
4704                                         {
4705                                                 v1 = rsurface.vertex3f + 3 * (surface->num_firstvertex + quadedges[i][0]);
4706                                                 v2 = rsurface.vertex3f + 3 * (surface->num_firstvertex + quadedges[i][1]);
4707 #if 0
4708                                                 Debug_PolygonBegin(NULL, 0, false, 0);
4709                                                 Debug_PolygonVertex(v1[0], v1[1], v1[2], 0, 0, 1, 0, 0, 1);
4710                                                 Debug_PolygonVertex((v1[0] + v2[0]) * 0.5f + rsurface.normal3f[3 * (surface->num_firstvertex + j)+0] * 4, (v1[1] + v2[1]) * 0.5f + rsurface.normal3f[3 * (surface->num_firstvertex + j)+1], (v1[2] + v2[2]) * 0.5f + rsurface.normal3f[3 * (surface->num_firstvertex + j)+2], 0, 0, 1, 1, 0, 1);
4711                                                 Debug_PolygonVertex(v2[0], v2[1], v2[2], 0, 0, 1, 0, 0, 1);
4712                                                 Debug_PolygonEnd();
4713 #endif
4714                                                 l = VectorDistance2(v1, v2);
4715                                                 // this length bias tries to make sense of square polygons, assuming they are meant to be upright
4716                                                 if (v1[2] != v2[2])
4717                                                         l += (1.0f / 1024.0f);
4718                                                 if (shortest[0].length2 > l || i == 0)
4719                                                 {
4720                                                         shortest[1] = shortest[0];
4721                                                         shortest[0].length2 = l;
4722                                                         shortest[0].v1 = v1;
4723                                                         shortest[0].v2 = v2;
4724                                                 }
4725                                                 else if (shortest[1].length2 > l || i == 1)
4726                                                 {
4727                                                         shortest[1].length2 = l;
4728                                                         shortest[1].v1 = v1;
4729                                                         shortest[1].v2 = v2;
4730                                                 }
4731                                         }
4732                                         VectorLerp(shortest[0].v1, 0.5f, shortest[0].v2, start);
4733                                         VectorLerp(shortest[1].v1, 0.5f, shortest[1].v2, end);
4734 #if 0
4735                                         Debug_PolygonBegin(NULL, 0, false, 0);
4736                                         Debug_PolygonVertex(start[0], start[1], start[2], 0, 0, 1, 1, 0, 1);
4737                                         Debug_PolygonVertex(center[0] + rsurface.normal3f[3 * (surface->num_firstvertex + j)+0] * 4, center[1] + rsurface.normal3f[3 * (surface->num_firstvertex + j)+1] * 4, center[2] + rsurface.normal3f[3 * (surface->num_firstvertex + j)+2] * 4, 0, 0, 0, 1, 0, 1);
4738                                         Debug_PolygonVertex(end[0], end[1], end[2], 0, 0, 0, 1, 1, 1);
4739                                         Debug_PolygonEnd();
4740 #endif
4741                                         // this calculates the right vector from the shortest edge
4742                                         // and the up vector from the edge midpoints
4743                                         VectorSubtract(shortest[0].v1, shortest[0].v2, right);
4744                                         VectorNormalize(right);
4745                                         VectorSubtract(end, start, up);
4746                                         VectorNormalize(up);
4747                                         // calculate a forward vector to use instead of the original plane normal (this is how we get a new right vector)
4748                                         //VectorSubtract(rsurface.modelorg, center, forward);
4749                                         Matrix4x4_Transform3x3(&rsurface.inversematrix, r_view.forward, forward);
4750                                         VectorNegate(forward, forward);
4751                                         VectorReflect(forward, 0, up, forward);
4752                                         VectorNormalize(forward);
4753                                         CrossProduct(up, forward, newright);
4754                                         VectorNormalize(newright);
4755 #if 0
4756                                         Debug_PolygonBegin(NULL, 0, false, 0);
4757                                         Debug_PolygonVertex(center[0] + rsurface.normal3f[3 * (surface->num_firstvertex + j)+0] * 8, center[1] + rsurface.normal3f[3 * (surface->num_firstvertex + j)+1] * 8, center[2] + rsurface.normal3f[3 * (surface->num_firstvertex + j)+2] * 8, 0, 0, 1, 0, 0, 1);
4758                                         Debug_PolygonVertex(center[0] + right[0] * 8, center[1] + right[1] * 8, center[2] + right[2] * 8, 0, 0, 0, 1, 0, 1);
4759                                         Debug_PolygonVertex(center[0] + up   [0] * 8, center[1] + up   [1] * 8, center[2] + up   [2] * 8, 0, 0, 0, 0, 1, 1);
4760                                         Debug_PolygonEnd();
4761 #endif
4762 #if 0
4763                                         Debug_PolygonBegin(NULL, 0, false, 0);
4764                                         Debug_PolygonVertex(center[0] + forward [0] * 8, center[1] + forward [1] * 8, center[2] + forward [2] * 8, 0, 0, 1, 0, 0, 1);
4765                                         Debug_PolygonVertex(center[0] + newright[0] * 8, center[1] + newright[1] * 8, center[2] + newright[2] * 8, 0, 0, 0, 1, 0, 1);
4766                                         Debug_PolygonVertex(center[0] + up      [0] * 8, center[1] + up      [1] * 8, center[2] + up      [2] * 8, 0, 0, 0, 0, 1, 1);
4767                                         Debug_PolygonEnd();
4768 #endif
4769                                         // rotate the quad around the up axis vector, this is made
4770                                         // especially easy by the fact we know the quad is flat,
4771                                         // so we only have to subtract the center position and
4772                                         // measure distance along the right vector, and then
4773                                         // multiply that by the newright vector and add back the
4774                                         // center position
4775                                         // we also need to subtract the old position to undo the
4776                                         // displacement from the center, which we do with a
4777                                         // DotProduct, the subtraction/addition of center is also
4778                                         // optimized into DotProducts here
4779                                         l = DotProduct(right, center);
4780                                         for (i = 0;i < 4;i++)
4781                                         {
4782                                                 v1 = rsurface.vertex3f + 3 * (surface->num_firstvertex + j + i);
4783                                                 f = DotProduct(right, v1) - l;
4784                                                 VectorMAMAM(1, v1, -f, right, f, newright, rsurface.array_deformedvertex3f + (surface->num_firstvertex+i+j) * 3);
4785                                         }
4786                                 }
4787                                 Mod_BuildNormals(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, rsurface.vertex3f, rsurface.modelelement3i + surface->num_firsttriangle * 3, rsurface.array_deformednormal3f, r_smoothnormals_areaweighting.integer);
4788                                 Mod_BuildTextureVectorsFromNormals(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, rsurface.vertex3f, rsurface.modeltexcoordtexture2f, rsurface.array_deformednormal3f, rsurface.modelelement3i + surface->num_firsttriangle * 3, rsurface.array_deformedsvector3f, rsurface.array_deformedtvector3f, r_smoothnormals_areaweighting.integer);
4789                         }
4790                         rsurface.vertex3f = rsurface.array_deformedvertex3f;
4791                         rsurface.vertex3f_bufferobject = 0;
4792                         rsurface.vertex3f_bufferoffset = 0;
4793                         rsurface.svector3f = rsurface.array_deformedsvector3f;
4794                         rsurface.svector3f_bufferobject = 0;
4795                         rsurface.svector3f_bufferoffset = 0;
4796                         rsurface.tvector3f = rsurface.array_deformedtvector3f;
4797                         rsurface.tvector3f_bufferobject = 0;
4798                         rsurface.tvector3f_bufferoffset = 0;
4799                         rsurface.normal3f = rsurface.array_deformednormal3f;
4800                         rsurface.normal3f_bufferobject = 0;
4801                         rsurface.normal3f_bufferoffset = 0;
4802                         break;
4803                 case Q3DEFORM_NORMAL:
4804                         // deform the normals to make reflections wavey
4805                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4806                         {
4807                                 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4808                                 for (j = 0;j < surface->num_vertices;j++)
4809                                 {
4810                                         float vertex[3];
4811                                         float *normal = (rsurface.array_deformednormal3f  + 3 * surface->num_firstvertex) + j*3;
4812                                         VectorScale((rsurface.vertex3f  + 3 * surface->num_firstvertex) + j*3, 0.98f, vertex);
4813                                         VectorCopy((rsurface.normal3f  + 3 * surface->num_firstvertex) + j*3, normal);
4814                                         normal[0] += deform->parms[0] * noise4f(      vertex[0], vertex[1], vertex[2], r_refdef.time * deform->parms[1]);
4815                                         normal[1] += deform->parms[0] * noise4f( 98 + vertex[0], vertex[1], vertex[2], r_refdef.time * deform->parms[1]);
4816                                         normal[2] += deform->parms[0] * noise4f(196 + vertex[0], vertex[1], vertex[2], r_refdef.time * deform->parms[1]);
4817                                         VectorNormalize(normal);
4818                                 }
4819                                 Mod_BuildTextureVectorsFromNormals(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, rsurface.vertex3f, rsurface.modeltexcoordtexture2f, rsurface.array_deformednormal3f, rsurface.modelelement3i + surface->num_firsttriangle * 3, rsurface.array_deformedsvector3f, rsurface.array_deformedtvector3f, r_smoothnormals_areaweighting.integer);
4820                         }
4821                         rsurface.svector3f = rsurface.array_deformedsvector3f;
4822                         rsurface.svector3f_bufferobject = 0;
4823                         rsurface.svector3f_bufferoffset = 0;
4824                         rsurface.tvector3f = rsurface.array_deformedtvector3f;
4825                         rsurface.tvector3f_bufferobject = 0;
4826                         rsurface.tvector3f_bufferoffset = 0;
4827                         rsurface.normal3f = rsurface.array_deformednormal3f;
4828                         rsurface.normal3f_bufferobject = 0;
4829                         rsurface.normal3f_bufferoffset = 0;
4830                         break;
4831                 case Q3DEFORM_WAVE:
4832                         // deform vertex array to make wavey water and flags and such
4833                         waveparms[0] = deform->waveparms[0];
4834                         waveparms[1] = deform->waveparms[1];
4835                         waveparms[2] = deform->waveparms[2];
4836                         waveparms[3] = deform->waveparms[3];
4837                         // this is how a divisor of vertex influence on deformation
4838                         animpos = deform->parms[0] ? 1.0f / deform->parms[0] : 100.0f;
4839                         scale = R_EvaluateQ3WaveFunc(deform->wavefunc, waveparms);
4840                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4841                         {
4842                                 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4843                                 for (j = 0;j < surface->num_vertices;j++)
4844                                 {
4845                                         float *vertex = (rsurface.array_deformedvertex3f  + 3 * surface->num_firstvertex) + j*3;
4846                                         VectorCopy((rsurface.vertex3f  + 3 * surface->num_firstvertex) + j*3, vertex);
4847                                         // if the wavefunc depends on time, evaluate it per-vertex
4848                                         if (waveparms[3])
4849                                         {
4850                                                 waveparms[2] = deform->waveparms[2] + (vertex[0] + vertex[1] + vertex[2]) * animpos;
4851                                                 scale = R_EvaluateQ3WaveFunc(deform->wavefunc, waveparms);
4852                                         }
4853                                         VectorMA(vertex, scale, (rsurface.normal3f  + 3 * surface->num_firstvertex) + j*3, vertex);
4854                                 }
4855                         }
4856                         rsurface.vertex3f = rsurface.array_deformedvertex3f;
4857                         rsurface.vertex3f_bufferobject = 0;
4858                         rsurface.vertex3f_bufferoffset = 0;
4859                         break;
4860                 case Q3DEFORM_BULGE:
4861                         // deform vertex array to make the surface have moving bulges
4862                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4863                         {
4864                                 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4865                                 for (j = 0;j < surface->num_vertices;j++)
4866                                 {
4867                                         scale = sin((rsurface.modeltexcoordtexture2f[2 * (surface->num_firstvertex + j)] * deform->parms[0] + r_refdef.time * deform->parms[2])) * deform->parms[1];
4868                                         VectorMA(rsurface.vertex3f + 3 * (surface->num_firstvertex + j), scale, rsurface.normal3f + 3 * (surface->num_firstvertex + j), rsurface.array_deformedvertex3f + 3 * (surface->num_firstvertex + j));
4869                                 }
4870                         }
4871                         rsurface.vertex3f = rsurface.array_deformedvertex3f;
4872                         rsurface.vertex3f_bufferobject = 0;
4873                         rsurface.vertex3f_bufferoffset = 0;
4874                         break;
4875                 case Q3DEFORM_MOVE:
4876                         // deform vertex array
4877                         scale = R_EvaluateQ3WaveFunc(deform->wavefunc, deform->waveparms);
4878                         VectorScale(deform->parms, scale, waveparms);
4879                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4880                         {
4881                                 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4882                                 for (j = 0;j < surface->num_vertices;j++)
4883                                         VectorAdd(rsurface.vertex3f + 3 * (surface->num_firstvertex + j), waveparms, rsurface.array_deformedvertex3f + 3 * (surface->num_firstvertex + j));
4884                         }
4885                         rsurface.vertex3f = rsurface.array_deformedvertex3f;
4886                         rsurface.vertex3f_bufferobject = 0;
4887                         rsurface.vertex3f_bufferoffset = 0;
4888                         break;
4889                 }
4890         }
4891         // generate texcoords based on the chosen texcoord source
4892         switch(rsurface.texture->tcgen.tcgen)
4893         {
4894         default:
4895         case Q3TCGEN_TEXTURE:
4896                 rsurface.texcoordtexture2f               = rsurface.modeltexcoordtexture2f;
4897                 rsurface.texcoordtexture2f_bufferobject  = rsurface.modeltexcoordtexture2f_bufferobject;
4898                 rsurface.texcoordtexture2f_bufferoffset  = rsurface.modeltexcoordtexture2f_bufferoffset;
4899                 break;
4900         case Q3TCGEN_LIGHTMAP:
4901                 rsurface.texcoordtexture2f               = rsurface.modeltexcoordlightmap2f;
4902                 rsurface.texcoordtexture2f_bufferobject  = rsurface.modeltexcoordlightmap2f_bufferobject;
4903                 rsurface.texcoordtexture2f_bufferoffset  = rsurface.modeltexcoordlightmap2f_bufferoffset;
4904                 break;
4905         case Q3TCGEN_VECTOR:
4906                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4907                 {
4908                         const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4909                         for (j = 0, v1 = rsurface.modelvertex3f + 3 * surface->num_firstvertex, out_tc = rsurface.array_generatedtexcoordtexture2f + 2 * surface->num_firstvertex;j < surface->num_vertices;j++, v1 += 3, out_tc += 2)
4910                         {
4911                                 out_tc[0] = DotProduct(v1, rsurface.texture->tcgen.parms);
4912                                 out_tc[1] = DotProduct(v1, rsurface.texture->tcgen.parms + 3);
4913                         }
4914                 }
4915                 rsurface.texcoordtexture2f               = rsurface.array_generatedtexcoordtexture2f;
4916                 rsurface.texcoordtexture2f_bufferobject  = 0;
4917                 rsurface.texcoordtexture2f_bufferoffset  = 0;
4918                 break;
4919         case Q3TCGEN_ENVIRONMENT:
4920                 // make environment reflections using a spheremap
4921                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4922                 {
4923                         const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4924                         const float *vertex = rsurface.modelvertex3f + 3 * surface->num_firstvertex;
4925                         const float *normal = rsurface.modelnormal3f + 3 * surface->num_firstvertex;
4926                         float *out_tc = rsurface.array_generatedtexcoordtexture2f + 2 * surface->num_firstvertex;
4927                         for (j = 0;j < surface->num_vertices;j++, vertex += 3, normal += 3, out_tc += 2)
4928                         {
4929                                 float l, d, eyedir[3];
4930                                 VectorSubtract(rsurface.modelorg, vertex, eyedir);
4931                                 l = 0.5f / VectorLength(eyedir);
4932                                 d = DotProduct(normal, eyedir)*2;
4933                                 out_tc[0] = 0.5f + (normal[1]*d - eyedir[1])*l;
4934                                 out_tc[1] = 0.5f - (normal[2]*d - eyedir[2])*l;
4935                         }
4936                 }
4937                 rsurface.texcoordtexture2f               = rsurface.array_generatedtexcoordtexture2f;
4938                 rsurface.texcoordtexture2f_bufferobject  = 0;
4939                 rsurface.texcoordtexture2f_bufferoffset  = 0;
4940                 break;
4941         }
4942         // the only tcmod that needs software vertex processing is turbulent, so
4943         // check for it here and apply the changes if needed
4944         // and we only support that as the first one
4945         // (handling a mixture of turbulent and other tcmods would be problematic
4946         //  without punting it entirely to a software path)
4947         if (rsurface.texture->tcmods[0].tcmod == Q3TCMOD_TURBULENT)
4948         {
4949                 amplitude = rsurface.texture->tcmods[0].parms[1];
4950                 animpos = rsurface.texture->tcmods[0].parms[2] + r_refdef.time * rsurface.texture->tcmods[0].parms[3];
4951                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
4952                 {
4953                         const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
4954                         for (j = 0, v1 = rsurface.modelvertex3f + 3 * surface->num_firstvertex, in_tc = rsurface.texcoordtexture2f + 2 * surface->num_firstvertex, out_tc = rsurface.array_generatedtexcoordtexture2f + 2 * surface->num_firstvertex;j < surface->num_vertices;j++, v1 += 3, in_tc += 2, out_tc += 2)
4955                         {
4956                                 out_tc[0] = in_tc[0] + amplitude * sin(((v1[0] + v1[2]) * 1.0 / 1024.0f + animpos) * M_PI * 2);
4957                                 out_tc[1] = in_tc[1] + amplitude * sin(((v1[1]        ) * 1.0 / 1024.0f + animpos) * M_PI * 2);
4958                         }
4959                 }
4960                 rsurface.texcoordtexture2f               = rsurface.array_generatedtexcoordtexture2f;
4961                 rsurface.texcoordtexture2f_bufferobject  = 0;
4962                 rsurface.texcoordtexture2f_bufferoffset  = 0;
4963         }
4964         rsurface.texcoordlightmap2f              = rsurface.modeltexcoordlightmap2f;
4965         rsurface.texcoordlightmap2f_bufferobject = rsurface.modeltexcoordlightmap2f_bufferobject;
4966         rsurface.texcoordlightmap2f_bufferoffset = rsurface.modeltexcoordlightmap2f_bufferoffset;
4967         R_Mesh_VertexPointer(rsurface.vertex3f, rsurface.vertex3f_bufferobject, rsurface.vertex3f_bufferoffset);
4968 }
4969
4970 void RSurf_DrawBatch_Simple(int texturenumsurfaces, msurface_t **texturesurfacelist)
4971 {
4972         int i, j;
4973         const msurface_t *surface = texturesurfacelist[0];
4974         const msurface_t *surface2;
4975         int firstvertex;
4976         int endvertex;
4977         int numvertices;
4978         int numtriangles;
4979         // TODO: lock all array ranges before render, rather than on each surface
4980         if (texturenumsurfaces == 1)
4981         {
4982                 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
4983                 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (rsurface.modelelement3i + 3 * surface->num_firsttriangle), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * surface->num_firsttriangle));
4984         }
4985         else if (r_batchmode.integer == 2)
4986         {
4987                 #define MAXBATCHTRIANGLES 4096
4988                 int batchtriangles = 0;
4989                 int batchelements[MAXBATCHTRIANGLES*3];
4990                 for (i = 0;i < texturenumsurfaces;i = j)
4991                 {
4992                         surface = texturesurfacelist[i];
4993                         j = i + 1;
4994                         if (surface->num_triangles > MAXBATCHTRIANGLES)
4995                         {
4996                                 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (rsurface.modelelement3i + 3 * surface->num_firsttriangle), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * surface->num_firsttriangle));
4997                                 continue;
4998                         }
4999                         memcpy(batchelements, rsurface.modelelement3i + 3 * surface->num_firsttriangle, surface->num_triangles * sizeof(int[3]));
5000                         batchtriangles = surface->num_triangles;
5001                         firstvertex = surface->num_firstvertex;
5002                         endvertex = surface->num_firstvertex + surface->num_vertices;
5003                         for (;j < texturenumsurfaces;j++)
5004                         {
5005                                 surface2 = texturesurfacelist[j];
5006                                 if (batchtriangles + surface2->num_triangles > MAXBATCHTRIANGLES)
5007                                         break;
5008                                 memcpy(batchelements + batchtriangles * 3, rsurface.modelelement3i + 3 * surface2->num_firsttriangle, surface2->num_triangles * sizeof(int[3]));
5009                                 batchtriangles += surface2->num_triangles;
5010                                 firstvertex = min(firstvertex, surface2->num_firstvertex);
5011                                 endvertex = max(endvertex, surface2->num_firstvertex + surface2->num_vertices);
5012                         }
5013                         surface2 = texturesurfacelist[j-1];
5014                         numvertices = endvertex - firstvertex;
5015                         R_Mesh_Draw(firstvertex, numvertices, batchtriangles, batchelements, 0, 0);
5016                 }
5017         }
5018         else if (r_batchmode.integer == 1)
5019         {
5020                 for (i = 0;i < texturenumsurfaces;i = j)
5021                 {
5022                         surface = texturesurfacelist[i];
5023                         for (j = i + 1, surface2 = surface + 1;j < texturenumsurfaces;j++, surface2++)
5024                                 if (texturesurfacelist[j] != surface2)
5025                                         break;
5026                         surface2 = texturesurfacelist[j-1];
5027                         numvertices = surface2->num_firstvertex + surface2->num_vertices - surface->num_firstvertex;
5028                         numtriangles = surface2->num_firsttriangle + surface2->num_triangles - surface->num_firsttriangle;
5029                         GL_LockArrays(surface->num_firstvertex, numvertices);
5030                         R_Mesh_Draw(surface->num_firstvertex, numvertices, numtriangles, (rsurface.modelelement3i + 3 * surface->num_firsttriangle), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * surface->num_firsttriangle));
5031                 }
5032         }
5033         else
5034         {
5035                 for (i = 0;i < texturenumsurfaces;i++)
5036                 {
5037                         surface = texturesurfacelist[i];
5038                         GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
5039                         R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (rsurface.modelelement3i + 3 * surface->num_firsttriangle), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * surface->num_firsttriangle));
5040                 }
5041         }
5042 }
5043
5044 static void RSurf_DrawBatch_WithLightmapSwitching_WithWaterTextureSwitching(int texturenumsurfaces, msurface_t **texturesurfacelist, int lightmaptexunit, int deluxemaptexunit, int refractiontexunit, int reflectiontexunit)
5045 {
5046         int i, planeindex, vertexindex;
5047         float d, bestd;
5048         vec3_t vert;
5049         const float *v;
5050         r_waterstate_waterplane_t *p, *bestp;
5051         msurface_t *surface;
5052         if (r_waterstate.renderingscene)
5053                 return;
5054         for (i = 0;i < texturenumsurfaces;i++)
5055         {
5056                 surface = texturesurfacelist[i];
5057                 if (lightmaptexunit >= 0)
5058                         R_Mesh_TexBind(lightmaptexunit, R_GetTexture(surface->lightmaptexture));
5059                 if (deluxemaptexunit >= 0)
5060                         R_Mesh_TexBind(deluxemaptexunit, R_GetTexture(surface->deluxemaptexture));
5061                 // pick the closest matching water plane
5062                 bestd = 0;
5063                 bestp = NULL;
5064                 for (planeindex = 0, p = r_waterstate.waterplanes;planeindex < r_waterstate.numwaterplanes;planeindex++, p++)
5065                 {
5066                         d = 0;
5067                         for (vertexindex = 0, v = rsurface.modelvertex3f + surface->num_firstvertex * 3;vertexindex < surface->num_vertices;vertexindex++, v += 3)
5068                         {
5069                                 Matrix4x4_Transform(&rsurface.matrix, v, vert);
5070                                 d += fabs(PlaneDiff(vert, &p->plane));
5071                         }
5072                         if (bestd > d || !bestp)
5073                         {
5074                                 bestd = d;
5075                                 bestp = p;
5076                         }
5077                 }
5078                 if (bestp)
5079                 {
5080                         if (refractiontexunit >= 0)
5081                                 R_Mesh_TexBind(refractiontexunit, R_GetTexture(bestp->texture_refraction));
5082                         if (reflectiontexunit >= 0)
5083                                 R_Mesh_TexBind(reflectiontexunit, R_GetTexture(bestp->texture_reflection));
5084                 }
5085                 else
5086                 {
5087                         if (refractiontexunit >= 0)
5088                                 R_Mesh_TexBind(refractiontexunit, R_GetTexture(r_texture_black));
5089                         if (reflectiontexunit >= 0)
5090                                 R_Mesh_TexBind(reflectiontexunit, R_GetTexture(r_texture_black));
5091                 }
5092                 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
5093                 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (rsurface.modelelement3i + 3 * surface->num_firsttriangle), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * surface->num_firsttriangle));
5094         }
5095 }
5096
5097 static void RSurf_DrawBatch_WithLightmapSwitching(int texturenumsurfaces, msurface_t **texturesurfacelist, int lightmaptexunit, int deluxemaptexunit)
5098 {
5099         int i;
5100         int j;
5101         const msurface_t *surface = texturesurfacelist[0];
5102         const msurface_t *surface2;
5103         int firstvertex;
5104         int endvertex;
5105         int numvertices;
5106         int numtriangles;
5107         // TODO: lock all array ranges before render, rather than on each surface
5108         if (texturenumsurfaces == 1)
5109         {
5110                 R_Mesh_TexBind(lightmaptexunit, R_GetTexture(surface->lightmaptexture));
5111                 if (deluxemaptexunit >= 0)
5112                         R_Mesh_TexBind(deluxemaptexunit, R_GetTexture(surface->deluxemaptexture));
5113                 GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
5114                 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (rsurface.modelelement3i + 3 * surface->num_firsttriangle), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * surface->num_firsttriangle));
5115         }
5116         else if (r_batchmode.integer == 2)
5117         {
5118                 #define MAXBATCHTRIANGLES 4096
5119                 int batchtriangles = 0;
5120                 int batchelements[MAXBATCHTRIANGLES*3];
5121                 for (i = 0;i < texturenumsurfaces;i = j)
5122                 {
5123                         surface = texturesurfacelist[i];
5124                         R_Mesh_TexBind(lightmaptexunit, R_GetTexture(surface->lightmaptexture));
5125                         if (deluxemaptexunit >= 0)
5126                                 R_Mesh_TexBind(deluxemaptexunit, R_GetTexture(surface->deluxemaptexture));
5127                         j = i + 1;
5128                         if (surface->num_triangles > MAXBATCHTRIANGLES)
5129                         {
5130                                 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (rsurface.modelelement3i + 3 * surface->num_firsttriangle), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * surface->num_firsttriangle));
5131                                 continue;
5132                         }
5133                         memcpy(batchelements, rsurface.modelelement3i + 3 * surface->num_firsttriangle, surface->num_triangles * sizeof(int[3]));
5134                         batchtriangles = surface->num_triangles;
5135                         firstvertex = surface->num_firstvertex;
5136                         endvertex = surface->num_firstvertex + surface->num_vertices;
5137                         for (;j < texturenumsurfaces;j++)
5138                         {
5139                                 surface2 = texturesurfacelist[j];
5140                                 if (surface2->lightmaptexture != surface->lightmaptexture || batchtriangles + surface2->num_triangles > MAXBATCHTRIANGLES)
5141                                         break;
5142                                 memcpy(batchelements + batchtriangles * 3, rsurface.modelelement3i + 3 * surface2->num_firsttriangle, surface2->num_triangles * sizeof(int[3]));
5143                                 batchtriangles += surface2->num_triangles;
5144                                 firstvertex = min(firstvertex, surface2->num_firstvertex);
5145                                 endvertex = max(endvertex, surface2->num_firstvertex + surface2->num_vertices);
5146                         }
5147                         surface2 = texturesurfacelist[j-1];
5148                         numvertices = endvertex - firstvertex;
5149                         R_Mesh_Draw(firstvertex, numvertices, batchtriangles, batchelements, 0, 0);
5150                 }
5151         }
5152         else if (r_batchmode.integer == 1)
5153         {
5154 #if 0
5155                 Con_Printf("%s batch sizes ignoring lightmap:", rsurface.texture->name);
5156                 for (i = 0;i < texturenumsurfaces;i = j)
5157                 {
5158                         surface = texturesurfacelist[i];
5159                         for (j = i + 1, surface2 = surface + 1;j < texturenumsurfaces;j++, surface2++)
5160                                 if (texturesurfacelist[j] != surface2)
5161                                         break;
5162                         Con_Printf(" %i", j - i);
5163                 }
5164                 Con_Printf("\n");
5165                 Con_Printf("%s batch sizes honoring lightmap:", rsurface.texture->name);
5166 #endif
5167                 for (i = 0;i < texturenumsurfaces;i = j)
5168                 {
5169                         surface = texturesurfacelist[i];
5170                         R_Mesh_TexBind(lightmaptexunit, R_GetTexture(surface->lightmaptexture));
5171                         if (deluxemaptexunit >= 0)
5172                                 R_Mesh_TexBind(deluxemaptexunit, R_GetTexture(surface->deluxemaptexture));
5173                         for (j = i + 1, surface2 = surface + 1;j < texturenumsurfaces;j++, surface2++)
5174                                 if (texturesurfacelist[j] != surface2 || texturesurfacelist[j]->lightmaptexture != surface->lightmaptexture)
5175                                         break;
5176 #if 0
5177                         Con_Printf(" %i", j - i);
5178 #endif
5179                         surface2 = texturesurfacelist[j-1];
5180                         numvertices = surface2->num_firstvertex + surface2->num_vertices - surface->num_firstvertex;
5181                         numtriangles = surface2->num_firsttriangle + surface2->num_triangles - surface->num_firsttriangle;
5182                         GL_LockArrays(surface->num_firstvertex, numvertices);
5183                         R_Mesh_Draw(surface->num_firstvertex, numvertices, numtriangles, (rsurface.modelelement3i + 3 * surface->num_firsttriangle), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * surface->num_firsttriangle));
5184                 }
5185 #if 0
5186                 Con_Printf("\n");
5187 #endif
5188         }
5189         else
5190         {
5191                 for (i = 0;i < texturenumsurfaces;i++)
5192                 {
5193                         surface = texturesurfacelist[i];
5194                         R_Mesh_TexBind(lightmaptexunit, R_GetTexture(surface->lightmaptexture));
5195                         if (deluxemaptexunit >= 0)
5196                                 R_Mesh_TexBind(deluxemaptexunit, R_GetTexture(surface->deluxemaptexture));
5197                         GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
5198                         R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (rsurface.modelelement3i + 3 * surface->num_firsttriangle), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * surface->num_firsttriangle));
5199                 }
5200         }
5201 }
5202
5203 static void RSurf_DrawBatch_ShowSurfaces(int texturenumsurfaces, msurface_t **texturesurfacelist)
5204 {
5205         int j;
5206         int texturesurfaceindex;
5207         if (r_showsurfaces.integer == 2)
5208         {
5209                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5210                 {
5211                         const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5212                         for (j = 0;j < surface->num_triangles;j++)
5213                         {
5214                                 float f = ((j + surface->num_firsttriangle) & 31) * (1.0f / 31.0f) * r_view.colorscale;
5215                                 GL_Color(f, f, f, 1);
5216                                 R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, 1, (rsurface.modelelement3i + 3 * (j + surface->num_firsttriangle)), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * (j + surface->num_firsttriangle)));
5217                         }
5218                 }
5219         }
5220         else
5221         {
5222                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5223                 {
5224                         const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5225                         int k = (int)(((size_t)surface) / sizeof(msurface_t));
5226                         GL_Color((k & 15) * (1.0f / 16.0f) * r_view.colorscale, ((k >> 4) & 15) * (1.0f / 16.0f) * r_view.colorscale, ((k >> 8) & 15) * (1.0f / 16.0f) * r_view.colorscale, 1);
5227                         GL_LockArrays(surface->num_firstvertex, surface->num_vertices);
5228                         R_Mesh_Draw(surface->num_firstvertex, surface->num_vertices, surface->num_triangles, (rsurface.modelelement3i + 3 * surface->num_firsttriangle), rsurface.modelelement3i_bufferobject, (sizeof(int[3]) * surface->num_firsttriangle));
5229                 }
5230         }
5231 }
5232
5233 static void RSurf_DrawBatch_GL11_ApplyFog(int texturenumsurfaces, msurface_t **texturesurfacelist)
5234 {
5235         int texturesurfaceindex;
5236         int i;
5237         float f;
5238         float *v, *c, *c2;
5239         if (rsurface.lightmapcolor4f)
5240         {
5241                 // generate color arrays for the surfaces in this list
5242                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5243                 {
5244                         const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5245                         for (i = 0, v = (rsurface.vertex3f + 3 * surface->num_firstvertex), c = (rsurface.lightmapcolor4f + 4 * surface->num_firstvertex), c2 = (rsurface.array_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4, c2 += 4)
5246                         {
5247                                 f = FogPoint_Model(v);
5248                                 c2[0] = c[0] * f;
5249                                 c2[1] = c[1] * f;
5250                                 c2[2] = c[2] * f;
5251                                 c2[3] = c[3];
5252                         }
5253                 }
5254         }
5255         else
5256         {
5257                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5258                 {
5259                         const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5260                         for (i = 0, v = (rsurface.vertex3f + 3 * surface->num_firstvertex), c2 = (rsurface.array_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c2 += 4)
5261                         {
5262                                 f = FogPoint_Model(v);
5263                                 c2[0] = f;
5264                                 c2[1] = f;
5265                                 c2[2] = f;
5266                                 c2[3] = 1;
5267                         }
5268                 }
5269         }
5270         rsurface.lightmapcolor4f = rsurface.array_color4f;
5271         rsurface.lightmapcolor4f_bufferobject = 0;
5272         rsurface.lightmapcolor4f_bufferoffset = 0;
5273 }
5274
5275 static void RSurf_DrawBatch_GL11_ApplyColor(int texturenumsurfaces, msurface_t **texturesurfacelist, float r, float g, float b, float a)
5276 {
5277         int texturesurfaceindex;
5278         int i;
5279         float *c, *c2;
5280         if (!rsurface.lightmapcolor4f)
5281                 return;
5282         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5283         {
5284                 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5285                 for (i = 0, c = (rsurface.lightmapcolor4f + 4 * surface->num_firstvertex), c2 = (rsurface.array_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, c += 4, c2 += 4)
5286                 {
5287                         c2[0] = c[0] * r;
5288                         c2[1] = c[1] * g;
5289                         c2[2] = c[2] * b;
5290                         c2[3] = c[3] * a;
5291                 }
5292         }
5293         rsurface.lightmapcolor4f = rsurface.array_color4f;
5294         rsurface.lightmapcolor4f_bufferobject = 0;
5295         rsurface.lightmapcolor4f_bufferoffset = 0;
5296 }
5297
5298 static void RSurf_DrawBatch_GL11_Lightmap(int texturenumsurfaces, msurface_t **texturesurfacelist, float r, float g, float b, float a, qboolean applycolor, qboolean applyfog)
5299 {
5300         // TODO: optimize
5301         rsurface.lightmapcolor4f = NULL;
5302         rsurface.lightmapcolor4f_bufferobject = 0;
5303         rsurface.lightmapcolor4f_bufferoffset = 0;
5304         if (applyfog)   RSurf_DrawBatch_GL11_ApplyFog(texturenumsurfaces, texturesurfacelist);
5305         if (applycolor) RSurf_DrawBatch_GL11_ApplyColor(texturenumsurfaces, texturesurfacelist, r, g, b, a);
5306         R_Mesh_ColorPointer(rsurface.lightmapcolor4f, rsurface.lightmapcolor4f_bufferobject, rsurface.lightmapcolor4f_bufferoffset);
5307         GL_Color(r, g, b, a);
5308         RSurf_DrawBatch_WithLightmapSwitching(texturenumsurfaces, texturesurfacelist, 0, -1);
5309 }
5310
5311 static void RSurf_DrawBatch_GL11_Unlit(int texturenumsurfaces, msurface_t **texturesurfacelist, float r, float g, float b, float a, qboolean applycolor, qboolean applyfog)
5312 {
5313         // TODO: optimize applyfog && applycolor case
5314         // just apply fog if necessary, and tint the fog color array if necessary
5315         rsurface.lightmapcolor4f = NULL;
5316         rsurface.lightmapcolor4f_bufferobject = 0;
5317         rsurface.lightmapcolor4f_bufferoffset = 0;
5318         if (applyfog)   RSurf_DrawBatch_GL11_ApplyFog(texturenumsurfaces, texturesurfacelist);
5319         if (applycolor) RSurf_DrawBatch_GL11_ApplyColor(texturenumsurfaces, texturesurfacelist, r, g, b, a);
5320         R_Mesh_ColorPointer(rsurface.lightmapcolor4f, rsurface.lightmapcolor4f_bufferobject, rsurface.lightmapcolor4f_bufferoffset);
5321         GL_Color(r, g, b, a);
5322         RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5323 }
5324
5325 static void RSurf_DrawBatch_GL11_VertexColor(int texturenumsurfaces, msurface_t **texturesurfacelist, float r, float g, float b, float a, qboolean applycolor, qboolean applyfog)
5326 {
5327         int texturesurfaceindex;
5328         int i;
5329         float *c;
5330         // TODO: optimize
5331         if (texturesurfacelist[0]->lightmapinfo && texturesurfacelist[0]->lightmapinfo->stainsamples)
5332         {
5333                 // generate color arrays for the surfaces in this list
5334                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5335                 {
5336                         const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5337                         for (i = 0, c = rsurface.array_color4f + 4 * surface->num_firstvertex;i < surface->num_vertices;i++, c += 4)
5338                         {
5339                                 if (surface->lightmapinfo->samples)
5340                                 {
5341                                         const unsigned char *lm = surface->lightmapinfo->samples + (rsurface.modellightmapoffsets + surface->num_firstvertex)[i];
5342                                         float scale = r_refdef.lightstylevalue[surface->lightmapinfo->styles[0]] * (1.0f / 32768.0f);
5343                                         VectorScale(lm, scale, c);
5344                                         if (surface->lightmapinfo->styles[1] != 255)
5345                                         {
5346                                                 int size3 = ((surface->lightmapinfo->extents[0]>>4)+1)*((surface->lightmapinfo->extents[1]>>4)+1)*3;
5347                                                 lm += size3;
5348                                                 scale = r_refdef.lightstylevalue[surface->lightmapinfo->styles[1]] * (1.0f / 32768.0f);
5349                                                 VectorMA(c, scale, lm, c);
5350                                                 if (surface->lightmapinfo->styles[2] != 255)
5351                                                 {
5352                                                         lm += size3;
5353                                                         scale = r_refdef.lightstylevalue[surface->lightmapinfo->styles[2]] * (1.0f / 32768.0f);
5354                                                         VectorMA(c, scale, lm, c);
5355                                                         if (surface->lightmapinfo->styles[3] != 255)
5356                                                         {
5357                                                                 lm += size3;
5358                                                                 scale = r_refdef.lightstylevalue[surface->lightmapinfo->styles[3]] * (1.0f / 32768.0f);
5359                                                                 VectorMA(c, scale, lm, c);
5360                                                         }
5361                                                 }
5362                                         }
5363                                 }
5364                                 else
5365                                         VectorClear(c);
5366                                 c[3] = 1;
5367                         }
5368                 }
5369                 rsurface.lightmapcolor4f = rsurface.array_color4f;
5370                 rsurface.lightmapcolor4f_bufferobject = 0;
5371                 rsurface.lightmapcolor4f_bufferoffset = 0;
5372         }
5373         else
5374         {
5375                 rsurface.lightmapcolor4f = rsurface.modellightmapcolor4f;
5376                 rsurface.lightmapcolor4f_bufferobject = rsurface.modellightmapcolor4f_bufferobject;
5377                 rsurface.lightmapcolor4f_bufferoffset = rsurface.modellightmapcolor4f_bufferoffset;
5378         }
5379         if (applyfog)   RSurf_DrawBatch_GL11_ApplyFog(texturenumsurfaces, texturesurfacelist);
5380         if (applycolor) RSurf_DrawBatch_GL11_ApplyColor(texturenumsurfaces, texturesurfacelist, r, g, b, a);
5381         R_Mesh_ColorPointer(rsurface.lightmapcolor4f, rsurface.lightmapcolor4f_bufferobject, rsurface.lightmapcolor4f_bufferoffset);
5382         GL_Color(r, g, b, a);
5383         RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5384 }
5385
5386 static void RSurf_DrawBatch_GL11_VertexShade(int texturenumsurfaces, msurface_t **texturesurfacelist, float r, float g, float b, float a, qboolean applycolor, qboolean applyfog)
5387 {
5388         int texturesurfaceindex;
5389         int i;
5390         float f;
5391         float *v, *c, *c2;
5392         vec3_t ambientcolor;
5393         vec3_t diffusecolor;
5394         vec3_t lightdir;
5395         // TODO: optimize
5396         // model lighting
5397         VectorCopy(rsurface.modellight_lightdir, lightdir);
5398         f = 0.5f * r_refdef.lightmapintensity;
5399         ambientcolor[0] = rsurface.modellight_ambient[0] * r * f;
5400         ambientcolor[1] = rsurface.modellight_ambient[1] * g * f;
5401         ambientcolor[2] = rsurface.modellight_ambient[2] * b * f;
5402         diffusecolor[0] = rsurface.modellight_diffuse[0] * r * f;
5403         diffusecolor[1] = rsurface.modellight_diffuse[1] * g * f;
5404         diffusecolor[2] = rsurface.modellight_diffuse[2] * b * f;
5405         if (VectorLength2(diffusecolor) > 0)
5406         {
5407                 // generate color arrays for the surfaces in this list
5408                 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5409                 {
5410                         const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5411                         int numverts = surface->num_vertices;
5412                         v = rsurface.vertex3f + 3 * surface->num_firstvertex;
5413                         c2 = rsurface.normal3f + 3 * surface->num_firstvertex;
5414                         c = rsurface.array_color4f + 4 * surface->num_firstvertex;
5415                         // q3-style directional shading
5416                         for (i = 0;i < numverts;i++, v += 3, c2 += 3, c += 4)
5417                         {
5418                                 if ((f = DotProduct(c2, lightdir)) > 0)
5419                                         VectorMA(ambientcolor, f, diffusecolor, c);
5420                                 else
5421                                         VectorCopy(ambientcolor, c);
5422                                 c[3] = a;
5423                         }
5424                 }
5425                 r = 1;
5426                 g = 1;
5427                 b = 1;
5428                 a = 1;
5429                 applycolor = false;
5430                 rsurface.lightmapcolor4f = rsurface.array_color4f;
5431                 rsurface.lightmapcolor4f_bufferobject = 0;
5432                 rsurface.lightmapcolor4f_bufferoffset = 0;
5433         }
5434         else
5435         {
5436                 r = ambientcolor[0];
5437                 g = ambientcolor[1];
5438                 b = ambientcolor[2];
5439                 rsurface.lightmapcolor4f = NULL;
5440                 rsurface.lightmapcolor4f_bufferobject = 0;
5441                 rsurface.lightmapcolor4f_bufferoffset = 0;
5442         }
5443         if (applyfog)   RSurf_DrawBatch_GL11_ApplyFog(texturenumsurfaces, texturesurfacelist);
5444         if (applycolor) RSurf_DrawBatch_GL11_ApplyColor(texturenumsurfaces, texturesurfacelist, r, g, b, a);
5445         R_Mesh_ColorPointer(rsurface.lightmapcolor4f, rsurface.lightmapcolor4f_bufferobject, rsurface.lightmapcolor4f_bufferoffset);
5446         GL_Color(r, g, b, a);
5447         RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5448 }
5449
5450 void RSurf_SetupDepthAndCulling(void)
5451 {
5452         // submodels are biased to avoid z-fighting with world surfaces that they
5453         // may be exactly overlapping (avoids z-fighting artifacts on certain
5454         // doors and things in Quake maps)
5455         GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
5456         GL_PolygonOffset(rsurface.basepolygonfactor + rsurface.texture->biaspolygonfactor, rsurface.basepolygonoffset + rsurface.texture->biaspolygonoffset);
5457         GL_DepthTest(!(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST));
5458         GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_view.cullface_back);
5459 }
5460
5461 static void R_DrawTextureSurfaceList_ShowSurfaces(int texturenumsurfaces, msurface_t **texturesurfacelist)
5462 {
5463         RSurf_SetupDepthAndCulling();
5464         if (rsurface.mode != RSURFMODE_SHOWSURFACES)
5465         {
5466                 rsurface.mode = RSURFMODE_SHOWSURFACES;
5467                 GL_DepthMask(true);
5468                 GL_BlendFunc(GL_ONE, GL_ZERO);
5469                 R_Mesh_ColorPointer(NULL, 0, 0);
5470                 R_Mesh_ResetTextureState();
5471         }
5472         RSurf_PrepareVerticesForBatch(false, false, texturenumsurfaces, texturesurfacelist);
5473         RSurf_DrawBatch_ShowSurfaces(texturenumsurfaces, texturesurfacelist);
5474 }
5475
5476 static void R_DrawTextureSurfaceList_Sky(int texturenumsurfaces, msurface_t **texturesurfacelist)
5477 {
5478         // transparent sky would be ridiculous
5479         if ((rsurface.texture->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED))
5480                 return;
5481         if (rsurface.mode != RSURFMODE_SKY)
5482         {
5483                 if (rsurface.mode == RSURFMODE_GLSL)
5484                 {
5485                         qglUseProgramObjectARB(0);CHECKGLERROR
5486                 }
5487                 rsurface.mode = RSURFMODE_SKY;
5488         }
5489         if (skyrendernow)
5490         {
5491                 skyrendernow = false;
5492                 R_Sky();
5493                 // restore entity matrix
5494                 R_Mesh_Matrix(&rsurface.matrix);
5495         }
5496         RSurf_SetupDepthAndCulling();
5497         GL_DepthMask(true);
5498         // LordHavoc: HalfLife maps have freaky skypolys so don't use
5499         // skymasking on them, and Quake3 never did sky masking (unlike
5500         // software Quake and software Quake2), so disable the sky masking
5501         // in Quake3 maps as it causes problems with q3map2 sky tricks,
5502         // and skymasking also looks very bad when noclipping outside the
5503         // level, so don't use it then either.
5504         if (r_refdef.worldmodel && r_refdef.worldmodel->type == mod_brushq1 && r_q1bsp_skymasking.integer && !r_viewcache.world_novis)
5505         {
5506                 GL_Color(r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2], 1);
5507                 R_Mesh_ColorPointer(NULL, 0, 0);
5508                 R_Mesh_ResetTextureState();
5509                 if (skyrendermasked)
5510                 {
5511                         // depth-only (masking)
5512                         GL_ColorMask(0,0,0,0);
5513                         // just to make sure that braindead drivers don't draw
5514                         // anything despite that colormask...
5515                         GL_BlendFunc(GL_ZERO, GL_ONE);
5516                 }
5517                 else
5518                 {
5519                         // fog sky
5520                         GL_BlendFunc(GL_ONE, GL_ZERO);
5521                 }
5522                 RSurf_PrepareVerticesForBatch(false, false, texturenumsurfaces, texturesurfacelist);
5523                 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5524                 if (skyrendermasked)
5525                         GL_ColorMask(r_view.colormask[0], r_view.colormask[1], r_view.colormask[2], 1);
5526         }
5527 }
5528
5529 static void R_DrawTextureSurfaceList_GL20(int texturenumsurfaces, msurface_t **texturesurfacelist)
5530 {
5531         if (r_waterstate.renderingscene && (rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION)))
5532                 return;
5533
5534         if (rsurface.mode != RSURFMODE_GLSL)
5535         {
5536                 rsurface.mode = RSURFMODE_GLSL;
5537                 R_Mesh_ResetTextureState();
5538                 GL_Color(1, 1, 1, 1);
5539         }
5540
5541         R_Mesh_TexMatrix(0, &rsurface.texture->currenttexmatrix);
5542         R_Mesh_TexBind(0, R_GetTexture(rsurface.texture->currentskinframe->nmap));
5543         R_Mesh_TexBind(1, R_GetTexture(rsurface.texture->basetexture));
5544         R_Mesh_TexBind(2, R_GetTexture(rsurface.texture->glosstexture));
5545         R_Mesh_TexBind(4, R_GetTexture(r_texture_fogattenuation));
5546         R_Mesh_TexBind(5, R_GetTexture(rsurface.texture->currentskinframe->pants));
5547         R_Mesh_TexBind(6, R_GetTexture(rsurface.texture->currentskinframe->shirt));
5548         if (rsurface.texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)
5549         {
5550                 R_Mesh_TexBind(7, R_GetTexture(r_texture_grey128));
5551                 R_Mesh_TexBind(8, R_GetTexture(r_texture_blanknormalmap));
5552                 R_Mesh_ColorPointer(NULL, 0, 0);
5553         }
5554         else if (rsurface.uselightmaptexture)
5555         {
5556                 R_Mesh_TexBind(7, R_GetTexture(texturesurfacelist[0]->lightmaptexture));
5557                 R_Mesh_TexBind(8, R_GetTexture(texturesurfacelist[0]->deluxemaptexture));
5558                 R_Mesh_ColorPointer(NULL, 0, 0);
5559         }
5560         else
5561         {
5562                 R_Mesh_TexBind(7, R_GetTexture(r_texture_white));
5563                 R_Mesh_TexBind(8, R_GetTexture(r_texture_blanknormalmap));
5564                 R_Mesh_ColorPointer(rsurface.modellightmapcolor4f, rsurface.modellightmapcolor4f_bufferobject, rsurface.modellightmapcolor4f_bufferoffset);
5565         }
5566         R_Mesh_TexBind(9, R_GetTexture(rsurface.texture->currentskinframe->glow));
5567         R_Mesh_TexBind(11, R_GetTexture(r_texture_white)); // changed per surface
5568         R_Mesh_TexBind(12, R_GetTexture(r_texture_white)); // changed per surface
5569
5570         if (rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION))
5571         {
5572                 // render background
5573                 GL_BlendFunc(GL_ONE, GL_ZERO);
5574                 GL_DepthMask(true);
5575                 GL_AlphaTest(false);
5576
5577                 GL_Color(1, 1, 1, 1);
5578                 R_Mesh_ColorPointer(NULL, 0, 0);
5579
5580                 R_SetupSurfaceShader(vec3_origin, rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT, 1, 1, rsurface.texture->specularscale, RSURFPASS_BACKGROUND);
5581                 if (r_glsl_permutation)
5582                 {
5583                         RSurf_PrepareVerticesForBatch(true, true, texturenumsurfaces, texturesurfacelist);
5584                         R_Mesh_TexCoordPointer(0, 2, rsurface.texcoordtexture2f, rsurface.texcoordtexture2f_bufferobject, rsurface.texcoordtexture2f_bufferoffset);
5585                         R_Mesh_TexCoordPointer(1, 3, rsurface.svector3f, rsurface.svector3f_bufferobject, rsurface.svector3f_bufferoffset);
5586                         R_Mesh_TexCoordPointer(2, 3, rsurface.tvector3f, rsurface.tvector3f_bufferobject, rsurface.tvector3f_bufferoffset);
5587                         R_Mesh_TexCoordPointer(3, 3, rsurface.normal3f, rsurface.normal3f_bufferobject, rsurface.normal3f_bufferoffset);
5588                         R_Mesh_TexCoordPointer(4, 2, rsurface.modeltexcoordlightmap2f, rsurface.modeltexcoordlightmap2f_bufferobject, rsurface.modeltexcoordlightmap2f_bufferoffset);
5589                         RSurf_DrawBatch_WithLightmapSwitching_WithWaterTextureSwitching(texturenumsurfaces, texturesurfacelist, -1, -1, r_glsl_permutation->loc_Texture_Refraction ? 11 : -1, r_glsl_permutation->loc_Texture_Reflection ? 12 : -1);
5590                 }
5591
5592                 GL_BlendFunc(rsurface.texture->currentlayers[0].blendfunc1, rsurface.texture->currentlayers[0].blendfunc2);
5593                 GL_DepthMask(false);
5594                 GL_AlphaTest((rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST) != 0);
5595                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)
5596                 {
5597                         R_Mesh_TexBind(7, R_GetTexture(r_texture_grey128));
5598                         R_Mesh_TexBind(8, R_GetTexture(r_texture_blanknormalmap));
5599                         R_Mesh_ColorPointer(NULL, 0, 0);
5600                 }
5601                 else if (rsurface.uselightmaptexture)
5602                 {
5603                         R_Mesh_TexBind(7, R_GetTexture(texturesurfacelist[0]->lightmaptexture));
5604                         R_Mesh_TexBind(8, R_GetTexture(texturesurfacelist[0]->deluxemaptexture));
5605                         R_Mesh_ColorPointer(NULL, 0, 0);
5606                 }
5607                 else
5608                 {
5609                         R_Mesh_TexBind(7, R_GetTexture(r_texture_white));
5610                         R_Mesh_TexBind(8, R_GetTexture(r_texture_blanknormalmap));
5611                         R_Mesh_ColorPointer(rsurface.modellightmapcolor4f, rsurface.modellightmapcolor4f_bufferobject, rsurface.modellightmapcolor4f_bufferoffset);
5612                 }
5613                 R_Mesh_TexBind(11, R_GetTexture(r_texture_white)); // changed per surface
5614                 R_Mesh_TexBind(12, R_GetTexture(r_texture_white)); // changed per surface
5615         }
5616
5617         R_SetupSurfaceShader(vec3_origin, rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT, 1, 1, rsurface.texture->specularscale, RSURFPASS_BASE);
5618         if (!r_glsl_permutation)
5619                 return;
5620
5621         RSurf_PrepareVerticesForBatch(r_glsl_permutation->loc_Texture_Normal >= 0 || r_glsl_permutation->loc_LightDir >= 0, r_glsl_permutation->loc_Texture_Normal >= 0, texturenumsurfaces, texturesurfacelist);
5622         R_Mesh_TexCoordPointer(0, 2, rsurface.texcoordtexture2f, rsurface.texcoordtexture2f_bufferobject, rsurface.texcoordtexture2f_bufferoffset);
5623         R_Mesh_TexCoordPointer(1, 3, rsurface.svector3f, rsurface.svector3f_bufferobject, rsurface.svector3f_bufferoffset);
5624         R_Mesh_TexCoordPointer(2, 3, rsurface.tvector3f, rsurface.tvector3f_bufferobject, rsurface.tvector3f_bufferoffset);
5625         R_Mesh_TexCoordPointer(3, 3, rsurface.normal3f, rsurface.normal3f_bufferobject, rsurface.normal3f_bufferoffset);
5626         R_Mesh_TexCoordPointer(4, 2, rsurface.modeltexcoordlightmap2f, rsurface.modeltexcoordlightmap2f_bufferobject, rsurface.modeltexcoordlightmap2f_bufferoffset);
5627
5628         if (r_glsl_permutation->loc_Texture_Refraction >= 0)
5629         {
5630                 GL_BlendFunc(GL_ONE, GL_ZERO);
5631                 GL_DepthMask(true);
5632                 GL_AlphaTest(false);
5633         }
5634
5635         if (rsurface.uselightmaptexture && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT))
5636         {
5637                 if (r_glsl_permutation->loc_Texture_Refraction >= 0 || r_glsl_permutation->loc_Texture_Reflection >= 0)
5638                         RSurf_DrawBatch_WithLightmapSwitching_WithWaterTextureSwitching(texturenumsurfaces, texturesurfacelist, 7, r_glsl_permutation->loc_Texture_Deluxemap >= 0 ? 8 : -1, r_glsl_permutation->loc_Texture_Refraction >= 0 ? 11 : -1, r_glsl_permutation->loc_Texture_Reflection >= 0 ? 12 : -1);
5639                 else
5640                         RSurf_DrawBatch_WithLightmapSwitching(texturenumsurfaces, texturesurfacelist, 7, r_glsl_permutation->loc_Texture_Deluxemap >= 0 ? 8 : -1);
5641         }
5642         else
5643         {
5644                 if (r_glsl_permutation->loc_Texture_Refraction >= 0 || r_glsl_permutation->loc_Texture_Reflection >= 0)
5645                         RSurf_DrawBatch_WithLightmapSwitching_WithWaterTextureSwitching(texturenumsurfaces, texturesurfacelist, -1, -1, r_glsl_permutation->loc_Texture_Refraction >= 0 ? 11 : -1, r_glsl_permutation->loc_Texture_Reflection >= 0 ? 12 : -1);
5646                 else
5647                         RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5648         }
5649         if (rsurface.texture->backgroundnumskinframes && !(rsurface.texture->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED))
5650         {
5651         }
5652 }
5653
5654 static void R_DrawTextureSurfaceList_GL13(int texturenumsurfaces, msurface_t **texturesurfacelist)
5655 {
5656         // OpenGL 1.3 path - anything not completely ancient
5657         int texturesurfaceindex;
5658         qboolean applycolor;
5659         qboolean applyfog;
5660         rmeshstate_t m;
5661         int layerindex;
5662         const texturelayer_t *layer;
5663         if (rsurface.mode != RSURFMODE_MULTIPASS)
5664                 rsurface.mode = RSURFMODE_MULTIPASS;
5665         RSurf_PrepareVerticesForBatch(true, false, texturenumsurfaces, texturesurfacelist);
5666
5667         for (layerindex = 0, layer = rsurface.texture->currentlayers;layerindex < rsurface.texture->currentnumlayers;layerindex++, layer++)
5668         {
5669                 vec4_t layercolor;
5670                 int layertexrgbscale;
5671                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
5672                 {
5673                         if (layerindex == 0)
5674                                 GL_AlphaTest(true);
5675                         else
5676                         {
5677                                 GL_AlphaTest(false);
5678                                 qglDepthFunc(GL_EQUAL);CHECKGLERROR
5679                         }
5680                 }
5681                 GL_DepthMask(layer->depthmask);
5682                 GL_BlendFunc(layer->blendfunc1, layer->blendfunc2);
5683                 if ((layer->color[0] > 2 || layer->color[1] > 2 || layer->color[2] > 2) && (gl_combine.integer || layer->depthmask))
5684                 {
5685                         layertexrgbscale = 4;
5686                         VectorScale(layer->color, 0.25f, layercolor);
5687                 }
5688                 else if ((layer->color[0] > 1 || layer->color[1] > 1 || layer->color[2] > 1) && (gl_combine.integer || layer->depthmask))
5689                 {
5690                         layertexrgbscale = 2;
5691                         VectorScale(layer->color, 0.5f, layercolor);
5692                 }
5693                 else
5694                 {
5695                         layertexrgbscale = 1;
5696                         VectorScale(layer->color, 1.0f, layercolor);
5697                 }
5698                 layercolor[3] = layer->color[3];
5699                 applycolor = layercolor[0] != 1 || layercolor[1] != 1 || layercolor[2] != 1 || layercolor[3] != 1;
5700                 R_Mesh_ColorPointer(NULL, 0, 0);
5701                 applyfog = (layer->flags & TEXTURELAYERFLAG_FOGDARKEN) != 0;
5702                 switch (layer->type)
5703                 {
5704                 case TEXTURELAYERTYPE_LITTEXTURE:
5705                         memset(&m, 0, sizeof(m));
5706                         m.tex[0] = R_GetTexture(r_texture_white);
5707                         m.pointer_texcoord[0] = rsurface.modeltexcoordlightmap2f;
5708                         m.pointer_texcoord_bufferobject[0] = rsurface.modeltexcoordlightmap2f_bufferobject;
5709                         m.pointer_texcoord_bufferoffset[0] = rsurface.modeltexcoordlightmap2f_bufferoffset;
5710                         m.tex[1] = R_GetTexture(layer->texture);
5711                         m.texmatrix[1] = layer->texmatrix;
5712                         m.texrgbscale[1] = layertexrgbscale;
5713                         m.pointer_texcoord[1] = rsurface.texcoordtexture2f;
5714                         m.pointer_texcoord_bufferobject[1] = rsurface.texcoordtexture2f_bufferobject;
5715                         m.pointer_texcoord_bufferoffset[1] = rsurface.texcoordtexture2f_bufferoffset;
5716                         R_Mesh_TextureState(&m);
5717                         if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
5718                                 RSurf_DrawBatch_GL11_VertexShade(texturenumsurfaces, texturesurfacelist, layercolor[0], layercolor[1], layercolor[2], layercolor[3], applycolor, applyfog);
5719                         else if (rsurface.uselightmaptexture)
5720                                 RSurf_DrawBatch_GL11_Lightmap(texturenumsurfaces, texturesurfacelist, layercolor[0], layercolor[1], layercolor[2], layercolor[3], applycolor, applyfog);
5721                         else
5722                                 RSurf_DrawBatch_GL11_VertexColor(texturenumsurfaces, texturesurfacelist, layercolor[0], layercolor[1], layercolor[2], layercolor[3], applycolor, applyfog);
5723                         break;
5724                 case TEXTURELAYERTYPE_TEXTURE:
5725                         memset(&m, 0, sizeof(m));
5726                         m.tex[0] = R_GetTexture(layer->texture);
5727                         m.texmatrix[0] = layer->texmatrix;
5728                         m.texrgbscale[0] = layertexrgbscale;
5729                         m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
5730                         m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
5731                         m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
5732                         R_Mesh_TextureState(&m);
5733                         RSurf_DrawBatch_GL11_Unlit(texturenumsurfaces, texturesurfacelist, layercolor[0], layercolor[1], layercolor[2], layercolor[3], applycolor, applyfog);
5734                         break;
5735                 case TEXTURELAYERTYPE_FOG:
5736                         memset(&m, 0, sizeof(m));
5737                         m.texrgbscale[0] = layertexrgbscale;
5738                         if (layer->texture)
5739                         {
5740                                 m.tex[0] = R_GetTexture(layer->texture);
5741                                 m.texmatrix[0] = layer->texmatrix;
5742                                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
5743                                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
5744                                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
5745                         }
5746                         R_Mesh_TextureState(&m);
5747                         // generate a color array for the fog pass
5748                         R_Mesh_ColorPointer(rsurface.array_color4f, 0, 0);
5749                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5750                         {
5751                                 int i;
5752                                 float f, *v, *c;
5753                                 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5754                                 for (i = 0, v = (rsurface.vertex3f + 3 * surface->num_firstvertex), c = (rsurface.array_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
5755                                 {
5756                                         f = 1 - FogPoint_Model(v);
5757                                         c[0] = layercolor[0];
5758                                         c[1] = layercolor[1];
5759                                         c[2] = layercolor[2];
5760                                         c[3] = f * layercolor[3];
5761                                 }
5762                         }
5763                         RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5764                         break;
5765                 default:
5766                         Con_Printf("R_DrawTextureSurfaceList: unknown layer type %i\n", layer->type);
5767                 }
5768                 GL_LockArrays(0, 0);
5769         }
5770         CHECKGLERROR
5771         if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
5772         {
5773                 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
5774                 GL_AlphaTest(false);
5775         }
5776 }
5777
5778 static void R_DrawTextureSurfaceList_GL11(int texturenumsurfaces, msurface_t **texturesurfacelist)
5779 {
5780         // OpenGL 1.1 - crusty old voodoo path
5781         int texturesurfaceindex;
5782         qboolean applyfog;
5783         rmeshstate_t m;
5784         int layerindex;
5785         const texturelayer_t *layer;
5786         if (rsurface.mode != RSURFMODE_MULTIPASS)
5787                 rsurface.mode = RSURFMODE_MULTIPASS;
5788         RSurf_PrepareVerticesForBatch(true, false, texturenumsurfaces, texturesurfacelist);
5789
5790         for (layerindex = 0, layer = rsurface.texture->currentlayers;layerindex < rsurface.texture->currentnumlayers;layerindex++, layer++)
5791         {
5792                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
5793                 {
5794                         if (layerindex == 0)
5795                                 GL_AlphaTest(true);
5796                         else
5797                         {
5798                                 GL_AlphaTest(false);
5799                                 qglDepthFunc(GL_EQUAL);CHECKGLERROR
5800                         }
5801                 }
5802                 GL_DepthMask(layer->depthmask);
5803                 GL_BlendFunc(layer->blendfunc1, layer->blendfunc2);
5804                 R_Mesh_ColorPointer(NULL, 0, 0);
5805                 applyfog = (layer->flags & TEXTURELAYERFLAG_FOGDARKEN) != 0;
5806                 switch (layer->type)
5807                 {
5808                 case TEXTURELAYERTYPE_LITTEXTURE:
5809                         if (layer->blendfunc1 == GL_ONE && layer->blendfunc2 == GL_ZERO)
5810                         {
5811                                 // two-pass lit texture with 2x rgbscale
5812                                 // first the lightmap pass
5813                                 memset(&m, 0, sizeof(m));
5814                                 m.tex[0] = R_GetTexture(r_texture_white);
5815                                 m.pointer_texcoord[0] = rsurface.modeltexcoordlightmap2f;
5816                                 m.pointer_texcoord_bufferobject[0] = rsurface.modeltexcoordlightmap2f_bufferobject;
5817                                 m.pointer_texcoord_bufferoffset[0] = rsurface.modeltexcoordlightmap2f_bufferoffset;
5818                                 R_Mesh_TextureState(&m);
5819                                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
5820                                         RSurf_DrawBatch_GL11_VertexShade(texturenumsurfaces, texturesurfacelist, 1, 1, 1, 1, false, false);
5821                                 else if (rsurface.uselightmaptexture)
5822                                         RSurf_DrawBatch_GL11_Lightmap(texturenumsurfaces, texturesurfacelist, 1, 1, 1, 1, false, false);
5823                                 else
5824                                         RSurf_DrawBatch_GL11_VertexColor(texturenumsurfaces, texturesurfacelist, 1, 1, 1, 1, false, false);
5825                                 GL_LockArrays(0, 0);
5826                                 // then apply the texture to it
5827                                 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
5828                                 memset(&m, 0, sizeof(m));
5829                                 m.tex[0] = R_GetTexture(layer->texture);
5830                                 m.texmatrix[0] = layer->texmatrix;
5831                                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
5832                                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
5833                                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
5834                                 R_Mesh_TextureState(&m);
5835                                 RSurf_DrawBatch_GL11_Unlit(texturenumsurfaces, texturesurfacelist, layer->color[0] * 0.5f, layer->color[1] * 0.5f, layer->color[2] * 0.5f, layer->color[3], layer->color[0] != 2 || layer->color[1] != 2 || layer->color[2] != 2 || layer->color[3] != 1, false);
5836                         }
5837                         else
5838                         {
5839                                 // single pass vertex-lighting-only texture with 1x rgbscale and transparency support
5840                                 memset(&m, 0, sizeof(m));
5841                                 m.tex[0] = R_GetTexture(layer->texture);
5842                                 m.texmatrix[0] = layer->texmatrix;
5843                                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
5844                                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
5845                                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
5846                                 R_Mesh_TextureState(&m);
5847                                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
5848                                         RSurf_DrawBatch_GL11_VertexShade(texturenumsurfaces, texturesurfacelist, layer->color[0], layer->color[1], layer->color[2], layer->color[3], layer->color[0] != 1 || layer->color[1] != 1 || layer->color[2] != 1 || layer->color[3] != 1, applyfog);
5849                                 else
5850                                         RSurf_DrawBatch_GL11_VertexColor(texturenumsurfaces, texturesurfacelist, layer->color[0], layer->color[1], layer->color[2], layer->color[3], layer->color[0] != 1 || layer->color[1] != 1 || layer->color[2] != 1 || layer->color[3] != 1, applyfog);
5851                         }
5852                         break;
5853                 case TEXTURELAYERTYPE_TEXTURE:
5854                         // singletexture unlit texture with transparency support
5855                         memset(&m, 0, sizeof(m));
5856                         m.tex[0] = R_GetTexture(layer->texture);
5857                         m.texmatrix[0] = layer->texmatrix;
5858                         m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
5859                         m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
5860                         m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
5861                         R_Mesh_TextureState(&m);
5862                         RSurf_DrawBatch_GL11_Unlit(texturenumsurfaces, texturesurfacelist, layer->color[0], layer->color[1], layer->color[2], layer->color[3], layer->color[0] != 1 || layer->color[1] != 1 || layer->color[2] != 1 || layer->color[3] != 1, applyfog);
5863                         break;
5864                 case TEXTURELAYERTYPE_FOG:
5865                         // singletexture fogging
5866                         R_Mesh_ColorPointer(rsurface.array_color4f, 0, 0);
5867                         if (layer->texture)
5868                         {
5869                                 memset(&m, 0, sizeof(m));
5870                                 m.tex[0] = R_GetTexture(layer->texture);
5871                                 m.texmatrix[0] = layer->texmatrix;
5872                                 m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
5873                                 m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
5874                                 m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
5875                                 R_Mesh_TextureState(&m);
5876                         }
5877                         else
5878                                 R_Mesh_ResetTextureState();
5879                         // generate a color array for the fog pass
5880                         for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
5881                         {
5882                                 int i;
5883                                 float f, *v, *c;
5884                                 const msurface_t *surface = texturesurfacelist[texturesurfaceindex];
5885                                 for (i = 0, v = (rsurface.vertex3f + 3 * surface->num_firstvertex), c = (rsurface.array_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
5886                                 {
5887                                         f = 1 - FogPoint_Model(v);
5888                                         c[0] = layer->color[0];
5889                                         c[1] = layer->color[1];
5890                                         c[2] = layer->color[2];
5891                                         c[3] = f * layer->color[3];
5892                                 }
5893                         }
5894                         RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5895                         break;
5896                 default:
5897                         Con_Printf("R_DrawTextureSurfaceList: unknown layer type %i\n", layer->type);
5898                 }
5899                 GL_LockArrays(0, 0);
5900         }
5901         CHECKGLERROR
5902         if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
5903         {
5904                 qglDepthFunc(GL_LEQUAL);CHECKGLERROR
5905                 GL_AlphaTest(false);
5906         }
5907 }
5908
5909 static void R_DrawTextureSurfaceList(int texturenumsurfaces, msurface_t **texturesurfacelist, qboolean writedepth, qboolean depthonly)
5910 {
5911         if (rsurface.texture->currentmaterialflags & MATERIALFLAG_NODRAW)
5912                 return;
5913         rsurface.rtlight = NULL;
5914         CHECKGLERROR
5915         if (depthonly)
5916         {
5917                 if ((rsurface.texture->currentmaterialflags & (MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_BLENDED | MATERIALFLAG_ALPHATEST)))
5918                         return;
5919                 if (r_waterstate.renderingscene && (rsurface.texture->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFLECTION)))
5920                         return;
5921                 if (rsurface.mode != RSURFMODE_MULTIPASS)
5922                         rsurface.mode = RSURFMODE_MULTIPASS;
5923                 if (r_depthfirst.integer == 3)
5924                 {
5925                         int i = (int)(texturesurfacelist[0] - rsurface.modelsurfaces);
5926                         if (!r_view.showdebug)
5927                                 GL_Color(0, 0, 0, 1);
5928                         else
5929                                 GL_Color(((i >> 6) & 7) / 7.0f, ((i >> 3) & 7) / 7.0f, (i & 7) / 7.0f,1);
5930                 }
5931                 else
5932                 {
5933                         GL_ColorMask(0,0,0,0);
5934                         GL_Color(1,1,1,1);
5935                 }
5936                 RSurf_SetupDepthAndCulling();
5937                 GL_DepthTest(true);
5938                 GL_BlendFunc(GL_ONE, GL_ZERO);
5939                 GL_DepthMask(true);
5940                 GL_AlphaTest(false);
5941                 R_Mesh_ColorPointer(NULL, 0, 0);
5942                 R_Mesh_ResetTextureState();
5943                 RSurf_PrepareVerticesForBatch(false, false, texturenumsurfaces, texturesurfacelist);
5944                 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5945                 GL_ColorMask(r_view.colormask[0], r_view.colormask[1], r_view.colormask[2], 1);
5946         }
5947         else if (r_depthfirst.integer == 3)
5948                 return;
5949         else if (!r_view.showdebug && (r_showsurfaces.integer || gl_lightmaps.integer))
5950         {
5951                 GL_Color(0, 0, 0, 1);
5952                 RSurf_DrawBatch_Simple(texturenumsurfaces, texturesurfacelist);
5953         }
5954         else if (r_showsurfaces.integer)
5955         {
5956                 if (rsurface.mode != RSURFMODE_MULTIPASS)
5957                         rsurface.mode = RSURFMODE_MULTIPASS;
5958                 RSurf_SetupDepthAndCulling();
5959                 GL_DepthTest(true);
5960                 GL_BlendFunc(GL_ONE, GL_ZERO);
5961                 GL_DepthMask(writedepth);
5962                 GL_Color(1,1,1,1);
5963                 GL_AlphaTest(false);
5964                 R_Mesh_ColorPointer(NULL, 0, 0);
5965                 R_Mesh_ResetTextureState();
5966                 RSurf_PrepareVerticesForBatch(false, false, texturenumsurfaces, texturesurfacelist);
5967                 R_DrawTextureSurfaceList_ShowSurfaces(texturenumsurfaces, texturesurfacelist);
5968         }
5969         else if (gl_lightmaps.integer)
5970         {
5971                 rmeshstate_t m;
5972                 if (rsurface.mode != RSURFMODE_MULTIPASS)
5973                         rsurface.mode = RSURFMODE_MULTIPASS;
5974                 GL_DepthRange(0, (rsurface.texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) ? 0.0625 : 1);
5975                 GL_DepthTest(true);
5976                 GL_CullFace((rsurface.texture->currentmaterialflags & MATERIALFLAG_NOCULLFACE) ? GL_NONE : r_view.cullface_back);
5977                 GL_BlendFunc(GL_ONE, GL_ZERO);
5978                 GL_DepthMask(writedepth);
5979                 GL_Color(1,1,1,1);
5980                 GL_AlphaTest(false);
5981                 R_Mesh_ColorPointer(NULL, 0, 0);
5982                 memset(&m, 0, sizeof(m));
5983                 m.tex[0] = R_GetTexture(r_texture_white);
5984                 m.pointer_texcoord[0] = rsurface.modeltexcoordlightmap2f;
5985                 m.pointer_texcoord_bufferobject[0] = rsurface.modeltexcoordlightmap2f_bufferobject;
5986                 m.pointer_texcoord_bufferoffset[0] = rsurface.modeltexcoordlightmap2f_bufferoffset;
5987                 R_Mesh_TextureState(&m);
5988                 RSurf_PrepareVerticesForBatch(rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT, false, texturenumsurfaces, texturesurfacelist);
5989                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
5990                         RSurf_DrawBatch_GL11_VertexShade(texturenumsurfaces, texturesurfacelist, 1, 1, 1, 1, false, false);
5991                 else if (rsurface.uselightmaptexture)
5992                         RSurf_DrawBatch_GL11_Lightmap(texturenumsurfaces, texturesurfacelist, 1, 1, 1, 1, false, false);
5993                 else
5994                         RSurf_DrawBatch_GL11_VertexColor(texturenumsurfaces, texturesurfacelist, 1, 1, 1, 1, false, false);
5995         }
5996         else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_SKY)
5997                 R_DrawTextureSurfaceList_Sky(texturenumsurfaces, texturesurfacelist);
5998         else if (rsurface.texture->currentnumlayers)
5999         {
6000                 // write depth for anything we skipped on the depth-only pass earlier
6001                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST)
6002                         writedepth = true;
6003                 RSurf_SetupDepthAndCulling();
6004                 GL_BlendFunc(rsurface.texture->currentlayers[0].blendfunc1, rsurface.texture->currentlayers[0].blendfunc2);
6005                 GL_DepthMask(writedepth && !(rsurface.texture->currentmaterialflags & MATERIALFLAG_BLENDED));
6006                 GL_AlphaTest((rsurface.texture->currentmaterialflags & MATERIALFLAG_ALPHATEST) != 0);
6007                 if (r_glsl.integer && gl_support_fragment_shader)
6008                         R_DrawTextureSurfaceList_GL20(texturenumsurfaces, texturesurfacelist);
6009                 else if (gl_combine.integer && r_textureunits.integer >= 2)
6010                         R_DrawTextureSurfaceList_GL13(texturenumsurfaces, texturesurfacelist);
6011                 else
6012                         R_DrawTextureSurfaceList_GL11(texturenumsurfaces, texturesurfacelist);
6013         }
6014         CHECKGLERROR
6015         GL_LockArrays(0, 0);
6016 }
6017
6018 static void R_DrawSurface_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
6019 {
6020         int i, j;
6021         int texturenumsurfaces, endsurface;
6022         texture_t *texture;
6023         msurface_t *surface;
6024         msurface_t *texturesurfacelist[1024];
6025
6026         // if the model is static it doesn't matter what value we give for
6027         // wantnormals and wanttangents, so this logic uses only rules applicable
6028         // to a model, knowing that they are meaningless otherwise
6029         if (ent == r_refdef.worldentity)
6030                 RSurf_ActiveWorldEntity();
6031         else if ((ent->effects & EF_FULLBRIGHT) || r_showsurfaces.integer || VectorLength2(ent->modellight_diffuse) < (1.0f / 256.0f))
6032                 RSurf_ActiveModelEntity(ent, false, false);
6033         else
6034                 RSurf_ActiveModelEntity(ent, true, r_glsl.integer && gl_support_fragment_shader);
6035
6036         for (i = 0;i < numsurfaces;i = j)
6037         {
6038                 j = i + 1;
6039                 surface = rsurface.modelsurfaces + surfacelist[i];
6040                 texture = surface->texture;
6041                 R_UpdateTextureInfo(ent, texture);
6042                 rsurface.texture = texture->currentframe;
6043                 rsurface.uselightmaptexture = surface->lightmaptexture != NULL;
6044                 // scan ahead until we find a different texture
6045                 endsurface = min(i + 1024, numsurfaces);
6046                 texturenumsurfaces = 0;
6047                 texturesurfacelist[texturenumsurfaces++] = surface;
6048                 for (;j < endsurface;j++)
6049                 {
6050                         surface = rsurface.modelsurfaces + surfacelist[j];
6051                         if (texture != surface->texture || rsurface.uselightmaptexture != (surface->lightmaptexture != NULL))
6052                                 break;
6053                         texturesurfacelist[texturenumsurfaces++] = surface;
6054                 }
6055                 // render the range of surfaces
6056                 R_DrawTextureSurfaceList(texturenumsurfaces, texturesurfacelist, true, false);
6057         }
6058
6059         RSurf_CleanUp();
6060 }
6061
6062 void R_QueueSurfaceList(entity_render_t *ent, int numsurfaces, msurface_t **surfacelist, int flagsmask, qboolean writedepth, qboolean depthonly, qboolean addwaterplanes)
6063 {
6064         int i, j;
6065         vec3_t tempcenter, center;
6066         texture_t *texture;
6067         // if we're rendering water textures (extra scene renders), use a separate loop to avoid burdening the main one
6068         if (addwaterplanes)
6069         {
6070                 for (i = 0;i < numsurfaces;i++)
6071                         if (surfacelist[i]->texture->currentframe->currentmaterialflags & (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION))
6072                                 R_Water_AddWaterPlane(surfacelist[i]);
6073                 return;
6074         }
6075         // break the surface list down into batches by texture and use of lightmapping
6076         for (i = 0;i < numsurfaces;i = j)
6077         {
6078                 j = i + 1;
6079                 // texture is the base texture pointer, rsurface.texture is the
6080                 // current frame/skin the texture is directing us to use (for example
6081                 // if a model has 2 skins and it is on skin 1, then skin 0 tells us to
6082                 // use skin 1 instead)
6083                 texture = surfacelist[i]->texture;
6084                 rsurface.texture = texture->currentframe;
6085                 rsurface.uselightmaptexture = surfacelist[i]->lightmaptexture != NULL;
6086                 if (!(rsurface.texture->currentmaterialflags & flagsmask))
6087                 {
6088                         // if this texture is not the kind we want, skip ahead to the next one
6089                         for (;j < numsurfaces && texture == surfacelist[j]->texture;j++)
6090                                 ;
6091                         continue;
6092                 }
6093                 if (rsurface.texture->currentmaterialflags & MATERIALFLAGMASK_DEPTHSORTED)
6094                 {
6095                         // transparent surfaces get pushed off into the transparent queue
6096                         const msurface_t *surface = surfacelist[i];
6097                         if (depthonly)
6098                                 continue;
6099                         tempcenter[0] = (surface->mins[0] + surface->maxs[0]) * 0.5f;
6100                         tempcenter[1] = (surface->mins[1] + surface->maxs[1]) * 0.5f;
6101                         tempcenter[2] = (surface->mins[2] + surface->maxs[2]) * 0.5f;
6102                         Matrix4x4_Transform(&rsurface.matrix, tempcenter, center);
6103                         R_MeshQueue_AddTransparent(rsurface.texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST ? r_view.origin : center, R_DrawSurface_TransparentCallback, ent, surface - rsurface.modelsurfaces, rsurface.rtlight);
6104                 }
6105                 else
6106                 {
6107                         // simply scan ahead until we find a different texture or lightmap state
6108                         for (;j < numsurfaces && texture == surfacelist[j]->texture && rsurface.uselightmaptexture == (surfacelist[j]->lightmaptexture != NULL);j++)
6109                                 ;
6110                         // render the range of surfaces
6111                         R_DrawTextureSurfaceList(j - i, surfacelist + i, writedepth, depthonly);
6112                 }
6113         }
6114 }
6115
6116 float locboxvertex3f[6*4*3] =
6117 {
6118         1,0,1, 1,0,0, 1,1,0, 1,1,1,
6119         0,1,1, 0,1,0, 0,0,0, 0,0,1,
6120         1,1,1, 1,1,0, 0,1,0, 0,1,1,
6121         0,0,1, 0,0,0, 1,0,0, 1,0,1,
6122         0,0,1, 1,0,1, 1,1,1, 0,1,1,
6123         1,0,0, 0,0,0, 0,1,0, 1,1,0
6124 };
6125
6126 int locboxelement3i[6*2*3] =
6127 {
6128          0, 1, 2, 0, 2, 3,
6129          4, 5, 6, 4, 6, 7,
6130          8, 9,10, 8,10,11,
6131         12,13,14, 12,14,15,
6132         16,17,18, 16,18,19,
6133         20,21,22, 20,22,23
6134 };
6135
6136 void R_DrawLoc_Callback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
6137 {
6138         int i, j;
6139         cl_locnode_t *loc = (cl_locnode_t *)ent;
6140         vec3_t mins, size;
6141         float vertex3f[6*4*3];
6142         CHECKGLERROR
6143         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
6144         GL_DepthMask(false);
6145         GL_DepthRange(0, 1);
6146         GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
6147         GL_DepthTest(true);
6148         GL_CullFace(GL_NONE);
6149         R_Mesh_Matrix(&identitymatrix);
6150
6151         R_Mesh_VertexPointer(vertex3f, 0, 0);
6152         R_Mesh_ColorPointer(NULL, 0, 0);
6153         R_Mesh_ResetTextureState();
6154
6155         i = surfacelist[0];
6156         GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f) * r_view.colorscale,
6157                          ((i & 0x0038) >> 3) * (1.0f / 7.0f) * r_view.colorscale,
6158                          ((i & 0x01C0) >> 6) * (1.0f / 7.0f) * r_view.colorscale,
6159                         surfacelist[0] < 0 ? 0.5f : 0.125f);
6160
6161         if (VectorCompare(loc->mins, loc->maxs))
6162         {
6163                 VectorSet(size, 2, 2, 2);
6164                 VectorMA(loc->mins, -0.5f, size, mins);
6165         }
6166         else
6167         {
6168                 VectorCopy(loc->mins, mins);
6169                 VectorSubtract(loc->maxs, loc->mins, size);
6170         }
6171
6172         for (i = 0;i < 6*4*3;)
6173                 for (j = 0;j < 3;j++, i++)
6174                         vertex3f[i] = mins[j] + size[j] * locboxvertex3f[i];
6175
6176         R_Mesh_Draw(0, 6*4, 6*2, locboxelement3i, 0, 0);
6177 }
6178
6179 void R_DrawLocs(void)
6180 {
6181         int index;
6182         cl_locnode_t *loc, *nearestloc;
6183         vec3_t center;
6184         nearestloc = CL_Locs_FindNearest(cl.movement_origin);
6185         for (loc = cl.locnodes, index = 0;loc;loc = loc->next, index++)
6186         {
6187                 VectorLerp(loc->mins, 0.5f, loc->maxs, center);
6188                 R_MeshQueue_AddTransparent(center, R_DrawLoc_Callback, (entity_render_t *)loc, loc == nearestloc ? -1 : index, NULL);
6189         }
6190 }
6191
6192 void R_DrawDebugModel(entity_render_t *ent)
6193 {
6194         int i, j, k, l, flagsmask;
6195         const int *elements;
6196         q3mbrush_t *brush;
6197         msurface_t *surface;
6198         model_t *model = ent->model;
6199         vec3_t v;
6200
6201         flagsmask = MATERIALFLAG_SKY | MATERIALFLAG_WATER | MATERIALFLAG_WALL;
6202
6203         R_Mesh_ColorPointer(NULL, 0, 0);
6204         R_Mesh_ResetTextureState();
6205         GL_DepthRange(0, 1);
6206         GL_DepthTest(!r_showdisabledepthtest.integer);
6207         GL_DepthMask(false);
6208         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
6209
6210         if (r_showcollisionbrushes.value > 0 && model->brush.num_brushes)
6211         {
6212                 GL_PolygonOffset(r_refdef.polygonfactor + r_showcollisionbrushes_polygonfactor.value, r_refdef.polygonoffset + r_showcollisionbrushes_polygonoffset.value);
6213                 for (i = 0, brush = model->brush.data_brushes + model->firstmodelbrush;i < model->nummodelbrushes;i++, brush++)
6214                 {
6215                         if (brush->colbrushf && brush->colbrushf->numtriangles)
6216                         {
6217                                 R_Mesh_VertexPointer(brush->colbrushf->points->v, 0, 0);
6218                                 GL_Color((i & 31) * (1.0f / 32.0f) * r_view.colorscale, ((i >> 5) & 31) * (1.0f / 32.0f) * r_view.colorscale, ((i >> 10) & 31) * (1.0f / 32.0f) * r_view.colorscale, r_showcollisionbrushes.value);
6219                                 R_Mesh_Draw(0, brush->colbrushf->numpoints, brush->colbrushf->numtriangles, brush->colbrushf->elements, 0, 0);
6220                         }
6221                 }
6222                 for (i = 0, surface = model->data_surfaces + model->firstmodelsurface;i < model->nummodelsurfaces;i++, surface++)
6223                 {
6224                         if (surface->num_collisiontriangles)
6225                         {
6226                                 R_Mesh_VertexPointer(surface->data_collisionvertex3f, 0, 0);
6227                                 GL_Color((i & 31) * (1.0f / 32.0f) * r_view.colorscale, ((i >> 5) & 31) * (1.0f / 32.0f) * r_view.colorscale, ((i >> 10) & 31) * (1.0f / 32.0f) * r_view.colorscale, r_showcollisionbrushes.value);
6228                                 R_Mesh_Draw(0, surface->num_collisionvertices, surface->num_collisiontriangles, surface->data_collisionelement3i, 0, 0);
6229                         }
6230                 }
6231         }
6232
6233         GL_PolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);
6234
6235         if (r_showtris.integer || r_shownormals.integer)
6236         {
6237                 if (r_showdisabledepthtest.integer)
6238                 {
6239                         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
6240                         GL_DepthMask(false);
6241                 }
6242                 else
6243                 {
6244                         GL_BlendFunc(GL_ONE, GL_ZERO);
6245                         GL_DepthMask(true);
6246                 }
6247                 for (i = 0, j = model->firstmodelsurface, surface = model->data_surfaces + j;i < model->nummodelsurfaces;i++, j++, surface++)
6248                 {
6249                         if (ent == r_refdef.worldentity && !r_viewcache.world_surfacevisible[j])
6250                                 continue;
6251                         rsurface.texture = surface->texture->currentframe;
6252                         if ((rsurface.texture->currentmaterialflags & flagsmask) && surface->num_triangles)
6253                         {
6254                                 RSurf_PrepareVerticesForBatch(true, true, 1, &surface);
6255                                 if (r_showtris.value > 0)
6256                                 {
6257                                         if (!rsurface.texture->currentlayers->depthmask)
6258                                                 GL_Color(r_view.colorscale, 0, 0, r_showtris.value);
6259                                         else if (ent == r_refdef.worldentity)
6260                                                 GL_Color(r_view.colorscale, r_view.colorscale, r_view.colorscale, r_showtris.value);
6261                                         else
6262                                                 GL_Color(0, r_view.colorscale, 0, r_showtris.value);
6263                                         elements = (ent->model->surfmesh.data_element3i + 3 * surface->num_firsttriangle);
6264                                         CHECKGLERROR
6265                                         qglBegin(GL_LINES);
6266                                         for (k = 0;k < surface->num_triangles;k++, elements += 3)
6267                                         {
6268 #define GLVERTEXELEMENT(n) qglVertex3f(rsurface.vertex3f[elements[n]*3+0], rsurface.vertex3f[elements[n]*3+1], rsurface.vertex3f[elements[n]*3+2])
6269                                                 GLVERTEXELEMENT(0);GLVERTEXELEMENT(1);
6270                                                 GLVERTEXELEMENT(1);GLVERTEXELEMENT(2);
6271                                                 GLVERTEXELEMENT(2);GLVERTEXELEMENT(0);
6272                                         }
6273                                         qglEnd();
6274                                         CHECKGLERROR
6275                                 }
6276                                 if (r_shownormals.value > 0)
6277                                 {
6278                                         qglBegin(GL_LINES);
6279                                         for (k = 0, l = surface->num_firstvertex;k < surface->num_vertices;k++, l++)
6280                                         {
6281                                                 VectorCopy(rsurface.vertex3f + l * 3, v);
6282                                                 GL_Color(r_view.colorscale, 0, 0, 1);
6283                                                 qglVertex3f(v[0], v[1], v[2]);
6284                                                 VectorMA(v, r_shownormals.value, rsurface.svector3f + l * 3, v);
6285                                                 GL_Color(r_view.colorscale, 1, 1, 1);
6286                                                 qglVertex3f(v[0], v[1], v[2]);
6287                                         }
6288                                         qglEnd();
6289                                         CHECKGLERROR
6290                                         qglBegin(GL_LINES);
6291                                         for (k = 0, l = surface->num_firstvertex;k < surface->num_vertices;k++, l++)
6292                                         {
6293                                                 VectorCopy(rsurface.vertex3f + l * 3, v);
6294                                                 GL_Color(0, r_view.colorscale, 0, 1);
6295                                                 qglVertex3f(v[0], v[1], v[2]);
6296                                                 VectorMA(v, r_shownormals.value, rsurface.tvector3f + l * 3, v);
6297                                                 GL_Color(r_view.colorscale, 1, 1, 1);
6298                                                 qglVertex3f(v[0], v[1], v[2]);
6299                                         }
6300                                         qglEnd();
6301                                         CHECKGLERROR
6302                                         qglBegin(GL_LINES);
6303                                         for (k = 0, l = surface->num_firstvertex;k < surface->num_vertices;k++, l++)
6304                                         {
6305                                                 VectorCopy(rsurface.vertex3f + l * 3, v);
6306                                                 GL_Color(0, 0, r_view.colorscale, 1);
6307                                                 qglVertex3f(v[0], v[1], v[2]);
6308                                                 VectorMA(v, r_shownormals.value, rsurface.normal3f + l * 3, v);
6309                                                 GL_Color(r_view.colorscale, 1, 1, 1);
6310                                                 qglVertex3f(v[0], v[1], v[2]);
6311                                         }
6312                                         qglEnd();
6313                                         CHECKGLERROR
6314                                 }
6315                         }
6316                 }
6317                 rsurface.texture = NULL;
6318         }
6319 }
6320
6321 extern void R_BuildLightMap(const entity_render_t *ent, msurface_t *surface);
6322 void R_DrawWorldSurfaces(qboolean skysurfaces, qboolean writedepth, qboolean depthonly, qboolean addwaterplanes, qboolean debug)
6323 {
6324         int i, j, endj, f, flagsmask;
6325         msurface_t *surface;
6326         texture_t *t;
6327         model_t *model = r_refdef.worldmodel;
6328         const int maxsurfacelist = 1024;
6329         int numsurfacelist = 0;
6330         msurface_t *surfacelist[1024];
6331         if (model == NULL)
6332                 return;
6333
6334         RSurf_ActiveWorldEntity();
6335
6336         // update light styles on this submodel
6337         if (!skysurfaces && !depthonly && !addwaterplanes && model->brushq1.num_lightstyles && r_refdef.lightmapintensity > 0)
6338         {
6339                 model_brush_lightstyleinfo_t *style;
6340                 for (i = 0, style = model->brushq1.data_lightstyleinfo;i < model->brushq1.num_lightstyles;i++, style++)
6341                 {
6342                         if (style->value != r_refdef.lightstylevalue[style->style])
6343                         {
6344                                 msurface_t *surfaces = model->data_surfaces;
6345                                 int *list = style->surfacelist;
6346                                 style->value = r_refdef.lightstylevalue[style->style];
6347                                 for (j = 0;j < style->numsurfaces;j++)
6348                                         surfaces[list[j]].cached_dlight = true;
6349                         }
6350                 }
6351         }
6352
6353         R_UpdateAllTextureInfo(r_refdef.worldentity);
6354         flagsmask = addwaterplanes ? (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION) : (skysurfaces ? MATERIALFLAG_SKY : (MATERIALFLAG_WATER | MATERIALFLAG_WALL));
6355
6356         if (debug)
6357         {
6358                 R_DrawDebugModel(r_refdef.worldentity);
6359                 return;
6360         }
6361
6362         f = 0;
6363         t = NULL;
6364         rsurface.uselightmaptexture = false;
6365         rsurface.texture = NULL;
6366         numsurfacelist = 0;
6367         j = model->firstmodelsurface;
6368         endj = j + model->nummodelsurfaces;
6369         while (j < endj)
6370         {
6371                 // quickly skip over non-visible surfaces
6372                 for (;j < endj && !r_viewcache.world_surfacevisible[j];j++)
6373                         ;
6374                 // quickly iterate over visible surfaces
6375                 for (;j < endj && r_viewcache.world_surfacevisible[j];j++)
6376                 {
6377                         // process this surface
6378                         surface = model->data_surfaces + j;
6379                         // if this surface fits the criteria, add it to the list
6380                         if (surface->num_triangles)
6381                         {
6382                                 // if lightmap parameters changed, rebuild lightmap texture
6383                                 if (surface->cached_dlight)
6384                                         R_BuildLightMap(r_refdef.worldentity, surface);
6385                                 // add face to draw list
6386                                 surfacelist[numsurfacelist++] = surface;
6387                                 r_refdef.stats.world_triangles += surface->num_triangles;
6388                                 if (numsurfacelist >= maxsurfacelist)
6389                                 {
6390                                         r_refdef.stats.world_surfaces += numsurfacelist;
6391                                         R_QueueSurfaceList(r_refdef.worldentity, numsurfacelist, surfacelist, flagsmask, writedepth, depthonly, addwaterplanes);
6392                                         numsurfacelist = 0;
6393                                 }
6394                         }
6395                 }
6396         }
6397         r_refdef.stats.world_surfaces += numsurfacelist;
6398         if (numsurfacelist)
6399                 R_QueueSurfaceList(r_refdef.worldentity, numsurfacelist, surfacelist, flagsmask, writedepth, depthonly, addwaterplanes);
6400         RSurf_CleanUp();
6401 }
6402
6403 void R_DrawModelSurfaces(entity_render_t *ent, qboolean skysurfaces, qboolean writedepth, qboolean depthonly, qboolean addwaterplanes, qboolean debug)
6404 {
6405         int i, j, f, flagsmask;
6406         msurface_t *surface, *endsurface;
6407         texture_t *t;
6408         model_t *model = ent->model;
6409         const int maxsurfacelist = 1024;
6410         int numsurfacelist = 0;
6411         msurface_t *surfacelist[1024];
6412         if (model == NULL)
6413                 return;
6414
6415         // if the model is static it doesn't matter what value we give for
6416         // wantnormals and wanttangents, so this logic uses only rules applicable
6417         // to a model, knowing that they are meaningless otherwise
6418         if (ent == r_refdef.worldentity)
6419                 RSurf_ActiveWorldEntity();
6420         else if ((ent->effects & EF_FULLBRIGHT) || r_showsurfaces.integer || VectorLength2(ent->modellight_diffuse) < (1.0f / 256.0f))
6421                 RSurf_ActiveModelEntity(ent, false, false);
6422         else
6423                 RSurf_ActiveModelEntity(ent, true, r_glsl.integer && gl_support_fragment_shader && !depthonly);
6424
6425         // update light styles
6426         if (!skysurfaces && !depthonly && !addwaterplanes && model->brushq1.num_lightstyles && r_refdef.lightmapintensity > 0)
6427         {
6428                 model_brush_lightstyleinfo_t *style;
6429                 for (i = 0, style = model->brushq1.data_lightstyleinfo;i < model->brushq1.num_lightstyles;i++, style++)
6430                 {
6431                         if (style->value != r_refdef.lightstylevalue[style->style])
6432                         {
6433                                 msurface_t *surfaces = model->data_surfaces;
6434                                 int *list = style->surfacelist;
6435                                 style->value = r_refdef.lightstylevalue[style->style];
6436                                 for (j = 0;j < style->numsurfaces;j++)
6437                                         surfaces[list[j]].cached_dlight = true;
6438                         }
6439                 }
6440         }
6441
6442         R_UpdateAllTextureInfo(ent);
6443         flagsmask = addwaterplanes ? (MATERIALFLAG_WATERSHADER | MATERIALFLAG_REFRACTION | MATERIALFLAG_REFLECTION) : (skysurfaces ? MATERIALFLAG_SKY : (MATERIALFLAG_WATER | MATERIALFLAG_WALL));
6444
6445         if (debug)
6446         {
6447                 R_DrawDebugModel(ent);
6448                 return;
6449         }
6450
6451         f = 0;
6452         t = NULL;
6453         rsurface.uselightmaptexture = false;
6454         rsurface.texture = NULL;
6455         numsurfacelist = 0;
6456         surface = model->data_surfaces + model->firstmodelsurface;
6457         endsurface = surface + model->nummodelsurfaces;
6458         for (;surface < endsurface;surface++)
6459         {
6460                 // if this surface fits the criteria, add it to the list
6461                 if (surface->num_triangles)
6462                 {
6463                         // if lightmap parameters changed, rebuild lightmap texture
6464                         if (surface->cached_dlight)
6465                                 R_BuildLightMap(ent, surface);
6466                         // add face to draw list
6467                         surfacelist[numsurfacelist++] = surface;
6468                         r_refdef.stats.entities_triangles += surface->num_triangles;
6469                         if (numsurfacelist >= maxsurfacelist)
6470                         {
6471                                 r_refdef.stats.entities_surfaces += numsurfacelist;
6472                                 R_QueueSurfaceList(ent, numsurfacelist, surfacelist, flagsmask, writedepth, depthonly, addwaterplanes);
6473                                 numsurfacelist = 0;
6474                         }
6475                 }
6476         }
6477         r_refdef.stats.entities_surfaces += numsurfacelist;
6478         if (numsurfacelist)
6479                 R_QueueSurfaceList(ent, numsurfacelist, surfacelist, flagsmask, writedepth, depthonly, addwaterplanes);
6480         RSurf_CleanUp();
6481 }