changed a ton of developer cvar checks into developer_extra/insane
[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 #include "ft2.h"
28
29 mempool_t *r_main_mempool;
30 rtexturepool_t *r_main_texturepool;
31
32 static int r_frame = 0; ///< used only by R_GetCurrentTexture
33
34 qboolean r_loadnormalmap;
35 qboolean r_loadgloss;
36 qboolean r_loadfog;
37
38 //
39 // screen size info
40 //
41 r_refdef_t r_refdef;
42
43 cvar_t r_motionblur = {CVAR_SAVE, "r_motionblur", "0", "motionblur value scale - 0.5 recommended"};
44 cvar_t r_damageblur = {CVAR_SAVE, "r_damageblur", "0", "motionblur based on damage"};
45 cvar_t r_motionblur_vmin = {CVAR_SAVE, "r_motionblur_vmin", "300", "minimum influence from velocity"};
46 cvar_t r_motionblur_vmax = {CVAR_SAVE, "r_motionblur_vmax", "600", "maximum influence from velocity"};
47 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)"};
48 cvar_t r_motionblur_vcoeff = {CVAR_SAVE, "r_motionblur_vcoeff", "0.05", "sliding average reaction time for velocity"};
49 cvar_t r_motionblur_maxblur = {CVAR_SAVE, "r_motionblur_maxblur", "0.88", "cap for motionblur alpha value"};
50 cvar_t r_motionblur_randomize = {CVAR_SAVE, "r_motionblur_randomize", "0.1", "randomizing coefficient to workaround ghosting"};
51
52 // TODO do we want a r_equalize_entities cvar that works on all ents, or would that be a cheat?
53 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"};
54 cvar_t r_equalize_entities_minambient = {CVAR_SAVE, "r_equalize_entities_minambient", "0.5", "light equalizing: ensure at least this ambient/diffuse ratio"};
55 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)"};
56 cvar_t r_equalize_entities_to = {CVAR_SAVE, "r_equalize_entities_to", "0.8", "light equalizing: target light level"};
57
58 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"};
59 cvar_t r_useinfinitefarclip = {CVAR_SAVE, "r_useinfinitefarclip", "1", "enables use of a special kind of projection matrix that has an extremely large farclip"};
60 cvar_t r_farclip_base = {0, "r_farclip_base", "65536", "farclip (furthest visible distance) for rendering when r_useinfinitefarclip is 0"};
61 cvar_t r_farclip_world = {0, "r_farclip_world", "2", "adds map size to farclip multiplied by this value"};
62 cvar_t r_nearclip = {0, "r_nearclip", "1", "distance from camera of nearclip plane" };
63 cvar_t r_showbboxes = {0, "r_showbboxes", "0", "shows bounding boxes of server entities, value controls opacity scaling (1 = 10%,  10 = 100%)"};
64 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)"};
65 cvar_t r_showtris = {0, "r_showtris", "0", "shows triangle outlines, value controls brightness (can be above 1)"};
66 cvar_t r_shownormals = {0, "r_shownormals", "0", "shows per-vertex surface normals and tangent vectors for bumpmapped lighting"};
67 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"};
68 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"};
69 cvar_t r_showcollisionbrushes = {0, "r_showcollisionbrushes", "0", "draws collision brushes in quake3 maps (mode 1), mode 2 disables rendering of world (trippy!)"};
70 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"};
71 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"};
72 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"};
73 cvar_t r_drawportals = {0, "r_drawportals", "0", "shows portals (separating polygons) in world interior in quake1 maps"};
74 cvar_t r_drawentities = {0, "r_drawentities","1", "draw entities (doors, players, projectiles, etc)"};
75 cvar_t r_drawviewmodel = {0, "r_drawviewmodel","1", "draw your weapon model"};
76 cvar_t r_cullentities_trace = {0, "r_cullentities_trace", "1", "probabistically cull invisible entities"};
77 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)"};
78 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)"};
79 cvar_t r_cullentities_trace_enlarge = {0, "r_cullentities_trace_enlarge", "0", "box enlargement for entity culling"};
80 cvar_t r_cullentities_trace_delay = {0, "r_cullentities_trace_delay", "1", "number of seconds until the entity gets actually culled"};
81 cvar_t r_speeds = {0, "r_speeds","0", "displays rendering statistics and per-subsystem timings"};
82 cvar_t r_fullbright = {0, "r_fullbright","0", "makes map very bright and renders faster"};
83 cvar_t r_wateralpha = {CVAR_SAVE, "r_wateralpha","1", "opacity of water polygons"};
84 cvar_t r_dynamic = {CVAR_SAVE, "r_dynamic","1", "enables dynamic lights (rocket glow and such)"};
85 cvar_t r_fullbrights = {CVAR_SAVE, "r_fullbrights", "1", "enables glowing pixels in quake textures (changes need r_restart to take effect)"};
86 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."};
87 cvar_t r_shadows_darken = {CVAR_SAVE, "r_shadows_darken", "0.5", "how much shadowed areas will be darkened"};
88 cvar_t r_shadows_throwdistance = {CVAR_SAVE, "r_shadows_throwdistance", "500", "how far to cast shadows from models"};
89 cvar_t r_shadows_throwdirection = {CVAR_SAVE, "r_shadows_throwdirection", "0 0 -1", "override throwing direction for r_shadows 2"};
90 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."};
91 cvar_t r_shadows_castfrombmodels = {CVAR_SAVE, "r_shadows_castfrombmodels", "0", "do cast shadows from bmodels"};
92 cvar_t r_q1bsp_skymasking = {0, "r_q1bsp_skymasking", "1", "allows sky polygons in quake1 maps to obscure other geometry"};
93 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"};
94 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"};
95 cvar_t r_polygonoffset_decals_factor = {0, "r_polygonoffset_decals_factor", "0", "biases depth values of decals to prevent z-fighting artifacts"};
96 cvar_t r_polygonoffset_decals_offset = {0, "r_polygonoffset_decals_offset", "-14", "biases depth values of decals to prevent z-fighting artifacts"};
97 cvar_t r_fog_exp2 = {0, "r_fog_exp2", "0", "uses GL_EXP2 fog (as in Nehahra) rather than realistic GL_EXP fog"};
98 cvar_t r_drawfog = {CVAR_SAVE, "r_drawfog", "1", "allows one to disable fog rendering"};
99 cvar_t r_transparentdepthmasking = {CVAR_SAVE, "r_transparentdepthmasking", "0", "enables depth writes on transparent meshes whose materially is normally opaque, this prevents seeing the inside of a transparent mesh"};
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 texture units to use in GL 1.1 and GL 1.3 rendering paths"};
111 static cvar_t gl_combine = {CVAR_READONLY, "gl_combine", "1", "indicates whether the OpenGL 1.3 rendering path is active"};
112 static cvar_t r_glsl = {CVAR_READONLY, "r_glsl", "1", "indicates whether the OpenGL 2.0 rendering path is active"};
113
114 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)"};
115 cvar_t r_glsl_offsetmapping = {CVAR_SAVE, "r_glsl_offsetmapping", "0", "offset mapping effect (also known as parallax mapping or virtual displacement mapping)"};
116 cvar_t r_glsl_offsetmapping_reliefmapping = {CVAR_SAVE, "r_glsl_offsetmapping_reliefmapping", "0", "relief mapping effect (higher quality)"};
117 cvar_t r_glsl_offsetmapping_scale = {CVAR_SAVE, "r_glsl_offsetmapping_scale", "0.04", "how deep the offset mapping effect is"};
118 cvar_t r_glsl_postprocess = {CVAR_SAVE, "r_glsl_postprocess", "0", "use a GLSL postprocessing shader"};
119 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)"};
120 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)"};
121 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)"};
122 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)"};
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 cvar_t r_framedatasize = {CVAR_SAVE, "r_framedatasize", "1", "size of renderer data cache used during one frame (for skeletal animation caching, light processing, etc)"};
163
164 extern cvar_t v_glslgamma;
165
166 extern qboolean v_flipped_state;
167
168 static struct r_bloomstate_s
169 {
170         qboolean enabled;
171         qboolean hdr;
172
173         int bloomwidth, bloomheight;
174
175         int screentexturewidth, screentextureheight;
176         rtexture_t *texture_screen; /// \note also used for motion blur if enabled!
177
178         int bloomtexturewidth, bloomtextureheight;
179         rtexture_t *texture_bloom;
180
181         // arrays for rendering the screen passes
182         float screentexcoord2f[8];
183         float bloomtexcoord2f[8];
184         float offsettexcoord2f[8];
185
186         r_viewport_t viewport;
187 }
188 r_bloomstate;
189
190 r_waterstate_t r_waterstate;
191
192 /// shadow volume bsp struct with automatically growing nodes buffer
193 svbsp_t r_svbsp;
194
195 rtexture_t *r_texture_blanknormalmap;
196 rtexture_t *r_texture_white;
197 rtexture_t *r_texture_grey128;
198 rtexture_t *r_texture_black;
199 rtexture_t *r_texture_notexture;
200 rtexture_t *r_texture_whitecube;
201 rtexture_t *r_texture_normalizationcube;
202 rtexture_t *r_texture_fogattenuation;
203 rtexture_t *r_texture_gammaramps;
204 unsigned int r_texture_gammaramps_serial;
205 //rtexture_t *r_texture_fogintensity;
206
207 unsigned int r_queries[MAX_OCCLUSION_QUERIES];
208 unsigned int r_numqueries;
209 unsigned int r_maxqueries;
210
211 typedef struct r_qwskincache_s
212 {
213         char name[MAX_QPATH];
214         skinframe_t *skinframe;
215 }
216 r_qwskincache_t;
217
218 static r_qwskincache_t *r_qwskincache;
219 static int r_qwskincache_size;
220
221 /// vertex coordinates for a quad that covers the screen exactly
222 const float r_screenvertex3f[12] =
223 {
224         0, 0, 0,
225         1, 0, 0,
226         1, 1, 0,
227         0, 1, 0
228 };
229
230 extern void R_DrawModelShadows(void);
231
232 void R_ModulateColors(float *in, float *out, int verts, float r, float g, float b)
233 {
234         int i;
235         for (i = 0;i < verts;i++)
236         {
237                 out[0] = in[0] * r;
238                 out[1] = in[1] * g;
239                 out[2] = in[2] * b;
240                 out[3] = in[3];
241                 in += 4;
242                 out += 4;
243         }
244 }
245
246 void R_FillColors(float *out, int verts, float r, float g, float b, float a)
247 {
248         int i;
249         for (i = 0;i < verts;i++)
250         {
251                 out[0] = r;
252                 out[1] = g;
253                 out[2] = b;
254                 out[3] = a;
255                 out += 4;
256         }
257 }
258
259 // FIXME: move this to client?
260 void FOG_clear(void)
261 {
262         if (gamemode == GAME_NEHAHRA)
263         {
264                 Cvar_Set("gl_fogenable", "0");
265                 Cvar_Set("gl_fogdensity", "0.2");
266                 Cvar_Set("gl_fogred", "0.3");
267                 Cvar_Set("gl_foggreen", "0.3");
268                 Cvar_Set("gl_fogblue", "0.3");
269         }
270         r_refdef.fog_density = 0;
271         r_refdef.fog_red = 0;
272         r_refdef.fog_green = 0;
273         r_refdef.fog_blue = 0;
274         r_refdef.fog_alpha = 1;
275         r_refdef.fog_start = 0;
276         r_refdef.fog_end = 16384;
277         r_refdef.fog_height = 1<<30;
278         r_refdef.fog_fadedepth = 128;
279 }
280
281 static void R_BuildBlankTextures(void)
282 {
283         unsigned char data[4];
284         data[2] = 128; // normal X
285         data[1] = 128; // normal Y
286         data[0] = 255; // normal Z
287         data[3] = 128; // height
288         r_texture_blanknormalmap = R_LoadTexture2D(r_main_texturepool, "blankbump", 1, 1, data, TEXTYPE_BGRA, TEXF_PERSISTENT, NULL);
289         data[0] = 255;
290         data[1] = 255;
291         data[2] = 255;
292         data[3] = 255;
293         r_texture_white = R_LoadTexture2D(r_main_texturepool, "blankwhite", 1, 1, data, TEXTYPE_BGRA, TEXF_PERSISTENT, NULL);
294         data[0] = 128;
295         data[1] = 128;
296         data[2] = 128;
297         data[3] = 255;
298         r_texture_grey128 = R_LoadTexture2D(r_main_texturepool, "blankgrey128", 1, 1, data, TEXTYPE_BGRA, TEXF_PERSISTENT, NULL);
299         data[0] = 0;
300         data[1] = 0;
301         data[2] = 0;
302         data[3] = 255;
303         r_texture_black = R_LoadTexture2D(r_main_texturepool, "blankblack", 1, 1, data, TEXTYPE_BGRA, TEXF_PERSISTENT, NULL);
304 }
305
306 static void R_BuildNoTexture(void)
307 {
308         int x, y;
309         unsigned char pix[16][16][4];
310         // this makes a light grey/dark grey checkerboard texture
311         for (y = 0;y < 16;y++)
312         {
313                 for (x = 0;x < 16;x++)
314                 {
315                         if ((y < 8) ^ (x < 8))
316                         {
317                                 pix[y][x][0] = 128;
318                                 pix[y][x][1] = 128;
319                                 pix[y][x][2] = 128;
320                                 pix[y][x][3] = 255;
321                         }
322                         else
323                         {
324                                 pix[y][x][0] = 64;
325                                 pix[y][x][1] = 64;
326                                 pix[y][x][2] = 64;
327                                 pix[y][x][3] = 255;
328                         }
329                 }
330         }
331         r_texture_notexture = R_LoadTexture2D(r_main_texturepool, "notexture", 16, 16, &pix[0][0][0], TEXTYPE_BGRA, TEXF_MIPMAP | TEXF_PERSISTENT, NULL);
332 }
333
334 static void R_BuildWhiteCube(void)
335 {
336         unsigned char data[6*1*1*4];
337         memset(data, 255, sizeof(data));
338         r_texture_whitecube = R_LoadTextureCubeMap(r_main_texturepool, "whitecube", 1, data, TEXTYPE_BGRA, TEXF_CLAMP | TEXF_PERSISTENT, NULL);
339 }
340
341 static void R_BuildNormalizationCube(void)
342 {
343         int x, y, side;
344         vec3_t v;
345         vec_t s, t, intensity;
346 #define NORMSIZE 64
347         unsigned char *data;
348         data = Mem_Alloc(tempmempool, 6*NORMSIZE*NORMSIZE*4);
349         for (side = 0;side < 6;side++)
350         {
351                 for (y = 0;y < NORMSIZE;y++)
352                 {
353                         for (x = 0;x < NORMSIZE;x++)
354                         {
355                                 s = (x + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
356                                 t = (y + 0.5f) * (2.0f / NORMSIZE) - 1.0f;
357                                 switch(side)
358                                 {
359                                 default:
360                                 case 0:
361                                         v[0] = 1;
362                                         v[1] = -t;
363                                         v[2] = -s;
364                                         break;
365                                 case 1:
366                                         v[0] = -1;
367                                         v[1] = -t;
368                                         v[2] = s;
369                                         break;
370                                 case 2:
371                                         v[0] = s;
372                                         v[1] = 1;
373                                         v[2] = t;
374                                         break;
375                                 case 3:
376                                         v[0] = s;
377                                         v[1] = -1;
378                                         v[2] = -t;
379                                         break;
380                                 case 4:
381                                         v[0] = s;
382                                         v[1] = -t;
383                                         v[2] = 1;
384                                         break;
385                                 case 5:
386                                         v[0] = -s;
387                                         v[1] = -t;
388                                         v[2] = -1;
389                                         break;
390                                 }
391                                 intensity = 127.0f / sqrt(DotProduct(v, v));
392                                 data[((side*64+y)*64+x)*4+2] = (unsigned char)(128.0f + intensity * v[0]);
393                                 data[((side*64+y)*64+x)*4+1] = (unsigned char)(128.0f + intensity * v[1]);
394                                 data[((side*64+y)*64+x)*4+0] = (unsigned char)(128.0f + intensity * v[2]);
395                                 data[((side*64+y)*64+x)*4+3] = 255;
396                         }
397                 }
398         }
399         r_texture_normalizationcube = R_LoadTextureCubeMap(r_main_texturepool, "normalcube", NORMSIZE, data, TEXTYPE_BGRA, TEXF_CLAMP | TEXF_PERSISTENT, NULL);
400         Mem_Free(data);
401 }
402
403 static void R_BuildFogTexture(void)
404 {
405         int x, b;
406 #define FOGWIDTH 256
407         unsigned char data1[FOGWIDTH][4];
408         //unsigned char data2[FOGWIDTH][4];
409         double d, r, alpha;
410
411         r_refdef.fogmasktable_start = r_refdef.fog_start;
412         r_refdef.fogmasktable_alpha = r_refdef.fog_alpha;
413         r_refdef.fogmasktable_range = r_refdef.fogrange;
414         r_refdef.fogmasktable_density = r_refdef.fog_density;
415
416         r = r_refdef.fogmasktable_range / FOGMASKTABLEWIDTH;
417         for (x = 0;x < FOGMASKTABLEWIDTH;x++)
418         {
419                 d = (x * r - r_refdef.fogmasktable_start);
420                 if(developer_extra.integer)
421                         Con_DPrintf("%f ", d);
422                 d = max(0, d);
423                 if (r_fog_exp2.integer)
424                         alpha = exp(-r_refdef.fogmasktable_density * r_refdef.fogmasktable_density * 0.0001 * d * d);
425                 else
426                         alpha = exp(-r_refdef.fogmasktable_density * 0.004 * d);
427                 if(developer_extra.integer)
428                         Con_DPrintf(" : %f ", alpha);
429                 alpha = 1 - (1 - alpha) * r_refdef.fogmasktable_alpha;
430                 if(developer_extra.integer)
431                         Con_DPrintf(" = %f\n", alpha);
432                 r_refdef.fogmasktable[x] = bound(0, alpha, 1);
433         }
434
435         for (x = 0;x < FOGWIDTH;x++)
436         {
437                 b = (int)(r_refdef.fogmasktable[x * (FOGMASKTABLEWIDTH - 1) / (FOGWIDTH - 1)] * 255);
438                 data1[x][0] = b;
439                 data1[x][1] = b;
440                 data1[x][2] = b;
441                 data1[x][3] = 255;
442                 //data2[x][0] = 255 - b;
443                 //data2[x][1] = 255 - b;
444                 //data2[x][2] = 255 - b;
445                 //data2[x][3] = 255;
446         }
447         if (r_texture_fogattenuation)
448         {
449                 R_UpdateTexture(r_texture_fogattenuation, &data1[0][0], 0, 0, FOGWIDTH, 1);
450                 //R_UpdateTexture(r_texture_fogattenuation, &data2[0][0], 0, 0, FOGWIDTH, 1);
451         }
452         else
453         {
454                 r_texture_fogattenuation = R_LoadTexture2D(r_main_texturepool, "fogattenuation", FOGWIDTH, 1, &data1[0][0], TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_CLAMP | TEXF_PERSISTENT | TEXF_ALLOWUPDATES, NULL);
455                 //r_texture_fogintensity = R_LoadTexture2D(r_main_texturepool, "fogintensity", FOGWIDTH, 1, &data2[0][0], TEXTYPE_BGRA, TEXF_FORCELINEAR | TEXF_CLAMP | TEXF_ALLOWUPDATES, NULL);
456         }
457 }
458
459 //=======================================================================================================================================================
460
461 static const char *builtinshaderstring =
462 "// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader\n"
463 "// written by Forest 'LordHavoc' Hale\n"
464 "// shadowmapping enhancements by Lee 'eihrul' Salzman\n"
465 "\n"
466 "#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE)\n"
467 "# define USEFOG\n"
468 "#endif\n"
469 "#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE)\n"
470 "#define USELIGHTMAP\n"
471 "#endif\n"
472 "#if defined(USESPECULAR) || defined(USEOFFSETMAPPING)\n"
473 "#define USEEYEVECTOR\n"
474 "#endif\n"
475 "\n"
476 "#if defined(USESHADOWMAPRECT) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USEDEFERREDLIGHTMAP)\n"
477 "# extension GL_ARB_texture_rectangle : enable\n"
478 "#endif\n"
479 "\n"
480 "#ifdef USESHADOWMAP2D\n"
481 "# ifdef GL_EXT_gpu_shader4\n"
482 "#   extension GL_EXT_gpu_shader4 : enable\n"
483 "# endif\n"
484 "# ifdef GL_ARB_texture_gather\n"
485 "#   extension GL_ARB_texture_gather : enable\n"
486 "# else\n"
487 "#   ifdef GL_AMD_texture_texture4\n"
488 "#     extension GL_AMD_texture_texture4 : enable\n"
489 "#   endif\n"
490 "# endif\n"
491 "#endif\n"
492 "\n"
493 "#ifdef USESHADOWMAPCUBE\n"
494 "# extension GL_EXT_gpu_shader4 : enable\n"
495 "#endif\n"
496 "\n"
497 "#ifdef USESHADOWSAMPLER\n"
498 "# extension GL_ARB_shadow : enable\n"
499 "#endif\n"
500 "\n"
501 "//#ifdef __GLSL_CG_DATA_TYPES\n"
502 "//# define myhalf half\n"
503 "//# define myhalf2 half2\n"
504 "//# define myhalf3 half3\n"
505 "//# define myhalf4 half4\n"
506 "//#else\n"
507 "# define myhalf float\n"
508 "# define myhalf2 vec2\n"
509 "# define myhalf3 vec3\n"
510 "# define myhalf4 vec4\n"
511 "//#endif\n"
512 "\n"
513 "#ifdef VERTEX_SHADER\n"
514 "uniform mat4 ModelViewProjectionMatrix;\n"
515 "#endif\n"
516 "\n"
517 "#ifdef MODE_DEPTH_OR_SHADOW\n"
518 "#ifdef VERTEX_SHADER\n"
519 "void main(void)\n"
520 "{\n"
521 "       gl_Position = ModelViewProjectionMatrix * gl_Vertex;\n"
522 "}\n"
523 "#endif\n"
524 "#else // !MODE_DEPTH_ORSHADOW\n"
525 "\n"
526 "\n"
527 "\n"
528 "\n"
529 "#ifdef MODE_SHOWDEPTH\n"
530 "#ifdef VERTEX_SHADER\n"
531 "void main(void)\n"
532 "{\n"
533 "       gl_Position = ModelViewProjectionMatrix * gl_Vertex;\n"
534 "       gl_FrontColor = vec4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0);\n"
535 "}\n"
536 "#endif\n"
537 "\n"
538 "#ifdef FRAGMENT_SHADER\n"
539 "void main(void)\n"
540 "{\n"
541 "       gl_FragColor = gl_Color;\n"
542 "}\n"
543 "#endif\n"
544 "#else // !MODE_SHOWDEPTH\n"
545 "\n"
546 "\n"
547 "\n"
548 "\n"
549 "#ifdef MODE_POSTPROCESS\n"
550 "varying vec2 TexCoord1;\n"
551 "varying vec2 TexCoord2;\n"
552 "\n"
553 "#ifdef VERTEX_SHADER\n"
554 "void main(void)\n"
555 "{\n"
556 "       gl_Position = ModelViewProjectionMatrix * gl_Vertex;\n"
557 "       TexCoord1 = gl_MultiTexCoord0.xy;\n"
558 "#ifdef USEBLOOM\n"
559 "       TexCoord2 = gl_MultiTexCoord1.xy;\n"
560 "#endif\n"
561 "}\n"
562 "#endif\n"
563 "\n"
564 "#ifdef FRAGMENT_SHADER\n"
565 "uniform sampler2D Texture_First;\n"
566 "#ifdef USEBLOOM\n"
567 "uniform sampler2D Texture_Second;\n"
568 "#endif\n"
569 "#ifdef USEGAMMARAMPS\n"
570 "uniform sampler2D Texture_GammaRamps;\n"
571 "#endif\n"
572 "#ifdef USESATURATION\n"
573 "uniform float Saturation;\n"
574 "#endif\n"
575 "#ifdef USEVIEWTINT\n"
576 "uniform vec4 ViewTintColor;\n"
577 "#endif\n"
578 "//uncomment these if you want to use them:\n"
579 "uniform vec4 UserVec1;\n"
580 "// uniform vec4 UserVec2;\n"
581 "// uniform vec4 UserVec3;\n"
582 "// uniform vec4 UserVec4;\n"
583 "// uniform float ClientTime;\n"
584 "uniform vec2 PixelSize;\n"
585 "void main(void)\n"
586 "{\n"
587 "       gl_FragColor = texture2D(Texture_First, TexCoord1);\n"
588 "#ifdef USEBLOOM\n"
589 "       gl_FragColor += texture2D(Texture_Second, TexCoord2);\n"
590 "#endif\n"
591 "#ifdef USEVIEWTINT\n"
592 "       gl_FragColor = mix(gl_FragColor, ViewTintColor, ViewTintColor.a);\n"
593 "#endif\n"
594 "\n"
595 "#ifdef USEPOSTPROCESSING\n"
596 "// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want\n"
597 "// 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"
598 "       gl_FragColor += texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.987688, -0.156434)) * UserVec1.y;\n"
599 "       gl_FragColor += texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.156434, -0.891007)) * UserVec1.y;\n"
600 "       gl_FragColor += texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.891007, -0.453990)) * UserVec1.y;\n"
601 "       gl_FragColor += texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2( 0.707107,  0.707107)) * UserVec1.y;\n"
602 "       gl_FragColor += texture2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*vec2(-0.453990,  0.891007)) * UserVec1.y;\n"
603 "       gl_FragColor /= (1 + 5 * UserVec1.y);\n"
604 "#endif\n"
605 "\n"
606 "#ifdef USESATURATION\n"
607 "       //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter\n"
608 "       float y = dot(gl_FragColor.rgb, vec3(0.299, 0.587, 0.114));\n"
609 "       //gl_FragColor = vec3(y) + (gl_FragColor.rgb - vec3(y)) * Saturation;\n"
610 "       gl_FragColor.rgb = mix(vec3(y), gl_FragColor.rgb, Saturation);\n"
611 "#endif\n"
612 "\n"
613 "#ifdef USEGAMMARAMPS\n"
614 "       gl_FragColor.r = texture2D(Texture_GammaRamps, vec2(gl_FragColor.r, 0)).r;\n"
615 "       gl_FragColor.g = texture2D(Texture_GammaRamps, vec2(gl_FragColor.g, 0)).g;\n"
616 "       gl_FragColor.b = texture2D(Texture_GammaRamps, vec2(gl_FragColor.b, 0)).b;\n"
617 "#endif\n"
618 "}\n"
619 "#endif\n"
620 "#else // !MODE_POSTPROCESS\n"
621 "\n"
622 "\n"
623 "\n"
624 "\n"
625 "#ifdef MODE_GENERIC\n"
626 "#ifdef USEDIFFUSE\n"
627 "varying vec2 TexCoord1;\n"
628 "#endif\n"
629 "#ifdef USESPECULAR\n"
630 "varying vec2 TexCoord2;\n"
631 "#endif\n"
632 "#ifdef VERTEX_SHADER\n"
633 "void main(void)\n"
634 "{\n"
635 "       gl_FrontColor = gl_Color;\n"
636 "#ifdef USEDIFFUSE\n"
637 "       TexCoord1 = gl_MultiTexCoord0.xy;\n"
638 "#endif\n"
639 "#ifdef USESPECULAR\n"
640 "       TexCoord2 = gl_MultiTexCoord1.xy;\n"
641 "#endif\n"
642 "       gl_Position = ModelViewProjectionMatrix * gl_Vertex;\n"
643 "}\n"
644 "#endif\n"
645 "\n"
646 "#ifdef FRAGMENT_SHADER\n"
647 "#ifdef USEDIFFUSE\n"
648 "uniform sampler2D Texture_First;\n"
649 "#endif\n"
650 "#ifdef USESPECULAR\n"
651 "uniform sampler2D Texture_Second;\n"
652 "#endif\n"
653 "\n"
654 "void main(void)\n"
655 "{\n"
656 "       gl_FragColor = gl_Color;\n"
657 "#ifdef USEDIFFUSE\n"
658 "       gl_FragColor *= texture2D(Texture_First, TexCoord1);\n"
659 "#endif\n"
660 "\n"
661 "#ifdef USESPECULAR\n"
662 "       vec4 tex2 = texture2D(Texture_Second, TexCoord2);\n"
663 "# ifdef USECOLORMAPPING\n"
664 "       gl_FragColor *= tex2;\n"
665 "# endif\n"
666 "# ifdef USEGLOW\n"
667 "       gl_FragColor += tex2;\n"
668 "# endif\n"
669 "# ifdef USEVERTEXTEXTUREBLEND\n"
670 "       gl_FragColor = mix(gl_FragColor, tex2, tex2.a);\n"
671 "# endif\n"
672 "#endif\n"
673 "}\n"
674 "#endif\n"
675 "#else // !MODE_GENERIC\n"
676 "\n"
677 "\n"
678 "\n"
679 "\n"
680 "#ifdef MODE_BLOOMBLUR\n"
681 "varying TexCoord;\n"
682 "#ifdef VERTEX_SHADER\n"
683 "void main(void)\n"
684 "{\n"
685 "       gl_FrontColor = gl_Color;\n"
686 "       TexCoord = gl_MultiTexCoord0.xy;\n"
687 "       gl_Position = ModelViewProjectionMatrix * gl_Vertex;\n"
688 "}\n"
689 "#endif\n"
690 "\n"
691 "#ifdef FRAGMENT_SHADER\n"
692 "uniform sampler2D Texture_First;\n"
693 "uniform vec4 BloomBlur_Parameters;\n"
694 "\n"
695 "void main(void)\n"
696 "{\n"
697 "       int i;\n"
698 "       vec2 tc = TexCoord;\n"
699 "       vec3 color = texture2D(Texture_First, tc).rgb;\n"
700 "       tc += BloomBlur_Parameters.xy;\n"
701 "       for (i = 1;i < SAMPLES;i++)\n"
702 "       {\n"
703 "               color += texture2D(Texture_First, tc).rgb;\n"
704 "               tc += BloomBlur_Parameters.xy;\n"
705 "       }\n"
706 "       gl_FragColor = vec4(color * BloomBlur_Parameters.z + vec3(BloomBlur_Parameters.w), 1);\n"
707 "}\n"
708 "#endif\n"
709 "#else // !MODE_BLOOMBLUR\n"
710 "#ifdef MODE_REFRACTION\n"
711 "varying vec2 TexCoord;\n"
712 "varying vec4 ModelViewProjectionPosition;\n"
713 "uniform mat4 TexMatrix;\n"
714 "#ifdef VERTEX_SHADER\n"
715 "\n"
716 "void main(void)\n"
717 "{\n"
718 "       TexCoord = vec2(TexMatrix * gl_MultiTexCoord0);\n"
719 "       gl_Position = ModelViewProjectionMatrix * gl_Vertex;\n"
720 "       ModelViewProjectionPosition = gl_Position;\n"
721 "}\n"
722 "#endif\n"
723 "\n"
724 "#ifdef FRAGMENT_SHADER\n"
725 "uniform sampler2D Texture_Normal;\n"
726 "uniform sampler2D Texture_Refraction;\n"
727 "uniform sampler2D Texture_Reflection;\n"
728 "\n"
729 "uniform vec4 DistortScaleRefractReflect;\n"
730 "uniform vec4 ScreenScaleRefractReflect;\n"
731 "uniform vec4 ScreenCenterRefractReflect;\n"
732 "uniform vec4 RefractColor;\n"
733 "uniform vec4 ReflectColor;\n"
734 "uniform float ReflectFactor;\n"
735 "uniform float ReflectOffset;\n"
736 "\n"
737 "void main(void)\n"
738 "{\n"
739 "       vec2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w);\n"
740 "       //vec2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(vec3(texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy;\n"
741 "       vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy;\n"
742 "       vec2 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xy * DistortScaleRefractReflect.xy;\n"
743 "       // FIXME temporary hack to detect the case that the reflection\n"
744 "       // gets blackened at edges due to leaving the area that contains actual\n"
745 "       // content.\n"
746 "       // Remove this 'ack once we have a better way to stop this thing from\n"
747 "       // 'appening.\n"
748 "       float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05);\n"
749 "       f      *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05);\n"
750 "       f      *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05);\n"
751 "       f      *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05);\n"
752 "       ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f);\n"
753 "       gl_FragColor = texture2D(Texture_Refraction, ScreenTexCoord) * RefractColor;\n"
754 "}\n"
755 "#endif\n"
756 "#else // !MODE_REFRACTION\n"
757 "\n"
758 "\n"
759 "\n"
760 "\n"
761 "#ifdef MODE_WATER\n"
762 "varying vec2 TexCoord;\n"
763 "varying vec3 EyeVector;\n"
764 "varying vec4 ModelViewProjectionPosition;\n"
765 "#ifdef VERTEX_SHADER\n"
766 "uniform vec3 EyePosition;\n"
767 "uniform mat4 TexMatrix;\n"
768 "\n"
769 "void main(void)\n"
770 "{\n"
771 "       TexCoord = vec2(TexMatrix * gl_MultiTexCoord0);\n"
772 "       vec3 EyeVectorModelSpace = EyePosition - gl_Vertex.xyz;\n"
773 "       EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz);\n"
774 "       EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz);\n"
775 "       EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz);\n"
776 "       gl_Position = ModelViewProjectionMatrix * gl_Vertex;\n"
777 "       ModelViewProjectionPosition = gl_Position;\n"
778 "}\n"
779 "#endif\n"
780 "\n"
781 "#ifdef FRAGMENT_SHADER\n"
782 "uniform sampler2D Texture_Normal;\n"
783 "uniform sampler2D Texture_Refraction;\n"
784 "uniform sampler2D Texture_Reflection;\n"
785 "\n"
786 "uniform vec4 DistortScaleRefractReflect;\n"
787 "uniform vec4 ScreenScaleRefractReflect;\n"
788 "uniform vec4 ScreenCenterRefractReflect;\n"
789 "uniform vec4 RefractColor;\n"
790 "uniform vec4 ReflectColor;\n"
791 "uniform float ReflectFactor;\n"
792 "uniform float ReflectOffset;\n"
793 "\n"
794 "void main(void)\n"
795 "{\n"
796 "       vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);\n"
797 "       //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(vec3(texture2D(Texture_Normal, TexCoord)) - vec3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
798 "       vec4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
799 "       vec4 ScreenTexCoord = SafeScreenTexCoord + vec2(normalize(vec3(texture2D(Texture_Normal, TexCoord)) - vec3(0.5))).xyxy * DistortScaleRefractReflect;\n"
800 "       // FIXME temporary hack to detect the case that the reflection\n"
801 "       // gets blackened at edges due to leaving the area that contains actual\n"
802 "       // content.\n"
803 "       // Remove this 'ack once we have a better way to stop this thing from\n"
804 "       // 'appening.\n"
805 "       float f = min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, 0.01)).rgb) / 0.05);\n"
806 "       f      *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(0.01, -0.01)).rgb) / 0.05);\n"
807 "       f      *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, 0.01)).rgb) / 0.05);\n"
808 "       f      *= min(1.0, length(texture2D(Texture_Refraction, ScreenTexCoord.xy + vec2(-0.01, -0.01)).rgb) / 0.05);\n"
809 "       ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f);\n"
810 "       f       = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, 0.01)).rgb) / 0.05);\n"
811 "       f      *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(0.01, -0.01)).rgb) / 0.05);\n"
812 "       f      *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, 0.01)).rgb) / 0.05);\n"
813 "       f      *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord.zw + vec2(-0.01, -0.01)).rgb) / 0.05);\n"
814 "       ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f);\n"
815 "       float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset;\n"
816 "       gl_FragColor = mix(texture2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, texture2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel);\n"
817 "}\n"
818 "#endif\n"
819 "#else // !MODE_WATER\n"
820 "\n"
821 "\n"
822 "\n"
823 "\n"
824 "// common definitions between vertex shader and fragment shader:\n"
825 "\n"
826 "varying vec2 TexCoord;\n"
827 "#ifdef USEVERTEXTEXTUREBLEND\n"
828 "varying vec2 TexCoord2;\n"
829 "#endif\n"
830 "#ifdef USELIGHTMAP\n"
831 "varying vec2 TexCoordLightmap;\n"
832 "#endif\n"
833 "\n"
834 "#ifdef MODE_LIGHTSOURCE\n"
835 "varying vec3 CubeVector;\n"
836 "#endif\n"
837 "\n"
838 "#ifdef MODE_LIGHTSOURCE\n"
839 "varying vec3 LightVector;\n"
840 "#endif\n"
841 "#if defined(MODE_LIGHTDIRECTION)\n"
842 "varying vec3 LightVector;\n"
843 "#endif\n"
844 "\n"
845 "#ifdef USEEYEVECTOR\n"
846 "varying vec3 EyeVector;\n"
847 "#endif\n"
848 "#ifdef USEFOG\n"
849 "varying vec4 EyeVectorModelSpaceFogPlaneVertexDist;\n"
850 "#endif\n"
851 "\n"
852 "#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY)\n"
853 "varying vec3 VectorS; // direction of S texcoord (sometimes crudely called tangent)\n"
854 "varying vec3 VectorT; // direction of T texcoord (sometimes crudely called binormal)\n"
855 "varying vec3 VectorR; // direction of R texcoord (surface normal)\n"
856 "#endif\n"
857 "\n"
858 "#ifdef USEREFLECTION\n"
859 "varying vec4 ModelViewProjectionPosition;\n"
860 "#endif\n"
861 "#ifdef MODE_DEFERREDLIGHTSOURCE\n"
862 "uniform vec3 LightPosition;\n"
863 "varying vec4 ModelViewPosition;\n"
864 "#endif\n"
865 "\n"
866 "#ifdef MODE_LIGHTSOURCE\n"
867 "uniform vec3 LightPosition;\n"
868 "#endif\n"
869 "uniform vec3 EyePosition;\n"
870 "#ifdef MODE_LIGHTDIRECTION\n"
871 "uniform vec3 LightDir;\n"
872 "#endif\n"
873 "uniform vec4 FogPlane;\n"
874 "\n"
875 "\n"
876 "\n"
877 "\n"
878 "\n"
879 "// 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"
880 "\n"
881 "// fragment shader specific:\n"
882 "#ifdef FRAGMENT_SHADER\n"
883 "\n"
884 "uniform sampler2D Texture_Normal;\n"
885 "uniform sampler2D Texture_Color;\n"
886 "//#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP)\n"
887 "uniform sampler2D Texture_Gloss;\n"
888 "//#endif\n"
889 "#ifdef USEGLOW\n"
890 "uniform sampler2D Texture_Glow;\n"
891 "#endif\n"
892 "#ifdef USEVERTEXTEXTUREBLEND\n"
893 "uniform sampler2D Texture_SecondaryNormal;\n"
894 "uniform sampler2D Texture_SecondaryColor;\n"
895 "//#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP)\n"
896 "uniform sampler2D Texture_SecondaryGloss;\n"
897 "//#endif\n"
898 "#ifdef USEGLOW\n"
899 "uniform sampler2D Texture_SecondaryGlow;\n"
900 "#endif\n"
901 "#endif\n"
902 "#ifdef USECOLORMAPPING\n"
903 "uniform sampler2D Texture_Pants;\n"
904 "uniform sampler2D Texture_Shirt;\n"
905 "#endif\n"
906 "#ifdef USEFOG\n"
907 "uniform sampler2D Texture_FogMask;\n"
908 "#endif\n"
909 "#ifdef USELIGHTMAP\n"
910 "uniform sampler2D Texture_Lightmap;\n"
911 "#endif\n"
912 "#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE)\n"
913 "uniform sampler2D Texture_Deluxemap;\n"
914 "#endif\n"
915 "#ifdef USEREFLECTION\n"
916 "uniform sampler2D Texture_Reflection;\n"
917 "#endif\n"
918 "\n"
919 "#ifdef MODE_DEFERREDLIGHTSOURCE\n"
920 "uniform sampler2DRect Texture_ScreenDepth;\n"
921 "uniform sampler2DRect Texture_ScreenNormalMap;\n"
922 "#endif\n"
923 "#ifdef USEDEFERREDLIGHTMAP\n"
924 "uniform sampler2DRect Texture_ScreenDiffuse;\n"
925 "uniform sampler2DRect Texture_ScreenSpecular;\n"
926 "#endif\n"
927 "\n"
928 "uniform myhalf3 Color_Pants;\n"
929 "uniform myhalf3 Color_Shirt;\n"
930 "uniform myhalf3 FogColor;\n"
931 "\n"
932 "#ifdef USEFOG\n"
933 "uniform float FogRangeRecip;\n"
934 "uniform float FogPlaneViewDist;\n"
935 "uniform float FogHeightFade;\n"
936 "float FogVertex(void)\n"
937 "{\n"
938 "       vec3 EyeVectorModelSpace = EyeVectorModelSpaceFogPlaneVertexDist.xyz;\n"
939 "       float FogPlaneVertexDist = EyeVectorModelSpaceFogPlaneVertexDist.w;\n"
940 "       float fogfrac;\n"
941 "#ifdef USEFOGOUTSIDE\n"
942 "       fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade);\n"
943 "#else\n"
944 "       fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade);\n"
945 "#endif\n"
946 "       return float(texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)));\n"
947 "}\n"
948 "#endif\n"
949 "\n"
950 "#ifdef USEOFFSETMAPPING\n"
951 "uniform float OffsetMapping_Scale;\n"
952 "vec2 OffsetMapping(vec2 TexCoord)\n"
953 "{\n"
954 "#ifdef USEOFFSETMAPPING_RELIEFMAPPING\n"
955 "       // 14 sample relief mapping: linear search and then binary search\n"
956 "       // this basically steps forward a small amount repeatedly until it finds\n"
957 "       // itself inside solid, then jitters forward and back using decreasing\n"
958 "       // amounts to find the impact\n"
959 "       //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1);\n"
960 "       //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1);\n"
961 "       vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1);\n"
962 "       vec3 RT = vec3(TexCoord, 1);\n"
963 "       OffsetVector *= 0.1;\n"
964 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
965 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
966 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
967 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
968 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
969 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
970 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
971 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
972 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
973 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z)          - 0.5);\n"
974 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5    - 0.25);\n"
975 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25   - 0.125);\n"
976 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125  - 0.0625);\n"
977 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125);\n"
978 "       return RT.xy;\n"
979 "#else\n"
980 "       // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits)\n"
981 "       // this basically moves forward the full distance, and then backs up based\n"
982 "       // on height of samples\n"
983 "       //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1));\n"
984 "       //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1));\n"
985 "       vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1));\n"
986 "       TexCoord += OffsetVector;\n"
987 "       OffsetVector *= 0.333;\n"
988 "       TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
989 "       TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
990 "       TexCoord -= OffsetVector * texture2D(Texture_Normal, TexCoord).a;\n"
991 "       return TexCoord;\n"
992 "#endif\n"
993 "}\n"
994 "#endif // USEOFFSETMAPPING\n"
995 "\n"
996 "#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE)\n"
997 "uniform sampler2D Texture_Attenuation;\n"
998 "uniform samplerCube Texture_Cube;\n"
999 "\n"
1000 "#ifdef USESHADOWMAPRECT\n"
1001 "# ifdef USESHADOWSAMPLER\n"
1002 "uniform sampler2DRectShadow Texture_ShadowMapRect;\n"
1003 "# else\n"
1004 "uniform sampler2DRect Texture_ShadowMapRect;\n"
1005 "# endif\n"
1006 "#endif\n"
1007 "\n"
1008 "#ifdef USESHADOWMAP2D\n"
1009 "# ifdef USESHADOWSAMPLER\n"
1010 "uniform sampler2DShadow Texture_ShadowMap2D;\n"
1011 "# else\n"
1012 "uniform sampler2D Texture_ShadowMap2D;\n"
1013 "# endif\n"
1014 "#endif\n"
1015 "\n"
1016 "#ifdef USESHADOWMAPVSDCT\n"
1017 "uniform samplerCube Texture_CubeProjection;\n"
1018 "#endif\n"
1019 "\n"
1020 "#ifdef USESHADOWMAPCUBE\n"
1021 "# ifdef USESHADOWSAMPLER\n"
1022 "uniform samplerCubeShadow Texture_ShadowMapCube;\n"
1023 "# else\n"
1024 "uniform samplerCube Texture_ShadowMapCube;\n"
1025 "# endif\n"
1026 "#endif\n"
1027 "\n"
1028 "#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE)\n"
1029 "uniform vec2 ShadowMap_TextureScale;\n"
1030 "uniform vec4 ShadowMap_Parameters;\n"
1031 "#endif\n"
1032 "\n"
1033 "#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D)\n"
1034 "# ifndef USESHADOWMAPVSDCT\n"
1035 "vec3 GetShadowMapTC2D(vec3 dir)\n"
1036 "{\n"
1037 "       vec3 adir = abs(dir);\n"
1038 "       vec2 tc;\n"
1039 "       vec2 offset;\n"
1040 "       float ma;\n"
1041 "       if (adir.x > adir.y)\n"
1042 "       {\n"
1043 "               if (adir.x > adir.z) // X\n"
1044 "               {\n"
1045 "                       ma = adir.x;\n"
1046 "                       tc = dir.zy;\n"
1047 "                       offset = vec2(mix(0.5, 1.5, dir.x < 0.0), 0.5);\n"
1048 "               }\n"
1049 "               else // Z\n"
1050 "               {\n"
1051 "                       ma = adir.z;\n"
1052 "                       tc = dir.xy;\n"
1053 "                       offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5);\n"
1054 "               }\n"
1055 "       }\n"
1056 "       else\n"
1057 "       {\n"
1058 "               if (adir.y > adir.z) // Y\n"
1059 "               {\n"
1060 "                       ma = adir.y;\n"
1061 "                       tc = dir.xz;\n"
1062 "                       offset = vec2(mix(0.5, 1.5, dir.y < 0.0), 1.5);\n"
1063 "               }\n"
1064 "               else // Z\n"
1065 "               {\n"
1066 "                       ma = adir.z;\n"
1067 "                       tc = dir.xy;\n"
1068 "                       offset = vec2(mix(0.5, 1.5, dir.z < 0.0), 2.5);\n"
1069 "               }\n"
1070 "       }\n"
1071 "\n"
1072 "       vec3 stc = vec3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma;\n"
1073 "       stc.xy += offset * ShadowMap_Parameters.y;\n"
1074 "       stc.z += ShadowMap_Parameters.z;\n"
1075 "       return stc;\n"
1076 "}\n"
1077 "# else\n"
1078 "vec3 GetShadowMapTC2D(vec3 dir)\n"
1079 "{\n"
1080 "       vec3 adir = abs(dir);\n"
1081 "       vec4 proj = textureCube(Texture_CubeProjection, dir);\n"
1082 "       float ma = max(max(adir.x, adir.y), adir.z);\n"
1083 "       vec3 stc = vec3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma;\n"
1084 "       stc.xy += proj.zw * ShadowMap_Parameters.y;\n"
1085 "       stc.z += ShadowMap_Parameters.z;\n"
1086 "       return stc;\n"
1087 "}\n"
1088 "# endif\n"
1089 "#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D)\n"
1090 "\n"
1091 "#ifdef USESHADOWMAPCUBE\n"
1092 "vec4 GetShadowMapTCCube(vec3 dir)\n"
1093 "{\n"
1094 "       vec3 adir = abs(dir);\n"
1095 "       return vec4(dir, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z));\n"
1096 "}\n"
1097 "#endif\n"
1098 "\n"
1099 "# ifdef USESHADOWMAPRECT\n"
1100 "float ShadowMapCompare(vec3 dir)\n"
1101 "{\n"
1102 "       vec3 shadowmaptc = GetShadowMapTC2D(dir);\n"
1103 "       float f;\n"
1104 "#  ifdef USESHADOWSAMPLER\n"
1105 "\n"
1106 "#    ifdef USESHADOWMAPPCF\n"
1107 "#      define texval(x, y) shadow2DRect(Texture_ShadowMapRect, shadowmaptc + vec3(x, y, 0.0)).r\n"
1108 "       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"
1109 "#    else\n"
1110 "       f = shadow2DRect(Texture_ShadowMapRect, shadowmaptc).r;\n"
1111 "#    endif\n"
1112 "\n"
1113 "#  else\n"
1114 "\n"
1115 "#    ifdef USESHADOWMAPPCF\n"
1116 "#      if USESHADOWMAPPCF > 1\n"
1117 "#        define texval(x, y) texture2DRect(Texture_ShadowMapRect, center + vec2(x, y)).r\n"
1118 "       vec2 center = shadowmaptc.xy - 0.5, offset = fract(center);\n"
1119 "       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"
1120 "       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"
1121 "       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"
1122 "       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"
1123 "       vec4 cols = row2 + row3 + mix(row1, row4, offset.y);\n"
1124 "       f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0));\n"
1125 "#      else\n"
1126 "#        define texval(x, y) texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy + vec2(x, y)).r\n"
1127 "       vec2 offset = fract(shadowmaptc.xy);\n"
1128 "       vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0)));\n"
1129 "       vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0)));\n"
1130 "       vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0)));\n"
1131 "       vec3 cols = row2 + mix(row1, row3, offset.y);\n"
1132 "       f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25));\n"
1133 "#      endif\n"
1134 "#    else\n"
1135 "       f = step(shadowmaptc.z, texture2DRect(Texture_ShadowMapRect, shadowmaptc.xy).r);\n"
1136 "#    endif\n"
1137 "\n"
1138 "#  endif\n"
1139 "       return f;\n"
1140 "}\n"
1141 "# endif\n"
1142 "\n"
1143 "# ifdef USESHADOWMAP2D\n"
1144 "float ShadowMapCompare(vec3 dir)\n"
1145 "{\n"
1146 "       vec3 shadowmaptc = GetShadowMapTC2D(dir);\n"
1147 "       float f;\n"
1148 "\n"
1149 "#  ifdef USESHADOWSAMPLER\n"
1150 "#    ifdef USESHADOWMAPPCF\n"
1151 "#      define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r  \n"
1152 "       vec2 center = shadowmaptc.xy*ShadowMap_TextureScale;\n"
1153 "       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"
1154 "#    else\n"
1155 "       f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r;\n"
1156 "#    endif\n"
1157 "#  else\n"
1158 "#    ifdef USESHADOWMAPPCF\n"
1159 "#     if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4)\n"
1160 "#      ifdef GL_ARB_texture_gather\n"
1161 "#        define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y))\n"
1162 "#      else\n"
1163 "#        define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x,y)*ShadowMap_TextureScale)\n"
1164 "#      endif\n"
1165 "       vec2 center = shadowmaptc.xy - 0.5, offset = fract(center);\n"
1166 "       center *= ShadowMap_TextureScale;\n"
1167 "       vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0));\n"
1168 "       vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0));\n"
1169 "       vec4 group3 = step(shadowmaptc.z, texval(-1.0,  1.0));\n"
1170 "       vec4 group4 = step(shadowmaptc.z, texval( 1.0,  1.0));\n"
1171 "       vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) +\n"
1172 "                               mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y);\n"
1173 "       f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0));\n"
1174 "#     else\n"
1175 "#      ifdef GL_EXT_gpu_shader4\n"
1176 "#        define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r\n"
1177 "#      else\n"
1178 "#        define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r  \n"
1179 "#      endif\n"
1180 "#      if USESHADOWMAPPCF > 1\n"
1181 "       vec2 center = shadowmaptc.xy - 0.5, offset = fract(center);\n"
1182 "       center *= ShadowMap_TextureScale;\n"
1183 "       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"
1184 "       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"
1185 "       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"
1186 "       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"
1187 "       vec4 cols = row2 + row3 + mix(row1, row4, offset.y);\n"
1188 "       f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0));\n"
1189 "#      else\n"
1190 "       vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy);\n"
1191 "       vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0)));\n"
1192 "       vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0)));\n"
1193 "       vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0)));\n"
1194 "       vec3 cols = row2 + mix(row1, row3, offset.y);\n"
1195 "       f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25));\n"
1196 "#      endif\n"
1197 "#     endif\n"
1198 "#    else\n"
1199 "       f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r);\n"
1200 "#    endif\n"
1201 "#  endif\n"
1202 "       return f;\n"
1203 "}\n"
1204 "# endif\n"
1205 "\n"
1206 "# ifdef USESHADOWMAPCUBE\n"
1207 "float ShadowMapCompare(vec3 dir)\n"
1208 "{\n"
1209 "       // apply depth texture cubemap as light filter\n"
1210 "       vec4 shadowmaptc = GetShadowMapTCCube(dir);\n"
1211 "       float f;\n"
1212 "#  ifdef USESHADOWSAMPLER\n"
1213 "       f = shadowCube(Texture_ShadowMapCube, shadowmaptc).r;\n"
1214 "#  else\n"
1215 "       f = step(shadowmaptc.w, textureCube(Texture_ShadowMapCube, shadowmaptc.xyz).r);\n"
1216 "#  endif\n"
1217 "       return f;\n"
1218 "}\n"
1219 "# endif\n"
1220 "#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE)\n"
1221 "#endif // FRAGMENT_SHADER\n"
1222 "\n"
1223 "\n"
1224 "\n"
1225 "\n"
1226 "#ifdef MODE_DEFERREDGEOMETRY\n"
1227 "#ifdef VERTEX_SHADER\n"
1228 "uniform mat4 TexMatrix;\n"
1229 "#ifdef USEVERTEXTEXTUREBLEND\n"
1230 "uniform mat4 BackgroundTexMatrix;\n"
1231 "#endif\n"
1232 "uniform mat4 ModelViewMatrix;\n"
1233 "void main(void)\n"
1234 "{\n"
1235 "       TexCoord = vec2(TexMatrix * gl_MultiTexCoord0);\n"
1236 "#ifdef USEVERTEXTEXTUREBLEND\n"
1237 "       gl_FrontColor = gl_Color;\n"
1238 "       TexCoord2 = vec2(BackgroundTexMatrix * gl_MultiTexCoord0);\n"
1239 "#endif\n"
1240 "\n"
1241 "       // transform unnormalized eye direction into tangent space\n"
1242 "#ifdef USEOFFSETMAPPING\n"
1243 "       vec3 EyeVectorModelSpace = EyePosition - gl_Vertex.xyz;\n"
1244 "       EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz);\n"
1245 "       EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz);\n"
1246 "       EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz);\n"
1247 "#endif\n"
1248 "\n"
1249 "       VectorS = (ModelViewMatrix * vec4(gl_MultiTexCoord1.xyz, 0)).xyz;\n"
1250 "       VectorT = (ModelViewMatrix * vec4(gl_MultiTexCoord2.xyz, 0)).xyz;\n"
1251 "       VectorR = (ModelViewMatrix * vec4(gl_MultiTexCoord3.xyz, 0)).xyz;\n"
1252 "       gl_Position = ModelViewProjectionMatrix * gl_Vertex;\n"
1253 "}\n"
1254 "#endif // VERTEX_SHADER\n"
1255 "\n"
1256 "#ifdef FRAGMENT_SHADER\n"
1257 "void main(void)\n"
1258 "{\n"
1259 "#ifdef USEOFFSETMAPPING\n"
1260 "       // apply offsetmapping\n"
1261 "       vec2 TexCoordOffset = OffsetMapping(TexCoord);\n"
1262 "#define TexCoord TexCoordOffset\n"
1263 "#endif\n"
1264 "\n"
1265 "#ifdef USEALPHAKILL\n"
1266 "       if (texture2D(Texture_Color, TexCoord).a < 0.5)\n"
1267 "               discard;\n"
1268 "#endif\n"
1269 "\n"
1270 "#ifdef USEVERTEXTEXTUREBLEND\n"
1271 "       float alpha = texture2D(Texture_Color, TexCoord).a;\n"
1272 "       float terrainblend = clamp(float(gl_Color.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0));\n"
1273 "       //float terrainblend = min(float(gl_Color.a) * alpha * 2.0, float(1.0));\n"
1274 "       //float terrainblend = float(gl_Color.a) * alpha > 0.5;\n"
1275 "#endif\n"
1276 "\n"
1277 "#ifdef USEVERTEXTEXTUREBLEND\n"
1278 "       vec3 surfacenormal = mix(vec3(texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(texture2D(Texture_Normal, TexCoord)), terrainblend) - vec3(0.5, 0.5, 0.5);\n"
1279 "#else\n"
1280 "       vec3 surfacenormal = vec3(texture2D(Texture_Normal, TexCoord)) - vec3(0.5, 0.5, 0.5);\n"
1281 "#endif\n"
1282 "\n"
1283 "       gl_FragColor = vec4(normalize(surfacenormal.x * VectorS + surfacenormal.y * VectorT + surfacenormal.z * VectorR) * 0.5 + vec3(0.5, 0.5, 0.5), 1);\n"
1284 "}\n"
1285 "#endif // FRAGMENT_SHADER\n"
1286 "#else // !MODE_DEFERREDGEOMETRY\n"
1287 "\n"
1288 "\n"
1289 "\n"
1290 "\n"
1291 "#ifdef MODE_DEFERREDLIGHTSOURCE\n"
1292 "#ifdef VERTEX_SHADER\n"
1293 "uniform mat4 ModelViewMatrix;\n"
1294 "void main(void)\n"
1295 "{\n"
1296 "       ModelViewPosition = ModelViewMatrix * gl_Vertex;\n"
1297 "       gl_Position = ModelViewProjectionMatrix * gl_Vertex;\n"
1298 "}\n"
1299 "#endif // VERTEX_SHADER\n"
1300 "\n"
1301 "#ifdef FRAGMENT_SHADER\n"
1302 "uniform mat4 ViewToLight;\n"
1303 "// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far));\n"
1304 "uniform vec2 ScreenToDepth;\n"
1305 "uniform myhalf3 DeferredColor_Ambient;\n"
1306 "uniform myhalf3 DeferredColor_Diffuse;\n"
1307 "#ifdef USESPECULAR\n"
1308 "uniform myhalf3 DeferredColor_Specular;\n"
1309 "uniform myhalf SpecularPower;\n"
1310 "#endif\n"
1311 "void main(void)\n"
1312 "{\n"
1313 "       // calculate viewspace pixel position\n"
1314 "       vec3 position;\n"
1315 "       position.z = ScreenToDepth.y / (texture2DRect(Texture_ScreenDepth, gl_FragCoord.xy).r + ScreenToDepth.x);\n"
1316 "       position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z);\n"
1317 "       // decode viewspace pixel normal\n"
1318 "       myhalf4 normalmap = texture2DRect(Texture_ScreenNormalMap, gl_FragCoord.xy);\n"
1319 "       myhalf3 surfacenormal = normalize(normalmap.rgb - myhalf3(0.5,0.5,0.5));\n"
1320 "       // surfacenormal = pixel normal in viewspace\n"
1321 "       // LightVector = pixel to light in viewspace\n"
1322 "       // CubeVector = position in lightspace\n"
1323 "       // eyevector = pixel to view in viewspace\n"
1324 "       vec3 CubeVector = vec3(ViewToLight * vec4(position,1));\n"
1325 "       myhalf fade = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0)));\n"
1326 "#ifdef USEDIFFUSE\n"
1327 "       // calculate diffuse shading\n"
1328 "       myhalf3 lightnormal = myhalf3(normalize(LightPosition - position));\n"
1329 "       myhalf diffuse = myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0));\n"
1330 "#endif\n"
1331 "#ifdef USESPECULAR\n"
1332 "       // calculate directional shading\n"
1333 "       vec3 eyevector = position * -1.0;\n"
1334 "#  ifdef USEEXACTSPECULARMATH\n"
1335 "       myhalf specular = pow(myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), SpecularPower);\n"
1336 "#  else\n"
1337 "       myhalf3 specularnormal = normalize(lightnormal + myhalf3(normalize(eyevector)));\n"
1338 "       myhalf specular = pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower);\n"
1339 "#  endif\n"
1340 "#endif\n"
1341 "\n"
1342 "#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D)\n"
1343 "       fade *= ShadowMapCompare(CubeVector);\n"
1344 "#endif\n"
1345 "\n"
1346 "#ifdef USEDIFFUSE\n"
1347 "       gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0);\n"
1348 "#else\n"
1349 "       gl_FragData[0] = vec4(DeferredColor_Ambient * fade, 1.0);\n"
1350 "#endif\n"
1351 "#ifdef USESPECULAR\n"
1352 "       gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0);\n"
1353 "#else\n"
1354 "       gl_FragData[1] = vec4(0.0, 0.0, 0.0, 1.0);\n"
1355 "#endif\n"
1356 "\n"
1357 "# ifdef USECUBEFILTER\n"
1358 "       vec3 cubecolor = textureCube(Texture_Cube, CubeVector).rgb;\n"
1359 "       gl_FragData[0] *= cubecolor;\n"
1360 "       gl_FragData[1] *= cubecolor;\n"
1361 "# endif\n"
1362 "}\n"
1363 "#endif // FRAGMENT_SHADER\n"
1364 "#else // !MODE_DEFERREDLIGHTSOURCE\n"
1365 "\n"
1366 "\n"
1367 "\n"
1368 "\n"
1369 "#ifdef VERTEX_SHADER\n"
1370 "uniform mat4 TexMatrix;\n"
1371 "#ifdef USEVERTEXTEXTUREBLEND\n"
1372 "uniform mat4 BackgroundTexMatrix;\n"
1373 "#endif\n"
1374 "#ifdef MODE_LIGHTSOURCE\n"
1375 "uniform mat4 ModelToLight;\n"
1376 "#endif\n"
1377 "void main(void)\n"
1378 "{\n"
1379 "#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND)\n"
1380 "       gl_FrontColor = gl_Color;\n"
1381 "#endif\n"
1382 "       // copy the surface texcoord\n"
1383 "       TexCoord = vec2(TexMatrix * gl_MultiTexCoord0);\n"
1384 "#ifdef USEVERTEXTEXTUREBLEND\n"
1385 "       TexCoord2 = vec2(BackgroundTexMatrix * gl_MultiTexCoord0);\n"
1386 "#endif\n"
1387 "#ifdef USELIGHTMAP\n"
1388 "       TexCoordLightmap = vec2(gl_MultiTexCoord4);\n"
1389 "#endif\n"
1390 "\n"
1391 "#ifdef MODE_LIGHTSOURCE\n"
1392 "       // transform vertex position into light attenuation/cubemap space\n"
1393 "       // (-1 to +1 across the light box)\n"
1394 "       CubeVector = vec3(ModelToLight * gl_Vertex);\n"
1395 "\n"
1396 "# ifdef USEDIFFUSE\n"
1397 "       // transform unnormalized light direction into tangent space\n"
1398 "       // (we use unnormalized to ensure that it interpolates correctly and then\n"
1399 "       //  normalize it per pixel)\n"
1400 "       vec3 lightminusvertex = LightPosition - gl_Vertex.xyz;\n"
1401 "       LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz);\n"
1402 "       LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz);\n"
1403 "       LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz);\n"
1404 "# endif\n"
1405 "#endif\n"
1406 "\n"
1407 "#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE)\n"
1408 "       LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz);\n"
1409 "       LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz);\n"
1410 "       LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz);\n"
1411 "#endif\n"
1412 "\n"
1413 "       // transform unnormalized eye direction into tangent space\n"
1414 "#ifdef USEEYEVECTOR\n"
1415 "       vec3 EyeVectorModelSpace = EyePosition - gl_Vertex.xyz;\n"
1416 "       EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz);\n"
1417 "       EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz);\n"
1418 "       EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz);\n"
1419 "#endif\n"
1420 "\n"
1421 "#ifdef USEFOG\n"
1422 "       EyeVectorModelSpaceFogPlaneVertexDist.xyz = EyePosition - gl_Vertex.xyz;\n"
1423 "       EyeVectorModelSpaceFogPlaneVertexDist.w = dot(FogPlane, gl_Vertex);\n"
1424 "#endif\n"
1425 "\n"
1426 "#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
1427 "       VectorS = gl_MultiTexCoord1.xyz;\n"
1428 "       VectorT = gl_MultiTexCoord2.xyz;\n"
1429 "       VectorR = gl_MultiTexCoord3.xyz;\n"
1430 "#endif\n"
1431 "\n"
1432 "       // transform vertex to camera space, using ftransform to match non-VS rendering\n"
1433 "       gl_Position = ModelViewProjectionMatrix * gl_Vertex;\n"
1434 "\n"
1435 "#ifdef USEREFLECTION\n"
1436 "       ModelViewProjectionPosition = gl_Position;\n"
1437 "#endif\n"
1438 "}\n"
1439 "#endif // VERTEX_SHADER\n"
1440 "\n"
1441 "\n"
1442 "\n"
1443 "\n"
1444 "#ifdef FRAGMENT_SHADER\n"
1445 "#ifdef USEDEFERREDLIGHTMAP\n"
1446 "uniform myhalf3 DeferredMod_Diffuse;\n"
1447 "uniform myhalf3 DeferredMod_Specular;\n"
1448 "#endif\n"
1449 "uniform myhalf3 Color_Ambient;\n"
1450 "uniform myhalf3 Color_Diffuse;\n"
1451 "uniform myhalf3 Color_Specular;\n"
1452 "uniform myhalf SpecularPower;\n"
1453 "#ifdef USEGLOW\n"
1454 "uniform myhalf3 Color_Glow;\n"
1455 "#endif\n"
1456 "uniform myhalf Alpha;\n"
1457 "#ifdef USEREFLECTION\n"
1458 "uniform vec4 DistortScaleRefractReflect;\n"
1459 "uniform vec4 ScreenScaleRefractReflect;\n"
1460 "uniform vec4 ScreenCenterRefractReflect;\n"
1461 "uniform myhalf4 ReflectColor;\n"
1462 "#endif\n"
1463 "#ifdef MODE_LIGHTDIRECTION\n"
1464 "uniform myhalf3 LightColor;\n"
1465 "#endif\n"
1466 "#ifdef MODE_LIGHTSOURCE\n"
1467 "uniform myhalf3 LightColor;\n"
1468 "#endif\n"
1469 "void main(void)\n"
1470 "{\n"
1471 "#ifdef USEOFFSETMAPPING\n"
1472 "       // apply offsetmapping\n"
1473 "       vec2 TexCoordOffset = OffsetMapping(TexCoord);\n"
1474 "#define TexCoord TexCoordOffset\n"
1475 "#endif\n"
1476 "\n"
1477 "       // combine the diffuse textures (base, pants, shirt)\n"
1478 "       myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord));\n"
1479 "#ifdef USEALPHAKILL\n"
1480 "       if (color.a < 0.5)\n"
1481 "               discard;\n"
1482 "#endif\n"
1483 "       color.a *= Alpha;\n"
1484 "#ifdef USECOLORMAPPING\n"
1485 "       color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt;\n"
1486 "#endif\n"
1487 "#ifdef USEVERTEXTEXTUREBLEND\n"
1488 "       myhalf terrainblend = clamp(myhalf(gl_Color.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0));\n"
1489 "       //myhalf terrainblend = min(myhalf(gl_Color.a) * color.a * 2.0, myhalf(1.0));\n"
1490 "       //myhalf terrainblend = myhalf(gl_Color.a) * color.a > 0.5;\n"
1491 "       color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend);\n"
1492 "       color.a = 1.0;\n"
1493 "       //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend);\n"
1494 "#endif\n"
1495 "\n"
1496 "       // get the surface normal\n"
1497 "#ifdef USEVERTEXTEXTUREBLEND\n"
1498 "       myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5));\n"
1499 "#else\n"
1500 "       myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5));\n"
1501 "#endif\n"
1502 "\n"
1503 "       // get the material colors\n"
1504 "       myhalf3 diffusetex = color.rgb;\n"
1505 "#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP)\n"
1506 "# ifdef USEVERTEXTEXTUREBLEND\n"
1507 "       myhalf3 glosstex = mix(myhalf3(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf3(texture2D(Texture_Gloss, TexCoord)), terrainblend);\n"
1508 "# else\n"
1509 "       myhalf3 glosstex = myhalf3(texture2D(Texture_Gloss, TexCoord));\n"
1510 "# endif\n"
1511 "#endif\n"
1512 "\n"
1513 "\n"
1514 "\n"
1515 "\n"
1516 "#ifdef MODE_LIGHTSOURCE\n"
1517 "       // light source\n"
1518 "       myhalf3 lightnormal = myhalf3(normalize(LightVector));\n"
1519 "       myhalf diffuse = myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0));\n"
1520 "       color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse);\n"
1521 "#ifdef USESPECULAR\n"
1522 "#ifdef USEEXACTSPECULARMATH\n"
1523 "       myhalf specular = pow(myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower);\n"
1524 "#else\n"
1525 "       myhalf3 specularnormal = normalize(lightnormal + myhalf3(normalize(EyeVector)));\n"
1526 "       myhalf specular = pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower);\n"
1527 "#endif\n"
1528 "       color.rgb += glosstex * (specular * Color_Specular);\n"
1529 "#endif\n"
1530 "       color.rgb *= LightColor;\n"
1531 "       color.rgb *= myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0)));\n"
1532 "#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D)\n"
1533 "       color.rgb *= ShadowMapCompare(CubeVector);\n"
1534 "#endif\n"
1535 "# ifdef USECUBEFILTER\n"
1536 "       color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector));\n"
1537 "# endif\n"
1538 "#endif // MODE_LIGHTSOURCE\n"
1539 "\n"
1540 "\n"
1541 "\n"
1542 "\n"
1543 "#ifdef MODE_LIGHTDIRECTION\n"
1544 "#define SHADING\n"
1545 "       myhalf3 lightnormal = myhalf3(normalize(LightVector));\n"
1546 "#define lightcolor LightColor\n"
1547 "#endif // MODE_LIGHTDIRECTION\n"
1548 "#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
1549 "#define SHADING\n"
1550 "       // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe)\n"
1551 "       myhalf3 lightnormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0);\n"
1552 "       myhalf3 lightcolor = myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap));\n"
1553 "       // convert modelspace light vector to tangentspace\n"
1554 "       myhalf3 lightnormal;\n"
1555 "       lightnormal.x = dot(lightnormal_modelspace, myhalf3(VectorS));\n"
1556 "       lightnormal.y = dot(lightnormal_modelspace, myhalf3(VectorT));\n"
1557 "       lightnormal.z = dot(lightnormal_modelspace, myhalf3(VectorR));\n"
1558 "       // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division)\n"
1559 "       // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar\n"
1560 "       // is used (the lightmap and deluxemap coords correspond to virtually random coordinates\n"
1561 "       // on that luxel, and NOT to its center, because recursive triangle subdivision is used\n"
1562 "       // to map the luxels to coordinates on the draw surfaces), which also causes\n"
1563 "       // deluxemaps to be wrong because light contributions from the wrong side of the surface\n"
1564 "       // are added up. To prevent divisions by zero or strong exaggerations, a max()\n"
1565 "       // nudge is done here at expense of some additional fps. This is ONLY needed for\n"
1566 "       // deluxemaps, tangentspace deluxemap avoid this problem by design.\n"
1567 "       lightcolor *= 1.0 / max(0.25, lightnormal.z);\n"
1568 "#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
1569 "#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n"
1570 "#define SHADING\n"
1571 "       // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light)\n"
1572 "       myhalf3 lightnormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0);\n"
1573 "       myhalf3 lightcolor = myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap));\n"
1574 "#endif\n"
1575 "\n"
1576 "\n"
1577 "\n"
1578 "\n"
1579 "#ifdef MODE_LIGHTMAP\n"
1580 "       color.rgb = diffusetex * (Color_Ambient + myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * Color_Diffuse);\n"
1581 "#endif // MODE_LIGHTMAP\n"
1582 "#ifdef MODE_VERTEXCOLOR\n"
1583 "       color.rgb = diffusetex * (Color_Ambient + myhalf3(gl_Color.rgb) * Color_Diffuse);\n"
1584 "#endif // MODE_VERTEXCOLOR\n"
1585 "#ifdef MODE_FLATCOLOR\n"
1586 "       color.rgb = diffusetex * Color_Ambient;\n"
1587 "#endif // MODE_FLATCOLOR\n"
1588 "\n"
1589 "\n"
1590 "\n"
1591 "\n"
1592 "#ifdef SHADING\n"
1593 "# ifdef USEDIFFUSE\n"
1594 "       myhalf diffuse = myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0));\n"
1595 "#  ifdef USESPECULAR\n"
1596 "#   ifdef USEEXACTSPECULARMATH\n"
1597 "       myhalf specular = pow(myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower);\n"
1598 "#   else\n"
1599 "       myhalf3 specularnormal = normalize(lightnormal + myhalf3(normalize(EyeVector)));\n"
1600 "       myhalf specular = pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower);\n"
1601 "#   endif\n"
1602 "       color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex * Color_Specular * specular) * lightcolor;\n"
1603 "#  else\n"
1604 "       color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor);\n"
1605 "#  endif\n"
1606 "# else\n"
1607 "       color.rgb = diffusetex * Color_Ambient;\n"
1608 "# endif\n"
1609 "#endif\n"
1610 "\n"
1611 "#ifdef USEDEFERREDLIGHTMAP\n"
1612 "       color.rgb += diffusetex * myhalf3(texture2DRect(Texture_ScreenDiffuse, gl_FragCoord.xy)) * DeferredMod_Diffuse;\n"
1613 "       color.rgb += glosstex * myhalf3(texture2DRect(Texture_ScreenSpecular, gl_FragCoord.xy)) * DeferredMod_Specular;\n"
1614 "#endif\n"
1615 "\n"
1616 "#ifdef USEGLOW\n"
1617 "#ifdef USEVERTEXTEXTUREBLEND\n"
1618 "       color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend) * Color_Glow;\n"
1619 "#else\n"
1620 "       color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * Color_Glow;\n"
1621 "#endif\n"
1622 "#endif\n"
1623 "\n"
1624 "#ifdef USEFOG\n"
1625 "#ifdef MODE_LIGHTSOURCE\n"
1626 "       color.rgb *= myhalf(FogVertex());\n"
1627 "#else\n"
1628 "       color.rgb = mix(FogColor, color.rgb, FogVertex());\n"
1629 "#endif\n"
1630 "#endif\n"
1631 "\n"
1632 "       // 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"
1633 "#ifdef USEREFLECTION\n"
1634 "       vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);\n"
1635 "       //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
1636 "       vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw;\n"
1637 "       vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw;\n"
1638 "       // FIXME temporary hack to detect the case that the reflection\n"
1639 "       // gets blackened at edges due to leaving the area that contains actual\n"
1640 "       // content.\n"
1641 "       // Remove this 'ack once we have a better way to stop this thing from\n"
1642 "       // 'appening.\n"
1643 "       float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05);\n"
1644 "       f      *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05);\n"
1645 "       f      *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05);\n"
1646 "       f      *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05);\n"
1647 "       ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f);\n"
1648 "       color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a);\n"
1649 "#endif\n"
1650 "\n"
1651 "       gl_FragColor = vec4(color);\n"
1652 "}\n"
1653 "#endif // FRAGMENT_SHADER\n"
1654 "\n"
1655 "#endif // !MODE_DEFERREDLIGHTSOURCE\n"
1656 "#endif // !MODE_DEFERREDGEOMETRY\n"
1657 "#endif // !MODE_WATER\n"
1658 "#endif // !MODE_REFRACTION\n"
1659 "#endif // !MODE_BLOOMBLUR\n"
1660 "#endif // !MODE_GENERIC\n"
1661 "#endif // !MODE_POSTPROCESS\n"
1662 "#endif // !MODE_SHOWDEPTH\n"
1663 "#endif // !MODE_DEPTH_OR_SHADOW\n"
1664 ;
1665
1666 /*
1667 =========================================================================================================================================================
1668
1669
1670
1671 =========================================================================================================================================================
1672
1673
1674
1675 =========================================================================================================================================================
1676
1677
1678
1679 =========================================================================================================================================================
1680
1681
1682
1683 =========================================================================================================================================================
1684
1685
1686
1687 =========================================================================================================================================================
1688
1689
1690
1691 =========================================================================================================================================================
1692 */
1693
1694 const char *builtincgshaderstring =
1695 "// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader\n"
1696 "// written by Forest 'LordHavoc' Hale\n"
1697 "// shadowmapping enhancements by Lee 'eihrul' Salzman\n"
1698 "\n"
1699 "#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE)\n"
1700 "# define USEFOG\n"
1701 "#endif\n"
1702 "#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE)\n"
1703 "#define USELIGHTMAP\n"
1704 "#endif\n"
1705 "#if defined(USESPECULAR) || defined(USEOFFSETMAPPING)\n"
1706 "#define USEEYEVECTOR\n"
1707 "#endif\n"
1708 "\n"
1709 "#ifdef MODE_DEPTH_OR_SHADOW\n"
1710 "#ifdef VERTEX_SHADER\n"
1711 "void main\n"
1712 "(\n"
1713 "float4 gl_Vertex : POSITION,\n"
1714 "uniform float4x4 ModelViewProjectionMatrix,\n"
1715 "out float4 gl_Position : POSITION\n"
1716 ")\n"
1717 "{\n"
1718 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
1719 "}\n"
1720 "#endif\n"
1721 "#else // !MODE_DEPTH_ORSHADOW\n"
1722 "\n"
1723 "\n"
1724 "\n"
1725 "\n"
1726 "#ifdef MODE_SHOWDEPTH\n"
1727 "#ifdef VERTEX_SHADER\n"
1728 "void main\n"
1729 "(\n"
1730 "float4 gl_Vertex : POSITION,\n"
1731 "uniform float4x4 ModelViewProjectionMatrix,\n"
1732 "out float4 gl_Position : POSITION,\n"
1733 "out float4 gl_FrontColor : COLOR0\n"
1734 ")\n"
1735 "{\n"
1736 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
1737 "       gl_FrontColor = float4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0);\n"
1738 "}\n"
1739 "#endif\n"
1740 "\n"
1741 "#ifdef FRAGMENT_SHADER\n"
1742 "void main\n"
1743 "(\n"
1744 "float4 gl_FrontColor : COLOR0,\n"
1745 "out float4 gl_FragColor : COLOR\n"
1746 ")\n"
1747 "{\n"
1748 "       gl_FragColor = gl_FrontColor;\n"
1749 "}\n"
1750 "#endif\n"
1751 "#else // !MODE_SHOWDEPTH\n"
1752 "\n"
1753 "\n"
1754 "\n"
1755 "\n"
1756 "#ifdef MODE_POSTPROCESS\n"
1757 "\n"
1758 "#ifdef VERTEX_SHADER\n"
1759 "void main\n"
1760 "(\n"
1761 "float4 gl_Vertex : POSITION,\n"
1762 "uniform float4x4 ModelViewProjectionMatrix,\n"
1763 "float4 gl_MultiTexCoord0 : TEXCOORD0,\n"
1764 "float4 gl_MultiTexCoord1 : TEXCOORD1,\n"
1765 "out float4 gl_Position : POSITION,\n"
1766 "out float2 TexCoord1 : TEXCOORD0,\n"
1767 "out float2 TexCoord2 : TEXCOORD1\n"
1768 ")\n"
1769 "{\n"
1770 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
1771 "       TexCoord1 = gl_MultiTexCoord0.xy;\n"
1772 "#ifdef USEBLOOM\n"
1773 "       TexCoord2 = gl_MultiTexCoord1.xy;\n"
1774 "#endif\n"
1775 "}\n"
1776 "#endif\n"
1777 "\n"
1778 "#ifdef FRAGMENT_SHADER\n"
1779 "void main\n"
1780 "(\n"
1781 "float2 TexCoord1 : TEXCOORD0,\n"
1782 "float2 TexCoord2 : TEXCOORD1,\n"
1783 "uniform sampler2D Texture_First,\n"
1784 "#ifdef USEBLOOM\n"
1785 "uniform sampler2D Texture_Second,\n"
1786 "#endif\n"
1787 "#ifdef USEGAMMARAMPS\n"
1788 "uniform sampler2D Texture_GammaRamps,\n"
1789 "#endif\n"
1790 "#ifdef USESATURATION\n"
1791 "uniform float Saturation,\n"
1792 "#endif\n"
1793 "#ifdef USEVIEWTINT\n"
1794 "uniform float4 ViewTintColor,\n"
1795 "#endif\n"
1796 "uniform float4 UserVec1,\n"
1797 "uniform float4 UserVec2,\n"
1798 "uniform float4 UserVec3,\n"
1799 "uniform float4 UserVec4,\n"
1800 "uniform float ClientTime,\n"
1801 "uniform float2 PixelSize,\n"
1802 "out float4 gl_FragColor : COLOR\n"
1803 ")\n"
1804 "{\n"
1805 "       gl_FragColor = tex2D(Texture_First, TexCoord1);\n"
1806 "#ifdef USEBLOOM\n"
1807 "       gl_FragColor += tex2D(Texture_Second, TexCoord2);\n"
1808 "#endif\n"
1809 "#ifdef USEVIEWTINT\n"
1810 "       gl_FragColor = mix(gl_FragColor, ViewTintColor, ViewTintColor.a);\n"
1811 "#endif\n"
1812 "\n"
1813 "#ifdef USEPOSTPROCESSING\n"
1814 "// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want\n"
1815 "// 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"
1816 "       gl_FragColor += tex2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*float2(-0.987688, -0.156434)) * UserVec1.y;\n"
1817 "       gl_FragColor += tex2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*float2(-0.156434, -0.891007)) * UserVec1.y;\n"
1818 "       gl_FragColor += tex2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*float2( 0.891007, -0.453990)) * UserVec1.y;\n"
1819 "       gl_FragColor += tex2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*float2( 0.707107,  0.707107)) * UserVec1.y;\n"
1820 "       gl_FragColor += tex2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*float2(-0.453990,  0.891007)) * UserVec1.y;\n"
1821 "       gl_FragColor /= (1 + 5 * UserVec1.y);\n"
1822 "#endif\n"
1823 "\n"
1824 "#ifdef USESATURATION\n"
1825 "       //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter\n"
1826 "       float y = dot(gl_FragColor.rgb, float3(0.299, 0.587, 0.114));\n"
1827 "       //gl_FragColor = float3(y) + (gl_FragColor.rgb - float3(y)) * Saturation;\n"
1828 "       gl_FragColor.rgb = mix(float3(y), gl_FragColor.rgb, Saturation);\n"
1829 "#endif\n"
1830 "\n"
1831 "#ifdef USEGAMMARAMPS\n"
1832 "       gl_FragColor.r = tex2D(Texture_GammaRamps, float2(gl_FragColor.r, 0)).r;\n"
1833 "       gl_FragColor.g = tex2D(Texture_GammaRamps, float2(gl_FragColor.g, 0)).g;\n"
1834 "       gl_FragColor.b = tex2D(Texture_GammaRamps, float2(gl_FragColor.b, 0)).b;\n"
1835 "#endif\n"
1836 "}\n"
1837 "#endif\n"
1838 "#else // !MODE_POSTPROCESS\n"
1839 "\n"
1840 "\n"
1841 "\n"
1842 "\n"
1843 "#ifdef MODE_GENERIC\n"
1844 "#ifdef VERTEX_SHADER\n"
1845 "void main\n"
1846 "(\n"
1847 "float4 gl_Vertex : POSITION,\n"
1848 "uniform float4x4 ModelViewProjectionMatrix,\n"
1849 "float4 gl_Color : COLOR0,\n"
1850 "float4 gl_MultiTexCoord0 : TEXCOORD0,\n"
1851 "float4 gl_MultiTexCoord1 : TEXCOORD1,\n"
1852 "out float4 gl_Position : POSITION,\n"
1853 "out float4 gl_FrontColor : COLOR,\n"
1854 "out float2 TexCoord1 : TEXCOORD0,\n"
1855 "out float2 TexCoord2 : TEXCOORD1\n"
1856 ")\n"
1857 "{\n"
1858 "       gl_FrontColor = gl_Color;\n"
1859 "#ifdef USEDIFFUSE\n"
1860 "       TexCoord1 = gl_MultiTexCoord0.xy;\n"
1861 "#endif\n"
1862 "#ifdef USESPECULAR\n"
1863 "       TexCoord2 = gl_MultiTexCoord1.xy;\n"
1864 "#endif\n"
1865 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
1866 "}\n"
1867 "#endif\n"
1868 "\n"
1869 "#ifdef FRAGMENT_SHADER\n"
1870 "\n"
1871 "void main\n"
1872 "(\n"
1873 "float4 gl_FrontColor : COLOR,\n"
1874 "float2 TexCoord1 : TEXCOORD0,\n"
1875 "float2 TexCoord2 : TEXCOORD1,\n"
1876 "#ifdef USEDIFFUSE\n"
1877 "uniform sampler2D Texture_First,\n"
1878 "#endif\n"
1879 "#ifdef USESPECULAR\n"
1880 "uniform sampler2D Texture_Second,\n"
1881 "#endif\n"
1882 "out float4 gl_FragColor : COLOR\n"
1883 ")\n"
1884 "{\n"
1885 "       gl_FragColor = gl_FrontColor;\n"
1886 "#ifdef USEDIFFUSE\n"
1887 "       gl_FragColor *= tex2D(Texture_First, TexCoord1);\n"
1888 "#endif\n"
1889 "\n"
1890 "#ifdef USESPECULAR\n"
1891 "       float4 tex2 = tex2D(Texture_Second, TexCoord2);\n"
1892 "# ifdef USECOLORMAPPING\n"
1893 "       gl_FragColor *= tex2;\n"
1894 "# endif\n"
1895 "# ifdef USEGLOW\n"
1896 "       gl_FragColor += tex2;\n"
1897 "# endif\n"
1898 "# ifdef USEVERTEXTEXTUREBLEND\n"
1899 "       gl_FragColor = mix(gl_FragColor, tex2, tex2.a);\n"
1900 "# endif\n"
1901 "#endif\n"
1902 "}\n"
1903 "#endif\n"
1904 "#else // !MODE_GENERIC\n"
1905 "\n"
1906 "\n"
1907 "\n"
1908 "\n"
1909 "#ifdef MODE_BLOOMBLUR\n"
1910 "#ifdef VERTEX_SHADER\n"
1911 "void main\n"
1912 "(\n"
1913 "float4 gl_Vertex : POSITION,\n"
1914 "uniform float4x4 ModelViewProjectionMatrix,\n"
1915 "float4 gl_MultiTexCoord0 : TEXCOORD0,\n"
1916 "out float4 gl_Position : POSITION,\n"
1917 "out float2 TexCoord : TEXCOORD0\n"
1918 ")\n"
1919 "{\n"
1920 "       TexCoord = gl_MultiTexCoord0.xy;\n"
1921 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
1922 "}\n"
1923 "#endif\n"
1924 "\n"
1925 "#ifdef FRAGMENT_SHADER\n"
1926 "\n"
1927 "void main\n"
1928 "(\n"
1929 "float2 TexCoord : TEXCOORD0,\n"
1930 "uniform sampler2D Texture_First,\n"
1931 "uniform float4 BloomBlur_Parameters,\n"
1932 "out float4 gl_FragColor : COLOR\n"
1933 ")\n"
1934 "{\n"
1935 "       int i;\n"
1936 "       float2 tc = TexCoord;\n"
1937 "       float3 color = tex2D(Texture_First, tc).rgb;\n"
1938 "       tc += BloomBlur_Parameters.xy;\n"
1939 "       for (i = 1;i < SAMPLES;i++)\n"
1940 "       {\n"
1941 "               color += tex2D(Texture_First, tc).rgb;\n"
1942 "               tc += BloomBlur_Parameters.xy;\n"
1943 "       }\n"
1944 "       gl_FragColor = float4(color * BloomBlur_Parameters.z + float3(BloomBlur_Parameters.w), 1);\n"
1945 "}\n"
1946 "#endif\n"
1947 "#else // !MODE_BLOOMBLUR\n"
1948 "#ifdef MODE_REFRACTION\n"
1949 "#ifdef VERTEX_SHADER\n"
1950 "void main\n"
1951 "(\n"
1952 "float4 gl_Vertex : POSITION,\n"
1953 "uniform float4x4 ModelViewProjectionMatrix,\n"
1954 "float4 gl_MultiTexCoord0 : TEXCOORD0,\n"
1955 "uniform mat4 TexMatrix,\n"
1956 "uniform float3 EyePosition,\n"
1957 "out float4 gl_Position : POSITION,\n"
1958 "out float2 TexCoord : TEXCOORD0,\n"
1959 "out float3 EyeVector : TEXCOORD1,\n"
1960 "out float4 ModelViewProjectionPosition : TEXCOORD2\n"
1961 ")\n"
1962 "{\n"
1963 "       TexCoord = float2(mul(TexMatrix, gl_MultiTexCoord0));\n"
1964 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
1965 "       ModelViewProjectionPosition = gl_Position;\n"
1966 "}\n"
1967 "#endif\n"
1968 "\n"
1969 "#ifdef FRAGMENT_SHADER\n"
1970 "void main\n"
1971 "(\n"
1972 "float2 TexCoord : TEXCOORD0,\n"
1973 "float3 EyeVector : TEXCOORD1,\n"
1974 "float4 ModelViewProjectionPosition : TEXCOORD2,\n"
1975 "uniform sampler2D Texture_Normal,\n"
1976 "uniform sampler2D Texture_Refraction,\n"
1977 "uniform sampler2D Texture_Reflection,\n"
1978 "uniform float4 DistortScaleRefractReflect,\n"
1979 "uniform float4 ScreenScaleRefractReflect,\n"
1980 "uniform float4 ScreenCenterRefractReflect,\n"
1981 "uniform float4 RefractColor,\n"
1982 "out float4 gl_FragColor : COLOR\n"
1983 ")\n"
1984 "{\n"
1985 "       float2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w);\n"
1986 "       //float2 ScreenTexCoord = (ModelViewProjectionPosition.xy + normalize(float3(tex2D(Texture_Normal, TexCoord)) - float3(0.5)).xy * DistortScaleRefractReflect.xy * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy;\n"
1987 "       float2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy;\n"
1988 "       float2 ScreenTexCoord = SafeScreenTexCoord + float2(normalize(float3(tex2D(Texture_Normal, TexCoord)) - float3(0.5))).xy * DistortScaleRefractReflect.xy;\n"
1989 "       // FIXME temporary hack to detect the case that the reflection\n"
1990 "       // gets blackened at edges due to leaving the area that contains actual\n"
1991 "       // content.\n"
1992 "       // Remove this 'ack once we have a better way to stop this thing from\n"
1993 "       // 'appening.\n"
1994 "       float f = min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord + float2(0.01, 0.01)).rgb) / 0.05);\n"
1995 "       f      *= min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord + float2(0.01, -0.01)).rgb) / 0.05);\n"
1996 "       f      *= min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord + float2(-0.01, 0.01)).rgb) / 0.05);\n"
1997 "       f      *= min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord + float2(-0.01, -0.01)).rgb) / 0.05);\n"
1998 "       ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f);\n"
1999 "       gl_FragColor = tex2D(Texture_Refraction, ScreenTexCoord) * RefractColor;\n"
2000 "}\n"
2001 "#endif\n"
2002 "#else // !MODE_REFRACTION\n"
2003 "\n"
2004 "\n"
2005 "\n"
2006 "\n"
2007 "#ifdef MODE_WATER\n"
2008 "#ifdef VERTEX_SHADER\n"
2009 "\n"
2010 "void main\n"
2011 "(\n"
2012 "float4 gl_Vertex : POSITION,\n"
2013 "uniform float4x4 ModelViewProjectionMatrix,\n"
2014 "float4 gl_MultiTexCoord0 : TEXCOORD0,\n"
2015 "uniform mat4 TexMatrix,\n"
2016 "uniform float3 EyePosition,\n"
2017 "out float4 gl_Position : POSITION,\n"
2018 "out float2 TexCoord : TEXCOORD0,\n"
2019 "out float3 EyeVector : TEXCOORD1,\n"
2020 "out float4 ModelViewProjectionPosition : TEXCOORD2\n"
2021 ")\n"
2022 "{\n"
2023 "       TexCoord = float2(mul(TexMatrix, gl_MultiTexCoord0));\n"
2024 "       float3 EyeVectorModelSpace = EyePosition - gl_Vertex.xyz;\n"
2025 "       EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz);\n"
2026 "       EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz);\n"
2027 "       EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz);\n"
2028 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
2029 "       ModelViewProjectionPosition = gl_Position;\n"
2030 "}\n"
2031 "#endif\n"
2032 "\n"
2033 "#ifdef FRAGMENT_SHADER\n"
2034 "void main\n"
2035 "(\n"
2036 "float2 TexCoord : TEXCOORD0,\n"
2037 "float3 EyeVector : TEXCOORD1,\n"
2038 "float4 ModelViewProjectionPosition : TEXCOORD2,\n"
2039 "uniform sampler2D Texture_Normal,\n"
2040 "uniform sampler2D Texture_Refraction,\n"
2041 "uniform sampler2D Texture_Reflection,\n"
2042 "uniform float4 DistortScaleRefractReflect,\n"
2043 "uniform float4 ScreenScaleRefractReflect,\n"
2044 "uniform float4 ScreenCenterRefractReflect,\n"
2045 "uniform float4 RefractColor,\n"
2046 "uniform float4 ReflectColor,\n"
2047 "uniform float ReflectFactor,\n"
2048 "uniform float ReflectOffset,\n"
2049 "out float4 gl_FragColor : COLOR\n"
2050 ")\n"
2051 "{\n"
2052 "       float4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);\n"
2053 "       //float4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(float3(tex2D(Texture_Normal, TexCoord)) - float3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
2054 "       float4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
2055 "       float4 ScreenTexCoord = SafeScreenTexCoord + float2(normalize(float3(tex2D(Texture_Normal, TexCoord)) - float3(0.5))).xyxy * DistortScaleRefractReflect;\n"
2056 "       // FIXME temporary hack to detect the case that the reflection\n"
2057 "       // gets blackened at edges due to leaving the area that contains actual\n"
2058 "       // content.\n"
2059 "       // Remove this 'ack once we have a better way to stop this thing from\n"
2060 "       // 'appening.\n"
2061 "       float f = min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord.xy + float2(0.01, 0.01)).rgb) / 0.05);\n"
2062 "       f      *= min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord.xy + float2(0.01, -0.01)).rgb) / 0.05);\n"
2063 "       f      *= min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord.xy + float2(-0.01, 0.01)).rgb) / 0.05);\n"
2064 "       f      *= min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord.xy + float2(-0.01, -0.01)).rgb) / 0.05);\n"
2065 "       ScreenTexCoord.xy = mix(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f);\n"
2066 "       f       = min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord.zw + float2(0.01, 0.01)).rgb) / 0.05);\n"
2067 "       f      *= min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord.zw + float2(0.01, -0.01)).rgb) / 0.05);\n"
2068 "       f      *= min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord.zw + float2(-0.01, 0.01)).rgb) / 0.05);\n"
2069 "       f      *= min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord.zw + float2(-0.01, -0.01)).rgb) / 0.05);\n"
2070 "       ScreenTexCoord.zw = mix(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f);\n"
2071 "       float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset;\n"
2072 "       gl_FragColor = mix(tex2D(Texture_Refraction, ScreenTexCoord.xy) * RefractColor, tex2D(Texture_Reflection, ScreenTexCoord.zw) * ReflectColor, Fresnel);\n"
2073 "}\n"
2074 "#endif\n"
2075 "#else // !MODE_WATER\n"
2076 "\n"
2077 "\n"
2078 "\n"
2079 "\n"
2080 "// 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"
2081 "\n"
2082 "// fragment shader specific:\n"
2083 "#ifdef FRAGMENT_SHADER\n"
2084 "\n"
2085 "#ifdef USEFOG\n"
2086 "float FogVertex(float3 EyeVectorModelSpace, float FogPlaneVertexDist, float FogRangeRecip, float FogPlaneViewDist, float FogHeightFade, sampler2D Texture_FogMask)\n"
2087 "{\n"
2088 "       float fogfrac;\n"
2089 "#ifdef USEFOGOUTSIDE\n"
2090 "       fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade);\n"
2091 "#else\n"
2092 "       fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade);\n"
2093 "#endif\n"
2094 "       return float(tex2D(Texture_FogMask, half2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)));\n"
2095 "}\n"
2096 "#endif\n"
2097 "\n"
2098 "#ifdef USEOFFSETMAPPING\n"
2099 "float2 OffsetMapping(float2 TexCoord, float OffsetMapping_Scale, float3 EyeVector, sampler2D Texture_Normal)\n"
2100 "{\n"
2101 "#ifdef USEOFFSETMAPPING_RELIEFMAPPING\n"
2102 "       // 14 sample relief mapping: linear search and then binary search\n"
2103 "       // this basically steps forward a small amount repeatedly until it finds\n"
2104 "       // itself inside solid, then jitters forward and back using decreasing\n"
2105 "       // amounts to find the impact\n"
2106 "       //float3 OffsetVector = float3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * float2(-1, 1), -1);\n"
2107 "       //float3 OffsetVector = float3(normalize(EyeVector.xy) * OffsetMapping_Scale * float2(-1, 1), -1);\n"
2108 "       float3 OffsetVector = float3(normalize(EyeVector).xy * OffsetMapping_Scale * float2(-1, 1), -1);\n"
2109 "       float3 RT = float3(TexCoord, 1);\n"
2110 "       OffsetVector *= 0.1;\n"
2111 "       RT += OffsetVector *  step(tex2D(Texture_Normal, RT.xy).a, RT.z);\n"
2112 "       RT += OffsetVector *  step(tex2D(Texture_Normal, RT.xy).a, RT.z);\n"
2113 "       RT += OffsetVector *  step(tex2D(Texture_Normal, RT.xy).a, RT.z);\n"
2114 "       RT += OffsetVector *  step(tex2D(Texture_Normal, RT.xy).a, RT.z);\n"
2115 "       RT += OffsetVector *  step(tex2D(Texture_Normal, RT.xy).a, RT.z);\n"
2116 "       RT += OffsetVector *  step(tex2D(Texture_Normal, RT.xy).a, RT.z);\n"
2117 "       RT += OffsetVector *  step(tex2D(Texture_Normal, RT.xy).a, RT.z);\n"
2118 "       RT += OffsetVector *  step(tex2D(Texture_Normal, RT.xy).a, RT.z);\n"
2119 "       RT += OffsetVector *  step(tex2D(Texture_Normal, RT.xy).a, RT.z);\n"
2120 "       RT += OffsetVector * (step(tex2D(Texture_Normal, RT.xy).a, RT.z)          - 0.5);\n"
2121 "       RT += OffsetVector * (step(tex2D(Texture_Normal, RT.xy).a, RT.z) * 0.5    - 0.25);\n"
2122 "       RT += OffsetVector * (step(tex2D(Texture_Normal, RT.xy).a, RT.z) * 0.25   - 0.125);\n"
2123 "       RT += OffsetVector * (step(tex2D(Texture_Normal, RT.xy).a, RT.z) * 0.125  - 0.0625);\n"
2124 "       RT += OffsetVector * (step(tex2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125);\n"
2125 "       return RT.xy;\n"
2126 "#else\n"
2127 "       // 3 sample offset mapping (only 3 samples because of ATI Radeon 9500-9800/X300 limits)\n"
2128 "       // this basically moves forward the full distance, and then backs up based\n"
2129 "       // on height of samples\n"
2130 "       //float2 OffsetVector = float2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * float2(-1, 1));\n"
2131 "       //float2 OffsetVector = float2(normalize(EyeVector.xy) * OffsetMapping_Scale * float2(-1, 1));\n"
2132 "       float2 OffsetVector = float2(normalize(EyeVector).xy * OffsetMapping_Scale * float2(-1, 1));\n"
2133 "       TexCoord += OffsetVector;\n"
2134 "       OffsetVector *= 0.333;\n"
2135 "       TexCoord -= OffsetVector * tex2D(Texture_Normal, TexCoord).a;\n"
2136 "       TexCoord -= OffsetVector * tex2D(Texture_Normal, TexCoord).a;\n"
2137 "       TexCoord -= OffsetVector * tex2D(Texture_Normal, TexCoord).a;\n"
2138 "       return TexCoord;\n"
2139 "#endif\n"
2140 "}\n"
2141 "#endif // USEOFFSETMAPPING\n"
2142 "\n"
2143 "#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE)\n"
2144 "#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D)\n"
2145 "# ifndef USESHADOWMAPVSDCT\n"
2146 "float3 GetShadowMapTC2D(float3 dir, float4 ShadowMap_Parameters)\n"
2147 "{\n"
2148 "       float3 adir = abs(dir);\n"
2149 "       float2 tc;\n"
2150 "       float2 offset;\n"
2151 "       float ma;\n"
2152 "       if (adir.x > adir.y)\n"
2153 "       {\n"
2154 "               if (adir.x > adir.z) // X\n"
2155 "               {\n"
2156 "                       ma = adir.x;\n"
2157 "                       tc = dir.zy;\n"
2158 "                       offset = float2(mix(0.5, 1.5, dir.x < 0.0), 0.5);\n"
2159 "               }\n"
2160 "               else // Z\n"
2161 "               {\n"
2162 "                       ma = adir.z;\n"
2163 "                       tc = dir.xy;\n"
2164 "                       offset = float2(mix(0.5, 1.5, dir.z < 0.0), 2.5);\n"
2165 "               }\n"
2166 "       }\n"
2167 "       else\n"
2168 "       {\n"
2169 "               if (adir.y > adir.z) // Y\n"
2170 "               {\n"
2171 "                       ma = adir.y;\n"
2172 "                       tc = dir.xz;\n"
2173 "                       offset = float2(mix(0.5, 1.5, dir.y < 0.0), 1.5);\n"
2174 "               }\n"
2175 "               else // Z\n"
2176 "               {\n"
2177 "                       ma = adir.z;\n"
2178 "                       tc = dir.xy;\n"
2179 "                       offset = float2(mix(0.5, 1.5, dir.z < 0.0), 2.5);\n"
2180 "               }\n"
2181 "       }\n"
2182 "\n"
2183 "       float3 stc = float3(tc * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma;\n"
2184 "       stc.xy += offset * ShadowMap_Parameters.y;\n"
2185 "       stc.z += ShadowMap_Parameters.z;\n"
2186 "       return stc;\n"
2187 "}\n"
2188 "# else\n"
2189 "float3 GetShadowMapTC2D(float3 dir, float4 ShadowMap_Parameters, samplerCUBE Texture_CubeProjection)\n"
2190 "{\n"
2191 "       float3 adir = abs(dir);\n"
2192 "       float4 proj = texCUBE(Texture_CubeProjection, dir);\n"
2193 "       float ma = max(max(adir.x, adir.y), adir.z);\n"
2194 "       float3 stc = float3(mix(dir.xy, dir.zz, proj.xy) * ShadowMap_Parameters.x, ShadowMap_Parameters.w) / ma;\n"
2195 "       stc.xy += proj.zw * ShadowMap_Parameters.y;\n"
2196 "       stc.z += ShadowMap_Parameters.z;\n"
2197 "       return stc;\n"
2198 "}\n"
2199 "# endif\n"
2200 "#endif // defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D)\n"
2201 "\n"
2202 "#ifdef USESHADOWMAPCUBE\n"
2203 "float4 GetShadowMapTCCube(float3 dir, float4 ShadowMap_Parameters)\n"
2204 "{\n"
2205 "    float3 adir = abs(dir);\n"
2206 "    return float4(dir, ShadowMap_Parameters.z + ShadowMap_Parameters.w / max(max(adir.x, adir.y), adir.z));\n"
2207 "}\n"
2208 "#endif\n"
2209 "\n"
2210 "# ifdef USESHADOWMAPRECT\n"
2211 "#ifdef USESHADOWMAPVSDCT\n"
2212 "float ShadowMapCompare(float3 dir, samplerRECT Texture_ShadowMapRect, float4 ShadowMap_Parameters, samplerCUBE Texture_CubeProjection)\n"
2213 "#else\n"
2214 "float ShadowMapCompare(float3 dir, samplerRECT Texture_ShadowMapRect, float4 ShadowMap_Parameters)\n"
2215 "#endif\n"
2216 "{\n"
2217 "#ifdef USESHADOWMAPVSDCT\n"
2218 "       float3 shadowmaptc = GetShadowMapTC2D(dir, ShadowMap_Parameters, Texture_CubeProjection);\n"
2219 "#else\n"
2220 "       float3 shadowmaptc = GetShadowMapTC2D(dir, ShadowMap_Parameters);\n"
2221 "#endif\n"
2222 "       float f;\n"
2223 "#  ifdef USESHADOWSAMPLER\n"
2224 "\n"
2225 "#    ifdef USESHADOWMAPPCF\n"
2226 "#      define texval(x, y) shadow2DRect(Texture_ShadowMapRect, shadowmaptc + float3(x, y, 0.0)).r\n"
2227 "    f = dot(float4(0.25), float4(texval(-0.4, 1.0), texval(-1.0, -0.4), texval(0.4, -1.0), texval(1.0, 0.4)));\n"
2228 "#    else\n"
2229 "    f = shadow2DRect(Texture_ShadowMapRect, shadowmaptc).r;\n"
2230 "#    endif\n"
2231 "\n"
2232 "#  else\n"
2233 "\n"
2234 "#    ifdef USESHADOWMAPPCF\n"
2235 "#      if USESHADOWMAPPCF > 1\n"
2236 "#        define texval(x, y) texRECT(Texture_ShadowMapRect, center + float2(x, y)).r\n"
2237 "    float2 center = shadowmaptc.xy - 0.5, offset = fract(center);\n"
2238 "    float4 row1 = step(shadowmaptc.z, float4(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0), texval( 2.0, -1.0)));\n"
2239 "    float4 row2 = step(shadowmaptc.z, float4(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0), texval( 2.0,  0.0)));\n"
2240 "    float4 row3 = step(shadowmaptc.z, float4(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0), texval( 2.0,  1.0)));\n"
2241 "    float4 row4 = step(shadowmaptc.z, float4(texval(-1.0,  2.0), texval( 0.0,  2.0), texval( 1.0,  2.0), texval( 2.0,  2.0)));\n"
2242 "    float4 cols = row2 + row3 + mix(row1, row4, offset.y);\n"
2243 "    f = dot(mix(cols.xyz, cols.yzw, offset.x), float3(1.0/9.0));\n"
2244 "#      else\n"
2245 "#        define texval(x, y) texRECT(Texture_ShadowMapRect, shadowmaptc.xy + float2(x, y)).r\n"
2246 "    float2 offset = fract(shadowmaptc.xy);\n"
2247 "    float3 row1 = step(shadowmaptc.z, float3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0)));\n"
2248 "    float3 row2 = step(shadowmaptc.z, float3(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0)));\n"
2249 "    float3 row3 = step(shadowmaptc.z, float3(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0)));\n"
2250 "    float3 cols = row2 + mix(row1, row3, offset.y);\n"
2251 "    f = dot(mix(cols.xy, cols.yz, offset.x), float2(0.25));\n"
2252 "#      endif\n"
2253 "#    else\n"
2254 "    f = step(shadowmaptc.z, texRECT(Texture_ShadowMapRect, shadowmaptc.xy).r);\n"
2255 "#    endif\n"
2256 "\n"
2257 "#  endif\n"
2258 "       return f;\n"
2259 "}\n"
2260 "# endif\n"
2261 "\n"
2262 "# ifdef USESHADOWMAP2D\n"
2263 "#ifdef USESHADOWMAPVSDCT\n"
2264 "float ShadowMapCompare(float3 dir, sampler2D Texture_ShadowMap2D, float4 ShadowMap_Parameters, float2 ShadowMap_TextureScale, samplerCUBE Texture_CubeProjection)\n"
2265 "#else\n"
2266 "float ShadowMapCompare(float3 dir, sampler2D Texture_ShadowMap2D, float4 ShadowMap_Parameters, float2 ShadowMap_TextureScale)\n"
2267 "#endif\n"
2268 "{\n"
2269 "#ifdef USESHADOWMAPVSDCT\n"
2270 "       float3 shadowmaptc = GetShadowMapTC2D(dir, ShadowMap_Parameters, Texture_CubeProjection);\n"
2271 "#else\n"
2272 "       float3 shadowmaptc = GetShadowMapTC2D(dir, ShadowMap_Parameters);\n"
2273 "#endif\n"
2274 "    float f;\n"
2275 "\n"
2276 "#  ifdef USESHADOWSAMPLER\n"
2277 "#    ifdef USESHADOWMAPPCF\n"
2278 "#      define texval(x, y) shadow2D(Texture_ShadowMap2D, float3(center + float2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r  \n"
2279 "    float2 center = shadowmaptc.xy*ShadowMap_TextureScale;\n"
2280 "    f = dot(float4(0.25), float4(texval(-0.4, 1.0), texval(-1.0, -0.4), texval(0.4, -1.0), texval(1.0, 0.4)));\n"
2281 "#    else\n"
2282 "    f = shadow2D(Texture_ShadowMap2D, float3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r;\n"
2283 "#    endif\n"
2284 "#  else\n"
2285 "#    ifdef USESHADOWMAPPCF\n"
2286 "#     if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4)\n"
2287 "#      ifdef GL_ARB_texture_gather\n"
2288 "#        define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec(x, y))\n"
2289 "#      else\n"
2290 "#        define texval(x, y) texture4(Texture_ShadowMap2D, center + float2(x,y)*ShadowMap_TextureScale)\n"
2291 "#      endif\n"
2292 "    float2 center = shadowmaptc.xy - 0.5, offset = fract(center);\n"
2293 "    center *= ShadowMap_TextureScale;\n"
2294 "    float4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0));\n"
2295 "    float4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0));\n"
2296 "    float4 group3 = step(shadowmaptc.z, texval(-1.0,  1.0));\n"
2297 "    float4 group4 = step(shadowmaptc.z, texval( 1.0,  1.0));\n"
2298 "    float4 cols = float4(group1.rg, group2.rg) + float4(group3.ab, group4.ab) +\n"
2299 "                mix(float4(group1.ab, group2.ab), float4(group3.rg, group4.rg), offset.y);\n"
2300 "    f = dot(mix(cols.xyz, cols.yzw, offset.x), float3(1.0/9.0));\n"
2301 "#     else\n"
2302 "#      ifdef GL_EXT_gpu_shader4\n"
2303 "#        define texval(x, y) tex2DOffset(Texture_ShadowMap2D, center, ifloat2(x, y)).r\n"
2304 "#      else\n"
2305 "#        define texval(x, y) tex2D(Texture_ShadowMap2D, center + float2(x, y)*ShadowMap_TextureScale).r  \n"
2306 "#      endif\n"
2307 "#      if USESHADOWMAPPCF > 1\n"
2308 "    float2 center = shadowmaptc.xy - 0.5, offset = fract(center);\n"
2309 "    center *= ShadowMap_TextureScale;\n"
2310 "    float4 row1 = step(shadowmaptc.z, float4(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0), texval( 2.0, -1.0)));\n"
2311 "    float4 row2 = step(shadowmaptc.z, float4(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0), texval( 2.0,  0.0)));\n"
2312 "    float4 row3 = step(shadowmaptc.z, float4(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0), texval( 2.0,  1.0)));\n"
2313 "    float4 row4 = step(shadowmaptc.z, float4(texval(-1.0,  2.0), texval( 0.0,  2.0), texval( 1.0,  2.0), texval( 2.0,  2.0)));\n"
2314 "    float4 cols = row2 + row3 + mix(row1, row4, offset.y);\n"
2315 "    f = dot(mix(cols.xyz, cols.yzw, offset.x), float3(1.0/9.0));\n"
2316 "#      else\n"
2317 "    float2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy);\n"
2318 "    float3 row1 = step(shadowmaptc.z, float3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0)));\n"
2319 "    float3 row2 = step(shadowmaptc.z, float3(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0)));\n"
2320 "    float3 row3 = step(shadowmaptc.z, float3(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0)));\n"
2321 "    float3 cols = row2 + mix(row1, row3, offset.y);\n"
2322 "    f = dot(mix(cols.xy, cols.yz, offset.x), float2(0.25));\n"
2323 "#      endif\n"
2324 "#     endif\n"
2325 "#    else\n"
2326 "    f = step(shadowmaptc.z, tex2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r);\n"
2327 "#    endif\n"
2328 "#  endif\n"
2329 "    return f;\n"
2330 "}\n"
2331 "# endif\n"
2332 "\n"
2333 "# ifdef USESHADOWMAPCUBE\n"
2334 "float ShadowMapCompare(float3 dir, samplerCUBE Texture_ShadowMapCube, float4 ShadowMap_Parameters)\n"
2335 "{\n"
2336 "    // apply depth texture cubemap as light filter\n"
2337 "    float4 shadowmaptc = GetShadowMapTCCube(dir, ShadowMap_Parameters);\n"
2338 "    float f;\n"
2339 "#  ifdef USESHADOWSAMPLER\n"
2340 "    f = shadowCube(Texture_ShadowMapCube, shadowmaptc).r;\n"
2341 "#  else\n"
2342 "    f = step(shadowmaptc.w, texCUBE(Texture_ShadowMapCube, shadowmaptc.xyz).r);\n"
2343 "#  endif\n"
2344 "    return f;\n"
2345 "}\n"
2346 "# endif\n"
2347 "#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE)\n"
2348 "#endif // FRAGMENT_SHADER\n"
2349 "\n"
2350 "\n"
2351 "\n"
2352 "\n"
2353 "#ifdef MODE_DEFERREDGEOMETRY\n"
2354 "#ifdef VERTEX_SHADER\n"
2355 "void main\n"
2356 "(\n"
2357 "float4 gl_Vertex : POSITION,\n"
2358 "uniform float4x4 ModelViewProjectionMatrix,\n"
2359 "#ifdef USEVERTEXTEXTUREBLEND\n"
2360 "float4 gl_Color : COLOR0,\n"
2361 "#endif\n"
2362 "float4 gl_MultiTexCoord0 : TEXCOORD0,\n"
2363 "float4 gl_MultiTexCoord1 : TEXCOORD1,\n"
2364 "float4 gl_MultiTexCoord2 : TEXCOORD2,\n"
2365 "float4 gl_MultiTexCoord3 : TEXCOORD3,\n"
2366 "uniform mat4 TexMatrix,\n"
2367 "#ifdef USEVERTEXTEXTUREBLEND\n"
2368 "uniform mat4 BackgroundTexMatrix,\n"
2369 "#endif\n"
2370 "uniform mat4 ModelViewMatrix,\n"
2371 "out float4 gl_Position : POSITION,\n"
2372 "out float4 gl_FrontColor : COLOR,\n"
2373 "out float4 TexCoordBoth : TEXCOORD0,\n"
2374 "out float3 VectorS : TEXCOORD5, // direction of S texcoord (sometimes crudely called tangent)\n"
2375 "out float3 VectorT : TEXCOORD6, // direction of T texcoord (sometimes crudely called binormal)\n"
2376 "out float3 VectorR : TEXCOORD7 // direction of R texcoord (surface normal)\n"
2377 ")\n"
2378 "{\n"
2379 "       TexCoordBoth = mul(TexMatrix, gl_MultiTexCoord0);\n"
2380 "#ifdef USEVERTEXTEXTUREBLEND\n"
2381 "       gl_FrontColor = gl_Color;\n"
2382 "       TexCoordBoth.zw = float2(Backgroundmul(TexMatrix, gl_MultiTexCoord0));\n"
2383 "#endif\n"
2384 "\n"
2385 "       // transform unnormalized eye direction into tangent space\n"
2386 "#ifdef USEOFFSETMAPPING\n"
2387 "       float3 EyeVectorModelSpace = EyePosition - gl_Vertex.xyz;\n"
2388 "       EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz);\n"
2389 "       EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz);\n"
2390 "       EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz);\n"
2391 "#endif\n"
2392 "\n"
2393 "       VectorS = mul(ModelViewMatrix, float4(gl_MultiTexCoord1.xyz, 0)).xyz;\n"
2394 "       VectorT = mul(ModelViewMatrix, float4(gl_MultiTexCoord2.xyz, 0)).xyz;\n"
2395 "       VectorR = mul(ModelViewMatrix, float4(gl_MultiTexCoord3.xyz, 0)).xyz;\n"
2396 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
2397 "}\n"
2398 "#endif // VERTEX_SHADER\n"
2399 "\n"
2400 "#ifdef FRAGMENT_SHADER\n"
2401 "void main\n"
2402 "(\n"
2403 "float4 TexCoordBoth : TEXCOORD0,\n"
2404 "float3 EyeVector : TEXCOORD2,\n"
2405 "float3 VectorS : TEXCOORD5, // direction of S texcoord (sometimes crudely called tangent)\n"
2406 "float3 VectorT : TEXCOORD6, // direction of T texcoord (sometimes crudely called binormal)\n"
2407 "float3 VectorR : TEXCOORD7, // direction of R texcoord (surface normal)\n"
2408 "uniform sampler2D Texture_Normal,\n"
2409 "#ifdef USEALPHAKILL\n"
2410 "uniform sampler2D Texture_Color,\n"
2411 "#endif\n"
2412 "#ifdef USEVERTEXTEXTUREBLEND\n"
2413 "uniform sampler2D Texture_SecondaryNormal,\n"
2414 "#endif\n"
2415 "#ifdef USEOFFSETMAPPING\n"
2416 "uniform float OffsetMapping_Scale,\n"
2417 "#endif\n"
2418 "uniform half SpecularPower,\n"
2419 "out float4 gl_FragColor : COLOR\n"
2420 ")\n"
2421 "{\n"
2422 "       float2 TexCoord = TexCoordBoth.xy;\n"
2423 "#ifdef USEOFFSETMAPPING\n"
2424 "       // apply offsetmapping\n"
2425 "       float2 TexCoordOffset = OffsetMapping(TexCoord, OffsetMapping_Scale, EyeVector, Texture_Normal);\n"
2426 "#define TexCoord TexCoordOffset\n"
2427 "#endif\n"
2428 "\n"
2429 "#ifdef USEALPHAKILL\n"
2430 "       if (tex2D(Texture_Color, TexCoord).a < 0.5)\n"
2431 "               discard;\n"
2432 "#endif\n"
2433 "\n"
2434 "#ifdef USEVERTEXTEXTUREBLEND\n"
2435 "       float alpha = tex2D(Texture_Color, TexCoord).a;\n"
2436 "       float terrainblend = clamp(float(gl_FrontColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0));\n"
2437 "       //float terrainblend = min(float(gl_FrontColor.a) * alpha * 2.0, float(1.0));\n"
2438 "       //float terrainblend = float(gl_FrontColor.a) * alpha > 0.5;\n"
2439 "#endif\n"
2440 "\n"
2441 "#ifdef USEVERTEXTEXTUREBLEND\n"
2442 "       float3 surfacenormal = mix(float3(tex2D(Texture_SecondaryNormal, TexCoord2)), float3(tex2D(Texture_Normal, TexCoord)), terrainblend) - float3(0.5, 0.5, 0.5);\n"
2443 "#else\n"
2444 "       float3 surfacenormal = float3(tex2D(Texture_Normal, TexCoord)) - float3(0.5, 0.5, 0.5);\n"
2445 "#endif\n"
2446 "\n"
2447 "       gl_FragColor = float4(normalize(surfacenormal.x * VectorS + surfacenormal.y * VectorT + surfacenormal.z * VectorR) * 0.5 + float3(0.5, 0.5, 0.5), 1);\n"
2448 "}\n"
2449 "#endif // FRAGMENT_SHADER\n"
2450 "#else // !MODE_DEFERREDGEOMETRY\n"
2451 "\n"
2452 "\n"
2453 "\n"
2454 "\n"
2455 "#ifdef MODE_DEFERREDLIGHTSOURCE\n"
2456 "#ifdef VERTEX_SHADER\n"
2457 "void main\n"
2458 "(\n"
2459 "float4 gl_Vertex : POSITION,\n"
2460 "uniform float4x4 ModelViewProjectionMatrix,\n"
2461 "uniform mat4 ModelViewMatrix,\n"
2462 "out float4 gl_Position : POSITION,\n"
2463 "out float4 ModelViewPosition : TEXCOORD0\n"
2464 ")\n"
2465 "{\n"
2466 "       ModelViewPosition = mul(ModelViewMatrix, gl_Vertex);\n"
2467 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
2468 "}\n"
2469 "#endif // VERTEX_SHADER\n"
2470 "\n"
2471 "#ifdef FRAGMENT_SHADER\n"
2472 "void main\n"
2473 "(\n"
2474 "float2 Pixel : WPOS,\n"
2475 "float4 ModelViewPosition : TEXCOORD0,\n"
2476 "uniform mat4 ViewToLight,\n"
2477 "uniform float2 ScreenToDepth, // ScreenToDepth = float2(Far / (Far - Near), Far * Near / (Near - Far));\n"
2478 "uniform float3 LightPosition,\n"
2479 "uniform half3 DeferredColor_Ambient,\n"
2480 "uniform half3 DeferredColor_Diffuse,\n"
2481 "#ifdef USESPECULAR\n"
2482 "uniform half3 DeferredColor_Specular,\n"
2483 "uniform half SpecularPower,\n"
2484 "#endif\n"
2485 "uniform sampler2D Texture_Attenuation,\n"
2486 "uniform samplerRECT Texture_ScreenDepth,\n"
2487 "uniform samplerRECT Texture_ScreenNormalMap,\n"
2488 "\n"
2489 "#ifdef USESHADOWMAPRECT\n"
2490 "# ifdef USESHADOWSAMPLER\n"
2491 "uniform samplerRECTShadow Texture_ShadowMapRect,\n"
2492 "# else\n"
2493 "uniform samplerRECT Texture_ShadowMapRect,\n"
2494 "# endif\n"
2495 "#endif\n"
2496 "\n"
2497 "#ifdef USESHADOWMAP2D\n"
2498 "# ifdef USESHADOWSAMPLER\n"
2499 "uniform sampler2DShadow Texture_ShadowMap2D,\n"
2500 "# else\n"
2501 "uniform sampler2D Texture_ShadowMap2D,\n"
2502 "# endif\n"
2503 "#endif\n"
2504 "\n"
2505 "#ifdef USESHADOWMAPVSDCT\n"
2506 "uniform samplerCUBE Texture_CubeProjection,\n"
2507 "#endif\n"
2508 "\n"
2509 "#ifdef USESHADOWMAPCUBE\n"
2510 "# ifdef USESHADOWSAMPLER\n"
2511 "uniform samplerCUBEShadow Texture_ShadowMapCube,\n"
2512 "# else\n"
2513 "uniform samplerCUBE Texture_ShadowMapCube,\n"
2514 "# endif\n"
2515 "#endif\n"
2516 "\n"
2517 "#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE)\n"
2518 "uniform float2 ShadowMap_TextureScale,\n"
2519 "uniform float4 ShadowMap_Parameters,\n"
2520 "#endif\n"
2521 "\n"
2522 "out float4 gl_FragData0 : COLOR0,\n"
2523 "out float4 gl_FragData1 : COLOR1\n"
2524 ")\n"
2525 "{\n"
2526 "       // calculate viewspace pixel position\n"
2527 "       float3 position;\n"
2528 "       position.z = ScreenToDepth.y / (texRECT(Texture_ScreenDepth, Pixel).r + ScreenToDepth.x);\n"
2529 "       position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z);\n"
2530 "       // decode viewspace pixel normal\n"
2531 "       half4 normalmap = texRECT(Texture_ScreenNormalMap, Pixel);\n"
2532 "       half3 surfacenormal = normalize(normalmap.rgb - half3(0.5,0.5,0.5));\n"
2533 "       // surfacenormal = pixel normal in viewspace\n"
2534 "       // LightVector = pixel to light in viewspace\n"
2535 "       // CubeVector = position in lightspace\n"
2536 "       // eyevector = pixel to view in viewspace\n"
2537 "       float3 CubeVector = float3(mul(ViewToLight, float4(position,1)));\n"
2538 "       half fade = half(tex2D(Texture_Attenuation, float2(length(CubeVector), 0.0)));\n"
2539 "#ifdef USEDIFFUSE\n"
2540 "       // calculate diffuse shading\n"
2541 "       half3 lightnormal = half3(normalize(LightPosition - position));\n"
2542 "       half diffuse = half(max(float(dot(surfacenormal, lightnormal)), 0.0));\n"
2543 "#endif\n"
2544 "#ifdef USESPECULAR\n"
2545 "       // calculate directional shading\n"
2546 "       float3 eyevector = position * -1.0;\n"
2547 "#  ifdef USEEXACTSPECULARMATH\n"
2548 "       half specular = pow(half(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), SpecularPower);\n"
2549 "#  else\n"
2550 "       half3 specularnormal = normalize(lightnormal + half3(normalize(eyevector)));\n"
2551 "       half specular = pow(half(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower);\n"
2552 "#  endif\n"
2553 "#endif\n"
2554 "\n"
2555 "#if defined(USESHADOWMAP2D) || defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE)\n"
2556 "       fade *= ShadowMapCompare(CubeVector,\n"
2557 "# if defined(USESHADOWMAP2D)\n"
2558 "Texture_ShadowMap2D, ShadowMap_Parameters, ShadowMap_TextureScale\n"
2559 "# endif\n"
2560 "# if defined(USESHADOWMAPRECT)\n"
2561 "Texture_ShadowMapRect, ShadowMap_Parameters\n"
2562 "# endif\n"
2563 "# if defined(USESHADOWMAPCUBE)\n"
2564 "Texture_ShadowMapCube, ShadowMap_Parameters\n"
2565 "# endif\n"
2566 "\n"
2567 "#ifdef USESHADOWMAPVSDCT\n"
2568 ", Texture_CubeProjection\n"
2569 "#endif\n"
2570 "       );\n"
2571 "#endif\n"
2572 "\n"
2573 "#ifdef USEDIFFUSE\n"
2574 "       gl_FragData0 = float4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0);\n"
2575 "#else\n"
2576 "       gl_FragData0 = float4(DeferredColor_Ambient * fade, 1.0);\n"
2577 "#endif\n"
2578 "#ifdef USESPECULAR\n"
2579 "       gl_FragData1 = float4(DeferredColor_Specular * (specular * fade), 1.0);\n"
2580 "#else\n"
2581 "       gl_FragData1 = float4(0.0, 0.0, 0.0, 1.0);\n"
2582 "#endif\n"
2583 "\n"
2584 "# ifdef USECUBEFILTER\n"
2585 "       float3 cubecolor = texCUBE(Texture_Cube, CubeVector).rgb;\n"
2586 "       gl_FragData0 *= cubecolor;\n"
2587 "       gl_FragData1 *= cubecolor;\n"
2588 "# endif\n"
2589 "}\n"
2590 "#endif // FRAGMENT_SHADER\n"
2591 "#else // !MODE_DEFERREDLIGHTSOURCE\n"
2592 "\n"
2593 "\n"
2594 "\n"
2595 "\n"
2596 "#ifdef VERTEX_SHADER\n"
2597 "void main\n"
2598 "(\n"
2599 "float4 gl_Vertex : POSITION,\n"
2600 "uniform float4x4 ModelViewProjectionMatrix,\n"
2601 "#if defined(USEVERTEXTEXTUREBLEND) || defined(MODE_VERTEXCOLOR)\n"
2602 "float4 gl_Color : COLOR0,\n"
2603 "#endif\n"
2604 "float4 gl_MultiTexCoord0 : TEXCOORD0,\n"
2605 "float4 gl_MultiTexCoord1 : TEXCOORD1,\n"
2606 "float4 gl_MultiTexCoord2 : TEXCOORD2,\n"
2607 "float4 gl_MultiTexCoord3 : TEXCOORD3,\n"
2608 "float4 gl_MultiTexCoord4 : TEXCOORD4,\n"
2609 "\n"
2610 "uniform float3 EyePosition,\n"
2611 "uniform mat4 TexMatrix,\n"
2612 "#ifdef USEVERTEXTEXTUREBLEND\n"
2613 "uniform mat4 BackgroundTexMatrix,\n"
2614 "#endif\n"
2615 "#ifdef MODE_LIGHTSOURCE\n"
2616 "uniform mat4 ModelToLight,\n"
2617 "#endif\n"
2618 "#ifdef MODE_LIGHTSOURCE\n"
2619 "uniform float3 LightPosition,\n"
2620 "#endif\n"
2621 "#ifdef MODE_LIGHTDIRECTION\n"
2622 "uniform float3 LightDir,\n"
2623 "#endif\n"
2624 "uniform float4 FogPlane,\n"
2625 "#ifdef MODE_DEFERREDLIGHTSOURCE\n"
2626 "uniform float3 LightPosition,\n"
2627 "#endif\n"
2628 "\n"
2629 "out float4 gl_FrontColor : COLOR,\n"
2630 "out float4 TexCoordBoth : TEXCOORD0,\n"
2631 "#ifdef USELIGHTMAP\n"
2632 "out float2 TexCoordLightmap : TEXCOORD1,\n"
2633 "#endif\n"
2634 "#ifdef USEEYEVECTOR\n"
2635 "out float3 EyeVector : TEXCOORD2,\n"
2636 "#endif\n"
2637 "#ifdef USEREFLECTION\n"
2638 "out float4 ModelViewProjectionPosition : TEXCOORD3,\n"
2639 "#endif\n"
2640 "#ifdef USEFOG\n"
2641 "out float4 EyeVectorModelSpaceFogPlaneVertexDist : TEXCOORD4,\n"
2642 "#endif\n"
2643 "#if defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)\n"
2644 "out float3 LightVector : TEXCOORD5,\n"
2645 "#endif\n"
2646 "#ifdef MODE_LIGHTSOURCE\n"
2647 "out float3 CubeVector : TEXCOORD3,\n"
2648 "#endif\n"
2649 "#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY)\n"
2650 "out float3 VectorS : TEXCOORD5, // direction of S texcoord (sometimes crudely called tangent)\n"
2651 "out float3 VectorT : TEXCOORD6, // direction of T texcoord (sometimes crudely called binormal)\n"
2652 "out float3 VectorR : TEXCOORD7, // direction of R texcoord (surface normal)\n"
2653 "#endif\n"
2654 "out float4 gl_Position : POSITION\n"
2655 ")\n"
2656 "{\n"
2657 "#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND)\n"
2658 "       gl_FrontColor = gl_Color;\n"
2659 "#endif\n"
2660 "       // copy the surface texcoord\n"
2661 "       TexCoordBoth = mul(TexMatrix, gl_MultiTexCoord0);\n"
2662 "#ifdef USEVERTEXTEXTUREBLEND\n"
2663 "       TexCoordBoth.zw = mul(BackgroundTexMatrix, gl_MultiTexCoord0).xy;\n"
2664 "#endif\n"
2665 "#ifdef USELIGHTMAP\n"
2666 "       TexCoordLightmap = float2(gl_MultiTexCoord4);\n"
2667 "#endif\n"
2668 "\n"
2669 "#ifdef MODE_LIGHTSOURCE\n"
2670 "       // transform vertex position into light attenuation/cubemap space\n"
2671 "       // (-1 to +1 across the light box)\n"
2672 "       CubeVector = float3(mul(ModelToLight, gl_Vertex));\n"
2673 "\n"
2674 "# ifdef USEDIFFUSE\n"
2675 "       // transform unnormalized light direction into tangent space\n"
2676 "       // (we use unnormalized to ensure that it interpolates correctly and then\n"
2677 "       //  normalize it per pixel)\n"
2678 "       float3 lightminusvertex = LightPosition - gl_Vertex.xyz;\n"
2679 "       LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz);\n"
2680 "       LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz);\n"
2681 "       LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz);\n"
2682 "# endif\n"
2683 "#endif\n"
2684 "\n"
2685 "#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE)\n"
2686 "       LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz);\n"
2687 "       LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz);\n"
2688 "       LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz);\n"
2689 "#endif\n"
2690 "\n"
2691 "       // transform unnormalized eye direction into tangent space\n"
2692 "#ifdef USEEYEVECTOR\n"
2693 "       float3 EyeVectorModelSpace = EyePosition - gl_Vertex.xyz;\n"
2694 "       EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz);\n"
2695 "       EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz);\n"
2696 "       EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz);\n"
2697 "#endif\n"
2698 "\n"
2699 "#ifdef USEFOG\n"
2700 "       EyeVectorModelSpaceFogPlaneVertexDist.xyz = EyePosition - gl_Vertex.xyz;\n"
2701 "       EyeVectorModelSpaceFogPlaneVertexDist.w = dot(FogPlane, gl_Vertex);\n"
2702 "#endif\n"
2703 "\n"
2704 "#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
2705 "       VectorS = gl_MultiTexCoord1.xyz;\n"
2706 "       VectorT = gl_MultiTexCoord2.xyz;\n"
2707 "       VectorR = gl_MultiTexCoord3.xyz;\n"
2708 "#endif\n"
2709 "\n"
2710 "       // transform vertex to camera space, using ftransform to match non-VS rendering\n"
2711 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
2712 "\n"
2713 "#ifdef USEREFLECTION\n"
2714 "       ModelViewProjectionPosition = gl_Position;\n"
2715 "#endif\n"
2716 "}\n"
2717 "#endif // VERTEX_SHADER\n"
2718 "\n"
2719 "\n"
2720 "\n"
2721 "\n"
2722 "#ifdef FRAGMENT_SHADER\n"
2723 "void main\n"
2724 "(\n"
2725 "#ifdef USEDEFERREDLIGHTMAP\n"
2726 "float2 Pixel : WPOS,\n"
2727 "#endif\n"
2728 "float4 gl_FrontColor : COLOR,\n"
2729 "float4 TexCoordBoth : TEXCOORD0,\n"
2730 "#ifdef USELIGHTMAP\n"
2731 "float2 TexCoordLightmap : TEXCOORD1,\n"
2732 "#endif\n"
2733 "#ifdef USEEYEVECTOR\n"
2734 "float3 EyeVector : TEXCOORD2,\n"
2735 "#endif\n"
2736 "#ifdef USEREFLECTION\n"
2737 "float4 ModelViewProjectionPosition : TEXCOORD3,\n"
2738 "#endif\n"
2739 "#ifdef USEFOG\n"
2740 "float4 EyeVectorModelSpaceFogPlaneVertexDist : TEXCOORD4,\n"
2741 "#endif\n"
2742 "#if defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)\n"
2743 "float3 LightVector : TEXCOORD5,\n"
2744 "#endif\n"
2745 "#ifdef MODE_LIGHTSOURCE\n"
2746 "float3 CubeVector : TEXCOORD3,\n"
2747 "#endif\n"
2748 "#ifdef MODE_DEFERREDLIGHTSOURCE\n"
2749 "float4 ModelViewPosition : TEXCOORD0,\n"
2750 "#endif\n"
2751 "#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY)\n"
2752 "float3 VectorS : TEXCOORD5, // direction of S texcoord (sometimes crudely called tangent)\n"
2753 "float3 VectorT : TEXCOORD6, // direction of T texcoord (sometimes crudely called binormal)\n"
2754 "float3 VectorR : TEXCOORD7, // direction of R texcoord (surface normal)\n"
2755 "#endif\n"
2756 "\n"
2757 "uniform sampler2D Texture_Normal,\n"
2758 "uniform sampler2D Texture_Color,\n"
2759 "#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP)\n"
2760 "uniform sampler2D Texture_Gloss,\n"
2761 "#endif\n"
2762 "#ifdef USEGLOW\n"
2763 "uniform sampler2D Texture_Glow,\n"
2764 "#endif\n"
2765 "#ifdef USEVERTEXTEXTUREBLEND\n"
2766 "uniform sampler2D Texture_SecondaryNormal,\n"
2767 "uniform sampler2D Texture_SecondaryColor,\n"
2768 "#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP)\n"
2769 "uniform sampler2D Texture_SecondaryGloss,\n"
2770 "#endif\n"
2771 "#ifdef USEGLOW\n"
2772 "uniform sampler2D Texture_SecondaryGlow,\n"
2773 "#endif\n"
2774 "#endif\n"
2775 "#ifdef USECOLORMAPPING\n"
2776 "uniform sampler2D Texture_Pants,\n"
2777 "uniform sampler2D Texture_Shirt,\n"
2778 "#endif\n"
2779 "#ifdef USEFOG\n"
2780 "uniform sampler2D Texture_FogMask,\n"
2781 "#endif\n"
2782 "#ifdef USELIGHTMAP\n"
2783 "uniform sampler2D Texture_Lightmap,\n"
2784 "#endif\n"
2785 "#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE)\n"
2786 "uniform sampler2D Texture_Deluxemap,\n"
2787 "#endif\n"
2788 "#ifdef USEREFLECTION\n"
2789 "uniform sampler2D Texture_Reflection,\n"
2790 "#endif\n"
2791 "\n"
2792 "//#ifdef MODE_DEFERREDLIGHTSOURCE\n"
2793 "uniform samplerRECT Texture_ScreenDepth,\n"
2794 "uniform samplerRECT Texture_ScreenNormalMap,\n"
2795 "//#endif\n"
2796 "#ifdef USEDEFERREDLIGHTMAP\n"
2797 "uniform samplerRECT Texture_ScreenDiffuse,\n"
2798 "uniform samplerRECT Texture_ScreenSpecular,\n"
2799 "#endif\n"
2800 "\n"
2801 "#ifdef USECOLORMAPPING\n"
2802 "uniform half3 Color_Pants,\n"
2803 "uniform half3 Color_Shirt,\n"
2804 "#endif\n"
2805 "#ifdef USEFOG\n"
2806 "uniform float3 FogColor,\n"
2807 "uniform float FogRangeRecip,\n"
2808 "uniform float FogPlaneViewDist,\n"
2809 "uniform float FogHeightFade,\n"
2810 "#endif\n"
2811 "\n"
2812 "#ifdef USEOFFSETMAPPING\n"
2813 "uniform float OffsetMapping_Scale,\n"
2814 "#endif\n"
2815 "\n"
2816 "#ifdef USEDEFERREDLIGHTMAP\n"
2817 "uniform half3 DeferredMod_Diffuse,\n"
2818 "uniform half3 DeferredMod_Specular,\n"
2819 "#endif\n"
2820 "uniform half3 Color_Ambient,\n"
2821 "uniform half3 Color_Diffuse,\n"
2822 "uniform half3 Color_Specular,\n"
2823 "uniform half SpecularPower,\n"
2824 "#ifdef USEGLOW\n"
2825 "uniform half3 Color_Glow,\n"
2826 "#endif\n"
2827 "uniform half Alpha,\n"
2828 "#ifdef USEREFLECTION\n"
2829 "uniform float4 DistortScaleRefractReflect,\n"
2830 "uniform float4 ScreenScaleRefractReflect,\n"
2831 "uniform float4 ScreenCenterRefractReflect,\n"
2832 "uniform half4 ReflectColor,\n"
2833 "#endif\n"
2834 "#ifdef MODE_LIGHTDIRECTION\n"
2835 "uniform half3 LightColor,\n"
2836 "#endif\n"
2837 "#ifdef MODE_LIGHTSOURCE\n"
2838 "uniform half3 LightColor,\n"
2839 "#endif\n"
2840 "\n"
2841 "#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE)\n"
2842 "uniform sampler2D Texture_Attenuation,\n"
2843 "uniform samplerCUBE Texture_Cube,\n"
2844 "\n"
2845 "#ifdef USESHADOWMAPRECT\n"
2846 "# ifdef USESHADOWSAMPLER\n"
2847 "uniform samplerRECTShadow Texture_ShadowMapRect,\n"
2848 "# else\n"
2849 "uniform samplerRECT Texture_ShadowMapRect,\n"
2850 "# endif\n"
2851 "#endif\n"
2852 "\n"
2853 "#ifdef USESHADOWMAP2D\n"
2854 "# ifdef USESHADOWSAMPLER\n"
2855 "uniform sampler2DShadow Texture_ShadowMap2D,\n"
2856 "# else\n"
2857 "uniform sampler2D Texture_ShadowMap2D,\n"
2858 "# endif\n"
2859 "#endif\n"
2860 "\n"
2861 "#ifdef USESHADOWMAPVSDCT\n"
2862 "uniform samplerCUBE Texture_CubeProjection,\n"
2863 "#endif\n"
2864 "\n"
2865 "#ifdef USESHADOWMAPCUBE\n"
2866 "# ifdef USESHADOWSAMPLER\n"
2867 "uniform samplerCUBEShadow Texture_ShadowMapCube,\n"
2868 "# else\n"
2869 "uniform samplerCUBE Texture_ShadowMapCube,\n"
2870 "# endif\n"
2871 "#endif\n"
2872 "\n"
2873 "#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAP2D) || defined(USESHADOWMAPCUBE)\n"
2874 "uniform float2 ShadowMap_TextureScale,\n"
2875 "uniform float4 ShadowMap_Parameters,\n"
2876 "#endif\n"
2877 "#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE)\n"
2878 "\n"
2879 "out float4 gl_FragColor : COLOR\n"
2880 ")\n"
2881 "{\n"
2882 "       float2 TexCoord = TexCoordBoth.xy;\n"
2883 "#ifdef USEVERTEXTEXTUREBLEND\n"
2884 "       float2 TexCoord2 = TexCoordBoth.zw;\n"
2885 "#endif\n"
2886 "#ifdef USEOFFSETMAPPING\n"
2887 "       // apply offsetmapping\n"
2888 "       float2 TexCoordOffset = OffsetMapping(TexCoord, OffsetMapping_Scale, EyeVector, Texture_Normal);\n"
2889 "#define TexCoord TexCoordOffset\n"
2890 "#endif\n"
2891 "\n"
2892 "       // combine the diffuse textures (base, pants, shirt)\n"
2893 "       half4 color = half4(tex2D(Texture_Color, TexCoord));\n"
2894 "#ifdef USEALPHAKILL\n"
2895 "       if (color.a < 0.5)\n"
2896 "               discard;\n"
2897 "#endif\n"
2898 "       color.a *= Alpha;\n"
2899 "#ifdef USECOLORMAPPING\n"
2900 "       color.rgb += half3(tex2D(Texture_Pants, TexCoord)) * Color_Pants + half3(tex2D(Texture_Shirt, TexCoord)) * Color_Shirt;\n"
2901 "#endif\n"
2902 "#ifdef USEVERTEXTEXTUREBLEND\n"
2903 "       float terrainblend = clamp(half(gl_FrontColor.a) * color.a * 2.0 - 0.5, half(0.0), half(1.0));\n"
2904 "       //half terrainblend = min(half(gl_FrontColor.a) * color.a * 2.0, half(1.0));\n"
2905 "       //half terrainblend = half(gl_FrontColor.a) * color.a > 0.5;\n"
2906 "       color.rgb = half3(mix(float3(tex2D(Texture_SecondaryColor, TexCoord2)), float3(color.rgb), terrainblend));\n"
2907 "       color.a = 1.0;\n"
2908 "       //color = mix(half4(1, 0, 0, 1), color, terrainblend);\n"
2909 "#endif\n"
2910 "\n"
2911 "       // get the surface normal\n"
2912 "#ifdef USEVERTEXTEXTUREBLEND\n"
2913 "       half3 surfacenormal = normalize(half3(mix(float3(tex2D(Texture_SecondaryNormal, TexCoord2)), float3(tex2D(Texture_Normal, TexCoord)), terrainblend)) - half3(0.5, 0.5, 0.5));\n"
2914 "#else\n"
2915 "       half3 surfacenormal = normalize(half3(tex2D(Texture_Normal, TexCoord)) - half3(0.5, 0.5, 0.5));\n"
2916 "#endif\n"
2917 "\n"
2918 "       // get the material colors\n"
2919 "       half3 diffusetex = color.rgb;\n"
2920 "#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP)\n"
2921 "# ifdef USEVERTEXTEXTUREBLEND\n"
2922 "       half3 glosstex = half3(mix(float3(tex2D(Texture_SecondaryGloss, TexCoord2)), float3(tex2D(Texture_Gloss, TexCoord)), terrainblend));\n"
2923 "# else\n"
2924 "       half3 glosstex = half3(tex2D(Texture_Gloss, TexCoord));\n"
2925 "# endif\n"
2926 "#endif\n"
2927 "\n"
2928 "\n"
2929 "\n"
2930 "\n"
2931 "#ifdef MODE_LIGHTSOURCE\n"
2932 "       // light source\n"
2933 "       half3 lightnormal = half3(normalize(LightVector));\n"
2934 "       half diffuse = half(max(float(dot(surfacenormal, lightnormal)), 0.0));\n"
2935 "       color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse);\n"
2936 "#ifdef USESPECULAR\n"
2937 "#ifdef USEEXACTSPECULARMATH\n"
2938 "       half specular = pow(half(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower);\n"
2939 "#else\n"
2940 "       half3 specularnormal = normalize(lightnormal + half3(normalize(EyeVector)));\n"
2941 "       half specular = pow(half(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower);\n"
2942 "#endif\n"
2943 "       color.rgb += glosstex * (specular * Color_Specular);\n"
2944 "#endif\n"
2945 "       color.rgb *= LightColor;\n"
2946 "       color.rgb *= half(tex2D(Texture_Attenuation, float2(length(CubeVector), 0.0)));\n"
2947 "#if defined(USESHADOWMAPRECT) || defined(USESHADOWMAPCUBE) || defined(USESHADOWMAP2D)\n"
2948 "       color.rgb *= ShadowMapCompare(CubeVector,\n"
2949 "# if defined(USESHADOWMAP2D)\n"
2950 "Texture_ShadowMap2D, ShadowMap_Parameters, ShadowMap_TextureScale\n"
2951 "# endif\n"
2952 "# if defined(USESHADOWMAPRECT)\n"
2953 "Texture_ShadowMapRect, ShadowMap_Parameters\n"
2954 "# endif\n"
2955 "# if defined(USESHADOWMAPCUBE)\n"
2956 "Texture_ShadowMapCube, ShadowMap_Parameters\n"
2957 "# endif\n"
2958 "\n"
2959 "#ifdef USESHADOWMAPVSDCT\n"
2960 ", Texture_CubeProjection\n"
2961 "#endif\n"
2962 "       );\n"
2963 "\n"
2964 "#endif\n"
2965 "# ifdef USECUBEFILTER\n"
2966 "       color.rgb *= half3(texCUBE(Texture_Cube, CubeVector));\n"
2967 "# endif\n"
2968 "#endif // MODE_LIGHTSOURCE\n"
2969 "\n"
2970 "\n"
2971 "\n"
2972 "\n"
2973 "#ifdef MODE_LIGHTDIRECTION\n"
2974 "#define SHADING\n"
2975 "       half3 lightnormal = half3(normalize(LightVector));\n"
2976 "#define lightcolor LightColor\n"
2977 "#endif // MODE_LIGHTDIRECTION\n"
2978 "#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
2979 "#define SHADING\n"
2980 "       // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe)\n"
2981 "       half3 lightnormal_modelspace = half3(tex2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + half3(-1.0, -1.0, -1.0);\n"
2982 "       half3 lightcolor = half3(tex2D(Texture_Lightmap, TexCoordLightmap));\n"
2983 "       // convert modelspace light vector to tangentspace\n"
2984 "       half3 lightnormal;\n"
2985 "       lightnormal.x = dot(lightnormal_modelspace, half3(VectorS));\n"
2986 "       lightnormal.y = dot(lightnormal_modelspace, half3(VectorT));\n"
2987 "       lightnormal.z = dot(lightnormal_modelspace, half3(VectorR));\n"
2988 "       // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division)\n"
2989 "       // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar\n"
2990 "       // is used (the lightmap and deluxemap coords correspond to virtually random coordinates\n"
2991 "       // on that luxel, and NOT to its center, because recursive triangle subdivision is used\n"
2992 "       // to map the luxels to coordinates on the draw surfaces), which also causes\n"
2993 "       // deluxemaps to be wrong because light contributions from the wrong side of the surface\n"
2994 "       // are added up. To prevent divisions by zero or strong exaggerations, a max()\n"
2995 "       // nudge is done here at expense of some additional fps. This is ONLY needed for\n"
2996 "       // deluxemaps, tangentspace deluxemap avoid this problem by design.\n"
2997 "       lightcolor *= 1.0 / max(0.25, lightnormal.z);\n"
2998 "#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
2999 "#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n"
3000 "#define SHADING\n"
3001 "       // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light)\n"
3002 "       half3 lightnormal = half3(tex2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + half3(-1.0, -1.0, -1.0);\n"
3003 "       half3 lightcolor = half3(tex2D(Texture_Lightmap, TexCoordLightmap));\n"
3004 "#endif\n"
3005 "\n"
3006 "\n"
3007 "\n"
3008 "\n"
3009 "#ifdef MODE_LIGHTMAP\n"
3010 "       color.rgb = diffusetex * (Color_Ambient + half3(tex2D(Texture_Lightmap, TexCoordLightmap)) * Color_Diffuse);\n"
3011 "#endif // MODE_LIGHTMAP\n"
3012 "#ifdef MODE_VERTEXCOLOR\n"
3013 "       color.rgb = diffusetex * (Color_Ambient + half3(gl_FrontColor.rgb) * Color_Diffuse);\n"
3014 "#endif // MODE_VERTEXCOLOR\n"
3015 "#ifdef MODE_FLATCOLOR\n"
3016 "       color.rgb = diffusetex * Color_Ambient;\n"
3017 "#endif // MODE_FLATCOLOR\n"
3018 "\n"
3019 "\n"
3020 "\n"
3021 "\n"
3022 "#ifdef SHADING\n"
3023 "# ifdef USEDIFFUSE\n"
3024 "       half diffuse = half(max(float(dot(surfacenormal, lightnormal)), 0.0));\n"
3025 "#  ifdef USESPECULAR\n"
3026 "#   ifdef USEEXACTSPECULARMATH\n"
3027 "       half specular = pow(half(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower);\n"
3028 "#   else\n"
3029 "       half3 specularnormal = normalize(lightnormal + half3(normalize(EyeVector)));\n"
3030 "       half specular = pow(half(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower);\n"
3031 "#   endif\n"
3032 "       color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex * Color_Specular * specular) * lightcolor;\n"
3033 "#  else\n"
3034 "       color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor);\n"
3035 "#  endif\n"
3036 "# else\n"
3037 "       color.rgb = diffusetex * Color_Ambient;\n"
3038 "# endif\n"
3039 "#endif\n"
3040 "\n"
3041 "#ifdef USEDEFERREDLIGHTMAP\n"
3042 "       color.rgb += diffusetex * half3(texRECT(Texture_ScreenDiffuse, Pixel)) * DeferredMod_Diffuse;\n"
3043 "       color.rgb += glosstex * half3(texRECT(Texture_ScreenSpecular, Pixel)) * DeferredMod_Specular;\n"
3044 "       color.rgb = half3(texRECT(Texture_ScreenDepth, Pixel));\n"
3045 "#endif\n"
3046 "\n"
3047 "#ifdef USEGLOW\n"
3048 "#ifdef USEVERTEXTEXTUREBLEND\n"
3049 "       color.rgb += mix(half3(tex2D(Texture_SecondaryGlow, TexCoord2)), half3(tex2D(Texture_Glow, TexCoord)), terrainblend) * Color_Glow;\n"
3050 "#else\n"
3051 "       color.rgb += half3(tex2D(Texture_Glow, TexCoord)) * Color_Glow;\n"
3052 "#endif\n"
3053 "#endif\n"
3054 "\n"
3055 "#ifdef USEFOG\n"
3056 "#ifdef MODE_LIGHTSOURCE\n"
3057 "       color.rgb *= half(FogVertex());\n"
3058 "#else\n"
3059 "       color.rgb = mix(FogColor, float3(color.rgb), FogVertex(EyeVectorModelSpaceFogPlaneVertexDist.xyz, EyeVectorModelSpaceFogPlaneVertexDist.w, FogRangeRecip, FogPlaneViewDist, FogHeightFade, Texture_FogMask));\n"
3060 "#endif\n"
3061 "#endif\n"
3062 "\n"
3063 "       // 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"
3064 "#ifdef USEREFLECTION\n"
3065 "       float4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);\n"
3066 "       //float4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(half3(tex2D(Texture_Normal, TexCoord)) - half3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
3067 "       float2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw;\n"
3068 "       float2 ScreenTexCoord = SafeScreenTexCoord + float3(normalize(half3(tex2D(Texture_Normal, TexCoord)) - half3(0.5))).xy * DistortScaleRefractReflect.zw;\n"
3069 "       // FIXME temporary hack to detect the case that the reflection\n"
3070 "       // gets blackened at edges due to leaving the area that contains actual\n"
3071 "       // content.\n"
3072 "       // Remove this 'ack once we have a better way to stop this thing from\n"
3073 "       // 'appening.\n"
3074 "       float f = min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord + float2(0.01, 0.01)).rgb) / 0.05);\n"
3075 "       f      *= min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord + float2(0.01, -0.01)).rgb) / 0.05);\n"
3076 "       f      *= min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord + float2(-0.01, 0.01)).rgb) / 0.05);\n"
3077 "       f      *= min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord + float2(-0.01, -0.01)).rgb) / 0.05);\n"
3078 "       ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f);\n"
3079 "       color.rgb = mix(color.rgb, half3(tex2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a);\n"
3080 "#endif\n"
3081 "\n"
3082 "       gl_FragColor = float4(color);\n"
3083 "}\n"
3084 "#endif // FRAGMENT_SHADER\n"
3085 "\n"
3086 "#endif // !MODE_DEFERREDLIGHTSOURCE\n"
3087 "#endif // !MODE_DEFERREDGEOMETRY\n"
3088 "#endif // !MODE_WATER\n"
3089 "#endif // !MODE_REFRACTION\n"
3090 "#endif // !MODE_BLOOMBLUR\n"
3091 "#endif // !MODE_GENERIC\n"
3092 "#endif // !MODE_POSTPROCESS\n"
3093 "#endif // !MODE_SHOWDEPTH\n"
3094 "#endif // !MODE_DEPTH_OR_SHADOW\n"
3095 ;
3096
3097 //=======================================================================================================================================================
3098
3099 typedef struct shaderpermutationinfo_s
3100 {
3101         const char *pretext;
3102         const char *name;
3103 }
3104 shaderpermutationinfo_t;
3105
3106 typedef struct shadermodeinfo_s
3107 {
3108         const char *vertexfilename;
3109         const char *geometryfilename;
3110         const char *fragmentfilename;
3111         const char *pretext;
3112         const char *name;
3113 }
3114 shadermodeinfo_t;
3115
3116 typedef enum shaderpermutation_e
3117 {
3118         SHADERPERMUTATION_DIFFUSE = 1<<0, ///< (lightsource) whether to use directional shading
3119         SHADERPERMUTATION_VERTEXTEXTUREBLEND = 1<<1, ///< indicates this is a two-layer material blend based on vertex alpha (q3bsp)
3120         SHADERPERMUTATION_VIEWTINT = 1<<2, ///< view tint (postprocessing only)
3121         SHADERPERMUTATION_COLORMAPPING = 1<<3, ///< indicates this is a colormapped skin
3122         SHADERPERMUTATION_SATURATION = 1<<4, ///< saturation (postprocessing only)
3123         SHADERPERMUTATION_FOGINSIDE = 1<<5, ///< tint the color by fog color or black if using additive blend mode
3124         SHADERPERMUTATION_FOGOUTSIDE = 1<<6, ///< tint the color by fog color or black if using additive blend mode
3125         SHADERPERMUTATION_GAMMARAMPS = 1<<7, ///< gamma (postprocessing only)
3126         SHADERPERMUTATION_CUBEFILTER = 1<<8, ///< (lightsource) use cubemap light filter
3127         SHADERPERMUTATION_GLOW = 1<<9, ///< (lightmap) blend in an additive glow texture
3128         SHADERPERMUTATION_BLOOM = 1<<10, ///< bloom (postprocessing only)
3129         SHADERPERMUTATION_SPECULAR = 1<<11, ///< (lightsource or deluxemapping) render specular effects
3130         SHADERPERMUTATION_POSTPROCESSING = 1<<12, ///< user defined postprocessing (postprocessing only)
3131         SHADERPERMUTATION_EXACTSPECULARMATH = 1<<13, ///< (lightsource or deluxemapping) use exact reflection map for specular effects, as opposed to the usual OpenGL approximation
3132         SHADERPERMUTATION_REFLECTION = 1<<14, ///< normalmap-perturbed reflection of the scene infront of the surface, preformed as an overlay on the surface
3133         SHADERPERMUTATION_OFFSETMAPPING = 1<<15, ///< adjust texcoords to roughly simulate a displacement mapped surface
3134         SHADERPERMUTATION_OFFSETMAPPING_RELIEFMAPPING = 1<<16, ///< adjust texcoords to accurately simulate a displacement mapped surface (requires OFFSETMAPPING to also be set!)
3135         SHADERPERMUTATION_SHADOWMAPRECT = 1<<17, ///< (lightsource) use shadowmap rectangle texture as light filter
3136         SHADERPERMUTATION_SHADOWMAPCUBE = 1<<18, ///< (lightsource) use shadowmap cubemap texture as light filter
3137         SHADERPERMUTATION_SHADOWMAP2D = 1<<19, ///< (lightsource) use shadowmap rectangle texture as light filter
3138         SHADERPERMUTATION_SHADOWMAPPCF = 1<<20, ///< (lightsource) use percentage closer filtering on shadowmap test results
3139         SHADERPERMUTATION_SHADOWMAPPCF2 = 1<<21, ///< (lightsource) use higher quality percentage closer filtering on shadowmap test results
3140         SHADERPERMUTATION_SHADOWSAMPLER = 1<<22, ///< (lightsource) use hardware shadowmap test
3141         SHADERPERMUTATION_SHADOWMAPVSDCT = 1<<23, ///< (lightsource) use virtual shadow depth cube texture for shadowmap indexing
3142         SHADERPERMUTATION_DEFERREDLIGHTMAP = 1<<24, ///< (lightmap) read Texture_ScreenDiffuse/Specular textures and add them on top of lightmapping
3143         SHADERPERMUTATION_ALPHAKILL = 1<<25, ///< (deferredgeometry) discard pixel if diffuse texture alpha below 0.5
3144         SHADERPERMUTATION_LIMIT = 1<<26, ///< size of permutations array
3145         SHADERPERMUTATION_COUNT = 27 ///< size of shaderpermutationinfo array
3146 }
3147 shaderpermutation_t;
3148
3149 // NOTE: MUST MATCH ORDER OF SHADERPERMUTATION_* DEFINES!
3150 shaderpermutationinfo_t shaderpermutationinfo[SHADERPERMUTATION_COUNT] =
3151 {
3152         {"#define USEDIFFUSE\n", " diffuse"},
3153         {"#define USEVERTEXTEXTUREBLEND\n", " vertextextureblend"},
3154         {"#define USEVIEWTINT\n", " viewtint"},
3155         {"#define USECOLORMAPPING\n", " colormapping"},
3156         {"#define USESATURATION\n", " saturation"},
3157         {"#define USEFOGINSIDE\n", " foginside"},
3158         {"#define USEFOGOUTSIDE\n", " fogoutside"},
3159         {"#define USEGAMMARAMPS\n", " gammaramps"},
3160         {"#define USECUBEFILTER\n", " cubefilter"},
3161         {"#define USEGLOW\n", " glow"},
3162         {"#define USEBLOOM\n", " bloom"},
3163         {"#define USESPECULAR\n", " specular"},
3164         {"#define USEPOSTPROCESSING\n", " postprocessing"},
3165         {"#define USEEXACTSPECULARMATH\n", " exactspecularmath"},
3166         {"#define USEREFLECTION\n", " reflection"},
3167         {"#define USEOFFSETMAPPING\n", " offsetmapping"},
3168         {"#define USEOFFSETMAPPING_RELIEFMAPPING\n", " reliefmapping"},
3169         {"#define USESHADOWMAPRECT\n", " shadowmaprect"},
3170         {"#define USESHADOWMAPCUBE\n", " shadowmapcube"},
3171         {"#define USESHADOWMAP2D\n", " shadowmap2d"},
3172         {"#define USESHADOWMAPPCF 1\n", " shadowmappcf"},
3173         {"#define USESHADOWMAPPCF 2\n", " shadowmappcf2"},
3174         {"#define USESHADOWSAMPLER\n", " shadowsampler"},
3175         {"#define USESHADOWMAPVSDCT\n", " shadowmapvsdct"},
3176         {"#define USEDEFERREDLIGHTMAP\n", " deferredlightmap"},
3177         {"#define USEALPHAKILL\n", " alphakill"},
3178 };
3179
3180 /// this enum is multiplied by SHADERPERMUTATION_MODEBASE
3181 typedef enum shadermode_e
3182 {
3183         SHADERMODE_GENERIC, ///< (particles/HUD/etc) vertex color, optionally multiplied by one texture
3184         SHADERMODE_POSTPROCESS, ///< postprocessing shader (r_glsl_postprocess)
3185         SHADERMODE_DEPTH_OR_SHADOW, ///< (depthfirst/shadows) vertex shader only
3186         SHADERMODE_FLATCOLOR, ///< (lightmap) modulate texture by uniform color (q1bsp, q3bsp)
3187         SHADERMODE_VERTEXCOLOR, ///< (lightmap) modulate texture by vertex colors (q3bsp)
3188         SHADERMODE_LIGHTMAP, ///< (lightmap) modulate texture by lightmap texture (q1bsp, q3bsp)
3189         SHADERMODE_LIGHTDIRECTIONMAP_MODELSPACE, ///< (lightmap) use directional pixel shading from texture containing modelspace light directions (q3bsp deluxemap)
3190         SHADERMODE_LIGHTDIRECTIONMAP_TANGENTSPACE, ///< (lightmap) use directional pixel shading from texture containing tangentspace light directions (q1bsp deluxemap)
3191         SHADERMODE_LIGHTDIRECTION, ///< (lightmap) use directional pixel shading from fixed light direction (q3bsp)
3192         SHADERMODE_LIGHTSOURCE, ///< (lightsource) use directional pixel shading from light source (rtlight)
3193         SHADERMODE_REFRACTION, ///< refract background (the material is rendered normally after this pass)
3194         SHADERMODE_WATER, ///< refract background and reflection (the material is rendered normally after this pass)
3195         SHADERMODE_SHOWDEPTH, ///< (debugging) renders depth as color
3196         SHADERMODE_DEFERREDGEOMETRY, ///< (deferred) render material properties to screenspace geometry buffers
3197         SHADERMODE_DEFERREDLIGHTSOURCE, ///< (deferred) use directional pixel shading from light source (rtlight) on screenspace geometry buffers
3198         SHADERMODE_COUNT
3199 }
3200 shadermode_t;
3201
3202 // NOTE: MUST MATCH ORDER OF SHADERMODE_* ENUMS!
3203 shadermodeinfo_t glslshadermodeinfo[SHADERMODE_COUNT] =
3204 {
3205         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_GENERIC\n", " generic"},
3206         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_POSTPROCESS\n", " postprocess"},
3207         {"glsl/default.glsl", NULL, NULL               , "#define MODE_DEPTH_OR_SHADOW\n", " depth/shadow"},
3208         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_FLATCOLOR\n", " flatcolor"},
3209         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_VERTEXCOLOR\n", " vertexcolor"},
3210         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTMAP\n", " lightmap"},
3211         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTDIRECTIONMAP_MODELSPACE\n", " lightdirectionmap_modelspace"},
3212         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n", " lightdirectionmap_tangentspace"},
3213         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTDIRECTION\n", " lightdirection"},
3214         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTSOURCE\n", " lightsource"},
3215         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_REFRACTION\n", " refraction"},
3216         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_WATER\n", " water"},
3217         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_SHOWDEPTH\n", " showdepth"},
3218         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_DEFERREDGEOMETRY\n", " deferredgeometry"},
3219         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_DEFERREDLIGHTSOURCE\n", " deferredlightsource"},
3220 };
3221
3222 #ifdef SUPPORTCG
3223 shadermodeinfo_t cgshadermodeinfo[SHADERMODE_COUNT] =
3224 {
3225         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_GENERIC\n", " generic"},
3226         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_POSTPROCESS\n", " postprocess"},
3227         {"cg/default.cg", NULL, NULL           , "#define MODE_DEPTH_OR_SHADOW\n", " depth/shadow"},
3228         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_FLATCOLOR\n", " flatcolor"},
3229         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_VERTEXCOLOR\n", " vertexcolor"},
3230         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_LIGHTMAP\n", " lightmap"},
3231         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_LIGHTDIRECTIONMAP_MODELSPACE\n", " lightdirectionmap_modelspace"},
3232         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n", " lightdirectionmap_tangentspace"},
3233         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_LIGHTDIRECTION\n", " lightdirection"},
3234         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_LIGHTSOURCE\n", " lightsource"},
3235         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_REFRACTION\n", " refraction"},
3236         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_WATER\n", " water"},
3237         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_SHOWDEPTH\n", " showdepth"},
3238         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_DEFERREDGEOMETRY\n", " deferredgeometry"},
3239         {"cg/default.cg", NULL, "cg/default.cg", "#define MODE_DEFERREDLIGHTSOURCE\n", " deferredlightsource"},
3240 };
3241 #endif
3242
3243 struct r_glsl_permutation_s;
3244 typedef struct r_glsl_permutation_s
3245 {
3246         /// hash lookup data
3247         struct r_glsl_permutation_s *hashnext;
3248         unsigned int mode;
3249         unsigned int permutation;
3250
3251         /// indicates if we have tried compiling this permutation already
3252         qboolean compiled;
3253         /// 0 if compilation failed
3254         int program;
3255         /// locations of detected uniforms in program object, or -1 if not found
3256         int loc_Texture_First;
3257         int loc_Texture_Second;
3258         int loc_Texture_GammaRamps;
3259         int loc_Texture_Normal;
3260         int loc_Texture_Color;
3261         int loc_Texture_Gloss;
3262         int loc_Texture_Glow;
3263         int loc_Texture_SecondaryNormal;
3264         int loc_Texture_SecondaryColor;
3265         int loc_Texture_SecondaryGloss;
3266         int loc_Texture_SecondaryGlow;
3267         int loc_Texture_Pants;
3268         int loc_Texture_Shirt;
3269         int loc_Texture_FogMask;
3270         int loc_Texture_Lightmap;
3271         int loc_Texture_Deluxemap;
3272         int loc_Texture_Attenuation;
3273         int loc_Texture_Cube;
3274         int loc_Texture_Refraction;
3275         int loc_Texture_Reflection;
3276         int loc_Texture_ShadowMapRect;
3277         int loc_Texture_ShadowMapCube;
3278         int loc_Texture_ShadowMap2D;
3279         int loc_Texture_CubeProjection;
3280         int loc_Texture_ScreenDepth;
3281         int loc_Texture_ScreenNormalMap;
3282         int loc_Texture_ScreenDiffuse;
3283         int loc_Texture_ScreenSpecular;
3284         int loc_Alpha;
3285         int loc_BloomBlur_Parameters;
3286         int loc_ClientTime;
3287         int loc_Color_Ambient;
3288         int loc_Color_Diffuse;
3289         int loc_Color_Specular;
3290         int loc_Color_Glow;
3291         int loc_Color_Pants;
3292         int loc_Color_Shirt;
3293         int loc_DeferredColor_Ambient;
3294         int loc_DeferredColor_Diffuse;
3295         int loc_DeferredColor_Specular;
3296         int loc_DeferredMod_Diffuse;
3297         int loc_DeferredMod_Specular;
3298         int loc_DistortScaleRefractReflect;
3299         int loc_EyePosition;
3300         int loc_FogColor;
3301         int loc_FogHeightFade;
3302         int loc_FogPlane;
3303         int loc_FogPlaneViewDist;
3304         int loc_FogRangeRecip;
3305         int loc_LightColor;
3306         int loc_LightDir;
3307         int loc_LightPosition;
3308         int loc_OffsetMapping_Scale;
3309         int loc_PixelSize;
3310         int loc_ReflectColor;
3311         int loc_ReflectFactor;
3312         int loc_ReflectOffset;
3313         int loc_RefractColor;
3314         int loc_Saturation;
3315         int loc_ScreenCenterRefractReflect;
3316         int loc_ScreenScaleRefractReflect;
3317         int loc_ScreenToDepth;
3318         int loc_ShadowMap_Parameters;
3319         int loc_ShadowMap_TextureScale;
3320         int loc_SpecularPower;
3321         int loc_UserVec1;
3322         int loc_UserVec2;
3323         int loc_UserVec3;
3324         int loc_UserVec4;
3325         int loc_ViewTintColor;
3326         int loc_ViewToLight;
3327         int loc_ModelToLight;
3328         int loc_TexMatrix;
3329         int loc_BackgroundTexMatrix;
3330         int loc_ModelViewProjectionMatrix;
3331         int loc_ModelViewMatrix;
3332 }
3333 r_glsl_permutation_t;
3334
3335 #define SHADERPERMUTATION_HASHSIZE 256
3336
3337 /// information about each possible shader permutation
3338 r_glsl_permutation_t *r_glsl_permutationhash[SHADERMODE_COUNT][SHADERPERMUTATION_HASHSIZE];
3339 /// currently selected permutation
3340 r_glsl_permutation_t *r_glsl_permutation;
3341 /// storage for permutations linked in the hash table
3342 memexpandablearray_t r_glsl_permutationarray;
3343
3344 static r_glsl_permutation_t *R_GLSL_FindPermutation(unsigned int mode, unsigned int permutation)
3345 {
3346         //unsigned int hashdepth = 0;
3347         unsigned int hashindex = (permutation * 0x1021) & (SHADERPERMUTATION_HASHSIZE - 1);
3348         r_glsl_permutation_t *p;
3349         for (p = r_glsl_permutationhash[mode][hashindex];p;p = p->hashnext)
3350         {
3351                 if (p->mode == mode && p->permutation == permutation)
3352                 {
3353                         //if (hashdepth > 10)
3354                         //      Con_Printf("R_GLSL_FindPermutation: Warning: %i:%i has hashdepth %i\n", mode, permutation, hashdepth);
3355                         return p;
3356                 }
3357                 //hashdepth++;
3358         }
3359         p = (r_glsl_permutation_t*)Mem_ExpandableArray_AllocRecord(&r_glsl_permutationarray);
3360         p->mode = mode;
3361         p->permutation = permutation;
3362         p->hashnext = r_glsl_permutationhash[mode][hashindex];
3363         r_glsl_permutationhash[mode][hashindex] = p;
3364         //if (hashdepth > 10)
3365         //      Con_Printf("R_GLSL_FindPermutation: Warning: %i:%i has hashdepth %i\n", mode, permutation, hashdepth);
3366         return p;
3367 }
3368
3369 static char *R_GLSL_GetText(const char *filename, qboolean printfromdisknotice)
3370 {
3371         char *shaderstring;
3372         if (!filename || !filename[0])
3373                 return NULL;
3374         shaderstring = (char *)FS_LoadFile(filename, r_main_mempool, false, NULL);
3375         if (shaderstring)
3376         {
3377                 if (printfromdisknotice)
3378                         Con_DPrintf("from disk %s... ", filename);
3379                 return shaderstring;
3380         }
3381         else if (!strcmp(filename, "glsl/default.glsl"))
3382         {
3383                 shaderstring = (char *) Mem_Alloc(r_main_mempool, strlen(builtinshaderstring) + 1);
3384                 memcpy(shaderstring, builtinshaderstring, strlen(builtinshaderstring) + 1);
3385         }
3386         return shaderstring;
3387 }
3388
3389 static void R_GLSL_CompilePermutation(r_glsl_permutation_t *p, unsigned int mode, unsigned int permutation)
3390 {
3391         int i;
3392         shadermodeinfo_t *modeinfo = glslshadermodeinfo + mode;
3393         int vertstrings_count = 0;
3394         int geomstrings_count = 0;
3395         int fragstrings_count = 0;
3396         char *vertexstring, *geometrystring, *fragmentstring;
3397         const char *vertstrings_list[32+3];
3398         const char *geomstrings_list[32+3];
3399         const char *fragstrings_list[32+3];
3400         char permutationname[256];
3401
3402         if (p->compiled)
3403                 return;
3404         p->compiled = true;
3405         p->program = 0;
3406
3407         permutationname[0] = 0;
3408         vertexstring   = R_GLSL_GetText(modeinfo->vertexfilename, true);
3409         geometrystring = R_GLSL_GetText(modeinfo->geometryfilename, false);
3410         fragmentstring = R_GLSL_GetText(modeinfo->fragmentfilename, false);
3411
3412         strlcat(permutationname, modeinfo->vertexfilename, sizeof(permutationname));
3413
3414         // the first pretext is which type of shader to compile as
3415         // (later these will all be bound together as a program object)
3416         vertstrings_list[vertstrings_count++] = "#define VERTEX_SHADER\n";
3417         geomstrings_list[geomstrings_count++] = "#define GEOMETRY_SHADER\n";
3418         fragstrings_list[fragstrings_count++] = "#define FRAGMENT_SHADER\n";
3419
3420         // the second pretext is the mode (for example a light source)
3421         vertstrings_list[vertstrings_count++] = modeinfo->pretext;
3422         geomstrings_list[geomstrings_count++] = modeinfo->pretext;
3423         fragstrings_list[fragstrings_count++] = modeinfo->pretext;
3424         strlcat(permutationname, modeinfo->name, sizeof(permutationname));
3425
3426         // now add all the permutation pretexts
3427         for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
3428         {
3429                 if (permutation & (1<<i))
3430                 {
3431                         vertstrings_list[vertstrings_count++] = shaderpermutationinfo[i].pretext;
3432                         geomstrings_list[geomstrings_count++] = shaderpermutationinfo[i].pretext;
3433                         fragstrings_list[fragstrings_count++] = shaderpermutationinfo[i].pretext;
3434                         strlcat(permutationname, shaderpermutationinfo[i].name, sizeof(permutationname));
3435                 }
3436                 else
3437                 {
3438                         // keep line numbers correct
3439                         vertstrings_list[vertstrings_count++] = "\n";
3440                         geomstrings_list[geomstrings_count++] = "\n";
3441                         fragstrings_list[fragstrings_count++] = "\n";
3442                 }
3443         }
3444
3445         // now append the shader text itself
3446         vertstrings_list[vertstrings_count++] = vertexstring;
3447         geomstrings_list[geomstrings_count++] = geometrystring;
3448         fragstrings_list[fragstrings_count++] = fragmentstring;
3449
3450         // if any sources were NULL, clear the respective list
3451         if (!vertexstring)
3452                 vertstrings_count = 0;
3453         if (!geometrystring)
3454                 geomstrings_count = 0;
3455         if (!fragmentstring)
3456                 fragstrings_count = 0;
3457
3458         // compile the shader program
3459         if (vertstrings_count + geomstrings_count + fragstrings_count)
3460                 p->program = GL_Backend_CompileProgram(vertstrings_count, vertstrings_list, geomstrings_count, geomstrings_list, fragstrings_count, fragstrings_list);
3461         if (p->program)
3462         {
3463                 CHECKGLERROR
3464                 qglUseProgramObjectARB(p->program);CHECKGLERROR
3465                 // look up all the uniform variable names we care about, so we don't
3466                 // have to look them up every time we set them
3467
3468                 p->loc_Texture_First              = qglGetUniformLocationARB(p->program, "Texture_First");
3469                 p->loc_Texture_Second             = qglGetUniformLocationARB(p->program, "Texture_Second");
3470                 p->loc_Texture_GammaRamps         = qglGetUniformLocationARB(p->program, "Texture_GammaRamps");
3471                 p->loc_Texture_Normal             = qglGetUniformLocationARB(p->program, "Texture_Normal");
3472                 p->loc_Texture_Color              = qglGetUniformLocationARB(p->program, "Texture_Color");
3473                 p->loc_Texture_Gloss              = qglGetUniformLocationARB(p->program, "Texture_Gloss");
3474                 p->loc_Texture_Glow               = qglGetUniformLocationARB(p->program, "Texture_Glow");
3475                 p->loc_Texture_SecondaryNormal    = qglGetUniformLocationARB(p->program, "Texture_SecondaryNormal");
3476                 p->loc_Texture_SecondaryColor     = qglGetUniformLocationARB(p->program, "Texture_SecondaryColor");
3477                 p->loc_Texture_SecondaryGloss     = qglGetUniformLocationARB(p->program, "Texture_SecondaryGloss");
3478                 p->loc_Texture_SecondaryGlow      = qglGetUniformLocationARB(p->program, "Texture_SecondaryGlow");
3479                 p->loc_Texture_Pants              = qglGetUniformLocationARB(p->program, "Texture_Pants");
3480                 p->loc_Texture_Shirt              = qglGetUniformLocationARB(p->program, "Texture_Shirt");
3481                 p->loc_Texture_FogMask            = qglGetUniformLocationARB(p->program, "Texture_FogMask");
3482                 p->loc_Texture_Lightmap           = qglGetUniformLocationARB(p->program, "Texture_Lightmap");
3483                 p->loc_Texture_Deluxemap          = qglGetUniformLocationARB(p->program, "Texture_Deluxemap");
3484                 p->loc_Texture_Attenuation        = qglGetUniformLocationARB(p->program, "Texture_Attenuation");
3485                 p->loc_Texture_Cube               = qglGetUniformLocationARB(p->program, "Texture_Cube");
3486                 p->loc_Texture_Refraction         = qglGetUniformLocationARB(p->program, "Texture_Refraction");
3487                 p->loc_Texture_Reflection         = qglGetUniformLocationARB(p->program, "Texture_Reflection");
3488                 p->loc_Texture_ShadowMapRect      = qglGetUniformLocationARB(p->program, "Texture_ShadowMapRect");
3489                 p->loc_Texture_ShadowMapCube      = qglGetUniformLocationARB(p->program, "Texture_ShadowMapCube");
3490                 p->loc_Texture_ShadowMap2D        = qglGetUniformLocationARB(p->program, "Texture_ShadowMap2D");
3491                 p->loc_Texture_CubeProjection     = qglGetUniformLocationARB(p->program, "Texture_CubeProjection");
3492                 p->loc_Texture_ScreenDepth        = qglGetUniformLocationARB(p->program, "Texture_ScreenDepth");
3493                 p->loc_Texture_ScreenNormalMap    = qglGetUniformLocationARB(p->program, "Texture_ScreenNormalMap");
3494                 p->loc_Texture_ScreenDiffuse      = qglGetUniformLocationARB(p->program, "Texture_ScreenDiffuse");
3495                 p->loc_Texture_ScreenSpecular     = qglGetUniformLocationARB(p->program, "Texture_ScreenSpecular");
3496                 p->loc_Alpha                      = qglGetUniformLocationARB(p->program, "Alpha");
3497                 p->loc_BloomBlur_Parameters       = qglGetUniformLocationARB(p->program, "BloomBlur_Parameters");
3498                 p->loc_ClientTime                 = qglGetUniformLocationARB(p->program, "ClientTime");
3499                 p->loc_Color_Ambient              = qglGetUniformLocationARB(p->program, "Color_Ambient");
3500                 p->loc_Color_Diffuse              = qglGetUniformLocationARB(p->program, "Color_Diffuse");
3501                 p->loc_Color_Specular             = qglGetUniformLocationARB(p->program, "Color_Specular");
3502                 p->loc_Color_Glow                 = qglGetUniformLocationARB(p->program, "Color_Glow");
3503                 p->loc_Color_Pants                = qglGetUniformLocationARB(p->program, "Color_Pants");
3504                 p->loc_Color_Shirt                = qglGetUniformLocationARB(p->program, "Color_Shirt");
3505                 p->loc_DeferredColor_Ambient      = qglGetUniformLocationARB(p->program, "DeferredColor_Ambient");
3506                 p->loc_DeferredColor_Diffuse      = qglGetUniformLocationARB(p->program, "DeferredColor_Diffuse");
3507                 p->loc_DeferredColor_Specular     = qglGetUniformLocationARB(p->program, "DeferredColor_Specular");
3508                 p->loc_DeferredMod_Diffuse        = qglGetUniformLocationARB(p->program, "DeferredMod_Diffuse");
3509                 p->loc_DeferredMod_Specular       = qglGetUniformLocationARB(p->program, "DeferredMod_Specular");
3510                 p->loc_DistortScaleRefractReflect = qglGetUniformLocationARB(p->program, "DistortScaleRefractReflect");
3511                 p->loc_EyePosition                = qglGetUniformLocationARB(p->program, "EyePosition");
3512                 p->loc_FogColor                   = qglGetUniformLocationARB(p->program, "FogColor");
3513                 p->loc_FogHeightFade              = qglGetUniformLocationARB(p->program, "FogHeightFade");
3514                 p->loc_FogPlane                   = qglGetUniformLocationARB(p->program, "FogPlane");
3515                 p->loc_FogPlaneViewDist           = qglGetUniformLocationARB(p->program, "FogPlaneViewDist");
3516                 p->loc_FogRangeRecip              = qglGetUniformLocationARB(p->program, "FogRangeRecip");
3517                 p->loc_LightColor                 = qglGetUniformLocationARB(p->program, "LightColor");
3518                 p->loc_LightDir                   = qglGetUniformLocationARB(p->program, "LightDir");
3519                 p->loc_LightPosition              = qglGetUniformLocationARB(p->program, "LightPosition");
3520                 p->loc_OffsetMapping_Scale        = qglGetUniformLocationARB(p->program, "OffsetMapping_Scale");
3521                 p->loc_PixelSize                  = qglGetUniformLocationARB(p->program, "PixelSize");
3522                 p->loc_ReflectColor               = qglGetUniformLocationARB(p->program, "ReflectColor");
3523                 p->loc_ReflectFactor              = qglGetUniformLocationARB(p->program, "ReflectFactor");
3524                 p->loc_ReflectOffset              = qglGetUniformLocationARB(p->program, "ReflectOffset");
3525                 p->loc_RefractColor               = qglGetUniformLocationARB(p->program, "RefractColor");
3526                 p->loc_Saturation                 = qglGetUniformLocationARB(p->program, "Saturation");
3527                 p->loc_ScreenCenterRefractReflect = qglGetUniformLocationARB(p->program, "ScreenCenterRefractReflect");
3528                 p->loc_ScreenScaleRefractReflect  = qglGetUniformLocationARB(p->program, "ScreenScaleRefractReflect");
3529                 p->loc_ScreenToDepth              = qglGetUniformLocationARB(p->program, "ScreenToDepth");
3530                 p->loc_ShadowMap_Parameters       = qglGetUniformLocationARB(p->program, "ShadowMap_Parameters");
3531                 p->loc_ShadowMap_TextureScale     = qglGetUniformLocationARB(p->program, "ShadowMap_TextureScale");
3532                 p->loc_SpecularPower              = qglGetUniformLocationARB(p->program, "SpecularPower");
3533                 p->loc_UserVec1                   = qglGetUniformLocationARB(p->program, "UserVec1");
3534                 p->loc_UserVec2                   = qglGetUniformLocationARB(p->program, "UserVec2");
3535                 p->loc_UserVec3                   = qglGetUniformLocationARB(p->program, "UserVec3");
3536                 p->loc_UserVec4                   = qglGetUniformLocationARB(p->program, "UserVec4");
3537                 p->loc_ViewTintColor              = qglGetUniformLocationARB(p->program, "ViewTintColor");
3538                 p->loc_ViewToLight                = qglGetUniformLocationARB(p->program, "ViewToLight");
3539                 p->loc_ModelToLight               = qglGetUniformLocationARB(p->program, "ModelToLight");
3540                 p->loc_TexMatrix                  = qglGetUniformLocationARB(p->program, "TexMatrix");
3541                 p->loc_BackgroundTexMatrix        = qglGetUniformLocationARB(p->program, "BackgroundTexMatrix");
3542                 p->loc_ModelViewMatrix            = qglGetUniformLocationARB(p->program, "ModelViewMatrix");
3543                 p->loc_ModelViewProjectionMatrix  = qglGetUniformLocationARB(p->program, "ModelViewProjectionMatrix");
3544                 // initialize the samplers to refer to the texture units we use
3545                 if (p->loc_Texture_First           >= 0) qglUniform1iARB(p->loc_Texture_First          , GL20TU_FIRST);
3546                 if (p->loc_Texture_Second          >= 0) qglUniform1iARB(p->loc_Texture_Second         , GL20TU_SECOND);
3547                 if (p->loc_Texture_GammaRamps      >= 0) qglUniform1iARB(p->loc_Texture_GammaRamps     , GL20TU_GAMMARAMPS);
3548                 if (p->loc_Texture_Normal          >= 0) qglUniform1iARB(p->loc_Texture_Normal         , GL20TU_NORMAL);
3549                 if (p->loc_Texture_Color           >= 0) qglUniform1iARB(p->loc_Texture_Color          , GL20TU_COLOR);
3550                 if (p->loc_Texture_Gloss           >= 0) qglUniform1iARB(p->loc_Texture_Gloss          , GL20TU_GLOSS);
3551                 if (p->loc_Texture_Glow            >= 0) qglUniform1iARB(p->loc_Texture_Glow           , GL20TU_GLOW);
3552                 if (p->loc_Texture_SecondaryNormal >= 0) qglUniform1iARB(p->loc_Texture_SecondaryNormal, GL20TU_SECONDARY_NORMAL);
3553                 if (p->loc_Texture_SecondaryColor  >= 0) qglUniform1iARB(p->loc_Texture_SecondaryColor , GL20TU_SECONDARY_COLOR);
3554                 if (p->loc_Texture_SecondaryGloss  >= 0) qglUniform1iARB(p->loc_Texture_SecondaryGloss , GL20TU_SECONDARY_GLOSS);
3555                 if (p->loc_Texture_SecondaryGlow   >= 0) qglUniform1iARB(p->loc_Texture_SecondaryGlow  , GL20TU_SECONDARY_GLOW);
3556                 if (p->loc_Texture_Pants           >= 0) qglUniform1iARB(p->loc_Texture_Pants          , GL20TU_PANTS);
3557                 if (p->loc_Texture_Shirt           >= 0) qglUniform1iARB(p->loc_Texture_Shirt          , GL20TU_SHIRT);
3558                 if (p->loc_Texture_FogMask         >= 0) qglUniform1iARB(p->loc_Texture_FogMask        , GL20TU_FOGMASK);
3559                 if (p->loc_Texture_Lightmap        >= 0) qglUniform1iARB(p->loc_Texture_Lightmap       , GL20TU_LIGHTMAP);
3560                 if (p->loc_Texture_Deluxemap       >= 0) qglUniform1iARB(p->loc_Texture_Deluxemap      , GL20TU_DELUXEMAP);
3561                 if (p->loc_Texture_Attenuation     >= 0) qglUniform1iARB(p->loc_Texture_Attenuation    , GL20TU_ATTENUATION);
3562                 if (p->loc_Texture_Cube            >= 0) qglUniform1iARB(p->loc_Texture_Cube           , GL20TU_CUBE);
3563                 if (p->loc_Texture_Refraction      >= 0) qglUniform1iARB(p->loc_Texture_Refraction     , GL20TU_REFRACTION);
3564                 if (p->loc_Texture_Reflection      >= 0) qglUniform1iARB(p->loc_Texture_Reflection     , GL20TU_REFLECTION);
3565                 if (p->loc_Texture_ShadowMapRect   >= 0) qglUniform1iARB(p->loc_Texture_ShadowMapRect  , GL20TU_SHADOWMAPRECT);
3566                 if (p->loc_Texture_ShadowMapCube   >= 0) qglUniform1iARB(p->loc_Texture_ShadowMapCube  , GL20TU_SHADOWMAPCUBE);
3567                 if (p->loc_Texture_ShadowMap2D     >= 0) qglUniform1iARB(p->loc_Texture_ShadowMap2D    , GL20TU_SHADOWMAP2D);
3568                 if (p->loc_Texture_CubeProjection  >= 0) qglUniform1iARB(p->loc_Texture_CubeProjection , GL20TU_CUBEPROJECTION);
3569                 if (p->loc_Texture_ScreenDepth     >= 0) qglUniform1iARB(p->loc_Texture_ScreenDepth    , GL20TU_SCREENDEPTH);
3570                 if (p->loc_Texture_ScreenNormalMap >= 0) qglUniform1iARB(p->loc_Texture_ScreenNormalMap, GL20TU_SCREENNORMALMAP);
3571                 if (p->loc_Texture_ScreenDiffuse   >= 0) qglUniform1iARB(p->loc_Texture_ScreenDiffuse  , GL20TU_SCREENDIFFUSE);
3572                 if (p->loc_Texture_ScreenSpecular  >= 0) qglUniform1iARB(p->loc_Texture_ScreenSpecular , GL20TU_SCREENSPECULAR);
3573                 CHECKGLERROR
3574                 Con_DPrintf("^5GLSL shader %s compiled.\n", permutationname);
3575         }
3576         else
3577                 Con_Printf("^1GLSL shader %s failed!  some features may not work properly.\n", permutationname);
3578
3579         // free the strings
3580         if (vertexstring)
3581                 Mem_Free(vertexstring);
3582         if (geometrystring)
3583                 Mem_Free(geometrystring);
3584         if (fragmentstring)
3585                 Mem_Free(fragmentstring);
3586 }
3587
3588 void R_SetupShader_SetPermutationGLSL(unsigned int mode, unsigned int permutation)
3589 {
3590         r_glsl_permutation_t *perm = R_GLSL_FindPermutation(mode, permutation);
3591         if (r_glsl_permutation != perm)
3592         {
3593                 r_glsl_permutation = perm;
3594                 if (!r_glsl_permutation->program)
3595                 {
3596                         if (!r_glsl_permutation->compiled)
3597                                 R_GLSL_CompilePermutation(perm, mode, permutation);
3598                         if (!r_glsl_permutation->program)
3599                         {
3600                                 // remove features until we find a valid permutation
3601                                 int i;
3602                                 for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
3603                                 {
3604                                         // reduce i more quickly whenever it would not remove any bits
3605                                         int j = 1<<(SHADERPERMUTATION_COUNT-1-i);
3606                                         if (!(permutation & j))
3607                                                 continue;
3608                                         permutation -= j;
3609                                         r_glsl_permutation = R_GLSL_FindPermutation(mode, permutation);
3610                                         if (!r_glsl_permutation->compiled)
3611                                                 R_GLSL_CompilePermutation(perm, mode, permutation);
3612                                         if (r_glsl_permutation->program)
3613                                                 break;
3614                                 }
3615                                 if (i >= SHADERPERMUTATION_COUNT)
3616                                 {
3617                                         //Con_Printf("Could not find a working OpenGL 2.0 shader for permutation %s %s\n", shadermodeinfo[mode].vertexfilename, shadermodeinfo[mode].pretext);
3618                                         r_glsl_permutation = R_GLSL_FindPermutation(mode, permutation);
3619                                         qglUseProgramObjectARB(0);CHECKGLERROR
3620                                         return; // no bit left to clear, entire mode is broken
3621                                 }
3622                         }
3623                 }
3624                 CHECKGLERROR
3625                 qglUseProgramObjectARB(r_glsl_permutation->program);CHECKGLERROR
3626         }
3627         if (r_glsl_permutation->loc_ModelViewProjectionMatrix >= 0) qglUniformMatrix4fvARB(r_glsl_permutation->loc_ModelViewProjectionMatrix, 1, false, gl_modelviewprojection16f);
3628 }
3629
3630 #ifdef SUPPORTCG
3631 #include <Cg/cgGL.h>
3632 struct r_cg_permutation_s;
3633 typedef struct r_cg_permutation_s
3634 {
3635         /// hash lookup data
3636         struct r_cg_permutation_s *hashnext;
3637         unsigned int mode;
3638         unsigned int permutation;
3639
3640         /// indicates if we have tried compiling this permutation already
3641         qboolean compiled;
3642         /// 0 if compilation failed
3643         CGprogram vprogram;
3644         CGprogram fprogram;
3645         /// locations of detected parameters in programs, or NULL if not found
3646         CGparameter vp_EyePosition;
3647         CGparameter vp_FogPlane;
3648         CGparameter vp_LightDir;
3649         CGparameter vp_LightPosition;
3650         CGparameter vp_ModelToLight;
3651         CGparameter vp_TexMatrix;
3652         CGparameter vp_BackgroundTexMatrix;
3653         CGparameter vp_ModelViewProjectionMatrix;
3654         CGparameter vp_ModelViewMatrix;
3655
3656         CGparameter fp_Texture_First;
3657         CGparameter fp_Texture_Second;
3658         CGparameter fp_Texture_GammaRamps;
3659         CGparameter fp_Texture_Normal;
3660         CGparameter fp_Texture_Color;
3661         CGparameter fp_Texture_Gloss;
3662         CGparameter fp_Texture_Glow;
3663         CGparameter fp_Texture_SecondaryNormal;
3664         CGparameter fp_Texture_SecondaryColor;
3665         CGparameter fp_Texture_SecondaryGloss;
3666         CGparameter fp_Texture_SecondaryGlow;
3667         CGparameter fp_Texture_Pants;
3668         CGparameter fp_Texture_Shirt;
3669         CGparameter fp_Texture_FogMask;
3670         CGparameter fp_Texture_Lightmap;
3671         CGparameter fp_Texture_Deluxemap;
3672         CGparameter fp_Texture_Attenuation;
3673         CGparameter fp_Texture_Cube;
3674         CGparameter fp_Texture_Refraction;
3675         CGparameter fp_Texture_Reflection;
3676         CGparameter fp_Texture_ShadowMapRect;
3677         CGparameter fp_Texture_ShadowMapCube;
3678         CGparameter fp_Texture_ShadowMap2D;
3679         CGparameter fp_Texture_CubeProjection;
3680         CGparameter fp_Texture_ScreenDepth;
3681         CGparameter fp_Texture_ScreenNormalMap;
3682         CGparameter fp_Texture_ScreenDiffuse;
3683         CGparameter fp_Texture_ScreenSpecular;
3684         CGparameter fp_Alpha;
3685         CGparameter fp_BloomBlur_Parameters;
3686         CGparameter fp_ClientTime;
3687         CGparameter fp_Color_Ambient;
3688         CGparameter fp_Color_Diffuse;
3689         CGparameter fp_Color_Specular;
3690         CGparameter fp_Color_Glow;
3691         CGparameter fp_Color_Pants;
3692         CGparameter fp_Color_Shirt;
3693         CGparameter fp_DeferredColor_Ambient;
3694         CGparameter fp_DeferredColor_Diffuse;
3695         CGparameter fp_DeferredColor_Specular;
3696         CGparameter fp_DeferredMod_Diffuse;
3697         CGparameter fp_DeferredMod_Specular;
3698         CGparameter fp_DistortScaleRefractReflect;
3699         CGparameter fp_EyePosition;
3700         CGparameter fp_FogColor;
3701         CGparameter fp_FogHeightFade;
3702         CGparameter fp_FogPlane;
3703         CGparameter fp_FogPlaneViewDist;
3704         CGparameter fp_FogRangeRecip;
3705         CGparameter fp_LightColor;
3706         CGparameter fp_LightDir;
3707         CGparameter fp_LightPosition;
3708         CGparameter fp_OffsetMapping_Scale;
3709         CGparameter fp_PixelSize;
3710         CGparameter fp_ReflectColor;
3711         CGparameter fp_ReflectFactor;
3712         CGparameter fp_ReflectOffset;
3713         CGparameter fp_RefractColor;
3714         CGparameter fp_Saturation;
3715         CGparameter fp_ScreenCenterRefractReflect;
3716         CGparameter fp_ScreenScaleRefractReflect;
3717         CGparameter fp_ScreenToDepth;
3718         CGparameter fp_ShadowMap_Parameters;
3719         CGparameter fp_ShadowMap_TextureScale;
3720         CGparameter fp_SpecularPower;
3721         CGparameter fp_UserVec1;
3722         CGparameter fp_UserVec2;
3723         CGparameter fp_UserVec3;
3724         CGparameter fp_UserVec4;
3725         CGparameter fp_ViewTintColor;
3726         CGparameter fp_ViewToLight;
3727 }
3728 r_cg_permutation_t;
3729
3730 /// information about each possible shader permutation
3731 r_cg_permutation_t *r_cg_permutationhash[SHADERMODE_COUNT][SHADERPERMUTATION_HASHSIZE];
3732 /// currently selected permutation
3733 r_cg_permutation_t *r_cg_permutation;
3734 /// storage for permutations linked in the hash table
3735 memexpandablearray_t r_cg_permutationarray;
3736
3737 #define CHECKCGERROR {CGerror err = cgGetError(), err2 = err;if (err){Con_Printf("%s:%i CG error %i: %s : %s\n", __FILE__, __LINE__, err, cgGetErrorString(err), cgGetLastErrorString(&err2));if (err == 1) Con_Printf("last listing:\n%s\n", cgGetLastListing(vid.cgcontext));}}
3738
3739 static r_cg_permutation_t *R_CG_FindPermutation(unsigned int mode, unsigned int permutation)
3740 {
3741         //unsigned int hashdepth = 0;
3742         unsigned int hashindex = (permutation * 0x1021) & (SHADERPERMUTATION_HASHSIZE - 1);
3743         r_cg_permutation_t *p;
3744         for (p = r_cg_permutationhash[mode][hashindex];p;p = p->hashnext)
3745         {
3746                 if (p->mode == mode && p->permutation == permutation)
3747                 {
3748                         //if (hashdepth > 10)
3749                         //      Con_Printf("R_CG_FindPermutation: Warning: %i:%i has hashdepth %i\n", mode, permutation, hashdepth);
3750                         return p;
3751                 }
3752                 //hashdepth++;
3753         }
3754         p = (r_cg_permutation_t*)Mem_ExpandableArray_AllocRecord(&r_cg_permutationarray);
3755         p->mode = mode;
3756         p->permutation = permutation;
3757         p->hashnext = r_cg_permutationhash[mode][hashindex];
3758         r_cg_permutationhash[mode][hashindex] = p;
3759         //if (hashdepth > 10)
3760         //      Con_Printf("R_CG_FindPermutation: Warning: %i:%i has hashdepth %i\n", mode, permutation, hashdepth);
3761         return p;
3762 }
3763
3764 static char *R_CG_GetText(const char *filename, qboolean printfromdisknotice)
3765 {
3766         char *shaderstring;
3767         if (!filename || !filename[0])
3768                 return NULL;
3769         shaderstring = (char *)FS_LoadFile(filename, r_main_mempool, false, NULL);
3770         if (shaderstring)
3771         {
3772                 if (printfromdisknotice)
3773                         Con_DPrintf("from disk %s... ", filename);
3774                 return shaderstring;
3775         }
3776         else if (!strcmp(filename, "cg/default.cg"))
3777         {
3778                 shaderstring = (char *) Mem_Alloc(r_main_mempool, strlen(builtincgshaderstring) + 1);
3779                 memcpy(shaderstring, builtincgshaderstring, strlen(builtincgshaderstring) + 1);
3780         }
3781         return shaderstring;
3782 }
3783
3784 static void R_CG_CompilePermutation(r_cg_permutation_t *p, unsigned int mode, unsigned int permutation)
3785 {
3786         int i;
3787         shadermodeinfo_t *modeinfo = cgshadermodeinfo + mode;
3788         int vertstrings_count = 0, vertstring_length = 0;
3789         int geomstrings_count = 0, geomstring_length = 0;
3790         int fragstrings_count = 0, fragstring_length = 0;
3791         char *t;
3792         char *vertexstring, *geometrystring, *fragmentstring;
3793         char *vertstring, *geomstring, *fragstring;
3794         const char *vertstrings_list[32+3];
3795         const char *geomstrings_list[32+3];
3796         const char *fragstrings_list[32+3];
3797         char permutationname[256];
3798         CGprofile vertexProfile;
3799         CGprofile fragmentProfile;
3800
3801         if (p->compiled)
3802                 return;
3803         p->compiled = true;
3804         p->vprogram = NULL;
3805         p->fprogram = NULL;
3806
3807         permutationname[0] = 0;
3808         vertexstring   = R_CG_GetText(modeinfo->vertexfilename, true);
3809         geometrystring = R_CG_GetText(modeinfo->geometryfilename, false);
3810         fragmentstring = R_CG_GetText(modeinfo->fragmentfilename, false);
3811
3812         strlcat(permutationname, modeinfo->vertexfilename, sizeof(permutationname));
3813
3814         // the first pretext is which type of shader to compile as
3815         // (later these will all be bound together as a program object)
3816         vertstrings_list[vertstrings_count++] = "#define VERTEX_SHADER\n";
3817         geomstrings_list[geomstrings_count++] = "#define GEOMETRY_SHADER\n";
3818         fragstrings_list[fragstrings_count++] = "#define FRAGMENT_SHADER\n";
3819
3820         // the second pretext is the mode (for example a light source)
3821         vertstrings_list[vertstrings_count++] = modeinfo->pretext;
3822         geomstrings_list[geomstrings_count++] = modeinfo->pretext;
3823         fragstrings_list[fragstrings_count++] = modeinfo->pretext;
3824         strlcat(permutationname, modeinfo->name, sizeof(permutationname));
3825
3826         // now add all the permutation pretexts
3827         for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
3828         {
3829                 if (permutation & (1<<i))
3830                 {
3831                         vertstrings_list[vertstrings_count++] = shaderpermutationinfo[i].pretext;
3832                         geomstrings_list[geomstrings_count++] = shaderpermutationinfo[i].pretext;
3833                         fragstrings_list[fragstrings_count++] = shaderpermutationinfo[i].pretext;
3834                         strlcat(permutationname, shaderpermutationinfo[i].name, sizeof(permutationname));
3835                 }
3836                 else
3837                 {
3838                         // keep line numbers correct
3839                         vertstrings_list[vertstrings_count++] = "\n";
3840                         geomstrings_list[geomstrings_count++] = "\n";
3841                         fragstrings_list[fragstrings_count++] = "\n";
3842                 }
3843         }
3844
3845         // now append the shader text itself
3846         vertstrings_list[vertstrings_count++] = vertexstring;
3847         geomstrings_list[geomstrings_count++] = geometrystring;
3848         fragstrings_list[fragstrings_count++] = fragmentstring;
3849
3850         // if any sources were NULL, clear the respective list
3851         if (!vertexstring)
3852                 vertstrings_count = 0;
3853         if (!geometrystring)
3854                 geomstrings_count = 0;
3855         if (!fragmentstring)
3856                 fragstrings_count = 0;
3857
3858         vertstring_length = 0;
3859         for (i = 0;i < vertstrings_count;i++)
3860                 vertstring_length += strlen(vertstrings_list[i]);
3861         vertstring = t = Mem_Alloc(tempmempool, vertstring_length + 1);
3862         for (i = 0;i < vertstrings_count;t += strlen(vertstrings_list[i]), i++)
3863                 memcpy(t, vertstrings_list[i], strlen(vertstrings_list[i]));
3864
3865         geomstring_length = 0;
3866         for (i = 0;i < geomstrings_count;i++)
3867                 geomstring_length += strlen(geomstrings_list[i]);
3868         geomstring = t = Mem_Alloc(tempmempool, geomstring_length + 1);
3869         for (i = 0;i < geomstrings_count;t += strlen(geomstrings_list[i]), i++)
3870                 memcpy(t, geomstrings_list[i], strlen(geomstrings_list[i]));
3871
3872         fragstring_length = 0;
3873         for (i = 0;i < fragstrings_count;i++)
3874                 fragstring_length += strlen(fragstrings_list[i]);
3875         fragstring = t = Mem_Alloc(tempmempool, fragstring_length + 1);
3876         for (i = 0;i < fragstrings_count;t += strlen(fragstrings_list[i]), i++)
3877                 memcpy(t, fragstrings_list[i], strlen(fragstrings_list[i]));
3878
3879         CHECKGLERROR
3880         CHECKCGERROR
3881         //vertexProfile = CG_PROFILE_ARBVP1;
3882         //fragmentProfile = CG_PROFILE_ARBFP1;
3883         vertexProfile = cgGLGetLatestProfile(CG_GL_VERTEX);CHECKCGERROR
3884         fragmentProfile = cgGLGetLatestProfile(CG_GL_FRAGMENT);CHECKCGERROR
3885         //cgGLSetOptimalOptions(vertexProfile);CHECKCGERROR
3886         //cgGLSetOptimalOptions(fragmentProfile);CHECKCGERROR
3887         //cgSetAutoCompile(vid.cgcontext, CG_COMPILE_MANUAL);CHECKCGERROR
3888         CHECKGLERROR
3889
3890         // compile the vertex program
3891         if (vertstring[0] && (p->vprogram = cgCreateProgram(vid.cgcontext, CG_SOURCE, vertstring, CG_PROFILE_ARBVP1, NULL, NULL)))
3892         {
3893                 CHECKCGERROR
3894                 cgCompileProgram(p->vprogram);CHECKCGERROR
3895                 if (!cgIsProgramCompiled(p->vprogram))
3896                 {
3897                         CHECKCGERROR
3898                         cgDestroyProgram(p->vprogram);CHECKCGERROR
3899                         p->vprogram = 0;
3900                 }
3901                 else
3902                 {
3903                         cgGLLoadProgram(p->vprogram);CHECKCGERROR CHECKGLERROR
3904                         cgGLEnableProfile(vertexProfile);CHECKCGERROR CHECKGLERROR
3905                         // look up all the uniform variable names we care about, so we don't
3906                         // have to look them up every time we set them
3907                         CHECKCGERROR
3908                         p->vp_EyePosition                = cgGetNamedParameter(p->vprogram, "EyePosition");
3909                         p->vp_FogPlane                   = cgGetNamedParameter(p->vprogram, "FogPlane");
3910                         p->vp_LightDir                   = cgGetNamedParameter(p->vprogram, "LightDir");
3911                         p->vp_LightPosition              = cgGetNamedParameter(p->vprogram, "LightPosition");
3912                         p->vp_ModelToLight               = cgGetNamedParameter(p->vprogram, "ModelToLight");
3913                         p->vp_TexMatrix                  = cgGetNamedParameter(p->vprogram, "TexMatrix");
3914                         p->vp_BackgroundTexMatrix        = cgGetNamedParameter(p->vprogram, "BackgroundTexMatrix");
3915                         p->vp_ModelViewProjectionMatrix  = cgGetNamedParameter(p->vprogram, "ModelViewProjectionMatrix");
3916                         p->vp_ModelViewMatrix            = cgGetNamedParameter(p->vprogram, "ModelViewMatrix");
3917                         CHECKCGERROR
3918                 }
3919         }
3920
3921         // compile the fragment program
3922         if (fragstring[0] && (p->fprogram = cgCreateProgram(vid.cgcontext, CG_SOURCE, fragstring, CG_PROFILE_ARBFP1, NULL, NULL)))
3923         {
3924                 cgCompileProgram(p->fprogram);CHECKCGERROR
3925                 if (!cgIsProgramCompiled(p->fprogram))
3926                 {
3927                         CHECKCGERROR
3928                         cgDestroyProgram(p->fprogram);CHECKCGERROR
3929                         p->fprogram = 0;
3930                 }
3931                 else
3932                 {
3933                         cgGLLoadProgram(p->fprogram);CHECKCGERROR CHECKGLERROR
3934                         cgGLEnableProfile(fragmentProfile);CHECKCGERROR CHECKGLERROR
3935                         CHECKCGERROR
3936                         p->fp_Texture_First              = cgGetNamedParameter(p->fprogram, "Texture_First");
3937                         p->fp_Texture_Second             = cgGetNamedParameter(p->fprogram, "Texture_Second");
3938                         p->fp_Texture_GammaRamps         = cgGetNamedParameter(p->fprogram, "Texture_GammaRamps");
3939                         p->fp_Texture_Normal             = cgGetNamedParameter(p->fprogram, "Texture_Normal");
3940                         p->fp_Texture_Color              = cgGetNamedParameter(p->fprogram, "Texture_Color");
3941                         p->fp_Texture_Gloss              = cgGetNamedParameter(p->fprogram, "Texture_Gloss");
3942                         p->fp_Texture_Glow               = cgGetNamedParameter(p->fprogram, "Texture_Glow");
3943                         p->fp_Texture_SecondaryNormal    = cgGetNamedParameter(p->fprogram, "Texture_SecondaryNormal");
3944                         p->fp_Texture_SecondaryColor     = cgGetNamedParameter(p->fprogram, "Texture_SecondaryColor");
3945                         p->fp_Texture_SecondaryGloss     = cgGetNamedParameter(p->fprogram, "Texture_SecondaryGloss");
3946                         p->fp_Texture_SecondaryGlow      = cgGetNamedParameter(p->fprogram, "Texture_SecondaryGlow");
3947                         p->fp_Texture_Pants              = cgGetNamedParameter(p->fprogram, "Texture_Pants");
3948                         p->fp_Texture_Shirt              = cgGetNamedParameter(p->fprogram, "Texture_Shirt");
3949                         p->fp_Texture_FogMask            = cgGetNamedParameter(p->fprogram, "Texture_FogMask");
3950                         p->fp_Texture_Lightmap           = cgGetNamedParameter(p->fprogram, "Texture_Lightmap");
3951                         p->fp_Texture_Deluxemap          = cgGetNamedParameter(p->fprogram, "Texture_Deluxemap");
3952                         p->fp_Texture_Attenuation        = cgGetNamedParameter(p->fprogram, "Texture_Attenuation");
3953                         p->fp_Texture_Cube               = cgGetNamedParameter(p->fprogram, "Texture_Cube");
3954                         p->fp_Texture_Refraction         = cgGetNamedParameter(p->fprogram, "Texture_Refraction");
3955                         p->fp_Texture_Reflection         = cgGetNamedParameter(p->fprogram, "Texture_Reflection");
3956                         p->fp_Texture_ShadowMapRect      = cgGetNamedParameter(p->fprogram, "Texture_ShadowMapRect");
3957                         p->fp_Texture_ShadowMapCube      = cgGetNamedParameter(p->fprogram, "Texture_ShadowMapCube");
3958                         p->fp_Texture_ShadowMap2D        = cgGetNamedParameter(p->fprogram, "Texture_ShadowMap2D");
3959                         p->fp_Texture_CubeProjection     = cgGetNamedParameter(p->fprogram, "Texture_CubeProjection");
3960                         p->fp_Texture_ScreenDepth      &n