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