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