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