changed offsetmapping to use forward step math, greatly improved quality
[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 "#ifdef MODE_DEFERREDBOUNCELIGHT\n"
1062 "varying highp vec4 ModelViewPosition;\n"
1063 "varying highp vec4 LightOriginInvRadius;\n"
1064 "varying mediump vec4 LightColor;\n"
1065 "#endif\n"
1066 "\n"
1067 "#ifdef MODE_LIGHTSOURCE\n"
1068 "uniform highp vec3 LightPosition;\n"
1069 "#endif\n"
1070 "uniform highp vec3 EyePosition;\n"
1071 "#ifdef MODE_LIGHTDIRECTION\n"
1072 "uniform highp vec3 LightDir;\n"
1073 "#endif\n"
1074 "uniform highp vec4 FogPlane;\n"
1075 "\n"
1076 "#ifdef USESHADOWMAPORTHO\n"
1077 "varying mediump vec3 ShadowMapTC;\n"
1078 "#endif\n"
1079 "\n"
1080 "#ifdef USEBOUNCEGRID\n"
1081 "varying mediump vec3 BounceGridTexCoord;\n"
1082 "#endif\n"
1083 "\n"
1084 "\n"
1085 "\n"
1086 "\n"
1087 "\n"
1088 "\n"
1089 "// 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"
1090 "\n"
1091 "// fragment shader specific:\n"
1092 "#ifdef FRAGMENT_SHADER\n"
1093 "\n"
1094 "uniform sampler2D Texture_Normal;\n"
1095 "uniform sampler2D Texture_Color;\n"
1096 "uniform sampler2D Texture_Gloss;\n"
1097 "#ifdef USEGLOW\n"
1098 "uniform sampler2D Texture_Glow;\n"
1099 "#endif\n"
1100 "#ifdef USEVERTEXTEXTUREBLEND\n"
1101 "uniform sampler2D Texture_SecondaryNormal;\n"
1102 "uniform sampler2D Texture_SecondaryColor;\n"
1103 "uniform sampler2D Texture_SecondaryGloss;\n"
1104 "#ifdef USEGLOW\n"
1105 "uniform sampler2D Texture_SecondaryGlow;\n"
1106 "#endif\n"
1107 "#endif\n"
1108 "#ifdef USECOLORMAPPING\n"
1109 "uniform sampler2D Texture_Pants;\n"
1110 "uniform sampler2D Texture_Shirt;\n"
1111 "#endif\n"
1112 "#ifdef USEFOG\n"
1113 "#ifdef USEFOGHEIGHTTEXTURE\n"
1114 "uniform sampler2D Texture_FogHeightTexture;\n"
1115 "#endif\n"
1116 "uniform sampler2D Texture_FogMask;\n"
1117 "#endif\n"
1118 "#ifdef USELIGHTMAP\n"
1119 "uniform sampler2D Texture_Lightmap;\n"
1120 "#endif\n"
1121 "#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE)\n"
1122 "uniform sampler2D Texture_Deluxemap;\n"
1123 "#endif\n"
1124 "#ifdef USEREFLECTION\n"
1125 "uniform sampler2D Texture_Reflection;\n"
1126 "#endif\n"
1127 "\n"
1128 "#ifdef MODE_DEFERREDLIGHTSOURCE\n"
1129 "uniform sampler2D Texture_ScreenDepth;\n"
1130 "uniform sampler2D Texture_ScreenNormalMap;\n"
1131 "#endif\n"
1132 "#ifdef USEDEFERREDLIGHTMAP\n"
1133 "uniform sampler2D Texture_ScreenDiffuse;\n"
1134 "uniform sampler2D Texture_ScreenSpecular;\n"
1135 "#endif\n"
1136 "#ifdef MODE_DEFERREDBOUNCELIGHT\n"
1137 "uniform sampler2D Texture_ScreenDepth;\n"
1138 "#endif\n"
1139 "\n"
1140 "uniform lowp vec3 Color_Pants;\n"
1141 "uniform lowp vec3 Color_Shirt;\n"
1142 "uniform lowp vec3 FogColor;\n"
1143 "\n"
1144 "#ifdef USEFOG\n"
1145 "uniform highp float FogRangeRecip;\n"
1146 "uniform highp float FogPlaneViewDist;\n"
1147 "uniform highp float FogHeightFade;\n"
1148 "vec3 FogVertex(vec3 surfacecolor)\n"
1149 "{\n"
1150 "       vec3 EyeVectorModelSpace = EyeVectorModelSpaceFogPlaneVertexDist.xyz;\n"
1151 "       float FogPlaneVertexDist = EyeVectorModelSpaceFogPlaneVertexDist.w;\n"
1152 "       float fogfrac;\n"
1153 "#ifdef USEFOGHEIGHTTEXTURE\n"
1154 "       vec4 fogheightpixel = texture2D(Texture_FogHeightTexture, vec2(1,1) + vec2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade));\n"
1155 "       fogfrac = fogheightpixel.a;\n"
1156 "       return mix(fogheightpixel.rgb * FogColor, surfacecolor, texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r);\n"
1157 "#else\n"
1158 "# ifdef USEFOGOUTSIDE\n"
1159 "       fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade);\n"
1160 "# else\n"
1161 "       fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade);\n"
1162 "# endif\n"
1163 "       return mix(FogColor, surfacecolor, texture2D(Texture_FogMask, myhalf2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r);\n"
1164 "#endif\n"
1165 "}\n"
1166 "#endif\n"
1167 "\n"
1168 "#ifdef USEOFFSETMAPPING\n"
1169 "uniform mediump float OffsetMapping_Scale;\n"
1170 "vec2 OffsetMapping(vec2 TexCoord)\n"
1171 "{\n"
1172 "#ifdef USEOFFSETMAPPING_RELIEFMAPPING\n"
1173 "       // 14 sample relief mapping: linear search and then binary search\n"
1174 "       // this basically steps forward a small amount repeatedly until it finds\n"
1175 "       // itself inside solid, then jitters forward and back using decreasing\n"
1176 "       // amounts to find the impact\n"
1177 "       //vec3 OffsetVector = vec3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1), -1);\n"
1178 "       //vec3 OffsetVector = vec3(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1), -1);\n"
1179 "       vec3 OffsetVector = vec3(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1), -1);\n"
1180 "       vec3 RT = vec3(TexCoord, 1);\n"
1181 "       OffsetVector *= 0.1;\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);\n"
1184 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
1185 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
1186 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
1187 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
1188 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
1189 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
1190 "       RT += OffsetVector *  step(texture2D(Texture_Normal, RT.xy).a, RT.z);\n"
1191 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z)          - 0.5);\n"
1192 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.5    - 0.25);\n"
1193 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.25   - 0.125);\n"
1194 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.125  - 0.0625);\n"
1195 "       RT += OffsetVector * (step(texture2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125);\n"
1196 "       return RT.xy;\n"
1197 "#else\n"
1198 "       // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits)\n"
1199 "       //vec2 OffsetVector = vec2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * vec2(-1, 1));\n"
1200 "       //vec2 OffsetVector = vec2(normalize(EyeVector.xy) * OffsetMapping_Scale * vec2(-1, 1));\n"
1201 "       vec2 OffsetVector = vec2(normalize(EyeVector).xy * OffsetMapping_Scale * vec2(-1, 1));\n"
1202 "       OffsetVector *= 0.5;\n"
1203 "       TexCoord += OffsetVector * (1.0 - texture2D(Texture_Normal, TexCoord).a);\n"
1204 "       TexCoord += OffsetVector * (1.0 - texture2D(Texture_Normal, TexCoord).a);\n"
1205 "       return TexCoord;\n"
1206 "#endif\n"
1207 "}\n"
1208 "#endif // USEOFFSETMAPPING\n"
1209 "\n"
1210 "#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE)\n"
1211 "uniform sampler2D Texture_Attenuation;\n"
1212 "uniform samplerCube Texture_Cube;\n"
1213 "#endif\n"
1214 "\n"
1215 "#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO)\n"
1216 "\n"
1217 "#ifdef USESHADOWMAP2D\n"
1218 "# ifdef USESHADOWSAMPLER\n"
1219 "uniform sampler2DShadow Texture_ShadowMap2D;\n"
1220 "# else\n"
1221 "uniform sampler2D Texture_ShadowMap2D;\n"
1222 "# endif\n"
1223 "#endif\n"
1224 "\n"
1225 "#ifdef USESHADOWMAPVSDCT\n"
1226 "uniform samplerCube Texture_CubeProjection;\n"
1227 "#endif\n"
1228 "\n"
1229 "#if defined(USESHADOWMAP2D)\n"
1230 "uniform mediump vec2 ShadowMap_TextureScale;\n"
1231 "uniform mediump vec4 ShadowMap_Parameters;\n"
1232 "#endif\n"
1233 "\n"
1234 "#if defined(USESHADOWMAP2D)\n"
1235 "# ifdef USESHADOWMAPORTHO\n"
1236 "#  define GetShadowMapTC2D(dir) (min(dir, ShadowMap_Parameters.xyz))\n"
1237 "# else\n"
1238 "#  ifdef USESHADOWMAPVSDCT\n"
1239 "vec3 GetShadowMapTC2D(vec3 dir)\n"
1240 "{\n"
1241 "       vec3 adir = abs(dir);\n"
1242 "       vec2 aparams = ShadowMap_Parameters.xy / max(max(adir.x, adir.y), adir.z);\n"
1243 "       vec4 proj = textureCube(Texture_CubeProjection, dir);\n"
1244 "       return vec3(mix(dir.xy, dir.zz, proj.xy) * aparams.x + proj.zw * ShadowMap_Parameters.z, aparams.y + ShadowMap_Parameters.w);\n"
1245 "}\n"
1246 "#  else\n"
1247 "vec3 GetShadowMapTC2D(vec3 dir)\n"
1248 "{\n"
1249 "       vec3 adir = abs(dir);\n"
1250 "       float ma = adir.z;\n"
1251 "       vec4 proj = vec4(dir, 2.5);\n"
1252 "       if (adir.x > ma) { ma = adir.x; proj = vec4(dir.zyx, 0.5); }\n"
1253 "       if (adir.y > ma) { ma = adir.y; proj = vec4(dir.xzy, 1.5); }\n"
1254 "       vec2 aparams = ShadowMap_Parameters.xy / ma;\n"
1255 "       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"
1256 "}\n"
1257 "#  endif\n"
1258 "# endif\n"
1259 "#endif // defined(USESHADOWMAP2D)\n"
1260 "\n"
1261 "# ifdef USESHADOWMAP2D\n"
1262 "float ShadowMapCompare(vec3 dir)\n"
1263 "{\n"
1264 "       vec3 shadowmaptc = GetShadowMapTC2D(dir);\n"
1265 "       float f;\n"
1266 "\n"
1267 "#  ifdef USESHADOWSAMPLER\n"
1268 "#    ifdef USESHADOWMAPPCF\n"
1269 "#      define texval(x, y) shadow2D(Texture_ShadowMap2D, vec3(center + vec2(x, y)*ShadowMap_TextureScale, shadowmaptc.z)).r  \n"
1270 "       vec2 center = shadowmaptc.xy*ShadowMap_TextureScale;\n"
1271 "       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"
1272 "#    else\n"
1273 "       f = shadow2D(Texture_ShadowMap2D, vec3(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z)).r;\n"
1274 "#    endif\n"
1275 "#  else\n"
1276 "#    ifdef USESHADOWMAPPCF\n"
1277 "#     if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4)\n"
1278 "#      ifdef GL_ARB_texture_gather\n"
1279 "#        define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, ivec2(x, y))\n"
1280 "#      else\n"
1281 "#        define texval(x, y) texture4(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale)\n"
1282 "#      endif\n"
1283 "       vec2 offset = fract(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale;\n"
1284 "#      if USESHADOWMAPPCF > 1\n"
1285 "   vec4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0));\n"
1286 "   vec4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0));\n"
1287 "   vec4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0));\n"
1288 "   vec4 group4 = step(shadowmaptc.z, texval(-2.0,  0.0));\n"
1289 "   vec4 group5 = step(shadowmaptc.z, texval( 0.0,  0.0));\n"
1290 "   vec4 group6 = step(shadowmaptc.z, texval( 2.0,  0.0));\n"
1291 "   vec4 group7 = step(shadowmaptc.z, texval(-2.0,  2.0));\n"
1292 "   vec4 group8 = step(shadowmaptc.z, texval( 0.0,  2.0));\n"
1293 "   vec4 group9 = step(shadowmaptc.z, texval( 2.0,  2.0));\n"
1294 "       vec4 locols = vec4(group1.ab, group3.ab);\n"
1295 "       vec4 hicols = vec4(group7.rg, group9.rg);\n"
1296 "       locols.yz += group2.ab;\n"
1297 "       hicols.yz += group8.rg;\n"
1298 "       vec4 midcols = vec4(group1.rg, group3.rg) + vec4(group7.ab, group9.ab) +\n"
1299 "                               vec4(group4.rg, group6.rg) + vec4(group4.ab, group6.ab) +\n"
1300 "                               mix(locols, hicols, offset.y);\n"
1301 "       vec4 cols = group5 + vec4(group2.rg, group8.ab);\n"
1302 "       cols.xyz += mix(midcols.xyz, midcols.yzw, offset.x);\n"
1303 "       f = dot(cols, vec4(1.0/25.0));\n"
1304 "#      else\n"
1305 "       vec4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0));\n"
1306 "       vec4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0));\n"
1307 "       vec4 group3 = step(shadowmaptc.z, texval(-1.0,  1.0));\n"
1308 "       vec4 group4 = step(shadowmaptc.z, texval( 1.0,  1.0));\n"
1309 "       vec4 cols = vec4(group1.rg, group2.rg) + vec4(group3.ab, group4.ab) +\n"
1310 "                               mix(vec4(group1.ab, group2.ab), vec4(group3.rg, group4.rg), offset.y);\n"
1311 "       f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0));\n"
1312 "#      endif\n"
1313 "#     else\n"
1314 "#      ifdef GL_EXT_gpu_shader4\n"
1315 "#        define texval(x, y) texture2DOffset(Texture_ShadowMap2D, center, ivec2(x, y)).r\n"
1316 "#      else\n"
1317 "#        define texval(x, y) texture2D(Texture_ShadowMap2D, center + vec2(x, y)*ShadowMap_TextureScale).r  \n"
1318 "#      endif\n"
1319 "#      if USESHADOWMAPPCF > 1\n"
1320 "       vec2 center = shadowmaptc.xy - 0.5, offset = fract(center);\n"
1321 "       center *= ShadowMap_TextureScale;\n"
1322 "       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"
1323 "       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"
1324 "       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"
1325 "       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"
1326 "       vec4 cols = row2 + row3 + mix(row1, row4, offset.y);\n"
1327 "       f = dot(mix(cols.xyz, cols.yzw, offset.x), vec3(1.0/9.0));\n"
1328 "#      else\n"
1329 "       vec2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = fract(shadowmaptc.xy);\n"
1330 "       vec3 row1 = step(shadowmaptc.z, vec3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0)));\n"
1331 "       vec3 row2 = step(shadowmaptc.z, vec3(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0)));\n"
1332 "       vec3 row3 = step(shadowmaptc.z, vec3(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0)));\n"
1333 "       vec3 cols = row2 + mix(row1, row3, offset.y);\n"
1334 "       f = dot(mix(cols.xy, cols.yz, offset.x), vec2(0.25));\n"
1335 "#      endif\n"
1336 "#     endif\n"
1337 "#    else\n"
1338 "       f = step(shadowmaptc.z, texture2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r);\n"
1339 "#    endif\n"
1340 "#  endif\n"
1341 "#  ifdef USESHADOWMAPORTHO\n"
1342 "       return mix(ShadowMap_Parameters.w, 1.0, f);\n"
1343 "#  else\n"
1344 "       return f;\n"
1345 "#  endif\n"
1346 "}\n"
1347 "# endif\n"
1348 "#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO)\n"
1349 "#endif // FRAGMENT_SHADER\n"
1350 "\n"
1351 "\n"
1352 "\n"
1353 "\n"
1354 "#ifdef MODE_DEFERREDGEOMETRY\n"
1355 "#ifdef VERTEX_SHADER\n"
1356 "uniform highp mat4 TexMatrix;\n"
1357 "#ifdef USEVERTEXTEXTUREBLEND\n"
1358 "uniform highp mat4 BackgroundTexMatrix;\n"
1359 "#endif\n"
1360 "uniform highp mat4 ModelViewMatrix;\n"
1361 "void main(void)\n"
1362 "{\n"
1363 "       TexCoord = vec2(TexMatrix * Attrib_TexCoord0);\n"
1364 "#ifdef USEVERTEXTEXTUREBLEND\n"
1365 "       VertexColor = Attrib_Color;\n"
1366 "       TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0);\n"
1367 "#endif\n"
1368 "\n"
1369 "       // transform unnormalized eye direction into tangent space\n"
1370 "#ifdef USEOFFSETMAPPING\n"
1371 "       vec3 EyeVectorModelSpace = EyePosition - Attrib_Position.xyz;\n"
1372 "       EyeVector.x = dot(EyeVectorModelSpace, Attrib_TexCoord1.xyz);\n"
1373 "       EyeVector.y = dot(EyeVectorModelSpace, Attrib_TexCoord2.xyz);\n"
1374 "       EyeVector.z = dot(EyeVectorModelSpace, Attrib_TexCoord3.xyz);\n"
1375 "#endif\n"
1376 "\n"
1377 "       VectorS = (ModelViewMatrix * vec4(Attrib_TexCoord1.xyz, 0)).xyz;\n"
1378 "       VectorT = (ModelViewMatrix * vec4(Attrib_TexCoord2.xyz, 0)).xyz;\n"
1379 "       VectorR = (ModelViewMatrix * vec4(Attrib_TexCoord3.xyz, 0)).xyz;\n"
1380 "       gl_Position = ModelViewProjectionMatrix * Attrib_Position;\n"
1381 "}\n"
1382 "#endif // VERTEX_SHADER\n"
1383 "\n"
1384 "#ifdef FRAGMENT_SHADER\n"
1385 "void main(void)\n"
1386 "{\n"
1387 "#ifdef USEOFFSETMAPPING\n"
1388 "       // apply offsetmapping\n"
1389 "       vec2 TexCoordOffset = OffsetMapping(TexCoord);\n"
1390 "#define TexCoord TexCoordOffset\n"
1391 "#endif\n"
1392 "\n"
1393 "#ifdef USEALPHAKILL\n"
1394 "       if (texture2D(Texture_Color, TexCoord).a < 0.5)\n"
1395 "               discard;\n"
1396 "#endif\n"
1397 "\n"
1398 "#ifdef USEVERTEXTEXTUREBLEND\n"
1399 "       float alpha = texture2D(Texture_Color, TexCoord).a;\n"
1400 "       float terrainblend = clamp(float(VertexColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0));\n"
1401 "       //float terrainblend = min(float(VertexColor.a) * alpha * 2.0, float(1.0));\n"
1402 "       //float terrainblend = float(VertexColor.a) * alpha > 0.5;\n"
1403 "#endif\n"
1404 "\n"
1405 "#ifdef USEVERTEXTEXTUREBLEND\n"
1406 "       vec3 surfacenormal = mix(vec3(texture2D(Texture_SecondaryNormal, TexCoord2)), vec3(texture2D(Texture_Normal, TexCoord)), terrainblend) - vec3(0.5, 0.5, 0.5);\n"
1407 "       float a = mix(texture2D(Texture_SecondaryGloss, TexCoord2).a, texture2D(Texture_Gloss, TexCoord).a, terrainblend);\n"
1408 "#else\n"
1409 "       vec3 surfacenormal = vec3(texture2D(Texture_Normal, TexCoord)) - vec3(0.5, 0.5, 0.5);\n"
1410 "       float a = texture2D(Texture_Gloss, TexCoord).a;\n"
1411 "#endif\n"
1412 "\n"
1413 "       gl_FragColor = vec4(normalize(surfacenormal.x * VectorS + surfacenormal.y * VectorT + surfacenormal.z * VectorR) * 0.5 + vec3(0.5, 0.5, 0.5), a);\n"
1414 "}\n"
1415 "#endif // FRAGMENT_SHADER\n"
1416 "#else // !MODE_DEFERREDGEOMETRY\n"
1417 "\n"
1418 "\n"
1419 "\n"
1420 "\n"
1421 "#ifdef MODE_DEFERREDLIGHTSOURCE\n"
1422 "#ifdef VERTEX_SHADER\n"
1423 "uniform highp mat4 ModelViewMatrix;\n"
1424 "void main(void)\n"
1425 "{\n"
1426 "       ModelViewPosition = ModelViewMatrix * Attrib_Position;\n"
1427 "       gl_Position = ModelViewProjectionMatrix * Attrib_Position;\n"
1428 "}\n"
1429 "#endif // VERTEX_SHADER\n"
1430 "\n"
1431 "#ifdef FRAGMENT_SHADER\n"
1432 "uniform highp mat4 ViewToLight;\n"
1433 "// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far));\n"
1434 "uniform highp vec2 ScreenToDepth;\n"
1435 "uniform myhalf3 DeferredColor_Ambient;\n"
1436 "uniform myhalf3 DeferredColor_Diffuse;\n"
1437 "#ifdef USESPECULAR\n"
1438 "uniform myhalf3 DeferredColor_Specular;\n"
1439 "uniform myhalf SpecularPower;\n"
1440 "#endif\n"
1441 "uniform myhalf2 PixelToScreenTexCoord;\n"
1442 "void main(void)\n"
1443 "{\n"
1444 "       // calculate viewspace pixel position\n"
1445 "       vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord;\n"
1446 "       vec3 position;\n"
1447 "       position.z = ScreenToDepth.y / (texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x);\n"
1448 "       position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z);\n"
1449 "       // decode viewspace pixel normal\n"
1450 "       myhalf4 normalmap = texture2D(Texture_ScreenNormalMap, ScreenTexCoord);\n"
1451 "       myhalf3 surfacenormal = normalize(normalmap.rgb - myhalf3(0.5,0.5,0.5));\n"
1452 "       // surfacenormal = pixel normal in viewspace\n"
1453 "       // LightVector = pixel to light in viewspace\n"
1454 "       // CubeVector = position in lightspace\n"
1455 "       // eyevector = pixel to view in viewspace\n"
1456 "       vec3 CubeVector = vec3(ViewToLight * vec4(position,1));\n"
1457 "       myhalf fade = myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0)));\n"
1458 "#ifdef USEDIFFUSE\n"
1459 "       // calculate diffuse shading\n"
1460 "       myhalf3 lightnormal = myhalf3(normalize(LightPosition - position));\n"
1461 "       myhalf diffuse = myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0));\n"
1462 "#endif\n"
1463 "#ifdef USESPECULAR\n"
1464 "       // calculate directional shading\n"
1465 "       vec3 eyevector = position * -1.0;\n"
1466 "#  ifdef USEEXACTSPECULARMATH\n"
1467 "       myhalf specular = pow(myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), SpecularPower * normalmap.a);\n"
1468 "#  else\n"
1469 "       myhalf3 specularnormal = normalize(lightnormal + myhalf3(normalize(eyevector)));\n"
1470 "       myhalf specular = pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower * normalmap.a);\n"
1471 "#  endif\n"
1472 "#endif\n"
1473 "\n"
1474 "#if defined(USESHADOWMAP2D)\n"
1475 "       fade *= ShadowMapCompare(CubeVector);\n"
1476 "#endif\n"
1477 "\n"
1478 "#ifdef USEDIFFUSE\n"
1479 "       gl_FragData[0] = vec4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0);\n"
1480 "#else\n"
1481 "       gl_FragData[0] = vec4(DeferredColor_Ambient * fade, 1.0);\n"
1482 "#endif\n"
1483 "#ifdef USESPECULAR\n"
1484 "       gl_FragData[1] = vec4(DeferredColor_Specular * (specular * fade), 1.0);\n"
1485 "#else\n"
1486 "       gl_FragData[1] = vec4(0.0, 0.0, 0.0, 1.0);\n"
1487 "#endif\n"
1488 "\n"
1489 "# ifdef USECUBEFILTER\n"
1490 "       vec3 cubecolor = textureCube(Texture_Cube, CubeVector).rgb;\n"
1491 "       gl_FragData[0].rgb *= cubecolor;\n"
1492 "       gl_FragData[1].rgb *= cubecolor;\n"
1493 "# endif\n"
1494 "}\n"
1495 "#endif // FRAGMENT_SHADER\n"
1496 "#else // !MODE_DEFERREDLIGHTSOURCE\n"
1497 "\n"
1498 "\n"
1499 "\n"
1500 "\n"
1501 "#ifdef MODE_DEFERREDBOUNCELIGHT\n"
1502 "#ifdef VERTEX_SHADER\n"
1503 "uniform highp mat4 ModelViewMatrix;\n"
1504 "void main(void)\n"
1505 "{\n"
1506 "       ModelViewPosition = ModelViewMatrix * Attrib_Position;\n"
1507 "       LightOriginInvRadius.xyz = (ModelViewMatrix * vec4(Attrib_TexCoord0.xyz, 1.0)).xyz;\n"
1508 "       LightOriginInvRadius.w = Attrib_TexCoord0.w;\n"
1509 "       LightColor = Attrib_Color;\n"
1510 "       gl_Position = ModelViewProjectionMatrix * Attrib_Position;\n"
1511 "}\n"
1512 "#endif // VERTEX_SHADER\n"
1513 "\n"
1514 "#ifdef FRAGMENT_SHADER\n"
1515 "// ScreenToDepth = vec2(Far / (Far - Near), Far * Near / (Near - Far));\n"
1516 "uniform highp vec2 ScreenToDepth;\n"
1517 "uniform myhalf2 PixelToScreenTexCoord;\n"
1518 "void main(void)\n"
1519 "{\n"
1520 "       // calculate viewspace pixel position\n"
1521 "       vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord;\n"
1522 "       vec3 position;\n"
1523 "       position.z = ScreenToDepth.y / (texture2D(Texture_ScreenDepth, ScreenTexCoord).r + ScreenToDepth.x);\n"
1524 "       position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z);\n"
1525 "       vec3 CubeVector = (position - LightOriginInvRadius.xyz) * LightOriginInvRadius.w;\n"
1526 "       gl_FragData[0] = vec4(LightColor.rgb * max(0.0, 1.0 - length(CubeVector)), 1.0);\n"
1527 "}\n"
1528 "#endif // FRAGMENT_SHADER\n"
1529 "#else // !MODE_DEFERREDBOUNCELIGHT\n"
1530 "\n"
1531 "\n"
1532 "\n"
1533 "\n"
1534 "#ifdef VERTEX_SHADER\n"
1535 "uniform highp mat4 TexMatrix;\n"
1536 "#ifdef USEVERTEXTEXTUREBLEND\n"
1537 "uniform highp mat4 BackgroundTexMatrix;\n"
1538 "#endif\n"
1539 "#ifdef MODE_LIGHTSOURCE\n"
1540 "uniform highp mat4 ModelToLight;\n"
1541 "#endif\n"
1542 "#ifdef USESHADOWMAPORTHO\n"
1543 "uniform highp mat4 ShadowMapMatrix;\n"
1544 "#endif\n"
1545 "#ifdef USEBOUNCEGRID\n"
1546 "uniform highp mat4 BounceGridMatrix;\n"
1547 "#endif\n"
1548 "void main(void)\n"
1549 "{\n"
1550 "#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND)\n"
1551 "       VertexColor = Attrib_Color;\n"
1552 "#endif\n"
1553 "       // copy the surface texcoord\n"
1554 "       TexCoord = vec2(TexMatrix * Attrib_TexCoord0);\n"
1555 "#ifdef USEVERTEXTEXTUREBLEND\n"
1556 "       TexCoord2 = vec2(BackgroundTexMatrix * Attrib_TexCoord0);\n"
1557 "#endif\n"
1558 "#ifdef USELIGHTMAP\n"
1559 "       TexCoordLightmap = vec2(Attrib_TexCoord4);\n"
1560 "#endif\n"
1561 "\n"
1562 "#ifdef USEBOUNCEGRID\n"
1563 "       BounceGridTexCoord = vec3(BounceGridMatrix * Attrib_Position);\n"
1564 "#endif\n"
1565 "\n"
1566 "#ifdef MODE_LIGHTSOURCE\n"
1567 "       // transform vertex position into light attenuation/cubemap space\n"
1568 "       // (-1 to +1 across the light box)\n"
1569 "       CubeVector = vec3(ModelToLight * Attrib_Position);\n"
1570 "\n"
1571 "# ifdef USEDIFFUSE\n"
1572 "       // transform unnormalized light direction into tangent space\n"
1573 "       // (we use unnormalized to ensure that it interpolates correctly and then\n"
1574 "       //  normalize it per pixel)\n"
1575 "       vec3 lightminusvertex = LightPosition - Attrib_Position.xyz;\n"
1576 "       LightVector.x = dot(lightminusvertex, Attrib_TexCoord1.xyz);\n"
1577 "       LightVector.y = dot(lightminusvertex, Attrib_TexCoord2.xyz);\n"
1578 "       LightVector.z = dot(lightminusvertex, Attrib_TexCoord3.xyz);\n"
1579 "# endif\n"
1580 "#endif\n"
1581 "\n"
1582 "#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE)\n"
1583 "       LightVector.x = dot(LightDir, Attrib_TexCoord1.xyz);\n"
1584 "       LightVector.y = dot(LightDir, Attrib_TexCoord2.xyz);\n"
1585 "       LightVector.z = dot(LightDir, Attrib_TexCoord3.xyz);\n"
1586 "#endif\n"
1587 "\n"
1588 "       // transform unnormalized eye direction into tangent space\n"
1589 "#ifdef USEEYEVECTOR\n"
1590 "       vec3 EyeVectorModelSpace = EyePosition - Attrib_Position.xyz;\n"
1591 "       EyeVector.x = dot(EyeVectorModelSpace, Attrib_TexCoord1.xyz);\n"
1592 "       EyeVector.y = dot(EyeVectorModelSpace, Attrib_TexCoord2.xyz);\n"
1593 "       EyeVector.z = dot(EyeVectorModelSpace, Attrib_TexCoord3.xyz);\n"
1594 "#endif\n"
1595 "\n"
1596 "#ifdef USEFOG\n"
1597 "       EyeVectorModelSpaceFogPlaneVertexDist.xyz = EyePosition - Attrib_Position.xyz;\n"
1598 "       EyeVectorModelSpaceFogPlaneVertexDist.w = dot(FogPlane, Attrib_Position);\n"
1599 "#endif\n"
1600 "\n"
1601 "#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(USEREFLECTCUBE)\n"
1602 "       VectorS = Attrib_TexCoord1.xyz;\n"
1603 "       VectorT = Attrib_TexCoord2.xyz;\n"
1604 "       VectorR = Attrib_TexCoord3.xyz;\n"
1605 "#endif\n"
1606 "\n"
1607 "       // transform vertex to camera space, using ftransform to match non-VS rendering\n"
1608 "       gl_Position = ModelViewProjectionMatrix * Attrib_Position;\n"
1609 "\n"
1610 "#ifdef USESHADOWMAPORTHO\n"
1611 "       ShadowMapTC = vec3(ShadowMapMatrix * gl_Position);\n"
1612 "#endif\n"
1613 "\n"
1614 "#ifdef USEREFLECTION\n"
1615 "       ModelViewProjectionPosition = gl_Position;\n"
1616 "#endif\n"
1617 "}\n"
1618 "#endif // VERTEX_SHADER\n"
1619 "\n"
1620 "\n"
1621 "\n"
1622 "\n"
1623 "#ifdef FRAGMENT_SHADER\n"
1624 "#ifdef USEDEFERREDLIGHTMAP\n"
1625 "uniform myhalf2 PixelToScreenTexCoord;\n"
1626 "uniform myhalf3 DeferredMod_Diffuse;\n"
1627 "uniform myhalf3 DeferredMod_Specular;\n"
1628 "#endif\n"
1629 "uniform myhalf3 Color_Ambient;\n"
1630 "uniform myhalf3 Color_Diffuse;\n"
1631 "uniform myhalf3 Color_Specular;\n"
1632 "uniform myhalf SpecularPower;\n"
1633 "#ifdef USEGLOW\n"
1634 "uniform myhalf3 Color_Glow;\n"
1635 "#endif\n"
1636 "uniform myhalf Alpha;\n"
1637 "#ifdef USEREFLECTION\n"
1638 "uniform mediump vec4 DistortScaleRefractReflect;\n"
1639 "uniform mediump vec4 ScreenScaleRefractReflect;\n"
1640 "uniform mediump vec4 ScreenCenterRefractReflect;\n"
1641 "uniform lowp vec4 ReflectColor;\n"
1642 "#endif\n"
1643 "#ifdef USEREFLECTCUBE\n"
1644 "uniform highp mat4 ModelToReflectCube;\n"
1645 "uniform sampler2D Texture_ReflectMask;\n"
1646 "uniform samplerCube Texture_ReflectCube;\n"
1647 "#endif\n"
1648 "#ifdef MODE_LIGHTDIRECTION\n"
1649 "uniform myhalf3 LightColor;\n"
1650 "#endif\n"
1651 "#ifdef MODE_LIGHTSOURCE\n"
1652 "uniform myhalf3 LightColor;\n"
1653 "#endif\n"
1654 "#ifdef USEBOUNCEGRID\n"
1655 "uniform sampler3D Texture_BounceGrid;\n"
1656 "uniform float BounceGridIntensity;\n"
1657 "#endif\n"
1658 "void main(void)\n"
1659 "{\n"
1660 "#ifdef USEOFFSETMAPPING\n"
1661 "       // apply offsetmapping\n"
1662 "       vec2 TexCoordOffset = OffsetMapping(TexCoord);\n"
1663 "#define TexCoord TexCoordOffset\n"
1664 "#endif\n"
1665 "\n"
1666 "       // combine the diffuse textures (base, pants, shirt)\n"
1667 "       myhalf4 color = myhalf4(texture2D(Texture_Color, TexCoord));\n"
1668 "#ifdef USEALPHAKILL\n"
1669 "       if (color.a < 0.5)\n"
1670 "               discard;\n"
1671 "#endif\n"
1672 "       color.a *= Alpha;\n"
1673 "#ifdef USECOLORMAPPING\n"
1674 "       color.rgb += myhalf3(texture2D(Texture_Pants, TexCoord)) * Color_Pants + myhalf3(texture2D(Texture_Shirt, TexCoord)) * Color_Shirt;\n"
1675 "#endif\n"
1676 "#ifdef USEVERTEXTEXTUREBLEND\n"
1677 "       myhalf terrainblend = clamp(myhalf(VertexColor.a) * color.a * 2.0 - 0.5, myhalf(0.0), myhalf(1.0));\n"
1678 "       //myhalf terrainblend = min(myhalf(VertexColor.a) * color.a * 2.0, myhalf(1.0));\n"
1679 "       //myhalf terrainblend = myhalf(VertexColor.a) * color.a > 0.5;\n"
1680 "       color.rgb = mix(myhalf3(texture2D(Texture_SecondaryColor, TexCoord2)), color.rgb, terrainblend);\n"
1681 "       color.a = 1.0;\n"
1682 "       //color = mix(myhalf4(1, 0, 0, 1), color, terrainblend);\n"
1683 "#endif\n"
1684 "\n"
1685 "       // get the surface normal\n"
1686 "#ifdef USEVERTEXTEXTUREBLEND\n"
1687 "       myhalf3 surfacenormal = normalize(mix(myhalf3(texture2D(Texture_SecondaryNormal, TexCoord2)), myhalf3(texture2D(Texture_Normal, TexCoord)), terrainblend) - myhalf3(0.5, 0.5, 0.5));\n"
1688 "#else\n"
1689 "       myhalf3 surfacenormal = normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5, 0.5, 0.5));\n"
1690 "#endif\n"
1691 "\n"
1692 "       // get the material colors\n"
1693 "       myhalf3 diffusetex = color.rgb;\n"
1694 "#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP)\n"
1695 "# ifdef USEVERTEXTEXTUREBLEND\n"
1696 "       myhalf4 glosstex = mix(myhalf4(texture2D(Texture_SecondaryGloss, TexCoord2)), myhalf4(texture2D(Texture_Gloss, TexCoord)), terrainblend);\n"
1697 "# else\n"
1698 "       myhalf4 glosstex = myhalf4(texture2D(Texture_Gloss, TexCoord));\n"
1699 "# endif\n"
1700 "#endif\n"
1701 "\n"
1702 "#ifdef USEREFLECTCUBE\n"
1703 "       vec3 TangentReflectVector = reflect(-EyeVector, surfacenormal);\n"
1704 "       vec3 ModelReflectVector = TangentReflectVector.x * VectorS + TangentReflectVector.y * VectorT + TangentReflectVector.z * VectorR;\n"
1705 "       vec3 ReflectCubeTexCoord = vec3(ModelToReflectCube * vec4(ModelReflectVector, 0));\n"
1706 "       diffusetex += myhalf3(texture2D(Texture_ReflectMask, TexCoord)) * myhalf3(textureCube(Texture_ReflectCube, ReflectCubeTexCoord));\n"
1707 "#endif\n"
1708 "\n"
1709 "\n"
1710 "\n"
1711 "\n"
1712 "#ifdef MODE_LIGHTSOURCE\n"
1713 "       // light source\n"
1714 "#ifdef USEDIFFUSE\n"
1715 "       myhalf3 lightnormal = myhalf3(normalize(LightVector));\n"
1716 "       myhalf diffuse = myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0));\n"
1717 "       color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse);\n"
1718 "#ifdef USESPECULAR\n"
1719 "#ifdef USEEXACTSPECULARMATH\n"
1720 "       myhalf specular = pow(myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower * glosstex.a);\n"
1721 "#else\n"
1722 "       myhalf3 specularnormal = normalize(lightnormal + myhalf3(normalize(EyeVector)));\n"
1723 "       myhalf specular = pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower * glosstex.a);\n"
1724 "#endif\n"
1725 "       color.rgb += glosstex.rgb * (specular * Color_Specular);\n"
1726 "#endif\n"
1727 "#else\n"
1728 "       color.rgb = diffusetex * Color_Ambient;\n"
1729 "#endif\n"
1730 "       color.rgb *= LightColor;\n"
1731 "       color.rgb *= myhalf(texture2D(Texture_Attenuation, vec2(length(CubeVector), 0.0)));\n"
1732 "#if defined(USESHADOWMAP2D)\n"
1733 "       color.rgb *= ShadowMapCompare(CubeVector);\n"
1734 "#endif\n"
1735 "# ifdef USECUBEFILTER\n"
1736 "       color.rgb *= myhalf3(textureCube(Texture_Cube, CubeVector));\n"
1737 "# endif\n"
1738 "#endif // MODE_LIGHTSOURCE\n"
1739 "\n"
1740 "\n"
1741 "\n"
1742 "\n"
1743 "#ifdef MODE_LIGHTDIRECTION\n"
1744 "#define SHADING\n"
1745 "#ifdef USEDIFFUSE\n"
1746 "       myhalf3 lightnormal = myhalf3(normalize(LightVector));\n"
1747 "#endif\n"
1748 "#define lightcolor LightColor\n"
1749 "#endif // MODE_LIGHTDIRECTION\n"
1750 "#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
1751 "#define SHADING\n"
1752 "       // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe)\n"
1753 "       myhalf3 lightnormal_modelspace = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0);\n"
1754 "       myhalf3 lightcolor = myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap));\n"
1755 "       // convert modelspace light vector to tangentspace\n"
1756 "       myhalf3 lightnormal;\n"
1757 "       lightnormal.x = dot(lightnormal_modelspace, myhalf3(VectorS));\n"
1758 "       lightnormal.y = dot(lightnormal_modelspace, myhalf3(VectorT));\n"
1759 "       lightnormal.z = dot(lightnormal_modelspace, myhalf3(VectorR));\n"
1760 "       lightnormal = normalize(lightnormal); // VectorS/T/R are not always perfectly normalized, and EXACTSPECULARMATH is very picky about this\n"
1761 "       // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division)\n"
1762 "       // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar\n"
1763 "       // is used (the lightmap and deluxemap coords correspond to virtually random coordinates\n"
1764 "       // on that luxel, and NOT to its center, because recursive triangle subdivision is used\n"
1765 "       // to map the luxels to coordinates on the draw surfaces), which also causes\n"
1766 "       // deluxemaps to be wrong because light contributions from the wrong side of the surface\n"
1767 "       // are added up. To prevent divisions by zero or strong exaggerations, a max()\n"
1768 "       // nudge is done here at expense of some additional fps. This is ONLY needed for\n"
1769 "       // deluxemaps, tangentspace deluxemap avoid this problem by design.\n"
1770 "       lightcolor *= 1.0 / max(0.25, lightnormal.z);\n"
1771 "#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
1772 "#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n"
1773 "#define SHADING\n"
1774 "       // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light)\n"
1775 "       myhalf3 lightnormal = myhalf3(texture2D(Texture_Deluxemap, TexCoordLightmap)) * 2.0 + myhalf3(-1.0, -1.0, -1.0);\n"
1776 "       myhalf3 lightcolor = myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap));\n"
1777 "#endif\n"
1778 "\n"
1779 "\n"
1780 "\n"
1781 "\n"
1782 "#ifdef MODE_FAKELIGHT\n"
1783 "#define SHADING\n"
1784 "myhalf3 lightnormal = myhalf3(normalize(EyeVector));\n"
1785 "myhalf3 lightcolor = myhalf3(1.0);\n"
1786 "#endif // MODE_FAKELIGHT\n"
1787 "\n"
1788 "\n"
1789 "\n"
1790 "\n"
1791 "#ifdef MODE_LIGHTMAP\n"
1792 "       color.rgb = diffusetex * (Color_Ambient + myhalf3(texture2D(Texture_Lightmap, TexCoordLightmap)) * Color_Diffuse);\n"
1793 "#endif // MODE_LIGHTMAP\n"
1794 "#ifdef MODE_VERTEXCOLOR\n"
1795 "       color.rgb = diffusetex * (Color_Ambient + myhalf3(VertexColor.rgb) * Color_Diffuse);\n"
1796 "#endif // MODE_VERTEXCOLOR\n"
1797 "#ifdef MODE_FLATCOLOR\n"
1798 "       color.rgb = diffusetex * Color_Ambient;\n"
1799 "#endif // MODE_FLATCOLOR\n"
1800 "\n"
1801 "\n"
1802 "\n"
1803 "\n"
1804 "#ifdef SHADING\n"
1805 "# ifdef USEDIFFUSE\n"
1806 "       myhalf diffuse = myhalf(max(float(dot(surfacenormal, lightnormal)), 0.0));\n"
1807 "#  ifdef USESPECULAR\n"
1808 "#   ifdef USEEXACTSPECULARMATH\n"
1809 "       myhalf specular = pow(myhalf(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower * glosstex.a);\n"
1810 "#   else\n"
1811 "       myhalf3 specularnormal = normalize(lightnormal + myhalf3(normalize(EyeVector)));\n"
1812 "       myhalf specular = pow(myhalf(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower * glosstex.a);\n"
1813 "#   endif\n"
1814 "       color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor;\n"
1815 "#  else\n"
1816 "       color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor);\n"
1817 "#  endif\n"
1818 "# else\n"
1819 "       color.rgb = diffusetex * Color_Ambient;\n"
1820 "# endif\n"
1821 "#endif\n"
1822 "\n"
1823 "#ifdef USESHADOWMAPORTHO\n"
1824 "       color.rgb *= ShadowMapCompare(ShadowMapTC);\n"
1825 "#endif\n"
1826 "\n"
1827 "#ifdef USEDEFERREDLIGHTMAP\n"
1828 "       vec2 ScreenTexCoord = gl_FragCoord.xy * PixelToScreenTexCoord;\n"
1829 "       color.rgb += diffusetex * myhalf3(texture2D(Texture_ScreenDiffuse, ScreenTexCoord)) * DeferredMod_Diffuse;\n"
1830 "       color.rgb += glosstex.rgb * myhalf3(texture2D(Texture_ScreenSpecular, ScreenTexCoord)) * DeferredMod_Specular;\n"
1831 "#endif\n"
1832 "\n"
1833 "#ifdef USEBOUNCEGRID\n"
1834 "       color.rgb += diffusetex * myhalf3(texture3D(Texture_BounceGrid, BounceGridTexCoord)) * BounceGridIntensity;\n"
1835 "#endif\n"
1836 "\n"
1837 "#ifdef USEGLOW\n"
1838 "#ifdef USEVERTEXTEXTUREBLEND\n"
1839 "       color.rgb += mix(myhalf3(texture2D(Texture_SecondaryGlow, TexCoord2)), myhalf3(texture2D(Texture_Glow, TexCoord)), terrainblend) * Color_Glow;\n"
1840 "#else\n"
1841 "       color.rgb += myhalf3(texture2D(Texture_Glow, TexCoord)) * Color_Glow;\n"
1842 "#endif\n"
1843 "#endif\n"
1844 "\n"
1845 "#ifdef USEFOG\n"
1846 "       color.rgb = FogVertex(color.rgb);\n"
1847 "#endif\n"
1848 "\n"
1849 "       // 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"
1850 "#ifdef USEREFLECTION\n"
1851 "       vec4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);\n"
1852 "       //vec4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
1853 "       vec2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw;\n"
1854 "       vec2 ScreenTexCoord = SafeScreenTexCoord + vec3(normalize(myhalf3(texture2D(Texture_Normal, TexCoord)) - myhalf3(0.5))).xy * DistortScaleRefractReflect.zw;\n"
1855 "       // FIXME temporary hack to detect the case that the reflection\n"
1856 "       // gets blackened at edges due to leaving the area that contains actual\n"
1857 "       // content.\n"
1858 "       // Remove this 'ack once we have a better way to stop this thing from\n"
1859 "       // 'appening.\n"
1860 "       float f = min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, 0.01)).rgb) / 0.05);\n"
1861 "       f      *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(0.01, -0.01)).rgb) / 0.05);\n"
1862 "       f      *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, 0.01)).rgb) / 0.05);\n"
1863 "       f      *= min(1.0, length(texture2D(Texture_Reflection, ScreenTexCoord + vec2(-0.01, -0.01)).rgb) / 0.05);\n"
1864 "       ScreenTexCoord = mix(SafeScreenTexCoord, ScreenTexCoord, f);\n"
1865 "       color.rgb = mix(color.rgb, myhalf3(texture2D(Texture_Reflection, ScreenTexCoord)) * ReflectColor.rgb, ReflectColor.a);\n"
1866 "#endif\n"
1867 "\n"
1868 "       gl_FragColor = vec4(color);\n"
1869 "}\n"
1870 "#endif // FRAGMENT_SHADER\n"
1871 "\n"
1872 "#endif // !MODE_DEFERREDBOUNCELIGHT\n"
1873 "#endif // !MODE_DEFERREDLIGHTSOURCE\n"
1874 "#endif // !MODE_DEFERREDGEOMETRY\n"
1875 "#endif // !MODE_WATER\n"
1876 "#endif // !MODE_REFRACTION\n"
1877 "#endif // !MODE_BLOOMBLUR\n"
1878 "#endif // !MODE_GENERIC\n"
1879 "#endif // !MODE_POSTPROCESS\n"
1880 "#endif // !MODE_SHOWDEPTH\n"
1881 "#endif // !MODE_DEPTH_OR_SHADOW\n"
1882 ;
1883
1884 /*
1885 =========================================================================================================================================================
1886
1887
1888
1889 =========================================================================================================================================================
1890
1891
1892
1893 =========================================================================================================================================================
1894
1895
1896
1897 =========================================================================================================================================================
1898
1899
1900
1901 =========================================================================================================================================================
1902
1903
1904
1905 =========================================================================================================================================================
1906
1907
1908
1909 =========================================================================================================================================================
1910 */
1911
1912 const char *builtinhlslshaderstring =
1913 "// ambient+diffuse+specular+normalmap+attenuation+cubemap+fog shader\n"
1914 "// written by Forest 'LordHavoc' Hale\n"
1915 "// shadowmapping enhancements by Lee 'eihrul' Salzman\n"
1916 "\n"
1917 "// FIXME: we need to get rid of ModelViewProjectionPosition to make room for the texcoord for this\n"
1918 "#if defined(USEREFLECTION)\n"
1919 "#undef USESHADOWMAPORTHO\n"
1920 "#endif\n"
1921 "\n"
1922 "#if defined(USEFOGINSIDE) || defined(USEFOGOUTSIDE) || defined(USEFOGHEIGHTTEXTURE)\n"
1923 "# define USEFOG\n"
1924 "#endif\n"
1925 "#if defined(MODE_LIGHTMAP) || defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE)\n"
1926 "#define USELIGHTMAP\n"
1927 "#endif\n"
1928 "#if defined(USESPECULAR) || defined(USEOFFSETMAPPING) || defined(USEREFLECTCUBE) || defined(MODE_FAKELIGHT)\n"
1929 "#define USEEYEVECTOR\n"
1930 "#endif\n"
1931 "\n"
1932 "#ifdef FRAGMENT_SHADER\n"
1933 "#ifdef HLSL\n"
1934 "//#undef USESHADOWMAPPCF\n"
1935 "//#define texDepth2D(tex,texcoord) tex2D(tex,texcoord).r\n"
1936 "#define texDepth2D(tex,texcoord) dot(tex2D(tex,texcoord).rgb, float3(1.0, 255.0/65536.0, 255.0/16777216.0))\n"
1937 "#else\n"
1938 "#define texDepth2D(tex,texcoord) tex2D(tex,texcoord).r\n"
1939 "#endif\n"
1940 "#endif\n"
1941 "\n"
1942 "#ifdef MODE_DEPTH_OR_SHADOW\n"
1943 "#ifdef VERTEX_SHADER\n"
1944 "void main\n"
1945 "(\n"
1946 "float4 gl_Vertex : POSITION,\n"
1947 "uniform float4x4 ModelViewProjectionMatrix : register(c8),\n"
1948 "out float4 gl_Position : POSITION,\n"
1949 "out float Depth : TEXCOORD0\n"
1950 ")\n"
1951 "{\n"
1952 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
1953 "       Depth = gl_Position.z;\n"
1954 "}\n"
1955 "#endif\n"
1956 "\n"
1957 "#ifdef FRAGMENT_SHADER\n"
1958 "void main\n"
1959 "(\n"
1960 "float Depth : TEXCOORD0,\n"
1961 "out float4 gl_FragColor : COLOR\n"
1962 ")\n"
1963 "{\n"
1964 "//     float4 temp = float4(Depth,Depth*(65536.0/255.0),Depth*(16777216.0/255.0),0.0);\n"
1965 "       float4 temp = float4(Depth,Depth*256.0,Depth*65536.0,0.0);\n"
1966 "       temp.yz -= floor(temp.yz);\n"
1967 "       gl_FragColor = temp;\n"
1968 "//     gl_FragColor = float4(Depth,0,0,0);\n"
1969 "}\n"
1970 "#endif\n"
1971 "#else // !MODE_DEPTH_ORSHADOW\n"
1972 "\n"
1973 "\n"
1974 "\n"
1975 "\n"
1976 "#ifdef MODE_SHOWDEPTH\n"
1977 "#ifdef VERTEX_SHADER\n"
1978 "void main\n"
1979 "(\n"
1980 "float4 gl_Vertex : POSITION,\n"
1981 "uniform float4x4 ModelViewProjectionMatrix : register(c8),\n"
1982 "out float4 gl_Position : POSITION,\n"
1983 "out float4 gl_FrontColor : COLOR0\n"
1984 ")\n"
1985 "{\n"
1986 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
1987 "       gl_FrontColor = float4(gl_Position.z, gl_Position.z, gl_Position.z, 1.0);\n"
1988 "}\n"
1989 "#endif\n"
1990 "\n"
1991 "#ifdef FRAGMENT_SHADER\n"
1992 "void main\n"
1993 "(\n"
1994 "float4 gl_FrontColor : COLOR0,\n"
1995 "out float4 gl_FragColor : COLOR\n"
1996 ")\n"
1997 "{\n"
1998 "       gl_FragColor = gl_FrontColor;\n"
1999 "}\n"
2000 "#endif\n"
2001 "#else // !MODE_SHOWDEPTH\n"
2002 "\n"
2003 "\n"
2004 "\n"
2005 "\n"
2006 "#ifdef MODE_POSTPROCESS\n"
2007 "\n"
2008 "#ifdef VERTEX_SHADER\n"
2009 "void main\n"
2010 "(\n"
2011 "float4 gl_Vertex : POSITION,\n"
2012 "uniform float4x4 ModelViewProjectionMatrix : register(c8),\n"
2013 "float4 gl_MultiTexCoord0 : TEXCOORD0,\n"
2014 "float4 gl_MultiTexCoord4 : TEXCOORD4,\n"
2015 "out float4 gl_Position : POSITION,\n"
2016 "out float2 TexCoord1 : TEXCOORD0,\n"
2017 "out float2 TexCoord2 : TEXCOORD1\n"
2018 ")\n"
2019 "{\n"
2020 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
2021 "       TexCoord1 = gl_MultiTexCoord0.xy;\n"
2022 "#ifdef USEBLOOM\n"
2023 "       TexCoord2 = gl_MultiTexCoord4.xy;\n"
2024 "#endif\n"
2025 "}\n"
2026 "#endif\n"
2027 "\n"
2028 "#ifdef FRAGMENT_SHADER\n"
2029 "void main\n"
2030 "(\n"
2031 "float2 TexCoord1 : TEXCOORD0,\n"
2032 "float2 TexCoord2 : TEXCOORD1,\n"
2033 "uniform sampler Texture_First : register(s0),\n"
2034 "#ifdef USEBLOOM\n"
2035 "uniform sampler Texture_Second : register(s1),\n"
2036 "#endif\n"
2037 "#ifdef USEGAMMARAMPS\n"
2038 "uniform sampler Texture_GammaRamps : register(s2),\n"
2039 "#endif\n"
2040 "#ifdef USESATURATION\n"
2041 "uniform float Saturation : register(c30),\n"
2042 "#endif\n"
2043 "#ifdef USEVIEWTINT\n"
2044 "uniform float4 ViewTintColor : register(c41),\n"
2045 "#endif\n"
2046 "uniform float4 UserVec1 : register(c37),\n"
2047 "uniform float4 UserVec2 : register(c38),\n"
2048 "uniform float4 UserVec3 : register(c39),\n"
2049 "uniform float4 UserVec4 : register(c40),\n"
2050 "uniform float ClientTime : register(c2),\n"
2051 "uniform float2 PixelSize : register(c25),\n"
2052 "uniform float4 BloomColorSubtract : register(c43),\n"
2053 "out float4 gl_FragColor : COLOR\n"
2054 ")\n"
2055 "{\n"
2056 "       gl_FragColor = tex2D(Texture_First, TexCoord1);\n"
2057 "#ifdef USEBLOOM\n"
2058 "       gl_FragColor += max(float4(0,0,0,0), tex2D(Texture_Second, TexCoord2) - BloomColorSubtract);\n"
2059 "#endif\n"
2060 "#ifdef USEVIEWTINT\n"
2061 "       gl_FragColor = lerp(gl_FragColor, ViewTintColor, ViewTintColor.a);\n"
2062 "#endif\n"
2063 "\n"
2064 "#ifdef USEPOSTPROCESSING\n"
2065 "// do r_glsl_dumpshader, edit glsl/default.glsl, and replace this by your own postprocessing if you want\n"
2066 "// 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"
2067 "       float sobel = 1.0;\n"
2068 "       // float2 ts = textureSize(Texture_First, 0);\n"
2069 "       // float2 px = float2(1/ts.x, 1/ts.y);\n"
2070 "       float2 px = PixelSize;\n"
2071 "       float3 x1 = tex2D(Texture_First, TexCoord1 + float2(-px.x, px.y)).rgb;\n"
2072 "       float3 x2 = tex2D(Texture_First, TexCoord1 + float2(-px.x,  0.0)).rgb;\n"
2073 "       float3 x3 = tex2D(Texture_First, TexCoord1 + float2(-px.x,-px.y)).rgb;\n"
2074 "       float3 x4 = tex2D(Texture_First, TexCoord1 + float2( px.x, px.y)).rgb;\n"
2075 "       float3 x5 = tex2D(Texture_First, TexCoord1 + float2( px.x,  0.0)).rgb;\n"
2076 "       float3 x6 = tex2D(Texture_First, TexCoord1 + float2( px.x,-px.y)).rgb;\n"
2077 "       float3 y1 = tex2D(Texture_First, TexCoord1 + float2( px.x,-px.y)).rgb;\n"
2078 "       float3 y2 = tex2D(Texture_First, TexCoord1 + float2(  0.0,-px.y)).rgb;\n"
2079 "       float3 y3 = tex2D(Texture_First, TexCoord1 + float2(-px.x,-px.y)).rgb;\n"
2080 "       float3 y4 = tex2D(Texture_First, TexCoord1 + float2( px.x, px.y)).rgb;\n"
2081 "       float3 y5 = tex2D(Texture_First, TexCoord1 + float2(  0.0, px.y)).rgb;\n"
2082 "       float3 y6 = tex2D(Texture_First, TexCoord1 + float2(-px.x, px.y)).rgb;\n"
2083 "       float px1 = -1.0 * dot(float3(0.3, 0.59, 0.11), x1);\n"
2084 "       float px2 = -2.0 * dot(float3(0.3, 0.59, 0.11), x2);\n"
2085 "       float px3 = -1.0 * dot(float3(0.3, 0.59, 0.11), x3);\n"
2086 "       float px4 =  1.0 * dot(float3(0.3, 0.59, 0.11), x4);\n"
2087 "       float px5 =  2.0 * dot(float3(0.3, 0.59, 0.11), x5);\n"
2088 "       float px6 =  1.0 * dot(float3(0.3, 0.59, 0.11), x6);\n"
2089 "       float py1 = -1.0 * dot(float3(0.3, 0.59, 0.11), y1);\n"
2090 "       float py2 = -2.0 * dot(float3(0.3, 0.59, 0.11), y2);\n"
2091 "       float py3 = -1.0 * dot(float3(0.3, 0.59, 0.11), y3);\n"
2092 "       float py4 =  1.0 * dot(float3(0.3, 0.59, 0.11), y4);\n"
2093 "       float py5 =  2.0 * dot(float3(0.3, 0.59, 0.11), y5);\n"
2094 "       float py6 =  1.0 * dot(float3(0.3, 0.59, 0.11), y6);\n"
2095 "       sobel = 0.25 * abs(px1 + px2 + px3 + px4 + px5 + px6) + 0.25 * abs(py1 + py2 + py3 + py4 + py5 + py6);\n"
2096 "       gl_FragColor += tex2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*float2(-0.987688, -0.156434)) * UserVec1.y;\n"
2097 "       gl_FragColor += tex2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*float2(-0.156434, -0.891007)) * UserVec1.y;\n"
2098 "       gl_FragColor += tex2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*float2( 0.891007, -0.453990)) * UserVec1.y;\n"
2099 "       gl_FragColor += tex2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*float2( 0.707107,  0.707107)) * UserVec1.y;\n"
2100 "       gl_FragColor += tex2D(Texture_First, TexCoord1 + PixelSize*UserVec1.x*float2(-0.453990,  0.891007)) * UserVec1.y;\n"
2101 "       gl_FragColor /= (1.0 + 5.0 * UserVec1.y);\n"
2102 "       gl_FragColor.rgb = gl_FragColor.rgb * (1.0 + UserVec2.x) + float3(1,1,1)*max(0.0, sobel - UserVec2.z)*UserVec2.y;\n"
2103 "#endif\n"
2104 "\n"
2105 "#ifdef USESATURATION\n"
2106 "       //apply saturation BEFORE gamma ramps, so v_glslgamma value does not matter\n"
2107 "       float y = dot(gl_FragColor.rgb, float3(0.299, 0.587, 0.114));\n"
2108 "       // 'vampire sight' effect, wheres red is compensated\n"
2109 "       #ifdef SATURATION_REDCOMPENSATE\n"
2110 "               float rboost = max(0.0, (gl_FragColor.r - max(gl_FragColor.g, gl_FragColor.b))*(1.0 - Saturation));\n"
2111 "               gl_FragColor.rgb = lerp(float3(y,y,y), gl_FragColor.rgb, Saturation);\n"
2112 "               gl_FragColor.r += r;\n"
2113 "       #else\n"
2114 "               // normal desaturation\n"
2115 "               //gl_FragColor = float3(y,y,y) + (gl_FragColor.rgb - float3(y)) * Saturation;\n"
2116 "               gl_FragColor.rgb = lerp(float3(y,y,y), gl_FragColor.rgb, Saturation);\n"
2117 "       #endif\n"
2118 "#endif\n"
2119 "\n"
2120 "#ifdef USEGAMMARAMPS\n"
2121 "       gl_FragColor.r = tex2D(Texture_GammaRamps, float2(gl_FragColor.r, 0)).r;\n"
2122 "       gl_FragColor.g = tex2D(Texture_GammaRamps, float2(gl_FragColor.g, 0)).g;\n"
2123 "       gl_FragColor.b = tex2D(Texture_GammaRamps, float2(gl_FragColor.b, 0)).b;\n"
2124 "#endif\n"
2125 "}\n"
2126 "#endif\n"
2127 "#else // !MODE_POSTPROCESS\n"
2128 "\n"
2129 "\n"
2130 "\n"
2131 "\n"
2132 "#ifdef MODE_GENERIC\n"
2133 "#ifdef VERTEX_SHADER\n"
2134 "void main\n"
2135 "(\n"
2136 "float4 gl_Vertex : POSITION,\n"
2137 "uniform float4x4 ModelViewProjectionMatrix : register(c8),\n"
2138 "float4 gl_Color : COLOR0,\n"
2139 "float4 gl_MultiTexCoord0 : TEXCOORD0,\n"
2140 "float4 gl_MultiTexCoord1 : TEXCOORD1,\n"
2141 "out float4 gl_Position : POSITION,\n"
2142 "#ifdef USEDIFFUSE\n"
2143 "out float2 TexCoord1 : TEXCOORD0,\n"
2144 "#endif\n"
2145 "#ifdef USESPECULAR\n"
2146 "out float2 TexCoord2 : TEXCOORD1,\n"
2147 "#endif\n"
2148 "out float4 gl_FrontColor : COLOR\n"
2149 ")\n"
2150 "{\n"
2151 "#ifdef HLSL\n"
2152 "       gl_FrontColor = gl_Color.bgra; // NOTE: D3DCOLOR is backwards\n"
2153 "#else\n"
2154 "       gl_FrontColor = gl_Color; // Cg is forward\n"
2155 "#endif\n"
2156 "#ifdef USEDIFFUSE\n"
2157 "       TexCoord1 = gl_MultiTexCoord0.xy;\n"
2158 "#endif\n"
2159 "#ifdef USESPECULAR\n"
2160 "       TexCoord2 = gl_MultiTexCoord1.xy;\n"
2161 "#endif\n"
2162 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
2163 "}\n"
2164 "#endif\n"
2165 "\n"
2166 "#ifdef FRAGMENT_SHADER\n"
2167 "\n"
2168 "void main\n"
2169 "(\n"
2170 "float4 gl_FrontColor : COLOR0,\n"
2171 "float2 TexCoord1 : TEXCOORD0,\n"
2172 "float2 TexCoord2 : TEXCOORD1,\n"
2173 "#ifdef USEDIFFUSE\n"
2174 "uniform sampler Texture_First : register(s0),\n"
2175 "#endif\n"
2176 "#ifdef USESPECULAR\n"
2177 "uniform sampler Texture_Second : register(s1),\n"
2178 "#endif\n"
2179 "out float4 gl_FragColor : COLOR\n"
2180 ")\n"
2181 "{\n"
2182 "#ifdef USEVIEWTINT\n"
2183 "       gl_FragColor = gl_FrontColor;\n"
2184 "#else\n"
2185 "       gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);\n"
2186 "#endif\n"
2187 "#ifdef USEDIFFUSE\n"
2188 "       gl_FragColor *= tex2D(Texture_First, TexCoord1);\n"
2189 "#endif\n"
2190 "\n"
2191 "#ifdef USESPECULAR\n"
2192 "       float4 tex2 = tex2D(Texture_Second, TexCoord2);\n"
2193 "# ifdef USECOLORMAPPING\n"
2194 "       gl_FragColor *= tex2;\n"
2195 "# endif\n"
2196 "# ifdef USEGLOW\n"
2197 "       gl_FragColor += tex2;\n"
2198 "# endif\n"
2199 "# ifdef USEVERTEXTEXTUREBLEND\n"
2200 "       gl_FragColor = lerp(gl_FragColor, tex2, tex2.a);\n"
2201 "# endif\n"
2202 "#endif\n"
2203 "}\n"
2204 "#endif\n"
2205 "#else // !MODE_GENERIC\n"
2206 "\n"
2207 "\n"
2208 "\n"
2209 "\n"
2210 "#ifdef MODE_BLOOMBLUR\n"
2211 "#ifdef VERTEX_SHADER\n"
2212 "void main\n"
2213 "(\n"
2214 "float4 gl_Vertex : POSITION,\n"
2215 "uniform float4x4 ModelViewProjectionMatrix : register(c8),\n"
2216 "float4 gl_MultiTexCoord0 : TEXCOORD0,\n"
2217 "out float4 gl_Position : POSITION,\n"
2218 "out float2 TexCoord : TEXCOORD0\n"
2219 ")\n"
2220 "{\n"
2221 "       TexCoord = gl_MultiTexCoord0.xy;\n"
2222 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
2223 "}\n"
2224 "#endif\n"
2225 "\n"
2226 "#ifdef FRAGMENT_SHADER\n"
2227 "\n"
2228 "void main\n"
2229 "(\n"
2230 "float2 TexCoord : TEXCOORD0,\n"
2231 "uniform sampler Texture_First : register(s0),\n"
2232 "uniform float4 BloomBlur_Parameters : register(c1),\n"
2233 "out float4 gl_FragColor : COLOR\n"
2234 ")\n"
2235 "{\n"
2236 "       int i;\n"
2237 "       float2 tc = TexCoord;\n"
2238 "       float3 color = tex2D(Texture_First, tc).rgb;\n"
2239 "       tc += BloomBlur_Parameters.xy;\n"
2240 "       for (i = 1;i < SAMPLES;i++)\n"
2241 "       {\n"
2242 "               color += tex2D(Texture_First, tc).rgb;\n"
2243 "               tc += BloomBlur_Parameters.xy;\n"
2244 "       }\n"
2245 "       gl_FragColor = float4(color * BloomBlur_Parameters.z + float3(BloomBlur_Parameters.w), 1);\n"
2246 "}\n"
2247 "#endif\n"
2248 "#else // !MODE_BLOOMBLUR\n"
2249 "#ifdef MODE_REFRACTION\n"
2250 "#ifdef VERTEX_SHADER\n"
2251 "void main\n"
2252 "(\n"
2253 "float4 gl_Vertex : POSITION,\n"
2254 "uniform float4x4 ModelViewProjectionMatrix : register(c8),\n"
2255 "float4 gl_MultiTexCoord0 : TEXCOORD0,\n"
2256 "uniform float4x4 TexMatrix : register(c0),\n"
2257 "uniform float3 EyePosition : register(c24),\n"
2258 "out float4 gl_Position : POSITION,\n"
2259 "out float2 TexCoord : TEXCOORD0,\n"
2260 "out float3 EyeVector : TEXCOORD1,\n"
2261 "out float4 ModelViewProjectionPosition : TEXCOORD2\n"
2262 ")\n"
2263 "{\n"
2264 "       TexCoord = mul(TexMatrix, gl_MultiTexCoord0).xy;\n"
2265 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
2266 "       ModelViewProjectionPosition = gl_Position;\n"
2267 "}\n"
2268 "#endif\n"
2269 "\n"
2270 "#ifdef FRAGMENT_SHADER\n"
2271 "void main\n"
2272 "(\n"
2273 "float2 TexCoord : TEXCOORD0,\n"
2274 "float3 EyeVector : TEXCOORD1,\n"
2275 "float4 ModelViewProjectionPosition : TEXCOORD2,\n"
2276 "uniform sampler Texture_Normal : register(s0),\n"
2277 "uniform sampler Texture_Refraction : register(s3),\n"
2278 "uniform sampler Texture_Reflection : register(s7),\n"
2279 "uniform float4 DistortScaleRefractReflect : register(c14),\n"
2280 "uniform float4 ScreenScaleRefractReflect : register(c32),\n"
2281 "uniform float4 ScreenCenterRefractReflect : register(c31),\n"
2282 "uniform float4 RefractColor : register(c29),\n"
2283 "out float4 gl_FragColor : COLOR\n"
2284 ")\n"
2285 "{\n"
2286 "       float2 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect.xy * (1.0 / ModelViewProjectionPosition.w);\n"
2287 "       //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"
2288 "       float2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect.xy;\n"
2289 "       float2 ScreenTexCoord = SafeScreenTexCoord + normalize(tex2D(Texture_Normal, TexCoord).rgb - float3(0.5,0.5,0.5)).xy * DistortScaleRefractReflect.xy;\n"
2290 "       // FIXME temporary hack to detect the case that the reflection\n"
2291 "       // gets blackened at edges due to leaving the area that contains actual\n"
2292 "       // content.\n"
2293 "       // Remove this 'ack once we have a better way to stop this thing from\n"
2294 "       // 'appening.\n"
2295 "       float f = min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord + float2(0.01, 0.01)).rgb) / 0.05);\n"
2296 "       f      *= min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord + float2(0.01, -0.01)).rgb) / 0.05);\n"
2297 "       f      *= min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord + float2(-0.01, 0.01)).rgb) / 0.05);\n"
2298 "       f      *= min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord + float2(-0.01, -0.01)).rgb) / 0.05);\n"
2299 "       ScreenTexCoord = lerp(SafeScreenTexCoord, ScreenTexCoord, f);\n"
2300 "       gl_FragColor = float4(tex2D(Texture_Refraction, ScreenTexCoord).rgb, 1) * RefractColor;\n"
2301 "}\n"
2302 "#endif\n"
2303 "#else // !MODE_REFRACTION\n"
2304 "\n"
2305 "\n"
2306 "\n"
2307 "\n"
2308 "#ifdef MODE_WATER\n"
2309 "#ifdef VERTEX_SHADER\n"
2310 "\n"
2311 "void main\n"
2312 "(\n"
2313 "float4 gl_Vertex : POSITION,\n"
2314 "uniform float4x4 ModelViewProjectionMatrix : register(c8),\n"
2315 "float4 gl_MultiTexCoord0 : TEXCOORD0,\n"
2316 "float4 gl_MultiTexCoord1 : TEXCOORD1,\n"
2317 "float4 gl_MultiTexCoord2 : TEXCOORD2,\n"
2318 "float4 gl_MultiTexCoord3 : TEXCOORD3,\n"
2319 "uniform float4x4 TexMatrix : register(c0),\n"
2320 "uniform float3 EyePosition : register(c24),\n"
2321 "out float4 gl_Position : POSITION,\n"
2322 "out float2 TexCoord : TEXCOORD0,\n"
2323 "out float3 EyeVector : TEXCOORD1,\n"
2324 "out float4 ModelViewProjectionPosition : TEXCOORD2\n"
2325 ")\n"
2326 "{\n"
2327 "       TexCoord = mul(TexMatrix, gl_MultiTexCoord0).xy;\n"
2328 "       float3 EyeVectorModelSpace = EyePosition - gl_Vertex.xyz;\n"
2329 "       EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz);\n"
2330 "       EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz);\n"
2331 "       EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz);\n"
2332 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
2333 "       ModelViewProjectionPosition = gl_Position;\n"
2334 "}\n"
2335 "#endif\n"
2336 "\n"
2337 "#ifdef FRAGMENT_SHADER\n"
2338 "void main\n"
2339 "(\n"
2340 "float2 TexCoord : TEXCOORD0,\n"
2341 "float3 EyeVector : TEXCOORD1,\n"
2342 "float4 ModelViewProjectionPosition : TEXCOORD2,\n"
2343 "uniform sampler Texture_Normal : register(s0),\n"
2344 "uniform sampler Texture_Refraction : register(s3),\n"
2345 "uniform sampler Texture_Reflection : register(s7),\n"
2346 "uniform float4 DistortScaleRefractReflect : register(c14),\n"
2347 "uniform float4 ScreenScaleRefractReflect : register(c32),\n"
2348 "uniform float4 ScreenCenterRefractReflect : register(c31),\n"
2349 "uniform float4 RefractColor : register(c29),\n"
2350 "uniform float4 ReflectColor : register(c26),\n"
2351 "uniform float ReflectFactor : register(c27),\n"
2352 "uniform float ReflectOffset : register(c28),\n"
2353 "out float4 gl_FragColor : COLOR\n"
2354 ")\n"
2355 "{\n"
2356 "       float4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);\n"
2357 "       //float4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(tex2D(Texture_Normal, TexCoord).rgb - float3(0.5,0.5,0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
2358 "       float4 SafeScreenTexCoord = ModelViewProjectionPosition.xyxy * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
2359 "       //SafeScreenTexCoord = gl_FragCoord.xyxy * float4(1.0 / 1920.0, 1.0 / 1200.0, 1.0 / 1920.0, 1.0 / 1200.0);\n"
2360 "       float4 ScreenTexCoord = SafeScreenTexCoord + float2(normalize(tex2D(Texture_Normal, TexCoord).rgb - float3(0.5,0.5,0.5)).xy).xyxy * DistortScaleRefractReflect;\n"
2361 "       // FIXME temporary hack to detect the case that the reflection\n"
2362 "       // gets blackened at edges due to leaving the area that contains actual\n"
2363 "       // content.\n"
2364 "       // Remove this 'ack once we have a better way to stop this thing from\n"
2365 "       // 'appening.\n"
2366 "       float f = min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord.xy + float2(0.01, 0.01)).rgb) / 0.05);\n"
2367 "       f      *= min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord.xy + float2(0.01, -0.01)).rgb) / 0.05);\n"
2368 "       f      *= min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord.xy + float2(-0.01, 0.01)).rgb) / 0.05);\n"
2369 "       f      *= min(1.0, length(tex2D(Texture_Refraction, ScreenTexCoord.xy + float2(-0.01, -0.01)).rgb) / 0.05);\n"
2370 "       ScreenTexCoord.xy = lerp(SafeScreenTexCoord.xy, ScreenTexCoord.xy, f);\n"
2371 "       f  = min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord.zw + float2(0.01, 0.01)).rgb) / 0.05);\n"
2372 "       f *= min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord.zw + float2(0.01, -0.01)).rgb) / 0.05);\n"
2373 "       f *= min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord.zw + float2(-0.01, 0.01)).rgb) / 0.05);\n"
2374 "       f *= min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord.zw + float2(-0.01, -0.01)).rgb) / 0.05);\n"
2375 "       ScreenTexCoord.zw = lerp(SafeScreenTexCoord.zw, ScreenTexCoord.zw, f);\n"
2376 "       float Fresnel = pow(min(1.0, 1.0 - float(normalize(EyeVector).z)), 2.0) * ReflectFactor + ReflectOffset;\n"
2377 "       gl_FragColor = lerp(float4(tex2D(Texture_Refraction, ScreenTexCoord.xy).rgb, 1) * RefractColor, float4(tex2D(Texture_Reflection, ScreenTexCoord.zw).rgb, 1) * ReflectColor, Fresnel);\n"
2378 "}\n"
2379 "#endif\n"
2380 "#else // !MODE_WATER\n"
2381 "\n"
2382 "\n"
2383 "\n"
2384 "\n"
2385 "// 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"
2386 "\n"
2387 "// fragment shader specific:\n"
2388 "#ifdef FRAGMENT_SHADER\n"
2389 "\n"
2390 "#ifdef USEFOG\n"
2391 "float3 FogVertex(float3 surfacecolor, float3 FogColor, float3 EyeVectorModelSpace, float FogPlaneVertexDist, float FogRangeRecip, float FogPlaneViewDist, float FogHeightFade, sampler Texture_FogMask, sampler Texture_FogHeightTexture)\n"
2392 "{\n"
2393 "       float fogfrac;\n"
2394 "#ifdef USEFOGHEIGHTTEXTURE\n"
2395 "       float4 fogheightpixel = tex2D(Texture_FogHeightTexture, float2(1,1) + float2(FogPlaneVertexDist, FogPlaneViewDist) * (-2.0 * FogHeightFade));\n"
2396 "       fogfrac = fogheightpixel.a;\n"
2397 "       return lerp(fogheightpixel.rgb * FogColor, surfacecolor, tex2D(Texture_FogMask, float2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r);\n"
2398 "#else\n"
2399 "# ifdef USEFOGOUTSIDE\n"
2400 "       fogfrac = min(0.0, FogPlaneVertexDist) / (FogPlaneVertexDist - FogPlaneViewDist) * min(1.0, min(0.0, FogPlaneVertexDist) * FogHeightFade);\n"
2401 "# else\n"
2402 "       fogfrac = FogPlaneViewDist / (FogPlaneViewDist - max(0.0, FogPlaneVertexDist)) * min(1.0, (min(0.0, FogPlaneVertexDist) + FogPlaneViewDist) * FogHeightFade);\n"
2403 "# endif\n"
2404 "       return lerp(FogColor, surfacecolor, tex2D(Texture_FogMask, float2(length(EyeVectorModelSpace)*fogfrac*FogRangeRecip, 0.0)).r);\n"
2405 "#endif\n"
2406 "}\n"
2407 "#endif\n"
2408 "\n"
2409 "#ifdef USEOFFSETMAPPING\n"
2410 "float2 OffsetMapping(float2 TexCoord, float OffsetMapping_Scale, float3 EyeVector, sampler Texture_Normal)\n"
2411 "{\n"
2412 "#ifdef USEOFFSETMAPPING_RELIEFMAPPING\n"
2413 "       // 14 sample relief mapping: linear search and then binary search\n"
2414 "       // this basically steps forward a small amount repeatedly until it finds\n"
2415 "       // itself inside solid, then jitters forward and back using decreasing\n"
2416 "       // amounts to find the impact\n"
2417 "       //float3 OffsetVector = float3(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * float2(-1, 1), -1);\n"
2418 "       //float3 OffsetVector = float3(normalize(EyeVector.xy) * OffsetMapping_Scale * float2(-1, 1), -1);\n"
2419 "       float3 OffsetVector = float3(normalize(EyeVector).xy * OffsetMapping_Scale * float2(-1, 1), -1);\n"
2420 "       float3 RT = float3(TexCoord, 1);\n"
2421 "       OffsetVector *= 0.1;\n"
2422 "       RT += OffsetVector *  step(tex2D(Texture_Normal, RT.xy).a, RT.z);\n"
2423 "       RT += OffsetVector *  step(tex2D(Texture_Normal, RT.xy).a, RT.z);\n"
2424 "       RT += OffsetVector *  step(tex2D(Texture_Normal, RT.xy).a, RT.z);\n"
2425 "       RT += OffsetVector *  step(tex2D(Texture_Normal, RT.xy).a, RT.z);\n"
2426 "       RT += OffsetVector *  step(tex2D(Texture_Normal, RT.xy).a, RT.z);\n"
2427 "       RT += OffsetVector *  step(tex2D(Texture_Normal, RT.xy).a, RT.z);\n"
2428 "       RT += OffsetVector *  step(tex2D(Texture_Normal, RT.xy).a, RT.z);\n"
2429 "       RT += OffsetVector *  step(tex2D(Texture_Normal, RT.xy).a, RT.z);\n"
2430 "       RT += OffsetVector *  step(tex2D(Texture_Normal, RT.xy).a, RT.z);\n"
2431 "       RT += OffsetVector * (step(tex2D(Texture_Normal, RT.xy).a, RT.z)          - 0.5);\n"
2432 "       RT += OffsetVector * (step(tex2D(Texture_Normal, RT.xy).a, RT.z) * 0.5    - 0.25);\n"
2433 "       RT += OffsetVector * (step(tex2D(Texture_Normal, RT.xy).a, RT.z) * 0.25   - 0.125);\n"
2434 "       RT += OffsetVector * (step(tex2D(Texture_Normal, RT.xy).a, RT.z) * 0.125  - 0.0625);\n"
2435 "       RT += OffsetVector * (step(tex2D(Texture_Normal, RT.xy).a, RT.z) * 0.0625 - 0.03125);\n"
2436 "       return RT.xy;\n"
2437 "#else\n"
2438 "       // 2 sample offset mapping (only 2 samples because of ATI Radeon 9500-9800/X300 limits)\n"
2439 "       //float2 OffsetVector = float2(EyeVector.xy * ((1.0 / EyeVector.z) * OffsetMapping_Scale) * float2(-1, 1));\n"
2440 "       //float2 OffsetVector = float2(normalize(EyeVector.xy) * OffsetMapping_Scale * float2(-1, 1));\n"
2441 "       float2 OffsetVector = float2(normalize(EyeVector).xy * OffsetMapping_Scale * float2(-1, 1));\n"
2442 "       OffsetVector *= 0.5;\n"
2443 "       TexCoord += OffsetVector * (1.0 - tex2D(Texture_Normal, TexCoord).a);\n"
2444 "       TexCoord += OffsetVector * (1.0 - tex2D(Texture_Normal, TexCoord).a);\n"
2445 "       return TexCoord;\n"
2446 "#endif\n"
2447 "}\n"
2448 "#endif // USEOFFSETMAPPING\n"
2449 "\n"
2450 "#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO)\n"
2451 "#if defined(USESHADOWMAP2D)\n"
2452 "# ifdef USESHADOWMAPORTHO\n"
2453 "#  define GetShadowMapTC2D(dir, ShadowMap_Parameters) (min(dir, ShadowMap_Parameters.xyz))\n"
2454 "# else\n"
2455 "#  ifdef USESHADOWMAPVSDCT\n"
2456 "float3 GetShadowMapTC2D(float3 dir, float4 ShadowMap_Parameters, samplerCUBE Texture_CubeProjection)\n"
2457 "{\n"
2458 "       float3 adir = abs(dir);\n"
2459 "       float2 aparams = ShadowMap_Parameters.xy / max(max(adir.x, adir.y), adir.z);\n"
2460 "       float4 proj = texCUBE(Texture_CubeProjection, dir);\n"
2461 "       return float3(lerp(dir.xy, dir.zz, proj.xy) * aparams.x + proj.zw * ShadowMap_Parameters.z, aparams.y + ShadowMap_Parameters.w);\n"
2462 "}\n"
2463 "#  else\n"
2464 "float3 GetShadowMapTC2D(float3 dir, float4 ShadowMap_Parameters)\n"
2465 "{\n"
2466 "       float3 adir = abs(dir);\n"
2467 "       float ma = adir.z;\n"
2468 "       float4 proj = float4(dir, 2.5);\n"
2469 "       if (adir.x > ma) { ma = adir.x; proj = float4(dir.zyx, 0.5); }\n"
2470 "       if (adir.y > ma) { ma = adir.y; proj = float4(dir.xzy, 1.5); }\n"
2471 "#ifdef HLSL\n"
2472 "       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"
2473 "#else\n"
2474 "       float2 aparams = ShadowMap_Parameters.xy / ma;\n"
2475 "       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"
2476 "#endif\n"
2477 "}\n"
2478 "#  endif\n"
2479 "# endif\n"
2480 "#endif // defined(USESHADOWMAP2D)\n"
2481 "\n"
2482 "# ifdef USESHADOWMAP2D\n"
2483 "#ifdef USESHADOWMAPVSDCT\n"
2484 "float ShadowMapCompare(float3 dir, sampler Texture_ShadowMap2D, float4 ShadowMap_Parameters, float2 ShadowMap_TextureScale, samplerCUBE Texture_CubeProjection)\n"
2485 "#else\n"
2486 "float ShadowMapCompare(float3 dir, sampler Texture_ShadowMap2D, float4 ShadowMap_Parameters, float2 ShadowMap_TextureScale)\n"
2487 "#endif\n"
2488 "{\n"
2489 "#ifdef USESHADOWMAPVSDCT\n"
2490 "       float3 shadowmaptc = GetShadowMapTC2D(dir, ShadowMap_Parameters, Texture_CubeProjection);\n"
2491 "#else\n"
2492 "       float3 shadowmaptc = GetShadowMapTC2D(dir, ShadowMap_Parameters);\n"
2493 "#endif\n"
2494 "       float f;\n"
2495 "\n"
2496 "#  ifdef USESHADOWSAMPLER\n"
2497 "#    ifdef USESHADOWMAPPCF\n"
2498 "#      define texval(x, y) tex2Dproj(Texture_ShadowMap2D, float4(center + float2(x, y)*ShadowMap_TextureScale, shadowmaptc.z, 1.0)).r  \n"
2499 "       float2 center = shadowmaptc.xy*ShadowMap_TextureScale;\n"
2500 "       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"
2501 "#    else\n"
2502 "       f = tex2Dproj(Texture_ShadowMap2D, float4(shadowmaptc.xy*ShadowMap_TextureScale, shadowmaptc.z, 1.0)).r;\n"
2503 "#    endif\n"
2504 "#  else\n"
2505 "#    ifdef USESHADOWMAPPCF\n"
2506 "#     if defined(GL_ARB_texture_gather) || defined(GL_AMD_texture_texture4)\n"
2507 "#      ifdef GL_ARB_texture_gather\n"
2508 "#        define texval(x, y) textureGatherOffset(Texture_ShadowMap2D, center, int2(x, y))\n"
2509 "#      else\n"
2510 "#        define texval(x, y) texture4(Texture_ShadowMap2D, center + float2(x, y)*ShadowMap_TextureScale)\n"
2511 "#      endif\n"
2512 "       float2 offset = frac(shadowmaptc.xy - 0.5), center = (shadowmaptc.xy - offset)*ShadowMap_TextureScale;\n"
2513 "#      if USESHADOWMAPPCF > 1\n"
2514 "       float4 group1 = step(shadowmaptc.z, texval(-2.0, -2.0));\n"
2515 "       float4 group2 = step(shadowmaptc.z, texval( 0.0, -2.0));\n"
2516 "       float4 group3 = step(shadowmaptc.z, texval( 2.0, -2.0));\n"
2517 "       float4 group4 = step(shadowmaptc.z, texval(-2.0,  0.0));\n"
2518 "       float4 group5 = step(shadowmaptc.z, texval( 0.0,  0.0));\n"
2519 "       float4 group6 = step(shadowmaptc.z, texval( 2.0,  0.0));\n"
2520 "       float4 group7 = step(shadowmaptc.z, texval(-2.0,  2.0));\n"
2521 "       float4 group8 = step(shadowmaptc.z, texval( 0.0,  2.0));\n"
2522 "       float4 group9 = step(shadowmaptc.z, texval( 2.0,  2.0));\n"
2523 "       float4 locols = float4(group1.ab, group3.ab);\n"
2524 "       float4 hicols = float4(group7.rg, group9.rg);\n"
2525 "       locols.yz += group2.ab;\n"
2526 "       hicols.yz += group8.rg;\n"
2527 "       float4 midcols = float4(group1.rg, group3.rg) + float4(group7.ab, group9.ab) +\n"
2528 "                               float4(group4.rg, group6.rg) + float4(group4.ab, group6.ab) +\n"
2529 "                               lerp(locols, hicols, offset.y);\n"
2530 "       float4 cols = group5 + float4(group2.rg, group8.ab);\n"
2531 "       cols.xyz += lerp(midcols.xyz, midcols.yzw, offset.x);\n"
2532 "       f = dot(cols, float4(1.0/25.0));\n"
2533 "#      else\n"
2534 "       float4 group1 = step(shadowmaptc.z, texval(-1.0, -1.0));\n"
2535 "       float4 group2 = step(shadowmaptc.z, texval( 1.0, -1.0));\n"
2536 "       float4 group3 = step(shadowmaptc.z, texval(-1.0,  1.0));\n"
2537 "       float4 group4 = step(shadowmaptc.z, texval( 1.0,  1.0));\n"
2538 "       float4 cols = float4(group1.rg, group2.rg) + float4(group3.ab, group4.ab) +\n"
2539 "                               lerp(float4(group1.ab, group2.ab), float4(group3.rg, group4.rg), offset.y);\n"
2540 "       f = dot(lerp(cols.xyz, cols.yzw, offset.x), float3(1.0/9.0));\n"
2541 "#      endif\n"
2542 "#     else\n"
2543 "#      ifdef GL_EXT_gpu_shader4\n"
2544 "#        define texval(x, y) tex2DOffset(Texture_ShadowMap2D, center, int2(x, y)).r\n"
2545 "#      else\n"
2546 "#        define texval(x, y) texDepth2D(Texture_ShadowMap2D, center + float2(x, y)*ShadowMap_TextureScale).r  \n"
2547 "#      endif\n"
2548 "#      if USESHADOWMAPPCF > 1\n"
2549 "       float2 center = shadowmaptc.xy - 0.5, offset = frac(center);\n"
2550 "       center *= ShadowMap_TextureScale;\n"
2551 "       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"
2552 "       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"
2553 "       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"
2554 "       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"
2555 "       float4 cols = row2 + row3 + lerp(row1, row4, offset.y);\n"
2556 "       f = dot(lerp(cols.xyz, cols.yzw, offset.x), float3(1.0/9.0));\n"
2557 "#      else\n"
2558 "       float2 center = shadowmaptc.xy*ShadowMap_TextureScale, offset = frac(shadowmaptc.xy);\n"
2559 "       float3 row1 = step(shadowmaptc.z, float3(texval(-1.0, -1.0), texval( 0.0, -1.0), texval( 1.0, -1.0)));\n"
2560 "       float3 row2 = step(shadowmaptc.z, float3(texval(-1.0,  0.0), texval( 0.0,  0.0), texval( 1.0,  0.0)));\n"
2561 "       float3 row3 = step(shadowmaptc.z, float3(texval(-1.0,  1.0), texval( 0.0,  1.0), texval( 1.0,  1.0)));\n"
2562 "       float3 cols = row2 + lerp(row1, row3, offset.y);\n"
2563 "       f = dot(lerp(cols.xy, cols.yz, offset.x), float2(0.25,0.25));\n"
2564 "#      endif\n"
2565 "#     endif\n"
2566 "#    else\n"
2567 "       f = step(shadowmaptc.z, tex2D(Texture_ShadowMap2D, shadowmaptc.xy*ShadowMap_TextureScale).r);\n"
2568 "#    endif\n"
2569 "#  endif\n"
2570 "#  ifdef USESHADOWMAPORTHO\n"
2571 "       return lerp(ShadowMap_Parameters.w, 1.0, f);\n"
2572 "#  else\n"
2573 "       return f;\n"
2574 "#  endif\n"
2575 "}\n"
2576 "# endif\n"
2577 "#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO)\n"
2578 "#endif // FRAGMENT_SHADER\n"
2579 "\n"
2580 "\n"
2581 "\n"
2582 "\n"
2583 "#ifdef MODE_DEFERREDGEOMETRY\n"
2584 "#ifdef VERTEX_SHADER\n"
2585 "void main\n"
2586 "(\n"
2587 "float4 gl_Vertex : POSITION,\n"
2588 "uniform float4x4 ModelViewProjectionMatrix : register(c8),\n"
2589 "#ifdef USEVERTEXTEXTUREBLEND\n"
2590 "float4 gl_Color : COLOR0,\n"
2591 "#endif\n"
2592 "float4 gl_MultiTexCoord0 : TEXCOORD0,\n"
2593 "float4 gl_MultiTexCoord1 : TEXCOORD1,\n"
2594 "float4 gl_MultiTexCoord2 : TEXCOORD2,\n"
2595 "float4 gl_MultiTexCoord3 : TEXCOORD3,\n"
2596 "uniform float4x4 TexMatrix : register(c0),\n"
2597 "#ifdef USEVERTEXTEXTUREBLEND\n"
2598 "uniform float4x4 BackgroundTexMatrix : register(c4),\n"
2599 "#endif\n"
2600 "uniform float4x4 ModelViewMatrix : register(c12),\n"
2601 "#ifdef USEOFFSETMAPPING\n"
2602 "uniform float3 EyePosition : register(c24),\n"
2603 "#endif\n"
2604 "out float4 gl_Position : POSITION,\n"
2605 "#ifdef USEVERTEXTEXTUREBLEND\n"
2606 "out float4 gl_FrontColor : COLOR,\n"
2607 "#endif\n"
2608 "out float4 TexCoordBoth : TEXCOORD0,\n"
2609 "#ifdef USEOFFSETMAPPING\n"
2610 "out float3 EyeVector : TEXCOORD2,\n"
2611 "#endif\n"
2612 "out float3 VectorS : TEXCOORD5, // direction of S texcoord (sometimes crudely called tangent)\n"
2613 "out float3 VectorT : TEXCOORD6, // direction of T texcoord (sometimes crudely called binormal)\n"
2614 "out float4 VectorR : TEXCOORD7 // direction of R texcoord (surface normal), Depth value\n"
2615 ")\n"
2616 "{\n"
2617 "       TexCoordBoth = mul(TexMatrix, gl_MultiTexCoord0);\n"
2618 "#ifdef USEVERTEXTEXTUREBLEND\n"
2619 "#ifdef HLSL\n"
2620 "       gl_FrontColor = gl_Color.bgra; // NOTE: D3DCOLOR is backwards\n"
2621 "#else\n"
2622 "       gl_FrontColor = gl_Color; // Cg is forward\n"
2623 "#endif\n"
2624 "       TexCoordBoth.zw = float2(Backgroundmul(TexMatrix, gl_MultiTexCoord0));\n"
2625 "#endif\n"
2626 "\n"
2627 "       // transform unnormalized eye direction into tangent space\n"
2628 "#ifdef USEOFFSETMAPPING\n"
2629 "       float3 EyeVectorModelSpace = EyePosition - gl_Vertex.xyz;\n"
2630 "       EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz);\n"
2631 "       EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz);\n"
2632 "       EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz);\n"
2633 "#endif\n"
2634 "\n"
2635 "       VectorS = mul(ModelViewMatrix, float4(gl_MultiTexCoord1.xyz, 0)).xyz;\n"
2636 "       VectorT = mul(ModelViewMatrix, float4(gl_MultiTexCoord2.xyz, 0)).xyz;\n"
2637 "       VectorR.xyz = mul(ModelViewMatrix, float4(gl_MultiTexCoord3.xyz, 0)).xyz;\n"
2638 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
2639 "       VectorR.w = gl_Position.z;\n"
2640 "}\n"
2641 "#endif // VERTEX_SHADER\n"
2642 "\n"
2643 "#ifdef FRAGMENT_SHADER\n"
2644 "void main\n"
2645 "(\n"
2646 "float4 TexCoordBoth : TEXCOORD0,\n"
2647 "float3 EyeVector : TEXCOORD2,\n"
2648 "float3 VectorS : TEXCOORD5, // direction of S texcoord (sometimes crudely called tangent)\n"
2649 "float3 VectorT : TEXCOORD6, // direction of T texcoord (sometimes crudely called binormal)\n"
2650 "float4 VectorR : TEXCOORD7, // direction of R texcoord (surface normal), Depth value\n"
2651 "uniform sampler Texture_Normal : register(s0),\n"
2652 "#ifdef USEALPHAKILL\n"
2653 "uniform sampler Texture_Color : register(s1),\n"
2654 "#endif\n"
2655 "uniform sampler Texture_Gloss : register(s2),\n"
2656 "#ifdef USEVERTEXTEXTUREBLEND\n"
2657 "uniform sampler Texture_SecondaryNormal : register(s4),\n"
2658 "uniform sampler Texture_SecondaryGloss : register(s6),\n"
2659 "#endif\n"
2660 "#ifdef USEOFFSETMAPPING\n"
2661 "uniform float OffsetMapping_Scale : register(c24),\n"
2662 "#endif\n"
2663 "uniform half SpecularPower : register(c36),\n"
2664 "#ifdef HLSL\n"
2665 "out float4 gl_FragData0 : COLOR0,\n"
2666 "out float4 gl_FragData1 : COLOR1\n"
2667 "#else\n"
2668 "out float4 gl_FragColor : COLOR\n"
2669 "#endif\n"
2670 ")\n"
2671 "{\n"
2672 "       float2 TexCoord = TexCoordBoth.xy;\n"
2673 "#ifdef USEOFFSETMAPPING\n"
2674 "       // apply offsetmapping\n"
2675 "       float2 TexCoordOffset = OffsetMapping(TexCoord, OffsetMapping_Scale, EyeVector, Texture_Normal);\n"
2676 "#define TexCoord TexCoordOffset\n"
2677 "#endif\n"
2678 "\n"
2679 "#ifdef USEALPHAKILL\n"
2680 "       if (tex2D(Texture_Color, TexCoord).a < 0.5)\n"
2681 "               discard;\n"
2682 "#endif\n"
2683 "\n"
2684 "#ifdef USEVERTEXTEXTUREBLEND\n"
2685 "       float alpha = tex2D(Texture_Color, TexCoord).a;\n"
2686 "       float terrainblend = clamp(float(gl_FrontColor.a) * alpha * 2.0 - 0.5, float(0.0), float(1.0));\n"
2687 "       //float terrainblend = min(float(gl_FrontColor.a) * alpha * 2.0, float(1.0));\n"
2688 "       //float terrainblend = float(gl_FrontColor.a) * alpha > 0.5;\n"
2689 "#endif\n"
2690 "\n"
2691 "#ifdef USEVERTEXTEXTUREBLEND\n"
2692 "       float3 surfacenormal = lerp(tex2D(Texture_SecondaryNormal, TexCoord2).rgb, tex2D(Texture_Normal, TexCoord).rgb, terrainblend) - float3(0.5, 0.5, 0.5);\n"
2693 "       float a = lerp(tex2D(Texture_SecondaryGloss, TexCoord2).a, tex2D(Texture_Gloss, TexCoord).a, terrainblend);\n"
2694 "#else\n"
2695 "       float3 surfacenormal = tex2D(Texture_Normal, TexCoord).rgb - float3(0.5, 0.5, 0.5);\n"
2696 "       float a = tex2D(Texture_Gloss, TexCoord).a;\n"
2697 "#endif\n"
2698 "\n"
2699 "#ifdef HLSL\n"
2700 "       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"
2701 "       float Depth = VectorR.w / 256.0;\n"
2702 "       float4 depthcolor = float4(Depth,Depth*65536.0/255.0,Depth*16777216.0/255.0,0.0);\n"
2703 "//     float4 depthcolor = float4(Depth,Depth*256.0,Depth*65536.0,0.0);\n"
2704 "       depthcolor.yz -= floor(depthcolor.yz);\n"
2705 "       gl_FragData1 = depthcolor;\n"
2706 "#else\n"
2707 "       gl_FragColor = float4(normalize(surfacenormal.x * VectorS + surfacenormal.y * VectorT + surfacenormal.z * VectorR) * 0.5 + float3(0.5, 0.5, 0.5), a);\n"
2708 "#endif\n"
2709 "}\n"
2710 "#endif // FRAGMENT_SHADER\n"
2711 "#else // !MODE_DEFERREDGEOMETRY\n"
2712 "\n"
2713 "\n"
2714 "\n"
2715 "\n"
2716 "#ifdef MODE_DEFERREDLIGHTSOURCE\n"
2717 "#ifdef VERTEX_SHADER\n"
2718 "void main\n"
2719 "(\n"
2720 "float4 gl_Vertex : POSITION,\n"
2721 "uniform float4x4 ModelViewProjectionMatrix : register(c8),\n"
2722 "uniform float4x4 ModelViewMatrix : register(c12),\n"
2723 "out float4 gl_Position : POSITION,\n"
2724 "out float4 ModelViewPosition : TEXCOORD0\n"
2725 ")\n"
2726 "{\n"
2727 "       ModelViewPosition = mul(ModelViewMatrix, gl_Vertex);\n"
2728 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
2729 "}\n"
2730 "#endif // VERTEX_SHADER\n"
2731 "\n"
2732 "#ifdef FRAGMENT_SHADER\n"
2733 "void main\n"
2734 "(\n"
2735 "#ifdef HLSL\n"
2736 "float2 Pixel : VPOS,\n"
2737 "#else\n"
2738 "float2 Pixel : WPOS,\n"
2739 "#endif\n"
2740 "float4 ModelViewPosition : TEXCOORD0,\n"
2741 "uniform float4x4 ViewToLight : register(c44),\n"
2742 "uniform float2 ScreenToDepth : register(c33), // ScreenToDepth = float2(Far / (Far - Near), Far * Near / (Near - Far));\n"
2743 "uniform float3 LightPosition : register(c23),\n"
2744 "uniform half2 PixelToScreenTexCoord : register(c42),\n"
2745 "uniform half3 DeferredColor_Ambient : register(c9),\n"
2746 "uniform half3 DeferredColor_Diffuse : register(c10),\n"
2747 "#ifdef USESPECULAR\n"
2748 "uniform half3 DeferredColor_Specular : register(c11),\n"
2749 "uniform half SpecularPower : register(c36),\n"
2750 "#endif\n"
2751 "uniform sampler Texture_Attenuation : register(s9),\n"
2752 "uniform sampler Texture_ScreenDepth : register(s13),\n"
2753 "uniform sampler Texture_ScreenNormalMap : register(s14),\n"
2754 "\n"
2755 "#ifdef USECUBEFILTER\n"
2756 "uniform samplerCUBE Texture_Cube : register(s10),\n"
2757 "#endif\n"
2758 "\n"
2759 "#ifdef USESHADOWMAP2D\n"
2760 "# ifdef USESHADOWSAMPLER\n"
2761 "uniform sampler Texture_ShadowMap2D : register(s15),\n"
2762 "# else\n"
2763 "uniform sampler Texture_ShadowMap2D : register(s15),\n"
2764 "# endif\n"
2765 "#endif\n"
2766 "\n"
2767 "#ifdef USESHADOWMAPVSDCT\n"
2768 "uniform samplerCUBE Texture_CubeProjection : register(s12),\n"
2769 "#endif\n"
2770 "\n"
2771 "#if defined(USESHADOWMAP2D)\n"
2772 "uniform float2 ShadowMap_TextureScale : register(c35),\n"
2773 "uniform float4 ShadowMap_Parameters : register(c34),\n"
2774 "#endif\n"
2775 "\n"
2776 "out float4 gl_FragData0 : COLOR0,\n"
2777 "out float4 gl_FragData1 : COLOR1\n"
2778 ")\n"
2779 "{\n"
2780 "       // calculate viewspace pixel position\n"
2781 "       float2 ScreenTexCoord = Pixel * PixelToScreenTexCoord;\n"
2782 "       //ScreenTexCoord.y = ScreenTexCoord.y * -1 + 1; // Cg is opposite?\n"
2783 "       float3 position;\n"
2784 "#ifdef HLSL\n"
2785 "       position.z = texDepth2D(Texture_ScreenDepth, ScreenTexCoord) * 256.0;\n"
2786 "#else\n"
2787 "       position.z = ScreenToDepth.y / (texDepth2D(Texture_ScreenDepth, ScreenTexCoord) + ScreenToDepth.x);\n"
2788 "#endif\n"
2789 "       position.xy = ModelViewPosition.xy * (position.z / ModelViewPosition.z);\n"
2790 "       // decode viewspace pixel normal\n"
2791 "       half4 normalmap = half4(tex2D(Texture_ScreenNormalMap, ScreenTexCoord));\n"
2792 "       half3 surfacenormal = half3(normalize(normalmap.rgb - half3(0.5,0.5,0.5)));\n"
2793 "       // surfacenormal = pixel normal in viewspace\n"
2794 "       // LightVector = pixel to light in viewspace\n"
2795 "       // CubeVector = position in lightspace\n"
2796 "       // eyevector = pixel to view in viewspace\n"
2797 "       float3 CubeVector = mul(ViewToLight, float4(position,1)).xyz;\n"
2798 "       half fade = half(tex2D(Texture_Attenuation, float2(length(CubeVector), 0.0)).r);\n"
2799 "#ifdef USEDIFFUSE\n"
2800 "       // calculate diffuse shading\n"
2801 "       half3 lightnormal = half3(normalize(LightPosition - position));\n"
2802 "       half diffuse = half(max(float(dot(surfacenormal, lightnormal)), 0.0));\n"
2803 "#endif\n"
2804 "#ifdef USESPECULAR\n"
2805 "       // calculate directional shading\n"
2806 "       float3 eyevector = position * -1.0;\n"
2807 "#  ifdef USEEXACTSPECULARMATH\n"
2808 "       half specular = half(pow(half(max(float(dot(reflect(lightnormal, surfacenormal), normalize(eyevector)))*-1.0, 0.0)), SpecularPower * normalmap.a));\n"
2809 "#  else\n"
2810 "       half3 specularnormal = half3(normalize(lightnormal + half3(normalize(eyevector))));\n"
2811 "       half specular = half(pow(half(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower * normalmap.a));\n"
2812 "#  endif\n"
2813 "#endif\n"
2814 "\n"
2815 "#if defined(USESHADOWMAP2D)\n"
2816 "       fade *= half(ShadowMapCompare(CubeVector, Texture_ShadowMap2D, ShadowMap_Parameters, ShadowMap_TextureScale\n"
2817 "#ifdef USESHADOWMAPVSDCT\n"
2818 ", Texture_CubeProjection\n"
2819 "#endif\n"
2820 "       ));\n"
2821 "#endif\n"
2822 "\n"
2823 "#ifdef USEDIFFUSE\n"
2824 "       gl_FragData0 = float4((DeferredColor_Ambient + DeferredColor_Diffuse * diffuse) * fade, 1.0);\n"
2825 "#else\n"
2826 "       gl_FragData0 = float4(DeferredColor_Ambient * fade, 1.0);\n"
2827 "#endif\n"
2828 "#ifdef USESPECULAR\n"
2829 "       gl_FragData1 = float4(DeferredColor_Specular * (specular * fade), 1.0);\n"
2830 "#else\n"
2831 "       gl_FragData1 = float4(0.0, 0.0, 0.0, 1.0);\n"
2832 "#endif\n"
2833 "\n"
2834 "# ifdef USECUBEFILTER\n"
2835 "       float3 cubecolor = texCUBE(Texture_Cube, CubeVector).rgb;\n"
2836 "       gl_FragData0.rgb *= cubecolor;\n"
2837 "       gl_FragData1.rgb *= cubecolor;\n"
2838 "# endif\n"
2839 "}\n"
2840 "#endif // FRAGMENT_SHADER\n"
2841 "#else // !MODE_DEFERREDLIGHTSOURCE\n"
2842 "\n"
2843 "\n"
2844 "\n"
2845 "\n"
2846 "#ifdef VERTEX_SHADER\n"
2847 "void main\n"
2848 "(\n"
2849 "float4 gl_Vertex : POSITION,\n"
2850 "uniform float4x4 ModelViewProjectionMatrix : register(c8),\n"
2851 "#if defined(USEVERTEXTEXTUREBLEND) || defined(MODE_VERTEXCOLOR)\n"
2852 "float4 gl_Color : COLOR0,\n"
2853 "#endif\n"
2854 "float4 gl_MultiTexCoord0 : TEXCOORD0,\n"
2855 "float4 gl_MultiTexCoord1 : TEXCOORD1,\n"
2856 "float4 gl_MultiTexCoord2 : TEXCOORD2,\n"
2857 "float4 gl_MultiTexCoord3 : TEXCOORD3,\n"
2858 "float4 gl_MultiTexCoord4 : TEXCOORD4,\n"
2859 "\n"
2860 "uniform float3 EyePosition : register(c24),\n"
2861 "uniform float4x4 TexMatrix : register(c0),\n"
2862 "#ifdef USEVERTEXTEXTUREBLEND\n"
2863 "uniform float4x4 BackgroundTexMatrix : register(c4),\n"
2864 "#endif\n"
2865 "#ifdef MODE_LIGHTSOURCE\n"
2866 "uniform float4x4 ModelToLight : register(c20),\n"
2867 "#endif\n"
2868 "#ifdef MODE_LIGHTSOURCE\n"
2869 "uniform float3 LightPosition : register(c27),\n"
2870 "#endif\n"
2871 "#ifdef MODE_LIGHTDIRECTION\n"
2872 "uniform float3 LightDir : register(c26),\n"
2873 "#endif\n"
2874 "uniform float4 FogPlane : register(c25),\n"
2875 "#ifdef MODE_DEFERREDLIGHTSOURCE\n"
2876 "uniform float3 LightPosition : register(c27),\n"
2877 "#endif\n"
2878 "#ifdef USESHADOWMAPORTHO\n"
2879 "uniform float4x4 ShadowMapMatrix : register(c16),\n"
2880 "#endif\n"
2881 "#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND)\n"
2882 "out float4 gl_FrontColor : COLOR,\n"
2883 "#endif\n"
2884 "out float4 TexCoordBoth : TEXCOORD0,\n"
2885 "#ifdef USELIGHTMAP\n"
2886 "out float2 TexCoordLightmap : TEXCOORD1,\n"
2887 "#endif\n"
2888 "#ifdef USEEYEVECTOR\n"
2889 "out float3 EyeVector : TEXCOORD2,\n"
2890 "#endif\n"
2891 "#ifdef USEREFLECTION\n"
2892 "out float4 ModelViewProjectionPosition : TEXCOORD3,\n"
2893 "#endif\n"
2894 "#ifdef USEFOG\n"
2895 "out float4 EyeVectorModelSpaceFogPlaneVertexDist : TEXCOORD4,\n"
2896 "#endif\n"
2897 "#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE) || defined(USEDIFFUSE)\n"
2898 "out float3 LightVector : TEXCOORD1,\n"
2899 "#endif\n"
2900 "#ifdef MODE_LIGHTSOURCE\n"
2901 "out float3 CubeVector : TEXCOORD3,\n"
2902 "#endif\n"
2903 "#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE)\n"
2904 "out float3 VectorS : TEXCOORD5, // direction of S texcoord (sometimes crudely called tangent)\n"
2905 "out float3 VectorT : TEXCOORD6, // direction of T texcoord (sometimes crudely called binormal)\n"
2906 "out float3 VectorR : TEXCOORD7, // direction of R texcoord (surface normal)\n"
2907 "#endif\n"
2908 "#ifdef USESHADOWMAPORTHO\n"
2909 "out float3 ShadowMapTC : TEXCOORD3, // CONFLICTS WITH USEREFLECTION!\n"
2910 "#endif\n"
2911 "out float4 gl_Position : POSITION\n"
2912 ")\n"
2913 "{\n"
2914 "#if defined(MODE_VERTEXCOLOR) || defined(USEVERTEXTEXTUREBLEND)\n"
2915 "#ifdef HLSL\n"
2916 "       gl_FrontColor = gl_Color.bgra; // NOTE: D3DCOLOR is backwards\n"
2917 "#else\n"
2918 "       gl_FrontColor = gl_Color; // Cg is forward\n"
2919 "#endif\n"
2920 "#endif\n"
2921 "       // copy the surface texcoord\n"
2922 "       TexCoordBoth = mul(TexMatrix, gl_MultiTexCoord0);\n"
2923 "#ifdef USEVERTEXTEXTUREBLEND\n"
2924 "       TexCoordBoth.zw = mul(BackgroundTexMatrix, gl_MultiTexCoord0).xy;\n"
2925 "#endif\n"
2926 "#ifdef USELIGHTMAP\n"
2927 "       TexCoordLightmap = gl_MultiTexCoord4.xy;\n"
2928 "#endif\n"
2929 "\n"
2930 "#ifdef MODE_LIGHTSOURCE\n"
2931 "       // transform vertex position into light attenuation/cubemap space\n"
2932 "       // (-1 to +1 across the light box)\n"
2933 "       CubeVector = mul(ModelToLight, gl_Vertex).xyz;\n"
2934 "\n"
2935 "# ifdef USEDIFFUSE\n"
2936 "       // transform unnormalized light direction into tangent space\n"
2937 "       // (we use unnormalized to ensure that it interpolates correctly and then\n"
2938 "       //  normalize it per pixel)\n"
2939 "       float3 lightminusvertex = LightPosition - gl_Vertex.xyz;\n"
2940 "       LightVector.x = dot(lightminusvertex, gl_MultiTexCoord1.xyz);\n"
2941 "       LightVector.y = dot(lightminusvertex, gl_MultiTexCoord2.xyz);\n"
2942 "       LightVector.z = dot(lightminusvertex, gl_MultiTexCoord3.xyz);\n"
2943 "# endif\n"
2944 "#endif\n"
2945 "\n"
2946 "#if defined(MODE_LIGHTDIRECTION) && defined(USEDIFFUSE)\n"
2947 "       LightVector.x = dot(LightDir, gl_MultiTexCoord1.xyz);\n"
2948 "       LightVector.y = dot(LightDir, gl_MultiTexCoord2.xyz);\n"
2949 "       LightVector.z = dot(LightDir, gl_MultiTexCoord3.xyz);\n"
2950 "#endif\n"
2951 "\n"
2952 "       // transform unnormalized eye direction into tangent space\n"
2953 "#ifdef USEEYEVECTOR\n"
2954 "       float3 EyeVectorModelSpace = EyePosition - gl_Vertex.xyz;\n"
2955 "       EyeVector.x = dot(EyeVectorModelSpace, gl_MultiTexCoord1.xyz);\n"
2956 "       EyeVector.y = dot(EyeVectorModelSpace, gl_MultiTexCoord2.xyz);\n"
2957 "       EyeVector.z = dot(EyeVectorModelSpace, gl_MultiTexCoord3.xyz);\n"
2958 "#endif\n"
2959 "\n"
2960 "#ifdef USEFOG\n"
2961 "       EyeVectorModelSpaceFogPlaneVertexDist.xyz = EyePosition - gl_Vertex.xyz;\n"
2962 "       EyeVectorModelSpaceFogPlaneVertexDist.w = dot(FogPlane, gl_Vertex);\n"
2963 "#endif\n"
2964 "\n"
2965 "#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
2966 "       VectorS = gl_MultiTexCoord1.xyz;\n"
2967 "       VectorT = gl_MultiTexCoord2.xyz;\n"
2968 "       VectorR = gl_MultiTexCoord3.xyz;\n"
2969 "#endif\n"
2970 "\n"
2971 "       // transform vertex to camera space, using ftransform to match non-VS rendering\n"
2972 "       gl_Position = mul(ModelViewProjectionMatrix, gl_Vertex);\n"
2973 "\n"
2974 "#ifdef USESHADOWMAPORTHO\n"
2975 "       ShadowMapTC = mul(ShadowMapMatrix, gl_Position).xyz;\n"
2976 "#endif\n"
2977 "\n"
2978 "#ifdef USEREFLECTION\n"
2979 "       ModelViewProjectionPosition = gl_Position;\n"
2980 "#endif\n"
2981 "}\n"
2982 "#endif // VERTEX_SHADER\n"
2983 "\n"
2984 "\n"
2985 "\n"
2986 "\n"
2987 "#ifdef FRAGMENT_SHADER\n"
2988 "void main\n"
2989 "(\n"
2990 "#ifdef USEDEFERREDLIGHTMAP\n"
2991 "#ifdef HLSL\n"
2992 "float2 Pixel : VPOS,\n"
2993 "#else\n"
2994 "float2 Pixel : WPOS,\n"
2995 "#endif\n"
2996 "#endif\n"
2997 "float4 gl_FrontColor : COLOR,\n"
2998 "float4 TexCoordBoth : TEXCOORD0,\n"
2999 "#ifdef USELIGHTMAP\n"
3000 "float2 TexCoordLightmap : TEXCOORD1,\n"
3001 "#endif\n"
3002 "#ifdef USEEYEVECTOR\n"
3003 "float3 EyeVector : TEXCOORD2,\n"
3004 "#endif\n"
3005 "#ifdef USEREFLECTION\n"
3006 "float4 ModelViewProjectionPosition : TEXCOORD3,\n"
3007 "#endif\n"
3008 "#ifdef USEFOG\n"
3009 "float4 EyeVectorModelSpaceFogPlaneVertexDist : TEXCOORD4,\n"
3010 "#endif\n"
3011 "#if defined(MODE_LIGHTSOURCE) || defined(MODE_LIGHTDIRECTION)\n"
3012 "float3 LightVector : TEXCOORD1,\n"
3013 "#endif\n"
3014 "#ifdef MODE_LIGHTSOURCE\n"
3015 "float3 CubeVector : TEXCOORD3,\n"
3016 "#endif\n"
3017 "#ifdef MODE_DEFERREDLIGHTSOURCE\n"
3018 "float4 ModelViewPosition : TEXCOORD0,\n"
3019 "#endif\n"
3020 "#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_DEFERREDGEOMETRY) || defined(USEREFLECTCUBE)\n"
3021 "float3 VectorS : TEXCOORD5, // direction of S texcoord (sometimes crudely called tangent)\n"
3022 "float3 VectorT : TEXCOORD6, // direction of T texcoord (sometimes crudely called binormal)\n"
3023 "float3 VectorR : TEXCOORD7, // direction of R texcoord (surface normal)\n"
3024 "#endif\n"
3025 "#ifdef USESHADOWMAPORTHO\n"
3026 "float3 ShadowMapTC : TEXCOORD3, // CONFLICTS WITH USEREFLECTION!\n"
3027 "#endif\n"
3028 "\n"
3029 "uniform sampler Texture_Normal : register(s0),\n"
3030 "uniform sampler Texture_Color : register(s1),\n"
3031 "#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP)\n"
3032 "uniform sampler Texture_Gloss : register(s2),\n"
3033 "#endif\n"
3034 "#ifdef USEGLOW\n"
3035 "uniform sampler Texture_Glow : register(s3),\n"
3036 "#endif\n"
3037 "#ifdef USEVERTEXTEXTUREBLEND\n"
3038 "uniform sampler Texture_SecondaryNormal : register(s4),\n"
3039 "uniform sampler Texture_SecondaryColor : register(s5),\n"
3040 "#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP)\n"
3041 "uniform sampler Texture_SecondaryGloss : register(s6),\n"
3042 "#endif\n"
3043 "#ifdef USEGLOW\n"
3044 "uniform sampler Texture_SecondaryGlow : register(s7),\n"
3045 "#endif\n"
3046 "#endif\n"
3047 "#ifdef USECOLORMAPPING\n"
3048 "uniform sampler Texture_Pants : register(s4),\n"
3049 "uniform sampler Texture_Shirt : register(s7),\n"
3050 "#endif\n"
3051 "#ifdef USEFOG\n"
3052 "uniform sampler Texture_FogHeightTexture : register(s14),\n"
3053 "uniform sampler Texture_FogMask : register(s8),\n"
3054 "#endif\n"
3055 "#ifdef USELIGHTMAP\n"
3056 "uniform sampler Texture_Lightmap : register(s9),\n"
3057 "#endif\n"
3058 "#if defined(MODE_LIGHTDIRECTIONMAP_MODELSPACE) || defined(MODE_LIGHTDIRECTIONMAP_TANGENTSPACE)\n"
3059 "uniform sampler Texture_Deluxemap : register(s10),\n"
3060 "#endif\n"
3061 "#ifdef USEREFLECTION\n"
3062 "uniform sampler Texture_Reflection : register(s7),\n"
3063 "#endif\n"
3064 "\n"
3065 "#ifdef MODE_DEFERREDLIGHTSOURCE\n"
3066 "uniform sampler Texture_ScreenDepth : register(s13),\n"
3067 "uniform sampler Texture_ScreenNormalMap : register(s14),\n"
3068 "#endif\n"
3069 "#ifdef USEDEFERREDLIGHTMAP\n"
3070 "uniform sampler Texture_ScreenDepth : register(s13),\n"
3071 "uniform sampler Texture_ScreenNormalMap : register(s14),\n"
3072 "uniform sampler Texture_ScreenDiffuse : register(s11),\n"
3073 "uniform sampler Texture_ScreenSpecular : register(s12),\n"
3074 "#endif\n"
3075 "\n"
3076 "#ifdef USECOLORMAPPING\n"
3077 "uniform half3 Color_Pants : register(c7),\n"
3078 "uniform half3 Color_Shirt : register(c8),\n"
3079 "#endif\n"
3080 "#ifdef USEFOG\n"
3081 "uniform float3 FogColor : register(c16),\n"
3082 "uniform float FogRangeRecip : register(c20),\n"
3083 "uniform float FogPlaneViewDist : register(c19),\n"
3084 "uniform float FogHeightFade : register(c17),\n"
3085 "#endif\n"
3086 "\n"
3087 "#ifdef USEOFFSETMAPPING\n"
3088 "uniform float OffsetMapping_Scale : register(c24),\n"
3089 "#endif\n"
3090 "\n"
3091 "#ifdef USEDEFERREDLIGHTMAP\n"
3092 "uniform half2 PixelToScreenTexCoord : register(c42),\n"
3093 "uniform half3 DeferredMod_Diffuse : register(c12),\n"
3094 "uniform half3 DeferredMod_Specular : register(c13),\n"
3095 "#endif\n"
3096 "uniform half3 Color_Ambient : register(c3),\n"
3097 "uniform half3 Color_Diffuse : register(c4),\n"
3098 "uniform half3 Color_Specular : register(c5),\n"
3099 "uniform half SpecularPower : register(c36),\n"
3100 "#ifdef USEGLOW\n"
3101 "uniform half3 Color_Glow : register(c6),\n"
3102 "#endif\n"
3103 "uniform half Alpha : register(c0),\n"
3104 "#ifdef USEREFLECTION\n"
3105 "uniform float4 DistortScaleRefractReflect : register(c14),\n"
3106 "uniform float4 ScreenScaleRefractReflect : register(c32),\n"
3107 "uniform float4 ScreenCenterRefractReflect : register(c31),\n"
3108 "uniform half4 ReflectColor : register(c26),\n"
3109 "#endif\n"
3110 "#ifdef USEREFLECTCUBE\n"
3111 "uniform float4x4 ModelToReflectCube : register(c48),\n"
3112 "uniform sampler Texture_ReflectMask : register(s5),\n"
3113 "uniform samplerCUBE Texture_ReflectCube : register(s6),\n"
3114 "#endif\n"
3115 "#ifdef MODE_LIGHTDIRECTION\n"
3116 "uniform half3 LightColor : register(c21),\n"
3117 "#endif\n"
3118 "#ifdef MODE_LIGHTSOURCE\n"
3119 "uniform half3 LightColor : register(c21),\n"
3120 "#endif\n"
3121 "\n"
3122 "#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE)\n"
3123 "uniform sampler Texture_Attenuation : register(s9),\n"
3124 "uniform samplerCUBE Texture_Cube : register(s10),\n"
3125 "#endif\n"
3126 "\n"
3127 "#if defined(MODE_LIGHTSOURCE) || defined(MODE_DEFERREDLIGHTSOURCE) || defined(USESHADOWMAPORTHO)\n"
3128 "\n"
3129 "#ifdef USESHADOWMAP2D\n"
3130 "# ifdef USESHADOWSAMPLER\n"
3131 "uniform sampler Texture_ShadowMap2D : register(s15),\n"
3132 "# else\n"
3133 "uniform sampler Texture_ShadowMap2D : register(s15),\n"
3134 "# endif\n"
3135 "#endif\n"
3136 "\n"
3137 "#ifdef USESHADOWMAPVSDCT\n"
3138 "uniform samplerCUBE Texture_CubeProjection : register(s12),\n"
3139 "#endif\n"
3140 "\n"
3141 "#if defined(USESHADOWMAP2D)\n"
3142 "uniform float2 ShadowMap_TextureScale : register(c35),\n"
3143 "uniform float4 ShadowMap_Parameters : register(c34),\n"
3144 "#endif\n"
3145 "#endif // !defined(MODE_LIGHTSOURCE) && !defined(MODE_DEFERREDLIGHTSOURCE) && !defined(USESHADOWMAPORTHO)\n"
3146 "\n"
3147 "out float4 gl_FragColor : COLOR\n"
3148 ")\n"
3149 "{\n"
3150 "       float2 TexCoord = TexCoordBoth.xy;\n"
3151 "#ifdef USEVERTEXTEXTUREBLEND\n"
3152 "       float2 TexCoord2 = TexCoordBoth.zw;\n"
3153 "#endif\n"
3154 "#ifdef USEOFFSETMAPPING\n"
3155 "       // apply offsetmapping\n"
3156 "       float2 TexCoordOffset = OffsetMapping(TexCoord, OffsetMapping_Scale, EyeVector, Texture_Normal);\n"
3157 "#define TexCoord TexCoordOffset\n"
3158 "#endif\n"
3159 "\n"
3160 "       // combine the diffuse textures (base, pants, shirt)\n"
3161 "       half4 color = half4(tex2D(Texture_Color, TexCoord));\n"
3162 "#ifdef USEALPHAKILL\n"
3163 "       if (color.a < 0.5)\n"
3164 "               discard;\n"
3165 "#endif\n"
3166 "       color.a *= Alpha;\n"
3167 "#ifdef USECOLORMAPPING\n"
3168 "       color.rgb += half3(tex2D(Texture_Pants, TexCoord).rgb) * Color_Pants + half3(tex2D(Texture_Shirt, TexCoord).rgb) * Color_Shirt;\n"
3169 "#endif\n"
3170 "#ifdef USEVERTEXTEXTUREBLEND\n"
3171 "       half terrainblend = clamp(half(gl_FrontColor.a) * color.a * 2.0 - 0.5, half(0.0), half(1.0));\n"
3172 "       //half terrainblend = min(half(gl_FrontColor.a) * color.a * 2.0, half(1.0));\n"
3173 "       //half terrainblend = half(gl_FrontColor.a) * color.a > 0.5;\n"
3174 "       color.rgb = half3(lerp(tex2D(Texture_SecondaryColor, TexCoord2).rgb, float3(color.rgb), terrainblend));\n"
3175 "       color.a = 1.0;\n"
3176 "       //color = half4(lerp(float4(1, 0, 0, 1), color, terrainblend));\n"
3177 "#endif\n"
3178 "\n"
3179 "       // get the surface normal\n"
3180 "#ifdef USEVERTEXTEXTUREBLEND\n"
3181 "       half3 surfacenormal = normalize(half3(lerp(tex2D(Texture_SecondaryNormal, TexCoord2).rgb, tex2D(Texture_Normal, TexCoord).rgb, terrainblend)) - half3(0.5, 0.5, 0.5));\n"
3182 "#else\n"
3183 "       half3 surfacenormal = half3(normalize(half3(tex2D(Texture_Normal, TexCoord).rgb) - half3(0.5, 0.5, 0.5)));\n"
3184 "#endif\n"
3185 "\n"
3186 "       // get the material colors\n"
3187 "       half3 diffusetex = color.rgb;\n"
3188 "#if defined(USESPECULAR) || defined(USEDEFERREDLIGHTMAP)\n"
3189 "# ifdef USEVERTEXTEXTUREBLEND\n"
3190 "       half4 glosstex = half4(lerp(tex2D(Texture_SecondaryGloss, TexCoord2), tex2D(Texture_Gloss, TexCoord), terrainblend));\n"
3191 "# else\n"
3192 "       half4 glosstex = half4(tex2D(Texture_Gloss, TexCoord));\n"
3193 "# endif\n"
3194 "#endif\n"
3195 "\n"
3196 "#ifdef USEREFLECTCUBE\n"
3197 "       float3 TangentReflectVector = reflect(-EyeVector, surfacenormal);\n"
3198 "       float3 ModelReflectVector = TangentReflectVector.x * VectorS + TangentReflectVector.y * VectorT + TangentReflectVector.z * VectorR;\n"
3199 "       float3 ReflectCubeTexCoord = mul(ModelToReflectCube, float4(ModelReflectVector, 0)).xyz;\n"
3200 "       diffusetex += half3(tex2D(Texture_ReflectMask, TexCoord).rgb) * half3(texCUBE(Texture_ReflectCube, ReflectCubeTexCoord).rgb);\n"
3201 "#endif\n"
3202 "\n"
3203 "\n"
3204 "\n"
3205 "\n"
3206 "#ifdef MODE_LIGHTSOURCE\n"
3207 "       // light source\n"
3208 "#ifdef USEDIFFUSE\n"
3209 "       half3 lightnormal = half3(normalize(LightVector));\n"
3210 "       half diffuse = half(max(float(dot(surfacenormal, lightnormal)), 0.0));\n"
3211 "       color.rgb = diffusetex * (Color_Ambient + diffuse * Color_Diffuse);\n"
3212 "#ifdef USESPECULAR\n"
3213 "#ifdef USEEXACTSPECULARMATH\n"
3214 "       half specular = half(pow(half(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower * glosstex.a));\n"
3215 "#else\n"
3216 "       half3 specularnormal = half3(normalize(lightnormal + half3(normalize(EyeVector))));\n"
3217 "       half specular = half(pow(half(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower * glosstex.a));\n"
3218 "#endif\n"
3219 "       color.rgb += glosstex.rgb * (specular * Color_Specular);\n"
3220 "#endif\n"
3221 "#else\n"
3222 "       color.rgb = diffusetex * Color_Ambient;\n"
3223 "#endif\n"
3224 "       color.rgb *= LightColor;\n"
3225 "       color.rgb *= half(tex2D(Texture_Attenuation, float2(length(CubeVector), 0.0)).r);\n"
3226 "#if defined(USESHADOWMAP2D)\n"
3227 "       color.rgb *= half(ShadowMapCompare(CubeVector, Texture_ShadowMap2D, ShadowMap_Parameters, ShadowMap_TextureScale\n"
3228 "#ifdef USESHADOWMAPVSDCT\n"
3229 ", Texture_CubeProjection\n"
3230 "#endif\n"
3231 "       ));\n"
3232 "\n"
3233 "#endif\n"
3234 "# ifdef USECUBEFILTER\n"
3235 "       color.rgb *= half3(texCUBE(Texture_Cube, CubeVector).rgb);\n"
3236 "# endif\n"
3237 "\n"
3238 "#ifdef USESHADOWMAP2D\n"
3239 "#ifdef USESHADOWMAPVSDCT\n"
3240 "//     float3 shadowmaptc = GetShadowMapTC2D(CubeVector, ShadowMap_Parameters, Texture_CubeProjection);\n"
3241 "#else\n"
3242 "//     float3 shadowmaptc = GetShadowMapTC2D(CubeVector, ShadowMap_Parameters);\n"
3243 "#endif\n"
3244 "//     color.rgb = half3(tex2D(Texture_ShadowMap2D, float2(0.1,0.1)).rgb);\n"
3245 "//     color.rgb = half3(tex2D(Texture_ShadowMap2D, shadowmaptc.xy * ShadowMap_TextureScale).rgb);\n"
3246 "//     color.rgb = half3(shadowmaptc.xyz * float3(ShadowMap_TextureScale,1.0));\n"
3247 "//     color.r = half(texDepth2D(Texture_ShadowMap2D, shadowmaptc.xy * ShadowMap_TextureScale));\n"
3248 "//     color.rgb = half3(tex2D(Texture_ShadowMap2D, float2(0.1,0.1)).rgb);\n"
3249 "//     color.rgb = half3(tex2D(Texture_ShadowMap2D, shadowmaptc.xy * ShadowMap_TextureScale).rgb);\n"
3250 "//     color.rgb = half3(shadowmaptc.xyz * float3(ShadowMap_TextureScale,1.0));\n"
3251 "//     color.r = half(texDepth2D(Texture_ShadowMap2D, shadowmaptc.xy * ShadowMap_TextureScale));\n"
3252 "//     color.r = half(shadowmaptc.z - texDepth2D(Texture_ShadowMap2D, shadowmaptc.xy * ShadowMap_TextureScale));\n"
3253 "//     color.r = half(shadowmaptc.z);\n"
3254 "//     color.r = half(texDepth2D(Texture_ShadowMap2D, shadowmaptc.xy * ShadowMap_TextureScale));\n"
3255 "//     color.r = half(shadowmaptc.z);\n"
3256 "//     color.r = 1;\n"
3257 "//     color.rgb = abs(CubeVector);\n"
3258 "#endif\n"
3259 "//     color.rgb = half3(1,1,1);\n"
3260 "#endif // MODE_LIGHTSOURCE\n"
3261 "\n"
3262 "\n"
3263 "\n"
3264 "\n"
3265 "#ifdef MODE_LIGHTDIRECTION\n"
3266 "#define SHADING\n"
3267 "#ifdef USEDIFFUSE\n"
3268 "       half3 lightnormal = half3(normalize(LightVector));\n"
3269 "#endif\n"
3270 "#define lightcolor LightColor\n"
3271 "#endif // MODE_LIGHTDIRECTION\n"
3272 "#ifdef MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
3273 "#define SHADING\n"
3274 "       // deluxemap lightmapping using light vectors in modelspace (q3map2 -light -deluxe)\n"
3275 "       half3 lightnormal_modelspace = half3(tex2D(Texture_Deluxemap, TexCoordLightmap).rgb) * 2.0 + half3(-1.0, -1.0, -1.0);\n"
3276 "       half3 lightcolor = half3(tex2D(Texture_Lightmap, TexCoordLightmap).rgb);\n"
3277 "       // convert modelspace light vector to tangentspace\n"
3278 "       half3 lightnormal;\n"
3279 "       lightnormal.x = dot(lightnormal_modelspace, half3(VectorS));\n"
3280 "       lightnormal.y = dot(lightnormal_modelspace, half3(VectorT));\n"
3281 "       lightnormal.z = dot(lightnormal_modelspace, half3(VectorR));\n"
3282 "       // calculate directional shading (and undoing the existing angle attenuation on the lightmap by the division)\n"
3283 "       // note that q3map2 is too stupid to calculate proper surface normals when q3map_nonplanar\n"
3284 "       // is used (the lightmap and deluxemap coords correspond to virtually random coordinates\n"
3285 "       // on that luxel, and NOT to its center, because recursive triangle subdivision is used\n"
3286 "       // to map the luxels to coordinates on the draw surfaces), which also causes\n"
3287 "       // deluxemaps to be wrong because light contributions from the wrong side of the surface\n"
3288 "       // are added up. To prevent divisions by zero or strong exaggerations, a max()\n"
3289 "       // nudge is done here at expense of some additional fps. This is ONLY needed for\n"
3290 "       // deluxemaps, tangentspace deluxemap avoid this problem by design.\n"
3291 "       lightcolor *= 1.0 / max(0.25, lightnormal.z);\n"
3292 "#endif // MODE_LIGHTDIRECTIONMAP_MODELSPACE\n"
3293 "#ifdef MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n"
3294 "#define SHADING\n"
3295 "       // deluxemap lightmapping using light vectors in tangentspace (hmap2 -light)\n"
3296 "       half3 lightnormal = half3(tex2D(Texture_Deluxemap, TexCoordLightmap).rgb) * 2.0 + half3(-1.0, -1.0, -1.0);\n"
3297 "       half3 lightcolor = half3(tex2D(Texture_Lightmap, TexCoordLightmap).rgb);\n"
3298 "#endif\n"
3299 "\n"
3300 "\n"
3301 "\n"
3302 "\n"
3303 "#ifdef MODE_FAKELIGHT\n"
3304 "#define SHADING\n"
3305 "half3 lightnormal = half3(normalize(EyeVector));\n"
3306 "half3 lightcolor = half3(1.0,1.0,1.0);\n"
3307 "#endif // MODE_FAKELIGHT\n"
3308 "\n"
3309 "\n"
3310 "\n"
3311 "\n"
3312 "#ifdef MODE_LIGHTMAP\n"
3313 "       color.rgb = diffusetex * (Color_Ambient + half3(tex2D(Texture_Lightmap, TexCoordLightmap).rgb) * Color_Diffuse);\n"
3314 "#endif // MODE_LIGHTMAP\n"
3315 "#ifdef MODE_VERTEXCOLOR\n"
3316 "       color.rgb = diffusetex * (Color_Ambient + half3(gl_FrontColor.rgb) * Color_Diffuse);\n"
3317 "#endif // MODE_VERTEXCOLOR\n"
3318 "#ifdef MODE_FLATCOLOR\n"
3319 "       color.rgb = diffusetex * Color_Ambient;\n"
3320 "#endif // MODE_FLATCOLOR\n"
3321 "\n"
3322 "\n"
3323 "\n"
3324 "\n"
3325 "#ifdef SHADING\n"
3326 "# ifdef USEDIFFUSE\n"
3327 "       half diffuse = half(max(float(dot(surfacenormal, lightnormal)), 0.0));\n"
3328 "#  ifdef USESPECULAR\n"
3329 "#   ifdef USEEXACTSPECULARMATH\n"
3330 "       half specular = half(pow(half(max(float(dot(reflect(lightnormal, surfacenormal), normalize(EyeVector)))*-1.0, 0.0)), SpecularPower * glosstex.a));\n"
3331 "#   else\n"
3332 "       half3 specularnormal = half3(normalize(lightnormal + half3(normalize(EyeVector))));\n"
3333 "       half specular = half(pow(half(max(float(dot(surfacenormal, specularnormal)), 0.0)), SpecularPower * glosstex.a));\n"
3334 "#   endif\n"
3335 "       color.rgb = diffusetex * Color_Ambient + (diffusetex * Color_Diffuse * diffuse + glosstex.rgb * Color_Specular * specular) * lightcolor;\n"
3336 "#  else\n"
3337 "       color.rgb = diffusetex * (Color_Ambient + Color_Diffuse * diffuse * lightcolor);\n"
3338 "#  endif\n"
3339 "# else\n"
3340 "       color.rgb = diffusetex * Color_Ambient;\n"
3341 "# endif\n"
3342 "#endif\n"
3343 "\n"
3344 "#ifdef USESHADOWMAPORTHO\n"
3345 "       color.rgb *= half(ShadowMapCompare(ShadowMapTC, Texture_ShadowMap2D, ShadowMap_Parameters, ShadowMap_TextureScale));\n"
3346 "#endif\n"
3347 "\n"
3348 "#ifdef USEDEFERREDLIGHTMAP\n"
3349 "       float2 ScreenTexCoord = Pixel * PixelToScreenTexCoord;\n"
3350 "       color.rgb += diffusetex * half3(tex2D(Texture_ScreenDiffuse, ScreenTexCoord).rgb) * DeferredMod_Diffuse;\n"
3351 "       color.rgb += glosstex.rgb * half3(tex2D(Texture_ScreenSpecular, ScreenTexCoord).rgb) * DeferredMod_Specular;\n"
3352 "//     color.rgb = half3(tex2D(Texture_ScreenDepth, ScreenTexCoord).rgb);\n"
3353 "//     color.r = half(texDepth2D(Texture_ScreenDepth, ScreenTexCoord)) * 1.0;\n"
3354 "#endif\n"
3355 "\n"
3356 "#ifdef USEGLOW\n"
3357 "#ifdef USEVERTEXTEXTUREBLEND\n"
3358 "       color.rgb += half3(lerp(tex2D(Texture_SecondaryGlow, TexCoord2).rgb, tex2D(Texture_Glow, TexCoord).rgb, terrainblend)) * Color_Glow;\n"
3359 "#else\n"
3360 "       color.rgb += half3(tex2D(Texture_Glow, TexCoord).rgb) * Color_Glow;\n"
3361 "#endif\n"
3362 "#endif\n"
3363 "\n"
3364 "#ifdef USEFOG\n"
3365 "       color.rgb = FogVertex(color.rgb, FogColor, EyeVectorModelSpaceFogPlaneVertexDist.xyz, EyeVectorModelSpaceFogPlaneVertexDist.w, FogRangeRecip, FogPlaneViewDist, FogHeightFade, Texture_FogMask, Texture_FogHeightTexture);\n"
3366 "#endif\n"
3367 "\n"
3368 "       // 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"
3369 "#ifdef USEREFLECTION\n"
3370 "       float4 ScreenScaleRefractReflectIW = ScreenScaleRefractReflect * (1.0 / ModelViewProjectionPosition.w);\n"
3371 "       //float4 ScreenTexCoord = (ModelViewProjectionPosition.xyxy + normalize(half3(tex2D(Texture_Normal, TexCoord).rgb) - half3(0.5,0.5,0.5)).xyxy * DistortScaleRefractReflect * 100) * ScreenScaleRefractReflectIW + ScreenCenterRefractReflect;\n"
3372 "       float2 SafeScreenTexCoord = ModelViewProjectionPosition.xy * ScreenScaleRefractReflectIW.zw + ScreenCenterRefractReflect.zw;\n"
3373 "       float2 ScreenTexCoord = SafeScreenTexCoord + float3(normalize(half3(tex2D(Texture_Normal, TexCoord).rgb) - half3(0.5,0.5,0.5))).xy * DistortScaleRefractReflect.zw;\n"
3374 "       // FIXME temporary hack to detect the case that the reflection\n"
3375 "       // gets blackened at edges due to leaving the area that contains actual\n"
3376 "       // content.\n"
3377 "       // Remove this 'ack once we have a better way to stop this thing from\n"
3378 "       // 'appening.\n"
3379 "       float f = min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord + float2(0.01, 0.01)).rgb) / 0.05);\n"
3380 "       f      *= min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord + float2(0.01, -0.01)).rgb) / 0.05);\n"
3381 "       f      *= min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord + float2(-0.01, 0.01)).rgb) / 0.05);\n"
3382 "       f      *= min(1.0, length(tex2D(Texture_Reflection, ScreenTexCoord + float2(-0.01, -0.01)).rgb) / 0.05);\n"
3383 "       ScreenTexCoord = lerp(SafeScreenTexCoord, ScreenTexCoord, f);\n"
3384 "       color.rgb = lerp(color.rgb, half3(tex2D(Texture_Reflection, ScreenTexCoord).rgb) * ReflectColor.rgb, ReflectColor.a);\n"
3385 "#endif\n"
3386 "\n"
3387 "       gl_FragColor = float4(color);\n"
3388 "}\n"
3389 "#endif // FRAGMENT_SHADER\n"
3390 "\n"
3391 "#endif // !MODE_DEFERREDLIGHTSOURCE\n"
3392 "#endif // !MODE_DEFERREDGEOMETRY\n"
3393 "#endif // !MODE_WATER\n"
3394 "#endif // !MODE_REFRACTION\n"
3395 "#endif // !MODE_BLOOMBLUR\n"
3396 "#endif // !MODE_GENERIC\n"
3397 "#endif // !MODE_POSTPROCESS\n"
3398 "#endif // !MODE_SHOWDEPTH\n"
3399 "#endif // !MODE_DEPTH_OR_SHADOW\n"
3400 ;
3401
3402 char *glslshaderstring = NULL;
3403 char *hlslshaderstring = NULL;
3404
3405 //=======================================================================================================================================================
3406
3407 typedef struct shaderpermutationinfo_s
3408 {
3409         const char *pretext;
3410         const char *name;
3411 }
3412 shaderpermutationinfo_t;
3413
3414 typedef struct shadermodeinfo_s
3415 {
3416         const char *vertexfilename;
3417         const char *geometryfilename;
3418         const char *fragmentfilename;
3419         const char *pretext;
3420         const char *name;
3421 }
3422 shadermodeinfo_t;
3423
3424 // NOTE: MUST MATCH ORDER OF SHADERPERMUTATION_* DEFINES!
3425 shaderpermutationinfo_t shaderpermutationinfo[SHADERPERMUTATION_COUNT] =
3426 {
3427         {"#define USEDIFFUSE\n", " diffuse"},
3428         {"#define USEVERTEXTEXTUREBLEND\n", " vertextextureblend"},
3429         {"#define USEVIEWTINT\n", " viewtint"},
3430         {"#define USECOLORMAPPING\n", " colormapping"},
3431         {"#define USESATURATION\n", " saturation"},
3432         {"#define USEFOGINSIDE\n", " foginside"},
3433         {"#define USEFOGOUTSIDE\n", " fogoutside"},
3434         {"#define USEFOGHEIGHTTEXTURE\n", " fogheighttexture"},
3435         {"#define USEGAMMARAMPS\n", " gammaramps"},
3436         {"#define USECUBEFILTER\n", " cubefilter"},
3437         {"#define USEGLOW\n", " glow"},
3438         {"#define USEBLOOM\n", " bloom"},
3439         {"#define USESPECULAR\n", " specular"},
3440         {"#define USEPOSTPROCESSING\n", " postprocessing"},
3441         {"#define USEREFLECTION\n", " reflection"},
3442         {"#define USEOFFSETMAPPING\n", " offsetmapping"},
3443         {"#define USEOFFSETMAPPING_RELIEFMAPPING\n", " reliefmapping"},
3444         {"#define USESHADOWMAP2D\n", " shadowmap2d"},
3445         {"#define USESHADOWMAPPCF 1\n", " shadowmappcf"},
3446         {"#define USESHADOWMAPPCF 2\n", " shadowmappcf2"},
3447         {"#define USESHADOWSAMPLER\n", " shadowsampler"},
3448         {"#define USESHADOWMAPVSDCT\n", " shadowmapvsdct"},
3449         {"#define USESHADOWMAPORTHO\n", " shadowmaportho"},
3450         {"#define USEDEFERREDLIGHTMAP\n", " deferredlightmap"},
3451         {"#define USEALPHAKILL\n", " alphakill"},
3452         {"#define USEREFLECTCUBE\n", " reflectcube"},
3453         {"#define USENORMALMAPSCROLLBLEND\n", " normalmapscrollblend"},
3454         {"#define USEBOUNCEGRID\n", " bouncegrid"},
3455 };
3456
3457 // NOTE: MUST MATCH ORDER OF SHADERMODE_* ENUMS!
3458 shadermodeinfo_t glslshadermodeinfo[SHADERMODE_COUNT] =
3459 {
3460         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_GENERIC\n", " generic"},
3461         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_POSTPROCESS\n", " postprocess"},
3462         {"glsl/default.glsl", NULL, NULL               , "#define MODE_DEPTH_OR_SHADOW\n", " depth/shadow"},
3463         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_FLATCOLOR\n", " flatcolor"},
3464         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_VERTEXCOLOR\n", " vertexcolor"},
3465         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTMAP\n", " lightmap"},
3466         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_FAKELIGHT\n", " fakelight"},
3467         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTDIRECTIONMAP_MODELSPACE\n", " lightdirectionmap_modelspace"},
3468         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n", " lightdirectionmap_tangentspace"},
3469         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTDIRECTION\n", " lightdirection"},
3470         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_LIGHTSOURCE\n", " lightsource"},
3471         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_REFRACTION\n", " refraction"},
3472         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_WATER\n", " water"},
3473         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_SHOWDEPTH\n", " showdepth"},
3474         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_DEFERREDGEOMETRY\n", " deferredgeometry"},
3475         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_DEFERREDLIGHTSOURCE\n", " deferredlightsource"},
3476         {"glsl/default.glsl", NULL, "glsl/default.glsl", "#define MODE_DEFERREDBOUNCELIGHT\n", " deferredbouncelight"},
3477 };
3478
3479 shadermodeinfo_t hlslshadermodeinfo[SHADERMODE_COUNT] =
3480 {
3481         {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_GENERIC\n", " generic"},
3482         {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_POSTPROCESS\n", " postprocess"},
3483         {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_DEPTH_OR_SHADOW\n", " depth/shadow"},
3484         {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_FLATCOLOR\n", " flatcolor"},
3485         {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_VERTEXCOLOR\n", " vertexcolor"},
3486         {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_LIGHTMAP\n", " lightmap"},
3487         {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_FAKELIGHT\n", " fakelight"},
3488         {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_LIGHTDIRECTIONMAP_MODELSPACE\n", " lightdirectionmap_modelspace"},
3489         {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_LIGHTDIRECTIONMAP_TANGENTSPACE\n", " lightdirectionmap_tangentspace"},
3490         {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_LIGHTDIRECTION\n", " lightdirection"},
3491         {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_LIGHTSOURCE\n", " lightsource"},
3492         {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_REFRACTION\n", " refraction"},
3493         {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_WATER\n", " water"},
3494         {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_SHOWDEPTH\n", " showdepth"},
3495         {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_DEFERREDGEOMETRY\n", " deferredgeometry"},
3496         {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_DEFERREDLIGHTSOURCE\n", " deferredlightsource"},
3497         {"hlsl/default.hlsl", NULL, "hlsl/default.hlsl", "#define MODE_DEFERREDBOUNCELIGHT\n", " deferredbouncelight"},
3498 };
3499
3500 struct r_glsl_permutation_s;
3501 typedef struct r_glsl_permutation_s
3502 {
3503         /// hash lookup data
3504         struct r_glsl_permutation_s *hashnext;
3505         unsigned int mode;
3506         unsigned int permutation;
3507
3508         /// indicates if we have tried compiling this permutation already
3509         qboolean compiled;
3510         /// 0 if compilation failed
3511         int program;
3512         // texture units assigned to each detected uniform
3513         int tex_Texture_First;
3514         int tex_Texture_Second;
3515         int tex_Texture_GammaRamps;
3516         int tex_Texture_Normal;
3517         int tex_Texture_Color;
3518         int tex_Texture_Gloss;
3519         int tex_Texture_Glow;
3520         int tex_Texture_SecondaryNormal;
3521         int tex_Texture_SecondaryColor;
3522         int tex_Texture_SecondaryGloss;
3523         int tex_Texture_SecondaryGlow;
3524         int tex_Texture_Pants;
3525         int tex_Texture_Shirt;
3526         int tex_Texture_FogHeightTexture;
3527         int tex_Texture_FogMask;
3528         int tex_Texture_Lightmap;
3529         int tex_Texture_Deluxemap;
3530         int tex_Texture_Attenuation;
3531         int tex_Texture_Cube;
3532         int tex_Texture_Refraction;
3533         int tex_Texture_Reflection;
3534         int tex_Texture_ShadowMap2D;
3535         int tex_Texture_CubeProjection;
3536         int tex_Texture_ScreenDepth;
3537         int tex_Texture_ScreenNormalMap;
3538         int tex_Texture_ScreenDiffuse;
3539         int tex_Texture_ScreenSpecular;
3540         int tex_Texture_ReflectMask;
3541         int tex_Texture_ReflectCube;
3542         int tex_Texture_BounceGrid;
3543         /// locations of detected uniforms in program object, or -1 if not found
3544         int loc_Texture_First;
3545         int loc_Texture_Second;
3546         int loc_Texture_GammaRamps;
3547         int loc_Texture_Normal;
3548         int loc_Texture_Color;
3549         int loc_Texture_Gloss;
3550         int loc_Texture_Glow;
3551         int loc_Texture_SecondaryNormal;
3552         int loc_Texture_SecondaryColor;
3553         int loc_Texture_SecondaryGloss;
3554         int loc_Texture_SecondaryGlow;
3555         int loc_Texture_Pants;
3556         int loc_Texture_Shirt;
3557         int loc_Texture_FogHeightTexture;
3558         int loc_Texture_FogMask;
3559         int loc_Texture_Lightmap;
3560         int loc_Texture_Deluxemap;
3561         int loc_Texture_Attenuation;
3562         int loc_Texture_Cube;
3563         int loc_Texture_Refraction;
3564         int loc_Texture_Reflection;
3565         int loc_Texture_ShadowMap2D;
3566         int loc_Texture_CubeProjection;
3567         int loc_Texture_ScreenDepth;
3568         int loc_Texture_ScreenNormalMap;
3569         int loc_Texture_ScreenDiffuse;
3570         int loc_Texture_ScreenSpecular;
3571         int loc_Texture_ReflectMask;
3572         int loc_Texture_ReflectCube;
3573         int loc_Texture_BounceGrid;
3574         int loc_Alpha;
3575         int loc_BloomBlur_Parameters;
3576         int loc_ClientTime;
3577         int loc_Color_Ambient;
3578         int loc_Color_Diffuse;
3579         int loc_Color_Specular;
3580         int loc_Color_Glow;
3581         int loc_Color_Pants;
3582         int loc_Color_Shirt;
3583         int loc_DeferredColor_Ambient;
3584         int loc_DeferredColor_Diffuse;
3585         int loc_DeferredColor_Specular;
3586         int loc_DeferredMod_Diffuse;
3587         int loc_DeferredMod_Specular;
3588         int loc_DistortScaleRefractReflect;
3589         int loc_EyePosition;
3590         int loc_FogColor;
3591         int loc_FogHeightFade;
3592         int loc_FogPlane;
3593         int loc_FogPlaneViewDist;
3594         int loc_FogRangeRecip;
3595         int loc_LightColor;
3596         int loc_LightDir;
3597         int loc_LightPosition;
3598         int loc_OffsetMapping_Scale;
3599         int loc_PixelSize;
3600         int loc_ReflectColor;
3601         int loc_ReflectFactor;
3602         int loc_ReflectOffset;
3603         int loc_RefractColor;
3604         int loc_Saturation;
3605         int loc_ScreenCenterRefractReflect;
3606         int loc_ScreenScaleRefractReflect;
3607         int loc_ScreenToDepth;
3608         int loc_ShadowMap_Parameters;
3609         int loc_ShadowMap_TextureScale;
3610         int loc_SpecularPower;
3611         int loc_UserVec1;
3612         int loc_UserVec2;
3613         int loc_UserVec3;
3614         int loc_UserVec4;
3615         int loc_ViewTintColor;
3616         int loc_ViewToLight;
3617         int loc_ModelToLight;
3618         int loc_TexMatrix;
3619         int loc_BackgroundTexMatrix;
3620         int loc_ModelViewProjectionMatrix;
3621         int loc_ModelViewMatrix;
3622         int loc_PixelToScreenTexCoord;
3623         int loc_ModelToReflectCube;
3624         int loc_ShadowMapMatrix;
3625         int loc_BloomColorSubtract;
3626         int loc_NormalmapScrollBlend;
3627         int loc_BounceGridMatrix;
3628         int loc_BounceGridIntensity;
3629 }
3630 r_glsl_permutation_t;
3631
3632 #define SHADERPERMUTATION_HASHSIZE 256
3633
3634
3635 // non-degradable "lightweight" shader parameters to keep the permutations simpler
3636 // these can NOT degrade! only use for simple stuff
3637 enum
3638 {
3639         SHADERSTATICPARM_SATURATION_REDCOMPENSATE = 0, ///< red compensation filter for saturation
3640         SHADERSTATICPARM_EXACTSPECULARMATH = 1, ///< (lightsource or deluxemapping) use exact reflection map for specular effects, as opposed to the usual OpenGL approximation
3641         SHADERSTATICPARM_POSTPROCESS_USERVEC1 = 2, ///< postprocess uservec1 is enabled
3642         SHADERSTATICPARM_POSTPROCESS_USERVEC2 = 3, ///< postprocess uservec2 is enabled
3643         SHADERSTATICPARM_POSTPROCESS_USERVEC3 = 4, ///< postprocess uservec3 is enabled
3644         SHADERSTATICPARM_POSTPROCESS_USERVEC4 = 5  ///< postprocess uservec4 is enabled
3645 };
3646 #define SHADERSTATICPARMS_COUNT 6
3647
3648 static const char *shaderstaticparmstrings_list[SHADERSTATICPARMS_COUNT];
3649 static int shaderstaticparms_count = 0;
3650
3651 static unsigned int r_compileshader_staticparms[(SHADERSTATICPARMS_COUNT + 0x1F) >> 5] = {0};
3652 #define R_COMPILESHADER_STATICPARM_ENABLE(p) r_compileshader_staticparms[(p) >> 5] |= (1 << ((p) & 0x1F))
3653 qboolean R_CompileShader_CheckStaticParms(void)
3654 {
3655         static int r_compileshader_staticparms_save[1];
3656         memcpy(r_compileshader_staticparms_save, r_compileshader_staticparms, sizeof(r_compileshader_staticparms));
3657         memset(r_compileshader_staticparms, 0, sizeof(r_compileshader_staticparms));
3658
3659         // detect all
3660         if (r_glsl_saturation_redcompensate.integer)
3661                 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_SATURATION_REDCOMPENSATE);
3662         if (r_shadow_glossexact.integer)
3663                 R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_EXACTSPECULARMATH);
3664         if (r_glsl_postprocess.integer)
3665         {
3666                 if (r_glsl_postprocess_uservec1_enable.integer)
3667                         R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_POSTPROCESS_USERVEC1);
3668                 if (r_glsl_postprocess_uservec2_enable.integer)
3669                         R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_POSTPROCESS_USERVEC2);
3670                 if (r_glsl_postprocess_uservec3_enable.integer)
3671                         R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_POSTPROCESS_USERVEC3);
3672                 if (r_glsl_postprocess_uservec4_enable.integer)
3673                         R_COMPILESHADER_STATICPARM_ENABLE(SHADERSTATICPARM_POSTPROCESS_USERVEC4);
3674         }
3675         return memcmp(r_compileshader_staticparms, r_compileshader_staticparms_save, sizeof(r_compileshader_staticparms)) != 0;
3676 }
3677
3678 #define R_COMPILESHADER_STATICPARM_EMIT(p, n) \
3679         if(r_compileshader_staticparms[(p) >> 5] & (1 << ((p) & 0x1F))) \
3680                 shaderstaticparmstrings_list[shaderstaticparms_count++] = "#define " n "\n"; \
3681         else \
3682                 shaderstaticparmstrings_list[shaderstaticparms_count++] = "\n"
3683 void R_CompileShader_AddStaticParms(unsigned int mode, unsigned int permutation)
3684 {
3685         shaderstaticparms_count = 0;
3686
3687         // emit all
3688         R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_SATURATION_REDCOMPENSATE, "SATURATION_REDCOMPENSATE");
3689         R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_EXACTSPECULARMATH, "USEEXACTSPECULARMATH");
3690         R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_POSTPROCESS_USERVEC1, "USERVEC1");
3691         R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_POSTPROCESS_USERVEC2, "USERVEC2");
3692         R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_POSTPROCESS_USERVEC3, "USERVEC3");
3693         R_COMPILESHADER_STATICPARM_EMIT(SHADERSTATICPARM_POSTPROCESS_USERVEC4, "USERVEC4");
3694 }
3695
3696 /// information about each possible shader permutation
3697 r_glsl_permutation_t *r_glsl_permutationhash[SHADERMODE_COUNT][SHADERPERMUTATION_HASHSIZE];
3698 /// currently selected permutation
3699 r_glsl_permutation_t *r_glsl_permutation;
3700 /// storage for permutations linked in the hash table
3701 memexpandablearray_t r_glsl_permutationarray;
3702
3703 static r_glsl_permutation_t *R_GLSL_FindPermutation(unsigned int mode, unsigned int permutation)
3704 {
3705         //unsigned int hashdepth = 0;
3706         unsigned int hashindex = (permutation * 0x1021) & (SHADERPERMUTATION_HASHSIZE - 1);
3707         r_glsl_permutation_t *p;
3708         for (p = r_glsl_permutationhash[mode][hashindex];p;p = p->hashnext)
3709         {
3710                 if (p->mode == mode && p->permutation == permutation)
3711                 {
3712                         //if (hashdepth > 10)
3713                         //      Con_Printf("R_GLSL_FindPermutation: Warning: %i:%i has hashdepth %i\n", mode, permutation, hashdepth);
3714                         return p;
3715                 }
3716                 //hashdepth++;
3717         }
3718         p = (r_glsl_permutation_t*)Mem_ExpandableArray_AllocRecord(&r_glsl_permutationarray);
3719         p->mode = mode;
3720         p->permutation = permutation;
3721         p->hashnext = r_glsl_permutationhash[mode][hashindex];
3722         r_glsl_permutationhash[mode][hashindex] = p;
3723         //if (hashdepth > 10)
3724         //      Con_Printf("R_GLSL_FindPermutation: Warning: %i:%i has hashdepth %i\n", mode, permutation, hashdepth);
3725         return p;
3726 }
3727
3728 static char *R_GLSL_GetText(const char *filename, qboolean printfromdisknotice)
3729 {
3730         char *shaderstring;
3731         if (!filename || !filename[0])
3732                 return NULL;
3733         if (!strcmp(filename, "glsl/default.glsl"))
3734         {
3735                 if (!glslshaderstring)
3736                 {
3737                         glslshaderstring = (char *)FS_LoadFile(filename, r_main_mempool, false, NULL);
3738                         if (glslshaderstring)
3739                                 Con_DPrintf("Loading shaders from file %s...\n", filename);
3740                         else
3741                                 glslshaderstring = (char *)builtinshaderstring;
3742                 }
3743                 shaderstring = (char *) Mem_Alloc(r_main_mempool, strlen(glslshaderstring) + 1);
3744                 memcpy(shaderstring, glslshaderstring, strlen(glslshaderstring) + 1);
3745                 return shaderstring;
3746         }
3747         shaderstring = (char *)FS_LoadFile(filename, r_main_mempool, false, NULL);
3748         if (shaderstring)
3749         {
3750                 if (printfromdisknotice)
3751                         Con_DPrintf("from disk %s... ", filename);
3752                 return shaderstring;
3753         }
3754         return shaderstring;
3755 }
3756
3757 static void R_GLSL_CompilePermutation(r_glsl_permutation_t *p, unsigned int mode, unsigned int permutation)
3758 {
3759         int i;
3760         int sampler;
3761         shadermodeinfo_t *modeinfo = glslshadermodeinfo + mode;
3762         char *vertexstring, *geometrystring, *fragmentstring;
3763         char permutationname[256];
3764         int vertstrings_count = 0;
3765         int geomstrings_count = 0;
3766         int fragstrings_count = 0;
3767         const char *vertstrings_list[32+3+SHADERSTATICPARMS_COUNT+1];
3768         const char *geomstrings_list[32+3+SHADERSTATICPARMS_COUNT+1];
3769         const char *fragstrings_list[32+3+SHADERSTATICPARMS_COUNT+1];
3770
3771         if (p->compiled)
3772                 return;
3773         p->compiled = true;
3774         p->program = 0;
3775
3776         permutationname[0] = 0;
3777         vertexstring   = R_GLSL_GetText(modeinfo->vertexfilename, true);
3778         geometrystring = R_GLSL_GetText(modeinfo->geometryfilename, false);
3779         fragmentstring = R_GLSL_GetText(modeinfo->fragmentfilename, false);
3780
3781         strlcat(permutationname, modeinfo->vertexfilename, sizeof(permutationname));
3782
3783         // the first pretext is which type of shader to compile as
3784         // (later these will all be bound together as a program object)
3785         vertstrings_list[vertstrings_count++] = "#define VERTEX_SHADER\n";
3786         geomstrings_list[geomstrings_count++] = "#define GEOMETRY_SHADER\n";
3787         fragstrings_list[fragstrings_count++] = "#define FRAGMENT_SHADER\n";
3788
3789         // the second pretext is the mode (for example a light source)
3790         vertstrings_list[vertstrings_count++] = modeinfo->pretext;
3791         geomstrings_list[geomstrings_count++] = modeinfo->pretext;
3792         fragstrings_list[fragstrings_count++] = modeinfo->pretext;
3793         strlcat(permutationname, modeinfo->name, sizeof(permutationname));
3794
3795         // now add all the permutation pretexts
3796         for (i = 0;i < SHADERPERMUTATION_COUNT;i++)
3797         {
3798                 if (permutation & (1<<i))
3799                 {
3800                         vertstrings_list[vertstrings_count++] = shaderpermutationinfo[i].pretext;
3801                         geomstrings_list[geomstrings_count++] = shaderpermutationinfo[i].pretext;
3802                         fragstrings_list[fragstrings_count++] = shaderpermutationinfo[i].pretext;
3803                         strlcat(permutationname, shaderpermutationinfo[i].name, sizeof(permutationname));
3804                 }
3805                 else
3806                 {
3807                         // keep line numbers correct
3808                         vertstrings_list[vertstrings_count++] = "\n";
3809                         geomstrings_list[geomstrings_count++] = "\n";
3810                         fragstrings_list[fragstrings_count++] = "\n";
3811                 }
3812         }
3813
3814         // add static parms
3815         R_CompileShader_AddStaticParms(mode, permutation);
3816         memcpy((char *)(vertstrings_list + vertstrings_count), shaderstaticparmstrings_list, sizeof(*vertstrings_list) * shaderstaticparms_count);
3817         vertstrings_count += shaderstaticparms_count;
3818         memcpy((char *)(geomstrings_list + geomstrings_count), shaderstaticparmstrings_list, sizeof(*vertstrings_list) * shaderstaticparms_count);
3819         geomstrings_count += shaderstaticparms_count;
3820         memcpy((char *)(fragstrings_list + fragstrings_count), shaderstaticparmstrings_list, sizeof(*vertstrings_list) * shaderstaticparms_count);
3821         fragstrings_count += shaderstaticparms_count;
3822
3823         // now append the shader text itself
3824         vertstrings_list[vertstrings_count++] = vertexstring;
3825         geomstrings_list[geomstrings_count++] = geometrystring;
3826         fragstrings_list[fragstrings_count++] = fragmentstring;
3827
3828         // if any sources were NULL, clear the respective list
3829         if (!vertexstring)
3830                 vertstrings_count = 0;
3831         if (!geometrystring)
3832                 geomstrings_count = 0;
3833         if (!fragmentstring)
3834                 fragstrings_count = 0;
3835
3836         // compile the shader program
3837         if (vertstrings_count + geomstrings_count + fragstrings_count)
3838                 p->program = GL_Backend_CompileProgram(vertstrings_count, vertstrings_list, geomstrings_count, geomstrings_list, fragstrings_count, fragstrings_list);
3839         if (p->program)
3840         {
3841                 CHECKGLERROR
3842                 qglUseProgram(p->program);CHECKGLERROR
3843                 // look up all the uniform variable names we care about, so we don't
3844                 // have to look them up every time we set them
3845
3846                 p->loc_Texture_First              = qglGetUniformLocation(p->program, "Texture_First");
3847                 p->loc_Texture_Second             = qglGetUniformLocation(p->program, "Texture_Second");
3848                 p->loc_Texture_GammaRamps         = qglGetUniformLocation(p->program, "Texture_GammaRamps");
3849                 p->loc_Texture_Normal             = qglGetUniformLocation(p->program, "Texture_Normal");
3850                 p->loc_Texture_Color              = qglGetUniformLocation(p->program, "Texture_Color");
3851                 p->loc_Texture_Gloss              = qglGetUniformLocation(p->program, "Texture_Gloss");
3852                 p->loc_Texture_Glow               = qglGetUniformLocation(p->program, "Texture_Glow");
3853                 p->loc_Texture_SecondaryNormal    = qglGetUniformLocation(p->program, "Texture_SecondaryNormal");
3854                 p->loc_Texture_SecondaryColor     = qglGetUniformLocation(p->program, "Texture_SecondaryColor");
3855                 p->loc_Texture_SecondaryGloss     = qglGetUniformLocation(p->program, "Texture_SecondaryGloss");
3856                 p->loc_Texture_SecondaryGlow      = qglGetUniformLocation(p->program, "Texture_SecondaryGlow");
3857                 p->loc_Texture_Pants              = qglGetUniformLocation(p->program, "Texture_Pants");
3858                 p->loc_Texture_Shirt              = qglGetUniformLocation(p->program, "Texture_Shirt");
3859                 p->loc_Texture_FogHeightTexture   = qglGetUniformLocation(p->program, "Texture_FogHeightTexture");
3860                 p->loc_Texture_FogMask            = qglGetUniformLocation(p->program, "Texture_FogMask");
3861                 p->loc_Texture_Lightmap           = qglGetUniformLocation(p->program, "Texture_Lightmap");
3862                 p->loc_Texture_Deluxemap          = qglGetUniformLocation(p->program, "Texture_Deluxemap");
3863                 p->loc_Texture_Attenuation        = qglGetUniformLocation(p->program, "Texture_Attenuation");
3864                 p->loc_Texture_Cube               = qglGetUniformLocation(p->program, "Texture_Cube");
3865                 p->loc_Texture_Refraction         = qglGetUniformLocation(p->program, "Texture_Refraction");
3866                 p->loc_Texture_Reflection         = qglGetUniformLocation(p->program, "Texture_Reflection");
3867                 p->loc_Texture_ShadowMap2D        = qglGetUniformLocation(p->program, "Texture_ShadowMap2D");
3868                 p->loc_Texture_CubeProjection     = qglGetUniformLocation(p->program, "Texture_CubeProjection");
3869                 p->loc_Texture_ScreenDepth        = qglGetUniformLocation(p->program, "Texture_ScreenDepth");
3870                 p->loc_Texture_ScreenNormalMap    = qglGetUniformLocation(p->program, "Texture_ScreenNormalMap");
3871                 p->loc_Texture_ScreenDiffuse      = qglGetUniformLocation(p->program, "Texture_ScreenDiffuse");
3872                 p->loc_Texture_ScreenSpecular     = qglGetUniformLocation(p->program, "Texture_ScreenSpecular");
3873                 p->loc_Texture_ReflectMask        = qglGetUniformLocation(p->program, "Texture_ReflectMask");
3874                 p->loc_Texture_ReflectCube        = qglGetUniformLocation(p->program, "Texture_ReflectCube");
3875                 p->loc_Texture_BounceGrid         = qglGetUniformLocation(p->program, "Texture_BounceGrid");
3876                 p->loc_Alpha                      = qglGetUniformLocation(p->program, "Alpha");
3877                 p->loc_BloomBlur_Parameters       = qglGetUniformLocation(p->program, "BloomBlur_Parameters");
3878                 p->loc_ClientTime                 = qglGetUniformLocation(p->program, "ClientTime");
3879                 p->loc_Color_Ambient              = qglGetUniformLocation(p->program, "Color_Ambient");
3880                 p->loc_Color_Diffuse              = qglGetUniformLocation(p->program, "Color_Diffuse");
3881                 p->loc_Color_Specular             = qglGetUniformLocation(p->program, "Color_Specular");
3882                 p->loc_Color_Glow                 = qglGetUniformLocation(p->program, "Color_Glow");
3883                 p->loc_Color_Pants                = qglGetUniformLocation(p->program, "Color_Pants");
3884                 p->loc_Color_Shirt                = qglGetUniformLocation(p->program, "Color_Shirt");
3885                 p->loc_DeferredColor_Ambient      = qglGetUniformLocation(p->program, "DeferredColor_Ambient");
3886                 p->loc_DeferredColor_Diffuse      = qglGetUniformLocation(p->program, "DeferredColor_Diffuse");
3887                 p->loc_DeferredColor_Specular     = qglGetUniformLocation(p->program, "DeferredColor_Specular");
3888                 p->loc_DeferredMod_Diffuse        = qglGetUniformLocation(p->program, "DeferredMod_Diffuse");
3889                 p->loc_DeferredMod_Specular       = qglGetUniformLocation(p->program, "DeferredMod_Specular");
3890                 p->loc_DistortScaleRefractReflect = qglGetUniformLocation(p->program, "DistortScaleRefractReflect");
3891                 p->loc_EyePosition                = qglGetUniformLocation(p->program, "EyePosition");
3892                 p->loc_FogColor                   = qglGetUniformLocation(p->program, "FogColor");
3893                 p->loc_FogHeightFade              = qglGetUniformLocation(p->program, "FogHeightFade");
3894                 p->loc_FogPlane                   = qglGetUniformLocation(p->program, "FogPlane");
3895                 p->loc_FogPlaneViewDist           = qglGetUniformLocation(p->program, "FogPlaneViewDist");
3896                 p->loc_FogRangeRecip              = qglGetUniformLocation(p->program, "FogRangeRecip");
3897                 p->loc_LightColor                 = qglGetUniformLocation(p->program, "LightColor");
3898                 p->loc_LightDir                   = qglGetUniformLocation(p->program, "LightDir");
3899                 p->loc_LightPosition              = qglGetUniformLocation(p->program, "LightPosition");
3900                 p->loc_OffsetMapping_Scale        = qglGetUniformLocation(p->program, "OffsetMapping_Scale");
3901                 p->loc_PixelSize                  = qglGetUniformLocation(p->program, "PixelSize");
3902                 p->loc_ReflectColor               = qglGetUniformLocation(p->program, "ReflectColor");
3903                 p->loc_ReflectFactor              = qglGetUniformLocation(p->program, "ReflectFactor");
3904                 p->loc_ReflectOffset              = qglGetUniformLocation(p->program, "ReflectOffset");
3905                 p->loc_RefractColor               = qglGetUniformLocation(p->program, "RefractColor");
3906                 p->loc_Saturation                 = qglGetUniformLocation(p->program, "Saturation");
3907                 p->loc_ScreenCenterRefractReflect = qglGetUniformLocation(p->program, "ScreenCenterRefractReflect");
3908                 p->loc_ScreenScaleRefractReflect  = qglGetUniformLocation(p->program, "ScreenScaleRefractReflect");
3909                 p->loc_ScreenToDepth              = qglGetUniformLocation(p->program, "ScreenToDepth");
3910                 p->loc_ShadowMap_Parameters       = qglGetUniformLocation(p->program, "ShadowMap_Parameters");
3911                 p->loc_ShadowMap_TextureScale     = qglGetUniformLocation(p->program, "ShadowMap_TextureScale");
3912                 p->loc_SpecularPower              = qglGetUniformLocation(p->program, "SpecularPower");
3913                 p->loc_UserVec1                   = qglGetUniformLocation(p->program, "UserVec1");
3914                 p->loc_UserVec2                   = qglGetUniformLocation(p->program, "UserVec2");
3915                 p->loc_UserVec3                   = qglGetUniformLocation(p->program, "UserVec3");
3916                 p->loc_UserVec4                   = qglGetUniformLocation(p->program, "UserVec4");
3917                 p->loc_ViewTintColor              = qglGetUniformLocation(p->program, "ViewTintColor");
3918                 p->loc_ViewToLight                = qglGetUniformLocation(p->program, "ViewToLight");
3919                 p->loc_ModelToLight               = qglGetUniformLocation(p->program, "ModelToLight");
3920                 p->loc_TexMatrix                  = qglGetUniformLocation(p->program, "TexMatrix");
3921                 p->loc_BackgroundTexMatrix        = qglGetUniformLocation(p->program, "BackgroundTexMatrix");
3922                 p->loc_ModelViewMatrix            = qglGetUniformLocation(p->program, "ModelViewMatrix");
3923                 p->loc_ModelViewProjectionMatrix  = qglGetUniformLocation(p->program, "ModelViewProjectionMatrix");
3924                 p->loc_PixelToScreenTexCoord      = qglGetUniformLocation(p->program, "PixelToScreenTexCoord");
3925                 p->loc_ModelToReflectCube         = qglGetUniformLocation(p->program, "ModelToReflectCube");
3926                 p->loc_ShadowMapMatrix            = qglGetUniformLocation(p->program, "ShadowMapMatrix");
3927                 p->loc_BloomColorSubtract         = qglGetUniformLocation(p->program, "BloomColorSubtract");
3928                 p->loc_NormalmapScrollBlend       = qglGetUniformLocation(p->program, "NormalmapScrollBlend");
3929                 p->loc_BounceGridMatrix           = qglGetUniformLocation(p->program, "BounceGridMatrix");
3930                 p->loc_BounceGridIntensity        = qglGetUniformLocation(p->program, "BounceGridIntensity");
3931                 // initialize the samplers to refer to the texture units we use
3932                 p->tex_Texture_First = -1;
3933                 p->tex_Texture_Second = -1;
3934                 p->tex_Texture_GammaRamps = -1;
3935                 p->tex_Texture_Normal = -1;
3936                 p->tex_Texture_Color = -1;
3937                 p->tex_Texture_Gloss = -1;
3938                 p->tex_Texture_Glow = -1;
3939                 p->tex_Texture_SecondaryNormal = -1;
3940                 p->tex_Texture_SecondaryColor = -1;
3941                 p->tex_Texture_SecondaryGloss = -1;
3942                 p->tex_Texture_SecondaryGlow = -1;
3943                 p->tex_Texture_Pants = -1;
3944                 p->tex_Texture_Shirt = -1;
3945                 p->tex_Texture_FogHeightTexture = -1;
3946                 p->tex_Texture_FogMask = -1;
3947                 p->tex_Texture_Lightmap = -1;
3948                 p->tex_Texture_Deluxemap = -1;
3949                 p->tex_Texture_Attenuation = -1;
3950                 p->tex_Texture_Cube = -1;
3951                 p->tex_Texture_Refraction = -1;
3952                 p->tex_Texture_Reflection = -1;
3953                 p->tex_Texture_ShadowMap2D = -1;
3954                 p->tex_Texture_CubeProjection = -1;
3955                 p->tex_Texture_ScreenDepth = -1;
3956                 p->tex_Texture_ScreenNormalMap = -1;
3957                 p->tex_Texture_ScreenDiffuse = -1;
3958                 p->tex_Texture_ScreenSpecular = -1;
3959                 p->tex_Texture_ReflectMask = -1;
3960                 p->tex_Texture_ReflectCube = -1;
3961                 p->tex_Texture_BounceGrid = -1;
3962                 sampler = 0;
3963                 if (p->loc_Texture_First           >= 0) {p->tex_Texture_First            = sampler;qglUniform1i(p->loc_Texture_First           , sampler);sampler++;}
3964                 if (p->loc_Texture_Second          >= 0) {p->tex_Texture_Second           = sampler;qglUniform1i(p->loc_Texture_Second          , sampler);sampler++;}
3965                 if (p->loc_Texture_GammaRamps      >= 0) {p->tex_Texture_GammaRamps       = sampler;qglUniform1i(p->loc_Texture_GammaRamps      , sampler);sampler++;}
3966                 if (p->loc_Texture_Normal          >= 0) {p->tex_Texture_Normal           = sampler;qglUniform1i(p->loc_Texture_Normal          , sampler);sampler++;}
3967                 if (p->loc_Texture_Color           >= 0) {p->tex_Texture_Color            = sampler;qglUniform1i(p->loc_Texture_Color           , sampler);sampler++;}
3968                 if (p->loc_Texture_Gloss           >= 0) {p->tex_Texture_Gloss            = sampler;qglUniform1i(p->loc_Texture_Gloss           , sampler);sampler++;}
3969                 if (p->loc_Texture_Glow            >= 0) {p->tex_Texture_Glow             = sampler;qglUniform1i(p->loc_Texture_Glow            , sampler);sampler++;}
3970                 if (p->loc_Texture_SecondaryNormal >= 0) {p->tex_Texture_SecondaryNormal  = sampler;qglUniform1i(p->loc_Texture_SecondaryNormal , sampler);sampler++;}
3971                 if (p->loc_Texture_SecondaryColor  >= 0) {p->tex_Texture_SecondaryColor   = sampler;qglUniform1i(p->loc_Texture_SecondaryColor  , sampler);sampler++;}
3972                 if (p->loc_Texture_SecondaryGloss  >= 0) {p->tex_Texture_SecondaryGloss   = sampler;qglUniform1i(p->loc_Texture_SecondaryGloss  , sampler);sampler++;}
3973                 if (p->loc_Texture_SecondaryGlow   >= 0) {p->tex_Texture_SecondaryGlow    = sampler;qglUniform1i(p->loc_Texture_SecondaryGlow   , sampler);sampler++;}
3974                 if (p->loc_Texture_Pants           >= 0) {p->tex_Texture_Pants            = sampler;qglUniform1i(p->loc_Texture_Pants           , sampler);sampler++;}
3975                 if (p->loc_Texture_Shirt           >= 0) {p->tex_Texture_Shirt            = sampler;qglUniform1i(p->loc_Texture_Shirt           , sampler);sampler++;}
3976                 if (p->loc_Texture_FogHeightTexture>= 0) {p->tex_Texture_FogHeightTexture = sampler;qglUniform1i(p->loc_Texture_FogHeightTexture, sampler);sampler++;}
3977                 if (p->loc_Texture_FogMask         >= 0) {p->tex_Texture_FogMask          = sampler;qglUniform1i(p->loc_Texture_FogMask         , sampler);sampler++;}
3978                 if (p->loc_Texture_Lightmap        >= 0) {p->tex_Texture_Lightmap         = sampler;qglUniform1i(p->loc_Texture_Lightmap        , sampler);sampler++;}
3979                 if (p->loc_Texture_Deluxemap       >= 0) {p->tex_Texture_Deluxemap        = sampler;qglUniform1i(p->loc_Texture_Deluxemap       , sampler);sampler++;}
3980                 if (p->loc_Texture_Attenuation     >= 0) {p->tex_Texture_Attenuation      = sampler;qglUniform1i(p->loc_Texture_Attenuation     , sampler);sampler++;}
3981                 if (p->loc_Texture_Cube            >= 0) {p->tex_Texture_Cube             = sampler;qglUniform1i(p->loc_Texture_Cube            , sampler);sampler++;}
3982                 if (p->loc_Texture_Refraction      >= 0) {p->tex_Texture_Refraction       = sampler;qglUniform1i(p->loc_Texture_Refraction      , sampler);sampler++;}
3983                 if (p->loc_Texture_Reflection      >= 0) {p->tex_Texture_Reflection       = sampler;qglUniform1i(p->loc_Texture_Reflection      , sampler);sampler++;}
3984                 if (p->loc_Texture_ShadowMap2D     >= 0) {p->tex_Texture_ShadowMap2D      = sampler;qglUniform1i(p->loc_Texture_ShadowMap2D     , sampler);sampler++;}
3985                 if (p->loc_Texture_CubeProjection  >= 0) {p->tex_Texture_CubeProjection   = sampler;qglUniform1i(p->loc_Texture_CubeProjection  , sampler);sampler++;}
3986                 if (p->loc_Texture_ScreenDepth     >= 0) {p->tex_Texture_ScreenDepth      = sampler;qglUniform1i(p->loc_Texture_ScreenDepth     , sampler);sampler++;}
3987                 if (p->loc_Texture_ScreenNormalMap >= 0) {p->tex_Texture_ScreenNormalMap  = sampler;qglUniform1i(p->loc_Texture_ScreenNormalMap , sampler);sampler++;}