converted all code using gl_support_* variables to use vid.support.*
[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 static int r_frame = 0; ///< used only by R_GetCurrentTexture
32
33 qboolean r_loadnormalmap;
34 qboolean r_loadgloss;
35 qboolean r_loadfog;
36
37 //
38 // screen size info
39 //
40 r_refdef_t r_refdef;
41
42 cvar_t r_motionblur = {CVAR_SAVE, "r_motionblur", "0", "motionblur value scale - 0.5 recommended"};
43 cvar_t r_damageblur = {CVAR_SAVE, "r_damageblur", "0", "motionblur based on damage"};
44 cvar_t r_motionblur_vmin = {CVAR_SAVE, "r_motionblur_vmin", "300", "minimum influence from velocity"};
45 cvar_t r_motionblur_vmax = {CVAR_SAVE, "r_motionblur_vmax", "600", "maximum influence from velocity"};
46 cvar_t r_motionblur_bmin = {CVAR_SAVE, "r_motionblur_bmin", "0.5", "velocity at which there is no blur yet (may be negative to always have some blur)"};
47 cvar_t r_motionblur_vcoeff = {CVAR_SAVE, "r_motionblur_vcoeff", "0.05", "sliding average reaction time for velocity"};
48 cvar_t r_motionblur_maxblur = {CVAR_SAVE, "r_motionblur_maxblur", "0.88", "cap for motionblur alpha value"};
49 cvar_t r_motionblur_randomize = {CVAR_SAVE, "r_motionblur_randomize", "0.1", "randomizing coefficient to workaround ghosting"};
50
51 // TODO do we want a r_equalize_entities cvar that works on all ents, or would that be a cheat?
52 cvar_t r_equalize_entities_fullbright = {CVAR_SAVE, "r_equalize_entities_fullbright", "0", "render fullbright entities by equalizing their lightness, not by not rendering light"};
53 cvar_t r_equalize_entities_minambient = {CVAR_SAVE, "r_equalize_entities_minambient", "0.5", "light equalizing: ensure at least this ambient/diffuse ratio"};
54 cvar_t r_equalize_entities_by = {CVAR_SAVE, "r_equalize_entities_by", "0.7", "light equalizing: exponent of dynamics compression (0 = no compression, 1 = full compression)"};
55 cvar_t r_equalize_entities_to = {CVAR_SAVE, "r_equalize_entities_to", "0.8", "light equalizing: target light level"};
56
57 cvar_t r_animcache = {CVAR_SAVE, "r_animcache", "1", "cache animation frames to save CPU usage, primarily optimizes shadows and reflections"};
58
59 cvar_t r_depthfirst = {CVAR_SAVE, "r_depthfirst", "0", "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"};
60 cvar_t r_useinfinitefarclip = {CVAR_SAVE, "r_useinfinitefarclip", "1", "enables use of a special kind of projection matrix that has an extremely large farclip"};
61 cvar_t r_farclip_base = {0, "r_farclip_base", "65536", "farclip (furthest visible distance) for rendering when r_useinfinitefarclip is 0"};
62 cvar_t r_farclip_world = {0, "r_farclip_world", "2", "adds map size to farclip multiplied by this value"};
63 cvar_t r_nearclip = {0, "r_nearclip", "1", "distance from camera of nearclip plane" };
64 cvar_t r_showbboxes = {0, "r_showbboxes", "0", "shows bounding boxes of server entities, value controls opacity scaling (1 = 10%,  10 = 100%)"};
65 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)"};
66 cvar_t r_showtris = {0, "r_showtris", "0", "shows triangle outlines, value controls brightness (can be above 1)"};
67 cvar_t r_shownormals = {0, "r_shownormals", "0", "shows per-vertex surface normals and tangent vectors for bumpmapped lighting"};
68 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"};
69 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"};
70 cvar_t r_showcollisionbrushes = {0, "r_showcollisionbrushes", "0", "draws collision brushes in quake3 maps (mode 1), mode 2 disables rendering of world (trippy!)"};
71 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"};
72 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"};
73 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"};
74 cvar_t r_drawportals = {0, "r_drawportals", "0", "shows portals (separating polygons) in world interior in quake1 maps"};
75 cvar_t r_drawentities = {0, "r_drawentities","1", "draw entities (doors, players, projectiles, etc)"};
76 cvar_t r_drawviewmodel = {0, "r_drawviewmodel","1", "draw your weapon model"};
77 cvar_t r_cullentities_trace = {0, "r_cullentities_trace", "1", "probabistically cull invisible entities"};
78 cvar_t r_cullentities_trace_samples = {0, "r_cullentities_trace_samples", "2", "number of samples to test for entity culling (in addition to center sample)"};
79 cvar_t r_cullentities_trace_tempentitysamples = {0, "r_cullentities_trace_tempentitysamples", "-1", "number of samples to test for entity culling of temp entities (including all CSQC entities), -1 disables trace culling on these entities to prevent flicker (pvs still applies)"};
80 cvar_t r_cullentities_trace_enlarge = {0, "r_cullentities_trace_enlarge", "0", "box enlargement for entity culling"};
81 cvar_t r_cullentities_trace_delay = {0, "r_cullentities_trace_delay", "1", "number of seconds until the entity gets actually culled"};
82 cvar_t r_speeds = {0, "r_speeds","0", "displays rendering statistics and per-subsystem timings"};
83 cvar_t r_fullbright = {0, "r_fullbright","0", "makes map very bright and renders faster"};
84 cvar_t r_wateralpha = {CVAR_SAVE, "r_wateralpha","1", "opacity of water polygons"};
85 cvar_t r_dynamic = {CVAR_SAVE, "r_dynamic","1", "enables dynamic lights (rocket glow and such)"};
86 cvar_t r_fullbrights = {CVAR_SAVE, "r_fullbrights", "1", "enables glowing pixels in quake textures (changes need r_restart to take effect)"};
87 cvar_t r_shadows = {CVAR_SAVE, "r_shadows", "0", "casts fake stencil shadows from models onto the world (rtlights are unaffected by this); when set to 2, always cast the shadows in the direction set by r_shadows_throwdirection, otherwise use the model lighting."};
88 cvar_t r_shadows_darken = {CVAR_SAVE, "r_shadows_darken", "0.5", "how much shadowed areas will be darkened"};
89 cvar_t r_shadows_throwdistance = {CVAR_SAVE, "r_shadows_throwdistance", "500", "how far to cast shadows from models"};
90 cvar_t r_shadows_throwdirection = {CVAR_SAVE, "r_shadows_throwdirection", "0 0 -1", "override throwing direction for r_shadows 2"};
91 cvar_t r_shadows_drawafterrtlighting = {CVAR_SAVE, "r_shadows_drawafterrtlighting", "0", "draw fake shadows AFTER realtime lightning is drawn. May be useful for simulating fast sunlight on large outdoor maps with only one noshadow rtlight. The price is less realistic appearance of dynamic light shadows."};
92 cvar_t r_shadows_castfrombmodels = {CVAR_SAVE, "r_shadows_castfrombmodels", "0", "do cast shadows from bmodels"};
93 cvar_t r_q1bsp_skymasking = {0, "r_q1bsp_skymasking", "1", "allows sky polygons in quake1 maps to obscure other geometry"};
94 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"};
95 cvar_t r_polygonoffset_submodel_offset = {0, "r_polygonoffset_submodel_offset", "14", "biases depth values of world submodels such as doors, to prevent z-fighting artifacts in Quake maps"};
96 cvar_t r_polygonoffset_decals_factor = {0, "r_polygonoffset_decals_factor", "0", "biases depth values of decals to prevent z-fighting artifacts"};
97 cvar_t r_polygonoffset_decals_offset = {0, "r_polygonoffset_decals_offset", "-14", "biases depth values of decals to prevent z-fighting artifacts"};
98 cvar_t r_fog_exp2 = {0, "r_fog_exp2", "0", "uses GL_EXP2 fog (as in Nehahra) rather than realistic GL_EXP fog"};
99 cvar_t r_drawfog = {CVAR_SAVE, "r_drawfog", "1", "allows one to disable fog rendering"};
100
101 cvar_t gl_fogenable = {0, "gl_fogenable", "0", "nehahra fog enable (for Nehahra compatibility only)"};
102 cvar_t gl_fogdensity = {0, "gl_fogdensity", "0.25", "nehahra fog density (recommend values below 0.1) (for Nehahra compatibility only)"};
103 cvar_t gl_fogred = {0, "gl_fogred","0.3", "nehahra fog color red value (for Nehahra compatibility only)"};
104 cvar_t gl_foggreen = {0, "gl_foggreen","0.3", "nehahra fog color green value (for Nehahra compatibility only)"};
105 cvar_t gl_fogblue = {0, "gl_fogblue","0.3", "nehahra fog color blue value (for Nehahra compatibility only)"};
106 cvar_t gl_fogstart = {0, "gl_fogstart", "0", "nehahra fog start distance (for Nehahra compatibility only)"};
107 cvar_t gl_fogend = {0, "gl_fogend","0", "nehahra fog end distance (for Nehahra compatibility only)"};
108 cvar_t gl_skyclip = {0, "gl_skyclip", "4608", "nehahra farclip distance - the real fog end (for Nehahra compatibility only)"};
109
110 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)"};
111
112 cvar_t r_glsl = {CVAR_SAVE, "r_glsl", "1", "enables use of OpenGL 2.0 pixel shaders for lighting"};
113 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)"};
114 cvar_t r_glsl_offsetmapping = {CVAR_SAVE, "r_glsl_offsetmapping", "0", "offset mapping effect (also known as parallax mapping or virtual displacement mapping)"};
115 cvar_t r_glsl_offsetmapping_reliefmapping = {CVAR_SAVE, "r_glsl_offsetmapping_reliefmapping", "0", "relief mapping effect (higher quality)"};
116 cvar_t r_glsl_offsetmapping_scale = {CVAR_SAVE, "r_glsl_offsetmapping_scale", "0.04", "how deep the offset mapping effect is"};
117 cvar_t r_glsl_postprocess = {CVAR_SAVE, "r_glsl_postprocess", "0", "use a GLSL postprocessing shader"};
118 cvar_t r_glsl_postprocess_uservec1 = {CVAR_SAVE, "r_glsl_postprocess_uservec1", "0 0 0 0", "a 4-component vector to pass as uservec1 to the postprocessing shader (only useful if default.glsl has been customized)"};
119 cvar_t r_glsl_postprocess_uservec2 = {CVAR_SAVE, "r_glsl_postprocess_uservec2", "0 0 0 0", "a 4-component vector to pass as uservec2 to the postprocessing shader (only useful if default.glsl has been customized)"};
120 cvar_t r_glsl_postprocess_uservec3 = {CVAR_SAVE, "r_glsl_postprocess_uservec3", "0 0 0 0", "a 4-component vector to pass as uservec3 to the postprocessing shader (only useful if default.glsl has been customized)"};
121 cvar_t r_glsl_postprocess_uservec4 = {CVAR_SAVE, "r_glsl_postprocess_uservec4", "0 0 0 0", "a 4-component vector to pass as uservec4 to the postprocessing shader (only useful if default.glsl has been customized)"};
122 cvar_t r_glsl_usegeneric = {CVAR_SAVE, "r_glsl_usegeneric", "1", "use shaders for rendering simple geometry (rather than conventional fixed-function rendering for this purpose)"};
123
124 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)"};
125 cvar_t r_water_clippingplanebias = {CVAR_SAVE, "r_water_clippingplanebias", "1", "a rather technical setting which avoids black pixels around water edges"};
126 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"};
127 cvar_t r_water_refractdistort = {CVAR_SAVE, "r_water_refractdistort", "0.01", "how much water refractions shimmer"};
128 cvar_t r_water_reflectdistort = {CVAR_SAVE, "r_water_reflectdistort", "0.01", "how much water reflections shimmer"};
129
130 cvar_t r_lerpsprites = {CVAR_SAVE, "r_lerpsprites", "1", "enables animation smoothing on sprites"};
131 cvar_t r_lerpmodels = {CVAR_SAVE, "r_lerpmodels", "1", "enables animation smoothing on models"};
132 cvar_t r_lerplightstyles = {CVAR_SAVE, "r_lerplightstyles", "0", "enable animation smoothing on flickering lights"};
133 cvar_t r_waterscroll = {CVAR_SAVE, "r_waterscroll", "1", "makes water scroll around, value controls how much"};
134
135 cvar_t r_bloom = {CVAR_SAVE, "r_bloom", "0", "enables bloom effect (makes bright pixels affect neighboring pixels)"};
136 cvar_t r_bloom_colorscale = {CVAR_SAVE, "r_bloom_colorscale", "1", "how bright the glow is"};
137 cvar_t r_bloom_brighten = {CVAR_SAVE, "r_bloom_brighten", "2", "how bright the glow is, after subtract/power"};
138 cvar_t r_bloom_blur = {CVAR_SAVE, "r_bloom_blur", "4", "how large the glow is"};
139 cvar_t r_bloom_resolution = {CVAR_SAVE, "r_bloom_resolution", "320", "what resolution to perform the bloom effect at (independent of screen resolution)"};
140 cvar_t r_bloom_colorexponent = {CVAR_SAVE, "r_bloom_colorexponent", "1", "how exagerated the glow is"};
141 cvar_t r_bloom_colorsubtract = {CVAR_SAVE, "r_bloom_colorsubtract", "0.125", "reduces bloom colors by a certain amount"};
142
143 cvar_t r_hdr = {CVAR_SAVE, "r_hdr", "0", "enables High Dynamic Range bloom effect (higher quality version of r_bloom)"};
144 cvar_t r_hdr_scenebrightness = {CVAR_SAVE, "r_hdr_scenebrightness", "1", "global rendering brightness"};
145 cvar_t r_hdr_glowintensity = {CVAR_SAVE, "r_hdr_glowintensity", "1", "how bright light emitting textures should appear"};
146 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)"};
147
148 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"};
149
150 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"};
151
152 cvar_t gl_lightmaps = {0, "gl_lightmaps", "0", "draws only lightmaps, no texture (for level designers)"};
153
154 cvar_t r_test = {0, "r_test", "0", "internal development use only, leave it alone (usually does nothing anyway)"};
155 cvar_t r_batchmode = {0, "r_batchmode", "1", "selects method of rendering multiple surfaces with one driver call (values are 0, 1, 2, etc...)"};
156 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"};
157 cvar_t r_track_sprites_flags = {CVAR_SAVE, "r_track_sprites_flags", "1", "1: Rotate sprites accodringly, 2: Make it a continuous rotation"};
158 cvar_t r_track_sprites_scalew = {CVAR_SAVE, "r_track_sprites_scalew", "1", "width scaling of tracked sprites"};
159 cvar_t r_track_sprites_scaleh = {CVAR_SAVE, "r_track_sprites_scaleh", "1", "height scaling of tracked sprites"};
160 cvar_t r_glsl_saturation = {CVAR_SAVE, "r_glsl_saturation", "1", "saturation multiplier (only working in glsl!)"};
161
162 extern cvar_t v_glslgamma;
163
164 extern qboolean v_flipped_state;
165
166 static struct r_bloomstate_s
167 {
168         qboolean enabled;
169         qboolean hdr;
170
171         int bloomwidth, bloomheight;
172
173         int screentexturewidth, screentextureheight;
174         rtexture_t *texture_screen; /// \note also used for motion blur if enabled!
175
176         int bloomtexturewidth, bloomtextureheight;
177         rtexture_t *texture_bloom;
178
179         // arrays for rendering the screen passes
180         float screentexcoord2f[8];
181         float bloomtexcoord2f[8];
182         float offsettexcoord2f[8];
183
184         r_viewport_t viewport;
185 }
186 r_bloomstate;
187
188 r_waterstate_t r_waterstate;
189
190 /// shadow volume bsp struct with automatically growing nodes buffer
191 svbsp_t r_svbsp;
192
193 rtexture_t *r_texture_blanknormalmap;
194 rtexture_t *r_texture_white;
195 rtexture_t *r_texture_grey128;
196 rtexture_t *r_texture_black;
197 rtexture_t *r_texture_notexture;
198 rtexture_t *r_texture_whitecube;
199 rtexture_t *r_texture_normalizationcube;
200 rtexture_t *r_texture_fogattenuation;
201 rtexture_t *r_texture_gammaramps;
202 unsigned int r_texture_gammaramps_serial;
203 //rtexture_t *r_texture_fogintensity;
204
205 unsigned int r_queries[MAX_OCCLUSION_QUERIES];
206 unsigned int r_numqueries;
207 unsigned int r_maxqueries;
208
209 typedef struct r_qwskincache_s
210 {
211         char name[MAX_QPATH];
212         skinframe_t *skinframe;
213 }
214 r_qwskincache_t;
215
216 static r_qwskincache_t *r_qwskincache;
217 static int r_qwskincache_size;
218
219 /// vertex coordinates for a quad that covers the screen exactly
220 const float r_screenvertex3f[12] =
221 {
222         0, 0, 0,
223         1, 0, 0,
224         1, 1, 0,
225         0, 1, 0
226 };
227
228 extern void R_DrawModelShadows(void);
229
230 void R_ModulateColors(float *in, float *out, int verts, float r, float g, float b)
231 {
232         int i;
233         for (i = 0;i < verts;i++)
234         {
235                 out[0] = in[0] * r;
236                 out[1] = in[1] * g;
237                 out[2] = in[2] * b;
238                 out[3] = in[3];
239                 in += 4;
240                 out += 4;
241         }
242 }
243
244 void R_FillColors(float *out, int verts, float r, float g, float b, float a)
245 {
246         int i;
247         for (i = 0;i < verts;i++)
248         {
249                 out[0] = r;
250                 out[1] = g;
251                 out[2] = b;
252                 out[3] = a;
253                 out += 4;
254         }
255 }
256
257 // FIXME: move this to client?
258 void FOG_clear(void)
259 {
260         if (gamemode == GAME_NEHAHRA)
261         {
262                 Cvar_Set("gl_fogenable", "0");
263                 Cvar_Set("gl_fogdensity", "0.2");
264                 Cvar_Set("gl_fogred", "0.3");
265                 Cvar_Set("gl_foggreen", "0.3");
266                 Cvar_Set("gl_fogblue", "0.3");
267         }
268         r_refdef.fog_density = 0;
269         r_refdef.fog_red = 0;
270         r_refdef.fog_green = 0;
271         r_refdef.fog_blue = 0;
272         r_refdef.fog_alpha = 1;
273         r_refdef.fog_start = 0;
274         r_refdef.fog_end = 16384;
275         r_refdef.fog_height = 1<<30;
276         r_refdef.fog_fadedepth = 128;
277 }
278
279 static void R_BuildBlankTextures(void)
280 {
281         unsigned char data[4];
282         data[2] = 128; // normal X
283         data[1] = 128; // normal Y
284         data[0] = 255; // normal Z
285         data[3] = 128; // height
286         r_texture_blanknormalmap = R_LoadTexture2D(r_main_texturepool, "blankbump", 1, 1, data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_PERSISTENT, NULL);
287         data[0] = 255;
288         data[1] = 255;
289         data[2] = 255;
290         data[3] = 255;
291         r_texture_white = R_LoadTexture2D(r_main_texturepool, "blankwhite", 1, 1, data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_PERSISTENT, NULL);
292         data[0] = 128;
293         data[1] = 128;
294         data[2] = 128;
295         data[3] = 255;
296         r_texture_grey128 = R_LoadTexture2D(r_main_texturepool, "blankgrey128", 1, 1, data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_PERSISTENT, NULL);
297         data[0] = 0;
298         data[1] = 0;
299         data[2] = 0;
300         data[3] = 255;
301         r_texture_black = R_LoadTexture2D(r_main_texturepool, "blankblack", 1, 1, data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_PERSISTENT, NULL);
302 }
303
304 static void R_BuildNoTexture(void)
305 {
306         int x, y;
307         unsigned char pix[16][16][4];
308         // this makes a light grey/dark grey checkerboard texture
309         for (y = 0;y < 16;y++)
310         {
311                 for (x = 0;x < 16;x++)
312                 {
313                         if ((y < 8) ^ (x < 8))
314                         {
315                                 pix[y][x][0] = 128;
316                                 pix[y][x][1] = 128;
317                                 pix[y][x][2] = 128;
318                                 pix[y][x][3] = 255;
319                         }
320                         else
321                         {
322                                 pix[y][x][0] = 64;
323                                 pix[y][x][1] = 64;
324                                 pix[y][x][2] = 64;
325                                 pix[y][x][3] = 255;
326                         }
327                 }
328         }
329         r_texture_notexture = R_LoadTexture2D(r_main_texturepool, "notexture", 16, 16, &pix[0][0][0], TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_MIPMAP | TEXF_PERSISTENT, NULL);
330 }
331
332 static void R_BuildWhiteCube(void)
333 {
334         unsigned char data[6*1*1*4];
335         memset(data, 255, sizeof(data));
336         r_texture_whitecube = R_LoadTextureCubeMap(r_main_texturepool, "whitecube", 1, data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_PERSISTENT, NULL);
337 }
338
339 static void R_BuildNormalizationCube(void)
340 {
341         int x, y, side;
342         vec3_t v;
343         vec_t s, t, intensity;
344 #define NORMSIZE 64
345         unsigned char data[6][NORMSIZE][NORMSIZE][4];
346         for (side = 0;side < 6;side++)
347         {
348                 for (y = 0;y < NORMSIZE;y++)
349                 {
350                         for (x = 0;x < NORMSIZE;x++)
351                         {
352                                 s = (x + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
353                                 t = (y + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
354                                 switch(side)
355                                 {
356                                 default:
357                                 case 0:
358                                         v[0] = 1;
359                                         v[1] = -t;
360                                         v[2] = -s;
361                                         break;
362                                 case 1:
363                                         v[0] = -1;
364                                         v[1] = -t;
365                                         v[2] = s;
366                                         break;
367                                 case 2:
368                                         v[0] = s;
369                                         v[1] = 1;
370                                         v[2] = t;
371                                         break;
372                                 case 3:
373                                         v[0] = s;
374                                         v[1] = -1;
375                                         v[2] = -t;
376                                         break;
377                                 case 4:
378                                         v[0] = s;
379                                         v[1] = -t;
380                                         v[2] = 1;
381                                         break;
382                                 case 5:
383                                         v[0] = -s;
384                                         v[1] = -t;
385                                         v[2] = -1;
386                                         break;
387                                 }
388                                 intensity = 127.0f / sqrt(DotProduct(v, v));
389                                 data[side][y][x][2] = (unsigned char)(128.0f + intensity * v[0]);
390                                 data[side][y][x][1] = (unsigned char)(128.0f + intensity * v[1]);
391                                 data[side][y][x][0] = (unsigned char)(128.0f + intensity * v[2]);
392                                 data[side][y][x][3] = 255;
393                         }
394                 }
395         }
396         r_texture_normalizationcube = R_LoadTextureCubeMap(r_main_texturepool, "normalcube", NORMSIZE, &data[0][0][0][0], TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_PERSISTENT, NULL);
397 }
398
399 static void R_BuildFogTexture(void)
400 {
401         int x, b;
402 #define FOGWIDTH 256
403         unsigned char data1[FOGWIDTH][4];
404         //unsigned char data2[FOGWIDTH][4];
405         double d, r, alpha;
406
407         r_refdef.fogmasktable_start = r_refdef.fog_start;
408         r_refdef.fogmasktable_alpha = r_refdef.fog_alpha;
409         r_refdef.fogmasktable_range = r_refdef.fogrange;
410         r_refdef.fogmasktable_density = r_refdef.fog_density;
411
412         r = r_refdef.fogmasktable_range / FOGMASKTABLEWIDTH;
413         for (x = 0;x < FOGMASKTABLEWIDTH;x++)
414         {
415                 d = (x * r - r_refdef.fogmasktable_start);
416                 if(developer.integer >= 100)
417                         Con_Printf("%f ", d);
418                 d = max(0, d);
419                 if (r_fog_exp2.integer)
420                         alpha = exp(-r_refdef.fogmasktable_density * r_refdef.fogmasktable_density * 0.0001 * d * d);
421                 else
422                         alpha = exp(-r_refdef.fogmasktable_density * 0.004 * d);
423                 if(developer.integer >= 100)
424                         Con_Printf(" : %f ", alpha);
425                 alpha = 1 - (1 - alpha) * r_refdef.fogmasktable_alpha;
426                 if(developer.integer >= 100)
427                         Con_Printf(" = %f\n", alpha);
428                 r_refdef.fogmasktable[x] = bound(0, alpha, 1);
429         }
430
431         for (x = 0;x < FOGWIDTH;x++)
432         {
433                 b = (int)(r_refdef.fogmasktable[x * (FOGMASKTABLEWIDTH - 1) / (FOGWIDTH - 1)] * 255);
434                 data1[x][0] = b;
435                 data1[x][1] = b;
436                 data1[x][2] = b;
437                 data1[x][3] = 255;
438                 //data2[x][0] = 255 - b;
439                 //data2[x][1] = 255 - b;
440                 //data2[x][2] = 255 - b;
441                 //data2[x][3] = 255;
442         }
443         if (r_texture_fogattenuation)
444         {
445                 R_UpdateTexture(r_texture_fogattenuation, &data1[0][0], 0, 0, FOGWIDTH, 1);
446                 //R_UpdateTexture(r_texture_fogattenuation, &data2[0][0], 0, 0, FOGWIDTH, 1);
447         }
448         else
449         {
450                 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);
451                 //r_texture_fogintensity = R_LoadTexture2D(r_main_texturepool, "fogintensity", FOGWIDTH, 1, &data2[0][0], TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_FORCELINEAR | TEXF_CLAMP, NULL);
452         }
453 }
454
455 static const char *builtinshaderstring =
456 "// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader\n"
457 "// written by Forest 'LordHavoc' Hale\n"
458 "\n"
459 "// enable various extensions depending on permutation:\n"
460 "\n"
461 "#ifdef USESHADOWMAPRECT\n"
462 "# extension GL_ARB_texture_rectangle : enable\n"
463 "#endif\n"
464 "\n"
465 "#ifdef USESHADOWMAP2D\n"
466 "# ifdef GL_EXT_gpu_shader4\n"
467 "#   extension GL_EXT_gpu_shader4 : enable\n"
468 "# endif\n"
469 "# ifdef GL_ARB_texture_gather\n"
470 "#   extension GL_ARB_texture_gather : enable\n"
471 "# else\n"
472 "#   ifdef GL_AMD_texture_texture4\n"
473 "#     extension GL_AMD_texture_texture4 : enable\n"
474 "#   endif\n"
475 "# endif\n"
476 "#endif\n"
477 "\n"
478 "#ifdef USESHADOWMAPCUBE\n"
479 "# extension GL_EXT_gpu_shader4 : enable\n"
480 "#endif\n"
481 "\n"
482 "#ifdef USESHADOWSAMPLER\n"
483 "# extension GL_ARB_shadow : enable\n"
484 "#endif\n"
485 "\n"
486 "// common definitions between vertex shader and fragment shader:\n"
487 "\n"
488 "//#ifdef __GLSL_CG_DATA_TYPES\n"
489 "//# define myhalf half\n"
490 "//# define myhalf2 half2\n"
491 "//# define myhalf3half3\n"
492 "//# define myhalf4 half4\n"
493 "//#else\n"
494 "# define myhalf float\n"
495 "# define myhalf2 vec2\n"
496 "# define myhalf3 vec3\n"
497 "# define myhalf4 vec4\n"
498 "//#endif\n"
499 "\n"
500 "#ifdef USEFOGINSIDE\n"
501 "# define USEFOG\n"
502 "#else\n"
503 "# ifdef USEFOGOUTSIDE\n"
504 "#  define USEFOG\n"
505 "# endif\n"
506 "#endif\n"
507 "\n"
508 "#ifdef MODE_DEPTH_OR_SHADOW\n"
509 "\n"
510 "# ifdef VERTEX_SHADER\n"
511 "void main(void)\n"
512 "{\n"
513 "       gl_Position = ftransform();\n"
514 "}\n"
515 "# endif\n"
516 "\n"
517 "#else\n"
518 "#ifdef MODE_SHOWDEPTH\n"
519 "# ifdef VERTEX_SHADER\n"
520 "void main(void)\n"
521 "{\n"
522 "       gl_Position = ftransform();\n"
523 "       gl_FrontColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0);\n"
524 "}\n"
525 "# endif\n"
526 "# ifdef FRAGMENT_SHADER\n"
527 "void main(void)\n"
528 "{\n"
529 "       gl_FragColor = gl_Color;\n"
530 "}\n"
531 "# endif\n"
532 "\n"
533 "#else // !MODE_SHOWDEPTH\n"
534 "\n"
535 "#ifdef MODE_POSTPROCESS\n"
536 "# ifdef VERTEX_SHADER\n"
537 "void main(void)\n"
538 "{\n"
539 "       gl_FrontColor = gl_Color;\n"
540 "       gl_Position = ftransform();\n"
541 "       gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;\n"
542 "#ifdef USEBLOOM\n"
543 "       gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1;\n"
544 "#endif\n"
545 "}\n"
546 "# endif\n"
547 "# ifdef FRAGMENT_SHADER\n"
548 "\n"
549 "uniform sampler2D Texture_First;\n"
550 "#ifdef USEBLOOM\n"
551 "uniform sampler2D Texture_Second;\n"
552 "#endif\n"
553 "#ifdef USEGAMMARAMPS\n"
554 "uniform sampler2D Texture_GammaRamps;\n"
555 "#endif\n"
556 "#ifdef USESATURATION\n"
557 "uniform float Saturation;\n"
558 "#endif\n"
559 "#ifdef USEVIEWTINT\n"
560 "uniform vec4 TintColor;\n"
561 "#endif\n"
562 "//uncomment these if you want to use them:\n"
563 "uniform vec4 UserVec1;\n"
564 "// uniform vec4 UserVec2;\n"
565 "// uniform vec4 UserVec3;\n"
566 "// uniform vec4 UserVec4;\n"
567 "// uniform float ClientTime;\n"
568 "uniform vec2 PixelSize;\n"
569 "void main(void)\n"
570 "{\n"
571 "       gl_FragColor = texture2D(Texture_First, gl_TexCoord[0].xy);\n"
572 "#ifdef USEBLOOM\n"
573 "       gl_FragColor += texture2D(Texture_Second, gl_TexCoord[1].xy);\n"
574 "#endif\n"
575 "#ifdef USEVIEWTINT\n"
576 "       gl_FragColor = mix(gl_FragColor, TintColor, TintColor.a);\n"
577 "#endif\n"
578 "\n"
579 "#ifdef USEPOSTPROCESSING\n"
580 "// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want\n"
581 "// this code does a blur with the radius specified in the first component of r_glsl_postprocess_uservec1 and blends it using the second component\n"
582 "       gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y;\n"
583 "       gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y;\n"
584 "       gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y;\n"
585 "       gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2( 0.707107,  0.707107)) * UserVec1.y;\n"
586 "       gl_FragColor += texture2D(Texture_First, gl_TexCoord[0].xy + PixelSize*UserVec1.x*vec2(-0.453990,  0.891007)) * UserVec1.y;\n"
587 "       gl_FragColor /= (1 + 5 * UserVec1.y);\n"
588 "#endif\n"
589 "\n"
590 "#ifdef USESATURATION\n"
591 "       //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter\n"
592 "       myhalf y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114));\n"
593 "       //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation;\n"
594 "       gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation);\n"
595 "#endif\n"
596 "\n"
597 "#ifdef USEGAMMARAMPS\n"
598 "       gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r;\n"
599 "       gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g;\n"
600 "       gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b;\n"
601 "#endif\n"
602 "}\n"
603 "# endif\n"
604 "\n"
605 "\n"
606 "#else\n"
607 "#ifdef MODE_GENERIC\n"
608 "# ifdef VERTEX_SHADER\n"
609 "void main(void)\n"
610 "{\n"
611 "       gl_FrontColor = gl_Color;\n"
612 "#  ifdef USEDIFFUSE\n"
613 "       gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;\n"
614 "#  endif\n"
615 "#  ifdef USESPECULAR\n"
616 "       gl_TexCoord[1] = gl_TextureMatrix[1] * gl_MultiTexCoord1;\n"
617 "#  endif\n"
618 "       gl_Position = ftransform();\n"
619 "}\n"
620 "# endif\n"
621 "# ifdef FRAGMENT_SHADER\n"
622 "\n"
623 "#  ifdef USEDIFFUSE\n"
624 "uniform sampler2D Texture_First;\n"
625 "#  endif\n"
626 "#  ifdef USESPECULAR\n"
627 "uniform sampler2D Texture_Second;\n"
628 "#  endif\n"
629 "\n"
630 "void main(void)\n"
631 "{\n"
632 "       gl_FragColor = gl_Color;\n"
633 "#  ifdef USEDIFFUSE\n"
634 "       gl_FragColor *= texture2D(Texture_First, gl_TexCoord[0].xy);\n"
635 "#  endif\n"
636 "\n"
637 "#  ifdef USESPECULAR\n"
638 "       vec4 tex2 = texture2D(Texture_Second, gl_TexCoord[1].xy);\n"
639 "#  endif\n"
640 "#  ifdef USECOLORMAPPING\n"
641 "       gl_FragColor *= tex2;\n"
642 "#  endif\n"
643 "#  ifdef USEGLOW\n"
644 "       gl_FragColor += tex2;\n"
645 "#  endif\n"
646 "#  ifdef USEVERTEXTEXTUREBLEND\n"
647 "       gl_FragColor = mix(gl_FragColor, tex2, tex2.a);\n"
648 "#  endif\n"
649 "}\n"
650 "# endif\n"
651 "\n"
652 "#else // !MODE_GENERIC\n"
653 "#ifdef MODE_BLOOMBLUR\n"
654 "# ifdef VERTEX_SHADER\n"
655 "void main(void)\n"
656 "{\n"
657 "       gl_FrontColor = gl_Color;\n"
658 "       gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;\n"
659 "       gl_Position = ftransform();\n"
660 "}\n"
661 "# endif\n"
662 "# ifdef FRAGMENT_SHADER\n"
663 "\n"
664 "uniform sampler2D Texture_First;\n"
665 "uniform vec4 BloomBlur_Parameters;\n"
666 "\n"
667 "void main(void)\n"
668 "{\n"
669 "       int i;\n"
670 "       vec2 tc = gl_TexCoord[0].xy;\n"
671 "       vec3 color = texture2D(Texture_First, tc).rgb;\n"
672 "       tc += BloomBlur_Parameters.xy;\n"
673 "       for (i = 1;i < SAMPLES;i++)\n"
674 "       {\n"
675 "               color += texture2D(Texture_First, tc).rgb;\n"
676 "               tc += BloomBlur_Parameters.xy;\n"
677 "       }\n"
678 "       gl_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1);\n"
679 "}\n"
680 "# endif\n"
681 "\n"
682 "#else // !MODE_BLOOMBLUR\n"
683 "\n"
684 "varying vec2 TexCoord;\n"
685 "#ifdef USEVERTEXTEXTUREBLEND\n"
686 "varying vec2 TexCoord2;\n"
687 "#endif\n"
688 "varying vec2 TexCoordLightmap;\n"
689 "\n"
690 "#ifdef MODE_LIGHTSOURCE\n"
691 "varying vec3 CubeVector;\n"
692 "#endif\n"
693 "\n"
694 "#ifdef MODE_LIGHTSOURCE\n"
695 "varying vec3 LightVector;\n"
696 "#endif\n"
697 "#ifdef MODE_LIGHTDIRECTION\n"
698 "varying vec3 LightVector;\n"
699 "#endif\n"
700 "\n"
701 "varying vec3 EyeVector;\n"
702 "#ifdef USEFOG\n"
703 "varying vec3 EyeVectorModelSpace;\n"
704 "varying float FogPlaneVertexDist;\n"
705 "#endif\n"
706 "\n"
707 "varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent)\n"
708 "varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal)\n"
709 "varying vec3 VectorR; // direction of R texcoord (surface normal)\n"
710 "\n"
711 "#ifdef MODE_WATER\n"
712 "varying vec4 ModelViewProjectionPosition;\n"
713 "#endif\n"
714 "#ifdef MODE_REFRACTION\n"
715 "varying vec4 ModelViewProjectionPosition;\n"
716 "#endif\n"
717 "#ifdef USEREFLECTION\n"
718 "varying vec4 ModelViewProjectionPosition;\n"
719 "#endif\n"
720 "\n"
721 "\n"
722 "\n"
723 "\n"
724 "\n"
725 "// vertex shader specific:\n"
726 "#ifdef VERTEX_SHADER\n"
727 "\n"
728 "uniform vec3 LightPosition;\n"
729 "uniform vec3 EyePosition;\n"
730 "uniform vec3 LightDir;\n"
731 "uniform vec4 FogPlane;\n"
732 "\n"
733 "// TODO: get rid of tangentt (texcoord2) and use a crossproduct to regenerate it from tangents (texcoord1) and normal (texcoord3), this would require sending a 4 component texcoord1 with W as 1 or -1 according to which side the texcoord2 should be on\n"
734 "\n"
735 "void main(void)\n"
736 "{\n"
737 "       gl_FrontColor = gl_Color;\n"
738 "       // copy the surface texcoord\n"
739 "       TexCoord = vec2(gl_TextureMatrix[0] * gl_MultiTexCoord0);\n"
740 "#ifdef USEVERTEXTEXTUREBLEND\n"
741 "       TexCoord2 = vec2(gl_TextureMatrix[1] * gl_MultiTexCoord0);\n"
742 "#endif\n"
743 "#ifndef MODE_LIGHTSOURCE\n"
744 "# ifndef MODE_LIGHTDIRECTION\n"
745 "       TexCoordLightmap = vec2(gl_MultiTexCoord4);\n"
746 "# endif\n"
747 "#endif\n"
748 "\n"
749 "#ifdef MODE_LIGHTSOURCE\n"
750 "       // transform vertex position into light attenuation/cubemap space\n"
751 "       // (-1 to +1 across the light box)\n"
752 "       CubeVector = vec3(gl_TextureMatrix[3] * gl_Vertex);\n"
753 "\n"
754 "       // transform unnormalized light direction into tangent space\n"
755 "       // (we use unnormalized to ensure that it interpolates correctly and then\n"
756 "       //  normalize it per pixel)\n"
757 "       vec3 lightminusvertex = LightPosition - gl_Vertex.xyz;\n"
758 "       LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz);\n"
759 "       LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz);\n"
760 "       LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz);\n"
761 "#endif\n"
762 "\n"
763 "#ifdef MODE_LIGHTDIRECTION\n"
764 "       LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz);\n"
765 "       LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz);\n"
766 "       LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz);\n"
767 "#endif\n"
768 "\n"
769 "       // transform unnormalized eye direction into tangent space\n"
770 "#ifndef USEFOG\n"
771 "       vec3 EyeVectorModelSpace;\n"
772 "#endif\n"
773 "       EyeVectorModelSpace = EyePosition - gl_Vertex.xyz;\n"
774 "       EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz);\n"
775 "       EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz);\n"
776 "       EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz);\n"
777 "\n"
778 "#ifdef USEFOG\n"
779 "       FogPlaneVertexDist = dot(FogPlane, gl_Vertex);\n"
780 "#endif\n"
781 "\n"
782 "#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
783 "       VectorS = gl_MultiTexCoord1.xyz;\n"
784 "       VectorT = gl_MultiTexCoord2.xyz;\n"
785 "       VectorR = gl_MultiTexCoord3.xyz;\n"
786 "#endif\n"
787 "\n"
788 "//#if defined(MODE_WATER) || defined(MODE_REFRACTION) || defined(USEREFLECTION)\n"
789 "//     ModelViewProjectionPosition = gl_Vertex * gl_ModelViewProjectionMatrix;\n"
790 "//     //ModelViewProjectionPosition_svector = (gl_Vertex + vec4(gl_MultiTexCoord1.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition;\n"
791 "//     //ModelViewProjectionPosition_tvector = (gl_Vertex + vec4(gl_MultiTexCoord2.xyz, 0)) * gl_ModelViewProjectionMatrix - ModelViewProjectionPosition;\n"
792 "//#endif\n"
793 "\n"
794 "// transform vertex to camera space, using ftransform to match non-VS\n"
795 "       // rendering\n"
796 "       gl_Position = ftransform();\n"
797 "\n"
798 "#ifdef MODE_WATER\n"
799 "       ModelViewProjectionPosition = gl_Position;\n"
800 "#endif\n"
801 "#ifdef MODE_REFRACTION\n"
802 "       ModelViewProjectionPosition = gl_Position;\n"
803 "#endif\n"
804 "#ifdef USEREFLECTION\n"
805 "       ModelViewProjectionPosition = gl_Position;\n"
806 "#endif\n"
807 "}\n"
808 "\n"
809 "#endif // VERTEX_SHADER\n"
810 "\n"
811 "\n"
812 "\n"
813 "\n"
814 "// fragment shader specific:\n"
815 "#ifdef FRAGMENT_SHADER\n"
816 "\n"
817 "// 13 textures, we can only use up to 16 on DX9-class hardware\n"
818 "uniform sampler2D Texture_Normal;\n"
819 "uniform sampler2D Texture_Color;\n"
820 "uniform sampler2D Texture_Gloss;\n"
821 "uniform sampler2D Texture_Glow;\n"
822 "uniform sampler2D Texture_SecondaryNormal;\n"
823 "uniform sampler2D Texture_SecondaryColor;\n"
824 "uniform sampler2D Texture_SecondaryGloss;\n"
825 "uniform sampler2D Texture_SecondaryGlow;\n"
826 "uniform sampler2D Texture_Pants;\n"
827 "uniform sampler2D Texture_Shirt;\n"
828 "uniform sampler2D Texture_FogMask;\n"
829 "uniform sampler2D Texture_Lightmap;\n"
830 "uniform sampler2D Texture_Deluxemap;\n"
831 "uniform sampler2D Texture_Refraction;\n"
832 "uniform sampler2D Texture_Reflection;\n"
833 "uniform sampler2D Texture_Attenuation;\n"
834 "uniform samplerCube Texture_Cube;\n"
835 "\n"
836 "#define showshadowmap 0\n"
837 "\n"
838 "#ifdef USESHADOWMAPRECT\n"
839 "# ifdef USESHADOWSAMPLER\n"
840 "uniform sampler2DRectShadow Texture_ShadowMapRect;\n"
841 "# else\n"
842 "uniform sampler2DRect Texture_ShadowMapRect;\n"
843 "# endif\n"
844 "#endif\n"
845 "\n"
846 "#ifdef USESHADOWMAP2D\n"
847 "# ifdef USESHADOWSAMPLER\n"
848 "uniform sampler2DShadow Texture_ShadowMap2D;\n"
849 "# else\n"
850 "uniform sampler2D Texture_ShadowMap2D;\n"
851 "# endif\n"
852 "#endif\n"
853 "\n"
854 "#ifdef USESHADOWMAPVSDCT\n"
855 "uniform samplerCube Texture_CubeProjection;\n"
856 "#endif\n"
857 "\n"
858 "#ifdef USESHADOWMAPCUBE\n"
859 "# ifdef USESHADOWSAMPLER\n"
860 "uniform samplerCubeShadow Texture_ShadowMapCube;\n"
861 "# else\n"
862 "uniform samplerCube Texture_ShadowMapCube;\n"
863 "# endif\n"
864 "#endif\n"
865 "\n"
866 "uniform myhalf3 LightColor;\n"
867 "uniform myhalf3 AmbientColor;\n"
868 "uniform myhalf3 DiffuseColor;\n"
869 "uniform myhalf3 SpecularColor;\n"
870 "uniform myhalf3 Color_Pants;\n"
871 "uniform myhalf3 Color_Shirt;\n"
872 "uniform myhalf3 FogColor;\n"
873 "\n"
874 "uniform myhalf4 TintColor;\n"
875 "\n"
876 "\n"
877 "//#ifdef MODE_WATER\n"
878 "uniform vec4 DistortScaleRefractReflect;\n"
879 "uniform vec4 ScreenScaleRefractReflect;\n"
880 "uniform vec4 ScreenCenterRefractReflect;\n"
881 "uniform myhalf4 RefractColor;\n"
882 "uniform myhalf4 ReflectColor;\n"
883 "uniform myhalf ReflectFactor;\n"
884 "uniform myhalf ReflectOffset;\n"
885 "//#else\n"
886 "//# ifdef MODE_REFRACTION\n"
887 "//uniform vec4 DistortScaleRefractReflect;\n"
888 "//uniform vec4 ScreenScaleRefractReflect;\n"
889 "//uniform vec4 ScreenCenterRefractReflect;\n"
890 "//uniform myhalf4 RefractColor;\n"
891 "//#  ifdef USEREFLECTION\n"
892 "//uniform myhalf4 ReflectColor;\n"
893 "//#  endif\n"
894 "//# else\n"
895 "//#  ifdef USEREFLECTION\n"
896 "//uniform vec4 DistortScaleRefractReflect;\n"
897 "//uniform vec4 ScreenScaleRefractReflect;\n"
898 "//uniform vec4 ScreenCenterRefractReflect;\n"
899 "//uniform myhalf4 ReflectColor;\n"
900 "//#  endif\n"
901 "//# endif\n"
902 "//#endif\n"
903 "\n"
904 "uniform myhalf3 GlowColor;\n"
905 "uniform myhalf SceneBrightness;\n"
906 "\n"
907 "uniform float OffsetMapping_Scale;\n"
908 "uniform float OffsetMapping_Bias;\n"
909 "uniform float FogRangeRecip;\n"
910 "uniform float FogPlaneViewDist;\n"
911 "uniform float FogHeightFade;\n"
912 "\n"
913 "uniform myhalf AmbientScale;\n"
914 "uniform myhalf DiffuseScale;\n"
915 "uniform myhalf SpecularScale;\n"
916 "uniform myhalf SpecularPower;\n"
917 "\n"
918 "#ifdef USEOFFSETMAPPING\n"
919 "vec2 OffsetMapping(vec2 TexCoord)\n"
920 "{\n"
921 "#ifdef USEOFFSETMAPPING_RELIEFMAPPING\n"
922 "       // 14 sample relief mapping: linear search and then binary search\n"
923 "       // this basically steps forward a small amount repeatedly until it finds\n"
924 "       // itself inside solid, then jitters forward and back using decreasing\n"
925 "       // amounts to find the impact\n"
926 "       //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1);\n"
927 "       //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1);\n"
928 "       vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1);\n"
929 "       vec3 RT = vec3(TexCoord, 1);\n"
930 "       OffsetVector *= 0.1;\n"
931 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
932 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
933 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
934 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
935 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
936 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
937 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
938 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
939 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
940 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z)          - 0.5);\n"
941 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5    - 0.25);\n"
942 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25   - 0.125);\n"
943 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125  - 0.0625);\n"
944 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125);\n"
945 "       return RT.xy;\n"
946 "#else\n"
947 "       // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits)\n"
948 "       // this basically moves forward the full distance, and then backs up based\n"
949 "       // on height of samples\n"
950 "       //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1));\n"
951 "       //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1));\n"
952 "       vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1));\n"
953 "       TexCoord += OffsetVector;\n"
954 "       OffsetVector *= 0.333;\n"
955 "       TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
956 "       TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
957 "       TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
958 "       return TexCoord;\n"
959 "#endif\n"
960 "}\n"
961 "#endif // USEOFFSETMAPPING\n"
962 "\n"
963 "#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE)\n"
964 "uniform vec2 ShadowMap_TextureScale;\n"
965 "uniform vec4 ShadowMap_Parameters;\n"
966 "#endif\n"
967 "\n"
968 "#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D)\n"
969 "vec3 GetShadowMapTC2D(vec3 dir)\n"
970 "{\n"
971 "       vec3 adir = abs(dir);\n"
972 "# ifndef USESHADOWMAPVSDCT\n"
973 "       vec2 tc;\n"
974 "       vec2 offset;\n"
975 "       float ma;\n"
976 "       if (adir.x > adir.y)\n"
977 "       {\n"
978 "               if (adir.x > adir.z) // X\n"
979 "               {\n"
980 "                       ma = adir.x;\n"
981 "                       tc = dir.zy;\n"
982 "                       offset = vec2(mix(0.5, 1.5, dir.x < 0.0), 0.5);\n"
983 "               }\n"
984 "               else // Z\n"
985 "               {\n"
986 "                       ma = adir.z;\n"
987 "                       tc = dir.xy;\n"
988 "                       offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5);\n"
989 "               }\n"
990 "       }\n"
991 "       else\n"
992 "       {\n"
993 "               if (adir.y > adir.z) // Y\n"
994 "               {\n"
995 "                       ma = adir.y;\n"
996 "                       tc = dir.xz;\n"
997 "                       offset = vec2(mix(0.5, 1.5, dir.y < 0.0), 1.5);\n"
998 "               }\n"
999 "               else // Z\n"
1000 "               {\n"
1001 "                       ma = adir.z;\n"
1002 "                       tc = dir.xy;\n"
1003 "                       offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5);\n"
1004 "               }\n"
1005 "       }\n"
1006 "\n"
1007 "       vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma;\n"
1008 "       stc.xy += offset * ShadowMap_Parameters.y;\n"
1009 "       stc.z += ShadowMap_Parameters.z;\n"
1010 "#  if showshadowmap\n"
1011 "       stc.xy *= ShadowMap_TextureScale;\n"
1012 "#  endif\n"
1013 "       return stc;\n"
1014 "# else\n"
1015 "       vec4 proj = textureCube(Texture_CubeProjection, dir);\n"
1016 "       float ma = max(max(adir.x, adir.y), adir.z);\n"
1017 "       vec3 stc = vec3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma;\n"
1018 "       stc.xy += proj.zw * ShadowMap_Parameters.y;\n"
1019 "       stc.z += ShadowMap_Parameters.z;\n"
1020 "#  if showshadowmap\n"
1021 "       stc.xy *= ShadowMap_TextureScale;\n"
1022 "#  endif\n"
1023 "       return stc;\n"
1024 "# endif\n"
1025 "}\n"
1026 "#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D)\n"
1027 "\n"
1028 "#ifdef USESHADOWMAPCUBE\n"
1029 "vec4 GetShadowMapTCCube(vec3 dir)\n"
1030 "{\n"
1031 "    vec3 adir = abs(dir);\n"
1032 "    return vec4(dir, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z));\n"
1033 "}\n"
1034 "#endif\n"
1035 "\n"
1036 "#if !showshadowmap\n"
1037 "# ifdef USESHADOWMAPRECT\n"
1038 "float ShadowMapCompare(vec3 dir)\n"
1039 "{\n"
1040 "       vec3 shadowmaptc = GetShadowMapTC2D(dir);\n"
1041 "       float f;\n"
1042 "#  ifdef USESHADOWSAMPLER\n"
1043 "\n"
1044 "#    ifdef USESHADOWMAPPCF\n"
1045 "#      define texval(x, y) shadow2DRect(Texture_ShadowMapRect, shadowmaptc + vec3(x, y, 0.0)).r\n"
1046 "    f = dot(vec4(0.25), vec4(texval(-0.4, 1.0), texval(-1.0, -0.4), texval(0.4, -1.0), texval(1.0, 0.4)));\n"
1047 "#    else\n"
1048 "    f = shadow2DRect(Texture_ShadowMapRect, shadowmaptc).r;\n"
1049 "#    endif\n"
1050 "\n"
1051 "#  else\n"
1052 "\n"
1053 "#    ifdef USESHADOWMAPPCF\n"
1054 "#      if USESHADOWMAPPCF > 1\n"
1055 "#        define texval(x, y) texture2DRect(Texture_ShadowMapRect, center + vec2(x, y)).r\n"
1056 "    vec2 center = shadowmaptc.xy - 0.5, offset = fract(center);\n"
1057 "    vec4 row1 = step(shadowmaptc.z, vec4(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0), texval( 2.0, -1.0)));\n"
1058 "    vec4 row2 = step(shadowmaptc.z, vec4(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0), texval( 2.0,  0.0)));\n"
1059 "    vec4 row3 = step(shadowmaptc.z, vec4(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0), texval( 2.0,  1.0)));\n"
1060 "    vec4 row4 = step(shadowmaptc.z, vec4(texval(-1.0,  2.0), texval( 0.0,  2.0), texval( 1.0,  2.0), texval( 2.0,  2.0)));\n"
1061 "    vec4 cols = row2 + row3 + mix(row1, row4, offset.y);\n"
1062 "    f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0));\n"
1063 "#      else\n"
1064 "#        define texval(x, y) texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy + vec2(x, y)).r\n"
1065 "    vec2 offset = fract(shadowmaptc.xy);\n"
1066 "    vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0)));\n"
1067 "    vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0)));\n"
1068 "    vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0)));\n"
1069 "    vec3 cols = row2 + mix(row1, row3, offset.y);\n"
1070 "    f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25));\n"
1071 "#      endif\n"
1072 "#    else\n"
1073 "    f = step(shadowmaptc.z, texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy).r);\n"
1074 "#    endif\n"
1075 "\n"
1076 "#  endif\n"
1077 "       return f;\n"
1078 "}\n"
1079 "# endif\n"
1080 "\n"
1081 "# ifdef USESHADOWMAP2D\n"
1082 "float ShadowMapCompare(vec3 dir)\n"
1083 "{\n"
1084 "    vec3 shadowmaptc = GetShadowMapTC2D(dir);\n"
1085 "    float f;\n"
1086 "\n"
1087 "#  ifdef USESHADOWSAMPLER\n"
1088 "#    ifdef USESHADOWMAPPCF\n"
1089 "#      define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r  \n"
1090 "    vec2 center = shadowmaptc.xy*ShadowMap_TextureScale;\n"
1091 "    f = dot(vec4(0.25), vec4(texval(-0.4, 1.0), texval(-1.0, -0.4), texval(0.4, -1.0), texval(1.0, 0.4)));\n"
1092 "#    else\n"
1093 "    f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r;\n"
1094 "#    endif\n"
1095 "#  else\n"
1096 "#    ifdef USESHADOWMAPPCF\n"
1097 "#     if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4)\n"
1098 "#      ifdef GL_ARB_texture_gather\n"
1099 "#        define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y))\n"
1100 "#      else\n"
1101 "#        define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x,y)*ShadowMap_TextureScale)\n"
1102 "#      endif\n"
1103 "    vec2 center = shadowmaptc.xy - 0.5, offset = fract(center);\n"
1104 "    center *= ShadowMap_TextureScale;\n"
1105 "    vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0));\n"
1106 "    vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0));\n"
1107 "    vec4 group3 = step(shadowmaptc.z, texval(-1.0,  1.0));\n"
1108 "    vec4 group4 = step(shadowmaptc.z, texval( 1.0,  1.0));\n"
1109 "    vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) +\n"
1110 "                mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y);\n"
1111 "    f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0));\n"
1112 "#     else\n"
1113 "#      ifdef GL_EXT_gpu_shader4\n"
1114 "#        define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r\n"
1115 "#      else\n"
1116 "#        define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r  \n"
1117 "#      endif\n"
1118 "#      if USESHADOWMAPPCF > 1\n"
1119 "    vec2 center = shadowmaptc.xy - 0.5, offset = fract(center);\n"
1120 "    center *= ShadowMap_TextureScale;\n"
1121 "    vec4 row1 = step(shadowmaptc.z, vec4(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0), texval( 2.0, -1.0)));\n"
1122 "    vec4 row2 = step(shadowmaptc.z, vec4(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0), texval( 2.0,  0.0)));\n"
1123 "    vec4 row3 = step(shadowmaptc.z, vec4(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0), texval( 2.0,  1.0)));\n"
1124 "    vec4 row4 = step(shadowmaptc.z, vec4(texval(-1.0,  2.0), texval( 0.0,  2.0), texval( 1.0,  2.0), texval( 2.0,  2.0)));\n"
1125 "    vec4 cols = row2 + row3 + mix(row1, row4, offset.y);\n"
1126 "    f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0));\n"
1127 "#      else\n"
1128 "    vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy);\n"
1129 "    vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0)));\n"
1130 "    vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0)));\n"
1131 "    vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0)));\n"
1132 "    vec3 cols = row2 + mix(row1, row3, offset.y);\n"
1133 "    f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25));\n"
1134 "#      endif\n"
1135 "#     endif\n"
1136 "#    else\n"
1137 "    f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r);\n"
1138 "#    endif\n"
1139 "#  endif\n"
1140 "    return f;\n"
1141 "}\n"
1142 "# endif\n"
1143 "\n"
1144 "# ifdef USESHADOWMAPCUBE\n"
1145 "float ShadowMapCompare(vec3 dir)\n"
1146 "{\n"
1147 "    // apply depth texture cubemap as light filter\n"
1148 "    vec4 shadowmaptc = GetShadowMapTCCube(dir);\n"
1149 "    float f;\n"
1150 "#  ifdef USESHADOWSAMPLER\n"
1151 "    f = shadowCube(Texture_ShadowMapCube, shadowmaptc).r;\n"
1152 "#  else\n"
1153 "    f = step(shadowmaptc.w, textureCube(Texture_ShadowMapCube, shadowmaptc.xyz).r);\n"
1154 "#  endif\n"
1155 "    return f;\n"
1156 "}\n"
1157 "# endif\n"
1158 "#endif\n"
1159 "\n"
1160 "#ifdef MODE_WATER\n"
1161 "\n"
1162 "// water pass\n"
1163 "void main(void)\n"
1164 "{\n"
1165 "#ifdef USEOFFSETMAPPING\n"
1166 "       // apply offsetmapping\n"
1167 "       vec2 TexCoordOffset = OffsetMapping(TexCoord);\n"
1168 "#define TexCoord TexCoordOffset\n"
1169 "#endif\n"
1170 "\n"
1171 "       vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);\n"
1172 "       //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
1173 "       vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
1174 "       vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xyxy * DistortScaleRefractReflect;\n"
1175 "       // FIXME temporary hack to detect the case that the reflection\n"
1176 "       // gets blackened at edges due to leaving the area that contains actual\n"
1177 "       // content.\n"
1178 "       // Remove this 'ack once we have a better way to stop this thing from\n"
1179 "       // 'appening.\n"
1180 "       float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, 0.01)).rgb) / 0.05);\n"
1181 "       f      *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, -0.01)).rgb) / 0.05);\n"
1182 "       f      *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, 0.01)).rgb) / 0.05);\n"
1183 "       f      *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, -0.01)).rgb) / 0.05);\n"
1184 "       ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f);\n"
1185 "       f       = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, 0.01)).rgb) / 0.05);\n"
1186 "       f      *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, -0.01)).rgb) / 0.05);\n"
1187 "       f      *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, 0.01)).rgb) / 0.05);\n"
1188 "       f      *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, -0.01)).rgb) / 0.05);\n"
1189 "       ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f);\n"
1190 "       float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset;\n"
1191 "       gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel);\n"
1192 "}\n"
1193 "\n"
1194 "#else // !MODE_WATER\n"
1195 "#ifdef MODE_REFRACTION\n"
1196 "\n"
1197 "// refraction pass\n"
1198 "void main(void)\n"
1199 "{\n"
1200 "#ifdef USEOFFSETMAPPING\n"
1201 "       // apply offsetmapping\n"
1202 "       vec2 TexCoordOffset = OffsetMapping(TexCoord);\n"
1203 "#define TexCoord TexCoordOffset\n"
1204 "#endif\n"
1205 "\n"
1206 "       vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w);\n"
1207 "       //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy;\n"
1208 "       vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy;\n"
1209 "       vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.xy;\n"
1210 "       // FIXME temporary hack to detect the case that the reflection\n"
1211 "       // gets blackened at edges due to leaving the area that contains actual\n"
1212 "       // content.\n"
1213 "       // Remove this 'ack once we have a better way to stop this thing from\n"
1214 "       // 'appening.\n"
1215 "       float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05);\n"
1216 "       f      *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05);\n"
1217 "       f      *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05);\n"
1218 "       f      *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05);\n"
1219 "       ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f);\n"
1220 "       gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor;\n"
1221 "}\n"
1222 "\n"
1223 "#else // !MODE_REFRACTION\n"
1224 "void main(void)\n"
1225 "{\n"
1226 "#ifdef USEOFFSETMAPPING\n"
1227 "       // apply offsetmapping\n"
1228 "       vec2 TexCoordOffset = OffsetMapping(TexCoord);\n"
1229 "#define TexCoord TexCoordOffset\n"
1230 "#endif\n"
1231 "\n"
1232 "       // combine the diffuse textures (base, pants, shirt)\n"
1233 "       myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord));\n"
1234 "#ifdef USECOLORMAPPING\n"
1235 "       color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt;\n"
1236 "#endif\n"
1237 "#ifdef USEVERTEXTEXTUREBLEND\n"
1238 "       myhalf terrainblend = clamp(myhalf(gl_Color.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0));\n"
1239 "       //myhalf terrainblend = min(myhalf(gl_Color.a) * color.a * 2.0, myhalf(1.0));\n"
1240 "       //myhalf terrainblend = myhalf(gl_Color.a) * color.a > 0.5;\n"
1241 "       color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend);\n"
1242 "       color.a = 1.0;\n"
1243 "       //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend);\n"
1244 "#endif\n"
1245 "\n"
1246 "#ifdef USEDIFFUSE\n"
1247 "       // get the surface normal and the gloss color\n"
1248 "# ifdef USEVERTEXTEXTUREBLEND\n"
1249 "       myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5));\n"
1250 "#  ifdef USESPECULAR\n"
1251 "       myhalf3 glosscolor = mix(myhalf3(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf3(texture2D(Texture_Gloss, TexCoord)), terrainblend);\n"
1252 "#  endif\n"
1253 "# else\n"
1254 "       myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5));\n"
1255 "#  ifdef USESPECULAR\n"
1256 "       myhalf3 glosscolor = myhalf3(texture2D(Texture_Gloss, TexCoord));\n"
1257 "#  endif\n"
1258 "# endif\n"
1259 "#endif\n"
1260 "\n"
1261 "\n"
1262 "\n"
1263 "#ifdef MODE_LIGHTSOURCE\n"
1264 "       // light source\n"
1265 "\n"
1266 "       // calculate surface normal, light normal, and specular normal\n"
1267 "       // compute color intensity for the two textures (colormap and glossmap)\n"
1268 "       // scale by light color and attenuation as efficiently as possible\n"
1269 "       // (do as much scalar math as possible rather than vector math)\n"
1270 "# ifdef USEDIFFUSE\n"
1271 "       // get the light normal\n"
1272 "       myhalf3 diffusenormal = myhalf3(normalize(LightVector));\n"
1273 "# endif\n"
1274 "# ifdef USESPECULAR\n"
1275 "#  ifndef USEEXACTSPECULARMATH\n"
1276 "       myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector)));\n"
1277 "\n"
1278 "#  endif\n"
1279 "       // calculate directional shading\n"
1280 "#  ifdef USEEXACTSPECULARMATH\n"
1281 "       color.rgb = 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(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower)) * glosscolor);\n"
1282 "#  else\n"
1283 "       color.rgb = 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)) * glosscolor);\n"
1284 "#  endif\n"
1285 "# else\n"
1286 "#  ifdef USEDIFFUSE\n"
1287 "       // calculate directional shading\n"
1288 "       color.rgb = color.rgb * (myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0))) * (AmbientScale + DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0))));\n"
1289 "#  else\n"
1290 "       // calculate directionless shading\n"
1291 "       color.rgb = color.rgb * myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0)));\n"
1292 "#  endif\n"
1293 "# endif\n"
1294 "\n"
1295 "#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D)\n"
1296 "#if !showshadowmap\n"
1297 "    color.rgb *= ShadowMapCompare(CubeVector);\n"
1298 "#endif\n"
1299 "#endif\n"
1300 "\n"
1301 "# ifdef USECUBEFILTER\n"
1302 "       // apply light cubemap filter\n"
1303 "       //color.rgb *= normalize(CubeVector) * 0.5 + 0.5;//vec3(textureCube(Texture_Cube, CubeVector));\n"
1304 "       color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector));\n"
1305 "# endif\n"
1306 "#endif // MODE_LIGHTSOURCE\n"
1307 "\n"
1308 "\n"
1309 "\n"
1310 "\n"
1311 "#ifdef MODE_LIGHTDIRECTION\n"
1312 "       // directional model lighting\n"
1313 "# ifdef USEDIFFUSE\n"
1314 "       // get the light normal\n"
1315 "       myhalf3 diffusenormal = myhalf3(normalize(LightVector));\n"
1316 "# endif\n"
1317 "# ifdef USESPECULAR\n"
1318 "       // calculate directional shading\n"
1319 "       color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0));\n"
1320 "#  ifdef USEEXACTSPECULARMATH\n"
1321 "       color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower);\n"
1322 "#  else\n"
1323 "       myhalf3 specularnormal = normalize(diffusenormal + myhalf3(normalize(EyeVector)));\n"
1324 "       color.rgb += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularColor * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower);\n"
1325 "#  endif\n"
1326 "# else\n"
1327 "#  ifdef USEDIFFUSE\n"
1328 "\n"
1329 "       // calculate directional shading\n"
1330 "       color.rgb *= AmbientColor + DiffuseColor * myhalf(max(float(dot(surfacenormal, diffusenormal)), 0.0));\n"
1331 "#  else\n"
1332 "       color.rgb *= AmbientColor;\n"
1333 "#  endif\n"
1334 "# endif\n"
1335 "#endif // MODE_LIGHTDIRECTION\n"
1336 "\n"
1337 "\n"
1338 "\n"
1339 "\n"
1340 "#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
1341 "       // deluxemap lightmapping using light vectors in modelspace (evil q3map2)\n"
1342 "\n"
1343 "       // get the light normal\n"
1344 "       myhalf3 diffusenormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0);\n"
1345 "       myhalf3 diffusenormal;\n"
1346 "       diffusenormal.x = dot(diffusenormal_modelspace, myhalf3(VectorS));\n"
1347 "       diffusenormal.y = dot(diffusenormal_modelspace, myhalf3(VectorT));\n"
1348 "       diffusenormal.z = dot(diffusenormal_modelspace, myhalf3(VectorR));\n"
1349 "       // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division)\n"
1350 "       // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar\n"
1351 "       // is used (the lightmap and deluxemap coords correspond to virtually random coordinates\n"
1352 "       // on that luxel, and NOT to its center, because recursive triangle subdivision is used\n"
1353 "       // to map the luxels to coordinates on the draw surfaces), which also causes\n"
1354 "       // deluxemaps to be wrong because light contributions from the wrong side of the surface\n"
1355 "       // are added up. To prevent divisions by zero or strong exaggerations, a max()\n"
1356 "       // nudge is done here at expense of some additional fps. This is ONLY needed for\n"
1357 "       // deluxemaps, tangentspace deluxemap avoid this problem by design.\n"
1358 "       myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / max(0.25, diffusenormal.z)), 0.0)));\n"
1359 "               // 0.25 supports up to 75.5 degrees normal/deluxe angle\n"
1360 "# ifdef USESPECULAR\n"
1361 "#  ifdef USEEXACTSPECULARMATH\n"
1362 "       tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(normalize(diffusenormal), surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower);\n"
1363 "#  else\n"
1364 "       myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector))));\n"
1365 "       tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower);\n"
1366 "#  endif\n"
1367 "# endif\n"
1368 "\n"
1369 "       // apply lightmap color\n"
1370 "       color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap));\n"
1371 "#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
1372 "\n"
1373 "\n"
1374 "\n"
1375 "\n"
1376 "#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n"
1377 "       // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light)\n"
1378 "\n"
1379 "       // get the light normal\n"
1380 "       myhalf3 diffusenormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0);\n"
1381 "       // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division)\n"
1382 "       myhalf3 tempcolor = color.rgb * (DiffuseScale * myhalf(max(float(dot(surfacenormal, diffusenormal) / diffusenormal.z), 0.0)));\n"
1383 "# ifdef USESPECULAR\n"
1384 "#  ifdef USEEXACTSPECULARMATH\n"
1385 "       tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(reflect(diffusenormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower);\n"
1386 "#  else\n"
1387 "       myhalf3 specularnormal = myhalf3(normalize(diffusenormal + myhalf3(normalize(EyeVector))));\n"
1388 "       tempcolor += myhalf3(texture2D(Texture_Gloss, TexCoord)) * SpecularScale * pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower);\n"
1389 "#  endif\n"
1390 "# endif\n"
1391 "\n"
1392 "       // apply lightmap color\n"
1393 "       color.rgb = color.rgb * AmbientScale + tempcolor * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap));\n"
1394 "#endif // MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n"
1395 "\n"
1396 "\n"
1397 "\n"
1398 "\n"
1399 "#ifdef MODE_LIGHTMAP\n"
1400 "       // apply lightmap color\n"
1401 "       color.rgb = color.rgb * myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * DiffuseScale + color.rgb * AmbientScale;\n"
1402 "#endif // MODE_LIGHTMAP\n"
1403 "\n"
1404 "\n"
1405 "\n"
1406 "\n"
1407 "#ifdef MODE_VERTEXCOLOR\n"
1408 "       // apply lightmap color\n"
1409 "       color.rgb = color.rgb * myhalf3(gl_Color.rgb) * DiffuseScale + color.rgb * AmbientScale;\n"
1410 "#endif // MODE_VERTEXCOLOR\n"
1411 "\n"
1412 "\n"
1413 "\n"
1414 "\n"
1415 "#ifdef MODE_FLATCOLOR\n"
1416 "#endif // MODE_FLATCOLOR\n"
1417 "\n"
1418 "\n"
1419 "\n"
1420 "\n"
1421 "\n"
1422 "\n"
1423 "\n"
1424 "       color *= TintColor;\n"
1425 "\n"
1426 "#ifdef USEGLOW\n"
1427 "#ifdef USEVERTEXTEXTUREBLEND\n"
1428 "       color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend);\n"
1429 "#else\n"
1430 "       color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * GlowColor;\n"
1431 "#endif\n"
1432 "#endif\n"
1433 "\n"
1434 "       color.rgb *= SceneBrightness;\n"
1435 "\n"
1436 "       // apply fog after Contrastboost/SceneBrightness because its color is already modified appropriately\n"
1437 "#ifdef USEFOG\n"
1438 "       float fogfrac;\n"
1439 "#ifdef USEFOGOUTSIDE\n"
1440 "       fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade);\n"
1441 "#else\n"
1442 "       fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade);\n"
1443 "#endif\n"
1444 "//     float FogHeightFade1 = -0.5/1024.0;\n"
1445 "//     if (FogPlaneViewDist >= 0.0)\n"
1446 "//             fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade1);\n"
1447 "//     else\n"
1448 "//             fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade1);\n"
1449 "//# ifdef USEFOGABOVE\n"
1450 "//     if (FogPlaneViewDist >= 0.0)\n"
1451 "//             fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist);\n"
1452 "//     else\n"
1453 "//             fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist));\n"
1454 "//     fogfrac *= min(1.0, (min(0.0, FogPlaneVertexDist) + min(0.0, FogPlaneViewDist))*FogHeightFade1);\n"
1455 "//     fogfrac *= min(1.0, (max(0.0, fade*FogPlaneVertexDist) + max(0.0, fade*FogPlaneViewDist)));\n"
1456 "//     fogfrac *= min(1.0, (max(0.0, FogHeightFade1*FogPlaneVertexDist) + max(0.0, FogHeightFade1*FogPlaneViewDist)));\n"
1457 "//     fogfrac *= min(1.0, (min(0.0, FogPlaneVertexDist) + min(0.0, FogPlaneViewDist))*FogHeightFade1);\n"
1458 "\n"
1459 "       //fogfrac *= min(1.0, max(0.0, (max(-2048, min(0, FogPlaneVertexDist)) + max(-2048, min(0, FogPlaneViewDist)))/-2048.0));\n"
1460 "       //float fade = -0.5/128.0;\n"
1461 "       //fogfrac *= max(0.0, min(1.0, fade*FogPlaneVertexDist)) + max(0.0, min(1.0, fade*FogPlaneViewDist));\n"
1462 "       //fogfrac *= max(0.0, min(1.0, FogHeightFade1*FogPlaneVertexDist)) + max(0.0, min(1.0, FogHeightFade1*FogPlaneViewDist));\n"
1463 "       //fogfrac *= min(1.0, max(0.0, FogHeightFade1*FogPlaneVertexDist)) + min(1.0, max(0.0, FogHeightFade1*FogPlaneViewDist));\n"
1464 "       //fogfrac *= min(1.0, max(0.0, FogHeightFade1*FogPlaneVertexDist) + max(0.0, FogHeightFade1*FogPlaneViewDist));\n"
1465 "       //fogfrac *= min(1.0, min(1.0, max(0.0, FogHeightFade1*FogPlaneVertexDist)) + min(1.0, max(0.0, FogHeightFade1*FogPlaneViewDist)));\n"
1466 "       //fogfrac *= min(1.0, max(0.0, FogHeightFade1*FogPlaneVertexDist) + max(0.0, FogHeightFade1*FogPlaneViewDist));\n"
1467 "       //fogfrac *= min(1.0, (min(0.0, FogPlaneVertexDist) + min(0.0, FogPlaneViewDist)) * FogHeightFade1);\n"
1468 "       //fogfrac *= min(1.0, (min(0.0, FogPlaneVertexDist) + min(0.0, FogPlaneViewDist)) * FogHeightFade1);\n"
1469 "//# endif\n"
1470 "       color.rgb = mix(FogColor, color.rgb, myhalf(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0))));\n"
1471 "#endif\n"
1472 "\n"
1473 "       // 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"
1474 "#ifdef USEREFLECTION\n"
1475 "       vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);\n"
1476 "       //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
1477 "       vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw;\n"
1478 "       vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw;\n"
1479 "       // FIXME temporary hack to detect the case that the reflection\n"
1480 "       // gets blackened at edges due to leaving the area that contains actual\n"
1481 "       // content.\n"
1482 "       // Remove this 'ack once we have a better way to stop this thing from\n"
1483 "       // 'appening.\n"
1484 "       float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05);\n"
1485 "       f      *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05);\n"
1486 "       f      *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05);\n"
1487 "       f      *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05);\n"
1488 "       ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f);\n"
1489 "       color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a);\n"
1490 "#endif\n"
1491 "\n"
1492 "       gl_FragColor = vec4(color);\n"
1493 "\n"
1494 "#if showshadowmap\n"
1495 "# ifdef USESHADOWMAPRECT\n"
1496 "#  ifdef USESHADOWSAMPLER\n"
1497 "       gl_FragColor = shadow2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xyz);\n"
1498 "#  else\n"
1499 "       gl_FragColor = texture2DRect(Texture_ShadowMapRect, GetShadowMapTC2D(CubeVector).xy);\n"
1500 "#  endif\n"
1501 "# endif\n"
1502 "# ifdef USESHADOWMAP2D\n"
1503 "#  ifdef USESHADOWSAMPLER\n"
1504 "    gl_FragColor = shadow2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xyz);\n"
1505 "#  else\n"
1506 "    gl_FragColor = texture2D(Texture_ShadowMap2D, GetShadowMapTC2D(CubeVector).xy);\n"
1507 "#  endif\n"
1508 "# endif\n"
1509 "\n"
1510 "# ifdef USESHADOWMAPCUBE\n"
1511 "#  ifdef USESHADOWSAMPLER\n"
1512 "    gl_FragColor = shadowCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector));\n"
1513 "#  else\n"
1514 "    gl_FragColor = textureCube(Texture_ShadowMapCube, GetShadowMapTCCube(CubeVector).xyz);\n"
1515 "#  endif\n"
1516 "# endif\n"
1517 "#endif\n"
1518 "}\n"
1519 "#endif // !MODE_REFRACTION\n"
1520 "#endif // !MODE_WATER\n"
1521 "\n"
1522 "#endif // FRAGMENT_SHADER\n"
1523 "\n"
1524 "#endif // !MODE_BLOOMBLUR\n"
1525 "#endif // !MODE_GENERIC\n"
1526 "#endif // !MODE_POSTPROCESS\n"
1527 "#endif // !MODE_SHOWDEPTH\n"
1528 "#endif // !MODE_DEPTH_OR_SHADOW\n"
1529 ;
1530
1531 typedef struct shaderpermutationinfo_s
1532 {
1533         const char *pretext;
1534         const char *name;
1535 }
1536 shaderpermutationinfo_t;
1537
1538 typedef struct shadermodeinfo_s
1539 {
1540         const char *vertexfilename;
1541         const char *geometryfilename;
1542         const char *fragmentfilename;
1543         const char *pretext;
1544         const char *name;
1545 }
1546 shadermodeinfo_t;
1547
1548 typedef enum shaderpermutation_e
1549 {
1550         SHADERPERMUTATION_DIFFUSE = 1<<0, ///< (lightsource) whether to use directional shading
1551         SHADERPERMUTATION_VERTEXTEXTUREBLEND = 1<<1, ///< indicates this is a two-layer material blend based on vertex alpha (q3bsp)
1552         SHADERPERMUTATION_VIEWTINT = 1<<2, ///< view tint (postprocessing only)
1553         SHADERPERMUTATION_COLORMAPPING = 1<<3, ///< indicates this is a colormapped skin
1554         SHADERPERMUTATION_SATURATION = 1<<4, ///< saturation (postprocessing only)
1555         SHADERPERMUTATION_FOGINSIDE = 1<<5, ///< tint the color by fog color or black if using additive blend mode
1556         SHADERPERMUTATION_FOGOUTSIDE = 1<<6, ///< tint the color by fog color or black if using additive blend mode
1557         SHADERPERMUTATION_GAMMARAMPS = 1<<7, ///< gamma (postprocessing only)
1558         SHADERPERMUTATION_CUBEFILTER = 1<<8, ///< (lightsource) use cubemap light filter
1559         SHADERPERMUTATION_GLOW = 1<<9, ///< (lightmap) blend in an additive glow texture
1560         SHADERPERMUTATION_BLOOM = 1<<10, ///< bloom (postprocessing only)
1561         SHADERPERMUTATION_SPECULAR = 1<<11, ///< (lightsource or deluxemapping) render specular effects
1562         SHADERPERMUTATION_POSTPROCESSING = 1<<12, ///< user defined postprocessing (postprocessing only)
1563         SHADERPERMUTATION_EXACTSPECULARMATH = 1<<13, ///< (lightsource or deluxemapping) use exact reflection map for specular effects, as opposed to the usual OpenGL approximation
1564         SHADERPERMUTATION_REFLECTION = 1<<14, ///< normalmap-perturbed reflection of the scene infront of the surface, preformed as an overlay on the surface
1565         SHADERPERMUTATION_OFFSETMAPPING = 1<<15, ///< adjust texcoords to roughly simulate a displacement mapped surface
1566         SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING = 1<<16, ///< adjust texcoords to accurately simulate a displacement mapped surface (requires OFFSETMAPPING to also be set!)
1567         SHADERPERMUTATION_SHADOWMAPRECT = 1<<17, ///< (lightsource) use shadowmap rectangle texture as light filter
1568         SHADERPERMUTATION_SHADOWMAPCUBE = 1<<18, ///< (lightsource) use shadowmap cubemap texture as light filter
1569         SHADERPERMUTATION_SHADOWMAP2D = 1<<19, ///< (lightsource) use shadowmap rectangle texture as light filter
1570         SHADERPERMUTATION_SHADOWMAPPCF = 1<<20, ///< (lightsource) use percentage closer filtering on shadowmap test results
1571         SHADERPERMUTATION_SHADOWMAPPCF2 = 1<<21, ///< (lightsource) use higher quality percentage closer filtering on shadowmap test results
1572         SHADERPERMUTATION_SHADOWSAMPLER = 1<<22, ///< (lightsource) use hardware shadowmap test
1573         SHADERPERMUTATION_SHADOWMAPVSDCT = 1<<23, ///< (lightsource) use virtual shadow depth cube texture for shadowmap indexing
1574         SHADERPERMUTATION_LIMIT = 1<<24, ///< size of permutations array
1575         SHADERPERMUTATION_COUNT = 24 ///< size of shaderpermutationinfo array
1576 }
1577 shaderpermutation_t;
1578
1579 // NOTE: MUST MATCH ORDER OF SHADERPERMUTATION_* DEFINES!
1580 shaderpermutationinfo_t shaderpermutationinfo[SHADERPERMUTATION_COUNT] =
1581 {
1582         {"#define USEDIFFUSE\n", " diffuse"},
1583         {"#define USEVERTEXTEXTUREBLEND\n", " vertextextureblend"},
1584         {"#define USEVIEWTINT\n", " viewtint"},
1585         {"#define USECOLORMAPPING\n", " colormapping"},
1586         {"#define USESATURATION\n", " saturation"},
1587         {"#define USEFOGINSIDE\n", " foginside"},
1588         {"#define USEFOGOUTSIDE\n", " fogoutside"},
1589         {"#define USEGAMMARAMPS\n", " gammaramps"},
1590         {"#define USECUBEFILTER\n", " cubefilter"},
1591         {"#define USEGLOW\n", " glow"},
1592         {"#define USEBLOOM\n", " bloom"},
1593         {"#define USESPECULAR\n", " specular"},
1594         {"#define USEPOSTPROCESSING\n", " postprocessing"},
1595         {"#define USEEXACTSPECULARMATH\n", " exactspecularmath"},
1596         {"#define USEREFLECTION\n", " reflection"},
1597         {"#define USEOFFSETMAPPING\n", " offsetmapping"},
1598         {"#define USEOFFSETMAPPING_RELIEFMAPPING\n", " reliefmapping"},
1599         {"#define USESHADOWMAPRECT\n", " shadowmaprect"},
1600         {"#define USESHADOWMAPCUBE\n", " shadowmapcube"},
1601         {"#define USESHADOWMAP2D\n", " shadowmap2d"},
1602         {"#define USESHADOWMAPPCF 1\n", " shadowmappcf"},
1603         {"#define USESHADOWMAPPCF 2\n", " shadowmappcf2"},
1604         {"#define USESHADOWSAMPLER\n", " shadowsampler"},
1605         {"#define USESHADOWMAPVSDCT\n", " shadowmapvsdct"},
1606 };
1607
1608 /// this enum is multiplied by SHADERPERMUTATION_MODEBASE
1609 typedef enum shadermode_e
1610 {
1611         SHADERMODE_GENERIC, ///< (particles/HUD/etc) vertex color, optionally multiplied by one texture
1612         SHADERMODE_POSTPROCESS, ///< postprocessing shader (r_glsl_postprocess)
1613         SHADERMODE_DEPTH_OR_SHADOW, ///< (depthfirst/shadows) vertex shader only
1614         SHADERMODE_FLATCOLOR, ///< (lightmap) modulate texture by uniform color (q1bsp, q3bsp)
1615         SHADERMODE_VERTEXCOLOR, ///< (lightmap) modulate texture by vertex colors (q3bsp)
1616         SHADERMODE_LIGHTMAP, ///< (lightmap) modulate texture by lightmap texture (q1bsp, q3bsp)
1617         SHADERMODE_LIGHTDIRECTIONMAP_MODELSPACE, ///< (lightmap) use directional pixel shading from texture containing modelspace light directions (q3bsp deluxemap)
1618         SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE, ///< (lightmap) use directional pixel shading from texture containing tangentspace light directions (q1bsp deluxemap)
1619         SHADERMODE_LIGHTDIRECTION, ///< (lightmap) use directional pixel shading from fixed light direction (q3bsp)
1620         SHADERMODE_LIGHTSOURCE, ///< (lightsource) use directional pixel shading from light source (rtlight)
1621         SHADERMODE_REFRACTION, ///< refract background (the material is rendered normally after this pass)
1622         SHADERMODE_WATER, ///< refract background and reflection (the material is rendered normally after this pass)
1623         SHADERMODE_SHOWDEPTH, ///< (debugging) renders depth as color
1624         SHADERMODE_COUNT
1625 }
1626 shadermode_t;
1627
1628 // NOTE: MUST MATCH ORDER OF SHADERMODE_* ENUMS!
1629 shadermodeinfo_t shadermodeinfo[SHADERMODE_COUNT] =
1630 {
1631         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_GENERIC\n", " generic"},
1632         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_POSTPROCESS\n", " postprocess"},
1633         {"glsl/default.glsl", NULL, NULL               , "#define MODE_DEPTH_OR_SHADOW\n", " depth/shadow"},
1634         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_FLATCOLOR\n", " flatcolor"},
1635         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_VERTEXCOLOR\n", " vertexcolor"},
1636         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTMAP\n", " lightmap"},
1637         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTDIRECTIONMAP_MODELSPACE\n", " lightdirectionmap_modelspace"},
1638         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n", " lightdirectionmap_tangentspace"},
1639         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTDIRECTION\n", " lightdirection"},
1640         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTSOURCE\n", " lightsource"},
1641         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_REFRACTION\n", " refraction"},
1642         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_WATER\n", " water"},
1643         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_SHOWDEPTH\n", " showdepth"},
1644 };
1645
1646 struct r_glsl_permutation_s;
1647 typedef struct r_glsl_permutation_s
1648 {
1649         /// hash lookup data
1650         struct r_glsl_permutation_s *hashnext;
1651         unsigned int mode;
1652         unsigned int permutation;
1653
1654         /// indicates if we have tried compiling this permutation already
1655         qboolean compiled;
1656         /// 0 if compilation failed
1657         int program;
1658         /// locations of detected uniforms in program object, or -1 if not found
1659         int loc_Texture_First;
1660         int loc_Texture_Second;
1661         int loc_Texture_GammaRamps;
1662         int loc_Texture_Normal;
1663         int loc_Texture_Color;
1664         int loc_Texture_Gloss;
1665         int loc_Texture_Glow;
1666         int loc_Texture_SecondaryNormal;
1667         int loc_Texture_SecondaryColor;
1668         int loc_Texture_SecondaryGloss;
1669         int loc_Texture_SecondaryGlow;
1670         int loc_Texture_Pants;
1671         int loc_Texture_Shirt;
1672         int loc_Texture_FogMask;
1673         int loc_Texture_Lightmap;
1674         int loc_Texture_Deluxemap;
1675         int loc_Texture_Attenuation;
1676         int loc_Texture_Cube;
1677         int loc_Texture_Refraction;
1678         int loc_Texture_Reflection;
1679         int loc_Texture_ShadowMapRect;
1680         int loc_Texture_ShadowMapCube;
1681         int loc_Texture_ShadowMap2D;
1682         int loc_Texture_CubeProjection;
1683         int loc_FogColor;
1684         int loc_LightPosition;
1685         int loc_EyePosition;
1686         int loc_Color_Pants;
1687         int loc_Color_Shirt;
1688         int loc_FogPlane;
1689         int loc_FogPlaneViewDist;
1690         int loc_FogRangeRecip;
1691         int loc_FogHeightFade;
1692         int loc_AmbientScale;
1693         int loc_DiffuseScale;
1694         int loc_SpecularScale;
1695         int loc_SpecularPower;
1696         int loc_GlowColor;
1697         int loc_SceneBrightness; // or: Scenebrightness * ContrastBoost
1698         int loc_OffsetMapping_Scale;
1699         int loc_TintColor;
1700         int loc_AmbientColor;
1701         int loc_DiffuseColor;
1702         int loc_SpecularColor;
1703         int loc_LightDir;
1704         int loc_ContrastBoostCoeff; ///< 1 - 1/ContrastBoost
1705         int loc_GammaCoeff; ///< 1 / gamma
1706         int loc_DistortScaleRefractReflect;
1707         int loc_ScreenScaleRefractReflect;
1708         int loc_ScreenCenterRefractReflect;
1709         int loc_RefractColor;
1710         int loc_ReflectColor;
1711         int loc_ReflectFactor;
1712         int loc_ReflectOffset;
1713         int loc_UserVec1;
1714         int loc_UserVec2;
1715         int loc_UserVec3;
1716         int loc_UserVec4;
1717         int loc_ClientTime;
1718         int loc_PixelSize;
1719         int loc_Saturation;
1720         int loc_ShadowMap_TextureScale;
1721         int loc_ShadowMap_Parameters;
1722 }
1723 r_glsl_permutation_t;
1724
1725 #define SHADERPERMUTATION_HASHSIZE 256
1726
1727 /// information about each possible shader permutation
1728 r_glsl_permutation_t *r_glsl_permutationhash[SHADERMODE_COUNT][SHADERPERMUTATION_HASHSIZE];
1729 /// currently selected permutation
1730 r_glsl_permutation_t *r_glsl_permutation;
1731 /// storage for permutations linked in the hash table
1732 memexpandablearray_t r_glsl_permutationarray;
1733
1734 static r_glsl_permutation_t *R_GLSL_FindPermutation(unsigned int mode, unsigned int permutation)
1735 {
1736         //unsigned int hashdepth = 0;
1737         unsigned int hashindex = (permutation * 0x1021) & (SHADERPERMUTATION_HASHSIZE - 1);
1738         r_glsl_permutation_t *p;
1739         for (p = r_glsl_permutationhash[mode][hashindex];p;p = p->hashnext)
1740         {
1741                 if (p->mode == mode && p->permutation == permutation)
1742                 {
1743                         //if (hashdepth > 10)
1744                         //      Con_Printf("R_GLSL_FindPermutation: Warning: %i:%i has hashdepth %i\n", mode, permutation, hashdepth);
1745                         return p;
1746                 }
1747                 //hashdepth++;
1748         }
1749         p = (r_glsl_permutation_t*)Mem_ExpandableArray_AllocRecord(&r_glsl_permutationarray);
1750         p->mode = mode;
1751         p->permutation = permutation;
1752         p->hashnext = r_glsl_permutationhash[mode][hashindex];
1753         r_glsl_permutationhash[mode][hashindex] = p;
1754         //if (hashdepth > 10)
1755         //      Con_Printf("R_GLSL_FindPermutation: Warning: %i:%i has hashdepth %i\n", mode, permutation, hashdepth);
1756         return p;
1757 }
1758
1759 static char *R_GLSL_GetText(const char *filename, qboolean printfromdisknotice)
1760 {
1761         char *shaderstring;
1762         if (!filename || !filename[0])
1763                 return NULL;
1764         shaderstring = (char *)FS_LoadFile(filename, r_main_mempool, false, NULL);
1765         if (shaderstring)
1766         {
1767                 if (printfromdisknotice)
1768                         Con_DPrint("from disk... ");
1769                 return shaderstring;
1770         }
1771         else if (!strcmp(filename, "glsl/default.glsl"))
1772         {
1773                 shaderstring = (char *) Mem_Alloc(r_main_mempool, strlen(builtinshaderstring) + 1);
1774                 memcpy(shaderstring, builtinshaderstring, strlen(builtinshaderstring) + 1);
1775         }
1776         return shaderstring;
1777 }
1778
1779 static void R_GLSL_CompilePermutation(r_glsl_permutation_t *p, unsigned int mode, unsigned int permutation)
1780 {
1781         int i;
1782         shadermodeinfo_t *modeinfo = shadermodeinfo + mode;
1783         int vertstrings_count = 0;
1784         int geomstrings_count = 0;
1785         int fragstrings_count = 0;
1786         char *vertexstring, *geometrystring, *fragmentstring;
1787         const char *vertstrings_list[32+3];
1788         const char *geomstrings_list[32+3];
1789         const char *fragstrings_list[32+3];
1790         char permutationname[256];
1791
1792         if (p->compiled)
1793                 return;
1794         p->compiled = true;
1795         p->program = 0;
1796
1797         permutationname[0] = 0;
1798         vertexstring   = R_GLSL_GetText(modeinfo->vertexfilename, true);
1799         geometrystring = R_GLSL_GetText(modeinfo->geometryfilename, false);
1800         fragmentstring = R_GLSL_GetText(modeinfo->fragmentfilename, false);
1801
1802         strlcat(permutationname, shadermodeinfo[mode].vertexfilename, sizeof(permutationname));
1803
1804         // the first pretext is which type of shader to compile as
1805         // (later these will all be bound together as a program object)
1806         vertstrings_list[vertstrings_count++] = "#define VERTEX_SHADER\n";
1807         geomstrings_list[geomstrings_count++] = "#define GEOMETRY_SHADER\n";
1808         fragstrings_list[fragstrings_count++] = "#define FRAGMENT_SHADER\n";
1809
1810         // the second pretext is the mode (for example a light source)
1811         vertstrings_list[vertstrings_count++] = shadermodeinfo[mode].pretext;
1812         geomstrings_list[geomstrings_count++] = shadermodeinfo[mode].pretext;
1813         fragstrings_list[fragstrings_count++] = shadermodeinfo[mode].pretext;
1814         strlcat(permutationname, modeinfo->name, sizeof(permutationname));
1815
1816         // now add all the permutation pretexts
1817         for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
1818         {
1819                 if (permutation & (1<<i))
1820                 {
1821                         vertstrings_list[vertstrings_count++] = shaderpermutationinfo[i].pretext;
1822                         geomstrings_list[geomstrings_count++] = shaderpermutationinfo[i].pretext;
1823                         fragstrings_list[fragstrings_count++] = shaderpermutationinfo[i].pretext;
1824                         strlcat(permutationname, shaderpermutationinfo[i].name, sizeof(permutationname));
1825                 }
1826                 else
1827                 {
1828                         // keep line numbers correct
1829                         vertstrings_list[vertstrings_count++] = "\n";
1830                         geomstrings_list[geomstrings_count++] = "\n";
1831                         fragstrings_list[fragstrings_count++] = "\n";
1832                 }
1833         }
1834
1835         // now append the shader text itself
1836         vertstrings_list[vertstrings_count++] = vertexstring;
1837         geomstrings_list[geomstrings_count++] = geometrystring;
1838         fragstrings_list[fragstrings_count++] = fragmentstring;
1839
1840         // if any sources were NULL, clear the respective list
1841         if (!vertexstring)
1842                 vertstrings_count = 0;
1843         if (!geometrystring)
1844                 geomstrings_count = 0;
1845         if (!fragmentstring)
1846                 fragstrings_count = 0;
1847
1848         // compile the shader program
1849         if (vertstrings_count + geomstrings_count + fragstrings_count)
1850                 p->program = GL_Backend_CompileProgram(vertstrings_count, vertstrings_list, geomstrings_count, geomstrings_list, fragstrings_count, fragstrings_list);
1851         if (p->program)
1852         {
1853                 CHECKGLERROR
1854                 qglUseProgramObjectARB(p->program);CHECKGLERROR
1855                 // look up all the uniform variable names we care about, so we don't
1856                 // have to look them up every time we set them
1857                 p->loc_Texture_First              = qglGetUniformLocationARB(p->program, "Texture_First");
1858                 p->loc_Texture_Second             = qglGetUniformLocationARB(p->program, "Texture_Second");
1859                 p->loc_Texture_GammaRamps         = qglGetUniformLocationARB(p->program, "Texture_GammaRamps");
1860                 p->loc_Texture_Normal             = qglGetUniformLocationARB(p->program, "Texture_Normal");
1861                 p->loc_Texture_Color              = qglGetUniformLocationARB(p->program, "Texture_Color");
1862                 p->loc_Texture_Gloss              = qglGetUniformLocationARB(p->program, "Texture_Gloss");
1863                 p->loc_Texture_Glow               = qglGetUniformLocationARB(p->program, "Texture_Glow");
1864                 p->loc_Texture_SecondaryNormal    = qglGetUniformLocationARB(p->program, "Texture_SecondaryNormal");
1865                 p->loc_Texture_SecondaryColor     = qglGetUniformLocationARB(p->program, "Texture_SecondaryColor");
1866                 p->loc_Texture_SecondaryGloss     = qglGetUniformLocationARB(p->program, "Texture_SecondaryGloss");
1867                 p->loc_Texture_SecondaryGlow      = qglGetUniformLocationARB(p->program, "Texture_SecondaryGlow");
1868                 p->loc_Texture_FogMask            = qglGetUniformLocationARB(p->program, "Texture_FogMask");
1869                 p->loc_Texture_Pants              = qglGetUniformLocationARB(p->program, "Texture_Pants");
1870                 p->loc_Texture_Shirt              = qglGetUniformLocationARB(p->program, "Texture_Shirt");
1871                 p->loc_Texture_Lightmap           = qglGetUniformLocationARB(p->program, "Texture_Lightmap");
1872                 p->loc_Texture_Deluxemap          = qglGetUniformLocationARB(p->program, "Texture_Deluxemap");
1873                 p->loc_Texture_Refraction         = qglGetUniformLocationARB(p->program, "Texture_Refraction");
1874                 p->loc_Texture_Reflection         = qglGetUniformLocationARB(p->program, "Texture_Reflection");
1875                 p->loc_Texture_Attenuation        = qglGetUniformLocationARB(p->program, "Texture_Attenuation");
1876                 p->loc_Texture_Cube               = qglGetUniformLocationARB(p->program, "Texture_Cube");
1877                 p->loc_Texture_ShadowMapRect      = qglGetUniformLocationARB(p->program, "Texture_ShadowMapRect");
1878                 p->loc_Texture_ShadowMapCube      = qglGetUniformLocationARB(p->program, "Texture_ShadowMapCube");
1879                 p->loc_Texture_ShadowMap2D        = qglGetUniformLocationARB(p->program, "Texture_ShadowMap2D");
1880                 p->loc_Texture_CubeProjection     = qglGetUniformLocationARB(p->program, "Texture_CubeProjection");  
1881                 p->loc_FogColor                   = qglGetUniformLocationARB(p->program, "FogColor");
1882                 p->loc_LightPosition              = qglGetUniformLocationARB(p->program, "LightPosition");
1883                 p->loc_EyePosition                = qglGetUniformLocationARB(p->program, "EyePosition");
1884                 p->loc_Color_Pants                = qglGetUniformLocationARB(p->program, "Color_Pants");
1885                 p->loc_Color_Shirt                = qglGetUniformLocationARB(p->program, "Color_Shirt");
1886                 p->loc_FogPlane                   = qglGetUniformLocationARB(p->program, "FogPlane");
1887                 p->loc_FogPlaneViewDist           = qglGetUniformLocationARB(p->program, "FogPlaneViewDist");
1888                 p->loc_FogRangeRecip              = qglGetUniformLocationARB(p->program, "FogRangeRecip");
1889                 p->loc_FogHeightFade              = qglGetUniformLocationARB(p->program, "FogHeightFade");
1890                 p->loc_AmbientScale               = qglGetUniformLocationARB(p->program, "AmbientScale");
1891                 p->loc_DiffuseScale               = qglGetUniformLocationARB(p->program, "DiffuseScale");
1892                 p->loc_SpecularPower              = qglGetUniformLocationARB(p->program, "SpecularPower");
1893                 p->loc_SpecularScale              = qglGetUniformLocationARB(p->program, "SpecularScale");
1894                 p->loc_GlowColor                  = qglGetUniformLocationARB(p->program, "GlowColor");
1895                 p->loc_SceneBrightness            = qglGetUniformLocationARB(p->program, "SceneBrightness");
1896                 p->loc_OffsetMapping_Scale        = qglGetUniformLocationARB(p->program, "OffsetMapping_Scale");
1897                 p->loc_TintColor                  = qglGetUniformLocationARB(p->program, "TintColor");
1898                 p->loc_AmbientColor               = qglGetUniformLocationARB(p->program, "AmbientColor");
1899                 p->loc_DiffuseColor               = qglGetUniformLocationARB(p->program, "DiffuseColor");
1900                 p->loc_SpecularColor              = qglGetUniformLocationARB(p->program, "SpecularColor");
1901                 p->loc_LightDir                   = qglGetUniformLocationARB(p->program, "LightDir");
1902                 p->loc_ContrastBoostCoeff         = qglGetUniformLocationARB(p->program, "ContrastBoostCoeff");
1903                 p->loc_DistortScaleRefractReflect = qglGetUniformLocationARB(p->program, "DistortScaleRefractReflect");
1904                 p->loc_ScreenScaleRefractReflect  = qglGetUniformLocationARB(p->program, "ScreenScaleRefractReflect");
1905                 p->loc_ScreenCenterRefractReflect = qglGetUniformLocationARB(p->program, "ScreenCenterRefractReflect");
1906                 p->loc_RefractColor               = qglGetUniformLocationARB(p->program, "RefractColor");
1907                 p->loc_ReflectColor               = qglGetUniformLocationARB(p->program, "ReflectColor");
1908                 p->loc_ReflectFactor              = qglGetUniformLocationARB(p->program, "ReflectFactor");
1909                 p->loc_ReflectOffset              = qglGetUniformLocationARB(p->program, "ReflectOffset");
1910                 p->loc_GammaCoeff                 = qglGetUniformLocationARB(p->program, "GammaCoeff");
1911                 p->loc_UserVec1                   = qglGetUniformLocationARB(p->program, "UserVec1");
1912                 p->loc_UserVec2                   = qglGetUniformLocationARB(p->program, "UserVec2");
1913                 p->loc_UserVec3                   = qglGetUniformLocationARB(p->program, "UserVec3");
1914                 p->loc_UserVec4                   = qglGetUniformLocationARB(p->program, "UserVec4");
1915                 p->loc_ClientTime                 = qglGetUniformLocationARB(p->program, "ClientTime");
1916                 p->loc_PixelSize                  = qglGetUniformLocationARB(p->program, "PixelSize");
1917                 p->loc_Saturation                 = qglGetUniformLocationARB(p->program, "Saturation");
1918                 p->loc_ShadowMap_TextureScale     = qglGetUniformLocationARB(p->program, "ShadowMap_TextureScale");
1919                 p->loc_ShadowMap_Parameters       = qglGetUniformLocationARB(p->program, "ShadowMap_Parameters");
1920                 // initialize the samplers to refer to the texture units we use
1921                 if (p->loc_Texture_First           >= 0) qglUniform1iARB(p->loc_Texture_First          , GL20TU_FIRST);
1922                 if (p->loc_Texture_Second          >= 0) qglUniform1iARB(p->loc_Texture_Second         , GL20TU_SECOND);
1923                 if (p->loc_Texture_GammaRamps      >= 0) qglUniform1iARB(p->loc_Texture_GammaRamps     , GL20TU_GAMMARAMPS);
1924                 if (p->loc_Texture_Normal          >= 0) qglUniform1iARB(p->loc_Texture_Normal         , GL20TU_NORMAL);
1925                 if (p->loc_Texture_Color           >= 0) qglUniform1iARB(p->loc_Texture_Color          , GL20TU_COLOR);
1926                 if (p->loc_Texture_Gloss           >= 0) qglUniform1iARB(p->loc_Texture_Gloss          , GL20TU_GLOSS);
1927                 if (p->loc_Texture_Glow            >= 0) qglUniform1iARB(p->loc_Texture_Glow           , GL20TU_GLOW);
1928                 if (p->loc_Texture_SecondaryNormal >= 0) qglUniform1iARB(p->loc_Texture_SecondaryNormal, GL20TU_SECONDARY_NORMAL);
1929                 if (p->loc_Texture_SecondaryColor  >= 0) qglUniform1iARB(p->loc_Texture_SecondaryColor , GL20TU_SECONDARY_COLOR);
1930                 if (p->loc_Texture_SecondaryGloss  >= 0) qglUniform1iARB(p->loc_Texture_SecondaryGloss , GL20TU_SECONDARY_GLOSS);
1931                 if (p->loc_Texture_SecondaryGlow   >= 0) qglUniform1iARB(p->loc_Texture_SecondaryGlow  , GL20TU_SECONDARY_GLOW);
1932                 if (p->loc_Texture_Pants           >= 0) qglUniform1iARB(p->loc_Texture_Pants          , GL20TU_PANTS);
1933                 if (p->loc_Texture_Shirt           >= 0) qglUniform1iARB(p->loc_Texture_Shirt          , GL20TU_SHIRT);
1934                 if (p->loc_Texture_FogMask         >= 0) qglUniform1iARB(p->loc_Texture_FogMask        , GL20TU_FOGMASK);
1935                 if (p->loc_Texture_Lightmap        >= 0) qglUniform1iARB(p->loc_Texture_Lightmap       , GL20TU_LIGHTMAP);
1936                 if (p->loc_Texture_Deluxemap       >= 0) qglUniform1iARB(p->loc_Texture_Deluxemap      , GL20TU_DELUXEMAP);
1937                 if (p->loc_Texture_Attenuation     >= 0) qglUniform1iARB(p->loc_Texture_Attenuation    , GL20TU_ATTENUATION);
1938                 if (p->loc_Texture_Cube            >= 0) qglUniform1iARB(p->loc_Texture_Cube           , GL20TU_CUBE);
1939                 if (p->loc_Texture_Refraction      >= 0) qglUniform1iARB(p->loc_Texture_Refraction     , GL20TU_REFRACTION);
1940                 if (p->loc_Texture_Reflection      >= 0) qglUniform1iARB(p->loc_Texture_Reflection     , GL20TU_REFLECTION);
1941                 if (p->loc_Texture_ShadowMapRect   >= 0) qglUniform1iARB(p->loc_Texture_ShadowMapRect  , GL20TU_SHADOWMAPRECT);
1942                 if (p->loc_Texture_ShadowMapCube   >= 0) qglUniform1iARB(p->loc_Texture_ShadowMapCube  , GL20TU_SHADOWMAPCUBE);
1943                 if (p->loc_Texture_ShadowMap2D     >= 0) qglUniform1iARB(p->loc_Texture_ShadowMap2D    , GL20TU_SHADOWMAP2D);
1944                 if (p->loc_Texture_CubeProjection  >= 0) qglUniform1iARB(p->loc_Texture_CubeProjection , GL20TU_CUBEPROJECTION);
1945                 CHECKGLERROR
1946                 if (developer.integer)
1947                         Con_Printf("GLSL shader %s compiled.\n", permutationname);
1948         }
1949         else
1950                 Con_Printf("GLSL shader %s failed!  some features may not work properly.\n", permutationname);
1951
1952         // free the strings
1953         if (vertexstring)
1954                 Mem_Free(vertexstring);
1955         if (geometrystring)
1956                 Mem_Free(geometrystring);
1957         if (fragmentstring)
1958                 Mem_Free(fragmentstring);
1959 }
1960
1961 void R_GLSL_Restart_f(void)
1962 {
1963         unsigned int i, limit;
1964         r_glsl_permutation_t *p;
1965         limit = Mem_ExpandableArray_IndexRange(&r_glsl_permutationarray);
1966         for (i = 0;i < limit;i++)
1967         {
1968                 if ((p = (r_glsl_permutation_t*)Mem_ExpandableArray_RecordAtIndex(&r_glsl_permutationarray, i)))
1969                 {
1970                         GL_Backend_FreeProgram(p->program);
1971                         Mem_ExpandableArray_FreeRecord(&r_glsl_permutationarray, (void*)p);
1972                 }
1973         }
1974         memset(r_glsl_permutationhash, 0, sizeof(r_glsl_permutationhash));
1975 }
1976
1977 void R_GLSL_DumpShader_f(void)
1978 {
1979         int i;
1980
1981         qfile_t *file = FS_OpenRealFile("glsl/default.glsl", "w", false);
1982         if(!file)
1983         {
1984                 Con_Printf("failed to write to glsl/default.glsl\n");
1985                 return;
1986         }
1987
1988         FS_Print(file, "/* The engine may define the following macros:\n");
1989         FS_Print(file, "#define VERTEX_SHADER\n#define GEOMETRY_SHADER\n#define FRAGMENT_SHADER\n");
1990         for (i = 0;i < SHADERMODE_COUNT;i++)
1991                 FS_Print(file, shadermodeinfo[i].pretext);
1992         for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
1993                 FS_Print(file, shaderpermutationinfo[i].pretext);
1994         FS_Print(file, "*/\n");
1995         FS_Print(file, builtinshaderstring);
1996         FS_Close(file);
1997
1998         Con_Printf("glsl/default.glsl written\n");
1999 }
2000
2001 void R_SetupShader_SetPermutation(unsigned int mode, unsigned int permutation)
2002 {
2003         r_glsl_permutation_t *perm = R_GLSL_FindPermutation(mode, permutation);
2004         if (r_glsl_permutation != perm)
2005         {
2006                 r_glsl_permutation = perm;
2007                 if (!r_glsl_permutation->program)
2008                 {
2009                         if (!r_glsl_permutation->compiled)
2010                                 R_GLSL_CompilePermutation(perm, mode, permutation);
2011                         if (!r_glsl_permutation->program)
2012                         {
2013                                 // remove features until we find a valid permutation
2014                                 int i;
2015                                 for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
2016                                 {
2017                                         // reduce i more quickly whenever it would not remove any bits
2018                                         int j = 1<<(SHADERPERMUTATION_COUNT-1-i);
2019                                         if (!(permutation & j))
2020                                                 continue;
2021                                         permutation -= j;
2022                                         r_glsl_permutation = R_GLSL_FindPermutation(mode, permutation);
2023                                         if (!r_glsl_permutation->compiled)
2024                                                 R_GLSL_CompilePermutation(perm, mode, permutation);
2025                                         if (r_glsl_permutation->program)
2026                                                 break;
2027                                 }
2028                                 if (i >= SHADERPERMUTATION_COUNT)
2029                                 {
2030                                         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");
2031                                         Cvar_SetValueQuick(&r_glsl, 0);
2032                                         R_GLSL_Restart_f(); // unload shaders
2033                                         return; // no bit left to clear
2034                                 }
2035                         }
2036                 }
2037                 CHECKGLERROR
2038                 qglUseProgramObjectARB(r_glsl_permutation->program);CHECKGLERROR
2039         }
2040 }
2041
2042 void R_SetupGenericShader(qboolean usetexture)
2043 {
2044         if (vid.support.arb_fragment_shader)
2045         {
2046                 if (r_glsl.integer && r_glsl_usegeneric.integer)
2047                         R_SetupShader_SetPermutation(SHADERMODE_GENERIC, usetexture ? SHADERPERMUTATION_DIFFUSE : 0);
2048                 else if (r_glsl_permutation)
2049                 {
2050                         r_glsl_permutation = NULL;
2051                         qglUseProgramObjectARB(0);CHECKGLERROR
2052                 }
2053         }
2054 }
2055
2056 void R_SetupGenericTwoTextureShader(int texturemode)
2057 {
2058         if (vid.support.arb_fragment_shader)
2059         {
2060                 if (r_glsl.integer && r_glsl_usegeneric.integer)
2061                         R_SetupShader_SetPermutation(SHADERMODE_GENERIC, SHADERPERMUTATION_DIFFUSE | SHADERPERMUTATION_SPECULAR | (r_shadow_glossexact.integer ? SHADERPERMUTATION_EXACTSPECULARMATH : 0) | (texturemode == GL_MODULATE ? SHADERPERMUTATION_COLORMAPPING : (texturemode == GL_ADD ? SHADERPERMUTATION_GLOW : (texturemode == GL_DECAL ? SHADERPERMUTATION_VERTEXTEXTUREBLEND : 0))));
2062                 else if (r_glsl_permutation)
2063                 {
2064                         r_glsl_permutation = NULL;
2065                         qglUseProgramObjectARB(0);CHECKGLERROR
2066                 }
2067         }
2068         if (!r_glsl_permutation)
2069         {
2070                 if (texturemode == GL_DECAL && gl_combine.integer)
2071                         texturemode = GL_INTERPOLATE_ARB;
2072                 R_Mesh_TexCombine(1, texturemode, texturemode, 1, 1);
2073         }
2074 }
2075
2076 void R_SetupDepthOrShadowShader(void)
2077 {
2078         if (vid.support.arb_fragment_shader)
2079         {
2080                 if (r_glsl.integer && r_glsl_usegeneric.integer)
2081                         R_SetupShader_SetPermutation(SHADERMODE_DEPTH_OR_SHADOW, 0);
2082                 else if (r_glsl_permutation)
2083                 {
2084                         r_glsl_permutation = NULL;
2085                         qglUseProgramObjectARB(0);CHECKGLERROR
2086                 }
2087         }
2088 }
2089
2090 void R_SetupShowDepthShader(void)
2091 {
2092         if (vid.support.arb_fragment_shader)
2093         {
2094                 if (r_glsl.integer && r_glsl_usegeneric.integer)
2095                         R_SetupShader_SetPermutation(SHADERMODE_SHOWDEPTH, 0);
2096                 else if (r_glsl_permutation)
2097                 {
2098                         r_glsl_permutation = NULL;
2099                         qglUseProgramObjectARB(0);CHECKGLERROR
2100                 }
2101         }
2102 }
2103
2104 extern rtexture_t *r_shadow_attenuationgradienttexture;
2105 extern rtexture_t *r_shadow_attenuation2dtexture;
2106 extern rtexture_t *r_shadow_attenuation3dtexture;
2107 extern qboolean r_shadow_usingshadowmaprect;
2108 extern qboolean r_shadow_usingshadowmapcube;
2109 extern qboolean r_shadow_usingshadowmap2d;
2110 extern float r_shadow_shadowmap_texturescale[2];
2111 extern float r_shadow_shadowmap_parameters[4];
2112 extern qboolean r_shadow_shadowmapvsdct;
2113 extern qboolean r_shadow_shadowmapsampler;
2114 extern int r_shadow_shadowmappcf;
2115 void R_SetupSurfaceShader(const vec3_t lightcolorbase, qboolean modellighting, float ambientscale, float diffusescale, float specularscale, rsurfacepass_t rsurfacepass)
2116 {
2117         // select a permutation of the lighting shader appropriate to this
2118         // combination of texture, entity, light source, and fogging, only use the
2119         // minimum features necessary to avoid wasting rendering time in the
2120         // fragment shader on features that are not being used
2121         unsigned int permutation = 0;
2122         unsigned int mode = 0;
2123         // TODO: implement geometry-shader based shadow volumes someday
2124         if (r_glsl_offsetmapping.integer)
2125         {
2126                 permutation |= SHADERPERMUTATION_OFFSETMAPPING;
2127                 if (r_glsl_offsetmapping_reliefmapping.integer)
2128                         permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;
2129         }
2130         if (rsurfacepass == RSURFPASS_BACKGROUND)
2131         {
2132                 // distorted background
2133                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_WATERSHADER)
2134                         mode = SHADERMODE_WATER;
2135                 else
2136                         mode = SHADERMODE_REFRACTION;
2137         }
2138         else if (rsurfacepass == RSURFPASS_RTLIGHT)
2139         {
2140                 // light source
2141                 mode = SHADERMODE_LIGHTSOURCE;
2142                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
2143                         permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
2144                 if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
2145                         permutation |= SHADERPERMUTATION_CUBEFILTER;
2146                 if (diffusescale > 0)
2147                         permutation |= SHADERPERMUTATION_DIFFUSE;
2148                 if (specularscale > 0)
2149                         permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
2150                 if (r_refdef.fogenabled)
2151                         permutation |= r_refdef.fogplaneviewabove ? SHADERPERMUTATION_FOGOUTSIDE : SHADERPERMUTATION_FOGINSIDE;
2152                 if (rsurface.texture->colormapping)
2153                         permutation |= SHADERPERMUTATION_COLORMAPPING;
2154                 if (r_shadow_usingshadowmaprect || r_shadow_usingshadowmap2d || r_shadow_usingshadowmapcube)
2155                 {
2156                         if (r_shadow_usingshadowmaprect)
2157                                 permutation |= SHADERPERMUTATION_SHADOWMAPRECT;
2158                         if (r_shadow_usingshadowmap2d)
2159                                 permutation |= SHADERPERMUTATION_SHADOWMAP2D;
2160                         if (r_shadow_usingshadowmapcube)
2161                                 permutation |= SHADERPERMUTATION_SHADOWMAPCUBE;
2162                         else if(r_shadow_shadowmapvsdct)
2163                                 permutation |= SHADERPERMUTATION_SHADOWMAPVSDCT;
2164
2165                         if (r_shadow_shadowmapsampler)
2166                                 permutation |= SHADERPERMUTATION_SHADOWSAMPLER;
2167                         if (r_shadow_shadowmappcf > 1)
2168                                 permutation |= SHADERPERMUTATION_SHADOWMAPPCF2;
2169                         else if (r_shadow_shadowmappcf)
2170                                 permutation |= SHADERPERMUTATION_SHADOWMAPPCF;
2171                 }
2172         }
2173         else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT)
2174         {
2175                 // unshaded geometry (fullbright or ambient model lighting)
2176                 mode = SHADERMODE_FLATCOLOR;
2177                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
2178                         permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
2179                 if (rsurface.texture->currentskinframe->glow && r_hdr_glowintensity.value > 0 && !gl_lightmaps.integer)
2180                         permutation |= SHADERPERMUTATION_GLOW;
2181                 if (r_refdef.fogenabled)
2182                         permutation |= r_refdef.fogplaneviewabove ? SHADERPERMUTATION_FOGOUTSIDE : SHADERPERMUTATION_FOGINSIDE;
2183                 if (rsurface.texture->colormapping)
2184                         permutation |= SHADERPERMUTATION_COLORMAPPING;
2185                 if (r_glsl_offsetmapping.integer)
2186                 {
2187                         permutation |= SHADERPERMUTATION_OFFSETMAPPING;
2188                         if (r_glsl_offsetmapping_reliefmapping.integer)
2189                                 permutation |= SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING;
2190                 }
2191                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
2192                         permutation |= SHADERPERMUTATION_REFLECTION;
2193         }
2194         else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT_DIRECTIONAL)
2195         {
2196                 // directional model lighting
2197                 mode = SHADERMODE_LIGHTDIRECTION;
2198                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
2199                         permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
2200                 if (rsurface.texture->currentskinframe->glow && r_hdr_glowintensity.value > 0 && !gl_lightmaps.integer)
2201                         permutation |= SHADERPERMUTATION_GLOW;
2202                 permutation |= SHADERPERMUTATION_DIFFUSE;
2203                 if (specularscale > 0)
2204                         permutation |= SHADERPERMUTATION_SPECULAR;
2205                 if (r_refdef.fogenabled)
2206                         permutation |= r_refdef.fogplaneviewabove ? SHADERPERMUTATION_FOGOUTSIDE : SHADERPERMUTATION_FOGINSIDE;
2207                 if (rsurface.texture->colormapping)
2208                         permutation |= SHADERPERMUTATION_COLORMAPPING;
2209                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
2210                         permutation |= SHADERPERMUTATION_REFLECTION;
2211         }
2212         else if (rsurface.texture->currentmaterialflags & MATERIALFLAG_MODELLIGHT)
2213         {
2214                 // ambient model lighting
2215                 mode = SHADERMODE_LIGHTDIRECTION;
2216                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
2217                         permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
2218                 if (rsurface.texture->currentskinframe->glow && r_hdr_glowintensity.value > 0 && !gl_lightmaps.integer)
2219                         permutation |= SHADERPERMUTATION_GLOW;
2220                 if (r_refdef.fogenabled)
2221                         permutation |= r_refdef.fogplaneviewabove ? SHADERPERMUTATION_FOGOUTSIDE : SHADERPERMUTATION_FOGINSIDE;
2222                 if (rsurface.texture->colormapping)
2223                         permutation |= SHADERPERMUTATION_COLORMAPPING;
2224                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
2225                         permutation |= SHADERPERMUTATION_REFLECTION;
2226         }
2227         else
2228         {
2229                 // lightmapped wall
2230                 if (r_glsl_deluxemapping.integer >= 1 && rsurface.uselightmaptexture && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brushq3.deluxemapping)
2231                 {
2232                         // deluxemapping (light direction texture)
2233                         if (rsurface.uselightmaptexture && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brushq3.deluxemapping && r_refdef.scene.worldmodel->brushq3.deluxemapping_modelspace)
2234                                 mode = SHADERMODE_LIGHTDIRECTIONMAP_MODELSPACE;
2235                         else
2236                                 mode = SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE;
2237                         permutation |= SHADERPERMUTATION_DIFFUSE;
2238                         if (specularscale > 0)
2239                                 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
2240                 }
2241                 else if (r_glsl_deluxemapping.integer >= 2)
2242                 {
2243                         // fake deluxemapping (uniform light direction in tangentspace)
2244                         mode = SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE;
2245                         permutation |= SHADERPERMUTATION_DIFFUSE;
2246                         if (specularscale > 0)
2247                                 permutation |= SHADERPERMUTATION_SPECULAR | SHADERPERMUTATION_DIFFUSE;
2248                 }
2249                 else if (rsurface.uselightmaptexture)
2250                 {
2251                         // ordinary lightmapping (q1bsp, q3bsp)
2252                         mode = SHADERMODE_LIGHTMAP;
2253                 }
2254                 else
2255                 {
2256                         // ordinary vertex coloring (q3bsp)
2257                         mode = SHADERMODE_VERTEXCOLOR;
2258                 }
2259                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_VERTEXTEXTUREBLEND)
2260                         permutation |= SHADERPERMUTATION_VERTEXTEXTUREBLEND;
2261                 if (rsurface.texture->currentskinframe->glow && r_hdr_glowintensity.value > 0 && !gl_lightmaps.integer)
2262                         permutation |= SHADERPERMUTATION_GLOW;
2263                 if (r_refdef.fogenabled)
2264                         permutation |= r_refdef.fogplaneviewabove ? SHADERPERMUTATION_FOGOUTSIDE : SHADERPERMUTATION_FOGINSIDE;
2265                 if (rsurface.texture->colormapping)
2266                         permutation |= SHADERPERMUTATION_COLORMAPPING;
2267                 if (rsurface.texture->currentmaterialflags & MATERIALFLAG_REFLECTION)
2268                         permutation |= SHADERPERMUTATION_REFLECTION;
2269         }
2270         if(permutation & SHADERPERMUTATION_SPECULAR)
2271                 if(r_shadow_glossexact.integer)
2272                         permutation |= SHADERPERMUTATION_EXACTSPECULARMATH;
2273         R_SetupShader_SetPermutation(mode, permutation);
2274         if (mode == SHADERMODE_LIGHTSOURCE)
2275         {
2276                 if (r_glsl_permutation->loc_LightPosition >= 0) qglUniform3fARB(r_glsl_permutation->loc_LightPosition, rsurface.entitylightorigin[0], rsurface.entitylightorigin[1], rsurface.entitylightorigin[2]);
2277                 if (permutation & SHADERPERMUTATION_DIFFUSE)
2278                 {
2279                         if (r_glsl_permutation->loc_TintColor >= 0) qglUniform4fARB(r_glsl_permutation->loc_TintColor, lightcolorbase[0], lightcolorbase[1], lightcolorbase[2], rsurface.texture->lightmapcolor[3]);
2280                         if (r_glsl_permutation->loc_AmbientScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_AmbientScale, ambientscale);
2281                         if (r_glsl_permutation->loc_DiffuseScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_DiffuseScale, diffusescale);
2282                         if (r_glsl_permutation->loc_SpecularScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularScale, specularscale);
2283                 }
2284                 else
2285                 {
2286                         // ambient only is simpler
2287                         if (r_glsl_permutation->loc_TintColor >= 0) qglUniform4fARB(r_glsl_permutation->loc_TintColor, lightcolorbase[0] * ambientscale, lightcolorbase[1] * ambientscale, lightcolorbase[2] * ambientscale, rsurface.texture->lightmapcolor[3]);
2288                         if (r_glsl_permutation->loc_AmbientScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_AmbientScale, 1);
2289                         if (r_glsl_permutation->loc_DiffuseScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_DiffuseScale, 0);
2290                         if (r_glsl_permutation->loc_SpecularScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularScale, 0);
2291                 }
2292                 // additive passes are only darkened by fog, not tinted
2293                 if (r_glsl_permutation->loc_FogColor >= 0)
2294                         qglUniform3fARB(r_glsl_permutation->loc_FogColor, 0, 0, 0);
2295                 if (r_glsl_permutation->loc_ShadowMap_TextureScale >= 0) qglUniform2fARB(r_glsl_permutation->loc_ShadowMap_TextureScale, r_shadow_shadowmap_texturescale[0], r_shadow_shadowmap_texturescale[1]);
2296                 if (r_glsl_permutation->loc_ShadowMap_Parameters >= 0) qglUniform4fARB(r_glsl_permutation->loc_ShadowMap_Parameters, r_shadow_shadowmap_parameters[0], r_shadow_shadowmap_parameters[1], r_shadow_shadowmap_parameters[2], r_shadow_shadowmap_parameters[3]);
2297         }
2298         else
2299         {
2300                 if (mode == SHADERMODE_LIGHTDIRECTION)
2301                 {
2302                         if (r_glsl_permutation->loc_AmbientColor  >= 0) qglUniform3fARB(r_glsl_permutation->loc_AmbientColor , rsurface.modellight_ambient[0] * ambientscale  * 0.5f, rsurface.modellight_ambient[1] * ambientscale  * 0.5f, rsurface.modellight_ambient[2] * ambientscale  * 0.5f);
2303                         if (r_glsl_permutation->loc_DiffuseColor  >= 0) qglUniform3fARB(r_glsl_permutation->loc_DiffuseColor , rsurface.modellight_diffuse[0] * diffusescale  * 0.5f, rsurface.modellight_diffuse[1] * diffusescale  * 0.5f, rsurface.modellight_diffuse[2] * diffusescale  * 0.5f);
2304                         if (r_glsl_permutation->loc_SpecularColor >= 0) qglUniform3fARB(r_glsl_permutation->loc_SpecularColor, rsurface.modellight_diffuse[0] * specularscale * 0.5f, rsurface.modellight_diffuse[1] * specularscale * 0.5f, rsurface.modellight_diffuse[2] * specularscale * 0.5f);
2305                         if (r_glsl_permutation->loc_LightDir      >= 0) qglUniform3fARB(r_glsl_permutation->loc_LightDir, rsurface.modellight_lightdir[0], rsurface.modellight_lightdir[1], rsurface.modellight_lightdir[2]);
2306                 }
2307                 else
2308                 {
2309                         if (r_glsl_permutation->loc_AmbientScale  >= 0) qglUniform1fARB(r_glsl_permutation->loc_AmbientScale, r_refdef.scene.ambient * 1.0f / 128.0f);
2310                         if (r_glsl_permutation->loc_DiffuseScale  >= 0) qglUniform1fARB(r_glsl_permutation->loc_DiffuseScale, r_refdef.lightmapintensity);
2311                         if (r_glsl_permutation->loc_SpecularScale >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularScale, r_refdef.lightmapintensity * specularscale);
2312                 }
2313                 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]);
2314                 if (r_glsl_permutation->loc_GlowColor >= 0) qglUniform3fARB(r_glsl_permutation->loc_GlowColor, rsurface.glowmod[0] * r_hdr_glowintensity.value, rsurface.glowmod[1] * r_hdr_glowintensity.value, rsurface.glowmod[2] * r_hdr_glowintensity.value);
2315                 // additive passes are only darkened by fog, not tinted
2316                 if (r_glsl_permutation->loc_FogColor >= 0)
2317                 {
2318                         if (rsurface.texture->currentmaterialflags & MATERIALFLAG_ADD)
2319                                 qglUniform3fARB(r_glsl_permutation->loc_FogColor, 0, 0, 0);
2320                         else
2321                                 qglUniform3fARB(r_glsl_permutation->loc_FogColor, r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2]);
2322                 }
2323                 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);
2324                 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]);
2325                 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]);
2326                 if (r_glsl_permutation->loc_RefractColor >= 0) qglUniform4fvARB(r_glsl_permutation->loc_RefractColor, 1, rsurface.texture->refractcolor4f);
2327                 if (r_glsl_permutation->loc_ReflectColor >= 0) qglUniform4fvARB(r_glsl_permutation->loc_ReflectColor, 1, rsurface.texture->reflectcolor4f);
2328                 if (r_glsl_permutation->loc_ReflectFactor >= 0) qglUniform1fARB(r_glsl_permutation->loc_ReflectFactor, rsurface.texture->reflectmax - rsurface.texture->reflectmin);
2329                 if (r_glsl_permutation->loc_ReflectOffset >= 0) qglUniform1fARB(r_glsl_permutation->loc_ReflectOffset, rsurface.texture->reflectmin);
2330         }
2331         if (r_glsl_permutation->loc_SceneBrightness >= 0) qglUniform1fARB(r_glsl_permutation->loc_SceneBrightness, r_refdef.view.colorscale);
2332         if (r_glsl_permutation->loc_EyePosition >= 0) qglUniform3fARB(r_glsl_permutation->loc_EyePosition, rsurface.localvieworigin[0], rsurface.localvieworigin[1], rsurface.localvieworigin[2]);
2333         if (r_glsl_permutation->loc_Color_Pants >= 0)
2334         {
2335                 if (rsurface.texture->currentskinframe->pants)
2336                         qglUniform3fARB(r_glsl_permutation->loc_Color_Pants, rsurface.colormap_pantscolor[0], rsurface.colormap_pantscolor[1], rsurface.colormap_pantscolor[2]);
2337                 else
2338                         qglUniform3fARB(r_glsl_permutation->loc_Color_Pants, 0, 0, 0);
2339         }
2340         if (r_glsl_permutation->loc_Color_Shirt >= 0)
2341         {
2342                 if (rsurface.texture->currentskinframe->shirt)
2343                         qglUniform3fARB(r_glsl_permutation->loc_Color_Shirt, rsurface.colormap_shirtcolor[0], rsurface.colormap_shirtcolor[1], rsurface.colormap_shirtcolor[2]);
2344                 else
2345                         qglUniform3fARB(r_glsl_permutation->loc_Color_Shirt, 0, 0, 0);
2346         }
2347         if (r_glsl_permutation->loc_FogPlane >= 0) qglUniform4fARB(r_glsl_permutation->loc_FogPlane, rsurface.fogplane[0], rsurface.fogplane[1], rsurface.fogplane[2], rsurface.fogplane[3]);
2348         if (r_glsl_permutation->loc_FogPlaneViewDist >= 0) qglUniform1fARB(r_glsl_permutation->loc_FogPlaneViewDist, rsurface.fogplaneviewdist);
2349         if (r_glsl_permutation->loc_FogRangeRecip >= 0) qglUniform1fARB(r_glsl_permutation->loc_FogRangeRecip, rsurface.fograngerecip);
2350         if (r_glsl_permutation->loc_FogHeightFade >= 0) qglUniform1fARB(r_glsl_permutation->loc_FogHeightFade, rsurface.fogheightfade);
2351         if(permutation & SHADERPERMUTATION_EXACTSPECULARMATH)
2352         {
2353                 if (r_glsl_permutation->loc_SpecularPower >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularPower, rsurface.texture->specularpower * 0.25);
2354         }
2355         else
2356         {
2357                 if (r_glsl_permutation->loc_SpecularPower >= 0) qglUniform1fARB(r_glsl_permutation->loc_SpecularPower, rsurface.texture->specularpower);
2358         }
2359         if (r_glsl_permutation->loc_OffsetMapping_Scale >= 0) qglUniform1fARB(r_glsl_permutation->loc_OffsetMapping_Scale, r_glsl_offsetmapping_scale.value);
2360         CHECKGLERROR
2361 }
2362
2363 #define SKINFRAME_HASH 1024
2364
2365 typedef struct
2366 {
2367         int loadsequence; // incremented each level change
2368         memexpandablearray_t array;
2369         skinframe_t *hash[SKINFRAME_HASH];
2370 }
2371 r_skinframe_t;
2372 r_skinframe_t r_skinframe;
2373
2374 void R_SkinFrame_PrepareForPurge(void)
2375 {
2376         r_skinframe.loadsequence++;
2377         // wrap it without hitting zero
2378         if (r_skinframe.loadsequence >= 200)
2379                 r_skinframe.loadsequence = 1;
2380 }
2381
2382 void R_SkinFrame_MarkUsed(skinframe_t *skinframe)
2383 {
2384         if (!skinframe)
2385                 return;
2386         // mark the skinframe as used for the purging code
2387         skinframe->loadsequence = r_skinframe.loadsequence;
2388 }
2389
2390 void R_SkinFrame_Purge(void)
2391 {
2392         int i;
2393         skinframe_t *s;
2394         for (i = 0;i < SKINFRAME_HASH;i++)
2395         {
2396                 for (s = r_skinframe.hash[i];s;s = s->next)
2397                 {
2398                         if (s->loadsequence && s->loadsequence != r_skinframe.loadsequence)
2399                         {
2400                                 if (s->merged == s->base)
2401                                         s->merged = NULL;
2402                                 // FIXME: maybe pass a pointer to the pointer to R_PurgeTexture and reset it to NULL inside? [11/29/2007 Black]
2403                                 R_PurgeTexture(s->stain );s->stain  = NULL;
2404                                 R_PurgeTexture(s->merged);s->merged = NULL;
2405                                 R_PurgeTexture(s->base  );s->base   = NULL;
2406                                 R_PurgeTexture(s->pants );s->pants  = NULL;
2407                                 R_PurgeTexture(s->shirt );s->shirt  = NULL;
2408                                 R_PurgeTexture(s->nmap  );s->nmap   = NULL;
2409                                 R_PurgeTexture(s->gloss );s->gloss  = NULL;
2410                                 R_PurgeTexture(s->glow  );s->glow   = NULL;
2411                                 R_PurgeTexture(s->fog   );s->fog    = NULL;
2412                                 s->loadsequence = 0;
2413                         }
2414                 }
2415         }
2416 }
2417
2418 skinframe_t *R_SkinFrame_FindNextByName( skinframe_t *last, const char *name ) {
2419         skinframe_t *item;
2420         char basename[MAX_QPATH];
2421
2422         Image_StripImageExtension(name, basename, sizeof(basename));
2423
2424         if( last == NULL ) {
2425                 int hashindex;
2426                 hashindex = CRC_Block((unsigned char *)basename, strlen(basename)) & (SKINFRAME_HASH - 1);
2427                 item = r_skinframe.hash[hashindex];
2428         } else {
2429                 item = last->next;
2430         }
2431
2432         // linearly search through the hash bucket
2433         for( ; item ; item = item->next ) {
2434                 if( !strcmp( item->basename, basename ) ) {
2435                         return item;
2436                 }
2437         }
2438         return NULL;
2439 }
2440
2441 skinframe_t *R_SkinFrame_Find(const char *name, int textureflags, int comparewidth, int compareheight, int comparecrc, qboolean add)
2442 {
2443         skinframe_t *item;
2444         int hashindex;
2445         char basename[MAX_QPATH];
2446
2447         Image_StripImageExtension(name, basename, sizeof(basename));
2448
2449         hashindex = CRC_Block((unsigned char *)basename, strlen(basename)) & (SKINFRAME_HASH - 1);
2450         for (item = r_skinframe.hash[hashindex];item;item = item->next)
2451                 if (!strcmp(item->basename, basename) && item->textureflags == textureflags && item->comparewidth == comparewidth && item->compareheight == compareheight && item->comparecrc == comparecrc)
2452                         break;
2453
2454         if (!item) {
2455                 rtexture_t *dyntexture;
2456                 // check whether its a dynamic texture
2457                 dyntexture = CL_GetDynTexture( basename );
2458                 if (!add && !dyntexture)
2459                         return NULL;
2460                 item = (skinframe_t *)Mem_ExpandableArray_AllocRecord(&r_skinframe.array);
2461                 memset(item, 0, sizeof(*item));
2462                 strlcpy(item->basename, basename, sizeof(item->basename));
2463                 item->base = dyntexture; // either NULL or dyntexture handle
2464                 item->textureflags = textureflags;
2465                 item->comparewidth = comparewidth;
2466                 item->compareheight = compareheight;
2467                 item->comparecrc = comparecrc;
2468                 item->next = r_skinframe.hash[hashindex];
2469                 r_skinframe.hash[hashindex] = item;
2470         }
2471         else if( item->base == NULL )
2472         {
2473                 rtexture_t *dyntexture;
2474                 // check whether its a dynamic texture
2475                 // 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]
2476                 dyntexture = CL_GetDynTexture( basename );
2477                 item->base = dyntexture; // either NULL or dyntexture handle
2478         }
2479
2480         R_SkinFrame_MarkUsed(item);
2481         return item;
2482 }
2483
2484 #define R_SKINFRAME_LOAD_AVERAGE_COLORS(cnt, getpixel) \
2485         { \
2486                 unsigned long long avgcolor[5], wsum; \
2487                 int pix, comp, w; \
2488                 avgcolor[0] = 0; \
2489                 avgcolor[1] = 0; \
2490                 avgcolor[2] = 0; \
2491                 avgcolor[3] = 0; \
2492                 avgcolor[4] = 0; \
2493                 wsum = 0; \
2494                 for(pix = 0; pix < cnt; ++pix) \
2495                 { \
2496                         w = 0; \
2497                         for(comp = 0; comp < 3; ++comp) \
2498                                 w += getpixel; \
2499                         if(w) /* ignore perfectly black pixels because that is better for model skins */ \
2500                         { \
2501                                 ++wsum; \
2502                                 /* comp = 3; -- not needed, comp is always 3 when we get here */ \
2503                                 w = getpixel; \
2504                                 for(comp = 0; comp < 3; ++comp) \
2505                                         avgcolor[comp] += getpixel * w; \
2506                                 avgcolor[3] += w; \
2507                         } \
2508                         /* comp = 3; -- not needed, comp is always 3 when we get here */ \
2509                         avgcolor[4] += getpixel; \
2510                 } \
2511                 if(avgcolor[3] == 0) /* no pixels seen? even worse */ \
2512                         avgcolor[3] = 1; \
2513                 skinframe->avgcolor[0] = avgcolor[2] / (255.0 * avgcolor[3]); \
2514                 skinframe->avgcolor[1] = avgcolor[1] / (255.0 * avgcolor[3]); \
2515                 skinframe->avgcolor[2] = avgcolor[0] / (255.0 * avgcolor[3]); \
2516                 skinframe->avgcolor[3] = avgcolor[4] / (255.0 * cnt); \
2517         }
2518
2519 skinframe_t *R_SkinFrame_LoadExternal(const char *name, int textureflags, qboolean complain)
2520 {
2521         int j;
2522         unsigned char *pixels;
2523         unsigned char *bumppixels;
2524         unsigned char *basepixels = NULL;
2525         int basepixels_width;
2526         int basepixels_height;
2527         skinframe_t *skinframe;
2528
2529         if (cls.state == ca_dedicated)
2530                 return NULL;
2531
2532         // return an existing skinframe if already loaded
2533         // if loading of the first image fails, don't make a new skinframe as it
2534         // would cause all future lookups of this to be missing
2535         skinframe = R_SkinFrame_Find(name, textureflags, 0, 0, 0, false);
2536         if (skinframe && skinframe->base)
2537                 return skinframe;
2538
2539         basepixels = loadimagepixelsbgra(name, complain, true);
2540         if (basepixels == NULL)
2541                 return NULL;
2542
2543         if (developer_loading.integer)
2544                 Con_Printf("loading skin \"%s\"\n", name);
2545
2546         // we've got some pixels to store, so really allocate this new texture now
2547         if (!skinframe)
2548                 skinframe = R_SkinFrame_Find(name, textureflags, 0, 0, 0, true);
2549         skinframe->stain = NULL;
2550         skinframe->merged = NULL;
2551         skinframe->base = r_texture_notexture;
2552         skinframe->pants = NULL;
2553         skinframe->shirt = NULL;
2554         skinframe->nmap = r_texture_blanknormalmap;
2555         skinframe->gloss = NULL;
2556         skinframe->glow = NULL;
2557         skinframe->fog = NULL;
2558         skinframe->hasalpha = false;
2559
2560         basepixels_width = image_width;
2561         basepixels_height = image_height;
2562         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);
2563
2564         if (textureflags & TEXF_ALPHA)
2565         {
2566                 for (j = 3;j < basepixels_width * basepixels_height * 4;j += 4)
2567                 {
2568                         if (basepixels[j] < 255)
2569                         {
2570                                 skinframe->hasalpha = true;
2571                                 break;
2572                         }
2573                 }
2574                 if (r_loadfog && skinframe->hasalpha)
2575                 {
2576                         // has transparent pixels
2577                         pixels = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
2578                         for (j = 0;j < image_width * image_height * 4;j += 4)
2579                         {
2580                                 pixels[j+0] = 255;
2581                                 pixels[j+1] = 255;
2582                                 pixels[j+2] = 255;
2583                                 pixels[j+3] = basepixels[j+3];
2584                         }
2585                         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);
2586                         Mem_Free(pixels);
2587                 }
2588         }
2589
2590         R_SKINFRAME_LOAD_AVERAGE_COLORS(basepixels_width * basepixels_height, basepixels[4 * pix + comp]);
2591         //Con_Printf("Texture %s has average colors %f %f %f alpha %f\n", name, skinframe->avgcolor[0], skinframe->avgcolor[1], skinframe->avgcolor[2], skinframe->avgcolor[3]);
2592
2593         // _norm is the name used by tenebrae and has been adopted as standard
2594         if (r_loadnormalmap)
2595         {
2596                 if ((pixels = loadimagepixelsbgra(va("%s_norm", skinframe->basename), false, false)) != NULL)
2597                 {
2598                         skinframe->nmap = R_LoadTexture2D (r_main_texturepool, va("%s_nmap", skinframe->basename), image_width, image_height, pixels, TEXTYPE_BGRA, (TEXF_ALPHA | skinframe->textureflags) & (gl_texturecompression_normal.integer ? ~0 : ~TEXF_COMPRESS), NULL);
2599                         Mem_Free(pixels);
2600                         pixels = NULL;
2601                 }
2602                 else if (r_shadow_bumpscale_bumpmap.value > 0 && (bumppixels = loadimagepixelsbgra(va("%s_bump", skinframe->basename), false, false)) != NULL)
2603                 {
2604                         pixels = (unsigned char *)Mem_Alloc(tempmempool, image_width * image_height * 4);
2605                         Image_HeightmapToNormalmap_BGRA(bumppixels, pixels, image_width, image_height, false, r_shadow_bumpscale_bumpmap.value);
2606                         skinframe->nmap = R_LoadTexture2D (r_main_texturepool, va("%s_nmap", skinframe->basename), image_width, image_height, pixels, TEXTYPE_BGRA, (TEXF_ALPHA | skinframe->textureflags) & (gl_texturecompression_normal.integer ? ~0 : ~TEXF_COMPRESS), NULL);
2607                         Mem_Free(pixels);
2608                         Mem_Free(bumppixels);
2609                 }
2610                 else if (r_shadow_bumpscale_basetexture.value > 0)
2611                 {
2612                         pixels = (unsigned char *)Mem_Alloc(tempmempool, basepixels_width * basepixels_height * 4);
2613                         Image_HeightmapToNormalmap_BGRA(basepixels, pixels, basepixels_width, basepixels_height, false, r_shadow_bumpscale_basetexture.value);
2614                         skinframe->nmap = R_LoadTexture2D (r_main_texturepool, va("%s_nmap", skinframe->basename), basepixels_width, basepixels_height, pixels, TEXTYPE_BGRA, (TEXF_ALPHA | skinframe->textureflags) & (gl_texturecompression_normal.integer ? ~0 : ~TEXF_COMPRESS), NULL);
2615                         Mem_Free(pixels);
2616                 }
2617         }
2618         // _luma is supported for tenebrae compatibility
2619         // (I think it's a very stupid name, but oh well)
2620         // _glow is the preferred name
2621         if ((pixels = loadimagepixelsbgra(va("%s_glow",  skinframe->basename), false, false)) || (pixels = loadimagepixelsbgra(va("%s_luma", skinframe->basename), false, false))) {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;}
2622         if (r_loadgloss && (pixels = loadimagepixelsbgra(va("%s_gloss", skinframe->basename), false, false))) {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;}
2623         if ((pixels = loadimagepixelsbgra(va("%s_pants", skinframe->basename), false, false))) {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;}
2624         if ((pixels = loadimagepixelsbgra(va("%s_shirt", skinframe->basename), false, false))) {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;}
2625
2626         if (basepixels)
2627                 Mem_Free(basepixels);
2628
2629         return skinframe;
2630 }
2631
2632 // this is only used by .spr32 sprites, HL .spr files, HL .bsp files
2633 skinframe_t *R_SkinFrame_LoadInternalBGRA(const char *name, int textureflags, const unsigned char *skindata, int width, int height)
2634 {
2635         int i;
2636         unsigned char *temp1, *temp2;
2637         skinframe_t *skinframe;
2638
2639         if (cls.state == ca_dedicated)
2640                 return NULL;
2641
2642         // if already loaded just return it, otherwise make a new skinframe
2643         skinframe = R_SkinFrame_Find(name, textureflags, width, height, skindata ? CRC_Block(skindata, width*height*4) : 0, true);
2644         if (skinframe && skinframe->base)
2645                 return skinframe;
2646
2647         skinframe->stain = NULL;
2648         skinframe->merged = NULL;
2649         skinframe->base = r_texture_notexture;
2650         skinframe->pants = NULL;
2651         skinframe->shirt = NULL;
2652         skinframe->nmap = r_texture_blanknormalmap;
2653         skinframe->gloss = NULL;
2654         skinframe->glow = NULL;
2655         skinframe->fog = NULL;
2656         skinframe->hasalpha = false;
2657
2658         // if no data was provided, then clearly the caller wanted to get a blank skinframe
2659         if (!skindata)
2660                 return NULL;
2661
2662         if (developer_loading.integer)
2663                 Con_Printf("loading 32bit skin \"%s\"\n", name);
2664
2665         if (r_loadnormalmap && r_shadow_bumpscale_basetexture.value > 0)
2666         {
2667                 temp1 = (unsigned char *)Mem_Alloc(tempmempool, width * height * 8);
2668                 temp2 = temp1 + width * height * 4;
2669                 Image_HeightmapToNormalmap_BGRA(skindata, temp2, width, height, false, r_shadow_bumpscale_basetexture.value);
2670                 skinframe->nmap = R_LoadTexture2D(r_main_texturepool, va("%s_nmap", skinframe->basename), width, height, temp2, TEXTYPE_BGRA, skinframe->textureflags | TEXF_ALPHA, NULL);
2671                 Mem_Free(temp1);
2672         }
2673         skinframe->base = skinframe->merged = R_LoadTexture2D(r_main_texturepool, skinframe->basename, width, height, skindata, TEXTYPE_BGRA, skinframe->textureflags, NULL);
2674         if (textureflags & TEXF_ALPHA)
2675         {
2676                 for (i = 3;i < width * height * 4;i += 4)
2677                 {
2678                         if (skindata[i] < 255)
2679                         {
2680                                 skinframe->hasalpha = true;
2681                                 break;
2682                         }
2683                 }
2684                 if (r_loadfog && skinframe->hasalpha)
2685                 {
2686                         unsigned char *fogpixels = (unsigned char *)Mem_Alloc(tempmempool, width * height * 4);
2687                         memcpy(fogpixels, skindata, width * height * 4);
2688                         for (i = 0;i < width * height * 4;i += 4)
2689                                 fogpixels[i] = fogpixels[i+1] = fogpixels[i+2] = 255;
2690                         skinframe->fog = R_LoadTexture2D(r_main_texturepool, va("%s_fog", skinframe->basename), width, height, fogpixels, TEXTYPE_BGRA, skinframe->textureflags, NULL);
2691                         Mem_Free(fogpixels);
2692                 }
2693         }
2694
2695         R_SKINFRAME_LOAD_AVERAGE_COLORS(width * height, skindata[4 * pix + comp]);
2696         //Con_Printf("Texture %s has average colors %f %f %f alpha %f\n", name, skinframe->avgcolor[0], skinframe->avgcolor[1], skinframe->avgcolor[2], skinframe->avgcolor[3]);
2697
2698         return skinframe;
2699 }
2700
2701 skinframe_t *R_SkinFrame_LoadInternalQuake(const char *name, int textureflags, int loadpantsandshirt, int loadglowtexture, const unsigned char *skindata, int width, int height)
2702 {
2703         int i;
2704         int featuresmask;
2705         skinframe_t *skinframe;
2706
2707         if (cls.state == ca_dedicated)
2708                 return NULL;
2709
2710         // if already loaded just return it, otherwise make a new skinframe
2711         skinframe = R_SkinFrame_Find(name, textureflags, width, height, skindata ? CRC_Block(skindata, width*height) : 0, true);
2712         if (skinframe && skinframe->base)
2713                 return skinframe;
2714
2715         skinframe->stain = NULL;
2716         skinframe->merged = NULL;
2717         skinframe->base = r_texture_notexture;
2718         skinframe->pants = NULL;
2719         skinframe->shirt = NULL;
2720         skinframe->nmap = r_texture_blanknormalmap;
2721         skinframe->gloss = NULL;
2722         skinframe->glow = NULL;
2723         skinframe->fog = NULL;
2724         skinframe->hasalpha = false;
2725
2726         // if no data was provided, then clearly the caller wanted to get a blank skinframe
2727         if (!skindata)
2728                 return NULL;
2729
2730         if (developer_loading.integer)
2731                 Con_Printf("loading quake skin \"%s\"\n", name);
2732
2733         // we actually don't upload anything until the first use, because mdl skins frequently go unused, and are almost never used in both modes (colormapped and non-colormapped)
2734         skinframe->qpixels = Mem_Alloc(r_main_mempool, width*height);
2735         memcpy(skinframe->qpixels, skindata, width*height);
2736         skinframe->qwidth = width;
2737         skinframe->qheight = height;
2738
2739         featuresmask = 0;
2740         for (i = 0;i < width * height;i++)
2741                 featuresmask |= palette_featureflags[skindata[i]];
2742
2743         skinframe->hasalpha = false;
2744         skinframe->qhascolormapping = loadpantsandshirt && (featuresmask & (PALETTEFEATURE_PANTS | PALETTEFEATURE_SHIRT));
2745         skinframe->qgeneratenmap = r_shadow_bumpscale_basetexture.value > 0;
2746         skinframe->qgeneratemerged = true;
2747         skinframe->qgeneratebase = skinframe->qhascolormapping;
2748         skinframe->qgenerateglow = loadglowtexture && (featuresmask & PALETTEFEATURE_GLOW);
2749
2750         R_SKINFRAME_LOAD_AVERAGE_COLORS(width * height, ((unsigned char *)palette_bgra_complete)[skindata[pix]*4 + comp]);
2751         //Con_Printf("Texture %s has average colors %f %f %f alpha %f\n", name, skinframe->avgcolor[0], skinframe->avgcolor[1], skinframe->avgcolor[2], skinframe->avgcolor[3]);
2752
2753         return skinframe;
2754 }
2755
2756 static void R_SkinFrame_GenerateTexturesFromQPixels(skinframe_t *skinframe, qboolean colormapped)
2757 {
2758         int width;
2759         int height;
2760         unsigned char *skindata;
2761
2762         if (!skinframe->qpixels)
2763                 return;
2764
2765         if (!skinframe->qhascolormapping)
2766                 colormapped = false;
2767
2768         if (colormapped)
2769         {
2770                 if (!skinframe->qgeneratebase)
2771                         return;
2772         }
2773         else
2774         {
2775                 if (!skinframe->qgeneratemerged)
2776                         return;
2777         }
2778
2779         width = skinframe->qwidth;
2780         height = skinframe->qheight;
2781         skindata = skinframe->qpixels;
2782
2783         if (skinframe->qgeneratenmap)
2784         {
2785                 unsigned char *temp1, *temp2;
2786                 skinframe->qgeneratenmap = false;
2787                 temp1 = (unsigned char *)Mem_Alloc(tempmempool, width * height * 8);
2788                 temp2 = temp1 + width * height * 4;
2789                 // use either a custom palette or the quake palette
2790                 Image_Copy8bitBGRA(skindata, temp1, width * height, palette_bgra_complete);
2791                 Image_HeightmapToNormalmap_BGRA(temp1, temp2, width, height, false, r_shadow_bumpscale_basetexture.value);
2792                 skinframe->nmap = R_LoadTexture2D(r_main_texturepool, va("%s_nmap", skinframe->basename), width, height, temp2, TEXTYPE_BGRA, skinframe->textureflags | TEXF_ALPHA, NULL);
2793                 Mem_Free(temp1);
2794         }
2795
2796         if (skinframe->qgenerateglow)
2797         {
2798                 skinframe->qgenerateglow = false;
2799                 skinframe->glow = R_LoadTexture2D(r_main_texturepool, va("%s_glow", skinframe->basename), width, height, skindata, TEXTYPE_PALETTE, skinframe->textureflags, palette_bgra_onlyfullbrights); // glow
2800         }
2801
2802         if (colormapped)
2803         {
2804                 skinframe->qgeneratebase = false;
2805                 skinframe->base  = R_LoadTexture2D(r_main_texturepool, va("%s_nospecial", skinframe->basename), width, height, skindata, TEXTYPE_PALETTE, skinframe->textureflags, skinframe->glow ? palette_bgra_nocolormapnofullbrights : palette_bgra_nocolormap);
2806                 skinframe->pants = R_LoadTexture2D(r_main_texturepool, va("%s_pants", skinframe->basename), width, height, skindata, TEXTYPE_PALETTE, skinframe->textureflags, palette_bgra_pantsaswhite);
2807                 skinframe->shirt = R_LoadTexture2D(r_main_texturepool, va("%s_shirt", skinframe->basename), width, height, skindata, TEXTYPE_PALETTE, skinframe->textureflags, palette_bgra_shirtaswhite);
2808         }
2809         else
2810         {
2811                 skinframe->qgeneratemerged = false;
2812                 skinframe->merged = R_LoadTexture2D(r_main_texturepool, skinframe->basename, width, height, skindata, TEXTYPE_PALETTE, skinframe->textureflags, skinframe->glow ? palette_bgra_nofullbrights : palette_bgra_complete);
2813         }
2814
2815         if (!skinframe->qgeneratemerged && !skinframe->qgeneratebase)
2816         {
2817                 Mem_Free(skinframe->qpixels);
2818                 skinframe->qpixels = NULL;
2819         }
2820 }
2821
2822 skinframe_t *R_SkinFrame_LoadInternal8bit(const char *name, int textureflags, const unsigned char *skindata, int width, int height, const unsigned int *palette, const unsigned int *alphapalette)
2823 {
2824         int i;
2825         skinframe_t *skinframe;
2826
2827         if (cls.state == ca_dedicated)
2828                 return NULL;
2829
2830         // if already loaded just return it, otherwise make a new skinframe
2831         skinframe = R_SkinFrame_Find(name, textureflags, width, height, skindata ? CRC_Block(skindata, width*height) : 0, true);
2832         if (skinframe && skinframe->base)
2833                 return skinframe;
2834
2835         skinframe->stain = NULL;
2836         skinframe->merged = NULL;
2837         skinframe->base = r_texture_notexture;
2838         skinframe->pants = NULL;
2839         skinframe->shirt = NULL;
2840         skinframe->nmap = r_texture_blanknormalmap;
2841         skinframe->gloss = NULL;
2842         skinframe->glow = NULL;
2843         skinframe->fog = NULL;
2844         skinframe->hasalpha = false;
2845
2846         // if no data was provided, then clearly the caller wanted to get a blank skinframe
2847         if (!skindata)
2848                 return NULL;
2849
2850         if (developer_loading.integer)
2851                 Con_Printf("loading embedded 8bit image \"%s\"\n", name);
2852
2853         skinframe->base = skinframe->merged = R_LoadTexture2D(r_main_texturepool, skinframe->basename, width, height, skindata, TEXTYPE_PALETTE, skinframe->textureflags, palette);
2854         if (textureflags & TEXF_ALPHA)
2855         {
2856                 for (i = 0;i < width * height;i++)
2857                 {
2858                         if (((unsigned char *)palette)[skindata[i]*4+3] < 255)
2859                         {
2860                                 skinframe->hasalpha = true;
2861                                 break;
2862                         }
2863                 }
2864                 if (r_loadfog && skinframe->hasalpha)
2865                         skinframe->fog = R_LoadTexture2D(r_main_texturepool, va("%s_fog", skinframe->basename), width, height, skindata, TEXTYPE_PALETTE, skinframe->textureflags, alphapalette);
2866         }
2867
2868         R_SKINFRAME_LOAD_AVERAGE_COLORS(width * height, ((unsigned char *)palette)[skindata[pix]*4 + comp]);
2869         //Con_Printf("Texture %s has average colors %f %f %f alpha %f\n", name, skinframe->avgcolor[0], skinframe->avgcolor[1], skinframe->avgcolor[2], skinframe->avgcolor[3]);
2870
2871         return skinframe;
2872 }
2873
2874 skinframe_t *R_SkinFrame_LoadMissing(void)
2875 {
2876         skinframe_t *skinframe;
2877
2878         if (cls.state == ca_dedicated)
2879                 return NULL;
2880
2881         skinframe = R_SkinFrame_Find("missing", TEXF_PRECACHE | TEXF_FORCENEAREST, 0, 0, 0, true);
2882         skinframe->stain = NULL;
2883         skinframe->merged = NULL;
2884         skinframe->base = r_texture_notexture;
2885         skinframe->pants = NULL;
2886         skinframe->shirt = NULL;
2887         skinframe->nmap = r_texture_blanknormalmap;
2888         skinframe->gloss = NULL;
2889         skinframe->glow = NULL;
2890         skinframe->fog = NULL;
2891         skinframe->hasalpha = false;
2892
2893         skinframe->avgcolor[0] = rand() / RAND_MAX;
2894         skinframe->avgcolor[1] = rand() / RAND_MAX;
2895         skinframe->avgcolor[2] = rand() / RAND_MAX;
2896         skinframe->avgcolor[3] = 1;
2897
2898         return skinframe;
2899 }
2900
2901 void R_Main_FreeViewCache(void)
2902 {
2903         if (r_refdef.viewcache.entityvisible)
2904                 Mem_Free(r_refdef.viewcache.entityvisible);
2905         if (r_refdef.viewcache.world_pvsbits)
2906                 Mem_Free(r_refdef.viewcache.world_pvsbits);
2907         if (r_refdef.viewcache.world_leafvisible)
2908                 Mem_Free(r_refdef.viewcache.world_leafvisible);
2909         if (r_refdef.viewcache.world_surfacevisible)
2910                 Mem_Free(r_refdef.viewcache.world_surfacevisible);
2911         memset(&r_refdef.viewcache, 0, sizeof(r_refdef.viewcache));
2912 }
2913
2914 void R_Main_ResizeViewCache(void)
2915 {
2916         int numentities = r_refdef.scene.numentities;
2917         int numclusters = r_refdef.scene.worldmodel ? r_refdef.scene.worldmodel->brush.num_pvsclusters : 1;
2918         int numclusterbytes = r_refdef.scene.worldmodel ? r_refdef.scene.worldmodel->brush.num_pvsclusterbytes : 1;
2919         int numleafs = r_refdef.scene.worldmodel ? r_refdef.scene.worldmodel->brush.num_leafs : 1;
2920         int numsurfaces = r_refdef.scene.worldmodel ? r_refdef.scene.worldmodel->num_surfaces : 1;
2921         if (r_refdef.viewcache.maxentities < numentities)
2922         {
2923                 r_refdef.viewcache.maxentities = numentities;
2924                 if (r_refdef.viewcache.entityvisible)
2925                         Mem_Free(r_refdef.viewcache.entityvisible);
2926                 r_refdef.viewcache.entityvisible = Mem_Alloc(r_main_mempool, r_refdef.viewcache.maxentities);
2927         }
2928         if (r_refdef.viewcache.world_numclusters != numclusters)
2929         {
2930                 r_refdef.viewcache.world_numclusters = numclusters;
2931                 r_refdef.viewcache.world_numclusterbytes = numclusterbytes;
2932                 if (r_refdef.viewcache.world_pvsbits)
2933                         Mem_Free(r_refdef.viewcache.world_pvsbits);
2934                 r_refdef.viewcache.world_pvsbits = Mem_Alloc(r_main_mempool, r_refdef.viewcache.world_numclusterbytes);
2935         }
2936         if (r_refdef.viewcache.world_numleafs != numleafs)
2937         {
2938                 r_refdef.viewcache.world_numleafs = numleafs;
2939                 if (r_refdef.viewcache.world_leafvisible)
2940                         Mem_Free(r_refdef.viewcache.world_leafvisible);
2941                 r_refdef.viewcache.world_leafvisible = Mem_Alloc(r_main_mempool, r_refdef.viewcache.world_numleafs);
2942         }
2943         if (r_refdef.viewcache.world_numsurfaces != numsurfaces)
2944         {
2945                 r_refdef.viewcache.world_numsurfaces = numsurfaces;
2946                 if (r_refdef.viewcache.world_surfacevisible)
2947                         Mem_Free(r_refdef.viewcache.world_surfacevisible);
2948                 r_refdef.viewcache.world_surfacevisible = Mem_Alloc(r_main_mempool, r_refdef.viewcache.world_numsurfaces);
2949         }
2950 }
2951
2952 void gl_main_start(void)
2953 {
2954         r_loadnormalmap = r_loadgloss = vid.support.arb_texture_env_dot3 || vid.support.arb_fragment_shader;
2955         r_loadfog = true;
2956
2957         r_numqueries = 0;
2958         r_maxqueries = 0;
2959         memset(r_queries, 0, sizeof(r_queries));
2960
2961         r_qwskincache = NULL;
2962         r_qwskincache_size = 0;
2963
2964         // set up r_skinframe loading system for textures
2965         memset(&r_skinframe, 0, sizeof(r_skinframe));
2966         r_skinframe.loadsequence = 1;
2967         Mem_ExpandableArray_NewArray(&r_skinframe.array, r_main_mempool, sizeof(skinframe_t), 256);
2968
2969         r_main_texturepool = R_AllocTexturePool();
2970         R_BuildBlankTextures();
2971         R_BuildNoTexture();
2972         if (vid.support.arb_texture_cube_map)
2973         {
2974                 R_BuildWhiteCube();
2975                 R_BuildNormalizationCube();
2976         }
2977         r_texture_fogattenuation = NULL;
2978         r_texture_gammaramps = NULL;
2979         //r_texture_fogintensity = NULL;
2980         memset(&r_bloomstate, 0, sizeof(r_bloomstate));
2981         memset(&r_waterstate, 0, sizeof(r_waterstate));
2982         memset(r_glsl_permutationhash, 0, sizeof(r_glsl_permutationhash));
2983         Mem_ExpandableArray_NewArray(&r_glsl_permutationarray, r_main_mempool, sizeof(r_glsl_permutation_t), 256);
2984         memset(&r_svbsp, 0, sizeof (r_svbsp));
2985
2986         r_refdef.fogmasktable_density = 0;
2987 }
2988
2989 extern rtexture_t *loadingscreentexture;
2990 void gl_main_shutdown(void)
2991 {
2992         R_Main_FreeViewCache();
2993
2994         if (r_maxqueries)
2995                 qglDeleteQueriesARB(r_maxqueries, r_queries);
2996
2997         r_numqueries = 0;
2998         r_maxqueries = 0;
2999         memset(r_queries, 0, sizeof(r_queries));
3000
3001         r_qwskincache = NULL;
3002         r_qwskincache_size = 0;
3003
3004         // clear out the r_skinframe state
3005         Mem_ExpandableArray_FreeArray(&r_skinframe.array);
3006         memset(&r_skinframe, 0, sizeof(r_skinframe));
3007
3008         if (r_svbsp.nodes)
3009                 Mem_Free(r_svbsp.nodes);
3010         memset(&r_svbsp, 0, sizeof (r_svbsp));
3011         R_FreeTexturePool(&r_main_texturepool);
3012         loadingscreentexture = NULL;
3013         r_texture_blanknormalmap = NULL;
3014         r_texture_white = NULL;
3015         r_texture_grey128 = NULL;
3016         r_texture_black = NULL;
3017         r_texture_whitecube = NULL;
3018         r_texture_normalizationcube = NULL;
3019         r_texture_fogattenuation = NULL;
3020         r_texture_gammaramps = NULL;
3021         //r_texture_fogintensity = NULL;
3022         memset(&r_bloomstate, 0, sizeof(r_bloomstate));
3023         memset(&r_waterstate, 0, sizeof(r_waterstate));
3024         R_GLSL_Restart_f();
3025 }
3026
3027 extern void CL_ParseEntityLump(char *entitystring);
3028 void gl_main_newmap(void)
3029 {
3030         // FIXME: move this code to client
3031         int l;
3032         char *entities, entname[MAX_QPATH];
3033         if (r_qwskincache)
3034                 Mem_Free(r_qwskincache);
3035         r_qwskincache = NULL;
3036         r_qwskincache_size = 0;
3037         if (cl.worldmodel)
3038         {
3039                 strlcpy(entname, cl.worldmodel->name, sizeof(entname));
3040                 l = (int)strlen(entname) - 4;
3041                 if (l >= 0 && !strcmp(entname + l, ".bsp"))
3042                 {
3043                         memcpy(entname + l, ".ent", 5);
3044                         if ((entities = (char *)FS_LoadFile(entname, tempmempool, true, NULL)))
3045                         {
3046                                 CL_ParseEntityLump(entities);
3047                                 Mem_Free(entities);
3048                                 return;
3049                         }
3050                 }
3051                 if (cl.worldmodel->brush.entities)
3052                         CL_ParseEntityLump(cl.worldmodel->brush.entities);
3053         }
3054         R_Main_FreeViewCache();
3055 }
3056
3057 void GL_Main_Init(void)
3058 {
3059         r_main_mempool = Mem_AllocPool("Renderer", 0, NULL);
3060
3061         Cmd_AddCommand("r_glsl_restart", R_GLSL_Restart_f, "unloads GLSL shaders, they will then be reloaded as needed");
3062         Cmd_AddCommand("r_glsl_dumpshader", R_GLSL_DumpShader_f, "dumps the engine internal default.glsl shader into glsl/default.glsl");
3063         // FIXME: the client should set up r_refdef.fog stuff including the fogmasktable
3064         if (gamemode == GAME_NEHAHRA)
3065         {
3066                 Cvar_RegisterVariable (&gl_fogenable);
3067                 Cvar_RegisterVariable (&gl_fogdensity);
3068                 Cvar_RegisterVariable (&gl_fogred);
3069                 Cvar_RegisterVariable (&gl_foggreen);
3070                 Cvar_RegisterVariable (&gl_fogblue);
3071                 Cvar_RegisterVariable (&gl_fogstart);
3072                 Cvar_RegisterVariable (&gl_fogend);
3073                 Cvar_RegisterVariable (&gl_skyclip);
3074         }
3075         Cvar_RegisterVariable(&r_motionblur);
3076         Cvar_RegisterVariable(&r_motionblur_maxblur);
3077         Cvar_RegisterVariable(&r_motionblur_bmin);
3078         Cvar_RegisterVariable(&r_motionblur_vmin);
3079         Cvar_RegisterVariable(&r_motionblur_vmax);
3080         Cvar_RegisterVariable(&r_motionblur_vcoeff);
3081         Cvar_RegisterVariable(&r_motionblur_randomize);
3082         Cvar_RegisterVariable(&r_damageblur);
3083         Cvar_RegisterVariable(&r_equalize_entities_fullbright);
3084         Cvar_RegisterVariable(&r_equalize_entities_minambient);
3085         Cvar_RegisterVariable(&r_equalize_entities_by);
3086         Cvar_RegisterVariable(&r_equalize_entities_to);
3087         Cvar_RegisterVariable(&r_animcache);
3088         Cvar_RegisterVariable(&r_depthfirst);
3089         Cvar_RegisterVariable(&r_useinfinitefarclip);
3090         Cvar_RegisterVariable(&r_farclip_base);
3091         Cvar_RegisterVariable(&r_farclip_world);
3092         Cvar_RegisterVariable(&r_nearclip);
3093         Cvar_RegisterVariable(&r_showbboxes);
3094         Cvar_RegisterVariable(&r_showsurfaces);
3095         Cvar_RegisterVariable(&r_showtris);
3096         Cvar_RegisterVariable(&r_shownormals);
3097         Cvar_RegisterVariable(&r_showlighting);
3098         Cvar_RegisterVariable(&r_showshadowvolumes);
3099         Cvar_RegisterVariable(&r_showcollisionbrushes);
3100         Cvar_RegisterVariable(&r_showcollisionbrushes_polygonfactor);
3101         Cvar_RegisterVariable(&r_showcollisionbrushes_polygonoffset);
3102         Cvar_RegisterVariable(&r_showdisabledepthtest);
3103         Cvar_RegisterVariable(&r_drawportals);
3104         Cvar_RegisterVariable(&r_drawentities);
3105         Cvar_RegisterVariable(&r_cullentities_trace);
3106         Cvar_RegisterVariable(&r_cullentities_trace_samples);
3107         Cvar_RegisterVariable(&r_cullentities_trace_tempentitysamples);
3108         Cvar_RegisterVariable(&r_cullentities_trace_enlarge);
3109         Cvar_RegisterVariable(&r_cullentities_trace_delay);
3110         Cvar_RegisterVariable(&r_drawviewmodel);
3111         Cvar_RegisterVariable(&r_speeds);
3112         Cvar_RegisterVariable(&r_fullbrights);
3113         Cvar_RegisterVariable(&r_wateralpha);
3114         Cvar_RegisterVariable(&r_dynamic);
3115         Cvar_RegisterVariable(&r_fullbright);
3116         Cvar_RegisterVariable(&r_shadows);
3117         Cvar_RegisterVariable(&r_shadows_darken);
3118         Cvar_RegisterVariable(&r_shadows_drawafterrtlighting);
3119         Cvar_RegisterVariable(&r_shadows_castfrombmodels);
3120         Cvar_RegisterVariable(&r_shadows_throwdistance);
3121         Cvar_RegisterVariable(&r_shadows_throwdirection);
3122         Cvar_RegisterVariable(&r_q1bsp_skymasking);
3123         Cvar_RegisterVariable(&r_polygonoffset_submodel_factor);
3124         Cvar_RegisterVariable(&r_polygonoffset_submodel_offset);
3125         Cvar_RegisterVariable(&r_polygonoffset_decals_factor);
3126         Cvar_RegisterVariable(&r_polygonoffset_decals_offset);
3127         Cvar_RegisterVariable(&r_fog_exp2);
3128         Cvar_RegisterVariable(&r_drawfog);
3129         Cvar_RegisterVariable(&r_textureunits);
3130         Cvar_RegisterVariable(&r_glsl);
3131         Cvar_RegisterVariable(&r_glsl_deluxemapping);
3132         Cvar_RegisterVariable(&r_glsl_offsetmapping);
3133         Cvar_RegisterVariable(&r_glsl_offsetmapping_reliefmapping);
3134         Cvar_RegisterVariable(&r_glsl_offsetmapping_scale);
3135         Cvar_RegisterVariable(&r_glsl_postprocess);
3136         Cvar_RegisterVariable(&r_glsl_postprocess_uservec1);
3137         Cvar_RegisterVariable(&r_glsl_postprocess_uservec2);
3138         Cvar_RegisterVariable(&r_glsl_postprocess_uservec3);
3139         Cvar_RegisterVariable(&r_glsl_postprocess_uservec4);
3140         Cvar_RegisterVariable(&r_glsl_usegeneric);
3141         Cvar_RegisterVariable(&r_water);
3142         Cvar_RegisterVariable(&r_water_resolutionmultiplier);
3143         Cvar_RegisterVariable(&r_water_clippingplanebias);
3144         Cvar_RegisterVariable(&r_water_refractdistort);
3145         Cvar_RegisterVariable(&r_water_reflectdistort);
3146         Cvar_RegisterVariable(&r_lerpsprites);
3147         Cvar_RegisterVariable(&r_lerpmodels);
3148         Cvar_RegisterVariable(&r_lerplightstyles);
3149         Cvar_RegisterVariable(&r_waterscroll);
3150         Cvar_RegisterVariable(&r_bloom);
3151         Cvar_RegisterVariable(&r_bloom_colorscale);
3152         Cvar_RegisterVariable(&r_bloom_brighten);
3153         Cvar_RegisterVariable(&r_bloom_blur);
3154         Cvar_RegisterVariable(&r_bloom_resolution);
3155         Cvar_RegisterVariable(&r_bloom_colorexponent);
3156         Cvar_RegisterVariable(&r_bloom_colorsubtract);
3157         Cvar_RegisterVariable(&r_hdr);
3158         Cvar_RegisterVariable(&r_hdr_scenebrightness);
3159         Cvar_RegisterVariable(&r_hdr_glowintensity);
3160         Cvar_RegisterVariable(&r_hdr_range);
3161         Cvar_RegisterVariable(&r_smoothnormals_areaweighting);
3162         Cvar_RegisterVariable(&developer_texturelogging);
3163         Cvar_RegisterVariable(&gl_lightmaps);
3164         Cvar_RegisterVariable(&r_test);
3165         Cvar_RegisterVariable(&r_batchmode);
3166         Cvar_RegisterVariable(&r_glsl_saturation);
3167         if (gamemode == GAME_NEHAHRA || gamemode == GAME_TENEBRAE)
3168                 Cvar_SetValue("r_fullbrights", 0);
3169         R_RegisterModule("GL_Main", gl_main_start, gl_main_shutdown, gl_main_newmap);
3170
3171         Cvar_RegisterVariable(&r_track_sprites);
3172         Cvar_RegisterVariable(&r_track_sprites_flags);
3173         Cvar_RegisterVariable(&r_track_sprites_scalew);
3174         Cvar_RegisterVariable(&r_track_sprites_scaleh);
3175 }
3176
3177 extern void R_Textures_Init(void);
3178 extern void GL_Draw_Init(void);
3179 extern void GL_Main_Init(void);
3180 extern void R_Shadow_Init(void);
3181 extern void R_Sky_Init(void);
3182 extern void GL_Surf_Init(void);
3183 extern void R_Particles_Init(void);
3184 extern void R_Explosion_Init(void);
3185 extern void gl_backend_init(void);
3186 extern void Sbar_Init(void);
3187 extern void R_LightningBeams_Init(void);
3188 extern void Mod_RenderInit(void);
3189
3190 void Render_Init(void)
3191 {
3192         gl_backend_init();
3193         R_Textures_Init();
3194         GL_Main_Init();
3195         GL_Draw_Init();
3196         R_Shadow_Init();
3197         R_Sky_Init();
3198         GL_Surf_Init();
3199         Sbar_Init();
3200         R_Particles_Init();
3201         R_Explosion_Init();
3202         R_LightningBeams_Init();
3203         Mod_RenderInit();
3204 }
3205
3206 /*
3207 ===============
3208 GL_Init
3209 ===============
3210 */
3211 extern char *ENGINE_EXTENSIONS;
3212 void GL_Init (void)
3213 {
3214         gl_renderer = (const char *)qglGetString(GL_RENDERER);
3215         gl_vendor = (const char *)qglGetString(GL_VENDOR);
3216         gl_version = (const char *)qglGetString(GL_VERSION);
3217         gl_extensions = (const char *)qglGetString(GL_EXTENSIONS);
3218
3219         if (!gl_extensions)
3220                 gl_extensions = "";
3221         if (!gl_platformextensions)
3222                 gl_platformextensions = "";
3223
3224         Con_Printf("GL_VENDOR: %s\n", gl_vendor);
3225         Con_Printf("GL_RENDERER: %s\n", gl_renderer);
3226         Con_Printf("GL_VERSION: %s\n", gl_version);
3227         Con_DPrintf("GL_EXTENSIONS: %s\n", gl_extensions);
3228         Con_DPrintf("%s_EXTENSIONS: %s\n", gl_platform, gl_platformextensions);
3229
3230         VID_CheckExtensions();
3231
3232         // LordHavoc: report supported extensions
3233         Con_DPrintf("\nQuakeC extensions for server and client: %s\nQuakeC extensions for menu: %s\n", vm_sv_extensions, vm_m_extensions );
3234
3235         // clear to black (loading plaque will be seen over this)
3236         CHECKGLERROR
3237         qglClearColor(0,0,0,1);CHECKGLERROR
3238         qglClear(GL_COLOR_BUFFER_BIT);CHECKGLERROR
3239 }
3240
3241 int R_CullBox(const vec3_t mins, const vec3_t maxs)
3242 {
3243         int i;
3244         mplane_t *p;
3245         for (i = 0;i < r_refdef.view.numfrustumplanes;i++)
3246         {
3247                 // skip nearclip plane, it often culls portals when you are very close, and is almost never useful
3248                 if (i == 4)
3249                         continue;
3250                 p = r_refdef.view.frustum + i;
3251                 switch(p->signbits)
3252                 {
3253                 default:
3254                 case 0:
3255                         if (p->normal[0]*maxs[0] + p->normal[1]*maxs[1] + p->normal[2]*maxs[2] < p->dist)
3256                                 return true;
3257                         break;
3258                 case 1:
3259                         if (p->normal[0]*mins[0] + p->normal[1]*maxs[1] + p->normal[2]*maxs[2] < p->dist)
3260                                 return true;
3261                         break;
3262                 case 2:
3263                         if (p->normal[0]*maxs[0] + p->normal[1]*mins[1] + p->normal[2]*maxs[2] < p->dist)
3264                                 return true;
3265                         break;
3266                 case 3:
3267                         if (p->normal[0]*mins[0] + p->normal[1]*mins[1] + p->normal[2]*maxs[2] < p->dist)
3268                                 return true;
3269                         break;
3270                 case 4:
3271                         if (p->normal[0]*maxs[0] + p->normal[1]*maxs[1] + p->normal[2]*mins[2] < p->dist)
3272                                 return true;
3273                         break;
3274                 case 5:
3275                         if (p->normal[0]*mins[0] + p->normal[1]*maxs[1] + p->normal[2]*mins[2] < p->dist)
3276                                 return true;
3277                         break;
3278                 case 6:
3279                         if (p->normal[0]*maxs[0] + p->normal[1]*mins[1] + p->normal[2]*mins[2] < p->dist)
3280                                 return true;
3281                         break;
3282                 case 7:
3283                         if (p->normal[0]*mins[0] + p->normal[1]*mins[1] + p->normal[2]*mins[2] < p->dist)
3284                                 return true;
3285                         break;
3286                 }
3287         }
3288         return false;
3289 }
3290
3291 int R_CullBoxCustomPlanes(const vec3_t mins, const vec3_t maxs, int numplanes, const mplane_t *planes)
3292 {
3293         int i;
3294         const mplane_t *p;
3295         for (i = 0;i < numplanes;i++)
3296         {
3297                 p = planes + i;
3298                 switch(p->signbits)
3299                 {
3300                 default:
3301                 case 0:
3302                         if (p->normal[0]*maxs[0] + p->normal[1]*maxs[1] + p->normal[2]*maxs[2] < p->dist)
3303                                 return true;
3304                         break;
3305                 case 1:
3306                         if (p->normal[0]*mins[0] + p->normal[1]*maxs[1] + p->normal[2]*maxs[2] < p->dist)
3307                                 return true;
3308                         break;
3309                 case 2:
3310                         if (p->normal[0]*maxs[0] + p->normal[1]*mins[1] + p->normal[2]*maxs[2] < p->dist)
3311                                 return true;
3312                         break;
3313                 case 3:
3314                         if (p->normal[0]*mins[0] + p->normal[1]*mins[1] + p->normal[2]*maxs[2] < p->dist)
3315                                 return true;
3316                         break;
3317                 case 4:
3318                         if (p->normal[0]*maxs[0] + p->normal[1]*maxs[1] + p->normal[2]*mins[2] < p->dist)
3319                                 return true;
3320                         break;
3321                 case 5:
3322                         if (p->normal[0]*mins[0] + p->normal[1]*maxs[1] + p->normal[2]*mins[2] < p->dist)
3323                                 return true;
3324                         break;
3325                 case 6:
3326                         if (p->normal[0]*maxs[0] + p->normal[1]*mins[1] + p->normal[2]*mins[2] < p->dist)
3327                                 return true;
3328                         break;
3329                 case 7:
3330                         if (p->normal[0]*mins[0] + p->normal[1]*mins[1] + p->normal[2]*mins[2] < p->dist)
3331                                 return true;
3332                         break;
3333                 }
3334         }
3335         return false;
3336 }
3337
3338 //==================================================================================
3339
3340 // LordHavoc: animcache written by Echon, refactored and reformatted by me
3341
3342 /**
3343  * Animation cache helps save re-animating a player mesh if it's re-rendered again in a given frame
3344  * (reflections, lighting, etc). All animation cache becomes invalid on the next frame and is flushed
3345  * (well, over-wrote). The memory for each cache is kept around to save on allocation thrashing.
3346  */
3347
3348 typedef struct r_animcache_entity_s
3349 {
3350         float *vertex3f;
3351         float *normal3f;
3352         float *svector3f;
3353         float *tvector3f;
3354         int maxvertices;
3355         qboolean wantnormals;
3356         qboolean wanttangents;
3357 }
3358 r_animcache_entity_t;
3359
3360 typedef struct r_animcache_s
3361 {
3362         r_animcache_entity_t entity[MAX_EDICTS];
3363         int maxindex;
3364         int currentindex;
3365 }
3366 r_animcache_t;
3367
3368 static r_animcache_t r_animcachestate;
3369
3370 void R_AnimCache_Free(void)
3371 {
3372         int idx;
3373         for (idx=0 ; idx<r_animcachestate.maxindex ; idx++)
3374         {
3375                 r_animcachestate.entity[idx].maxvertices = 0;
3376                 Mem_Free(r_animcachestate.entity[idx].vertex3f);
3377                 r_animcachestate.entity[idx].vertex3f = NULL;
3378                 r_animcachestate.entity[idx].normal3f = NULL;
3379                 r_animcachestate.entity[idx].svector3f = NULL;
3380                 r_animcachestate.entity[idx].tvector3f = NULL;
3381         }
3382         r_animcachestate.currentindex = 0;
3383         r_animcachestate.maxindex = 0;
3384 }
3385
3386 void R_AnimCache_ResizeEntityCache(const int cacheIdx, const int numvertices)
3387 {
3388         int arraySize;
3389         float *base;
3390         r_animcache_entity_t *cache = &r_animcachestate.entity[cacheIdx];
3391
3392         if (cache->maxvertices >= numvertices)
3393                 return;
3394
3395         // Release existing memory
3396         if (cache->vertex3f)
3397                 Mem_Free(cache->vertex3f);
3398
3399         // Pad by 1024 verts
3400         cache->maxvertices = (numvertices + 1023) & ~1023;
3401         arraySize = cache->maxvertices * 3;
3402
3403         // Allocate, even if we don't need this memory in this instance it will get ignored and potentially used later
3404         base = (float *)Mem_Alloc(r_main_mempool, arraySize * sizeof(float) * 4);
3405         r_animcachestate.entity[cacheIdx].vertex3f = base;
3406         r_animcachestate.entity[cacheIdx].normal3f = base + arraySize;
3407         r_animcachestate.entity[cacheIdx].svector3f = base + arraySize*2;
3408         r_animcachestate.entity[cacheIdx].tvector3f = base + arraySize*3;
3409
3410 //      Con_Printf("allocated cache for %i (%f KB)\n", cacheIdx, (arraySize*sizeof(float)*4)/1024.0f);
3411 }
3412
3413 void R_AnimCache_NewFrame(void)
3414 {
3415         int i;
3416
3417         if (r_animcache.integer && r_drawentities.integer)
3418                 r_animcachestate.maxindex = sizeof(r_animcachestate.entity) / sizeof(r_animcachestate.entity[0]);
3419         else if (r_animcachestate.maxindex)
3420                 R_AnimCache_Free();
3421
3422         r_animcachestate.currentindex = 0;
3423
3424         for (i = 0;i < r_refdef.scene.numentities;i++)
3425                 r_refdef.scene.entities[i]->animcacheindex = -1;
3426 }
3427
3428 qboolean R_AnimCache_GetEntity(entity_render_t *ent, qboolean wantnormals, qboolean wanttangents)
3429 {
3430         dp_model_t *model = ent->model;
3431         r_animcache_entity_t *c;
3432         // see if it's already cached this frame
3433         if (ent->animcacheindex >= 0)
3434         {
3435                 // add normals/tangents if needed
3436                 c = r_animcachestate.entity + ent->animcacheindex;
3437                 if (c->wantnormals)
3438                         wantnormals = false;
3439                 if (c->wanttangents)
3440                         wanttangents = false;
3441                 if (wantnormals || wanttangents)
3442                         model->AnimateVertices(model, ent->frameblend, ent->skeleton, NULL, wantnormals ? c->normal3f : NULL, wanttangents ? c->svector3f : NULL, wanttangents ? c->tvector3f : NULL);
3443         }
3444         else
3445         {
3446                 // see if this ent is worth caching
3447                 if (r_animcachestate.maxindex <= r_animcachestate.currentindex)
3448                         return false;
3449                 if (!model || !model->Draw || !model->surfmesh.isanimated || !model->AnimateVertices || (ent->frameblend[0].lerp == 1 && ent->frameblend[0].subframe == 0))
3450                         return false;
3451                 // assign it a cache entry and make sure the arrays are big enough
3452                 R_AnimCache_ResizeEntityCache(r_animcachestate.currentindex, model->surfmesh.num_vertices);
3453                 ent->animcacheindex = r_animcachestate.currentindex++;
3454                 c = r_animcachestate.entity + ent->animcacheindex;
3455                 c->wantnormals = wantnormals;
3456                 c->wanttangents = wanttangents;
3457                 model->AnimateVertices(model, ent->frameblend, ent->skeleton, c->vertex3f, wantnormals ? c->normal3f : NULL, wanttangents ? c->svector3f : NULL, wanttangents ? c->tvector3f : NULL);
3458         }
3459         return true;
3460 }
3461
3462 void R_AnimCache_CacheVisibleEntities(void)
3463 {
3464         int i;
3465         qboolean wantnormals;
3466         qboolean wanttangents;
3467
3468         if (!r_animcachestate.maxindex)
3469                 return;
3470
3471         wantnormals = !r_showsurfaces.integer;
3472         wanttangents = !r_showsurfaces.integer && (r_glsl.integer || r_refdef.scene.rtworld || r_refdef.scene.rtdlight);
3473
3474         // TODO: thread this?
3475
3476         for (i = 0;i < r_refdef.scene.numentities;i++)
3477         {
3478                 if (!r_refdef.viewcache.entityvisible[i])
3479                         continue;
3480                 R_AnimCache_GetEntity(r_refdef.scene.entities[i], wantnormals, wanttangents);
3481         }
3482 }
3483
3484 //==================================================================================
3485
3486 static void R_View_UpdateEntityLighting (void)
3487 {
3488         int i;
3489         entity_render_t *ent;
3490         vec3_t tempdiffusenormal, avg;
3491         vec_t f, fa, fd, fdd;
3492
3493         for (i = 0;i < r_refdef.scene.numentities;i++)
3494         {
3495                 ent = r_refdef.scene.entities[i];
3496
3497                 // skip unseen models
3498                 if (!r_refdef.viewcache.entityvisible[i] && r_shadows.integer != 1)
3499                         continue;
3500
3501                 // skip bsp models
3502                 if (ent->model && ent->model->brush.num_leafs)
3503                 {
3504                         // TODO: use modellight for r_ambient settings on world?
3505                         VectorSet(ent->modellight_ambient, 0, 0, 0);
3506                         VectorSet(ent->modellight_diffuse, 0, 0, 0);
3507                         VectorSet(ent->modellight_lightdir, 0, 0, 1);
3508                         continue;
3509                 }
3510
3511                 // fetch the lighting from the worldmodel data
3512                 VectorSet(ent->modellight_ambient, r_refdef.scene.ambient * (2.0f / 128.0f), r_refdef.scene.ambient * (2.0f / 128.0f), r_refdef.scene.ambient * (2.0f / 128.0f));
3513                 VectorClear(ent->modellight_diffuse);
3514                 VectorClear(tempdiffusenormal);
3515                 if ((ent->flags & RENDER_LIGHT) && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.LightPoint)
3516                 {
3517                         vec3_t org;
3518                         Matrix4x4_OriginFromMatrix(&ent->matrix, org);
3519                         r_refdef.scene.worldmodel->brush.LightPoint(r_refdef.scene.worldmodel, org, ent->modellight_ambient, ent->modellight_diffuse, tempdiffusenormal);
3520                         if(ent->flags & RENDER_EQUALIZE)
3521                         {
3522                                 // first fix up ambient lighting...
3523                                 if(r_equalize_entities_minambient.value > 0)
3524                                 {
3525                                         fd = 0.299f * ent->modellight_diffuse[0] + 0.587f * ent->modellight_diffuse[1] + 0.114f * ent->modellight_diffuse[2];
3526                                         if(fd > 0)
3527                                         {
3528                                                 fa = (0.299f * ent->modellight_ambient[0] + 0.587f * ent->modellight_ambient[1] + 0.114f * ent->modellight_ambient[2]);
3529                                                 if(fa < r_equalize_entities_minambient.value * fd)
3530                                                 {
3531                                                         // solve:
3532                                                         //   fa'/fd' = minambient
3533                                                         //   fa'+0.25*fd' = fa+0.25*fd
3534                                                         //   ...
3535                                                         //   fa' = fd' * minambient
3536                                                         //   fd'*(0.25+minambient) = fa+0.25*fd
3537                                                         //   ...
3538                                                         //   fd' = (fa+0.25*fd) * 1 / (0.25+minambient)
3539                                                         //   fa' = (fa+0.25*fd) * minambient / (0.25+minambient)
3540                                                         //   ...
3541                                                         fdd = (fa + 0.25f * fd) / (0.25f + r_equalize_entities_minambient.value);
3542                                                         f = fdd / fd; // f>0 because all this is additive; f<1 because fdd<fd because this follows from fa < r_equalize_entities_minambient.value * fd
3543                                                         VectorMA(ent->modellight_ambient, (1-f)*0.25f, ent->modellight_diffuse, ent->modellight_ambient);
3544                                                         VectorScale(ent->modellight_diffuse, f, ent->modellight_diffuse);
3545                                                 }
3546                                         }
3547                                 }
3548
3549                                 if(r_equalize_entities_to.value > 0 && r_equalize_entities_by.value != 0)
3550                                 {
3551                                         VectorMA(ent->modellight_ambient, 0.25f, ent->modellight_diffuse, avg);
3552                                         f = 0.299f * avg[0] + 0.587f * avg[1] + 0.114f * avg[2];
3553                                         if(f > 0)
3554                                         {
3555                                                 f = pow(f / r_equalize_entities_to.value, -r_equalize_entities_by.value);
3556                                                 VectorScale(ent->modellight_ambient, f, ent->modellight_ambient);
3557                                                 VectorScale(ent->modellight_diffuse, f, ent->modellight_diffuse);
3558                                         }
3559                                 }
3560                         }
3561                 }
3562                 else // highly rare
3563                         VectorSet(ent->modellight_ambient, 1, 1, 1);
3564
3565                 // move the light direction into modelspace coordinates for lighting code
3566                 Matrix4x4_Transform3x3(&ent->inversematrix, tempdiffusenormal, ent->modellight_lightdir);
3567                 if(VectorLength2(ent->modellight_lightdir) == 0)
3568                         VectorSet(ent->modellight_lightdir, 0, 0, 1); // have to set SOME valid vector here
3569                 VectorNormalize(ent->modellight_lightdir);
3570         }
3571 }
3572
3573 #define MAX_LINEOFSIGHTTRACES 64
3574
3575 static qboolean R_CanSeeBox(int numsamples, vec_t enlarge, vec3_t eye, vec3_t entboxmins, vec3_t entboxmaxs)
3576 {
3577         int i;
3578         vec3_t boxmins, boxmaxs;
3579         vec3_t start;
3580         vec3_t end;
3581         dp_model_t *model = r_refdef.scene.worldmodel;
3582
3583         if (!model || !model->brush.TraceLineOfSight)
3584                 return true;
3585
3586         // expand the box a little
3587         boxmins[0] = (enlarge+1) * entboxmins[0] - enlarge * entboxmaxs[0];
3588         boxmaxs[0] = (enlarge+1) * entboxmaxs[0] - enlarge * entboxmins[0];
3589         boxmins[1] = (enlarge+1) * entboxmins[1] - enlarge * entboxmaxs[1];
3590         boxmaxs[1] = (enlarge+1) * entboxmaxs[1] - enlarge * entboxmins[1];
3591         boxmins[2] = (enlarge+1) * entboxmins[2] - enlarge * entboxmaxs[2];
3592         boxmaxs[2] = (enlarge+1) * entboxmaxs[2] - enlarge * entboxmins[2];
3593
3594         // try center
3595         VectorCopy(eye, start);
3596         VectorMAM(0.5f, boxmins, 0.5f, boxmaxs, end);
3597         if (model->brush.TraceLineOfSight(model, start, end))
3598                 return true;
3599
3600         // try various random positions
3601         for (i = 0;i < numsamples;i++)
3602         {
3603                 VectorSet(end, lhrandom(boxmins[0], boxmaxs[0]), lhrandom(boxmins[1], boxmaxs[1]), lhrandom(boxmins[2], boxmaxs[2]));
3604                 if (model->brush.TraceLineOfSight(model, start, end))
3605                         return true;
3606         }
3607
3608         return false;
3609 }
3610
3611
3612 static void R_View_UpdateEntityVisible (void)
3613 {
3614         int i;
3615         int renderimask;
3616         int samples;
3617         entity_render_t *ent;
3618
3619         if (!r_drawentities.integer)
3620                 return;
3621
3622         renderimask = r_refdef.envmap ? (RENDER_EXTERIORMODEL | RENDER_VIEWMODEL) : ((chase_active.integer || r_waterstate.renderingscene) ? RENDER_VIEWMODEL : RENDER_EXTERIORMODEL);
3623         if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.BoxTouchingVisibleLeafs)
3624         {
3625                 // worldmodel can check visibility
3626                 memset(r_refdef.viewcache.entityvisible, 0, r_refdef.scene.numentities);
3627                 for (i = 0;i < r_refdef.scene.numentities;i++)
3628                 {
3629                         ent = r_refdef.scene.entities[i];
3630                         if (!(ent->flags & renderimask))
3631                         if (!R_CullBox(ent->mins, ent->maxs) || (ent->model->type == mod_sprite && (ent->model->sprite.sprnum_type == SPR_LABEL || ent->model->sprite.sprnum_type == SPR_LABEL_SCALE)))
3632                         if ((ent->flags & (RENDER_NODEPTHTEST | RENDER_VIEWMODEL)) || r_refdef.scene.worldmodel->brush.BoxTouchingVisibleLeafs(r_refdef.scene.worldmodel, r_refdef.viewcache.world_leafvisible, ent->mins, ent->maxs))
3633                                 r_refdef.viewcache.entityvisible[i] = true;
3634                 }
3635                 if(r_cullentities_trace.integer && r_refdef.scene.worldmodel->brush.TraceLineOfSight)
3636                 {
3637                         for (i = 0;i < r_refdef.scene.numentities;i++)
3638                         {
3639                                 ent = r_refdef.scene.entities[i];
3640                                 if(r_refdef.viewcache.entityvisible[i] && !(ent->flags & (RENDER_VIEWMODEL | RENDER_NOCULL | RENDER_NODEPTHTEST)) && !(ent->model && (ent->model->name[0] == '*')))
3641                                 {
3642                                         samples = ent->entitynumber ? r_cullentities_trace_samples.integer : r_cullentities_trace_tempentitysamples.integer;
3643                                         if (samples < 0)
3644                                                 continue; // temp entities do pvs only
3645                                         if(R_CanSeeBox(samples, r_cullentities_trace_enlarge.value, r_refdef.view.origin, ent->mins, ent->maxs))
3646                                                 ent->last_trace_visibility = realtime;
3647                                         if(ent->last_trace_visibility < realtime - r_cullentities_trace_delay.value)
3648                                                 r_refdef.viewcache.entityvisible[i] = 0;
3649                                 }
3650                         }
3651                 }
3652         }
3653         else
3654         {
3655                 // no worldmodel or it can't check visibility
3656                 for (i = 0;i < r_refdef.scene.numentities;i++)
3657                 {
3658                         ent = r_refdef.scene.entities[i];
3659                         r_refdef.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));
3660                 }
3661         }
3662 }
3663
3664 /// only used if skyrendermasked, and normally returns false
3665 int R_DrawBrushModelsSky (void)
3666 {
3667         int i, sky;
3668         entity_render_t *ent;
3669
3670         if (!r_drawentities.integer)
3671                 return false;
3672
3673         sky = false;
3674         for (i = 0;i < r_refdef.scene.numentities;i++)
3675         {