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