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