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