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