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